dat.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * NILFS disk address translation.
  4. *
  5. * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
  6. *
  7. * Written by Koji Sato.
  8. */
  9. #include <linux/types.h>
  10. #include <linux/buffer_head.h>
  11. #include <linux/string.h>
  12. #include <linux/errno.h>
  13. #include "nilfs.h"
  14. #include "mdt.h"
  15. #include "alloc.h"
  16. #include "dat.h"
  17. #define NILFS_CNO_MIN ((__u64)1)
  18. #define NILFS_CNO_MAX (~(__u64)0)
  19. /**
  20. * struct nilfs_dat_info - on-memory private data of DAT file
  21. * @mi: on-memory private data of metadata file
  22. * @palloc_cache: persistent object allocator cache of DAT file
  23. * @shadow: shadow map of DAT file
  24. */
  25. struct nilfs_dat_info {
  26. struct nilfs_mdt_info mi;
  27. struct nilfs_palloc_cache palloc_cache;
  28. struct nilfs_shadow_map shadow;
  29. };
  30. static inline struct nilfs_dat_info *NILFS_DAT_I(struct inode *dat)
  31. {
  32. return (struct nilfs_dat_info *)NILFS_MDT(dat);
  33. }
  34. static int nilfs_dat_prepare_entry(struct inode *dat,
  35. struct nilfs_palloc_req *req, int create)
  36. {
  37. int ret;
  38. ret = nilfs_palloc_get_entry_block(dat, req->pr_entry_nr,
  39. create, &req->pr_entry_bh);
  40. if (unlikely(ret == -ENOENT)) {
  41. nilfs_err(dat->i_sb,
  42. "DAT doesn't have a block to manage vblocknr = %llu",
  43. (unsigned long long)req->pr_entry_nr);
  44. /*
  45. * Return internal code -EINVAL to notify bmap layer of
  46. * metadata corruption.
  47. */
  48. ret = -EINVAL;
  49. }
  50. return ret;
  51. }
  52. static void nilfs_dat_commit_entry(struct inode *dat,
  53. struct nilfs_palloc_req *req)
  54. {
  55. mark_buffer_dirty(req->pr_entry_bh);
  56. nilfs_mdt_mark_dirty(dat);
  57. brelse(req->pr_entry_bh);
  58. }
  59. static void nilfs_dat_abort_entry(struct inode *dat,
  60. struct nilfs_palloc_req *req)
  61. {
  62. brelse(req->pr_entry_bh);
  63. }
  64. int nilfs_dat_prepare_alloc(struct inode *dat, struct nilfs_palloc_req *req)
  65. {
  66. int ret;
  67. ret = nilfs_palloc_prepare_alloc_entry(dat, req, true);
  68. if (ret < 0)
  69. return ret;
  70. ret = nilfs_dat_prepare_entry(dat, req, 1);
  71. if (ret < 0)
  72. nilfs_palloc_abort_alloc_entry(dat, req);
  73. return ret;
  74. }
  75. void nilfs_dat_commit_alloc(struct inode *dat, struct nilfs_palloc_req *req)
  76. {
  77. struct nilfs_dat_entry *entry;
  78. size_t offset;
  79. offset = nilfs_palloc_entry_offset(dat, req->pr_entry_nr,
  80. req->pr_entry_bh);
  81. entry = kmap_local_folio(req->pr_entry_bh->b_folio, offset);
  82. entry->de_start = cpu_to_le64(NILFS_CNO_MIN);
  83. entry->de_end = cpu_to_le64(NILFS_CNO_MAX);
  84. entry->de_blocknr = cpu_to_le64(0);
  85. kunmap_local(entry);
  86. nilfs_palloc_commit_alloc_entry(dat, req);
  87. nilfs_dat_commit_entry(dat, req);
  88. }
  89. void nilfs_dat_abort_alloc(struct inode *dat, struct nilfs_palloc_req *req)
  90. {
  91. nilfs_dat_abort_entry(dat, req);
  92. nilfs_palloc_abort_alloc_entry(dat, req);
  93. }
  94. static void nilfs_dat_commit_free(struct inode *dat,
  95. struct nilfs_palloc_req *req)
  96. {
  97. struct nilfs_dat_entry *entry;
  98. size_t offset;
  99. offset = nilfs_palloc_entry_offset(dat, req->pr_entry_nr,
  100. req->pr_entry_bh);
  101. entry = kmap_local_folio(req->pr_entry_bh->b_folio, offset);
  102. entry->de_start = cpu_to_le64(NILFS_CNO_MIN);
  103. entry->de_end = cpu_to_le64(NILFS_CNO_MIN);
  104. entry->de_blocknr = cpu_to_le64(0);
  105. kunmap_local(entry);
  106. nilfs_dat_commit_entry(dat, req);
  107. if (unlikely(req->pr_desc_bh == NULL || req->pr_bitmap_bh == NULL)) {
  108. nilfs_error(dat->i_sb,
  109. "state inconsistency probably due to duplicate use of vblocknr = %llu",
  110. (unsigned long long)req->pr_entry_nr);
  111. return;
  112. }
  113. nilfs_palloc_commit_free_entry(dat, req);
  114. }
  115. int nilfs_dat_prepare_start(struct inode *dat, struct nilfs_palloc_req *req)
  116. {
  117. return nilfs_dat_prepare_entry(dat, req, 0);
  118. }
  119. void nilfs_dat_commit_start(struct inode *dat, struct nilfs_palloc_req *req,
  120. sector_t blocknr)
  121. {
  122. struct nilfs_dat_entry *entry;
  123. size_t offset;
  124. offset = nilfs_palloc_entry_offset(dat, req->pr_entry_nr,
  125. req->pr_entry_bh);
  126. entry = kmap_local_folio(req->pr_entry_bh->b_folio, offset);
  127. entry->de_start = cpu_to_le64(nilfs_mdt_cno(dat));
  128. entry->de_blocknr = cpu_to_le64(blocknr);
  129. kunmap_local(entry);
  130. nilfs_dat_commit_entry(dat, req);
  131. }
  132. int nilfs_dat_prepare_end(struct inode *dat, struct nilfs_palloc_req *req)
  133. {
  134. struct nilfs_dat_entry *entry;
  135. __u64 start;
  136. sector_t blocknr;
  137. size_t offset;
  138. int ret;
  139. ret = nilfs_dat_prepare_entry(dat, req, 0);
  140. if (ret < 0)
  141. return ret;
  142. offset = nilfs_palloc_entry_offset(dat, req->pr_entry_nr,
  143. req->pr_entry_bh);
  144. entry = kmap_local_folio(req->pr_entry_bh->b_folio, offset);
  145. start = le64_to_cpu(entry->de_start);
  146. blocknr = le64_to_cpu(entry->de_blocknr);
  147. kunmap_local(entry);
  148. if (blocknr == 0) {
  149. ret = nilfs_palloc_prepare_free_entry(dat, req);
  150. if (ret < 0) {
  151. nilfs_dat_abort_entry(dat, req);
  152. return ret;
  153. }
  154. }
  155. if (unlikely(start > nilfs_mdt_cno(dat))) {
  156. nilfs_err(dat->i_sb,
  157. "vblocknr = %llu has abnormal lifetime: start cno (= %llu) > current cno (= %llu)",
  158. (unsigned long long)req->pr_entry_nr,
  159. (unsigned long long)start,
  160. (unsigned long long)nilfs_mdt_cno(dat));
  161. nilfs_dat_abort_entry(dat, req);
  162. return -EINVAL;
  163. }
  164. return 0;
  165. }
  166. void nilfs_dat_commit_end(struct inode *dat, struct nilfs_palloc_req *req,
  167. int dead)
  168. {
  169. struct nilfs_dat_entry *entry;
  170. __u64 start, end;
  171. sector_t blocknr;
  172. size_t offset;
  173. offset = nilfs_palloc_entry_offset(dat, req->pr_entry_nr,
  174. req->pr_entry_bh);
  175. entry = kmap_local_folio(req->pr_entry_bh->b_folio, offset);
  176. end = start = le64_to_cpu(entry->de_start);
  177. if (!dead) {
  178. end = nilfs_mdt_cno(dat);
  179. WARN_ON(start > end);
  180. }
  181. entry->de_end = cpu_to_le64(end);
  182. blocknr = le64_to_cpu(entry->de_blocknr);
  183. kunmap_local(entry);
  184. if (blocknr == 0)
  185. nilfs_dat_commit_free(dat, req);
  186. else
  187. nilfs_dat_commit_entry(dat, req);
  188. }
  189. void nilfs_dat_abort_end(struct inode *dat, struct nilfs_palloc_req *req)
  190. {
  191. struct nilfs_dat_entry *entry;
  192. __u64 start;
  193. sector_t blocknr;
  194. size_t offset;
  195. offset = nilfs_palloc_entry_offset(dat, req->pr_entry_nr,
  196. req->pr_entry_bh);
  197. entry = kmap_local_folio(req->pr_entry_bh->b_folio, offset);
  198. start = le64_to_cpu(entry->de_start);
  199. blocknr = le64_to_cpu(entry->de_blocknr);
  200. kunmap_local(entry);
  201. if (start == nilfs_mdt_cno(dat) && blocknr == 0)
  202. nilfs_palloc_abort_free_entry(dat, req);
  203. nilfs_dat_abort_entry(dat, req);
  204. }
  205. int nilfs_dat_prepare_update(struct inode *dat,
  206. struct nilfs_palloc_req *oldreq,
  207. struct nilfs_palloc_req *newreq)
  208. {
  209. int ret;
  210. ret = nilfs_dat_prepare_end(dat, oldreq);
  211. if (!ret) {
  212. ret = nilfs_dat_prepare_alloc(dat, newreq);
  213. if (ret < 0)
  214. nilfs_dat_abort_end(dat, oldreq);
  215. }
  216. return ret;
  217. }
  218. void nilfs_dat_commit_update(struct inode *dat,
  219. struct nilfs_palloc_req *oldreq,
  220. struct nilfs_palloc_req *newreq, int dead)
  221. {
  222. nilfs_dat_commit_end(dat, oldreq, dead);
  223. nilfs_dat_commit_alloc(dat, newreq);
  224. }
  225. void nilfs_dat_abort_update(struct inode *dat,
  226. struct nilfs_palloc_req *oldreq,
  227. struct nilfs_palloc_req *newreq)
  228. {
  229. nilfs_dat_abort_end(dat, oldreq);
  230. nilfs_dat_abort_alloc(dat, newreq);
  231. }
  232. /**
  233. * nilfs_dat_mark_dirty - mark the DAT block buffer containing the specified
  234. * virtual block address entry as dirty
  235. * @dat: DAT file inode
  236. * @vblocknr: virtual block number
  237. *
  238. * Return: 0 on success, or one of the following negative error codes on
  239. * failure:
  240. * * %-EINVAL - Invalid DAT entry (internal code).
  241. * * %-EIO - I/O error (including metadata corruption).
  242. * * %-ENOMEM - Insufficient memory available.
  243. */
  244. int nilfs_dat_mark_dirty(struct inode *dat, __u64 vblocknr)
  245. {
  246. struct nilfs_palloc_req req;
  247. int ret;
  248. req.pr_entry_nr = vblocknr;
  249. ret = nilfs_dat_prepare_entry(dat, &req, 0);
  250. if (ret == 0)
  251. nilfs_dat_commit_entry(dat, &req);
  252. return ret;
  253. }
  254. /**
  255. * nilfs_dat_freev - free virtual block numbers
  256. * @dat: DAT file inode
  257. * @vblocknrs: array of virtual block numbers
  258. * @nitems: number of virtual block numbers
  259. *
  260. * Description: nilfs_dat_freev() frees the virtual block numbers specified by
  261. * @vblocknrs and @nitems.
  262. *
  263. * Return: 0 on success, or one of the following negative error codes on
  264. * failure:
  265. * * %-EIO - I/O error (including metadata corruption).
  266. * * %-ENOENT - The virtual block number have not been allocated.
  267. * * %-ENOMEM - Insufficient memory available.
  268. */
  269. int nilfs_dat_freev(struct inode *dat, __u64 *vblocknrs, size_t nitems)
  270. {
  271. return nilfs_palloc_freev(dat, vblocknrs, nitems);
  272. }
  273. /**
  274. * nilfs_dat_move - change a block number
  275. * @dat: DAT file inode
  276. * @vblocknr: virtual block number
  277. * @blocknr: block number
  278. *
  279. * Description: nilfs_dat_move() changes the block number associated with
  280. * @vblocknr to @blocknr.
  281. *
  282. * Return: 0 on success, or one of the following negative error codes on
  283. * failure:
  284. * * %-EIO - I/O error (including metadata corruption).
  285. * * %-ENOMEM - Insufficient memory available.
  286. */
  287. int nilfs_dat_move(struct inode *dat, __u64 vblocknr, sector_t blocknr)
  288. {
  289. struct buffer_head *entry_bh;
  290. struct nilfs_dat_entry *entry;
  291. size_t offset;
  292. int ret;
  293. ret = nilfs_palloc_get_entry_block(dat, vblocknr, 0, &entry_bh);
  294. if (ret < 0)
  295. return ret;
  296. /*
  297. * The given disk block number (blocknr) is not yet written to
  298. * the device at this point.
  299. *
  300. * To prevent nilfs_dat_translate() from returning the
  301. * uncommitted block number, this makes a copy of the entry
  302. * buffer and redirects nilfs_dat_translate() to the copy.
  303. */
  304. if (!buffer_nilfs_redirected(entry_bh)) {
  305. ret = nilfs_mdt_freeze_buffer(dat, entry_bh);
  306. if (ret) {
  307. brelse(entry_bh);
  308. return ret;
  309. }
  310. }
  311. offset = nilfs_palloc_entry_offset(dat, vblocknr, entry_bh);
  312. entry = kmap_local_folio(entry_bh->b_folio, offset);
  313. if (unlikely(entry->de_blocknr == cpu_to_le64(0))) {
  314. nilfs_crit(dat->i_sb,
  315. "%s: invalid vblocknr = %llu, [%llu, %llu)",
  316. __func__, (unsigned long long)vblocknr,
  317. (unsigned long long)le64_to_cpu(entry->de_start),
  318. (unsigned long long)le64_to_cpu(entry->de_end));
  319. kunmap_local(entry);
  320. brelse(entry_bh);
  321. return -EINVAL;
  322. }
  323. WARN_ON(blocknr == 0);
  324. entry->de_blocknr = cpu_to_le64(blocknr);
  325. kunmap_local(entry);
  326. mark_buffer_dirty(entry_bh);
  327. nilfs_mdt_mark_dirty(dat);
  328. brelse(entry_bh);
  329. return 0;
  330. }
  331. /**
  332. * nilfs_dat_translate - translate a virtual block number to a block number
  333. * @dat: DAT file inode
  334. * @vblocknr: virtual block number
  335. * @blocknrp: pointer to a block number
  336. *
  337. * Description: nilfs_dat_translate() maps the virtual block number @vblocknr
  338. * to the corresponding block number. The block number associated with
  339. * @vblocknr is stored in the place pointed to by @blocknrp.
  340. *
  341. * Return: 0 on success, or one of the following negative error codes on
  342. * failure:
  343. * * %-EIO - I/O error (including metadata corruption).
  344. * * %-ENOENT - A block number associated with @vblocknr does not exist.
  345. * * %-ENOMEM - Insufficient memory available.
  346. */
  347. int nilfs_dat_translate(struct inode *dat, __u64 vblocknr, sector_t *blocknrp)
  348. {
  349. struct buffer_head *entry_bh, *bh;
  350. struct nilfs_dat_entry *entry;
  351. sector_t blocknr;
  352. size_t offset;
  353. int ret;
  354. ret = nilfs_palloc_get_entry_block(dat, vblocknr, 0, &entry_bh);
  355. if (ret < 0)
  356. return ret;
  357. if (!nilfs_doing_gc() && buffer_nilfs_redirected(entry_bh)) {
  358. bh = nilfs_mdt_get_frozen_buffer(dat, entry_bh);
  359. if (bh) {
  360. WARN_ON(!buffer_uptodate(bh));
  361. brelse(entry_bh);
  362. entry_bh = bh;
  363. }
  364. }
  365. offset = nilfs_palloc_entry_offset(dat, vblocknr, entry_bh);
  366. entry = kmap_local_folio(entry_bh->b_folio, offset);
  367. blocknr = le64_to_cpu(entry->de_blocknr);
  368. if (blocknr == 0) {
  369. ret = -ENOENT;
  370. goto out;
  371. }
  372. *blocknrp = blocknr;
  373. out:
  374. kunmap_local(entry);
  375. brelse(entry_bh);
  376. return ret;
  377. }
  378. ssize_t nilfs_dat_get_vinfo(struct inode *dat, void *buf, unsigned int visz,
  379. size_t nvi)
  380. {
  381. struct buffer_head *entry_bh;
  382. struct nilfs_dat_entry *entry, *first_entry;
  383. struct nilfs_vinfo *vinfo = buf;
  384. __u64 first, last;
  385. size_t offset;
  386. unsigned long entries_per_block = NILFS_MDT(dat)->mi_entries_per_block;
  387. unsigned int entry_size = NILFS_MDT(dat)->mi_entry_size;
  388. int i, j, n, ret;
  389. for (i = 0; i < nvi; i += n) {
  390. ret = nilfs_palloc_get_entry_block(dat, vinfo->vi_vblocknr,
  391. 0, &entry_bh);
  392. if (ret < 0)
  393. return ret;
  394. first = vinfo->vi_vblocknr;
  395. first = div64_ul(first, entries_per_block);
  396. first *= entries_per_block;
  397. /* first virtual block number in this block */
  398. last = first + entries_per_block - 1;
  399. /* last virtual block number in this block */
  400. offset = nilfs_palloc_entry_offset(dat, first, entry_bh);
  401. first_entry = kmap_local_folio(entry_bh->b_folio, offset);
  402. for (j = i, n = 0;
  403. j < nvi && vinfo->vi_vblocknr >= first &&
  404. vinfo->vi_vblocknr <= last;
  405. j++, n++, vinfo = (void *)vinfo + visz) {
  406. entry = (void *)first_entry +
  407. (vinfo->vi_vblocknr - first) * entry_size;
  408. vinfo->vi_start = le64_to_cpu(entry->de_start);
  409. vinfo->vi_end = le64_to_cpu(entry->de_end);
  410. vinfo->vi_blocknr = le64_to_cpu(entry->de_blocknr);
  411. }
  412. kunmap_local(first_entry);
  413. brelse(entry_bh);
  414. }
  415. return nvi;
  416. }
  417. /**
  418. * nilfs_dat_read - read or get dat inode
  419. * @sb: super block instance
  420. * @entry_size: size of a dat entry
  421. * @raw_inode: on-disk dat inode
  422. * @inodep: buffer to store the inode
  423. *
  424. * Return: 0 on success, or a negative error code on failure.
  425. */
  426. int nilfs_dat_read(struct super_block *sb, size_t entry_size,
  427. struct nilfs_inode *raw_inode, struct inode **inodep)
  428. {
  429. static struct lock_class_key dat_lock_key;
  430. struct inode *dat;
  431. struct nilfs_dat_info *di;
  432. int err;
  433. if (entry_size > sb->s_blocksize) {
  434. nilfs_err(sb, "too large DAT entry size: %zu bytes",
  435. entry_size);
  436. return -EINVAL;
  437. } else if (entry_size < NILFS_MIN_DAT_ENTRY_SIZE) {
  438. nilfs_err(sb, "too small DAT entry size: %zu bytes",
  439. entry_size);
  440. return -EINVAL;
  441. }
  442. dat = nilfs_iget_locked(sb, NULL, NILFS_DAT_INO);
  443. if (unlikely(!dat))
  444. return -ENOMEM;
  445. if (!(inode_state_read_once(dat) & I_NEW))
  446. goto out;
  447. err = nilfs_mdt_init(dat, NILFS_MDT_GFP, sizeof(*di));
  448. if (err)
  449. goto failed;
  450. err = nilfs_palloc_init_blockgroup(dat, entry_size);
  451. if (err)
  452. goto failed;
  453. di = NILFS_DAT_I(dat);
  454. lockdep_set_class(&di->mi.mi_sem, &dat_lock_key);
  455. nilfs_palloc_setup_cache(dat, &di->palloc_cache);
  456. err = nilfs_mdt_setup_shadow_map(dat, &di->shadow);
  457. if (err)
  458. goto failed;
  459. err = nilfs_read_inode_common(dat, raw_inode);
  460. if (err)
  461. goto failed;
  462. unlock_new_inode(dat);
  463. out:
  464. *inodep = dat;
  465. return 0;
  466. failed:
  467. iget_failed(dat);
  468. return err;
  469. }