direct.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * 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. goto dat_error;
  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. goto dat_error;
  72. ptr2 = blocknr;
  73. }
  74. if (ptr2 != ptr + cnt)
  75. break;
  76. }
  77. *ptrp = ptr;
  78. return cnt;
  79. dat_error:
  80. if (ret == -ENOENT)
  81. ret = -EINVAL; /* Notify bmap layer of metadata corruption */
  82. return ret;
  83. }
  84. static __u64
  85. nilfs_direct_find_target_v(const struct nilfs_bmap *direct, __u64 key)
  86. {
  87. __u64 ptr;
  88. ptr = nilfs_bmap_find_target_seq(direct, key);
  89. if (ptr != NILFS_BMAP_INVALID_PTR)
  90. /* sequential access */
  91. return ptr;
  92. /* block group */
  93. return nilfs_bmap_find_target_in_group(direct);
  94. }
  95. static int nilfs_direct_insert(struct nilfs_bmap *bmap, __u64 key, __u64 ptr)
  96. {
  97. union nilfs_bmap_ptr_req req;
  98. struct inode *dat = NULL;
  99. struct buffer_head *bh;
  100. int ret;
  101. if (key > NILFS_DIRECT_KEY_MAX)
  102. return -ENOENT;
  103. if (nilfs_direct_get_ptr(bmap, key) != NILFS_BMAP_INVALID_PTR)
  104. return -EEXIST;
  105. if (NILFS_BMAP_USE_VBN(bmap)) {
  106. req.bpr_ptr = nilfs_direct_find_target_v(bmap, key);
  107. dat = nilfs_bmap_get_dat(bmap);
  108. }
  109. ret = nilfs_bmap_prepare_alloc_ptr(bmap, &req, dat);
  110. if (!ret) {
  111. /* ptr must be a pointer to a buffer head. */
  112. bh = (struct buffer_head *)((unsigned long)ptr);
  113. set_buffer_nilfs_volatile(bh);
  114. nilfs_bmap_commit_alloc_ptr(bmap, &req, dat);
  115. nilfs_direct_set_ptr(bmap, key, req.bpr_ptr);
  116. if (!nilfs_bmap_dirty(bmap))
  117. nilfs_bmap_set_dirty(bmap);
  118. if (NILFS_BMAP_USE_VBN(bmap))
  119. nilfs_bmap_set_target_v(bmap, key, req.bpr_ptr);
  120. nilfs_inode_add_blocks(bmap->b_inode, 1);
  121. }
  122. return ret;
  123. }
  124. static int nilfs_direct_delete(struct nilfs_bmap *bmap, __u64 key)
  125. {
  126. union nilfs_bmap_ptr_req req;
  127. struct inode *dat;
  128. int ret;
  129. if (key > NILFS_DIRECT_KEY_MAX ||
  130. nilfs_direct_get_ptr(bmap, key) == NILFS_BMAP_INVALID_PTR)
  131. return -ENOENT;
  132. dat = NILFS_BMAP_USE_VBN(bmap) ? nilfs_bmap_get_dat(bmap) : NULL;
  133. req.bpr_ptr = nilfs_direct_get_ptr(bmap, key);
  134. ret = nilfs_bmap_prepare_end_ptr(bmap, &req, dat);
  135. if (!ret) {
  136. nilfs_bmap_commit_end_ptr(bmap, &req, dat);
  137. nilfs_direct_set_ptr(bmap, key, NILFS_BMAP_INVALID_PTR);
  138. nilfs_inode_sub_blocks(bmap->b_inode, 1);
  139. }
  140. return ret;
  141. }
  142. static int nilfs_direct_seek_key(const struct nilfs_bmap *direct, __u64 start,
  143. __u64 *keyp)
  144. {
  145. __u64 key;
  146. for (key = start; key <= NILFS_DIRECT_KEY_MAX; key++) {
  147. if (nilfs_direct_get_ptr(direct, key) !=
  148. NILFS_BMAP_INVALID_PTR) {
  149. *keyp = key;
  150. return 0;
  151. }
  152. }
  153. return -ENOENT;
  154. }
  155. static int nilfs_direct_last_key(const struct nilfs_bmap *direct, __u64 *keyp)
  156. {
  157. __u64 key, lastkey;
  158. lastkey = NILFS_DIRECT_KEY_MAX + 1;
  159. for (key = NILFS_DIRECT_KEY_MIN; key <= NILFS_DIRECT_KEY_MAX; key++)
  160. if (nilfs_direct_get_ptr(direct, key) !=
  161. NILFS_BMAP_INVALID_PTR)
  162. lastkey = key;
  163. if (lastkey == NILFS_DIRECT_KEY_MAX + 1)
  164. return -ENOENT;
  165. *keyp = lastkey;
  166. return 0;
  167. }
  168. static int nilfs_direct_check_insert(const struct nilfs_bmap *bmap, __u64 key)
  169. {
  170. return key > NILFS_DIRECT_KEY_MAX;
  171. }
  172. static int nilfs_direct_gather_data(struct nilfs_bmap *direct,
  173. __u64 *keys, __u64 *ptrs, int nitems)
  174. {
  175. __u64 key;
  176. __u64 ptr;
  177. int n;
  178. if (nitems > NILFS_DIRECT_NBLOCKS)
  179. nitems = NILFS_DIRECT_NBLOCKS;
  180. n = 0;
  181. for (key = 0; key < nitems; key++) {
  182. ptr = nilfs_direct_get_ptr(direct, key);
  183. if (ptr != NILFS_BMAP_INVALID_PTR) {
  184. keys[n] = key;
  185. ptrs[n] = ptr;
  186. n++;
  187. }
  188. }
  189. return n;
  190. }
  191. int nilfs_direct_delete_and_convert(struct nilfs_bmap *bmap,
  192. __u64 key, __u64 *keys, __u64 *ptrs, int n)
  193. {
  194. __le64 *dptrs;
  195. int ret, i, j;
  196. /* no need to allocate any resource for conversion */
  197. /* delete */
  198. ret = bmap->b_ops->bop_delete(bmap, key);
  199. if (ret < 0)
  200. return ret;
  201. /* free resources */
  202. if (bmap->b_ops->bop_clear != NULL)
  203. bmap->b_ops->bop_clear(bmap);
  204. /* convert */
  205. dptrs = nilfs_direct_dptrs(bmap);
  206. for (i = 0, j = 0; i < NILFS_DIRECT_NBLOCKS; i++) {
  207. if ((j < n) && (i == keys[j])) {
  208. dptrs[i] = (i != key) ?
  209. cpu_to_le64(ptrs[j]) :
  210. NILFS_BMAP_INVALID_PTR;
  211. j++;
  212. } else
  213. dptrs[i] = NILFS_BMAP_INVALID_PTR;
  214. }
  215. nilfs_direct_init(bmap);
  216. return 0;
  217. }
  218. static int nilfs_direct_propagate(struct nilfs_bmap *bmap,
  219. struct buffer_head *bh)
  220. {
  221. struct nilfs_palloc_req oldreq, newreq;
  222. struct inode *dat;
  223. __u64 key;
  224. __u64 ptr;
  225. int ret;
  226. if (!NILFS_BMAP_USE_VBN(bmap))
  227. return 0;
  228. dat = nilfs_bmap_get_dat(bmap);
  229. key = nilfs_bmap_data_get_key(bmap, bh);
  230. ptr = nilfs_direct_get_ptr(bmap, key);
  231. if (ptr == NILFS_BMAP_INVALID_PTR)
  232. return -EINVAL;
  233. if (!buffer_nilfs_volatile(bh)) {
  234. oldreq.pr_entry_nr = ptr;
  235. newreq.pr_entry_nr = ptr;
  236. ret = nilfs_dat_prepare_update(dat, &oldreq, &newreq);
  237. if (ret < 0)
  238. return ret;
  239. nilfs_dat_commit_update(dat, &oldreq, &newreq,
  240. bmap->b_ptr_type == NILFS_BMAP_PTR_VS);
  241. set_buffer_nilfs_volatile(bh);
  242. nilfs_direct_set_ptr(bmap, key, newreq.pr_entry_nr);
  243. } else
  244. ret = nilfs_dat_mark_dirty(dat, ptr);
  245. return ret;
  246. }
  247. static int nilfs_direct_assign_v(struct nilfs_bmap *direct,
  248. __u64 key, __u64 ptr,
  249. struct buffer_head **bh,
  250. sector_t blocknr,
  251. union nilfs_binfo *binfo)
  252. {
  253. struct inode *dat = nilfs_bmap_get_dat(direct);
  254. union nilfs_bmap_ptr_req req;
  255. int ret;
  256. req.bpr_ptr = ptr;
  257. ret = nilfs_dat_prepare_start(dat, &req.bpr_req);
  258. if (!ret) {
  259. nilfs_dat_commit_start(dat, &req.bpr_req, blocknr);
  260. binfo->bi_v.bi_vblocknr = cpu_to_le64(ptr);
  261. binfo->bi_v.bi_blkoff = cpu_to_le64(key);
  262. }
  263. return ret;
  264. }
  265. static int nilfs_direct_assign_p(struct nilfs_bmap *direct,
  266. __u64 key, __u64 ptr,
  267. struct buffer_head **bh,
  268. sector_t blocknr,
  269. union nilfs_binfo *binfo)
  270. {
  271. nilfs_direct_set_ptr(direct, key, blocknr);
  272. binfo->bi_dat.bi_blkoff = cpu_to_le64(key);
  273. binfo->bi_dat.bi_level = 0;
  274. memset(binfo->bi_dat.bi_pad, 0, sizeof(binfo->bi_dat.bi_pad));
  275. return 0;
  276. }
  277. static int nilfs_direct_assign(struct nilfs_bmap *bmap,
  278. struct buffer_head **bh,
  279. sector_t blocknr,
  280. union nilfs_binfo *binfo)
  281. {
  282. __u64 key;
  283. __u64 ptr;
  284. key = nilfs_bmap_data_get_key(bmap, *bh);
  285. if (unlikely(key > NILFS_DIRECT_KEY_MAX)) {
  286. nilfs_crit(bmap->b_inode->i_sb,
  287. "%s (ino=%lu): invalid key: %llu",
  288. __func__,
  289. bmap->b_inode->i_ino, (unsigned long long)key);
  290. return -EINVAL;
  291. }
  292. ptr = nilfs_direct_get_ptr(bmap, key);
  293. if (unlikely(ptr == NILFS_BMAP_INVALID_PTR)) {
  294. nilfs_crit(bmap->b_inode->i_sb,
  295. "%s (ino=%lu): invalid pointer: %llu",
  296. __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. }