xref: /openbmc/linux/fs/ocfs2/alloc.h (revision 5e96581a)
1ccd979bdSMark Fasheh /* -*- mode: c; c-basic-offset: 8; -*-
2ccd979bdSMark Fasheh  * vim: noexpandtab sw=8 ts=8 sts=0:
3ccd979bdSMark Fasheh  *
4ccd979bdSMark Fasheh  * alloc.h
5ccd979bdSMark Fasheh  *
6ccd979bdSMark Fasheh  * Function prototypes
7ccd979bdSMark Fasheh  *
8ccd979bdSMark Fasheh  * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
9ccd979bdSMark Fasheh  *
10ccd979bdSMark Fasheh  * This program is free software; you can redistribute it and/or
11ccd979bdSMark Fasheh  * modify it under the terms of the GNU General Public
12ccd979bdSMark Fasheh  * License as published by the Free Software Foundation; either
13ccd979bdSMark Fasheh  * version 2 of the License, or (at your option) any later version.
14ccd979bdSMark Fasheh  *
15ccd979bdSMark Fasheh  * This program is distributed in the hope that it will be useful,
16ccd979bdSMark Fasheh  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17ccd979bdSMark Fasheh  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18ccd979bdSMark Fasheh  * General Public License for more details.
19ccd979bdSMark Fasheh  *
20ccd979bdSMark Fasheh  * You should have received a copy of the GNU General Public
21ccd979bdSMark Fasheh  * License along with this program; if not, write to the
22ccd979bdSMark Fasheh  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23ccd979bdSMark Fasheh  * Boston, MA 021110-1307, USA.
24ccd979bdSMark Fasheh  */
25ccd979bdSMark Fasheh 
26ccd979bdSMark Fasheh #ifndef OCFS2_ALLOC_H
27ccd979bdSMark Fasheh #define OCFS2_ALLOC_H
28ccd979bdSMark Fasheh 
29e7d4cb6bSTao Ma 
30ca12b7c4STao Ma /*
31ca12b7c4STao Ma  * For xattr tree leaf, we limit the leaf byte size to be 64K.
32ca12b7c4STao Ma  */
33ca12b7c4STao Ma #define OCFS2_MAX_XATTR_TREE_LEAF_SIZE 65536
34ca12b7c4STao Ma 
35f99b9b7cSJoel Becker /*
36f99b9b7cSJoel Becker  * ocfs2_extent_tree and ocfs2_extent_tree_operations are used to abstract
37f99b9b7cSJoel Becker  * the b-tree operations in ocfs2. Now all the b-tree operations are not
38f99b9b7cSJoel Becker  * limited to ocfs2_dinode only. Any data which need to allocate clusters
39f99b9b7cSJoel Becker  * to store can use b-tree. And it only needs to implement its ocfs2_extent_tree
40f99b9b7cSJoel Becker  * and operation.
41f99b9b7cSJoel Becker  *
42f99b9b7cSJoel Becker  * ocfs2_extent_tree becomes the first-class object for extent tree
43f99b9b7cSJoel Becker  * manipulation.  Callers of the alloc.c code need to fill it via one of
448d6220d6SJoel Becker  * the ocfs2_init_*_extent_tree() operations below.
45f99b9b7cSJoel Becker  *
46f99b9b7cSJoel Becker  * ocfs2_extent_tree contains info for the root of the b-tree, it must have a
47f99b9b7cSJoel Becker  * root ocfs2_extent_list and a root_bh so that they can be used in the b-tree
48f99b9b7cSJoel Becker  * functions.
49f99b9b7cSJoel Becker  * ocfs2_extent_tree_operations abstract the normal operations we do for
50f99b9b7cSJoel Becker  * the root of extent b-tree.
51f99b9b7cSJoel Becker  */
52f99b9b7cSJoel Becker struct ocfs2_extent_tree_operations;
53f99b9b7cSJoel Becker struct ocfs2_extent_tree {
54f99b9b7cSJoel Becker 	struct ocfs2_extent_tree_operations	*et_ops;
55f99b9b7cSJoel Becker 	struct buffer_head			*et_root_bh;
56f99b9b7cSJoel Becker 	struct ocfs2_extent_list		*et_root_el;
57f99b9b7cSJoel Becker 	void					*et_object;
58f99b9b7cSJoel Becker 	unsigned int				et_max_leaf_clusters;
59f99b9b7cSJoel Becker };
60f99b9b7cSJoel Becker 
61f99b9b7cSJoel Becker /*
628d6220d6SJoel Becker  * ocfs2_init_*_extent_tree() will fill an ocfs2_extent_tree from the
638d6220d6SJoel Becker  * specified object buffer.
64f99b9b7cSJoel Becker  */
658d6220d6SJoel Becker void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
66f56654c4STao Ma 				   struct inode *inode,
67f99b9b7cSJoel Becker 				   struct buffer_head *bh);
688d6220d6SJoel Becker void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
69ccd979bdSMark Fasheh 				       struct inode *inode,
70f99b9b7cSJoel Becker 				       struct buffer_head *bh);
718d6220d6SJoel Becker void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
72f99b9b7cSJoel Becker 					struct inode *inode,
73f99b9b7cSJoel Becker 					struct buffer_head *bh,
741a09f556SJoel Becker 					struct ocfs2_xattr_value_root *xv);
75f99b9b7cSJoel Becker 
765e96581aSJoel Becker /*
775e96581aSJoel Becker  * Read an extent block into *bh.  If *bh is NULL, a bh will be
785e96581aSJoel Becker  * allocated.  This is a cached read.  The extent block will be validated
795e96581aSJoel Becker  * with ocfs2_validate_extent_block().
805e96581aSJoel Becker  */
815e96581aSJoel Becker int ocfs2_read_extent_block(struct inode *inode, u64 eb_blkno,
825e96581aSJoel Becker 			    struct buffer_head **bh);
835e96581aSJoel Becker 
84f99b9b7cSJoel Becker struct ocfs2_alloc_context;
85f99b9b7cSJoel Becker int ocfs2_insert_extent(struct ocfs2_super *osb,
86ba492615STao Ma 			handle_t *handle,
87ba492615STao Ma 			struct inode *inode,
88f99b9b7cSJoel Becker 			struct ocfs2_extent_tree *et,
89ba492615STao Ma 			u32 cpos,
90ba492615STao Ma 			u64 start_blk,
91ba492615STao Ma 			u32 new_clusters,
92ba492615STao Ma 			u8 flags,
93ba492615STao Ma 			struct ocfs2_alloc_context *meta_ac);
94f99b9b7cSJoel Becker 
950eb8d47eSTao Ma enum ocfs2_alloc_restarted {
960eb8d47eSTao Ma 	RESTART_NONE = 0,
970eb8d47eSTao Ma 	RESTART_TRANS,
980eb8d47eSTao Ma 	RESTART_META
990eb8d47eSTao Ma };
1000eb8d47eSTao Ma int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
1010eb8d47eSTao Ma 				struct inode *inode,
1020eb8d47eSTao Ma 				u32 *logical_offset,
1030eb8d47eSTao Ma 				u32 clusters_to_add,
1040eb8d47eSTao Ma 				int mark_unwritten,
105f99b9b7cSJoel Becker 				struct ocfs2_extent_tree *et,
1060eb8d47eSTao Ma 				handle_t *handle,
1070eb8d47eSTao Ma 				struct ocfs2_alloc_context *data_ac,
1080eb8d47eSTao Ma 				struct ocfs2_alloc_context *meta_ac,
109f99b9b7cSJoel Becker 				enum ocfs2_alloc_restarted *reason_ret);
110328d5752SMark Fasheh struct ocfs2_cached_dealloc_ctxt;
111f99b9b7cSJoel Becker int ocfs2_mark_extent_written(struct inode *inode,
112f99b9b7cSJoel Becker 			      struct ocfs2_extent_tree *et,
113328d5752SMark Fasheh 			      handle_t *handle, u32 cpos, u32 len, u32 phys,
114328d5752SMark Fasheh 			      struct ocfs2_alloc_context *meta_ac,
115f99b9b7cSJoel Becker 			      struct ocfs2_cached_dealloc_ctxt *dealloc);
116f99b9b7cSJoel Becker int ocfs2_remove_extent(struct inode *inode,
117f99b9b7cSJoel Becker 			struct ocfs2_extent_tree *et,
118063c4561SMark Fasheh 			u32 cpos, u32 len, handle_t *handle,
119063c4561SMark Fasheh 			struct ocfs2_alloc_context *meta_ac,
120f99b9b7cSJoel Becker 			struct ocfs2_cached_dealloc_ctxt *dealloc);
121fecc0112SMark Fasheh int ocfs2_remove_btree_range(struct inode *inode,
122fecc0112SMark Fasheh 			     struct ocfs2_extent_tree *et,
123fecc0112SMark Fasheh 			     u32 cpos, u32 phys_cpos, u32 len,
124fecc0112SMark Fasheh 			     struct ocfs2_cached_dealloc_ctxt *dealloc);
125fecc0112SMark Fasheh 
126ccd979bdSMark Fasheh int ocfs2_num_free_extents(struct ocfs2_super *osb,
127ccd979bdSMark Fasheh 			   struct inode *inode,
128f99b9b7cSJoel Becker 			   struct ocfs2_extent_tree *et);
129e7d4cb6bSTao Ma 
130811f933dSTao Ma /*
131811f933dSTao Ma  * how many new metadata chunks would an allocation need at maximum?
132811f933dSTao Ma  *
133811f933dSTao Ma  * Please note that the caller must make sure that root_el is the root
134811f933dSTao Ma  * of extent tree. So for an inode, it should be &fe->id2.i_list. Otherwise
135811f933dSTao Ma  * the result may be wrong.
136811f933dSTao Ma  */
137811f933dSTao Ma static inline int ocfs2_extend_meta_needed(struct ocfs2_extent_list *root_el)
138ccd979bdSMark Fasheh {
139ccd979bdSMark Fasheh 	/*
140ccd979bdSMark Fasheh 	 * Rather than do all the work of determining how much we need
141ccd979bdSMark Fasheh 	 * (involves a ton of reads and locks), just ask for the
142ccd979bdSMark Fasheh 	 * maximal limit.  That's a tree depth shift.  So, one block for
143ccd979bdSMark Fasheh 	 * level of the tree (current l_tree_depth), one block for the
144ccd979bdSMark Fasheh 	 * new tree_depth==0 extent_block, and one block at the new
145ccd979bdSMark Fasheh 	 * top-of-the tree.
146ccd979bdSMark Fasheh 	 */
147811f933dSTao Ma 	return le16_to_cpu(root_el->l_tree_depth) + 2;
148ccd979bdSMark Fasheh }
149ccd979bdSMark Fasheh 
1505b6a3a2bSMark Fasheh void ocfs2_dinode_new_extent_list(struct inode *inode, struct ocfs2_dinode *di);
1511afc32b9SMark Fasheh void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di);
1521afc32b9SMark Fasheh int ocfs2_convert_inline_data_to_extents(struct inode *inode,
1531afc32b9SMark Fasheh 					 struct buffer_head *di_bh);
1541afc32b9SMark Fasheh 
155ccd979bdSMark Fasheh int ocfs2_truncate_log_init(struct ocfs2_super *osb);
156ccd979bdSMark Fasheh void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb);
157ccd979bdSMark Fasheh void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
158ccd979bdSMark Fasheh 				       int cancel);
159ccd979bdSMark Fasheh int ocfs2_flush_truncate_log(struct ocfs2_super *osb);
160ccd979bdSMark Fasheh int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
161ccd979bdSMark Fasheh 				      int slot_num,
162ccd979bdSMark Fasheh 				      struct ocfs2_dinode **tl_copy);
163ccd979bdSMark Fasheh int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
164ccd979bdSMark Fasheh 					 struct ocfs2_dinode *tl_copy);
165063c4561SMark Fasheh int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb);
166063c4561SMark Fasheh int ocfs2_truncate_log_append(struct ocfs2_super *osb,
167063c4561SMark Fasheh 			      handle_t *handle,
168063c4561SMark Fasheh 			      u64 start_blk,
169063c4561SMark Fasheh 			      unsigned int num_clusters);
170063c4561SMark Fasheh int __ocfs2_flush_truncate_log(struct ocfs2_super *osb);
171ccd979bdSMark Fasheh 
1722b604351SMark Fasheh /*
1732b604351SMark Fasheh  * Process local structure which describes the block unlinks done
1742b604351SMark Fasheh  * during an operation. This is populated via
1752b604351SMark Fasheh  * ocfs2_cache_block_dealloc().
1762b604351SMark Fasheh  *
1772b604351SMark Fasheh  * ocfs2_run_deallocs() should be called after the potentially
1782b604351SMark Fasheh  * de-allocating routines. No journal handles should be open, and most
1792b604351SMark Fasheh  * locks should have been dropped.
1802b604351SMark Fasheh  */
1812b604351SMark Fasheh struct ocfs2_cached_dealloc_ctxt {
1822b604351SMark Fasheh 	struct ocfs2_per_slot_free_list		*c_first_suballocator;
1832891d290STao Ma 	struct ocfs2_cached_block_free 		*c_global_allocator;
1842b604351SMark Fasheh };
1852b604351SMark Fasheh static inline void ocfs2_init_dealloc_ctxt(struct ocfs2_cached_dealloc_ctxt *c)
1862b604351SMark Fasheh {
1872b604351SMark Fasheh 	c->c_first_suballocator = NULL;
1882891d290STao Ma 	c->c_global_allocator = NULL;
1892b604351SMark Fasheh }
1902891d290STao Ma int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
1912891d290STao Ma 				u64 blkno, unsigned int bit);
19278f30c31STao Ma static inline int ocfs2_dealloc_has_cluster(struct ocfs2_cached_dealloc_ctxt *c)
19378f30c31STao Ma {
19478f30c31STao Ma 	return c->c_global_allocator != NULL;
19578f30c31STao Ma }
1962b604351SMark Fasheh int ocfs2_run_deallocs(struct ocfs2_super *osb,
1972b604351SMark Fasheh 		       struct ocfs2_cached_dealloc_ctxt *ctxt);
1982b604351SMark Fasheh 
199ccd979bdSMark Fasheh struct ocfs2_truncate_context {
20059a5e416SMark Fasheh 	struct ocfs2_cached_dealloc_ctxt tc_dealloc;
201ccd979bdSMark Fasheh 	int tc_ext_alloc_locked; /* is it cluster locked? */
202ccd979bdSMark Fasheh 	/* these get destroyed once it's passed to ocfs2_commit_truncate. */
203ccd979bdSMark Fasheh 	struct buffer_head *tc_last_eb_bh;
204ccd979bdSMark Fasheh };
205ccd979bdSMark Fasheh 
20635edec1dSMark Fasheh int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
20735edec1dSMark Fasheh 				  u64 range_start, u64 range_end);
208ccd979bdSMark Fasheh int ocfs2_prepare_truncate(struct ocfs2_super *osb,
209ccd979bdSMark Fasheh 			   struct inode *inode,
210ccd979bdSMark Fasheh 			   struct buffer_head *fe_bh,
211ccd979bdSMark Fasheh 			   struct ocfs2_truncate_context **tc);
212ccd979bdSMark Fasheh int ocfs2_commit_truncate(struct ocfs2_super *osb,
213ccd979bdSMark Fasheh 			  struct inode *inode,
214ccd979bdSMark Fasheh 			  struct buffer_head *fe_bh,
215ccd979bdSMark Fasheh 			  struct ocfs2_truncate_context *tc);
2161afc32b9SMark Fasheh int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
2171afc32b9SMark Fasheh 			  unsigned int start, unsigned int end, int trunc);
218ccd979bdSMark Fasheh 
219363041a5SMark Fasheh int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el,
220363041a5SMark Fasheh 		    u32 cpos, struct buffer_head **leaf_bh);
221328d5752SMark Fasheh int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster);
222363041a5SMark Fasheh 
223e48edee2SMark Fasheh /*
224e48edee2SMark Fasheh  * Helper function to look at the # of clusters in an extent record.
225e48edee2SMark Fasheh  */
226e48edee2SMark Fasheh static inline unsigned int ocfs2_rec_clusters(struct ocfs2_extent_list *el,
227e48edee2SMark Fasheh 					      struct ocfs2_extent_rec *rec)
228e48edee2SMark Fasheh {
229e48edee2SMark Fasheh 	/*
230e48edee2SMark Fasheh 	 * Cluster count in extent records is slightly different
231e48edee2SMark Fasheh 	 * between interior nodes and leaf nodes. This is to support
232e48edee2SMark Fasheh 	 * unwritten extents which need a flags field in leaf node
233e48edee2SMark Fasheh 	 * records, thus shrinking the available space for a clusters
234e48edee2SMark Fasheh 	 * field.
235e48edee2SMark Fasheh 	 */
236e48edee2SMark Fasheh 	if (el->l_tree_depth)
237e48edee2SMark Fasheh 		return le32_to_cpu(rec->e_int_clusters);
238e48edee2SMark Fasheh 	else
239e48edee2SMark Fasheh 		return le16_to_cpu(rec->e_leaf_clusters);
240e48edee2SMark Fasheh }
241e48edee2SMark Fasheh 
24200dc417fSMark Fasheh /*
24300dc417fSMark Fasheh  * This is only valid for leaf nodes, which are the only ones that can
24400dc417fSMark Fasheh  * have empty extents anyway.
24500dc417fSMark Fasheh  */
24600dc417fSMark Fasheh static inline int ocfs2_is_empty_extent(struct ocfs2_extent_rec *rec)
24700dc417fSMark Fasheh {
24800dc417fSMark Fasheh 	return !rec->e_leaf_clusters;
24900dc417fSMark Fasheh }
25000dc417fSMark Fasheh 
251ccd979bdSMark Fasheh #endif /* OCFS2_ALLOC_H */
252