dat.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. struct nilfs_dat_info {
  33. struct nilfs_mdt_info mi;
  34. struct nilfs_palloc_cache palloc_cache;
  35. struct nilfs_shadow_map shadow;
  36. };
  37. static inline struct nilfs_dat_info *NILFS_DAT_I(struct inode *dat)
  38. {
  39. return (struct nilfs_dat_info *)NILFS_MDT(dat);
  40. }
  41. static int nilfs_dat_prepare_entry(struct inode *dat,
  42. struct nilfs_palloc_req *req, int create)
  43. {
  44. return nilfs_palloc_get_entry_block(dat, req->pr_entry_nr,
  45. create, &req->pr_entry_bh);
  46. }
  47. static void nilfs_dat_commit_entry(struct inode *dat,
  48. struct nilfs_palloc_req *req)
  49. {
  50. mark_buffer_dirty(req->pr_entry_bh);
  51. nilfs_mdt_mark_dirty(dat);
  52. brelse(req->pr_entry_bh);
  53. }
  54. static void nilfs_dat_abort_entry(struct inode *dat,
  55. struct nilfs_palloc_req *req)
  56. {
  57. brelse(req->pr_entry_bh);
  58. }
  59. int nilfs_dat_prepare_alloc(struct inode *dat, struct nilfs_palloc_req *req)
  60. {
  61. int ret;
  62. ret = nilfs_palloc_prepare_alloc_entry(dat, req);
  63. if (ret < 0)
  64. return ret;
  65. ret = nilfs_dat_prepare_entry(dat, req, 1);
  66. if (ret < 0)
  67. nilfs_palloc_abort_alloc_entry(dat, req);
  68. return ret;
  69. }
  70. void nilfs_dat_commit_alloc(struct inode *dat, struct nilfs_palloc_req *req)
  71. {
  72. struct nilfs_dat_entry *entry;
  73. void *kaddr;
  74. kaddr = kmap_atomic(req->pr_entry_bh->b_page);
  75. entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
  76. req->pr_entry_bh, kaddr);
  77. entry->de_start = cpu_to_le64(NILFS_CNO_MIN);
  78. entry->de_end = cpu_to_le64(NILFS_CNO_MAX);
  79. entry->de_blocknr = cpu_to_le64(0);
  80. kunmap_atomic(kaddr);
  81. nilfs_palloc_commit_alloc_entry(dat, req);
  82. nilfs_dat_commit_entry(dat, req);
  83. }
  84. void nilfs_dat_abort_alloc(struct inode *dat, struct nilfs_palloc_req *req)
  85. {
  86. nilfs_dat_abort_entry(dat, req);
  87. nilfs_palloc_abort_alloc_entry(dat, req);
  88. }
  89. static void nilfs_dat_commit_free(struct inode *dat,
  90. struct nilfs_palloc_req *req)
  91. {
  92. struct nilfs_dat_entry *entry;
  93. void *kaddr;
  94. kaddr = kmap_atomic(req->pr_entry_bh->b_page);
  95. entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
  96. req->pr_entry_bh, kaddr);
  97. entry->de_start = cpu_to_le64(NILFS_CNO_MIN);
  98. entry->de_end = cpu_to_le64(NILFS_CNO_MIN);
  99. entry->de_blocknr = cpu_to_le64(0);
  100. kunmap_atomic(kaddr);
  101. nilfs_dat_commit_entry(dat, req);
  102. nilfs_palloc_commit_free_entry(dat, req);
  103. }
  104. int nilfs_dat_prepare_start(struct inode *dat, struct nilfs_palloc_req *req)
  105. {
  106. int ret;
  107. ret = nilfs_dat_prepare_entry(dat, req, 0);
  108. WARN_ON(ret == -ENOENT);
  109. return ret;
  110. }
  111. void nilfs_dat_commit_start(struct inode *dat, struct nilfs_palloc_req *req,
  112. sector_t blocknr)
  113. {
  114. struct nilfs_dat_entry *entry;
  115. void *kaddr;
  116. kaddr = kmap_atomic(req->pr_entry_bh->b_page);
  117. entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
  118. req->pr_entry_bh, kaddr);
  119. entry->de_start = cpu_to_le64(nilfs_mdt_cno(dat));
  120. entry->de_blocknr = cpu_to_le64(blocknr);
  121. kunmap_atomic(kaddr);
  122. nilfs_dat_commit_entry(dat, req);
  123. }
  124. int nilfs_dat_prepare_end(struct inode *dat, struct nilfs_palloc_req *req)
  125. {
  126. struct nilfs_dat_entry *entry;
  127. __u64 start;
  128. sector_t blocknr;
  129. void *kaddr;
  130. int ret;
  131. ret = nilfs_dat_prepare_entry(dat, req, 0);
  132. if (ret < 0) {
  133. WARN_ON(ret == -ENOENT);
  134. return ret;
  135. }
  136. kaddr = kmap_atomic(req->pr_entry_bh->b_page);
  137. entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
  138. req->pr_entry_bh, kaddr);
  139. start = le64_to_cpu(entry->de_start);
  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. printk(KERN_CRIT "%s: vbn = %llu, [%llu, %llu)\n", __func__,
  307. (unsigned long long)vblocknr,
  308. (unsigned long long)le64_to_cpu(entry->de_start),
  309. (unsigned long long)le64_to_cpu(entry->de_end));
  310. kunmap_atomic(kaddr);
  311. brelse(entry_bh);
  312. return -EINVAL;
  313. }
  314. WARN_ON(blocknr == 0);
  315. entry->de_blocknr = cpu_to_le64(blocknr);
  316. kunmap_atomic(kaddr);
  317. mark_buffer_dirty(entry_bh);
  318. nilfs_mdt_mark_dirty(dat);
  319. brelse(entry_bh);
  320. return 0;
  321. }
  322. /**
  323. * nilfs_dat_translate - translate a virtual block number to a block number
  324. * @dat: DAT file inode
  325. * @vblocknr: virtual block number
  326. * @blocknrp: pointer to a block number
  327. *
  328. * Description: nilfs_dat_translate() maps the virtual block number @vblocknr
  329. * to the corresponding block number.
  330. *
  331. * Return Value: On success, 0 is returned and the block number associated
  332. * with @vblocknr is stored in the place pointed by @blocknrp. On error, one
  333. * of the following negative error codes is returned.
  334. *
  335. * %-EIO - I/O error.
  336. *
  337. * %-ENOMEM - Insufficient amount of memory available.
  338. *
  339. * %-ENOENT - A block number associated with @vblocknr does not exist.
  340. */
  341. int nilfs_dat_translate(struct inode *dat, __u64 vblocknr, sector_t *blocknrp)
  342. {
  343. struct buffer_head *entry_bh, *bh;
  344. struct nilfs_dat_entry *entry;
  345. sector_t blocknr;
  346. void *kaddr;
  347. int ret;
  348. ret = nilfs_palloc_get_entry_block(dat, vblocknr, 0, &entry_bh);
  349. if (ret < 0)
  350. return ret;
  351. if (!nilfs_doing_gc() && buffer_nilfs_redirected(entry_bh)) {
  352. bh = nilfs_mdt_get_frozen_buffer(dat, entry_bh);
  353. if (bh) {
  354. WARN_ON(!buffer_uptodate(bh));
  355. brelse(entry_bh);
  356. entry_bh = bh;
  357. }
  358. }
  359. kaddr = kmap_atomic(entry_bh->b_page);
  360. entry = nilfs_palloc_block_get_entry(dat, vblocknr, entry_bh, kaddr);
  361. blocknr = le64_to_cpu(entry->de_blocknr);
  362. if (blocknr == 0) {
  363. ret = -ENOENT;
  364. goto out;
  365. }
  366. *blocknrp = blocknr;
  367. out:
  368. kunmap_atomic(kaddr);
  369. brelse(entry_bh);
  370. return ret;
  371. }
  372. ssize_t nilfs_dat_get_vinfo(struct inode *dat, void *buf, unsigned visz,
  373. size_t nvi)
  374. {
  375. struct buffer_head *entry_bh;
  376. struct nilfs_dat_entry *entry;
  377. struct nilfs_vinfo *vinfo = buf;
  378. __u64 first, last;
  379. void *kaddr;
  380. unsigned long entries_per_block = NILFS_MDT(dat)->mi_entries_per_block;
  381. int i, j, n, ret;
  382. for (i = 0; i < nvi; i += n) {
  383. ret = nilfs_palloc_get_entry_block(dat, vinfo->vi_vblocknr,
  384. 0, &entry_bh);
  385. if (ret < 0)
  386. return ret;
  387. kaddr = kmap_atomic(entry_bh->b_page);
  388. /* last virtual block number in this block */
  389. first = vinfo->vi_vblocknr;
  390. do_div(first, entries_per_block);
  391. first *= entries_per_block;
  392. last = first + entries_per_block - 1;
  393. for (j = i, n = 0;
  394. j < nvi && vinfo->vi_vblocknr >= first &&
  395. vinfo->vi_vblocknr <= last;
  396. j++, n++, vinfo = (void *)vinfo + visz) {
  397. entry = nilfs_palloc_block_get_entry(
  398. dat, vinfo->vi_vblocknr, entry_bh, kaddr);
  399. vinfo->vi_start = le64_to_cpu(entry->de_start);
  400. vinfo->vi_end = le64_to_cpu(entry->de_end);
  401. vinfo->vi_blocknr = le64_to_cpu(entry->de_blocknr);
  402. }
  403. kunmap_atomic(kaddr);
  404. brelse(entry_bh);
  405. }
  406. return nvi;
  407. }
  408. /**
  409. * nilfs_dat_read - read or get dat inode
  410. * @sb: super block instance
  411. * @entry_size: size of a dat entry
  412. * @raw_inode: on-disk dat inode
  413. * @inodep: buffer to store the inode
  414. */
  415. int nilfs_dat_read(struct super_block *sb, size_t entry_size,
  416. struct nilfs_inode *raw_inode, struct inode **inodep)
  417. {
  418. static struct lock_class_key dat_lock_key;
  419. struct inode *dat;
  420. struct nilfs_dat_info *di;
  421. int err;
  422. dat = nilfs_iget_locked(sb, NULL, NILFS_DAT_INO);
  423. if (unlikely(!dat))
  424. return -ENOMEM;
  425. if (!(dat->i_state & I_NEW))
  426. goto out;
  427. err = nilfs_mdt_init(dat, NILFS_MDT_GFP, sizeof(*di));
  428. if (err)
  429. goto failed;
  430. err = nilfs_palloc_init_blockgroup(dat, entry_size);
  431. if (err)
  432. goto failed;
  433. di = NILFS_DAT_I(dat);
  434. lockdep_set_class(&di->mi.mi_sem, &dat_lock_key);
  435. nilfs_palloc_setup_cache(dat, &di->palloc_cache);
  436. nilfs_mdt_setup_shadow_map(dat, &di->shadow);
  437. err = nilfs_read_inode_common(dat, raw_inode);
  438. if (err)
  439. goto failed;
  440. unlock_new_inode(dat);
  441. out:
  442. *inodep = dat;
  443. return 0;
  444. failed:
  445. iget_failed(dat);
  446. return err;
  447. }