dat.c 13 KB

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