bmap.c 14 KB

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