direct.c 9.9 KB

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