bmap.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * NILFS block mapping.
  4. *
  5. * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
  6. *
  7. * Written by Koji Sato.
  8. */
  9. #ifndef _NILFS_BMAP_H
  10. #define _NILFS_BMAP_H
  11. #include <linux/types.h>
  12. #include <linux/fs.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/nilfs2_ondisk.h> /* nilfs_binfo, nilfs_inode, etc */
  15. #include "alloc.h"
  16. #include "dat.h"
  17. #define NILFS_BMAP_INVALID_PTR 0
  18. #define nilfs_bmap_keydiff_abs(diff) ((diff) < 0 ? -(diff) : (diff))
  19. struct nilfs_bmap;
  20. /**
  21. * union nilfs_bmap_ptr_req - request for bmap ptr
  22. * @bpr_ptr: bmap pointer
  23. * @bpr_req: request for persistent allocator
  24. */
  25. union nilfs_bmap_ptr_req {
  26. __u64 bpr_ptr;
  27. struct nilfs_palloc_req bpr_req;
  28. };
  29. /**
  30. * struct nilfs_bmap_stats - bmap statistics
  31. * @bs_nblocks: number of blocks created or deleted
  32. */
  33. struct nilfs_bmap_stats {
  34. unsigned int bs_nblocks;
  35. };
  36. /**
  37. * struct nilfs_bmap_operations - bmap operation table
  38. * @bop_lookup: single block search operation
  39. * @bop_lookup_contig: consecutive block search operation
  40. * @bop_insert: block insertion operation
  41. * @bop_delete: block delete operation
  42. * @bop_clear: block mapping resource release operation
  43. * @bop_propagate: operation to propagate dirty state towards the
  44. * mapping root
  45. * @bop_lookup_dirty_buffers: operation to collect dirty block buffers
  46. * @bop_assign: disk block address assignment operation
  47. * @bop_mark: operation to mark in-use blocks as dirty for
  48. * relocation by GC
  49. * @bop_seek_key: find valid block key operation
  50. * @bop_last_key: find last valid block key operation
  51. */
  52. struct nilfs_bmap_operations {
  53. int (*bop_lookup)(const struct nilfs_bmap *, __u64, int, __u64 *);
  54. int (*bop_lookup_contig)(const struct nilfs_bmap *, __u64, __u64 *,
  55. unsigned int);
  56. int (*bop_insert)(struct nilfs_bmap *, __u64, __u64);
  57. int (*bop_delete)(struct nilfs_bmap *, __u64);
  58. void (*bop_clear)(struct nilfs_bmap *);
  59. int (*bop_propagate)(struct nilfs_bmap *, struct buffer_head *);
  60. void (*bop_lookup_dirty_buffers)(struct nilfs_bmap *,
  61. struct list_head *);
  62. int (*bop_assign)(struct nilfs_bmap *,
  63. struct buffer_head **,
  64. sector_t,
  65. union nilfs_binfo *);
  66. int (*bop_mark)(struct nilfs_bmap *, __u64, int);
  67. int (*bop_seek_key)(const struct nilfs_bmap *, __u64, __u64 *);
  68. int (*bop_last_key)(const struct nilfs_bmap *, __u64 *);
  69. /* private: internal use only */
  70. int (*bop_check_insert)(const struct nilfs_bmap *, __u64);
  71. int (*bop_check_delete)(struct nilfs_bmap *, __u64);
  72. int (*bop_gather_data)(struct nilfs_bmap *, __u64 *, __u64 *, int);
  73. };
  74. #define NILFS_BMAP_SIZE (NILFS_INODE_BMAP_SIZE * sizeof(__le64))
  75. #define NILFS_BMAP_KEY_BIT BITS_PER_LONG
  76. #define NILFS_BMAP_NEW_PTR_INIT (1UL << (BITS_PER_LONG - 1))
  77. static inline int nilfs_bmap_is_new_ptr(unsigned long ptr)
  78. {
  79. return !!(ptr & NILFS_BMAP_NEW_PTR_INIT);
  80. }
  81. /**
  82. * struct nilfs_bmap - bmap structure
  83. * @b_u: raw data
  84. * @b_sem: semaphore
  85. * @b_inode: owner of bmap
  86. * @b_ops: bmap operation table
  87. * @b_last_allocated_key: last allocated key for data block
  88. * @b_last_allocated_ptr: last allocated ptr for data block
  89. * @b_ptr_type: pointer type
  90. * @b_state: state
  91. * @b_nchildren_per_block: maximum number of child nodes for non-root nodes
  92. */
  93. struct nilfs_bmap {
  94. union {
  95. __u8 u_flags;
  96. __le64 u_data[NILFS_BMAP_SIZE / sizeof(__le64)];
  97. } b_u;
  98. struct rw_semaphore b_sem;
  99. struct inode *b_inode;
  100. const struct nilfs_bmap_operations *b_ops;
  101. __u64 b_last_allocated_key;
  102. __u64 b_last_allocated_ptr;
  103. int b_ptr_type;
  104. int b_state;
  105. __u16 b_nchildren_per_block;
  106. };
  107. /* pointer type */
  108. #define NILFS_BMAP_PTR_P 0 /* physical block number (i.e. LBN) */
  109. #define NILFS_BMAP_PTR_VS 1 /*
  110. * virtual block number (single
  111. * version)
  112. */
  113. #define NILFS_BMAP_PTR_VM 2 /*
  114. * virtual block number (has multiple
  115. * versions)
  116. */
  117. #define NILFS_BMAP_PTR_U (-1) /* never perform pointer operations */
  118. #define NILFS_BMAP_USE_VBN(bmap) ((bmap)->b_ptr_type > 0)
  119. /* state */
  120. #define NILFS_BMAP_DIRTY 0x00000001
  121. /**
  122. * struct nilfs_bmap_store - shadow copy of bmap state
  123. * @data: cached raw block mapping of on-disk inode
  124. * @last_allocated_key: cached value of last allocated key for data block
  125. * @last_allocated_ptr: cached value of last allocated ptr for data block
  126. * @state: cached value of state field of bmap structure
  127. */
  128. struct nilfs_bmap_store {
  129. __le64 data[NILFS_BMAP_SIZE / sizeof(__le64)];
  130. __u64 last_allocated_key;
  131. __u64 last_allocated_ptr;
  132. int state;
  133. };
  134. int nilfs_bmap_test_and_clear_dirty(struct nilfs_bmap *);
  135. int nilfs_bmap_read(struct nilfs_bmap *, struct nilfs_inode *);
  136. void nilfs_bmap_write(struct nilfs_bmap *, struct nilfs_inode *);
  137. int nilfs_bmap_lookup_contig(struct nilfs_bmap *, __u64, __u64 *, unsigned int);
  138. int nilfs_bmap_insert(struct nilfs_bmap *bmap, __u64 key, unsigned long rec);
  139. int nilfs_bmap_delete(struct nilfs_bmap *bmap, __u64 key);
  140. int nilfs_bmap_seek_key(struct nilfs_bmap *bmap, __u64 start, __u64 *keyp);
  141. int nilfs_bmap_last_key(struct nilfs_bmap *bmap, __u64 *keyp);
  142. int nilfs_bmap_truncate(struct nilfs_bmap *bmap, __u64 key);
  143. void nilfs_bmap_clear(struct nilfs_bmap *);
  144. int nilfs_bmap_propagate(struct nilfs_bmap *, struct buffer_head *);
  145. void nilfs_bmap_lookup_dirty_buffers(struct nilfs_bmap *, struct list_head *);
  146. int nilfs_bmap_assign(struct nilfs_bmap *, struct buffer_head **,
  147. unsigned long, union nilfs_binfo *);
  148. int nilfs_bmap_lookup_at_level(struct nilfs_bmap *, __u64, int, __u64 *);
  149. int nilfs_bmap_mark(struct nilfs_bmap *, __u64, int);
  150. void nilfs_bmap_init_gc(struct nilfs_bmap *);
  151. void nilfs_bmap_save(const struct nilfs_bmap *, struct nilfs_bmap_store *);
  152. void nilfs_bmap_restore(struct nilfs_bmap *, const struct nilfs_bmap_store *);
  153. static inline int nilfs_bmap_lookup(struct nilfs_bmap *bmap, __u64 key,
  154. __u64 *ptr)
  155. {
  156. return nilfs_bmap_lookup_at_level(bmap, key, 1, ptr);
  157. }
  158. /*
  159. * Internal use only
  160. */
  161. struct inode *nilfs_bmap_get_dat(const struct nilfs_bmap *);
  162. static inline int nilfs_bmap_prepare_alloc_ptr(struct nilfs_bmap *bmap,
  163. union nilfs_bmap_ptr_req *req,
  164. struct inode *dat)
  165. {
  166. if (dat)
  167. return nilfs_dat_prepare_alloc(dat, &req->bpr_req);
  168. /* ignore target ptr */
  169. req->bpr_ptr = bmap->b_last_allocated_ptr++;
  170. return 0;
  171. }
  172. static inline void nilfs_bmap_commit_alloc_ptr(struct nilfs_bmap *bmap,
  173. union nilfs_bmap_ptr_req *req,
  174. struct inode *dat)
  175. {
  176. if (dat)
  177. nilfs_dat_commit_alloc(dat, &req->bpr_req);
  178. }
  179. static inline void nilfs_bmap_abort_alloc_ptr(struct nilfs_bmap *bmap,
  180. union nilfs_bmap_ptr_req *req,
  181. struct inode *dat)
  182. {
  183. if (dat)
  184. nilfs_dat_abort_alloc(dat, &req->bpr_req);
  185. else
  186. bmap->b_last_allocated_ptr--;
  187. }
  188. static inline int nilfs_bmap_prepare_end_ptr(struct nilfs_bmap *bmap,
  189. union nilfs_bmap_ptr_req *req,
  190. struct inode *dat)
  191. {
  192. return dat ? nilfs_dat_prepare_end(dat, &req->bpr_req) : 0;
  193. }
  194. static inline void nilfs_bmap_commit_end_ptr(struct nilfs_bmap *bmap,
  195. union nilfs_bmap_ptr_req *req,
  196. struct inode *dat)
  197. {
  198. if (dat)
  199. nilfs_dat_commit_end(dat, &req->bpr_req,
  200. bmap->b_ptr_type == NILFS_BMAP_PTR_VS);
  201. }
  202. static inline void nilfs_bmap_abort_end_ptr(struct nilfs_bmap *bmap,
  203. union nilfs_bmap_ptr_req *req,
  204. struct inode *dat)
  205. {
  206. if (dat)
  207. nilfs_dat_abort_end(dat, &req->bpr_req);
  208. }
  209. static inline void nilfs_bmap_set_target_v(struct nilfs_bmap *bmap, __u64 key,
  210. __u64 ptr)
  211. {
  212. bmap->b_last_allocated_key = key;
  213. bmap->b_last_allocated_ptr = ptr;
  214. }
  215. __u64 nilfs_bmap_data_get_key(const struct nilfs_bmap *,
  216. const struct buffer_head *);
  217. __u64 nilfs_bmap_find_target_seq(const struct nilfs_bmap *, __u64);
  218. __u64 nilfs_bmap_find_target_in_group(const struct nilfs_bmap *);
  219. /* Assume that bmap semaphore is locked. */
  220. static inline int nilfs_bmap_dirty(const struct nilfs_bmap *bmap)
  221. {
  222. return !!(bmap->b_state & NILFS_BMAP_DIRTY);
  223. }
  224. /* Assume that bmap semaphore is locked. */
  225. static inline void nilfs_bmap_set_dirty(struct nilfs_bmap *bmap)
  226. {
  227. bmap->b_state |= NILFS_BMAP_DIRTY;
  228. }
  229. /* Assume that bmap semaphore is locked. */
  230. static inline void nilfs_bmap_clear_dirty(struct nilfs_bmap *bmap)
  231. {
  232. bmap->b_state &= ~NILFS_BMAP_DIRTY;
  233. }
  234. #define NILFS_BMAP_LARGE 0x1
  235. #define NILFS_BMAP_SMALL_LOW NILFS_DIRECT_KEY_MIN
  236. #define NILFS_BMAP_SMALL_HIGH NILFS_DIRECT_KEY_MAX
  237. #define NILFS_BMAP_LARGE_LOW NILFS_BTREE_ROOT_NCHILDREN_MAX
  238. #define NILFS_BMAP_LARGE_HIGH NILFS_BTREE_KEY_MAX
  239. #endif /* _NILFS_BMAP_H */