dat.c 11 KB

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