162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * alloc.h 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Function prototypes 662306a36Sopenharmony_ci * 762306a36Sopenharmony_ci * Copyright (C) 2002, 2004 Oracle. All rights reserved. 862306a36Sopenharmony_ci */ 962306a36Sopenharmony_ci 1062306a36Sopenharmony_ci#ifndef OCFS2_ALLOC_H 1162306a36Sopenharmony_ci#define OCFS2_ALLOC_H 1262306a36Sopenharmony_ci 1362306a36Sopenharmony_ci 1462306a36Sopenharmony_ci/* 1562306a36Sopenharmony_ci * For xattr tree leaf, we limit the leaf byte size to be 64K. 1662306a36Sopenharmony_ci */ 1762306a36Sopenharmony_ci#define OCFS2_MAX_XATTR_TREE_LEAF_SIZE 65536 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_ci/* 2062306a36Sopenharmony_ci * ocfs2_extent_tree and ocfs2_extent_tree_operations are used to abstract 2162306a36Sopenharmony_ci * the b-tree operations in ocfs2. Now all the b-tree operations are not 2262306a36Sopenharmony_ci * limited to ocfs2_dinode only. Any data which need to allocate clusters 2362306a36Sopenharmony_ci * to store can use b-tree. And it only needs to implement its ocfs2_extent_tree 2462306a36Sopenharmony_ci * and operation. 2562306a36Sopenharmony_ci * 2662306a36Sopenharmony_ci * ocfs2_extent_tree becomes the first-class object for extent tree 2762306a36Sopenharmony_ci * manipulation. Callers of the alloc.c code need to fill it via one of 2862306a36Sopenharmony_ci * the ocfs2_init_*_extent_tree() operations below. 2962306a36Sopenharmony_ci * 3062306a36Sopenharmony_ci * ocfs2_extent_tree contains info for the root of the b-tree, it must have a 3162306a36Sopenharmony_ci * root ocfs2_extent_list and a root_bh so that they can be used in the b-tree 3262306a36Sopenharmony_ci * functions. It needs the ocfs2_caching_info structure associated with 3362306a36Sopenharmony_ci * I/O on the tree. With metadata ecc, we now call different journal_access 3462306a36Sopenharmony_ci * functions for each type of metadata, so it must have the 3562306a36Sopenharmony_ci * root_journal_access function. 3662306a36Sopenharmony_ci * ocfs2_extent_tree_operations abstract the normal operations we do for 3762306a36Sopenharmony_ci * the root of extent b-tree. 3862306a36Sopenharmony_ci */ 3962306a36Sopenharmony_cistruct ocfs2_extent_tree_operations; 4062306a36Sopenharmony_cistruct ocfs2_extent_tree { 4162306a36Sopenharmony_ci const struct ocfs2_extent_tree_operations *et_ops; 4262306a36Sopenharmony_ci struct buffer_head *et_root_bh; 4362306a36Sopenharmony_ci struct ocfs2_extent_list *et_root_el; 4462306a36Sopenharmony_ci struct ocfs2_caching_info *et_ci; 4562306a36Sopenharmony_ci ocfs2_journal_access_func et_root_journal_access; 4662306a36Sopenharmony_ci void *et_object; 4762306a36Sopenharmony_ci unsigned int et_max_leaf_clusters; 4862306a36Sopenharmony_ci struct ocfs2_cached_dealloc_ctxt *et_dealloc; 4962306a36Sopenharmony_ci}; 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_ci/* 5262306a36Sopenharmony_ci * ocfs2_init_*_extent_tree() will fill an ocfs2_extent_tree from the 5362306a36Sopenharmony_ci * specified object buffer. 5462306a36Sopenharmony_ci */ 5562306a36Sopenharmony_civoid ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et, 5662306a36Sopenharmony_ci struct ocfs2_caching_info *ci, 5762306a36Sopenharmony_ci struct buffer_head *bh); 5862306a36Sopenharmony_civoid ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et, 5962306a36Sopenharmony_ci struct ocfs2_caching_info *ci, 6062306a36Sopenharmony_ci struct buffer_head *bh); 6162306a36Sopenharmony_cistruct ocfs2_xattr_value_buf; 6262306a36Sopenharmony_civoid ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et, 6362306a36Sopenharmony_ci struct ocfs2_caching_info *ci, 6462306a36Sopenharmony_ci struct ocfs2_xattr_value_buf *vb); 6562306a36Sopenharmony_civoid ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree *et, 6662306a36Sopenharmony_ci struct ocfs2_caching_info *ci, 6762306a36Sopenharmony_ci struct buffer_head *bh); 6862306a36Sopenharmony_civoid ocfs2_init_refcount_extent_tree(struct ocfs2_extent_tree *et, 6962306a36Sopenharmony_ci struct ocfs2_caching_info *ci, 7062306a36Sopenharmony_ci struct buffer_head *bh); 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_ci/* 7362306a36Sopenharmony_ci * Read an extent block into *bh. If *bh is NULL, a bh will be 7462306a36Sopenharmony_ci * allocated. This is a cached read. The extent block will be validated 7562306a36Sopenharmony_ci * with ocfs2_validate_extent_block(). 7662306a36Sopenharmony_ci */ 7762306a36Sopenharmony_ciint ocfs2_read_extent_block(struct ocfs2_caching_info *ci, u64 eb_blkno, 7862306a36Sopenharmony_ci struct buffer_head **bh); 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_cistruct ocfs2_alloc_context; 8162306a36Sopenharmony_ciint ocfs2_insert_extent(handle_t *handle, 8262306a36Sopenharmony_ci struct ocfs2_extent_tree *et, 8362306a36Sopenharmony_ci u32 cpos, 8462306a36Sopenharmony_ci u64 start_blk, 8562306a36Sopenharmony_ci u32 new_clusters, 8662306a36Sopenharmony_ci u8 flags, 8762306a36Sopenharmony_ci struct ocfs2_alloc_context *meta_ac); 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_cienum ocfs2_alloc_restarted { 9062306a36Sopenharmony_ci RESTART_NONE = 0, 9162306a36Sopenharmony_ci RESTART_TRANS, 9262306a36Sopenharmony_ci RESTART_META 9362306a36Sopenharmony_ci}; 9462306a36Sopenharmony_ciint ocfs2_add_clusters_in_btree(handle_t *handle, 9562306a36Sopenharmony_ci struct ocfs2_extent_tree *et, 9662306a36Sopenharmony_ci u32 *logical_offset, 9762306a36Sopenharmony_ci u32 clusters_to_add, 9862306a36Sopenharmony_ci int mark_unwritten, 9962306a36Sopenharmony_ci struct ocfs2_alloc_context *data_ac, 10062306a36Sopenharmony_ci struct ocfs2_alloc_context *meta_ac, 10162306a36Sopenharmony_ci enum ocfs2_alloc_restarted *reason_ret); 10262306a36Sopenharmony_cistruct ocfs2_cached_dealloc_ctxt; 10362306a36Sopenharmony_cistruct ocfs2_path; 10462306a36Sopenharmony_ciint ocfs2_split_extent(handle_t *handle, 10562306a36Sopenharmony_ci struct ocfs2_extent_tree *et, 10662306a36Sopenharmony_ci struct ocfs2_path *path, 10762306a36Sopenharmony_ci int split_index, 10862306a36Sopenharmony_ci struct ocfs2_extent_rec *split_rec, 10962306a36Sopenharmony_ci struct ocfs2_alloc_context *meta_ac, 11062306a36Sopenharmony_ci struct ocfs2_cached_dealloc_ctxt *dealloc); 11162306a36Sopenharmony_ciint ocfs2_mark_extent_written(struct inode *inode, 11262306a36Sopenharmony_ci struct ocfs2_extent_tree *et, 11362306a36Sopenharmony_ci handle_t *handle, u32 cpos, u32 len, u32 phys, 11462306a36Sopenharmony_ci struct ocfs2_alloc_context *meta_ac, 11562306a36Sopenharmony_ci struct ocfs2_cached_dealloc_ctxt *dealloc); 11662306a36Sopenharmony_ciint ocfs2_change_extent_flag(handle_t *handle, 11762306a36Sopenharmony_ci struct ocfs2_extent_tree *et, 11862306a36Sopenharmony_ci u32 cpos, u32 len, u32 phys, 11962306a36Sopenharmony_ci struct ocfs2_alloc_context *meta_ac, 12062306a36Sopenharmony_ci struct ocfs2_cached_dealloc_ctxt *dealloc, 12162306a36Sopenharmony_ci int new_flags, int clear_flags); 12262306a36Sopenharmony_ciint ocfs2_remove_extent(handle_t *handle, struct ocfs2_extent_tree *et, 12362306a36Sopenharmony_ci u32 cpos, u32 len, 12462306a36Sopenharmony_ci struct ocfs2_alloc_context *meta_ac, 12562306a36Sopenharmony_ci struct ocfs2_cached_dealloc_ctxt *dealloc); 12662306a36Sopenharmony_ciint ocfs2_remove_btree_range(struct inode *inode, 12762306a36Sopenharmony_ci struct ocfs2_extent_tree *et, 12862306a36Sopenharmony_ci u32 cpos, u32 phys_cpos, u32 len, int flags, 12962306a36Sopenharmony_ci struct ocfs2_cached_dealloc_ctxt *dealloc, 13062306a36Sopenharmony_ci u64 refcount_loc, bool refcount_tree_locked); 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ciint ocfs2_num_free_extents(struct ocfs2_extent_tree *et); 13362306a36Sopenharmony_ci 13462306a36Sopenharmony_ci/* 13562306a36Sopenharmony_ci * how many new metadata chunks would an allocation need at maximum? 13662306a36Sopenharmony_ci * 13762306a36Sopenharmony_ci * Please note that the caller must make sure that root_el is the root 13862306a36Sopenharmony_ci * of extent tree. So for an inode, it should be &fe->id2.i_list. Otherwise 13962306a36Sopenharmony_ci * the result may be wrong. 14062306a36Sopenharmony_ci */ 14162306a36Sopenharmony_cistatic inline int ocfs2_extend_meta_needed(struct ocfs2_extent_list *root_el) 14262306a36Sopenharmony_ci{ 14362306a36Sopenharmony_ci /* 14462306a36Sopenharmony_ci * Rather than do all the work of determining how much we need 14562306a36Sopenharmony_ci * (involves a ton of reads and locks), just ask for the 14662306a36Sopenharmony_ci * maximal limit. That's a tree depth shift. So, one block for 14762306a36Sopenharmony_ci * level of the tree (current l_tree_depth), one block for the 14862306a36Sopenharmony_ci * new tree_depth==0 extent_block, and one block at the new 14962306a36Sopenharmony_ci * top-of-the tree. 15062306a36Sopenharmony_ci */ 15162306a36Sopenharmony_ci return le16_to_cpu(root_el->l_tree_depth) + 2; 15262306a36Sopenharmony_ci} 15362306a36Sopenharmony_ci 15462306a36Sopenharmony_civoid ocfs2_dinode_new_extent_list(struct inode *inode, struct ocfs2_dinode *di); 15562306a36Sopenharmony_civoid ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di); 15662306a36Sopenharmony_ciint ocfs2_convert_inline_data_to_extents(struct inode *inode, 15762306a36Sopenharmony_ci struct buffer_head *di_bh); 15862306a36Sopenharmony_ci 15962306a36Sopenharmony_ciint ocfs2_truncate_log_init(struct ocfs2_super *osb); 16062306a36Sopenharmony_civoid ocfs2_truncate_log_shutdown(struct ocfs2_super *osb); 16162306a36Sopenharmony_civoid ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb, 16262306a36Sopenharmony_ci int cancel); 16362306a36Sopenharmony_ciint ocfs2_flush_truncate_log(struct ocfs2_super *osb); 16462306a36Sopenharmony_ciint ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb, 16562306a36Sopenharmony_ci int slot_num, 16662306a36Sopenharmony_ci struct ocfs2_dinode **tl_copy); 16762306a36Sopenharmony_ciint ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb, 16862306a36Sopenharmony_ci struct ocfs2_dinode *tl_copy); 16962306a36Sopenharmony_ciint ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb); 17062306a36Sopenharmony_ciint ocfs2_truncate_log_append(struct ocfs2_super *osb, 17162306a36Sopenharmony_ci handle_t *handle, 17262306a36Sopenharmony_ci u64 start_blk, 17362306a36Sopenharmony_ci unsigned int num_clusters); 17462306a36Sopenharmony_ciint __ocfs2_flush_truncate_log(struct ocfs2_super *osb); 17562306a36Sopenharmony_ciint ocfs2_try_to_free_truncate_log(struct ocfs2_super *osb, 17662306a36Sopenharmony_ci unsigned int needed); 17762306a36Sopenharmony_ci 17862306a36Sopenharmony_ci/* 17962306a36Sopenharmony_ci * Process local structure which describes the block unlinks done 18062306a36Sopenharmony_ci * during an operation. This is populated via 18162306a36Sopenharmony_ci * ocfs2_cache_block_dealloc(). 18262306a36Sopenharmony_ci * 18362306a36Sopenharmony_ci * ocfs2_run_deallocs() should be called after the potentially 18462306a36Sopenharmony_ci * de-allocating routines. No journal handles should be open, and most 18562306a36Sopenharmony_ci * locks should have been dropped. 18662306a36Sopenharmony_ci */ 18762306a36Sopenharmony_cistruct ocfs2_cached_dealloc_ctxt { 18862306a36Sopenharmony_ci struct ocfs2_per_slot_free_list *c_first_suballocator; 18962306a36Sopenharmony_ci struct ocfs2_cached_block_free *c_global_allocator; 19062306a36Sopenharmony_ci}; 19162306a36Sopenharmony_cistatic inline void ocfs2_init_dealloc_ctxt(struct ocfs2_cached_dealloc_ctxt *c) 19262306a36Sopenharmony_ci{ 19362306a36Sopenharmony_ci c->c_first_suballocator = NULL; 19462306a36Sopenharmony_ci c->c_global_allocator = NULL; 19562306a36Sopenharmony_ci} 19662306a36Sopenharmony_ciint ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt, 19762306a36Sopenharmony_ci u64 blkno, unsigned int bit); 19862306a36Sopenharmony_ciint ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt, 19962306a36Sopenharmony_ci int type, int slot, u64 suballoc, u64 blkno, 20062306a36Sopenharmony_ci unsigned int bit); 20162306a36Sopenharmony_cistatic inline int ocfs2_dealloc_has_cluster(struct ocfs2_cached_dealloc_ctxt *c) 20262306a36Sopenharmony_ci{ 20362306a36Sopenharmony_ci return c->c_global_allocator != NULL; 20462306a36Sopenharmony_ci} 20562306a36Sopenharmony_ciint ocfs2_run_deallocs(struct ocfs2_super *osb, 20662306a36Sopenharmony_ci struct ocfs2_cached_dealloc_ctxt *ctxt); 20762306a36Sopenharmony_ci 20862306a36Sopenharmony_cistruct ocfs2_truncate_context { 20962306a36Sopenharmony_ci struct ocfs2_cached_dealloc_ctxt tc_dealloc; 21062306a36Sopenharmony_ci int tc_ext_alloc_locked; /* is it cluster locked? */ 21162306a36Sopenharmony_ci /* these get destroyed once it's passed to ocfs2_commit_truncate. */ 21262306a36Sopenharmony_ci struct buffer_head *tc_last_eb_bh; 21362306a36Sopenharmony_ci}; 21462306a36Sopenharmony_ci 21562306a36Sopenharmony_ciint ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle, 21662306a36Sopenharmony_ci u64 range_start, u64 range_end); 21762306a36Sopenharmony_ciint ocfs2_commit_truncate(struct ocfs2_super *osb, 21862306a36Sopenharmony_ci struct inode *inode, 21962306a36Sopenharmony_ci struct buffer_head *di_bh); 22062306a36Sopenharmony_ciint ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh, 22162306a36Sopenharmony_ci unsigned int start, unsigned int end, int trunc); 22262306a36Sopenharmony_ci 22362306a36Sopenharmony_ciint ocfs2_find_leaf(struct ocfs2_caching_info *ci, 22462306a36Sopenharmony_ci struct ocfs2_extent_list *root_el, u32 cpos, 22562306a36Sopenharmony_ci struct buffer_head **leaf_bh); 22662306a36Sopenharmony_ciint ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster); 22762306a36Sopenharmony_ci 22862306a36Sopenharmony_ciint ocfs2_trim_fs(struct super_block *sb, struct fstrim_range *range); 22962306a36Sopenharmony_ci/* 23062306a36Sopenharmony_ci * Helper function to look at the # of clusters in an extent record. 23162306a36Sopenharmony_ci */ 23262306a36Sopenharmony_cistatic inline unsigned int ocfs2_rec_clusters(struct ocfs2_extent_list *el, 23362306a36Sopenharmony_ci struct ocfs2_extent_rec *rec) 23462306a36Sopenharmony_ci{ 23562306a36Sopenharmony_ci /* 23662306a36Sopenharmony_ci * Cluster count in extent records is slightly different 23762306a36Sopenharmony_ci * between interior nodes and leaf nodes. This is to support 23862306a36Sopenharmony_ci * unwritten extents which need a flags field in leaf node 23962306a36Sopenharmony_ci * records, thus shrinking the available space for a clusters 24062306a36Sopenharmony_ci * field. 24162306a36Sopenharmony_ci */ 24262306a36Sopenharmony_ci if (el->l_tree_depth) 24362306a36Sopenharmony_ci return le32_to_cpu(rec->e_int_clusters); 24462306a36Sopenharmony_ci else 24562306a36Sopenharmony_ci return le16_to_cpu(rec->e_leaf_clusters); 24662306a36Sopenharmony_ci} 24762306a36Sopenharmony_ci 24862306a36Sopenharmony_ci/* 24962306a36Sopenharmony_ci * This is only valid for leaf nodes, which are the only ones that can 25062306a36Sopenharmony_ci * have empty extents anyway. 25162306a36Sopenharmony_ci */ 25262306a36Sopenharmony_cistatic inline int ocfs2_is_empty_extent(struct ocfs2_extent_rec *rec) 25362306a36Sopenharmony_ci{ 25462306a36Sopenharmony_ci return !rec->e_leaf_clusters; 25562306a36Sopenharmony_ci} 25662306a36Sopenharmony_ci 25762306a36Sopenharmony_ciint ocfs2_grab_pages(struct inode *inode, loff_t start, loff_t end, 25862306a36Sopenharmony_ci struct page **pages, int *num); 25962306a36Sopenharmony_civoid ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle, 26062306a36Sopenharmony_ci unsigned int from, unsigned int to, 26162306a36Sopenharmony_ci struct page *page, int zero, u64 *phys); 26262306a36Sopenharmony_ci/* 26362306a36Sopenharmony_ci * Structures which describe a path through a btree, and functions to 26462306a36Sopenharmony_ci * manipulate them. 26562306a36Sopenharmony_ci * 26662306a36Sopenharmony_ci * The idea here is to be as generic as possible with the tree 26762306a36Sopenharmony_ci * manipulation code. 26862306a36Sopenharmony_ci */ 26962306a36Sopenharmony_cistruct ocfs2_path_item { 27062306a36Sopenharmony_ci struct buffer_head *bh; 27162306a36Sopenharmony_ci struct ocfs2_extent_list *el; 27262306a36Sopenharmony_ci}; 27362306a36Sopenharmony_ci 27462306a36Sopenharmony_ci#define OCFS2_MAX_PATH_DEPTH 5 27562306a36Sopenharmony_ci 27662306a36Sopenharmony_cistruct ocfs2_path { 27762306a36Sopenharmony_ci int p_tree_depth; 27862306a36Sopenharmony_ci ocfs2_journal_access_func p_root_access; 27962306a36Sopenharmony_ci struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH]; 28062306a36Sopenharmony_ci}; 28162306a36Sopenharmony_ci 28262306a36Sopenharmony_ci#define path_root_bh(_path) ((_path)->p_node[0].bh) 28362306a36Sopenharmony_ci#define path_root_el(_path) ((_path)->p_node[0].el) 28462306a36Sopenharmony_ci#define path_root_access(_path)((_path)->p_root_access) 28562306a36Sopenharmony_ci#define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh) 28662306a36Sopenharmony_ci#define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el) 28762306a36Sopenharmony_ci#define path_num_items(_path) ((_path)->p_tree_depth + 1) 28862306a36Sopenharmony_ci 28962306a36Sopenharmony_civoid ocfs2_reinit_path(struct ocfs2_path *path, int keep_root); 29062306a36Sopenharmony_civoid ocfs2_free_path(struct ocfs2_path *path); 29162306a36Sopenharmony_ciint ocfs2_find_path(struct ocfs2_caching_info *ci, 29262306a36Sopenharmony_ci struct ocfs2_path *path, 29362306a36Sopenharmony_ci u32 cpos); 29462306a36Sopenharmony_cistruct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path); 29562306a36Sopenharmony_cistruct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et); 29662306a36Sopenharmony_ciint ocfs2_path_bh_journal_access(handle_t *handle, 29762306a36Sopenharmony_ci struct ocfs2_caching_info *ci, 29862306a36Sopenharmony_ci struct ocfs2_path *path, 29962306a36Sopenharmony_ci int idx); 30062306a36Sopenharmony_ciint ocfs2_journal_access_path(struct ocfs2_caching_info *ci, 30162306a36Sopenharmony_ci handle_t *handle, 30262306a36Sopenharmony_ci struct ocfs2_path *path); 30362306a36Sopenharmony_ciint ocfs2_find_cpos_for_right_leaf(struct super_block *sb, 30462306a36Sopenharmony_ci struct ocfs2_path *path, u32 *cpos); 30562306a36Sopenharmony_ciint ocfs2_find_cpos_for_left_leaf(struct super_block *sb, 30662306a36Sopenharmony_ci struct ocfs2_path *path, u32 *cpos); 30762306a36Sopenharmony_ciint ocfs2_find_subtree_root(struct ocfs2_extent_tree *et, 30862306a36Sopenharmony_ci struct ocfs2_path *left, 30962306a36Sopenharmony_ci struct ocfs2_path *right); 31062306a36Sopenharmony_ci#endif /* OCFS2_ALLOC_H */ 311