bmap.c 14 KB

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