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