direct.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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_insert(struct nilfs_bmap *bmap, __u64 key, __u64 ptr)
  114. {
  115. struct nilfs_direct *direct = (struct nilfs_direct *)bmap;
  116. union nilfs_bmap_ptr_req req;
  117. struct inode *dat = NULL;
  118. struct buffer_head *bh;
  119. int ret;
  120. if (key > NILFS_DIRECT_KEY_MAX)
  121. return -ENOENT;
  122. if (nilfs_direct_get_ptr(direct, key) != NILFS_BMAP_INVALID_PTR)
  123. return -EEXIST;
  124. if (NILFS_BMAP_USE_VBN(bmap)) {
  125. req.bpr_ptr = nilfs_direct_find_target_v(direct, key);
  126. dat = nilfs_bmap_get_dat(bmap);
  127. }
  128. ret = nilfs_bmap_prepare_alloc_ptr(bmap, &req, dat);
  129. if (!ret) {
  130. /* ptr must be a pointer to a buffer head. */
  131. bh = (struct buffer_head *)((unsigned long)ptr);
  132. set_buffer_nilfs_volatile(bh);
  133. nilfs_bmap_commit_alloc_ptr(bmap, &req, dat);
  134. nilfs_direct_set_ptr(direct, key, req.bpr_ptr);
  135. if (!nilfs_bmap_dirty(bmap))
  136. nilfs_bmap_set_dirty(bmap);
  137. if (NILFS_BMAP_USE_VBN(bmap))
  138. nilfs_direct_set_target_v(direct, key, req.bpr_ptr);
  139. nilfs_bmap_add_blocks(bmap, 1);
  140. }
  141. return ret;
  142. }
  143. static int nilfs_direct_delete(struct nilfs_bmap *bmap, __u64 key)
  144. {
  145. struct nilfs_direct *direct = (struct nilfs_direct *)bmap;
  146. union nilfs_bmap_ptr_req req;
  147. struct inode *dat;
  148. int ret;
  149. if (key > NILFS_DIRECT_KEY_MAX ||
  150. nilfs_direct_get_ptr(direct, key) == NILFS_BMAP_INVALID_PTR)
  151. return -ENOENT;
  152. dat = NILFS_BMAP_USE_VBN(bmap) ? nilfs_bmap_get_dat(bmap) : NULL;
  153. req.bpr_ptr = nilfs_direct_get_ptr(direct, key);
  154. ret = nilfs_bmap_prepare_end_ptr(bmap, &req, dat);
  155. if (!ret) {
  156. nilfs_bmap_commit_end_ptr(bmap, &req, dat);
  157. nilfs_direct_set_ptr(direct, key, NILFS_BMAP_INVALID_PTR);
  158. nilfs_bmap_sub_blocks(bmap, 1);
  159. }
  160. return ret;
  161. }
  162. static int nilfs_direct_last_key(const struct nilfs_bmap *bmap, __u64 *keyp)
  163. {
  164. struct nilfs_direct *direct;
  165. __u64 key, lastkey;
  166. direct = (struct nilfs_direct *)bmap;
  167. lastkey = NILFS_DIRECT_KEY_MAX + 1;
  168. for (key = NILFS_DIRECT_KEY_MIN; key <= NILFS_DIRECT_KEY_MAX; key++)
  169. if (nilfs_direct_get_ptr(direct, key) !=
  170. NILFS_BMAP_INVALID_PTR)
  171. lastkey = key;
  172. if (lastkey == NILFS_DIRECT_KEY_MAX + 1)
  173. return -ENOENT;
  174. *keyp = lastkey;
  175. return 0;
  176. }
  177. static int nilfs_direct_check_insert(const struct nilfs_bmap *bmap, __u64 key)
  178. {
  179. return key > NILFS_DIRECT_KEY_MAX;
  180. }
  181. static int nilfs_direct_gather_data(struct nilfs_bmap *bmap,
  182. __u64 *keys, __u64 *ptrs, int nitems)
  183. {
  184. struct nilfs_direct *direct;
  185. __u64 key;
  186. __u64 ptr;
  187. int n;
  188. direct = (struct nilfs_direct *)bmap;
  189. if (nitems > NILFS_DIRECT_NBLOCKS)
  190. nitems = NILFS_DIRECT_NBLOCKS;
  191. n = 0;
  192. for (key = 0; key < nitems; key++) {
  193. ptr = nilfs_direct_get_ptr(direct, key);
  194. if (ptr != NILFS_BMAP_INVALID_PTR) {
  195. keys[n] = key;
  196. ptrs[n] = ptr;
  197. n++;
  198. }
  199. }
  200. return n;
  201. }
  202. int nilfs_direct_delete_and_convert(struct nilfs_bmap *bmap,
  203. __u64 key, __u64 *keys, __u64 *ptrs, int n)
  204. {
  205. struct nilfs_direct *direct;
  206. __le64 *dptrs;
  207. int ret, i, j;
  208. /* no need to allocate any resource for conversion */
  209. /* delete */
  210. ret = bmap->b_ops->bop_delete(bmap, key);
  211. if (ret < 0)
  212. return ret;
  213. /* free resources */
  214. if (bmap->b_ops->bop_clear != NULL)
  215. bmap->b_ops->bop_clear(bmap);
  216. /* convert */
  217. direct = (struct nilfs_direct *)bmap;
  218. dptrs = nilfs_direct_dptrs(direct);
  219. for (i = 0, j = 0; i < NILFS_DIRECT_NBLOCKS; i++) {
  220. if ((j < n) && (i == keys[j])) {
  221. dptrs[i] = (i != key) ?
  222. nilfs_bmap_ptr_to_dptr(ptrs[j]) :
  223. NILFS_BMAP_INVALID_PTR;
  224. j++;
  225. } else
  226. dptrs[i] = NILFS_BMAP_INVALID_PTR;
  227. }
  228. nilfs_direct_init(bmap);
  229. return 0;
  230. }
  231. static int nilfs_direct_propagate(const struct nilfs_bmap *bmap,
  232. struct buffer_head *bh)
  233. {
  234. struct nilfs_direct *direct = (struct nilfs_direct *)bmap;
  235. struct nilfs_palloc_req oldreq, newreq;
  236. struct inode *dat;
  237. __u64 key;
  238. __u64 ptr;
  239. int ret;
  240. if (!NILFS_BMAP_USE_VBN(bmap))
  241. return 0;
  242. dat = nilfs_bmap_get_dat(bmap);
  243. key = nilfs_bmap_data_get_key(bmap, bh);
  244. ptr = nilfs_direct_get_ptr(direct, key);
  245. if (!buffer_nilfs_volatile(bh)) {
  246. oldreq.pr_entry_nr = ptr;
  247. newreq.pr_entry_nr = ptr;
  248. ret = nilfs_dat_prepare_update(dat, &oldreq, &newreq);
  249. if (ret < 0)
  250. return ret;
  251. nilfs_dat_commit_update(dat, &oldreq, &newreq,
  252. bmap->b_ptr_type == NILFS_BMAP_PTR_VS);
  253. set_buffer_nilfs_volatile(bh);
  254. nilfs_direct_set_ptr(direct, key, newreq.pr_entry_nr);
  255. } else
  256. ret = nilfs_dat_mark_dirty(dat, ptr);
  257. return ret;
  258. }
  259. static int nilfs_direct_assign_v(struct nilfs_direct *direct,
  260. __u64 key, __u64 ptr,
  261. struct buffer_head **bh,
  262. sector_t blocknr,
  263. union nilfs_binfo *binfo)
  264. {
  265. struct inode *dat = nilfs_bmap_get_dat(&direct->d_bmap);
  266. union nilfs_bmap_ptr_req req;
  267. int ret;
  268. req.bpr_ptr = ptr;
  269. ret = nilfs_dat_prepare_start(dat, &req.bpr_req);
  270. if (!ret) {
  271. nilfs_dat_commit_start(dat, &req.bpr_req, blocknr);
  272. binfo->bi_v.bi_vblocknr = nilfs_bmap_ptr_to_dptr(ptr);
  273. binfo->bi_v.bi_blkoff = nilfs_bmap_key_to_dkey(key);
  274. }
  275. return ret;
  276. }
  277. static int nilfs_direct_assign_p(struct nilfs_direct *direct,
  278. __u64 key, __u64 ptr,
  279. struct buffer_head **bh,
  280. sector_t blocknr,
  281. union nilfs_binfo *binfo)
  282. {
  283. nilfs_direct_set_ptr(direct, key, blocknr);
  284. binfo->bi_dat.bi_blkoff = nilfs_bmap_key_to_dkey(key);
  285. binfo->bi_dat.bi_level = 0;
  286. return 0;
  287. }
  288. static int nilfs_direct_assign(struct nilfs_bmap *bmap,
  289. struct buffer_head **bh,
  290. sector_t blocknr,
  291. union nilfs_binfo *binfo)
  292. {
  293. struct nilfs_direct *direct;
  294. __u64 key;
  295. __u64 ptr;
  296. direct = (struct nilfs_direct *)bmap;
  297. key = nilfs_bmap_data_get_key(bmap, *bh);
  298. if (unlikely(key > NILFS_DIRECT_KEY_MAX)) {
  299. printk(KERN_CRIT "%s: invalid key: %llu\n", __func__,
  300. (unsigned long long)key);
  301. return -EINVAL;
  302. }
  303. ptr = nilfs_direct_get_ptr(direct, key);
  304. if (unlikely(ptr == NILFS_BMAP_INVALID_PTR)) {
  305. printk(KERN_CRIT "%s: invalid pointer: %llu\n", __func__,
  306. (unsigned long long)ptr);
  307. return -EINVAL;
  308. }
  309. return NILFS_BMAP_USE_VBN(bmap) ?
  310. nilfs_direct_assign_v(direct, key, ptr, bh, blocknr, binfo) :
  311. nilfs_direct_assign_p(direct, key, ptr, bh, blocknr, binfo);
  312. }
  313. static const struct nilfs_bmap_operations nilfs_direct_ops = {
  314. .bop_lookup = nilfs_direct_lookup,
  315. .bop_lookup_contig = nilfs_direct_lookup_contig,
  316. .bop_insert = nilfs_direct_insert,
  317. .bop_delete = nilfs_direct_delete,
  318. .bop_clear = NULL,
  319. .bop_propagate = nilfs_direct_propagate,
  320. .bop_lookup_dirty_buffers = NULL,
  321. .bop_assign = nilfs_direct_assign,
  322. .bop_mark = NULL,
  323. .bop_last_key = nilfs_direct_last_key,
  324. .bop_check_insert = nilfs_direct_check_insert,
  325. .bop_check_delete = NULL,
  326. .bop_gather_data = nilfs_direct_gather_data,
  327. };
  328. int nilfs_direct_init(struct nilfs_bmap *bmap)
  329. {
  330. bmap->b_ops = &nilfs_direct_ops;
  331. return 0;
  332. }