xref: /openbmc/linux/fs/ocfs2/alloc.h (revision fa60ce2c)
1328970deSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
2*fa60ce2cSMasahiro Yamada /*
3ccd979bdSMark Fasheh  * alloc.h
4ccd979bdSMark Fasheh  *
5ccd979bdSMark Fasheh  * Function prototypes
6ccd979bdSMark Fasheh  *
7ccd979bdSMark Fasheh  * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
8ccd979bdSMark Fasheh  */
9ccd979bdSMark Fasheh 
10ccd979bdSMark Fasheh #ifndef OCFS2_ALLOC_H
11ccd979bdSMark Fasheh #define OCFS2_ALLOC_H
12ccd979bdSMark Fasheh 
13e7d4cb6bSTao Ma 
14ca12b7c4STao Ma /*
15ca12b7c4STao Ma  * For xattr tree leaf, we limit the leaf byte size to be 64K.
16ca12b7c4STao Ma  */
17ca12b7c4STao Ma #define OCFS2_MAX_XATTR_TREE_LEAF_SIZE 65536
18ca12b7c4STao Ma 
19f99b9b7cSJoel Becker /*
20f99b9b7cSJoel Becker  * ocfs2_extent_tree and ocfs2_extent_tree_operations are used to abstract
21f99b9b7cSJoel Becker  * the b-tree operations in ocfs2. Now all the b-tree operations are not
22f99b9b7cSJoel Becker  * limited to ocfs2_dinode only. Any data which need to allocate clusters
23f99b9b7cSJoel Becker  * to store can use b-tree. And it only needs to implement its ocfs2_extent_tree
24f99b9b7cSJoel Becker  * and operation.
25f99b9b7cSJoel Becker  *
26f99b9b7cSJoel Becker  * ocfs2_extent_tree becomes the first-class object for extent tree
27f99b9b7cSJoel Becker  * manipulation.  Callers of the alloc.c code need to fill it via one of
288d6220d6SJoel Becker  * the ocfs2_init_*_extent_tree() operations below.
29f99b9b7cSJoel Becker  *
30f99b9b7cSJoel Becker  * ocfs2_extent_tree contains info for the root of the b-tree, it must have a
31f99b9b7cSJoel Becker  * root ocfs2_extent_list and a root_bh so that they can be used in the b-tree
32d9a0a1f8SJoel Becker  * functions.  It needs the ocfs2_caching_info structure associated with
33d9a0a1f8SJoel Becker  * I/O on the tree.  With metadata ecc, we now call different journal_access
3413723d00SJoel Becker  * functions for each type of metadata, so it must have the
3513723d00SJoel Becker  * root_journal_access function.
36f99b9b7cSJoel Becker  * ocfs2_extent_tree_operations abstract the normal operations we do for
37f99b9b7cSJoel Becker  * the root of extent b-tree.
38f99b9b7cSJoel Becker  */
39f99b9b7cSJoel Becker struct ocfs2_extent_tree_operations;
40f99b9b7cSJoel Becker struct ocfs2_extent_tree {
419e62dc09SJulia Lawall 	const struct ocfs2_extent_tree_operations *et_ops;
42f99b9b7cSJoel Becker 	struct buffer_head			*et_root_bh;
43f99b9b7cSJoel Becker 	struct ocfs2_extent_list		*et_root_el;
44d9a0a1f8SJoel Becker 	struct ocfs2_caching_info		*et_ci;
4513723d00SJoel Becker 	ocfs2_journal_access_func		et_root_journal_access;
46f99b9b7cSJoel Becker 	void					*et_object;
47f99b9b7cSJoel Becker 	unsigned int				et_max_leaf_clusters;
4871a36944SChangwei Ge 	struct ocfs2_cached_dealloc_ctxt	*et_dealloc;
49f99b9b7cSJoel Becker };
50f99b9b7cSJoel Becker 
51f99b9b7cSJoel Becker /*
528d6220d6SJoel Becker  * ocfs2_init_*_extent_tree() will fill an ocfs2_extent_tree from the
538d6220d6SJoel Becker  * specified object buffer.
54f99b9b7cSJoel Becker  */
558d6220d6SJoel Becker void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
565e404e9eSJoel Becker 				   struct ocfs2_caching_info *ci,
57f99b9b7cSJoel Becker 				   struct buffer_head *bh);
588d6220d6SJoel Becker void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
595e404e9eSJoel Becker 				       struct ocfs2_caching_info *ci,
60f99b9b7cSJoel Becker 				       struct buffer_head *bh);
612a50a743SJoel Becker struct ocfs2_xattr_value_buf;
628d6220d6SJoel Becker void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
635e404e9eSJoel Becker 					struct ocfs2_caching_info *ci,
642a50a743SJoel Becker 					struct ocfs2_xattr_value_buf *vb);
659b7895efSMark Fasheh void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree *et,
665e404e9eSJoel Becker 				    struct ocfs2_caching_info *ci,
679b7895efSMark Fasheh 				    struct buffer_head *bh);
68fe924415STao Ma void ocfs2_init_refcount_extent_tree(struct ocfs2_extent_tree *et,
69fe924415STao Ma 				     struct ocfs2_caching_info *ci,
70fe924415STao Ma 				     struct buffer_head *bh);
71f99b9b7cSJoel Becker 
725e96581aSJoel Becker /*
735e96581aSJoel Becker  * Read an extent block into *bh.  If *bh is NULL, a bh will be
745e96581aSJoel Becker  * allocated.  This is a cached read.  The extent block will be validated
755e96581aSJoel Becker  * with ocfs2_validate_extent_block().
765e96581aSJoel Becker  */
773d03a305SJoel Becker int ocfs2_read_extent_block(struct ocfs2_caching_info *ci, u64 eb_blkno,
785e96581aSJoel Becker 			    struct buffer_head **bh);
795e96581aSJoel Becker 
80f99b9b7cSJoel Becker struct ocfs2_alloc_context;
81cc79d8c1SJoel Becker int ocfs2_insert_extent(handle_t *handle,
82f99b9b7cSJoel Becker 			struct ocfs2_extent_tree *et,
83ba492615STao Ma 			u32 cpos,
84ba492615STao Ma 			u64 start_blk,
85ba492615STao Ma 			u32 new_clusters,
86ba492615STao Ma 			u8 flags,
87ba492615STao Ma 			struct ocfs2_alloc_context *meta_ac);
88f99b9b7cSJoel Becker 
890eb8d47eSTao Ma enum ocfs2_alloc_restarted {
900eb8d47eSTao Ma 	RESTART_NONE = 0,
910eb8d47eSTao Ma 	RESTART_TRANS,
920eb8d47eSTao Ma 	RESTART_META
930eb8d47eSTao Ma };
94cbee7e1aSJoel Becker int ocfs2_add_clusters_in_btree(handle_t *handle,
95cbee7e1aSJoel Becker 				struct ocfs2_extent_tree *et,
960eb8d47eSTao Ma 				u32 *logical_offset,
970eb8d47eSTao Ma 				u32 clusters_to_add,
980eb8d47eSTao Ma 				int mark_unwritten,
990eb8d47eSTao Ma 				struct ocfs2_alloc_context *data_ac,
1000eb8d47eSTao Ma 				struct ocfs2_alloc_context *meta_ac,
101f99b9b7cSJoel Becker 				enum ocfs2_alloc_restarted *reason_ret);
102328d5752SMark Fasheh struct ocfs2_cached_dealloc_ctxt;
103e2e9f608STao Ma struct ocfs2_path;
104e2e9f608STao Ma int ocfs2_split_extent(handle_t *handle,
105e2e9f608STao Ma 		       struct ocfs2_extent_tree *et,
106e2e9f608STao Ma 		       struct ocfs2_path *path,
107e2e9f608STao Ma 		       int split_index,
108e2e9f608STao Ma 		       struct ocfs2_extent_rec *split_rec,
109e2e9f608STao Ma 		       struct ocfs2_alloc_context *meta_ac,
110e2e9f608STao Ma 		       struct ocfs2_cached_dealloc_ctxt *dealloc);
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);
1161aa75feaSTao Ma int ocfs2_change_extent_flag(handle_t *handle,
1171aa75feaSTao Ma 			     struct ocfs2_extent_tree *et,
1181aa75feaSTao Ma 			     u32 cpos, u32 len, u32 phys,
1191aa75feaSTao Ma 			     struct ocfs2_alloc_context *meta_ac,
1201aa75feaSTao Ma 			     struct ocfs2_cached_dealloc_ctxt *dealloc,
1211aa75feaSTao Ma 			     int new_flags, int clear_flags);
122dbdcf6a4SJoel Becker int ocfs2_remove_extent(handle_t *handle, struct ocfs2_extent_tree *et,
123dbdcf6a4SJoel Becker 			u32 cpos, u32 len,
124063c4561SMark Fasheh 			struct ocfs2_alloc_context *meta_ac,
125f99b9b7cSJoel Becker 			struct ocfs2_cached_dealloc_ctxt *dealloc);
126fecc0112SMark Fasheh int ocfs2_remove_btree_range(struct inode *inode,
127fecc0112SMark Fasheh 			     struct ocfs2_extent_tree *et,
12878f94673STristan Ye 			     u32 cpos, u32 phys_cpos, u32 len, int flags,
12978f94673STristan Ye 			     struct ocfs2_cached_dealloc_ctxt *dealloc,
130f62f12b3SJunxiao Bi 			     u64 refcount_loc, bool refcount_tree_locked);
131fecc0112SMark Fasheh 
132964f14a0SJun Piao int ocfs2_num_free_extents(struct ocfs2_extent_tree *et);
133e7d4cb6bSTao Ma 
134811f933dSTao Ma /*
135811f933dSTao Ma  * how many new metadata chunks would an allocation need at maximum?
136811f933dSTao Ma  *
137811f933dSTao Ma  * Please note that the caller must make sure that root_el is the root
138811f933dSTao Ma  * of extent tree. So for an inode, it should be &fe->id2.i_list. Otherwise
139811f933dSTao Ma  * the result may be wrong.
140811f933dSTao Ma  */
ocfs2_extend_meta_needed(struct ocfs2_extent_list * root_el)141811f933dSTao Ma static inline int ocfs2_extend_meta_needed(struct ocfs2_extent_list *root_el)
142ccd979bdSMark Fasheh {
143ccd979bdSMark Fasheh 	/*
144ccd979bdSMark Fasheh 	 * Rather than do all the work of determining how much we need
145ccd979bdSMark Fasheh 	 * (involves a ton of reads and locks), just ask for the
146ccd979bdSMark Fasheh 	 * maximal limit.  That's a tree depth shift.  So, one block for
147ccd979bdSMark Fasheh 	 * level of the tree (current l_tree_depth), one block for the
148ccd979bdSMark Fasheh 	 * new tree_depth==0 extent_block, and one block at the new
149ccd979bdSMark Fasheh 	 * top-of-the tree.
150ccd979bdSMark Fasheh 	 */
151811f933dSTao Ma 	return le16_to_cpu(root_el->l_tree_depth) + 2;
152ccd979bdSMark Fasheh }
153ccd979bdSMark Fasheh 
1545b6a3a2bSMark Fasheh void ocfs2_dinode_new_extent_list(struct inode *inode, struct ocfs2_dinode *di);
1551afc32b9SMark Fasheh void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di);
1561afc32b9SMark Fasheh int ocfs2_convert_inline_data_to_extents(struct inode *inode,
1571afc32b9SMark Fasheh 					 struct buffer_head *di_bh);
1581afc32b9SMark Fasheh 
159ccd979bdSMark Fasheh int ocfs2_truncate_log_init(struct ocfs2_super *osb);
160ccd979bdSMark Fasheh void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb);
161ccd979bdSMark Fasheh void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
162ccd979bdSMark Fasheh 				       int cancel);
163ccd979bdSMark Fasheh int ocfs2_flush_truncate_log(struct ocfs2_super *osb);
164ccd979bdSMark Fasheh int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
165ccd979bdSMark Fasheh 				      int slot_num,
166ccd979bdSMark Fasheh 				      struct ocfs2_dinode **tl_copy);
167ccd979bdSMark Fasheh int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
168ccd979bdSMark Fasheh 					 struct ocfs2_dinode *tl_copy);
169063c4561SMark Fasheh int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb);
170063c4561SMark Fasheh int ocfs2_truncate_log_append(struct ocfs2_super *osb,
171063c4561SMark Fasheh 			      handle_t *handle,
172063c4561SMark Fasheh 			      u64 start_blk,
173063c4561SMark Fasheh 			      unsigned int num_clusters);
174063c4561SMark Fasheh int __ocfs2_flush_truncate_log(struct ocfs2_super *osb);
1752070ad1aSEric Ren int ocfs2_try_to_free_truncate_log(struct ocfs2_super *osb,
1762070ad1aSEric Ren 				   unsigned int needed);
177ccd979bdSMark Fasheh 
1782b604351SMark Fasheh /*
1792b604351SMark Fasheh  * Process local structure which describes the block unlinks done
1802b604351SMark Fasheh  * during an operation. This is populated via
1812b604351SMark Fasheh  * ocfs2_cache_block_dealloc().
1822b604351SMark Fasheh  *
1832b604351SMark Fasheh  * ocfs2_run_deallocs() should be called after the potentially
1842b604351SMark Fasheh  * de-allocating routines. No journal handles should be open, and most
1852b604351SMark Fasheh  * locks should have been dropped.
1862b604351SMark Fasheh  */
1872b604351SMark Fasheh struct ocfs2_cached_dealloc_ctxt {
1882b604351SMark Fasheh 	struct ocfs2_per_slot_free_list		*c_first_suballocator;
1892891d290STao Ma 	struct ocfs2_cached_block_free 		*c_global_allocator;
1902b604351SMark Fasheh };
ocfs2_init_dealloc_ctxt(struct ocfs2_cached_dealloc_ctxt * c)1912b604351SMark Fasheh static inline void ocfs2_init_dealloc_ctxt(struct ocfs2_cached_dealloc_ctxt *c)
1922b604351SMark Fasheh {
1932b604351SMark Fasheh 	c->c_first_suballocator = NULL;
1942891d290STao Ma 	c->c_global_allocator = NULL;
1952b604351SMark Fasheh }
1962891d290STao Ma int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
1972891d290STao Ma 				u64 blkno, unsigned int bit);
1981823cb0bSTao Ma int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
19974380c47STao Ma 			      int type, int slot, u64 suballoc, u64 blkno,
2001823cb0bSTao Ma 			      unsigned int bit);
ocfs2_dealloc_has_cluster(struct ocfs2_cached_dealloc_ctxt * c)20178f30c31STao Ma static inline int ocfs2_dealloc_has_cluster(struct ocfs2_cached_dealloc_ctxt *c)
20278f30c31STao Ma {
20378f30c31STao Ma 	return c->c_global_allocator != NULL;
20478f30c31STao Ma }
2052b604351SMark Fasheh int ocfs2_run_deallocs(struct ocfs2_super *osb,
2062b604351SMark Fasheh 		       struct ocfs2_cached_dealloc_ctxt *ctxt);
2072b604351SMark Fasheh 
208ccd979bdSMark Fasheh struct ocfs2_truncate_context {
20959a5e416SMark Fasheh 	struct ocfs2_cached_dealloc_ctxt tc_dealloc;
210ccd979bdSMark Fasheh 	int tc_ext_alloc_locked; /* is it cluster locked? */
211ccd979bdSMark Fasheh 	/* these get destroyed once it's passed to ocfs2_commit_truncate. */
212ccd979bdSMark Fasheh 	struct buffer_head *tc_last_eb_bh;
213ccd979bdSMark Fasheh };
214ccd979bdSMark Fasheh 
21535edec1dSMark Fasheh int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
21635edec1dSMark Fasheh 				  u64 range_start, u64 range_end);
217ccd979bdSMark Fasheh int ocfs2_commit_truncate(struct ocfs2_super *osb,
218ccd979bdSMark Fasheh 			  struct inode *inode,
21978f94673STristan Ye 			  struct buffer_head *di_bh);
2201afc32b9SMark Fasheh int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
2211afc32b9SMark Fasheh 			  unsigned int start, unsigned int end, int trunc);
222ccd979bdSMark Fasheh 
223facdb77fSJoel Becker int ocfs2_find_leaf(struct ocfs2_caching_info *ci,
224facdb77fSJoel Becker 		    struct ocfs2_extent_list *root_el, u32 cpos,
225facdb77fSJoel Becker 		    struct buffer_head **leaf_bh);
226328d5752SMark Fasheh int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster);
227363041a5SMark Fasheh 
228e80de36dSTao Ma int ocfs2_trim_fs(struct super_block *sb, struct fstrim_range *range);
229e48edee2SMark Fasheh /*
230e48edee2SMark Fasheh  * Helper function to look at the # of clusters in an extent record.
231e48edee2SMark Fasheh  */
ocfs2_rec_clusters(struct ocfs2_extent_list * el,struct ocfs2_extent_rec * rec)232e48edee2SMark Fasheh static inline unsigned int ocfs2_rec_clusters(struct ocfs2_extent_list *el,
233e48edee2SMark Fasheh 					      struct ocfs2_extent_rec *rec)
234e48edee2SMark Fasheh {
235e48edee2SMark Fasheh 	/*
236e48edee2SMark Fasheh 	 * Cluster count in extent records is slightly different
237e48edee2SMark Fasheh 	 * between interior nodes and leaf nodes. This is to support
238e48edee2SMark Fasheh 	 * unwritten extents which need a flags field in leaf node
239e48edee2SMark Fasheh 	 * records, thus shrinking the available space for a clusters
240e48edee2SMark Fasheh 	 * field.
241e48edee2SMark Fasheh 	 */
242e48edee2SMark Fasheh 	if (el->l_tree_depth)
243e48edee2SMark Fasheh 		return le32_to_cpu(rec->e_int_clusters);
244e48edee2SMark Fasheh 	else
245e48edee2SMark Fasheh 		return le16_to_cpu(rec->e_leaf_clusters);
246e48edee2SMark Fasheh }
247e48edee2SMark Fasheh 
24800dc417fSMark Fasheh /*
24900dc417fSMark Fasheh  * This is only valid for leaf nodes, which are the only ones that can
25000dc417fSMark Fasheh  * have empty extents anyway.
25100dc417fSMark Fasheh  */
ocfs2_is_empty_extent(struct ocfs2_extent_rec * rec)25200dc417fSMark Fasheh static inline int ocfs2_is_empty_extent(struct ocfs2_extent_rec *rec)
25300dc417fSMark Fasheh {
25400dc417fSMark Fasheh 	return !rec->e_leaf_clusters;
25500dc417fSMark Fasheh }
25600dc417fSMark Fasheh 
2576f70fa51STao Ma int ocfs2_grab_pages(struct inode *inode, loff_t start, loff_t end,
2586f70fa51STao Ma 		     struct page **pages, int *num);
2596f70fa51STao Ma void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
2606f70fa51STao Ma 			      unsigned int from, unsigned int to,
2616f70fa51STao Ma 			      struct page *page, int zero, u64 *phys);
262e2e9f608STao Ma /*
263e2e9f608STao Ma  * Structures which describe a path through a btree, and functions to
264e2e9f608STao Ma  * manipulate them.
265e2e9f608STao Ma  *
266e2e9f608STao Ma  * The idea here is to be as generic as possible with the tree
267e2e9f608STao Ma  * manipulation code.
268e2e9f608STao Ma  */
269e2e9f608STao Ma struct ocfs2_path_item {
270e2e9f608STao Ma 	struct buffer_head		*bh;
271e2e9f608STao Ma 	struct ocfs2_extent_list	*el;
272e2e9f608STao Ma };
273e2e9f608STao Ma 
274e2e9f608STao Ma #define OCFS2_MAX_PATH_DEPTH	5
275e2e9f608STao Ma 
276e2e9f608STao Ma struct ocfs2_path {
277e2e9f608STao Ma 	int				p_tree_depth;
278e2e9f608STao Ma 	ocfs2_journal_access_func	p_root_access;
279e2e9f608STao Ma 	struct ocfs2_path_item		p_node[OCFS2_MAX_PATH_DEPTH];
280e2e9f608STao Ma };
281e2e9f608STao Ma 
282e2e9f608STao Ma #define path_root_bh(_path) ((_path)->p_node[0].bh)
283e2e9f608STao Ma #define path_root_el(_path) ((_path)->p_node[0].el)
284e2e9f608STao Ma #define path_root_access(_path)((_path)->p_root_access)
285e2e9f608STao Ma #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
286e2e9f608STao Ma #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
287e2e9f608STao Ma #define path_num_items(_path) ((_path)->p_tree_depth + 1)
288e2e9f608STao Ma 
289e2e9f608STao Ma void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root);
290e2e9f608STao Ma void ocfs2_free_path(struct ocfs2_path *path);
291e2e9f608STao Ma int ocfs2_find_path(struct ocfs2_caching_info *ci,
292e2e9f608STao Ma 		    struct ocfs2_path *path,
293e2e9f608STao Ma 		    u32 cpos);
294e2e9f608STao Ma struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path);
295e2e9f608STao Ma struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et);
296e2e9f608STao Ma int ocfs2_path_bh_journal_access(handle_t *handle,
297e2e9f608STao Ma 				 struct ocfs2_caching_info *ci,
298e2e9f608STao Ma 				 struct ocfs2_path *path,
299e2e9f608STao Ma 				 int idx);
300e2e9f608STao Ma int ocfs2_journal_access_path(struct ocfs2_caching_info *ci,
301e2e9f608STao Ma 			      handle_t *handle,
302e2e9f608STao Ma 			      struct ocfs2_path *path);
30338a04e43STao Ma int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
30438a04e43STao Ma 				   struct ocfs2_path *path, u32 *cpos);
305ee149a7cSTristan Ye int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
306ee149a7cSTristan Ye 				  struct ocfs2_path *path, u32 *cpos);
30738a04e43STao Ma int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et,
30838a04e43STao Ma 			    struct ocfs2_path *left,
30938a04e43STao Ma 			    struct ocfs2_path *right);
310ccd979bdSMark Fasheh #endif /* OCFS2_ALLOC_H */
311