bmap.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /*
  2. * bmap.c - NILFS block mapping.
  3. *
  4. * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * Written by Koji Sato.
  17. */
  18. #include <linux/fs.h>
  19. #include <linux/string.h>
  20. #include <linux/errno.h>
  21. #include "nilfs.h"
  22. #include "bmap.h"
  23. #include "btree.h"
  24. #include "direct.h"
  25. #include "btnode.h"
  26. #include "mdt.h"
  27. #include "dat.h"
  28. #include "alloc.h"
  29. struct inode *nilfs_bmap_get_dat(const struct nilfs_bmap *bmap)
  30. {
  31. struct the_nilfs *nilfs = bmap->b_inode->i_sb->s_fs_info;
  32. return nilfs->ns_dat;
  33. }
  34. static int nilfs_bmap_convert_error(struct nilfs_bmap *bmap,
  35. const char *fname, int err)
  36. {
  37. struct inode *inode = bmap->b_inode;
  38. if (err == -EINVAL) {
  39. __nilfs_error(inode->i_sb, fname,
  40. "broken bmap (inode number=%lu)", inode->i_ino);
  41. err = -EIO;
  42. }
  43. return err;
  44. }
  45. /**
  46. * nilfs_bmap_lookup_at_level - find a data block or node block
  47. * @bmap: bmap
  48. * @key: key
  49. * @level: level
  50. * @ptrp: place to store the value associated to @key
  51. *
  52. * Description: nilfs_bmap_lookup_at_level() finds a record whose key
  53. * matches @key in the block at @level of the bmap.
  54. *
  55. * Return Value: On success, 0 is returned and the record associated with @key
  56. * is stored in the place pointed by @ptrp. On error, one of the following
  57. * negative error codes is returned.
  58. *
  59. * %-EIO - I/O error.
  60. *
  61. * %-ENOMEM - Insufficient amount of memory available.
  62. *
  63. * %-ENOENT - A record associated with @key does not exist.
  64. */
  65. int nilfs_bmap_lookup_at_level(struct nilfs_bmap *bmap, __u64 key, int level,
  66. __u64 *ptrp)
  67. {
  68. sector_t blocknr;
  69. int ret;
  70. down_read(&bmap->b_sem);
  71. ret = bmap->b_ops->bop_lookup(bmap, key, level, ptrp);
  72. if (ret < 0) {
  73. ret = nilfs_bmap_convert_error(bmap, __func__, ret);
  74. goto out;
  75. }
  76. if (NILFS_BMAP_USE_VBN(bmap)) {
  77. ret = nilfs_dat_translate(nilfs_bmap_get_dat(bmap), *ptrp,
  78. &blocknr);
  79. if (!ret)
  80. *ptrp = blocknr;
  81. }
  82. out:
  83. up_read(&bmap->b_sem);
  84. return ret;
  85. }
  86. int nilfs_bmap_lookup_contig(struct nilfs_bmap *bmap, __u64 key, __u64 *ptrp,
  87. unsigned int maxblocks)
  88. {
  89. int ret;
  90. down_read(&bmap->b_sem);
  91. ret = bmap->b_ops->bop_lookup_contig(bmap, key, ptrp, maxblocks);
  92. up_read(&bmap->b_sem);
  93. return nilfs_bmap_convert_error(bmap, __func__, ret);
  94. }
  95. static int nilfs_bmap_do_insert(struct nilfs_bmap *bmap, __u64 key, __u64 ptr)
  96. {
  97. __u64 keys[NILFS_BMAP_SMALL_HIGH + 1];
  98. __u64 ptrs[NILFS_BMAP_SMALL_HIGH + 1];
  99. int ret, n;
  100. if (bmap->b_ops->bop_check_insert != NULL) {
  101. ret = bmap->b_ops->bop_check_insert(bmap, key);
  102. if (ret > 0) {
  103. n = bmap->b_ops->bop_gather_data(
  104. bmap, keys, ptrs, NILFS_BMAP_SMALL_HIGH + 1);
  105. if (n < 0)
  106. return n;
  107. ret = nilfs_btree_convert_and_insert(
  108. bmap, key, ptr, keys, ptrs, n);
  109. if (ret == 0)
  110. bmap->b_u.u_flags |= NILFS_BMAP_LARGE;
  111. return ret;
  112. } else if (ret < 0)
  113. return ret;
  114. }
  115. return bmap->b_ops->bop_insert(bmap, key, ptr);
  116. }
  117. /**
  118. * nilfs_bmap_insert - insert a new key-record pair into a bmap
  119. * @bmap: bmap
  120. * @key: key
  121. * @rec: record
  122. *
  123. * Description: nilfs_bmap_insert() inserts the new key-record pair specified
  124. * by @key and @rec into @bmap.
  125. *
  126. * Return Value: On success, 0 is returned. On error, one of the following
  127. * negative error codes is returned.
  128. *
  129. * %-EIO - I/O error.
  130. *
  131. * %-ENOMEM - Insufficient amount of memory available.
  132. *
  133. * %-EEXIST - A record associated with @key already exist.
  134. */
  135. int nilfs_bmap_insert(struct nilfs_bmap *bmap, __u64 key, unsigned long rec)
  136. {
  137. int ret;
  138. down_write(&bmap->b_sem);
  139. ret = nilfs_bmap_do_insert(bmap, key, rec);
  140. up_write(&bmap->b_sem);
  141. return nilfs_bmap_convert_error(bmap, __func__, ret);
  142. }
  143. static int nilfs_bmap_do_delete(struct nilfs_bmap *bmap, __u64 key)
  144. {
  145. __u64 keys[NILFS_BMAP_LARGE_LOW + 1];
  146. __u64 ptrs[NILFS_BMAP_LARGE_LOW + 1];
  147. int ret, n;
  148. if (bmap->b_ops->bop_check_delete != NULL) {
  149. ret = bmap->b_ops->bop_check_delete(bmap, key);
  150. if (ret > 0) {
  151. n = bmap->b_ops->bop_gather_data(
  152. bmap, keys, ptrs, NILFS_BMAP_LARGE_LOW + 1);
  153. if (n < 0)
  154. return n;
  155. ret = nilfs_direct_delete_and_convert(
  156. bmap, key, keys, ptrs, n);
  157. if (ret == 0)
  158. bmap->b_u.u_flags &= ~NILFS_BMAP_LARGE;
  159. return ret;
  160. } else if (ret < 0)
  161. return ret;
  162. }
  163. return bmap->b_ops->bop_delete(bmap, key);
  164. }
  165. /**
  166. * nilfs_bmap_seek_key - seek a valid entry and return its key
  167. * @bmap: bmap struct
  168. * @start: start key number
  169. * @keyp: place to store valid key
  170. *
  171. * Description: nilfs_bmap_seek_key() seeks a valid key on @bmap
  172. * starting from @start, and stores it to @keyp if found.
  173. *
  174. * Return Value: On success, 0 is returned. On error, one of the following
  175. * negative error codes is returned.
  176. *
  177. * %-EIO - I/O error.
  178. *
  179. * %-ENOMEM - Insufficient amount of memory available.
  180. *
  181. * %-ENOENT - No valid entry was found
  182. */
  183. int nilfs_bmap_seek_key(struct nilfs_bmap *bmap, __u64 start, __u64 *keyp)
  184. {
  185. int ret;
  186. down_read(&bmap->b_sem);
  187. ret = bmap->b_ops->bop_seek_key(bmap, start, keyp);
  188. up_read(&bmap->b_sem);
  189. if (ret < 0)
  190. ret = nilfs_bmap_convert_error(bmap, __func__, ret);
  191. return ret;
  192. }
  193. int nilfs_bmap_last_key(struct nilfs_bmap *bmap, __u64 *keyp)
  194. {
  195. int ret;
  196. down_read(&bmap->b_sem);
  197. ret = bmap->b_ops->bop_last_key(bmap, keyp);
  198. up_read(&bmap->b_sem);
  199. if (ret < 0)
  200. ret = nilfs_bmap_convert_error(bmap, __func__, ret);
  201. return ret;
  202. }
  203. /**
  204. * nilfs_bmap_delete - delete a key-record pair from a bmap
  205. * @bmap: bmap
  206. * @key: key
  207. *
  208. * Description: nilfs_bmap_delete() deletes the key-record pair specified by
  209. * @key from @bmap.
  210. *
  211. * Return Value: On success, 0 is returned. On error, one of the following
  212. * negative error codes is returned.
  213. *
  214. * %-EIO - I/O error.
  215. *
  216. * %-ENOMEM - Insufficient amount of memory available.
  217. *
  218. * %-ENOENT - A record associated with @key does not exist.
  219. */
  220. int nilfs_bmap_delete(struct nilfs_bmap *bmap, __u64 key)
  221. {
  222. int ret;
  223. down_write(&bmap->b_sem);
  224. ret = nilfs_bmap_do_delete(bmap, key);
  225. up_write(&bmap->b_sem);
  226. return nilfs_bmap_convert_error(bmap, __func__, ret);
  227. }
  228. static int nilfs_bmap_do_truncate(struct nilfs_bmap *bmap, __u64 key)
  229. {
  230. __u64 lastkey;
  231. int ret;
  232. ret = bmap->b_ops->bop_last_key(bmap, &lastkey);
  233. if (ret < 0) {
  234. if (ret == -ENOENT)
  235. ret = 0;
  236. return ret;
  237. }
  238. while (key <= lastkey) {
  239. ret = nilfs_bmap_do_delete(bmap, lastkey);
  240. if (ret < 0)
  241. return ret;
  242. ret = bmap->b_ops->bop_last_key(bmap, &lastkey);
  243. if (ret < 0) {
  244. if (ret == -ENOENT)
  245. ret = 0;
  246. return ret;
  247. }
  248. }
  249. return 0;
  250. }
  251. /**
  252. * nilfs_bmap_truncate - truncate a bmap to a specified key
  253. * @bmap: bmap
  254. * @key: key
  255. *
  256. * Description: nilfs_bmap_truncate() removes key-record pairs whose keys are
  257. * greater than or equal to @key from @bmap.
  258. *
  259. * Return Value: On success, 0 is returned. On error, one of the following
  260. * negative error codes is returned.
  261. *
  262. * %-EIO - I/O error.
  263. *
  264. * %-ENOMEM - Insufficient amount of memory available.
  265. */
  266. int nilfs_bmap_truncate(struct nilfs_bmap *bmap, __u64 key)
  267. {
  268. int ret;
  269. down_write(&bmap->b_sem);
  270. ret = nilfs_bmap_do_truncate(bmap, key);
  271. up_write(&bmap->b_sem);
  272. return nilfs_bmap_convert_error(bmap, __func__, ret);
  273. }
  274. /**
  275. * nilfs_bmap_clear - free resources a bmap holds
  276. * @bmap: bmap
  277. *
  278. * Description: nilfs_bmap_clear() frees resources associated with @bmap.
  279. */
  280. void nilfs_bmap_clear(struct nilfs_bmap *bmap)
  281. {
  282. down_write(&bmap->b_sem);
  283. if (bmap->b_ops->bop_clear != NULL)
  284. bmap->b_ops->bop_clear(bmap);
  285. up_write(&bmap->b_sem);
  286. }
  287. /**
  288. * nilfs_bmap_propagate - propagate dirty state
  289. * @bmap: bmap
  290. * @bh: buffer head
  291. *
  292. * Description: nilfs_bmap_propagate() marks the buffers that directly or
  293. * indirectly refer to the block specified by @bh dirty.
  294. *
  295. * Return Value: On success, 0 is returned. On error, one of the following
  296. * negative error codes is returned.
  297. *
  298. * %-EIO - I/O error.
  299. *
  300. * %-ENOMEM - Insufficient amount of memory available.
  301. */
  302. int nilfs_bmap_propagate(struct nilfs_bmap *bmap, struct buffer_head *bh)
  303. {
  304. int ret;
  305. down_write(&bmap->b_sem);
  306. ret = bmap->b_ops->bop_propagate(bmap, bh);
  307. up_write(&bmap->b_sem);
  308. return nilfs_bmap_convert_error(bmap, __func__, ret);
  309. }
  310. /**
  311. * nilfs_bmap_lookup_dirty_buffers -
  312. * @bmap: bmap
  313. * @listp: pointer to buffer head list
  314. */
  315. void nilfs_bmap_lookup_dirty_buffers(struct nilfs_bmap *bmap,
  316. struct list_head *listp)
  317. {
  318. if (bmap->b_ops->bop_lookup_dirty_buffers != NULL)
  319. bmap->b_ops->bop_lookup_dirty_buffers(bmap, listp);
  320. }
  321. /**
  322. * nilfs_bmap_assign - assign a new block number to a block
  323. * @bmap: bmap
  324. * @bhp: pointer to buffer head
  325. * @blocknr: block number
  326. * @binfo: block information
  327. *
  328. * Description: nilfs_bmap_assign() assigns the block number @blocknr to the
  329. * buffer specified by @bh.
  330. *
  331. * Return Value: On success, 0 is returned and the buffer head of a newly
  332. * create buffer and the block information associated with the buffer are
  333. * stored in the place pointed by @bh and @binfo, respectively. On error, one
  334. * of the following negative error codes is returned.
  335. *
  336. * %-EIO - I/O error.
  337. *
  338. * %-ENOMEM - Insufficient amount of memory available.
  339. */
  340. int nilfs_bmap_assign(struct nilfs_bmap *bmap,
  341. struct buffer_head **bh,
  342. unsigned long blocknr,
  343. union nilfs_binfo *binfo)
  344. {
  345. int ret;
  346. down_write(&bmap->b_sem);
  347. ret = bmap->b_ops->bop_assign(bmap, bh, blocknr, binfo);
  348. up_write(&bmap->b_sem);
  349. return nilfs_bmap_convert_error(bmap, __func__, ret);
  350. }
  351. /**
  352. * nilfs_bmap_mark - mark block dirty
  353. * @bmap: bmap
  354. * @key: key
  355. * @level: level
  356. *
  357. * Description: nilfs_bmap_mark() marks the block specified by @key and @level
  358. * as dirty.
  359. *
  360. * Return Value: On success, 0 is returned. On error, one of the following
  361. * negative error codes is returned.
  362. *
  363. * %-EIO - I/O error.
  364. *
  365. * %-ENOMEM - Insufficient amount of memory available.
  366. */
  367. int nilfs_bmap_mark(struct nilfs_bmap *bmap, __u64 key, int level)
  368. {
  369. int ret;
  370. if (bmap->b_ops->bop_mark == NULL)
  371. return 0;
  372. down_write(&bmap->b_sem);
  373. ret = bmap->b_ops->bop_mark(bmap, key, level);
  374. up_write(&bmap->b_sem);
  375. return nilfs_bmap_convert_error(bmap, __func__, ret);
  376. }
  377. /**
  378. * nilfs_bmap_test_and_clear_dirty - test and clear a bmap dirty state
  379. * @bmap: bmap
  380. *
  381. * Description: nilfs_test_and_clear() is the atomic operation to test and
  382. * clear the dirty state of @bmap.
  383. *
  384. * Return Value: 1 is returned if @bmap is dirty, or 0 if clear.
  385. */
  386. int nilfs_bmap_test_and_clear_dirty(struct nilfs_bmap *bmap)
  387. {
  388. int ret;
  389. down_write(&bmap->b_sem);
  390. ret = nilfs_bmap_dirty(bmap);
  391. nilfs_bmap_clear_dirty(bmap);
  392. up_write(&bmap->b_sem);
  393. return ret;
  394. }
  395. /*
  396. * Internal use only
  397. */
  398. __u64 nilfs_bmap_data_get_key(const struct nilfs_bmap *bmap,
  399. const struct buffer_head *bh)
  400. {
  401. struct buffer_head *pbh;
  402. __u64 key;
  403. key = page_index(bh->b_page) << (PAGE_SHIFT -
  404. bmap->b_inode->i_blkbits);
  405. for (pbh = page_buffers(bh->b_page); pbh != bh; pbh = pbh->b_this_page)
  406. key++;
  407. return key;
  408. }
  409. __u64 nilfs_bmap_find_target_seq(const struct nilfs_bmap *bmap, __u64 key)
  410. {
  411. __s64 diff;
  412. diff = key - bmap->b_last_allocated_key;
  413. if ((nilfs_bmap_keydiff_abs(diff) < NILFS_INODE_BMAP_SIZE) &&
  414. (bmap->b_last_allocated_ptr != NILFS_BMAP_INVALID_PTR) &&
  415. (bmap->b_last_allocated_ptr + diff > 0))
  416. return bmap->b_last_allocated_ptr + diff;
  417. else
  418. return NILFS_BMAP_INVALID_PTR;
  419. }
  420. #define NILFS_BMAP_GROUP_DIV 8
  421. __u64 nilfs_bmap_find_target_in_group(const struct nilfs_bmap *bmap)
  422. {
  423. struct inode *dat = nilfs_bmap_get_dat(bmap);
  424. unsigned long entries_per_group = nilfs_palloc_entries_per_group(dat);
  425. unsigned long group = bmap->b_inode->i_ino / entries_per_group;
  426. return group * entries_per_group +
  427. (bmap->b_inode->i_ino % NILFS_BMAP_GROUP_DIV) *
  428. (entries_per_group / NILFS_BMAP_GROUP_DIV);
  429. }
  430. static struct lock_class_key nilfs_bmap_dat_lock_key;
  431. static struct lock_class_key nilfs_bmap_mdt_lock_key;
  432. /**
  433. * nilfs_bmap_read - read a bmap from an inode
  434. * @bmap: bmap
  435. * @raw_inode: on-disk inode
  436. *
  437. * Description: nilfs_bmap_read() initializes the bmap @bmap.
  438. *
  439. * Return Value: On success, 0 is returned. On error, the following negative
  440. * error code is returned.
  441. *
  442. * %-ENOMEM - Insufficient amount of memory available.
  443. */
  444. int nilfs_bmap_read(struct nilfs_bmap *bmap, struct nilfs_inode *raw_inode)
  445. {
  446. if (raw_inode == NULL)
  447. memset(bmap->b_u.u_data, 0, NILFS_BMAP_SIZE);
  448. else
  449. memcpy(bmap->b_u.u_data, raw_inode->i_bmap, NILFS_BMAP_SIZE);
  450. init_rwsem(&bmap->b_sem);
  451. bmap->b_state = 0;
  452. bmap->b_inode = &NILFS_BMAP_I(bmap)->vfs_inode;
  453. switch (bmap->b_inode->i_ino) {
  454. case NILFS_DAT_INO:
  455. bmap->b_ptr_type = NILFS_BMAP_PTR_P;
  456. bmap->b_last_allocated_key = 0;
  457. bmap->b_last_allocated_ptr = NILFS_BMAP_NEW_PTR_INIT;
  458. lockdep_set_class(&bmap->b_sem, &nilfs_bmap_dat_lock_key);
  459. break;
  460. case NILFS_CPFILE_INO:
  461. case NILFS_SUFILE_INO:
  462. bmap->b_ptr_type = NILFS_BMAP_PTR_VS;
  463. bmap->b_last_allocated_key = 0;
  464. bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR;
  465. lockdep_set_class(&bmap->b_sem, &nilfs_bmap_mdt_lock_key);
  466. break;
  467. case NILFS_IFILE_INO:
  468. lockdep_set_class(&bmap->b_sem, &nilfs_bmap_mdt_lock_key);
  469. /* Fall through */
  470. default:
  471. bmap->b_ptr_type = NILFS_BMAP_PTR_VM;
  472. bmap->b_last_allocated_key = 0;
  473. bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR;
  474. break;
  475. }
  476. return (bmap->b_u.u_flags & NILFS_BMAP_LARGE) ?
  477. nilfs_btree_init(bmap) : nilfs_direct_init(bmap);
  478. }
  479. /**
  480. * nilfs_bmap_write - write back a bmap to an inode
  481. * @bmap: bmap
  482. * @raw_inode: on-disk inode
  483. *
  484. * Description: nilfs_bmap_write() stores @bmap in @raw_inode.
  485. */
  486. void nilfs_bmap_write(struct nilfs_bmap *bmap, struct nilfs_inode *raw_inode)
  487. {
  488. down_write(&bmap->b_sem);
  489. memcpy(raw_inode->i_bmap, bmap->b_u.u_data,
  490. NILFS_INODE_BMAP_SIZE * sizeof(__le64));
  491. if (bmap->b_inode->i_ino == NILFS_DAT_INO)
  492. bmap->b_last_allocated_ptr = NILFS_BMAP_NEW_PTR_INIT;
  493. up_write(&bmap->b_sem);
  494. }
  495. void nilfs_bmap_init_gc(struct nilfs_bmap *bmap)
  496. {
  497. memset(&bmap->b_u, 0, NILFS_BMAP_SIZE);
  498. init_rwsem(&bmap->b_sem);
  499. bmap->b_inode = &NILFS_BMAP_I(bmap)->vfs_inode;
  500. bmap->b_ptr_type = NILFS_BMAP_PTR_U;
  501. bmap->b_last_allocated_key = 0;
  502. bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR;
  503. bmap->b_state = 0;
  504. nilfs_btree_init_gc(bmap);
  505. }
  506. void nilfs_bmap_save(const struct nilfs_bmap *bmap,
  507. struct nilfs_bmap_store *store)
  508. {
  509. memcpy(store->data, bmap->b_u.u_data, sizeof(store->data));
  510. store->last_allocated_key = bmap->b_last_allocated_key;
  511. store->last_allocated_ptr = bmap->b_last_allocated_ptr;
  512. store->state = bmap->b_state;
  513. }
  514. void nilfs_bmap_restore(struct nilfs_bmap *bmap,
  515. const struct nilfs_bmap_store *store)
  516. {
  517. memcpy(bmap->b_u.u_data, store->data, sizeof(store->data));
  518. bmap->b_last_allocated_key = store->last_allocated_key;
  519. bmap->b_last_allocated_ptr = store->last_allocated_ptr;
  520. bmap->b_state = store->state;
  521. }