bmap.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. #define NILFS_BMAP_INVALID_PTR 0
  30. #define nilfs_bmap_dkey_to_key(dkey) le64_to_cpu(dkey)
  31. #define nilfs_bmap_key_to_dkey(key) cpu_to_le64(key)
  32. #define nilfs_bmap_dptr_to_ptr(dptr) le64_to_cpu(dptr)
  33. #define nilfs_bmap_ptr_to_dptr(ptr) cpu_to_le64(ptr)
  34. #define nilfs_bmap_keydiff_abs(diff) ((diff) < 0 ? -(diff) : (diff))
  35. struct nilfs_bmap;
  36. /**
  37. * union nilfs_bmap_ptr_req - request for bmap ptr
  38. * @bpr_ptr: bmap pointer
  39. * @bpr_req: request for persistent allocator
  40. */
  41. union nilfs_bmap_ptr_req {
  42. __u64 bpr_ptr;
  43. struct nilfs_palloc_req bpr_req;
  44. };
  45. /**
  46. * struct nilfs_bmap_stats - bmap statistics
  47. * @bs_nblocks: number of blocks created or deleted
  48. */
  49. struct nilfs_bmap_stats {
  50. unsigned int bs_nblocks;
  51. };
  52. /**
  53. * struct nilfs_bmap_operations - bmap operation table
  54. */
  55. struct nilfs_bmap_operations {
  56. int (*bop_lookup)(const struct nilfs_bmap *, __u64, int, __u64 *);
  57. int (*bop_insert)(struct nilfs_bmap *, __u64, __u64);
  58. int (*bop_delete)(struct nilfs_bmap *, __u64);
  59. void (*bop_clear)(struct nilfs_bmap *);
  60. int (*bop_propagate)(const struct nilfs_bmap *, struct buffer_head *);
  61. void (*bop_lookup_dirty_buffers)(struct nilfs_bmap *,
  62. struct list_head *);
  63. int (*bop_assign)(struct nilfs_bmap *,
  64. struct buffer_head **,
  65. sector_t,
  66. union nilfs_binfo *);
  67. int (*bop_mark)(struct nilfs_bmap *, __u64, int);
  68. /* The following functions are internal use only. */
  69. int (*bop_last_key)(const struct nilfs_bmap *, __u64 *);
  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. /**
  75. * struct nilfs_bmap_ptr_operations - bmap ptr operation table
  76. */
  77. struct nilfs_bmap_ptr_operations {
  78. int (*bpop_prepare_alloc_ptr)(struct nilfs_bmap *,
  79. union nilfs_bmap_ptr_req *);
  80. void (*bpop_commit_alloc_ptr)(struct nilfs_bmap *,
  81. union nilfs_bmap_ptr_req *);
  82. void (*bpop_abort_alloc_ptr)(struct nilfs_bmap *,
  83. union nilfs_bmap_ptr_req *);
  84. int (*bpop_prepare_start_ptr)(struct nilfs_bmap *,
  85. union nilfs_bmap_ptr_req *);
  86. void (*bpop_commit_start_ptr)(struct nilfs_bmap *,
  87. union nilfs_bmap_ptr_req *,
  88. sector_t);
  89. void (*bpop_abort_start_ptr)(struct nilfs_bmap *,
  90. union nilfs_bmap_ptr_req *);
  91. int (*bpop_prepare_end_ptr)(struct nilfs_bmap *,
  92. union nilfs_bmap_ptr_req *);
  93. void (*bpop_commit_end_ptr)(struct nilfs_bmap *,
  94. union nilfs_bmap_ptr_req *);
  95. void (*bpop_abort_end_ptr)(struct nilfs_bmap *,
  96. union nilfs_bmap_ptr_req *);
  97. int (*bpop_translate)(const struct nilfs_bmap *, __u64, __u64 *);
  98. };
  99. #define NILFS_BMAP_SIZE (NILFS_INODE_BMAP_SIZE * sizeof(__le64))
  100. #define NILFS_BMAP_KEY_BIT (sizeof(unsigned long) * 8 /* CHAR_BIT */)
  101. #define NILFS_BMAP_NEW_PTR_INIT \
  102. (1UL << (sizeof(unsigned long) * 8 /* CHAR_BIT */ - 1))
  103. static inline int nilfs_bmap_is_new_ptr(unsigned long ptr)
  104. {
  105. return !!(ptr & NILFS_BMAP_NEW_PTR_INIT);
  106. }
  107. /**
  108. * struct nilfs_bmap - bmap structure
  109. * @b_u: raw data
  110. * @b_sem: semaphore
  111. * @b_inode: owner of bmap
  112. * @b_ops: bmap operation table
  113. * @b_pops: bmap ptr operation table
  114. * @b_low: low watermark of conversion
  115. * @b_high: high watermark of conversion
  116. * @b_last_allocated_key: last allocated key for data block
  117. * @b_last_allocated_ptr: last allocated ptr for data block
  118. * @b_state: state
  119. */
  120. struct nilfs_bmap {
  121. union {
  122. __u8 u_flags;
  123. __le64 u_data[NILFS_BMAP_SIZE / sizeof(__le64)];
  124. } b_u;
  125. struct rw_semaphore b_sem;
  126. struct inode *b_inode;
  127. const struct nilfs_bmap_operations *b_ops;
  128. const struct nilfs_bmap_ptr_operations *b_pops;
  129. __u64 b_low;
  130. __u64 b_high;
  131. __u64 b_last_allocated_key;
  132. __u64 b_last_allocated_ptr;
  133. int b_state;
  134. };
  135. /* state */
  136. #define NILFS_BMAP_DIRTY 0x00000001
  137. int nilfs_bmap_test_and_clear_dirty(struct nilfs_bmap *);
  138. int nilfs_bmap_read(struct nilfs_bmap *, struct nilfs_inode *);
  139. void nilfs_bmap_write(struct nilfs_bmap *, struct nilfs_inode *);
  140. int nilfs_bmap_lookup(struct nilfs_bmap *, unsigned long, unsigned long *);
  141. int nilfs_bmap_insert(struct nilfs_bmap *, unsigned long, unsigned long);
  142. int nilfs_bmap_delete(struct nilfs_bmap *, unsigned long);
  143. int nilfs_bmap_last_key(struct nilfs_bmap *, unsigned long *);
  144. int nilfs_bmap_truncate(struct nilfs_bmap *, unsigned long);
  145. void nilfs_bmap_clear(struct nilfs_bmap *);
  146. int nilfs_bmap_propagate(struct nilfs_bmap *, struct buffer_head *);
  147. void nilfs_bmap_lookup_dirty_buffers(struct nilfs_bmap *, struct list_head *);
  148. int nilfs_bmap_assign(struct nilfs_bmap *, struct buffer_head **,
  149. unsigned long, union nilfs_binfo *);
  150. int nilfs_bmap_lookup_at_level(struct nilfs_bmap *, __u64, int, __u64 *);
  151. int nilfs_bmap_mark(struct nilfs_bmap *, __u64, int);
  152. void nilfs_bmap_init_gc(struct nilfs_bmap *);
  153. void nilfs_bmap_init_gcdat(struct nilfs_bmap *, struct nilfs_bmap *);
  154. void nilfs_bmap_commit_gcdat(struct nilfs_bmap *, struct nilfs_bmap *);
  155. /*
  156. * Internal use only
  157. */
  158. int nilfs_bmap_move_v(const struct nilfs_bmap *, __u64, sector_t);
  159. int nilfs_bmap_mark_dirty(const struct nilfs_bmap *, __u64);
  160. __u64 nilfs_bmap_data_get_key(const struct nilfs_bmap *,
  161. const struct buffer_head *);
  162. __u64 nilfs_bmap_find_target_seq(const struct nilfs_bmap *, __u64);
  163. __u64 nilfs_bmap_find_target_in_group(const struct nilfs_bmap *);
  164. int nilfs_bmap_prepare_update(struct nilfs_bmap *,
  165. union nilfs_bmap_ptr_req *,
  166. union nilfs_bmap_ptr_req *);
  167. void nilfs_bmap_commit_update(struct nilfs_bmap *,
  168. union nilfs_bmap_ptr_req *,
  169. union nilfs_bmap_ptr_req *);
  170. void nilfs_bmap_abort_update(struct nilfs_bmap *,
  171. union nilfs_bmap_ptr_req *,
  172. union nilfs_bmap_ptr_req *);
  173. void nilfs_bmap_add_blocks(const struct nilfs_bmap *, int);
  174. void nilfs_bmap_sub_blocks(const struct nilfs_bmap *, int);
  175. int nilfs_bmap_get_block(const struct nilfs_bmap *, __u64,
  176. struct buffer_head **);
  177. void nilfs_bmap_put_block(const struct nilfs_bmap *, struct buffer_head *);
  178. int nilfs_bmap_get_new_block(const struct nilfs_bmap *, __u64,
  179. struct buffer_head **);
  180. void nilfs_bmap_delete_block(const struct nilfs_bmap *, struct buffer_head *);
  181. /* Assume that bmap semaphore is locked. */
  182. static inline int nilfs_bmap_dirty(const struct nilfs_bmap *bmap)
  183. {
  184. return !!(bmap->b_state & NILFS_BMAP_DIRTY);
  185. }
  186. /* Assume that bmap semaphore is locked. */
  187. static inline void nilfs_bmap_set_dirty(struct nilfs_bmap *bmap)
  188. {
  189. bmap->b_state |= NILFS_BMAP_DIRTY;
  190. }
  191. /* Assume that bmap semaphore is locked. */
  192. static inline void nilfs_bmap_clear_dirty(struct nilfs_bmap *bmap)
  193. {
  194. bmap->b_state &= ~NILFS_BMAP_DIRTY;
  195. }
  196. #define NILFS_BMAP_LARGE 0x1
  197. #define NILFS_BMAP_SMALL_LOW NILFS_DIRECT_KEY_MIN
  198. #define NILFS_BMAP_SMALL_HIGH NILFS_DIRECT_KEY_MAX
  199. #define NILFS_BMAP_LARGE_LOW NILFS_BTREE_ROOT_NCHILDREN_MAX
  200. #define NILFS_BMAP_LARGE_HIGH NILFS_BTREE_KEY_MAX
  201. #endif /* _NILFS_BMAP_H */