direct.c 8.9 KB

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