xref: /openbmc/linux/fs/ocfs2/alloc.h (revision 328970de)
1328970deSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
2ccd979bdSMark Fasheh /* -*- mode: c; c-basic-offset: 8; -*-
3ccd979bdSMark Fasheh  * vim: noexpandtab sw=8 ts=8 sts=0:
4ccd979bdSMark Fasheh  *
5ccd979bdSMark Fasheh  * alloc.h
6ccd979bdSMark Fasheh  *
7ccd979bdSMark Fasheh  * Function prototypes
8ccd979bdSMark Fasheh  *
9ccd979bdSMark Fasheh  * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
10ccd979bdSMark Fasheh  */
11ccd979bdSMark Fasheh 
12ccd979bdSMark Fasheh #ifndef OCFS2_ALLOC_H
13ccd979bdSMark Fasheh #define OCFS2_ALLOC_H
14ccd979bdSMark Fasheh 
15e7d4cb6bSTao Ma 
16ca12b7c4STao Ma /*
17ca12b7c4STao Ma  * For xattr tree leaf, we limit the leaf byte size to be 64K.
18ca12b7c4STao Ma  */
19ca12b7c4STao Ma #define OCFS2_MAX_XATTR_TREE_LEAF_SIZE 65536
20ca12b7c4STao Ma 
21f99b9b7cSJoel Becker /*
22f99b9b7cSJoel Becker  * ocfs2_extent_tree and ocfs2_extent_tree_operations are used to abstract
23f99b9b7cSJoel Becker  * the b-tree operations in ocfs2. Now all the b-tree operations are not
24f99b9b7cSJoel Becker  * limited to ocfs2_dinode only. Any data which need to allocate clusters
25f99b9b7cSJoel Becker  * to store can use b-tree. And it only needs to implement its ocfs2_extent_tree
26f99b9b7cSJoel Becker  * and operation.
27f99b9b7cSJoel Becker  *
28f99b9b7cSJoel Becker  * ocfs2_extent_tree becomes the first-class object for extent tree
29f99b9b7cSJoel Becker  * manipulation.  Callers of the alloc.c code need to fill it via one of
308d6220d6SJoel Becker  * the ocfs2_init_*_extent_tree() operations below.
31f99b9b7cSJoel Becker  *
32f99b9b7cSJoel Becker  * ocfs2_extent_tree contains info for the root of the b-tree, it must have a
33f99b9b7cSJoel Becker  * root ocfs2_extent_list and a root_bh so that they can be used in the b-tree
34d9a0a1f8SJoel Becker  * functions.  It needs the ocfs2_caching_info structure associated with
35d9a0a1f8SJoel Becker  * I/O on the tree.  With metadata ecc, we now call different journal_access
3613723d00SJoel Becker  * functions for each type of metadata, so it must have the
3713723d00SJoel Becker  * root_journal_access function.
38f99b9b7cSJoel Becker  * ocfs2_extent_tree_operations abstract the normal operations we do for
39f99b9b7cSJoel Becker  * the root of extent b-tree.
40f99b9b7cSJoel Becker  */
41f99b9b7cSJoel Becker struct ocfs2_extent_tree_operations;
42f99b9b7cSJoel Becker struct ocfs2_extent_tree {
439e62dc09SJulia Lawall 	const struct ocfs2_extent_tree_operations *et_ops;
44f99b9b7cSJoel Becker 	struct buffer_head			*et_root_bh;
45f99b9b7cSJoel Becker 	struct ocfs2_extent_list		*et_root_el;
46d9a0a1f8SJoel Becker 	struct ocfs2_caching_info		*et_ci;
4713723d00SJoel Becker 	ocfs2_journal_access_func		et_root_journal_access;
48f99b9b7cSJoel Becker 	void					*et_object;
49f99b9b7cSJoel Becker 	unsigned int				et_max_leaf_clusters;
5071a36944SChangwei Ge 	struct ocfs2_cached_dealloc_ctxt	*et_dealloc;
51f99b9b7cSJoel Becker };
52f99b9b7cSJoel Becker 
53f99b9b7cSJoel Becker /*
548d6220d6SJoel Becker  * ocfs2_init_*_extent_tree() will fill an ocfs2_extent_tree from the
558d6220d6SJoel Becker  * specified object buffer.
56f99b9b7cSJoel Becker  */
578d6220d6SJoel Becker void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
585e404e9eSJoel Becker 				   struct ocfs2_caching_info *ci,
59f99b9b7cSJoel Becker 				   struct buffer_head *bh);
608d6220d6SJoel Becker void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
615e404e9eSJoel Becker 				       struct ocfs2_caching_info *ci,
62f99b9b7cSJoel Becker 				       struct buffer_head *bh);
632a50a743SJoel Becker struct ocfs2_xattr_value_buf;
648d6220d6SJoel Becker void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
655e404e9eSJoel Becker 					struct ocfs2_caching_info *ci,
662a50a743SJoel Becker 					struct ocfs2_xattr_value_buf *vb);
679b7895efSMark Fasheh void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree *et,
685e404e9eSJoel Becker 				    struct ocfs2_caching_info *ci,
699b7895efSMark Fasheh 				    struct buffer_head *bh);
70fe924415STao Ma void ocfs2_init_refcount_extent_tree(struct ocfs2_extent_tree *et,
71fe924415STao Ma 				     struct ocfs2_caching_info *ci,
72fe924415STao Ma 				     struct buffer_head *bh);
73f99b9b7cSJoel Becker 
745e96581aSJoel Becker /*
755e96581aSJoel Becker  * Read an extent block into *bh.  If *bh is NULL, a bh will be
765e96581aSJoel Becker  * allocated.  This is a cached read.  The extent block will be validated
775e96581aSJoel Becker  * with ocfs2_validate_extent_block().
785e96581aSJoel Becker  */
793d03a305SJoel Becker int ocfs2_read_extent_block(struct ocfs2_caching_info *ci, u64 eb_blkno,
805e96581aSJoel Becker 			    struct buffer_head **bh);
815e96581aSJoel Becker 
82f99b9b7cSJoel Becker struct ocfs2_alloc_context;
83cc79d8c1SJoel Becker int ocfs2_insert_extent(handle_t *handle,
84f99b9b7cSJoel Becker 			struct ocfs2_extent_tree *et,
85ba492615STao Ma 			u32 cpos,
86ba492615STao Ma 			u64 start_blk,
87ba492615STao Ma 			u32 new_clusters,
88ba492615STao Ma 			u8 flags,
89ba492615STao Ma 			struct ocfs2_alloc_context *meta_ac);
90f99b9b7cSJoel Becker 
910eb8d47eSTao Ma enum ocfs2_alloc_restarted {
920eb8d47eSTao Ma 	RESTART_NONE = 0,
930eb8d47eSTao Ma 	RESTART_TRANS,
940eb8d47eSTao Ma 	RESTART_META
950eb8d47eSTao Ma };
96cbee7e1aSJoel Becker int ocfs2_add_clusters_in_btree(handle_t *handle,
97cbee7e1aSJoel Becker 				struct ocfs2_extent_tree *et,
980eb8d47eSTao Ma 				u32 *logical_offset,
990eb8d47eSTao Ma 				u32 clusters_to_add,
1000eb8d47eSTao Ma 				int mark_unwritten,
1010eb8d47eSTao Ma 				struct ocfs2_alloc_context *data_ac,
1020eb8d47eSTao Ma 				struct ocfs2_alloc_context *meta_ac,
103f99b9b7cSJoel Becker 				enum ocfs2_alloc_restarted *reason_ret);
104328d5752SMark Fasheh struct ocfs2_cached_dealloc_ctxt;
105e2e9f608STao Ma struct ocfs2_path;
106e2e9f608STao Ma int ocfs2_split_extent(handle_t *handle,
107e2e9f608STao Ma 		       struct ocfs2_extent_tree *et,
108e2e9f608STao Ma 		       struct ocfs2_path *path,
109e2e9f608STao Ma 		       int split_index,
110e2e9f608STao Ma 		       struct ocfs2_extent_rec *split_rec,
111e2e9f608STao Ma 		       struct ocfs2_alloc_context *meta_ac,
112e2e9f608STao Ma 		       struct ocfs2_cached_dealloc_ctxt *dealloc);
113f99b9b7cSJoel Becker int ocfs2_mark_extent_written(struct inode *inode,
114f99b9b7cSJoel Becker 			      struct ocfs2_extent_tree *et,
115328d5752SMark Fasheh 			      handle_t *handle, u32 cpos, u32 len, u32 phys,
116328d5752SMark Fasheh 			      struct ocfs2_alloc_context *meta_ac,
117f99b9b7cSJoel Becker 			      struct ocfs2_cached_dealloc_ctxt *dealloc);
1181aa75feaSTao Ma int ocfs2_change_extent_flag(handle_t *handle,
1191aa75feaSTao Ma 			     struct ocfs2_extent_tree *et,
1201aa75feaSTao Ma 			     u32 cpos, u32 len, u32 phys,
1211aa75feaSTao Ma 			     struct ocfs2_alloc_context *meta_ac,
1221aa75feaSTao Ma 			     struct ocfs2_cached_dealloc_ctxt *dealloc,
1231aa75feaSTao Ma 			     int new_flags, int clear_flags);
124dbdcf6a4SJoel Becker int ocfs2_remove_extent(handle_t *handle, struct ocfs2_extent_tree *et,
125dbdcf6a4SJoel Becker 			u32 cpos, u32 len,
126063c4561SMark Fasheh 			struct ocfs2_alloc_context *meta_ac,
127f99b9b7cSJoel Becker 			struct ocfs2_cached_dealloc_ctxt *dealloc);
128fecc0112SMark Fasheh int ocfs2_remove_btree_range(struct inode *inode,
129fecc0112SMark Fasheh 			     struct ocfs2_extent_tree *et,
13078f94673STristan Ye 			     u32 cpos, u32 phys_cpos, u32 len, int flags,
13178f94673STristan Ye 			     struct ocfs2_cached_dealloc_ctxt *dealloc,
132f62f12b3SJunxiao Bi 			     u64 refcount_loc, bool refcount_tree_locked);
133fecc0112SMark Fasheh 
134964f14a0SJun Piao int ocfs2_num_free_extents(struct ocfs2_extent_tree *et);
135e7d4cb6bSTao Ma 
136811f933dSTao Ma /*
137811f933dSTao Ma  * how many new metadata chunks would an allocation need at maximum?
138811f933dSTao Ma  *
139811f933dSTao Ma  * Please note that the caller must make sure that root_el is the root
140811f933dSTao Ma  * of extent tree. So for an inode, it should be &fe->id2.i_list. Otherwise
141811f933dSTao Ma  * the result may be wrong.
142811f933dSTao Ma  */
143811f933dSTao Ma static inline int ocfs2_extend_meta_needed(struct ocfs2_extent_list *root_el)
144ccd979bdSMark Fasheh {
145ccd979bdSMark Fasheh 	/*
146ccd979bdSMark Fasheh 	 * Rather than do all the work of determining how much we need
147ccd979bdSMark Fasheh 	 * (involves a ton of reads and locks), just ask for the
148ccd979bdSMark Fasheh 	 * maximal limit.  That's a tree depth shift.  So, one block for
149ccd979bdSMark Fasheh 	 * level of the tree (current l_tree_depth), one block for the
150ccd979bdSMark Fasheh 	 * new tree_depth==0 extent_block, and one block at the new
151ccd979bdSMark Fasheh 	 * top-of-the tree.
152ccd979bdSMark Fasheh 	 */
153811f933dSTao Ma 	return le16_to_cpu(root_el->l_tree_depth) + 2;
154ccd979bdSMark Fasheh }
155ccd979bdSMark Fasheh 
1565b6a3a2bSMark Fasheh void ocfs2_dinode_new_extent_list(struct inode *inode, struct ocfs2_dinode *di);
1571afc32b9SMark Fasheh void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di);
1581afc32b9SMark Fasheh int ocfs2_convert_inline_data_to_extents(struct inode *inode,
1591afc32b9SMark Fasheh 					 struct buffer_head *di_bh);
1601afc32b9SMark Fasheh 
161ccd979bdSMark Fasheh int ocfs2_truncate_log_init(struct ocfs2_super *osb);
162ccd979bdSMark Fasheh void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb);
163ccd979bdSMark Fasheh void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
164ccd979bdSMark Fasheh 				       int cancel);
165ccd979bdSMark Fasheh int ocfs2_flush_truncate_log(struct ocfs2_super *osb);
166ccd979bdSMark Fasheh int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
167ccd979bdSMark Fasheh 				      int slot_num,
168ccd979bdSMark Fasheh 				      struct ocfs2_dinode **tl_copy);
169ccd979bdSMark Fasheh int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
170ccd979bdSMark Fasheh 					 struct ocfs2_dinode *tl_copy);
171063c4561SMark Fasheh int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb);
172063c4561SMark Fasheh int ocfs2_truncate_log_append(struct ocfs2_super *osb,
173063c4561SMark Fasheh 			      handle_t *handle,
174063c4561SMark Fasheh 			      u64 start_blk,
175063c4561SMark Fasheh 			      unsigned int num_clusters);
176063c4561SMark Fasheh int __ocfs2_flush_truncate_log(struct ocfs2_super *osb);
1772070ad1aSEric Ren int ocfs2_try_to_free_truncate_log(struct ocfs2_super *osb,
1782070ad1aSEric Ren 				   unsigned int needed);
179ccd979bdSMark Fasheh 
1802b604351SMark Fasheh /*
1812b604351SMark Fasheh  * Process local structure which describes the block unlinks done
1822b604351SMark Fasheh  * during an operation. This is populated via
1832b604351SMark Fasheh  * ocfs2_cache_block_dealloc().
1842b604351SMark Fasheh  *
1852b604351SMark Fasheh  * ocfs2_run_deallocs() should be called after the potentially
1862b604351SMark Fasheh  * de-allocating routines. No journal handles should be open, and most
1872b604351SMark Fasheh  * locks should have been dropped.
1882b604351SMark Fasheh  */
1892b604351SMark Fasheh struct ocfs2_cached_dealloc_ctxt {
1902b604351SMark Fasheh 	struct ocfs2_per_slot_free_list		*c_first_suballocator;
1912891d290STao Ma 	struct ocfs2_cached_block_free 		*c_global_allocator;
1922b604351SMark Fasheh };
1932b604351SMark Fasheh static inline void ocfs2_init_dealloc_ctxt(struct ocfs2_cached_dealloc_ctxt *c)
1942b604351SMark Fasheh {
1952b604351SMark Fasheh 	c->c_first_suballocator = NULL;
1962891d290STao Ma 	c->c_global_allocator = NULL;
1972b604351SMark Fasheh }
1982891d290STao Ma int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
1992891d290STao Ma 				u64 blkno, unsigned int bit);
2001823cb0bSTao Ma int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
20174380c47STao Ma 			      int type, int slot, u64 suballoc, u64 blkno,
2021823cb0bSTao Ma 			      unsigned int bit);
20378f30c31STao Ma static inline int ocfs2_dealloc_has_cluster(struct ocfs2_cached_dealloc_ctxt *c)
20478f30c31STao Ma {
20578f30c31STao Ma 	return c->c_global_allocator != NULL;
20678f30c31STao Ma }
2072b604351SMark Fasheh int ocfs2_run_deallocs(struct ocfs2_super *osb,
2082b604351SMark Fasheh 		       struct ocfs2_cached_dealloc_ctxt *ctxt);
2092b604351SMark Fasheh 
210ccd979bdSMark Fasheh struct ocfs2_truncate_context {
21159a5e416SMark Fasheh 	struct ocfs2_cached_dealloc_ctxt tc_dealloc;
212ccd979bdSMark Fasheh 	int tc_ext_alloc_locked; /* is it cluster locked? */
213ccd979bdSMark Fasheh 	/* these get destroyed once it's passed to ocfs2_commit_truncate. */
214ccd979bdSMark Fasheh 	struct buffer_head *tc_last_eb_bh;
215ccd979bdSMark Fasheh };
216ccd979bdSMark Fasheh 
21735edec1dSMark Fasheh int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
21835edec1dSMark Fasheh 				  u64 range_start, u64 range_end);
219ccd979bdSMark Fasheh int ocfs2_commit_truncate(struct ocfs2_super *osb,
220ccd979bdSMark Fasheh 			  struct inode *inode,
22178f94673STristan Ye 			  struct buffer_head *di_bh);
2221afc32b9SMark Fasheh int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
2231afc32b9SMark Fasheh 			  unsigned int start, unsigned int end, int trunc);
224ccd979bdSMark Fasheh 
225facdb77fSJoel Becker int ocfs2_find_leaf(struct ocfs2_caching_info *ci,
226facdb77fSJoel Becker 		    struct ocfs2_extent_list *root_el, u32 cpos,
227facdb77fSJoel Becker 		    struct buffer_head **leaf_bh);
228328d5752SMark Fasheh int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster);
229363041a5SMark Fasheh 
230e80de36dSTao Ma int ocfs2_trim_fs(struct super_block *sb, struct fstrim_range *range);
231e48edee2SMark Fasheh /*
232e48edee2SMark Fasheh  * Helper function to look at the # of clusters in an extent record.
233e48edee2SMark Fasheh  */
234e48edee2SMark Fasheh static inline unsigned int ocfs2_rec_clusters(struct ocfs2_extent_list *el,
235e48edee2SMark Fasheh 					      struct ocfs2_extent_rec *rec)
236e48edee2SMark Fasheh {
237e48edee2SMark Fasheh 	/*
238e48edee2SMark Fasheh 	 * Cluster count in extent records is slightly different
239e48edee2SMark Fasheh 	 * between interior nodes and leaf nodes. This is to support
240e48edee2SMark Fasheh 	 * unwritten extents which need a flags field in leaf node
241e48edee2SMark Fasheh 	 * records, thus shrinking the available space for a clusters
242e48edee2SMark Fasheh 	 * field.
243e48edee2SMark Fasheh 	 */
244e48edee2SMark Fasheh 	if (el->l_tree_depth)
245e48edee2SMark Fasheh 		return le32_to_cpu(rec->e_int_clusters);
246e48edee2SMark Fasheh 	else
247e48edee2SMark Fasheh 		return le16_to_cpu(rec->e_leaf_clusters);
248e48edee2SMark Fasheh }
249e48edee2SMark Fasheh 
25000dc417fSMark Fasheh /*
25100dc417fSMark Fasheh  * This is only valid for leaf nodes, which are the only ones that can
25200dc417fSMark Fasheh  * have empty extents anyway.
25300dc417fSMark Fasheh  */
25400dc417fSMark Fasheh static inline int ocfs2_is_empty_extent(struct ocfs2_extent_rec *rec)
25500dc417fSMark Fasheh {
25600dc417fSMark Fasheh 	return !rec->e_leaf_clusters;
25700dc417fSMark Fasheh }
25800dc417fSMark Fasheh 
2596f70fa51STao Ma int ocfs2_grab_pages(struct inode *inode, loff_t start, loff_t end,
2606f70fa51STao Ma 		     struct page **pages, int *num);
2616f70fa51STao Ma void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
2626f70fa51STao Ma 			      unsigned int from, unsigned int to,
2636f70fa51STao Ma 			      struct page *page, int zero, u64 *phys);
264e2e9f608STao Ma /*
265e2e9f608STao Ma  * Structures which describe a path through a btree, and functions to
266e2e9f608STao Ma  * manipulate them.
267e2e9f608STao Ma  *
268e2e9f608STao Ma  * The idea here is to be as generic as possible with the tree
269e2e9f608STao Ma  * manipulation code.
270e2e9f608STao Ma  */
271e2e9f608STao Ma struct ocfs2_path_item {
272e2e9f608STao Ma 	struct buffer_head		*bh;
273e2e9f608STao Ma 	struct ocfs2_extent_list	*el;
274e2e9f608STao Ma };
275e2e9f608STao Ma 
276e2e9f608STao Ma #define OCFS2_MAX_PATH_DEPTH	5
277e2e9f608STao Ma 
278e2e9f608STao Ma struct ocfs2_path {
279e2e9f608STao Ma 	int				p_tree_depth;
280e2e9f608STao Ma 	ocfs2_journal_access_func	p_root_access;
281e2e9f608STao Ma 	struct ocfs2_path_item		p_node[OCFS2_MAX_PATH_DEPTH];
282e2e9f608STao Ma };
283e2e9f608STao Ma 
284e2e9f608STao Ma #define path_root_bh(_path) ((_path)->p_node[0].bh)
285e2e9f608STao Ma #define path_root_el(_path) ((_path)->p_node[0].el)
286e2e9f608STao Ma #define path_root_access(_path)((_path)->p_root_access)
287e2e9f608STao Ma #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
288e2e9f608STao Ma #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
289e2e9f608STao Ma #define path_num_items(_path) ((_path)->p_tree_depth + 1)
290e2e9f608STao Ma 
291e2e9f608STao Ma void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root);
292e2e9f608STao Ma void ocfs2_free_path(struct ocfs2_path *path);
293e2e9f608STao Ma int ocfs2_find_path(struct ocfs2_caching_info *ci,
294e2e9f608STao Ma 		    struct ocfs2_path *path,
295e2e9f608STao Ma 		    u32 cpos);
296e2e9f608STao Ma struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path);
297e2e9f608STao Ma struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et);
298e2e9f608STao Ma int ocfs2_path_bh_journal_access(handle_t *handle,
299e2e9f608STao Ma 				 struct ocfs2_caching_info *ci,
300e2e9f608STao Ma 				 struct ocfs2_path *path,
301e2e9f608STao Ma 				 int idx);
302e2e9f608STao Ma int ocfs2_journal_access_path(struct ocfs2_caching_info *ci,
303e2e9f608STao Ma 			      handle_t *handle,
304e2e9f608STao Ma 			      struct ocfs2_path *path);
30538a04e43STao Ma int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
30638a04e43STao Ma 				   struct ocfs2_path *path, u32 *cpos);
307ee149a7cSTristan Ye int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
308ee149a7cSTristan Ye 				  struct ocfs2_path *path, u32 *cpos);
30938a04e43STao Ma int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et,
31038a04e43STao Ma 			    struct ocfs2_path *left,
31138a04e43STao Ma 			    struct ocfs2_path *right);
312ccd979bdSMark Fasheh #endif /* OCFS2_ALLOC_H */
313