suballoc.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * suballoc.h
  4. *
  5. * Defines sub allocator api
  6. *
  7. * Copyright (C) 2003, 2004 Oracle. All rights reserved.
  8. */
  9. #ifndef _CHAINALLOC_H_
  10. #define _CHAINALLOC_H_
  11. struct ocfs2_suballoc_result;
  12. typedef int (group_search_t)(struct inode *,
  13. struct buffer_head *,
  14. u32, /* bits_wanted */
  15. u32, /* min_bits */
  16. u64, /* max_block */
  17. struct ocfs2_suballoc_result *);
  18. /* found bits */
  19. struct ocfs2_alloc_context {
  20. struct inode *ac_inode; /* which bitmap are we allocating from? */
  21. struct buffer_head *ac_bh; /* file entry bh */
  22. u32 ac_alloc_slot; /* which slot are we allocating from? */
  23. u32 ac_bits_wanted;
  24. u32 ac_bits_given;
  25. #define OCFS2_AC_USE_LOCAL 1
  26. #define OCFS2_AC_USE_MAIN 2
  27. #define OCFS2_AC_USE_INODE 3
  28. #define OCFS2_AC_USE_META 4
  29. #define OCFS2_AC_USE_MAIN_DISCONTIG 5
  30. u32 ac_which;
  31. /* these are used by the chain search */
  32. u16 ac_chain;
  33. int ac_disable_chain_relink;
  34. group_search_t *ac_group_search;
  35. u64 ac_last_group;
  36. u64 ac_max_block; /* Highest block number to allocate. 0 is
  37. the same as ~0 - unlimited */
  38. int ac_find_loc_only; /* hack for reflink operation ordering */
  39. struct ocfs2_suballoc_result *ac_find_loc_priv; /* */
  40. struct ocfs2_alloc_reservation *ac_resv;
  41. };
  42. void ocfs2_init_steal_slots(struct ocfs2_super *osb);
  43. void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac);
  44. static inline int ocfs2_alloc_context_bits_left(struct ocfs2_alloc_context *ac)
  45. {
  46. return ac->ac_bits_wanted - ac->ac_bits_given;
  47. }
  48. /*
  49. * Please note that the caller must make sure that root_el is the root
  50. * of extent tree. So for an inode, it should be &fe->id2.i_list. Otherwise
  51. * the result may be wrong.
  52. */
  53. int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
  54. struct ocfs2_extent_list *root_el,
  55. struct ocfs2_alloc_context **ac);
  56. int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb,
  57. int blocks,
  58. struct ocfs2_alloc_context **ac);
  59. int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
  60. struct ocfs2_alloc_context **ac);
  61. int ocfs2_reserve_clusters(struct ocfs2_super *osb,
  62. u32 bits_wanted,
  63. struct ocfs2_alloc_context **ac);
  64. int ocfs2_alloc_dinode_update_counts(struct inode *inode,
  65. handle_t *handle,
  66. struct buffer_head *di_bh,
  67. u32 num_bits,
  68. u16 chain);
  69. void ocfs2_rollback_alloc_dinode_counts(struct inode *inode,
  70. struct buffer_head *di_bh,
  71. u32 num_bits,
  72. u16 chain);
  73. u16 ocfs2_find_max_contig_free_bits(void *bitmap,
  74. u16 total_bits, u16 start);
  75. int ocfs2_block_group_set_bits(handle_t *handle,
  76. struct inode *alloc_inode,
  77. struct ocfs2_group_desc *bg,
  78. struct buffer_head *group_bh,
  79. unsigned int bit_off,
  80. unsigned int num_bits,
  81. unsigned int max_contig_bits,
  82. int fastpath);
  83. int ocfs2_claim_metadata(handle_t *handle,
  84. struct ocfs2_alloc_context *ac,
  85. u32 bits_wanted,
  86. u64 *suballoc_loc,
  87. u16 *suballoc_bit_start,
  88. u32 *num_bits,
  89. u64 *blkno_start);
  90. int ocfs2_claim_new_inode(handle_t *handle,
  91. struct inode *dir,
  92. struct buffer_head *parent_fe_bh,
  93. struct ocfs2_alloc_context *ac,
  94. u64 *suballoc_loc,
  95. u16 *suballoc_bit,
  96. u64 *fe_blkno);
  97. int ocfs2_claim_clusters(handle_t *handle,
  98. struct ocfs2_alloc_context *ac,
  99. u32 min_clusters,
  100. u32 *cluster_start,
  101. u32 *num_clusters);
  102. /*
  103. * Use this variant of ocfs2_claim_clusters to specify a maximum
  104. * number of clusters smaller than the allocation reserved.
  105. */
  106. int __ocfs2_claim_clusters(handle_t *handle,
  107. struct ocfs2_alloc_context *ac,
  108. u32 min_clusters,
  109. u32 max_clusters,
  110. u32 *cluster_start,
  111. u32 *num_clusters);
  112. int ocfs2_free_suballoc_bits(handle_t *handle,
  113. struct inode *alloc_inode,
  114. struct buffer_head *alloc_bh,
  115. unsigned int start_bit,
  116. u64 bg_blkno,
  117. unsigned int count);
  118. int ocfs2_free_dinode(handle_t *handle,
  119. struct inode *inode_alloc_inode,
  120. struct buffer_head *inode_alloc_bh,
  121. struct ocfs2_dinode *di);
  122. int ocfs2_free_clusters(handle_t *handle,
  123. struct inode *bitmap_inode,
  124. struct buffer_head *bitmap_bh,
  125. u64 start_blk,
  126. unsigned int num_clusters);
  127. int ocfs2_release_clusters(handle_t *handle,
  128. struct inode *bitmap_inode,
  129. struct buffer_head *bitmap_bh,
  130. u64 start_blk,
  131. unsigned int num_clusters);
  132. static inline u64 ocfs2_which_suballoc_group(u64 block, unsigned int bit)
  133. {
  134. u64 group = block - (u64) bit;
  135. return group;
  136. }
  137. static inline u32 ocfs2_cluster_from_desc(struct ocfs2_super *osb,
  138. u64 bg_blkno)
  139. {
  140. /* This should work for all block group descriptors as only
  141. * the 1st group descriptor of the cluster bitmap is
  142. * different. */
  143. if (bg_blkno == osb->first_cluster_group_blkno)
  144. return 0;
  145. /* the rest of the block groups are located at the beginning
  146. * of their 1st cluster, so a direct translation just
  147. * works. */
  148. return ocfs2_blocks_to_clusters(osb->sb, bg_blkno);
  149. }
  150. static inline int ocfs2_is_cluster_bitmap(struct inode *inode)
  151. {
  152. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  153. return osb->bitmap_blkno == OCFS2_I(inode)->ip_blkno;
  154. }
  155. /* This is for local alloc ONLY. Others should use the task-specific
  156. * apis above. */
  157. int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
  158. struct ocfs2_alloc_context *ac);
  159. void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac);
  160. /* given a cluster offset, calculate which block group it belongs to
  161. * and return that block offset. */
  162. u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster);
  163. /*
  164. * By default, ocfs2_read_group_descriptor() calls ocfs2_error() when it
  165. * finds a problem. A caller that wants to check a group descriptor
  166. * without going readonly should read the block with ocfs2_read_block[s]()
  167. * and then checking it with this function. This is only resize, really.
  168. * Everyone else should be using ocfs2_read_group_descriptor().
  169. */
  170. int ocfs2_check_group_descriptor(struct super_block *sb,
  171. struct ocfs2_dinode *di,
  172. struct buffer_head *bh);
  173. /*
  174. * Read a group descriptor block into *bh. If *bh is NULL, a bh will be
  175. * allocated. This is a cached read. The descriptor will be validated with
  176. * ocfs2_validate_group_descriptor().
  177. */
  178. int ocfs2_read_group_descriptor(struct inode *inode, struct ocfs2_dinode *di,
  179. u64 gd_blkno, struct buffer_head **bh);
  180. int ocfs2_lock_allocators(struct inode *inode, struct ocfs2_extent_tree *et,
  181. u32 clusters_to_add, u32 extents_to_split,
  182. struct ocfs2_alloc_context **data_ac,
  183. struct ocfs2_alloc_context **meta_ac);
  184. int ocfs2_test_inode_bit(struct ocfs2_super *osb, u64 blkno, int *res);
  185. /*
  186. * The following two interfaces are for ocfs2_create_inode_in_orphan().
  187. */
  188. int ocfs2_find_new_inode_loc(struct inode *dir,
  189. struct buffer_head *parent_fe_bh,
  190. struct ocfs2_alloc_context *ac,
  191. u64 *fe_blkno);
  192. int ocfs2_claim_new_inode_at_loc(handle_t *handle,
  193. struct inode *dir,
  194. struct ocfs2_alloc_context *ac,
  195. u64 *suballoc_loc,
  196. u16 *suballoc_bit,
  197. u64 di_blkno);
  198. #endif /* _CHAINALLOC_H_ */