direct.c 9.2 KB

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