suballoc.h 6.9 KB

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