xref: /OK3568_Linux_fs/kernel/fs/ocfs2/alloc.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /* -*- mode: c; c-basic-offset: 8; -*-
3*4882a593Smuzhiyun  * vim: noexpandtab sw=8 ts=8 sts=0:
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * alloc.c
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Extent allocs and frees
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <linux/fs.h>
13*4882a593Smuzhiyun #include <linux/types.h>
14*4882a593Smuzhiyun #include <linux/slab.h>
15*4882a593Smuzhiyun #include <linux/highmem.h>
16*4882a593Smuzhiyun #include <linux/swap.h>
17*4882a593Smuzhiyun #include <linux/quotaops.h>
18*4882a593Smuzhiyun #include <linux/blkdev.h>
19*4882a593Smuzhiyun #include <linux/sched/signal.h>
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun #include <cluster/masklog.h>
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #include "ocfs2.h"
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #include "alloc.h"
26*4882a593Smuzhiyun #include "aops.h"
27*4882a593Smuzhiyun #include "blockcheck.h"
28*4882a593Smuzhiyun #include "dlmglue.h"
29*4882a593Smuzhiyun #include "extent_map.h"
30*4882a593Smuzhiyun #include "inode.h"
31*4882a593Smuzhiyun #include "journal.h"
32*4882a593Smuzhiyun #include "localalloc.h"
33*4882a593Smuzhiyun #include "suballoc.h"
34*4882a593Smuzhiyun #include "sysfile.h"
35*4882a593Smuzhiyun #include "file.h"
36*4882a593Smuzhiyun #include "super.h"
37*4882a593Smuzhiyun #include "uptodate.h"
38*4882a593Smuzhiyun #include "xattr.h"
39*4882a593Smuzhiyun #include "refcounttree.h"
40*4882a593Smuzhiyun #include "ocfs2_trace.h"
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun #include "buffer_head_io.h"
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun enum ocfs2_contig_type {
45*4882a593Smuzhiyun 	CONTIG_NONE = 0,
46*4882a593Smuzhiyun 	CONTIG_LEFT,
47*4882a593Smuzhiyun 	CONTIG_RIGHT,
48*4882a593Smuzhiyun 	CONTIG_LEFTRIGHT,
49*4882a593Smuzhiyun };
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun static enum ocfs2_contig_type
52*4882a593Smuzhiyun 	ocfs2_extent_rec_contig(struct super_block *sb,
53*4882a593Smuzhiyun 				struct ocfs2_extent_rec *ext,
54*4882a593Smuzhiyun 				struct ocfs2_extent_rec *insert_rec);
55*4882a593Smuzhiyun /*
56*4882a593Smuzhiyun  * Operations for a specific extent tree type.
57*4882a593Smuzhiyun  *
58*4882a593Smuzhiyun  * To implement an on-disk btree (extent tree) type in ocfs2, add
59*4882a593Smuzhiyun  * an ocfs2_extent_tree_operations structure and the matching
60*4882a593Smuzhiyun  * ocfs2_init_<thingy>_extent_tree() function.  That's pretty much it
61*4882a593Smuzhiyun  * for the allocation portion of the extent tree.
62*4882a593Smuzhiyun  */
63*4882a593Smuzhiyun struct ocfs2_extent_tree_operations {
64*4882a593Smuzhiyun 	/*
65*4882a593Smuzhiyun 	 * last_eb_blk is the block number of the right most leaf extent
66*4882a593Smuzhiyun 	 * block.  Most on-disk structures containing an extent tree store
67*4882a593Smuzhiyun 	 * this value for fast access.  The ->eo_set_last_eb_blk() and
68*4882a593Smuzhiyun 	 * ->eo_get_last_eb_blk() operations access this value.  They are
69*4882a593Smuzhiyun 	 *  both required.
70*4882a593Smuzhiyun 	 */
71*4882a593Smuzhiyun 	void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et,
72*4882a593Smuzhiyun 				   u64 blkno);
73*4882a593Smuzhiyun 	u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et);
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	/*
76*4882a593Smuzhiyun 	 * The on-disk structure usually keeps track of how many total
77*4882a593Smuzhiyun 	 * clusters are stored in this extent tree.  This function updates
78*4882a593Smuzhiyun 	 * that value.  new_clusters is the delta, and must be
79*4882a593Smuzhiyun 	 * added to the total.  Required.
80*4882a593Smuzhiyun 	 */
81*4882a593Smuzhiyun 	void (*eo_update_clusters)(struct ocfs2_extent_tree *et,
82*4882a593Smuzhiyun 				   u32 new_clusters);
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	/*
85*4882a593Smuzhiyun 	 * If this extent tree is supported by an extent map, insert
86*4882a593Smuzhiyun 	 * a record into the map.
87*4882a593Smuzhiyun 	 */
88*4882a593Smuzhiyun 	void (*eo_extent_map_insert)(struct ocfs2_extent_tree *et,
89*4882a593Smuzhiyun 				     struct ocfs2_extent_rec *rec);
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	/*
92*4882a593Smuzhiyun 	 * If this extent tree is supported by an extent map, truncate the
93*4882a593Smuzhiyun 	 * map to clusters,
94*4882a593Smuzhiyun 	 */
95*4882a593Smuzhiyun 	void (*eo_extent_map_truncate)(struct ocfs2_extent_tree *et,
96*4882a593Smuzhiyun 				       u32 clusters);
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	/*
99*4882a593Smuzhiyun 	 * If ->eo_insert_check() exists, it is called before rec is
100*4882a593Smuzhiyun 	 * inserted into the extent tree.  It is optional.
101*4882a593Smuzhiyun 	 */
102*4882a593Smuzhiyun 	int (*eo_insert_check)(struct ocfs2_extent_tree *et,
103*4882a593Smuzhiyun 			       struct ocfs2_extent_rec *rec);
104*4882a593Smuzhiyun 	int (*eo_sanity_check)(struct ocfs2_extent_tree *et);
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	/*
107*4882a593Smuzhiyun 	 * --------------------------------------------------------------
108*4882a593Smuzhiyun 	 * The remaining are internal to ocfs2_extent_tree and don't have
109*4882a593Smuzhiyun 	 * accessor functions
110*4882a593Smuzhiyun 	 */
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	/*
113*4882a593Smuzhiyun 	 * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el.
114*4882a593Smuzhiyun 	 * It is required.
115*4882a593Smuzhiyun 	 */
116*4882a593Smuzhiyun 	void (*eo_fill_root_el)(struct ocfs2_extent_tree *et);
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	/*
119*4882a593Smuzhiyun 	 * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if
120*4882a593Smuzhiyun 	 * it exists.  If it does not, et->et_max_leaf_clusters is set
121*4882a593Smuzhiyun 	 * to 0 (unlimited).  Optional.
122*4882a593Smuzhiyun 	 */
123*4882a593Smuzhiyun 	void (*eo_fill_max_leaf_clusters)(struct ocfs2_extent_tree *et);
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	/*
126*4882a593Smuzhiyun 	 * ->eo_extent_contig test whether the 2 ocfs2_extent_rec
127*4882a593Smuzhiyun 	 * are contiguous or not. Optional. Don't need to set it if use
128*4882a593Smuzhiyun 	 * ocfs2_extent_rec as the tree leaf.
129*4882a593Smuzhiyun 	 */
130*4882a593Smuzhiyun 	enum ocfs2_contig_type
131*4882a593Smuzhiyun 		(*eo_extent_contig)(struct ocfs2_extent_tree *et,
132*4882a593Smuzhiyun 				    struct ocfs2_extent_rec *ext,
133*4882a593Smuzhiyun 				    struct ocfs2_extent_rec *insert_rec);
134*4882a593Smuzhiyun };
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun /*
138*4882a593Smuzhiyun  * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check
139*4882a593Smuzhiyun  * in the methods.
140*4882a593Smuzhiyun  */
141*4882a593Smuzhiyun static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et);
142*4882a593Smuzhiyun static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
143*4882a593Smuzhiyun 					 u64 blkno);
144*4882a593Smuzhiyun static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et,
145*4882a593Smuzhiyun 					 u32 clusters);
146*4882a593Smuzhiyun static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et,
147*4882a593Smuzhiyun 					   struct ocfs2_extent_rec *rec);
148*4882a593Smuzhiyun static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et,
149*4882a593Smuzhiyun 					     u32 clusters);
150*4882a593Smuzhiyun static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et,
151*4882a593Smuzhiyun 				     struct ocfs2_extent_rec *rec);
152*4882a593Smuzhiyun static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et);
153*4882a593Smuzhiyun static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et);
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun static int ocfs2_reuse_blk_from_dealloc(handle_t *handle,
156*4882a593Smuzhiyun 					struct ocfs2_extent_tree *et,
157*4882a593Smuzhiyun 					struct buffer_head **new_eb_bh,
158*4882a593Smuzhiyun 					int blk_wanted, int *blk_given);
159*4882a593Smuzhiyun static int ocfs2_is_dealloc_empty(struct ocfs2_extent_tree *et);
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun static const struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
162*4882a593Smuzhiyun 	.eo_set_last_eb_blk	= ocfs2_dinode_set_last_eb_blk,
163*4882a593Smuzhiyun 	.eo_get_last_eb_blk	= ocfs2_dinode_get_last_eb_blk,
164*4882a593Smuzhiyun 	.eo_update_clusters	= ocfs2_dinode_update_clusters,
165*4882a593Smuzhiyun 	.eo_extent_map_insert	= ocfs2_dinode_extent_map_insert,
166*4882a593Smuzhiyun 	.eo_extent_map_truncate	= ocfs2_dinode_extent_map_truncate,
167*4882a593Smuzhiyun 	.eo_insert_check	= ocfs2_dinode_insert_check,
168*4882a593Smuzhiyun 	.eo_sanity_check	= ocfs2_dinode_sanity_check,
169*4882a593Smuzhiyun 	.eo_fill_root_el	= ocfs2_dinode_fill_root_el,
170*4882a593Smuzhiyun };
171*4882a593Smuzhiyun 
ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree * et,u64 blkno)172*4882a593Smuzhiyun static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
173*4882a593Smuzhiyun 					 u64 blkno)
174*4882a593Smuzhiyun {
175*4882a593Smuzhiyun 	struct ocfs2_dinode *di = et->et_object;
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
178*4882a593Smuzhiyun 	di->i_last_eb_blk = cpu_to_le64(blkno);
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun 
ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree * et)181*4882a593Smuzhiyun static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun 	struct ocfs2_dinode *di = et->et_object;
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
186*4882a593Smuzhiyun 	return le64_to_cpu(di->i_last_eb_blk);
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun 
ocfs2_dinode_update_clusters(struct ocfs2_extent_tree * et,u32 clusters)189*4882a593Smuzhiyun static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et,
190*4882a593Smuzhiyun 					 u32 clusters)
191*4882a593Smuzhiyun {
192*4882a593Smuzhiyun 	struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci);
193*4882a593Smuzhiyun 	struct ocfs2_dinode *di = et->et_object;
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	le32_add_cpu(&di->i_clusters, clusters);
196*4882a593Smuzhiyun 	spin_lock(&oi->ip_lock);
197*4882a593Smuzhiyun 	oi->ip_clusters = le32_to_cpu(di->i_clusters);
198*4882a593Smuzhiyun 	spin_unlock(&oi->ip_lock);
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun 
ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree * et,struct ocfs2_extent_rec * rec)201*4882a593Smuzhiyun static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et,
202*4882a593Smuzhiyun 					   struct ocfs2_extent_rec *rec)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun 	struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode;
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	ocfs2_extent_map_insert_rec(inode, rec);
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun 
ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree * et,u32 clusters)209*4882a593Smuzhiyun static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et,
210*4882a593Smuzhiyun 					     u32 clusters)
211*4882a593Smuzhiyun {
212*4882a593Smuzhiyun 	struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode;
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	ocfs2_extent_map_trunc(inode, clusters);
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun 
ocfs2_dinode_insert_check(struct ocfs2_extent_tree * et,struct ocfs2_extent_rec * rec)217*4882a593Smuzhiyun static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et,
218*4882a593Smuzhiyun 				     struct ocfs2_extent_rec *rec)
219*4882a593Smuzhiyun {
220*4882a593Smuzhiyun 	struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci);
221*4882a593Smuzhiyun 	struct ocfs2_super *osb = OCFS2_SB(oi->vfs_inode.i_sb);
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 	BUG_ON(oi->ip_dyn_features & OCFS2_INLINE_DATA_FL);
224*4882a593Smuzhiyun 	mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
225*4882a593Smuzhiyun 			(oi->ip_clusters != le32_to_cpu(rec->e_cpos)),
226*4882a593Smuzhiyun 			"Device %s, asking for sparse allocation: inode %llu, "
227*4882a593Smuzhiyun 			"cpos %u, clusters %u\n",
228*4882a593Smuzhiyun 			osb->dev_str,
229*4882a593Smuzhiyun 			(unsigned long long)oi->ip_blkno,
230*4882a593Smuzhiyun 			rec->e_cpos, oi->ip_clusters);
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 	return 0;
233*4882a593Smuzhiyun }
234*4882a593Smuzhiyun 
ocfs2_dinode_sanity_check(struct ocfs2_extent_tree * et)235*4882a593Smuzhiyun static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et)
236*4882a593Smuzhiyun {
237*4882a593Smuzhiyun 	struct ocfs2_dinode *di = et->et_object;
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
240*4882a593Smuzhiyun 	BUG_ON(!OCFS2_IS_VALID_DINODE(di));
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	return 0;
243*4882a593Smuzhiyun }
244*4882a593Smuzhiyun 
ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree * et)245*4882a593Smuzhiyun static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et)
246*4882a593Smuzhiyun {
247*4882a593Smuzhiyun 	struct ocfs2_dinode *di = et->et_object;
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 	et->et_root_el = &di->id2.i_list;
250*4882a593Smuzhiyun }
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 
ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree * et)253*4882a593Smuzhiyun static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et)
254*4882a593Smuzhiyun {
255*4882a593Smuzhiyun 	struct ocfs2_xattr_value_buf *vb = et->et_object;
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	et->et_root_el = &vb->vb_xv->xr_list;
258*4882a593Smuzhiyun }
259*4882a593Smuzhiyun 
ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree * et,u64 blkno)260*4882a593Smuzhiyun static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
261*4882a593Smuzhiyun 					      u64 blkno)
262*4882a593Smuzhiyun {
263*4882a593Smuzhiyun 	struct ocfs2_xattr_value_buf *vb = et->et_object;
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 	vb->vb_xv->xr_last_eb_blk = cpu_to_le64(blkno);
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun 
ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree * et)268*4882a593Smuzhiyun static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
269*4882a593Smuzhiyun {
270*4882a593Smuzhiyun 	struct ocfs2_xattr_value_buf *vb = et->et_object;
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 	return le64_to_cpu(vb->vb_xv->xr_last_eb_blk);
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun 
ocfs2_xattr_value_update_clusters(struct ocfs2_extent_tree * et,u32 clusters)275*4882a593Smuzhiyun static void ocfs2_xattr_value_update_clusters(struct ocfs2_extent_tree *et,
276*4882a593Smuzhiyun 					      u32 clusters)
277*4882a593Smuzhiyun {
278*4882a593Smuzhiyun 	struct ocfs2_xattr_value_buf *vb = et->et_object;
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	le32_add_cpu(&vb->vb_xv->xr_clusters, clusters);
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun static const struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = {
284*4882a593Smuzhiyun 	.eo_set_last_eb_blk	= ocfs2_xattr_value_set_last_eb_blk,
285*4882a593Smuzhiyun 	.eo_get_last_eb_blk	= ocfs2_xattr_value_get_last_eb_blk,
286*4882a593Smuzhiyun 	.eo_update_clusters	= ocfs2_xattr_value_update_clusters,
287*4882a593Smuzhiyun 	.eo_fill_root_el	= ocfs2_xattr_value_fill_root_el,
288*4882a593Smuzhiyun };
289*4882a593Smuzhiyun 
ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree * et)290*4882a593Smuzhiyun static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et)
291*4882a593Smuzhiyun {
292*4882a593Smuzhiyun 	struct ocfs2_xattr_block *xb = et->et_object;
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	et->et_root_el = &xb->xb_attrs.xb_root.xt_list;
295*4882a593Smuzhiyun }
296*4882a593Smuzhiyun 
ocfs2_xattr_tree_fill_max_leaf_clusters(struct ocfs2_extent_tree * et)297*4882a593Smuzhiyun static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct ocfs2_extent_tree *et)
298*4882a593Smuzhiyun {
299*4882a593Smuzhiyun 	struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
300*4882a593Smuzhiyun 	et->et_max_leaf_clusters =
301*4882a593Smuzhiyun 		ocfs2_clusters_for_bytes(sb, OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
302*4882a593Smuzhiyun }
303*4882a593Smuzhiyun 
ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree * et,u64 blkno)304*4882a593Smuzhiyun static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
305*4882a593Smuzhiyun 					     u64 blkno)
306*4882a593Smuzhiyun {
307*4882a593Smuzhiyun 	struct ocfs2_xattr_block *xb = et->et_object;
308*4882a593Smuzhiyun 	struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun 	xt->xt_last_eb_blk = cpu_to_le64(blkno);
311*4882a593Smuzhiyun }
312*4882a593Smuzhiyun 
ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree * et)313*4882a593Smuzhiyun static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
314*4882a593Smuzhiyun {
315*4882a593Smuzhiyun 	struct ocfs2_xattr_block *xb = et->et_object;
316*4882a593Smuzhiyun 	struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	return le64_to_cpu(xt->xt_last_eb_blk);
319*4882a593Smuzhiyun }
320*4882a593Smuzhiyun 
ocfs2_xattr_tree_update_clusters(struct ocfs2_extent_tree * et,u32 clusters)321*4882a593Smuzhiyun static void ocfs2_xattr_tree_update_clusters(struct ocfs2_extent_tree *et,
322*4882a593Smuzhiyun 					     u32 clusters)
323*4882a593Smuzhiyun {
324*4882a593Smuzhiyun 	struct ocfs2_xattr_block *xb = et->et_object;
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 	le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
327*4882a593Smuzhiyun }
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun static const struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
330*4882a593Smuzhiyun 	.eo_set_last_eb_blk	= ocfs2_xattr_tree_set_last_eb_blk,
331*4882a593Smuzhiyun 	.eo_get_last_eb_blk	= ocfs2_xattr_tree_get_last_eb_blk,
332*4882a593Smuzhiyun 	.eo_update_clusters	= ocfs2_xattr_tree_update_clusters,
333*4882a593Smuzhiyun 	.eo_fill_root_el	= ocfs2_xattr_tree_fill_root_el,
334*4882a593Smuzhiyun 	.eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters,
335*4882a593Smuzhiyun };
336*4882a593Smuzhiyun 
ocfs2_dx_root_set_last_eb_blk(struct ocfs2_extent_tree * et,u64 blkno)337*4882a593Smuzhiyun static void ocfs2_dx_root_set_last_eb_blk(struct ocfs2_extent_tree *et,
338*4882a593Smuzhiyun 					  u64 blkno)
339*4882a593Smuzhiyun {
340*4882a593Smuzhiyun 	struct ocfs2_dx_root_block *dx_root = et->et_object;
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 	dx_root->dr_last_eb_blk = cpu_to_le64(blkno);
343*4882a593Smuzhiyun }
344*4882a593Smuzhiyun 
ocfs2_dx_root_get_last_eb_blk(struct ocfs2_extent_tree * et)345*4882a593Smuzhiyun static u64 ocfs2_dx_root_get_last_eb_blk(struct ocfs2_extent_tree *et)
346*4882a593Smuzhiyun {
347*4882a593Smuzhiyun 	struct ocfs2_dx_root_block *dx_root = et->et_object;
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun 	return le64_to_cpu(dx_root->dr_last_eb_blk);
350*4882a593Smuzhiyun }
351*4882a593Smuzhiyun 
ocfs2_dx_root_update_clusters(struct ocfs2_extent_tree * et,u32 clusters)352*4882a593Smuzhiyun static void ocfs2_dx_root_update_clusters(struct ocfs2_extent_tree *et,
353*4882a593Smuzhiyun 					  u32 clusters)
354*4882a593Smuzhiyun {
355*4882a593Smuzhiyun 	struct ocfs2_dx_root_block *dx_root = et->et_object;
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun 	le32_add_cpu(&dx_root->dr_clusters, clusters);
358*4882a593Smuzhiyun }
359*4882a593Smuzhiyun 
ocfs2_dx_root_sanity_check(struct ocfs2_extent_tree * et)360*4882a593Smuzhiyun static int ocfs2_dx_root_sanity_check(struct ocfs2_extent_tree *et)
361*4882a593Smuzhiyun {
362*4882a593Smuzhiyun 	struct ocfs2_dx_root_block *dx_root = et->et_object;
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 	BUG_ON(!OCFS2_IS_VALID_DX_ROOT(dx_root));
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 	return 0;
367*4882a593Smuzhiyun }
368*4882a593Smuzhiyun 
ocfs2_dx_root_fill_root_el(struct ocfs2_extent_tree * et)369*4882a593Smuzhiyun static void ocfs2_dx_root_fill_root_el(struct ocfs2_extent_tree *et)
370*4882a593Smuzhiyun {
371*4882a593Smuzhiyun 	struct ocfs2_dx_root_block *dx_root = et->et_object;
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 	et->et_root_el = &dx_root->dr_list;
374*4882a593Smuzhiyun }
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun static const struct ocfs2_extent_tree_operations ocfs2_dx_root_et_ops = {
377*4882a593Smuzhiyun 	.eo_set_last_eb_blk	= ocfs2_dx_root_set_last_eb_blk,
378*4882a593Smuzhiyun 	.eo_get_last_eb_blk	= ocfs2_dx_root_get_last_eb_blk,
379*4882a593Smuzhiyun 	.eo_update_clusters	= ocfs2_dx_root_update_clusters,
380*4882a593Smuzhiyun 	.eo_sanity_check	= ocfs2_dx_root_sanity_check,
381*4882a593Smuzhiyun 	.eo_fill_root_el	= ocfs2_dx_root_fill_root_el,
382*4882a593Smuzhiyun };
383*4882a593Smuzhiyun 
ocfs2_refcount_tree_fill_root_el(struct ocfs2_extent_tree * et)384*4882a593Smuzhiyun static void ocfs2_refcount_tree_fill_root_el(struct ocfs2_extent_tree *et)
385*4882a593Smuzhiyun {
386*4882a593Smuzhiyun 	struct ocfs2_refcount_block *rb = et->et_object;
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun 	et->et_root_el = &rb->rf_list;
389*4882a593Smuzhiyun }
390*4882a593Smuzhiyun 
ocfs2_refcount_tree_set_last_eb_blk(struct ocfs2_extent_tree * et,u64 blkno)391*4882a593Smuzhiyun static void ocfs2_refcount_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
392*4882a593Smuzhiyun 						u64 blkno)
393*4882a593Smuzhiyun {
394*4882a593Smuzhiyun 	struct ocfs2_refcount_block *rb = et->et_object;
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 	rb->rf_last_eb_blk = cpu_to_le64(blkno);
397*4882a593Smuzhiyun }
398*4882a593Smuzhiyun 
ocfs2_refcount_tree_get_last_eb_blk(struct ocfs2_extent_tree * et)399*4882a593Smuzhiyun static u64 ocfs2_refcount_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
400*4882a593Smuzhiyun {
401*4882a593Smuzhiyun 	struct ocfs2_refcount_block *rb = et->et_object;
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun 	return le64_to_cpu(rb->rf_last_eb_blk);
404*4882a593Smuzhiyun }
405*4882a593Smuzhiyun 
ocfs2_refcount_tree_update_clusters(struct ocfs2_extent_tree * et,u32 clusters)406*4882a593Smuzhiyun static void ocfs2_refcount_tree_update_clusters(struct ocfs2_extent_tree *et,
407*4882a593Smuzhiyun 						u32 clusters)
408*4882a593Smuzhiyun {
409*4882a593Smuzhiyun 	struct ocfs2_refcount_block *rb = et->et_object;
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun 	le32_add_cpu(&rb->rf_clusters, clusters);
412*4882a593Smuzhiyun }
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun static enum ocfs2_contig_type
ocfs2_refcount_tree_extent_contig(struct ocfs2_extent_tree * et,struct ocfs2_extent_rec * ext,struct ocfs2_extent_rec * insert_rec)415*4882a593Smuzhiyun ocfs2_refcount_tree_extent_contig(struct ocfs2_extent_tree *et,
416*4882a593Smuzhiyun 				  struct ocfs2_extent_rec *ext,
417*4882a593Smuzhiyun 				  struct ocfs2_extent_rec *insert_rec)
418*4882a593Smuzhiyun {
419*4882a593Smuzhiyun 	return CONTIG_NONE;
420*4882a593Smuzhiyun }
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun static const struct ocfs2_extent_tree_operations ocfs2_refcount_tree_et_ops = {
423*4882a593Smuzhiyun 	.eo_set_last_eb_blk	= ocfs2_refcount_tree_set_last_eb_blk,
424*4882a593Smuzhiyun 	.eo_get_last_eb_blk	= ocfs2_refcount_tree_get_last_eb_blk,
425*4882a593Smuzhiyun 	.eo_update_clusters	= ocfs2_refcount_tree_update_clusters,
426*4882a593Smuzhiyun 	.eo_fill_root_el	= ocfs2_refcount_tree_fill_root_el,
427*4882a593Smuzhiyun 	.eo_extent_contig	= ocfs2_refcount_tree_extent_contig,
428*4882a593Smuzhiyun };
429*4882a593Smuzhiyun 
__ocfs2_init_extent_tree(struct ocfs2_extent_tree * et,struct ocfs2_caching_info * ci,struct buffer_head * bh,ocfs2_journal_access_func access,void * obj,const struct ocfs2_extent_tree_operations * ops)430*4882a593Smuzhiyun static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et,
431*4882a593Smuzhiyun 				     struct ocfs2_caching_info *ci,
432*4882a593Smuzhiyun 				     struct buffer_head *bh,
433*4882a593Smuzhiyun 				     ocfs2_journal_access_func access,
434*4882a593Smuzhiyun 				     void *obj,
435*4882a593Smuzhiyun 				     const struct ocfs2_extent_tree_operations *ops)
436*4882a593Smuzhiyun {
437*4882a593Smuzhiyun 	et->et_ops = ops;
438*4882a593Smuzhiyun 	et->et_root_bh = bh;
439*4882a593Smuzhiyun 	et->et_ci = ci;
440*4882a593Smuzhiyun 	et->et_root_journal_access = access;
441*4882a593Smuzhiyun 	if (!obj)
442*4882a593Smuzhiyun 		obj = (void *)bh->b_data;
443*4882a593Smuzhiyun 	et->et_object = obj;
444*4882a593Smuzhiyun 	et->et_dealloc = NULL;
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 	et->et_ops->eo_fill_root_el(et);
447*4882a593Smuzhiyun 	if (!et->et_ops->eo_fill_max_leaf_clusters)
448*4882a593Smuzhiyun 		et->et_max_leaf_clusters = 0;
449*4882a593Smuzhiyun 	else
450*4882a593Smuzhiyun 		et->et_ops->eo_fill_max_leaf_clusters(et);
451*4882a593Smuzhiyun }
452*4882a593Smuzhiyun 
ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree * et,struct ocfs2_caching_info * ci,struct buffer_head * bh)453*4882a593Smuzhiyun void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
454*4882a593Smuzhiyun 				   struct ocfs2_caching_info *ci,
455*4882a593Smuzhiyun 				   struct buffer_head *bh)
456*4882a593Smuzhiyun {
457*4882a593Smuzhiyun 	__ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_di,
458*4882a593Smuzhiyun 				 NULL, &ocfs2_dinode_et_ops);
459*4882a593Smuzhiyun }
460*4882a593Smuzhiyun 
ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree * et,struct ocfs2_caching_info * ci,struct buffer_head * bh)461*4882a593Smuzhiyun void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
462*4882a593Smuzhiyun 				       struct ocfs2_caching_info *ci,
463*4882a593Smuzhiyun 				       struct buffer_head *bh)
464*4882a593Smuzhiyun {
465*4882a593Smuzhiyun 	__ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_xb,
466*4882a593Smuzhiyun 				 NULL, &ocfs2_xattr_tree_et_ops);
467*4882a593Smuzhiyun }
468*4882a593Smuzhiyun 
ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree * et,struct ocfs2_caching_info * ci,struct ocfs2_xattr_value_buf * vb)469*4882a593Smuzhiyun void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
470*4882a593Smuzhiyun 					struct ocfs2_caching_info *ci,
471*4882a593Smuzhiyun 					struct ocfs2_xattr_value_buf *vb)
472*4882a593Smuzhiyun {
473*4882a593Smuzhiyun 	__ocfs2_init_extent_tree(et, ci, vb->vb_bh, vb->vb_access, vb,
474*4882a593Smuzhiyun 				 &ocfs2_xattr_value_et_ops);
475*4882a593Smuzhiyun }
476*4882a593Smuzhiyun 
ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree * et,struct ocfs2_caching_info * ci,struct buffer_head * bh)477*4882a593Smuzhiyun void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree *et,
478*4882a593Smuzhiyun 				    struct ocfs2_caching_info *ci,
479*4882a593Smuzhiyun 				    struct buffer_head *bh)
480*4882a593Smuzhiyun {
481*4882a593Smuzhiyun 	__ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_dr,
482*4882a593Smuzhiyun 				 NULL, &ocfs2_dx_root_et_ops);
483*4882a593Smuzhiyun }
484*4882a593Smuzhiyun 
ocfs2_init_refcount_extent_tree(struct ocfs2_extent_tree * et,struct ocfs2_caching_info * ci,struct buffer_head * bh)485*4882a593Smuzhiyun void ocfs2_init_refcount_extent_tree(struct ocfs2_extent_tree *et,
486*4882a593Smuzhiyun 				     struct ocfs2_caching_info *ci,
487*4882a593Smuzhiyun 				     struct buffer_head *bh)
488*4882a593Smuzhiyun {
489*4882a593Smuzhiyun 	__ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_rb,
490*4882a593Smuzhiyun 				 NULL, &ocfs2_refcount_tree_et_ops);
491*4882a593Smuzhiyun }
492*4882a593Smuzhiyun 
ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree * et,u64 new_last_eb_blk)493*4882a593Smuzhiyun static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
494*4882a593Smuzhiyun 					    u64 new_last_eb_blk)
495*4882a593Smuzhiyun {
496*4882a593Smuzhiyun 	et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
497*4882a593Smuzhiyun }
498*4882a593Smuzhiyun 
ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree * et)499*4882a593Smuzhiyun static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
500*4882a593Smuzhiyun {
501*4882a593Smuzhiyun 	return et->et_ops->eo_get_last_eb_blk(et);
502*4882a593Smuzhiyun }
503*4882a593Smuzhiyun 
ocfs2_et_update_clusters(struct ocfs2_extent_tree * et,u32 clusters)504*4882a593Smuzhiyun static inline void ocfs2_et_update_clusters(struct ocfs2_extent_tree *et,
505*4882a593Smuzhiyun 					    u32 clusters)
506*4882a593Smuzhiyun {
507*4882a593Smuzhiyun 	et->et_ops->eo_update_clusters(et, clusters);
508*4882a593Smuzhiyun }
509*4882a593Smuzhiyun 
ocfs2_et_extent_map_insert(struct ocfs2_extent_tree * et,struct ocfs2_extent_rec * rec)510*4882a593Smuzhiyun static inline void ocfs2_et_extent_map_insert(struct ocfs2_extent_tree *et,
511*4882a593Smuzhiyun 					      struct ocfs2_extent_rec *rec)
512*4882a593Smuzhiyun {
513*4882a593Smuzhiyun 	if (et->et_ops->eo_extent_map_insert)
514*4882a593Smuzhiyun 		et->et_ops->eo_extent_map_insert(et, rec);
515*4882a593Smuzhiyun }
516*4882a593Smuzhiyun 
ocfs2_et_extent_map_truncate(struct ocfs2_extent_tree * et,u32 clusters)517*4882a593Smuzhiyun static inline void ocfs2_et_extent_map_truncate(struct ocfs2_extent_tree *et,
518*4882a593Smuzhiyun 						u32 clusters)
519*4882a593Smuzhiyun {
520*4882a593Smuzhiyun 	if (et->et_ops->eo_extent_map_truncate)
521*4882a593Smuzhiyun 		et->et_ops->eo_extent_map_truncate(et, clusters);
522*4882a593Smuzhiyun }
523*4882a593Smuzhiyun 
ocfs2_et_root_journal_access(handle_t * handle,struct ocfs2_extent_tree * et,int type)524*4882a593Smuzhiyun static inline int ocfs2_et_root_journal_access(handle_t *handle,
525*4882a593Smuzhiyun 					       struct ocfs2_extent_tree *et,
526*4882a593Smuzhiyun 					       int type)
527*4882a593Smuzhiyun {
528*4882a593Smuzhiyun 	return et->et_root_journal_access(handle, et->et_ci, et->et_root_bh,
529*4882a593Smuzhiyun 					  type);
530*4882a593Smuzhiyun }
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun static inline enum ocfs2_contig_type
ocfs2_et_extent_contig(struct ocfs2_extent_tree * et,struct ocfs2_extent_rec * rec,struct ocfs2_extent_rec * insert_rec)533*4882a593Smuzhiyun 	ocfs2_et_extent_contig(struct ocfs2_extent_tree *et,
534*4882a593Smuzhiyun 			       struct ocfs2_extent_rec *rec,
535*4882a593Smuzhiyun 			       struct ocfs2_extent_rec *insert_rec)
536*4882a593Smuzhiyun {
537*4882a593Smuzhiyun 	if (et->et_ops->eo_extent_contig)
538*4882a593Smuzhiyun 		return et->et_ops->eo_extent_contig(et, rec, insert_rec);
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun 	return ocfs2_extent_rec_contig(
541*4882a593Smuzhiyun 				ocfs2_metadata_cache_get_super(et->et_ci),
542*4882a593Smuzhiyun 				rec, insert_rec);
543*4882a593Smuzhiyun }
544*4882a593Smuzhiyun 
ocfs2_et_insert_check(struct ocfs2_extent_tree * et,struct ocfs2_extent_rec * rec)545*4882a593Smuzhiyun static inline int ocfs2_et_insert_check(struct ocfs2_extent_tree *et,
546*4882a593Smuzhiyun 					struct ocfs2_extent_rec *rec)
547*4882a593Smuzhiyun {
548*4882a593Smuzhiyun 	int ret = 0;
549*4882a593Smuzhiyun 
550*4882a593Smuzhiyun 	if (et->et_ops->eo_insert_check)
551*4882a593Smuzhiyun 		ret = et->et_ops->eo_insert_check(et, rec);
552*4882a593Smuzhiyun 	return ret;
553*4882a593Smuzhiyun }
554*4882a593Smuzhiyun 
ocfs2_et_sanity_check(struct ocfs2_extent_tree * et)555*4882a593Smuzhiyun static inline int ocfs2_et_sanity_check(struct ocfs2_extent_tree *et)
556*4882a593Smuzhiyun {
557*4882a593Smuzhiyun 	int ret = 0;
558*4882a593Smuzhiyun 
559*4882a593Smuzhiyun 	if (et->et_ops->eo_sanity_check)
560*4882a593Smuzhiyun 		ret = et->et_ops->eo_sanity_check(et);
561*4882a593Smuzhiyun 	return ret;
562*4882a593Smuzhiyun }
563*4882a593Smuzhiyun 
564*4882a593Smuzhiyun static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
565*4882a593Smuzhiyun 					 struct ocfs2_extent_block *eb);
566*4882a593Smuzhiyun static void ocfs2_adjust_rightmost_records(handle_t *handle,
567*4882a593Smuzhiyun 					   struct ocfs2_extent_tree *et,
568*4882a593Smuzhiyun 					   struct ocfs2_path *path,
569*4882a593Smuzhiyun 					   struct ocfs2_extent_rec *insert_rec);
570*4882a593Smuzhiyun /*
571*4882a593Smuzhiyun  * Reset the actual path elements so that we can re-use the structure
572*4882a593Smuzhiyun  * to build another path. Generally, this involves freeing the buffer
573*4882a593Smuzhiyun  * heads.
574*4882a593Smuzhiyun  */
ocfs2_reinit_path(struct ocfs2_path * path,int keep_root)575*4882a593Smuzhiyun void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
576*4882a593Smuzhiyun {
577*4882a593Smuzhiyun 	int i, start = 0, depth = 0;
578*4882a593Smuzhiyun 	struct ocfs2_path_item *node;
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun 	if (keep_root)
581*4882a593Smuzhiyun 		start = 1;
582*4882a593Smuzhiyun 
583*4882a593Smuzhiyun 	for(i = start; i < path_num_items(path); i++) {
584*4882a593Smuzhiyun 		node = &path->p_node[i];
585*4882a593Smuzhiyun 
586*4882a593Smuzhiyun 		brelse(node->bh);
587*4882a593Smuzhiyun 		node->bh = NULL;
588*4882a593Smuzhiyun 		node->el = NULL;
589*4882a593Smuzhiyun 	}
590*4882a593Smuzhiyun 
591*4882a593Smuzhiyun 	/*
592*4882a593Smuzhiyun 	 * Tree depth may change during truncate, or insert. If we're
593*4882a593Smuzhiyun 	 * keeping the root extent list, then make sure that our path
594*4882a593Smuzhiyun 	 * structure reflects the proper depth.
595*4882a593Smuzhiyun 	 */
596*4882a593Smuzhiyun 	if (keep_root)
597*4882a593Smuzhiyun 		depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
598*4882a593Smuzhiyun 	else
599*4882a593Smuzhiyun 		path_root_access(path) = NULL;
600*4882a593Smuzhiyun 
601*4882a593Smuzhiyun 	path->p_tree_depth = depth;
602*4882a593Smuzhiyun }
603*4882a593Smuzhiyun 
ocfs2_free_path(struct ocfs2_path * path)604*4882a593Smuzhiyun void ocfs2_free_path(struct ocfs2_path *path)
605*4882a593Smuzhiyun {
606*4882a593Smuzhiyun 	if (path) {
607*4882a593Smuzhiyun 		ocfs2_reinit_path(path, 0);
608*4882a593Smuzhiyun 		kfree(path);
609*4882a593Smuzhiyun 	}
610*4882a593Smuzhiyun }
611*4882a593Smuzhiyun 
612*4882a593Smuzhiyun /*
613*4882a593Smuzhiyun  * All the elements of src into dest. After this call, src could be freed
614*4882a593Smuzhiyun  * without affecting dest.
615*4882a593Smuzhiyun  *
616*4882a593Smuzhiyun  * Both paths should have the same root. Any non-root elements of dest
617*4882a593Smuzhiyun  * will be freed.
618*4882a593Smuzhiyun  */
ocfs2_cp_path(struct ocfs2_path * dest,struct ocfs2_path * src)619*4882a593Smuzhiyun static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
620*4882a593Smuzhiyun {
621*4882a593Smuzhiyun 	int i;
622*4882a593Smuzhiyun 
623*4882a593Smuzhiyun 	BUG_ON(path_root_bh(dest) != path_root_bh(src));
624*4882a593Smuzhiyun 	BUG_ON(path_root_el(dest) != path_root_el(src));
625*4882a593Smuzhiyun 	BUG_ON(path_root_access(dest) != path_root_access(src));
626*4882a593Smuzhiyun 
627*4882a593Smuzhiyun 	ocfs2_reinit_path(dest, 1);
628*4882a593Smuzhiyun 
629*4882a593Smuzhiyun 	for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
630*4882a593Smuzhiyun 		dest->p_node[i].bh = src->p_node[i].bh;
631*4882a593Smuzhiyun 		dest->p_node[i].el = src->p_node[i].el;
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun 		if (dest->p_node[i].bh)
634*4882a593Smuzhiyun 			get_bh(dest->p_node[i].bh);
635*4882a593Smuzhiyun 	}
636*4882a593Smuzhiyun }
637*4882a593Smuzhiyun 
638*4882a593Smuzhiyun /*
639*4882a593Smuzhiyun  * Make the *dest path the same as src and re-initialize src path to
640*4882a593Smuzhiyun  * have a root only.
641*4882a593Smuzhiyun  */
ocfs2_mv_path(struct ocfs2_path * dest,struct ocfs2_path * src)642*4882a593Smuzhiyun static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
643*4882a593Smuzhiyun {
644*4882a593Smuzhiyun 	int i;
645*4882a593Smuzhiyun 
646*4882a593Smuzhiyun 	BUG_ON(path_root_bh(dest) != path_root_bh(src));
647*4882a593Smuzhiyun 	BUG_ON(path_root_access(dest) != path_root_access(src));
648*4882a593Smuzhiyun 
649*4882a593Smuzhiyun 	for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
650*4882a593Smuzhiyun 		brelse(dest->p_node[i].bh);
651*4882a593Smuzhiyun 
652*4882a593Smuzhiyun 		dest->p_node[i].bh = src->p_node[i].bh;
653*4882a593Smuzhiyun 		dest->p_node[i].el = src->p_node[i].el;
654*4882a593Smuzhiyun 
655*4882a593Smuzhiyun 		src->p_node[i].bh = NULL;
656*4882a593Smuzhiyun 		src->p_node[i].el = NULL;
657*4882a593Smuzhiyun 	}
658*4882a593Smuzhiyun }
659*4882a593Smuzhiyun 
660*4882a593Smuzhiyun /*
661*4882a593Smuzhiyun  * Insert an extent block at given index.
662*4882a593Smuzhiyun  *
663*4882a593Smuzhiyun  * This will not take an additional reference on eb_bh.
664*4882a593Smuzhiyun  */
ocfs2_path_insert_eb(struct ocfs2_path * path,int index,struct buffer_head * eb_bh)665*4882a593Smuzhiyun static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
666*4882a593Smuzhiyun 					struct buffer_head *eb_bh)
667*4882a593Smuzhiyun {
668*4882a593Smuzhiyun 	struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
669*4882a593Smuzhiyun 
670*4882a593Smuzhiyun 	/*
671*4882a593Smuzhiyun 	 * Right now, no root bh is an extent block, so this helps
672*4882a593Smuzhiyun 	 * catch code errors with dinode trees. The assertion can be
673*4882a593Smuzhiyun 	 * safely removed if we ever need to insert extent block
674*4882a593Smuzhiyun 	 * structures at the root.
675*4882a593Smuzhiyun 	 */
676*4882a593Smuzhiyun 	BUG_ON(index == 0);
677*4882a593Smuzhiyun 
678*4882a593Smuzhiyun 	path->p_node[index].bh = eb_bh;
679*4882a593Smuzhiyun 	path->p_node[index].el = &eb->h_list;
680*4882a593Smuzhiyun }
681*4882a593Smuzhiyun 
ocfs2_new_path(struct buffer_head * root_bh,struct ocfs2_extent_list * root_el,ocfs2_journal_access_func access)682*4882a593Smuzhiyun static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
683*4882a593Smuzhiyun 					 struct ocfs2_extent_list *root_el,
684*4882a593Smuzhiyun 					 ocfs2_journal_access_func access)
685*4882a593Smuzhiyun {
686*4882a593Smuzhiyun 	struct ocfs2_path *path;
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun 	BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
689*4882a593Smuzhiyun 
690*4882a593Smuzhiyun 	path = kzalloc(sizeof(*path), GFP_NOFS);
691*4882a593Smuzhiyun 	if (path) {
692*4882a593Smuzhiyun 		path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
693*4882a593Smuzhiyun 		get_bh(root_bh);
694*4882a593Smuzhiyun 		path_root_bh(path) = root_bh;
695*4882a593Smuzhiyun 		path_root_el(path) = root_el;
696*4882a593Smuzhiyun 		path_root_access(path) = access;
697*4882a593Smuzhiyun 	}
698*4882a593Smuzhiyun 
699*4882a593Smuzhiyun 	return path;
700*4882a593Smuzhiyun }
701*4882a593Smuzhiyun 
ocfs2_new_path_from_path(struct ocfs2_path * path)702*4882a593Smuzhiyun struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path)
703*4882a593Smuzhiyun {
704*4882a593Smuzhiyun 	return ocfs2_new_path(path_root_bh(path), path_root_el(path),
705*4882a593Smuzhiyun 			      path_root_access(path));
706*4882a593Smuzhiyun }
707*4882a593Smuzhiyun 
ocfs2_new_path_from_et(struct ocfs2_extent_tree * et)708*4882a593Smuzhiyun struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et)
709*4882a593Smuzhiyun {
710*4882a593Smuzhiyun 	return ocfs2_new_path(et->et_root_bh, et->et_root_el,
711*4882a593Smuzhiyun 			      et->et_root_journal_access);
712*4882a593Smuzhiyun }
713*4882a593Smuzhiyun 
714*4882a593Smuzhiyun /*
715*4882a593Smuzhiyun  * Journal the buffer at depth idx.  All idx>0 are extent_blocks,
716*4882a593Smuzhiyun  * otherwise it's the root_access function.
717*4882a593Smuzhiyun  *
718*4882a593Smuzhiyun  * I don't like the way this function's name looks next to
719*4882a593Smuzhiyun  * ocfs2_journal_access_path(), but I don't have a better one.
720*4882a593Smuzhiyun  */
ocfs2_path_bh_journal_access(handle_t * handle,struct ocfs2_caching_info * ci,struct ocfs2_path * path,int idx)721*4882a593Smuzhiyun int ocfs2_path_bh_journal_access(handle_t *handle,
722*4882a593Smuzhiyun 				 struct ocfs2_caching_info *ci,
723*4882a593Smuzhiyun 				 struct ocfs2_path *path,
724*4882a593Smuzhiyun 				 int idx)
725*4882a593Smuzhiyun {
726*4882a593Smuzhiyun 	ocfs2_journal_access_func access = path_root_access(path);
727*4882a593Smuzhiyun 
728*4882a593Smuzhiyun 	if (!access)
729*4882a593Smuzhiyun 		access = ocfs2_journal_access;
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun 	if (idx)
732*4882a593Smuzhiyun 		access = ocfs2_journal_access_eb;
733*4882a593Smuzhiyun 
734*4882a593Smuzhiyun 	return access(handle, ci, path->p_node[idx].bh,
735*4882a593Smuzhiyun 		      OCFS2_JOURNAL_ACCESS_WRITE);
736*4882a593Smuzhiyun }
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun /*
739*4882a593Smuzhiyun  * Convenience function to journal all components in a path.
740*4882a593Smuzhiyun  */
ocfs2_journal_access_path(struct ocfs2_caching_info * ci,handle_t * handle,struct ocfs2_path * path)741*4882a593Smuzhiyun int ocfs2_journal_access_path(struct ocfs2_caching_info *ci,
742*4882a593Smuzhiyun 			      handle_t *handle,
743*4882a593Smuzhiyun 			      struct ocfs2_path *path)
744*4882a593Smuzhiyun {
745*4882a593Smuzhiyun 	int i, ret = 0;
746*4882a593Smuzhiyun 
747*4882a593Smuzhiyun 	if (!path)
748*4882a593Smuzhiyun 		goto out;
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun 	for(i = 0; i < path_num_items(path); i++) {
751*4882a593Smuzhiyun 		ret = ocfs2_path_bh_journal_access(handle, ci, path, i);
752*4882a593Smuzhiyun 		if (ret < 0) {
753*4882a593Smuzhiyun 			mlog_errno(ret);
754*4882a593Smuzhiyun 			goto out;
755*4882a593Smuzhiyun 		}
756*4882a593Smuzhiyun 	}
757*4882a593Smuzhiyun 
758*4882a593Smuzhiyun out:
759*4882a593Smuzhiyun 	return ret;
760*4882a593Smuzhiyun }
761*4882a593Smuzhiyun 
762*4882a593Smuzhiyun /*
763*4882a593Smuzhiyun  * Return the index of the extent record which contains cluster #v_cluster.
764*4882a593Smuzhiyun  * -1 is returned if it was not found.
765*4882a593Smuzhiyun  *
766*4882a593Smuzhiyun  * Should work fine on interior and exterior nodes.
767*4882a593Smuzhiyun  */
ocfs2_search_extent_list(struct ocfs2_extent_list * el,u32 v_cluster)768*4882a593Smuzhiyun int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
769*4882a593Smuzhiyun {
770*4882a593Smuzhiyun 	int ret = -1;
771*4882a593Smuzhiyun 	int i;
772*4882a593Smuzhiyun 	struct ocfs2_extent_rec *rec;
773*4882a593Smuzhiyun 	u32 rec_end, rec_start, clusters;
774*4882a593Smuzhiyun 
775*4882a593Smuzhiyun 	for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
776*4882a593Smuzhiyun 		rec = &el->l_recs[i];
777*4882a593Smuzhiyun 
778*4882a593Smuzhiyun 		rec_start = le32_to_cpu(rec->e_cpos);
779*4882a593Smuzhiyun 		clusters = ocfs2_rec_clusters(el, rec);
780*4882a593Smuzhiyun 
781*4882a593Smuzhiyun 		rec_end = rec_start + clusters;
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun 		if (v_cluster >= rec_start && v_cluster < rec_end) {
784*4882a593Smuzhiyun 			ret = i;
785*4882a593Smuzhiyun 			break;
786*4882a593Smuzhiyun 		}
787*4882a593Smuzhiyun 	}
788*4882a593Smuzhiyun 
789*4882a593Smuzhiyun 	return ret;
790*4882a593Smuzhiyun }
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun /*
793*4882a593Smuzhiyun  * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
794*4882a593Smuzhiyun  * ocfs2_extent_rec_contig only work properly against leaf nodes!
795*4882a593Smuzhiyun  */
ocfs2_block_extent_contig(struct super_block * sb,struct ocfs2_extent_rec * ext,u64 blkno)796*4882a593Smuzhiyun static int ocfs2_block_extent_contig(struct super_block *sb,
797*4882a593Smuzhiyun 				     struct ocfs2_extent_rec *ext,
798*4882a593Smuzhiyun 				     u64 blkno)
799*4882a593Smuzhiyun {
800*4882a593Smuzhiyun 	u64 blk_end = le64_to_cpu(ext->e_blkno);
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun 	blk_end += ocfs2_clusters_to_blocks(sb,
803*4882a593Smuzhiyun 				    le16_to_cpu(ext->e_leaf_clusters));
804*4882a593Smuzhiyun 
805*4882a593Smuzhiyun 	return blkno == blk_end;
806*4882a593Smuzhiyun }
807*4882a593Smuzhiyun 
ocfs2_extents_adjacent(struct ocfs2_extent_rec * left,struct ocfs2_extent_rec * right)808*4882a593Smuzhiyun static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
809*4882a593Smuzhiyun 				  struct ocfs2_extent_rec *right)
810*4882a593Smuzhiyun {
811*4882a593Smuzhiyun 	u32 left_range;
812*4882a593Smuzhiyun 
813*4882a593Smuzhiyun 	left_range = le32_to_cpu(left->e_cpos) +
814*4882a593Smuzhiyun 		le16_to_cpu(left->e_leaf_clusters);
815*4882a593Smuzhiyun 
816*4882a593Smuzhiyun 	return (left_range == le32_to_cpu(right->e_cpos));
817*4882a593Smuzhiyun }
818*4882a593Smuzhiyun 
819*4882a593Smuzhiyun static enum ocfs2_contig_type
ocfs2_extent_rec_contig(struct super_block * sb,struct ocfs2_extent_rec * ext,struct ocfs2_extent_rec * insert_rec)820*4882a593Smuzhiyun 	ocfs2_extent_rec_contig(struct super_block *sb,
821*4882a593Smuzhiyun 				struct ocfs2_extent_rec *ext,
822*4882a593Smuzhiyun 				struct ocfs2_extent_rec *insert_rec)
823*4882a593Smuzhiyun {
824*4882a593Smuzhiyun 	u64 blkno = le64_to_cpu(insert_rec->e_blkno);
825*4882a593Smuzhiyun 
826*4882a593Smuzhiyun 	/*
827*4882a593Smuzhiyun 	 * Refuse to coalesce extent records with different flag
828*4882a593Smuzhiyun 	 * fields - we don't want to mix unwritten extents with user
829*4882a593Smuzhiyun 	 * data.
830*4882a593Smuzhiyun 	 */
831*4882a593Smuzhiyun 	if (ext->e_flags != insert_rec->e_flags)
832*4882a593Smuzhiyun 		return CONTIG_NONE;
833*4882a593Smuzhiyun 
834*4882a593Smuzhiyun 	if (ocfs2_extents_adjacent(ext, insert_rec) &&
835*4882a593Smuzhiyun 	    ocfs2_block_extent_contig(sb, ext, blkno))
836*4882a593Smuzhiyun 			return CONTIG_RIGHT;
837*4882a593Smuzhiyun 
838*4882a593Smuzhiyun 	blkno = le64_to_cpu(ext->e_blkno);
839*4882a593Smuzhiyun 	if (ocfs2_extents_adjacent(insert_rec, ext) &&
840*4882a593Smuzhiyun 	    ocfs2_block_extent_contig(sb, insert_rec, blkno))
841*4882a593Smuzhiyun 		return CONTIG_LEFT;
842*4882a593Smuzhiyun 
843*4882a593Smuzhiyun 	return CONTIG_NONE;
844*4882a593Smuzhiyun }
845*4882a593Smuzhiyun 
846*4882a593Smuzhiyun /*
847*4882a593Smuzhiyun  * NOTE: We can have pretty much any combination of contiguousness and
848*4882a593Smuzhiyun  * appending.
849*4882a593Smuzhiyun  *
850*4882a593Smuzhiyun  * The usefulness of APPEND_TAIL is more in that it lets us know that
851*4882a593Smuzhiyun  * we'll have to update the path to that leaf.
852*4882a593Smuzhiyun  */
853*4882a593Smuzhiyun enum ocfs2_append_type {
854*4882a593Smuzhiyun 	APPEND_NONE = 0,
855*4882a593Smuzhiyun 	APPEND_TAIL,
856*4882a593Smuzhiyun };
857*4882a593Smuzhiyun 
858*4882a593Smuzhiyun enum ocfs2_split_type {
859*4882a593Smuzhiyun 	SPLIT_NONE = 0,
860*4882a593Smuzhiyun 	SPLIT_LEFT,
861*4882a593Smuzhiyun 	SPLIT_RIGHT,
862*4882a593Smuzhiyun };
863*4882a593Smuzhiyun 
864*4882a593Smuzhiyun struct ocfs2_insert_type {
865*4882a593Smuzhiyun 	enum ocfs2_split_type	ins_split;
866*4882a593Smuzhiyun 	enum ocfs2_append_type	ins_appending;
867*4882a593Smuzhiyun 	enum ocfs2_contig_type	ins_contig;
868*4882a593Smuzhiyun 	int			ins_contig_index;
869*4882a593Smuzhiyun 	int			ins_tree_depth;
870*4882a593Smuzhiyun };
871*4882a593Smuzhiyun 
872*4882a593Smuzhiyun struct ocfs2_merge_ctxt {
873*4882a593Smuzhiyun 	enum ocfs2_contig_type	c_contig_type;
874*4882a593Smuzhiyun 	int			c_has_empty_extent;
875*4882a593Smuzhiyun 	int			c_split_covers_rec;
876*4882a593Smuzhiyun };
877*4882a593Smuzhiyun 
ocfs2_validate_extent_block(struct super_block * sb,struct buffer_head * bh)878*4882a593Smuzhiyun static int ocfs2_validate_extent_block(struct super_block *sb,
879*4882a593Smuzhiyun 				       struct buffer_head *bh)
880*4882a593Smuzhiyun {
881*4882a593Smuzhiyun 	int rc;
882*4882a593Smuzhiyun 	struct ocfs2_extent_block *eb =
883*4882a593Smuzhiyun 		(struct ocfs2_extent_block *)bh->b_data;
884*4882a593Smuzhiyun 
885*4882a593Smuzhiyun 	trace_ocfs2_validate_extent_block((unsigned long long)bh->b_blocknr);
886*4882a593Smuzhiyun 
887*4882a593Smuzhiyun 	BUG_ON(!buffer_uptodate(bh));
888*4882a593Smuzhiyun 
889*4882a593Smuzhiyun 	/*
890*4882a593Smuzhiyun 	 * If the ecc fails, we return the error but otherwise
891*4882a593Smuzhiyun 	 * leave the filesystem running.  We know any error is
892*4882a593Smuzhiyun 	 * local to this block.
893*4882a593Smuzhiyun 	 */
894*4882a593Smuzhiyun 	rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check);
895*4882a593Smuzhiyun 	if (rc) {
896*4882a593Smuzhiyun 		mlog(ML_ERROR, "Checksum failed for extent block %llu\n",
897*4882a593Smuzhiyun 		     (unsigned long long)bh->b_blocknr);
898*4882a593Smuzhiyun 		return rc;
899*4882a593Smuzhiyun 	}
900*4882a593Smuzhiyun 
901*4882a593Smuzhiyun 	/*
902*4882a593Smuzhiyun 	 * Errors after here are fatal.
903*4882a593Smuzhiyun 	 */
904*4882a593Smuzhiyun 
905*4882a593Smuzhiyun 	if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
906*4882a593Smuzhiyun 		rc = ocfs2_error(sb,
907*4882a593Smuzhiyun 				 "Extent block #%llu has bad signature %.*s\n",
908*4882a593Smuzhiyun 				 (unsigned long long)bh->b_blocknr, 7,
909*4882a593Smuzhiyun 				 eb->h_signature);
910*4882a593Smuzhiyun 		goto bail;
911*4882a593Smuzhiyun 	}
912*4882a593Smuzhiyun 
913*4882a593Smuzhiyun 	if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) {
914*4882a593Smuzhiyun 		rc = ocfs2_error(sb,
915*4882a593Smuzhiyun 				 "Extent block #%llu has an invalid h_blkno of %llu\n",
916*4882a593Smuzhiyun 				 (unsigned long long)bh->b_blocknr,
917*4882a593Smuzhiyun 				 (unsigned long long)le64_to_cpu(eb->h_blkno));
918*4882a593Smuzhiyun 		goto bail;
919*4882a593Smuzhiyun 	}
920*4882a593Smuzhiyun 
921*4882a593Smuzhiyun 	if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation)
922*4882a593Smuzhiyun 		rc = ocfs2_error(sb,
923*4882a593Smuzhiyun 				 "Extent block #%llu has an invalid h_fs_generation of #%u\n",
924*4882a593Smuzhiyun 				 (unsigned long long)bh->b_blocknr,
925*4882a593Smuzhiyun 				 le32_to_cpu(eb->h_fs_generation));
926*4882a593Smuzhiyun bail:
927*4882a593Smuzhiyun 	return rc;
928*4882a593Smuzhiyun }
929*4882a593Smuzhiyun 
ocfs2_read_extent_block(struct ocfs2_caching_info * ci,u64 eb_blkno,struct buffer_head ** bh)930*4882a593Smuzhiyun int ocfs2_read_extent_block(struct ocfs2_caching_info *ci, u64 eb_blkno,
931*4882a593Smuzhiyun 			    struct buffer_head **bh)
932*4882a593Smuzhiyun {
933*4882a593Smuzhiyun 	int rc;
934*4882a593Smuzhiyun 	struct buffer_head *tmp = *bh;
935*4882a593Smuzhiyun 
936*4882a593Smuzhiyun 	rc = ocfs2_read_block(ci, eb_blkno, &tmp,
937*4882a593Smuzhiyun 			      ocfs2_validate_extent_block);
938*4882a593Smuzhiyun 
939*4882a593Smuzhiyun 	/* If ocfs2_read_block() got us a new bh, pass it up. */
940*4882a593Smuzhiyun 	if (!rc && !*bh)
941*4882a593Smuzhiyun 		*bh = tmp;
942*4882a593Smuzhiyun 
943*4882a593Smuzhiyun 	return rc;
944*4882a593Smuzhiyun }
945*4882a593Smuzhiyun 
946*4882a593Smuzhiyun 
947*4882a593Smuzhiyun /*
948*4882a593Smuzhiyun  * How many free extents have we got before we need more meta data?
949*4882a593Smuzhiyun  */
ocfs2_num_free_extents(struct ocfs2_extent_tree * et)950*4882a593Smuzhiyun int ocfs2_num_free_extents(struct ocfs2_extent_tree *et)
951*4882a593Smuzhiyun {
952*4882a593Smuzhiyun 	int retval;
953*4882a593Smuzhiyun 	struct ocfs2_extent_list *el = NULL;
954*4882a593Smuzhiyun 	struct ocfs2_extent_block *eb;
955*4882a593Smuzhiyun 	struct buffer_head *eb_bh = NULL;
956*4882a593Smuzhiyun 	u64 last_eb_blk = 0;
957*4882a593Smuzhiyun 
958*4882a593Smuzhiyun 	el = et->et_root_el;
959*4882a593Smuzhiyun 	last_eb_blk = ocfs2_et_get_last_eb_blk(et);
960*4882a593Smuzhiyun 
961*4882a593Smuzhiyun 	if (last_eb_blk) {
962*4882a593Smuzhiyun 		retval = ocfs2_read_extent_block(et->et_ci, last_eb_blk,
963*4882a593Smuzhiyun 						 &eb_bh);
964*4882a593Smuzhiyun 		if (retval < 0) {
965*4882a593Smuzhiyun 			mlog_errno(retval);
966*4882a593Smuzhiyun 			goto bail;
967*4882a593Smuzhiyun 		}
968*4882a593Smuzhiyun 		eb = (struct ocfs2_extent_block *) eb_bh->b_data;
969*4882a593Smuzhiyun 		el = &eb->h_list;
970*4882a593Smuzhiyun 	}
971*4882a593Smuzhiyun 
972*4882a593Smuzhiyun 	BUG_ON(el->l_tree_depth != 0);
973*4882a593Smuzhiyun 
974*4882a593Smuzhiyun 	retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
975*4882a593Smuzhiyun bail:
976*4882a593Smuzhiyun 	brelse(eb_bh);
977*4882a593Smuzhiyun 
978*4882a593Smuzhiyun 	trace_ocfs2_num_free_extents(retval);
979*4882a593Smuzhiyun 	return retval;
980*4882a593Smuzhiyun }
981*4882a593Smuzhiyun 
982*4882a593Smuzhiyun /* expects array to already be allocated
983*4882a593Smuzhiyun  *
984*4882a593Smuzhiyun  * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
985*4882a593Smuzhiyun  * l_count for you
986*4882a593Smuzhiyun  */
ocfs2_create_new_meta_bhs(handle_t * handle,struct ocfs2_extent_tree * et,int wanted,struct ocfs2_alloc_context * meta_ac,struct buffer_head * bhs[])987*4882a593Smuzhiyun static int ocfs2_create_new_meta_bhs(handle_t *handle,
988*4882a593Smuzhiyun 				     struct ocfs2_extent_tree *et,
989*4882a593Smuzhiyun 				     int wanted,
990*4882a593Smuzhiyun 				     struct ocfs2_alloc_context *meta_ac,
991*4882a593Smuzhiyun 				     struct buffer_head *bhs[])
992*4882a593Smuzhiyun {
993*4882a593Smuzhiyun 	int count, status, i;
994*4882a593Smuzhiyun 	u16 suballoc_bit_start;
995*4882a593Smuzhiyun 	u32 num_got;
996*4882a593Smuzhiyun 	u64 suballoc_loc, first_blkno;
997*4882a593Smuzhiyun 	struct ocfs2_super *osb =
998*4882a593Smuzhiyun 		OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
999*4882a593Smuzhiyun 	struct ocfs2_extent_block *eb;
1000*4882a593Smuzhiyun 
1001*4882a593Smuzhiyun 	count = 0;
1002*4882a593Smuzhiyun 	while (count < wanted) {
1003*4882a593Smuzhiyun 		status = ocfs2_claim_metadata(handle,
1004*4882a593Smuzhiyun 					      meta_ac,
1005*4882a593Smuzhiyun 					      wanted - count,
1006*4882a593Smuzhiyun 					      &suballoc_loc,
1007*4882a593Smuzhiyun 					      &suballoc_bit_start,
1008*4882a593Smuzhiyun 					      &num_got,
1009*4882a593Smuzhiyun 					      &first_blkno);
1010*4882a593Smuzhiyun 		if (status < 0) {
1011*4882a593Smuzhiyun 			mlog_errno(status);
1012*4882a593Smuzhiyun 			goto bail;
1013*4882a593Smuzhiyun 		}
1014*4882a593Smuzhiyun 
1015*4882a593Smuzhiyun 		for(i = count;  i < (num_got + count); i++) {
1016*4882a593Smuzhiyun 			bhs[i] = sb_getblk(osb->sb, first_blkno);
1017*4882a593Smuzhiyun 			if (bhs[i] == NULL) {
1018*4882a593Smuzhiyun 				status = -ENOMEM;
1019*4882a593Smuzhiyun 				mlog_errno(status);
1020*4882a593Smuzhiyun 				goto bail;
1021*4882a593Smuzhiyun 			}
1022*4882a593Smuzhiyun 			ocfs2_set_new_buffer_uptodate(et->et_ci, bhs[i]);
1023*4882a593Smuzhiyun 
1024*4882a593Smuzhiyun 			status = ocfs2_journal_access_eb(handle, et->et_ci,
1025*4882a593Smuzhiyun 							 bhs[i],
1026*4882a593Smuzhiyun 							 OCFS2_JOURNAL_ACCESS_CREATE);
1027*4882a593Smuzhiyun 			if (status < 0) {
1028*4882a593Smuzhiyun 				mlog_errno(status);
1029*4882a593Smuzhiyun 				goto bail;
1030*4882a593Smuzhiyun 			}
1031*4882a593Smuzhiyun 
1032*4882a593Smuzhiyun 			memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
1033*4882a593Smuzhiyun 			eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
1034*4882a593Smuzhiyun 			/* Ok, setup the minimal stuff here. */
1035*4882a593Smuzhiyun 			strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
1036*4882a593Smuzhiyun 			eb->h_blkno = cpu_to_le64(first_blkno);
1037*4882a593Smuzhiyun 			eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
1038*4882a593Smuzhiyun 			eb->h_suballoc_slot =
1039*4882a593Smuzhiyun 				cpu_to_le16(meta_ac->ac_alloc_slot);
1040*4882a593Smuzhiyun 			eb->h_suballoc_loc = cpu_to_le64(suballoc_loc);
1041*4882a593Smuzhiyun 			eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1042*4882a593Smuzhiyun 			eb->h_list.l_count =
1043*4882a593Smuzhiyun 				cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
1044*4882a593Smuzhiyun 
1045*4882a593Smuzhiyun 			suballoc_bit_start++;
1046*4882a593Smuzhiyun 			first_blkno++;
1047*4882a593Smuzhiyun 
1048*4882a593Smuzhiyun 			/* We'll also be dirtied by the caller, so
1049*4882a593Smuzhiyun 			 * this isn't absolutely necessary. */
1050*4882a593Smuzhiyun 			ocfs2_journal_dirty(handle, bhs[i]);
1051*4882a593Smuzhiyun 		}
1052*4882a593Smuzhiyun 
1053*4882a593Smuzhiyun 		count += num_got;
1054*4882a593Smuzhiyun 	}
1055*4882a593Smuzhiyun 
1056*4882a593Smuzhiyun 	status = 0;
1057*4882a593Smuzhiyun bail:
1058*4882a593Smuzhiyun 	if (status < 0) {
1059*4882a593Smuzhiyun 		for(i = 0; i < wanted; i++) {
1060*4882a593Smuzhiyun 			brelse(bhs[i]);
1061*4882a593Smuzhiyun 			bhs[i] = NULL;
1062*4882a593Smuzhiyun 		}
1063*4882a593Smuzhiyun 	}
1064*4882a593Smuzhiyun 	return status;
1065*4882a593Smuzhiyun }
1066*4882a593Smuzhiyun 
1067*4882a593Smuzhiyun /*
1068*4882a593Smuzhiyun  * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
1069*4882a593Smuzhiyun  *
1070*4882a593Smuzhiyun  * Returns the sum of the rightmost extent rec logical offset and
1071*4882a593Smuzhiyun  * cluster count.
1072*4882a593Smuzhiyun  *
1073*4882a593Smuzhiyun  * ocfs2_add_branch() uses this to determine what logical cluster
1074*4882a593Smuzhiyun  * value should be populated into the leftmost new branch records.
1075*4882a593Smuzhiyun  *
1076*4882a593Smuzhiyun  * ocfs2_shift_tree_depth() uses this to determine the # clusters
1077*4882a593Smuzhiyun  * value for the new topmost tree record.
1078*4882a593Smuzhiyun  */
ocfs2_sum_rightmost_rec(struct ocfs2_extent_list * el)1079*4882a593Smuzhiyun static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list  *el)
1080*4882a593Smuzhiyun {
1081*4882a593Smuzhiyun 	int i;
1082*4882a593Smuzhiyun 
1083*4882a593Smuzhiyun 	i = le16_to_cpu(el->l_next_free_rec) - 1;
1084*4882a593Smuzhiyun 
1085*4882a593Smuzhiyun 	return le32_to_cpu(el->l_recs[i].e_cpos) +
1086*4882a593Smuzhiyun 		ocfs2_rec_clusters(el, &el->l_recs[i]);
1087*4882a593Smuzhiyun }
1088*4882a593Smuzhiyun 
1089*4882a593Smuzhiyun /*
1090*4882a593Smuzhiyun  * Change range of the branches in the right most path according to the leaf
1091*4882a593Smuzhiyun  * extent block's rightmost record.
1092*4882a593Smuzhiyun  */
ocfs2_adjust_rightmost_branch(handle_t * handle,struct ocfs2_extent_tree * et)1093*4882a593Smuzhiyun static int ocfs2_adjust_rightmost_branch(handle_t *handle,
1094*4882a593Smuzhiyun 					 struct ocfs2_extent_tree *et)
1095*4882a593Smuzhiyun {
1096*4882a593Smuzhiyun 	int status;
1097*4882a593Smuzhiyun 	struct ocfs2_path *path = NULL;
1098*4882a593Smuzhiyun 	struct ocfs2_extent_list *el;
1099*4882a593Smuzhiyun 	struct ocfs2_extent_rec *rec;
1100*4882a593Smuzhiyun 
1101*4882a593Smuzhiyun 	path = ocfs2_new_path_from_et(et);
1102*4882a593Smuzhiyun 	if (!path) {
1103*4882a593Smuzhiyun 		status = -ENOMEM;
1104*4882a593Smuzhiyun 		return status;
1105*4882a593Smuzhiyun 	}
1106*4882a593Smuzhiyun 
1107*4882a593Smuzhiyun 	status = ocfs2_find_path(et->et_ci, path, UINT_MAX);
1108*4882a593Smuzhiyun 	if (status < 0) {
1109*4882a593Smuzhiyun 		mlog_errno(status);
1110*4882a593Smuzhiyun 		goto out;
1111*4882a593Smuzhiyun 	}
1112*4882a593Smuzhiyun 
1113*4882a593Smuzhiyun 	status = ocfs2_extend_trans(handle, path_num_items(path));
1114*4882a593Smuzhiyun 	if (status < 0) {
1115*4882a593Smuzhiyun 		mlog_errno(status);
1116*4882a593Smuzhiyun 		goto out;
1117*4882a593Smuzhiyun 	}
1118*4882a593Smuzhiyun 
1119*4882a593Smuzhiyun 	status = ocfs2_journal_access_path(et->et_ci, handle, path);
1120*4882a593Smuzhiyun 	if (status < 0) {
1121*4882a593Smuzhiyun 		mlog_errno(status);
1122*4882a593Smuzhiyun 		goto out;
1123*4882a593Smuzhiyun 	}
1124*4882a593Smuzhiyun 
1125*4882a593Smuzhiyun 	el = path_leaf_el(path);
1126*4882a593Smuzhiyun 	rec = &el->l_recs[le16_to_cpu(el->l_next_free_rec) - 1];
1127*4882a593Smuzhiyun 
1128*4882a593Smuzhiyun 	ocfs2_adjust_rightmost_records(handle, et, path, rec);
1129*4882a593Smuzhiyun 
1130*4882a593Smuzhiyun out:
1131*4882a593Smuzhiyun 	ocfs2_free_path(path);
1132*4882a593Smuzhiyun 	return status;
1133*4882a593Smuzhiyun }
1134*4882a593Smuzhiyun 
1135*4882a593Smuzhiyun /*
1136*4882a593Smuzhiyun  * Add an entire tree branch to our inode. eb_bh is the extent block
1137*4882a593Smuzhiyun  * to start at, if we don't want to start the branch at the root
1138*4882a593Smuzhiyun  * structure.
1139*4882a593Smuzhiyun  *
1140*4882a593Smuzhiyun  * last_eb_bh is required as we have to update it's next_leaf pointer
1141*4882a593Smuzhiyun  * for the new last extent block.
1142*4882a593Smuzhiyun  *
1143*4882a593Smuzhiyun  * the new branch will be 'empty' in the sense that every block will
1144*4882a593Smuzhiyun  * contain a single record with cluster count == 0.
1145*4882a593Smuzhiyun  */
ocfs2_add_branch(handle_t * handle,struct ocfs2_extent_tree * et,struct buffer_head * eb_bh,struct buffer_head ** last_eb_bh,struct ocfs2_alloc_context * meta_ac)1146*4882a593Smuzhiyun static int ocfs2_add_branch(handle_t *handle,
1147*4882a593Smuzhiyun 			    struct ocfs2_extent_tree *et,
1148*4882a593Smuzhiyun 			    struct buffer_head *eb_bh,
1149*4882a593Smuzhiyun 			    struct buffer_head **last_eb_bh,
1150*4882a593Smuzhiyun 			    struct ocfs2_alloc_context *meta_ac)
1151*4882a593Smuzhiyun {
1152*4882a593Smuzhiyun 	int status, new_blocks, i, block_given = 0;
1153*4882a593Smuzhiyun 	u64 next_blkno, new_last_eb_blk;
1154*4882a593Smuzhiyun 	struct buffer_head *bh;
1155*4882a593Smuzhiyun 	struct buffer_head **new_eb_bhs = NULL;
1156*4882a593Smuzhiyun 	struct ocfs2_extent_block *eb;
1157*4882a593Smuzhiyun 	struct ocfs2_extent_list  *eb_el;
1158*4882a593Smuzhiyun 	struct ocfs2_extent_list  *el;
1159*4882a593Smuzhiyun 	u32 new_cpos, root_end;
1160*4882a593Smuzhiyun 
1161*4882a593Smuzhiyun 	BUG_ON(!last_eb_bh || !*last_eb_bh);
1162*4882a593Smuzhiyun 
1163*4882a593Smuzhiyun 	if (eb_bh) {
1164*4882a593Smuzhiyun 		eb = (struct ocfs2_extent_block *) eb_bh->b_data;
1165*4882a593Smuzhiyun 		el = &eb->h_list;
1166*4882a593Smuzhiyun 	} else
1167*4882a593Smuzhiyun 		el = et->et_root_el;
1168*4882a593Smuzhiyun 
1169*4882a593Smuzhiyun 	/* we never add a branch to a leaf. */
1170*4882a593Smuzhiyun 	BUG_ON(!el->l_tree_depth);
1171*4882a593Smuzhiyun 
1172*4882a593Smuzhiyun 	new_blocks = le16_to_cpu(el->l_tree_depth);
1173*4882a593Smuzhiyun 
1174*4882a593Smuzhiyun 	eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
1175*4882a593Smuzhiyun 	new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
1176*4882a593Smuzhiyun 	root_end = ocfs2_sum_rightmost_rec(et->et_root_el);
1177*4882a593Smuzhiyun 
1178*4882a593Smuzhiyun 	/*
1179*4882a593Smuzhiyun 	 * If there is a gap before the root end and the real end
1180*4882a593Smuzhiyun 	 * of the righmost leaf block, we need to remove the gap
1181*4882a593Smuzhiyun 	 * between new_cpos and root_end first so that the tree
1182*4882a593Smuzhiyun 	 * is consistent after we add a new branch(it will start
1183*4882a593Smuzhiyun 	 * from new_cpos).
1184*4882a593Smuzhiyun 	 */
1185*4882a593Smuzhiyun 	if (root_end > new_cpos) {
1186*4882a593Smuzhiyun 		trace_ocfs2_adjust_rightmost_branch(
1187*4882a593Smuzhiyun 			(unsigned long long)
1188*4882a593Smuzhiyun 			ocfs2_metadata_cache_owner(et->et_ci),
1189*4882a593Smuzhiyun 			root_end, new_cpos);
1190*4882a593Smuzhiyun 
1191*4882a593Smuzhiyun 		status = ocfs2_adjust_rightmost_branch(handle, et);
1192*4882a593Smuzhiyun 		if (status) {
1193*4882a593Smuzhiyun 			mlog_errno(status);
1194*4882a593Smuzhiyun 			goto bail;
1195*4882a593Smuzhiyun 		}
1196*4882a593Smuzhiyun 	}
1197*4882a593Smuzhiyun 
1198*4882a593Smuzhiyun 	/* allocate the number of new eb blocks we need */
1199*4882a593Smuzhiyun 	new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
1200*4882a593Smuzhiyun 			     GFP_KERNEL);
1201*4882a593Smuzhiyun 	if (!new_eb_bhs) {
1202*4882a593Smuzhiyun 		status = -ENOMEM;
1203*4882a593Smuzhiyun 		mlog_errno(status);
1204*4882a593Smuzhiyun 		goto bail;
1205*4882a593Smuzhiyun 	}
1206*4882a593Smuzhiyun 
1207*4882a593Smuzhiyun 	/* Firstyly, try to reuse dealloc since we have already estimated how
1208*4882a593Smuzhiyun 	 * many extent blocks we may use.
1209*4882a593Smuzhiyun 	 */
1210*4882a593Smuzhiyun 	if (!ocfs2_is_dealloc_empty(et)) {
1211*4882a593Smuzhiyun 		status = ocfs2_reuse_blk_from_dealloc(handle, et,
1212*4882a593Smuzhiyun 						      new_eb_bhs, new_blocks,
1213*4882a593Smuzhiyun 						      &block_given);
1214*4882a593Smuzhiyun 		if (status < 0) {
1215*4882a593Smuzhiyun 			mlog_errno(status);
1216*4882a593Smuzhiyun 			goto bail;
1217*4882a593Smuzhiyun 		}
1218*4882a593Smuzhiyun 	}
1219*4882a593Smuzhiyun 
1220*4882a593Smuzhiyun 	BUG_ON(block_given > new_blocks);
1221*4882a593Smuzhiyun 
1222*4882a593Smuzhiyun 	if (block_given < new_blocks) {
1223*4882a593Smuzhiyun 		BUG_ON(!meta_ac);
1224*4882a593Smuzhiyun 		status = ocfs2_create_new_meta_bhs(handle, et,
1225*4882a593Smuzhiyun 						   new_blocks - block_given,
1226*4882a593Smuzhiyun 						   meta_ac,
1227*4882a593Smuzhiyun 						   &new_eb_bhs[block_given]);
1228*4882a593Smuzhiyun 		if (status < 0) {
1229*4882a593Smuzhiyun 			mlog_errno(status);
1230*4882a593Smuzhiyun 			goto bail;
1231*4882a593Smuzhiyun 		}
1232*4882a593Smuzhiyun 	}
1233*4882a593Smuzhiyun 
1234*4882a593Smuzhiyun 	/* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
1235*4882a593Smuzhiyun 	 * linked with the rest of the tree.
1236*4882a593Smuzhiyun 	 * conversly, new_eb_bhs[0] is the new bottommost leaf.
1237*4882a593Smuzhiyun 	 *
1238*4882a593Smuzhiyun 	 * when we leave the loop, new_last_eb_blk will point to the
1239*4882a593Smuzhiyun 	 * newest leaf, and next_blkno will point to the topmost extent
1240*4882a593Smuzhiyun 	 * block. */
1241*4882a593Smuzhiyun 	next_blkno = new_last_eb_blk = 0;
1242*4882a593Smuzhiyun 	for(i = 0; i < new_blocks; i++) {
1243*4882a593Smuzhiyun 		bh = new_eb_bhs[i];
1244*4882a593Smuzhiyun 		eb = (struct ocfs2_extent_block *) bh->b_data;
1245*4882a593Smuzhiyun 		/* ocfs2_create_new_meta_bhs() should create it right! */
1246*4882a593Smuzhiyun 		BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
1247*4882a593Smuzhiyun 		eb_el = &eb->h_list;
1248*4882a593Smuzhiyun 
1249*4882a593Smuzhiyun 		status = ocfs2_journal_access_eb(handle, et->et_ci, bh,
1250*4882a593Smuzhiyun 						 OCFS2_JOURNAL_ACCESS_CREATE);
1251*4882a593Smuzhiyun 		if (status < 0) {
1252*4882a593Smuzhiyun 			mlog_errno(status);
1253*4882a593Smuzhiyun 			goto bail;
1254*4882a593Smuzhiyun 		}
1255*4882a593Smuzhiyun 
1256*4882a593Smuzhiyun 		eb->h_next_leaf_blk = 0;
1257*4882a593Smuzhiyun 		eb_el->l_tree_depth = cpu_to_le16(i);
1258*4882a593Smuzhiyun 		eb_el->l_next_free_rec = cpu_to_le16(1);
1259*4882a593Smuzhiyun 		/*
1260*4882a593Smuzhiyun 		 * This actually counts as an empty extent as
1261*4882a593Smuzhiyun 		 * c_clusters == 0
1262*4882a593Smuzhiyun 		 */
1263*4882a593Smuzhiyun 		eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
1264*4882a593Smuzhiyun 		eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
1265*4882a593Smuzhiyun 		/*
1266*4882a593Smuzhiyun 		 * eb_el isn't always an interior node, but even leaf
1267*4882a593Smuzhiyun 		 * nodes want a zero'd flags and reserved field so
1268*4882a593Smuzhiyun 		 * this gets the whole 32 bits regardless of use.
1269*4882a593Smuzhiyun 		 */
1270*4882a593Smuzhiyun 		eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
1271*4882a593Smuzhiyun 		if (!eb_el->l_tree_depth)
1272*4882a593Smuzhiyun 			new_last_eb_blk = le64_to_cpu(eb->h_blkno);
1273*4882a593Smuzhiyun 
1274*4882a593Smuzhiyun 		ocfs2_journal_dirty(handle, bh);
1275*4882a593Smuzhiyun 		next_blkno = le64_to_cpu(eb->h_blkno);
1276*4882a593Smuzhiyun 	}
1277*4882a593Smuzhiyun 
1278*4882a593Smuzhiyun 	/* This is a bit hairy. We want to update up to three blocks
1279*4882a593Smuzhiyun 	 * here without leaving any of them in an inconsistent state
1280*4882a593Smuzhiyun 	 * in case of error. We don't have to worry about
1281*4882a593Smuzhiyun 	 * journal_dirty erroring as it won't unless we've aborted the
1282*4882a593Smuzhiyun 	 * handle (in which case we would never be here) so reserving
1283*4882a593Smuzhiyun 	 * the write with journal_access is all we need to do. */
1284*4882a593Smuzhiyun 	status = ocfs2_journal_access_eb(handle, et->et_ci, *last_eb_bh,
1285*4882a593Smuzhiyun 					 OCFS2_JOURNAL_ACCESS_WRITE);
1286*4882a593Smuzhiyun 	if (status < 0) {
1287*4882a593Smuzhiyun 		mlog_errno(status);
1288*4882a593Smuzhiyun 		goto bail;
1289*4882a593Smuzhiyun 	}
1290*4882a593Smuzhiyun 	status = ocfs2_et_root_journal_access(handle, et,
1291*4882a593Smuzhiyun 					      OCFS2_JOURNAL_ACCESS_WRITE);
1292*4882a593Smuzhiyun 	if (status < 0) {
1293*4882a593Smuzhiyun 		mlog_errno(status);
1294*4882a593Smuzhiyun 		goto bail;
1295*4882a593Smuzhiyun 	}
1296*4882a593Smuzhiyun 	if (eb_bh) {
1297*4882a593Smuzhiyun 		status = ocfs2_journal_access_eb(handle, et->et_ci, eb_bh,
1298*4882a593Smuzhiyun 						 OCFS2_JOURNAL_ACCESS_WRITE);
1299*4882a593Smuzhiyun 		if (status < 0) {
1300*4882a593Smuzhiyun 			mlog_errno(status);
1301*4882a593Smuzhiyun 			goto bail;
1302*4882a593Smuzhiyun 		}
1303*4882a593Smuzhiyun 	}
1304*4882a593Smuzhiyun 
1305*4882a593Smuzhiyun 	/* Link the new branch into the rest of the tree (el will
1306*4882a593Smuzhiyun 	 * either be on the root_bh, or the extent block passed in. */
1307*4882a593Smuzhiyun 	i = le16_to_cpu(el->l_next_free_rec);
1308*4882a593Smuzhiyun 	el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
1309*4882a593Smuzhiyun 	el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
1310*4882a593Smuzhiyun 	el->l_recs[i].e_int_clusters = 0;
1311*4882a593Smuzhiyun 	le16_add_cpu(&el->l_next_free_rec, 1);
1312*4882a593Smuzhiyun 
1313*4882a593Smuzhiyun 	/* fe needs a new last extent block pointer, as does the
1314*4882a593Smuzhiyun 	 * next_leaf on the previously last-extent-block. */
1315*4882a593Smuzhiyun 	ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
1316*4882a593Smuzhiyun 
1317*4882a593Smuzhiyun 	eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
1318*4882a593Smuzhiyun 	eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
1319*4882a593Smuzhiyun 
1320*4882a593Smuzhiyun 	ocfs2_journal_dirty(handle, *last_eb_bh);
1321*4882a593Smuzhiyun 	ocfs2_journal_dirty(handle, et->et_root_bh);
1322*4882a593Smuzhiyun 	if (eb_bh)
1323*4882a593Smuzhiyun 		ocfs2_journal_dirty(handle, eb_bh);
1324*4882a593Smuzhiyun 
1325*4882a593Smuzhiyun 	/*
1326*4882a593Smuzhiyun 	 * Some callers want to track the rightmost leaf so pass it
1327*4882a593Smuzhiyun 	 * back here.
1328*4882a593Smuzhiyun 	 */
1329*4882a593Smuzhiyun 	brelse(*last_eb_bh);
1330*4882a593Smuzhiyun 	get_bh(new_eb_bhs[0]);
1331*4882a593Smuzhiyun 	*last_eb_bh = new_eb_bhs[0];
1332*4882a593Smuzhiyun 
1333*4882a593Smuzhiyun 	status = 0;
1334*4882a593Smuzhiyun bail:
1335*4882a593Smuzhiyun 	if (new_eb_bhs) {
1336*4882a593Smuzhiyun 		for (i = 0; i < new_blocks; i++)
1337*4882a593Smuzhiyun 			brelse(new_eb_bhs[i]);
1338*4882a593Smuzhiyun 		kfree(new_eb_bhs);
1339*4882a593Smuzhiyun 	}
1340*4882a593Smuzhiyun 
1341*4882a593Smuzhiyun 	return status;
1342*4882a593Smuzhiyun }
1343*4882a593Smuzhiyun 
1344*4882a593Smuzhiyun /*
1345*4882a593Smuzhiyun  * adds another level to the allocation tree.
1346*4882a593Smuzhiyun  * returns back the new extent block so you can add a branch to it
1347*4882a593Smuzhiyun  * after this call.
1348*4882a593Smuzhiyun  */
ocfs2_shift_tree_depth(handle_t * handle,struct ocfs2_extent_tree * et,struct ocfs2_alloc_context * meta_ac,struct buffer_head ** ret_new_eb_bh)1349*4882a593Smuzhiyun static int ocfs2_shift_tree_depth(handle_t *handle,
1350*4882a593Smuzhiyun 				  struct ocfs2_extent_tree *et,
1351*4882a593Smuzhiyun 				  struct ocfs2_alloc_context *meta_ac,
1352*4882a593Smuzhiyun 				  struct buffer_head **ret_new_eb_bh)
1353*4882a593Smuzhiyun {
1354*4882a593Smuzhiyun 	int status, i, block_given = 0;
1355*4882a593Smuzhiyun 	u32 new_clusters;
1356*4882a593Smuzhiyun 	struct buffer_head *new_eb_bh = NULL;
1357*4882a593Smuzhiyun 	struct ocfs2_extent_block *eb;
1358*4882a593Smuzhiyun 	struct ocfs2_extent_list  *root_el;
1359*4882a593Smuzhiyun 	struct ocfs2_extent_list  *eb_el;
1360*4882a593Smuzhiyun 
1361*4882a593Smuzhiyun 	if (!ocfs2_is_dealloc_empty(et)) {
1362*4882a593Smuzhiyun 		status = ocfs2_reuse_blk_from_dealloc(handle, et,
1363*4882a593Smuzhiyun 						      &new_eb_bh, 1,
1364*4882a593Smuzhiyun 						      &block_given);
1365*4882a593Smuzhiyun 	} else if (meta_ac) {
1366*4882a593Smuzhiyun 		status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac,
1367*4882a593Smuzhiyun 						   &new_eb_bh);
1368*4882a593Smuzhiyun 
1369*4882a593Smuzhiyun 	} else {
1370*4882a593Smuzhiyun 		BUG();
1371*4882a593Smuzhiyun 	}
1372*4882a593Smuzhiyun 
1373*4882a593Smuzhiyun 	if (status < 0) {
1374*4882a593Smuzhiyun 		mlog_errno(status);
1375*4882a593Smuzhiyun 		goto bail;
1376*4882a593Smuzhiyun 	}
1377*4882a593Smuzhiyun 
1378*4882a593Smuzhiyun 	eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
1379*4882a593Smuzhiyun 	/* ocfs2_create_new_meta_bhs() should create it right! */
1380*4882a593Smuzhiyun 	BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
1381*4882a593Smuzhiyun 
1382*4882a593Smuzhiyun 	eb_el = &eb->h_list;
1383*4882a593Smuzhiyun 	root_el = et->et_root_el;
1384*4882a593Smuzhiyun 
1385*4882a593Smuzhiyun 	status = ocfs2_journal_access_eb(handle, et->et_ci, new_eb_bh,
1386*4882a593Smuzhiyun 					 OCFS2_JOURNAL_ACCESS_CREATE);
1387*4882a593Smuzhiyun 	if (status < 0) {
1388*4882a593Smuzhiyun 		mlog_errno(status);
1389*4882a593Smuzhiyun 		goto bail;
1390*4882a593Smuzhiyun 	}
1391*4882a593Smuzhiyun 
1392*4882a593Smuzhiyun 	/* copy the root extent list data into the new extent block */
1393*4882a593Smuzhiyun 	eb_el->l_tree_depth = root_el->l_tree_depth;
1394*4882a593Smuzhiyun 	eb_el->l_next_free_rec = root_el->l_next_free_rec;
1395*4882a593Smuzhiyun 	for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1396*4882a593Smuzhiyun 		eb_el->l_recs[i] = root_el->l_recs[i];
1397*4882a593Smuzhiyun 
1398*4882a593Smuzhiyun 	ocfs2_journal_dirty(handle, new_eb_bh);
1399*4882a593Smuzhiyun 
1400*4882a593Smuzhiyun 	status = ocfs2_et_root_journal_access(handle, et,
1401*4882a593Smuzhiyun 					      OCFS2_JOURNAL_ACCESS_WRITE);
1402*4882a593Smuzhiyun 	if (status < 0) {
1403*4882a593Smuzhiyun 		mlog_errno(status);
1404*4882a593Smuzhiyun 		goto bail;
1405*4882a593Smuzhiyun 	}
1406*4882a593Smuzhiyun 
1407*4882a593Smuzhiyun 	new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1408*4882a593Smuzhiyun 
1409*4882a593Smuzhiyun 	/* update root_bh now */
1410*4882a593Smuzhiyun 	le16_add_cpu(&root_el->l_tree_depth, 1);
1411*4882a593Smuzhiyun 	root_el->l_recs[0].e_cpos = 0;
1412*4882a593Smuzhiyun 	root_el->l_recs[0].e_blkno = eb->h_blkno;
1413*4882a593Smuzhiyun 	root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1414*4882a593Smuzhiyun 	for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1415*4882a593Smuzhiyun 		memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1416*4882a593Smuzhiyun 	root_el->l_next_free_rec = cpu_to_le16(1);
1417*4882a593Smuzhiyun 
1418*4882a593Smuzhiyun 	/* If this is our 1st tree depth shift, then last_eb_blk
1419*4882a593Smuzhiyun 	 * becomes the allocated extent block */
1420*4882a593Smuzhiyun 	if (root_el->l_tree_depth == cpu_to_le16(1))
1421*4882a593Smuzhiyun 		ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
1422*4882a593Smuzhiyun 
1423*4882a593Smuzhiyun 	ocfs2_journal_dirty(handle, et->et_root_bh);
1424*4882a593Smuzhiyun 
1425*4882a593Smuzhiyun 	*ret_new_eb_bh = new_eb_bh;
1426*4882a593Smuzhiyun 	new_eb_bh = NULL;
1427*4882a593Smuzhiyun 	status = 0;
1428*4882a593Smuzhiyun bail:
1429*4882a593Smuzhiyun 	brelse(new_eb_bh);
1430*4882a593Smuzhiyun 
1431*4882a593Smuzhiyun 	return status;
1432*4882a593Smuzhiyun }
1433*4882a593Smuzhiyun 
1434*4882a593Smuzhiyun /*
1435*4882a593Smuzhiyun  * Should only be called when there is no space left in any of the
1436*4882a593Smuzhiyun  * leaf nodes. What we want to do is find the lowest tree depth
1437*4882a593Smuzhiyun  * non-leaf extent block with room for new records. There are three
1438*4882a593Smuzhiyun  * valid results of this search:
1439*4882a593Smuzhiyun  *
1440*4882a593Smuzhiyun  * 1) a lowest extent block is found, then we pass it back in
1441*4882a593Smuzhiyun  *    *lowest_eb_bh and return '0'
1442*4882a593Smuzhiyun  *
1443*4882a593Smuzhiyun  * 2) the search fails to find anything, but the root_el has room. We
1444*4882a593Smuzhiyun  *    pass NULL back in *lowest_eb_bh, but still return '0'
1445*4882a593Smuzhiyun  *
1446*4882a593Smuzhiyun  * 3) the search fails to find anything AND the root_el is full, in
1447*4882a593Smuzhiyun  *    which case we return > 0
1448*4882a593Smuzhiyun  *
1449*4882a593Smuzhiyun  * return status < 0 indicates an error.
1450*4882a593Smuzhiyun  */
ocfs2_find_branch_target(struct ocfs2_extent_tree * et,struct buffer_head ** target_bh)1451*4882a593Smuzhiyun static int ocfs2_find_branch_target(struct ocfs2_extent_tree *et,
1452*4882a593Smuzhiyun 				    struct buffer_head **target_bh)
1453*4882a593Smuzhiyun {
1454*4882a593Smuzhiyun 	int status = 0, i;
1455*4882a593Smuzhiyun 	u64 blkno;
1456*4882a593Smuzhiyun 	struct ocfs2_extent_block *eb;
1457*4882a593Smuzhiyun 	struct ocfs2_extent_list  *el;
1458*4882a593Smuzhiyun 	struct buffer_head *bh = NULL;
1459*4882a593Smuzhiyun 	struct buffer_head *lowest_bh = NULL;
1460*4882a593Smuzhiyun 
1461*4882a593Smuzhiyun 	*target_bh = NULL;
1462*4882a593Smuzhiyun 
1463*4882a593Smuzhiyun 	el = et->et_root_el;
1464*4882a593Smuzhiyun 
1465*4882a593Smuzhiyun 	while(le16_to_cpu(el->l_tree_depth) > 1) {
1466*4882a593Smuzhiyun 		if (le16_to_cpu(el->l_next_free_rec) == 0) {
1467*4882a593Smuzhiyun 			status = ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1468*4882a593Smuzhiyun 					"Owner %llu has empty extent list (next_free_rec == 0)\n",
1469*4882a593Smuzhiyun 					(unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
1470*4882a593Smuzhiyun 			goto bail;
1471*4882a593Smuzhiyun 		}
1472*4882a593Smuzhiyun 		i = le16_to_cpu(el->l_next_free_rec) - 1;
1473*4882a593Smuzhiyun 		blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1474*4882a593Smuzhiyun 		if (!blkno) {
1475*4882a593Smuzhiyun 			status = ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1476*4882a593Smuzhiyun 					"Owner %llu has extent list where extent # %d has no physical block start\n",
1477*4882a593Smuzhiyun 					(unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i);
1478*4882a593Smuzhiyun 			goto bail;
1479*4882a593Smuzhiyun 		}
1480*4882a593Smuzhiyun 
1481*4882a593Smuzhiyun 		brelse(bh);
1482*4882a593Smuzhiyun 		bh = NULL;
1483*4882a593Smuzhiyun 
1484*4882a593Smuzhiyun 		status = ocfs2_read_extent_block(et->et_ci, blkno, &bh);
1485*4882a593Smuzhiyun 		if (status < 0) {
1486*4882a593Smuzhiyun 			mlog_errno(status);
1487*4882a593Smuzhiyun 			goto bail;
1488*4882a593Smuzhiyun 		}
1489*4882a593Smuzhiyun 
1490*4882a593Smuzhiyun 		eb = (struct ocfs2_extent_block *) bh->b_data;
1491*4882a593Smuzhiyun 		el = &eb->h_list;
1492*4882a593Smuzhiyun 
1493*4882a593Smuzhiyun 		if (le16_to_cpu(el->l_next_free_rec) <
1494*4882a593Smuzhiyun 		    le16_to_cpu(el->l_count)) {
1495*4882a593Smuzhiyun 			brelse(lowest_bh);
1496*4882a593Smuzhiyun 			lowest_bh = bh;
1497*4882a593Smuzhiyun 			get_bh(lowest_bh);
1498*4882a593Smuzhiyun 		}
1499*4882a593Smuzhiyun 	}
1500*4882a593Smuzhiyun 
1501*4882a593Smuzhiyun 	/* If we didn't find one and the fe doesn't have any room,
1502*4882a593Smuzhiyun 	 * then return '1' */
1503*4882a593Smuzhiyun 	el = et->et_root_el;
1504*4882a593Smuzhiyun 	if (!lowest_bh && (el->l_next_free_rec == el->l_count))
1505*4882a593Smuzhiyun 		status = 1;
1506*4882a593Smuzhiyun 
1507*4882a593Smuzhiyun 	*target_bh = lowest_bh;
1508*4882a593Smuzhiyun bail:
1509*4882a593Smuzhiyun 	brelse(bh);
1510*4882a593Smuzhiyun 
1511*4882a593Smuzhiyun 	return status;
1512*4882a593Smuzhiyun }
1513*4882a593Smuzhiyun 
1514*4882a593Smuzhiyun /*
1515*4882a593Smuzhiyun  * Grow a b-tree so that it has more records.
1516*4882a593Smuzhiyun  *
1517*4882a593Smuzhiyun  * We might shift the tree depth in which case existing paths should
1518*4882a593Smuzhiyun  * be considered invalid.
1519*4882a593Smuzhiyun  *
1520*4882a593Smuzhiyun  * Tree depth after the grow is returned via *final_depth.
1521*4882a593Smuzhiyun  *
1522*4882a593Smuzhiyun  * *last_eb_bh will be updated by ocfs2_add_branch().
1523*4882a593Smuzhiyun  */
ocfs2_grow_tree(handle_t * handle,struct ocfs2_extent_tree * et,int * final_depth,struct buffer_head ** last_eb_bh,struct ocfs2_alloc_context * meta_ac)1524*4882a593Smuzhiyun static int ocfs2_grow_tree(handle_t *handle, struct ocfs2_extent_tree *et,
1525*4882a593Smuzhiyun 			   int *final_depth, struct buffer_head **last_eb_bh,
1526*4882a593Smuzhiyun 			   struct ocfs2_alloc_context *meta_ac)
1527*4882a593Smuzhiyun {
1528*4882a593Smuzhiyun 	int ret, shift;
1529*4882a593Smuzhiyun 	struct ocfs2_extent_list *el = et->et_root_el;
1530*4882a593Smuzhiyun 	int depth = le16_to_cpu(el->l_tree_depth);
1531*4882a593Smuzhiyun 	struct buffer_head *bh = NULL;
1532*4882a593Smuzhiyun 
1533*4882a593Smuzhiyun 	BUG_ON(meta_ac == NULL && ocfs2_is_dealloc_empty(et));
1534*4882a593Smuzhiyun 
1535*4882a593Smuzhiyun 	shift = ocfs2_find_branch_target(et, &bh);
1536*4882a593Smuzhiyun 	if (shift < 0) {
1537*4882a593Smuzhiyun 		ret = shift;
1538*4882a593Smuzhiyun 		mlog_errno(ret);
1539*4882a593Smuzhiyun 		goto out;
1540*4882a593Smuzhiyun 	}
1541*4882a593Smuzhiyun 
1542*4882a593Smuzhiyun 	/* We traveled all the way to the bottom of the allocation tree
1543*4882a593Smuzhiyun 	 * and didn't find room for any more extents - we need to add
1544*4882a593Smuzhiyun 	 * another tree level */
1545*4882a593Smuzhiyun 	if (shift) {
1546*4882a593Smuzhiyun 		BUG_ON(bh);
1547*4882a593Smuzhiyun 		trace_ocfs2_grow_tree(
1548*4882a593Smuzhiyun 			(unsigned long long)
1549*4882a593Smuzhiyun 			ocfs2_metadata_cache_owner(et->et_ci),
1550*4882a593Smuzhiyun 			depth);
1551*4882a593Smuzhiyun 
1552*4882a593Smuzhiyun 		/* ocfs2_shift_tree_depth will return us a buffer with
1553*4882a593Smuzhiyun 		 * the new extent block (so we can pass that to
1554*4882a593Smuzhiyun 		 * ocfs2_add_branch). */
1555*4882a593Smuzhiyun 		ret = ocfs2_shift_tree_depth(handle, et, meta_ac, &bh);
1556*4882a593Smuzhiyun 		if (ret < 0) {
1557*4882a593Smuzhiyun 			mlog_errno(ret);
1558*4882a593Smuzhiyun 			goto out;
1559*4882a593Smuzhiyun 		}
1560*4882a593Smuzhiyun 		depth++;
1561*4882a593Smuzhiyun 		if (depth == 1) {
1562*4882a593Smuzhiyun 			/*
1563*4882a593Smuzhiyun 			 * Special case: we have room now if we shifted from
1564*4882a593Smuzhiyun 			 * tree_depth 0, so no more work needs to be done.
1565*4882a593Smuzhiyun 			 *
1566*4882a593Smuzhiyun 			 * We won't be calling add_branch, so pass
1567*4882a593Smuzhiyun 			 * back *last_eb_bh as the new leaf. At depth
1568*4882a593Smuzhiyun 			 * zero, it should always be null so there's
1569*4882a593Smuzhiyun 			 * no reason to brelse.
1570*4882a593Smuzhiyun 			 */
1571*4882a593Smuzhiyun 			BUG_ON(*last_eb_bh);
1572*4882a593Smuzhiyun 			get_bh(bh);
1573*4882a593Smuzhiyun 			*last_eb_bh = bh;
1574*4882a593Smuzhiyun 			goto out;
1575*4882a593Smuzhiyun 		}
1576*4882a593Smuzhiyun 	}
1577*4882a593Smuzhiyun 
1578*4882a593Smuzhiyun 	/* call ocfs2_add_branch to add the final part of the tree with
1579*4882a593Smuzhiyun 	 * the new data. */
1580*4882a593Smuzhiyun 	ret = ocfs2_add_branch(handle, et, bh, last_eb_bh,
1581*4882a593Smuzhiyun 			       meta_ac);
1582*4882a593Smuzhiyun 	if (ret < 0)
1583*4882a593Smuzhiyun 		mlog_errno(ret);
1584*4882a593Smuzhiyun 
1585*4882a593Smuzhiyun out:
1586*4882a593Smuzhiyun 	if (final_depth)
1587*4882a593Smuzhiyun 		*final_depth = depth;
1588*4882a593Smuzhiyun 	brelse(bh);
1589*4882a593Smuzhiyun 	return ret;
1590*4882a593Smuzhiyun }
1591*4882a593Smuzhiyun 
1592*4882a593Smuzhiyun /*
1593*4882a593Smuzhiyun  * This function will discard the rightmost extent record.
1594*4882a593Smuzhiyun  */
ocfs2_shift_records_right(struct ocfs2_extent_list * el)1595*4882a593Smuzhiyun static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1596*4882a593Smuzhiyun {
1597*4882a593Smuzhiyun 	int next_free = le16_to_cpu(el->l_next_free_rec);
1598*4882a593Smuzhiyun 	int count = le16_to_cpu(el->l_count);
1599*4882a593Smuzhiyun 	unsigned int num_bytes;
1600*4882a593Smuzhiyun 
1601*4882a593Smuzhiyun 	BUG_ON(!next_free);
1602*4882a593Smuzhiyun 	/* This will cause us to go off the end of our extent list. */
1603*4882a593Smuzhiyun 	BUG_ON(next_free >= count);
1604*4882a593Smuzhiyun 
1605*4882a593Smuzhiyun 	num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1606*4882a593Smuzhiyun 
1607*4882a593Smuzhiyun 	memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1608*4882a593Smuzhiyun }
1609*4882a593Smuzhiyun 
ocfs2_rotate_leaf(struct ocfs2_extent_list * el,struct ocfs2_extent_rec * insert_rec)1610*4882a593Smuzhiyun static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1611*4882a593Smuzhiyun 			      struct ocfs2_extent_rec *insert_rec)
1612*4882a593Smuzhiyun {
1613*4882a593Smuzhiyun 	int i, insert_index, next_free, has_empty, num_bytes;
1614*4882a593Smuzhiyun 	u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1615*4882a593Smuzhiyun 	struct ocfs2_extent_rec *rec;
1616*4882a593Smuzhiyun 
1617*4882a593Smuzhiyun 	next_free = le16_to_cpu(el->l_next_free_rec);
1618*4882a593Smuzhiyun 	has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1619*4882a593Smuzhiyun 
1620*4882a593Smuzhiyun 	BUG_ON(!next_free);
1621*4882a593Smuzhiyun 
1622*4882a593Smuzhiyun 	/* The tree code before us didn't allow enough room in the leaf. */
1623*4882a593Smuzhiyun 	BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
1624*4882a593Smuzhiyun 
1625*4882a593Smuzhiyun 	/*
1626*4882a593Smuzhiyun 	 * The easiest way to approach this is to just remove the
1627*4882a593Smuzhiyun 	 * empty extent and temporarily decrement next_free.
1628*4882a593Smuzhiyun 	 */
1629*4882a593Smuzhiyun 	if (has_empty) {
1630*4882a593Smuzhiyun 		/*
1631*4882a593Smuzhiyun 		 * If next_free was 1 (only an empty extent), this
1632*4882a593Smuzhiyun 		 * loop won't execute, which is fine. We still want
1633*4882a593Smuzhiyun 		 * the decrement above to happen.
1634*4882a593Smuzhiyun 		 */
1635*4882a593Smuzhiyun 		for(i = 0; i < (next_free - 1); i++)
1636*4882a593Smuzhiyun 			el->l_recs[i] = el->l_recs[i+1];
1637*4882a593Smuzhiyun 
1638*4882a593Smuzhiyun 		next_free--;
1639*4882a593Smuzhiyun 	}
1640*4882a593Smuzhiyun 
1641*4882a593Smuzhiyun 	/*
1642*4882a593Smuzhiyun 	 * Figure out what the new record index should be.
1643*4882a593Smuzhiyun 	 */
1644*4882a593Smuzhiyun 	for(i = 0; i < next_free; i++) {
1645*4882a593Smuzhiyun 		rec = &el->l_recs[i];
1646*4882a593Smuzhiyun 
1647*4882a593Smuzhiyun 		if (insert_cpos < le32_to_cpu(rec->e_cpos))
1648*4882a593Smuzhiyun 			break;
1649*4882a593Smuzhiyun 	}
1650*4882a593Smuzhiyun 	insert_index = i;
1651*4882a593Smuzhiyun 
1652*4882a593Smuzhiyun 	trace_ocfs2_rotate_leaf(insert_cpos, insert_index,
1653*4882a593Smuzhiyun 				has_empty, next_free,
1654*4882a593Smuzhiyun 				le16_to_cpu(el->l_count));
1655*4882a593Smuzhiyun 
1656*4882a593Smuzhiyun 	BUG_ON(insert_index < 0);
1657*4882a593Smuzhiyun 	BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1658*4882a593Smuzhiyun 	BUG_ON(insert_index > next_free);
1659*4882a593Smuzhiyun 
1660*4882a593Smuzhiyun 	/*
1661*4882a593Smuzhiyun 	 * No need to memmove if we're just adding to the tail.
1662*4882a593Smuzhiyun 	 */
1663*4882a593Smuzhiyun 	if (insert_index != next_free) {
1664*4882a593Smuzhiyun 		BUG_ON(next_free >= le16_to_cpu(el->l_count));
1665*4882a593Smuzhiyun 
1666*4882a593Smuzhiyun 		num_bytes = next_free - insert_index;
1667*4882a593Smuzhiyun 		num_bytes *= sizeof(struct ocfs2_extent_rec);
1668*4882a593Smuzhiyun 		memmove(&el->l_recs[insert_index + 1],
1669*4882a593Smuzhiyun 			&el->l_recs[insert_index],
1670*4882a593Smuzhiyun 			num_bytes);
1671*4882a593Smuzhiyun 	}
1672*4882a593Smuzhiyun 
1673*4882a593Smuzhiyun 	/*
1674*4882a593Smuzhiyun 	 * Either we had an empty extent, and need to re-increment or
1675*4882a593Smuzhiyun 	 * there was no empty extent on a non full rightmost leaf node,
1676*4882a593Smuzhiyun 	 * in which case we still need to increment.
1677*4882a593Smuzhiyun 	 */
1678*4882a593Smuzhiyun 	next_free++;
1679*4882a593Smuzhiyun 	el->l_next_free_rec = cpu_to_le16(next_free);
1680*4882a593Smuzhiyun 	/*
1681*4882a593Smuzhiyun 	 * Make sure none of the math above just messed up our tree.
1682*4882a593Smuzhiyun 	 */
1683*4882a593Smuzhiyun 	BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1684*4882a593Smuzhiyun 
1685*4882a593Smuzhiyun 	el->l_recs[insert_index] = *insert_rec;
1686*4882a593Smuzhiyun 
1687*4882a593Smuzhiyun }
1688*4882a593Smuzhiyun 
ocfs2_remove_empty_extent(struct ocfs2_extent_list * el)1689*4882a593Smuzhiyun static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1690*4882a593Smuzhiyun {
1691*4882a593Smuzhiyun 	int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1692*4882a593Smuzhiyun 
1693*4882a593Smuzhiyun 	BUG_ON(num_recs == 0);
1694*4882a593Smuzhiyun 
1695*4882a593Smuzhiyun 	if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1696*4882a593Smuzhiyun 		num_recs--;
1697*4882a593Smuzhiyun 		size = num_recs * sizeof(struct ocfs2_extent_rec);
1698*4882a593Smuzhiyun 		memmove(&el->l_recs[0], &el->l_recs[1], size);
1699*4882a593Smuzhiyun 		memset(&el->l_recs[num_recs], 0,
1700*4882a593Smuzhiyun 		       sizeof(struct ocfs2_extent_rec));
1701*4882a593Smuzhiyun 		el->l_next_free_rec = cpu_to_le16(num_recs);
1702*4882a593Smuzhiyun 	}
1703*4882a593Smuzhiyun }
1704*4882a593Smuzhiyun 
1705*4882a593Smuzhiyun /*
1706*4882a593Smuzhiyun  * Create an empty extent record .
1707*4882a593Smuzhiyun  *
1708*4882a593Smuzhiyun  * l_next_free_rec may be updated.
1709*4882a593Smuzhiyun  *
1710*4882a593Smuzhiyun  * If an empty extent already exists do nothing.
1711*4882a593Smuzhiyun  */
ocfs2_create_empty_extent(struct ocfs2_extent_list * el)1712*4882a593Smuzhiyun static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1713*4882a593Smuzhiyun {
1714*4882a593Smuzhiyun 	int next_free = le16_to_cpu(el->l_next_free_rec);
1715*4882a593Smuzhiyun 
1716*4882a593Smuzhiyun 	BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1717*4882a593Smuzhiyun 
1718*4882a593Smuzhiyun 	if (next_free == 0)
1719*4882a593Smuzhiyun 		goto set_and_inc;
1720*4882a593Smuzhiyun 
1721*4882a593Smuzhiyun 	if (ocfs2_is_empty_extent(&el->l_recs[0]))
1722*4882a593Smuzhiyun 		return;
1723*4882a593Smuzhiyun 
1724*4882a593Smuzhiyun 	mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1725*4882a593Smuzhiyun 			"Asked to create an empty extent in a full list:\n"
1726*4882a593Smuzhiyun 			"count = %u, tree depth = %u",
1727*4882a593Smuzhiyun 			le16_to_cpu(el->l_count),
1728*4882a593Smuzhiyun 			le16_to_cpu(el->l_tree_depth));
1729*4882a593Smuzhiyun 
1730*4882a593Smuzhiyun 	ocfs2_shift_records_right(el);
1731*4882a593Smuzhiyun 
1732*4882a593Smuzhiyun set_and_inc:
1733*4882a593Smuzhiyun 	le16_add_cpu(&el->l_next_free_rec, 1);
1734*4882a593Smuzhiyun 	memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1735*4882a593Smuzhiyun }
1736*4882a593Smuzhiyun 
1737*4882a593Smuzhiyun /*
1738*4882a593Smuzhiyun  * For a rotation which involves two leaf nodes, the "root node" is
1739*4882a593Smuzhiyun  * the lowest level tree node which contains a path to both leafs. This
1740*4882a593Smuzhiyun  * resulting set of information can be used to form a complete "subtree"
1741*4882a593Smuzhiyun  *
1742*4882a593Smuzhiyun  * This function is passed two full paths from the dinode down to a
1743*4882a593Smuzhiyun  * pair of adjacent leaves. It's task is to figure out which path
1744*4882a593Smuzhiyun  * index contains the subtree root - this can be the root index itself
1745*4882a593Smuzhiyun  * in a worst-case rotation.
1746*4882a593Smuzhiyun  *
1747*4882a593Smuzhiyun  * The array index of the subtree root is passed back.
1748*4882a593Smuzhiyun  */
ocfs2_find_subtree_root(struct ocfs2_extent_tree * et,struct ocfs2_path * left,struct ocfs2_path * right)1749*4882a593Smuzhiyun int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et,
1750*4882a593Smuzhiyun 			    struct ocfs2_path *left,
1751*4882a593Smuzhiyun 			    struct ocfs2_path *right)
1752*4882a593Smuzhiyun {
1753*4882a593Smuzhiyun 	int i = 0;
1754*4882a593Smuzhiyun 
1755*4882a593Smuzhiyun 	/*
1756*4882a593Smuzhiyun 	 * Check that the caller passed in two paths from the same tree.
1757*4882a593Smuzhiyun 	 */
1758*4882a593Smuzhiyun 	BUG_ON(path_root_bh(left) != path_root_bh(right));
1759*4882a593Smuzhiyun 
1760*4882a593Smuzhiyun 	do {
1761*4882a593Smuzhiyun 		i++;
1762*4882a593Smuzhiyun 
1763*4882a593Smuzhiyun 		/*
1764*4882a593Smuzhiyun 		 * The caller didn't pass two adjacent paths.
1765*4882a593Smuzhiyun 		 */
1766*4882a593Smuzhiyun 		mlog_bug_on_msg(i > left->p_tree_depth,
1767*4882a593Smuzhiyun 				"Owner %llu, left depth %u, right depth %u\n"
1768*4882a593Smuzhiyun 				"left leaf blk %llu, right leaf blk %llu\n",
1769*4882a593Smuzhiyun 				(unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
1770*4882a593Smuzhiyun 				left->p_tree_depth, right->p_tree_depth,
1771*4882a593Smuzhiyun 				(unsigned long long)path_leaf_bh(left)->b_blocknr,
1772*4882a593Smuzhiyun 				(unsigned long long)path_leaf_bh(right)->b_blocknr);
1773*4882a593Smuzhiyun 	} while (left->p_node[i].bh->b_blocknr ==
1774*4882a593Smuzhiyun 		 right->p_node[i].bh->b_blocknr);
1775*4882a593Smuzhiyun 
1776*4882a593Smuzhiyun 	return i - 1;
1777*4882a593Smuzhiyun }
1778*4882a593Smuzhiyun 
1779*4882a593Smuzhiyun typedef void (path_insert_t)(void *, struct buffer_head *);
1780*4882a593Smuzhiyun 
1781*4882a593Smuzhiyun /*
1782*4882a593Smuzhiyun  * Traverse a btree path in search of cpos, starting at root_el.
1783*4882a593Smuzhiyun  *
1784*4882a593Smuzhiyun  * This code can be called with a cpos larger than the tree, in which
1785*4882a593Smuzhiyun  * case it will return the rightmost path.
1786*4882a593Smuzhiyun  */
__ocfs2_find_path(struct ocfs2_caching_info * ci,struct ocfs2_extent_list * root_el,u32 cpos,path_insert_t * func,void * data)1787*4882a593Smuzhiyun static int __ocfs2_find_path(struct ocfs2_caching_info *ci,
1788*4882a593Smuzhiyun 			     struct ocfs2_extent_list *root_el, u32 cpos,
1789*4882a593Smuzhiyun 			     path_insert_t *func, void *data)
1790*4882a593Smuzhiyun {
1791*4882a593Smuzhiyun 	int i, ret = 0;
1792*4882a593Smuzhiyun 	u32 range;
1793*4882a593Smuzhiyun 	u64 blkno;
1794*4882a593Smuzhiyun 	struct buffer_head *bh = NULL;
1795*4882a593Smuzhiyun 	struct ocfs2_extent_block *eb;
1796*4882a593Smuzhiyun 	struct ocfs2_extent_list *el;
1797*4882a593Smuzhiyun 	struct ocfs2_extent_rec *rec;
1798*4882a593Smuzhiyun 
1799*4882a593Smuzhiyun 	el = root_el;
1800*4882a593Smuzhiyun 	while (el->l_tree_depth) {
1801*4882a593Smuzhiyun 		if (le16_to_cpu(el->l_next_free_rec) == 0) {
1802*4882a593Smuzhiyun 			ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1803*4882a593Smuzhiyun 				    "Owner %llu has empty extent list at depth %u\n",
1804*4882a593Smuzhiyun 				    (unsigned long long)ocfs2_metadata_cache_owner(ci),
1805*4882a593Smuzhiyun 				    le16_to_cpu(el->l_tree_depth));
1806*4882a593Smuzhiyun 			ret = -EROFS;
1807*4882a593Smuzhiyun 			goto out;
1808*4882a593Smuzhiyun 
1809*4882a593Smuzhiyun 		}
1810*4882a593Smuzhiyun 
1811*4882a593Smuzhiyun 		for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1812*4882a593Smuzhiyun 			rec = &el->l_recs[i];
1813*4882a593Smuzhiyun 
1814*4882a593Smuzhiyun 			/*
1815*4882a593Smuzhiyun 			 * In the case that cpos is off the allocation
1816*4882a593Smuzhiyun 			 * tree, this should just wind up returning the
1817*4882a593Smuzhiyun 			 * rightmost record.
1818*4882a593Smuzhiyun 			 */
1819*4882a593Smuzhiyun 			range = le32_to_cpu(rec->e_cpos) +
1820*4882a593Smuzhiyun 				ocfs2_rec_clusters(el, rec);
1821*4882a593Smuzhiyun 			if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1822*4882a593Smuzhiyun 			    break;
1823*4882a593Smuzhiyun 		}
1824*4882a593Smuzhiyun 
1825*4882a593Smuzhiyun 		blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1826*4882a593Smuzhiyun 		if (blkno == 0) {
1827*4882a593Smuzhiyun 			ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1828*4882a593Smuzhiyun 				    "Owner %llu has bad blkno in extent list at depth %u (index %d)\n",
1829*4882a593Smuzhiyun 				    (unsigned long long)ocfs2_metadata_cache_owner(ci),
1830*4882a593Smuzhiyun 				    le16_to_cpu(el->l_tree_depth), i);
1831*4882a593Smuzhiyun 			ret = -EROFS;
1832*4882a593Smuzhiyun 			goto out;
1833*4882a593Smuzhiyun 		}
1834*4882a593Smuzhiyun 
1835*4882a593Smuzhiyun 		brelse(bh);
1836*4882a593Smuzhiyun 		bh = NULL;
1837*4882a593Smuzhiyun 		ret = ocfs2_read_extent_block(ci, blkno, &bh);
1838*4882a593Smuzhiyun 		if (ret) {
1839*4882a593Smuzhiyun 			mlog_errno(ret);
1840*4882a593Smuzhiyun 			goto out;
1841*4882a593Smuzhiyun 		}
1842*4882a593Smuzhiyun 
1843*4882a593Smuzhiyun 		eb = (struct ocfs2_extent_block *) bh->b_data;
1844*4882a593Smuzhiyun 		el = &eb->h_list;
1845*4882a593Smuzhiyun 
1846*4882a593Smuzhiyun 		if (le16_to_cpu(el->l_next_free_rec) >
1847*4882a593Smuzhiyun 		    le16_to_cpu(el->l_count)) {
1848*4882a593Smuzhiyun 			ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1849*4882a593Smuzhiyun 				    "Owner %llu has bad count in extent list at block %llu (next free=%u, count=%u)\n",
1850*4882a593Smuzhiyun 				    (unsigned long long)ocfs2_metadata_cache_owner(ci),
1851*4882a593Smuzhiyun 				    (unsigned long long)bh->b_blocknr,
1852*4882a593Smuzhiyun 				    le16_to_cpu(el->l_next_free_rec),
1853*4882a593Smuzhiyun 				    le16_to_cpu(el->l_count));
1854*4882a593Smuzhiyun 			ret = -EROFS;
1855*4882a593Smuzhiyun 			goto out;
1856*4882a593Smuzhiyun 		}
1857*4882a593Smuzhiyun 
1858*4882a593Smuzhiyun 		if (func)
1859*4882a593Smuzhiyun 			func(data, bh);
1860*4882a593Smuzhiyun 	}
1861*4882a593Smuzhiyun 
1862*4882a593Smuzhiyun out:
1863*4882a593Smuzhiyun 	/*
1864*4882a593Smuzhiyun 	 * Catch any trailing bh that the loop didn't handle.
1865*4882a593Smuzhiyun 	 */
1866*4882a593Smuzhiyun 	brelse(bh);
1867*4882a593Smuzhiyun 
1868*4882a593Smuzhiyun 	return ret;
1869*4882a593Smuzhiyun }
1870*4882a593Smuzhiyun 
1871*4882a593Smuzhiyun /*
1872*4882a593Smuzhiyun  * Given an initialized path (that is, it has a valid root extent
1873*4882a593Smuzhiyun  * list), this function will traverse the btree in search of the path
1874*4882a593Smuzhiyun  * which would contain cpos.
1875*4882a593Smuzhiyun  *
1876*4882a593Smuzhiyun  * The path traveled is recorded in the path structure.
1877*4882a593Smuzhiyun  *
1878*4882a593Smuzhiyun  * Note that this will not do any comparisons on leaf node extent
1879*4882a593Smuzhiyun  * records, so it will work fine in the case that we just added a tree
1880*4882a593Smuzhiyun  * branch.
1881*4882a593Smuzhiyun  */
1882*4882a593Smuzhiyun struct find_path_data {
1883*4882a593Smuzhiyun 	int index;
1884*4882a593Smuzhiyun 	struct ocfs2_path *path;
1885*4882a593Smuzhiyun };
find_path_ins(void * data,struct buffer_head * bh)1886*4882a593Smuzhiyun static void find_path_ins(void *data, struct buffer_head *bh)
1887*4882a593Smuzhiyun {
1888*4882a593Smuzhiyun 	struct find_path_data *fp = data;
1889*4882a593Smuzhiyun 
1890*4882a593Smuzhiyun 	get_bh(bh);
1891*4882a593Smuzhiyun 	ocfs2_path_insert_eb(fp->path, fp->index, bh);
1892*4882a593Smuzhiyun 	fp->index++;
1893*4882a593Smuzhiyun }
ocfs2_find_path(struct ocfs2_caching_info * ci,struct ocfs2_path * path,u32 cpos)1894*4882a593Smuzhiyun int ocfs2_find_path(struct ocfs2_caching_info *ci,
1895*4882a593Smuzhiyun 		    struct ocfs2_path *path, u32 cpos)
1896*4882a593Smuzhiyun {
1897*4882a593Smuzhiyun 	struct find_path_data data;
1898*4882a593Smuzhiyun 
1899*4882a593Smuzhiyun 	data.index = 1;
1900*4882a593Smuzhiyun 	data.path = path;
1901*4882a593Smuzhiyun 	return __ocfs2_find_path(ci, path_root_el(path), cpos,
1902*4882a593Smuzhiyun 				 find_path_ins, &data);
1903*4882a593Smuzhiyun }
1904*4882a593Smuzhiyun 
find_leaf_ins(void * data,struct buffer_head * bh)1905*4882a593Smuzhiyun static void find_leaf_ins(void *data, struct buffer_head *bh)
1906*4882a593Smuzhiyun {
1907*4882a593Smuzhiyun 	struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1908*4882a593Smuzhiyun 	struct ocfs2_extent_list *el = &eb->h_list;
1909*4882a593Smuzhiyun 	struct buffer_head **ret = data;
1910*4882a593Smuzhiyun 
1911*4882a593Smuzhiyun 	/* We want to retain only the leaf block. */
1912*4882a593Smuzhiyun 	if (le16_to_cpu(el->l_tree_depth) == 0) {
1913*4882a593Smuzhiyun 		get_bh(bh);
1914*4882a593Smuzhiyun 		*ret = bh;
1915*4882a593Smuzhiyun 	}
1916*4882a593Smuzhiyun }
1917*4882a593Smuzhiyun /*
1918*4882a593Smuzhiyun  * Find the leaf block in the tree which would contain cpos. No
1919*4882a593Smuzhiyun  * checking of the actual leaf is done.
1920*4882a593Smuzhiyun  *
1921*4882a593Smuzhiyun  * Some paths want to call this instead of allocating a path structure
1922*4882a593Smuzhiyun  * and calling ocfs2_find_path().
1923*4882a593Smuzhiyun  *
1924*4882a593Smuzhiyun  * This function doesn't handle non btree extent lists.
1925*4882a593Smuzhiyun  */
ocfs2_find_leaf(struct ocfs2_caching_info * ci,struct ocfs2_extent_list * root_el,u32 cpos,struct buffer_head ** leaf_bh)1926*4882a593Smuzhiyun int ocfs2_find_leaf(struct ocfs2_caching_info *ci,
1927*4882a593Smuzhiyun 		    struct ocfs2_extent_list *root_el, u32 cpos,
1928*4882a593Smuzhiyun 		    struct buffer_head **leaf_bh)
1929*4882a593Smuzhiyun {
1930*4882a593Smuzhiyun 	int ret;
1931*4882a593Smuzhiyun 	struct buffer_head *bh = NULL;
1932*4882a593Smuzhiyun 
1933*4882a593Smuzhiyun 	ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh);
1934*4882a593Smuzhiyun 	if (ret) {
1935*4882a593Smuzhiyun 		mlog_errno(ret);
1936*4882a593Smuzhiyun 		goto out;
1937*4882a593Smuzhiyun 	}
1938*4882a593Smuzhiyun 
1939*4882a593Smuzhiyun 	*leaf_bh = bh;
1940*4882a593Smuzhiyun out:
1941*4882a593Smuzhiyun 	return ret;
1942*4882a593Smuzhiyun }
1943*4882a593Smuzhiyun 
1944*4882a593Smuzhiyun /*
1945*4882a593Smuzhiyun  * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1946*4882a593Smuzhiyun  *
1947*4882a593Smuzhiyun  * Basically, we've moved stuff around at the bottom of the tree and
1948*4882a593Smuzhiyun  * we need to fix up the extent records above the changes to reflect
1949*4882a593Smuzhiyun  * the new changes.
1950*4882a593Smuzhiyun  *
1951*4882a593Smuzhiyun  * left_rec: the record on the left.
1952*4882a593Smuzhiyun  * right_rec: the record to the right of left_rec
1953*4882a593Smuzhiyun  * right_child_el: is the child list pointed to by right_rec
1954*4882a593Smuzhiyun  *
1955*4882a593Smuzhiyun  * By definition, this only works on interior nodes.
1956*4882a593Smuzhiyun  */
ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec * left_rec,struct ocfs2_extent_rec * right_rec,struct ocfs2_extent_list * right_child_el)1957*4882a593Smuzhiyun static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1958*4882a593Smuzhiyun 				  struct ocfs2_extent_rec *right_rec,
1959*4882a593Smuzhiyun 				  struct ocfs2_extent_list *right_child_el)
1960*4882a593Smuzhiyun {
1961*4882a593Smuzhiyun 	u32 left_clusters, right_end;
1962*4882a593Smuzhiyun 
1963*4882a593Smuzhiyun 	/*
1964*4882a593Smuzhiyun 	 * Interior nodes never have holes. Their cpos is the cpos of
1965*4882a593Smuzhiyun 	 * the leftmost record in their child list. Their cluster
1966*4882a593Smuzhiyun 	 * count covers the full theoretical range of their child list
1967*4882a593Smuzhiyun 	 * - the range between their cpos and the cpos of the record
1968*4882a593Smuzhiyun 	 * immediately to their right.
1969*4882a593Smuzhiyun 	 */
1970*4882a593Smuzhiyun 	left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
1971*4882a593Smuzhiyun 	if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) {
1972*4882a593Smuzhiyun 		BUG_ON(right_child_el->l_tree_depth);
1973*4882a593Smuzhiyun 		BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1974*4882a593Smuzhiyun 		left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1975*4882a593Smuzhiyun 	}
1976*4882a593Smuzhiyun 	left_clusters -= le32_to_cpu(left_rec->e_cpos);
1977*4882a593Smuzhiyun 	left_rec->e_int_clusters = cpu_to_le32(left_clusters);
1978*4882a593Smuzhiyun 
1979*4882a593Smuzhiyun 	/*
1980*4882a593Smuzhiyun 	 * Calculate the rightmost cluster count boundary before
1981*4882a593Smuzhiyun 	 * moving cpos - we will need to adjust clusters after
1982*4882a593Smuzhiyun 	 * updating e_cpos to keep the same highest cluster count.
1983*4882a593Smuzhiyun 	 */
1984*4882a593Smuzhiyun 	right_end = le32_to_cpu(right_rec->e_cpos);
1985*4882a593Smuzhiyun 	right_end += le32_to_cpu(right_rec->e_int_clusters);
1986*4882a593Smuzhiyun 
1987*4882a593Smuzhiyun 	right_rec->e_cpos = left_rec->e_cpos;
1988*4882a593Smuzhiyun 	le32_add_cpu(&right_rec->e_cpos, left_clusters);
1989*4882a593Smuzhiyun 
1990*4882a593Smuzhiyun 	right_end -= le32_to_cpu(right_rec->e_cpos);
1991*4882a593Smuzhiyun 	right_rec->e_int_clusters = cpu_to_le32(right_end);
1992*4882a593Smuzhiyun }
1993*4882a593Smuzhiyun 
1994*4882a593Smuzhiyun /*
1995*4882a593Smuzhiyun  * Adjust the adjacent root node records involved in a
1996*4882a593Smuzhiyun  * rotation. left_el_blkno is passed in as a key so that we can easily
1997*4882a593Smuzhiyun  * find it's index in the root list.
1998*4882a593Smuzhiyun  */
ocfs2_adjust_root_records(struct ocfs2_extent_list * root_el,struct ocfs2_extent_list * left_el,struct ocfs2_extent_list * right_el,u64 left_el_blkno)1999*4882a593Smuzhiyun static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
2000*4882a593Smuzhiyun 				      struct ocfs2_extent_list *left_el,
2001*4882a593Smuzhiyun 				      struct ocfs2_extent_list *right_el,
2002*4882a593Smuzhiyun 				      u64 left_el_blkno)
2003*4882a593Smuzhiyun {
2004*4882a593Smuzhiyun 	int i;
2005*4882a593Smuzhiyun 
2006*4882a593Smuzhiyun 	BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
2007*4882a593Smuzhiyun 	       le16_to_cpu(left_el->l_tree_depth));
2008*4882a593Smuzhiyun 
2009*4882a593Smuzhiyun 	for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
2010*4882a593Smuzhiyun 		if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
2011*4882a593Smuzhiyun 			break;
2012*4882a593Smuzhiyun 	}
2013*4882a593Smuzhiyun 
2014*4882a593Smuzhiyun 	/*
2015*4882a593Smuzhiyun 	 * The path walking code should have never returned a root and
2016*4882a593Smuzhiyun 	 * two paths which are not adjacent.
2017*4882a593Smuzhiyun 	 */
2018*4882a593Smuzhiyun 	BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
2019*4882a593Smuzhiyun 
2020*4882a593Smuzhiyun 	ocfs2_adjust_adjacent_records(&root_el->l_recs[i],
2021*4882a593Smuzhiyun 				      &root_el->l_recs[i + 1], right_el);
2022*4882a593Smuzhiyun }
2023*4882a593Smuzhiyun 
2024*4882a593Smuzhiyun /*
2025*4882a593Smuzhiyun  * We've changed a leaf block (in right_path) and need to reflect that
2026*4882a593Smuzhiyun  * change back up the subtree.
2027*4882a593Smuzhiyun  *
2028*4882a593Smuzhiyun  * This happens in multiple places:
2029*4882a593Smuzhiyun  *   - When we've moved an extent record from the left path leaf to the right
2030*4882a593Smuzhiyun  *     path leaf to make room for an empty extent in the left path leaf.
2031*4882a593Smuzhiyun  *   - When our insert into the right path leaf is at the leftmost edge
2032*4882a593Smuzhiyun  *     and requires an update of the path immediately to it's left. This
2033*4882a593Smuzhiyun  *     can occur at the end of some types of rotation and appending inserts.
2034*4882a593Smuzhiyun  *   - When we've adjusted the last extent record in the left path leaf and the
2035*4882a593Smuzhiyun  *     1st extent record in the right path leaf during cross extent block merge.
2036*4882a593Smuzhiyun  */
ocfs2_complete_edge_insert(handle_t * handle,struct ocfs2_path * left_path,struct ocfs2_path * right_path,int subtree_index)2037*4882a593Smuzhiyun static void ocfs2_complete_edge_insert(handle_t *handle,
2038*4882a593Smuzhiyun 				       struct ocfs2_path *left_path,
2039*4882a593Smuzhiyun 				       struct ocfs2_path *right_path,
2040*4882a593Smuzhiyun 				       int subtree_index)
2041*4882a593Smuzhiyun {
2042*4882a593Smuzhiyun 	int i, idx;
2043*4882a593Smuzhiyun 	struct ocfs2_extent_list *el, *left_el, *right_el;
2044*4882a593Smuzhiyun 	struct ocfs2_extent_rec *left_rec, *right_rec;
2045*4882a593Smuzhiyun 	struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2046*4882a593Smuzhiyun 
2047*4882a593Smuzhiyun 	/*
2048*4882a593Smuzhiyun 	 * Update the counts and position values within all the
2049*4882a593Smuzhiyun 	 * interior nodes to reflect the leaf rotation we just did.
2050*4882a593Smuzhiyun 	 *
2051*4882a593Smuzhiyun 	 * The root node is handled below the loop.
2052*4882a593Smuzhiyun 	 *
2053*4882a593Smuzhiyun 	 * We begin the loop with right_el and left_el pointing to the
2054*4882a593Smuzhiyun 	 * leaf lists and work our way up.
2055*4882a593Smuzhiyun 	 *
2056*4882a593Smuzhiyun 	 * NOTE: within this loop, left_el and right_el always refer
2057*4882a593Smuzhiyun 	 * to the *child* lists.
2058*4882a593Smuzhiyun 	 */
2059*4882a593Smuzhiyun 	left_el = path_leaf_el(left_path);
2060*4882a593Smuzhiyun 	right_el = path_leaf_el(right_path);
2061*4882a593Smuzhiyun 	for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
2062*4882a593Smuzhiyun 		trace_ocfs2_complete_edge_insert(i);
2063*4882a593Smuzhiyun 
2064*4882a593Smuzhiyun 		/*
2065*4882a593Smuzhiyun 		 * One nice property of knowing that all of these
2066*4882a593Smuzhiyun 		 * nodes are below the root is that we only deal with
2067*4882a593Smuzhiyun 		 * the leftmost right node record and the rightmost
2068*4882a593Smuzhiyun 		 * left node record.
2069*4882a593Smuzhiyun 		 */
2070*4882a593Smuzhiyun 		el = left_path->p_node[i].el;
2071*4882a593Smuzhiyun 		idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
2072*4882a593Smuzhiyun 		left_rec = &el->l_recs[idx];
2073*4882a593Smuzhiyun 
2074*4882a593Smuzhiyun 		el = right_path->p_node[i].el;
2075*4882a593Smuzhiyun 		right_rec = &el->l_recs[0];
2076*4882a593Smuzhiyun 
2077*4882a593Smuzhiyun 		ocfs2_adjust_adjacent_records(left_rec, right_rec, right_el);
2078*4882a593Smuzhiyun 
2079*4882a593Smuzhiyun 		ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
2080*4882a593Smuzhiyun 		ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
2081*4882a593Smuzhiyun 
2082*4882a593Smuzhiyun 		/*
2083*4882a593Smuzhiyun 		 * Setup our list pointers now so that the current
2084*4882a593Smuzhiyun 		 * parents become children in the next iteration.
2085*4882a593Smuzhiyun 		 */
2086*4882a593Smuzhiyun 		left_el = left_path->p_node[i].el;
2087*4882a593Smuzhiyun 		right_el = right_path->p_node[i].el;
2088*4882a593Smuzhiyun 	}
2089*4882a593Smuzhiyun 
2090*4882a593Smuzhiyun 	/*
2091*4882a593Smuzhiyun 	 * At the root node, adjust the two adjacent records which
2092*4882a593Smuzhiyun 	 * begin our path to the leaves.
2093*4882a593Smuzhiyun 	 */
2094*4882a593Smuzhiyun 
2095*4882a593Smuzhiyun 	el = left_path->p_node[subtree_index].el;
2096*4882a593Smuzhiyun 	left_el = left_path->p_node[subtree_index + 1].el;
2097*4882a593Smuzhiyun 	right_el = right_path->p_node[subtree_index + 1].el;
2098*4882a593Smuzhiyun 
2099*4882a593Smuzhiyun 	ocfs2_adjust_root_records(el, left_el, right_el,
2100*4882a593Smuzhiyun 				  left_path->p_node[subtree_index + 1].bh->b_blocknr);
2101*4882a593Smuzhiyun 
2102*4882a593Smuzhiyun 	root_bh = left_path->p_node[subtree_index].bh;
2103*4882a593Smuzhiyun 
2104*4882a593Smuzhiyun 	ocfs2_journal_dirty(handle, root_bh);
2105*4882a593Smuzhiyun }
2106*4882a593Smuzhiyun 
ocfs2_rotate_subtree_right(handle_t * handle,struct ocfs2_extent_tree * et,struct ocfs2_path * left_path,struct ocfs2_path * right_path,int subtree_index)2107*4882a593Smuzhiyun static int ocfs2_rotate_subtree_right(handle_t *handle,
2108*4882a593Smuzhiyun 				      struct ocfs2_extent_tree *et,
2109*4882a593Smuzhiyun 				      struct ocfs2_path *left_path,
2110*4882a593Smuzhiyun 				      struct ocfs2_path *right_path,
2111*4882a593Smuzhiyun 				      int subtree_index)
2112*4882a593Smuzhiyun {
2113*4882a593Smuzhiyun 	int ret, i;
2114*4882a593Smuzhiyun 	struct buffer_head *right_leaf_bh;
2115*4882a593Smuzhiyun 	struct buffer_head *left_leaf_bh = NULL;
2116*4882a593Smuzhiyun 	struct buffer_head *root_bh;
2117*4882a593Smuzhiyun 	struct ocfs2_extent_list *right_el, *left_el;
2118*4882a593Smuzhiyun 	struct ocfs2_extent_rec move_rec;
2119*4882a593Smuzhiyun 
2120*4882a593Smuzhiyun 	left_leaf_bh = path_leaf_bh(left_path);
2121*4882a593Smuzhiyun 	left_el = path_leaf_el(left_path);
2122*4882a593Smuzhiyun 
2123*4882a593Smuzhiyun 	if (left_el->l_next_free_rec != left_el->l_count) {
2124*4882a593Smuzhiyun 		ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
2125*4882a593Smuzhiyun 			    "Inode %llu has non-full interior leaf node %llu (next free = %u)\n",
2126*4882a593Smuzhiyun 			    (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2127*4882a593Smuzhiyun 			    (unsigned long long)left_leaf_bh->b_blocknr,
2128*4882a593Smuzhiyun 			    le16_to_cpu(left_el->l_next_free_rec));
2129*4882a593Smuzhiyun 		return -EROFS;
2130*4882a593Smuzhiyun 	}
2131*4882a593Smuzhiyun 
2132*4882a593Smuzhiyun 	/*
2133*4882a593Smuzhiyun 	 * This extent block may already have an empty record, so we
2134*4882a593Smuzhiyun 	 * return early if so.
2135*4882a593Smuzhiyun 	 */
2136*4882a593Smuzhiyun 	if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
2137*4882a593Smuzhiyun 		return 0;
2138*4882a593Smuzhiyun 
2139*4882a593Smuzhiyun 	root_bh = left_path->p_node[subtree_index].bh;
2140*4882a593Smuzhiyun 	BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2141*4882a593Smuzhiyun 
2142*4882a593Smuzhiyun 	ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
2143*4882a593Smuzhiyun 					   subtree_index);
2144*4882a593Smuzhiyun 	if (ret) {
2145*4882a593Smuzhiyun 		mlog_errno(ret);
2146*4882a593Smuzhiyun 		goto out;
2147*4882a593Smuzhiyun 	}
2148*4882a593Smuzhiyun 
2149*4882a593Smuzhiyun 	for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2150*4882a593Smuzhiyun 		ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2151*4882a593Smuzhiyun 						   right_path, i);
2152*4882a593Smuzhiyun 		if (ret) {
2153*4882a593Smuzhiyun 			mlog_errno(ret);
2154*4882a593Smuzhiyun 			goto out;
2155*4882a593Smuzhiyun 		}
2156*4882a593Smuzhiyun 
2157*4882a593Smuzhiyun 		ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2158*4882a593Smuzhiyun 						   left_path, i);
2159*4882a593Smuzhiyun 		if (ret) {
2160*4882a593Smuzhiyun 			mlog_errno(ret);
2161*4882a593Smuzhiyun 			goto out;
2162*4882a593Smuzhiyun 		}
2163*4882a593Smuzhiyun 	}
2164*4882a593Smuzhiyun 
2165*4882a593Smuzhiyun 	right_leaf_bh = path_leaf_bh(right_path);
2166*4882a593Smuzhiyun 	right_el = path_leaf_el(right_path);
2167*4882a593Smuzhiyun 
2168*4882a593Smuzhiyun 	/* This is a code error, not a disk corruption. */
2169*4882a593Smuzhiyun 	mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
2170*4882a593Smuzhiyun 			"because rightmost leaf block %llu is empty\n",
2171*4882a593Smuzhiyun 			(unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2172*4882a593Smuzhiyun 			(unsigned long long)right_leaf_bh->b_blocknr);
2173*4882a593Smuzhiyun 
2174*4882a593Smuzhiyun 	ocfs2_create_empty_extent(right_el);
2175*4882a593Smuzhiyun 
2176*4882a593Smuzhiyun 	ocfs2_journal_dirty(handle, right_leaf_bh);
2177*4882a593Smuzhiyun 
2178*4882a593Smuzhiyun 	/* Do the copy now. */
2179*4882a593Smuzhiyun 	i = le16_to_cpu(left_el->l_next_free_rec) - 1;
2180*4882a593Smuzhiyun 	move_rec = left_el->l_recs[i];
2181*4882a593Smuzhiyun 	right_el->l_recs[0] = move_rec;
2182*4882a593Smuzhiyun 
2183*4882a593Smuzhiyun 	/*
2184*4882a593Smuzhiyun 	 * Clear out the record we just copied and shift everything
2185*4882a593Smuzhiyun 	 * over, leaving an empty extent in the left leaf.
2186*4882a593Smuzhiyun 	 *
2187*4882a593Smuzhiyun 	 * We temporarily subtract from next_free_rec so that the
2188*4882a593Smuzhiyun 	 * shift will lose the tail record (which is now defunct).
2189*4882a593Smuzhiyun 	 */
2190*4882a593Smuzhiyun 	le16_add_cpu(&left_el->l_next_free_rec, -1);
2191*4882a593Smuzhiyun 	ocfs2_shift_records_right(left_el);
2192*4882a593Smuzhiyun 	memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2193*4882a593Smuzhiyun 	le16_add_cpu(&left_el->l_next_free_rec, 1);
2194*4882a593Smuzhiyun 
2195*4882a593Smuzhiyun 	ocfs2_journal_dirty(handle, left_leaf_bh);
2196*4882a593Smuzhiyun 
2197*4882a593Smuzhiyun 	ocfs2_complete_edge_insert(handle, left_path, right_path,
2198*4882a593Smuzhiyun 				   subtree_index);
2199*4882a593Smuzhiyun 
2200*4882a593Smuzhiyun out:
2201*4882a593Smuzhiyun 	return ret;
2202*4882a593Smuzhiyun }
2203*4882a593Smuzhiyun 
2204*4882a593Smuzhiyun /*
2205*4882a593Smuzhiyun  * Given a full path, determine what cpos value would return us a path
2206*4882a593Smuzhiyun  * containing the leaf immediately to the left of the current one.
2207*4882a593Smuzhiyun  *
2208*4882a593Smuzhiyun  * Will return zero if the path passed in is already the leftmost path.
2209*4882a593Smuzhiyun  */
ocfs2_find_cpos_for_left_leaf(struct super_block * sb,struct ocfs2_path * path,u32 * cpos)2210*4882a593Smuzhiyun int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
2211*4882a593Smuzhiyun 				  struct ocfs2_path *path, u32 *cpos)
2212*4882a593Smuzhiyun {
2213*4882a593Smuzhiyun 	int i, j, ret = 0;
2214*4882a593Smuzhiyun 	u64 blkno;
2215*4882a593Smuzhiyun 	struct ocfs2_extent_list *el;
2216*4882a593Smuzhiyun 
2217*4882a593Smuzhiyun 	BUG_ON(path->p_tree_depth == 0);
2218*4882a593Smuzhiyun 
2219*4882a593Smuzhiyun 	*cpos = 0;
2220*4882a593Smuzhiyun 
2221*4882a593Smuzhiyun 	blkno = path_leaf_bh(path)->b_blocknr;
2222*4882a593Smuzhiyun 
2223*4882a593Smuzhiyun 	/* Start at the tree node just above the leaf and work our way up. */
2224*4882a593Smuzhiyun 	i = path->p_tree_depth - 1;
2225*4882a593Smuzhiyun 	while (i >= 0) {
2226*4882a593Smuzhiyun 		el = path->p_node[i].el;
2227*4882a593Smuzhiyun 
2228*4882a593Smuzhiyun 		/*
2229*4882a593Smuzhiyun 		 * Find the extent record just before the one in our
2230*4882a593Smuzhiyun 		 * path.
2231*4882a593Smuzhiyun 		 */
2232*4882a593Smuzhiyun 		for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2233*4882a593Smuzhiyun 			if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2234*4882a593Smuzhiyun 				if (j == 0) {
2235*4882a593Smuzhiyun 					if (i == 0) {
2236*4882a593Smuzhiyun 						/*
2237*4882a593Smuzhiyun 						 * We've determined that the
2238*4882a593Smuzhiyun 						 * path specified is already
2239*4882a593Smuzhiyun 						 * the leftmost one - return a
2240*4882a593Smuzhiyun 						 * cpos of zero.
2241*4882a593Smuzhiyun 						 */
2242*4882a593Smuzhiyun 						goto out;
2243*4882a593Smuzhiyun 					}
2244*4882a593Smuzhiyun 					/*
2245*4882a593Smuzhiyun 					 * The leftmost record points to our
2246*4882a593Smuzhiyun 					 * leaf - we need to travel up the
2247*4882a593Smuzhiyun 					 * tree one level.
2248*4882a593Smuzhiyun 					 */
2249*4882a593Smuzhiyun 					goto next_node;
2250*4882a593Smuzhiyun 				}
2251*4882a593Smuzhiyun 
2252*4882a593Smuzhiyun 				*cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
2253*4882a593Smuzhiyun 				*cpos = *cpos + ocfs2_rec_clusters(el,
2254*4882a593Smuzhiyun 							   &el->l_recs[j - 1]);
2255*4882a593Smuzhiyun 				*cpos = *cpos - 1;
2256*4882a593Smuzhiyun 				goto out;
2257*4882a593Smuzhiyun 			}
2258*4882a593Smuzhiyun 		}
2259*4882a593Smuzhiyun 
2260*4882a593Smuzhiyun 		/*
2261*4882a593Smuzhiyun 		 * If we got here, we never found a valid node where
2262*4882a593Smuzhiyun 		 * the tree indicated one should be.
2263*4882a593Smuzhiyun 		 */
2264*4882a593Smuzhiyun 		ocfs2_error(sb, "Invalid extent tree at extent block %llu\n",
2265*4882a593Smuzhiyun 			    (unsigned long long)blkno);
2266*4882a593Smuzhiyun 		ret = -EROFS;
2267*4882a593Smuzhiyun 		goto out;
2268*4882a593Smuzhiyun 
2269*4882a593Smuzhiyun next_node:
2270*4882a593Smuzhiyun 		blkno = path->p_node[i].bh->b_blocknr;
2271*4882a593Smuzhiyun 		i--;
2272*4882a593Smuzhiyun 	}
2273*4882a593Smuzhiyun 
2274*4882a593Smuzhiyun out:
2275*4882a593Smuzhiyun 	return ret;
2276*4882a593Smuzhiyun }
2277*4882a593Smuzhiyun 
2278*4882a593Smuzhiyun /*
2279*4882a593Smuzhiyun  * Extend the transaction by enough credits to complete the rotation,
2280*4882a593Smuzhiyun  * and still leave at least the original number of credits allocated
2281*4882a593Smuzhiyun  * to this transaction.
2282*4882a593Smuzhiyun  */
ocfs2_extend_rotate_transaction(handle_t * handle,int subtree_depth,int op_credits,struct ocfs2_path * path)2283*4882a593Smuzhiyun static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
2284*4882a593Smuzhiyun 					   int op_credits,
2285*4882a593Smuzhiyun 					   struct ocfs2_path *path)
2286*4882a593Smuzhiyun {
2287*4882a593Smuzhiyun 	int ret = 0;
2288*4882a593Smuzhiyun 	int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
2289*4882a593Smuzhiyun 
2290*4882a593Smuzhiyun 	if (jbd2_handle_buffer_credits(handle) < credits)
2291*4882a593Smuzhiyun 		ret = ocfs2_extend_trans(handle,
2292*4882a593Smuzhiyun 				credits - jbd2_handle_buffer_credits(handle));
2293*4882a593Smuzhiyun 
2294*4882a593Smuzhiyun 	return ret;
2295*4882a593Smuzhiyun }
2296*4882a593Smuzhiyun 
2297*4882a593Smuzhiyun /*
2298*4882a593Smuzhiyun  * Trap the case where we're inserting into the theoretical range past
2299*4882a593Smuzhiyun  * the _actual_ left leaf range. Otherwise, we'll rotate a record
2300*4882a593Smuzhiyun  * whose cpos is less than ours into the right leaf.
2301*4882a593Smuzhiyun  *
2302*4882a593Smuzhiyun  * It's only necessary to look at the rightmost record of the left
2303*4882a593Smuzhiyun  * leaf because the logic that calls us should ensure that the
2304*4882a593Smuzhiyun  * theoretical ranges in the path components above the leaves are
2305*4882a593Smuzhiyun  * correct.
2306*4882a593Smuzhiyun  */
ocfs2_rotate_requires_path_adjustment(struct ocfs2_path * left_path,u32 insert_cpos)2307*4882a593Smuzhiyun static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2308*4882a593Smuzhiyun 						 u32 insert_cpos)
2309*4882a593Smuzhiyun {
2310*4882a593Smuzhiyun 	struct ocfs2_extent_list *left_el;
2311*4882a593Smuzhiyun 	struct ocfs2_extent_rec *rec;
2312*4882a593Smuzhiyun 	int next_free;
2313*4882a593Smuzhiyun 
2314*4882a593Smuzhiyun 	left_el = path_leaf_el(left_path);
2315*4882a593Smuzhiyun 	next_free = le16_to_cpu(left_el->l_next_free_rec);
2316*4882a593Smuzhiyun 	rec = &left_el->l_recs[next_free - 1];
2317*4882a593Smuzhiyun 
2318*4882a593Smuzhiyun 	if (insert_cpos > le32_to_cpu(rec->e_cpos))
2319*4882a593Smuzhiyun 		return 1;
2320*4882a593Smuzhiyun 	return 0;
2321*4882a593Smuzhiyun }
2322*4882a593Smuzhiyun 
ocfs2_leftmost_rec_contains(struct ocfs2_extent_list * el,u32 cpos)2323*4882a593Smuzhiyun static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2324*4882a593Smuzhiyun {
2325*4882a593Smuzhiyun 	int next_free = le16_to_cpu(el->l_next_free_rec);
2326*4882a593Smuzhiyun 	unsigned int range;
2327*4882a593Smuzhiyun 	struct ocfs2_extent_rec *rec;
2328*4882a593Smuzhiyun 
2329*4882a593Smuzhiyun 	if (next_free == 0)
2330*4882a593Smuzhiyun 		return 0;
2331*4882a593Smuzhiyun 
2332*4882a593Smuzhiyun 	rec = &el->l_recs[0];
2333*4882a593Smuzhiyun 	if (ocfs2_is_empty_extent(rec)) {
2334*4882a593Smuzhiyun 		/* Empty list. */
2335*4882a593Smuzhiyun 		if (next_free == 1)
2336*4882a593Smuzhiyun 			return 0;
2337*4882a593Smuzhiyun 		rec = &el->l_recs[1];
2338*4882a593Smuzhiyun 	}
2339*4882a593Smuzhiyun 
2340*4882a593Smuzhiyun 	range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2341*4882a593Smuzhiyun 	if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2342*4882a593Smuzhiyun 		return 1;
2343*4882a593Smuzhiyun 	return 0;
2344*4882a593Smuzhiyun }
2345*4882a593Smuzhiyun 
2346*4882a593Smuzhiyun /*
2347*4882a593Smuzhiyun  * Rotate all the records in a btree right one record, starting at insert_cpos.
2348*4882a593Smuzhiyun  *
2349*4882a593Smuzhiyun  * The path to the rightmost leaf should be passed in.
2350*4882a593Smuzhiyun  *
2351*4882a593Smuzhiyun  * The array is assumed to be large enough to hold an entire path (tree depth).
2352*4882a593Smuzhiyun  *
2353*4882a593Smuzhiyun  * Upon successful return from this function:
2354*4882a593Smuzhiyun  *
2355*4882a593Smuzhiyun  * - The 'right_path' array will contain a path to the leaf block
2356*4882a593Smuzhiyun  *   whose range contains e_cpos.
2357*4882a593Smuzhiyun  * - That leaf block will have a single empty extent in list index 0.
2358*4882a593Smuzhiyun  * - In the case that the rotation requires a post-insert update,
2359*4882a593Smuzhiyun  *   *ret_left_path will contain a valid path which can be passed to
2360*4882a593Smuzhiyun  *   ocfs2_insert_path().
2361*4882a593Smuzhiyun  */
ocfs2_rotate_tree_right(handle_t * handle,struct ocfs2_extent_tree * et,enum ocfs2_split_type split,u32 insert_cpos,struct ocfs2_path * right_path,struct ocfs2_path ** ret_left_path)2362*4882a593Smuzhiyun static int ocfs2_rotate_tree_right(handle_t *handle,
2363*4882a593Smuzhiyun 				   struct ocfs2_extent_tree *et,
2364*4882a593Smuzhiyun 				   enum ocfs2_split_type split,
2365*4882a593Smuzhiyun 				   u32 insert_cpos,
2366*4882a593Smuzhiyun 				   struct ocfs2_path *right_path,
2367*4882a593Smuzhiyun 				   struct ocfs2_path **ret_left_path)
2368*4882a593Smuzhiyun {
2369*4882a593Smuzhiyun 	int ret, start, orig_credits = jbd2_handle_buffer_credits(handle);
2370*4882a593Smuzhiyun 	u32 cpos;
2371*4882a593Smuzhiyun 	struct ocfs2_path *left_path = NULL;
2372*4882a593Smuzhiyun 	struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
2373*4882a593Smuzhiyun 
2374*4882a593Smuzhiyun 	*ret_left_path = NULL;
2375*4882a593Smuzhiyun 
2376*4882a593Smuzhiyun 	left_path = ocfs2_new_path_from_path(right_path);
2377*4882a593Smuzhiyun 	if (!left_path) {
2378*4882a593Smuzhiyun 		ret = -ENOMEM;
2379*4882a593Smuzhiyun 		mlog_errno(ret);
2380*4882a593Smuzhiyun 		goto out;
2381*4882a593Smuzhiyun 	}
2382*4882a593Smuzhiyun 
2383*4882a593Smuzhiyun 	ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
2384*4882a593Smuzhiyun 	if (ret) {
2385*4882a593Smuzhiyun 		mlog_errno(ret);
2386*4882a593Smuzhiyun 		goto out;
2387*4882a593Smuzhiyun 	}
2388*4882a593Smuzhiyun 
2389*4882a593Smuzhiyun 	trace_ocfs2_rotate_tree_right(
2390*4882a593Smuzhiyun 		(unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2391*4882a593Smuzhiyun 		insert_cpos, cpos);
2392*4882a593Smuzhiyun 
2393*4882a593Smuzhiyun 	/*
2394*4882a593Smuzhiyun 	 * What we want to do here is:
2395*4882a593Smuzhiyun 	 *
2396*4882a593Smuzhiyun 	 * 1) Start with the rightmost path.
2397*4882a593Smuzhiyun 	 *
2398*4882a593Smuzhiyun 	 * 2) Determine a path to the leaf block directly to the left
2399*4882a593Smuzhiyun 	 *    of that leaf.
2400*4882a593Smuzhiyun 	 *
2401*4882a593Smuzhiyun 	 * 3) Determine the 'subtree root' - the lowest level tree node
2402*4882a593Smuzhiyun 	 *    which contains a path to both leaves.
2403*4882a593Smuzhiyun 	 *
2404*4882a593Smuzhiyun 	 * 4) Rotate the subtree.
2405*4882a593Smuzhiyun 	 *
2406*4882a593Smuzhiyun 	 * 5) Find the next subtree by considering the left path to be
2407*4882a593Smuzhiyun 	 *    the new right path.
2408*4882a593Smuzhiyun 	 *
2409*4882a593Smuzhiyun 	 * The check at the top of this while loop also accepts
2410*4882a593Smuzhiyun 	 * insert_cpos == cpos because cpos is only a _theoretical_
2411*4882a593Smuzhiyun 	 * value to get us the left path - insert_cpos might very well
2412*4882a593Smuzhiyun 	 * be filling that hole.
2413*4882a593Smuzhiyun 	 *
2414*4882a593Smuzhiyun 	 * Stop at a cpos of '0' because we either started at the
2415*4882a593Smuzhiyun 	 * leftmost branch (i.e., a tree with one branch and a
2416*4882a593Smuzhiyun 	 * rotation inside of it), or we've gone as far as we can in
2417*4882a593Smuzhiyun 	 * rotating subtrees.
2418*4882a593Smuzhiyun 	 */
2419*4882a593Smuzhiyun 	while (cpos && insert_cpos <= cpos) {
2420*4882a593Smuzhiyun 		trace_ocfs2_rotate_tree_right(
2421*4882a593Smuzhiyun 			(unsigned long long)
2422*4882a593Smuzhiyun 			ocfs2_metadata_cache_owner(et->et_ci),
2423*4882a593Smuzhiyun 			insert_cpos, cpos);
2424*4882a593Smuzhiyun 
2425*4882a593Smuzhiyun 		ret = ocfs2_find_path(et->et_ci, left_path, cpos);
2426*4882a593Smuzhiyun 		if (ret) {
2427*4882a593Smuzhiyun 			mlog_errno(ret);
2428*4882a593Smuzhiyun 			goto out;
2429*4882a593Smuzhiyun 		}
2430*4882a593Smuzhiyun 
2431*4882a593Smuzhiyun 		mlog_bug_on_msg(path_leaf_bh(left_path) ==
2432*4882a593Smuzhiyun 				path_leaf_bh(right_path),
2433*4882a593Smuzhiyun 				"Owner %llu: error during insert of %u "
2434*4882a593Smuzhiyun 				"(left path cpos %u) results in two identical "
2435*4882a593Smuzhiyun 				"paths ending at %llu\n",
2436*4882a593Smuzhiyun 				(unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2437*4882a593Smuzhiyun 				insert_cpos, cpos,
2438*4882a593Smuzhiyun 				(unsigned long long)
2439*4882a593Smuzhiyun 				path_leaf_bh(left_path)->b_blocknr);
2440*4882a593Smuzhiyun 
2441*4882a593Smuzhiyun 		if (split == SPLIT_NONE &&
2442*4882a593Smuzhiyun 		    ocfs2_rotate_requires_path_adjustment(left_path,
2443*4882a593Smuzhiyun 							  insert_cpos)) {
2444*4882a593Smuzhiyun 
2445*4882a593Smuzhiyun 			/*
2446*4882a593Smuzhiyun 			 * We've rotated the tree as much as we
2447*4882a593Smuzhiyun 			 * should. The rest is up to
2448*4882a593Smuzhiyun 			 * ocfs2_insert_path() to complete, after the
2449*4882a593Smuzhiyun 			 * record insertion. We indicate this
2450*4882a593Smuzhiyun 			 * situation by returning the left path.
2451*4882a593Smuzhiyun 			 *
2452*4882a593Smuzhiyun 			 * The reason we don't adjust the records here
2453*4882a593Smuzhiyun 			 * before the record insert is that an error
2454*4882a593Smuzhiyun 			 * later might break the rule where a parent
2455*4882a593Smuzhiyun 			 * record e_cpos will reflect the actual
2456*4882a593Smuzhiyun 			 * e_cpos of the 1st nonempty record of the
2457*4882a593Smuzhiyun 			 * child list.
2458*4882a593Smuzhiyun 			 */
2459*4882a593Smuzhiyun 			*ret_left_path = left_path;
2460*4882a593Smuzhiyun 			goto out_ret_path;
2461*4882a593Smuzhiyun 		}
2462*4882a593Smuzhiyun 
2463*4882a593Smuzhiyun 		start = ocfs2_find_subtree_root(et, left_path, right_path);
2464*4882a593Smuzhiyun 
2465*4882a593Smuzhiyun 		trace_ocfs2_rotate_subtree(start,
2466*4882a593Smuzhiyun 			(unsigned long long)
2467*4882a593Smuzhiyun 			right_path->p_node[start].bh->b_blocknr,
2468*4882a593Smuzhiyun 			right_path->p_tree_depth);
2469*4882a593Smuzhiyun 
2470*4882a593Smuzhiyun 		ret = ocfs2_extend_rotate_transaction(handle, start,
2471*4882a593Smuzhiyun 						      orig_credits, right_path);
2472*4882a593Smuzhiyun 		if (ret) {
2473*4882a593Smuzhiyun 			mlog_errno(ret);
2474*4882a593Smuzhiyun 			goto out;
2475*4882a593Smuzhiyun 		}
2476*4882a593Smuzhiyun 
2477*4882a593Smuzhiyun 		ret = ocfs2_rotate_subtree_right(handle, et, left_path,
2478*4882a593Smuzhiyun 						 right_path, start);
2479*4882a593Smuzhiyun 		if (ret) {
2480*4882a593Smuzhiyun 			mlog_errno(ret);
2481*4882a593Smuzhiyun 			goto out;
2482*4882a593Smuzhiyun 		}
2483*4882a593Smuzhiyun 
2484*4882a593Smuzhiyun 		if (split != SPLIT_NONE &&
2485*4882a593Smuzhiyun 		    ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2486*4882a593Smuzhiyun 						insert_cpos)) {
2487*4882a593Smuzhiyun 			/*
2488*4882a593Smuzhiyun 			 * A rotate moves the rightmost left leaf
2489*4882a593Smuzhiyun 			 * record over to the leftmost right leaf
2490*4882a593Smuzhiyun 			 * slot. If we're doing an extent split
2491*4882a593Smuzhiyun 			 * instead of a real insert, then we have to
2492*4882a593Smuzhiyun 			 * check that the extent to be split wasn't
2493*4882a593Smuzhiyun 			 * just moved over. If it was, then we can
2494*4882a593Smuzhiyun 			 * exit here, passing left_path back -
2495*4882a593Smuzhiyun 			 * ocfs2_split_extent() is smart enough to
2496*4882a593Smuzhiyun 			 * search both leaves.
2497*4882a593Smuzhiyun 			 */
2498*4882a593Smuzhiyun 			*ret_left_path = left_path;
2499*4882a593Smuzhiyun 			goto out_ret_path;
2500*4882a593Smuzhiyun 		}
2501*4882a593Smuzhiyun 
2502*4882a593Smuzhiyun 		/*
2503*4882a593Smuzhiyun 		 * There is no need to re-read the next right path
2504*4882a593Smuzhiyun 		 * as we know that it'll be our current left
2505*4882a593Smuzhiyun 		 * path. Optimize by copying values instead.
2506*4882a593Smuzhiyun 		 */
2507*4882a593Smuzhiyun 		ocfs2_mv_path(right_path, left_path);
2508*4882a593Smuzhiyun 
2509*4882a593Smuzhiyun 		ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
2510*4882a593Smuzhiyun 		if (ret) {
2511*4882a593Smuzhiyun 			mlog_errno(ret);
2512*4882a593Smuzhiyun 			goto out;
2513*4882a593Smuzhiyun 		}
2514*4882a593Smuzhiyun 	}
2515*4882a593Smuzhiyun 
2516*4882a593Smuzhiyun out:
2517*4882a593Smuzhiyun 	ocfs2_free_path(left_path);
2518*4882a593Smuzhiyun 
2519*4882a593Smuzhiyun out_ret_path:
2520*4882a593Smuzhiyun 	return ret;
2521*4882a593Smuzhiyun }
2522*4882a593Smuzhiyun 
ocfs2_update_edge_lengths(handle_t * handle,struct ocfs2_extent_tree * et,struct ocfs2_path * path)2523*4882a593Smuzhiyun static int ocfs2_update_edge_lengths(handle_t *handle,
2524*4882a593Smuzhiyun 				     struct ocfs2_extent_tree *et,
2525*4882a593Smuzhiyun 				     struct ocfs2_path *path)
2526*4882a593Smuzhiyun {
2527*4882a593Smuzhiyun 	int i, idx, ret;
2528*4882a593Smuzhiyun 	struct ocfs2_extent_rec *rec;
2529*4882a593Smuzhiyun 	struct ocfs2_extent_list *el;
2530*4882a593Smuzhiyun 	struct ocfs2_extent_block *eb;
2531*4882a593Smuzhiyun 	u32 range;
2532*4882a593Smuzhiyun 
2533*4882a593Smuzhiyun 	ret = ocfs2_journal_access_path(et->et_ci, handle, path);
2534*4882a593Smuzhiyun 	if (ret) {
2535*4882a593Smuzhiyun 		mlog_errno(ret);
2536*4882a593Smuzhiyun 		goto out;
2537*4882a593Smuzhiyun 	}
2538*4882a593Smuzhiyun 
2539*4882a593Smuzhiyun 	/* Path should always be rightmost. */
2540*4882a593Smuzhiyun 	eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2541*4882a593Smuzhiyun 	BUG_ON(eb->h_next_leaf_blk != 0ULL);
2542*4882a593Smuzhiyun 
2543*4882a593Smuzhiyun 	el = &eb->h_list;
2544*4882a593Smuzhiyun 	BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2545*4882a593Smuzhiyun 	idx = le16_to_cpu(el->l_next_free_rec) - 1;
2546*4882a593Smuzhiyun 	rec = &el->l_recs[idx];
2547*4882a593Smuzhiyun 	range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2548*4882a593Smuzhiyun 
2549*4882a593Smuzhiyun 	for (i = 0; i < path->p_tree_depth; i++) {
2550*4882a593Smuzhiyun 		el = path->p_node[i].el;
2551*4882a593Smuzhiyun 		idx = le16_to_cpu(el->l_next_free_rec) - 1;
2552*4882a593Smuzhiyun 		rec = &el->l_recs[idx];
2553*4882a593Smuzhiyun 
2554*4882a593Smuzhiyun 		rec->e_int_clusters = cpu_to_le32(range);
2555*4882a593Smuzhiyun 		le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2556*4882a593Smuzhiyun 
2557*4882a593Smuzhiyun 		ocfs2_journal_dirty(handle, path->p_node[i].bh);
2558*4882a593Smuzhiyun 	}
2559*4882a593Smuzhiyun out:
2560*4882a593Smuzhiyun 	return ret;
2561*4882a593Smuzhiyun }
2562*4882a593Smuzhiyun 
ocfs2_unlink_path(handle_t * handle,struct ocfs2_extent_tree * et,struct ocfs2_cached_dealloc_ctxt * dealloc,struct ocfs2_path * path,int unlink_start)2563*4882a593Smuzhiyun static void ocfs2_unlink_path(handle_t *handle,
2564*4882a593Smuzhiyun 			      struct ocfs2_extent_tree *et,
2565*4882a593Smuzhiyun 			      struct ocfs2_cached_dealloc_ctxt *dealloc,
2566*4882a593Smuzhiyun 			      struct ocfs2_path *path, int unlink_start)
2567*4882a593Smuzhiyun {
2568*4882a593Smuzhiyun 	int ret, i;
2569*4882a593Smuzhiyun 	struct ocfs2_extent_block *eb;
2570*4882a593Smuzhiyun 	struct ocfs2_extent_list *el;
2571*4882a593Smuzhiyun 	struct buffer_head *bh;
2572*4882a593Smuzhiyun 
2573*4882a593Smuzhiyun 	for(i = unlink_start; i < path_num_items(path); i++) {
2574*4882a593Smuzhiyun 		bh = path->p_node[i].bh;
2575*4882a593Smuzhiyun 
2576*4882a593Smuzhiyun 		eb = (struct ocfs2_extent_block *)bh->b_data;
2577*4882a593Smuzhiyun 		/*
2578*4882a593Smuzhiyun 		 * Not all nodes might have had their final count
2579*4882a593Smuzhiyun 		 * decremented by the caller - handle this here.
2580*4882a593Smuzhiyun 		 */
2581*4882a593Smuzhiyun 		el = &eb->h_list;
2582*4882a593Smuzhiyun 		if (le16_to_cpu(el->l_next_free_rec) > 1) {
2583*4882a593Smuzhiyun 			mlog(ML_ERROR,
2584*4882a593Smuzhiyun 			     "Inode %llu, attempted to remove extent block "
2585*4882a593Smuzhiyun 			     "%llu with %u records\n",
2586*4882a593Smuzhiyun 			     (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2587*4882a593Smuzhiyun 			     (unsigned long long)le64_to_cpu(eb->h_blkno),
2588*4882a593Smuzhiyun 			     le16_to_cpu(el->l_next_free_rec));
2589*4882a593Smuzhiyun 
2590*4882a593Smuzhiyun 			ocfs2_journal_dirty(handle, bh);
2591*4882a593Smuzhiyun 			ocfs2_remove_from_cache(et->et_ci, bh);
2592*4882a593Smuzhiyun 			continue;
2593*4882a593Smuzhiyun 		}
2594*4882a593Smuzhiyun 
2595*4882a593Smuzhiyun 		el->l_next_free_rec = 0;
2596*4882a593Smuzhiyun 		memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2597*4882a593Smuzhiyun 
2598*4882a593Smuzhiyun 		ocfs2_journal_dirty(handle, bh);
2599*4882a593Smuzhiyun 
2600*4882a593Smuzhiyun 		ret = ocfs2_cache_extent_block_free(dealloc, eb);
2601*4882a593Smuzhiyun 		if (ret)
2602*4882a593Smuzhiyun 			mlog_errno(ret);
2603*4882a593Smuzhiyun 
2604*4882a593Smuzhiyun 		ocfs2_remove_from_cache(et->et_ci, bh);
2605*4882a593Smuzhiyun 	}
2606*4882a593Smuzhiyun }
2607*4882a593Smuzhiyun 
ocfs2_unlink_subtree(handle_t * handle,struct ocfs2_extent_tree * et,struct ocfs2_path * left_path,struct ocfs2_path * right_path,int subtree_index,struct ocfs2_cached_dealloc_ctxt * dealloc)2608*4882a593Smuzhiyun static void ocfs2_unlink_subtree(handle_t *handle,
2609*4882a593Smuzhiyun 				 struct ocfs2_extent_tree *et,
2610*4882a593Smuzhiyun 				 struct ocfs2_path *left_path,
2611*4882a593Smuzhiyun 				 struct ocfs2_path *right_path,
2612*4882a593Smuzhiyun 				 int subtree_index,
2613*4882a593Smuzhiyun 				 struct ocfs2_cached_dealloc_ctxt *dealloc)
2614*4882a593Smuzhiyun {
2615*4882a593Smuzhiyun 	int i;
2616*4882a593Smuzhiyun 	struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2617*4882a593Smuzhiyun 	struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2618*4882a593Smuzhiyun 	struct ocfs2_extent_block *eb;
2619*4882a593Smuzhiyun 
2620*4882a593Smuzhiyun 	eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2621*4882a593Smuzhiyun 
2622*4882a593Smuzhiyun 	for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2623*4882a593Smuzhiyun 		if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2624*4882a593Smuzhiyun 			break;
2625*4882a593Smuzhiyun 
2626*4882a593Smuzhiyun 	BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2627*4882a593Smuzhiyun 
2628*4882a593Smuzhiyun 	memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2629*4882a593Smuzhiyun 	le16_add_cpu(&root_el->l_next_free_rec, -1);
2630*4882a593Smuzhiyun 
2631*4882a593Smuzhiyun 	eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2632*4882a593Smuzhiyun 	eb->h_next_leaf_blk = 0;
2633*4882a593Smuzhiyun 
2634*4882a593Smuzhiyun 	ocfs2_journal_dirty(handle, root_bh);
2635*4882a593Smuzhiyun 	ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2636*4882a593Smuzhiyun 
2637*4882a593Smuzhiyun 	ocfs2_unlink_path(handle, et, dealloc, right_path,
2638*4882a593Smuzhiyun 			  subtree_index + 1);
2639*4882a593Smuzhiyun }
2640*4882a593Smuzhiyun 
ocfs2_rotate_subtree_left(handle_t * handle,struct ocfs2_extent_tree * et,struct ocfs2_path * left_path,struct ocfs2_path * right_path,int subtree_index,struct ocfs2_cached_dealloc_ctxt * dealloc,int * deleted)2641*4882a593Smuzhiyun static int ocfs2_rotate_subtree_left(handle_t *handle,
2642*4882a593Smuzhiyun 				     struct ocfs2_extent_tree *et,
2643*4882a593Smuzhiyun 				     struct ocfs2_path *left_path,
2644*4882a593Smuzhiyun 				     struct ocfs2_path *right_path,
2645*4882a593Smuzhiyun 				     int subtree_index,
2646*4882a593Smuzhiyun 				     struct ocfs2_cached_dealloc_ctxt *dealloc,
2647*4882a593Smuzhiyun 				     int *deleted)
2648*4882a593Smuzhiyun {
2649*4882a593Smuzhiyun 	int ret, i, del_right_subtree = 0, right_has_empty = 0;
2650*4882a593Smuzhiyun 	struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
2651*4882a593Smuzhiyun 	struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2652*4882a593Smuzhiyun 	struct ocfs2_extent_block *eb;
2653*4882a593Smuzhiyun 
2654*4882a593Smuzhiyun 	*deleted = 0;
2655*4882a593Smuzhiyun 
2656*4882a593Smuzhiyun 	right_leaf_el = path_leaf_el(right_path);
2657*4882a593Smuzhiyun 	left_leaf_el = path_leaf_el(left_path);
2658*4882a593Smuzhiyun 	root_bh = left_path->p_node[subtree_index].bh;
2659*4882a593Smuzhiyun 	BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2660*4882a593Smuzhiyun 
2661*4882a593Smuzhiyun 	if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2662*4882a593Smuzhiyun 		return 0;
2663*4882a593Smuzhiyun 
2664*4882a593Smuzhiyun 	eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2665*4882a593Smuzhiyun 	if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2666*4882a593Smuzhiyun 		/*
2667*4882a593Smuzhiyun 		 * It's legal for us to proceed if the right leaf is
2668*4882a593Smuzhiyun 		 * the rightmost one and it has an empty extent. There
2669*4882a593Smuzhiyun 		 * are two cases to handle - whether the leaf will be
2670*4882a593Smuzhiyun 		 * empty after removal or not. If the leaf isn't empty
2671*4882a593Smuzhiyun 		 * then just remove the empty extent up front. The
2672*4882a593Smuzhiyun 		 * next block will handle empty leaves by flagging
2673*4882a593Smuzhiyun 		 * them for unlink.
2674*4882a593Smuzhiyun 		 *
2675*4882a593Smuzhiyun 		 * Non rightmost leaves will throw -EAGAIN and the
2676*4882a593Smuzhiyun 		 * caller can manually move the subtree and retry.
2677*4882a593Smuzhiyun 		 */
2678*4882a593Smuzhiyun 
2679*4882a593Smuzhiyun 		if (eb->h_next_leaf_blk != 0ULL)
2680*4882a593Smuzhiyun 			return -EAGAIN;
2681*4882a593Smuzhiyun 
2682*4882a593Smuzhiyun 		if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
2683*4882a593Smuzhiyun 			ret = ocfs2_journal_access_eb(handle, et->et_ci,
2684*4882a593Smuzhiyun 						      path_leaf_bh(right_path),
2685*4882a593Smuzhiyun 						      OCFS2_JOURNAL_ACCESS_WRITE);
2686*4882a593Smuzhiyun 			if (ret) {
2687*4882a593Smuzhiyun 				mlog_errno(ret);
2688*4882a593Smuzhiyun 				goto out;
2689*4882a593Smuzhiyun 			}
2690*4882a593Smuzhiyun 
2691*4882a593Smuzhiyun 			ocfs2_remove_empty_extent(right_leaf_el);
2692*4882a593Smuzhiyun 		} else
2693*4882a593Smuzhiyun 			right_has_empty = 1;
2694*4882a593Smuzhiyun 	}
2695*4882a593Smuzhiyun 
2696*4882a593Smuzhiyun 	if (eb->h_next_leaf_blk == 0ULL &&
2697*4882a593Smuzhiyun 	    le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2698*4882a593Smuzhiyun 		/*
2699*4882a593Smuzhiyun 		 * We have to update i_last_eb_blk during the meta
2700*4882a593Smuzhiyun 		 * data delete.
2701*4882a593Smuzhiyun 		 */
2702*4882a593Smuzhiyun 		ret = ocfs2_et_root_journal_access(handle, et,
2703*4882a593Smuzhiyun 						   OCFS2_JOURNAL_ACCESS_WRITE);
2704*4882a593Smuzhiyun 		if (ret) {
2705*4882a593Smuzhiyun 			mlog_errno(ret);
2706*4882a593Smuzhiyun 			goto out;
2707*4882a593Smuzhiyun 		}
2708*4882a593Smuzhiyun 
2709*4882a593Smuzhiyun 		del_right_subtree = 1;
2710*4882a593Smuzhiyun 	}
2711*4882a593Smuzhiyun 
2712*4882a593Smuzhiyun 	/*
2713*4882a593Smuzhiyun 	 * Getting here with an empty extent in the right path implies
2714*4882a593Smuzhiyun 	 * that it's the rightmost path and will be deleted.
2715*4882a593Smuzhiyun 	 */
2716*4882a593Smuzhiyun 	BUG_ON(right_has_empty && !del_right_subtree);
2717*4882a593Smuzhiyun 
2718*4882a593Smuzhiyun 	ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
2719*4882a593Smuzhiyun 					   subtree_index);
2720*4882a593Smuzhiyun 	if (ret) {
2721*4882a593Smuzhiyun 		mlog_errno(ret);
2722*4882a593Smuzhiyun 		goto out;
2723*4882a593Smuzhiyun 	}
2724*4882a593Smuzhiyun 
2725*4882a593Smuzhiyun 	for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2726*4882a593Smuzhiyun 		ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2727*4882a593Smuzhiyun 						   right_path, i);
2728*4882a593Smuzhiyun 		if (ret) {
2729*4882a593Smuzhiyun 			mlog_errno(ret);
2730*4882a593Smuzhiyun 			goto out;
2731*4882a593Smuzhiyun 		}
2732*4882a593Smuzhiyun 
2733*4882a593Smuzhiyun 		ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2734*4882a593Smuzhiyun 						   left_path, i);
2735*4882a593Smuzhiyun 		if (ret) {
2736*4882a593Smuzhiyun 			mlog_errno(ret);
2737*4882a593Smuzhiyun 			goto out;
2738*4882a593Smuzhiyun 		}
2739*4882a593Smuzhiyun 	}
2740*4882a593Smuzhiyun 
2741*4882a593Smuzhiyun 	if (!right_has_empty) {
2742*4882a593Smuzhiyun 		/*
2743*4882a593Smuzhiyun 		 * Only do this if we're moving a real
2744*4882a593Smuzhiyun 		 * record. Otherwise, the action is delayed until
2745*4882a593Smuzhiyun 		 * after removal of the right path in which case we
2746*4882a593Smuzhiyun 		 * can do a simple shift to remove the empty extent.
2747*4882a593Smuzhiyun 		 */
2748*4882a593Smuzhiyun 		ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2749*4882a593Smuzhiyun 		memset(&right_leaf_el->l_recs[0], 0,
2750*4882a593Smuzhiyun 		       sizeof(struct ocfs2_extent_rec));
2751*4882a593Smuzhiyun 	}
2752*4882a593Smuzhiyun 	if (eb->h_next_leaf_blk == 0ULL) {
2753*4882a593Smuzhiyun 		/*
2754*4882a593Smuzhiyun 		 * Move recs over to get rid of empty extent, decrease
2755*4882a593Smuzhiyun 		 * next_free. This is allowed to remove the last
2756*4882a593Smuzhiyun 		 * extent in our leaf (setting l_next_free_rec to
2757*4882a593Smuzhiyun 		 * zero) - the delete code below won't care.
2758*4882a593Smuzhiyun 		 */
2759*4882a593Smuzhiyun 		ocfs2_remove_empty_extent(right_leaf_el);
2760*4882a593Smuzhiyun 	}
2761*4882a593Smuzhiyun 
2762*4882a593Smuzhiyun 	ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2763*4882a593Smuzhiyun 	ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2764*4882a593Smuzhiyun 
2765*4882a593Smuzhiyun 	if (del_right_subtree) {
2766*4882a593Smuzhiyun 		ocfs2_unlink_subtree(handle, et, left_path, right_path,
2767*4882a593Smuzhiyun 				     subtree_index, dealloc);
2768*4882a593Smuzhiyun 		ret = ocfs2_update_edge_lengths(handle, et, left_path);
2769*4882a593Smuzhiyun 		if (ret) {
2770*4882a593Smuzhiyun 			mlog_errno(ret);
2771*4882a593Smuzhiyun 			goto out;
2772*4882a593Smuzhiyun 		}
2773*4882a593Smuzhiyun 
2774*4882a593Smuzhiyun 		eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2775*4882a593Smuzhiyun 		ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
2776*4882a593Smuzhiyun 
2777*4882a593Smuzhiyun 		/*
2778*4882a593Smuzhiyun 		 * Removal of the extent in the left leaf was skipped
2779*4882a593Smuzhiyun 		 * above so we could delete the right path
2780*4882a593Smuzhiyun 		 * 1st.
2781*4882a593Smuzhiyun 		 */
2782*4882a593Smuzhiyun 		if (right_has_empty)
2783*4882a593Smuzhiyun 			ocfs2_remove_empty_extent(left_leaf_el);
2784*4882a593Smuzhiyun 
2785*4882a593Smuzhiyun 		ocfs2_journal_dirty(handle, et_root_bh);
2786*4882a593Smuzhiyun 
2787*4882a593Smuzhiyun 		*deleted = 1;
2788*4882a593Smuzhiyun 	} else
2789*4882a593Smuzhiyun 		ocfs2_complete_edge_insert(handle, left_path, right_path,
2790*4882a593Smuzhiyun 					   subtree_index);
2791*4882a593Smuzhiyun 
2792*4882a593Smuzhiyun out:
2793*4882a593Smuzhiyun 	return ret;
2794*4882a593Smuzhiyun }
2795*4882a593Smuzhiyun 
2796*4882a593Smuzhiyun /*
2797*4882a593Smuzhiyun  * Given a full path, determine what cpos value would return us a path
2798*4882a593Smuzhiyun  * containing the leaf immediately to the right of the current one.
2799*4882a593Smuzhiyun  *
2800*4882a593Smuzhiyun  * Will return zero if the path passed in is already the rightmost path.
2801*4882a593Smuzhiyun  *
2802*4882a593Smuzhiyun  * This looks similar, but is subtly different to
2803*4882a593Smuzhiyun  * ocfs2_find_cpos_for_left_leaf().
2804*4882a593Smuzhiyun  */
ocfs2_find_cpos_for_right_leaf(struct super_block * sb,struct ocfs2_path * path,u32 * cpos)2805*4882a593Smuzhiyun int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2806*4882a593Smuzhiyun 				   struct ocfs2_path *path, u32 *cpos)
2807*4882a593Smuzhiyun {
2808*4882a593Smuzhiyun 	int i, j, ret = 0;
2809*4882a593Smuzhiyun 	u64 blkno;
2810*4882a593Smuzhiyun 	struct ocfs2_extent_list *el;
2811*4882a593Smuzhiyun 
2812*4882a593Smuzhiyun 	*cpos = 0;
2813*4882a593Smuzhiyun 
2814*4882a593Smuzhiyun 	if (path->p_tree_depth == 0)
2815*4882a593Smuzhiyun 		return 0;
2816*4882a593Smuzhiyun 
2817*4882a593Smuzhiyun 	blkno = path_leaf_bh(path)->b_blocknr;
2818*4882a593Smuzhiyun 
2819*4882a593Smuzhiyun 	/* Start at the tree node just above the leaf and work our way up. */
2820*4882a593Smuzhiyun 	i = path->p_tree_depth - 1;
2821*4882a593Smuzhiyun 	while (i >= 0) {
2822*4882a593Smuzhiyun 		int next_free;
2823*4882a593Smuzhiyun 
2824*4882a593Smuzhiyun 		el = path->p_node[i].el;
2825*4882a593Smuzhiyun 
2826*4882a593Smuzhiyun 		/*
2827*4882a593Smuzhiyun 		 * Find the extent record just after the one in our
2828*4882a593Smuzhiyun 		 * path.
2829*4882a593Smuzhiyun 		 */
2830*4882a593Smuzhiyun 		next_free = le16_to_cpu(el->l_next_free_rec);
2831*4882a593Smuzhiyun 		for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2832*4882a593Smuzhiyun 			if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2833*4882a593Smuzhiyun 				if (j == (next_free - 1)) {
2834*4882a593Smuzhiyun 					if (i == 0) {
2835*4882a593Smuzhiyun 						/*
2836*4882a593Smuzhiyun 						 * We've determined that the
2837*4882a593Smuzhiyun 						 * path specified is already
2838*4882a593Smuzhiyun 						 * the rightmost one - return a
2839*4882a593Smuzhiyun 						 * cpos of zero.
2840*4882a593Smuzhiyun 						 */
2841*4882a593Smuzhiyun 						goto out;
2842*4882a593Smuzhiyun 					}
2843*4882a593Smuzhiyun 					/*
2844*4882a593Smuzhiyun 					 * The rightmost record points to our
2845*4882a593Smuzhiyun 					 * leaf - we need to travel up the
2846*4882a593Smuzhiyun 					 * tree one level.
2847*4882a593Smuzhiyun 					 */
2848*4882a593Smuzhiyun 					goto next_node;
2849*4882a593Smuzhiyun 				}
2850*4882a593Smuzhiyun 
2851*4882a593Smuzhiyun 				*cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2852*4882a593Smuzhiyun 				goto out;
2853*4882a593Smuzhiyun 			}
2854*4882a593Smuzhiyun 		}
2855*4882a593Smuzhiyun 
2856*4882a593Smuzhiyun 		/*
2857*4882a593Smuzhiyun 		 * If we got here, we never found a valid node where
2858*4882a593Smuzhiyun 		 * the tree indicated one should be.
2859*4882a593Smuzhiyun 		 */
2860*4882a593Smuzhiyun 		ocfs2_error(sb, "Invalid extent tree at extent block %llu\n",
2861*4882a593Smuzhiyun 			    (unsigned long long)blkno);
2862*4882a593Smuzhiyun 		ret = -EROFS;
2863*4882a593Smuzhiyun 		goto out;
2864*4882a593Smuzhiyun 
2865*4882a593Smuzhiyun next_node:
2866*4882a593Smuzhiyun 		blkno = path->p_node[i].bh->b_blocknr;
2867*4882a593Smuzhiyun 		i--;
2868*4882a593Smuzhiyun 	}
2869*4882a593Smuzhiyun 
2870*4882a593Smuzhiyun out:
2871*4882a593Smuzhiyun 	return ret;
2872*4882a593Smuzhiyun }
2873*4882a593Smuzhiyun 
ocfs2_rotate_rightmost_leaf_left(handle_t * handle,struct ocfs2_extent_tree * et,struct ocfs2_path * path)2874*4882a593Smuzhiyun static int ocfs2_rotate_rightmost_leaf_left(handle_t *handle,
2875*4882a593Smuzhiyun 					    struct ocfs2_extent_tree *et,
2876*4882a593Smuzhiyun 					    struct ocfs2_path *path)
2877*4882a593Smuzhiyun {
2878*4882a593Smuzhiyun 	int ret;
2879*4882a593Smuzhiyun 	struct buffer_head *bh = path_leaf_bh(path);
2880*4882a593Smuzhiyun 	struct ocfs2_extent_list *el = path_leaf_el(path);
2881*4882a593Smuzhiyun 
2882*4882a593Smuzhiyun 	if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2883*4882a593Smuzhiyun 		return 0;
2884*4882a593Smuzhiyun 
2885*4882a593Smuzhiyun 	ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
2886*4882a593Smuzhiyun 					   path_num_items(path) - 1);
2887*4882a593Smuzhiyun 	if (ret) {
2888*4882a593Smuzhiyun 		mlog_errno(ret);
2889*4882a593Smuzhiyun 		goto out;
2890*4882a593Smuzhiyun 	}
2891*4882a593Smuzhiyun 
2892*4882a593Smuzhiyun 	ocfs2_remove_empty_extent(el);
2893*4882a593Smuzhiyun 	ocfs2_journal_dirty(handle, bh);
2894*4882a593Smuzhiyun 
2895*4882a593Smuzhiyun out:
2896*4882a593Smuzhiyun 	return ret;
2897*4882a593Smuzhiyun }
2898*4882a593Smuzhiyun 
__ocfs2_rotate_tree_left(handle_t * handle,struct ocfs2_extent_tree * et,int orig_credits,struct ocfs2_path * path,struct ocfs2_cached_dealloc_ctxt * dealloc,struct ocfs2_path ** empty_extent_path)2899*4882a593Smuzhiyun static int __ocfs2_rotate_tree_left(handle_t *handle,
2900*4882a593Smuzhiyun 				    struct ocfs2_extent_tree *et,
2901*4882a593Smuzhiyun 				    int orig_credits,
2902*4882a593Smuzhiyun 				    struct ocfs2_path *path,
2903*4882a593Smuzhiyun 				    struct ocfs2_cached_dealloc_ctxt *dealloc,
2904*4882a593Smuzhiyun 				    struct ocfs2_path **empty_extent_path)
2905*4882a593Smuzhiyun {
2906*4882a593Smuzhiyun 	int ret, subtree_root, deleted;
2907*4882a593Smuzhiyun 	u32 right_cpos;
2908*4882a593Smuzhiyun 	struct ocfs2_path *left_path = NULL;
2909*4882a593Smuzhiyun 	struct ocfs2_path *right_path = NULL;
2910*4882a593Smuzhiyun 	struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
2911*4882a593Smuzhiyun 
2912*4882a593Smuzhiyun 	if (!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])))
2913*4882a593Smuzhiyun 		return 0;
2914*4882a593Smuzhiyun 
2915*4882a593Smuzhiyun 	*empty_extent_path = NULL;
2916*4882a593Smuzhiyun 
2917*4882a593Smuzhiyun 	ret = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
2918*4882a593Smuzhiyun 	if (ret) {
2919*4882a593Smuzhiyun 		mlog_errno(ret);
2920*4882a593Smuzhiyun 		goto out;
2921*4882a593Smuzhiyun 	}
2922*4882a593Smuzhiyun 
2923*4882a593Smuzhiyun 	left_path = ocfs2_new_path_from_path(path);
2924*4882a593Smuzhiyun 	if (!left_path) {
2925*4882a593Smuzhiyun 		ret = -ENOMEM;
2926*4882a593Smuzhiyun 		mlog_errno(ret);
2927*4882a593Smuzhiyun 		goto out;
2928*4882a593Smuzhiyun 	}
2929*4882a593Smuzhiyun 
2930*4882a593Smuzhiyun 	ocfs2_cp_path(left_path, path);
2931*4882a593Smuzhiyun 
2932*4882a593Smuzhiyun 	right_path = ocfs2_new_path_from_path(path);
2933*4882a593Smuzhiyun 	if (!right_path) {
2934*4882a593Smuzhiyun 		ret = -ENOMEM;
2935*4882a593Smuzhiyun 		mlog_errno(ret);
2936*4882a593Smuzhiyun 		goto out;
2937*4882a593Smuzhiyun 	}
2938*4882a593Smuzhiyun 
2939*4882a593Smuzhiyun 	while (right_cpos) {
2940*4882a593Smuzhiyun 		ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
2941*4882a593Smuzhiyun 		if (ret) {
2942*4882a593Smuzhiyun 			mlog_errno(ret);
2943*4882a593Smuzhiyun 			goto out;
2944*4882a593Smuzhiyun 		}
2945*4882a593Smuzhiyun 
2946*4882a593Smuzhiyun 		subtree_root = ocfs2_find_subtree_root(et, left_path,
2947*4882a593Smuzhiyun 						       right_path);
2948*4882a593Smuzhiyun 
2949*4882a593Smuzhiyun 		trace_ocfs2_rotate_subtree(subtree_root,
2950*4882a593Smuzhiyun 		     (unsigned long long)
2951*4882a593Smuzhiyun 		     right_path->p_node[subtree_root].bh->b_blocknr,
2952*4882a593Smuzhiyun 		     right_path->p_tree_depth);
2953*4882a593Smuzhiyun 
2954*4882a593Smuzhiyun 		ret = ocfs2_extend_rotate_transaction(handle, 0,
2955*4882a593Smuzhiyun 						      orig_credits, left_path);
2956*4882a593Smuzhiyun 		if (ret) {
2957*4882a593Smuzhiyun 			mlog_errno(ret);
2958*4882a593Smuzhiyun 			goto out;
2959*4882a593Smuzhiyun 		}
2960*4882a593Smuzhiyun 
2961*4882a593Smuzhiyun 		/*
2962*4882a593Smuzhiyun 		 * Caller might still want to make changes to the
2963*4882a593Smuzhiyun 		 * tree root, so re-add it to the journal here.
2964*4882a593Smuzhiyun 		 */
2965*4882a593Smuzhiyun 		ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2966*4882a593Smuzhiyun 						   left_path, 0);
2967*4882a593Smuzhiyun 		if (ret) {
2968*4882a593Smuzhiyun 			mlog_errno(ret);
2969*4882a593Smuzhiyun 			goto out;
2970*4882a593Smuzhiyun 		}
2971*4882a593Smuzhiyun 
2972*4882a593Smuzhiyun 		ret = ocfs2_rotate_subtree_left(handle, et, left_path,
2973*4882a593Smuzhiyun 						right_path, subtree_root,
2974*4882a593Smuzhiyun 						dealloc, &deleted);
2975*4882a593Smuzhiyun 		if (ret == -EAGAIN) {
2976*4882a593Smuzhiyun 			/*
2977*4882a593Smuzhiyun 			 * The rotation has to temporarily stop due to
2978*4882a593Smuzhiyun 			 * the right subtree having an empty
2979*4882a593Smuzhiyun 			 * extent. Pass it back to the caller for a
2980*4882a593Smuzhiyun 			 * fixup.
2981*4882a593Smuzhiyun 			 */
2982*4882a593Smuzhiyun 			*empty_extent_path = right_path;
2983*4882a593Smuzhiyun 			right_path = NULL;
2984*4882a593Smuzhiyun 			goto out;
2985*4882a593Smuzhiyun 		}
2986*4882a593Smuzhiyun 		if (ret) {
2987*4882a593Smuzhiyun 			mlog_errno(ret);
2988*4882a593Smuzhiyun 			goto out;
2989*4882a593Smuzhiyun 		}
2990*4882a593Smuzhiyun 
2991*4882a593Smuzhiyun 		/*
2992*4882a593Smuzhiyun 		 * The subtree rotate might have removed records on
2993*4882a593Smuzhiyun 		 * the rightmost edge. If so, then rotation is
2994*4882a593Smuzhiyun 		 * complete.
2995*4882a593Smuzhiyun 		 */
2996*4882a593Smuzhiyun 		if (deleted)
2997*4882a593Smuzhiyun 			break;
2998*4882a593Smuzhiyun 
2999*4882a593Smuzhiyun 		ocfs2_mv_path(left_path, right_path);
3000*4882a593Smuzhiyun 
3001*4882a593Smuzhiyun 		ret = ocfs2_find_cpos_for_right_leaf(sb, left_path,
3002*4882a593Smuzhiyun 						     &right_cpos);
3003*4882a593Smuzhiyun 		if (ret) {
3004*4882a593Smuzhiyun 			mlog_errno(ret);
3005*4882a593Smuzhiyun 			goto out;
3006*4882a593Smuzhiyun 		}
3007*4882a593Smuzhiyun 	}
3008*4882a593Smuzhiyun 
3009*4882a593Smuzhiyun out:
3010*4882a593Smuzhiyun 	ocfs2_free_path(right_path);
3011*4882a593Smuzhiyun 	ocfs2_free_path(left_path);
3012*4882a593Smuzhiyun 
3013*4882a593Smuzhiyun 	return ret;
3014*4882a593Smuzhiyun }
3015*4882a593Smuzhiyun 
ocfs2_remove_rightmost_path(handle_t * handle,struct ocfs2_extent_tree * et,struct ocfs2_path * path,struct ocfs2_cached_dealloc_ctxt * dealloc)3016*4882a593Smuzhiyun static int ocfs2_remove_rightmost_path(handle_t *handle,
3017*4882a593Smuzhiyun 				struct ocfs2_extent_tree *et,
3018*4882a593Smuzhiyun 				struct ocfs2_path *path,
3019*4882a593Smuzhiyun 				struct ocfs2_cached_dealloc_ctxt *dealloc)
3020*4882a593Smuzhiyun {
3021*4882a593Smuzhiyun 	int ret, subtree_index;
3022*4882a593Smuzhiyun 	u32 cpos;
3023*4882a593Smuzhiyun 	struct ocfs2_path *left_path = NULL;
3024*4882a593Smuzhiyun 	struct ocfs2_extent_block *eb;
3025*4882a593Smuzhiyun 	struct ocfs2_extent_list *el;
3026*4882a593Smuzhiyun 
3027*4882a593Smuzhiyun 	ret = ocfs2_et_sanity_check(et);
3028*4882a593Smuzhiyun 	if (ret)
3029*4882a593Smuzhiyun 		goto out;
3030*4882a593Smuzhiyun 
3031*4882a593Smuzhiyun 	ret = ocfs2_journal_access_path(et->et_ci, handle, path);
3032*4882a593Smuzhiyun 	if (ret) {
3033*4882a593Smuzhiyun 		mlog_errno(ret);
3034*4882a593Smuzhiyun 		goto out;
3035*4882a593Smuzhiyun 	}
3036*4882a593Smuzhiyun 
3037*4882a593Smuzhiyun 	ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3038*4882a593Smuzhiyun 					    path, &cpos);
3039*4882a593Smuzhiyun 	if (ret) {
3040*4882a593Smuzhiyun 		mlog_errno(ret);
3041*4882a593Smuzhiyun 		goto out;
3042*4882a593Smuzhiyun 	}
3043*4882a593Smuzhiyun 
3044*4882a593Smuzhiyun 	if (cpos) {
3045*4882a593Smuzhiyun 		/*
3046*4882a593Smuzhiyun 		 * We have a path to the left of this one - it needs
3047*4882a593Smuzhiyun 		 * an update too.
3048*4882a593Smuzhiyun 		 */
3049*4882a593Smuzhiyun 		left_path = ocfs2_new_path_from_path(path);
3050*4882a593Smuzhiyun 		if (!left_path) {
3051*4882a593Smuzhiyun 			ret = -ENOMEM;
3052*4882a593Smuzhiyun 			mlog_errno(ret);
3053*4882a593Smuzhiyun 			goto out;
3054*4882a593Smuzhiyun 		}
3055*4882a593Smuzhiyun 
3056*4882a593Smuzhiyun 		ret = ocfs2_find_path(et->et_ci, left_path, cpos);
3057*4882a593Smuzhiyun 		if (ret) {
3058*4882a593Smuzhiyun 			mlog_errno(ret);
3059*4882a593Smuzhiyun 			goto out;
3060*4882a593Smuzhiyun 		}
3061*4882a593Smuzhiyun 
3062*4882a593Smuzhiyun 		ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
3063*4882a593Smuzhiyun 		if (ret) {
3064*4882a593Smuzhiyun 			mlog_errno(ret);
3065*4882a593Smuzhiyun 			goto out;
3066*4882a593Smuzhiyun 		}
3067*4882a593Smuzhiyun 
3068*4882a593Smuzhiyun 		subtree_index = ocfs2_find_subtree_root(et, left_path, path);
3069*4882a593Smuzhiyun 
3070*4882a593Smuzhiyun 		ocfs2_unlink_subtree(handle, et, left_path, path,
3071*4882a593Smuzhiyun 				     subtree_index, dealloc);
3072*4882a593Smuzhiyun 		ret = ocfs2_update_edge_lengths(handle, et, left_path);
3073*4882a593Smuzhiyun 		if (ret) {
3074*4882a593Smuzhiyun 			mlog_errno(ret);
3075*4882a593Smuzhiyun 			goto out;
3076*4882a593Smuzhiyun 		}
3077*4882a593Smuzhiyun 
3078*4882a593Smuzhiyun 		eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
3079*4882a593Smuzhiyun 		ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
3080*4882a593Smuzhiyun 	} else {
3081*4882a593Smuzhiyun 		/*
3082*4882a593Smuzhiyun 		 * 'path' is also the leftmost path which
3083*4882a593Smuzhiyun 		 * means it must be the only one. This gets
3084*4882a593Smuzhiyun 		 * handled differently because we want to
3085*4882a593Smuzhiyun 		 * revert the root back to having extents
3086*4882a593Smuzhiyun 		 * in-line.
3087*4882a593Smuzhiyun 		 */
3088*4882a593Smuzhiyun 		ocfs2_unlink_path(handle, et, dealloc, path, 1);
3089*4882a593Smuzhiyun 
3090*4882a593Smuzhiyun 		el = et->et_root_el;
3091*4882a593Smuzhiyun 		el->l_tree_depth = 0;
3092*4882a593Smuzhiyun 		el->l_next_free_rec = 0;
3093*4882a593Smuzhiyun 		memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3094*4882a593Smuzhiyun 
3095*4882a593Smuzhiyun 		ocfs2_et_set_last_eb_blk(et, 0);
3096*4882a593Smuzhiyun 	}
3097*4882a593Smuzhiyun 
3098*4882a593Smuzhiyun 	ocfs2_journal_dirty(handle, path_root_bh(path));
3099*4882a593Smuzhiyun 
3100*4882a593Smuzhiyun out:
3101*4882a593Smuzhiyun 	ocfs2_free_path(left_path);
3102*4882a593Smuzhiyun 	return ret;
3103*4882a593Smuzhiyun }
3104*4882a593Smuzhiyun 
ocfs2_remove_rightmost_empty_extent(struct ocfs2_super * osb,struct ocfs2_extent_tree * et,struct ocfs2_path * path,struct ocfs2_cached_dealloc_ctxt * dealloc)3105*4882a593Smuzhiyun static int ocfs2_remove_rightmost_empty_extent(struct ocfs2_super *osb,
3106*4882a593Smuzhiyun 				struct ocfs2_extent_tree *et,
3107*4882a593Smuzhiyun 				struct ocfs2_path *path,
3108*4882a593Smuzhiyun 				struct ocfs2_cached_dealloc_ctxt *dealloc)
3109*4882a593Smuzhiyun {
3110*4882a593Smuzhiyun 	handle_t *handle;
3111*4882a593Smuzhiyun 	int ret;
3112*4882a593Smuzhiyun 	int credits = path->p_tree_depth * 2 + 1;
3113*4882a593Smuzhiyun 
3114*4882a593Smuzhiyun 	handle = ocfs2_start_trans(osb, credits);
3115*4882a593Smuzhiyun 	if (IS_ERR(handle)) {
3116*4882a593Smuzhiyun 		ret = PTR_ERR(handle);
3117*4882a593Smuzhiyun 		mlog_errno(ret);
3118*4882a593Smuzhiyun 		return ret;
3119*4882a593Smuzhiyun 	}
3120*4882a593Smuzhiyun 
3121*4882a593Smuzhiyun 	ret = ocfs2_remove_rightmost_path(handle, et, path, dealloc);
3122*4882a593Smuzhiyun 	if (ret)
3123*4882a593Smuzhiyun 		mlog_errno(ret);
3124*4882a593Smuzhiyun 
3125*4882a593Smuzhiyun 	ocfs2_commit_trans(osb, handle);
3126*4882a593Smuzhiyun 	return ret;
3127*4882a593Smuzhiyun }
3128*4882a593Smuzhiyun 
3129*4882a593Smuzhiyun /*
3130*4882a593Smuzhiyun  * Left rotation of btree records.
3131*4882a593Smuzhiyun  *
3132*4882a593Smuzhiyun  * In many ways, this is (unsurprisingly) the opposite of right
3133*4882a593Smuzhiyun  * rotation. We start at some non-rightmost path containing an empty
3134*4882a593Smuzhiyun  * extent in the leaf block. The code works its way to the rightmost
3135*4882a593Smuzhiyun  * path by rotating records to the left in every subtree.
3136*4882a593Smuzhiyun  *
3137*4882a593Smuzhiyun  * This is used by any code which reduces the number of extent records
3138*4882a593Smuzhiyun  * in a leaf. After removal, an empty record should be placed in the
3139*4882a593Smuzhiyun  * leftmost list position.
3140*4882a593Smuzhiyun  *
3141*4882a593Smuzhiyun  * This won't handle a length update of the rightmost path records if
3142*4882a593Smuzhiyun  * the rightmost tree leaf record is removed so the caller is
3143*4882a593Smuzhiyun  * responsible for detecting and correcting that.
3144*4882a593Smuzhiyun  */
ocfs2_rotate_tree_left(handle_t * handle,struct ocfs2_extent_tree * et,struct ocfs2_path * path,struct ocfs2_cached_dealloc_ctxt * dealloc)3145*4882a593Smuzhiyun static int ocfs2_rotate_tree_left(handle_t *handle,
3146*4882a593Smuzhiyun 				  struct ocfs2_extent_tree *et,
3147*4882a593Smuzhiyun 				  struct ocfs2_path *path,
3148*4882a593Smuzhiyun 				  struct ocfs2_cached_dealloc_ctxt *dealloc)
3149*4882a593Smuzhiyun {
3150*4882a593Smuzhiyun 	int ret, orig_credits = jbd2_handle_buffer_credits(handle);
3151*4882a593Smuzhiyun 	struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
3152*4882a593Smuzhiyun 	struct ocfs2_extent_block *eb;
3153*4882a593Smuzhiyun 	struct ocfs2_extent_list *el;
3154*4882a593Smuzhiyun 
3155*4882a593Smuzhiyun 	el = path_leaf_el(path);
3156*4882a593Smuzhiyun 	if (!ocfs2_is_empty_extent(&el->l_recs[0]))
3157*4882a593Smuzhiyun 		return 0;
3158*4882a593Smuzhiyun 
3159*4882a593Smuzhiyun 	if (path->p_tree_depth == 0) {
3160*4882a593Smuzhiyun rightmost_no_delete:
3161*4882a593Smuzhiyun 		/*
3162*4882a593Smuzhiyun 		 * Inline extents. This is trivially handled, so do
3163*4882a593Smuzhiyun 		 * it up front.
3164*4882a593Smuzhiyun 		 */
3165*4882a593Smuzhiyun 		ret = ocfs2_rotate_rightmost_leaf_left(handle, et, path);
3166*4882a593Smuzhiyun 		if (ret)
3167*4882a593Smuzhiyun 			mlog_errno(ret);
3168*4882a593Smuzhiyun 		goto out;
3169*4882a593Smuzhiyun 	}
3170*4882a593Smuzhiyun 
3171*4882a593Smuzhiyun 	/*
3172*4882a593Smuzhiyun 	 * Handle rightmost branch now. There's several cases:
3173*4882a593Smuzhiyun 	 *  1) simple rotation leaving records in there. That's trivial.
3174*4882a593Smuzhiyun 	 *  2) rotation requiring a branch delete - there's no more
3175*4882a593Smuzhiyun 	 *     records left. Two cases of this:
3176*4882a593Smuzhiyun 	 *     a) There are branches to the left.
3177*4882a593Smuzhiyun 	 *     b) This is also the leftmost (the only) branch.
3178*4882a593Smuzhiyun 	 *
3179*4882a593Smuzhiyun 	 *  1) is handled via ocfs2_rotate_rightmost_leaf_left()
3180*4882a593Smuzhiyun 	 *  2a) we need the left branch so that we can update it with the unlink
3181*4882a593Smuzhiyun 	 *  2b) we need to bring the root back to inline extents.
3182*4882a593Smuzhiyun 	 */
3183*4882a593Smuzhiyun 
3184*4882a593Smuzhiyun 	eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
3185*4882a593Smuzhiyun 	el = &eb->h_list;
3186*4882a593Smuzhiyun 	if (eb->h_next_leaf_blk == 0) {
3187*4882a593Smuzhiyun 		/*
3188*4882a593Smuzhiyun 		 * This gets a bit tricky if we're going to delete the
3189*4882a593Smuzhiyun 		 * rightmost path. Get the other cases out of the way
3190*4882a593Smuzhiyun 		 * 1st.
3191*4882a593Smuzhiyun 		 */
3192*4882a593Smuzhiyun 		if (le16_to_cpu(el->l_next_free_rec) > 1)
3193*4882a593Smuzhiyun 			goto rightmost_no_delete;
3194*4882a593Smuzhiyun 
3195*4882a593Smuzhiyun 		if (le16_to_cpu(el->l_next_free_rec) == 0) {
3196*4882a593Smuzhiyun 			ret = ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
3197*4882a593Smuzhiyun 					"Owner %llu has empty extent block at %llu\n",
3198*4882a593Smuzhiyun 					(unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
3199*4882a593Smuzhiyun 					(unsigned long long)le64_to_cpu(eb->h_blkno));
3200*4882a593Smuzhiyun 			goto out;
3201*4882a593Smuzhiyun 		}
3202*4882a593Smuzhiyun 
3203*4882a593Smuzhiyun 		/*
3204*4882a593Smuzhiyun 		 * XXX: The caller can not trust "path" any more after
3205*4882a593Smuzhiyun 		 * this as it will have been deleted. What do we do?
3206*4882a593Smuzhiyun 		 *
3207*4882a593Smuzhiyun 		 * In theory the rotate-for-merge code will never get
3208*4882a593Smuzhiyun 		 * here because it'll always ask for a rotate in a
3209*4882a593Smuzhiyun 		 * nonempty list.
3210*4882a593Smuzhiyun 		 */
3211*4882a593Smuzhiyun 
3212*4882a593Smuzhiyun 		ret = ocfs2_remove_rightmost_path(handle, et, path,
3213*4882a593Smuzhiyun 						  dealloc);
3214*4882a593Smuzhiyun 		if (ret)
3215*4882a593Smuzhiyun 			mlog_errno(ret);
3216*4882a593Smuzhiyun 		goto out;
3217*4882a593Smuzhiyun 	}
3218*4882a593Smuzhiyun 
3219*4882a593Smuzhiyun 	/*
3220*4882a593Smuzhiyun 	 * Now we can loop, remembering the path we get from -EAGAIN
3221*4882a593Smuzhiyun 	 * and restarting from there.
3222*4882a593Smuzhiyun 	 */
3223*4882a593Smuzhiyun try_rotate:
3224*4882a593Smuzhiyun 	ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, path,
3225*4882a593Smuzhiyun 				       dealloc, &restart_path);
3226*4882a593Smuzhiyun 	if (ret && ret != -EAGAIN) {
3227*4882a593Smuzhiyun 		mlog_errno(ret);
3228*4882a593Smuzhiyun 		goto out;
3229*4882a593Smuzhiyun 	}
3230*4882a593Smuzhiyun 
3231*4882a593Smuzhiyun 	while (ret == -EAGAIN) {
3232*4882a593Smuzhiyun 		tmp_path = restart_path;
3233*4882a593Smuzhiyun 		restart_path = NULL;
3234*4882a593Smuzhiyun 
3235*4882a593Smuzhiyun 		ret = __ocfs2_rotate_tree_left(handle, et, orig_credits,
3236*4882a593Smuzhiyun 					       tmp_path, dealloc,
3237*4882a593Smuzhiyun 					       &restart_path);
3238*4882a593Smuzhiyun 		if (ret && ret != -EAGAIN) {
3239*4882a593Smuzhiyun 			mlog_errno(ret);
3240*4882a593Smuzhiyun 			goto out;
3241*4882a593Smuzhiyun 		}
3242*4882a593Smuzhiyun 
3243*4882a593Smuzhiyun 		ocfs2_free_path(tmp_path);
3244*4882a593Smuzhiyun 		tmp_path = NULL;
3245*4882a593Smuzhiyun 
3246*4882a593Smuzhiyun 		if (ret == 0)
3247*4882a593Smuzhiyun 			goto try_rotate;
3248*4882a593Smuzhiyun 	}
3249*4882a593Smuzhiyun 
3250*4882a593Smuzhiyun out:
3251*4882a593Smuzhiyun 	ocfs2_free_path(tmp_path);
3252*4882a593Smuzhiyun 	ocfs2_free_path(restart_path);
3253*4882a593Smuzhiyun 	return ret;
3254*4882a593Smuzhiyun }
3255*4882a593Smuzhiyun 
ocfs2_cleanup_merge(struct ocfs2_extent_list * el,int index)3256*4882a593Smuzhiyun static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
3257*4882a593Smuzhiyun 				int index)
3258*4882a593Smuzhiyun {
3259*4882a593Smuzhiyun 	struct ocfs2_extent_rec *rec = &el->l_recs[index];
3260*4882a593Smuzhiyun 	unsigned int size;
3261*4882a593Smuzhiyun 
3262*4882a593Smuzhiyun 	if (rec->e_leaf_clusters == 0) {
3263*4882a593Smuzhiyun 		/*
3264*4882a593Smuzhiyun 		 * We consumed all of the merged-from record. An empty
3265*4882a593Smuzhiyun 		 * extent cannot exist anywhere but the 1st array
3266*4882a593Smuzhiyun 		 * position, so move things over if the merged-from
3267*4882a593Smuzhiyun 		 * record doesn't occupy that position.
3268*4882a593Smuzhiyun 		 *
3269*4882a593Smuzhiyun 		 * This creates a new empty extent so the caller
3270*4882a593Smuzhiyun 		 * should be smart enough to have removed any existing
3271*4882a593Smuzhiyun 		 * ones.
3272*4882a593Smuzhiyun 		 */
3273*4882a593Smuzhiyun 		if (index > 0) {
3274*4882a593Smuzhiyun 			BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3275*4882a593Smuzhiyun 			size = index * sizeof(struct ocfs2_extent_rec);
3276*4882a593Smuzhiyun 			memmove(&el->l_recs[1], &el->l_recs[0], size);
3277*4882a593Smuzhiyun 		}
3278*4882a593Smuzhiyun 
3279*4882a593Smuzhiyun 		/*
3280*4882a593Smuzhiyun 		 * Always memset - the caller doesn't check whether it
3281*4882a593Smuzhiyun 		 * created an empty extent, so there could be junk in
3282*4882a593Smuzhiyun 		 * the other fields.
3283*4882a593Smuzhiyun 		 */
3284*4882a593Smuzhiyun 		memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3285*4882a593Smuzhiyun 	}
3286*4882a593Smuzhiyun }
3287*4882a593Smuzhiyun 
ocfs2_get_right_path(struct ocfs2_extent_tree * et,struct ocfs2_path * left_path,struct ocfs2_path ** ret_right_path)3288*4882a593Smuzhiyun static int ocfs2_get_right_path(struct ocfs2_extent_tree *et,
3289*4882a593Smuzhiyun 				struct ocfs2_path *left_path,
3290*4882a593Smuzhiyun 				struct ocfs2_path **ret_right_path)
3291*4882a593Smuzhiyun {
3292*4882a593Smuzhiyun 	int ret;
3293*4882a593Smuzhiyun 	u32 right_cpos;
3294*4882a593Smuzhiyun 	struct ocfs2_path *right_path = NULL;
3295*4882a593Smuzhiyun 	struct ocfs2_extent_list *left_el;
3296*4882a593Smuzhiyun 
3297*4882a593Smuzhiyun 	*ret_right_path = NULL;
3298*4882a593Smuzhiyun 
3299*4882a593Smuzhiyun 	/* This function shouldn't be called for non-trees. */
3300*4882a593Smuzhiyun 	BUG_ON(left_path->p_tree_depth == 0);
3301*4882a593Smuzhiyun 
3302*4882a593Smuzhiyun 	left_el = path_leaf_el(left_path);
3303*4882a593Smuzhiyun 	BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3304*4882a593Smuzhiyun 
3305*4882a593Smuzhiyun 	ret = ocfs2_find_cpos_for_right_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3306*4882a593Smuzhiyun 					     left_path, &right_cpos);
3307*4882a593Smuzhiyun 	if (ret) {
3308*4882a593Smuzhiyun 		mlog_errno(ret);
3309*4882a593Smuzhiyun 		goto out;
3310*4882a593Smuzhiyun 	}
3311*4882a593Smuzhiyun 
3312*4882a593Smuzhiyun 	/* This function shouldn't be called for the rightmost leaf. */
3313*4882a593Smuzhiyun 	BUG_ON(right_cpos == 0);
3314*4882a593Smuzhiyun 
3315*4882a593Smuzhiyun 	right_path = ocfs2_new_path_from_path(left_path);
3316*4882a593Smuzhiyun 	if (!right_path) {
3317*4882a593Smuzhiyun 		ret = -ENOMEM;
3318*4882a593Smuzhiyun 		mlog_errno(ret);
3319*4882a593Smuzhiyun 		goto out;
3320*4882a593Smuzhiyun 	}
3321*4882a593Smuzhiyun 
3322*4882a593Smuzhiyun 	ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
3323*4882a593Smuzhiyun 	if (ret) {
3324*4882a593Smuzhiyun 		mlog_errno(ret);
3325*4882a593Smuzhiyun 		goto out;
3326*4882a593Smuzhiyun 	}
3327*4882a593Smuzhiyun 
3328*4882a593Smuzhiyun 	*ret_right_path = right_path;
3329*4882a593Smuzhiyun out:
3330*4882a593Smuzhiyun 	if (ret)
3331*4882a593Smuzhiyun 		ocfs2_free_path(right_path);
3332*4882a593Smuzhiyun 	return ret;
3333*4882a593Smuzhiyun }
3334*4882a593Smuzhiyun 
3335*4882a593Smuzhiyun /*
3336*4882a593Smuzhiyun  * Remove split_rec clusters from the record at index and merge them
3337*4882a593Smuzhiyun  * onto the beginning of the record "next" to it.
3338*4882a593Smuzhiyun  * For index < l_count - 1, the next means the extent rec at index + 1.
3339*4882a593Smuzhiyun  * For index == l_count - 1, the "next" means the 1st extent rec of the
3340*4882a593Smuzhiyun  * next extent block.
3341*4882a593Smuzhiyun  */
ocfs2_merge_rec_right(struct ocfs2_path * left_path,handle_t * handle,struct ocfs2_extent_tree * et,struct ocfs2_extent_rec * split_rec,int index)3342*4882a593Smuzhiyun static int ocfs2_merge_rec_right(struct ocfs2_path *left_path,
3343*4882a593Smuzhiyun 				 handle_t *handle,
3344*4882a593Smuzhiyun 				 struct ocfs2_extent_tree *et,
3345*4882a593Smuzhiyun 				 struct ocfs2_extent_rec *split_rec,
3346*4882a593Smuzhiyun 				 int index)
3347*4882a593Smuzhiyun {
3348*4882a593Smuzhiyun 	int ret, next_free, i;
3349*4882a593Smuzhiyun 	unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3350*4882a593Smuzhiyun 	struct ocfs2_extent_rec *left_rec;
3351*4882a593Smuzhiyun 	struct ocfs2_extent_rec *right_rec;
3352*4882a593Smuzhiyun 	struct ocfs2_extent_list *right_el;
3353*4882a593Smuzhiyun 	struct ocfs2_path *right_path = NULL;
3354*4882a593Smuzhiyun 	int subtree_index = 0;
3355*4882a593Smuzhiyun 	struct ocfs2_extent_list *el = path_leaf_el(left_path);
3356*4882a593Smuzhiyun 	struct buffer_head *bh = path_leaf_bh(left_path);
3357*4882a593Smuzhiyun 	struct buffer_head *root_bh = NULL;
3358*4882a593Smuzhiyun 
3359*4882a593Smuzhiyun 	BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
3360*4882a593Smuzhiyun 	left_rec = &el->l_recs[index];
3361*4882a593Smuzhiyun 
3362*4882a593Smuzhiyun 	if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
3363*4882a593Smuzhiyun 	    le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3364*4882a593Smuzhiyun 		/* we meet with a cross extent block merge. */
3365*4882a593Smuzhiyun 		ret = ocfs2_get_right_path(et, left_path, &right_path);
3366*4882a593Smuzhiyun 		if (ret) {
3367*4882a593Smuzhiyun 			mlog_errno(ret);
3368*4882a593Smuzhiyun 			return ret;
3369*4882a593Smuzhiyun 		}
3370*4882a593Smuzhiyun 
3371*4882a593Smuzhiyun 		right_el = path_leaf_el(right_path);
3372*4882a593Smuzhiyun 		next_free = le16_to_cpu(right_el->l_next_free_rec);
3373*4882a593Smuzhiyun 		BUG_ON(next_free <= 0);
3374*4882a593Smuzhiyun 		right_rec = &right_el->l_recs[0];
3375*4882a593Smuzhiyun 		if (ocfs2_is_empty_extent(right_rec)) {
3376*4882a593Smuzhiyun 			BUG_ON(next_free <= 1);
3377*4882a593Smuzhiyun 			right_rec = &right_el->l_recs[1];
3378*4882a593Smuzhiyun 		}
3379*4882a593Smuzhiyun 
3380*4882a593Smuzhiyun 		BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3381*4882a593Smuzhiyun 		       le16_to_cpu(left_rec->e_leaf_clusters) !=
3382*4882a593Smuzhiyun 		       le32_to_cpu(right_rec->e_cpos));
3383*4882a593Smuzhiyun 
3384*4882a593Smuzhiyun 		subtree_index = ocfs2_find_subtree_root(et, left_path,
3385*4882a593Smuzhiyun 							right_path);
3386*4882a593Smuzhiyun 
3387*4882a593Smuzhiyun 		ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3388*4882a593Smuzhiyun 					jbd2_handle_buffer_credits(handle),
3389*4882a593Smuzhiyun 					right_path);
3390*4882a593Smuzhiyun 		if (ret) {
3391*4882a593Smuzhiyun 			mlog_errno(ret);
3392*4882a593Smuzhiyun 			goto out;
3393*4882a593Smuzhiyun 		}
3394*4882a593Smuzhiyun 
3395*4882a593Smuzhiyun 		root_bh = left_path->p_node[subtree_index].bh;
3396*4882a593Smuzhiyun 		BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3397*4882a593Smuzhiyun 
3398*4882a593Smuzhiyun 		ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
3399*4882a593Smuzhiyun 						   subtree_index);
3400*4882a593Smuzhiyun 		if (ret) {
3401*4882a593Smuzhiyun 			mlog_errno(ret);
3402*4882a593Smuzhiyun 			goto out;
3403*4882a593Smuzhiyun 		}
3404*4882a593Smuzhiyun 
3405*4882a593Smuzhiyun 		for (i = subtree_index + 1;
3406*4882a593Smuzhiyun 		     i < path_num_items(right_path); i++) {
3407*4882a593Smuzhiyun 			ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
3408*4882a593Smuzhiyun 							   right_path, i);
3409*4882a593Smuzhiyun 			if (ret) {
3410*4882a593Smuzhiyun 				mlog_errno(ret);
3411*4882a593Smuzhiyun 				goto out;
3412*4882a593Smuzhiyun 			}
3413*4882a593Smuzhiyun 
3414*4882a593Smuzhiyun 			ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
3415*4882a593Smuzhiyun 							   left_path, i);
3416*4882a593Smuzhiyun 			if (ret) {
3417*4882a593Smuzhiyun 				mlog_errno(ret);
3418*4882a593Smuzhiyun 				goto out;
3419*4882a593Smuzhiyun 			}
3420*4882a593Smuzhiyun 		}
3421*4882a593Smuzhiyun 
3422*4882a593Smuzhiyun 	} else {
3423*4882a593Smuzhiyun 		BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3424*4882a593Smuzhiyun 		right_rec = &el->l_recs[index + 1];
3425*4882a593Smuzhiyun 	}
3426*4882a593Smuzhiyun 
3427*4882a593Smuzhiyun 	ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path,
3428*4882a593Smuzhiyun 					   path_num_items(left_path) - 1);
3429*4882a593Smuzhiyun 	if (ret) {
3430*4882a593Smuzhiyun 		mlog_errno(ret);
3431*4882a593Smuzhiyun 		goto out;
3432*4882a593Smuzhiyun 	}
3433*4882a593Smuzhiyun 
3434*4882a593Smuzhiyun 	le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3435*4882a593Smuzhiyun 
3436*4882a593Smuzhiyun 	le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3437*4882a593Smuzhiyun 	le64_add_cpu(&right_rec->e_blkno,
3438*4882a593Smuzhiyun 		     -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3439*4882a593Smuzhiyun 					       split_clusters));
3440*4882a593Smuzhiyun 	le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3441*4882a593Smuzhiyun 
3442*4882a593Smuzhiyun 	ocfs2_cleanup_merge(el, index);
3443*4882a593Smuzhiyun 
3444*4882a593Smuzhiyun 	ocfs2_journal_dirty(handle, bh);
3445*4882a593Smuzhiyun 	if (right_path) {
3446*4882a593Smuzhiyun 		ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3447*4882a593Smuzhiyun 		ocfs2_complete_edge_insert(handle, left_path, right_path,
3448*4882a593Smuzhiyun 					   subtree_index);
3449*4882a593Smuzhiyun 	}
3450*4882a593Smuzhiyun out:
3451*4882a593Smuzhiyun 	ocfs2_free_path(right_path);
3452*4882a593Smuzhiyun 	return ret;
3453*4882a593Smuzhiyun }
3454*4882a593Smuzhiyun 
ocfs2_get_left_path(struct ocfs2_extent_tree * et,struct ocfs2_path * right_path,struct ocfs2_path ** ret_left_path)3455*4882a593Smuzhiyun static int ocfs2_get_left_path(struct ocfs2_extent_tree *et,
3456*4882a593Smuzhiyun 			       struct ocfs2_path *right_path,
3457*4882a593Smuzhiyun 			       struct ocfs2_path **ret_left_path)
3458*4882a593Smuzhiyun {
3459*4882a593Smuzhiyun 	int ret;
3460*4882a593Smuzhiyun 	u32 left_cpos;
3461*4882a593Smuzhiyun 	struct ocfs2_path *left_path = NULL;
3462*4882a593Smuzhiyun 
3463*4882a593Smuzhiyun 	*ret_left_path = NULL;
3464*4882a593Smuzhiyun 
3465*4882a593Smuzhiyun 	/* This function shouldn't be called for non-trees. */
3466*4882a593Smuzhiyun 	BUG_ON(right_path->p_tree_depth == 0);
3467*4882a593Smuzhiyun 
3468*4882a593Smuzhiyun 	ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3469*4882a593Smuzhiyun 					    right_path, &left_cpos);
3470*4882a593Smuzhiyun 	if (ret) {
3471*4882a593Smuzhiyun 		mlog_errno(ret);
3472*4882a593Smuzhiyun 		goto out;
3473*4882a593Smuzhiyun 	}
3474*4882a593Smuzhiyun 
3475*4882a593Smuzhiyun 	/* This function shouldn't be called for the leftmost leaf. */
3476*4882a593Smuzhiyun 	BUG_ON(left_cpos == 0);
3477*4882a593Smuzhiyun 
3478*4882a593Smuzhiyun 	left_path = ocfs2_new_path_from_path(right_path);
3479*4882a593Smuzhiyun 	if (!left_path) {
3480*4882a593Smuzhiyun 		ret = -ENOMEM;
3481*4882a593Smuzhiyun 		mlog_errno(ret);
3482*4882a593Smuzhiyun 		goto out;
3483*4882a593Smuzhiyun 	}
3484*4882a593Smuzhiyun 
3485*4882a593Smuzhiyun 	ret = ocfs2_find_path(et->et_ci, left_path, left_cpos);
3486*4882a593Smuzhiyun 	if (ret) {
3487*4882a593Smuzhiyun 		mlog_errno(ret);
3488*4882a593Smuzhiyun 		goto out;
3489*4882a593Smuzhiyun 	}
3490*4882a593Smuzhiyun 
3491*4882a593Smuzhiyun 	*ret_left_path = left_path;
3492*4882a593Smuzhiyun out:
3493*4882a593Smuzhiyun 	if (ret)
3494*4882a593Smuzhiyun 		ocfs2_free_path(left_path);
3495*4882a593Smuzhiyun 	return ret;
3496*4882a593Smuzhiyun }
3497*4882a593Smuzhiyun 
3498*4882a593Smuzhiyun /*
3499*4882a593Smuzhiyun  * Remove split_rec clusters from the record at index and merge them
3500*4882a593Smuzhiyun  * onto the tail of the record "before" it.
3501*4882a593Smuzhiyun  * For index > 0, the "before" means the extent rec at index - 1.
3502*4882a593Smuzhiyun  *
3503*4882a593Smuzhiyun  * For index == 0, the "before" means the last record of the previous
3504*4882a593Smuzhiyun  * extent block. And there is also a situation that we may need to
3505*4882a593Smuzhiyun  * remove the rightmost leaf extent block in the right_path and change
3506*4882a593Smuzhiyun  * the right path to indicate the new rightmost path.
3507*4882a593Smuzhiyun  */
ocfs2_merge_rec_left(struct ocfs2_path * right_path,handle_t * handle,struct ocfs2_extent_tree * et,struct ocfs2_extent_rec * split_rec,struct ocfs2_cached_dealloc_ctxt * dealloc,int index)3508*4882a593Smuzhiyun static int ocfs2_merge_rec_left(struct ocfs2_path *right_path,
3509*4882a593Smuzhiyun 				handle_t *handle,
3510*4882a593Smuzhiyun 				struct ocfs2_extent_tree *et,
3511*4882a593Smuzhiyun 				struct ocfs2_extent_rec *split_rec,
3512*4882a593Smuzhiyun 				struct ocfs2_cached_dealloc_ctxt *dealloc,
3513*4882a593Smuzhiyun 				int index)
3514*4882a593Smuzhiyun {
3515*4882a593Smuzhiyun 	int ret, i, subtree_index = 0, has_empty_extent = 0;
3516*4882a593Smuzhiyun 	unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3517*4882a593Smuzhiyun 	struct ocfs2_extent_rec *left_rec;
3518*4882a593Smuzhiyun 	struct ocfs2_extent_rec *right_rec;
3519*4882a593Smuzhiyun 	struct ocfs2_extent_list *el = path_leaf_el(right_path);
3520*4882a593Smuzhiyun 	struct buffer_head *bh = path_leaf_bh(right_path);
3521*4882a593Smuzhiyun 	struct buffer_head *root_bh = NULL;
3522*4882a593Smuzhiyun 	struct ocfs2_path *left_path = NULL;
3523*4882a593Smuzhiyun 	struct ocfs2_extent_list *left_el;
3524*4882a593Smuzhiyun 
3525*4882a593Smuzhiyun 	BUG_ON(index < 0);
3526*4882a593Smuzhiyun 
3527*4882a593Smuzhiyun 	right_rec = &el->l_recs[index];
3528*4882a593Smuzhiyun 	if (index == 0) {
3529*4882a593Smuzhiyun 		/* we meet with a cross extent block merge. */
3530*4882a593Smuzhiyun 		ret = ocfs2_get_left_path(et, right_path, &left_path);
3531*4882a593Smuzhiyun 		if (ret) {
3532*4882a593Smuzhiyun 			mlog_errno(ret);
3533*4882a593Smuzhiyun 			return ret;
3534*4882a593Smuzhiyun 		}
3535*4882a593Smuzhiyun 
3536*4882a593Smuzhiyun 		left_el = path_leaf_el(left_path);
3537*4882a593Smuzhiyun 		BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3538*4882a593Smuzhiyun 		       le16_to_cpu(left_el->l_count));
3539*4882a593Smuzhiyun 
3540*4882a593Smuzhiyun 		left_rec = &left_el->l_recs[
3541*4882a593Smuzhiyun 				le16_to_cpu(left_el->l_next_free_rec) - 1];
3542*4882a593Smuzhiyun 		BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3543*4882a593Smuzhiyun 		       le16_to_cpu(left_rec->e_leaf_clusters) !=
3544*4882a593Smuzhiyun 		       le32_to_cpu(split_rec->e_cpos));
3545*4882a593Smuzhiyun 
3546*4882a593Smuzhiyun 		subtree_index = ocfs2_find_subtree_root(et, left_path,
3547*4882a593Smuzhiyun 							right_path);
3548*4882a593Smuzhiyun 
3549*4882a593Smuzhiyun 		ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3550*4882a593Smuzhiyun 					jbd2_handle_buffer_credits(handle),
3551*4882a593Smuzhiyun 					left_path);
3552*4882a593Smuzhiyun 		if (ret) {
3553*4882a593Smuzhiyun 			mlog_errno(ret);
3554*4882a593Smuzhiyun 			goto out;
3555*4882a593Smuzhiyun 		}
3556*4882a593Smuzhiyun 
3557*4882a593Smuzhiyun 		root_bh = left_path->p_node[subtree_index].bh;
3558*4882a593Smuzhiyun 		BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3559*4882a593Smuzhiyun 
3560*4882a593Smuzhiyun 		ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
3561*4882a593Smuzhiyun 						   subtree_index);
3562*4882a593Smuzhiyun 		if (ret) {
3563*4882a593Smuzhiyun 			mlog_errno(ret);
3564*4882a593Smuzhiyun 			goto out;
3565*4882a593Smuzhiyun 		}
3566*4882a593Smuzhiyun 
3567*4882a593Smuzhiyun 		for (i = subtree_index + 1;
3568*4882a593Smuzhiyun 		     i < path_num_items(right_path); i++) {
3569*4882a593Smuzhiyun 			ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
3570*4882a593Smuzhiyun 							   right_path, i);
3571*4882a593Smuzhiyun 			if (ret) {
3572*4882a593Smuzhiyun 				mlog_errno(ret);
3573*4882a593Smuzhiyun 				goto out;
3574*4882a593Smuzhiyun 			}
3575*4882a593Smuzhiyun 
3576*4882a593Smuzhiyun 			ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
3577*4882a593Smuzhiyun 							   left_path, i);
3578*4882a593Smuzhiyun 			if (ret) {
3579*4882a593Smuzhiyun 				mlog_errno(ret);
3580*4882a593Smuzhiyun 				goto out;
3581*4882a593Smuzhiyun 			}
3582*4882a593Smuzhiyun 		}
3583*4882a593Smuzhiyun 	} else {
3584*4882a593Smuzhiyun 		left_rec = &el->l_recs[index - 1];
3585*4882a593Smuzhiyun 		if (ocfs2_is_empty_extent(&el->l_recs[0]))
3586*4882a593Smuzhiyun 			has_empty_extent = 1;
3587*4882a593Smuzhiyun 	}
3588*4882a593Smuzhiyun 
3589*4882a593Smuzhiyun 	ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
3590*4882a593Smuzhiyun 					   path_num_items(right_path) - 1);
3591*4882a593Smuzhiyun 	if (ret) {
3592*4882a593Smuzhiyun 		mlog_errno(ret);
3593*4882a593Smuzhiyun 		goto out;
3594*4882a593Smuzhiyun 	}
3595*4882a593Smuzhiyun 
3596*4882a593Smuzhiyun 	if (has_empty_extent && index == 1) {
3597*4882a593Smuzhiyun 		/*
3598*4882a593Smuzhiyun 		 * The easy case - we can just plop the record right in.
3599*4882a593Smuzhiyun 		 */
3600*4882a593Smuzhiyun 		*left_rec = *split_rec;
3601*4882a593Smuzhiyun 	} else
3602*4882a593Smuzhiyun 		le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
3603*4882a593Smuzhiyun 
3604*4882a593Smuzhiyun 	le32_add_cpu(&right_rec->e_cpos, split_clusters);
3605*4882a593Smuzhiyun 	le64_add_cpu(&right_rec->e_blkno,
3606*4882a593Smuzhiyun 		     ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3607*4882a593Smuzhiyun 					      split_clusters));
3608*4882a593Smuzhiyun 	le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3609*4882a593Smuzhiyun 
3610*4882a593Smuzhiyun 	ocfs2_cleanup_merge(el, index);
3611*4882a593Smuzhiyun 
3612*4882a593Smuzhiyun 	ocfs2_journal_dirty(handle, bh);
3613*4882a593Smuzhiyun 	if (left_path) {
3614*4882a593Smuzhiyun 		ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3615*4882a593Smuzhiyun 
3616*4882a593Smuzhiyun 		/*
3617*4882a593Smuzhiyun 		 * In the situation that the right_rec is empty and the extent
3618*4882a593Smuzhiyun 		 * block is empty also,  ocfs2_complete_edge_insert can't handle
3619*4882a593Smuzhiyun 		 * it and we need to delete the right extent block.
3620*4882a593Smuzhiyun 		 */
3621*4882a593Smuzhiyun 		if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3622*4882a593Smuzhiyun 		    le16_to_cpu(el->l_next_free_rec) == 1) {
3623*4882a593Smuzhiyun 			/* extend credit for ocfs2_remove_rightmost_path */
3624*4882a593Smuzhiyun 			ret = ocfs2_extend_rotate_transaction(handle, 0,
3625*4882a593Smuzhiyun 					jbd2_handle_buffer_credits(handle),
3626*4882a593Smuzhiyun 					right_path);
3627*4882a593Smuzhiyun 			if (ret) {
3628*4882a593Smuzhiyun 				mlog_errno(ret);
3629*4882a593Smuzhiyun 				goto out;
3630*4882a593Smuzhiyun 			}
3631*4882a593Smuzhiyun 
3632*4882a593Smuzhiyun 			ret = ocfs2_remove_rightmost_path(handle, et,
3633*4882a593Smuzhiyun 							  right_path,
3634*4882a593Smuzhiyun 							  dealloc);
3635*4882a593Smuzhiyun 			if (ret) {
3636*4882a593Smuzhiyun 				mlog_errno(ret);
3637*4882a593Smuzhiyun 				goto out;
3638*4882a593Smuzhiyun 			}
3639*4882a593Smuzhiyun 
3640*4882a593Smuzhiyun 			/* Now the rightmost extent block has been deleted.
3641*4882a593Smuzhiyun 			 * So we use the new rightmost path.
3642*4882a593Smuzhiyun 			 */
3643*4882a593Smuzhiyun 			ocfs2_mv_path(right_path, left_path);
3644*4882a593Smuzhiyun 			left_path = NULL;
3645*4882a593Smuzhiyun 		} else
3646*4882a593Smuzhiyun 			ocfs2_complete_edge_insert(handle, left_path,
3647*4882a593Smuzhiyun 						   right_path, subtree_index);
3648*4882a593Smuzhiyun 	}
3649*4882a593Smuzhiyun out:
3650*4882a593Smuzhiyun 	ocfs2_free_path(left_path);
3651*4882a593Smuzhiyun 	return ret;
3652*4882a593Smuzhiyun }
3653*4882a593Smuzhiyun 
ocfs2_try_to_merge_extent(handle_t * handle,struct ocfs2_extent_tree * et,struct ocfs2_path * path,int split_index,struct ocfs2_extent_rec * split_rec,struct ocfs2_cached_dealloc_ctxt * dealloc,struct ocfs2_merge_ctxt * ctxt)3654*4882a593Smuzhiyun static int ocfs2_try_to_merge_extent(handle_t *handle,
3655*4882a593Smuzhiyun 				     struct ocfs2_extent_tree *et,
3656*4882a593Smuzhiyun 				     struct ocfs2_path *path,
3657*4882a593Smuzhiyun 				     int split_index,
3658*4882a593Smuzhiyun 				     struct ocfs2_extent_rec *split_rec,
3659*4882a593Smuzhiyun 				     struct ocfs2_cached_dealloc_ctxt *dealloc,
3660*4882a593Smuzhiyun 				     struct ocfs2_merge_ctxt *ctxt)
3661*4882a593Smuzhiyun {
3662*4882a593Smuzhiyun 	int ret = 0;
3663*4882a593Smuzhiyun 	struct ocfs2_extent_list *el = path_leaf_el(path);
3664*4882a593Smuzhiyun 	struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3665*4882a593Smuzhiyun 
3666*4882a593Smuzhiyun 	BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3667*4882a593Smuzhiyun 
3668*4882a593Smuzhiyun 	if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3669*4882a593Smuzhiyun 		/* extend credit for ocfs2_remove_rightmost_path */
3670*4882a593Smuzhiyun 		ret = ocfs2_extend_rotate_transaction(handle, 0,
3671*4882a593Smuzhiyun 				jbd2_handle_buffer_credits(handle),
3672*4882a593Smuzhiyun 				path);
3673*4882a593Smuzhiyun 		if (ret) {
3674*4882a593Smuzhiyun 			mlog_errno(ret);
3675*4882a593Smuzhiyun 			goto out;
3676*4882a593Smuzhiyun 		}
3677*4882a593Smuzhiyun 		/*
3678*4882a593Smuzhiyun 		 * The merge code will need to create an empty
3679*4882a593Smuzhiyun 		 * extent to take the place of the newly
3680*4882a593Smuzhiyun 		 * emptied slot. Remove any pre-existing empty
3681*4882a593Smuzhiyun 		 * extents - having more than one in a leaf is
3682*4882a593Smuzhiyun 		 * illegal.
3683*4882a593Smuzhiyun 		 */
3684*4882a593Smuzhiyun 		ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
3685*4882a593Smuzhiyun 		if (ret) {
3686*4882a593Smuzhiyun 			mlog_errno(ret);
3687*4882a593Smuzhiyun 			goto out;
3688*4882a593Smuzhiyun 		}
3689*4882a593Smuzhiyun 		split_index--;
3690*4882a593Smuzhiyun 		rec = &el->l_recs[split_index];
3691*4882a593Smuzhiyun 	}
3692*4882a593Smuzhiyun 
3693*4882a593Smuzhiyun 	if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3694*4882a593Smuzhiyun 		/*
3695*4882a593Smuzhiyun 		 * Left-right contig implies this.
3696*4882a593Smuzhiyun 		 */
3697*4882a593Smuzhiyun 		BUG_ON(!ctxt->c_split_covers_rec);
3698*4882a593Smuzhiyun 
3699*4882a593Smuzhiyun 		/*
3700*4882a593Smuzhiyun 		 * Since the leftright insert always covers the entire
3701*4882a593Smuzhiyun 		 * extent, this call will delete the insert record
3702*4882a593Smuzhiyun 		 * entirely, resulting in an empty extent record added to
3703*4882a593Smuzhiyun 		 * the extent block.
3704*4882a593Smuzhiyun 		 *
3705*4882a593Smuzhiyun 		 * Since the adding of an empty extent shifts
3706*4882a593Smuzhiyun 		 * everything back to the right, there's no need to
3707*4882a593Smuzhiyun 		 * update split_index here.
3708*4882a593Smuzhiyun 		 *
3709*4882a593Smuzhiyun 		 * When the split_index is zero, we need to merge it to the
3710*4882a593Smuzhiyun 		 * prevoius extent block. It is more efficient and easier
3711*4882a593Smuzhiyun 		 * if we do merge_right first and merge_left later.
3712*4882a593Smuzhiyun 		 */
3713*4882a593Smuzhiyun 		ret = ocfs2_merge_rec_right(path, handle, et, split_rec,
3714*4882a593Smuzhiyun 					    split_index);
3715*4882a593Smuzhiyun 		if (ret) {
3716*4882a593Smuzhiyun 			mlog_errno(ret);
3717*4882a593Smuzhiyun 			goto out;
3718*4882a593Smuzhiyun 		}
3719*4882a593Smuzhiyun 
3720*4882a593Smuzhiyun 		/*
3721*4882a593Smuzhiyun 		 * We can only get this from logic error above.
3722*4882a593Smuzhiyun 		 */
3723*4882a593Smuzhiyun 		BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3724*4882a593Smuzhiyun 
3725*4882a593Smuzhiyun 		/* extend credit for ocfs2_remove_rightmost_path */
3726*4882a593Smuzhiyun 		ret = ocfs2_extend_rotate_transaction(handle, 0,
3727*4882a593Smuzhiyun 					jbd2_handle_buffer_credits(handle),
3728*4882a593Smuzhiyun 					path);
3729*4882a593Smuzhiyun 		if (ret) {
3730*4882a593Smuzhiyun 			mlog_errno(ret);
3731*4882a593Smuzhiyun 			goto out;
3732*4882a593Smuzhiyun 		}
3733*4882a593Smuzhiyun 
3734*4882a593Smuzhiyun 		/* The merge left us with an empty extent, remove it. */
3735*4882a593Smuzhiyun 		ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
3736*4882a593Smuzhiyun 		if (ret) {
3737*4882a593Smuzhiyun 			mlog_errno(ret);
3738*4882a593Smuzhiyun 			goto out;
3739*4882a593Smuzhiyun 		}
3740*4882a593Smuzhiyun 
3741*4882a593Smuzhiyun 		rec = &el->l_recs[split_index];
3742*4882a593Smuzhiyun 
3743*4882a593Smuzhiyun 		/*
3744*4882a593Smuzhiyun 		 * Note that we don't pass split_rec here on purpose -
3745*4882a593Smuzhiyun 		 * we've merged it into the rec already.
3746*4882a593Smuzhiyun 		 */
3747*4882a593Smuzhiyun 		ret = ocfs2_merge_rec_left(path, handle, et, rec,
3748*4882a593Smuzhiyun 					   dealloc, split_index);
3749*4882a593Smuzhiyun 
3750*4882a593Smuzhiyun 		if (ret) {
3751*4882a593Smuzhiyun 			mlog_errno(ret);
3752*4882a593Smuzhiyun 			goto out;
3753*4882a593Smuzhiyun 		}
3754*4882a593Smuzhiyun 
3755*4882a593Smuzhiyun 		/* extend credit for ocfs2_remove_rightmost_path */
3756*4882a593Smuzhiyun 		ret = ocfs2_extend_rotate_transaction(handle, 0,
3757*4882a593Smuzhiyun 				jbd2_handle_buffer_credits(handle),
3758*4882a593Smuzhiyun 				path);
3759*4882a593Smuzhiyun 		if (ret) {
3760*4882a593Smuzhiyun 			mlog_errno(ret);
3761*4882a593Smuzhiyun 			goto out;
3762*4882a593Smuzhiyun 		}
3763*4882a593Smuzhiyun 
3764*4882a593Smuzhiyun 		ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
3765*4882a593Smuzhiyun 		/*
3766*4882a593Smuzhiyun 		 * Error from this last rotate is not critical, so
3767*4882a593Smuzhiyun 		 * print but don't bubble it up.
3768*4882a593Smuzhiyun 		 */
3769*4882a593Smuzhiyun 		if (ret)
3770*4882a593Smuzhiyun 			mlog_errno(ret);
3771*4882a593Smuzhiyun 		ret = 0;
3772*4882a593Smuzhiyun 	} else {
3773*4882a593Smuzhiyun 		/*
3774*4882a593Smuzhiyun 		 * Merge a record to the left or right.
3775*4882a593Smuzhiyun 		 *
3776*4882a593Smuzhiyun 		 * 'contig_type' is relative to the existing record,
3777*4882a593Smuzhiyun 		 * so for example, if we're "right contig", it's to
3778*4882a593Smuzhiyun 		 * the record on the left (hence the left merge).
3779*4882a593Smuzhiyun 		 */
3780*4882a593Smuzhiyun 		if (ctxt->c_contig_type == CONTIG_RIGHT) {
3781*4882a593Smuzhiyun 			ret = ocfs2_merge_rec_left(path, handle, et,
3782*4882a593Smuzhiyun 						   split_rec, dealloc,
3783*4882a593Smuzhiyun 						   split_index);
3784*4882a593Smuzhiyun 			if (ret) {
3785*4882a593Smuzhiyun 				mlog_errno(ret);
3786*4882a593Smuzhiyun 				goto out;
3787*4882a593Smuzhiyun 			}
3788*4882a593Smuzhiyun 		} else {
3789*4882a593Smuzhiyun 			ret = ocfs2_merge_rec_right(path, handle,
3790*4882a593Smuzhiyun 						    et, split_rec,
3791*4882a593Smuzhiyun 						    split_index);
3792*4882a593Smuzhiyun 			if (ret) {
3793*4882a593Smuzhiyun 				mlog_errno(ret);
3794*4882a593Smuzhiyun 				goto out;
3795*4882a593Smuzhiyun 			}
3796*4882a593Smuzhiyun 		}
3797*4882a593Smuzhiyun 
3798*4882a593Smuzhiyun 		if (ctxt->c_split_covers_rec) {
3799*4882a593Smuzhiyun 			/* extend credit for ocfs2_remove_rightmost_path */
3800*4882a593Smuzhiyun 			ret = ocfs2_extend_rotate_transaction(handle, 0,
3801*4882a593Smuzhiyun 					jbd2_handle_buffer_credits(handle),
3802*4882a593Smuzhiyun 					path);
3803*4882a593Smuzhiyun 			if (ret) {
3804*4882a593Smuzhiyun 				mlog_errno(ret);
3805*4882a593Smuzhiyun 				ret = 0;
3806*4882a593Smuzhiyun 				goto out;
3807*4882a593Smuzhiyun 			}
3808*4882a593Smuzhiyun 
3809*4882a593Smuzhiyun 			/*
3810*4882a593Smuzhiyun 			 * The merge may have left an empty extent in
3811*4882a593Smuzhiyun 			 * our leaf. Try to rotate it away.
3812*4882a593Smuzhiyun 			 */
3813*4882a593Smuzhiyun 			ret = ocfs2_rotate_tree_left(handle, et, path,
3814*4882a593Smuzhiyun 						     dealloc);
3815*4882a593Smuzhiyun 			if (ret)
3816*4882a593Smuzhiyun 				mlog_errno(ret);
3817*4882a593Smuzhiyun 			ret = 0;
3818*4882a593Smuzhiyun 		}
3819*4882a593Smuzhiyun 	}
3820*4882a593Smuzhiyun 
3821*4882a593Smuzhiyun out:
3822*4882a593Smuzhiyun 	return ret;
3823*4882a593Smuzhiyun }
3824*4882a593Smuzhiyun 
ocfs2_subtract_from_rec(struct super_block * sb,enum ocfs2_split_type split,struct ocfs2_extent_rec * rec,struct ocfs2_extent_rec * split_rec)3825*4882a593Smuzhiyun static void ocfs2_subtract_from_rec(struct super_block *sb,
3826*4882a593Smuzhiyun 				    enum ocfs2_split_type split,
3827*4882a593Smuzhiyun 				    struct ocfs2_extent_rec *rec,
3828*4882a593Smuzhiyun 				    struct ocfs2_extent_rec *split_rec)
3829*4882a593Smuzhiyun {
3830*4882a593Smuzhiyun 	u64 len_blocks;
3831*4882a593Smuzhiyun 
3832*4882a593Smuzhiyun 	len_blocks = ocfs2_clusters_to_blocks(sb,
3833*4882a593Smuzhiyun 				le16_to_cpu(split_rec->e_leaf_clusters));
3834*4882a593Smuzhiyun 
3835*4882a593Smuzhiyun 	if (split == SPLIT_LEFT) {
3836*4882a593Smuzhiyun 		/*
3837*4882a593Smuzhiyun 		 * Region is on the left edge of the existing
3838*4882a593Smuzhiyun 		 * record.
3839*4882a593Smuzhiyun 		 */
3840*4882a593Smuzhiyun 		le32_add_cpu(&rec->e_cpos,
3841*4882a593Smuzhiyun 			     le16_to_cpu(split_rec->e_leaf_clusters));
3842*4882a593Smuzhiyun 		le64_add_cpu(&rec->e_blkno, len_blocks);
3843*4882a593Smuzhiyun 		le16_add_cpu(&rec->e_leaf_clusters,
3844*4882a593Smuzhiyun 			     -le16_to_cpu(split_rec->e_leaf_clusters));
3845*4882a593Smuzhiyun 	} else {
3846*4882a593Smuzhiyun 		/*
3847*4882a593Smuzhiyun 		 * Region is on the right edge of the existing
3848*4882a593Smuzhiyun 		 * record.
3849*4882a593Smuzhiyun 		 */
3850*4882a593Smuzhiyun 		le16_add_cpu(&rec->e_leaf_clusters,
3851*4882a593Smuzhiyun 			     -le16_to_cpu(split_rec->e_leaf_clusters));
3852*4882a593Smuzhiyun 	}
3853*4882a593Smuzhiyun }
3854*4882a593Smuzhiyun 
3855*4882a593Smuzhiyun /*
3856*4882a593Smuzhiyun  * Do the final bits of extent record insertion at the target leaf
3857*4882a593Smuzhiyun  * list. If this leaf is part of an allocation tree, it is assumed
3858*4882a593Smuzhiyun  * that the tree above has been prepared.
3859*4882a593Smuzhiyun  */
ocfs2_insert_at_leaf(struct ocfs2_extent_tree * et,struct ocfs2_extent_rec * insert_rec,struct ocfs2_extent_list * el,struct ocfs2_insert_type * insert)3860*4882a593Smuzhiyun static void ocfs2_insert_at_leaf(struct ocfs2_extent_tree *et,
3861*4882a593Smuzhiyun 				 struct ocfs2_extent_rec *insert_rec,
3862*4882a593Smuzhiyun 				 struct ocfs2_extent_list *el,
3863*4882a593Smuzhiyun 				 struct ocfs2_insert_type *insert)
3864*4882a593Smuzhiyun {
3865*4882a593Smuzhiyun 	int i = insert->ins_contig_index;
3866*4882a593Smuzhiyun 	unsigned int range;
3867*4882a593Smuzhiyun 	struct ocfs2_extent_rec *rec;
3868*4882a593Smuzhiyun 
3869*4882a593Smuzhiyun 	BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
3870*4882a593Smuzhiyun 
3871*4882a593Smuzhiyun 	if (insert->ins_split != SPLIT_NONE) {
3872*4882a593Smuzhiyun 		i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3873*4882a593Smuzhiyun 		BUG_ON(i == -1);
3874*4882a593Smuzhiyun 		rec = &el->l_recs[i];
3875*4882a593Smuzhiyun 		ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
3876*4882a593Smuzhiyun 					insert->ins_split, rec,
3877*4882a593Smuzhiyun 					insert_rec);
3878*4882a593Smuzhiyun 		goto rotate;
3879*4882a593Smuzhiyun 	}
3880*4882a593Smuzhiyun 
3881*4882a593Smuzhiyun 	/*
3882*4882a593Smuzhiyun 	 * Contiguous insert - either left or right.
3883*4882a593Smuzhiyun 	 */
3884*4882a593Smuzhiyun 	if (insert->ins_contig != CONTIG_NONE) {
3885*4882a593Smuzhiyun 		rec = &el->l_recs[i];
3886*4882a593Smuzhiyun 		if (insert->ins_contig == CONTIG_LEFT) {
3887*4882a593Smuzhiyun 			rec->e_blkno = insert_rec->e_blkno;
3888*4882a593Smuzhiyun 			rec->e_cpos = insert_rec->e_cpos;
3889*4882a593Smuzhiyun 		}
3890*4882a593Smuzhiyun 		le16_add_cpu(&rec->e_leaf_clusters,
3891*4882a593Smuzhiyun 			     le16_to_cpu(insert_rec->e_leaf_clusters));
3892*4882a593Smuzhiyun 		return;
3893*4882a593Smuzhiyun 	}
3894*4882a593Smuzhiyun 
3895*4882a593Smuzhiyun 	/*
3896*4882a593Smuzhiyun 	 * Handle insert into an empty leaf.
3897*4882a593Smuzhiyun 	 */
3898*4882a593Smuzhiyun 	if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3899*4882a593Smuzhiyun 	    ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3900*4882a593Smuzhiyun 	     ocfs2_is_empty_extent(&el->l_recs[0]))) {
3901*4882a593Smuzhiyun 		el->l_recs[0] = *insert_rec;
3902*4882a593Smuzhiyun 		el->l_next_free_rec = cpu_to_le16(1);
3903*4882a593Smuzhiyun 		return;
3904*4882a593Smuzhiyun 	}
3905*4882a593Smuzhiyun 
3906*4882a593Smuzhiyun 	/*
3907*4882a593Smuzhiyun 	 * Appending insert.
3908*4882a593Smuzhiyun 	 */
3909*4882a593Smuzhiyun 	if (insert->ins_appending == APPEND_TAIL) {
3910*4882a593Smuzhiyun 		i = le16_to_cpu(el->l_next_free_rec) - 1;
3911*4882a593Smuzhiyun 		rec = &el->l_recs[i];
3912*4882a593Smuzhiyun 		range = le32_to_cpu(rec->e_cpos)
3913*4882a593Smuzhiyun 			+ le16_to_cpu(rec->e_leaf_clusters);
3914*4882a593Smuzhiyun 		BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3915*4882a593Smuzhiyun 
3916*4882a593Smuzhiyun 		mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3917*4882a593Smuzhiyun 				le16_to_cpu(el->l_count),
3918*4882a593Smuzhiyun 				"owner %llu, depth %u, count %u, next free %u, "
3919*4882a593Smuzhiyun 				"rec.cpos %u, rec.clusters %u, "
3920*4882a593Smuzhiyun 				"insert.cpos %u, insert.clusters %u\n",
3921*4882a593Smuzhiyun 				ocfs2_metadata_cache_owner(et->et_ci),
3922*4882a593Smuzhiyun 				le16_to_cpu(el->l_tree_depth),
3923*4882a593Smuzhiyun 				le16_to_cpu(el->l_count),
3924*4882a593Smuzhiyun 				le16_to_cpu(el->l_next_free_rec),
3925*4882a593Smuzhiyun 				le32_to_cpu(el->l_recs[i].e_cpos),
3926*4882a593Smuzhiyun 				le16_to_cpu(el->l_recs[i].e_leaf_clusters),
3927*4882a593Smuzhiyun 				le32_to_cpu(insert_rec->e_cpos),
3928*4882a593Smuzhiyun 				le16_to_cpu(insert_rec->e_leaf_clusters));
3929*4882a593Smuzhiyun 		i++;
3930*4882a593Smuzhiyun 		el->l_recs[i] = *insert_rec;
3931*4882a593Smuzhiyun 		le16_add_cpu(&el->l_next_free_rec, 1);
3932*4882a593Smuzhiyun 		return;
3933*4882a593Smuzhiyun 	}
3934*4882a593Smuzhiyun 
3935*4882a593Smuzhiyun rotate:
3936*4882a593Smuzhiyun 	/*
3937*4882a593Smuzhiyun 	 * Ok, we have to rotate.
3938*4882a593Smuzhiyun 	 *
3939*4882a593Smuzhiyun 	 * At this point, it is safe to assume that inserting into an
3940*4882a593Smuzhiyun 	 * empty leaf and appending to a leaf have both been handled
3941*4882a593Smuzhiyun 	 * above.
3942*4882a593Smuzhiyun 	 *
3943*4882a593Smuzhiyun 	 * This leaf needs to have space, either by the empty 1st
3944*4882a593Smuzhiyun 	 * extent record, or by virtue of an l_next_free_rec < l_count.
3945*4882a593Smuzhiyun 	 */
3946*4882a593Smuzhiyun 	ocfs2_rotate_leaf(el, insert_rec);
3947*4882a593Smuzhiyun }
3948*4882a593Smuzhiyun 
ocfs2_adjust_rightmost_records(handle_t * handle,struct ocfs2_extent_tree * et,struct ocfs2_path * path,struct ocfs2_extent_rec * insert_rec)3949*4882a593Smuzhiyun static void ocfs2_adjust_rightmost_records(handle_t *handle,
3950*4882a593Smuzhiyun 					   struct ocfs2_extent_tree *et,
3951*4882a593Smuzhiyun 					   struct ocfs2_path *path,
3952*4882a593Smuzhiyun 					   struct ocfs2_extent_rec *insert_rec)
3953*4882a593Smuzhiyun {
3954*4882a593Smuzhiyun 	int i, next_free;
3955*4882a593Smuzhiyun 	struct buffer_head *bh;
3956*4882a593Smuzhiyun 	struct ocfs2_extent_list *el;
3957*4882a593Smuzhiyun 	struct ocfs2_extent_rec *rec;
3958*4882a593Smuzhiyun 
3959*4882a593Smuzhiyun 	/*
3960*4882a593Smuzhiyun 	 * Update everything except the leaf block.
3961*4882a593Smuzhiyun 	 */
3962*4882a593Smuzhiyun 	for (i = 0; i < path->p_tree_depth; i++) {
3963*4882a593Smuzhiyun 		bh = path->p_node[i].bh;
3964*4882a593Smuzhiyun 		el = path->p_node[i].el;
3965*4882a593Smuzhiyun 
3966*4882a593Smuzhiyun 		next_free = le16_to_cpu(el->l_next_free_rec);
3967*4882a593Smuzhiyun 		if (next_free == 0) {
3968*4882a593Smuzhiyun 			ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
3969*4882a593Smuzhiyun 				    "Owner %llu has a bad extent list\n",
3970*4882a593Smuzhiyun 				    (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
3971*4882a593Smuzhiyun 			return;
3972*4882a593Smuzhiyun 		}
3973*4882a593Smuzhiyun 
3974*4882a593Smuzhiyun 		rec = &el->l_recs[next_free - 1];
3975*4882a593Smuzhiyun 
3976*4882a593Smuzhiyun 		rec->e_int_clusters = insert_rec->e_cpos;
3977*4882a593Smuzhiyun 		le32_add_cpu(&rec->e_int_clusters,
3978*4882a593Smuzhiyun 			     le16_to_cpu(insert_rec->e_leaf_clusters));
3979*4882a593Smuzhiyun 		le32_add_cpu(&rec->e_int_clusters,
3980*4882a593Smuzhiyun 			     -le32_to_cpu(rec->e_cpos));
3981*4882a593Smuzhiyun 
3982*4882a593Smuzhiyun 		ocfs2_journal_dirty(handle, bh);
3983*4882a593Smuzhiyun 	}
3984*4882a593Smuzhiyun }
3985*4882a593Smuzhiyun 
ocfs2_append_rec_to_path(handle_t * handle,struct ocfs2_extent_tree * et,struct ocfs2_extent_rec * insert_rec,struct ocfs2_path * right_path,struct ocfs2_path ** ret_left_path)3986*4882a593Smuzhiyun static int ocfs2_append_rec_to_path(handle_t *handle,
3987*4882a593Smuzhiyun 				    struct ocfs2_extent_tree *et,
3988*4882a593Smuzhiyun 				    struct ocfs2_extent_rec *insert_rec,
3989*4882a593Smuzhiyun 				    struct ocfs2_path *right_path,
3990*4882a593Smuzhiyun 				    struct ocfs2_path **ret_left_path)
3991*4882a593Smuzhiyun {
3992*4882a593Smuzhiyun 	int ret, next_free;
3993*4882a593Smuzhiyun 	struct ocfs2_extent_list *el;
3994*4882a593Smuzhiyun 	struct ocfs2_path *left_path = NULL;
3995*4882a593Smuzhiyun 
3996*4882a593Smuzhiyun 	*ret_left_path = NULL;
3997*4882a593Smuzhiyun 
3998*4882a593Smuzhiyun 	/*
3999*4882a593Smuzhiyun 	 * This shouldn't happen for non-trees. The extent rec cluster
4000*4882a593Smuzhiyun 	 * count manipulation below only works for interior nodes.
4001*4882a593Smuzhiyun 	 */
4002*4882a593Smuzhiyun 	BUG_ON(right_path->p_tree_depth == 0);
4003*4882a593Smuzhiyun 
4004*4882a593Smuzhiyun 	/*
4005*4882a593Smuzhiyun 	 * If our appending insert is at the leftmost edge of a leaf,
4006*4882a593Smuzhiyun 	 * then we might need to update the rightmost records of the
4007*4882a593Smuzhiyun 	 * neighboring path.
4008*4882a593Smuzhiyun 	 */
4009*4882a593Smuzhiyun 	el = path_leaf_el(right_path);
4010*4882a593Smuzhiyun 	next_free = le16_to_cpu(el->l_next_free_rec);
4011*4882a593Smuzhiyun 	if (next_free == 0 ||
4012*4882a593Smuzhiyun 	    (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
4013*4882a593Smuzhiyun 		u32 left_cpos;
4014*4882a593Smuzhiyun 
4015*4882a593Smuzhiyun 		ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
4016*4882a593Smuzhiyun 						    right_path, &left_cpos);
4017*4882a593Smuzhiyun 		if (ret) {
4018*4882a593Smuzhiyun 			mlog_errno(ret);
4019*4882a593Smuzhiyun 			goto out;
4020*4882a593Smuzhiyun 		}
4021*4882a593Smuzhiyun 
4022*4882a593Smuzhiyun 		trace_ocfs2_append_rec_to_path(
4023*4882a593Smuzhiyun 			(unsigned long long)
4024*4882a593Smuzhiyun 			ocfs2_metadata_cache_owner(et->et_ci),
4025*4882a593Smuzhiyun 			le32_to_cpu(insert_rec->e_cpos),
4026*4882a593Smuzhiyun 			left_cpos);
4027*4882a593Smuzhiyun 
4028*4882a593Smuzhiyun 		/*
4029*4882a593Smuzhiyun 		 * No need to worry if the append is already in the
4030*4882a593Smuzhiyun 		 * leftmost leaf.
4031*4882a593Smuzhiyun 		 */
4032*4882a593Smuzhiyun 		if (left_cpos) {
4033*4882a593Smuzhiyun 			left_path = ocfs2_new_path_from_path(right_path);
4034*4882a593Smuzhiyun 			if (!left_path) {
4035*4882a593Smuzhiyun 				ret = -ENOMEM;
4036*4882a593Smuzhiyun 				mlog_errno(ret);
4037*4882a593Smuzhiyun 				goto out;
4038*4882a593Smuzhiyun 			}
4039*4882a593Smuzhiyun 
4040*4882a593Smuzhiyun 			ret = ocfs2_find_path(et->et_ci, left_path,
4041*4882a593Smuzhiyun 					      left_cpos);
4042*4882a593Smuzhiyun 			if (ret) {
4043*4882a593Smuzhiyun 				mlog_errno(ret);
4044*4882a593Smuzhiyun 				goto out;
4045*4882a593Smuzhiyun 			}
4046*4882a593Smuzhiyun 
4047*4882a593Smuzhiyun 			/*
4048*4882a593Smuzhiyun 			 * ocfs2_insert_path() will pass the left_path to the
4049*4882a593Smuzhiyun 			 * journal for us.
4050*4882a593Smuzhiyun 			 */
4051*4882a593Smuzhiyun 		}
4052*4882a593Smuzhiyun 	}
4053*4882a593Smuzhiyun 
4054*4882a593Smuzhiyun 	ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
4055*4882a593Smuzhiyun 	if (ret) {
4056*4882a593Smuzhiyun 		mlog_errno(ret);
4057*4882a593Smuzhiyun 		goto out;
4058*4882a593Smuzhiyun 	}
4059*4882a593Smuzhiyun 
4060*4882a593Smuzhiyun 	ocfs2_adjust_rightmost_records(handle, et, right_path, insert_rec);
4061*4882a593Smuzhiyun 
4062*4882a593Smuzhiyun 	*ret_left_path = left_path;
4063*4882a593Smuzhiyun 	ret = 0;
4064*4882a593Smuzhiyun out:
4065*4882a593Smuzhiyun 	if (ret != 0)
4066*4882a593Smuzhiyun 		ocfs2_free_path(left_path);
4067*4882a593Smuzhiyun 
4068*4882a593Smuzhiyun 	return ret;
4069*4882a593Smuzhiyun }
4070*4882a593Smuzhiyun 
ocfs2_split_record(struct ocfs2_extent_tree * et,struct ocfs2_path * left_path,struct ocfs2_path * right_path,struct ocfs2_extent_rec * split_rec,enum ocfs2_split_type split)4071*4882a593Smuzhiyun static void ocfs2_split_record(struct ocfs2_extent_tree *et,
4072*4882a593Smuzhiyun 			       struct ocfs2_path *left_path,
4073*4882a593Smuzhiyun 			       struct ocfs2_path *right_path,
4074*4882a593Smuzhiyun 			       struct ocfs2_extent_rec *split_rec,
4075*4882a593Smuzhiyun 			       enum ocfs2_split_type split)
4076*4882a593Smuzhiyun {
4077*4882a593Smuzhiyun 	int index;
4078*4882a593Smuzhiyun 	u32 cpos = le32_to_cpu(split_rec->e_cpos);
4079*4882a593Smuzhiyun 	struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
4080*4882a593Smuzhiyun 	struct ocfs2_extent_rec *rec, *tmprec;
4081*4882a593Smuzhiyun 
4082*4882a593Smuzhiyun 	right_el = path_leaf_el(right_path);
4083*4882a593Smuzhiyun 	if (left_path)
4084*4882a593Smuzhiyun 		left_el = path_leaf_el(left_path);
4085*4882a593Smuzhiyun 
4086*4882a593Smuzhiyun 	el = right_el;
4087*4882a593Smuzhiyun 	insert_el = right_el;
4088*4882a593Smuzhiyun 	index = ocfs2_search_extent_list(el, cpos);
4089*4882a593Smuzhiyun 	if (index != -1) {
4090*4882a593Smuzhiyun 		if (index == 0 && left_path) {
4091*4882a593Smuzhiyun 			BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
4092*4882a593Smuzhiyun 
4093*4882a593Smuzhiyun 			/*
4094*4882a593Smuzhiyun 			 * This typically means that the record
4095*4882a593Smuzhiyun 			 * started in the left path but moved to the
4096*4882a593Smuzhiyun 			 * right as a result of rotation. We either
4097*4882a593Smuzhiyun 			 * move the existing record to the left, or we
4098*4882a593Smuzhiyun 			 * do the later insert there.
4099*4882a593Smuzhiyun 			 *
4100*4882a593Smuzhiyun 			 * In this case, the left path should always
4101*4882a593Smuzhiyun 			 * exist as the rotate code will have passed
4102*4882a593Smuzhiyun 			 * it back for a post-insert update.
4103*4882a593Smuzhiyun 			 */
4104*4882a593Smuzhiyun 
4105*4882a593Smuzhiyun 			if (split == SPLIT_LEFT) {
4106*4882a593Smuzhiyun 				/*
4107*4882a593Smuzhiyun 				 * It's a left split. Since we know
4108*4882a593Smuzhiyun 				 * that the rotate code gave us an
4109*4882a593Smuzhiyun 				 * empty extent in the left path, we
4110*4882a593Smuzhiyun 				 * can just do the insert there.
4111*4882a593Smuzhiyun 				 */
4112*4882a593Smuzhiyun 				insert_el = left_el;
4113*4882a593Smuzhiyun 			} else {
4114*4882a593Smuzhiyun 				/*
4115*4882a593Smuzhiyun 				 * Right split - we have to move the
4116*4882a593Smuzhiyun 				 * existing record over to the left
4117*4882a593Smuzhiyun 				 * leaf. The insert will be into the
4118*4882a593Smuzhiyun 				 * newly created empty extent in the
4119*4882a593Smuzhiyun 				 * right leaf.
4120*4882a593Smuzhiyun 				 */
4121*4882a593Smuzhiyun 				tmprec = &right_el->l_recs[index];
4122*4882a593Smuzhiyun 				ocfs2_rotate_leaf(left_el, tmprec);
4123*4882a593Smuzhiyun 				el = left_el;
4124*4882a593Smuzhiyun 
4125*4882a593Smuzhiyun 				memset(tmprec, 0, sizeof(*tmprec));
4126*4882a593Smuzhiyun 				index = ocfs2_search_extent_list(left_el, cpos);
4127*4882a593Smuzhiyun 				BUG_ON(index == -1);
4128*4882a593Smuzhiyun 			}
4129*4882a593Smuzhiyun 		}
4130*4882a593Smuzhiyun 	} else {
4131*4882a593Smuzhiyun 		BUG_ON(!left_path);
4132*4882a593Smuzhiyun 		BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
4133*4882a593Smuzhiyun 		/*
4134*4882a593Smuzhiyun 		 * Left path is easy - we can just allow the insert to
4135*4882a593Smuzhiyun 		 * happen.
4136*4882a593Smuzhiyun 		 */
4137*4882a593Smuzhiyun 		el = left_el;
4138*4882a593Smuzhiyun 		insert_el = left_el;
4139*4882a593Smuzhiyun 		index = ocfs2_search_extent_list(el, cpos);
4140*4882a593Smuzhiyun 		BUG_ON(index == -1);
4141*4882a593Smuzhiyun 	}
4142*4882a593Smuzhiyun 
4143*4882a593Smuzhiyun 	rec = &el->l_recs[index];
4144*4882a593Smuzhiyun 	ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
4145*4882a593Smuzhiyun 				split, rec, split_rec);
4146*4882a593Smuzhiyun 	ocfs2_rotate_leaf(insert_el, split_rec);
4147*4882a593Smuzhiyun }
4148*4882a593Smuzhiyun 
4149*4882a593Smuzhiyun /*
4150*4882a593Smuzhiyun  * This function only does inserts on an allocation b-tree. For tree
4151*4882a593Smuzhiyun  * depth = 0, ocfs2_insert_at_leaf() is called directly.
4152*4882a593Smuzhiyun  *
4153*4882a593Smuzhiyun  * right_path is the path we want to do the actual insert
4154*4882a593Smuzhiyun  * in. left_path should only be passed in if we need to update that
4155*4882a593Smuzhiyun  * portion of the tree after an edge insert.
4156*4882a593Smuzhiyun  */
ocfs2_insert_path(handle_t * handle,struct ocfs2_extent_tree * et,struct ocfs2_path * left_path,struct ocfs2_path * right_path,struct ocfs2_extent_rec * insert_rec,struct ocfs2_insert_type * insert)4157*4882a593Smuzhiyun static int ocfs2_insert_path(handle_t *handle,
4158*4882a593Smuzhiyun 			     struct ocfs2_extent_tree *et,
4159*4882a593Smuzhiyun 			     struct ocfs2_path *left_path,
4160*4882a593Smuzhiyun 			     struct ocfs2_path *right_path,
4161*4882a593Smuzhiyun 			     struct ocfs2_extent_rec *insert_rec,
4162*4882a593Smuzhiyun 			     struct ocfs2_insert_type *insert)
4163*4882a593Smuzhiyun {
4164*4882a593Smuzhiyun 	int ret, subtree_index;
4165*4882a593Smuzhiyun 	struct buffer_head *leaf_bh = path_leaf_bh(right_path);
4166*4882a593Smuzhiyun 
4167*4882a593Smuzhiyun 	if (left_path) {
4168*4882a593Smuzhiyun 		/*
4169*4882a593Smuzhiyun 		 * There's a chance that left_path got passed back to
4170*4882a593Smuzhiyun 		 * us without being accounted for in the
4171*4882a593Smuzhiyun 		 * journal. Extend our transaction here to be sure we
4172*4882a593Smuzhiyun 		 * can change those blocks.
4173*4882a593Smuzhiyun 		 */
4174*4882a593Smuzhiyun 		ret = ocfs2_extend_trans(handle, left_path->p_tree_depth);
4175*4882a593Smuzhiyun 		if (ret < 0) {
4176*4882a593Smuzhiyun 			mlog_errno(ret);
4177*4882a593Smuzhiyun 			goto out;
4178*4882a593Smuzhiyun 		}
4179*4882a593Smuzhiyun 
4180*4882a593Smuzhiyun 		ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
4181*4882a593Smuzhiyun 		if (ret < 0) {
4182*4882a593Smuzhiyun 			mlog_errno(ret);
4183*4882a593Smuzhiyun 			goto out;
4184*4882a593Smuzhiyun 		}
4185*4882a593Smuzhiyun 	}
4186*4882a593Smuzhiyun 
4187*4882a593Smuzhiyun 	/*
4188*4882a593Smuzhiyun 	 * Pass both paths to the journal. The majority of inserts
4189*4882a593Smuzhiyun 	 * will be touching all components anyway.
4190*4882a593Smuzhiyun 	 */
4191*4882a593Smuzhiyun 	ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
4192*4882a593Smuzhiyun 	if (ret < 0) {
4193*4882a593Smuzhiyun 		mlog_errno(ret);
4194*4882a593Smuzhiyun 		goto out;
4195*4882a593Smuzhiyun 	}
4196*4882a593Smuzhiyun 
4197*4882a593Smuzhiyun 	if (insert->ins_split != SPLIT_NONE) {
4198*4882a593Smuzhiyun 		/*
4199*4882a593Smuzhiyun 		 * We could call ocfs2_insert_at_leaf() for some types
4200*4882a593Smuzhiyun 		 * of splits, but it's easier to just let one separate
4201*4882a593Smuzhiyun 		 * function sort it all out.
4202*4882a593Smuzhiyun 		 */
4203*4882a593Smuzhiyun 		ocfs2_split_record(et, left_path, right_path,
4204*4882a593Smuzhiyun 				   insert_rec, insert->ins_split);
4205*4882a593Smuzhiyun 
4206*4882a593Smuzhiyun 		/*
4207*4882a593Smuzhiyun 		 * Split might have modified either leaf and we don't
4208*4882a593Smuzhiyun 		 * have a guarantee that the later edge insert will
4209*4882a593Smuzhiyun 		 * dirty this for us.
4210*4882a593Smuzhiyun 		 */
4211*4882a593Smuzhiyun 		if (left_path)
4212*4882a593Smuzhiyun 			ocfs2_journal_dirty(handle,
4213*4882a593Smuzhiyun 					    path_leaf_bh(left_path));
4214*4882a593Smuzhiyun 	} else
4215*4882a593Smuzhiyun 		ocfs2_insert_at_leaf(et, insert_rec, path_leaf_el(right_path),
4216*4882a593Smuzhiyun 				     insert);
4217*4882a593Smuzhiyun 
4218*4882a593Smuzhiyun 	ocfs2_journal_dirty(handle, leaf_bh);
4219*4882a593Smuzhiyun 
4220*4882a593Smuzhiyun 	if (left_path) {
4221*4882a593Smuzhiyun 		/*
4222*4882a593Smuzhiyun 		 * The rotate code has indicated that we need to fix
4223*4882a593Smuzhiyun 		 * up portions of the tree after the insert.
4224*4882a593Smuzhiyun 		 *
4225*4882a593Smuzhiyun 		 * XXX: Should we extend the transaction here?
4226*4882a593Smuzhiyun 		 */
4227*4882a593Smuzhiyun 		subtree_index = ocfs2_find_subtree_root(et, left_path,
4228*4882a593Smuzhiyun 							right_path);
4229*4882a593Smuzhiyun 		ocfs2_complete_edge_insert(handle, left_path, right_path,
4230*4882a593Smuzhiyun 					   subtree_index);
4231*4882a593Smuzhiyun 	}
4232*4882a593Smuzhiyun 
4233*4882a593Smuzhiyun 	ret = 0;
4234*4882a593Smuzhiyun out:
4235*4882a593Smuzhiyun 	return ret;
4236*4882a593Smuzhiyun }
4237*4882a593Smuzhiyun 
ocfs2_do_insert_extent(handle_t * handle,struct ocfs2_extent_tree * et,struct ocfs2_extent_rec * insert_rec,struct ocfs2_insert_type * type)4238*4882a593Smuzhiyun static int ocfs2_do_insert_extent(handle_t *handle,
4239*4882a593Smuzhiyun 				  struct ocfs2_extent_tree *et,
4240*4882a593Smuzhiyun 				  struct ocfs2_extent_rec *insert_rec,
4241*4882a593Smuzhiyun 				  struct ocfs2_insert_type *type)
4242*4882a593Smuzhiyun {
4243*4882a593Smuzhiyun 	int ret, rotate = 0;
4244*4882a593Smuzhiyun 	u32 cpos;
4245*4882a593Smuzhiyun 	struct ocfs2_path *right_path = NULL;
4246*4882a593Smuzhiyun 	struct ocfs2_path *left_path = NULL;
4247*4882a593Smuzhiyun 	struct ocfs2_extent_list *el;
4248*4882a593Smuzhiyun 
4249*4882a593Smuzhiyun 	el = et->et_root_el;
4250*4882a593Smuzhiyun 
4251*4882a593Smuzhiyun 	ret = ocfs2_et_root_journal_access(handle, et,
4252*4882a593Smuzhiyun 					   OCFS2_JOURNAL_ACCESS_WRITE);
4253*4882a593Smuzhiyun 	if (ret) {
4254*4882a593Smuzhiyun 		mlog_errno(ret);
4255*4882a593Smuzhiyun 		goto out;
4256*4882a593Smuzhiyun 	}
4257*4882a593Smuzhiyun 
4258*4882a593Smuzhiyun 	if (le16_to_cpu(el->l_tree_depth) == 0) {
4259*4882a593Smuzhiyun 		ocfs2_insert_at_leaf(et, insert_rec, el, type);
4260*4882a593Smuzhiyun 		goto out_update_clusters;
4261*4882a593Smuzhiyun 	}
4262*4882a593Smuzhiyun 
4263*4882a593Smuzhiyun 	right_path = ocfs2_new_path_from_et(et);
4264*4882a593Smuzhiyun 	if (!right_path) {
4265*4882a593Smuzhiyun 		ret = -ENOMEM;
4266*4882a593Smuzhiyun 		mlog_errno(ret);
4267*4882a593Smuzhiyun 		goto out;
4268*4882a593Smuzhiyun 	}
4269*4882a593Smuzhiyun 
4270*4882a593Smuzhiyun 	/*
4271*4882a593Smuzhiyun 	 * Determine the path to start with. Rotations need the
4272*4882a593Smuzhiyun 	 * rightmost path, everything else can go directly to the
4273*4882a593Smuzhiyun 	 * target leaf.
4274*4882a593Smuzhiyun 	 */
4275*4882a593Smuzhiyun 	cpos = le32_to_cpu(insert_rec->e_cpos);
4276*4882a593Smuzhiyun 	if (type->ins_appending == APPEND_NONE &&
4277*4882a593Smuzhiyun 	    type->ins_contig == CONTIG_NONE) {
4278*4882a593Smuzhiyun 		rotate = 1;
4279*4882a593Smuzhiyun 		cpos = UINT_MAX;
4280*4882a593Smuzhiyun 	}
4281*4882a593Smuzhiyun 
4282*4882a593Smuzhiyun 	ret = ocfs2_find_path(et->et_ci, right_path, cpos);
4283*4882a593Smuzhiyun 	if (ret) {
4284*4882a593Smuzhiyun 		mlog_errno(ret);
4285*4882a593Smuzhiyun 		goto out;
4286*4882a593Smuzhiyun 	}
4287*4882a593Smuzhiyun 
4288*4882a593Smuzhiyun 	/*
4289*4882a593Smuzhiyun 	 * Rotations and appends need special treatment - they modify
4290*4882a593Smuzhiyun 	 * parts of the tree's above them.
4291*4882a593Smuzhiyun 	 *
4292*4882a593Smuzhiyun 	 * Both might pass back a path immediate to the left of the
4293*4882a593Smuzhiyun 	 * one being inserted to. This will be cause
4294*4882a593Smuzhiyun 	 * ocfs2_insert_path() to modify the rightmost records of
4295*4882a593Smuzhiyun 	 * left_path to account for an edge insert.
4296*4882a593Smuzhiyun 	 *
4297*4882a593Smuzhiyun 	 * XXX: When modifying this code, keep in mind that an insert
4298*4882a593Smuzhiyun 	 * can wind up skipping both of these two special cases...
4299*4882a593Smuzhiyun 	 */
4300*4882a593Smuzhiyun 	if (rotate) {
4301*4882a593Smuzhiyun 		ret = ocfs2_rotate_tree_right(handle, et, type->ins_split,
4302*4882a593Smuzhiyun 					      le32_to_cpu(insert_rec->e_cpos),
4303*4882a593Smuzhiyun 					      right_path, &left_path);
4304*4882a593Smuzhiyun 		if (ret) {
4305*4882a593Smuzhiyun 			mlog_errno(ret);
4306*4882a593Smuzhiyun 			goto out;
4307*4882a593Smuzhiyun 		}
4308*4882a593Smuzhiyun 
4309*4882a593Smuzhiyun 		/*
4310*4882a593Smuzhiyun 		 * ocfs2_rotate_tree_right() might have extended the
4311*4882a593Smuzhiyun 		 * transaction without re-journaling our tree root.
4312*4882a593Smuzhiyun 		 */
4313*4882a593Smuzhiyun 		ret = ocfs2_et_root_journal_access(handle, et,
4314*4882a593Smuzhiyun 						   OCFS2_JOURNAL_ACCESS_WRITE);
4315*4882a593Smuzhiyun 		if (ret) {
4316*4882a593Smuzhiyun 			mlog_errno(ret);
4317*4882a593Smuzhiyun 			goto out;
4318*4882a593Smuzhiyun 		}
4319*4882a593Smuzhiyun 	} else if (type->ins_appending == APPEND_TAIL
4320*4882a593Smuzhiyun 		   && type->ins_contig != CONTIG_LEFT) {
4321*4882a593Smuzhiyun 		ret = ocfs2_append_rec_to_path(handle, et, insert_rec,
4322*4882a593Smuzhiyun 					       right_path, &left_path);
4323*4882a593Smuzhiyun 		if (ret) {
4324*4882a593Smuzhiyun 			mlog_errno(ret);
4325*4882a593Smuzhiyun 			goto out;
4326*4882a593Smuzhiyun 		}
4327*4882a593Smuzhiyun 	}
4328*4882a593Smuzhiyun 
4329*4882a593Smuzhiyun 	ret = ocfs2_insert_path(handle, et, left_path, right_path,
4330*4882a593Smuzhiyun 				insert_rec, type);
4331*4882a593Smuzhiyun 	if (ret) {
4332*4882a593Smuzhiyun 		mlog_errno(ret);
4333*4882a593Smuzhiyun 		goto out;
4334*4882a593Smuzhiyun 	}
4335*4882a593Smuzhiyun 
4336*4882a593Smuzhiyun out_update_clusters:
4337*4882a593Smuzhiyun 	if (type->ins_split == SPLIT_NONE)
4338*4882a593Smuzhiyun 		ocfs2_et_update_clusters(et,
4339*4882a593Smuzhiyun 					 le16_to_cpu(insert_rec->e_leaf_clusters));
4340*4882a593Smuzhiyun 
4341*4882a593Smuzhiyun 	ocfs2_journal_dirty(handle, et->et_root_bh);
4342*4882a593Smuzhiyun 
4343*4882a593Smuzhiyun out:
4344*4882a593Smuzhiyun 	ocfs2_free_path(left_path);
4345*4882a593Smuzhiyun 	ocfs2_free_path(right_path);
4346*4882a593Smuzhiyun 
4347*4882a593Smuzhiyun 	return ret;
4348*4882a593Smuzhiyun }
4349*4882a593Smuzhiyun 
ocfs2_figure_merge_contig_type(struct ocfs2_extent_tree * et,struct ocfs2_path * path,struct ocfs2_extent_list * el,int index,struct ocfs2_extent_rec * split_rec,struct ocfs2_merge_ctxt * ctxt)4350*4882a593Smuzhiyun static int ocfs2_figure_merge_contig_type(struct ocfs2_extent_tree *et,
4351*4882a593Smuzhiyun 			       struct ocfs2_path *path,
4352*4882a593Smuzhiyun 			       struct ocfs2_extent_list *el, int index,
4353*4882a593Smuzhiyun 			       struct ocfs2_extent_rec *split_rec,
4354*4882a593Smuzhiyun 			       struct ocfs2_merge_ctxt *ctxt)
4355*4882a593Smuzhiyun {
4356*4882a593Smuzhiyun 	int status = 0;
4357*4882a593Smuzhiyun 	enum ocfs2_contig_type ret = CONTIG_NONE;
4358*4882a593Smuzhiyun 	u32 left_cpos, right_cpos;
4359*4882a593Smuzhiyun 	struct ocfs2_extent_rec *rec = NULL;
4360*4882a593Smuzhiyun 	struct ocfs2_extent_list *new_el;
4361*4882a593Smuzhiyun 	struct ocfs2_path *left_path = NULL, *right_path = NULL;
4362*4882a593Smuzhiyun 	struct buffer_head *bh;
4363*4882a593Smuzhiyun 	struct ocfs2_extent_block *eb;
4364*4882a593Smuzhiyun 	struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
4365*4882a593Smuzhiyun 
4366*4882a593Smuzhiyun 	if (index > 0) {
4367*4882a593Smuzhiyun 		rec = &el->l_recs[index - 1];
4368*4882a593Smuzhiyun 	} else if (path->p_tree_depth > 0) {
4369*4882a593Smuzhiyun 		status = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
4370*4882a593Smuzhiyun 		if (status)
4371*4882a593Smuzhiyun 			goto exit;
4372*4882a593Smuzhiyun 
4373*4882a593Smuzhiyun 		if (left_cpos != 0) {
4374*4882a593Smuzhiyun 			left_path = ocfs2_new_path_from_path(path);
4375*4882a593Smuzhiyun 			if (!left_path) {
4376*4882a593Smuzhiyun 				status = -ENOMEM;
4377*4882a593Smuzhiyun 				mlog_errno(status);
4378*4882a593Smuzhiyun 				goto exit;
4379*4882a593Smuzhiyun 			}
4380*4882a593Smuzhiyun 
4381*4882a593Smuzhiyun 			status = ocfs2_find_path(et->et_ci, left_path,
4382*4882a593Smuzhiyun 						 left_cpos);
4383*4882a593Smuzhiyun 			if (status)
4384*4882a593Smuzhiyun 				goto free_left_path;
4385*4882a593Smuzhiyun 
4386*4882a593Smuzhiyun 			new_el = path_leaf_el(left_path);
4387*4882a593Smuzhiyun 
4388*4882a593Smuzhiyun 			if (le16_to_cpu(new_el->l_next_free_rec) !=
4389*4882a593Smuzhiyun 			    le16_to_cpu(new_el->l_count)) {
4390*4882a593Smuzhiyun 				bh = path_leaf_bh(left_path);
4391*4882a593Smuzhiyun 				eb = (struct ocfs2_extent_block *)bh->b_data;
4392*4882a593Smuzhiyun 				status = ocfs2_error(sb,
4393*4882a593Smuzhiyun 						"Extent block #%llu has an invalid l_next_free_rec of %d.  It should have matched the l_count of %d\n",
4394*4882a593Smuzhiyun 						(unsigned long long)le64_to_cpu(eb->h_blkno),
4395*4882a593Smuzhiyun 						le16_to_cpu(new_el->l_next_free_rec),
4396*4882a593Smuzhiyun 						le16_to_cpu(new_el->l_count));
4397*4882a593Smuzhiyun 				goto free_left_path;
4398*4882a593Smuzhiyun 			}
4399*4882a593Smuzhiyun 			rec = &new_el->l_recs[
4400*4882a593Smuzhiyun 				le16_to_cpu(new_el->l_next_free_rec) - 1];
4401*4882a593Smuzhiyun 		}
4402*4882a593Smuzhiyun 	}
4403*4882a593Smuzhiyun 
4404*4882a593Smuzhiyun 	/*
4405*4882a593Smuzhiyun 	 * We're careful to check for an empty extent record here -
4406*4882a593Smuzhiyun 	 * the merge code will know what to do if it sees one.
4407*4882a593Smuzhiyun 	 */
4408*4882a593Smuzhiyun 	if (rec) {
4409*4882a593Smuzhiyun 		if (index == 1 && ocfs2_is_empty_extent(rec)) {
4410*4882a593Smuzhiyun 			if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4411*4882a593Smuzhiyun 				ret = CONTIG_RIGHT;
4412*4882a593Smuzhiyun 		} else {
4413*4882a593Smuzhiyun 			ret = ocfs2_et_extent_contig(et, rec, split_rec);
4414*4882a593Smuzhiyun 		}
4415*4882a593Smuzhiyun 	}
4416*4882a593Smuzhiyun 
4417*4882a593Smuzhiyun 	rec = NULL;
4418*4882a593Smuzhiyun 	if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4419*4882a593Smuzhiyun 		rec = &el->l_recs[index + 1];
4420*4882a593Smuzhiyun 	else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4421*4882a593Smuzhiyun 		 path->p_tree_depth > 0) {
4422*4882a593Smuzhiyun 		status = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
4423*4882a593Smuzhiyun 		if (status)
4424*4882a593Smuzhiyun 			goto free_left_path;
4425*4882a593Smuzhiyun 
4426*4882a593Smuzhiyun 		if (right_cpos == 0)
4427*4882a593Smuzhiyun 			goto free_left_path;
4428*4882a593Smuzhiyun 
4429*4882a593Smuzhiyun 		right_path = ocfs2_new_path_from_path(path);
4430*4882a593Smuzhiyun 		if (!right_path) {
4431*4882a593Smuzhiyun 			status = -ENOMEM;
4432*4882a593Smuzhiyun 			mlog_errno(status);
4433*4882a593Smuzhiyun 			goto free_left_path;
4434*4882a593Smuzhiyun 		}
4435*4882a593Smuzhiyun 
4436*4882a593Smuzhiyun 		status = ocfs2_find_path(et->et_ci, right_path, right_cpos);
4437*4882a593Smuzhiyun 		if (status)
4438*4882a593Smuzhiyun 			goto free_right_path;
4439*4882a593Smuzhiyun 
4440*4882a593Smuzhiyun 		new_el = path_leaf_el(right_path);
4441*4882a593Smuzhiyun 		rec = &new_el->l_recs[0];
4442*4882a593Smuzhiyun 		if (ocfs2_is_empty_extent(rec)) {
4443*4882a593Smuzhiyun 			if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4444*4882a593Smuzhiyun 				bh = path_leaf_bh(right_path);
4445*4882a593Smuzhiyun 				eb = (struct ocfs2_extent_block *)bh->b_data;
4446*4882a593Smuzhiyun 				status = ocfs2_error(sb,
4447*4882a593Smuzhiyun 						"Extent block #%llu has an invalid l_next_free_rec of %d\n",
4448*4882a593Smuzhiyun 						(unsigned long long)le64_to_cpu(eb->h_blkno),
4449*4882a593Smuzhiyun 						le16_to_cpu(new_el->l_next_free_rec));
4450*4882a593Smuzhiyun 				goto free_right_path;
4451*4882a593Smuzhiyun 			}
4452*4882a593Smuzhiyun 			rec = &new_el->l_recs[1];
4453*4882a593Smuzhiyun 		}
4454*4882a593Smuzhiyun 	}
4455*4882a593Smuzhiyun 
4456*4882a593Smuzhiyun 	if (rec) {
4457*4882a593Smuzhiyun 		enum ocfs2_contig_type contig_type;
4458*4882a593Smuzhiyun 
4459*4882a593Smuzhiyun 		contig_type = ocfs2_et_extent_contig(et, rec, split_rec);
4460*4882a593Smuzhiyun 
4461*4882a593Smuzhiyun 		if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4462*4882a593Smuzhiyun 			ret = CONTIG_LEFTRIGHT;
4463*4882a593Smuzhiyun 		else if (ret == CONTIG_NONE)
4464*4882a593Smuzhiyun 			ret = contig_type;
4465*4882a593Smuzhiyun 	}
4466*4882a593Smuzhiyun 
4467*4882a593Smuzhiyun free_right_path:
4468*4882a593Smuzhiyun 	ocfs2_free_path(right_path);
4469*4882a593Smuzhiyun free_left_path:
4470*4882a593Smuzhiyun 	ocfs2_free_path(left_path);
4471*4882a593Smuzhiyun exit:
4472*4882a593Smuzhiyun 	if (status == 0)
4473*4882a593Smuzhiyun 		ctxt->c_contig_type = ret;
4474*4882a593Smuzhiyun 
4475*4882a593Smuzhiyun 	return status;
4476*4882a593Smuzhiyun }
4477*4882a593Smuzhiyun 
ocfs2_figure_contig_type(struct ocfs2_extent_tree * et,struct ocfs2_insert_type * insert,struct ocfs2_extent_list * el,struct ocfs2_extent_rec * insert_rec)4478*4882a593Smuzhiyun static void ocfs2_figure_contig_type(struct ocfs2_extent_tree *et,
4479*4882a593Smuzhiyun 				     struct ocfs2_insert_type *insert,
4480*4882a593Smuzhiyun 				     struct ocfs2_extent_list *el,
4481*4882a593Smuzhiyun 				     struct ocfs2_extent_rec *insert_rec)
4482*4882a593Smuzhiyun {
4483*4882a593Smuzhiyun 	int i;
4484*4882a593Smuzhiyun 	enum ocfs2_contig_type contig_type = CONTIG_NONE;
4485*4882a593Smuzhiyun 
4486*4882a593Smuzhiyun 	BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4487*4882a593Smuzhiyun 
4488*4882a593Smuzhiyun 	for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4489*4882a593Smuzhiyun 		contig_type = ocfs2_et_extent_contig(et, &el->l_recs[i],
4490*4882a593Smuzhiyun 						     insert_rec);
4491*4882a593Smuzhiyun 		if (contig_type != CONTIG_NONE) {
4492*4882a593Smuzhiyun 			insert->ins_contig_index = i;
4493*4882a593Smuzhiyun 			break;
4494*4882a593Smuzhiyun 		}
4495*4882a593Smuzhiyun 	}
4496*4882a593Smuzhiyun 	insert->ins_contig = contig_type;
4497*4882a593Smuzhiyun 
4498*4882a593Smuzhiyun 	if (insert->ins_contig != CONTIG_NONE) {
4499*4882a593Smuzhiyun 		struct ocfs2_extent_rec *rec =
4500*4882a593Smuzhiyun 				&el->l_recs[insert->ins_contig_index];
4501*4882a593Smuzhiyun 		unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4502*4882a593Smuzhiyun 				   le16_to_cpu(insert_rec->e_leaf_clusters);
4503*4882a593Smuzhiyun 
4504*4882a593Smuzhiyun 		/*
4505*4882a593Smuzhiyun 		 * Caller might want us to limit the size of extents, don't
4506*4882a593Smuzhiyun 		 * calculate contiguousness if we might exceed that limit.
4507*4882a593Smuzhiyun 		 */
4508*4882a593Smuzhiyun 		if (et->et_max_leaf_clusters &&
4509*4882a593Smuzhiyun 		    (len > et->et_max_leaf_clusters))
4510*4882a593Smuzhiyun 			insert->ins_contig = CONTIG_NONE;
4511*4882a593Smuzhiyun 	}
4512*4882a593Smuzhiyun }
4513*4882a593Smuzhiyun 
4514*4882a593Smuzhiyun /*
4515*4882a593Smuzhiyun  * This should only be called against the righmost leaf extent list.
4516*4882a593Smuzhiyun  *
4517*4882a593Smuzhiyun  * ocfs2_figure_appending_type() will figure out whether we'll have to
4518*4882a593Smuzhiyun  * insert at the tail of the rightmost leaf.
4519*4882a593Smuzhiyun  *
4520*4882a593Smuzhiyun  * This should also work against the root extent list for tree's with 0
4521*4882a593Smuzhiyun  * depth. If we consider the root extent list to be the rightmost leaf node
4522*4882a593Smuzhiyun  * then the logic here makes sense.
4523*4882a593Smuzhiyun  */
ocfs2_figure_appending_type(struct ocfs2_insert_type * insert,struct ocfs2_extent_list * el,struct ocfs2_extent_rec * insert_rec)4524*4882a593Smuzhiyun static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4525*4882a593Smuzhiyun 					struct ocfs2_extent_list *el,
4526*4882a593Smuzhiyun 					struct ocfs2_extent_rec *insert_rec)
4527*4882a593Smuzhiyun {
4528*4882a593Smuzhiyun 	int i;
4529*4882a593Smuzhiyun 	u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4530*4882a593Smuzhiyun 	struct ocfs2_extent_rec *rec;
4531*4882a593Smuzhiyun 
4532*4882a593Smuzhiyun 	insert->ins_appending = APPEND_NONE;
4533*4882a593Smuzhiyun 
4534*4882a593Smuzhiyun 	BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4535*4882a593Smuzhiyun 
4536*4882a593Smuzhiyun 	if (!el->l_next_free_rec)
4537*4882a593Smuzhiyun 		goto set_tail_append;
4538*4882a593Smuzhiyun 
4539*4882a593Smuzhiyun 	if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4540*4882a593Smuzhiyun 		/* Were all records empty? */
4541*4882a593Smuzhiyun 		if (le16_to_cpu(el->l_next_free_rec) == 1)
4542*4882a593Smuzhiyun 			goto set_tail_append;
4543*4882a593Smuzhiyun 	}
4544*4882a593Smuzhiyun 
4545*4882a593Smuzhiyun 	i = le16_to_cpu(el->l_next_free_rec) - 1;
4546*4882a593Smuzhiyun 	rec = &el->l_recs[i];
4547*4882a593Smuzhiyun 
4548*4882a593Smuzhiyun 	if (cpos >=
4549*4882a593Smuzhiyun 	    (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
4550*4882a593Smuzhiyun 		goto set_tail_append;
4551*4882a593Smuzhiyun 
4552*4882a593Smuzhiyun 	return;
4553*4882a593Smuzhiyun 
4554*4882a593Smuzhiyun set_tail_append:
4555*4882a593Smuzhiyun 	insert->ins_appending = APPEND_TAIL;
4556*4882a593Smuzhiyun }
4557*4882a593Smuzhiyun 
4558*4882a593Smuzhiyun /*
4559*4882a593Smuzhiyun  * Helper function called at the beginning of an insert.
4560*4882a593Smuzhiyun  *
4561*4882a593Smuzhiyun  * This computes a few things that are commonly used in the process of
4562*4882a593Smuzhiyun  * inserting into the btree:
4563*4882a593Smuzhiyun  *   - Whether the new extent is contiguous with an existing one.
4564*4882a593Smuzhiyun  *   - The current tree depth.
4565*4882a593Smuzhiyun  *   - Whether the insert is an appending one.
4566*4882a593Smuzhiyun  *   - The total # of free records in the tree.
4567*4882a593Smuzhiyun  *
4568*4882a593Smuzhiyun  * All of the information is stored on the ocfs2_insert_type
4569*4882a593Smuzhiyun  * structure.
4570*4882a593Smuzhiyun  */
ocfs2_figure_insert_type(struct ocfs2_extent_tree * et,struct buffer_head ** last_eb_bh,struct ocfs2_extent_rec * insert_rec,int * free_records,struct ocfs2_insert_type * insert)4571*4882a593Smuzhiyun static int ocfs2_figure_insert_type(struct ocfs2_extent_tree *et,
4572*4882a593Smuzhiyun 				    struct buffer_head **last_eb_bh,
4573*4882a593Smuzhiyun 				    struct ocfs2_extent_rec *insert_rec,
4574*4882a593Smuzhiyun 				    int *free_records,
4575*4882a593Smuzhiyun 				    struct ocfs2_insert_type *insert)
4576*4882a593Smuzhiyun {
4577*4882a593Smuzhiyun 	int ret;
4578*4882a593Smuzhiyun 	struct ocfs2_extent_block *eb;
4579*4882a593Smuzhiyun 	struct ocfs2_extent_list *el;
4580*4882a593Smuzhiyun 	struct ocfs2_path *path = NULL;
4581*4882a593Smuzhiyun 	struct buffer_head *bh = NULL;
4582*4882a593Smuzhiyun 
4583*4882a593Smuzhiyun 	insert->ins_split = SPLIT_NONE;
4584*4882a593Smuzhiyun 
4585*4882a593Smuzhiyun 	el = et->et_root_el;
4586*4882a593Smuzhiyun 	insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4587*4882a593Smuzhiyun 
4588*4882a593Smuzhiyun 	if (el->l_tree_depth) {
4589*4882a593Smuzhiyun 		/*
4590*4882a593Smuzhiyun 		 * If we have tree depth, we read in the
4591*4882a593Smuzhiyun 		 * rightmost extent block ahead of time as
4592*4882a593Smuzhiyun 		 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4593*4882a593Smuzhiyun 		 * may want it later.
4594*4882a593Smuzhiyun 		 */
4595*4882a593Smuzhiyun 		ret = ocfs2_read_extent_block(et->et_ci,
4596*4882a593Smuzhiyun 					      ocfs2_et_get_last_eb_blk(et),
4597*4882a593Smuzhiyun 					      &bh);
4598*4882a593Smuzhiyun 		if (ret) {
4599*4882a593Smuzhiyun 			mlog_errno(ret);
4600*4882a593Smuzhiyun 			goto out;
4601*4882a593Smuzhiyun 		}
4602*4882a593Smuzhiyun 		eb = (struct ocfs2_extent_block *) bh->b_data;
4603*4882a593Smuzhiyun 		el = &eb->h_list;
4604*4882a593Smuzhiyun 	}
4605*4882a593Smuzhiyun 
4606*4882a593Smuzhiyun 	/*
4607*4882a593Smuzhiyun 	 * Unless we have a contiguous insert, we'll need to know if
4608*4882a593Smuzhiyun 	 * there is room left in our allocation tree for another
4609*4882a593Smuzhiyun 	 * extent record.
4610*4882a593Smuzhiyun 	 *
4611*4882a593Smuzhiyun 	 * XXX: This test is simplistic, we can search for empty
4612*4882a593Smuzhiyun 	 * extent records too.
4613*4882a593Smuzhiyun 	 */
4614*4882a593Smuzhiyun 	*free_records = le16_to_cpu(el->l_count) -
4615*4882a593Smuzhiyun 		le16_to_cpu(el->l_next_free_rec);
4616*4882a593Smuzhiyun 
4617*4882a593Smuzhiyun 	if (!insert->ins_tree_depth) {
4618*4882a593Smuzhiyun 		ocfs2_figure_contig_type(et, insert, el, insert_rec);
4619*4882a593Smuzhiyun 		ocfs2_figure_appending_type(insert, el, insert_rec);
4620*4882a593Smuzhiyun 		return 0;
4621*4882a593Smuzhiyun 	}
4622*4882a593Smuzhiyun 
4623*4882a593Smuzhiyun 	path = ocfs2_new_path_from_et(et);
4624*4882a593Smuzhiyun 	if (!path) {
4625*4882a593Smuzhiyun 		ret = -ENOMEM;
4626*4882a593Smuzhiyun 		mlog_errno(ret);
4627*4882a593Smuzhiyun 		goto out;
4628*4882a593Smuzhiyun 	}
4629*4882a593Smuzhiyun 
4630*4882a593Smuzhiyun 	/*
4631*4882a593Smuzhiyun 	 * In the case that we're inserting past what the tree
4632*4882a593Smuzhiyun 	 * currently accounts for, ocfs2_find_path() will return for
4633*4882a593Smuzhiyun 	 * us the rightmost tree path. This is accounted for below in
4634*4882a593Smuzhiyun 	 * the appending code.
4635*4882a593Smuzhiyun 	 */
4636*4882a593Smuzhiyun 	ret = ocfs2_find_path(et->et_ci, path, le32_to_cpu(insert_rec->e_cpos));
4637*4882a593Smuzhiyun 	if (ret) {
4638*4882a593Smuzhiyun 		mlog_errno(ret);
4639*4882a593Smuzhiyun 		goto out;
4640*4882a593Smuzhiyun 	}
4641*4882a593Smuzhiyun 
4642*4882a593Smuzhiyun 	el = path_leaf_el(path);
4643*4882a593Smuzhiyun 
4644*4882a593Smuzhiyun 	/*
4645*4882a593Smuzhiyun 	 * Now that we have the path, there's two things we want to determine:
4646*4882a593Smuzhiyun 	 * 1) Contiguousness (also set contig_index if this is so)
4647*4882a593Smuzhiyun 	 *
4648*4882a593Smuzhiyun 	 * 2) Are we doing an append? We can trivially break this up
4649*4882a593Smuzhiyun          *     into two types of appends: simple record append, or a
4650*4882a593Smuzhiyun          *     rotate inside the tail leaf.
4651*4882a593Smuzhiyun 	 */
4652*4882a593Smuzhiyun 	ocfs2_figure_contig_type(et, insert, el, insert_rec);
4653*4882a593Smuzhiyun 
4654*4882a593Smuzhiyun 	/*
4655*4882a593Smuzhiyun 	 * The insert code isn't quite ready to deal with all cases of
4656*4882a593Smuzhiyun 	 * left contiguousness. Specifically, if it's an insert into
4657*4882a593Smuzhiyun 	 * the 1st record in a leaf, it will require the adjustment of
4658*4882a593Smuzhiyun 	 * cluster count on the last record of the path directly to it's
4659*4882a593Smuzhiyun 	 * left. For now, just catch that case and fool the layers
4660*4882a593Smuzhiyun 	 * above us. This works just fine for tree_depth == 0, which
4661*4882a593Smuzhiyun 	 * is why we allow that above.
4662*4882a593Smuzhiyun 	 */
4663*4882a593Smuzhiyun 	if (insert->ins_contig == CONTIG_LEFT &&
4664*4882a593Smuzhiyun 	    insert->ins_contig_index == 0)
4665*4882a593Smuzhiyun 		insert->ins_contig = CONTIG_NONE;
4666*4882a593Smuzhiyun 
4667*4882a593Smuzhiyun 	/*
4668*4882a593Smuzhiyun 	 * Ok, so we can simply compare against last_eb to figure out
4669*4882a593Smuzhiyun 	 * whether the path doesn't exist. This will only happen in
4670*4882a593Smuzhiyun 	 * the case that we're doing a tail append, so maybe we can
4671*4882a593Smuzhiyun 	 * take advantage of that information somehow.
4672*4882a593Smuzhiyun 	 */
4673*4882a593Smuzhiyun 	if (ocfs2_et_get_last_eb_blk(et) ==
4674*4882a593Smuzhiyun 	    path_leaf_bh(path)->b_blocknr) {
4675*4882a593Smuzhiyun 		/*
4676*4882a593Smuzhiyun 		 * Ok, ocfs2_find_path() returned us the rightmost
4677*4882a593Smuzhiyun 		 * tree path. This might be an appending insert. There are
4678*4882a593Smuzhiyun 		 * two cases:
4679*4882a593Smuzhiyun 		 *    1) We're doing a true append at the tail:
4680*4882a593Smuzhiyun 		 *	-This might even be off the end of the leaf
4681*4882a593Smuzhiyun 		 *    2) We're "appending" by rotating in the tail
4682*4882a593Smuzhiyun 		 */
4683*4882a593Smuzhiyun 		ocfs2_figure_appending_type(insert, el, insert_rec);
4684*4882a593Smuzhiyun 	}
4685*4882a593Smuzhiyun 
4686*4882a593Smuzhiyun out:
4687*4882a593Smuzhiyun 	ocfs2_free_path(path);
4688*4882a593Smuzhiyun 
4689*4882a593Smuzhiyun 	if (ret == 0)
4690*4882a593Smuzhiyun 		*last_eb_bh = bh;
4691*4882a593Smuzhiyun 	else
4692*4882a593Smuzhiyun 		brelse(bh);
4693*4882a593Smuzhiyun 	return ret;
4694*4882a593Smuzhiyun }
4695*4882a593Smuzhiyun 
4696*4882a593Smuzhiyun /*
4697*4882a593Smuzhiyun  * Insert an extent into a btree.
4698*4882a593Smuzhiyun  *
4699*4882a593Smuzhiyun  * The caller needs to update the owning btree's cluster count.
4700*4882a593Smuzhiyun  */
ocfs2_insert_extent(handle_t * handle,struct ocfs2_extent_tree * et,u32 cpos,u64 start_blk,u32 new_clusters,u8 flags,struct ocfs2_alloc_context * meta_ac)4701*4882a593Smuzhiyun int ocfs2_insert_extent(handle_t *handle,
4702*4882a593Smuzhiyun 			struct ocfs2_extent_tree *et,
4703*4882a593Smuzhiyun 			u32 cpos,
4704*4882a593Smuzhiyun 			u64 start_blk,
4705*4882a593Smuzhiyun 			u32 new_clusters,
4706*4882a593Smuzhiyun 			u8 flags,
4707*4882a593Smuzhiyun 			struct ocfs2_alloc_context *meta_ac)
4708*4882a593Smuzhiyun {
4709*4882a593Smuzhiyun 	int status;
4710*4882a593Smuzhiyun 	int free_records;
4711*4882a593Smuzhiyun 	struct buffer_head *last_eb_bh = NULL;
4712*4882a593Smuzhiyun 	struct ocfs2_insert_type insert = {0, };
4713*4882a593Smuzhiyun 	struct ocfs2_extent_rec rec;
4714*4882a593Smuzhiyun 
4715*4882a593Smuzhiyun 	trace_ocfs2_insert_extent_start(
4716*4882a593Smuzhiyun 		(unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
4717*4882a593Smuzhiyun 		cpos, new_clusters);
4718*4882a593Smuzhiyun 
4719*4882a593Smuzhiyun 	memset(&rec, 0, sizeof(rec));
4720*4882a593Smuzhiyun 	rec.e_cpos = cpu_to_le32(cpos);
4721*4882a593Smuzhiyun 	rec.e_blkno = cpu_to_le64(start_blk);
4722*4882a593Smuzhiyun 	rec.e_leaf_clusters = cpu_to_le16(new_clusters);
4723*4882a593Smuzhiyun 	rec.e_flags = flags;
4724*4882a593Smuzhiyun 	status = ocfs2_et_insert_check(et, &rec);
4725*4882a593Smuzhiyun 	if (status) {
4726*4882a593Smuzhiyun 		mlog_errno(status);
4727*4882a593Smuzhiyun 		goto bail;
4728*4882a593Smuzhiyun 	}
4729*4882a593Smuzhiyun 
4730*4882a593Smuzhiyun 	status = ocfs2_figure_insert_type(et, &last_eb_bh, &rec,
4731*4882a593Smuzhiyun 					  &free_records, &insert);
4732*4882a593Smuzhiyun 	if (status < 0) {
4733*4882a593Smuzhiyun 		mlog_errno(status);
4734*4882a593Smuzhiyun 		goto bail;
4735*4882a593Smuzhiyun 	}
4736*4882a593Smuzhiyun 
4737*4882a593Smuzhiyun 	trace_ocfs2_insert_extent(insert.ins_appending, insert.ins_contig,
4738*4882a593Smuzhiyun 				  insert.ins_contig_index, free_records,
4739*4882a593Smuzhiyun 				  insert.ins_tree_depth);
4740*4882a593Smuzhiyun 
4741*4882a593Smuzhiyun 	if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
4742*4882a593Smuzhiyun 		status = ocfs2_grow_tree(handle, et,
4743*4882a593Smuzhiyun 					 &insert.ins_tree_depth, &last_eb_bh,
4744*4882a593Smuzhiyun 					 meta_ac);
4745*4882a593Smuzhiyun 		if (status) {
4746*4882a593Smuzhiyun 			mlog_errno(status);
4747*4882a593Smuzhiyun 			goto bail;
4748*4882a593Smuzhiyun 		}
4749*4882a593Smuzhiyun 	}
4750*4882a593Smuzhiyun 
4751*4882a593Smuzhiyun 	/* Finally, we can add clusters. This might rotate the tree for us. */
4752*4882a593Smuzhiyun 	status = ocfs2_do_insert_extent(handle, et, &rec, &insert);
4753*4882a593Smuzhiyun 	if (status < 0)
4754*4882a593Smuzhiyun 		mlog_errno(status);
4755*4882a593Smuzhiyun 	else
4756*4882a593Smuzhiyun 		ocfs2_et_extent_map_insert(et, &rec);
4757*4882a593Smuzhiyun 
4758*4882a593Smuzhiyun bail:
4759*4882a593Smuzhiyun 	brelse(last_eb_bh);
4760*4882a593Smuzhiyun 
4761*4882a593Smuzhiyun 	return status;
4762*4882a593Smuzhiyun }
4763*4882a593Smuzhiyun 
4764*4882a593Smuzhiyun /*
4765*4882a593Smuzhiyun  * Allcate and add clusters into the extent b-tree.
4766*4882a593Smuzhiyun  * The new clusters(clusters_to_add) will be inserted at logical_offset.
4767*4882a593Smuzhiyun  * The extent b-tree's root is specified by et, and
4768*4882a593Smuzhiyun  * it is not limited to the file storage. Any extent tree can use this
4769*4882a593Smuzhiyun  * function if it implements the proper ocfs2_extent_tree.
4770*4882a593Smuzhiyun  */
ocfs2_add_clusters_in_btree(handle_t * handle,struct ocfs2_extent_tree * et,u32 * logical_offset,u32 clusters_to_add,int mark_unwritten,struct ocfs2_alloc_context * data_ac,struct ocfs2_alloc_context * meta_ac,enum ocfs2_alloc_restarted * reason_ret)4771*4882a593Smuzhiyun int ocfs2_add_clusters_in_btree(handle_t *handle,
4772*4882a593Smuzhiyun 				struct ocfs2_extent_tree *et,
4773*4882a593Smuzhiyun 				u32 *logical_offset,
4774*4882a593Smuzhiyun 				u32 clusters_to_add,
4775*4882a593Smuzhiyun 				int mark_unwritten,
4776*4882a593Smuzhiyun 				struct ocfs2_alloc_context *data_ac,
4777*4882a593Smuzhiyun 				struct ocfs2_alloc_context *meta_ac,
4778*4882a593Smuzhiyun 				enum ocfs2_alloc_restarted *reason_ret)
4779*4882a593Smuzhiyun {
4780*4882a593Smuzhiyun 	int status = 0, err = 0;
4781*4882a593Smuzhiyun 	int need_free = 0;
4782*4882a593Smuzhiyun 	int free_extents;
4783*4882a593Smuzhiyun 	enum ocfs2_alloc_restarted reason = RESTART_NONE;
4784*4882a593Smuzhiyun 	u32 bit_off, num_bits;
4785*4882a593Smuzhiyun 	u64 block;
4786*4882a593Smuzhiyun 	u8 flags = 0;
4787*4882a593Smuzhiyun 	struct ocfs2_super *osb =
4788*4882a593Smuzhiyun 		OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
4789*4882a593Smuzhiyun 
4790*4882a593Smuzhiyun 	BUG_ON(!clusters_to_add);
4791*4882a593Smuzhiyun 
4792*4882a593Smuzhiyun 	if (mark_unwritten)
4793*4882a593Smuzhiyun 		flags = OCFS2_EXT_UNWRITTEN;
4794*4882a593Smuzhiyun 
4795*4882a593Smuzhiyun 	free_extents = ocfs2_num_free_extents(et);
4796*4882a593Smuzhiyun 	if (free_extents < 0) {
4797*4882a593Smuzhiyun 		status = free_extents;
4798*4882a593Smuzhiyun 		mlog_errno(status);
4799*4882a593Smuzhiyun 		goto leave;
4800*4882a593Smuzhiyun 	}
4801*4882a593Smuzhiyun 
4802*4882a593Smuzhiyun 	/* there are two cases which could cause us to EAGAIN in the
4803*4882a593Smuzhiyun 	 * we-need-more-metadata case:
4804*4882a593Smuzhiyun 	 * 1) we haven't reserved *any*
4805*4882a593Smuzhiyun 	 * 2) we are so fragmented, we've needed to add metadata too
4806*4882a593Smuzhiyun 	 *    many times. */
4807*4882a593Smuzhiyun 	if (!free_extents && !meta_ac) {
4808*4882a593Smuzhiyun 		err = -1;
4809*4882a593Smuzhiyun 		status = -EAGAIN;
4810*4882a593Smuzhiyun 		reason = RESTART_META;
4811*4882a593Smuzhiyun 		goto leave;
4812*4882a593Smuzhiyun 	} else if ((!free_extents)
4813*4882a593Smuzhiyun 		   && (ocfs2_alloc_context_bits_left(meta_ac)
4814*4882a593Smuzhiyun 		       < ocfs2_extend_meta_needed(et->et_root_el))) {
4815*4882a593Smuzhiyun 		err = -2;
4816*4882a593Smuzhiyun 		status = -EAGAIN;
4817*4882a593Smuzhiyun 		reason = RESTART_META;
4818*4882a593Smuzhiyun 		goto leave;
4819*4882a593Smuzhiyun 	}
4820*4882a593Smuzhiyun 
4821*4882a593Smuzhiyun 	status = __ocfs2_claim_clusters(handle, data_ac, 1,
4822*4882a593Smuzhiyun 					clusters_to_add, &bit_off, &num_bits);
4823*4882a593Smuzhiyun 	if (status < 0) {
4824*4882a593Smuzhiyun 		if (status != -ENOSPC)
4825*4882a593Smuzhiyun 			mlog_errno(status);
4826*4882a593Smuzhiyun 		goto leave;
4827*4882a593Smuzhiyun 	}
4828*4882a593Smuzhiyun 
4829*4882a593Smuzhiyun 	BUG_ON(num_bits > clusters_to_add);
4830*4882a593Smuzhiyun 
4831*4882a593Smuzhiyun 	/* reserve our write early -- insert_extent may update the tree root */
4832*4882a593Smuzhiyun 	status = ocfs2_et_root_journal_access(handle, et,
4833*4882a593Smuzhiyun 					      OCFS2_JOURNAL_ACCESS_WRITE);
4834*4882a593Smuzhiyun 	if (status < 0) {
4835*4882a593Smuzhiyun 		mlog_errno(status);
4836*4882a593Smuzhiyun 		need_free = 1;
4837*4882a593Smuzhiyun 		goto bail;
4838*4882a593Smuzhiyun 	}
4839*4882a593Smuzhiyun 
4840*4882a593Smuzhiyun 	block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4841*4882a593Smuzhiyun 	trace_ocfs2_add_clusters_in_btree(
4842*4882a593Smuzhiyun 	     (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
4843*4882a593Smuzhiyun 	     bit_off, num_bits);
4844*4882a593Smuzhiyun 	status = ocfs2_insert_extent(handle, et, *logical_offset, block,
4845*4882a593Smuzhiyun 				     num_bits, flags, meta_ac);
4846*4882a593Smuzhiyun 	if (status < 0) {
4847*4882a593Smuzhiyun 		mlog_errno(status);
4848*4882a593Smuzhiyun 		need_free = 1;
4849*4882a593Smuzhiyun 		goto bail;
4850*4882a593Smuzhiyun 	}
4851*4882a593Smuzhiyun 
4852*4882a593Smuzhiyun 	ocfs2_journal_dirty(handle, et->et_root_bh);
4853*4882a593Smuzhiyun 
4854*4882a593Smuzhiyun 	clusters_to_add -= num_bits;
4855*4882a593Smuzhiyun 	*logical_offset += num_bits;
4856*4882a593Smuzhiyun 
4857*4882a593Smuzhiyun 	if (clusters_to_add) {
4858*4882a593Smuzhiyun 		err = clusters_to_add;
4859*4882a593Smuzhiyun 		status = -EAGAIN;
4860*4882a593Smuzhiyun 		reason = RESTART_TRANS;
4861*4882a593Smuzhiyun 	}
4862*4882a593Smuzhiyun 
4863*4882a593Smuzhiyun bail:
4864*4882a593Smuzhiyun 	if (need_free) {
4865*4882a593Smuzhiyun 		if (data_ac->ac_which == OCFS2_AC_USE_LOCAL)
4866*4882a593Smuzhiyun 			ocfs2_free_local_alloc_bits(osb, handle, data_ac,
4867*4882a593Smuzhiyun 					bit_off, num_bits);
4868*4882a593Smuzhiyun 		else
4869*4882a593Smuzhiyun 			ocfs2_free_clusters(handle,
4870*4882a593Smuzhiyun 					data_ac->ac_inode,
4871*4882a593Smuzhiyun 					data_ac->ac_bh,
4872*4882a593Smuzhiyun 					ocfs2_clusters_to_blocks(osb->sb, bit_off),
4873*4882a593Smuzhiyun 					num_bits);
4874*4882a593Smuzhiyun 	}
4875*4882a593Smuzhiyun 
4876*4882a593Smuzhiyun leave:
4877*4882a593Smuzhiyun 	if (reason_ret)
4878*4882a593Smuzhiyun 		*reason_ret = reason;
4879*4882a593Smuzhiyun 	trace_ocfs2_add_clusters_in_btree_ret(status, reason, err);
4880*4882a593Smuzhiyun 	return status;
4881*4882a593Smuzhiyun }
4882*4882a593Smuzhiyun 
ocfs2_make_right_split_rec(struct super_block * sb,struct ocfs2_extent_rec * split_rec,u32 cpos,struct ocfs2_extent_rec * rec)4883*4882a593Smuzhiyun static void ocfs2_make_right_split_rec(struct super_block *sb,
4884*4882a593Smuzhiyun 				       struct ocfs2_extent_rec *split_rec,
4885*4882a593Smuzhiyun 				       u32 cpos,
4886*4882a593Smuzhiyun 				       struct ocfs2_extent_rec *rec)
4887*4882a593Smuzhiyun {
4888*4882a593Smuzhiyun 	u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4889*4882a593Smuzhiyun 	u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4890*4882a593Smuzhiyun 
4891*4882a593Smuzhiyun 	memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4892*4882a593Smuzhiyun 
4893*4882a593Smuzhiyun 	split_rec->e_cpos = cpu_to_le32(cpos);
4894*4882a593Smuzhiyun 	split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4895*4882a593Smuzhiyun 
4896*4882a593Smuzhiyun 	split_rec->e_blkno = rec->e_blkno;
4897*4882a593Smuzhiyun 	le64_add_cpu(&split_rec->e_blkno,
4898*4882a593Smuzhiyun 		     ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4899*4882a593Smuzhiyun 
4900*4882a593Smuzhiyun 	split_rec->e_flags = rec->e_flags;
4901*4882a593Smuzhiyun }
4902*4882a593Smuzhiyun 
ocfs2_split_and_insert(handle_t * handle,struct ocfs2_extent_tree * et,struct ocfs2_path * path,struct buffer_head ** last_eb_bh,int split_index,struct ocfs2_extent_rec * orig_split_rec,struct ocfs2_alloc_context * meta_ac)4903*4882a593Smuzhiyun static int ocfs2_split_and_insert(handle_t *handle,
4904*4882a593Smuzhiyun 				  struct ocfs2_extent_tree *et,
4905*4882a593Smuzhiyun 				  struct ocfs2_path *path,
4906*4882a593Smuzhiyun 				  struct buffer_head **last_eb_bh,
4907*4882a593Smuzhiyun 				  int split_index,
4908*4882a593Smuzhiyun 				  struct ocfs2_extent_rec *orig_split_rec,
4909*4882a593Smuzhiyun 				  struct ocfs2_alloc_context *meta_ac)
4910*4882a593Smuzhiyun {
4911*4882a593Smuzhiyun 	int ret = 0, depth;
4912*4882a593Smuzhiyun 	unsigned int insert_range, rec_range, do_leftright = 0;
4913*4882a593Smuzhiyun 	struct ocfs2_extent_rec tmprec;
4914*4882a593Smuzhiyun 	struct ocfs2_extent_list *rightmost_el;
4915*4882a593Smuzhiyun 	struct ocfs2_extent_rec rec;
4916*4882a593Smuzhiyun 	struct ocfs2_extent_rec split_rec = *orig_split_rec;
4917*4882a593Smuzhiyun 	struct ocfs2_insert_type insert;
4918*4882a593Smuzhiyun 	struct ocfs2_extent_block *eb;
4919*4882a593Smuzhiyun 
4920*4882a593Smuzhiyun leftright:
4921*4882a593Smuzhiyun 	/*
4922*4882a593Smuzhiyun 	 * Store a copy of the record on the stack - it might move
4923*4882a593Smuzhiyun 	 * around as the tree is manipulated below.
4924*4882a593Smuzhiyun 	 */
4925*4882a593Smuzhiyun 	rec = path_leaf_el(path)->l_recs[split_index];
4926*4882a593Smuzhiyun 
4927*4882a593Smuzhiyun 	rightmost_el = et->et_root_el;
4928*4882a593Smuzhiyun 
4929*4882a593Smuzhiyun 	depth = le16_to_cpu(rightmost_el->l_tree_depth);
4930*4882a593Smuzhiyun 	if (depth) {
4931*4882a593Smuzhiyun 		BUG_ON(!(*last_eb_bh));
4932*4882a593Smuzhiyun 		eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4933*4882a593Smuzhiyun 		rightmost_el = &eb->h_list;
4934*4882a593Smuzhiyun 	}
4935*4882a593Smuzhiyun 
4936*4882a593Smuzhiyun 	if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4937*4882a593Smuzhiyun 	    le16_to_cpu(rightmost_el->l_count)) {
4938*4882a593Smuzhiyun 		ret = ocfs2_grow_tree(handle, et,
4939*4882a593Smuzhiyun 				      &depth, last_eb_bh, meta_ac);
4940*4882a593Smuzhiyun 		if (ret) {
4941*4882a593Smuzhiyun 			mlog_errno(ret);
4942*4882a593Smuzhiyun 			goto out;
4943*4882a593Smuzhiyun 		}
4944*4882a593Smuzhiyun 	}
4945*4882a593Smuzhiyun 
4946*4882a593Smuzhiyun 	memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4947*4882a593Smuzhiyun 	insert.ins_appending = APPEND_NONE;
4948*4882a593Smuzhiyun 	insert.ins_contig = CONTIG_NONE;
4949*4882a593Smuzhiyun 	insert.ins_tree_depth = depth;
4950*4882a593Smuzhiyun 
4951*4882a593Smuzhiyun 	insert_range = le32_to_cpu(split_rec.e_cpos) +
4952*4882a593Smuzhiyun 		le16_to_cpu(split_rec.e_leaf_clusters);
4953*4882a593Smuzhiyun 	rec_range = le32_to_cpu(rec.e_cpos) +
4954*4882a593Smuzhiyun 		le16_to_cpu(rec.e_leaf_clusters);
4955*4882a593Smuzhiyun 
4956*4882a593Smuzhiyun 	if (split_rec.e_cpos == rec.e_cpos) {
4957*4882a593Smuzhiyun 		insert.ins_split = SPLIT_LEFT;
4958*4882a593Smuzhiyun 	} else if (insert_range == rec_range) {
4959*4882a593Smuzhiyun 		insert.ins_split = SPLIT_RIGHT;
4960*4882a593Smuzhiyun 	} else {
4961*4882a593Smuzhiyun 		/*
4962*4882a593Smuzhiyun 		 * Left/right split. We fake this as a right split
4963*4882a593Smuzhiyun 		 * first and then make a second pass as a left split.
4964*4882a593Smuzhiyun 		 */
4965*4882a593Smuzhiyun 		insert.ins_split = SPLIT_RIGHT;
4966*4882a593Smuzhiyun 
4967*4882a593Smuzhiyun 		ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
4968*4882a593Smuzhiyun 					   &tmprec, insert_range, &rec);
4969*4882a593Smuzhiyun 
4970*4882a593Smuzhiyun 		split_rec = tmprec;
4971*4882a593Smuzhiyun 
4972*4882a593Smuzhiyun 		BUG_ON(do_leftright);
4973*4882a593Smuzhiyun 		do_leftright = 1;
4974*4882a593Smuzhiyun 	}
4975*4882a593Smuzhiyun 
4976*4882a593Smuzhiyun 	ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
4977*4882a593Smuzhiyun 	if (ret) {
4978*4882a593Smuzhiyun 		mlog_errno(ret);
4979*4882a593Smuzhiyun 		goto out;
4980*4882a593Smuzhiyun 	}
4981*4882a593Smuzhiyun 
4982*4882a593Smuzhiyun 	if (do_leftright == 1) {
4983*4882a593Smuzhiyun 		u32 cpos;
4984*4882a593Smuzhiyun 		struct ocfs2_extent_list *el;
4985*4882a593Smuzhiyun 
4986*4882a593Smuzhiyun 		do_leftright++;
4987*4882a593Smuzhiyun 		split_rec = *orig_split_rec;
4988*4882a593Smuzhiyun 
4989*4882a593Smuzhiyun 		ocfs2_reinit_path(path, 1);
4990*4882a593Smuzhiyun 
4991*4882a593Smuzhiyun 		cpos = le32_to_cpu(split_rec.e_cpos);
4992*4882a593Smuzhiyun 		ret = ocfs2_find_path(et->et_ci, path, cpos);
4993*4882a593Smuzhiyun 		if (ret) {
4994*4882a593Smuzhiyun 			mlog_errno(ret);
4995*4882a593Smuzhiyun 			goto out;
4996*4882a593Smuzhiyun 		}
4997*4882a593Smuzhiyun 
4998*4882a593Smuzhiyun 		el = path_leaf_el(path);
4999*4882a593Smuzhiyun 		split_index = ocfs2_search_extent_list(el, cpos);
5000*4882a593Smuzhiyun 		if (split_index == -1) {
5001*4882a593Smuzhiyun 			ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5002*4882a593Smuzhiyun 				    "Owner %llu has an extent at cpos %u which can no longer be found\n",
5003*4882a593Smuzhiyun 				    (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5004*4882a593Smuzhiyun 				    cpos);
5005*4882a593Smuzhiyun 			ret = -EROFS;
5006*4882a593Smuzhiyun 			goto out;
5007*4882a593Smuzhiyun 		}
5008*4882a593Smuzhiyun 		goto leftright;
5009*4882a593Smuzhiyun 	}
5010*4882a593Smuzhiyun out:
5011*4882a593Smuzhiyun 
5012*4882a593Smuzhiyun 	return ret;
5013*4882a593Smuzhiyun }
5014*4882a593Smuzhiyun 
ocfs2_replace_extent_rec(handle_t * handle,struct ocfs2_extent_tree * et,struct ocfs2_path * path,struct ocfs2_extent_list * el,int split_index,struct ocfs2_extent_rec * split_rec)5015*4882a593Smuzhiyun static int ocfs2_replace_extent_rec(handle_t *handle,
5016*4882a593Smuzhiyun 				    struct ocfs2_extent_tree *et,
5017*4882a593Smuzhiyun 				    struct ocfs2_path *path,
5018*4882a593Smuzhiyun 				    struct ocfs2_extent_list *el,
5019*4882a593Smuzhiyun 				    int split_index,
5020*4882a593Smuzhiyun 				    struct ocfs2_extent_rec *split_rec)
5021*4882a593Smuzhiyun {
5022*4882a593Smuzhiyun 	int ret;
5023*4882a593Smuzhiyun 
5024*4882a593Smuzhiyun 	ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
5025*4882a593Smuzhiyun 					   path_num_items(path) - 1);
5026*4882a593Smuzhiyun 	if (ret) {
5027*4882a593Smuzhiyun 		mlog_errno(ret);
5028*4882a593Smuzhiyun 		goto out;
5029*4882a593Smuzhiyun 	}
5030*4882a593Smuzhiyun 
5031*4882a593Smuzhiyun 	el->l_recs[split_index] = *split_rec;
5032*4882a593Smuzhiyun 
5033*4882a593Smuzhiyun 	ocfs2_journal_dirty(handle, path_leaf_bh(path));
5034*4882a593Smuzhiyun out:
5035*4882a593Smuzhiyun 	return ret;
5036*4882a593Smuzhiyun }
5037*4882a593Smuzhiyun 
5038*4882a593Smuzhiyun /*
5039*4882a593Smuzhiyun  * Split part or all of the extent record at split_index in the leaf
5040*4882a593Smuzhiyun  * pointed to by path. Merge with the contiguous extent record if needed.
5041*4882a593Smuzhiyun  *
5042*4882a593Smuzhiyun  * Care is taken to handle contiguousness so as to not grow the tree.
5043*4882a593Smuzhiyun  *
5044*4882a593Smuzhiyun  * meta_ac is not strictly necessary - we only truly need it if growth
5045*4882a593Smuzhiyun  * of the tree is required. All other cases will degrade into a less
5046*4882a593Smuzhiyun  * optimal tree layout.
5047*4882a593Smuzhiyun  *
5048*4882a593Smuzhiyun  * last_eb_bh should be the rightmost leaf block for any extent
5049*4882a593Smuzhiyun  * btree. Since a split may grow the tree or a merge might shrink it,
5050*4882a593Smuzhiyun  * the caller cannot trust the contents of that buffer after this call.
5051*4882a593Smuzhiyun  *
5052*4882a593Smuzhiyun  * This code is optimized for readability - several passes might be
5053*4882a593Smuzhiyun  * made over certain portions of the tree. All of those blocks will
5054*4882a593Smuzhiyun  * have been brought into cache (and pinned via the journal), so the
5055*4882a593Smuzhiyun  * extra overhead is not expressed in terms of disk reads.
5056*4882a593Smuzhiyun  */
ocfs2_split_extent(handle_t * handle,struct ocfs2_extent_tree * et,struct ocfs2_path * path,int split_index,struct ocfs2_extent_rec * split_rec,struct ocfs2_alloc_context * meta_ac,struct ocfs2_cached_dealloc_ctxt * dealloc)5057*4882a593Smuzhiyun int ocfs2_split_extent(handle_t *handle,
5058*4882a593Smuzhiyun 		       struct ocfs2_extent_tree *et,
5059*4882a593Smuzhiyun 		       struct ocfs2_path *path,
5060*4882a593Smuzhiyun 		       int split_index,
5061*4882a593Smuzhiyun 		       struct ocfs2_extent_rec *split_rec,
5062*4882a593Smuzhiyun 		       struct ocfs2_alloc_context *meta_ac,
5063*4882a593Smuzhiyun 		       struct ocfs2_cached_dealloc_ctxt *dealloc)
5064*4882a593Smuzhiyun {
5065*4882a593Smuzhiyun 	int ret = 0;
5066*4882a593Smuzhiyun 	struct ocfs2_extent_list *el = path_leaf_el(path);
5067*4882a593Smuzhiyun 	struct buffer_head *last_eb_bh = NULL;
5068*4882a593Smuzhiyun 	struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
5069*4882a593Smuzhiyun 	struct ocfs2_merge_ctxt ctxt;
5070*4882a593Smuzhiyun 
5071*4882a593Smuzhiyun 	if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
5072*4882a593Smuzhiyun 	    ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
5073*4882a593Smuzhiyun 	     (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
5074*4882a593Smuzhiyun 		ret = -EIO;
5075*4882a593Smuzhiyun 		mlog_errno(ret);
5076*4882a593Smuzhiyun 		goto out;
5077*4882a593Smuzhiyun 	}
5078*4882a593Smuzhiyun 
5079*4882a593Smuzhiyun 	ret = ocfs2_figure_merge_contig_type(et, path, el,
5080*4882a593Smuzhiyun 					     split_index,
5081*4882a593Smuzhiyun 					     split_rec,
5082*4882a593Smuzhiyun 					     &ctxt);
5083*4882a593Smuzhiyun 	if (ret) {
5084*4882a593Smuzhiyun 		mlog_errno(ret);
5085*4882a593Smuzhiyun 		goto out;
5086*4882a593Smuzhiyun 	}
5087*4882a593Smuzhiyun 
5088*4882a593Smuzhiyun 	/*
5089*4882a593Smuzhiyun 	 * The core merge / split code wants to know how much room is
5090*4882a593Smuzhiyun 	 * left in this allocation tree, so we pass the
5091*4882a593Smuzhiyun 	 * rightmost extent list.
5092*4882a593Smuzhiyun 	 */
5093*4882a593Smuzhiyun 	if (path->p_tree_depth) {
5094*4882a593Smuzhiyun 		ret = ocfs2_read_extent_block(et->et_ci,
5095*4882a593Smuzhiyun 					      ocfs2_et_get_last_eb_blk(et),
5096*4882a593Smuzhiyun 					      &last_eb_bh);
5097*4882a593Smuzhiyun 		if (ret) {
5098*4882a593Smuzhiyun 			mlog_errno(ret);
5099*4882a593Smuzhiyun 			goto out;
5100*4882a593Smuzhiyun 		}
5101*4882a593Smuzhiyun 	}
5102*4882a593Smuzhiyun 
5103*4882a593Smuzhiyun 	if (rec->e_cpos == split_rec->e_cpos &&
5104*4882a593Smuzhiyun 	    rec->e_leaf_clusters == split_rec->e_leaf_clusters)
5105*4882a593Smuzhiyun 		ctxt.c_split_covers_rec = 1;
5106*4882a593Smuzhiyun 	else
5107*4882a593Smuzhiyun 		ctxt.c_split_covers_rec = 0;
5108*4882a593Smuzhiyun 
5109*4882a593Smuzhiyun 	ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
5110*4882a593Smuzhiyun 
5111*4882a593Smuzhiyun 	trace_ocfs2_split_extent(split_index, ctxt.c_contig_type,
5112*4882a593Smuzhiyun 				 ctxt.c_has_empty_extent,
5113*4882a593Smuzhiyun 				 ctxt.c_split_covers_rec);
5114*4882a593Smuzhiyun 
5115*4882a593Smuzhiyun 	if (ctxt.c_contig_type == CONTIG_NONE) {
5116*4882a593Smuzhiyun 		if (ctxt.c_split_covers_rec)
5117*4882a593Smuzhiyun 			ret = ocfs2_replace_extent_rec(handle, et, path, el,
5118*4882a593Smuzhiyun 						       split_index, split_rec);
5119*4882a593Smuzhiyun 		else
5120*4882a593Smuzhiyun 			ret = ocfs2_split_and_insert(handle, et, path,
5121*4882a593Smuzhiyun 						     &last_eb_bh, split_index,
5122*4882a593Smuzhiyun 						     split_rec, meta_ac);
5123*4882a593Smuzhiyun 		if (ret)
5124*4882a593Smuzhiyun 			mlog_errno(ret);
5125*4882a593Smuzhiyun 	} else {
5126*4882a593Smuzhiyun 		ret = ocfs2_try_to_merge_extent(handle, et, path,
5127*4882a593Smuzhiyun 						split_index, split_rec,
5128*4882a593Smuzhiyun 						dealloc, &ctxt);
5129*4882a593Smuzhiyun 		if (ret)
5130*4882a593Smuzhiyun 			mlog_errno(ret);
5131*4882a593Smuzhiyun 	}
5132*4882a593Smuzhiyun 
5133*4882a593Smuzhiyun out:
5134*4882a593Smuzhiyun 	brelse(last_eb_bh);
5135*4882a593Smuzhiyun 	return ret;
5136*4882a593Smuzhiyun }
5137*4882a593Smuzhiyun 
5138*4882a593Smuzhiyun /*
5139*4882a593Smuzhiyun  * Change the flags of the already-existing extent at cpos for len clusters.
5140*4882a593Smuzhiyun  *
5141*4882a593Smuzhiyun  * new_flags: the flags we want to set.
5142*4882a593Smuzhiyun  * clear_flags: the flags we want to clear.
5143*4882a593Smuzhiyun  * phys: the new physical offset we want this new extent starts from.
5144*4882a593Smuzhiyun  *
5145*4882a593Smuzhiyun  * If the existing extent is larger than the request, initiate a
5146*4882a593Smuzhiyun  * split. An attempt will be made at merging with adjacent extents.
5147*4882a593Smuzhiyun  *
5148*4882a593Smuzhiyun  * The caller is responsible for passing down meta_ac if we'll need it.
5149*4882a593Smuzhiyun  */
ocfs2_change_extent_flag(handle_t * handle,struct ocfs2_extent_tree * et,u32 cpos,u32 len,u32 phys,struct ocfs2_alloc_context * meta_ac,struct ocfs2_cached_dealloc_ctxt * dealloc,int new_flags,int clear_flags)5150*4882a593Smuzhiyun int ocfs2_change_extent_flag(handle_t *handle,
5151*4882a593Smuzhiyun 			     struct ocfs2_extent_tree *et,
5152*4882a593Smuzhiyun 			     u32 cpos, u32 len, u32 phys,
5153*4882a593Smuzhiyun 			     struct ocfs2_alloc_context *meta_ac,
5154*4882a593Smuzhiyun 			     struct ocfs2_cached_dealloc_ctxt *dealloc,
5155*4882a593Smuzhiyun 			     int new_flags, int clear_flags)
5156*4882a593Smuzhiyun {
5157*4882a593Smuzhiyun 	int ret, index;
5158*4882a593Smuzhiyun 	struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
5159*4882a593Smuzhiyun 	u64 start_blkno = ocfs2_clusters_to_blocks(sb, phys);
5160*4882a593Smuzhiyun 	struct ocfs2_extent_rec split_rec;
5161*4882a593Smuzhiyun 	struct ocfs2_path *left_path = NULL;
5162*4882a593Smuzhiyun 	struct ocfs2_extent_list *el;
5163*4882a593Smuzhiyun 	struct ocfs2_extent_rec *rec;
5164*4882a593Smuzhiyun 
5165*4882a593Smuzhiyun 	left_path = ocfs2_new_path_from_et(et);
5166*4882a593Smuzhiyun 	if (!left_path) {
5167*4882a593Smuzhiyun 		ret = -ENOMEM;
5168*4882a593Smuzhiyun 		mlog_errno(ret);
5169*4882a593Smuzhiyun 		goto out;
5170*4882a593Smuzhiyun 	}
5171*4882a593Smuzhiyun 
5172*4882a593Smuzhiyun 	ret = ocfs2_find_path(et->et_ci, left_path, cpos);
5173*4882a593Smuzhiyun 	if (ret) {
5174*4882a593Smuzhiyun 		mlog_errno(ret);
5175*4882a593Smuzhiyun 		goto out;
5176*4882a593Smuzhiyun 	}
5177*4882a593Smuzhiyun 	el = path_leaf_el(left_path);
5178*4882a593Smuzhiyun 
5179*4882a593Smuzhiyun 	index = ocfs2_search_extent_list(el, cpos);
5180*4882a593Smuzhiyun 	if (index == -1) {
5181*4882a593Smuzhiyun 		ocfs2_error(sb,
5182*4882a593Smuzhiyun 			    "Owner %llu has an extent at cpos %u which can no longer be found\n",
5183*4882a593Smuzhiyun 			    (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5184*4882a593Smuzhiyun 			    cpos);
5185*4882a593Smuzhiyun 		ret = -EROFS;
5186*4882a593Smuzhiyun 		goto out;
5187*4882a593Smuzhiyun 	}
5188*4882a593Smuzhiyun 
5189*4882a593Smuzhiyun 	ret = -EIO;
5190*4882a593Smuzhiyun 	rec = &el->l_recs[index];
5191*4882a593Smuzhiyun 	if (new_flags && (rec->e_flags & new_flags)) {
5192*4882a593Smuzhiyun 		mlog(ML_ERROR, "Owner %llu tried to set %d flags on an "
5193*4882a593Smuzhiyun 		     "extent that already had them\n",
5194*4882a593Smuzhiyun 		     (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5195*4882a593Smuzhiyun 		     new_flags);
5196*4882a593Smuzhiyun 		goto out;
5197*4882a593Smuzhiyun 	}
5198*4882a593Smuzhiyun 
5199*4882a593Smuzhiyun 	if (clear_flags && !(rec->e_flags & clear_flags)) {
5200*4882a593Smuzhiyun 		mlog(ML_ERROR, "Owner %llu tried to clear %d flags on an "
5201*4882a593Smuzhiyun 		     "extent that didn't have them\n",
5202*4882a593Smuzhiyun 		     (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5203*4882a593Smuzhiyun 		     clear_flags);
5204*4882a593Smuzhiyun 		goto out;
5205*4882a593Smuzhiyun 	}
5206*4882a593Smuzhiyun 
5207*4882a593Smuzhiyun 	memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
5208*4882a593Smuzhiyun 	split_rec.e_cpos = cpu_to_le32(cpos);
5209*4882a593Smuzhiyun 	split_rec.e_leaf_clusters = cpu_to_le16(len);
5210*4882a593Smuzhiyun 	split_rec.e_blkno = cpu_to_le64(start_blkno);
5211*4882a593Smuzhiyun 	split_rec.e_flags = rec->e_flags;
5212*4882a593Smuzhiyun 	if (new_flags)
5213*4882a593Smuzhiyun 		split_rec.e_flags |= new_flags;
5214*4882a593Smuzhiyun 	if (clear_flags)
5215*4882a593Smuzhiyun 		split_rec.e_flags &= ~clear_flags;
5216*4882a593Smuzhiyun 
5217*4882a593Smuzhiyun 	ret = ocfs2_split_extent(handle, et, left_path,
5218*4882a593Smuzhiyun 				 index, &split_rec, meta_ac,
5219*4882a593Smuzhiyun 				 dealloc);
5220*4882a593Smuzhiyun 	if (ret)
5221*4882a593Smuzhiyun 		mlog_errno(ret);
5222*4882a593Smuzhiyun 
5223*4882a593Smuzhiyun out:
5224*4882a593Smuzhiyun 	ocfs2_free_path(left_path);
5225*4882a593Smuzhiyun 	return ret;
5226*4882a593Smuzhiyun 
5227*4882a593Smuzhiyun }
5228*4882a593Smuzhiyun 
5229*4882a593Smuzhiyun /*
5230*4882a593Smuzhiyun  * Mark the already-existing extent at cpos as written for len clusters.
5231*4882a593Smuzhiyun  * This removes the unwritten extent flag.
5232*4882a593Smuzhiyun  *
5233*4882a593Smuzhiyun  * If the existing extent is larger than the request, initiate a
5234*4882a593Smuzhiyun  * split. An attempt will be made at merging with adjacent extents.
5235*4882a593Smuzhiyun  *
5236*4882a593Smuzhiyun  * The caller is responsible for passing down meta_ac if we'll need it.
5237*4882a593Smuzhiyun  */
ocfs2_mark_extent_written(struct inode * inode,struct ocfs2_extent_tree * et,handle_t * handle,u32 cpos,u32 len,u32 phys,struct ocfs2_alloc_context * meta_ac,struct ocfs2_cached_dealloc_ctxt * dealloc)5238*4882a593Smuzhiyun int ocfs2_mark_extent_written(struct inode *inode,
5239*4882a593Smuzhiyun 			      struct ocfs2_extent_tree *et,
5240*4882a593Smuzhiyun 			      handle_t *handle, u32 cpos, u32 len, u32 phys,
5241*4882a593Smuzhiyun 			      struct ocfs2_alloc_context *meta_ac,
5242*4882a593Smuzhiyun 			      struct ocfs2_cached_dealloc_ctxt *dealloc)
5243*4882a593Smuzhiyun {
5244*4882a593Smuzhiyun 	int ret;
5245*4882a593Smuzhiyun 
5246*4882a593Smuzhiyun 	trace_ocfs2_mark_extent_written(
5247*4882a593Smuzhiyun 		(unsigned long long)OCFS2_I(inode)->ip_blkno,
5248*4882a593Smuzhiyun 		cpos, len, phys);
5249*4882a593Smuzhiyun 
5250*4882a593Smuzhiyun 	if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
5251*4882a593Smuzhiyun 		ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents that are being written to, but the feature bit is not set in the super block\n",
5252*4882a593Smuzhiyun 			    (unsigned long long)OCFS2_I(inode)->ip_blkno);
5253*4882a593Smuzhiyun 		ret = -EROFS;
5254*4882a593Smuzhiyun 		goto out;
5255*4882a593Smuzhiyun 	}
5256*4882a593Smuzhiyun 
5257*4882a593Smuzhiyun 	/*
5258*4882a593Smuzhiyun 	 * XXX: This should be fixed up so that we just re-insert the
5259*4882a593Smuzhiyun 	 * next extent records.
5260*4882a593Smuzhiyun 	 */
5261*4882a593Smuzhiyun 	ocfs2_et_extent_map_truncate(et, 0);
5262*4882a593Smuzhiyun 
5263*4882a593Smuzhiyun 	ret = ocfs2_change_extent_flag(handle, et, cpos,
5264*4882a593Smuzhiyun 				       len, phys, meta_ac, dealloc,
5265*4882a593Smuzhiyun 				       0, OCFS2_EXT_UNWRITTEN);
5266*4882a593Smuzhiyun 	if (ret)
5267*4882a593Smuzhiyun 		mlog_errno(ret);
5268*4882a593Smuzhiyun 
5269*4882a593Smuzhiyun out:
5270*4882a593Smuzhiyun 	return ret;
5271*4882a593Smuzhiyun }
5272*4882a593Smuzhiyun 
ocfs2_split_tree(handle_t * handle,struct ocfs2_extent_tree * et,struct ocfs2_path * path,int index,u32 new_range,struct ocfs2_alloc_context * meta_ac)5273*4882a593Smuzhiyun static int ocfs2_split_tree(handle_t *handle, struct ocfs2_extent_tree *et,
5274*4882a593Smuzhiyun 			    struct ocfs2_path *path,
5275*4882a593Smuzhiyun 			    int index, u32 new_range,
5276*4882a593Smuzhiyun 			    struct ocfs2_alloc_context *meta_ac)
5277*4882a593Smuzhiyun {
5278*4882a593Smuzhiyun 	int ret, depth, credits;
5279*4882a593Smuzhiyun 	struct buffer_head *last_eb_bh = NULL;
5280*4882a593Smuzhiyun 	struct ocfs2_extent_block *eb;
5281*4882a593Smuzhiyun 	struct ocfs2_extent_list *rightmost_el, *el;
5282*4882a593Smuzhiyun 	struct ocfs2_extent_rec split_rec;
5283*4882a593Smuzhiyun 	struct ocfs2_extent_rec *rec;
5284*4882a593Smuzhiyun 	struct ocfs2_insert_type insert;
5285*4882a593Smuzhiyun 
5286*4882a593Smuzhiyun 	/*
5287*4882a593Smuzhiyun 	 * Setup the record to split before we grow the tree.
5288*4882a593Smuzhiyun 	 */
5289*4882a593Smuzhiyun 	el = path_leaf_el(path);
5290*4882a593Smuzhiyun 	rec = &el->l_recs[index];
5291*4882a593Smuzhiyun 	ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
5292*4882a593Smuzhiyun 				   &split_rec, new_range, rec);
5293*4882a593Smuzhiyun 
5294*4882a593Smuzhiyun 	depth = path->p_tree_depth;
5295*4882a593Smuzhiyun 	if (depth > 0) {
5296*4882a593Smuzhiyun 		ret = ocfs2_read_extent_block(et->et_ci,
5297*4882a593Smuzhiyun 					      ocfs2_et_get_last_eb_blk(et),
5298*4882a593Smuzhiyun 					      &last_eb_bh);
5299*4882a593Smuzhiyun 		if (ret < 0) {
5300*4882a593Smuzhiyun 			mlog_errno(ret);
5301*4882a593Smuzhiyun 			goto out;
5302*4882a593Smuzhiyun 		}
5303*4882a593Smuzhiyun 
5304*4882a593Smuzhiyun 		eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5305*4882a593Smuzhiyun 		rightmost_el = &eb->h_list;
5306*4882a593Smuzhiyun 	} else
5307*4882a593Smuzhiyun 		rightmost_el = path_leaf_el(path);
5308*4882a593Smuzhiyun 
5309*4882a593Smuzhiyun 	credits = path->p_tree_depth +
5310*4882a593Smuzhiyun 		  ocfs2_extend_meta_needed(et->et_root_el);
5311*4882a593Smuzhiyun 	ret = ocfs2_extend_trans(handle, credits);
5312*4882a593Smuzhiyun 	if (ret) {
5313*4882a593Smuzhiyun 		mlog_errno(ret);
5314*4882a593Smuzhiyun 		goto out;
5315*4882a593Smuzhiyun 	}
5316*4882a593Smuzhiyun 
5317*4882a593Smuzhiyun 	if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
5318*4882a593Smuzhiyun 	    le16_to_cpu(rightmost_el->l_count)) {
5319*4882a593Smuzhiyun 		ret = ocfs2_grow_tree(handle, et, &depth, &last_eb_bh,
5320*4882a593Smuzhiyun 				      meta_ac);
5321*4882a593Smuzhiyun 		if (ret) {
5322*4882a593Smuzhiyun 			mlog_errno(ret);
5323*4882a593Smuzhiyun 			goto out;
5324*4882a593Smuzhiyun 		}
5325*4882a593Smuzhiyun 	}
5326*4882a593Smuzhiyun 
5327*4882a593Smuzhiyun 	memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5328*4882a593Smuzhiyun 	insert.ins_appending = APPEND_NONE;
5329*4882a593Smuzhiyun 	insert.ins_contig = CONTIG_NONE;
5330*4882a593Smuzhiyun 	insert.ins_split = SPLIT_RIGHT;
5331*4882a593Smuzhiyun 	insert.ins_tree_depth = depth;
5332*4882a593Smuzhiyun 
5333*4882a593Smuzhiyun 	ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
5334*4882a593Smuzhiyun 	if (ret)
5335*4882a593Smuzhiyun 		mlog_errno(ret);
5336*4882a593Smuzhiyun 
5337*4882a593Smuzhiyun out:
5338*4882a593Smuzhiyun 	brelse(last_eb_bh);
5339*4882a593Smuzhiyun 	return ret;
5340*4882a593Smuzhiyun }
5341*4882a593Smuzhiyun 
ocfs2_truncate_rec(handle_t * handle,struct ocfs2_extent_tree * et,struct ocfs2_path * path,int index,struct ocfs2_cached_dealloc_ctxt * dealloc,u32 cpos,u32 len)5342*4882a593Smuzhiyun static int ocfs2_truncate_rec(handle_t *handle,
5343*4882a593Smuzhiyun 			      struct ocfs2_extent_tree *et,
5344*4882a593Smuzhiyun 			      struct ocfs2_path *path, int index,
5345*4882a593Smuzhiyun 			      struct ocfs2_cached_dealloc_ctxt *dealloc,
5346*4882a593Smuzhiyun 			      u32 cpos, u32 len)
5347*4882a593Smuzhiyun {
5348*4882a593Smuzhiyun 	int ret;
5349*4882a593Smuzhiyun 	u32 left_cpos, rec_range, trunc_range;
5350*4882a593Smuzhiyun 	int is_rightmost_tree_rec = 0;
5351*4882a593Smuzhiyun 	struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
5352*4882a593Smuzhiyun 	struct ocfs2_path *left_path = NULL;
5353*4882a593Smuzhiyun 	struct ocfs2_extent_list *el = path_leaf_el(path);
5354*4882a593Smuzhiyun 	struct ocfs2_extent_rec *rec;
5355*4882a593Smuzhiyun 	struct ocfs2_extent_block *eb;
5356*4882a593Smuzhiyun 
5357*4882a593Smuzhiyun 	if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
5358*4882a593Smuzhiyun 		/* extend credit for ocfs2_remove_rightmost_path */
5359*4882a593Smuzhiyun 		ret = ocfs2_extend_rotate_transaction(handle, 0,
5360*4882a593Smuzhiyun 				jbd2_handle_buffer_credits(handle),
5361*4882a593Smuzhiyun 				path);
5362*4882a593Smuzhiyun 		if (ret) {
5363*4882a593Smuzhiyun 			mlog_errno(ret);
5364*4882a593Smuzhiyun 			goto out;
5365*4882a593Smuzhiyun 		}
5366*4882a593Smuzhiyun 
5367*4882a593Smuzhiyun 		ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
5368*4882a593Smuzhiyun 		if (ret) {
5369*4882a593Smuzhiyun 			mlog_errno(ret);
5370*4882a593Smuzhiyun 			goto out;
5371*4882a593Smuzhiyun 		}
5372*4882a593Smuzhiyun 
5373*4882a593Smuzhiyun 		index--;
5374*4882a593Smuzhiyun 	}
5375*4882a593Smuzhiyun 
5376*4882a593Smuzhiyun 	if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5377*4882a593Smuzhiyun 	    path->p_tree_depth) {
5378*4882a593Smuzhiyun 		/*
5379*4882a593Smuzhiyun 		 * Check whether this is the rightmost tree record. If
5380*4882a593Smuzhiyun 		 * we remove all of this record or part of its right
5381*4882a593Smuzhiyun 		 * edge then an update of the record lengths above it
5382*4882a593Smuzhiyun 		 * will be required.
5383*4882a593Smuzhiyun 		 */
5384*4882a593Smuzhiyun 		eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5385*4882a593Smuzhiyun 		if (eb->h_next_leaf_blk == 0)
5386*4882a593Smuzhiyun 			is_rightmost_tree_rec = 1;
5387*4882a593Smuzhiyun 	}
5388*4882a593Smuzhiyun 
5389*4882a593Smuzhiyun 	rec = &el->l_recs[index];
5390*4882a593Smuzhiyun 	if (index == 0 && path->p_tree_depth &&
5391*4882a593Smuzhiyun 	    le32_to_cpu(rec->e_cpos) == cpos) {
5392*4882a593Smuzhiyun 		/*
5393*4882a593Smuzhiyun 		 * Changing the leftmost offset (via partial or whole
5394*4882a593Smuzhiyun 		 * record truncate) of an interior (or rightmost) path
5395*4882a593Smuzhiyun 		 * means we have to update the subtree that is formed
5396*4882a593Smuzhiyun 		 * by this leaf and the one to it's left.
5397*4882a593Smuzhiyun 		 *
5398*4882a593Smuzhiyun 		 * There are two cases we can skip:
5399*4882a593Smuzhiyun 		 *   1) Path is the leftmost one in our btree.
5400*4882a593Smuzhiyun 		 *   2) The leaf is rightmost and will be empty after
5401*4882a593Smuzhiyun 		 *      we remove the extent record - the rotate code
5402*4882a593Smuzhiyun 		 *      knows how to update the newly formed edge.
5403*4882a593Smuzhiyun 		 */
5404*4882a593Smuzhiyun 
5405*4882a593Smuzhiyun 		ret = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
5406*4882a593Smuzhiyun 		if (ret) {
5407*4882a593Smuzhiyun 			mlog_errno(ret);
5408*4882a593Smuzhiyun 			goto out;
5409*4882a593Smuzhiyun 		}
5410*4882a593Smuzhiyun 
5411*4882a593Smuzhiyun 		if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
5412*4882a593Smuzhiyun 			left_path = ocfs2_new_path_from_path(path);
5413*4882a593Smuzhiyun 			if (!left_path) {
5414*4882a593Smuzhiyun 				ret = -ENOMEM;
5415*4882a593Smuzhiyun 				mlog_errno(ret);
5416*4882a593Smuzhiyun 				goto out;
5417*4882a593Smuzhiyun 			}
5418*4882a593Smuzhiyun 
5419*4882a593Smuzhiyun 			ret = ocfs2_find_path(et->et_ci, left_path,
5420*4882a593Smuzhiyun 					      left_cpos);
5421*4882a593Smuzhiyun 			if (ret) {
5422*4882a593Smuzhiyun 				mlog_errno(ret);
5423*4882a593Smuzhiyun 				goto out;
5424*4882a593Smuzhiyun 			}
5425*4882a593Smuzhiyun 		}
5426*4882a593Smuzhiyun 	}
5427*4882a593Smuzhiyun 
5428*4882a593Smuzhiyun 	ret = ocfs2_extend_rotate_transaction(handle, 0,
5429*4882a593Smuzhiyun 					jbd2_handle_buffer_credits(handle),
5430*4882a593Smuzhiyun 					path);
5431*4882a593Smuzhiyun 	if (ret) {
5432*4882a593Smuzhiyun 		mlog_errno(ret);
5433*4882a593Smuzhiyun 		goto out;
5434*4882a593Smuzhiyun 	}
5435*4882a593Smuzhiyun 
5436*4882a593Smuzhiyun 	ret = ocfs2_journal_access_path(et->et_ci, handle, path);
5437*4882a593Smuzhiyun 	if (ret) {
5438*4882a593Smuzhiyun 		mlog_errno(ret);
5439*4882a593Smuzhiyun 		goto out;
5440*4882a593Smuzhiyun 	}
5441*4882a593Smuzhiyun 
5442*4882a593Smuzhiyun 	ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
5443*4882a593Smuzhiyun 	if (ret) {
5444*4882a593Smuzhiyun 		mlog_errno(ret);
5445*4882a593Smuzhiyun 		goto out;
5446*4882a593Smuzhiyun 	}
5447*4882a593Smuzhiyun 
5448*4882a593Smuzhiyun 	rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5449*4882a593Smuzhiyun 	trunc_range = cpos + len;
5450*4882a593Smuzhiyun 
5451*4882a593Smuzhiyun 	if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5452*4882a593Smuzhiyun 		int next_free;
5453*4882a593Smuzhiyun 
5454*4882a593Smuzhiyun 		memset(rec, 0, sizeof(*rec));
5455*4882a593Smuzhiyun 		ocfs2_cleanup_merge(el, index);
5456*4882a593Smuzhiyun 
5457*4882a593Smuzhiyun 		next_free = le16_to_cpu(el->l_next_free_rec);
5458*4882a593Smuzhiyun 		if (is_rightmost_tree_rec && next_free > 1) {
5459*4882a593Smuzhiyun 			/*
5460*4882a593Smuzhiyun 			 * We skip the edge update if this path will
5461*4882a593Smuzhiyun 			 * be deleted by the rotate code.
5462*4882a593Smuzhiyun 			 */
5463*4882a593Smuzhiyun 			rec = &el->l_recs[next_free - 1];
5464*4882a593Smuzhiyun 			ocfs2_adjust_rightmost_records(handle, et, path,
5465*4882a593Smuzhiyun 						       rec);
5466*4882a593Smuzhiyun 		}
5467*4882a593Smuzhiyun 	} else if (le32_to_cpu(rec->e_cpos) == cpos) {
5468*4882a593Smuzhiyun 		/* Remove leftmost portion of the record. */
5469*4882a593Smuzhiyun 		le32_add_cpu(&rec->e_cpos, len);
5470*4882a593Smuzhiyun 		le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5471*4882a593Smuzhiyun 		le16_add_cpu(&rec->e_leaf_clusters, -len);
5472*4882a593Smuzhiyun 	} else if (rec_range == trunc_range) {
5473*4882a593Smuzhiyun 		/* Remove rightmost portion of the record */
5474*4882a593Smuzhiyun 		le16_add_cpu(&rec->e_leaf_clusters, -len);
5475*4882a593Smuzhiyun 		if (is_rightmost_tree_rec)
5476*4882a593Smuzhiyun 			ocfs2_adjust_rightmost_records(handle, et, path, rec);
5477*4882a593Smuzhiyun 	} else {
5478*4882a593Smuzhiyun 		/* Caller should have trapped this. */
5479*4882a593Smuzhiyun 		mlog(ML_ERROR, "Owner %llu: Invalid record truncate: (%u, %u) "
5480*4882a593Smuzhiyun 		     "(%u, %u)\n",
5481*4882a593Smuzhiyun 		     (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5482*4882a593Smuzhiyun 		     le32_to_cpu(rec->e_cpos),
5483*4882a593Smuzhiyun 		     le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5484*4882a593Smuzhiyun 		BUG();
5485*4882a593Smuzhiyun 	}
5486*4882a593Smuzhiyun 
5487*4882a593Smuzhiyun 	if (left_path) {
5488*4882a593Smuzhiyun 		int subtree_index;
5489*4882a593Smuzhiyun 
5490*4882a593Smuzhiyun 		subtree_index = ocfs2_find_subtree_root(et, left_path, path);
5491*4882a593Smuzhiyun 		ocfs2_complete_edge_insert(handle, left_path, path,
5492*4882a593Smuzhiyun 					   subtree_index);
5493*4882a593Smuzhiyun 	}
5494*4882a593Smuzhiyun 
5495*4882a593Smuzhiyun 	ocfs2_journal_dirty(handle, path_leaf_bh(path));
5496*4882a593Smuzhiyun 
5497*4882a593Smuzhiyun 	ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
5498*4882a593Smuzhiyun 	if (ret)
5499*4882a593Smuzhiyun 		mlog_errno(ret);
5500*4882a593Smuzhiyun 
5501*4882a593Smuzhiyun out:
5502*4882a593Smuzhiyun 	ocfs2_free_path(left_path);
5503*4882a593Smuzhiyun 	return ret;
5504*4882a593Smuzhiyun }
5505*4882a593Smuzhiyun 
ocfs2_remove_extent(handle_t * handle,struct ocfs2_extent_tree * et,u32 cpos,u32 len,struct ocfs2_alloc_context * meta_ac,struct ocfs2_cached_dealloc_ctxt * dealloc)5506*4882a593Smuzhiyun int ocfs2_remove_extent(handle_t *handle,
5507*4882a593Smuzhiyun 			struct ocfs2_extent_tree *et,
5508*4882a593Smuzhiyun 			u32 cpos, u32 len,
5509*4882a593Smuzhiyun 			struct ocfs2_alloc_context *meta_ac,
5510*4882a593Smuzhiyun 			struct ocfs2_cached_dealloc_ctxt *dealloc)
5511*4882a593Smuzhiyun {
5512*4882a593Smuzhiyun 	int ret, index;
5513*4882a593Smuzhiyun 	u32 rec_range, trunc_range;
5514*4882a593Smuzhiyun 	struct ocfs2_extent_rec *rec;
5515*4882a593Smuzhiyun 	struct ocfs2_extent_list *el;
5516*4882a593Smuzhiyun 	struct ocfs2_path *path = NULL;
5517*4882a593Smuzhiyun 
5518*4882a593Smuzhiyun 	/*
5519*4882a593Smuzhiyun 	 * XXX: Why are we truncating to 0 instead of wherever this
5520*4882a593Smuzhiyun 	 * affects us?
5521*4882a593Smuzhiyun 	 */
5522*4882a593Smuzhiyun 	ocfs2_et_extent_map_truncate(et, 0);
5523*4882a593Smuzhiyun 
5524*4882a593Smuzhiyun 	path = ocfs2_new_path_from_et(et);
5525*4882a593Smuzhiyun 	if (!path) {
5526*4882a593Smuzhiyun 		ret = -ENOMEM;
5527*4882a593Smuzhiyun 		mlog_errno(ret);
5528*4882a593Smuzhiyun 		goto out;
5529*4882a593Smuzhiyun 	}
5530*4882a593Smuzhiyun 
5531*4882a593Smuzhiyun 	ret = ocfs2_find_path(et->et_ci, path, cpos);
5532*4882a593Smuzhiyun 	if (ret) {
5533*4882a593Smuzhiyun 		mlog_errno(ret);
5534*4882a593Smuzhiyun 		goto out;
5535*4882a593Smuzhiyun 	}
5536*4882a593Smuzhiyun 
5537*4882a593Smuzhiyun 	el = path_leaf_el(path);
5538*4882a593Smuzhiyun 	index = ocfs2_search_extent_list(el, cpos);
5539*4882a593Smuzhiyun 	if (index == -1) {
5540*4882a593Smuzhiyun 		ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5541*4882a593Smuzhiyun 			    "Owner %llu has an extent at cpos %u which can no longer be found\n",
5542*4882a593Smuzhiyun 			    (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5543*4882a593Smuzhiyun 			    cpos);
5544*4882a593Smuzhiyun 		ret = -EROFS;
5545*4882a593Smuzhiyun 		goto out;
5546*4882a593Smuzhiyun 	}
5547*4882a593Smuzhiyun 
5548*4882a593Smuzhiyun 	/*
5549*4882a593Smuzhiyun 	 * We have 3 cases of extent removal:
5550*4882a593Smuzhiyun 	 *   1) Range covers the entire extent rec
5551*4882a593Smuzhiyun 	 *   2) Range begins or ends on one edge of the extent rec
5552*4882a593Smuzhiyun 	 *   3) Range is in the middle of the extent rec (no shared edges)
5553*4882a593Smuzhiyun 	 *
5554*4882a593Smuzhiyun 	 * For case 1 we remove the extent rec and left rotate to
5555*4882a593Smuzhiyun 	 * fill the hole.
5556*4882a593Smuzhiyun 	 *
5557*4882a593Smuzhiyun 	 * For case 2 we just shrink the existing extent rec, with a
5558*4882a593Smuzhiyun 	 * tree update if the shrinking edge is also the edge of an
5559*4882a593Smuzhiyun 	 * extent block.
5560*4882a593Smuzhiyun 	 *
5561*4882a593Smuzhiyun 	 * For case 3 we do a right split to turn the extent rec into
5562*4882a593Smuzhiyun 	 * something case 2 can handle.
5563*4882a593Smuzhiyun 	 */
5564*4882a593Smuzhiyun 	rec = &el->l_recs[index];
5565*4882a593Smuzhiyun 	rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5566*4882a593Smuzhiyun 	trunc_range = cpos + len;
5567*4882a593Smuzhiyun 
5568*4882a593Smuzhiyun 	BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5569*4882a593Smuzhiyun 
5570*4882a593Smuzhiyun 	trace_ocfs2_remove_extent(
5571*4882a593Smuzhiyun 		(unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5572*4882a593Smuzhiyun 		cpos, len, index, le32_to_cpu(rec->e_cpos),
5573*4882a593Smuzhiyun 		ocfs2_rec_clusters(el, rec));
5574*4882a593Smuzhiyun 
5575*4882a593Smuzhiyun 	if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
5576*4882a593Smuzhiyun 		ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5577*4882a593Smuzhiyun 					 cpos, len);
5578*4882a593Smuzhiyun 		if (ret) {
5579*4882a593Smuzhiyun 			mlog_errno(ret);
5580*4882a593Smuzhiyun 			goto out;
5581*4882a593Smuzhiyun 		}
5582*4882a593Smuzhiyun 	} else {
5583*4882a593Smuzhiyun 		ret = ocfs2_split_tree(handle, et, path, index,
5584*4882a593Smuzhiyun 				       trunc_range, meta_ac);
5585*4882a593Smuzhiyun 		if (ret) {
5586*4882a593Smuzhiyun 			mlog_errno(ret);
5587*4882a593Smuzhiyun 			goto out;
5588*4882a593Smuzhiyun 		}
5589*4882a593Smuzhiyun 
5590*4882a593Smuzhiyun 		/*
5591*4882a593Smuzhiyun 		 * The split could have manipulated the tree enough to
5592*4882a593Smuzhiyun 		 * move the record location, so we have to look for it again.
5593*4882a593Smuzhiyun 		 */
5594*4882a593Smuzhiyun 		ocfs2_reinit_path(path, 1);
5595*4882a593Smuzhiyun 
5596*4882a593Smuzhiyun 		ret = ocfs2_find_path(et->et_ci, path, cpos);
5597*4882a593Smuzhiyun 		if (ret) {
5598*4882a593Smuzhiyun 			mlog_errno(ret);
5599*4882a593Smuzhiyun 			goto out;
5600*4882a593Smuzhiyun 		}
5601*4882a593Smuzhiyun 
5602*4882a593Smuzhiyun 		el = path_leaf_el(path);
5603*4882a593Smuzhiyun 		index = ocfs2_search_extent_list(el, cpos);
5604*4882a593Smuzhiyun 		if (index == -1) {
5605*4882a593Smuzhiyun 			ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5606*4882a593Smuzhiyun 				    "Owner %llu: split at cpos %u lost record\n",
5607*4882a593Smuzhiyun 				    (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5608*4882a593Smuzhiyun 				    cpos);
5609*4882a593Smuzhiyun 			ret = -EROFS;
5610*4882a593Smuzhiyun 			goto out;
5611*4882a593Smuzhiyun 		}
5612*4882a593Smuzhiyun 
5613*4882a593Smuzhiyun 		/*
5614*4882a593Smuzhiyun 		 * Double check our values here. If anything is fishy,
5615*4882a593Smuzhiyun 		 * it's easier to catch it at the top level.
5616*4882a593Smuzhiyun 		 */
5617*4882a593Smuzhiyun 		rec = &el->l_recs[index];
5618*4882a593Smuzhiyun 		rec_range = le32_to_cpu(rec->e_cpos) +
5619*4882a593Smuzhiyun 			ocfs2_rec_clusters(el, rec);
5620*4882a593Smuzhiyun 		if (rec_range != trunc_range) {
5621*4882a593Smuzhiyun 			ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5622*4882a593Smuzhiyun 				    "Owner %llu: error after split at cpos %u trunc len %u, existing record is (%u,%u)\n",
5623*4882a593Smuzhiyun 				    (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5624*4882a593Smuzhiyun 				    cpos, len, le32_to_cpu(rec->e_cpos),
5625*4882a593Smuzhiyun 				    ocfs2_rec_clusters(el, rec));
5626*4882a593Smuzhiyun 			ret = -EROFS;
5627*4882a593Smuzhiyun 			goto out;
5628*4882a593Smuzhiyun 		}
5629*4882a593Smuzhiyun 
5630*4882a593Smuzhiyun 		ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5631*4882a593Smuzhiyun 					 cpos, len);
5632*4882a593Smuzhiyun 		if (ret)
5633*4882a593Smuzhiyun 			mlog_errno(ret);
5634*4882a593Smuzhiyun 	}
5635*4882a593Smuzhiyun 
5636*4882a593Smuzhiyun out:
5637*4882a593Smuzhiyun 	ocfs2_free_path(path);
5638*4882a593Smuzhiyun 	return ret;
5639*4882a593Smuzhiyun }
5640*4882a593Smuzhiyun 
5641*4882a593Smuzhiyun /*
5642*4882a593Smuzhiyun  * ocfs2_reserve_blocks_for_rec_trunc() would look basically the
5643*4882a593Smuzhiyun  * same as ocfs2_lock_alloctors(), except for it accepts a blocks
5644*4882a593Smuzhiyun  * number to reserve some extra blocks, and it only handles meta
5645*4882a593Smuzhiyun  * data allocations.
5646*4882a593Smuzhiyun  *
5647*4882a593Smuzhiyun  * Currently, only ocfs2_remove_btree_range() uses it for truncating
5648*4882a593Smuzhiyun  * and punching holes.
5649*4882a593Smuzhiyun  */
ocfs2_reserve_blocks_for_rec_trunc(struct inode * inode,struct ocfs2_extent_tree * et,u32 extents_to_split,struct ocfs2_alloc_context ** ac,int extra_blocks)5650*4882a593Smuzhiyun static int ocfs2_reserve_blocks_for_rec_trunc(struct inode *inode,
5651*4882a593Smuzhiyun 					      struct ocfs2_extent_tree *et,
5652*4882a593Smuzhiyun 					      u32 extents_to_split,
5653*4882a593Smuzhiyun 					      struct ocfs2_alloc_context **ac,
5654*4882a593Smuzhiyun 					      int extra_blocks)
5655*4882a593Smuzhiyun {
5656*4882a593Smuzhiyun 	int ret = 0, num_free_extents;
5657*4882a593Smuzhiyun 	unsigned int max_recs_needed = 2 * extents_to_split;
5658*4882a593Smuzhiyun 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5659*4882a593Smuzhiyun 
5660*4882a593Smuzhiyun 	*ac = NULL;
5661*4882a593Smuzhiyun 
5662*4882a593Smuzhiyun 	num_free_extents = ocfs2_num_free_extents(et);
5663*4882a593Smuzhiyun 	if (num_free_extents < 0) {
5664*4882a593Smuzhiyun 		ret = num_free_extents;
5665*4882a593Smuzhiyun 		mlog_errno(ret);
5666*4882a593Smuzhiyun 		goto out;
5667*4882a593Smuzhiyun 	}
5668*4882a593Smuzhiyun 
5669*4882a593Smuzhiyun 	if (!num_free_extents ||
5670*4882a593Smuzhiyun 	    (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed))
5671*4882a593Smuzhiyun 		extra_blocks += ocfs2_extend_meta_needed(et->et_root_el);
5672*4882a593Smuzhiyun 
5673*4882a593Smuzhiyun 	if (extra_blocks) {
5674*4882a593Smuzhiyun 		ret = ocfs2_reserve_new_metadata_blocks(osb, extra_blocks, ac);
5675*4882a593Smuzhiyun 		if (ret < 0) {
5676*4882a593Smuzhiyun 			if (ret != -ENOSPC)
5677*4882a593Smuzhiyun 				mlog_errno(ret);
5678*4882a593Smuzhiyun 		}
5679*4882a593Smuzhiyun 	}
5680*4882a593Smuzhiyun 
5681*4882a593Smuzhiyun out:
5682*4882a593Smuzhiyun 	if (ret) {
5683*4882a593Smuzhiyun 		if (*ac) {
5684*4882a593Smuzhiyun 			ocfs2_free_alloc_context(*ac);
5685*4882a593Smuzhiyun 			*ac = NULL;
5686*4882a593Smuzhiyun 		}
5687*4882a593Smuzhiyun 	}
5688*4882a593Smuzhiyun 
5689*4882a593Smuzhiyun 	return ret;
5690*4882a593Smuzhiyun }
5691*4882a593Smuzhiyun 
ocfs2_remove_btree_range(struct inode * inode,struct ocfs2_extent_tree * et,u32 cpos,u32 phys_cpos,u32 len,int flags,struct ocfs2_cached_dealloc_ctxt * dealloc,u64 refcount_loc,bool refcount_tree_locked)5692*4882a593Smuzhiyun int ocfs2_remove_btree_range(struct inode *inode,
5693*4882a593Smuzhiyun 			     struct ocfs2_extent_tree *et,
5694*4882a593Smuzhiyun 			     u32 cpos, u32 phys_cpos, u32 len, int flags,
5695*4882a593Smuzhiyun 			     struct ocfs2_cached_dealloc_ctxt *dealloc,
5696*4882a593Smuzhiyun 			     u64 refcount_loc, bool refcount_tree_locked)
5697*4882a593Smuzhiyun {
5698*4882a593Smuzhiyun 	int ret, credits = 0, extra_blocks = 0;
5699*4882a593Smuzhiyun 	u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
5700*4882a593Smuzhiyun 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5701*4882a593Smuzhiyun 	struct inode *tl_inode = osb->osb_tl_inode;
5702*4882a593Smuzhiyun 	handle_t *handle;
5703*4882a593Smuzhiyun 	struct ocfs2_alloc_context *meta_ac = NULL;
5704*4882a593Smuzhiyun 	struct ocfs2_refcount_tree *ref_tree = NULL;
5705*4882a593Smuzhiyun 
5706*4882a593Smuzhiyun 	if ((flags & OCFS2_EXT_REFCOUNTED) && len) {
5707*4882a593Smuzhiyun 		BUG_ON(!ocfs2_is_refcount_inode(inode));
5708*4882a593Smuzhiyun 
5709*4882a593Smuzhiyun 		if (!refcount_tree_locked) {
5710*4882a593Smuzhiyun 			ret = ocfs2_lock_refcount_tree(osb, refcount_loc, 1,
5711*4882a593Smuzhiyun 						       &ref_tree, NULL);
5712*4882a593Smuzhiyun 			if (ret) {
5713*4882a593Smuzhiyun 				mlog_errno(ret);
5714*4882a593Smuzhiyun 				goto bail;
5715*4882a593Smuzhiyun 			}
5716*4882a593Smuzhiyun 		}
5717*4882a593Smuzhiyun 
5718*4882a593Smuzhiyun 		ret = ocfs2_prepare_refcount_change_for_del(inode,
5719*4882a593Smuzhiyun 							    refcount_loc,
5720*4882a593Smuzhiyun 							    phys_blkno,
5721*4882a593Smuzhiyun 							    len,
5722*4882a593Smuzhiyun 							    &credits,
5723*4882a593Smuzhiyun 							    &extra_blocks);
5724*4882a593Smuzhiyun 		if (ret < 0) {
5725*4882a593Smuzhiyun 			mlog_errno(ret);
5726*4882a593Smuzhiyun 			goto bail;
5727*4882a593Smuzhiyun 		}
5728*4882a593Smuzhiyun 	}
5729*4882a593Smuzhiyun 
5730*4882a593Smuzhiyun 	ret = ocfs2_reserve_blocks_for_rec_trunc(inode, et, 1, &meta_ac,
5731*4882a593Smuzhiyun 						 extra_blocks);
5732*4882a593Smuzhiyun 	if (ret) {
5733*4882a593Smuzhiyun 		mlog_errno(ret);
5734*4882a593Smuzhiyun 		goto bail;
5735*4882a593Smuzhiyun 	}
5736*4882a593Smuzhiyun 
5737*4882a593Smuzhiyun 	inode_lock(tl_inode);
5738*4882a593Smuzhiyun 
5739*4882a593Smuzhiyun 	if (ocfs2_truncate_log_needs_flush(osb)) {
5740*4882a593Smuzhiyun 		ret = __ocfs2_flush_truncate_log(osb);
5741*4882a593Smuzhiyun 		if (ret < 0) {
5742*4882a593Smuzhiyun 			mlog_errno(ret);
5743*4882a593Smuzhiyun 			goto out;
5744*4882a593Smuzhiyun 		}
5745*4882a593Smuzhiyun 	}
5746*4882a593Smuzhiyun 
5747*4882a593Smuzhiyun 	handle = ocfs2_start_trans(osb,
5748*4882a593Smuzhiyun 			ocfs2_remove_extent_credits(osb->sb) + credits);
5749*4882a593Smuzhiyun 	if (IS_ERR(handle)) {
5750*4882a593Smuzhiyun 		ret = PTR_ERR(handle);
5751*4882a593Smuzhiyun 		mlog_errno(ret);
5752*4882a593Smuzhiyun 		goto out;
5753*4882a593Smuzhiyun 	}
5754*4882a593Smuzhiyun 
5755*4882a593Smuzhiyun 	ret = ocfs2_et_root_journal_access(handle, et,
5756*4882a593Smuzhiyun 					   OCFS2_JOURNAL_ACCESS_WRITE);
5757*4882a593Smuzhiyun 	if (ret) {
5758*4882a593Smuzhiyun 		mlog_errno(ret);
5759*4882a593Smuzhiyun 		goto out_commit;
5760*4882a593Smuzhiyun 	}
5761*4882a593Smuzhiyun 
5762*4882a593Smuzhiyun 	dquot_free_space_nodirty(inode,
5763*4882a593Smuzhiyun 				  ocfs2_clusters_to_bytes(inode->i_sb, len));
5764*4882a593Smuzhiyun 
5765*4882a593Smuzhiyun 	ret = ocfs2_remove_extent(handle, et, cpos, len, meta_ac, dealloc);
5766*4882a593Smuzhiyun 	if (ret) {
5767*4882a593Smuzhiyun 		mlog_errno(ret);
5768*4882a593Smuzhiyun 		goto out_commit;
5769*4882a593Smuzhiyun 	}
5770*4882a593Smuzhiyun 
5771*4882a593Smuzhiyun 	ocfs2_et_update_clusters(et, -len);
5772*4882a593Smuzhiyun 	ocfs2_update_inode_fsync_trans(handle, inode, 1);
5773*4882a593Smuzhiyun 
5774*4882a593Smuzhiyun 	ocfs2_journal_dirty(handle, et->et_root_bh);
5775*4882a593Smuzhiyun 
5776*4882a593Smuzhiyun 	if (phys_blkno) {
5777*4882a593Smuzhiyun 		if (flags & OCFS2_EXT_REFCOUNTED)
5778*4882a593Smuzhiyun 			ret = ocfs2_decrease_refcount(inode, handle,
5779*4882a593Smuzhiyun 					ocfs2_blocks_to_clusters(osb->sb,
5780*4882a593Smuzhiyun 								 phys_blkno),
5781*4882a593Smuzhiyun 					len, meta_ac,
5782*4882a593Smuzhiyun 					dealloc, 1);
5783*4882a593Smuzhiyun 		else
5784*4882a593Smuzhiyun 			ret = ocfs2_truncate_log_append(osb, handle,
5785*4882a593Smuzhiyun 							phys_blkno, len);
5786*4882a593Smuzhiyun 		if (ret)
5787*4882a593Smuzhiyun 			mlog_errno(ret);
5788*4882a593Smuzhiyun 
5789*4882a593Smuzhiyun 	}
5790*4882a593Smuzhiyun 
5791*4882a593Smuzhiyun out_commit:
5792*4882a593Smuzhiyun 	ocfs2_commit_trans(osb, handle);
5793*4882a593Smuzhiyun out:
5794*4882a593Smuzhiyun 	inode_unlock(tl_inode);
5795*4882a593Smuzhiyun bail:
5796*4882a593Smuzhiyun 	if (meta_ac)
5797*4882a593Smuzhiyun 		ocfs2_free_alloc_context(meta_ac);
5798*4882a593Smuzhiyun 
5799*4882a593Smuzhiyun 	if (ref_tree)
5800*4882a593Smuzhiyun 		ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
5801*4882a593Smuzhiyun 
5802*4882a593Smuzhiyun 	return ret;
5803*4882a593Smuzhiyun }
5804*4882a593Smuzhiyun 
ocfs2_truncate_log_needs_flush(struct ocfs2_super * osb)5805*4882a593Smuzhiyun int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
5806*4882a593Smuzhiyun {
5807*4882a593Smuzhiyun 	struct buffer_head *tl_bh = osb->osb_tl_bh;
5808*4882a593Smuzhiyun 	struct ocfs2_dinode *di;
5809*4882a593Smuzhiyun 	struct ocfs2_truncate_log *tl;
5810*4882a593Smuzhiyun 
5811*4882a593Smuzhiyun 	di = (struct ocfs2_dinode *) tl_bh->b_data;
5812*4882a593Smuzhiyun 	tl = &di->id2.i_dealloc;
5813*4882a593Smuzhiyun 
5814*4882a593Smuzhiyun 	mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5815*4882a593Smuzhiyun 			"slot %d, invalid truncate log parameters: used = "
5816*4882a593Smuzhiyun 			"%u, count = %u\n", osb->slot_num,
5817*4882a593Smuzhiyun 			le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5818*4882a593Smuzhiyun 	return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5819*4882a593Smuzhiyun }
5820*4882a593Smuzhiyun 
ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log * tl,unsigned int new_start)5821*4882a593Smuzhiyun static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5822*4882a593Smuzhiyun 					   unsigned int new_start)
5823*4882a593Smuzhiyun {
5824*4882a593Smuzhiyun 	unsigned int tail_index;
5825*4882a593Smuzhiyun 	unsigned int current_tail;
5826*4882a593Smuzhiyun 
5827*4882a593Smuzhiyun 	/* No records, nothing to coalesce */
5828*4882a593Smuzhiyun 	if (!le16_to_cpu(tl->tl_used))
5829*4882a593Smuzhiyun 		return 0;
5830*4882a593Smuzhiyun 
5831*4882a593Smuzhiyun 	tail_index = le16_to_cpu(tl->tl_used) - 1;
5832*4882a593Smuzhiyun 	current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5833*4882a593Smuzhiyun 	current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5834*4882a593Smuzhiyun 
5835*4882a593Smuzhiyun 	return current_tail == new_start;
5836*4882a593Smuzhiyun }
5837*4882a593Smuzhiyun 
ocfs2_truncate_log_append(struct ocfs2_super * osb,handle_t * handle,u64 start_blk,unsigned int num_clusters)5838*4882a593Smuzhiyun int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5839*4882a593Smuzhiyun 			      handle_t *handle,
5840*4882a593Smuzhiyun 			      u64 start_blk,
5841*4882a593Smuzhiyun 			      unsigned int num_clusters)
5842*4882a593Smuzhiyun {
5843*4882a593Smuzhiyun 	int status, index;
5844*4882a593Smuzhiyun 	unsigned int start_cluster, tl_count;
5845*4882a593Smuzhiyun 	struct inode *tl_inode = osb->osb_tl_inode;
5846*4882a593Smuzhiyun 	struct buffer_head *tl_bh = osb->osb_tl_bh;
5847*4882a593Smuzhiyun 	struct ocfs2_dinode *di;
5848*4882a593Smuzhiyun 	struct ocfs2_truncate_log *tl;
5849*4882a593Smuzhiyun 
5850*4882a593Smuzhiyun 	BUG_ON(inode_trylock(tl_inode));
5851*4882a593Smuzhiyun 
5852*4882a593Smuzhiyun 	start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5853*4882a593Smuzhiyun 
5854*4882a593Smuzhiyun 	di = (struct ocfs2_dinode *) tl_bh->b_data;
5855*4882a593Smuzhiyun 
5856*4882a593Smuzhiyun 	/* tl_bh is loaded from ocfs2_truncate_log_init().  It's validated
5857*4882a593Smuzhiyun 	 * by the underlying call to ocfs2_read_inode_block(), so any
5858*4882a593Smuzhiyun 	 * corruption is a code bug */
5859*4882a593Smuzhiyun 	BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5860*4882a593Smuzhiyun 
5861*4882a593Smuzhiyun 	tl = &di->id2.i_dealloc;
5862*4882a593Smuzhiyun 	tl_count = le16_to_cpu(tl->tl_count);
5863*4882a593Smuzhiyun 	mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5864*4882a593Smuzhiyun 			tl_count == 0,
5865*4882a593Smuzhiyun 			"Truncate record count on #%llu invalid "
5866*4882a593Smuzhiyun 			"wanted %u, actual %u\n",
5867*4882a593Smuzhiyun 			(unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
5868*4882a593Smuzhiyun 			ocfs2_truncate_recs_per_inode(osb->sb),
5869*4882a593Smuzhiyun 			le16_to_cpu(tl->tl_count));
5870*4882a593Smuzhiyun 
5871*4882a593Smuzhiyun 	/* Caller should have known to flush before calling us. */
5872*4882a593Smuzhiyun 	index = le16_to_cpu(tl->tl_used);
5873*4882a593Smuzhiyun 	if (index >= tl_count) {
5874*4882a593Smuzhiyun 		status = -ENOSPC;
5875*4882a593Smuzhiyun 		mlog_errno(status);
5876*4882a593Smuzhiyun 		goto bail;
5877*4882a593Smuzhiyun 	}
5878*4882a593Smuzhiyun 
5879*4882a593Smuzhiyun 	status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
5880*4882a593Smuzhiyun 					 OCFS2_JOURNAL_ACCESS_WRITE);
5881*4882a593Smuzhiyun 	if (status < 0) {
5882*4882a593Smuzhiyun 		mlog_errno(status);
5883*4882a593Smuzhiyun 		goto bail;
5884*4882a593Smuzhiyun 	}
5885*4882a593Smuzhiyun 
5886*4882a593Smuzhiyun 	trace_ocfs2_truncate_log_append(
5887*4882a593Smuzhiyun 		(unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index,
5888*4882a593Smuzhiyun 		start_cluster, num_clusters);
5889*4882a593Smuzhiyun 	if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5890*4882a593Smuzhiyun 		/*
5891*4882a593Smuzhiyun 		 * Move index back to the record we are coalescing with.
5892*4882a593Smuzhiyun 		 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5893*4882a593Smuzhiyun 		 */
5894*4882a593Smuzhiyun 		index--;
5895*4882a593Smuzhiyun 
5896*4882a593Smuzhiyun 		num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5897*4882a593Smuzhiyun 		trace_ocfs2_truncate_log_append(
5898*4882a593Smuzhiyun 			(unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
5899*4882a593Smuzhiyun 			index, le32_to_cpu(tl->tl_recs[index].t_start),
5900*4882a593Smuzhiyun 			num_clusters);
5901*4882a593Smuzhiyun 	} else {
5902*4882a593Smuzhiyun 		tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5903*4882a593Smuzhiyun 		tl->tl_used = cpu_to_le16(index + 1);
5904*4882a593Smuzhiyun 	}
5905*4882a593Smuzhiyun 	tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5906*4882a593Smuzhiyun 
5907*4882a593Smuzhiyun 	ocfs2_journal_dirty(handle, tl_bh);
5908*4882a593Smuzhiyun 
5909*4882a593Smuzhiyun 	osb->truncated_clusters += num_clusters;
5910*4882a593Smuzhiyun bail:
5911*4882a593Smuzhiyun 	return status;
5912*4882a593Smuzhiyun }
5913*4882a593Smuzhiyun 
ocfs2_replay_truncate_records(struct ocfs2_super * osb,struct inode * data_alloc_inode,struct buffer_head * data_alloc_bh)5914*4882a593Smuzhiyun static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
5915*4882a593Smuzhiyun 					 struct inode *data_alloc_inode,
5916*4882a593Smuzhiyun 					 struct buffer_head *data_alloc_bh)
5917*4882a593Smuzhiyun {
5918*4882a593Smuzhiyun 	int status = 0;
5919*4882a593Smuzhiyun 	int i;
5920*4882a593Smuzhiyun 	unsigned int num_clusters;
5921*4882a593Smuzhiyun 	u64 start_blk;
5922*4882a593Smuzhiyun 	struct ocfs2_truncate_rec rec;
5923*4882a593Smuzhiyun 	struct ocfs2_dinode *di;
5924*4882a593Smuzhiyun 	struct ocfs2_truncate_log *tl;
5925*4882a593Smuzhiyun 	struct inode *tl_inode = osb->osb_tl_inode;
5926*4882a593Smuzhiyun 	struct buffer_head *tl_bh = osb->osb_tl_bh;
5927*4882a593Smuzhiyun 	handle_t *handle;
5928*4882a593Smuzhiyun 
5929*4882a593Smuzhiyun 	di = (struct ocfs2_dinode *) tl_bh->b_data;
5930*4882a593Smuzhiyun 	tl = &di->id2.i_dealloc;
5931*4882a593Smuzhiyun 	i = le16_to_cpu(tl->tl_used) - 1;
5932*4882a593Smuzhiyun 	while (i >= 0) {
5933*4882a593Smuzhiyun 		handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5934*4882a593Smuzhiyun 		if (IS_ERR(handle)) {
5935*4882a593Smuzhiyun 			status = PTR_ERR(handle);
5936*4882a593Smuzhiyun 			mlog_errno(status);
5937*4882a593Smuzhiyun 			goto bail;
5938*4882a593Smuzhiyun 		}
5939*4882a593Smuzhiyun 
5940*4882a593Smuzhiyun 		/* Caller has given us at least enough credits to
5941*4882a593Smuzhiyun 		 * update the truncate log dinode */
5942*4882a593Smuzhiyun 		status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
5943*4882a593Smuzhiyun 						 OCFS2_JOURNAL_ACCESS_WRITE);
5944*4882a593Smuzhiyun 		if (status < 0) {
5945*4882a593Smuzhiyun 			mlog_errno(status);
5946*4882a593Smuzhiyun 			goto bail;
5947*4882a593Smuzhiyun 		}
5948*4882a593Smuzhiyun 
5949*4882a593Smuzhiyun 		tl->tl_used = cpu_to_le16(i);
5950*4882a593Smuzhiyun 
5951*4882a593Smuzhiyun 		ocfs2_journal_dirty(handle, tl_bh);
5952*4882a593Smuzhiyun 
5953*4882a593Smuzhiyun 		rec = tl->tl_recs[i];
5954*4882a593Smuzhiyun 		start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5955*4882a593Smuzhiyun 						    le32_to_cpu(rec.t_start));
5956*4882a593Smuzhiyun 		num_clusters = le32_to_cpu(rec.t_clusters);
5957*4882a593Smuzhiyun 
5958*4882a593Smuzhiyun 		/* if start_blk is not set, we ignore the record as
5959*4882a593Smuzhiyun 		 * invalid. */
5960*4882a593Smuzhiyun 		if (start_blk) {
5961*4882a593Smuzhiyun 			trace_ocfs2_replay_truncate_records(
5962*4882a593Smuzhiyun 				(unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
5963*4882a593Smuzhiyun 				i, le32_to_cpu(rec.t_start), num_clusters);
5964*4882a593Smuzhiyun 
5965*4882a593Smuzhiyun 			status = ocfs2_free_clusters(handle, data_alloc_inode,
5966*4882a593Smuzhiyun 						     data_alloc_bh, start_blk,
5967*4882a593Smuzhiyun 						     num_clusters);
5968*4882a593Smuzhiyun 			if (status < 0) {
5969*4882a593Smuzhiyun 				mlog_errno(status);
5970*4882a593Smuzhiyun 				goto bail;
5971*4882a593Smuzhiyun 			}
5972*4882a593Smuzhiyun 		}
5973*4882a593Smuzhiyun 
5974*4882a593Smuzhiyun 		ocfs2_commit_trans(osb, handle);
5975*4882a593Smuzhiyun 		i--;
5976*4882a593Smuzhiyun 	}
5977*4882a593Smuzhiyun 
5978*4882a593Smuzhiyun 	osb->truncated_clusters = 0;
5979*4882a593Smuzhiyun 
5980*4882a593Smuzhiyun bail:
5981*4882a593Smuzhiyun 	return status;
5982*4882a593Smuzhiyun }
5983*4882a593Smuzhiyun 
5984*4882a593Smuzhiyun /* Expects you to already be holding tl_inode->i_mutex */
__ocfs2_flush_truncate_log(struct ocfs2_super * osb)5985*4882a593Smuzhiyun int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5986*4882a593Smuzhiyun {
5987*4882a593Smuzhiyun 	int status;
5988*4882a593Smuzhiyun 	unsigned int num_to_flush;
5989*4882a593Smuzhiyun 	struct inode *tl_inode = osb->osb_tl_inode;
5990*4882a593Smuzhiyun 	struct inode *data_alloc_inode = NULL;
5991*4882a593Smuzhiyun 	struct buffer_head *tl_bh = osb->osb_tl_bh;
5992*4882a593Smuzhiyun 	struct buffer_head *data_alloc_bh = NULL;
5993*4882a593Smuzhiyun 	struct ocfs2_dinode *di;
5994*4882a593Smuzhiyun 	struct ocfs2_truncate_log *tl;
5995*4882a593Smuzhiyun 	struct ocfs2_journal *journal = osb->journal;
5996*4882a593Smuzhiyun 
5997*4882a593Smuzhiyun 	BUG_ON(inode_trylock(tl_inode));
5998*4882a593Smuzhiyun 
5999*4882a593Smuzhiyun 	di = (struct ocfs2_dinode *) tl_bh->b_data;
6000*4882a593Smuzhiyun 
6001*4882a593Smuzhiyun 	/* tl_bh is loaded from ocfs2_truncate_log_init().  It's validated
6002*4882a593Smuzhiyun 	 * by the underlying call to ocfs2_read_inode_block(), so any
6003*4882a593Smuzhiyun 	 * corruption is a code bug */
6004*4882a593Smuzhiyun 	BUG_ON(!OCFS2_IS_VALID_DINODE(di));
6005*4882a593Smuzhiyun 
6006*4882a593Smuzhiyun 	tl = &di->id2.i_dealloc;
6007*4882a593Smuzhiyun 	num_to_flush = le16_to_cpu(tl->tl_used);
6008*4882a593Smuzhiyun 	trace_ocfs2_flush_truncate_log(
6009*4882a593Smuzhiyun 		(unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
6010*4882a593Smuzhiyun 		num_to_flush);
6011*4882a593Smuzhiyun 	if (!num_to_flush) {
6012*4882a593Smuzhiyun 		status = 0;
6013*4882a593Smuzhiyun 		goto out;
6014*4882a593Smuzhiyun 	}
6015*4882a593Smuzhiyun 
6016*4882a593Smuzhiyun 	/* Appending truncate log(TA) and flushing truncate log(TF) are
6017*4882a593Smuzhiyun 	 * two separated transactions. They can be both committed but not
6018*4882a593Smuzhiyun 	 * checkpointed. If crash occurs then, both two transaction will be
6019*4882a593Smuzhiyun 	 * replayed with several already released to global bitmap clusters.
6020*4882a593Smuzhiyun 	 * Then truncate log will be replayed resulting in cluster double free.
6021*4882a593Smuzhiyun 	 */
6022*4882a593Smuzhiyun 	jbd2_journal_lock_updates(journal->j_journal);
6023*4882a593Smuzhiyun 	status = jbd2_journal_flush(journal->j_journal);
6024*4882a593Smuzhiyun 	jbd2_journal_unlock_updates(journal->j_journal);
6025*4882a593Smuzhiyun 	if (status < 0) {
6026*4882a593Smuzhiyun 		mlog_errno(status);
6027*4882a593Smuzhiyun 		goto out;
6028*4882a593Smuzhiyun 	}
6029*4882a593Smuzhiyun 
6030*4882a593Smuzhiyun 	data_alloc_inode = ocfs2_get_system_file_inode(osb,
6031*4882a593Smuzhiyun 						       GLOBAL_BITMAP_SYSTEM_INODE,
6032*4882a593Smuzhiyun 						       OCFS2_INVALID_SLOT);
6033*4882a593Smuzhiyun 	if (!data_alloc_inode) {
6034*4882a593Smuzhiyun 		status = -EINVAL;
6035*4882a593Smuzhiyun 		mlog(ML_ERROR, "Could not get bitmap inode!\n");
6036*4882a593Smuzhiyun 		goto out;
6037*4882a593Smuzhiyun 	}
6038*4882a593Smuzhiyun 
6039*4882a593Smuzhiyun 	inode_lock(data_alloc_inode);
6040*4882a593Smuzhiyun 
6041*4882a593Smuzhiyun 	status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
6042*4882a593Smuzhiyun 	if (status < 0) {
6043*4882a593Smuzhiyun 		mlog_errno(status);
6044*4882a593Smuzhiyun 		goto out_mutex;
6045*4882a593Smuzhiyun 	}
6046*4882a593Smuzhiyun 
6047*4882a593Smuzhiyun 	status = ocfs2_replay_truncate_records(osb, data_alloc_inode,
6048*4882a593Smuzhiyun 					       data_alloc_bh);
6049*4882a593Smuzhiyun 	if (status < 0)
6050*4882a593Smuzhiyun 		mlog_errno(status);
6051*4882a593Smuzhiyun 
6052*4882a593Smuzhiyun 	brelse(data_alloc_bh);
6053*4882a593Smuzhiyun 	ocfs2_inode_unlock(data_alloc_inode, 1);
6054*4882a593Smuzhiyun 
6055*4882a593Smuzhiyun out_mutex:
6056*4882a593Smuzhiyun 	inode_unlock(data_alloc_inode);
6057*4882a593Smuzhiyun 	iput(data_alloc_inode);
6058*4882a593Smuzhiyun 
6059*4882a593Smuzhiyun out:
6060*4882a593Smuzhiyun 	return status;
6061*4882a593Smuzhiyun }
6062*4882a593Smuzhiyun 
ocfs2_flush_truncate_log(struct ocfs2_super * osb)6063*4882a593Smuzhiyun int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
6064*4882a593Smuzhiyun {
6065*4882a593Smuzhiyun 	int status;
6066*4882a593Smuzhiyun 	struct inode *tl_inode = osb->osb_tl_inode;
6067*4882a593Smuzhiyun 
6068*4882a593Smuzhiyun 	inode_lock(tl_inode);
6069*4882a593Smuzhiyun 	status = __ocfs2_flush_truncate_log(osb);
6070*4882a593Smuzhiyun 	inode_unlock(tl_inode);
6071*4882a593Smuzhiyun 
6072*4882a593Smuzhiyun 	return status;
6073*4882a593Smuzhiyun }
6074*4882a593Smuzhiyun 
ocfs2_truncate_log_worker(struct work_struct * work)6075*4882a593Smuzhiyun static void ocfs2_truncate_log_worker(struct work_struct *work)
6076*4882a593Smuzhiyun {
6077*4882a593Smuzhiyun 	int status;
6078*4882a593Smuzhiyun 	struct ocfs2_super *osb =
6079*4882a593Smuzhiyun 		container_of(work, struct ocfs2_super,
6080*4882a593Smuzhiyun 			     osb_truncate_log_wq.work);
6081*4882a593Smuzhiyun 
6082*4882a593Smuzhiyun 	status = ocfs2_flush_truncate_log(osb);
6083*4882a593Smuzhiyun 	if (status < 0)
6084*4882a593Smuzhiyun 		mlog_errno(status);
6085*4882a593Smuzhiyun 	else
6086*4882a593Smuzhiyun 		ocfs2_init_steal_slots(osb);
6087*4882a593Smuzhiyun }
6088*4882a593Smuzhiyun 
6089*4882a593Smuzhiyun #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
ocfs2_schedule_truncate_log_flush(struct ocfs2_super * osb,int cancel)6090*4882a593Smuzhiyun void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
6091*4882a593Smuzhiyun 				       int cancel)
6092*4882a593Smuzhiyun {
6093*4882a593Smuzhiyun 	if (osb->osb_tl_inode &&
6094*4882a593Smuzhiyun 			atomic_read(&osb->osb_tl_disable) == 0) {
6095*4882a593Smuzhiyun 		/* We want to push off log flushes while truncates are
6096*4882a593Smuzhiyun 		 * still running. */
6097*4882a593Smuzhiyun 		if (cancel)
6098*4882a593Smuzhiyun 			cancel_delayed_work(&osb->osb_truncate_log_wq);
6099*4882a593Smuzhiyun 
6100*4882a593Smuzhiyun 		queue_delayed_work(osb->ocfs2_wq, &osb->osb_truncate_log_wq,
6101*4882a593Smuzhiyun 				   OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
6102*4882a593Smuzhiyun 	}
6103*4882a593Smuzhiyun }
6104*4882a593Smuzhiyun 
6105*4882a593Smuzhiyun /*
6106*4882a593Smuzhiyun  * Try to flush truncate logs if we can free enough clusters from it.
6107*4882a593Smuzhiyun  * As for return value, "< 0" means error, "0" no space and "1" means
6108*4882a593Smuzhiyun  * we have freed enough spaces and let the caller try to allocate again.
6109*4882a593Smuzhiyun  */
ocfs2_try_to_free_truncate_log(struct ocfs2_super * osb,unsigned int needed)6110*4882a593Smuzhiyun int ocfs2_try_to_free_truncate_log(struct ocfs2_super *osb,
6111*4882a593Smuzhiyun 					unsigned int needed)
6112*4882a593Smuzhiyun {
6113*4882a593Smuzhiyun 	tid_t target;
6114*4882a593Smuzhiyun 	int ret = 0;
6115*4882a593Smuzhiyun 	unsigned int truncated_clusters;
6116*4882a593Smuzhiyun 
6117*4882a593Smuzhiyun 	inode_lock(osb->osb_tl_inode);
6118*4882a593Smuzhiyun 	truncated_clusters = osb->truncated_clusters;
6119*4882a593Smuzhiyun 	inode_unlock(osb->osb_tl_inode);
6120*4882a593Smuzhiyun 
6121*4882a593Smuzhiyun 	/*
6122*4882a593Smuzhiyun 	 * Check whether we can succeed in allocating if we free
6123*4882a593Smuzhiyun 	 * the truncate log.
6124*4882a593Smuzhiyun 	 */
6125*4882a593Smuzhiyun 	if (truncated_clusters < needed)
6126*4882a593Smuzhiyun 		goto out;
6127*4882a593Smuzhiyun 
6128*4882a593Smuzhiyun 	ret = ocfs2_flush_truncate_log(osb);
6129*4882a593Smuzhiyun 	if (ret) {
6130*4882a593Smuzhiyun 		mlog_errno(ret);
6131*4882a593Smuzhiyun 		goto out;
6132*4882a593Smuzhiyun 	}
6133*4882a593Smuzhiyun 
6134*4882a593Smuzhiyun 	if (jbd2_journal_start_commit(osb->journal->j_journal, &target)) {
6135*4882a593Smuzhiyun 		jbd2_log_wait_commit(osb->journal->j_journal, target);
6136*4882a593Smuzhiyun 		ret = 1;
6137*4882a593Smuzhiyun 	}
6138*4882a593Smuzhiyun out:
6139*4882a593Smuzhiyun 	return ret;
6140*4882a593Smuzhiyun }
6141*4882a593Smuzhiyun 
ocfs2_get_truncate_log_info(struct ocfs2_super * osb,int slot_num,struct inode ** tl_inode,struct buffer_head ** tl_bh)6142*4882a593Smuzhiyun static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
6143*4882a593Smuzhiyun 				       int slot_num,
6144*4882a593Smuzhiyun 				       struct inode **tl_inode,
6145*4882a593Smuzhiyun 				       struct buffer_head **tl_bh)
6146*4882a593Smuzhiyun {
6147*4882a593Smuzhiyun 	int status;
6148*4882a593Smuzhiyun 	struct inode *inode = NULL;
6149*4882a593Smuzhiyun 	struct buffer_head *bh = NULL;
6150*4882a593Smuzhiyun 
6151*4882a593Smuzhiyun 	inode = ocfs2_get_system_file_inode(osb,
6152*4882a593Smuzhiyun 					   TRUNCATE_LOG_SYSTEM_INODE,
6153*4882a593Smuzhiyun 					   slot_num);
6154*4882a593Smuzhiyun 	if (!inode) {
6155*4882a593Smuzhiyun 		status = -EINVAL;
6156*4882a593Smuzhiyun 		mlog(ML_ERROR, "Could not get load truncate log inode!\n");
6157*4882a593Smuzhiyun 		goto bail;
6158*4882a593Smuzhiyun 	}
6159*4882a593Smuzhiyun 
6160*4882a593Smuzhiyun 	status = ocfs2_read_inode_block(inode, &bh);
6161*4882a593Smuzhiyun 	if (status < 0) {
6162*4882a593Smuzhiyun 		iput(inode);
6163*4882a593Smuzhiyun 		mlog_errno(status);
6164*4882a593Smuzhiyun 		goto bail;
6165*4882a593Smuzhiyun 	}
6166*4882a593Smuzhiyun 
6167*4882a593Smuzhiyun 	*tl_inode = inode;
6168*4882a593Smuzhiyun 	*tl_bh    = bh;
6169*4882a593Smuzhiyun bail:
6170*4882a593Smuzhiyun 	return status;
6171*4882a593Smuzhiyun }
6172*4882a593Smuzhiyun 
6173*4882a593Smuzhiyun /* called during the 1st stage of node recovery. we stamp a clean
6174*4882a593Smuzhiyun  * truncate log and pass back a copy for processing later. if the
6175*4882a593Smuzhiyun  * truncate log does not require processing, a *tl_copy is set to
6176*4882a593Smuzhiyun  * NULL. */
ocfs2_begin_truncate_log_recovery(struct ocfs2_super * osb,int slot_num,struct ocfs2_dinode ** tl_copy)6177*4882a593Smuzhiyun int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
6178*4882a593Smuzhiyun 				      int slot_num,
6179*4882a593Smuzhiyun 				      struct ocfs2_dinode **tl_copy)
6180*4882a593Smuzhiyun {
6181*4882a593Smuzhiyun 	int status;
6182*4882a593Smuzhiyun 	struct inode *tl_inode = NULL;
6183*4882a593Smuzhiyun 	struct buffer_head *tl_bh = NULL;
6184*4882a593Smuzhiyun 	struct ocfs2_dinode *di;
6185*4882a593Smuzhiyun 	struct ocfs2_truncate_log *tl;
6186*4882a593Smuzhiyun 
6187*4882a593Smuzhiyun 	*tl_copy = NULL;
6188*4882a593Smuzhiyun 
6189*4882a593Smuzhiyun 	trace_ocfs2_begin_truncate_log_recovery(slot_num);
6190*4882a593Smuzhiyun 
6191*4882a593Smuzhiyun 	status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
6192*4882a593Smuzhiyun 	if (status < 0) {
6193*4882a593Smuzhiyun 		mlog_errno(status);
6194*4882a593Smuzhiyun 		goto bail;
6195*4882a593Smuzhiyun 	}
6196*4882a593Smuzhiyun 
6197*4882a593Smuzhiyun 	di = (struct ocfs2_dinode *) tl_bh->b_data;
6198*4882a593Smuzhiyun 
6199*4882a593Smuzhiyun 	/* tl_bh is loaded from ocfs2_get_truncate_log_info().  It's
6200*4882a593Smuzhiyun 	 * validated by the underlying call to ocfs2_read_inode_block(),
6201*4882a593Smuzhiyun 	 * so any corruption is a code bug */
6202*4882a593Smuzhiyun 	BUG_ON(!OCFS2_IS_VALID_DINODE(di));
6203*4882a593Smuzhiyun 
6204*4882a593Smuzhiyun 	tl = &di->id2.i_dealloc;
6205*4882a593Smuzhiyun 	if (le16_to_cpu(tl->tl_used)) {
6206*4882a593Smuzhiyun 		trace_ocfs2_truncate_log_recovery_num(le16_to_cpu(tl->tl_used));
6207*4882a593Smuzhiyun 
6208*4882a593Smuzhiyun 		/*
6209*4882a593Smuzhiyun 		 * Assuming the write-out below goes well, this copy will be
6210*4882a593Smuzhiyun 		 * passed back to recovery for processing.
6211*4882a593Smuzhiyun 		 */
6212*4882a593Smuzhiyun 		*tl_copy = kmemdup(tl_bh->b_data, tl_bh->b_size, GFP_KERNEL);
6213*4882a593Smuzhiyun 		if (!(*tl_copy)) {
6214*4882a593Smuzhiyun 			status = -ENOMEM;
6215*4882a593Smuzhiyun 			mlog_errno(status);
6216*4882a593Smuzhiyun 			goto bail;
6217*4882a593Smuzhiyun 		}
6218*4882a593Smuzhiyun 
6219*4882a593Smuzhiyun 		/* All we need to do to clear the truncate log is set
6220*4882a593Smuzhiyun 		 * tl_used. */
6221*4882a593Smuzhiyun 		tl->tl_used = 0;
6222*4882a593Smuzhiyun 
6223*4882a593Smuzhiyun 		ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check);
6224*4882a593Smuzhiyun 		status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode));
6225*4882a593Smuzhiyun 		if (status < 0) {
6226*4882a593Smuzhiyun 			mlog_errno(status);
6227*4882a593Smuzhiyun 			goto bail;
6228*4882a593Smuzhiyun 		}
6229*4882a593Smuzhiyun 	}
6230*4882a593Smuzhiyun 
6231*4882a593Smuzhiyun bail:
6232*4882a593Smuzhiyun 	iput(tl_inode);
6233*4882a593Smuzhiyun 	brelse(tl_bh);
6234*4882a593Smuzhiyun 
6235*4882a593Smuzhiyun 	if (status < 0) {
6236*4882a593Smuzhiyun 		kfree(*tl_copy);
6237*4882a593Smuzhiyun 		*tl_copy = NULL;
6238*4882a593Smuzhiyun 		mlog_errno(status);
6239*4882a593Smuzhiyun 	}
6240*4882a593Smuzhiyun 
6241*4882a593Smuzhiyun 	return status;
6242*4882a593Smuzhiyun }
6243*4882a593Smuzhiyun 
ocfs2_complete_truncate_log_recovery(struct ocfs2_super * osb,struct ocfs2_dinode * tl_copy)6244*4882a593Smuzhiyun int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
6245*4882a593Smuzhiyun 					 struct ocfs2_dinode *tl_copy)
6246*4882a593Smuzhiyun {
6247*4882a593Smuzhiyun 	int status = 0;
6248*4882a593Smuzhiyun 	int i;
6249*4882a593Smuzhiyun 	unsigned int clusters, num_recs, start_cluster;
6250*4882a593Smuzhiyun 	u64 start_blk;
6251*4882a593Smuzhiyun 	handle_t *handle;
6252*4882a593Smuzhiyun 	struct inode *tl_inode = osb->osb_tl_inode;
6253*4882a593Smuzhiyun 	struct ocfs2_truncate_log *tl;
6254*4882a593Smuzhiyun 
6255*4882a593Smuzhiyun 	if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
6256*4882a593Smuzhiyun 		mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
6257*4882a593Smuzhiyun 		return -EINVAL;
6258*4882a593Smuzhiyun 	}
6259*4882a593Smuzhiyun 
6260*4882a593Smuzhiyun 	tl = &tl_copy->id2.i_dealloc;
6261*4882a593Smuzhiyun 	num_recs = le16_to_cpu(tl->tl_used);
6262*4882a593Smuzhiyun 	trace_ocfs2_complete_truncate_log_recovery(
6263*4882a593Smuzhiyun 		(unsigned long long)le64_to_cpu(tl_copy->i_blkno),
6264*4882a593Smuzhiyun 		num_recs);
6265*4882a593Smuzhiyun 
6266*4882a593Smuzhiyun 	inode_lock(tl_inode);
6267*4882a593Smuzhiyun 	for(i = 0; i < num_recs; i++) {
6268*4882a593Smuzhiyun 		if (ocfs2_truncate_log_needs_flush(osb)) {
6269*4882a593Smuzhiyun 			status = __ocfs2_flush_truncate_log(osb);
6270*4882a593Smuzhiyun 			if (status < 0) {
6271*4882a593Smuzhiyun 				mlog_errno(status);
6272*4882a593Smuzhiyun 				goto bail_up;
6273*4882a593Smuzhiyun 			}
6274*4882a593Smuzhiyun 		}
6275*4882a593Smuzhiyun 
6276*4882a593Smuzhiyun 		handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6277*4882a593Smuzhiyun 		if (IS_ERR(handle)) {
6278*4882a593Smuzhiyun 			status = PTR_ERR(handle);
6279*4882a593Smuzhiyun 			mlog_errno(status);
6280*4882a593Smuzhiyun 			goto bail_up;
6281*4882a593Smuzhiyun 		}
6282*4882a593Smuzhiyun 
6283*4882a593Smuzhiyun 		clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
6284*4882a593Smuzhiyun 		start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
6285*4882a593Smuzhiyun 		start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
6286*4882a593Smuzhiyun 
6287*4882a593Smuzhiyun 		status = ocfs2_truncate_log_append(osb, handle,
6288*4882a593Smuzhiyun 						   start_blk, clusters);
6289*4882a593Smuzhiyun 		ocfs2_commit_trans(osb, handle);
6290*4882a593Smuzhiyun 		if (status < 0) {
6291*4882a593Smuzhiyun 			mlog_errno(status);
6292*4882a593Smuzhiyun 			goto bail_up;
6293*4882a593Smuzhiyun 		}
6294*4882a593Smuzhiyun 	}
6295*4882a593Smuzhiyun 
6296*4882a593Smuzhiyun bail_up:
6297*4882a593Smuzhiyun 	inode_unlock(tl_inode);
6298*4882a593Smuzhiyun 
6299*4882a593Smuzhiyun 	return status;
6300*4882a593Smuzhiyun }
6301*4882a593Smuzhiyun 
ocfs2_truncate_log_shutdown(struct ocfs2_super * osb)6302*4882a593Smuzhiyun void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
6303*4882a593Smuzhiyun {
6304*4882a593Smuzhiyun 	int status;
6305*4882a593Smuzhiyun 	struct inode *tl_inode = osb->osb_tl_inode;
6306*4882a593Smuzhiyun 
6307*4882a593Smuzhiyun 	atomic_set(&osb->osb_tl_disable, 1);
6308*4882a593Smuzhiyun 
6309*4882a593Smuzhiyun 	if (tl_inode) {
6310*4882a593Smuzhiyun 		cancel_delayed_work(&osb->osb_truncate_log_wq);
6311*4882a593Smuzhiyun 		flush_workqueue(osb->ocfs2_wq);
6312*4882a593Smuzhiyun 
6313*4882a593Smuzhiyun 		status = ocfs2_flush_truncate_log(osb);
6314*4882a593Smuzhiyun 		if (status < 0)
6315*4882a593Smuzhiyun 			mlog_errno(status);
6316*4882a593Smuzhiyun 
6317*4882a593Smuzhiyun 		brelse(osb->osb_tl_bh);
6318*4882a593Smuzhiyun 		iput(osb->osb_tl_inode);
6319*4882a593Smuzhiyun 	}
6320*4882a593Smuzhiyun }
6321*4882a593Smuzhiyun 
ocfs2_truncate_log_init(struct ocfs2_super * osb)6322*4882a593Smuzhiyun int ocfs2_truncate_log_init(struct ocfs2_super *osb)
6323*4882a593Smuzhiyun {
6324*4882a593Smuzhiyun 	int status;
6325*4882a593Smuzhiyun 	struct inode *tl_inode = NULL;
6326*4882a593Smuzhiyun 	struct buffer_head *tl_bh = NULL;
6327*4882a593Smuzhiyun 
6328*4882a593Smuzhiyun 	status = ocfs2_get_truncate_log_info(osb,
6329*4882a593Smuzhiyun 					     osb->slot_num,
6330*4882a593Smuzhiyun 					     &tl_inode,
6331*4882a593Smuzhiyun 					     &tl_bh);
6332*4882a593Smuzhiyun 	if (status < 0)
6333*4882a593Smuzhiyun 		mlog_errno(status);
6334*4882a593Smuzhiyun 
6335*4882a593Smuzhiyun 	/* ocfs2_truncate_log_shutdown keys on the existence of
6336*4882a593Smuzhiyun 	 * osb->osb_tl_inode so we don't set any of the osb variables
6337*4882a593Smuzhiyun 	 * until we're sure all is well. */
6338*4882a593Smuzhiyun 	INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
6339*4882a593Smuzhiyun 			  ocfs2_truncate_log_worker);
6340*4882a593Smuzhiyun 	atomic_set(&osb->osb_tl_disable, 0);
6341*4882a593Smuzhiyun 	osb->osb_tl_bh    = tl_bh;
6342*4882a593Smuzhiyun 	osb->osb_tl_inode = tl_inode;
6343*4882a593Smuzhiyun 
6344*4882a593Smuzhiyun 	return status;
6345*4882a593Smuzhiyun }
6346*4882a593Smuzhiyun 
6347*4882a593Smuzhiyun /*
6348*4882a593Smuzhiyun  * Delayed de-allocation of suballocator blocks.
6349*4882a593Smuzhiyun  *
6350*4882a593Smuzhiyun  * Some sets of block de-allocations might involve multiple suballocator inodes.
6351*4882a593Smuzhiyun  *
6352*4882a593Smuzhiyun  * The locking for this can get extremely complicated, especially when
6353*4882a593Smuzhiyun  * the suballocator inodes to delete from aren't known until deep
6354*4882a593Smuzhiyun  * within an unrelated codepath.
6355*4882a593Smuzhiyun  *
6356*4882a593Smuzhiyun  * ocfs2_extent_block structures are a good example of this - an inode
6357*4882a593Smuzhiyun  * btree could have been grown by any number of nodes each allocating
6358*4882a593Smuzhiyun  * out of their own suballoc inode.
6359*4882a593Smuzhiyun  *
6360*4882a593Smuzhiyun  * These structures allow the delay of block de-allocation until a
6361*4882a593Smuzhiyun  * later time, when locking of multiple cluster inodes won't cause
6362*4882a593Smuzhiyun  * deadlock.
6363*4882a593Smuzhiyun  */
6364*4882a593Smuzhiyun 
6365*4882a593Smuzhiyun /*
6366*4882a593Smuzhiyun  * Describe a single bit freed from a suballocator.  For the block
6367*4882a593Smuzhiyun  * suballocators, it represents one block.  For the global cluster
6368*4882a593Smuzhiyun  * allocator, it represents some clusters and free_bit indicates
6369*4882a593Smuzhiyun  * clusters number.
6370*4882a593Smuzhiyun  */
6371*4882a593Smuzhiyun struct ocfs2_cached_block_free {
6372*4882a593Smuzhiyun 	struct ocfs2_cached_block_free		*free_next;
6373*4882a593Smuzhiyun 	u64					free_bg;
6374*4882a593Smuzhiyun 	u64					free_blk;
6375*4882a593Smuzhiyun 	unsigned int				free_bit;
6376*4882a593Smuzhiyun };
6377*4882a593Smuzhiyun 
6378*4882a593Smuzhiyun struct ocfs2_per_slot_free_list {
6379*4882a593Smuzhiyun 	struct ocfs2_per_slot_free_list		*f_next_suballocator;
6380*4882a593Smuzhiyun 	int					f_inode_type;
6381*4882a593Smuzhiyun 	int					f_slot;
6382*4882a593Smuzhiyun 	struct ocfs2_cached_block_free		*f_first;
6383*4882a593Smuzhiyun };
6384*4882a593Smuzhiyun 
ocfs2_free_cached_blocks(struct ocfs2_super * osb,int sysfile_type,int slot,struct ocfs2_cached_block_free * head)6385*4882a593Smuzhiyun static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
6386*4882a593Smuzhiyun 				    int sysfile_type,
6387*4882a593Smuzhiyun 				    int slot,
6388*4882a593Smuzhiyun 				    struct ocfs2_cached_block_free *head)
6389*4882a593Smuzhiyun {
6390*4882a593Smuzhiyun 	int ret;
6391*4882a593Smuzhiyun 	u64 bg_blkno;
6392*4882a593Smuzhiyun 	handle_t *handle;
6393*4882a593Smuzhiyun 	struct inode *inode;
6394*4882a593Smuzhiyun 	struct buffer_head *di_bh = NULL;
6395*4882a593Smuzhiyun 	struct ocfs2_cached_block_free *tmp;
6396*4882a593Smuzhiyun 
6397*4882a593Smuzhiyun 	inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
6398*4882a593Smuzhiyun 	if (!inode) {
6399*4882a593Smuzhiyun 		ret = -EINVAL;
6400*4882a593Smuzhiyun 		mlog_errno(ret);
6401*4882a593Smuzhiyun 		goto out;
6402*4882a593Smuzhiyun 	}
6403*4882a593Smuzhiyun 
6404*4882a593Smuzhiyun 	inode_lock(inode);
6405*4882a593Smuzhiyun 
6406*4882a593Smuzhiyun 	ret = ocfs2_inode_lock(inode, &di_bh, 1);
6407*4882a593Smuzhiyun 	if (ret) {
6408*4882a593Smuzhiyun 		mlog_errno(ret);
6409*4882a593Smuzhiyun 		goto out_mutex;
6410*4882a593Smuzhiyun 	}
6411*4882a593Smuzhiyun 
6412*4882a593Smuzhiyun 	while (head) {
6413*4882a593Smuzhiyun 		if (head->free_bg)
6414*4882a593Smuzhiyun 			bg_blkno = head->free_bg;
6415*4882a593Smuzhiyun 		else
6416*4882a593Smuzhiyun 			bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
6417*4882a593Smuzhiyun 							      head->free_bit);
6418*4882a593Smuzhiyun 		handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
6419*4882a593Smuzhiyun 		if (IS_ERR(handle)) {
6420*4882a593Smuzhiyun 			ret = PTR_ERR(handle);
6421*4882a593Smuzhiyun 			mlog_errno(ret);
6422*4882a593Smuzhiyun 			goto out_unlock;
6423*4882a593Smuzhiyun 		}
6424*4882a593Smuzhiyun 
6425*4882a593Smuzhiyun 		trace_ocfs2_free_cached_blocks(
6426*4882a593Smuzhiyun 		     (unsigned long long)head->free_blk, head->free_bit);
6427*4882a593Smuzhiyun 
6428*4882a593Smuzhiyun 		ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
6429*4882a593Smuzhiyun 					       head->free_bit, bg_blkno, 1);
6430*4882a593Smuzhiyun 		if (ret)
6431*4882a593Smuzhiyun 			mlog_errno(ret);
6432*4882a593Smuzhiyun 
6433*4882a593Smuzhiyun 		ocfs2_commit_trans(osb, handle);
6434*4882a593Smuzhiyun 
6435*4882a593Smuzhiyun 		tmp = head;
6436*4882a593Smuzhiyun 		head = head->free_next;
6437*4882a593Smuzhiyun 		kfree(tmp);
6438*4882a593Smuzhiyun 	}
6439*4882a593Smuzhiyun 
6440*4882a593Smuzhiyun out_unlock:
6441*4882a593Smuzhiyun 	ocfs2_inode_unlock(inode, 1);
6442*4882a593Smuzhiyun 	brelse(di_bh);
6443*4882a593Smuzhiyun out_mutex:
6444*4882a593Smuzhiyun 	inode_unlock(inode);
6445*4882a593Smuzhiyun 	iput(inode);
6446*4882a593Smuzhiyun out:
6447*4882a593Smuzhiyun 	while(head) {
6448*4882a593Smuzhiyun 		/* Premature exit may have left some dangling items. */
6449*4882a593Smuzhiyun 		tmp = head;
6450*4882a593Smuzhiyun 		head = head->free_next;
6451*4882a593Smuzhiyun 		kfree(tmp);
6452*4882a593Smuzhiyun 	}
6453*4882a593Smuzhiyun 
6454*4882a593Smuzhiyun 	return ret;
6455*4882a593Smuzhiyun }
6456*4882a593Smuzhiyun 
ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt * ctxt,u64 blkno,unsigned int bit)6457*4882a593Smuzhiyun int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6458*4882a593Smuzhiyun 				u64 blkno, unsigned int bit)
6459*4882a593Smuzhiyun {
6460*4882a593Smuzhiyun 	int ret = 0;
6461*4882a593Smuzhiyun 	struct ocfs2_cached_block_free *item;
6462*4882a593Smuzhiyun 
6463*4882a593Smuzhiyun 	item = kzalloc(sizeof(*item), GFP_NOFS);
6464*4882a593Smuzhiyun 	if (item == NULL) {
6465*4882a593Smuzhiyun 		ret = -ENOMEM;
6466*4882a593Smuzhiyun 		mlog_errno(ret);
6467*4882a593Smuzhiyun 		return ret;
6468*4882a593Smuzhiyun 	}
6469*4882a593Smuzhiyun 
6470*4882a593Smuzhiyun 	trace_ocfs2_cache_cluster_dealloc((unsigned long long)blkno, bit);
6471*4882a593Smuzhiyun 
6472*4882a593Smuzhiyun 	item->free_blk = blkno;
6473*4882a593Smuzhiyun 	item->free_bit = bit;
6474*4882a593Smuzhiyun 	item->free_next = ctxt->c_global_allocator;
6475*4882a593Smuzhiyun 
6476*4882a593Smuzhiyun 	ctxt->c_global_allocator = item;
6477*4882a593Smuzhiyun 	return ret;
6478*4882a593Smuzhiyun }
6479*4882a593Smuzhiyun 
ocfs2_free_cached_clusters(struct ocfs2_super * osb,struct ocfs2_cached_block_free * head)6480*4882a593Smuzhiyun static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
6481*4882a593Smuzhiyun 				      struct ocfs2_cached_block_free *head)
6482*4882a593Smuzhiyun {
6483*4882a593Smuzhiyun 	struct ocfs2_cached_block_free *tmp;
6484*4882a593Smuzhiyun 	struct inode *tl_inode = osb->osb_tl_inode;
6485*4882a593Smuzhiyun 	handle_t *handle;
6486*4882a593Smuzhiyun 	int ret = 0;
6487*4882a593Smuzhiyun 
6488*4882a593Smuzhiyun 	inode_lock(tl_inode);
6489*4882a593Smuzhiyun 
6490*4882a593Smuzhiyun 	while (head) {
6491*4882a593Smuzhiyun 		if (ocfs2_truncate_log_needs_flush(osb)) {
6492*4882a593Smuzhiyun 			ret = __ocfs2_flush_truncate_log(osb);
6493*4882a593Smuzhiyun 			if (ret < 0) {
6494*4882a593Smuzhiyun 				mlog_errno(ret);
6495*4882a593Smuzhiyun 				break;
6496*4882a593Smuzhiyun 			}
6497*4882a593Smuzhiyun 		}
6498*4882a593Smuzhiyun 
6499*4882a593Smuzhiyun 		handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6500*4882a593Smuzhiyun 		if (IS_ERR(handle)) {
6501*4882a593Smuzhiyun 			ret = PTR_ERR(handle);
6502*4882a593Smuzhiyun 			mlog_errno(ret);
6503*4882a593Smuzhiyun 			break;
6504*4882a593Smuzhiyun 		}
6505*4882a593Smuzhiyun 
6506*4882a593Smuzhiyun 		ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
6507*4882a593Smuzhiyun 						head->free_bit);
6508*4882a593Smuzhiyun 
6509*4882a593Smuzhiyun 		ocfs2_commit_trans(osb, handle);
6510*4882a593Smuzhiyun 		tmp = head;
6511*4882a593Smuzhiyun 		head = head->free_next;
6512*4882a593Smuzhiyun 		kfree(tmp);
6513*4882a593Smuzhiyun 
6514*4882a593Smuzhiyun 		if (ret < 0) {
6515*4882a593Smuzhiyun 			mlog_errno(ret);
6516*4882a593Smuzhiyun 			break;
6517*4882a593Smuzhiyun 		}
6518*4882a593Smuzhiyun 	}
6519*4882a593Smuzhiyun 
6520*4882a593Smuzhiyun 	inode_unlock(tl_inode);
6521*4882a593Smuzhiyun 
6522*4882a593Smuzhiyun 	while (head) {
6523*4882a593Smuzhiyun 		/* Premature exit may have left some dangling items. */
6524*4882a593Smuzhiyun 		tmp = head;
6525*4882a593Smuzhiyun 		head = head->free_next;
6526*4882a593Smuzhiyun 		kfree(tmp);
6527*4882a593Smuzhiyun 	}
6528*4882a593Smuzhiyun 
6529*4882a593Smuzhiyun 	return ret;
6530*4882a593Smuzhiyun }
6531*4882a593Smuzhiyun 
ocfs2_run_deallocs(struct ocfs2_super * osb,struct ocfs2_cached_dealloc_ctxt * ctxt)6532*4882a593Smuzhiyun int ocfs2_run_deallocs(struct ocfs2_super *osb,
6533*4882a593Smuzhiyun 		       struct ocfs2_cached_dealloc_ctxt *ctxt)
6534*4882a593Smuzhiyun {
6535*4882a593Smuzhiyun 	int ret = 0, ret2;
6536*4882a593Smuzhiyun 	struct ocfs2_per_slot_free_list *fl;
6537*4882a593Smuzhiyun 
6538*4882a593Smuzhiyun 	if (!ctxt)
6539*4882a593Smuzhiyun 		return 0;
6540*4882a593Smuzhiyun 
6541*4882a593Smuzhiyun 	while (ctxt->c_first_suballocator) {
6542*4882a593Smuzhiyun 		fl = ctxt->c_first_suballocator;
6543*4882a593Smuzhiyun 
6544*4882a593Smuzhiyun 		if (fl->f_first) {
6545*4882a593Smuzhiyun 			trace_ocfs2_run_deallocs(fl->f_inode_type,
6546*4882a593Smuzhiyun 						 fl->f_slot);
6547*4882a593Smuzhiyun 			ret2 = ocfs2_free_cached_blocks(osb,
6548*4882a593Smuzhiyun 							fl->f_inode_type,
6549*4882a593Smuzhiyun 							fl->f_slot,
6550*4882a593Smuzhiyun 							fl->f_first);
6551*4882a593Smuzhiyun 			if (ret2)
6552*4882a593Smuzhiyun 				mlog_errno(ret2);
6553*4882a593Smuzhiyun 			if (!ret)
6554*4882a593Smuzhiyun 				ret = ret2;
6555*4882a593Smuzhiyun 		}
6556*4882a593Smuzhiyun 
6557*4882a593Smuzhiyun 		ctxt->c_first_suballocator = fl->f_next_suballocator;
6558*4882a593Smuzhiyun 		kfree(fl);
6559*4882a593Smuzhiyun 	}
6560*4882a593Smuzhiyun 
6561*4882a593Smuzhiyun 	if (ctxt->c_global_allocator) {
6562*4882a593Smuzhiyun 		ret2 = ocfs2_free_cached_clusters(osb,
6563*4882a593Smuzhiyun 						  ctxt->c_global_allocator);
6564*4882a593Smuzhiyun 		if (ret2)
6565*4882a593Smuzhiyun 			mlog_errno(ret2);
6566*4882a593Smuzhiyun 		if (!ret)
6567*4882a593Smuzhiyun 			ret = ret2;
6568*4882a593Smuzhiyun 
6569*4882a593Smuzhiyun 		ctxt->c_global_allocator = NULL;
6570*4882a593Smuzhiyun 	}
6571*4882a593Smuzhiyun 
6572*4882a593Smuzhiyun 	return ret;
6573*4882a593Smuzhiyun }
6574*4882a593Smuzhiyun 
6575*4882a593Smuzhiyun static struct ocfs2_per_slot_free_list *
ocfs2_find_per_slot_free_list(int type,int slot,struct ocfs2_cached_dealloc_ctxt * ctxt)6576*4882a593Smuzhiyun ocfs2_find_per_slot_free_list(int type,
6577*4882a593Smuzhiyun 			      int slot,
6578*4882a593Smuzhiyun 			      struct ocfs2_cached_dealloc_ctxt *ctxt)
6579*4882a593Smuzhiyun {
6580*4882a593Smuzhiyun 	struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6581*4882a593Smuzhiyun 
6582*4882a593Smuzhiyun 	while (fl) {
6583*4882a593Smuzhiyun 		if (fl->f_inode_type == type && fl->f_slot == slot)
6584*4882a593Smuzhiyun 			return fl;
6585*4882a593Smuzhiyun 
6586*4882a593Smuzhiyun 		fl = fl->f_next_suballocator;
6587*4882a593Smuzhiyun 	}
6588*4882a593Smuzhiyun 
6589*4882a593Smuzhiyun 	fl = kmalloc(sizeof(*fl), GFP_NOFS);
6590*4882a593Smuzhiyun 	if (fl) {
6591*4882a593Smuzhiyun 		fl->f_inode_type = type;
6592*4882a593Smuzhiyun 		fl->f_slot = slot;
6593*4882a593Smuzhiyun 		fl->f_first = NULL;
6594*4882a593Smuzhiyun 		fl->f_next_suballocator = ctxt->c_first_suballocator;
6595*4882a593Smuzhiyun 
6596*4882a593Smuzhiyun 		ctxt->c_first_suballocator = fl;
6597*4882a593Smuzhiyun 	}
6598*4882a593Smuzhiyun 	return fl;
6599*4882a593Smuzhiyun }
6600*4882a593Smuzhiyun 
6601*4882a593Smuzhiyun static struct ocfs2_per_slot_free_list *
ocfs2_find_preferred_free_list(int type,int preferred_slot,int * real_slot,struct ocfs2_cached_dealloc_ctxt * ctxt)6602*4882a593Smuzhiyun ocfs2_find_preferred_free_list(int type,
6603*4882a593Smuzhiyun 			       int preferred_slot,
6604*4882a593Smuzhiyun 			       int *real_slot,
6605*4882a593Smuzhiyun 			       struct ocfs2_cached_dealloc_ctxt *ctxt)
6606*4882a593Smuzhiyun {
6607*4882a593Smuzhiyun 	struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6608*4882a593Smuzhiyun 
6609*4882a593Smuzhiyun 	while (fl) {
6610*4882a593Smuzhiyun 		if (fl->f_inode_type == type && fl->f_slot == preferred_slot) {
6611*4882a593Smuzhiyun 			*real_slot = fl->f_slot;
6612*4882a593Smuzhiyun 			return fl;
6613*4882a593Smuzhiyun 		}
6614*4882a593Smuzhiyun 
6615*4882a593Smuzhiyun 		fl = fl->f_next_suballocator;
6616*4882a593Smuzhiyun 	}
6617*4882a593Smuzhiyun 
6618*4882a593Smuzhiyun 	/* If we can't find any free list matching preferred slot, just use
6619*4882a593Smuzhiyun 	 * the first one.
6620*4882a593Smuzhiyun 	 */
6621*4882a593Smuzhiyun 	fl = ctxt->c_first_suballocator;
6622*4882a593Smuzhiyun 	*real_slot = fl->f_slot;
6623*4882a593Smuzhiyun 
6624*4882a593Smuzhiyun 	return fl;
6625*4882a593Smuzhiyun }
6626*4882a593Smuzhiyun 
6627*4882a593Smuzhiyun /* Return Value 1 indicates empty */
ocfs2_is_dealloc_empty(struct ocfs2_extent_tree * et)6628*4882a593Smuzhiyun static int ocfs2_is_dealloc_empty(struct ocfs2_extent_tree *et)
6629*4882a593Smuzhiyun {
6630*4882a593Smuzhiyun 	struct ocfs2_per_slot_free_list *fl = NULL;
6631*4882a593Smuzhiyun 
6632*4882a593Smuzhiyun 	if (!et->et_dealloc)
6633*4882a593Smuzhiyun 		return 1;
6634*4882a593Smuzhiyun 
6635*4882a593Smuzhiyun 	fl = et->et_dealloc->c_first_suballocator;
6636*4882a593Smuzhiyun 	if (!fl)
6637*4882a593Smuzhiyun 		return 1;
6638*4882a593Smuzhiyun 
6639*4882a593Smuzhiyun 	if (!fl->f_first)
6640*4882a593Smuzhiyun 		return 1;
6641*4882a593Smuzhiyun 
6642*4882a593Smuzhiyun 	return 0;
6643*4882a593Smuzhiyun }
6644*4882a593Smuzhiyun 
6645*4882a593Smuzhiyun /* If extent was deleted from tree due to extent rotation and merging, and
6646*4882a593Smuzhiyun  * no metadata is reserved ahead of time. Try to reuse some extents
6647*4882a593Smuzhiyun  * just deleted. This is only used to reuse extent blocks.
6648*4882a593Smuzhiyun  * It is supposed to find enough extent blocks in dealloc if our estimation
6649*4882a593Smuzhiyun  * on metadata is accurate.
6650*4882a593Smuzhiyun  */
ocfs2_reuse_blk_from_dealloc(handle_t * handle,struct ocfs2_extent_tree * et,struct buffer_head ** new_eb_bh,int blk_wanted,int * blk_given)6651*4882a593Smuzhiyun static int ocfs2_reuse_blk_from_dealloc(handle_t *handle,
6652*4882a593Smuzhiyun 					struct ocfs2_extent_tree *et,
6653*4882a593Smuzhiyun 					struct buffer_head **new_eb_bh,
6654*4882a593Smuzhiyun 					int blk_wanted, int *blk_given)
6655*4882a593Smuzhiyun {
6656*4882a593Smuzhiyun 	int i, status = 0, real_slot;
6657*4882a593Smuzhiyun 	struct ocfs2_cached_dealloc_ctxt *dealloc;
6658*4882a593Smuzhiyun 	struct ocfs2_per_slot_free_list *fl;
6659*4882a593Smuzhiyun 	struct ocfs2_cached_block_free *bf;
6660*4882a593Smuzhiyun 	struct ocfs2_extent_block *eb;
6661*4882a593Smuzhiyun 	struct ocfs2_super *osb =
6662*4882a593Smuzhiyun 		OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
6663*4882a593Smuzhiyun 
6664*4882a593Smuzhiyun 	*blk_given = 0;
6665*4882a593Smuzhiyun 
6666*4882a593Smuzhiyun 	/* If extent tree doesn't have a dealloc, this is not faulty. Just
6667*4882a593Smuzhiyun 	 * tell upper caller dealloc can't provide any block and it should
6668*4882a593Smuzhiyun 	 * ask for alloc to claim more space.
6669*4882a593Smuzhiyun 	 */
6670*4882a593Smuzhiyun 	dealloc = et->et_dealloc;
6671*4882a593Smuzhiyun 	if (!dealloc)
6672*4882a593Smuzhiyun 		goto bail;
6673*4882a593Smuzhiyun 
6674*4882a593Smuzhiyun 	for (i = 0; i < blk_wanted; i++) {
6675*4882a593Smuzhiyun 		/* Prefer to use local slot */
6676*4882a593Smuzhiyun 		fl = ocfs2_find_preferred_free_list(EXTENT_ALLOC_SYSTEM_INODE,
6677*4882a593Smuzhiyun 						    osb->slot_num, &real_slot,
6678*4882a593Smuzhiyun 						    dealloc);
6679*4882a593Smuzhiyun 		/* If no more block can be reused, we should claim more
6680*4882a593Smuzhiyun 		 * from alloc. Just return here normally.
6681*4882a593Smuzhiyun 		 */
6682*4882a593Smuzhiyun 		if (!fl) {
6683*4882a593Smuzhiyun 			status = 0;
6684*4882a593Smuzhiyun 			break;
6685*4882a593Smuzhiyun 		}
6686*4882a593Smuzhiyun 
6687*4882a593Smuzhiyun 		bf = fl->f_first;
6688*4882a593Smuzhiyun 		fl->f_first = bf->free_next;
6689*4882a593Smuzhiyun 
6690*4882a593Smuzhiyun 		new_eb_bh[i] = sb_getblk(osb->sb, bf->free_blk);
6691*4882a593Smuzhiyun 		if (new_eb_bh[i] == NULL) {
6692*4882a593Smuzhiyun 			status = -ENOMEM;
6693*4882a593Smuzhiyun 			mlog_errno(status);
6694*4882a593Smuzhiyun 			goto bail;
6695*4882a593Smuzhiyun 		}
6696*4882a593Smuzhiyun 
6697*4882a593Smuzhiyun 		mlog(0, "Reusing block(%llu) from "
6698*4882a593Smuzhiyun 		     "dealloc(local slot:%d, real slot:%d)\n",
6699*4882a593Smuzhiyun 		     bf->free_blk, osb->slot_num, real_slot);
6700*4882a593Smuzhiyun 
6701*4882a593Smuzhiyun 		ocfs2_set_new_buffer_uptodate(et->et_ci, new_eb_bh[i]);
6702*4882a593Smuzhiyun 
6703*4882a593Smuzhiyun 		status = ocfs2_journal_access_eb(handle, et->et_ci,
6704*4882a593Smuzhiyun 						 new_eb_bh[i],
6705*4882a593Smuzhiyun 						 OCFS2_JOURNAL_ACCESS_CREATE);
6706*4882a593Smuzhiyun 		if (status < 0) {
6707*4882a593Smuzhiyun 			mlog_errno(status);
6708*4882a593Smuzhiyun 			goto bail;
6709*4882a593Smuzhiyun 		}
6710*4882a593Smuzhiyun 
6711*4882a593Smuzhiyun 		memset(new_eb_bh[i]->b_data, 0, osb->sb->s_blocksize);
6712*4882a593Smuzhiyun 		eb = (struct ocfs2_extent_block *) new_eb_bh[i]->b_data;
6713*4882a593Smuzhiyun 
6714*4882a593Smuzhiyun 		/* We can't guarantee that buffer head is still cached, so
6715*4882a593Smuzhiyun 		 * polutlate the extent block again.
6716*4882a593Smuzhiyun 		 */
6717*4882a593Smuzhiyun 		strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
6718*4882a593Smuzhiyun 		eb->h_blkno = cpu_to_le64(bf->free_blk);
6719*4882a593Smuzhiyun 		eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
6720*4882a593Smuzhiyun 		eb->h_suballoc_slot = cpu_to_le16(real_slot);
6721*4882a593Smuzhiyun 		eb->h_suballoc_loc = cpu_to_le64(bf->free_bg);
6722*4882a593Smuzhiyun 		eb->h_suballoc_bit = cpu_to_le16(bf->free_bit);
6723*4882a593Smuzhiyun 		eb->h_list.l_count =
6724*4882a593Smuzhiyun 			cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
6725*4882a593Smuzhiyun 
6726*4882a593Smuzhiyun 		/* We'll also be dirtied by the caller, so
6727*4882a593Smuzhiyun 		 * this isn't absolutely necessary.
6728*4882a593Smuzhiyun 		 */
6729*4882a593Smuzhiyun 		ocfs2_journal_dirty(handle, new_eb_bh[i]);
6730*4882a593Smuzhiyun 
6731*4882a593Smuzhiyun 		if (!fl->f_first) {
6732*4882a593Smuzhiyun 			dealloc->c_first_suballocator = fl->f_next_suballocator;
6733*4882a593Smuzhiyun 			kfree(fl);
6734*4882a593Smuzhiyun 		}
6735*4882a593Smuzhiyun 		kfree(bf);
6736*4882a593Smuzhiyun 	}
6737*4882a593Smuzhiyun 
6738*4882a593Smuzhiyun 	*blk_given = i;
6739*4882a593Smuzhiyun 
6740*4882a593Smuzhiyun bail:
6741*4882a593Smuzhiyun 	if (unlikely(status < 0)) {
6742*4882a593Smuzhiyun 		for (i = 0; i < blk_wanted; i++)
6743*4882a593Smuzhiyun 			brelse(new_eb_bh[i]);
6744*4882a593Smuzhiyun 	}
6745*4882a593Smuzhiyun 
6746*4882a593Smuzhiyun 	return status;
6747*4882a593Smuzhiyun }
6748*4882a593Smuzhiyun 
ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt * ctxt,int type,int slot,u64 suballoc,u64 blkno,unsigned int bit)6749*4882a593Smuzhiyun int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6750*4882a593Smuzhiyun 			      int type, int slot, u64 suballoc,
6751*4882a593Smuzhiyun 			      u64 blkno, unsigned int bit)
6752*4882a593Smuzhiyun {
6753*4882a593Smuzhiyun 	int ret;
6754*4882a593Smuzhiyun 	struct ocfs2_per_slot_free_list *fl;
6755*4882a593Smuzhiyun 	struct ocfs2_cached_block_free *item;
6756*4882a593Smuzhiyun 
6757*4882a593Smuzhiyun 	fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6758*4882a593Smuzhiyun 	if (fl == NULL) {
6759*4882a593Smuzhiyun 		ret = -ENOMEM;
6760*4882a593Smuzhiyun 		mlog_errno(ret);
6761*4882a593Smuzhiyun 		goto out;
6762*4882a593Smuzhiyun 	}
6763*4882a593Smuzhiyun 
6764*4882a593Smuzhiyun 	item = kzalloc(sizeof(*item), GFP_NOFS);
6765*4882a593Smuzhiyun 	if (item == NULL) {
6766*4882a593Smuzhiyun 		ret = -ENOMEM;
6767*4882a593Smuzhiyun 		mlog_errno(ret);
6768*4882a593Smuzhiyun 		goto out;
6769*4882a593Smuzhiyun 	}
6770*4882a593Smuzhiyun 
6771*4882a593Smuzhiyun 	trace_ocfs2_cache_block_dealloc(type, slot,
6772*4882a593Smuzhiyun 					(unsigned long long)suballoc,
6773*4882a593Smuzhiyun 					(unsigned long long)blkno, bit);
6774*4882a593Smuzhiyun 
6775*4882a593Smuzhiyun 	item->free_bg = suballoc;
6776*4882a593Smuzhiyun 	item->free_blk = blkno;
6777*4882a593Smuzhiyun 	item->free_bit = bit;
6778*4882a593Smuzhiyun 	item->free_next = fl->f_first;
6779*4882a593Smuzhiyun 
6780*4882a593Smuzhiyun 	fl->f_first = item;
6781*4882a593Smuzhiyun 
6782*4882a593Smuzhiyun 	ret = 0;
6783*4882a593Smuzhiyun out:
6784*4882a593Smuzhiyun 	return ret;
6785*4882a593Smuzhiyun }
6786*4882a593Smuzhiyun 
ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt * ctxt,struct ocfs2_extent_block * eb)6787*4882a593Smuzhiyun static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6788*4882a593Smuzhiyun 					 struct ocfs2_extent_block *eb)
6789*4882a593Smuzhiyun {
6790*4882a593Smuzhiyun 	return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6791*4882a593Smuzhiyun 					 le16_to_cpu(eb->h_suballoc_slot),
6792*4882a593Smuzhiyun 					 le64_to_cpu(eb->h_suballoc_loc),
6793*4882a593Smuzhiyun 					 le64_to_cpu(eb->h_blkno),
6794*4882a593Smuzhiyun 					 le16_to_cpu(eb->h_suballoc_bit));
6795*4882a593Smuzhiyun }
6796*4882a593Smuzhiyun 
ocfs2_zero_func(handle_t * handle,struct buffer_head * bh)6797*4882a593Smuzhiyun static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
6798*4882a593Smuzhiyun {
6799*4882a593Smuzhiyun 	set_buffer_uptodate(bh);
6800*4882a593Smuzhiyun 	mark_buffer_dirty(bh);
6801*4882a593Smuzhiyun 	return 0;
6802*4882a593Smuzhiyun }
6803*4882a593Smuzhiyun 
ocfs2_map_and_dirty_page(struct inode * inode,handle_t * handle,unsigned int from,unsigned int to,struct page * page,int zero,u64 * phys)6804*4882a593Smuzhiyun void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6805*4882a593Smuzhiyun 			      unsigned int from, unsigned int to,
6806*4882a593Smuzhiyun 			      struct page *page, int zero, u64 *phys)
6807*4882a593Smuzhiyun {
6808*4882a593Smuzhiyun 	int ret, partial = 0;
6809*4882a593Smuzhiyun 	loff_t start_byte = ((loff_t)page->index << PAGE_SHIFT) + from;
6810*4882a593Smuzhiyun 	loff_t length = to - from;
6811*4882a593Smuzhiyun 
6812*4882a593Smuzhiyun 	ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6813*4882a593Smuzhiyun 	if (ret)
6814*4882a593Smuzhiyun 		mlog_errno(ret);
6815*4882a593Smuzhiyun 
6816*4882a593Smuzhiyun 	if (zero)
6817*4882a593Smuzhiyun 		zero_user_segment(page, from, to);
6818*4882a593Smuzhiyun 
6819*4882a593Smuzhiyun 	/*
6820*4882a593Smuzhiyun 	 * Need to set the buffers we zero'd into uptodate
6821*4882a593Smuzhiyun 	 * here if they aren't - ocfs2_map_page_blocks()
6822*4882a593Smuzhiyun 	 * might've skipped some
6823*4882a593Smuzhiyun 	 */
6824*4882a593Smuzhiyun 	ret = walk_page_buffers(handle, page_buffers(page),
6825*4882a593Smuzhiyun 				from, to, &partial,
6826*4882a593Smuzhiyun 				ocfs2_zero_func);
6827*4882a593Smuzhiyun 	if (ret < 0)
6828*4882a593Smuzhiyun 		mlog_errno(ret);
6829*4882a593Smuzhiyun 	else if (ocfs2_should_order_data(inode)) {
6830*4882a593Smuzhiyun 		ret = ocfs2_jbd2_inode_add_write(handle, inode,
6831*4882a593Smuzhiyun 						 start_byte, length);
6832*4882a593Smuzhiyun 		if (ret < 0)
6833*4882a593Smuzhiyun 			mlog_errno(ret);
6834*4882a593Smuzhiyun 	}
6835*4882a593Smuzhiyun 
6836*4882a593Smuzhiyun 	if (!partial)
6837*4882a593Smuzhiyun 		SetPageUptodate(page);
6838*4882a593Smuzhiyun 
6839*4882a593Smuzhiyun 	flush_dcache_page(page);
6840*4882a593Smuzhiyun }
6841*4882a593Smuzhiyun 
ocfs2_zero_cluster_pages(struct inode * inode,loff_t start,loff_t end,struct page ** pages,int numpages,u64 phys,handle_t * handle)6842*4882a593Smuzhiyun static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6843*4882a593Smuzhiyun 				     loff_t end, struct page **pages,
6844*4882a593Smuzhiyun 				     int numpages, u64 phys, handle_t *handle)
6845*4882a593Smuzhiyun {
6846*4882a593Smuzhiyun 	int i;
6847*4882a593Smuzhiyun 	struct page *page;
6848*4882a593Smuzhiyun 	unsigned int from, to = PAGE_SIZE;
6849*4882a593Smuzhiyun 	struct super_block *sb = inode->i_sb;
6850*4882a593Smuzhiyun 
6851*4882a593Smuzhiyun 	BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6852*4882a593Smuzhiyun 
6853*4882a593Smuzhiyun 	if (numpages == 0)
6854*4882a593Smuzhiyun 		goto out;
6855*4882a593Smuzhiyun 
6856*4882a593Smuzhiyun 	to = PAGE_SIZE;
6857*4882a593Smuzhiyun 	for(i = 0; i < numpages; i++) {
6858*4882a593Smuzhiyun 		page = pages[i];
6859*4882a593Smuzhiyun 
6860*4882a593Smuzhiyun 		from = start & (PAGE_SIZE - 1);
6861*4882a593Smuzhiyun 		if ((end >> PAGE_SHIFT) == page->index)
6862*4882a593Smuzhiyun 			to = end & (PAGE_SIZE - 1);
6863*4882a593Smuzhiyun 
6864*4882a593Smuzhiyun 		BUG_ON(from > PAGE_SIZE);
6865*4882a593Smuzhiyun 		BUG_ON(to > PAGE_SIZE);
6866*4882a593Smuzhiyun 
6867*4882a593Smuzhiyun 		ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6868*4882a593Smuzhiyun 					 &phys);
6869*4882a593Smuzhiyun 
6870*4882a593Smuzhiyun 		start = (page->index + 1) << PAGE_SHIFT;
6871*4882a593Smuzhiyun 	}
6872*4882a593Smuzhiyun out:
6873*4882a593Smuzhiyun 	if (pages)
6874*4882a593Smuzhiyun 		ocfs2_unlock_and_free_pages(pages, numpages);
6875*4882a593Smuzhiyun }
6876*4882a593Smuzhiyun 
ocfs2_grab_pages(struct inode * inode,loff_t start,loff_t end,struct page ** pages,int * num)6877*4882a593Smuzhiyun int ocfs2_grab_pages(struct inode *inode, loff_t start, loff_t end,
6878*4882a593Smuzhiyun 		     struct page **pages, int *num)
6879*4882a593Smuzhiyun {
6880*4882a593Smuzhiyun 	int numpages, ret = 0;
6881*4882a593Smuzhiyun 	struct address_space *mapping = inode->i_mapping;
6882*4882a593Smuzhiyun 	unsigned long index;
6883*4882a593Smuzhiyun 	loff_t last_page_bytes;
6884*4882a593Smuzhiyun 
6885*4882a593Smuzhiyun 	BUG_ON(start > end);
6886*4882a593Smuzhiyun 
6887*4882a593Smuzhiyun 	numpages = 0;
6888*4882a593Smuzhiyun 	last_page_bytes = PAGE_ALIGN(end);
6889*4882a593Smuzhiyun 	index = start >> PAGE_SHIFT;
6890*4882a593Smuzhiyun 	do {
6891*4882a593Smuzhiyun 		pages[numpages] = find_or_create_page(mapping, index, GFP_NOFS);
6892*4882a593Smuzhiyun 		if (!pages[numpages]) {
6893*4882a593Smuzhiyun 			ret = -ENOMEM;
6894*4882a593Smuzhiyun 			mlog_errno(ret);
6895*4882a593Smuzhiyun 			goto out;
6896*4882a593Smuzhiyun 		}
6897*4882a593Smuzhiyun 
6898*4882a593Smuzhiyun 		numpages++;
6899*4882a593Smuzhiyun 		index++;
6900*4882a593Smuzhiyun 	} while (index < (last_page_bytes >> PAGE_SHIFT));
6901*4882a593Smuzhiyun 
6902*4882a593Smuzhiyun out:
6903*4882a593Smuzhiyun 	if (ret != 0) {
6904*4882a593Smuzhiyun 		if (pages)
6905*4882a593Smuzhiyun 			ocfs2_unlock_and_free_pages(pages, numpages);
6906*4882a593Smuzhiyun 		numpages = 0;
6907*4882a593Smuzhiyun 	}
6908*4882a593Smuzhiyun 
6909*4882a593Smuzhiyun 	*num = numpages;
6910*4882a593Smuzhiyun 
6911*4882a593Smuzhiyun 	return ret;
6912*4882a593Smuzhiyun }
6913*4882a593Smuzhiyun 
ocfs2_grab_eof_pages(struct inode * inode,loff_t start,loff_t end,struct page ** pages,int * num)6914*4882a593Smuzhiyun static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
6915*4882a593Smuzhiyun 				struct page **pages, int *num)
6916*4882a593Smuzhiyun {
6917*4882a593Smuzhiyun 	struct super_block *sb = inode->i_sb;
6918*4882a593Smuzhiyun 
6919*4882a593Smuzhiyun 	BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6920*4882a593Smuzhiyun 	       (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6921*4882a593Smuzhiyun 
6922*4882a593Smuzhiyun 	return ocfs2_grab_pages(inode, start, end, pages, num);
6923*4882a593Smuzhiyun }
6924*4882a593Smuzhiyun 
6925*4882a593Smuzhiyun /*
6926*4882a593Smuzhiyun  * Zero the area past i_size but still within an allocated
6927*4882a593Smuzhiyun  * cluster. This avoids exposing nonzero data on subsequent file
6928*4882a593Smuzhiyun  * extends.
6929*4882a593Smuzhiyun  *
6930*4882a593Smuzhiyun  * We need to call this before i_size is updated on the inode because
6931*4882a593Smuzhiyun  * otherwise block_write_full_page() will skip writeout of pages past
6932*4882a593Smuzhiyun  * i_size. The new_i_size parameter is passed for this reason.
6933*4882a593Smuzhiyun  */
ocfs2_zero_range_for_truncate(struct inode * inode,handle_t * handle,u64 range_start,u64 range_end)6934*4882a593Smuzhiyun int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6935*4882a593Smuzhiyun 				  u64 range_start, u64 range_end)
6936*4882a593Smuzhiyun {
6937*4882a593Smuzhiyun 	int ret = 0, numpages;
6938*4882a593Smuzhiyun 	struct page **pages = NULL;
6939*4882a593Smuzhiyun 	u64 phys;
6940*4882a593Smuzhiyun 	unsigned int ext_flags;
6941*4882a593Smuzhiyun 	struct super_block *sb = inode->i_sb;
6942*4882a593Smuzhiyun 
6943*4882a593Smuzhiyun 	/*
6944*4882a593Smuzhiyun 	 * File systems which don't support sparse files zero on every
6945*4882a593Smuzhiyun 	 * extend.
6946*4882a593Smuzhiyun 	 */
6947*4882a593Smuzhiyun 	if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
6948*4882a593Smuzhiyun 		return 0;
6949*4882a593Smuzhiyun 
6950*4882a593Smuzhiyun 	pages = kcalloc(ocfs2_pages_per_cluster(sb),
6951*4882a593Smuzhiyun 			sizeof(struct page *), GFP_NOFS);
6952*4882a593Smuzhiyun 	if (pages == NULL) {
6953*4882a593Smuzhiyun 		ret = -ENOMEM;
6954*4882a593Smuzhiyun 		mlog_errno(ret);
6955*4882a593Smuzhiyun 		goto out;
6956*4882a593Smuzhiyun 	}
6957*4882a593Smuzhiyun 
6958*4882a593Smuzhiyun 	if (range_start == range_end)
6959*4882a593Smuzhiyun 		goto out;
6960*4882a593Smuzhiyun 
6961*4882a593Smuzhiyun 	ret = ocfs2_extent_map_get_blocks(inode,
6962*4882a593Smuzhiyun 					  range_start >> sb->s_blocksize_bits,
6963*4882a593Smuzhiyun 					  &phys, NULL, &ext_flags);
6964*4882a593Smuzhiyun 	if (ret) {
6965*4882a593Smuzhiyun 		mlog_errno(ret);
6966*4882a593Smuzhiyun 		goto out;
6967*4882a593Smuzhiyun 	}
6968*4882a593Smuzhiyun 
6969*4882a593Smuzhiyun 	/*
6970*4882a593Smuzhiyun 	 * Tail is a hole, or is marked unwritten. In either case, we
6971*4882a593Smuzhiyun 	 * can count on read and write to return/push zero's.
6972*4882a593Smuzhiyun 	 */
6973*4882a593Smuzhiyun 	if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
6974*4882a593Smuzhiyun 		goto out;
6975*4882a593Smuzhiyun 
6976*4882a593Smuzhiyun 	ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
6977*4882a593Smuzhiyun 				   &numpages);
6978*4882a593Smuzhiyun 	if (ret) {
6979*4882a593Smuzhiyun 		mlog_errno(ret);
6980*4882a593Smuzhiyun 		goto out;
6981*4882a593Smuzhiyun 	}
6982*4882a593Smuzhiyun 
6983*4882a593Smuzhiyun 	ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
6984*4882a593Smuzhiyun 				 numpages, phys, handle);
6985*4882a593Smuzhiyun 
6986*4882a593Smuzhiyun 	/*
6987*4882a593Smuzhiyun 	 * Initiate writeout of the pages we zero'd here. We don't
6988*4882a593Smuzhiyun 	 * wait on them - the truncate_inode_pages() call later will
6989*4882a593Smuzhiyun 	 * do that for us.
6990*4882a593Smuzhiyun 	 */
6991*4882a593Smuzhiyun 	ret = filemap_fdatawrite_range(inode->i_mapping, range_start,
6992*4882a593Smuzhiyun 				       range_end - 1);
6993*4882a593Smuzhiyun 	if (ret)
6994*4882a593Smuzhiyun 		mlog_errno(ret);
6995*4882a593Smuzhiyun 
6996*4882a593Smuzhiyun out:
6997*4882a593Smuzhiyun 	kfree(pages);
6998*4882a593Smuzhiyun 
6999*4882a593Smuzhiyun 	return ret;
7000*4882a593Smuzhiyun }
7001*4882a593Smuzhiyun 
ocfs2_zero_dinode_id2_with_xattr(struct inode * inode,struct ocfs2_dinode * di)7002*4882a593Smuzhiyun static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
7003*4882a593Smuzhiyun 					     struct ocfs2_dinode *di)
7004*4882a593Smuzhiyun {
7005*4882a593Smuzhiyun 	unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
7006*4882a593Smuzhiyun 	unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
7007*4882a593Smuzhiyun 
7008*4882a593Smuzhiyun 	if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
7009*4882a593Smuzhiyun 		memset(&di->id2, 0, blocksize -
7010*4882a593Smuzhiyun 				    offsetof(struct ocfs2_dinode, id2) -
7011*4882a593Smuzhiyun 				    xattrsize);
7012*4882a593Smuzhiyun 	else
7013*4882a593Smuzhiyun 		memset(&di->id2, 0, blocksize -
7014*4882a593Smuzhiyun 				    offsetof(struct ocfs2_dinode, id2));
7015*4882a593Smuzhiyun }
7016*4882a593Smuzhiyun 
ocfs2_dinode_new_extent_list(struct inode * inode,struct ocfs2_dinode * di)7017*4882a593Smuzhiyun void ocfs2_dinode_new_extent_list(struct inode *inode,
7018*4882a593Smuzhiyun 				  struct ocfs2_dinode *di)
7019*4882a593Smuzhiyun {
7020*4882a593Smuzhiyun 	ocfs2_zero_dinode_id2_with_xattr(inode, di);
7021*4882a593Smuzhiyun 	di->id2.i_list.l_tree_depth = 0;
7022*4882a593Smuzhiyun 	di->id2.i_list.l_next_free_rec = 0;
7023*4882a593Smuzhiyun 	di->id2.i_list.l_count = cpu_to_le16(
7024*4882a593Smuzhiyun 		ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
7025*4882a593Smuzhiyun }
7026*4882a593Smuzhiyun 
ocfs2_set_inode_data_inline(struct inode * inode,struct ocfs2_dinode * di)7027*4882a593Smuzhiyun void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
7028*4882a593Smuzhiyun {
7029*4882a593Smuzhiyun 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
7030*4882a593Smuzhiyun 	struct ocfs2_inline_data *idata = &di->id2.i_data;
7031*4882a593Smuzhiyun 
7032*4882a593Smuzhiyun 	spin_lock(&oi->ip_lock);
7033*4882a593Smuzhiyun 	oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
7034*4882a593Smuzhiyun 	di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7035*4882a593Smuzhiyun 	spin_unlock(&oi->ip_lock);
7036*4882a593Smuzhiyun 
7037*4882a593Smuzhiyun 	/*
7038*4882a593Smuzhiyun 	 * We clear the entire i_data structure here so that all
7039*4882a593Smuzhiyun 	 * fields can be properly initialized.
7040*4882a593Smuzhiyun 	 */
7041*4882a593Smuzhiyun 	ocfs2_zero_dinode_id2_with_xattr(inode, di);
7042*4882a593Smuzhiyun 
7043*4882a593Smuzhiyun 	idata->id_count = cpu_to_le16(
7044*4882a593Smuzhiyun 			ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
7045*4882a593Smuzhiyun }
7046*4882a593Smuzhiyun 
ocfs2_convert_inline_data_to_extents(struct inode * inode,struct buffer_head * di_bh)7047*4882a593Smuzhiyun int ocfs2_convert_inline_data_to_extents(struct inode *inode,
7048*4882a593Smuzhiyun 					 struct buffer_head *di_bh)
7049*4882a593Smuzhiyun {
7050*4882a593Smuzhiyun 	int ret, has_data, num_pages = 0;
7051*4882a593Smuzhiyun 	int need_free = 0;
7052*4882a593Smuzhiyun 	u32 bit_off, num;
7053*4882a593Smuzhiyun 	handle_t *handle;
7054*4882a593Smuzhiyun 	u64 block;
7055*4882a593Smuzhiyun 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
7056*4882a593Smuzhiyun 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7057*4882a593Smuzhiyun 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7058*4882a593Smuzhiyun 	struct ocfs2_alloc_context *data_ac = NULL;
7059*4882a593Smuzhiyun 	struct page *page = NULL;
7060*4882a593Smuzhiyun 	struct ocfs2_extent_tree et;
7061*4882a593Smuzhiyun 	int did_quota = 0;
7062*4882a593Smuzhiyun 
7063*4882a593Smuzhiyun 	has_data = i_size_read(inode) ? 1 : 0;
7064*4882a593Smuzhiyun 
7065*4882a593Smuzhiyun 	if (has_data) {
7066*4882a593Smuzhiyun 		ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
7067*4882a593Smuzhiyun 		if (ret) {
7068*4882a593Smuzhiyun 			mlog_errno(ret);
7069*4882a593Smuzhiyun 			goto out;
7070*4882a593Smuzhiyun 		}
7071*4882a593Smuzhiyun 	}
7072*4882a593Smuzhiyun 
7073*4882a593Smuzhiyun 	handle = ocfs2_start_trans(osb,
7074*4882a593Smuzhiyun 				   ocfs2_inline_to_extents_credits(osb->sb));
7075*4882a593Smuzhiyun 	if (IS_ERR(handle)) {
7076*4882a593Smuzhiyun 		ret = PTR_ERR(handle);
7077*4882a593Smuzhiyun 		mlog_errno(ret);
7078*4882a593Smuzhiyun 		goto out;
7079*4882a593Smuzhiyun 	}
7080*4882a593Smuzhiyun 
7081*4882a593Smuzhiyun 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
7082*4882a593Smuzhiyun 				      OCFS2_JOURNAL_ACCESS_WRITE);
7083*4882a593Smuzhiyun 	if (ret) {
7084*4882a593Smuzhiyun 		mlog_errno(ret);
7085*4882a593Smuzhiyun 		goto out_commit;
7086*4882a593Smuzhiyun 	}
7087*4882a593Smuzhiyun 
7088*4882a593Smuzhiyun 	if (has_data) {
7089*4882a593Smuzhiyun 		unsigned int page_end = min_t(unsigned, PAGE_SIZE,
7090*4882a593Smuzhiyun 							osb->s_clustersize);
7091*4882a593Smuzhiyun 		u64 phys;
7092*4882a593Smuzhiyun 
7093*4882a593Smuzhiyun 		ret = dquot_alloc_space_nodirty(inode,
7094*4882a593Smuzhiyun 				       ocfs2_clusters_to_bytes(osb->sb, 1));
7095*4882a593Smuzhiyun 		if (ret)
7096*4882a593Smuzhiyun 			goto out_commit;
7097*4882a593Smuzhiyun 		did_quota = 1;
7098*4882a593Smuzhiyun 
7099*4882a593Smuzhiyun 		data_ac->ac_resv = &oi->ip_la_data_resv;
7100*4882a593Smuzhiyun 
7101*4882a593Smuzhiyun 		ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off,
7102*4882a593Smuzhiyun 					   &num);
7103*4882a593Smuzhiyun 		if (ret) {
7104*4882a593Smuzhiyun 			mlog_errno(ret);
7105*4882a593Smuzhiyun 			goto out_commit;
7106*4882a593Smuzhiyun 		}
7107*4882a593Smuzhiyun 
7108*4882a593Smuzhiyun 		/*
7109*4882a593Smuzhiyun 		 * Save two copies, one for insert, and one that can
7110*4882a593Smuzhiyun 		 * be changed by ocfs2_map_and_dirty_page() below.
7111*4882a593Smuzhiyun 		 */
7112*4882a593Smuzhiyun 		block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
7113*4882a593Smuzhiyun 
7114*4882a593Smuzhiyun 		ret = ocfs2_grab_eof_pages(inode, 0, page_end, &page,
7115*4882a593Smuzhiyun 					   &num_pages);
7116*4882a593Smuzhiyun 		if (ret) {
7117*4882a593Smuzhiyun 			mlog_errno(ret);
7118*4882a593Smuzhiyun 			need_free = 1;
7119*4882a593Smuzhiyun 			goto out_commit;
7120*4882a593Smuzhiyun 		}
7121*4882a593Smuzhiyun 
7122*4882a593Smuzhiyun 		/*
7123*4882a593Smuzhiyun 		 * This should populate the 1st page for us and mark
7124*4882a593Smuzhiyun 		 * it up to date.
7125*4882a593Smuzhiyun 		 */
7126*4882a593Smuzhiyun 		ret = ocfs2_read_inline_data(inode, page, di_bh);
7127*4882a593Smuzhiyun 		if (ret) {
7128*4882a593Smuzhiyun 			mlog_errno(ret);
7129*4882a593Smuzhiyun 			need_free = 1;
7130*4882a593Smuzhiyun 			goto out_unlock;
7131*4882a593Smuzhiyun 		}
7132*4882a593Smuzhiyun 
7133*4882a593Smuzhiyun 		ocfs2_map_and_dirty_page(inode, handle, 0, page_end, page, 0,
7134*4882a593Smuzhiyun 					 &phys);
7135*4882a593Smuzhiyun 	}
7136*4882a593Smuzhiyun 
7137*4882a593Smuzhiyun 	spin_lock(&oi->ip_lock);
7138*4882a593Smuzhiyun 	oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
7139*4882a593Smuzhiyun 	di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7140*4882a593Smuzhiyun 	spin_unlock(&oi->ip_lock);
7141*4882a593Smuzhiyun 
7142*4882a593Smuzhiyun 	ocfs2_update_inode_fsync_trans(handle, inode, 1);
7143*4882a593Smuzhiyun 	ocfs2_dinode_new_extent_list(inode, di);
7144*4882a593Smuzhiyun 
7145*4882a593Smuzhiyun 	ocfs2_journal_dirty(handle, di_bh);
7146*4882a593Smuzhiyun 
7147*4882a593Smuzhiyun 	if (has_data) {
7148*4882a593Smuzhiyun 		/*
7149*4882a593Smuzhiyun 		 * An error at this point should be extremely rare. If
7150*4882a593Smuzhiyun 		 * this proves to be false, we could always re-build
7151*4882a593Smuzhiyun 		 * the in-inode data from our pages.
7152*4882a593Smuzhiyun 		 */
7153*4882a593Smuzhiyun 		ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
7154*4882a593Smuzhiyun 		ret = ocfs2_insert_extent(handle, &et, 0, block, 1, 0, NULL);
7155*4882a593Smuzhiyun 		if (ret) {
7156*4882a593Smuzhiyun 			mlog_errno(ret);
7157*4882a593Smuzhiyun 			need_free = 1;
7158*4882a593Smuzhiyun 			goto out_unlock;
7159*4882a593Smuzhiyun 		}
7160*4882a593Smuzhiyun 
7161*4882a593Smuzhiyun 		inode->i_blocks = ocfs2_inode_sector_count(inode);
7162*4882a593Smuzhiyun 	}
7163*4882a593Smuzhiyun 
7164*4882a593Smuzhiyun out_unlock:
7165*4882a593Smuzhiyun 	if (page)
7166*4882a593Smuzhiyun 		ocfs2_unlock_and_free_pages(&page, num_pages);
7167*4882a593Smuzhiyun 
7168*4882a593Smuzhiyun out_commit:
7169*4882a593Smuzhiyun 	if (ret < 0 && did_quota)
7170*4882a593Smuzhiyun 		dquot_free_space_nodirty(inode,
7171*4882a593Smuzhiyun 					  ocfs2_clusters_to_bytes(osb->sb, 1));
7172*4882a593Smuzhiyun 
7173*4882a593Smuzhiyun 	if (need_free) {
7174*4882a593Smuzhiyun 		if (data_ac->ac_which == OCFS2_AC_USE_LOCAL)
7175*4882a593Smuzhiyun 			ocfs2_free_local_alloc_bits(osb, handle, data_ac,
7176*4882a593Smuzhiyun 					bit_off, num);
7177*4882a593Smuzhiyun 		else
7178*4882a593Smuzhiyun 			ocfs2_free_clusters(handle,
7179*4882a593Smuzhiyun 					data_ac->ac_inode,
7180*4882a593Smuzhiyun 					data_ac->ac_bh,
7181*4882a593Smuzhiyun 					ocfs2_clusters_to_blocks(osb->sb, bit_off),
7182*4882a593Smuzhiyun 					num);
7183*4882a593Smuzhiyun 	}
7184*4882a593Smuzhiyun 
7185*4882a593Smuzhiyun 	ocfs2_commit_trans(osb, handle);
7186*4882a593Smuzhiyun 
7187*4882a593Smuzhiyun out:
7188*4882a593Smuzhiyun 	if (data_ac)
7189*4882a593Smuzhiyun 		ocfs2_free_alloc_context(data_ac);
7190*4882a593Smuzhiyun 	return ret;
7191*4882a593Smuzhiyun }
7192*4882a593Smuzhiyun 
7193*4882a593Smuzhiyun /*
7194*4882a593Smuzhiyun  * It is expected, that by the time you call this function,
7195*4882a593Smuzhiyun  * inode->i_size and fe->i_size have been adjusted.
7196*4882a593Smuzhiyun  *
7197*4882a593Smuzhiyun  * WARNING: This will kfree the truncate context
7198*4882a593Smuzhiyun  */
ocfs2_commit_truncate(struct ocfs2_super * osb,struct inode * inode,struct buffer_head * di_bh)7199*4882a593Smuzhiyun int ocfs2_commit_truncate(struct ocfs2_super *osb,
7200*4882a593Smuzhiyun 			  struct inode *inode,
7201*4882a593Smuzhiyun 			  struct buffer_head *di_bh)
7202*4882a593Smuzhiyun {
7203*4882a593Smuzhiyun 	int status = 0, i, flags = 0;
7204*4882a593Smuzhiyun 	u32 new_highest_cpos, range, trunc_cpos, trunc_len, phys_cpos, coff;
7205*4882a593Smuzhiyun 	u64 blkno = 0;
7206*4882a593Smuzhiyun 	struct ocfs2_extent_list *el;
7207*4882a593Smuzhiyun 	struct ocfs2_extent_rec *rec;
7208*4882a593Smuzhiyun 	struct ocfs2_path *path = NULL;
7209*4882a593Smuzhiyun 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7210*4882a593Smuzhiyun 	struct ocfs2_extent_list *root_el = &(di->id2.i_list);
7211*4882a593Smuzhiyun 	u64 refcount_loc = le64_to_cpu(di->i_refcount_loc);
7212*4882a593Smuzhiyun 	struct ocfs2_extent_tree et;
7213*4882a593Smuzhiyun 	struct ocfs2_cached_dealloc_ctxt dealloc;
7214*4882a593Smuzhiyun 	struct ocfs2_refcount_tree *ref_tree = NULL;
7215*4882a593Smuzhiyun 
7216*4882a593Smuzhiyun 	ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
7217*4882a593Smuzhiyun 	ocfs2_init_dealloc_ctxt(&dealloc);
7218*4882a593Smuzhiyun 
7219*4882a593Smuzhiyun 	new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
7220*4882a593Smuzhiyun 						     i_size_read(inode));
7221*4882a593Smuzhiyun 
7222*4882a593Smuzhiyun 	path = ocfs2_new_path(di_bh, &di->id2.i_list,
7223*4882a593Smuzhiyun 			      ocfs2_journal_access_di);
7224*4882a593Smuzhiyun 	if (!path) {
7225*4882a593Smuzhiyun 		status = -ENOMEM;
7226*4882a593Smuzhiyun 		mlog_errno(status);
7227*4882a593Smuzhiyun 		goto bail;
7228*4882a593Smuzhiyun 	}
7229*4882a593Smuzhiyun 
7230*4882a593Smuzhiyun 	ocfs2_extent_map_trunc(inode, new_highest_cpos);
7231*4882a593Smuzhiyun 
7232*4882a593Smuzhiyun start:
7233*4882a593Smuzhiyun 	/*
7234*4882a593Smuzhiyun 	 * Check that we still have allocation to delete.
7235*4882a593Smuzhiyun 	 */
7236*4882a593Smuzhiyun 	if (OCFS2_I(inode)->ip_clusters == 0) {
7237*4882a593Smuzhiyun 		status = 0;
7238*4882a593Smuzhiyun 		goto bail;
7239*4882a593Smuzhiyun 	}
7240*4882a593Smuzhiyun 
7241*4882a593Smuzhiyun 	/*
7242*4882a593Smuzhiyun 	 * Truncate always works against the rightmost tree branch.
7243*4882a593Smuzhiyun 	 */
7244*4882a593Smuzhiyun 	status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX);
7245*4882a593Smuzhiyun 	if (status) {
7246*4882a593Smuzhiyun 		mlog_errno(status);
7247*4882a593Smuzhiyun 		goto bail;
7248*4882a593Smuzhiyun 	}
7249*4882a593Smuzhiyun 
7250*4882a593Smuzhiyun 	trace_ocfs2_commit_truncate(
7251*4882a593Smuzhiyun 		(unsigned long long)OCFS2_I(inode)->ip_blkno,
7252*4882a593Smuzhiyun 		new_highest_cpos,
7253*4882a593Smuzhiyun 		OCFS2_I(inode)->ip_clusters,
7254*4882a593Smuzhiyun 		path->p_tree_depth);
7255*4882a593Smuzhiyun 
7256*4882a593Smuzhiyun 	/*
7257*4882a593Smuzhiyun 	 * By now, el will point to the extent list on the bottom most
7258*4882a593Smuzhiyun 	 * portion of this tree. Only the tail record is considered in
7259*4882a593Smuzhiyun 	 * each pass.
7260*4882a593Smuzhiyun 	 *
7261*4882a593Smuzhiyun 	 * We handle the following cases, in order:
7262*4882a593Smuzhiyun 	 * - empty extent: delete the remaining branch
7263*4882a593Smuzhiyun 	 * - remove the entire record
7264*4882a593Smuzhiyun 	 * - remove a partial record
7265*4882a593Smuzhiyun 	 * - no record needs to be removed (truncate has completed)
7266*4882a593Smuzhiyun 	 */
7267*4882a593Smuzhiyun 	el = path_leaf_el(path);
7268*4882a593Smuzhiyun 	if (le16_to_cpu(el->l_next_free_rec) == 0) {
7269*4882a593Smuzhiyun 		ocfs2_error(inode->i_sb,
7270*4882a593Smuzhiyun 			    "Inode %llu has empty extent block at %llu\n",
7271*4882a593Smuzhiyun 			    (unsigned long long)OCFS2_I(inode)->ip_blkno,
7272*4882a593Smuzhiyun 			    (unsigned long long)path_leaf_bh(path)->b_blocknr);
7273*4882a593Smuzhiyun 		status = -EROFS;
7274*4882a593Smuzhiyun 		goto bail;
7275*4882a593Smuzhiyun 	}
7276*4882a593Smuzhiyun 
7277*4882a593Smuzhiyun 	i = le16_to_cpu(el->l_next_free_rec) - 1;
7278*4882a593Smuzhiyun 	rec = &el->l_recs[i];
7279*4882a593Smuzhiyun 	flags = rec->e_flags;
7280*4882a593Smuzhiyun 	range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
7281*4882a593Smuzhiyun 
7282*4882a593Smuzhiyun 	if (i == 0 && ocfs2_is_empty_extent(rec)) {
7283*4882a593Smuzhiyun 		/*
7284*4882a593Smuzhiyun 		 * Lower levels depend on this never happening, but it's best
7285*4882a593Smuzhiyun 		 * to check it up here before changing the tree.
7286*4882a593Smuzhiyun 		*/
7287*4882a593Smuzhiyun 		if (root_el->l_tree_depth && rec->e_int_clusters == 0) {
7288*4882a593Smuzhiyun 			mlog(ML_ERROR, "Inode %lu has an empty "
7289*4882a593Smuzhiyun 				    "extent record, depth %u\n", inode->i_ino,
7290*4882a593Smuzhiyun 				    le16_to_cpu(root_el->l_tree_depth));
7291*4882a593Smuzhiyun 			status = ocfs2_remove_rightmost_empty_extent(osb,
7292*4882a593Smuzhiyun 					&et, path, &dealloc);
7293*4882a593Smuzhiyun 			if (status) {
7294*4882a593Smuzhiyun 				mlog_errno(status);
7295*4882a593Smuzhiyun 				goto bail;
7296*4882a593Smuzhiyun 			}
7297*4882a593Smuzhiyun 
7298*4882a593Smuzhiyun 			ocfs2_reinit_path(path, 1);
7299*4882a593Smuzhiyun 			goto start;
7300*4882a593Smuzhiyun 		} else {
7301*4882a593Smuzhiyun 			trunc_cpos = le32_to_cpu(rec->e_cpos);
7302*4882a593Smuzhiyun 			trunc_len = 0;
7303*4882a593Smuzhiyun 			blkno = 0;
7304*4882a593Smuzhiyun 		}
7305*4882a593Smuzhiyun 	} else if (le32_to_cpu(rec->e_cpos) >= new_highest_cpos) {
7306*4882a593Smuzhiyun 		/*
7307*4882a593Smuzhiyun 		 * Truncate entire record.
7308*4882a593Smuzhiyun 		 */
7309*4882a593Smuzhiyun 		trunc_cpos = le32_to_cpu(rec->e_cpos);
7310*4882a593Smuzhiyun 		trunc_len = ocfs2_rec_clusters(el, rec);
7311*4882a593Smuzhiyun 		blkno = le64_to_cpu(rec->e_blkno);
7312*4882a593Smuzhiyun 	} else if (range > new_highest_cpos) {
7313*4882a593Smuzhiyun 		/*
7314*4882a593Smuzhiyun 		 * Partial truncate. it also should be
7315*4882a593Smuzhiyun 		 * the last truncate we're doing.
7316*4882a593Smuzhiyun 		 */
7317*4882a593Smuzhiyun 		trunc_cpos = new_highest_cpos;
7318*4882a593Smuzhiyun 		trunc_len = range - new_highest_cpos;
7319*4882a593Smuzhiyun 		coff = new_highest_cpos - le32_to_cpu(rec->e_cpos);
7320*4882a593Smuzhiyun 		blkno = le64_to_cpu(rec->e_blkno) +
7321*4882a593Smuzhiyun 				ocfs2_clusters_to_blocks(inode->i_sb, coff);
7322*4882a593Smuzhiyun 	} else {
7323*4882a593Smuzhiyun 		/*
7324*4882a593Smuzhiyun 		 * Truncate completed, leave happily.
7325*4882a593Smuzhiyun 		 */
7326*4882a593Smuzhiyun 		status = 0;
7327*4882a593Smuzhiyun 		goto bail;
7328*4882a593Smuzhiyun 	}
7329*4882a593Smuzhiyun 
7330*4882a593Smuzhiyun 	phys_cpos = ocfs2_blocks_to_clusters(inode->i_sb, blkno);
7331*4882a593Smuzhiyun 
7332*4882a593Smuzhiyun 	if ((flags & OCFS2_EXT_REFCOUNTED) && trunc_len && !ref_tree) {
7333*4882a593Smuzhiyun 		status = ocfs2_lock_refcount_tree(osb, refcount_loc, 1,
7334*4882a593Smuzhiyun 				&ref_tree, NULL);
7335*4882a593Smuzhiyun 		if (status) {
7336*4882a593Smuzhiyun 			mlog_errno(status);
7337*4882a593Smuzhiyun 			goto bail;
7338*4882a593Smuzhiyun 		}
7339*4882a593Smuzhiyun 	}
7340*4882a593Smuzhiyun 
7341*4882a593Smuzhiyun 	status = ocfs2_remove_btree_range(inode, &et, trunc_cpos,
7342*4882a593Smuzhiyun 					  phys_cpos, trunc_len, flags, &dealloc,
7343*4882a593Smuzhiyun 					  refcount_loc, true);
7344*4882a593Smuzhiyun 	if (status < 0) {
7345*4882a593Smuzhiyun 		mlog_errno(status);
7346*4882a593Smuzhiyun 		goto bail;
7347*4882a593Smuzhiyun 	}
7348*4882a593Smuzhiyun 
7349*4882a593Smuzhiyun 	ocfs2_reinit_path(path, 1);
7350*4882a593Smuzhiyun 
7351*4882a593Smuzhiyun 	/*
7352*4882a593Smuzhiyun 	 * The check above will catch the case where we've truncated
7353*4882a593Smuzhiyun 	 * away all allocation.
7354*4882a593Smuzhiyun 	 */
7355*4882a593Smuzhiyun 	goto start;
7356*4882a593Smuzhiyun 
7357*4882a593Smuzhiyun bail:
7358*4882a593Smuzhiyun 	if (ref_tree)
7359*4882a593Smuzhiyun 		ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
7360*4882a593Smuzhiyun 
7361*4882a593Smuzhiyun 	ocfs2_schedule_truncate_log_flush(osb, 1);
7362*4882a593Smuzhiyun 
7363*4882a593Smuzhiyun 	ocfs2_run_deallocs(osb, &dealloc);
7364*4882a593Smuzhiyun 
7365*4882a593Smuzhiyun 	ocfs2_free_path(path);
7366*4882a593Smuzhiyun 
7367*4882a593Smuzhiyun 	return status;
7368*4882a593Smuzhiyun }
7369*4882a593Smuzhiyun 
7370*4882a593Smuzhiyun /*
7371*4882a593Smuzhiyun  * 'start' is inclusive, 'end' is not.
7372*4882a593Smuzhiyun  */
ocfs2_truncate_inline(struct inode * inode,struct buffer_head * di_bh,unsigned int start,unsigned int end,int trunc)7373*4882a593Smuzhiyun int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7374*4882a593Smuzhiyun 			  unsigned int start, unsigned int end, int trunc)
7375*4882a593Smuzhiyun {
7376*4882a593Smuzhiyun 	int ret;
7377*4882a593Smuzhiyun 	unsigned int numbytes;
7378*4882a593Smuzhiyun 	handle_t *handle;
7379*4882a593Smuzhiyun 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7380*4882a593Smuzhiyun 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7381*4882a593Smuzhiyun 	struct ocfs2_inline_data *idata = &di->id2.i_data;
7382*4882a593Smuzhiyun 
7383*4882a593Smuzhiyun 	/* No need to punch hole beyond i_size. */
7384*4882a593Smuzhiyun 	if (start >= i_size_read(inode))
7385*4882a593Smuzhiyun 		return 0;
7386*4882a593Smuzhiyun 
7387*4882a593Smuzhiyun 	if (end > i_size_read(inode))
7388*4882a593Smuzhiyun 		end = i_size_read(inode);
7389*4882a593Smuzhiyun 
7390*4882a593Smuzhiyun 	BUG_ON(start > end);
7391*4882a593Smuzhiyun 
7392*4882a593Smuzhiyun 	if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7393*4882a593Smuzhiyun 	    !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7394*4882a593Smuzhiyun 	    !ocfs2_supports_inline_data(osb)) {
7395*4882a593Smuzhiyun 		ocfs2_error(inode->i_sb,
7396*4882a593Smuzhiyun 			    "Inline data flags for inode %llu don't agree! Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7397*4882a593Smuzhiyun 			    (unsigned long long)OCFS2_I(inode)->ip_blkno,
7398*4882a593Smuzhiyun 			    le16_to_cpu(di->i_dyn_features),
7399*4882a593Smuzhiyun 			    OCFS2_I(inode)->ip_dyn_features,
7400*4882a593Smuzhiyun 			    osb->s_feature_incompat);
7401*4882a593Smuzhiyun 		ret = -EROFS;
7402*4882a593Smuzhiyun 		goto out;
7403*4882a593Smuzhiyun 	}
7404*4882a593Smuzhiyun 
7405*4882a593Smuzhiyun 	handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7406*4882a593Smuzhiyun 	if (IS_ERR(handle)) {
7407*4882a593Smuzhiyun 		ret = PTR_ERR(handle);
7408*4882a593Smuzhiyun 		mlog_errno(ret);
7409*4882a593Smuzhiyun 		goto out;
7410*4882a593Smuzhiyun 	}
7411*4882a593Smuzhiyun 
7412*4882a593Smuzhiyun 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
7413*4882a593Smuzhiyun 				      OCFS2_JOURNAL_ACCESS_WRITE);
7414*4882a593Smuzhiyun 	if (ret) {
7415*4882a593Smuzhiyun 		mlog_errno(ret);
7416*4882a593Smuzhiyun 		goto out_commit;
7417*4882a593Smuzhiyun 	}
7418*4882a593Smuzhiyun 
7419*4882a593Smuzhiyun 	numbytes = end - start;
7420*4882a593Smuzhiyun 	memset(idata->id_data + start, 0, numbytes);
7421*4882a593Smuzhiyun 
7422*4882a593Smuzhiyun 	/*
7423*4882a593Smuzhiyun 	 * No need to worry about the data page here - it's been
7424*4882a593Smuzhiyun 	 * truncated already and inline data doesn't need it for
7425*4882a593Smuzhiyun 	 * pushing zero's to disk, so we'll let readpage pick it up
7426*4882a593Smuzhiyun 	 * later.
7427*4882a593Smuzhiyun 	 */
7428*4882a593Smuzhiyun 	if (trunc) {
7429*4882a593Smuzhiyun 		i_size_write(inode, start);
7430*4882a593Smuzhiyun 		di->i_size = cpu_to_le64(start);
7431*4882a593Smuzhiyun 	}
7432*4882a593Smuzhiyun 
7433*4882a593Smuzhiyun 	inode->i_blocks = ocfs2_inode_sector_count(inode);
7434*4882a593Smuzhiyun 	inode->i_ctime = inode->i_mtime = current_time(inode);
7435*4882a593Smuzhiyun 
7436*4882a593Smuzhiyun 	di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7437*4882a593Smuzhiyun 	di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7438*4882a593Smuzhiyun 
7439*4882a593Smuzhiyun 	ocfs2_update_inode_fsync_trans(handle, inode, 1);
7440*4882a593Smuzhiyun 	ocfs2_journal_dirty(handle, di_bh);
7441*4882a593Smuzhiyun 
7442*4882a593Smuzhiyun out_commit:
7443*4882a593Smuzhiyun 	ocfs2_commit_trans(osb, handle);
7444*4882a593Smuzhiyun 
7445*4882a593Smuzhiyun out:
7446*4882a593Smuzhiyun 	return ret;
7447*4882a593Smuzhiyun }
7448*4882a593Smuzhiyun 
ocfs2_trim_extent(struct super_block * sb,struct ocfs2_group_desc * gd,u64 group,u32 start,u32 count)7449*4882a593Smuzhiyun static int ocfs2_trim_extent(struct super_block *sb,
7450*4882a593Smuzhiyun 			     struct ocfs2_group_desc *gd,
7451*4882a593Smuzhiyun 			     u64 group, u32 start, u32 count)
7452*4882a593Smuzhiyun {
7453*4882a593Smuzhiyun 	u64 discard, bcount;
7454*4882a593Smuzhiyun 	struct ocfs2_super *osb = OCFS2_SB(sb);
7455*4882a593Smuzhiyun 
7456*4882a593Smuzhiyun 	bcount = ocfs2_clusters_to_blocks(sb, count);
7457*4882a593Smuzhiyun 	discard = ocfs2_clusters_to_blocks(sb, start);
7458*4882a593Smuzhiyun 
7459*4882a593Smuzhiyun 	/*
7460*4882a593Smuzhiyun 	 * For the first cluster group, the gd->bg_blkno is not at the start
7461*4882a593Smuzhiyun 	 * of the group, but at an offset from the start. If we add it while
7462*4882a593Smuzhiyun 	 * calculating discard for first group, we will wrongly start fstrim a
7463*4882a593Smuzhiyun 	 * few blocks after the desried start block and the range can cross
7464*4882a593Smuzhiyun 	 * over into the next cluster group. So, add it only if this is not
7465*4882a593Smuzhiyun 	 * the first cluster group.
7466*4882a593Smuzhiyun 	 */
7467*4882a593Smuzhiyun 	if (group != osb->first_cluster_group_blkno)
7468*4882a593Smuzhiyun 		discard += le64_to_cpu(gd->bg_blkno);
7469*4882a593Smuzhiyun 
7470*4882a593Smuzhiyun 	trace_ocfs2_trim_extent(sb, (unsigned long long)discard, bcount);
7471*4882a593Smuzhiyun 
7472*4882a593Smuzhiyun 	return sb_issue_discard(sb, discard, bcount, GFP_NOFS, 0);
7473*4882a593Smuzhiyun }
7474*4882a593Smuzhiyun 
ocfs2_trim_group(struct super_block * sb,struct ocfs2_group_desc * gd,u64 group,u32 start,u32 max,u32 minbits)7475*4882a593Smuzhiyun static int ocfs2_trim_group(struct super_block *sb,
7476*4882a593Smuzhiyun 			    struct ocfs2_group_desc *gd, u64 group,
7477*4882a593Smuzhiyun 			    u32 start, u32 max, u32 minbits)
7478*4882a593Smuzhiyun {
7479*4882a593Smuzhiyun 	int ret = 0, count = 0, next;
7480*4882a593Smuzhiyun 	void *bitmap = gd->bg_bitmap;
7481*4882a593Smuzhiyun 
7482*4882a593Smuzhiyun 	if (le16_to_cpu(gd->bg_free_bits_count) < minbits)
7483*4882a593Smuzhiyun 		return 0;
7484*4882a593Smuzhiyun 
7485*4882a593Smuzhiyun 	trace_ocfs2_trim_group((unsigned long long)le64_to_cpu(gd->bg_blkno),
7486*4882a593Smuzhiyun 			       start, max, minbits);
7487*4882a593Smuzhiyun 
7488*4882a593Smuzhiyun 	while (start < max) {
7489*4882a593Smuzhiyun 		start = ocfs2_find_next_zero_bit(bitmap, max, start);
7490*4882a593Smuzhiyun 		if (start >= max)
7491*4882a593Smuzhiyun 			break;
7492*4882a593Smuzhiyun 		next = ocfs2_find_next_bit(bitmap, max, start);
7493*4882a593Smuzhiyun 
7494*4882a593Smuzhiyun 		if ((next - start) >= minbits) {
7495*4882a593Smuzhiyun 			ret = ocfs2_trim_extent(sb, gd, group,
7496*4882a593Smuzhiyun 						start, next - start);
7497*4882a593Smuzhiyun 			if (ret < 0) {
7498*4882a593Smuzhiyun 				mlog_errno(ret);
7499*4882a593Smuzhiyun 				break;
7500*4882a593Smuzhiyun 			}
7501*4882a593Smuzhiyun 			count += next - start;
7502*4882a593Smuzhiyun 		}
7503*4882a593Smuzhiyun 		start = next + 1;
7504*4882a593Smuzhiyun 
7505*4882a593Smuzhiyun 		if (fatal_signal_pending(current)) {
7506*4882a593Smuzhiyun 			count = -ERESTARTSYS;
7507*4882a593Smuzhiyun 			break;
7508*4882a593Smuzhiyun 		}
7509*4882a593Smuzhiyun 
7510*4882a593Smuzhiyun 		if ((le16_to_cpu(gd->bg_free_bits_count) - count) < minbits)
7511*4882a593Smuzhiyun 			break;
7512*4882a593Smuzhiyun 	}
7513*4882a593Smuzhiyun 
7514*4882a593Smuzhiyun 	if (ret < 0)
7515*4882a593Smuzhiyun 		count = ret;
7516*4882a593Smuzhiyun 
7517*4882a593Smuzhiyun 	return count;
7518*4882a593Smuzhiyun }
7519*4882a593Smuzhiyun 
7520*4882a593Smuzhiyun static
ocfs2_trim_mainbm(struct super_block * sb,struct fstrim_range * range)7521*4882a593Smuzhiyun int ocfs2_trim_mainbm(struct super_block *sb, struct fstrim_range *range)
7522*4882a593Smuzhiyun {
7523*4882a593Smuzhiyun 	struct ocfs2_super *osb = OCFS2_SB(sb);
7524*4882a593Smuzhiyun 	u64 start, len, trimmed = 0, first_group, last_group = 0, group = 0;
7525*4882a593Smuzhiyun 	int ret, cnt;
7526*4882a593Smuzhiyun 	u32 first_bit, last_bit, minlen;
7527*4882a593Smuzhiyun 	struct buffer_head *main_bm_bh = NULL;
7528*4882a593Smuzhiyun 	struct inode *main_bm_inode = NULL;
7529*4882a593Smuzhiyun 	struct buffer_head *gd_bh = NULL;
7530*4882a593Smuzhiyun 	struct ocfs2_dinode *main_bm;
7531*4882a593Smuzhiyun 	struct ocfs2_group_desc *gd = NULL;
7532*4882a593Smuzhiyun 
7533*4882a593Smuzhiyun 	start = range->start >> osb->s_clustersize_bits;
7534*4882a593Smuzhiyun 	len = range->len >> osb->s_clustersize_bits;
7535*4882a593Smuzhiyun 	minlen = range->minlen >> osb->s_clustersize_bits;
7536*4882a593Smuzhiyun 
7537*4882a593Smuzhiyun 	if (minlen >= osb->bitmap_cpg || range->len < sb->s_blocksize)
7538*4882a593Smuzhiyun 		return -EINVAL;
7539*4882a593Smuzhiyun 
7540*4882a593Smuzhiyun 	trace_ocfs2_trim_mainbm(start, len, minlen);
7541*4882a593Smuzhiyun 
7542*4882a593Smuzhiyun next_group:
7543*4882a593Smuzhiyun 	main_bm_inode = ocfs2_get_system_file_inode(osb,
7544*4882a593Smuzhiyun 						    GLOBAL_BITMAP_SYSTEM_INODE,
7545*4882a593Smuzhiyun 						    OCFS2_INVALID_SLOT);
7546*4882a593Smuzhiyun 	if (!main_bm_inode) {
7547*4882a593Smuzhiyun 		ret = -EIO;
7548*4882a593Smuzhiyun 		mlog_errno(ret);
7549*4882a593Smuzhiyun 		goto out;
7550*4882a593Smuzhiyun 	}
7551*4882a593Smuzhiyun 
7552*4882a593Smuzhiyun 	inode_lock(main_bm_inode);
7553*4882a593Smuzhiyun 
7554*4882a593Smuzhiyun 	ret = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 0);
7555*4882a593Smuzhiyun 	if (ret < 0) {
7556*4882a593Smuzhiyun 		mlog_errno(ret);
7557*4882a593Smuzhiyun 		goto out_mutex;
7558*4882a593Smuzhiyun 	}
7559*4882a593Smuzhiyun 	main_bm = (struct ocfs2_dinode *)main_bm_bh->b_data;
7560*4882a593Smuzhiyun 
7561*4882a593Smuzhiyun 	/*
7562*4882a593Smuzhiyun 	 * Do some check before trim the first group.
7563*4882a593Smuzhiyun 	 */
7564*4882a593Smuzhiyun 	if (!group) {
7565*4882a593Smuzhiyun 		if (start >= le32_to_cpu(main_bm->i_clusters)) {
7566*4882a593Smuzhiyun 			ret = -EINVAL;
7567*4882a593Smuzhiyun 			goto out_unlock;
7568*4882a593Smuzhiyun 		}
7569*4882a593Smuzhiyun 
7570*4882a593Smuzhiyun 		if (start + len > le32_to_cpu(main_bm->i_clusters))
7571*4882a593Smuzhiyun 			len = le32_to_cpu(main_bm->i_clusters) - start;
7572*4882a593Smuzhiyun 
7573*4882a593Smuzhiyun 		/*
7574*4882a593Smuzhiyun 		 * Determine first and last group to examine based on
7575*4882a593Smuzhiyun 		 * start and len
7576*4882a593Smuzhiyun 		 */
7577*4882a593Smuzhiyun 		first_group = ocfs2_which_cluster_group(main_bm_inode, start);
7578*4882a593Smuzhiyun 		if (first_group == osb->first_cluster_group_blkno)
7579*4882a593Smuzhiyun 			first_bit = start;
7580*4882a593Smuzhiyun 		else
7581*4882a593Smuzhiyun 			first_bit = start - ocfs2_blocks_to_clusters(sb,
7582*4882a593Smuzhiyun 								first_group);
7583*4882a593Smuzhiyun 		last_group = ocfs2_which_cluster_group(main_bm_inode,
7584*4882a593Smuzhiyun 						       start + len - 1);
7585*4882a593Smuzhiyun 		group = first_group;
7586*4882a593Smuzhiyun 	}
7587*4882a593Smuzhiyun 
7588*4882a593Smuzhiyun 	do {
7589*4882a593Smuzhiyun 		if (first_bit + len >= osb->bitmap_cpg)
7590*4882a593Smuzhiyun 			last_bit = osb->bitmap_cpg;
7591*4882a593Smuzhiyun 		else
7592*4882a593Smuzhiyun 			last_bit = first_bit + len;
7593*4882a593Smuzhiyun 
7594*4882a593Smuzhiyun 		ret = ocfs2_read_group_descriptor(main_bm_inode,
7595*4882a593Smuzhiyun 						  main_bm, group,
7596*4882a593Smuzhiyun 						  &gd_bh);
7597*4882a593Smuzhiyun 		if (ret < 0) {
7598*4882a593Smuzhiyun 			mlog_errno(ret);
7599*4882a593Smuzhiyun 			break;
7600*4882a593Smuzhiyun 		}
7601*4882a593Smuzhiyun 
7602*4882a593Smuzhiyun 		gd = (struct ocfs2_group_desc *)gd_bh->b_data;
7603*4882a593Smuzhiyun 		cnt = ocfs2_trim_group(sb, gd, group,
7604*4882a593Smuzhiyun 				       first_bit, last_bit, minlen);
7605*4882a593Smuzhiyun 		brelse(gd_bh);
7606*4882a593Smuzhiyun 		gd_bh = NULL;
7607*4882a593Smuzhiyun 		if (cnt < 0) {
7608*4882a593Smuzhiyun 			ret = cnt;
7609*4882a593Smuzhiyun 			mlog_errno(ret);
7610*4882a593Smuzhiyun 			break;
7611*4882a593Smuzhiyun 		}
7612*4882a593Smuzhiyun 
7613*4882a593Smuzhiyun 		trimmed += cnt;
7614*4882a593Smuzhiyun 		len -= osb->bitmap_cpg - first_bit;
7615*4882a593Smuzhiyun 		first_bit = 0;
7616*4882a593Smuzhiyun 		if (group == osb->first_cluster_group_blkno)
7617*4882a593Smuzhiyun 			group = ocfs2_clusters_to_blocks(sb, osb->bitmap_cpg);
7618*4882a593Smuzhiyun 		else
7619*4882a593Smuzhiyun 			group += ocfs2_clusters_to_blocks(sb, osb->bitmap_cpg);
7620*4882a593Smuzhiyun 	} while (0);
7621*4882a593Smuzhiyun 
7622*4882a593Smuzhiyun out_unlock:
7623*4882a593Smuzhiyun 	ocfs2_inode_unlock(main_bm_inode, 0);
7624*4882a593Smuzhiyun 	brelse(main_bm_bh);
7625*4882a593Smuzhiyun 	main_bm_bh = NULL;
7626*4882a593Smuzhiyun out_mutex:
7627*4882a593Smuzhiyun 	inode_unlock(main_bm_inode);
7628*4882a593Smuzhiyun 	iput(main_bm_inode);
7629*4882a593Smuzhiyun 
7630*4882a593Smuzhiyun 	/*
7631*4882a593Smuzhiyun 	 * If all the groups trim are not done or failed, but we should release
7632*4882a593Smuzhiyun 	 * main_bm related locks for avoiding the current IO starve, then go to
7633*4882a593Smuzhiyun 	 * trim the next group
7634*4882a593Smuzhiyun 	 */
7635*4882a593Smuzhiyun 	if (ret >= 0 && group <= last_group) {
7636*4882a593Smuzhiyun 		cond_resched();
7637*4882a593Smuzhiyun 		goto next_group;
7638*4882a593Smuzhiyun 	}
7639*4882a593Smuzhiyun out:
7640*4882a593Smuzhiyun 	range->len = trimmed * sb->s_blocksize;
7641*4882a593Smuzhiyun 	return ret;
7642*4882a593Smuzhiyun }
7643*4882a593Smuzhiyun 
ocfs2_trim_fs(struct super_block * sb,struct fstrim_range * range)7644*4882a593Smuzhiyun int ocfs2_trim_fs(struct super_block *sb, struct fstrim_range *range)
7645*4882a593Smuzhiyun {
7646*4882a593Smuzhiyun 	int ret;
7647*4882a593Smuzhiyun 	struct ocfs2_super *osb = OCFS2_SB(sb);
7648*4882a593Smuzhiyun 	struct ocfs2_trim_fs_info info, *pinfo = NULL;
7649*4882a593Smuzhiyun 
7650*4882a593Smuzhiyun 	ocfs2_trim_fs_lock_res_init(osb);
7651*4882a593Smuzhiyun 
7652*4882a593Smuzhiyun 	trace_ocfs2_trim_fs(range->start, range->len, range->minlen);
7653*4882a593Smuzhiyun 
7654*4882a593Smuzhiyun 	ret = ocfs2_trim_fs_lock(osb, NULL, 1);
7655*4882a593Smuzhiyun 	if (ret < 0) {
7656*4882a593Smuzhiyun 		if (ret != -EAGAIN) {
7657*4882a593Smuzhiyun 			mlog_errno(ret);
7658*4882a593Smuzhiyun 			ocfs2_trim_fs_lock_res_uninit(osb);
7659*4882a593Smuzhiyun 			return ret;
7660*4882a593Smuzhiyun 		}
7661*4882a593Smuzhiyun 
7662*4882a593Smuzhiyun 		mlog(ML_NOTICE, "Wait for trim on device (%s) to "
7663*4882a593Smuzhiyun 		     "finish, which is running from another node.\n",
7664*4882a593Smuzhiyun 		     osb->dev_str);
7665*4882a593Smuzhiyun 		ret = ocfs2_trim_fs_lock(osb, &info, 0);
7666*4882a593Smuzhiyun 		if (ret < 0) {
7667*4882a593Smuzhiyun 			mlog_errno(ret);
7668*4882a593Smuzhiyun 			ocfs2_trim_fs_lock_res_uninit(osb);
7669*4882a593Smuzhiyun 			return ret;
7670*4882a593Smuzhiyun 		}
7671*4882a593Smuzhiyun 
7672*4882a593Smuzhiyun 		if (info.tf_valid && info.tf_success &&
7673*4882a593Smuzhiyun 		    info.tf_start == range->start &&
7674*4882a593Smuzhiyun 		    info.tf_len == range->len &&
7675*4882a593Smuzhiyun 		    info.tf_minlen == range->minlen) {
7676*4882a593Smuzhiyun 			/* Avoid sending duplicated trim to a shared device */
7677*4882a593Smuzhiyun 			mlog(ML_NOTICE, "The same trim on device (%s) was "
7678*4882a593Smuzhiyun 			     "just done from node (%u), return.\n",
7679*4882a593Smuzhiyun 			     osb->dev_str, info.tf_nodenum);
7680*4882a593Smuzhiyun 			range->len = info.tf_trimlen;
7681*4882a593Smuzhiyun 			goto out;
7682*4882a593Smuzhiyun 		}
7683*4882a593Smuzhiyun 	}
7684*4882a593Smuzhiyun 
7685*4882a593Smuzhiyun 	info.tf_nodenum = osb->node_num;
7686*4882a593Smuzhiyun 	info.tf_start = range->start;
7687*4882a593Smuzhiyun 	info.tf_len = range->len;
7688*4882a593Smuzhiyun 	info.tf_minlen = range->minlen;
7689*4882a593Smuzhiyun 
7690*4882a593Smuzhiyun 	ret = ocfs2_trim_mainbm(sb, range);
7691*4882a593Smuzhiyun 
7692*4882a593Smuzhiyun 	info.tf_trimlen = range->len;
7693*4882a593Smuzhiyun 	info.tf_success = (ret < 0 ? 0 : 1);
7694*4882a593Smuzhiyun 	pinfo = &info;
7695*4882a593Smuzhiyun out:
7696*4882a593Smuzhiyun 	ocfs2_trim_fs_unlock(osb, pinfo);
7697*4882a593Smuzhiyun 	ocfs2_trim_fs_lock_res_uninit(osb);
7698*4882a593Smuzhiyun 	return ret;
7699*4882a593Smuzhiyun }
7700