xref: /openbmc/linux/fs/ocfs2/alloc.h (revision ca12b7c4)
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 enum ocfs2_extent_tree_type {
30e7d4cb6bSTao Ma 	OCFS2_DINODE_EXTENT = 0,
31f56654c4STao Ma 	OCFS2_XATTR_VALUE_EXTENT,
32ba492615STao Ma 	OCFS2_XATTR_TREE_EXTENT,
33e7d4cb6bSTao Ma };
34e7d4cb6bSTao Ma 
35ca12b7c4STao Ma /*
36ca12b7c4STao Ma  * For xattr tree leaf, we limit the leaf byte size to be 64K.
37ca12b7c4STao Ma  */
38ca12b7c4STao Ma #define OCFS2_MAX_XATTR_TREE_LEAF_SIZE 65536
39ca12b7c4STao Ma 
40ccd979bdSMark Fasheh struct ocfs2_alloc_context;
41f56654c4STao Ma int ocfs2_dinode_insert_extent(struct ocfs2_super *osb,
42f56654c4STao Ma 			       handle_t *handle,
43f56654c4STao Ma 			       struct inode *inode,
44f56654c4STao Ma 			       struct buffer_head *root_bh,
45f56654c4STao Ma 			       u32 cpos,
46f56654c4STao Ma 			       u64 start_blk,
47f56654c4STao Ma 			       u32 new_clusters,
48f56654c4STao Ma 			       u8 flags,
49f56654c4STao Ma 			       struct ocfs2_alloc_context *meta_ac);
50f56654c4STao Ma int ocfs2_xattr_value_insert_extent(struct ocfs2_super *osb,
511fabe148SMark Fasheh 				    handle_t *handle,
52ccd979bdSMark Fasheh 				    struct inode *inode,
53e7d4cb6bSTao Ma 				    struct buffer_head *root_bh,
54dcd0538fSMark Fasheh 				    u32 cpos,
55dcd0538fSMark Fasheh 				    u64 start_blk,
56ccd979bdSMark Fasheh 				    u32 new_clusters,
572ae99a60SMark Fasheh 				    u8 flags,
58e7d4cb6bSTao Ma 				    struct ocfs2_alloc_context *meta_ac,
59f56654c4STao Ma 				    void *private);
60ba492615STao Ma int ocfs2_xattr_tree_insert_extent(struct ocfs2_super *osb,
61ba492615STao Ma 				   handle_t *handle,
62ba492615STao Ma 				   struct inode *inode,
63ba492615STao Ma 				   struct buffer_head *root_bh,
64ba492615STao Ma 				   u32 cpos,
65ba492615STao Ma 				   u64 start_blk,
66ba492615STao Ma 				   u32 new_clusters,
67ba492615STao Ma 				   u8 flags,
68ba492615STao Ma 				   struct ocfs2_alloc_context *meta_ac);
690eb8d47eSTao Ma enum ocfs2_alloc_restarted {
700eb8d47eSTao Ma 	RESTART_NONE = 0,
710eb8d47eSTao Ma 	RESTART_TRANS,
720eb8d47eSTao Ma 	RESTART_META
730eb8d47eSTao Ma };
740eb8d47eSTao Ma int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
750eb8d47eSTao Ma 				struct inode *inode,
760eb8d47eSTao Ma 				u32 *logical_offset,
770eb8d47eSTao Ma 				u32 clusters_to_add,
780eb8d47eSTao Ma 				int mark_unwritten,
790eb8d47eSTao Ma 				struct buffer_head *root_bh,
800eb8d47eSTao Ma 				struct ocfs2_extent_list *root_el,
810eb8d47eSTao Ma 				handle_t *handle,
820eb8d47eSTao Ma 				struct ocfs2_alloc_context *data_ac,
830eb8d47eSTao Ma 				struct ocfs2_alloc_context *meta_ac,
840eb8d47eSTao Ma 				enum ocfs2_alloc_restarted *reason_ret,
85f56654c4STao Ma 				enum ocfs2_extent_tree_type type,
86f56654c4STao Ma 				void *private);
87328d5752SMark Fasheh struct ocfs2_cached_dealloc_ctxt;
88e7d4cb6bSTao Ma int ocfs2_mark_extent_written(struct inode *inode, struct buffer_head *root_bh,
89328d5752SMark Fasheh 			      handle_t *handle, u32 cpos, u32 len, u32 phys,
90328d5752SMark Fasheh 			      struct ocfs2_alloc_context *meta_ac,
91e7d4cb6bSTao Ma 			      struct ocfs2_cached_dealloc_ctxt *dealloc,
92f56654c4STao Ma 			      enum ocfs2_extent_tree_type et_type,
93f56654c4STao Ma 			      void *private);
94e7d4cb6bSTao Ma int ocfs2_remove_extent(struct inode *inode, struct buffer_head *root_bh,
95063c4561SMark Fasheh 			u32 cpos, u32 len, handle_t *handle,
96063c4561SMark Fasheh 			struct ocfs2_alloc_context *meta_ac,
97e7d4cb6bSTao Ma 			struct ocfs2_cached_dealloc_ctxt *dealloc,
98f56654c4STao Ma 			enum ocfs2_extent_tree_type et_type,
99f56654c4STao Ma 			void *private);
100ccd979bdSMark Fasheh int ocfs2_num_free_extents(struct ocfs2_super *osb,
101ccd979bdSMark Fasheh 			   struct inode *inode,
102e7d4cb6bSTao Ma 			   struct buffer_head *root_bh,
103f56654c4STao Ma 			   enum ocfs2_extent_tree_type et_type,
104f56654c4STao Ma 			   void *private);
105e7d4cb6bSTao Ma 
106811f933dSTao Ma /*
107811f933dSTao Ma  * how many new metadata chunks would an allocation need at maximum?
108811f933dSTao Ma  *
109811f933dSTao Ma  * Please note that the caller must make sure that root_el is the root
110811f933dSTao Ma  * of extent tree. So for an inode, it should be &fe->id2.i_list. Otherwise
111811f933dSTao Ma  * the result may be wrong.
112811f933dSTao Ma  */
113811f933dSTao Ma static inline int ocfs2_extend_meta_needed(struct ocfs2_extent_list *root_el)
114ccd979bdSMark Fasheh {
115ccd979bdSMark Fasheh 	/*
116ccd979bdSMark Fasheh 	 * Rather than do all the work of determining how much we need
117ccd979bdSMark Fasheh 	 * (involves a ton of reads and locks), just ask for the
118ccd979bdSMark Fasheh 	 * maximal limit.  That's a tree depth shift.  So, one block for
119ccd979bdSMark Fasheh 	 * level of the tree (current l_tree_depth), one block for the
120ccd979bdSMark Fasheh 	 * new tree_depth==0 extent_block, and one block at the new
121ccd979bdSMark Fasheh 	 * top-of-the tree.
122ccd979bdSMark Fasheh 	 */
123811f933dSTao Ma 	return le16_to_cpu(root_el->l_tree_depth) + 2;
124ccd979bdSMark Fasheh }
125ccd979bdSMark Fasheh 
1265b6a3a2bSMark Fasheh void ocfs2_dinode_new_extent_list(struct inode *inode, struct ocfs2_dinode *di);
1271afc32b9SMark Fasheh void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di);
1281afc32b9SMark Fasheh int ocfs2_convert_inline_data_to_extents(struct inode *inode,
1291afc32b9SMark Fasheh 					 struct buffer_head *di_bh);
1301afc32b9SMark Fasheh 
131ccd979bdSMark Fasheh int ocfs2_truncate_log_init(struct ocfs2_super *osb);
132ccd979bdSMark Fasheh void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb);
133ccd979bdSMark Fasheh void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
134ccd979bdSMark Fasheh 				       int cancel);
135ccd979bdSMark Fasheh int ocfs2_flush_truncate_log(struct ocfs2_super *osb);
136ccd979bdSMark Fasheh int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
137ccd979bdSMark Fasheh 				      int slot_num,
138ccd979bdSMark Fasheh 				      struct ocfs2_dinode **tl_copy);
139ccd979bdSMark Fasheh int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
140ccd979bdSMark Fasheh 					 struct ocfs2_dinode *tl_copy);
141063c4561SMark Fasheh int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb);
142063c4561SMark Fasheh int ocfs2_truncate_log_append(struct ocfs2_super *osb,
143063c4561SMark Fasheh 			      handle_t *handle,
144063c4561SMark Fasheh 			      u64 start_blk,
145063c4561SMark Fasheh 			      unsigned int num_clusters);
146063c4561SMark Fasheh int __ocfs2_flush_truncate_log(struct ocfs2_super *osb);
147ccd979bdSMark Fasheh 
1482b604351SMark Fasheh /*
1492b604351SMark Fasheh  * Process local structure which describes the block unlinks done
1502b604351SMark Fasheh  * during an operation. This is populated via
1512b604351SMark Fasheh  * ocfs2_cache_block_dealloc().
1522b604351SMark Fasheh  *
1532b604351SMark Fasheh  * ocfs2_run_deallocs() should be called after the potentially
1542b604351SMark Fasheh  * de-allocating routines. No journal handles should be open, and most
1552b604351SMark Fasheh  * locks should have been dropped.
1562b604351SMark Fasheh  */
1572b604351SMark Fasheh struct ocfs2_cached_dealloc_ctxt {
1582b604351SMark Fasheh 	struct ocfs2_per_slot_free_list		*c_first_suballocator;
1592b604351SMark Fasheh };
1602b604351SMark Fasheh static inline void ocfs2_init_dealloc_ctxt(struct ocfs2_cached_dealloc_ctxt *c)
1612b604351SMark Fasheh {
1622b604351SMark Fasheh 	c->c_first_suballocator = NULL;
1632b604351SMark Fasheh }
1642b604351SMark Fasheh int ocfs2_run_deallocs(struct ocfs2_super *osb,
1652b604351SMark Fasheh 		       struct ocfs2_cached_dealloc_ctxt *ctxt);
1662b604351SMark Fasheh 
167ccd979bdSMark Fasheh struct ocfs2_truncate_context {
16859a5e416SMark Fasheh 	struct ocfs2_cached_dealloc_ctxt tc_dealloc;
169ccd979bdSMark Fasheh 	int tc_ext_alloc_locked; /* is it cluster locked? */
170ccd979bdSMark Fasheh 	/* these get destroyed once it's passed to ocfs2_commit_truncate. */
171ccd979bdSMark Fasheh 	struct buffer_head *tc_last_eb_bh;
172ccd979bdSMark Fasheh };
173ccd979bdSMark Fasheh 
17435edec1dSMark Fasheh int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
17535edec1dSMark Fasheh 				  u64 range_start, u64 range_end);
176ccd979bdSMark Fasheh int ocfs2_prepare_truncate(struct ocfs2_super *osb,
177ccd979bdSMark Fasheh 			   struct inode *inode,
178ccd979bdSMark Fasheh 			   struct buffer_head *fe_bh,
179ccd979bdSMark Fasheh 			   struct ocfs2_truncate_context **tc);
180ccd979bdSMark Fasheh int ocfs2_commit_truncate(struct ocfs2_super *osb,
181ccd979bdSMark Fasheh 			  struct inode *inode,
182ccd979bdSMark Fasheh 			  struct buffer_head *fe_bh,
183ccd979bdSMark Fasheh 			  struct ocfs2_truncate_context *tc);
1841afc32b9SMark Fasheh int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
1851afc32b9SMark Fasheh 			  unsigned int start, unsigned int end, int trunc);
186ccd979bdSMark Fasheh 
187363041a5SMark Fasheh int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el,
188363041a5SMark Fasheh 		    u32 cpos, struct buffer_head **leaf_bh);
189328d5752SMark Fasheh int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster);
190363041a5SMark Fasheh 
191e48edee2SMark Fasheh /*
192e48edee2SMark Fasheh  * Helper function to look at the # of clusters in an extent record.
193e48edee2SMark Fasheh  */
194e48edee2SMark Fasheh static inline unsigned int ocfs2_rec_clusters(struct ocfs2_extent_list *el,
195e48edee2SMark Fasheh 					      struct ocfs2_extent_rec *rec)
196e48edee2SMark Fasheh {
197e48edee2SMark Fasheh 	/*
198e48edee2SMark Fasheh 	 * Cluster count in extent records is slightly different
199e48edee2SMark Fasheh 	 * between interior nodes and leaf nodes. This is to support
200e48edee2SMark Fasheh 	 * unwritten extents which need a flags field in leaf node
201e48edee2SMark Fasheh 	 * records, thus shrinking the available space for a clusters
202e48edee2SMark Fasheh 	 * field.
203e48edee2SMark Fasheh 	 */
204e48edee2SMark Fasheh 	if (el->l_tree_depth)
205e48edee2SMark Fasheh 		return le32_to_cpu(rec->e_int_clusters);
206e48edee2SMark Fasheh 	else
207e48edee2SMark Fasheh 		return le16_to_cpu(rec->e_leaf_clusters);
208e48edee2SMark Fasheh }
209e48edee2SMark Fasheh 
21000dc417fSMark Fasheh /*
21100dc417fSMark Fasheh  * This is only valid for leaf nodes, which are the only ones that can
21200dc417fSMark Fasheh  * have empty extents anyway.
21300dc417fSMark Fasheh  */
21400dc417fSMark Fasheh static inline int ocfs2_is_empty_extent(struct ocfs2_extent_rec *rec)
21500dc417fSMark Fasheh {
21600dc417fSMark Fasheh 	return !rec->e_leaf_clusters;
21700dc417fSMark Fasheh }
21800dc417fSMark Fasheh 
219ccd979bdSMark Fasheh #endif /* OCFS2_ALLOC_H */
220