dat.c 13 KB

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