bmap.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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, __u64 key, unsigned long rec)
  140. {
  141. int ret;
  142. down_write(&bmap->b_sem);
  143. ret = nilfs_bmap_do_insert(bmap, key, rec);
  144. up_write(&bmap->b_sem);
  145. return nilfs_bmap_convert_error(bmap, __func__, ret);
  146. }
  147. static int nilfs_bmap_do_delete(struct nilfs_bmap *bmap, __u64 key)
  148. {
  149. __u64 keys[NILFS_BMAP_LARGE_LOW + 1];
  150. __u64 ptrs[NILFS_BMAP_LARGE_LOW + 1];
  151. int ret, n;
  152. if (bmap->b_ops->bop_check_delete != NULL) {
  153. ret = bmap->b_ops->bop_check_delete(bmap, key);
  154. if (ret > 0) {
  155. n = bmap->b_ops->bop_gather_data(
  156. bmap, keys, ptrs, NILFS_BMAP_LARGE_LOW + 1);
  157. if (n < 0)
  158. return n;
  159. ret = nilfs_direct_delete_and_convert(
  160. bmap, key, keys, ptrs, n);
  161. if (ret == 0)
  162. bmap->b_u.u_flags &= ~NILFS_BMAP_LARGE;
  163. return ret;
  164. } else if (ret < 0)
  165. return ret;
  166. }
  167. return bmap->b_ops->bop_delete(bmap, key);
  168. }
  169. /**
  170. * nilfs_bmap_seek_key - seek a valid entry and return its key
  171. * @bmap: bmap struct
  172. * @start: start key number
  173. * @keyp: place to store valid key
  174. *
  175. * Description: nilfs_bmap_seek_key() seeks a valid key on @bmap
  176. * starting from @start, and stores it to @keyp if found.
  177. *
  178. * Return Value: On success, 0 is returned. On error, one of the following
  179. * negative error codes is returned.
  180. *
  181. * %-EIO - I/O error.
  182. *
  183. * %-ENOMEM - Insufficient amount of memory available.
  184. *
  185. * %-ENOENT - No valid entry was found
  186. */
  187. int nilfs_bmap_seek_key(struct nilfs_bmap *bmap, __u64 start, __u64 *keyp)
  188. {
  189. int ret;
  190. down_read(&bmap->b_sem);
  191. ret = bmap->b_ops->bop_seek_key(bmap, start, keyp);
  192. up_read(&bmap->b_sem);
  193. if (ret < 0)
  194. ret = nilfs_bmap_convert_error(bmap, __func__, ret);
  195. return ret;
  196. }
  197. int nilfs_bmap_last_key(struct nilfs_bmap *bmap, __u64 *keyp)
  198. {
  199. int ret;
  200. down_read(&bmap->b_sem);
  201. ret = bmap->b_ops->bop_last_key(bmap, keyp);
  202. up_read(&bmap->b_sem);
  203. if (ret < 0)
  204. ret = nilfs_bmap_convert_error(bmap, __func__, ret);
  205. return ret;
  206. }
  207. /**
  208. * nilfs_bmap_delete - delete a key-record pair from a bmap
  209. * @bmap: bmap
  210. * @key: key
  211. *
  212. * Description: nilfs_bmap_delete() deletes the key-record pair specified by
  213. * @key from @bmap.
  214. *
  215. * Return Value: On success, 0 is returned. On error, one of the following
  216. * negative error codes is returned.
  217. *
  218. * %-EIO - I/O error.
  219. *
  220. * %-ENOMEM - Insufficient amount of memory available.
  221. *
  222. * %-ENOENT - A record associated with @key does not exist.
  223. */
  224. int nilfs_bmap_delete(struct nilfs_bmap *bmap, __u64 key)
  225. {
  226. int ret;
  227. down_write(&bmap->b_sem);
  228. ret = nilfs_bmap_do_delete(bmap, key);
  229. up_write(&bmap->b_sem);
  230. return nilfs_bmap_convert_error(bmap, __func__, ret);
  231. }
  232. static int nilfs_bmap_do_truncate(struct nilfs_bmap *bmap, __u64 key)
  233. {
  234. __u64 lastkey;
  235. int ret;
  236. ret = bmap->b_ops->bop_last_key(bmap, &lastkey);
  237. if (ret < 0) {
  238. if (ret == -ENOENT)
  239. ret = 0;
  240. return ret;
  241. }
  242. while (key <= lastkey) {
  243. ret = nilfs_bmap_do_delete(bmap, lastkey);
  244. if (ret < 0)
  245. return ret;
  246. ret = bmap->b_ops->bop_last_key(bmap, &lastkey);
  247. if (ret < 0) {
  248. if (ret == -ENOENT)
  249. ret = 0;
  250. return ret;
  251. }
  252. }
  253. return 0;
  254. }
  255. /**
  256. * nilfs_bmap_truncate - truncate a bmap to a specified key
  257. * @bmap: bmap
  258. * @key: key
  259. *
  260. * Description: nilfs_bmap_truncate() removes key-record pairs whose keys are
  261. * greater than or equal to @key from @bmap.
  262. *
  263. * Return Value: On success, 0 is returned. On error, one of the following
  264. * negative error codes is returned.
  265. *
  266. * %-EIO - I/O error.
  267. *
  268. * %-ENOMEM - Insufficient amount of memory available.
  269. */
  270. int nilfs_bmap_truncate(struct nilfs_bmap *bmap, __u64 key)
  271. {
  272. int ret;
  273. down_write(&bmap->b_sem);
  274. ret = nilfs_bmap_do_truncate(bmap, key);
  275. up_write(&bmap->b_sem);
  276. return nilfs_bmap_convert_error(bmap, __func__, ret);
  277. }
  278. /**
  279. * nilfs_bmap_clear - free resources a bmap holds
  280. * @bmap: bmap
  281. *
  282. * Description: nilfs_bmap_clear() frees resources associated with @bmap.
  283. */
  284. void nilfs_bmap_clear(struct nilfs_bmap *bmap)
  285. {
  286. down_write(&bmap->b_sem);
  287. if (bmap->b_ops->bop_clear != NULL)
  288. bmap->b_ops->bop_clear(bmap);
  289. up_write(&bmap->b_sem);
  290. }
  291. /**
  292. * nilfs_bmap_propagate - propagate dirty state
  293. * @bmap: bmap
  294. * @bh: buffer head
  295. *
  296. * Description: nilfs_bmap_propagate() marks the buffers that directly or
  297. * indirectly refer to the block specified by @bh dirty.
  298. *
  299. * Return Value: On success, 0 is returned. On error, one of the following
  300. * negative error codes is returned.
  301. *
  302. * %-EIO - I/O error.
  303. *
  304. * %-ENOMEM - Insufficient amount of memory available.
  305. */
  306. int nilfs_bmap_propagate(struct nilfs_bmap *bmap, struct buffer_head *bh)
  307. {
  308. int ret;
  309. down_write(&bmap->b_sem);
  310. ret = bmap->b_ops->bop_propagate(bmap, bh);
  311. up_write(&bmap->b_sem);
  312. return nilfs_bmap_convert_error(bmap, __func__, ret);
  313. }
  314. /**
  315. * nilfs_bmap_lookup_dirty_buffers -
  316. * @bmap: bmap
  317. * @listp: pointer to buffer head list
  318. */
  319. void nilfs_bmap_lookup_dirty_buffers(struct nilfs_bmap *bmap,
  320. struct list_head *listp)
  321. {
  322. if (bmap->b_ops->bop_lookup_dirty_buffers != NULL)
  323. bmap->b_ops->bop_lookup_dirty_buffers(bmap, listp);
  324. }
  325. /**
  326. * nilfs_bmap_assign - assign a new block number to a block
  327. * @bmap: bmap
  328. * @bhp: pointer to buffer head
  329. * @blocknr: block number
  330. * @binfo: block information
  331. *
  332. * Description: nilfs_bmap_assign() assigns the block number @blocknr to the
  333. * buffer specified by @bh.
  334. *
  335. * Return Value: On success, 0 is returned and the buffer head of a newly
  336. * create buffer and the block information associated with the buffer are
  337. * stored in the place pointed by @bh and @binfo, respectively. On error, one
  338. * of the following negative error codes is returned.
  339. *
  340. * %-EIO - I/O error.
  341. *
  342. * %-ENOMEM - Insufficient amount of memory available.
  343. */
  344. int nilfs_bmap_assign(struct nilfs_bmap *bmap,
  345. struct buffer_head **bh,
  346. unsigned long blocknr,
  347. union nilfs_binfo *binfo)
  348. {
  349. int ret;
  350. down_write(&bmap->b_sem);
  351. ret = bmap->b_ops->bop_assign(bmap, bh, blocknr, binfo);
  352. up_write(&bmap->b_sem);
  353. return nilfs_bmap_convert_error(bmap, __func__, ret);
  354. }
  355. /**
  356. * nilfs_bmap_mark - mark block dirty
  357. * @bmap: bmap
  358. * @key: key
  359. * @level: level
  360. *
  361. * Description: nilfs_bmap_mark() marks the block specified by @key and @level
  362. * as dirty.
  363. *
  364. * Return Value: On success, 0 is returned. On error, one of the following
  365. * negative error codes is returned.
  366. *
  367. * %-EIO - I/O error.
  368. *
  369. * %-ENOMEM - Insufficient amount of memory available.
  370. */
  371. int nilfs_bmap_mark(struct nilfs_bmap *bmap, __u64 key, int level)
  372. {
  373. int ret;
  374. if (bmap->b_ops->bop_mark == NULL)
  375. return 0;
  376. down_write(&bmap->b_sem);
  377. ret = bmap->b_ops->bop_mark(bmap, key, level);
  378. up_write(&bmap->b_sem);
  379. return nilfs_bmap_convert_error(bmap, __func__, ret);
  380. }
  381. /**
  382. * nilfs_bmap_test_and_clear_dirty - test and clear a bmap dirty state
  383. * @bmap: bmap
  384. *
  385. * Description: nilfs_test_and_clear() is the atomic operation to test and
  386. * clear the dirty state of @bmap.
  387. *
  388. * Return Value: 1 is returned if @bmap is dirty, or 0 if clear.
  389. */
  390. int nilfs_bmap_test_and_clear_dirty(struct nilfs_bmap *bmap)
  391. {
  392. int ret;
  393. down_write(&bmap->b_sem);
  394. ret = nilfs_bmap_dirty(bmap);
  395. nilfs_bmap_clear_dirty(bmap);
  396. up_write(&bmap->b_sem);
  397. return ret;
  398. }
  399. /*
  400. * Internal use only
  401. */
  402. __u64 nilfs_bmap_data_get_key(const struct nilfs_bmap *bmap,
  403. const struct buffer_head *bh)
  404. {
  405. struct buffer_head *pbh;
  406. __u64 key;
  407. key = page_index(bh->b_page) << (PAGE_CACHE_SHIFT -
  408. bmap->b_inode->i_blkbits);
  409. for (pbh = page_buffers(bh->b_page); pbh != bh; pbh = pbh->b_this_page)
  410. key++;
  411. return key;
  412. }
  413. __u64 nilfs_bmap_find_target_seq(const struct nilfs_bmap *bmap, __u64 key)
  414. {
  415. __s64 diff;
  416. diff = key - bmap->b_last_allocated_key;
  417. if ((nilfs_bmap_keydiff_abs(diff) < NILFS_INODE_BMAP_SIZE) &&
  418. (bmap->b_last_allocated_ptr != NILFS_BMAP_INVALID_PTR) &&
  419. (bmap->b_last_allocated_ptr + diff > 0))
  420. return bmap->b_last_allocated_ptr + diff;
  421. else
  422. return NILFS_BMAP_INVALID_PTR;
  423. }
  424. #define NILFS_BMAP_GROUP_DIV 8
  425. __u64 nilfs_bmap_find_target_in_group(const struct nilfs_bmap *bmap)
  426. {
  427. struct inode *dat = nilfs_bmap_get_dat(bmap);
  428. unsigned long entries_per_group = nilfs_palloc_entries_per_group(dat);
  429. unsigned long group = bmap->b_inode->i_ino / entries_per_group;
  430. return group * entries_per_group +
  431. (bmap->b_inode->i_ino % NILFS_BMAP_GROUP_DIV) *
  432. (entries_per_group / NILFS_BMAP_GROUP_DIV);
  433. }
  434. static struct lock_class_key nilfs_bmap_dat_lock_key;
  435. static struct lock_class_key nilfs_bmap_mdt_lock_key;
  436. /**
  437. * nilfs_bmap_read - read a bmap from an inode
  438. * @bmap: bmap
  439. * @raw_inode: on-disk inode
  440. *
  441. * Description: nilfs_bmap_read() initializes the bmap @bmap.
  442. *
  443. * Return Value: On success, 0 is returned. On error, the following negative
  444. * error code is returned.
  445. *
  446. * %-ENOMEM - Insufficient amount of memory available.
  447. */
  448. int nilfs_bmap_read(struct nilfs_bmap *bmap, struct nilfs_inode *raw_inode)
  449. {
  450. if (raw_inode == NULL)
  451. memset(bmap->b_u.u_data, 0, NILFS_BMAP_SIZE);
  452. else
  453. memcpy(bmap->b_u.u_data, raw_inode->i_bmap, NILFS_BMAP_SIZE);
  454. init_rwsem(&bmap->b_sem);
  455. bmap->b_state = 0;
  456. bmap->b_inode = &NILFS_BMAP_I(bmap)->vfs_inode;
  457. switch (bmap->b_inode->i_ino) {
  458. case NILFS_DAT_INO:
  459. bmap->b_ptr_type = NILFS_BMAP_PTR_P;
  460. bmap->b_last_allocated_key = 0;
  461. bmap->b_last_allocated_ptr = NILFS_BMAP_NEW_PTR_INIT;
  462. lockdep_set_class(&bmap->b_sem, &nilfs_bmap_dat_lock_key);
  463. break;
  464. case NILFS_CPFILE_INO:
  465. case NILFS_SUFILE_INO:
  466. bmap->b_ptr_type = NILFS_BMAP_PTR_VS;
  467. bmap->b_last_allocated_key = 0;
  468. bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR;
  469. lockdep_set_class(&bmap->b_sem, &nilfs_bmap_mdt_lock_key);
  470. break;
  471. case NILFS_IFILE_INO:
  472. lockdep_set_class(&bmap->b_sem, &nilfs_bmap_mdt_lock_key);
  473. /* Fall through */
  474. default:
  475. bmap->b_ptr_type = NILFS_BMAP_PTR_VM;
  476. bmap->b_last_allocated_key = 0;
  477. bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR;
  478. break;
  479. }
  480. return (bmap->b_u.u_flags & NILFS_BMAP_LARGE) ?
  481. nilfs_btree_init(bmap) : nilfs_direct_init(bmap);
  482. }
  483. /**
  484. * nilfs_bmap_write - write back a bmap to an inode
  485. * @bmap: bmap
  486. * @raw_inode: on-disk inode
  487. *
  488. * Description: nilfs_bmap_write() stores @bmap in @raw_inode.
  489. */
  490. void nilfs_bmap_write(struct nilfs_bmap *bmap, struct nilfs_inode *raw_inode)
  491. {
  492. down_write(&bmap->b_sem);
  493. memcpy(raw_inode->i_bmap, bmap->b_u.u_data,
  494. NILFS_INODE_BMAP_SIZE * sizeof(__le64));
  495. if (bmap->b_inode->i_ino == NILFS_DAT_INO)
  496. bmap->b_last_allocated_ptr = NILFS_BMAP_NEW_PTR_INIT;
  497. up_write(&bmap->b_sem);
  498. }
  499. void nilfs_bmap_init_gc(struct nilfs_bmap *bmap)
  500. {
  501. memset(&bmap->b_u, 0, NILFS_BMAP_SIZE);
  502. init_rwsem(&bmap->b_sem);
  503. bmap->b_inode = &NILFS_BMAP_I(bmap)->vfs_inode;
  504. bmap->b_ptr_type = NILFS_BMAP_PTR_U;
  505. bmap->b_last_allocated_key = 0;
  506. bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR;
  507. bmap->b_state = 0;
  508. nilfs_btree_init_gc(bmap);
  509. }
  510. void nilfs_bmap_save(const struct nilfs_bmap *bmap,
  511. struct nilfs_bmap_store *store)
  512. {
  513. memcpy(store->data, bmap->b_u.u_data, sizeof(store->data));
  514. store->last_allocated_key = bmap->b_last_allocated_key;
  515. store->last_allocated_ptr = bmap->b_last_allocated_ptr;
  516. store->state = bmap->b_state;
  517. }
  518. void nilfs_bmap_restore(struct nilfs_bmap *bmap,
  519. const struct nilfs_bmap_store *store)
  520. {
  521. memcpy(bmap->b_u.u_data, store->data, sizeof(store->data));
  522. bmap->b_last_allocated_key = store->last_allocated_key;
  523. bmap->b_last_allocated_ptr = store->last_allocated_ptr;
  524. bmap->b_state = store->state;
  525. }