dat.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. static int nilfs_dat_prepare_entry(struct inode *dat,
  33. struct nilfs_palloc_req *req, int create)
  34. {
  35. return nilfs_palloc_get_entry_block(dat, req->pr_entry_nr,
  36. create, &req->pr_entry_bh);
  37. }
  38. static void nilfs_dat_commit_entry(struct inode *dat,
  39. struct nilfs_palloc_req *req)
  40. {
  41. nilfs_mdt_mark_buffer_dirty(req->pr_entry_bh);
  42. nilfs_mdt_mark_dirty(dat);
  43. brelse(req->pr_entry_bh);
  44. }
  45. static void nilfs_dat_abort_entry(struct inode *dat,
  46. struct nilfs_palloc_req *req)
  47. {
  48. brelse(req->pr_entry_bh);
  49. }
  50. int nilfs_dat_prepare_alloc(struct inode *dat, struct nilfs_palloc_req *req)
  51. {
  52. int ret;
  53. ret = nilfs_palloc_prepare_alloc_entry(dat, req);
  54. if (ret < 0)
  55. return ret;
  56. ret = nilfs_dat_prepare_entry(dat, req, 1);
  57. if (ret < 0)
  58. nilfs_palloc_abort_alloc_entry(dat, req);
  59. return ret;
  60. }
  61. void nilfs_dat_commit_alloc(struct inode *dat, struct nilfs_palloc_req *req)
  62. {
  63. struct nilfs_dat_entry *entry;
  64. void *kaddr;
  65. kaddr = kmap_atomic(req->pr_entry_bh->b_page, KM_USER0);
  66. entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
  67. req->pr_entry_bh, kaddr);
  68. entry->de_start = cpu_to_le64(NILFS_CNO_MIN);
  69. entry->de_end = cpu_to_le64(NILFS_CNO_MAX);
  70. entry->de_blocknr = cpu_to_le64(0);
  71. kunmap_atomic(kaddr, KM_USER0);
  72. nilfs_palloc_commit_alloc_entry(dat, req);
  73. nilfs_dat_commit_entry(dat, req);
  74. }
  75. void nilfs_dat_abort_alloc(struct inode *dat, struct nilfs_palloc_req *req)
  76. {
  77. nilfs_dat_abort_entry(dat, req);
  78. nilfs_palloc_abort_alloc_entry(dat, req);
  79. }
  80. void nilfs_dat_commit_free(struct inode *dat, struct nilfs_palloc_req *req)
  81. {
  82. struct nilfs_dat_entry *entry;
  83. void *kaddr;
  84. kaddr = kmap_atomic(req->pr_entry_bh->b_page, KM_USER0);
  85. entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
  86. req->pr_entry_bh, kaddr);
  87. entry->de_start = cpu_to_le64(NILFS_CNO_MIN);
  88. entry->de_end = cpu_to_le64(NILFS_CNO_MIN);
  89. entry->de_blocknr = cpu_to_le64(0);
  90. kunmap_atomic(kaddr, KM_USER0);
  91. nilfs_dat_commit_entry(dat, req);
  92. nilfs_palloc_commit_free_entry(dat, req);
  93. }
  94. int nilfs_dat_prepare_start(struct inode *dat, struct nilfs_palloc_req *req)
  95. {
  96. int ret;
  97. ret = nilfs_dat_prepare_entry(dat, req, 0);
  98. WARN_ON(ret == -ENOENT);
  99. return ret;
  100. }
  101. void nilfs_dat_commit_start(struct inode *dat, struct nilfs_palloc_req *req,
  102. sector_t blocknr)
  103. {
  104. struct nilfs_dat_entry *entry;
  105. void *kaddr;
  106. kaddr = kmap_atomic(req->pr_entry_bh->b_page, KM_USER0);
  107. entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
  108. req->pr_entry_bh, kaddr);
  109. entry->de_start = cpu_to_le64(nilfs_mdt_cno(dat));
  110. entry->de_blocknr = cpu_to_le64(blocknr);
  111. kunmap_atomic(kaddr, KM_USER0);
  112. nilfs_dat_commit_entry(dat, req);
  113. }
  114. int nilfs_dat_prepare_end(struct inode *dat, struct nilfs_palloc_req *req)
  115. {
  116. struct nilfs_dat_entry *entry;
  117. __u64 start;
  118. sector_t blocknr;
  119. void *kaddr;
  120. int ret;
  121. ret = nilfs_dat_prepare_entry(dat, req, 0);
  122. if (ret < 0) {
  123. WARN_ON(ret == -ENOENT);
  124. return ret;
  125. }
  126. kaddr = kmap_atomic(req->pr_entry_bh->b_page, KM_USER0);
  127. entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
  128. req->pr_entry_bh, kaddr);
  129. start = le64_to_cpu(entry->de_start);
  130. blocknr = le64_to_cpu(entry->de_blocknr);
  131. kunmap_atomic(kaddr, KM_USER0);
  132. if (blocknr == 0) {
  133. ret = nilfs_palloc_prepare_free_entry(dat, req);
  134. if (ret < 0) {
  135. nilfs_dat_abort_entry(dat, req);
  136. return ret;
  137. }
  138. }
  139. return 0;
  140. }
  141. void nilfs_dat_commit_end(struct inode *dat, struct nilfs_palloc_req *req,
  142. int dead)
  143. {
  144. struct nilfs_dat_entry *entry;
  145. __u64 start, end;
  146. sector_t blocknr;
  147. void *kaddr;
  148. kaddr = kmap_atomic(req->pr_entry_bh->b_page, KM_USER0);
  149. entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
  150. req->pr_entry_bh, kaddr);
  151. end = start = le64_to_cpu(entry->de_start);
  152. if (!dead) {
  153. end = nilfs_mdt_cno(dat);
  154. WARN_ON(start > end);
  155. }
  156. entry->de_end = cpu_to_le64(end);
  157. blocknr = le64_to_cpu(entry->de_blocknr);
  158. kunmap_atomic(kaddr, KM_USER0);
  159. if (blocknr == 0)
  160. nilfs_dat_commit_free(dat, req);
  161. else
  162. nilfs_dat_commit_entry(dat, req);
  163. }
  164. void nilfs_dat_abort_end(struct inode *dat, struct nilfs_palloc_req *req)
  165. {
  166. struct nilfs_dat_entry *entry;
  167. __u64 start;
  168. sector_t blocknr;
  169. void *kaddr;
  170. kaddr = kmap_atomic(req->pr_entry_bh->b_page, KM_USER0);
  171. entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
  172. req->pr_entry_bh, kaddr);
  173. start = le64_to_cpu(entry->de_start);
  174. blocknr = le64_to_cpu(entry->de_blocknr);
  175. kunmap_atomic(kaddr, KM_USER0);
  176. if (start == nilfs_mdt_cno(dat) && blocknr == 0)
  177. nilfs_palloc_abort_free_entry(dat, req);
  178. nilfs_dat_abort_entry(dat, req);
  179. }
  180. int nilfs_dat_prepare_update(struct inode *dat,
  181. struct nilfs_palloc_req *oldreq,
  182. struct nilfs_palloc_req *newreq)
  183. {
  184. int ret;
  185. ret = nilfs_dat_prepare_end(dat, oldreq);
  186. if (!ret) {
  187. ret = nilfs_dat_prepare_alloc(dat, newreq);
  188. if (ret < 0)
  189. nilfs_dat_abort_end(dat, oldreq);
  190. }
  191. return ret;
  192. }
  193. void nilfs_dat_commit_update(struct inode *dat,
  194. struct nilfs_palloc_req *oldreq,
  195. struct nilfs_palloc_req *newreq, int dead)
  196. {
  197. nilfs_dat_commit_end(dat, oldreq, dead);
  198. nilfs_dat_commit_alloc(dat, newreq);
  199. }
  200. void nilfs_dat_abort_update(struct inode *dat,
  201. struct nilfs_palloc_req *oldreq,
  202. struct nilfs_palloc_req *newreq)
  203. {
  204. nilfs_dat_abort_end(dat, oldreq);
  205. nilfs_dat_abort_alloc(dat, newreq);
  206. }
  207. /**
  208. * nilfs_dat_mark_dirty -
  209. * @dat: DAT file inode
  210. * @vblocknr: virtual block number
  211. *
  212. * Description:
  213. *
  214. * Return Value: On success, 0 is returned. On error, one of the following
  215. * negative error codes is returned.
  216. *
  217. * %-EIO - I/O error.
  218. *
  219. * %-ENOMEM - Insufficient amount of memory available.
  220. */
  221. int nilfs_dat_mark_dirty(struct inode *dat, __u64 vblocknr)
  222. {
  223. struct nilfs_palloc_req req;
  224. int ret;
  225. req.pr_entry_nr = vblocknr;
  226. ret = nilfs_dat_prepare_entry(dat, &req, 0);
  227. if (ret == 0)
  228. nilfs_dat_commit_entry(dat, &req);
  229. return ret;
  230. }
  231. /**
  232. * nilfs_dat_freev - free virtual block numbers
  233. * @dat: DAT file inode
  234. * @vblocknrs: array of virtual block numbers
  235. * @nitems: number of virtual block numbers
  236. *
  237. * Description: nilfs_dat_freev() frees the virtual block numbers specified by
  238. * @vblocknrs and @nitems.
  239. *
  240. * Return Value: On success, 0 is returned. On error, one of the following
  241. * nagative error codes is returned.
  242. *
  243. * %-EIO - I/O error.
  244. *
  245. * %-ENOMEM - Insufficient amount of memory available.
  246. *
  247. * %-ENOENT - The virtual block number have not been allocated.
  248. */
  249. int nilfs_dat_freev(struct inode *dat, __u64 *vblocknrs, size_t nitems)
  250. {
  251. return nilfs_palloc_freev(dat, vblocknrs, nitems);
  252. }
  253. /**
  254. * nilfs_dat_move - change a block number
  255. * @dat: DAT file inode
  256. * @vblocknr: virtual block number
  257. * @blocknr: block number
  258. *
  259. * Description: nilfs_dat_move() changes the block number associated with
  260. * @vblocknr to @blocknr.
  261. *
  262. * Return Value: On success, 0 is returned. On error, one of the following
  263. * negative error codes is returned.
  264. *
  265. * %-EIO - I/O error.
  266. *
  267. * %-ENOMEM - Insufficient amount of memory available.
  268. */
  269. int nilfs_dat_move(struct inode *dat, __u64 vblocknr, sector_t blocknr)
  270. {
  271. struct buffer_head *entry_bh;
  272. struct nilfs_dat_entry *entry;
  273. void *kaddr;
  274. int ret;
  275. ret = nilfs_palloc_get_entry_block(dat, vblocknr, 0, &entry_bh);
  276. if (ret < 0)
  277. return ret;
  278. kaddr = kmap_atomic(entry_bh->b_page, KM_USER0);
  279. entry = nilfs_palloc_block_get_entry(dat, vblocknr, entry_bh, kaddr);
  280. if (unlikely(entry->de_blocknr == cpu_to_le64(0))) {
  281. printk(KERN_CRIT "%s: vbn = %llu, [%llu, %llu)\n", __func__,
  282. (unsigned long long)vblocknr,
  283. (unsigned long long)le64_to_cpu(entry->de_start),
  284. (unsigned long long)le64_to_cpu(entry->de_end));
  285. kunmap_atomic(kaddr, KM_USER0);
  286. brelse(entry_bh);
  287. return -EINVAL;
  288. }
  289. WARN_ON(blocknr == 0);
  290. entry->de_blocknr = cpu_to_le64(blocknr);
  291. kunmap_atomic(kaddr, KM_USER0);
  292. nilfs_mdt_mark_buffer_dirty(entry_bh);
  293. nilfs_mdt_mark_dirty(dat);
  294. brelse(entry_bh);
  295. return 0;
  296. }
  297. /**
  298. * nilfs_dat_translate - translate a virtual block number to a block number
  299. * @dat: DAT file inode
  300. * @vblocknr: virtual block number
  301. * @blocknrp: pointer to a block number
  302. *
  303. * Description: nilfs_dat_translate() maps the virtual block number @vblocknr
  304. * to the corresponding block number.
  305. *
  306. * Return Value: On success, 0 is returned and the block number associated
  307. * with @vblocknr is stored in the place pointed by @blocknrp. On error, one
  308. * of the following negative error codes is returned.
  309. *
  310. * %-EIO - I/O error.
  311. *
  312. * %-ENOMEM - Insufficient amount of memory available.
  313. *
  314. * %-ENOENT - A block number associated with @vblocknr does not exist.
  315. */
  316. int nilfs_dat_translate(struct inode *dat, __u64 vblocknr, sector_t *blocknrp)
  317. {
  318. struct buffer_head *entry_bh;
  319. struct nilfs_dat_entry *entry;
  320. sector_t blocknr;
  321. void *kaddr;
  322. int ret;
  323. ret = nilfs_palloc_get_entry_block(dat, vblocknr, 0, &entry_bh);
  324. if (ret < 0)
  325. return ret;
  326. kaddr = kmap_atomic(entry_bh->b_page, KM_USER0);
  327. entry = nilfs_palloc_block_get_entry(dat, vblocknr, entry_bh, kaddr);
  328. blocknr = le64_to_cpu(entry->de_blocknr);
  329. if (blocknr == 0) {
  330. ret = -ENOENT;
  331. goto out;
  332. }
  333. if (blocknrp != NULL)
  334. *blocknrp = blocknr;
  335. out:
  336. kunmap_atomic(kaddr, KM_USER0);
  337. brelse(entry_bh);
  338. return ret;
  339. }
  340. ssize_t nilfs_dat_get_vinfo(struct inode *dat, void *buf, unsigned visz,
  341. size_t nvi)
  342. {
  343. struct buffer_head *entry_bh;
  344. struct nilfs_dat_entry *entry;
  345. struct nilfs_vinfo *vinfo = buf;
  346. __u64 first, last;
  347. void *kaddr;
  348. unsigned long entries_per_block = NILFS_MDT(dat)->mi_entries_per_block;
  349. int i, j, n, ret;
  350. for (i = 0; i < nvi; i += n) {
  351. ret = nilfs_palloc_get_entry_block(dat, vinfo->vi_vblocknr,
  352. 0, &entry_bh);
  353. if (ret < 0)
  354. return ret;
  355. kaddr = kmap_atomic(entry_bh->b_page, KM_USER0);
  356. /* last virtual block number in this block */
  357. first = vinfo->vi_vblocknr;
  358. do_div(first, entries_per_block);
  359. first *= entries_per_block;
  360. last = first + entries_per_block - 1;
  361. for (j = i, n = 0;
  362. j < nvi && vinfo->vi_vblocknr >= first &&
  363. vinfo->vi_vblocknr <= last;
  364. j++, n++, vinfo = (void *)vinfo + visz) {
  365. entry = nilfs_palloc_block_get_entry(
  366. dat, vinfo->vi_vblocknr, entry_bh, kaddr);
  367. vinfo->vi_start = le64_to_cpu(entry->de_start);
  368. vinfo->vi_end = le64_to_cpu(entry->de_end);
  369. vinfo->vi_blocknr = le64_to_cpu(entry->de_blocknr);
  370. }
  371. kunmap_atomic(kaddr, KM_USER0);
  372. brelse(entry_bh);
  373. }
  374. return nvi;
  375. }