bmap.c 14 KB

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