bmap.c 15 KB

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