bmap.c 15 KB

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