direct.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * direct.c - NILFS direct block pointer.
  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/errno.h>
  23. #include "nilfs.h"
  24. #include "page.h"
  25. #include "direct.h"
  26. #include "alloc.h"
  27. #include "dat.h"
  28. static inline __le64 *nilfs_direct_dptrs(const struct nilfs_direct *direct)
  29. {
  30. return (__le64 *)
  31. ((struct nilfs_direct_node *)direct->d_bmap.b_u.u_data + 1);
  32. }
  33. static inline __u64
  34. nilfs_direct_get_ptr(const struct nilfs_direct *direct, __u64 key)
  35. {
  36. return nilfs_bmap_dptr_to_ptr(*(nilfs_direct_dptrs(direct) + key));
  37. }
  38. static inline void nilfs_direct_set_ptr(struct nilfs_direct *direct,
  39. __u64 key, __u64 ptr)
  40. {
  41. *(nilfs_direct_dptrs(direct) + key) = nilfs_bmap_ptr_to_dptr(ptr);
  42. }
  43. static int nilfs_direct_lookup(const struct nilfs_bmap *bmap,
  44. __u64 key, int level, __u64 *ptrp)
  45. {
  46. struct nilfs_direct *direct;
  47. __u64 ptr;
  48. direct = (struct nilfs_direct *)bmap;
  49. if ((key > NILFS_DIRECT_KEY_MAX) ||
  50. (level != 1) || /* XXX: use macro for level 1 */
  51. ((ptr = nilfs_direct_get_ptr(direct, key)) ==
  52. NILFS_BMAP_INVALID_PTR))
  53. return -ENOENT;
  54. if (ptrp != NULL)
  55. *ptrp = ptr;
  56. return 0;
  57. }
  58. static int nilfs_direct_lookup_contig(const struct nilfs_bmap *bmap,
  59. __u64 key, __u64 *ptrp,
  60. unsigned maxblocks)
  61. {
  62. struct nilfs_direct *direct = (struct nilfs_direct *)bmap;
  63. struct inode *dat = NULL;
  64. __u64 ptr, ptr2;
  65. sector_t blocknr;
  66. int ret, cnt;
  67. if (key > NILFS_DIRECT_KEY_MAX ||
  68. (ptr = nilfs_direct_get_ptr(direct, key)) ==
  69. NILFS_BMAP_INVALID_PTR)
  70. return -ENOENT;
  71. if (NILFS_BMAP_USE_VBN(bmap)) {
  72. dat = nilfs_bmap_get_dat(bmap);
  73. ret = nilfs_dat_translate(dat, ptr, &blocknr);
  74. if (ret < 0)
  75. return ret;
  76. ptr = blocknr;
  77. }
  78. maxblocks = min_t(unsigned, maxblocks, NILFS_DIRECT_KEY_MAX - key + 1);
  79. for (cnt = 1; cnt < maxblocks &&
  80. (ptr2 = nilfs_direct_get_ptr(direct, key + cnt)) !=
  81. NILFS_BMAP_INVALID_PTR;
  82. cnt++) {
  83. if (dat) {
  84. ret = nilfs_dat_translate(dat, ptr2, &blocknr);
  85. if (ret < 0)
  86. return ret;
  87. ptr2 = blocknr;
  88. }
  89. if (ptr2 != ptr + cnt)
  90. break;
  91. }
  92. *ptrp = ptr;
  93. return cnt;
  94. }
  95. static __u64
  96. nilfs_direct_find_target_v(const struct nilfs_direct *direct, __u64 key)
  97. {
  98. __u64 ptr;
  99. ptr = nilfs_bmap_find_target_seq(&direct->d_bmap, key);
  100. if (ptr != NILFS_BMAP_INVALID_PTR)
  101. /* sequential access */
  102. return ptr;
  103. else
  104. /* block group */
  105. return nilfs_bmap_find_target_in_group(&direct->d_bmap);
  106. }
  107. static void nilfs_direct_set_target_v(struct nilfs_direct *direct,
  108. __u64 key, __u64 ptr)
  109. {
  110. direct->d_bmap.b_last_allocated_key = key;
  111. direct->d_bmap.b_last_allocated_ptr = ptr;
  112. }
  113. static int nilfs_direct_prepare_insert(struct nilfs_direct *direct,
  114. __u64 key,
  115. union nilfs_bmap_ptr_req *req,
  116. struct nilfs_bmap_stats *stats)
  117. {
  118. int ret;
  119. if (NILFS_BMAP_USE_VBN(&direct->d_bmap))
  120. req->bpr_ptr = nilfs_direct_find_target_v(direct, key);
  121. ret = nilfs_bmap_prepare_alloc_ptr(&direct->d_bmap, req);
  122. if (ret < 0)
  123. return ret;
  124. stats->bs_nblocks = 1;
  125. return 0;
  126. }
  127. static void nilfs_direct_commit_insert(struct nilfs_direct *direct,
  128. union nilfs_bmap_ptr_req *req,
  129. __u64 key, __u64 ptr)
  130. {
  131. struct buffer_head *bh;
  132. /* ptr must be a pointer to a buffer head. */
  133. bh = (struct buffer_head *)((unsigned long)ptr);
  134. set_buffer_nilfs_volatile(bh);
  135. nilfs_bmap_commit_alloc_ptr(&direct->d_bmap, req);
  136. nilfs_direct_set_ptr(direct, key, req->bpr_ptr);
  137. if (!nilfs_bmap_dirty(&direct->d_bmap))
  138. nilfs_bmap_set_dirty(&direct->d_bmap);
  139. if (NILFS_BMAP_USE_VBN(&direct->d_bmap))
  140. nilfs_direct_set_target_v(direct, key, req->bpr_ptr);
  141. }
  142. static int nilfs_direct_insert(struct nilfs_bmap *bmap, __u64 key, __u64 ptr)
  143. {
  144. struct nilfs_direct *direct;
  145. union nilfs_bmap_ptr_req req;
  146. struct nilfs_bmap_stats stats;
  147. int ret;
  148. direct = (struct nilfs_direct *)bmap;
  149. if (key > NILFS_DIRECT_KEY_MAX)
  150. return -ENOENT;
  151. if (nilfs_direct_get_ptr(direct, key) != NILFS_BMAP_INVALID_PTR)
  152. return -EEXIST;
  153. ret = nilfs_direct_prepare_insert(direct, key, &req, &stats);
  154. if (ret < 0)
  155. return ret;
  156. nilfs_direct_commit_insert(direct, &req, key, ptr);
  157. nilfs_bmap_add_blocks(bmap, stats.bs_nblocks);
  158. return 0;
  159. }
  160. static int nilfs_direct_prepare_delete(struct nilfs_direct *direct,
  161. union nilfs_bmap_ptr_req *req,
  162. __u64 key,
  163. struct nilfs_bmap_stats *stats)
  164. {
  165. int ret;
  166. req->bpr_ptr = nilfs_direct_get_ptr(direct, key);
  167. ret = nilfs_bmap_prepare_end_ptr(&direct->d_bmap, req);
  168. if (!ret)
  169. stats->bs_nblocks = 1;
  170. return ret;
  171. }
  172. static void nilfs_direct_commit_delete(struct nilfs_direct *direct,
  173. union nilfs_bmap_ptr_req *req,
  174. __u64 key)
  175. {
  176. nilfs_bmap_commit_end_ptr(&direct->d_bmap, req);
  177. nilfs_direct_set_ptr(direct, key, NILFS_BMAP_INVALID_PTR);
  178. }
  179. static int nilfs_direct_delete(struct nilfs_bmap *bmap, __u64 key)
  180. {
  181. struct nilfs_direct *direct;
  182. union nilfs_bmap_ptr_req req;
  183. struct nilfs_bmap_stats stats;
  184. int ret;
  185. direct = (struct nilfs_direct *)bmap;
  186. if ((key > NILFS_DIRECT_KEY_MAX) ||
  187. nilfs_direct_get_ptr(direct, key) == NILFS_BMAP_INVALID_PTR)
  188. return -ENOENT;
  189. ret = nilfs_direct_prepare_delete(direct, &req, key, &stats);
  190. if (ret < 0)
  191. return ret;
  192. nilfs_direct_commit_delete(direct, &req, key);
  193. nilfs_bmap_sub_blocks(bmap, stats.bs_nblocks);
  194. return 0;
  195. }
  196. static int nilfs_direct_last_key(const struct nilfs_bmap *bmap, __u64 *keyp)
  197. {
  198. struct nilfs_direct *direct;
  199. __u64 key, lastkey;
  200. direct = (struct nilfs_direct *)bmap;
  201. lastkey = NILFS_DIRECT_KEY_MAX + 1;
  202. for (key = NILFS_DIRECT_KEY_MIN; key <= NILFS_DIRECT_KEY_MAX; key++)
  203. if (nilfs_direct_get_ptr(direct, key) !=
  204. NILFS_BMAP_INVALID_PTR)
  205. lastkey = key;
  206. if (lastkey == NILFS_DIRECT_KEY_MAX + 1)
  207. return -ENOENT;
  208. *keyp = lastkey;
  209. return 0;
  210. }
  211. static int nilfs_direct_check_insert(const struct nilfs_bmap *bmap, __u64 key)
  212. {
  213. return key > NILFS_DIRECT_KEY_MAX;
  214. }
  215. static int nilfs_direct_gather_data(struct nilfs_bmap *bmap,
  216. __u64 *keys, __u64 *ptrs, int nitems)
  217. {
  218. struct nilfs_direct *direct;
  219. __u64 key;
  220. __u64 ptr;
  221. int n;
  222. direct = (struct nilfs_direct *)bmap;
  223. if (nitems > NILFS_DIRECT_NBLOCKS)
  224. nitems = NILFS_DIRECT_NBLOCKS;
  225. n = 0;
  226. for (key = 0; key < nitems; key++) {
  227. ptr = nilfs_direct_get_ptr(direct, key);
  228. if (ptr != NILFS_BMAP_INVALID_PTR) {
  229. keys[n] = key;
  230. ptrs[n] = ptr;
  231. n++;
  232. }
  233. }
  234. return n;
  235. }
  236. int nilfs_direct_delete_and_convert(struct nilfs_bmap *bmap,
  237. __u64 key, __u64 *keys, __u64 *ptrs, int n)
  238. {
  239. struct nilfs_direct *direct;
  240. __le64 *dptrs;
  241. int ret, i, j;
  242. /* no need to allocate any resource for conversion */
  243. /* delete */
  244. ret = bmap->b_ops->bop_delete(bmap, key);
  245. if (ret < 0)
  246. return ret;
  247. /* free resources */
  248. if (bmap->b_ops->bop_clear != NULL)
  249. bmap->b_ops->bop_clear(bmap);
  250. /* convert */
  251. direct = (struct nilfs_direct *)bmap;
  252. dptrs = nilfs_direct_dptrs(direct);
  253. for (i = 0, j = 0; i < NILFS_DIRECT_NBLOCKS; i++) {
  254. if ((j < n) && (i == keys[j])) {
  255. dptrs[i] = (i != key) ?
  256. nilfs_bmap_ptr_to_dptr(ptrs[j]) :
  257. NILFS_BMAP_INVALID_PTR;
  258. j++;
  259. } else
  260. dptrs[i] = NILFS_BMAP_INVALID_PTR;
  261. }
  262. nilfs_direct_init(bmap);
  263. return 0;
  264. }
  265. static int nilfs_direct_propagate_v(struct nilfs_direct *direct,
  266. struct buffer_head *bh)
  267. {
  268. union nilfs_bmap_ptr_req oldreq, newreq;
  269. __u64 key;
  270. __u64 ptr;
  271. int ret;
  272. key = nilfs_bmap_data_get_key(&direct->d_bmap, bh);
  273. ptr = nilfs_direct_get_ptr(direct, key);
  274. if (!buffer_nilfs_volatile(bh)) {
  275. oldreq.bpr_ptr = ptr;
  276. newreq.bpr_ptr = ptr;
  277. ret = nilfs_bmap_prepare_update_v(&direct->d_bmap, &oldreq,
  278. &newreq);
  279. if (ret < 0)
  280. return ret;
  281. nilfs_bmap_commit_update_v(&direct->d_bmap, &oldreq, &newreq);
  282. set_buffer_nilfs_volatile(bh);
  283. nilfs_direct_set_ptr(direct, key, newreq.bpr_ptr);
  284. } else
  285. ret = nilfs_bmap_mark_dirty(&direct->d_bmap, ptr);
  286. return ret;
  287. }
  288. static int nilfs_direct_propagate(const struct nilfs_bmap *bmap,
  289. struct buffer_head *bh)
  290. {
  291. struct nilfs_direct *direct = (struct nilfs_direct *)bmap;
  292. return NILFS_BMAP_USE_VBN(bmap) ?
  293. nilfs_direct_propagate_v(direct, bh) : 0;
  294. }
  295. static int nilfs_direct_assign_v(struct nilfs_direct *direct,
  296. __u64 key, __u64 ptr,
  297. struct buffer_head **bh,
  298. sector_t blocknr,
  299. union nilfs_binfo *binfo)
  300. {
  301. union nilfs_bmap_ptr_req req;
  302. int ret;
  303. req.bpr_ptr = ptr;
  304. ret = nilfs_bmap_start_v(&direct->d_bmap, &req, blocknr);
  305. if (unlikely(ret < 0))
  306. return ret;
  307. binfo->bi_v.bi_vblocknr = nilfs_bmap_ptr_to_dptr(ptr);
  308. binfo->bi_v.bi_blkoff = nilfs_bmap_key_to_dkey(key);
  309. return 0;
  310. }
  311. static int nilfs_direct_assign_p(struct nilfs_direct *direct,
  312. __u64 key, __u64 ptr,
  313. struct buffer_head **bh,
  314. sector_t blocknr,
  315. union nilfs_binfo *binfo)
  316. {
  317. nilfs_direct_set_ptr(direct, key, blocknr);
  318. binfo->bi_dat.bi_blkoff = nilfs_bmap_key_to_dkey(key);
  319. binfo->bi_dat.bi_level = 0;
  320. return 0;
  321. }
  322. static int nilfs_direct_assign(struct nilfs_bmap *bmap,
  323. struct buffer_head **bh,
  324. sector_t blocknr,
  325. union nilfs_binfo *binfo)
  326. {
  327. struct nilfs_direct *direct;
  328. __u64 key;
  329. __u64 ptr;
  330. direct = (struct nilfs_direct *)bmap;
  331. key = nilfs_bmap_data_get_key(bmap, *bh);
  332. if (unlikely(key > NILFS_DIRECT_KEY_MAX)) {
  333. printk(KERN_CRIT "%s: invalid key: %llu\n", __func__,
  334. (unsigned long long)key);
  335. return -EINVAL;
  336. }
  337. ptr = nilfs_direct_get_ptr(direct, key);
  338. if (unlikely(ptr == NILFS_BMAP_INVALID_PTR)) {
  339. printk(KERN_CRIT "%s: invalid pointer: %llu\n", __func__,
  340. (unsigned long long)ptr);
  341. return -EINVAL;
  342. }
  343. return NILFS_BMAP_USE_VBN(bmap) ?
  344. nilfs_direct_assign_v(direct, key, ptr, bh, blocknr, binfo) :
  345. nilfs_direct_assign_p(direct, key, ptr, bh, blocknr, binfo);
  346. }
  347. static const struct nilfs_bmap_operations nilfs_direct_ops = {
  348. .bop_lookup = nilfs_direct_lookup,
  349. .bop_lookup_contig = nilfs_direct_lookup_contig,
  350. .bop_insert = nilfs_direct_insert,
  351. .bop_delete = nilfs_direct_delete,
  352. .bop_clear = NULL,
  353. .bop_propagate = nilfs_direct_propagate,
  354. .bop_lookup_dirty_buffers = NULL,
  355. .bop_assign = nilfs_direct_assign,
  356. .bop_mark = NULL,
  357. .bop_last_key = nilfs_direct_last_key,
  358. .bop_check_insert = nilfs_direct_check_insert,
  359. .bop_check_delete = NULL,
  360. .bop_gather_data = nilfs_direct_gather_data,
  361. };
  362. int nilfs_direct_init(struct nilfs_bmap *bmap)
  363. {
  364. bmap->b_ops = &nilfs_direct_ops;
  365. return 0;
  366. }