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