bmap.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * bmap.h - NILFS block mapping.
  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. #ifndef _NILFS_BMAP_H
  23. #define _NILFS_BMAP_H
  24. #include <linux/types.h>
  25. #include <linux/fs.h>
  26. #include <linux/buffer_head.h>
  27. #include <linux/nilfs2_fs.h>
  28. #include "alloc.h"
  29. #include "dat.h"
  30. #define NILFS_BMAP_INVALID_PTR 0
  31. #define nilfs_bmap_keydiff_abs(diff) ((diff) < 0 ? -(diff) : (diff))
  32. struct nilfs_bmap;
  33. /**
  34. * union nilfs_bmap_ptr_req - request for bmap ptr
  35. * @bpr_ptr: bmap pointer
  36. * @bpr_req: request for persistent allocator
  37. */
  38. union nilfs_bmap_ptr_req {
  39. __u64 bpr_ptr;
  40. struct nilfs_palloc_req bpr_req;
  41. };
  42. /**
  43. * struct nilfs_bmap_stats - bmap statistics
  44. * @bs_nblocks: number of blocks created or deleted
  45. */
  46. struct nilfs_bmap_stats {
  47. unsigned int bs_nblocks;
  48. };
  49. /**
  50. * struct nilfs_bmap_operations - bmap operation table
  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);
  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. /* The following functions are 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 (sizeof(unsigned long) * 8 /* CHAR_BIT */)
  76. #define NILFS_BMAP_NEW_PTR_INIT \
  77. (1UL << (sizeof(unsigned long) * 8 /* CHAR_BIT */ - 1))
  78. static inline int nilfs_bmap_is_new_ptr(unsigned long ptr)
  79. {
  80. return !!(ptr & NILFS_BMAP_NEW_PTR_INIT);
  81. }
  82. /**
  83. * struct nilfs_bmap - bmap structure
  84. * @b_u: raw data
  85. * @b_sem: semaphore
  86. * @b_inode: owner of bmap
  87. * @b_ops: bmap operation table
  88. * @b_last_allocated_key: last allocated key for data block
  89. * @b_last_allocated_ptr: last allocated ptr for data block
  90. * @b_ptr_type: pointer type
  91. * @b_state: state
  92. * @b_nchildren_per_block: maximum number of child nodes for non-root nodes
  93. */
  94. struct nilfs_bmap {
  95. union {
  96. __u8 u_flags;
  97. __le64 u_data[NILFS_BMAP_SIZE / sizeof(__le64)];
  98. } b_u;
  99. struct rw_semaphore b_sem;
  100. struct inode *b_inode;
  101. const struct nilfs_bmap_operations *b_ops;
  102. __u64 b_last_allocated_key;
  103. __u64 b_last_allocated_ptr;
  104. int b_ptr_type;
  105. int b_state;
  106. __u16 b_nchildren_per_block;
  107. };
  108. /* pointer type */
  109. #define NILFS_BMAP_PTR_P 0 /* physical block number (i.e. LBN) */
  110. #define NILFS_BMAP_PTR_VS 1 /* virtual block number (single
  111. version) */
  112. #define NILFS_BMAP_PTR_VM 2 /* virtual block number (has multiple
  113. versions) */
  114. #define NILFS_BMAP_PTR_U (-1) /* never perform pointer operations */
  115. #define NILFS_BMAP_USE_VBN(bmap) ((bmap)->b_ptr_type > 0)
  116. /* state */
  117. #define NILFS_BMAP_DIRTY 0x00000001
  118. /**
  119. * struct nilfs_bmap_store - shadow copy of bmap state
  120. * @data: cached raw block mapping of on-disk inode
  121. * @last_allocated_key: cached value of last allocated key for data block
  122. * @last_allocated_ptr: cached value of last allocated ptr for data block
  123. * @state: cached value of state field of bmap structure
  124. */
  125. struct nilfs_bmap_store {
  126. __le64 data[NILFS_BMAP_SIZE / sizeof(__le64)];
  127. __u64 last_allocated_key;
  128. __u64 last_allocated_ptr;
  129. int state;
  130. };
  131. int nilfs_bmap_test_and_clear_dirty(struct nilfs_bmap *);
  132. int nilfs_bmap_read(struct nilfs_bmap *, struct nilfs_inode *);
  133. void nilfs_bmap_write(struct nilfs_bmap *, struct nilfs_inode *);
  134. int nilfs_bmap_lookup_contig(struct nilfs_bmap *, __u64, __u64 *, unsigned);
  135. int nilfs_bmap_insert(struct nilfs_bmap *bmap, __u64 key, unsigned long rec);
  136. int nilfs_bmap_delete(struct nilfs_bmap *bmap, __u64 key);
  137. int nilfs_bmap_seek_key(struct nilfs_bmap *bmap, __u64 start, __u64 *keyp);
  138. int nilfs_bmap_last_key(struct nilfs_bmap *bmap, __u64 *keyp);
  139. int nilfs_bmap_truncate(struct nilfs_bmap *bmap, __u64 key);
  140. void nilfs_bmap_clear(struct nilfs_bmap *);
  141. int nilfs_bmap_propagate(struct nilfs_bmap *, struct buffer_head *);
  142. void nilfs_bmap_lookup_dirty_buffers(struct nilfs_bmap *, struct list_head *);
  143. int nilfs_bmap_assign(struct nilfs_bmap *, struct buffer_head **,
  144. unsigned long, union nilfs_binfo *);
  145. int nilfs_bmap_lookup_at_level(struct nilfs_bmap *, __u64, int, __u64 *);
  146. int nilfs_bmap_mark(struct nilfs_bmap *, __u64, int);
  147. void nilfs_bmap_init_gc(struct nilfs_bmap *);
  148. void nilfs_bmap_save(const struct nilfs_bmap *, struct nilfs_bmap_store *);
  149. void nilfs_bmap_restore(struct nilfs_bmap *, const struct nilfs_bmap_store *);
  150. static inline int nilfs_bmap_lookup(struct nilfs_bmap *bmap, __u64 key,
  151. __u64 *ptr)
  152. {
  153. return nilfs_bmap_lookup_at_level(bmap, key, 1, ptr);
  154. }
  155. /*
  156. * Internal use only
  157. */
  158. struct inode *nilfs_bmap_get_dat(const struct nilfs_bmap *);
  159. static inline int nilfs_bmap_prepare_alloc_ptr(struct nilfs_bmap *bmap,
  160. union nilfs_bmap_ptr_req *req,
  161. struct inode *dat)
  162. {
  163. if (dat)
  164. return nilfs_dat_prepare_alloc(dat, &req->bpr_req);
  165. /* ignore target ptr */
  166. req->bpr_ptr = bmap->b_last_allocated_ptr++;
  167. return 0;
  168. }
  169. static inline void nilfs_bmap_commit_alloc_ptr(struct nilfs_bmap *bmap,
  170. union nilfs_bmap_ptr_req *req,
  171. struct inode *dat)
  172. {
  173. if (dat)
  174. nilfs_dat_commit_alloc(dat, &req->bpr_req);
  175. }
  176. static inline void nilfs_bmap_abort_alloc_ptr(struct nilfs_bmap *bmap,
  177. union nilfs_bmap_ptr_req *req,
  178. struct inode *dat)
  179. {
  180. if (dat)
  181. nilfs_dat_abort_alloc(dat, &req->bpr_req);
  182. else
  183. bmap->b_last_allocated_ptr--;
  184. }
  185. static inline int nilfs_bmap_prepare_end_ptr(struct nilfs_bmap *bmap,
  186. union nilfs_bmap_ptr_req *req,
  187. struct inode *dat)
  188. {
  189. return dat ? nilfs_dat_prepare_end(dat, &req->bpr_req) : 0;
  190. }
  191. static inline void nilfs_bmap_commit_end_ptr(struct nilfs_bmap *bmap,
  192. union nilfs_bmap_ptr_req *req,
  193. struct inode *dat)
  194. {
  195. if (dat)
  196. nilfs_dat_commit_end(dat, &req->bpr_req,
  197. bmap->b_ptr_type == NILFS_BMAP_PTR_VS);
  198. }
  199. static inline void nilfs_bmap_abort_end_ptr(struct nilfs_bmap *bmap,
  200. union nilfs_bmap_ptr_req *req,
  201. struct inode *dat)
  202. {
  203. if (dat)
  204. nilfs_dat_abort_end(dat, &req->bpr_req);
  205. }
  206. static inline void nilfs_bmap_set_target_v(struct nilfs_bmap *bmap, __u64 key,
  207. __u64 ptr)
  208. {
  209. bmap->b_last_allocated_key = key;
  210. bmap->b_last_allocated_ptr = ptr;
  211. }
  212. __u64 nilfs_bmap_data_get_key(const struct nilfs_bmap *,
  213. const struct buffer_head *);
  214. __u64 nilfs_bmap_find_target_seq(const struct nilfs_bmap *, __u64);
  215. __u64 nilfs_bmap_find_target_in_group(const struct nilfs_bmap *);
  216. /* Assume that bmap semaphore is locked. */
  217. static inline int nilfs_bmap_dirty(const struct nilfs_bmap *bmap)
  218. {
  219. return !!(bmap->b_state & NILFS_BMAP_DIRTY);
  220. }
  221. /* Assume that bmap semaphore is locked. */
  222. static inline void nilfs_bmap_set_dirty(struct nilfs_bmap *bmap)
  223. {
  224. bmap->b_state |= NILFS_BMAP_DIRTY;
  225. }
  226. /* Assume that bmap semaphore is locked. */
  227. static inline void nilfs_bmap_clear_dirty(struct nilfs_bmap *bmap)
  228. {
  229. bmap->b_state &= ~NILFS_BMAP_DIRTY;
  230. }
  231. #define NILFS_BMAP_LARGE 0x1
  232. #define NILFS_BMAP_SMALL_LOW NILFS_DIRECT_KEY_MIN
  233. #define NILFS_BMAP_SMALL_HIGH NILFS_DIRECT_KEY_MAX
  234. #define NILFS_BMAP_LARGE_LOW NILFS_BTREE_ROOT_NCHILDREN_MAX
  235. #define NILFS_BMAP_LARGE_HIGH NILFS_BTREE_KEY_MAX
  236. #endif /* _NILFS_BMAP_H */