bmap.c 14 KB

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