dat.c 11 KB

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