dat.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. __u64 start;
  134. sector_t blocknr;
  135. void *kaddr;
  136. int ret;
  137. ret = nilfs_dat_prepare_entry(dat, req, 0);
  138. if (ret < 0) {
  139. WARN_ON(ret == -ENOENT);
  140. return ret;
  141. }
  142. kaddr = kmap_atomic(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_atomic(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. return 0;
  156. }
  157. void nilfs_dat_commit_end(struct inode *dat, struct nilfs_palloc_req *req,
  158. int dead)
  159. {
  160. struct nilfs_dat_entry *entry;
  161. __u64 start, end;
  162. sector_t blocknr;
  163. void *kaddr;
  164. kaddr = kmap_atomic(req->pr_entry_bh->b_page);
  165. entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
  166. req->pr_entry_bh, kaddr);
  167. end = start = le64_to_cpu(entry->de_start);
  168. if (!dead) {
  169. end = nilfs_mdt_cno(dat);
  170. WARN_ON(start > end);
  171. }
  172. entry->de_end = cpu_to_le64(end);
  173. blocknr = le64_to_cpu(entry->de_blocknr);
  174. kunmap_atomic(kaddr);
  175. if (blocknr == 0)
  176. nilfs_dat_commit_free(dat, req);
  177. else
  178. nilfs_dat_commit_entry(dat, req);
  179. }
  180. void nilfs_dat_abort_end(struct inode *dat, struct nilfs_palloc_req *req)
  181. {
  182. struct nilfs_dat_entry *entry;
  183. __u64 start;
  184. sector_t blocknr;
  185. void *kaddr;
  186. kaddr = kmap_atomic(req->pr_entry_bh->b_page);
  187. entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
  188. req->pr_entry_bh, kaddr);
  189. start = le64_to_cpu(entry->de_start);
  190. blocknr = le64_to_cpu(entry->de_blocknr);
  191. kunmap_atomic(kaddr);
  192. if (start == nilfs_mdt_cno(dat) && blocknr == 0)
  193. nilfs_palloc_abort_free_entry(dat, req);
  194. nilfs_dat_abort_entry(dat, req);
  195. }
  196. int nilfs_dat_prepare_update(struct inode *dat,
  197. struct nilfs_palloc_req *oldreq,
  198. struct nilfs_palloc_req *newreq)
  199. {
  200. int ret;
  201. ret = nilfs_dat_prepare_end(dat, oldreq);
  202. if (!ret) {
  203. ret = nilfs_dat_prepare_alloc(dat, newreq);
  204. if (ret < 0)
  205. nilfs_dat_abort_end(dat, oldreq);
  206. }
  207. return ret;
  208. }
  209. void nilfs_dat_commit_update(struct inode *dat,
  210. struct nilfs_palloc_req *oldreq,
  211. struct nilfs_palloc_req *newreq, int dead)
  212. {
  213. nilfs_dat_commit_end(dat, oldreq, dead);
  214. nilfs_dat_commit_alloc(dat, newreq);
  215. }
  216. void nilfs_dat_abort_update(struct inode *dat,
  217. struct nilfs_palloc_req *oldreq,
  218. struct nilfs_palloc_req *newreq)
  219. {
  220. nilfs_dat_abort_end(dat, oldreq);
  221. nilfs_dat_abort_alloc(dat, newreq);
  222. }
  223. /**
  224. * nilfs_dat_mark_dirty -
  225. * @dat: DAT file inode
  226. * @vblocknr: virtual block number
  227. *
  228. * Description:
  229. *
  230. * Return Value: On success, 0 is returned. On error, one of the following
  231. * negative error codes is returned.
  232. *
  233. * %-EIO - I/O error.
  234. *
  235. * %-ENOMEM - Insufficient amount of memory available.
  236. */
  237. int nilfs_dat_mark_dirty(struct inode *dat, __u64 vblocknr)
  238. {
  239. struct nilfs_palloc_req req;
  240. int ret;
  241. req.pr_entry_nr = vblocknr;
  242. ret = nilfs_dat_prepare_entry(dat, &req, 0);
  243. if (ret == 0)
  244. nilfs_dat_commit_entry(dat, &req);
  245. return ret;
  246. }
  247. /**
  248. * nilfs_dat_freev - free virtual block numbers
  249. * @dat: DAT file inode
  250. * @vblocknrs: array of virtual block numbers
  251. * @nitems: number of virtual block numbers
  252. *
  253. * Description: nilfs_dat_freev() frees the virtual block numbers specified by
  254. * @vblocknrs and @nitems.
  255. *
  256. * Return Value: On success, 0 is returned. On error, one of the following
  257. * negative error codes is returned.
  258. *
  259. * %-EIO - I/O error.
  260. *
  261. * %-ENOMEM - Insufficient amount of memory available.
  262. *
  263. * %-ENOENT - The virtual block number have not been allocated.
  264. */
  265. int nilfs_dat_freev(struct inode *dat, __u64 *vblocknrs, size_t nitems)
  266. {
  267. return nilfs_palloc_freev(dat, vblocknrs, nitems);
  268. }
  269. /**
  270. * nilfs_dat_move - change a block number
  271. * @dat: DAT file inode
  272. * @vblocknr: virtual block number
  273. * @blocknr: block number
  274. *
  275. * Description: nilfs_dat_move() changes the block number associated with
  276. * @vblocknr to @blocknr.
  277. *
  278. * Return Value: On success, 0 is returned. On error, one of the following
  279. * negative error codes is returned.
  280. *
  281. * %-EIO - I/O error.
  282. *
  283. * %-ENOMEM - Insufficient amount of memory available.
  284. */
  285. int nilfs_dat_move(struct inode *dat, __u64 vblocknr, sector_t blocknr)
  286. {
  287. struct buffer_head *entry_bh;
  288. struct nilfs_dat_entry *entry;
  289. void *kaddr;
  290. int ret;
  291. ret = nilfs_palloc_get_entry_block(dat, vblocknr, 0, &entry_bh);
  292. if (ret < 0)
  293. return ret;
  294. /*
  295. * The given disk block number (blocknr) is not yet written to
  296. * the device at this point.
  297. *
  298. * To prevent nilfs_dat_translate() from returning the
  299. * uncommitted block number, this makes a copy of the entry
  300. * buffer and redirects nilfs_dat_translate() to the copy.
  301. */
  302. if (!buffer_nilfs_redirected(entry_bh)) {
  303. ret = nilfs_mdt_freeze_buffer(dat, entry_bh);
  304. if (ret) {
  305. brelse(entry_bh);
  306. return ret;
  307. }
  308. }
  309. kaddr = kmap_atomic(entry_bh->b_page);
  310. entry = nilfs_palloc_block_get_entry(dat, vblocknr, entry_bh, kaddr);
  311. if (unlikely(entry->de_blocknr == cpu_to_le64(0))) {
  312. printk(KERN_CRIT "%s: vbn = %llu, [%llu, %llu)\n", __func__,
  313. (unsigned long long)vblocknr,
  314. (unsigned long long)le64_to_cpu(entry->de_start),
  315. (unsigned long long)le64_to_cpu(entry->de_end));
  316. kunmap_atomic(kaddr);
  317. brelse(entry_bh);
  318. return -EINVAL;
  319. }
  320. WARN_ON(blocknr == 0);
  321. entry->de_blocknr = cpu_to_le64(blocknr);
  322. kunmap_atomic(kaddr);
  323. mark_buffer_dirty(entry_bh);
  324. nilfs_mdt_mark_dirty(dat);
  325. brelse(entry_bh);
  326. return 0;
  327. }
  328. /**
  329. * nilfs_dat_translate - translate a virtual block number to a block number
  330. * @dat: DAT file inode
  331. * @vblocknr: virtual block number
  332. * @blocknrp: pointer to a block number
  333. *
  334. * Description: nilfs_dat_translate() maps the virtual block number @vblocknr
  335. * to the corresponding block number.
  336. *
  337. * Return Value: On success, 0 is returned and the block number associated
  338. * with @vblocknr is stored in the place pointed by @blocknrp. On error, one
  339. * of the following negative error codes is returned.
  340. *
  341. * %-EIO - I/O error.
  342. *
  343. * %-ENOMEM - Insufficient amount of memory available.
  344. *
  345. * %-ENOENT - A block number associated with @vblocknr does not exist.
  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. void *kaddr;
  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. kaddr = kmap_atomic(entry_bh->b_page);
  366. entry = nilfs_palloc_block_get_entry(dat, vblocknr, entry_bh, kaddr);
  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_atomic(kaddr);
  375. brelse(entry_bh);
  376. return ret;
  377. }
  378. ssize_t nilfs_dat_get_vinfo(struct inode *dat, void *buf, unsigned visz,
  379. size_t nvi)
  380. {
  381. struct buffer_head *entry_bh;
  382. struct nilfs_dat_entry *entry;
  383. struct nilfs_vinfo *vinfo = buf;
  384. __u64 first, last;
  385. void *kaddr;
  386. unsigned long entries_per_block = NILFS_MDT(dat)->mi_entries_per_block;
  387. int i, j, n, ret;
  388. for (i = 0; i < nvi; i += n) {
  389. ret = nilfs_palloc_get_entry_block(dat, vinfo->vi_vblocknr,
  390. 0, &entry_bh);
  391. if (ret < 0)
  392. return ret;
  393. kaddr = kmap_atomic(entry_bh->b_page);
  394. /* last virtual block number in this block */
  395. first = vinfo->vi_vblocknr;
  396. do_div(first, entries_per_block);
  397. first *= entries_per_block;
  398. last = first + entries_per_block - 1;
  399. for (j = i, n = 0;
  400. j < nvi && vinfo->vi_vblocknr >= first &&
  401. vinfo->vi_vblocknr <= last;
  402. j++, n++, vinfo = (void *)vinfo + visz) {
  403. entry = nilfs_palloc_block_get_entry(
  404. dat, vinfo->vi_vblocknr, entry_bh, kaddr);
  405. vinfo->vi_start = le64_to_cpu(entry->de_start);
  406. vinfo->vi_end = le64_to_cpu(entry->de_end);
  407. vinfo->vi_blocknr = le64_to_cpu(entry->de_blocknr);
  408. }
  409. kunmap_atomic(kaddr);
  410. brelse(entry_bh);
  411. }
  412. return nvi;
  413. }
  414. /**
  415. * nilfs_dat_read - read or get dat inode
  416. * @sb: super block instance
  417. * @entry_size: size of a dat entry
  418. * @raw_inode: on-disk dat inode
  419. * @inodep: buffer to store the inode
  420. */
  421. int nilfs_dat_read(struct super_block *sb, size_t entry_size,
  422. struct nilfs_inode *raw_inode, struct inode **inodep)
  423. {
  424. static struct lock_class_key dat_lock_key;
  425. struct inode *dat;
  426. struct nilfs_dat_info *di;
  427. int err;
  428. if (entry_size > sb->s_blocksize) {
  429. printk(KERN_ERR
  430. "NILFS: too large DAT entry size: %zu bytes.\n",
  431. entry_size);
  432. return -EINVAL;
  433. } else if (entry_size < NILFS_MIN_DAT_ENTRY_SIZE) {
  434. printk(KERN_ERR
  435. "NILFS: too small DAT entry size: %zu bytes.\n",
  436. entry_size);
  437. return -EINVAL;
  438. }
  439. dat = nilfs_iget_locked(sb, NULL, NILFS_DAT_INO);
  440. if (unlikely(!dat))
  441. return -ENOMEM;
  442. if (!(dat->i_state & I_NEW))
  443. goto out;
  444. err = nilfs_mdt_init(dat, NILFS_MDT_GFP, sizeof(*di));
  445. if (err)
  446. goto failed;
  447. err = nilfs_palloc_init_blockgroup(dat, entry_size);
  448. if (err)
  449. goto failed;
  450. di = NILFS_DAT_I(dat);
  451. lockdep_set_class(&di->mi.mi_sem, &dat_lock_key);
  452. nilfs_palloc_setup_cache(dat, &di->palloc_cache);
  453. nilfs_mdt_setup_shadow_map(dat, &di->shadow);
  454. err = nilfs_read_inode_common(dat, raw_inode);
  455. if (err)
  456. goto failed;
  457. unlock_new_inode(dat);
  458. out:
  459. *inodep = dat;
  460. return 0;
  461. failed:
  462. iget_failed(dat);
  463. return err;
  464. }