dat.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. void *kaddr;
  79. kaddr = kmap_local_page(req->pr_entry_bh->b_page);
  80. entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
  81. req->pr_entry_bh, kaddr);
  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(kaddr);
  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. void *kaddr;
  99. kaddr = kmap_local_page(req->pr_entry_bh->b_page);
  100. entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
  101. req->pr_entry_bh, kaddr);
  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(kaddr);
  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. void *kaddr;
  124. kaddr = kmap_local_page(req->pr_entry_bh->b_page);
  125. entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
  126. req->pr_entry_bh, kaddr);
  127. entry->de_start = cpu_to_le64(nilfs_mdt_cno(dat));
  128. entry->de_blocknr = cpu_to_le64(blocknr);
  129. kunmap_local(kaddr);
  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. void *kaddr;
  138. int ret;
  139. ret = nilfs_dat_prepare_entry(dat, req, 0);
  140. if (ret < 0)
  141. return ret;
  142. kaddr = kmap_local_page(req->pr_entry_bh->b_page);
  143. entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
  144. req->pr_entry_bh, kaddr);
  145. start = le64_to_cpu(entry->de_start);
  146. blocknr = le64_to_cpu(entry->de_blocknr);
  147. kunmap_local(kaddr);
  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. void *kaddr;
  173. kaddr = kmap_local_page(req->pr_entry_bh->b_page);
  174. entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
  175. req->pr_entry_bh, kaddr);
  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(kaddr);
  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. void *kaddr;
  195. kaddr = kmap_local_page(req->pr_entry_bh->b_page);
  196. entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
  197. req->pr_entry_bh, kaddr);
  198. start = le64_to_cpu(entry->de_start);
  199. blocknr = le64_to_cpu(entry->de_blocknr);
  200. kunmap_local(kaddr);
  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 the following negative error code on failure.
  239. * * %-EINVAL - Invalid DAT entry (internal code).
  240. * * %-EIO - I/O error (including metadata corruption).
  241. * * %-ENOMEM - Insufficient memory available.
  242. */
  243. int nilfs_dat_mark_dirty(struct inode *dat, __u64 vblocknr)
  244. {
  245. struct nilfs_palloc_req req;
  246. int ret;
  247. req.pr_entry_nr = vblocknr;
  248. ret = nilfs_dat_prepare_entry(dat, &req, 0);
  249. if (ret == 0)
  250. nilfs_dat_commit_entry(dat, &req);
  251. return ret;
  252. }
  253. /**
  254. * nilfs_dat_freev - free virtual block numbers
  255. * @dat: DAT file inode
  256. * @vblocknrs: array of virtual block numbers
  257. * @nitems: number of virtual block numbers
  258. *
  259. * Description: nilfs_dat_freev() frees the virtual block numbers specified by
  260. * @vblocknrs and @nitems.
  261. *
  262. * Return Value: On success, 0 is returned. On error, one of the following
  263. * negative error codes is returned.
  264. *
  265. * %-EIO - I/O error.
  266. *
  267. * %-ENOMEM - Insufficient amount of memory available.
  268. *
  269. * %-ENOENT - The virtual block number have not been allocated.
  270. */
  271. int nilfs_dat_freev(struct inode *dat, __u64 *vblocknrs, size_t nitems)
  272. {
  273. return nilfs_palloc_freev(dat, vblocknrs, nitems);
  274. }
  275. /**
  276. * nilfs_dat_move - change a block number
  277. * @dat: DAT file inode
  278. * @vblocknr: virtual block number
  279. * @blocknr: block number
  280. *
  281. * Description: nilfs_dat_move() changes the block number associated with
  282. * @vblocknr to @blocknr.
  283. *
  284. * Return Value: On success, 0 is returned. On error, one of the following
  285. * negative error codes is returned.
  286. *
  287. * %-EIO - I/O error.
  288. *
  289. * %-ENOMEM - Insufficient amount of memory available.
  290. */
  291. int nilfs_dat_move(struct inode *dat, __u64 vblocknr, sector_t blocknr)
  292. {
  293. struct buffer_head *entry_bh;
  294. struct nilfs_dat_entry *entry;
  295. void *kaddr;
  296. int ret;
  297. ret = nilfs_palloc_get_entry_block(dat, vblocknr, 0, &entry_bh);
  298. if (ret < 0)
  299. return ret;
  300. /*
  301. * The given disk block number (blocknr) is not yet written to
  302. * the device at this point.
  303. *
  304. * To prevent nilfs_dat_translate() from returning the
  305. * uncommitted block number, this makes a copy of the entry
  306. * buffer and redirects nilfs_dat_translate() to the copy.
  307. */
  308. if (!buffer_nilfs_redirected(entry_bh)) {
  309. ret = nilfs_mdt_freeze_buffer(dat, entry_bh);
  310. if (ret) {
  311. brelse(entry_bh);
  312. return ret;
  313. }
  314. }
  315. kaddr = kmap_local_page(entry_bh->b_page);
  316. entry = nilfs_palloc_block_get_entry(dat, vblocknr, entry_bh, kaddr);
  317. if (unlikely(entry->de_blocknr == cpu_to_le64(0))) {
  318. nilfs_crit(dat->i_sb,
  319. "%s: invalid vblocknr = %llu, [%llu, %llu)",
  320. __func__, (unsigned long long)vblocknr,
  321. (unsigned long long)le64_to_cpu(entry->de_start),
  322. (unsigned long long)le64_to_cpu(entry->de_end));
  323. kunmap_local(kaddr);
  324. brelse(entry_bh);
  325. return -EINVAL;
  326. }
  327. WARN_ON(blocknr == 0);
  328. entry->de_blocknr = cpu_to_le64(blocknr);
  329. kunmap_local(kaddr);
  330. mark_buffer_dirty(entry_bh);
  331. nilfs_mdt_mark_dirty(dat);
  332. brelse(entry_bh);
  333. return 0;
  334. }
  335. /**
  336. * nilfs_dat_translate - translate a virtual block number to a block number
  337. * @dat: DAT file inode
  338. * @vblocknr: virtual block number
  339. * @blocknrp: pointer to a block number
  340. *
  341. * Description: nilfs_dat_translate() maps the virtual block number @vblocknr
  342. * to the corresponding block number.
  343. *
  344. * Return Value: On success, 0 is returned and the block number associated
  345. * with @vblocknr is stored in the place pointed by @blocknrp. On error, one
  346. * of the following negative error codes is returned.
  347. *
  348. * %-EIO - I/O error.
  349. *
  350. * %-ENOMEM - Insufficient amount of memory available.
  351. *
  352. * %-ENOENT - A block number associated with @vblocknr does not exist.
  353. */
  354. int nilfs_dat_translate(struct inode *dat, __u64 vblocknr, sector_t *blocknrp)
  355. {
  356. struct buffer_head *entry_bh, *bh;
  357. struct nilfs_dat_entry *entry;
  358. sector_t blocknr;
  359. void *kaddr;
  360. int ret;
  361. ret = nilfs_palloc_get_entry_block(dat, vblocknr, 0, &entry_bh);
  362. if (ret < 0)
  363. return ret;
  364. if (!nilfs_doing_gc() && buffer_nilfs_redirected(entry_bh)) {
  365. bh = nilfs_mdt_get_frozen_buffer(dat, entry_bh);
  366. if (bh) {
  367. WARN_ON(!buffer_uptodate(bh));
  368. brelse(entry_bh);
  369. entry_bh = bh;
  370. }
  371. }
  372. kaddr = kmap_local_page(entry_bh->b_page);
  373. entry = nilfs_palloc_block_get_entry(dat, vblocknr, entry_bh, kaddr);
  374. blocknr = le64_to_cpu(entry->de_blocknr);
  375. if (blocknr == 0) {
  376. ret = -ENOENT;
  377. goto out;
  378. }
  379. *blocknrp = blocknr;
  380. out:
  381. kunmap_local(kaddr);
  382. brelse(entry_bh);
  383. return ret;
  384. }
  385. ssize_t nilfs_dat_get_vinfo(struct inode *dat, void *buf, unsigned int visz,
  386. size_t nvi)
  387. {
  388. struct buffer_head *entry_bh;
  389. struct nilfs_dat_entry *entry;
  390. struct nilfs_vinfo *vinfo = buf;
  391. __u64 first, last;
  392. void *kaddr;
  393. unsigned long entries_per_block = NILFS_MDT(dat)->mi_entries_per_block;
  394. int i, j, n, ret;
  395. for (i = 0; i < nvi; i += n) {
  396. ret = nilfs_palloc_get_entry_block(dat, vinfo->vi_vblocknr,
  397. 0, &entry_bh);
  398. if (ret < 0)
  399. return ret;
  400. kaddr = kmap_local_page(entry_bh->b_page);
  401. /* last virtual block number in this block */
  402. first = vinfo->vi_vblocknr;
  403. first = div64_ul(first, entries_per_block);
  404. first *= entries_per_block;
  405. last = first + entries_per_block - 1;
  406. for (j = i, n = 0;
  407. j < nvi && vinfo->vi_vblocknr >= first &&
  408. vinfo->vi_vblocknr <= last;
  409. j++, n++, vinfo = (void *)vinfo + visz) {
  410. entry = nilfs_palloc_block_get_entry(
  411. dat, vinfo->vi_vblocknr, entry_bh, kaddr);
  412. vinfo->vi_start = le64_to_cpu(entry->de_start);
  413. vinfo->vi_end = le64_to_cpu(entry->de_end);
  414. vinfo->vi_blocknr = le64_to_cpu(entry->de_blocknr);
  415. }
  416. kunmap_local(kaddr);
  417. brelse(entry_bh);
  418. }
  419. return nvi;
  420. }
  421. /**
  422. * nilfs_dat_read - read or get dat inode
  423. * @sb: super block instance
  424. * @entry_size: size of a dat entry
  425. * @raw_inode: on-disk dat inode
  426. * @inodep: buffer to store the inode
  427. */
  428. int nilfs_dat_read(struct super_block *sb, size_t entry_size,
  429. struct nilfs_inode *raw_inode, struct inode **inodep)
  430. {
  431. static struct lock_class_key dat_lock_key;
  432. struct inode *dat;
  433. struct nilfs_dat_info *di;
  434. int err;
  435. if (entry_size > sb->s_blocksize) {
  436. nilfs_err(sb, "too large DAT entry size: %zu bytes",
  437. entry_size);
  438. return -EINVAL;
  439. } else if (entry_size < NILFS_MIN_DAT_ENTRY_SIZE) {
  440. nilfs_err(sb, "too small DAT entry size: %zu bytes",
  441. entry_size);
  442. return -EINVAL;
  443. }
  444. dat = nilfs_iget_locked(sb, NULL, NILFS_DAT_INO);
  445. if (unlikely(!dat))
  446. return -ENOMEM;
  447. if (!(dat->i_state & I_NEW))
  448. goto out;
  449. err = nilfs_mdt_init(dat, NILFS_MDT_GFP, sizeof(*di));
  450. if (err)
  451. goto failed;
  452. err = nilfs_palloc_init_blockgroup(dat, entry_size);
  453. if (err)
  454. goto failed;
  455. di = NILFS_DAT_I(dat);
  456. lockdep_set_class(&di->mi.mi_sem, &dat_lock_key);
  457. nilfs_palloc_setup_cache(dat, &di->palloc_cache);
  458. err = nilfs_mdt_setup_shadow_map(dat, &di->shadow);
  459. if (err)
  460. goto failed;
  461. err = nilfs_read_inode_common(dat, raw_inode);
  462. if (err)
  463. goto failed;
  464. unlock_new_inode(dat);
  465. out:
  466. *inodep = dat;
  467. return 0;
  468. failed:
  469. iget_failed(dat);
  470. return err;
  471. }