direct.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * direct.c - NILFS direct block pointer.
  4. *
  5. * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
  6. *
  7. * Written by Koji Sato.
  8. */
  9. #include <linux/errno.h>
  10. #include "nilfs.h"
  11. #include "page.h"
  12. #include "direct.h"
  13. #include "alloc.h"
  14. #include "dat.h"
  15. static inline __le64 *nilfs_direct_dptrs(const struct nilfs_bmap *direct)
  16. {
  17. return (__le64 *)
  18. ((struct nilfs_direct_node *)direct->b_u.u_data + 1);
  19. }
  20. static inline __u64
  21. nilfs_direct_get_ptr(const struct nilfs_bmap *direct, __u64 key)
  22. {
  23. return le64_to_cpu(*(nilfs_direct_dptrs(direct) + key));
  24. }
  25. static inline void nilfs_direct_set_ptr(struct nilfs_bmap *direct,
  26. __u64 key, __u64 ptr)
  27. {
  28. *(nilfs_direct_dptrs(direct) + key) = cpu_to_le64(ptr);
  29. }
  30. static int nilfs_direct_lookup(const struct nilfs_bmap *direct,
  31. __u64 key, int level, __u64 *ptrp)
  32. {
  33. __u64 ptr;
  34. if (key > NILFS_DIRECT_KEY_MAX || level != 1)
  35. return -ENOENT;
  36. ptr = nilfs_direct_get_ptr(direct, key);
  37. if (ptr == NILFS_BMAP_INVALID_PTR)
  38. return -ENOENT;
  39. *ptrp = ptr;
  40. return 0;
  41. }
  42. static int nilfs_direct_lookup_contig(const struct nilfs_bmap *direct,
  43. __u64 key, __u64 *ptrp,
  44. unsigned int maxblocks)
  45. {
  46. struct inode *dat = NULL;
  47. __u64 ptr, ptr2;
  48. sector_t blocknr;
  49. int ret, cnt;
  50. if (key > NILFS_DIRECT_KEY_MAX)
  51. return -ENOENT;
  52. ptr = nilfs_direct_get_ptr(direct, key);
  53. if (ptr == NILFS_BMAP_INVALID_PTR)
  54. return -ENOENT;
  55. if (NILFS_BMAP_USE_VBN(direct)) {
  56. dat = nilfs_bmap_get_dat(direct);
  57. ret = nilfs_dat_translate(dat, ptr, &blocknr);
  58. if (ret < 0)
  59. return ret;
  60. ptr = blocknr;
  61. }
  62. maxblocks = min_t(unsigned int, maxblocks,
  63. NILFS_DIRECT_KEY_MAX - key + 1);
  64. for (cnt = 1; cnt < maxblocks &&
  65. (ptr2 = nilfs_direct_get_ptr(direct, key + cnt)) !=
  66. NILFS_BMAP_INVALID_PTR;
  67. cnt++) {
  68. if (dat) {
  69. ret = nilfs_dat_translate(dat, ptr2, &blocknr);
  70. if (ret < 0)
  71. return ret;
  72. ptr2 = blocknr;
  73. }
  74. if (ptr2 != ptr + cnt)
  75. break;
  76. }
  77. *ptrp = ptr;
  78. return cnt;
  79. }
  80. static __u64
  81. nilfs_direct_find_target_v(const struct nilfs_bmap *direct, __u64 key)
  82. {
  83. __u64 ptr;
  84. ptr = nilfs_bmap_find_target_seq(direct, key);
  85. if (ptr != NILFS_BMAP_INVALID_PTR)
  86. /* sequential access */
  87. return ptr;
  88. /* block group */
  89. return nilfs_bmap_find_target_in_group(direct);
  90. }
  91. static int nilfs_direct_insert(struct nilfs_bmap *bmap, __u64 key, __u64 ptr)
  92. {
  93. union nilfs_bmap_ptr_req req;
  94. struct inode *dat = NULL;
  95. struct buffer_head *bh;
  96. int ret;
  97. if (key > NILFS_DIRECT_KEY_MAX)
  98. return -ENOENT;
  99. if (nilfs_direct_get_ptr(bmap, key) != NILFS_BMAP_INVALID_PTR)
  100. return -EEXIST;
  101. if (NILFS_BMAP_USE_VBN(bmap)) {
  102. req.bpr_ptr = nilfs_direct_find_target_v(bmap, key);
  103. dat = nilfs_bmap_get_dat(bmap);
  104. }
  105. ret = nilfs_bmap_prepare_alloc_ptr(bmap, &req, dat);
  106. if (!ret) {
  107. /* ptr must be a pointer to a buffer head. */
  108. bh = (struct buffer_head *)((unsigned long)ptr);
  109. set_buffer_nilfs_volatile(bh);
  110. nilfs_bmap_commit_alloc_ptr(bmap, &req, dat);
  111. nilfs_direct_set_ptr(bmap, key, req.bpr_ptr);
  112. if (!nilfs_bmap_dirty(bmap))
  113. nilfs_bmap_set_dirty(bmap);
  114. if (NILFS_BMAP_USE_VBN(bmap))
  115. nilfs_bmap_set_target_v(bmap, key, req.bpr_ptr);
  116. nilfs_inode_add_blocks(bmap->b_inode, 1);
  117. }
  118. return ret;
  119. }
  120. static int nilfs_direct_delete(struct nilfs_bmap *bmap, __u64 key)
  121. {
  122. union nilfs_bmap_ptr_req req;
  123. struct inode *dat;
  124. int ret;
  125. if (key > NILFS_DIRECT_KEY_MAX ||
  126. nilfs_direct_get_ptr(bmap, key) == NILFS_BMAP_INVALID_PTR)
  127. return -ENOENT;
  128. dat = NILFS_BMAP_USE_VBN(bmap) ? nilfs_bmap_get_dat(bmap) : NULL;
  129. req.bpr_ptr = nilfs_direct_get_ptr(bmap, key);
  130. ret = nilfs_bmap_prepare_end_ptr(bmap, &req, dat);
  131. if (!ret) {
  132. nilfs_bmap_commit_end_ptr(bmap, &req, dat);
  133. nilfs_direct_set_ptr(bmap, key, NILFS_BMAP_INVALID_PTR);
  134. nilfs_inode_sub_blocks(bmap->b_inode, 1);
  135. }
  136. return ret;
  137. }
  138. static int nilfs_direct_seek_key(const struct nilfs_bmap *direct, __u64 start,
  139. __u64 *keyp)
  140. {
  141. __u64 key;
  142. for (key = start; key <= NILFS_DIRECT_KEY_MAX; key++) {
  143. if (nilfs_direct_get_ptr(direct, key) !=
  144. NILFS_BMAP_INVALID_PTR) {
  145. *keyp = key;
  146. return 0;
  147. }
  148. }
  149. return -ENOENT;
  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. nilfs_crit(bmap->b_inode->i_sb,
  280. "%s (ino=%lu): invalid key: %llu",
  281. __func__,
  282. bmap->b_inode->i_ino, (unsigned long long)key);
  283. return -EINVAL;
  284. }
  285. ptr = nilfs_direct_get_ptr(bmap, key);
  286. if (unlikely(ptr == NILFS_BMAP_INVALID_PTR)) {
  287. nilfs_crit(bmap->b_inode->i_sb,
  288. "%s (ino=%lu): invalid pointer: %llu",
  289. __func__,
  290. bmap->b_inode->i_ino, (unsigned long long)ptr);
  291. return -EINVAL;
  292. }
  293. return NILFS_BMAP_USE_VBN(bmap) ?
  294. nilfs_direct_assign_v(bmap, key, ptr, bh, blocknr, binfo) :
  295. nilfs_direct_assign_p(bmap, key, ptr, bh, blocknr, binfo);
  296. }
  297. static const struct nilfs_bmap_operations nilfs_direct_ops = {
  298. .bop_lookup = nilfs_direct_lookup,
  299. .bop_lookup_contig = nilfs_direct_lookup_contig,
  300. .bop_insert = nilfs_direct_insert,
  301. .bop_delete = nilfs_direct_delete,
  302. .bop_clear = NULL,
  303. .bop_propagate = nilfs_direct_propagate,
  304. .bop_lookup_dirty_buffers = NULL,
  305. .bop_assign = nilfs_direct_assign,
  306. .bop_mark = NULL,
  307. .bop_seek_key = nilfs_direct_seek_key,
  308. .bop_last_key = nilfs_direct_last_key,
  309. .bop_check_insert = nilfs_direct_check_insert,
  310. .bop_check_delete = NULL,
  311. .bop_gather_data = nilfs_direct_gather_data,
  312. };
  313. int nilfs_direct_init(struct nilfs_bmap *bmap)
  314. {
  315. bmap->b_ops = &nilfs_direct_ops;
  316. return 0;
  317. }