dat.c 13 KB

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