dat.c 13 KB

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