xref: /openbmc/linux/fs/ocfs2/alloc.c (revision d22aa615)
1ccd979bdSMark Fasheh /* -*- mode: c; c-basic-offset: 8; -*-
2ccd979bdSMark Fasheh  * vim: noexpandtab sw=8 ts=8 sts=0:
3ccd979bdSMark Fasheh  *
4ccd979bdSMark Fasheh  * alloc.c
5ccd979bdSMark Fasheh  *
6ccd979bdSMark Fasheh  * Extent allocs and frees
7ccd979bdSMark Fasheh  *
8ccd979bdSMark Fasheh  * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
9ccd979bdSMark Fasheh  *
10ccd979bdSMark Fasheh  * This program is free software; you can redistribute it and/or
11ccd979bdSMark Fasheh  * modify it under the terms of the GNU General Public
12ccd979bdSMark Fasheh  * License as published by the Free Software Foundation; either
13ccd979bdSMark Fasheh  * version 2 of the License, or (at your option) any later version.
14ccd979bdSMark Fasheh  *
15ccd979bdSMark Fasheh  * This program is distributed in the hope that it will be useful,
16ccd979bdSMark Fasheh  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17ccd979bdSMark Fasheh  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18ccd979bdSMark Fasheh  * General Public License for more details.
19ccd979bdSMark Fasheh  *
20ccd979bdSMark Fasheh  * You should have received a copy of the GNU General Public
21ccd979bdSMark Fasheh  * License along with this program; if not, write to the
22ccd979bdSMark Fasheh  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23ccd979bdSMark Fasheh  * Boston, MA 021110-1307, USA.
24ccd979bdSMark Fasheh  */
25ccd979bdSMark Fasheh 
26ccd979bdSMark Fasheh #include <linux/fs.h>
27ccd979bdSMark Fasheh #include <linux/types.h>
28ccd979bdSMark Fasheh #include <linux/slab.h>
29ccd979bdSMark Fasheh #include <linux/highmem.h>
3060b11392SMark Fasheh #include <linux/swap.h>
31a90714c1SJan Kara #include <linux/quotaops.h>
32e80de36dSTao Ma #include <linux/blkdev.h>
33174cd4b1SIngo Molnar #include <linux/sched/signal.h>
34ccd979bdSMark Fasheh 
35ccd979bdSMark Fasheh #include <cluster/masklog.h>
36ccd979bdSMark Fasheh 
37ccd979bdSMark Fasheh #include "ocfs2.h"
38ccd979bdSMark Fasheh 
39ccd979bdSMark Fasheh #include "alloc.h"
4060b11392SMark Fasheh #include "aops.h"
41d6b32bbbSJoel Becker #include "blockcheck.h"
42ccd979bdSMark Fasheh #include "dlmglue.h"
43ccd979bdSMark Fasheh #include "extent_map.h"
44ccd979bdSMark Fasheh #include "inode.h"
45ccd979bdSMark Fasheh #include "journal.h"
46ccd979bdSMark Fasheh #include "localalloc.h"
47ccd979bdSMark Fasheh #include "suballoc.h"
48ccd979bdSMark Fasheh #include "sysfile.h"
49ccd979bdSMark Fasheh #include "file.h"
50ccd979bdSMark Fasheh #include "super.h"
51ccd979bdSMark Fasheh #include "uptodate.h"
522a50a743SJoel Becker #include "xattr.h"
53bcbbb24aSTao Ma #include "refcounttree.h"
54a09d09b8STao Ma #include "ocfs2_trace.h"
55ccd979bdSMark Fasheh 
56ccd979bdSMark Fasheh #include "buffer_head_io.h"
57ccd979bdSMark Fasheh 
58853a3a14STao Ma enum ocfs2_contig_type {
59853a3a14STao Ma 	CONTIG_NONE = 0,
60853a3a14STao Ma 	CONTIG_LEFT,
61853a3a14STao Ma 	CONTIG_RIGHT,
62853a3a14STao Ma 	CONTIG_LEFTRIGHT,
63853a3a14STao Ma };
64e7d4cb6bSTao Ma 
65853a3a14STao Ma static enum ocfs2_contig_type
66853a3a14STao Ma 	ocfs2_extent_rec_contig(struct super_block *sb,
67853a3a14STao Ma 				struct ocfs2_extent_rec *ext,
68853a3a14STao Ma 				struct ocfs2_extent_rec *insert_rec);
691625f8acSJoel Becker /*
701625f8acSJoel Becker  * Operations for a specific extent tree type.
711625f8acSJoel Becker  *
721625f8acSJoel Becker  * To implement an on-disk btree (extent tree) type in ocfs2, add
731625f8acSJoel Becker  * an ocfs2_extent_tree_operations structure and the matching
748d6220d6SJoel Becker  * ocfs2_init_<thingy>_extent_tree() function.  That's pretty much it
751625f8acSJoel Becker  * for the allocation portion of the extent tree.
761625f8acSJoel Becker  */
77e7d4cb6bSTao Ma struct ocfs2_extent_tree_operations {
781625f8acSJoel Becker 	/*
791625f8acSJoel Becker 	 * last_eb_blk is the block number of the right most leaf extent
801625f8acSJoel Becker 	 * block.  Most on-disk structures containing an extent tree store
811625f8acSJoel Becker 	 * this value for fast access.  The ->eo_set_last_eb_blk() and
821625f8acSJoel Becker 	 * ->eo_get_last_eb_blk() operations access this value.  They are
831625f8acSJoel Becker 	 *  both required.
841625f8acSJoel Becker 	 */
8535dc0aa3SJoel Becker 	void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et,
8635dc0aa3SJoel Becker 				   u64 blkno);
8735dc0aa3SJoel Becker 	u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et);
881625f8acSJoel Becker 
891625f8acSJoel Becker 	/*
901625f8acSJoel Becker 	 * The on-disk structure usually keeps track of how many total
911625f8acSJoel Becker 	 * clusters are stored in this extent tree.  This function updates
921625f8acSJoel Becker 	 * that value.  new_clusters is the delta, and must be
931625f8acSJoel Becker 	 * added to the total.  Required.
941625f8acSJoel Becker 	 */
956136ca5fSJoel Becker 	void (*eo_update_clusters)(struct ocfs2_extent_tree *et,
96e7d4cb6bSTao Ma 				   u32 new_clusters);
971625f8acSJoel Becker 
981625f8acSJoel Becker 	/*
9992ba470cSJoel Becker 	 * If this extent tree is supported by an extent map, insert
10092ba470cSJoel Becker 	 * a record into the map.
10192ba470cSJoel Becker 	 */
10292ba470cSJoel Becker 	void (*eo_extent_map_insert)(struct ocfs2_extent_tree *et,
10392ba470cSJoel Becker 				     struct ocfs2_extent_rec *rec);
10492ba470cSJoel Becker 
10592ba470cSJoel Becker 	/*
1064c911eefSJoel Becker 	 * If this extent tree is supported by an extent map, truncate the
1074c911eefSJoel Becker 	 * map to clusters,
1084c911eefSJoel Becker 	 */
1094c911eefSJoel Becker 	void (*eo_extent_map_truncate)(struct ocfs2_extent_tree *et,
1104c911eefSJoel Becker 				       u32 clusters);
1114c911eefSJoel Becker 
1124c911eefSJoel Becker 	/*
1131625f8acSJoel Becker 	 * If ->eo_insert_check() exists, it is called before rec is
1141625f8acSJoel Becker 	 * inserted into the extent tree.  It is optional.
1151625f8acSJoel Becker 	 */
1166136ca5fSJoel Becker 	int (*eo_insert_check)(struct ocfs2_extent_tree *et,
1171e61ee79SJoel Becker 			       struct ocfs2_extent_rec *rec);
1186136ca5fSJoel Becker 	int (*eo_sanity_check)(struct ocfs2_extent_tree *et);
1190ce1010fSJoel Becker 
1201625f8acSJoel Becker 	/*
1211625f8acSJoel Becker 	 * --------------------------------------------------------------
1221625f8acSJoel Becker 	 * The remaining are internal to ocfs2_extent_tree and don't have
1231625f8acSJoel Becker 	 * accessor functions
1241625f8acSJoel Becker 	 */
1251625f8acSJoel Becker 
1261625f8acSJoel Becker 	/*
1271625f8acSJoel Becker 	 * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el.
1281625f8acSJoel Becker 	 * It is required.
1291625f8acSJoel Becker 	 */
1300ce1010fSJoel Becker 	void (*eo_fill_root_el)(struct ocfs2_extent_tree *et);
1311625f8acSJoel Becker 
1321625f8acSJoel Becker 	/*
1331625f8acSJoel Becker 	 * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if
1341625f8acSJoel Becker 	 * it exists.  If it does not, et->et_max_leaf_clusters is set
1351625f8acSJoel Becker 	 * to 0 (unlimited).  Optional.
1361625f8acSJoel Becker 	 */
1376136ca5fSJoel Becker 	void (*eo_fill_max_leaf_clusters)(struct ocfs2_extent_tree *et);
138853a3a14STao Ma 
139853a3a14STao Ma 	/*
140853a3a14STao Ma 	 * ->eo_extent_contig test whether the 2 ocfs2_extent_rec
141853a3a14STao Ma 	 * are contiguous or not. Optional. Don't need to set it if use
142853a3a14STao Ma 	 * ocfs2_extent_rec as the tree leaf.
143853a3a14STao Ma 	 */
144853a3a14STao Ma 	enum ocfs2_contig_type
145853a3a14STao Ma 		(*eo_extent_contig)(struct ocfs2_extent_tree *et,
146853a3a14STao Ma 				    struct ocfs2_extent_rec *ext,
147853a3a14STao Ma 				    struct ocfs2_extent_rec *insert_rec);
148e7d4cb6bSTao Ma };
149e7d4cb6bSTao Ma 
150f99b9b7cSJoel Becker 
151f99b9b7cSJoel Becker /*
152f99b9b7cSJoel Becker  * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check
153f99b9b7cSJoel Becker  * in the methods.
154f99b9b7cSJoel Becker  */
155f99b9b7cSJoel Becker static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et);
156f99b9b7cSJoel Becker static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
157f99b9b7cSJoel Becker 					 u64 blkno);
1586136ca5fSJoel Becker static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et,
159f99b9b7cSJoel Becker 					 u32 clusters);
16092ba470cSJoel Becker static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et,
16192ba470cSJoel Becker 					   struct ocfs2_extent_rec *rec);
1624c911eefSJoel Becker static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et,
1634c911eefSJoel Becker 					     u32 clusters);
1646136ca5fSJoel Becker static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et,
165f99b9b7cSJoel Becker 				     struct ocfs2_extent_rec *rec);
1666136ca5fSJoel Becker static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et);
167f99b9b7cSJoel Becker static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et);
1689e62dc09SJulia Lawall static const struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
169f99b9b7cSJoel Becker 	.eo_set_last_eb_blk	= ocfs2_dinode_set_last_eb_blk,
170f99b9b7cSJoel Becker 	.eo_get_last_eb_blk	= ocfs2_dinode_get_last_eb_blk,
171f99b9b7cSJoel Becker 	.eo_update_clusters	= ocfs2_dinode_update_clusters,
17292ba470cSJoel Becker 	.eo_extent_map_insert	= ocfs2_dinode_extent_map_insert,
1734c911eefSJoel Becker 	.eo_extent_map_truncate	= ocfs2_dinode_extent_map_truncate,
174f99b9b7cSJoel Becker 	.eo_insert_check	= ocfs2_dinode_insert_check,
175f99b9b7cSJoel Becker 	.eo_sanity_check	= ocfs2_dinode_sanity_check,
176f99b9b7cSJoel Becker 	.eo_fill_root_el	= ocfs2_dinode_fill_root_el,
177e7d4cb6bSTao Ma };
178e7d4cb6bSTao Ma 
179e7d4cb6bSTao Ma static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
180e7d4cb6bSTao Ma 					 u64 blkno)
181e7d4cb6bSTao Ma {
182ea5efa15SJoel Becker 	struct ocfs2_dinode *di = et->et_object;
183e7d4cb6bSTao Ma 
184f99b9b7cSJoel Becker 	BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
185e7d4cb6bSTao Ma 	di->i_last_eb_blk = cpu_to_le64(blkno);
186e7d4cb6bSTao Ma }
187e7d4cb6bSTao Ma 
188e7d4cb6bSTao Ma static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
189e7d4cb6bSTao Ma {
190ea5efa15SJoel Becker 	struct ocfs2_dinode *di = et->et_object;
191e7d4cb6bSTao Ma 
192f99b9b7cSJoel Becker 	BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
193e7d4cb6bSTao Ma 	return le64_to_cpu(di->i_last_eb_blk);
194e7d4cb6bSTao Ma }
195e7d4cb6bSTao Ma 
1966136ca5fSJoel Becker static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et,
197e7d4cb6bSTao Ma 					 u32 clusters)
198e7d4cb6bSTao Ma {
1996136ca5fSJoel Becker 	struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci);
200ea5efa15SJoel Becker 	struct ocfs2_dinode *di = et->et_object;
201e7d4cb6bSTao Ma 
202e7d4cb6bSTao Ma 	le32_add_cpu(&di->i_clusters, clusters);
2036136ca5fSJoel Becker 	spin_lock(&oi->ip_lock);
2046136ca5fSJoel Becker 	oi->ip_clusters = le32_to_cpu(di->i_clusters);
2056136ca5fSJoel Becker 	spin_unlock(&oi->ip_lock);
206e7d4cb6bSTao Ma }
207e7d4cb6bSTao Ma 
20892ba470cSJoel Becker static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et,
20992ba470cSJoel Becker 					   struct ocfs2_extent_rec *rec)
21092ba470cSJoel Becker {
21192ba470cSJoel Becker 	struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode;
21292ba470cSJoel Becker 
21392ba470cSJoel Becker 	ocfs2_extent_map_insert_rec(inode, rec);
21492ba470cSJoel Becker }
21592ba470cSJoel Becker 
2164c911eefSJoel Becker static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et,
2174c911eefSJoel Becker 					     u32 clusters)
2184c911eefSJoel Becker {
2194c911eefSJoel Becker 	struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode;
2204c911eefSJoel Becker 
2214c911eefSJoel Becker 	ocfs2_extent_map_trunc(inode, clusters);
2224c911eefSJoel Becker }
2234c911eefSJoel Becker 
2246136ca5fSJoel Becker static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et,
2251e61ee79SJoel Becker 				     struct ocfs2_extent_rec *rec)
2261e61ee79SJoel Becker {
2276136ca5fSJoel Becker 	struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci);
2286136ca5fSJoel Becker 	struct ocfs2_super *osb = OCFS2_SB(oi->vfs_inode.i_sb);
2291e61ee79SJoel Becker 
2306136ca5fSJoel Becker 	BUG_ON(oi->ip_dyn_features & OCFS2_INLINE_DATA_FL);
2311e61ee79SJoel Becker 	mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
2326136ca5fSJoel Becker 			(oi->ip_clusters != le32_to_cpu(rec->e_cpos)),
2331e61ee79SJoel Becker 			"Device %s, asking for sparse allocation: inode %llu, "
2341e61ee79SJoel Becker 			"cpos %u, clusters %u\n",
2351e61ee79SJoel Becker 			osb->dev_str,
2366136ca5fSJoel Becker 			(unsigned long long)oi->ip_blkno,
2376136ca5fSJoel Becker 			rec->e_cpos, oi->ip_clusters);
2381e61ee79SJoel Becker 
2391e61ee79SJoel Becker 	return 0;
2401e61ee79SJoel Becker }
2411e61ee79SJoel Becker 
2426136ca5fSJoel Becker static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et)
243e7d4cb6bSTao Ma {
24410995aa2SJoel Becker 	struct ocfs2_dinode *di = et->et_object;
245e7d4cb6bSTao Ma 
246f99b9b7cSJoel Becker 	BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
24710995aa2SJoel Becker 	BUG_ON(!OCFS2_IS_VALID_DINODE(di));
248e7d4cb6bSTao Ma 
24910995aa2SJoel Becker 	return 0;
250e7d4cb6bSTao Ma }
251e7d4cb6bSTao Ma 
252f99b9b7cSJoel Becker static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et)
253f99b9b7cSJoel Becker {
254f99b9b7cSJoel Becker 	struct ocfs2_dinode *di = et->et_object;
255f99b9b7cSJoel Becker 
256f99b9b7cSJoel Becker 	et->et_root_el = &di->id2.i_list;
257f99b9b7cSJoel Becker }
258f99b9b7cSJoel Becker 
259e7d4cb6bSTao Ma 
2600ce1010fSJoel Becker static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et)
2610ce1010fSJoel Becker {
2622a50a743SJoel Becker 	struct ocfs2_xattr_value_buf *vb = et->et_object;
2630ce1010fSJoel Becker 
2642a50a743SJoel Becker 	et->et_root_el = &vb->vb_xv->xr_list;
2650ce1010fSJoel Becker }
2660ce1010fSJoel Becker 
267f56654c4STao Ma static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
268f56654c4STao Ma 					      u64 blkno)
269f56654c4STao Ma {
2702a50a743SJoel Becker 	struct ocfs2_xattr_value_buf *vb = et->et_object;
271f56654c4STao Ma 
2722a50a743SJoel Becker 	vb->vb_xv->xr_last_eb_blk = cpu_to_le64(blkno);
273f56654c4STao Ma }
274f56654c4STao Ma 
275f56654c4STao Ma static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
276f56654c4STao Ma {
2772a50a743SJoel Becker 	struct ocfs2_xattr_value_buf *vb = et->et_object;
278f56654c4STao Ma 
2792a50a743SJoel Becker 	return le64_to_cpu(vb->vb_xv->xr_last_eb_blk);
280f56654c4STao Ma }
281f56654c4STao Ma 
2826136ca5fSJoel Becker static void ocfs2_xattr_value_update_clusters(struct ocfs2_extent_tree *et,
283f56654c4STao Ma 					      u32 clusters)
284f56654c4STao Ma {
2852a50a743SJoel Becker 	struct ocfs2_xattr_value_buf *vb = et->et_object;
286f56654c4STao Ma 
2872a50a743SJoel Becker 	le32_add_cpu(&vb->vb_xv->xr_clusters, clusters);
288f56654c4STao Ma }
289f56654c4STao Ma 
2909e62dc09SJulia Lawall static const struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = {
29135dc0aa3SJoel Becker 	.eo_set_last_eb_blk	= ocfs2_xattr_value_set_last_eb_blk,
29235dc0aa3SJoel Becker 	.eo_get_last_eb_blk	= ocfs2_xattr_value_get_last_eb_blk,
29335dc0aa3SJoel Becker 	.eo_update_clusters	= ocfs2_xattr_value_update_clusters,
2940ce1010fSJoel Becker 	.eo_fill_root_el	= ocfs2_xattr_value_fill_root_el,
295f56654c4STao Ma };
296f56654c4STao Ma 
2970ce1010fSJoel Becker static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et)
2980ce1010fSJoel Becker {
2990ce1010fSJoel Becker 	struct ocfs2_xattr_block *xb = et->et_object;
3000ce1010fSJoel Becker 
3010ce1010fSJoel Becker 	et->et_root_el = &xb->xb_attrs.xb_root.xt_list;
3020ce1010fSJoel Becker }
3030ce1010fSJoel Becker 
3046136ca5fSJoel Becker static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct ocfs2_extent_tree *et)
305943cced3SJoel Becker {
3066136ca5fSJoel Becker 	struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
307943cced3SJoel Becker 	et->et_max_leaf_clusters =
3086136ca5fSJoel Becker 		ocfs2_clusters_for_bytes(sb, OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
309943cced3SJoel Becker }
310943cced3SJoel Becker 
311ba492615STao Ma static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
312ba492615STao Ma 					     u64 blkno)
313ba492615STao Ma {
314ea5efa15SJoel Becker 	struct ocfs2_xattr_block *xb = et->et_object;
315ba492615STao Ma 	struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
316ba492615STao Ma 
317ba492615STao Ma 	xt->xt_last_eb_blk = cpu_to_le64(blkno);
318ba492615STao Ma }
319ba492615STao Ma 
320ba492615STao Ma static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
321ba492615STao Ma {
322ea5efa15SJoel Becker 	struct ocfs2_xattr_block *xb = et->et_object;
323ba492615STao Ma 	struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
324ba492615STao Ma 
325ba492615STao Ma 	return le64_to_cpu(xt->xt_last_eb_blk);
326ba492615STao Ma }
327ba492615STao Ma 
3286136ca5fSJoel Becker static void ocfs2_xattr_tree_update_clusters(struct ocfs2_extent_tree *et,
329ba492615STao Ma 					     u32 clusters)
330ba492615STao Ma {
331ea5efa15SJoel Becker 	struct ocfs2_xattr_block *xb = et->et_object;
332ba492615STao Ma 
333ba492615STao Ma 	le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
334ba492615STao Ma }
335ba492615STao Ma 
3369e62dc09SJulia Lawall static const struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
33735dc0aa3SJoel Becker 	.eo_set_last_eb_blk	= ocfs2_xattr_tree_set_last_eb_blk,
33835dc0aa3SJoel Becker 	.eo_get_last_eb_blk	= ocfs2_xattr_tree_get_last_eb_blk,
33935dc0aa3SJoel Becker 	.eo_update_clusters	= ocfs2_xattr_tree_update_clusters,
3400ce1010fSJoel Becker 	.eo_fill_root_el	= ocfs2_xattr_tree_fill_root_el,
341943cced3SJoel Becker 	.eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters,
342ba492615STao Ma };
343ba492615STao Ma 
3449b7895efSMark Fasheh static void ocfs2_dx_root_set_last_eb_blk(struct ocfs2_extent_tree *et,
3459b7895efSMark Fasheh 					  u64 blkno)
3469b7895efSMark Fasheh {
3479b7895efSMark Fasheh 	struct ocfs2_dx_root_block *dx_root = et->et_object;
3489b7895efSMark Fasheh 
3499b7895efSMark Fasheh 	dx_root->dr_last_eb_blk = cpu_to_le64(blkno);
3509b7895efSMark Fasheh }
3519b7895efSMark Fasheh 
3529b7895efSMark Fasheh static u64 ocfs2_dx_root_get_last_eb_blk(struct ocfs2_extent_tree *et)
3539b7895efSMark Fasheh {
3549b7895efSMark Fasheh 	struct ocfs2_dx_root_block *dx_root = et->et_object;
3559b7895efSMark Fasheh 
3569b7895efSMark Fasheh 	return le64_to_cpu(dx_root->dr_last_eb_blk);
3579b7895efSMark Fasheh }
3589b7895efSMark Fasheh 
3596136ca5fSJoel Becker static void ocfs2_dx_root_update_clusters(struct ocfs2_extent_tree *et,
3609b7895efSMark Fasheh 					  u32 clusters)
3619b7895efSMark Fasheh {
3629b7895efSMark Fasheh 	struct ocfs2_dx_root_block *dx_root = et->et_object;
3639b7895efSMark Fasheh 
3649b7895efSMark Fasheh 	le32_add_cpu(&dx_root->dr_clusters, clusters);
3659b7895efSMark Fasheh }
3669b7895efSMark Fasheh 
3676136ca5fSJoel Becker static int ocfs2_dx_root_sanity_check(struct ocfs2_extent_tree *et)
3689b7895efSMark Fasheh {
3699b7895efSMark Fasheh 	struct ocfs2_dx_root_block *dx_root = et->et_object;
3709b7895efSMark Fasheh 
3719b7895efSMark Fasheh 	BUG_ON(!OCFS2_IS_VALID_DX_ROOT(dx_root));
3729b7895efSMark Fasheh 
3739b7895efSMark Fasheh 	return 0;
3749b7895efSMark Fasheh }
3759b7895efSMark Fasheh 
3769b7895efSMark Fasheh static void ocfs2_dx_root_fill_root_el(struct ocfs2_extent_tree *et)
3779b7895efSMark Fasheh {
3789b7895efSMark Fasheh 	struct ocfs2_dx_root_block *dx_root = et->et_object;
3799b7895efSMark Fasheh 
3809b7895efSMark Fasheh 	et->et_root_el = &dx_root->dr_list;
3819b7895efSMark Fasheh }
3829b7895efSMark Fasheh 
3839e62dc09SJulia Lawall static const struct ocfs2_extent_tree_operations ocfs2_dx_root_et_ops = {
3849b7895efSMark Fasheh 	.eo_set_last_eb_blk	= ocfs2_dx_root_set_last_eb_blk,
3859b7895efSMark Fasheh 	.eo_get_last_eb_blk	= ocfs2_dx_root_get_last_eb_blk,
3869b7895efSMark Fasheh 	.eo_update_clusters	= ocfs2_dx_root_update_clusters,
3879b7895efSMark Fasheh 	.eo_sanity_check	= ocfs2_dx_root_sanity_check,
3889b7895efSMark Fasheh 	.eo_fill_root_el	= ocfs2_dx_root_fill_root_el,
3899b7895efSMark Fasheh };
3909b7895efSMark Fasheh 
391fe924415STao Ma static void ocfs2_refcount_tree_fill_root_el(struct ocfs2_extent_tree *et)
392fe924415STao Ma {
393fe924415STao Ma 	struct ocfs2_refcount_block *rb = et->et_object;
394fe924415STao Ma 
395fe924415STao Ma 	et->et_root_el = &rb->rf_list;
396fe924415STao Ma }
397fe924415STao Ma 
398fe924415STao Ma static void ocfs2_refcount_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
399fe924415STao Ma 						u64 blkno)
400fe924415STao Ma {
401fe924415STao Ma 	struct ocfs2_refcount_block *rb = et->et_object;
402fe924415STao Ma 
403fe924415STao Ma 	rb->rf_last_eb_blk = cpu_to_le64(blkno);
404fe924415STao Ma }
405fe924415STao Ma 
406fe924415STao Ma static u64 ocfs2_refcount_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
407fe924415STao Ma {
408fe924415STao Ma 	struct ocfs2_refcount_block *rb = et->et_object;
409fe924415STao Ma 
410fe924415STao Ma 	return le64_to_cpu(rb->rf_last_eb_blk);
411fe924415STao Ma }
412fe924415STao Ma 
413fe924415STao Ma static void ocfs2_refcount_tree_update_clusters(struct ocfs2_extent_tree *et,
414fe924415STao Ma 						u32 clusters)
415fe924415STao Ma {
416fe924415STao Ma 	struct ocfs2_refcount_block *rb = et->et_object;
417fe924415STao Ma 
418fe924415STao Ma 	le32_add_cpu(&rb->rf_clusters, clusters);
419fe924415STao Ma }
420fe924415STao Ma 
421fe924415STao Ma static enum ocfs2_contig_type
422fe924415STao Ma ocfs2_refcount_tree_extent_contig(struct ocfs2_extent_tree *et,
423fe924415STao Ma 				  struct ocfs2_extent_rec *ext,
424fe924415STao Ma 				  struct ocfs2_extent_rec *insert_rec)
425fe924415STao Ma {
426fe924415STao Ma 	return CONTIG_NONE;
427fe924415STao Ma }
428fe924415STao Ma 
4299e62dc09SJulia Lawall static const struct ocfs2_extent_tree_operations ocfs2_refcount_tree_et_ops = {
430fe924415STao Ma 	.eo_set_last_eb_blk	= ocfs2_refcount_tree_set_last_eb_blk,
431fe924415STao Ma 	.eo_get_last_eb_blk	= ocfs2_refcount_tree_get_last_eb_blk,
432fe924415STao Ma 	.eo_update_clusters	= ocfs2_refcount_tree_update_clusters,
433fe924415STao Ma 	.eo_fill_root_el	= ocfs2_refcount_tree_fill_root_el,
434fe924415STao Ma 	.eo_extent_contig	= ocfs2_refcount_tree_extent_contig,
435fe924415STao Ma };
436fe924415STao Ma 
4378d6220d6SJoel Becker static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et,
4385e404e9eSJoel Becker 				     struct ocfs2_caching_info *ci,
439ca12b7c4STao Ma 				     struct buffer_head *bh,
44013723d00SJoel Becker 				     ocfs2_journal_access_func access,
4411a09f556SJoel Becker 				     void *obj,
4429e62dc09SJulia Lawall 				     const struct ocfs2_extent_tree_operations *ops)
443e7d4cb6bSTao Ma {
4441a09f556SJoel Becker 	et->et_ops = ops;
445ce1d9ea6SJoel Becker 	et->et_root_bh = bh;
4465e404e9eSJoel Becker 	et->et_ci = ci;
44713723d00SJoel Becker 	et->et_root_journal_access = access;
448ea5efa15SJoel Becker 	if (!obj)
449ea5efa15SJoel Becker 		obj = (void *)bh->b_data;
450ea5efa15SJoel Becker 	et->et_object = obj;
451e7d4cb6bSTao Ma 
4520ce1010fSJoel Becker 	et->et_ops->eo_fill_root_el(et);
453943cced3SJoel Becker 	if (!et->et_ops->eo_fill_max_leaf_clusters)
454943cced3SJoel Becker 		et->et_max_leaf_clusters = 0;
455943cced3SJoel Becker 	else
4566136ca5fSJoel Becker 		et->et_ops->eo_fill_max_leaf_clusters(et);
457e7d4cb6bSTao Ma }
458e7d4cb6bSTao Ma 
4598d6220d6SJoel Becker void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
4605e404e9eSJoel Becker 				   struct ocfs2_caching_info *ci,
4611a09f556SJoel Becker 				   struct buffer_head *bh)
4621a09f556SJoel Becker {
4635e404e9eSJoel Becker 	__ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_di,
46413723d00SJoel Becker 				 NULL, &ocfs2_dinode_et_ops);
4651a09f556SJoel Becker }
4661a09f556SJoel Becker 
4678d6220d6SJoel Becker void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
4685e404e9eSJoel Becker 				       struct ocfs2_caching_info *ci,
4691a09f556SJoel Becker 				       struct buffer_head *bh)
4701a09f556SJoel Becker {
4715e404e9eSJoel Becker 	__ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_xb,
47213723d00SJoel Becker 				 NULL, &ocfs2_xattr_tree_et_ops);
4731a09f556SJoel Becker }
4741a09f556SJoel Becker 
4758d6220d6SJoel Becker void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
4765e404e9eSJoel Becker 					struct ocfs2_caching_info *ci,
4772a50a743SJoel Becker 					struct ocfs2_xattr_value_buf *vb)
4781a09f556SJoel Becker {
4795e404e9eSJoel Becker 	__ocfs2_init_extent_tree(et, ci, vb->vb_bh, vb->vb_access, vb,
4801a09f556SJoel Becker 				 &ocfs2_xattr_value_et_ops);
4811a09f556SJoel Becker }
4821a09f556SJoel Becker 
4839b7895efSMark Fasheh void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree *et,
4845e404e9eSJoel Becker 				    struct ocfs2_caching_info *ci,
4859b7895efSMark Fasheh 				    struct buffer_head *bh)
4869b7895efSMark Fasheh {
4875e404e9eSJoel Becker 	__ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_dr,
4889b7895efSMark Fasheh 				 NULL, &ocfs2_dx_root_et_ops);
4899b7895efSMark Fasheh }
4909b7895efSMark Fasheh 
491fe924415STao Ma void ocfs2_init_refcount_extent_tree(struct ocfs2_extent_tree *et,
492fe924415STao Ma 				     struct ocfs2_caching_info *ci,
493fe924415STao Ma 				     struct buffer_head *bh)
494fe924415STao Ma {
495fe924415STao Ma 	__ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_rb,
496fe924415STao Ma 				 NULL, &ocfs2_refcount_tree_et_ops);
497fe924415STao Ma }
498fe924415STao Ma 
49935dc0aa3SJoel Becker static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
500e7d4cb6bSTao Ma 					    u64 new_last_eb_blk)
501e7d4cb6bSTao Ma {
502ce1d9ea6SJoel Becker 	et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
503e7d4cb6bSTao Ma }
504e7d4cb6bSTao Ma 
50535dc0aa3SJoel Becker static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
506e7d4cb6bSTao Ma {
507ce1d9ea6SJoel Becker 	return et->et_ops->eo_get_last_eb_blk(et);
508e7d4cb6bSTao Ma }
509e7d4cb6bSTao Ma 
5106136ca5fSJoel Becker static inline void ocfs2_et_update_clusters(struct ocfs2_extent_tree *et,
511e7d4cb6bSTao Ma 					    u32 clusters)
512e7d4cb6bSTao Ma {
5136136ca5fSJoel Becker 	et->et_ops->eo_update_clusters(et, clusters);
51435dc0aa3SJoel Becker }
51535dc0aa3SJoel Becker 
51692ba470cSJoel Becker static inline void ocfs2_et_extent_map_insert(struct ocfs2_extent_tree *et,
51792ba470cSJoel Becker 					      struct ocfs2_extent_rec *rec)
51892ba470cSJoel Becker {
51992ba470cSJoel Becker 	if (et->et_ops->eo_extent_map_insert)
52092ba470cSJoel Becker 		et->et_ops->eo_extent_map_insert(et, rec);
52192ba470cSJoel Becker }
52292ba470cSJoel Becker 
5234c911eefSJoel Becker static inline void ocfs2_et_extent_map_truncate(struct ocfs2_extent_tree *et,
5244c911eefSJoel Becker 						u32 clusters)
5254c911eefSJoel Becker {
5264c911eefSJoel Becker 	if (et->et_ops->eo_extent_map_truncate)
5274c911eefSJoel Becker 		et->et_ops->eo_extent_map_truncate(et, clusters);
5284c911eefSJoel Becker }
5294c911eefSJoel Becker 
53013723d00SJoel Becker static inline int ocfs2_et_root_journal_access(handle_t *handle,
53113723d00SJoel Becker 					       struct ocfs2_extent_tree *et,
53213723d00SJoel Becker 					       int type)
53313723d00SJoel Becker {
534d9a0a1f8SJoel Becker 	return et->et_root_journal_access(handle, et->et_ci, et->et_root_bh,
53513723d00SJoel Becker 					  type);
53613723d00SJoel Becker }
53713723d00SJoel Becker 
538853a3a14STao Ma static inline enum ocfs2_contig_type
539853a3a14STao Ma 	ocfs2_et_extent_contig(struct ocfs2_extent_tree *et,
540853a3a14STao Ma 			       struct ocfs2_extent_rec *rec,
541853a3a14STao Ma 			       struct ocfs2_extent_rec *insert_rec)
542853a3a14STao Ma {
543853a3a14STao Ma 	if (et->et_ops->eo_extent_contig)
544853a3a14STao Ma 		return et->et_ops->eo_extent_contig(et, rec, insert_rec);
545853a3a14STao Ma 
546853a3a14STao Ma 	return ocfs2_extent_rec_contig(
547853a3a14STao Ma 				ocfs2_metadata_cache_get_super(et->et_ci),
548853a3a14STao Ma 				rec, insert_rec);
549853a3a14STao Ma }
550853a3a14STao Ma 
5516136ca5fSJoel Becker static inline int ocfs2_et_insert_check(struct ocfs2_extent_tree *et,
5521e61ee79SJoel Becker 					struct ocfs2_extent_rec *rec)
5531e61ee79SJoel Becker {
5541e61ee79SJoel Becker 	int ret = 0;
5551e61ee79SJoel Becker 
5561e61ee79SJoel Becker 	if (et->et_ops->eo_insert_check)
5576136ca5fSJoel Becker 		ret = et->et_ops->eo_insert_check(et, rec);
5581e61ee79SJoel Becker 	return ret;
5591e61ee79SJoel Becker }
5601e61ee79SJoel Becker 
5616136ca5fSJoel Becker static inline int ocfs2_et_sanity_check(struct ocfs2_extent_tree *et)
56235dc0aa3SJoel Becker {
5631e61ee79SJoel Becker 	int ret = 0;
5641e61ee79SJoel Becker 
5651e61ee79SJoel Becker 	if (et->et_ops->eo_sanity_check)
5666136ca5fSJoel Becker 		ret = et->et_ops->eo_sanity_check(et);
5671e61ee79SJoel Becker 	return ret;
568e7d4cb6bSTao Ma }
569e7d4cb6bSTao Ma 
57059a5e416SMark Fasheh static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
57159a5e416SMark Fasheh 					 struct ocfs2_extent_block *eb);
572d401dc12SJoel Becker static void ocfs2_adjust_rightmost_records(handle_t *handle,
573d401dc12SJoel Becker 					   struct ocfs2_extent_tree *et,
5746b791bccSTao Ma 					   struct ocfs2_path *path,
5756b791bccSTao Ma 					   struct ocfs2_extent_rec *insert_rec);
576dcd0538fSMark Fasheh /*
577dcd0538fSMark Fasheh  * Reset the actual path elements so that we can re-use the structure
578dcd0538fSMark Fasheh  * to build another path. Generally, this involves freeing the buffer
579dcd0538fSMark Fasheh  * heads.
580dcd0538fSMark Fasheh  */
581e2e9f608STao Ma void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
582dcd0538fSMark Fasheh {
583dcd0538fSMark Fasheh 	int i, start = 0, depth = 0;
584dcd0538fSMark Fasheh 	struct ocfs2_path_item *node;
585dcd0538fSMark Fasheh 
586dcd0538fSMark Fasheh 	if (keep_root)
587dcd0538fSMark Fasheh 		start = 1;
588dcd0538fSMark Fasheh 
589dcd0538fSMark Fasheh 	for(i = start; i < path_num_items(path); i++) {
590dcd0538fSMark Fasheh 		node = &path->p_node[i];
591dcd0538fSMark Fasheh 
592dcd0538fSMark Fasheh 		brelse(node->bh);
593dcd0538fSMark Fasheh 		node->bh = NULL;
594dcd0538fSMark Fasheh 		node->el = NULL;
595dcd0538fSMark Fasheh 	}
596dcd0538fSMark Fasheh 
597dcd0538fSMark Fasheh 	/*
598dcd0538fSMark Fasheh 	 * Tree depth may change during truncate, or insert. If we're
599dcd0538fSMark Fasheh 	 * keeping the root extent list, then make sure that our path
600dcd0538fSMark Fasheh 	 * structure reflects the proper depth.
601dcd0538fSMark Fasheh 	 */
602dcd0538fSMark Fasheh 	if (keep_root)
603dcd0538fSMark Fasheh 		depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
60413723d00SJoel Becker 	else
60513723d00SJoel Becker 		path_root_access(path) = NULL;
606dcd0538fSMark Fasheh 
607dcd0538fSMark Fasheh 	path->p_tree_depth = depth;
608dcd0538fSMark Fasheh }
609dcd0538fSMark Fasheh 
610e2e9f608STao Ma void ocfs2_free_path(struct ocfs2_path *path)
611dcd0538fSMark Fasheh {
612dcd0538fSMark Fasheh 	if (path) {
613dcd0538fSMark Fasheh 		ocfs2_reinit_path(path, 0);
614dcd0538fSMark Fasheh 		kfree(path);
615dcd0538fSMark Fasheh 	}
616dcd0538fSMark Fasheh }
617dcd0538fSMark Fasheh 
618dcd0538fSMark Fasheh /*
619328d5752SMark Fasheh  * All the elements of src into dest. After this call, src could be freed
620328d5752SMark Fasheh  * without affecting dest.
621328d5752SMark Fasheh  *
622328d5752SMark Fasheh  * Both paths should have the same root. Any non-root elements of dest
623328d5752SMark Fasheh  * will be freed.
624328d5752SMark Fasheh  */
625328d5752SMark Fasheh static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
626328d5752SMark Fasheh {
627328d5752SMark Fasheh 	int i;
628328d5752SMark Fasheh 
629328d5752SMark Fasheh 	BUG_ON(path_root_bh(dest) != path_root_bh(src));
630328d5752SMark Fasheh 	BUG_ON(path_root_el(dest) != path_root_el(src));
63113723d00SJoel Becker 	BUG_ON(path_root_access(dest) != path_root_access(src));
632328d5752SMark Fasheh 
633328d5752SMark Fasheh 	ocfs2_reinit_path(dest, 1);
634328d5752SMark Fasheh 
635328d5752SMark Fasheh 	for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
636328d5752SMark Fasheh 		dest->p_node[i].bh = src->p_node[i].bh;
637328d5752SMark Fasheh 		dest->p_node[i].el = src->p_node[i].el;
638328d5752SMark Fasheh 
639328d5752SMark Fasheh 		if (dest->p_node[i].bh)
640328d5752SMark Fasheh 			get_bh(dest->p_node[i].bh);
641328d5752SMark Fasheh 	}
642328d5752SMark Fasheh }
643328d5752SMark Fasheh 
644328d5752SMark Fasheh /*
645dcd0538fSMark Fasheh  * Make the *dest path the same as src and re-initialize src path to
646dcd0538fSMark Fasheh  * have a root only.
647dcd0538fSMark Fasheh  */
648dcd0538fSMark Fasheh static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
649dcd0538fSMark Fasheh {
650dcd0538fSMark Fasheh 	int i;
651dcd0538fSMark Fasheh 
652dcd0538fSMark Fasheh 	BUG_ON(path_root_bh(dest) != path_root_bh(src));
65313723d00SJoel Becker 	BUG_ON(path_root_access(dest) != path_root_access(src));
654dcd0538fSMark Fasheh 
655dcd0538fSMark Fasheh 	for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
656dcd0538fSMark Fasheh 		brelse(dest->p_node[i].bh);
657dcd0538fSMark Fasheh 
658dcd0538fSMark Fasheh 		dest->p_node[i].bh = src->p_node[i].bh;
659dcd0538fSMark Fasheh 		dest->p_node[i].el = src->p_node[i].el;
660dcd0538fSMark Fasheh 
661dcd0538fSMark Fasheh 		src->p_node[i].bh = NULL;
662dcd0538fSMark Fasheh 		src->p_node[i].el = NULL;
663dcd0538fSMark Fasheh 	}
664dcd0538fSMark Fasheh }
665dcd0538fSMark Fasheh 
666dcd0538fSMark Fasheh /*
667dcd0538fSMark Fasheh  * Insert an extent block at given index.
668dcd0538fSMark Fasheh  *
669dcd0538fSMark Fasheh  * This will not take an additional reference on eb_bh.
670dcd0538fSMark Fasheh  */
671dcd0538fSMark Fasheh static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
672dcd0538fSMark Fasheh 					struct buffer_head *eb_bh)
673dcd0538fSMark Fasheh {
674dcd0538fSMark Fasheh 	struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
675dcd0538fSMark Fasheh 
676dcd0538fSMark Fasheh 	/*
677dcd0538fSMark Fasheh 	 * Right now, no root bh is an extent block, so this helps
678dcd0538fSMark Fasheh 	 * catch code errors with dinode trees. The assertion can be
679dcd0538fSMark Fasheh 	 * safely removed if we ever need to insert extent block
680dcd0538fSMark Fasheh 	 * structures at the root.
681dcd0538fSMark Fasheh 	 */
682dcd0538fSMark Fasheh 	BUG_ON(index == 0);
683dcd0538fSMark Fasheh 
684dcd0538fSMark Fasheh 	path->p_node[index].bh = eb_bh;
685dcd0538fSMark Fasheh 	path->p_node[index].el = &eb->h_list;
686dcd0538fSMark Fasheh }
687dcd0538fSMark Fasheh 
688dcd0538fSMark Fasheh static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
68913723d00SJoel Becker 					 struct ocfs2_extent_list *root_el,
69013723d00SJoel Becker 					 ocfs2_journal_access_func access)
691dcd0538fSMark Fasheh {
692dcd0538fSMark Fasheh 	struct ocfs2_path *path;
693dcd0538fSMark Fasheh 
694dcd0538fSMark Fasheh 	BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
695dcd0538fSMark Fasheh 
696dcd0538fSMark Fasheh 	path = kzalloc(sizeof(*path), GFP_NOFS);
697dcd0538fSMark Fasheh 	if (path) {
698dcd0538fSMark Fasheh 		path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
699dcd0538fSMark Fasheh 		get_bh(root_bh);
700dcd0538fSMark Fasheh 		path_root_bh(path) = root_bh;
701dcd0538fSMark Fasheh 		path_root_el(path) = root_el;
70213723d00SJoel Becker 		path_root_access(path) = access;
703dcd0538fSMark Fasheh 	}
704dcd0538fSMark Fasheh 
705dcd0538fSMark Fasheh 	return path;
706dcd0538fSMark Fasheh }
707dcd0538fSMark Fasheh 
708e2e9f608STao Ma struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path)
709ffdd7a54SJoel Becker {
71013723d00SJoel Becker 	return ocfs2_new_path(path_root_bh(path), path_root_el(path),
71113723d00SJoel Becker 			      path_root_access(path));
712ffdd7a54SJoel Becker }
713ffdd7a54SJoel Becker 
714e2e9f608STao Ma struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et)
715ffdd7a54SJoel Becker {
71613723d00SJoel Becker 	return ocfs2_new_path(et->et_root_bh, et->et_root_el,
71713723d00SJoel Becker 			      et->et_root_journal_access);
71813723d00SJoel Becker }
71913723d00SJoel Becker 
72013723d00SJoel Becker /*
72113723d00SJoel Becker  * Journal the buffer at depth idx.  All idx>0 are extent_blocks,
72213723d00SJoel Becker  * otherwise it's the root_access function.
72313723d00SJoel Becker  *
72413723d00SJoel Becker  * I don't like the way this function's name looks next to
72513723d00SJoel Becker  * ocfs2_journal_access_path(), but I don't have a better one.
72613723d00SJoel Becker  */
727e2e9f608STao Ma int ocfs2_path_bh_journal_access(handle_t *handle,
7280cf2f763SJoel Becker 				 struct ocfs2_caching_info *ci,
72913723d00SJoel Becker 				 struct ocfs2_path *path,
73013723d00SJoel Becker 				 int idx)
73113723d00SJoel Becker {
73213723d00SJoel Becker 	ocfs2_journal_access_func access = path_root_access(path);
73313723d00SJoel Becker 
73413723d00SJoel Becker 	if (!access)
73513723d00SJoel Becker 		access = ocfs2_journal_access;
73613723d00SJoel Becker 
73713723d00SJoel Becker 	if (idx)
73813723d00SJoel Becker 		access = ocfs2_journal_access_eb;
73913723d00SJoel Becker 
7400cf2f763SJoel Becker 	return access(handle, ci, path->p_node[idx].bh,
74113723d00SJoel Becker 		      OCFS2_JOURNAL_ACCESS_WRITE);
742ffdd7a54SJoel Becker }
743ffdd7a54SJoel Becker 
744dcd0538fSMark Fasheh /*
745dcd0538fSMark Fasheh  * Convenience function to journal all components in a path.
746dcd0538fSMark Fasheh  */
747e2e9f608STao Ma int ocfs2_journal_access_path(struct ocfs2_caching_info *ci,
7480cf2f763SJoel Becker 			      handle_t *handle,
749dcd0538fSMark Fasheh 			      struct ocfs2_path *path)
750dcd0538fSMark Fasheh {
751dcd0538fSMark Fasheh 	int i, ret = 0;
752dcd0538fSMark Fasheh 
753dcd0538fSMark Fasheh 	if (!path)
754dcd0538fSMark Fasheh 		goto out;
755dcd0538fSMark Fasheh 
756dcd0538fSMark Fasheh 	for(i = 0; i < path_num_items(path); i++) {
7570cf2f763SJoel Becker 		ret = ocfs2_path_bh_journal_access(handle, ci, path, i);
758dcd0538fSMark Fasheh 		if (ret < 0) {
759dcd0538fSMark Fasheh 			mlog_errno(ret);
760dcd0538fSMark Fasheh 			goto out;
761dcd0538fSMark Fasheh 		}
762dcd0538fSMark Fasheh 	}
763dcd0538fSMark Fasheh 
764dcd0538fSMark Fasheh out:
765dcd0538fSMark Fasheh 	return ret;
766dcd0538fSMark Fasheh }
767dcd0538fSMark Fasheh 
768328d5752SMark Fasheh /*
769328d5752SMark Fasheh  * Return the index of the extent record which contains cluster #v_cluster.
770328d5752SMark Fasheh  * -1 is returned if it was not found.
771328d5752SMark Fasheh  *
772328d5752SMark Fasheh  * Should work fine on interior and exterior nodes.
773328d5752SMark Fasheh  */
774328d5752SMark Fasheh int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
775328d5752SMark Fasheh {
776328d5752SMark Fasheh 	int ret = -1;
777328d5752SMark Fasheh 	int i;
778328d5752SMark Fasheh 	struct ocfs2_extent_rec *rec;
779328d5752SMark Fasheh 	u32 rec_end, rec_start, clusters;
780328d5752SMark Fasheh 
781328d5752SMark Fasheh 	for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
782328d5752SMark Fasheh 		rec = &el->l_recs[i];
783328d5752SMark Fasheh 
784328d5752SMark Fasheh 		rec_start = le32_to_cpu(rec->e_cpos);
785328d5752SMark Fasheh 		clusters = ocfs2_rec_clusters(el, rec);
786328d5752SMark Fasheh 
787328d5752SMark Fasheh 		rec_end = rec_start + clusters;
788328d5752SMark Fasheh 
789328d5752SMark Fasheh 		if (v_cluster >= rec_start && v_cluster < rec_end) {
790328d5752SMark Fasheh 			ret = i;
791328d5752SMark Fasheh 			break;
792328d5752SMark Fasheh 		}
793328d5752SMark Fasheh 	}
794328d5752SMark Fasheh 
795328d5752SMark Fasheh 	return ret;
796328d5752SMark Fasheh }
797328d5752SMark Fasheh 
798e48edee2SMark Fasheh /*
799e48edee2SMark Fasheh  * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
800853a3a14STao Ma  * ocfs2_extent_rec_contig only work properly against leaf nodes!
801e48edee2SMark Fasheh  */
802dcd0538fSMark Fasheh static int ocfs2_block_extent_contig(struct super_block *sb,
803ccd979bdSMark Fasheh 				     struct ocfs2_extent_rec *ext,
804ccd979bdSMark Fasheh 				     u64 blkno)
805ccd979bdSMark Fasheh {
806e48edee2SMark Fasheh 	u64 blk_end = le64_to_cpu(ext->e_blkno);
807e48edee2SMark Fasheh 
808e48edee2SMark Fasheh 	blk_end += ocfs2_clusters_to_blocks(sb,
809e48edee2SMark Fasheh 				    le16_to_cpu(ext->e_leaf_clusters));
810e48edee2SMark Fasheh 
811e48edee2SMark Fasheh 	return blkno == blk_end;
812ccd979bdSMark Fasheh }
813ccd979bdSMark Fasheh 
814dcd0538fSMark Fasheh static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
815dcd0538fSMark Fasheh 				  struct ocfs2_extent_rec *right)
816dcd0538fSMark Fasheh {
817e48edee2SMark Fasheh 	u32 left_range;
818e48edee2SMark Fasheh 
819e48edee2SMark Fasheh 	left_range = le32_to_cpu(left->e_cpos) +
820e48edee2SMark Fasheh 		le16_to_cpu(left->e_leaf_clusters);
821e48edee2SMark Fasheh 
822e48edee2SMark Fasheh 	return (left_range == le32_to_cpu(right->e_cpos));
823dcd0538fSMark Fasheh }
824dcd0538fSMark Fasheh 
825dcd0538fSMark Fasheh static enum ocfs2_contig_type
826853a3a14STao Ma 	ocfs2_extent_rec_contig(struct super_block *sb,
827dcd0538fSMark Fasheh 				struct ocfs2_extent_rec *ext,
828dcd0538fSMark Fasheh 				struct ocfs2_extent_rec *insert_rec)
829dcd0538fSMark Fasheh {
830dcd0538fSMark Fasheh 	u64 blkno = le64_to_cpu(insert_rec->e_blkno);
831dcd0538fSMark Fasheh 
832328d5752SMark Fasheh 	/*
833328d5752SMark Fasheh 	 * Refuse to coalesce extent records with different flag
834328d5752SMark Fasheh 	 * fields - we don't want to mix unwritten extents with user
835328d5752SMark Fasheh 	 * data.
836328d5752SMark Fasheh 	 */
837328d5752SMark Fasheh 	if (ext->e_flags != insert_rec->e_flags)
838328d5752SMark Fasheh 		return CONTIG_NONE;
839328d5752SMark Fasheh 
840dcd0538fSMark Fasheh 	if (ocfs2_extents_adjacent(ext, insert_rec) &&
841b4a17651SJoel Becker 	    ocfs2_block_extent_contig(sb, ext, blkno))
842dcd0538fSMark Fasheh 			return CONTIG_RIGHT;
843dcd0538fSMark Fasheh 
844dcd0538fSMark Fasheh 	blkno = le64_to_cpu(ext->e_blkno);
845dcd0538fSMark Fasheh 	if (ocfs2_extents_adjacent(insert_rec, ext) &&
846b4a17651SJoel Becker 	    ocfs2_block_extent_contig(sb, insert_rec, blkno))
847dcd0538fSMark Fasheh 		return CONTIG_LEFT;
848dcd0538fSMark Fasheh 
849dcd0538fSMark Fasheh 	return CONTIG_NONE;
850dcd0538fSMark Fasheh }
851dcd0538fSMark Fasheh 
852dcd0538fSMark Fasheh /*
853dcd0538fSMark Fasheh  * NOTE: We can have pretty much any combination of contiguousness and
854dcd0538fSMark Fasheh  * appending.
855dcd0538fSMark Fasheh  *
856dcd0538fSMark Fasheh  * The usefulness of APPEND_TAIL is more in that it lets us know that
857dcd0538fSMark Fasheh  * we'll have to update the path to that leaf.
858dcd0538fSMark Fasheh  */
859dcd0538fSMark Fasheh enum ocfs2_append_type {
860dcd0538fSMark Fasheh 	APPEND_NONE = 0,
861dcd0538fSMark Fasheh 	APPEND_TAIL,
862dcd0538fSMark Fasheh };
863dcd0538fSMark Fasheh 
864328d5752SMark Fasheh enum ocfs2_split_type {
865328d5752SMark Fasheh 	SPLIT_NONE = 0,
866328d5752SMark Fasheh 	SPLIT_LEFT,
867328d5752SMark Fasheh 	SPLIT_RIGHT,
868328d5752SMark Fasheh };
869328d5752SMark Fasheh 
870dcd0538fSMark Fasheh struct ocfs2_insert_type {
871328d5752SMark Fasheh 	enum ocfs2_split_type	ins_split;
872dcd0538fSMark Fasheh 	enum ocfs2_append_type	ins_appending;
873dcd0538fSMark Fasheh 	enum ocfs2_contig_type	ins_contig;
874dcd0538fSMark Fasheh 	int			ins_contig_index;
875dcd0538fSMark Fasheh 	int			ins_tree_depth;
876dcd0538fSMark Fasheh };
877dcd0538fSMark Fasheh 
878328d5752SMark Fasheh struct ocfs2_merge_ctxt {
879328d5752SMark Fasheh 	enum ocfs2_contig_type	c_contig_type;
880328d5752SMark Fasheh 	int			c_has_empty_extent;
881328d5752SMark Fasheh 	int			c_split_covers_rec;
882328d5752SMark Fasheh };
883328d5752SMark Fasheh 
8845e96581aSJoel Becker static int ocfs2_validate_extent_block(struct super_block *sb,
8855e96581aSJoel Becker 				       struct buffer_head *bh)
8865e96581aSJoel Becker {
887d6b32bbbSJoel Becker 	int rc;
8885e96581aSJoel Becker 	struct ocfs2_extent_block *eb =
8895e96581aSJoel Becker 		(struct ocfs2_extent_block *)bh->b_data;
8905e96581aSJoel Becker 
891a09d09b8STao Ma 	trace_ocfs2_validate_extent_block((unsigned long long)bh->b_blocknr);
892970e4936SJoel Becker 
893d6b32bbbSJoel Becker 	BUG_ON(!buffer_uptodate(bh));
894d6b32bbbSJoel Becker 
895d6b32bbbSJoel Becker 	/*
896d6b32bbbSJoel Becker 	 * If the ecc fails, we return the error but otherwise
897d6b32bbbSJoel Becker 	 * leave the filesystem running.  We know any error is
898d6b32bbbSJoel Becker 	 * local to this block.
899d6b32bbbSJoel Becker 	 */
900d6b32bbbSJoel Becker 	rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check);
90113723d00SJoel Becker 	if (rc) {
90213723d00SJoel Becker 		mlog(ML_ERROR, "Checksum failed for extent block %llu\n",
90313723d00SJoel Becker 		     (unsigned long long)bh->b_blocknr);
904d6b32bbbSJoel Becker 		return rc;
90513723d00SJoel Becker 	}
906d6b32bbbSJoel Becker 
907d6b32bbbSJoel Becker 	/*
908d6b32bbbSJoel Becker 	 * Errors after here are fatal.
909d6b32bbbSJoel Becker 	 */
910d6b32bbbSJoel Becker 
9115e96581aSJoel Becker 	if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
91217a5b9abSGoldwyn Rodrigues 		rc = ocfs2_error(sb,
9137ecef14aSJoe Perches 				 "Extent block #%llu has bad signature %.*s\n",
9145e96581aSJoel Becker 				 (unsigned long long)bh->b_blocknr, 7,
9155e96581aSJoel Becker 				 eb->h_signature);
91617a5b9abSGoldwyn Rodrigues 		goto bail;
9175e96581aSJoel Becker 	}
9185e96581aSJoel Becker 
9195e96581aSJoel Becker 	if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) {
92017a5b9abSGoldwyn Rodrigues 		rc = ocfs2_error(sb,
9217ecef14aSJoe Perches 				 "Extent block #%llu has an invalid h_blkno of %llu\n",
9225e96581aSJoel Becker 				 (unsigned long long)bh->b_blocknr,
9235e96581aSJoel Becker 				 (unsigned long long)le64_to_cpu(eb->h_blkno));
92417a5b9abSGoldwyn Rodrigues 		goto bail;
9255e96581aSJoel Becker 	}
9265e96581aSJoel Becker 
9275e96581aSJoel Becker 	if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) {
92817a5b9abSGoldwyn Rodrigues 		rc = ocfs2_error(sb,
9297ecef14aSJoe Perches 				 "Extent block #%llu has an invalid h_fs_generation of #%u\n",
9305e96581aSJoel Becker 				 (unsigned long long)bh->b_blocknr,
9315e96581aSJoel Becker 				 le32_to_cpu(eb->h_fs_generation));
93217a5b9abSGoldwyn Rodrigues 		goto bail;
9335e96581aSJoel Becker 	}
93417a5b9abSGoldwyn Rodrigues bail:
93517a5b9abSGoldwyn Rodrigues 	return rc;
9365e96581aSJoel Becker }
9375e96581aSJoel Becker 
9383d03a305SJoel Becker int ocfs2_read_extent_block(struct ocfs2_caching_info *ci, u64 eb_blkno,
9395e96581aSJoel Becker 			    struct buffer_head **bh)
9405e96581aSJoel Becker {
9415e96581aSJoel Becker 	int rc;
9425e96581aSJoel Becker 	struct buffer_head *tmp = *bh;
9435e96581aSJoel Becker 
9443d03a305SJoel Becker 	rc = ocfs2_read_block(ci, eb_blkno, &tmp,
945970e4936SJoel Becker 			      ocfs2_validate_extent_block);
9465e96581aSJoel Becker 
9475e96581aSJoel Becker 	/* If ocfs2_read_block() got us a new bh, pass it up. */
948970e4936SJoel Becker 	if (!rc && !*bh)
9495e96581aSJoel Becker 		*bh = tmp;
9505e96581aSJoel Becker 
9515e96581aSJoel Becker 	return rc;
9525e96581aSJoel Becker }
9535e96581aSJoel Becker 
9545e96581aSJoel Becker 
955ccd979bdSMark Fasheh /*
956ccd979bdSMark Fasheh  * How many free extents have we got before we need more meta data?
957ccd979bdSMark Fasheh  */
958964f14a0SJun Piao int ocfs2_num_free_extents(struct ocfs2_extent_tree *et)
959ccd979bdSMark Fasheh {
960ccd979bdSMark Fasheh 	int retval;
961e7d4cb6bSTao Ma 	struct ocfs2_extent_list *el = NULL;
962ccd979bdSMark Fasheh 	struct ocfs2_extent_block *eb;
963ccd979bdSMark Fasheh 	struct buffer_head *eb_bh = NULL;
964e7d4cb6bSTao Ma 	u64 last_eb_blk = 0;
965ccd979bdSMark Fasheh 
966f99b9b7cSJoel Becker 	el = et->et_root_el;
967f99b9b7cSJoel Becker 	last_eb_blk = ocfs2_et_get_last_eb_blk(et);
968e7d4cb6bSTao Ma 
969e7d4cb6bSTao Ma 	if (last_eb_blk) {
9703d03a305SJoel Becker 		retval = ocfs2_read_extent_block(et->et_ci, last_eb_blk,
9713d03a305SJoel Becker 						 &eb_bh);
972ccd979bdSMark Fasheh 		if (retval < 0) {
973ccd979bdSMark Fasheh 			mlog_errno(retval);
974ccd979bdSMark Fasheh 			goto bail;
975ccd979bdSMark Fasheh 		}
976ccd979bdSMark Fasheh 		eb = (struct ocfs2_extent_block *) eb_bh->b_data;
977ccd979bdSMark Fasheh 		el = &eb->h_list;
978e7d4cb6bSTao Ma 	}
979ccd979bdSMark Fasheh 
980ccd979bdSMark Fasheh 	BUG_ON(el->l_tree_depth != 0);
981ccd979bdSMark Fasheh 
982ccd979bdSMark Fasheh 	retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
983ccd979bdSMark Fasheh bail:
984ccd979bdSMark Fasheh 	brelse(eb_bh);
985ccd979bdSMark Fasheh 
986a09d09b8STao Ma 	trace_ocfs2_num_free_extents(retval);
987ccd979bdSMark Fasheh 	return retval;
988ccd979bdSMark Fasheh }
989ccd979bdSMark Fasheh 
990ccd979bdSMark Fasheh /* expects array to already be allocated
991ccd979bdSMark Fasheh  *
992ccd979bdSMark Fasheh  * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
993ccd979bdSMark Fasheh  * l_count for you
994ccd979bdSMark Fasheh  */
99542a5a7a9SJoel Becker static int ocfs2_create_new_meta_bhs(handle_t *handle,
99642a5a7a9SJoel Becker 				     struct ocfs2_extent_tree *et,
997ccd979bdSMark Fasheh 				     int wanted,
998ccd979bdSMark Fasheh 				     struct ocfs2_alloc_context *meta_ac,
999ccd979bdSMark Fasheh 				     struct buffer_head *bhs[])
1000ccd979bdSMark Fasheh {
1001ccd979bdSMark Fasheh 	int count, status, i;
1002ccd979bdSMark Fasheh 	u16 suballoc_bit_start;
1003ccd979bdSMark Fasheh 	u32 num_got;
10042b6cb576SJoel Becker 	u64 suballoc_loc, first_blkno;
100542a5a7a9SJoel Becker 	struct ocfs2_super *osb =
100642a5a7a9SJoel Becker 		OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
1007ccd979bdSMark Fasheh 	struct ocfs2_extent_block *eb;
1008ccd979bdSMark Fasheh 
1009ccd979bdSMark Fasheh 	count = 0;
1010ccd979bdSMark Fasheh 	while (count < wanted) {
10111ed9b777SJoel Becker 		status = ocfs2_claim_metadata(handle,
1012ccd979bdSMark Fasheh 					      meta_ac,
1013ccd979bdSMark Fasheh 					      wanted - count,
10142b6cb576SJoel Becker 					      &suballoc_loc,
1015ccd979bdSMark Fasheh 					      &suballoc_bit_start,
1016ccd979bdSMark Fasheh 					      &num_got,
1017ccd979bdSMark Fasheh 					      &first_blkno);
1018ccd979bdSMark Fasheh 		if (status < 0) {
1019ccd979bdSMark Fasheh 			mlog_errno(status);
1020ccd979bdSMark Fasheh 			goto bail;
1021ccd979bdSMark Fasheh 		}
1022ccd979bdSMark Fasheh 
1023ccd979bdSMark Fasheh 		for(i = count;  i < (num_got + count); i++) {
1024ccd979bdSMark Fasheh 			bhs[i] = sb_getblk(osb->sb, first_blkno);
1025ccd979bdSMark Fasheh 			if (bhs[i] == NULL) {
10267391a294SRui Xiang 				status = -ENOMEM;
1027ccd979bdSMark Fasheh 				mlog_errno(status);
1028ccd979bdSMark Fasheh 				goto bail;
1029ccd979bdSMark Fasheh 			}
103042a5a7a9SJoel Becker 			ocfs2_set_new_buffer_uptodate(et->et_ci, bhs[i]);
1031ccd979bdSMark Fasheh 
103242a5a7a9SJoel Becker 			status = ocfs2_journal_access_eb(handle, et->et_ci,
103342a5a7a9SJoel Becker 							 bhs[i],
1034ccd979bdSMark Fasheh 							 OCFS2_JOURNAL_ACCESS_CREATE);
1035ccd979bdSMark Fasheh 			if (status < 0) {
1036ccd979bdSMark Fasheh 				mlog_errno(status);
1037ccd979bdSMark Fasheh 				goto bail;
1038ccd979bdSMark Fasheh 			}
1039ccd979bdSMark Fasheh 
1040ccd979bdSMark Fasheh 			memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
1041ccd979bdSMark Fasheh 			eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
1042ccd979bdSMark Fasheh 			/* Ok, setup the minimal stuff here. */
1043ccd979bdSMark Fasheh 			strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
1044ccd979bdSMark Fasheh 			eb->h_blkno = cpu_to_le64(first_blkno);
1045ccd979bdSMark Fasheh 			eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
1046b89c5428STiger Yang 			eb->h_suballoc_slot =
1047b89c5428STiger Yang 				cpu_to_le16(meta_ac->ac_alloc_slot);
10482b6cb576SJoel Becker 			eb->h_suballoc_loc = cpu_to_le64(suballoc_loc);
1049ccd979bdSMark Fasheh 			eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1050ccd979bdSMark Fasheh 			eb->h_list.l_count =
1051ccd979bdSMark Fasheh 				cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
1052ccd979bdSMark Fasheh 
1053ccd979bdSMark Fasheh 			suballoc_bit_start++;
1054ccd979bdSMark Fasheh 			first_blkno++;
1055ccd979bdSMark Fasheh 
1056ccd979bdSMark Fasheh 			/* We'll also be dirtied by the caller, so
1057ccd979bdSMark Fasheh 			 * this isn't absolutely necessary. */
1058ec20cec7SJoel Becker 			ocfs2_journal_dirty(handle, bhs[i]);
1059ccd979bdSMark Fasheh 		}
1060ccd979bdSMark Fasheh 
1061ccd979bdSMark Fasheh 		count += num_got;
1062ccd979bdSMark Fasheh 	}
1063ccd979bdSMark Fasheh 
1064ccd979bdSMark Fasheh 	status = 0;
1065ccd979bdSMark Fasheh bail:
1066ccd979bdSMark Fasheh 	if (status < 0) {
1067ccd979bdSMark Fasheh 		for(i = 0; i < wanted; i++) {
1068ccd979bdSMark Fasheh 			brelse(bhs[i]);
1069ccd979bdSMark Fasheh 			bhs[i] = NULL;
1070ccd979bdSMark Fasheh 		}
1071c1e8d35eSTao Ma 		mlog_errno(status);
1072ccd979bdSMark Fasheh 	}
1073ccd979bdSMark Fasheh 	return status;
1074ccd979bdSMark Fasheh }
1075ccd979bdSMark Fasheh 
1076ccd979bdSMark Fasheh /*
1077dcd0538fSMark Fasheh  * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
1078dcd0538fSMark Fasheh  *
1079dcd0538fSMark Fasheh  * Returns the sum of the rightmost extent rec logical offset and
1080dcd0538fSMark Fasheh  * cluster count.
1081dcd0538fSMark Fasheh  *
1082dcd0538fSMark Fasheh  * ocfs2_add_branch() uses this to determine what logical cluster
1083dcd0538fSMark Fasheh  * value should be populated into the leftmost new branch records.
1084dcd0538fSMark Fasheh  *
1085dcd0538fSMark Fasheh  * ocfs2_shift_tree_depth() uses this to determine the # clusters
1086dcd0538fSMark Fasheh  * value for the new topmost tree record.
1087dcd0538fSMark Fasheh  */
1088dcd0538fSMark Fasheh static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list  *el)
1089dcd0538fSMark Fasheh {
1090dcd0538fSMark Fasheh 	int i;
1091dcd0538fSMark Fasheh 
1092dcd0538fSMark Fasheh 	i = le16_to_cpu(el->l_next_free_rec) - 1;
1093dcd0538fSMark Fasheh 
1094dcd0538fSMark Fasheh 	return le32_to_cpu(el->l_recs[i].e_cpos) +
1095e48edee2SMark Fasheh 		ocfs2_rec_clusters(el, &el->l_recs[i]);
1096dcd0538fSMark Fasheh }
1097dcd0538fSMark Fasheh 
1098dcd0538fSMark Fasheh /*
10996b791bccSTao Ma  * Change range of the branches in the right most path according to the leaf
11006b791bccSTao Ma  * extent block's rightmost record.
11016b791bccSTao Ma  */
11026b791bccSTao Ma static int ocfs2_adjust_rightmost_branch(handle_t *handle,
11036b791bccSTao Ma 					 struct ocfs2_extent_tree *et)
11046b791bccSTao Ma {
11056b791bccSTao Ma 	int status;
11066b791bccSTao Ma 	struct ocfs2_path *path = NULL;
11076b791bccSTao Ma 	struct ocfs2_extent_list *el;
11086b791bccSTao Ma 	struct ocfs2_extent_rec *rec;
11096b791bccSTao Ma 
11106b791bccSTao Ma 	path = ocfs2_new_path_from_et(et);
11116b791bccSTao Ma 	if (!path) {
11126b791bccSTao Ma 		status = -ENOMEM;
11136b791bccSTao Ma 		return status;
11146b791bccSTao Ma 	}
11156b791bccSTao Ma 
1116facdb77fSJoel Becker 	status = ocfs2_find_path(et->et_ci, path, UINT_MAX);
11176b791bccSTao Ma 	if (status < 0) {
11186b791bccSTao Ma 		mlog_errno(status);
11196b791bccSTao Ma 		goto out;
11206b791bccSTao Ma 	}
11216b791bccSTao Ma 
1122c901fb00STao Ma 	status = ocfs2_extend_trans(handle, path_num_items(path));
11236b791bccSTao Ma 	if (status < 0) {
11246b791bccSTao Ma 		mlog_errno(status);
11256b791bccSTao Ma 		goto out;
11266b791bccSTao Ma 	}
11276b791bccSTao Ma 
1128d401dc12SJoel Becker 	status = ocfs2_journal_access_path(et->et_ci, handle, path);
11296b791bccSTao Ma 	if (status < 0) {
11306b791bccSTao Ma 		mlog_errno(status);
11316b791bccSTao Ma 		goto out;
11326b791bccSTao Ma 	}
11336b791bccSTao Ma 
11346b791bccSTao Ma 	el = path_leaf_el(path);
11353a251f04SAl Viro 	rec = &el->l_recs[le16_to_cpu(el->l_next_free_rec) - 1];
11366b791bccSTao Ma 
1137d401dc12SJoel Becker 	ocfs2_adjust_rightmost_records(handle, et, path, rec);
11386b791bccSTao Ma 
11396b791bccSTao Ma out:
11406b791bccSTao Ma 	ocfs2_free_path(path);
11416b791bccSTao Ma 	return status;
11426b791bccSTao Ma }
11436b791bccSTao Ma 
11446b791bccSTao Ma /*
1145ccd979bdSMark Fasheh  * Add an entire tree branch to our inode. eb_bh is the extent block
1146d401dc12SJoel Becker  * to start at, if we don't want to start the branch at the root
1147ccd979bdSMark Fasheh  * structure.
1148ccd979bdSMark Fasheh  *
1149ccd979bdSMark Fasheh  * last_eb_bh is required as we have to update it's next_leaf pointer
1150ccd979bdSMark Fasheh  * for the new last extent block.
1151ccd979bdSMark Fasheh  *
1152ccd979bdSMark Fasheh  * the new branch will be 'empty' in the sense that every block will
1153e48edee2SMark Fasheh  * contain a single record with cluster count == 0.
1154ccd979bdSMark Fasheh  */
1155d401dc12SJoel Becker static int ocfs2_add_branch(handle_t *handle,
1156e7d4cb6bSTao Ma 			    struct ocfs2_extent_tree *et,
1157ccd979bdSMark Fasheh 			    struct buffer_head *eb_bh,
1158328d5752SMark Fasheh 			    struct buffer_head **last_eb_bh,
1159ccd979bdSMark Fasheh 			    struct ocfs2_alloc_context *meta_ac)
1160ccd979bdSMark Fasheh {
1161ccd979bdSMark Fasheh 	int status, new_blocks, i;
1162ccd979bdSMark Fasheh 	u64 next_blkno, new_last_eb_blk;
1163ccd979bdSMark Fasheh 	struct buffer_head *bh;
1164ccd979bdSMark Fasheh 	struct buffer_head **new_eb_bhs = NULL;
1165ccd979bdSMark Fasheh 	struct ocfs2_extent_block *eb;
1166ccd979bdSMark Fasheh 	struct ocfs2_extent_list  *eb_el;
1167ccd979bdSMark Fasheh 	struct ocfs2_extent_list  *el;
11686b791bccSTao Ma 	u32 new_cpos, root_end;
1169ccd979bdSMark Fasheh 
1170328d5752SMark Fasheh 	BUG_ON(!last_eb_bh || !*last_eb_bh);
1171ccd979bdSMark Fasheh 
1172ccd979bdSMark Fasheh 	if (eb_bh) {
1173ccd979bdSMark Fasheh 		eb = (struct ocfs2_extent_block *) eb_bh->b_data;
1174ccd979bdSMark Fasheh 		el = &eb->h_list;
1175ccd979bdSMark Fasheh 	} else
1176ce1d9ea6SJoel Becker 		el = et->et_root_el;
1177ccd979bdSMark Fasheh 
1178ccd979bdSMark Fasheh 	/* we never add a branch to a leaf. */
1179ccd979bdSMark Fasheh 	BUG_ON(!el->l_tree_depth);
1180ccd979bdSMark Fasheh 
1181ccd979bdSMark Fasheh 	new_blocks = le16_to_cpu(el->l_tree_depth);
1182ccd979bdSMark Fasheh 
11836b791bccSTao Ma 	eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
11846b791bccSTao Ma 	new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
11856b791bccSTao Ma 	root_end = ocfs2_sum_rightmost_rec(et->et_root_el);
11866b791bccSTao Ma 
11876b791bccSTao Ma 	/*
11886b791bccSTao Ma 	 * If there is a gap before the root end and the real end
11896b791bccSTao Ma 	 * of the righmost leaf block, we need to remove the gap
11906b791bccSTao Ma 	 * between new_cpos and root_end first so that the tree
11916b791bccSTao Ma 	 * is consistent after we add a new branch(it will start
11926b791bccSTao Ma 	 * from new_cpos).
11936b791bccSTao Ma 	 */
11946b791bccSTao Ma 	if (root_end > new_cpos) {
1195a09d09b8STao Ma 		trace_ocfs2_adjust_rightmost_branch(
1196a09d09b8STao Ma 			(unsigned long long)
1197a09d09b8STao Ma 			ocfs2_metadata_cache_owner(et->et_ci),
11986b791bccSTao Ma 			root_end, new_cpos);
1199a09d09b8STao Ma 
1200d401dc12SJoel Becker 		status = ocfs2_adjust_rightmost_branch(handle, et);
12016b791bccSTao Ma 		if (status) {
12026b791bccSTao Ma 			mlog_errno(status);
12036b791bccSTao Ma 			goto bail;
12046b791bccSTao Ma 		}
12056b791bccSTao Ma 	}
12066b791bccSTao Ma 
1207ccd979bdSMark Fasheh 	/* allocate the number of new eb blocks we need */
1208ccd979bdSMark Fasheh 	new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
1209ccd979bdSMark Fasheh 			     GFP_KERNEL);
1210ccd979bdSMark Fasheh 	if (!new_eb_bhs) {
1211ccd979bdSMark Fasheh 		status = -ENOMEM;
1212ccd979bdSMark Fasheh 		mlog_errno(status);
1213ccd979bdSMark Fasheh 		goto bail;
1214ccd979bdSMark Fasheh 	}
1215ccd979bdSMark Fasheh 
121642a5a7a9SJoel Becker 	status = ocfs2_create_new_meta_bhs(handle, et, new_blocks,
1217ccd979bdSMark Fasheh 					   meta_ac, new_eb_bhs);
1218ccd979bdSMark Fasheh 	if (status < 0) {
1219ccd979bdSMark Fasheh 		mlog_errno(status);
1220ccd979bdSMark Fasheh 		goto bail;
1221ccd979bdSMark Fasheh 	}
1222ccd979bdSMark Fasheh 
1223ccd979bdSMark Fasheh 	/* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
1224ccd979bdSMark Fasheh 	 * linked with the rest of the tree.
1225ccd979bdSMark Fasheh 	 * conversly, new_eb_bhs[0] is the new bottommost leaf.
1226ccd979bdSMark Fasheh 	 *
1227ccd979bdSMark Fasheh 	 * when we leave the loop, new_last_eb_blk will point to the
1228ccd979bdSMark Fasheh 	 * newest leaf, and next_blkno will point to the topmost extent
1229ccd979bdSMark Fasheh 	 * block. */
1230ccd979bdSMark Fasheh 	next_blkno = new_last_eb_blk = 0;
1231ccd979bdSMark Fasheh 	for(i = 0; i < new_blocks; i++) {
1232ccd979bdSMark Fasheh 		bh = new_eb_bhs[i];
1233ccd979bdSMark Fasheh 		eb = (struct ocfs2_extent_block *) bh->b_data;
12345e96581aSJoel Becker 		/* ocfs2_create_new_meta_bhs() should create it right! */
12355e96581aSJoel Becker 		BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
1236ccd979bdSMark Fasheh 		eb_el = &eb->h_list;
1237ccd979bdSMark Fasheh 
1238d401dc12SJoel Becker 		status = ocfs2_journal_access_eb(handle, et->et_ci, bh,
1239ccd979bdSMark Fasheh 						 OCFS2_JOURNAL_ACCESS_CREATE);
1240ccd979bdSMark Fasheh 		if (status < 0) {
1241ccd979bdSMark Fasheh 			mlog_errno(status);
1242ccd979bdSMark Fasheh 			goto bail;
1243ccd979bdSMark Fasheh 		}
1244ccd979bdSMark Fasheh 
1245ccd979bdSMark Fasheh 		eb->h_next_leaf_blk = 0;
1246ccd979bdSMark Fasheh 		eb_el->l_tree_depth = cpu_to_le16(i);
1247ccd979bdSMark Fasheh 		eb_el->l_next_free_rec = cpu_to_le16(1);
1248dcd0538fSMark Fasheh 		/*
1249dcd0538fSMark Fasheh 		 * This actually counts as an empty extent as
1250dcd0538fSMark Fasheh 		 * c_clusters == 0
1251dcd0538fSMark Fasheh 		 */
1252dcd0538fSMark Fasheh 		eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
1253ccd979bdSMark Fasheh 		eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
1254e48edee2SMark Fasheh 		/*
1255e48edee2SMark Fasheh 		 * eb_el isn't always an interior node, but even leaf
1256e48edee2SMark Fasheh 		 * nodes want a zero'd flags and reserved field so
1257e48edee2SMark Fasheh 		 * this gets the whole 32 bits regardless of use.
1258e48edee2SMark Fasheh 		 */
1259e48edee2SMark Fasheh 		eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
1260ccd979bdSMark Fasheh 		if (!eb_el->l_tree_depth)
1261ccd979bdSMark Fasheh 			new_last_eb_blk = le64_to_cpu(eb->h_blkno);
1262ccd979bdSMark Fasheh 
1263ec20cec7SJoel Becker 		ocfs2_journal_dirty(handle, bh);
1264ccd979bdSMark Fasheh 		next_blkno = le64_to_cpu(eb->h_blkno);
1265ccd979bdSMark Fasheh 	}
1266ccd979bdSMark Fasheh 
1267ccd979bdSMark Fasheh 	/* This is a bit hairy. We want to update up to three blocks
1268ccd979bdSMark Fasheh 	 * here without leaving any of them in an inconsistent state
1269ccd979bdSMark Fasheh 	 * in case of error. We don't have to worry about
1270ccd979bdSMark Fasheh 	 * journal_dirty erroring as it won't unless we've aborted the
1271ccd979bdSMark Fasheh 	 * handle (in which case we would never be here) so reserving
1272ccd979bdSMark Fasheh 	 * the write with journal_access is all we need to do. */
1273d401dc12SJoel Becker 	status = ocfs2_journal_access_eb(handle, et->et_ci, *last_eb_bh,
1274ccd979bdSMark Fasheh 					 OCFS2_JOURNAL_ACCESS_WRITE);
1275ccd979bdSMark Fasheh 	if (status < 0) {
1276ccd979bdSMark Fasheh 		mlog_errno(status);
1277ccd979bdSMark Fasheh 		goto bail;
1278ccd979bdSMark Fasheh 	}
1279d9a0a1f8SJoel Becker 	status = ocfs2_et_root_journal_access(handle, et,
1280ccd979bdSMark Fasheh 					      OCFS2_JOURNAL_ACCESS_WRITE);
1281ccd979bdSMark Fasheh 	if (status < 0) {
1282ccd979bdSMark Fasheh 		mlog_errno(status);
1283ccd979bdSMark Fasheh 		goto bail;
1284ccd979bdSMark Fasheh 	}
1285ccd979bdSMark Fasheh 	if (eb_bh) {
1286d401dc12SJoel Becker 		status = ocfs2_journal_access_eb(handle, et->et_ci, eb_bh,
1287ccd979bdSMark Fasheh 						 OCFS2_JOURNAL_ACCESS_WRITE);
1288ccd979bdSMark Fasheh 		if (status < 0) {
1289ccd979bdSMark Fasheh 			mlog_errno(status);
1290ccd979bdSMark Fasheh 			goto bail;
1291ccd979bdSMark Fasheh 		}
1292ccd979bdSMark Fasheh 	}
1293ccd979bdSMark Fasheh 
1294ccd979bdSMark Fasheh 	/* Link the new branch into the rest of the tree (el will
1295e7d4cb6bSTao Ma 	 * either be on the root_bh, or the extent block passed in. */
1296ccd979bdSMark Fasheh 	i = le16_to_cpu(el->l_next_free_rec);
1297ccd979bdSMark Fasheh 	el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
1298dcd0538fSMark Fasheh 	el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
1299e48edee2SMark Fasheh 	el->l_recs[i].e_int_clusters = 0;
1300ccd979bdSMark Fasheh 	le16_add_cpu(&el->l_next_free_rec, 1);
1301ccd979bdSMark Fasheh 
1302ccd979bdSMark Fasheh 	/* fe needs a new last extent block pointer, as does the
1303ccd979bdSMark Fasheh 	 * next_leaf on the previously last-extent-block. */
130435dc0aa3SJoel Becker 	ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
1305ccd979bdSMark Fasheh 
1306328d5752SMark Fasheh 	eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
1307ccd979bdSMark Fasheh 	eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
1308ccd979bdSMark Fasheh 
1309ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, *last_eb_bh);
1310ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, et->et_root_bh);
1311ec20cec7SJoel Becker 	if (eb_bh)
1312ec20cec7SJoel Becker 		ocfs2_journal_dirty(handle, eb_bh);
1313ccd979bdSMark Fasheh 
1314328d5752SMark Fasheh 	/*
1315328d5752SMark Fasheh 	 * Some callers want to track the rightmost leaf so pass it
1316328d5752SMark Fasheh 	 * back here.
1317328d5752SMark Fasheh 	 */
1318328d5752SMark Fasheh 	brelse(*last_eb_bh);
1319328d5752SMark Fasheh 	get_bh(new_eb_bhs[0]);
1320328d5752SMark Fasheh 	*last_eb_bh = new_eb_bhs[0];
1321328d5752SMark Fasheh 
1322ccd979bdSMark Fasheh 	status = 0;
1323ccd979bdSMark Fasheh bail:
1324ccd979bdSMark Fasheh 	if (new_eb_bhs) {
1325ccd979bdSMark Fasheh 		for (i = 0; i < new_blocks; i++)
1326ccd979bdSMark Fasheh 			brelse(new_eb_bhs[i]);
1327ccd979bdSMark Fasheh 		kfree(new_eb_bhs);
1328ccd979bdSMark Fasheh 	}
1329ccd979bdSMark Fasheh 
1330ccd979bdSMark Fasheh 	return status;
1331ccd979bdSMark Fasheh }
1332ccd979bdSMark Fasheh 
1333ccd979bdSMark Fasheh /*
1334ccd979bdSMark Fasheh  * adds another level to the allocation tree.
1335ccd979bdSMark Fasheh  * returns back the new extent block so you can add a branch to it
1336ccd979bdSMark Fasheh  * after this call.
1337ccd979bdSMark Fasheh  */
1338d401dc12SJoel Becker static int ocfs2_shift_tree_depth(handle_t *handle,
1339e7d4cb6bSTao Ma 				  struct ocfs2_extent_tree *et,
1340ccd979bdSMark Fasheh 				  struct ocfs2_alloc_context *meta_ac,
1341ccd979bdSMark Fasheh 				  struct buffer_head **ret_new_eb_bh)
1342ccd979bdSMark Fasheh {
1343ccd979bdSMark Fasheh 	int status, i;
1344dcd0538fSMark Fasheh 	u32 new_clusters;
1345ccd979bdSMark Fasheh 	struct buffer_head *new_eb_bh = NULL;
1346ccd979bdSMark Fasheh 	struct ocfs2_extent_block *eb;
1347e7d4cb6bSTao Ma 	struct ocfs2_extent_list  *root_el;
1348ccd979bdSMark Fasheh 	struct ocfs2_extent_list  *eb_el;
1349ccd979bdSMark Fasheh 
135042a5a7a9SJoel Becker 	status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac,
1351ccd979bdSMark Fasheh 					   &new_eb_bh);
1352ccd979bdSMark Fasheh 	if (status < 0) {
1353ccd979bdSMark Fasheh 		mlog_errno(status);
1354ccd979bdSMark Fasheh 		goto bail;
1355ccd979bdSMark Fasheh 	}
1356ccd979bdSMark Fasheh 
1357ccd979bdSMark Fasheh 	eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
13585e96581aSJoel Becker 	/* ocfs2_create_new_meta_bhs() should create it right! */
13595e96581aSJoel Becker 	BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
1360ccd979bdSMark Fasheh 
1361ccd979bdSMark Fasheh 	eb_el = &eb->h_list;
1362ce1d9ea6SJoel Becker 	root_el = et->et_root_el;
1363ccd979bdSMark Fasheh 
1364d401dc12SJoel Becker 	status = ocfs2_journal_access_eb(handle, et->et_ci, new_eb_bh,
1365ccd979bdSMark Fasheh 					 OCFS2_JOURNAL_ACCESS_CREATE);
1366ccd979bdSMark Fasheh 	if (status < 0) {
1367ccd979bdSMark Fasheh 		mlog_errno(status);
1368ccd979bdSMark Fasheh 		goto bail;
1369ccd979bdSMark Fasheh 	}
1370ccd979bdSMark Fasheh 
1371e7d4cb6bSTao Ma 	/* copy the root extent list data into the new extent block */
1372e7d4cb6bSTao Ma 	eb_el->l_tree_depth = root_el->l_tree_depth;
1373e7d4cb6bSTao Ma 	eb_el->l_next_free_rec = root_el->l_next_free_rec;
1374e7d4cb6bSTao Ma 	for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1375e7d4cb6bSTao Ma 		eb_el->l_recs[i] = root_el->l_recs[i];
1376ccd979bdSMark Fasheh 
1377ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, new_eb_bh);
1378ccd979bdSMark Fasheh 
1379d9a0a1f8SJoel Becker 	status = ocfs2_et_root_journal_access(handle, et,
1380ccd979bdSMark Fasheh 					      OCFS2_JOURNAL_ACCESS_WRITE);
1381ccd979bdSMark Fasheh 	if (status < 0) {
1382ccd979bdSMark Fasheh 		mlog_errno(status);
1383ccd979bdSMark Fasheh 		goto bail;
1384ccd979bdSMark Fasheh 	}
1385ccd979bdSMark Fasheh 
1386dcd0538fSMark Fasheh 	new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1387dcd0538fSMark Fasheh 
1388e7d4cb6bSTao Ma 	/* update root_bh now */
1389e7d4cb6bSTao Ma 	le16_add_cpu(&root_el->l_tree_depth, 1);
1390e7d4cb6bSTao Ma 	root_el->l_recs[0].e_cpos = 0;
1391e7d4cb6bSTao Ma 	root_el->l_recs[0].e_blkno = eb->h_blkno;
1392e7d4cb6bSTao Ma 	root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1393e7d4cb6bSTao Ma 	for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1394e7d4cb6bSTao Ma 		memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1395e7d4cb6bSTao Ma 	root_el->l_next_free_rec = cpu_to_le16(1);
1396ccd979bdSMark Fasheh 
1397ccd979bdSMark Fasheh 	/* If this is our 1st tree depth shift, then last_eb_blk
1398ccd979bdSMark Fasheh 	 * becomes the allocated extent block */
1399e7d4cb6bSTao Ma 	if (root_el->l_tree_depth == cpu_to_le16(1))
140035dc0aa3SJoel Becker 		ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
1401ccd979bdSMark Fasheh 
1402ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, et->et_root_bh);
1403ccd979bdSMark Fasheh 
1404ccd979bdSMark Fasheh 	*ret_new_eb_bh = new_eb_bh;
1405ccd979bdSMark Fasheh 	new_eb_bh = NULL;
1406ccd979bdSMark Fasheh 	status = 0;
1407ccd979bdSMark Fasheh bail:
1408ccd979bdSMark Fasheh 	brelse(new_eb_bh);
1409ccd979bdSMark Fasheh 
1410ccd979bdSMark Fasheh 	return status;
1411ccd979bdSMark Fasheh }
1412ccd979bdSMark Fasheh 
1413ccd979bdSMark Fasheh /*
1414ccd979bdSMark Fasheh  * Should only be called when there is no space left in any of the
1415ccd979bdSMark Fasheh  * leaf nodes. What we want to do is find the lowest tree depth
1416ccd979bdSMark Fasheh  * non-leaf extent block with room for new records. There are three
1417ccd979bdSMark Fasheh  * valid results of this search:
1418ccd979bdSMark Fasheh  *
1419ccd979bdSMark Fasheh  * 1) a lowest extent block is found, then we pass it back in
1420ccd979bdSMark Fasheh  *    *lowest_eb_bh and return '0'
1421ccd979bdSMark Fasheh  *
1422e7d4cb6bSTao Ma  * 2) the search fails to find anything, but the root_el has room. We
1423ccd979bdSMark Fasheh  *    pass NULL back in *lowest_eb_bh, but still return '0'
1424ccd979bdSMark Fasheh  *
1425e7d4cb6bSTao Ma  * 3) the search fails to find anything AND the root_el is full, in
1426ccd979bdSMark Fasheh  *    which case we return > 0
1427ccd979bdSMark Fasheh  *
1428ccd979bdSMark Fasheh  * return status < 0 indicates an error.
1429ccd979bdSMark Fasheh  */
1430d401dc12SJoel Becker static int ocfs2_find_branch_target(struct ocfs2_extent_tree *et,
1431ccd979bdSMark Fasheh 				    struct buffer_head **target_bh)
1432ccd979bdSMark Fasheh {
1433ccd979bdSMark Fasheh 	int status = 0, i;
1434ccd979bdSMark Fasheh 	u64 blkno;
1435ccd979bdSMark Fasheh 	struct ocfs2_extent_block *eb;
1436ccd979bdSMark Fasheh 	struct ocfs2_extent_list  *el;
1437ccd979bdSMark Fasheh 	struct buffer_head *bh = NULL;
1438ccd979bdSMark Fasheh 	struct buffer_head *lowest_bh = NULL;
1439ccd979bdSMark Fasheh 
1440ccd979bdSMark Fasheh 	*target_bh = NULL;
1441ccd979bdSMark Fasheh 
1442ce1d9ea6SJoel Becker 	el = et->et_root_el;
1443ccd979bdSMark Fasheh 
1444ccd979bdSMark Fasheh 	while(le16_to_cpu(el->l_tree_depth) > 1) {
1445ccd979bdSMark Fasheh 		if (le16_to_cpu(el->l_next_free_rec) == 0) {
14463d03a305SJoel Becker 			ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
14477ecef14aSJoe Perches 				    "Owner %llu has empty extent list (next_free_rec == 0)\n",
14483d03a305SJoel Becker 				    (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
1449ccd979bdSMark Fasheh 			status = -EIO;
1450ccd979bdSMark Fasheh 			goto bail;
1451ccd979bdSMark Fasheh 		}
1452ccd979bdSMark Fasheh 		i = le16_to_cpu(el->l_next_free_rec) - 1;
1453ccd979bdSMark Fasheh 		blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1454ccd979bdSMark Fasheh 		if (!blkno) {
14553d03a305SJoel Becker 			ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
14567ecef14aSJoe Perches 				    "Owner %llu has extent list where extent # %d has no physical block start\n",
14573d03a305SJoel Becker 				    (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i);
1458ccd979bdSMark Fasheh 			status = -EIO;
1459ccd979bdSMark Fasheh 			goto bail;
1460ccd979bdSMark Fasheh 		}
1461ccd979bdSMark Fasheh 
1462ccd979bdSMark Fasheh 		brelse(bh);
1463ccd979bdSMark Fasheh 		bh = NULL;
1464ccd979bdSMark Fasheh 
14653d03a305SJoel Becker 		status = ocfs2_read_extent_block(et->et_ci, blkno, &bh);
1466ccd979bdSMark Fasheh 		if (status < 0) {
1467ccd979bdSMark Fasheh 			mlog_errno(status);
1468ccd979bdSMark Fasheh 			goto bail;
1469ccd979bdSMark Fasheh 		}
1470ccd979bdSMark Fasheh 
1471ccd979bdSMark Fasheh 		eb = (struct ocfs2_extent_block *) bh->b_data;
1472ccd979bdSMark Fasheh 		el = &eb->h_list;
1473ccd979bdSMark Fasheh 
1474ccd979bdSMark Fasheh 		if (le16_to_cpu(el->l_next_free_rec) <
1475ccd979bdSMark Fasheh 		    le16_to_cpu(el->l_count)) {
1476ccd979bdSMark Fasheh 			brelse(lowest_bh);
1477ccd979bdSMark Fasheh 			lowest_bh = bh;
1478ccd979bdSMark Fasheh 			get_bh(lowest_bh);
1479ccd979bdSMark Fasheh 		}
1480ccd979bdSMark Fasheh 	}
1481ccd979bdSMark Fasheh 
1482ccd979bdSMark Fasheh 	/* If we didn't find one and the fe doesn't have any room,
1483ccd979bdSMark Fasheh 	 * then return '1' */
1484ce1d9ea6SJoel Becker 	el = et->et_root_el;
1485e7d4cb6bSTao Ma 	if (!lowest_bh && (el->l_next_free_rec == el->l_count))
1486ccd979bdSMark Fasheh 		status = 1;
1487ccd979bdSMark Fasheh 
1488ccd979bdSMark Fasheh 	*target_bh = lowest_bh;
1489ccd979bdSMark Fasheh bail:
1490ccd979bdSMark Fasheh 	brelse(bh);
1491ccd979bdSMark Fasheh 
1492ccd979bdSMark Fasheh 	return status;
1493ccd979bdSMark Fasheh }
1494ccd979bdSMark Fasheh 
1495e48edee2SMark Fasheh /*
1496c3afcbb3SMark Fasheh  * Grow a b-tree so that it has more records.
1497c3afcbb3SMark Fasheh  *
1498c3afcbb3SMark Fasheh  * We might shift the tree depth in which case existing paths should
1499c3afcbb3SMark Fasheh  * be considered invalid.
1500c3afcbb3SMark Fasheh  *
1501c3afcbb3SMark Fasheh  * Tree depth after the grow is returned via *final_depth.
1502328d5752SMark Fasheh  *
1503328d5752SMark Fasheh  * *last_eb_bh will be updated by ocfs2_add_branch().
1504c3afcbb3SMark Fasheh  */
1505d401dc12SJoel Becker static int ocfs2_grow_tree(handle_t *handle, struct ocfs2_extent_tree *et,
1506d401dc12SJoel Becker 			   int *final_depth, struct buffer_head **last_eb_bh,
1507c3afcbb3SMark Fasheh 			   struct ocfs2_alloc_context *meta_ac)
1508c3afcbb3SMark Fasheh {
1509c3afcbb3SMark Fasheh 	int ret, shift;
1510ce1d9ea6SJoel Becker 	struct ocfs2_extent_list *el = et->et_root_el;
1511e7d4cb6bSTao Ma 	int depth = le16_to_cpu(el->l_tree_depth);
1512c3afcbb3SMark Fasheh 	struct buffer_head *bh = NULL;
1513c3afcbb3SMark Fasheh 
1514c3afcbb3SMark Fasheh 	BUG_ON(meta_ac == NULL);
1515c3afcbb3SMark Fasheh 
1516d401dc12SJoel Becker 	shift = ocfs2_find_branch_target(et, &bh);
1517c3afcbb3SMark Fasheh 	if (shift < 0) {
1518c3afcbb3SMark Fasheh 		ret = shift;
1519c3afcbb3SMark Fasheh 		mlog_errno(ret);
1520c3afcbb3SMark Fasheh 		goto out;
1521c3afcbb3SMark Fasheh 	}
1522c3afcbb3SMark Fasheh 
1523c3afcbb3SMark Fasheh 	/* We traveled all the way to the bottom of the allocation tree
1524c3afcbb3SMark Fasheh 	 * and didn't find room for any more extents - we need to add
1525c3afcbb3SMark Fasheh 	 * another tree level */
1526c3afcbb3SMark Fasheh 	if (shift) {
1527c3afcbb3SMark Fasheh 		BUG_ON(bh);
1528a09d09b8STao Ma 		trace_ocfs2_grow_tree(
1529a09d09b8STao Ma 			(unsigned long long)
1530a09d09b8STao Ma 			ocfs2_metadata_cache_owner(et->et_ci),
1531a09d09b8STao Ma 			depth);
1532c3afcbb3SMark Fasheh 
1533c3afcbb3SMark Fasheh 		/* ocfs2_shift_tree_depth will return us a buffer with
1534c3afcbb3SMark Fasheh 		 * the new extent block (so we can pass that to
1535c3afcbb3SMark Fasheh 		 * ocfs2_add_branch). */
1536d401dc12SJoel Becker 		ret = ocfs2_shift_tree_depth(handle, et, meta_ac, &bh);
1537c3afcbb3SMark Fasheh 		if (ret < 0) {
1538c3afcbb3SMark Fasheh 			mlog_errno(ret);
1539c3afcbb3SMark Fasheh 			goto out;
1540c3afcbb3SMark Fasheh 		}
1541c3afcbb3SMark Fasheh 		depth++;
1542328d5752SMark Fasheh 		if (depth == 1) {
1543328d5752SMark Fasheh 			/*
1544328d5752SMark Fasheh 			 * Special case: we have room now if we shifted from
1545328d5752SMark Fasheh 			 * tree_depth 0, so no more work needs to be done.
1546328d5752SMark Fasheh 			 *
1547328d5752SMark Fasheh 			 * We won't be calling add_branch, so pass
1548328d5752SMark Fasheh 			 * back *last_eb_bh as the new leaf. At depth
1549328d5752SMark Fasheh 			 * zero, it should always be null so there's
1550328d5752SMark Fasheh 			 * no reason to brelse.
1551328d5752SMark Fasheh 			 */
1552328d5752SMark Fasheh 			BUG_ON(*last_eb_bh);
1553328d5752SMark Fasheh 			get_bh(bh);
1554328d5752SMark Fasheh 			*last_eb_bh = bh;
1555c3afcbb3SMark Fasheh 			goto out;
1556c3afcbb3SMark Fasheh 		}
1557328d5752SMark Fasheh 	}
1558c3afcbb3SMark Fasheh 
1559c3afcbb3SMark Fasheh 	/* call ocfs2_add_branch to add the final part of the tree with
1560c3afcbb3SMark Fasheh 	 * the new data. */
1561d401dc12SJoel Becker 	ret = ocfs2_add_branch(handle, et, bh, last_eb_bh,
1562c3afcbb3SMark Fasheh 			       meta_ac);
1563c3afcbb3SMark Fasheh 	if (ret < 0) {
1564c3afcbb3SMark Fasheh 		mlog_errno(ret);
1565c3afcbb3SMark Fasheh 		goto out;
1566c3afcbb3SMark Fasheh 	}
1567c3afcbb3SMark Fasheh 
1568c3afcbb3SMark Fasheh out:
1569c3afcbb3SMark Fasheh 	if (final_depth)
1570c3afcbb3SMark Fasheh 		*final_depth = depth;
1571c3afcbb3SMark Fasheh 	brelse(bh);
1572c3afcbb3SMark Fasheh 	return ret;
1573c3afcbb3SMark Fasheh }
1574c3afcbb3SMark Fasheh 
1575c3afcbb3SMark Fasheh /*
1576dcd0538fSMark Fasheh  * This function will discard the rightmost extent record.
1577dcd0538fSMark Fasheh  */
1578dcd0538fSMark Fasheh static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1579dcd0538fSMark Fasheh {
1580dcd0538fSMark Fasheh 	int next_free = le16_to_cpu(el->l_next_free_rec);
1581dcd0538fSMark Fasheh 	int count = le16_to_cpu(el->l_count);
1582dcd0538fSMark Fasheh 	unsigned int num_bytes;
1583dcd0538fSMark Fasheh 
1584dcd0538fSMark Fasheh 	BUG_ON(!next_free);
1585dcd0538fSMark Fasheh 	/* This will cause us to go off the end of our extent list. */
1586dcd0538fSMark Fasheh 	BUG_ON(next_free >= count);
1587dcd0538fSMark Fasheh 
1588dcd0538fSMark Fasheh 	num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1589dcd0538fSMark Fasheh 
1590dcd0538fSMark Fasheh 	memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1591dcd0538fSMark Fasheh }
1592dcd0538fSMark Fasheh 
1593dcd0538fSMark Fasheh static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1594dcd0538fSMark Fasheh 			      struct ocfs2_extent_rec *insert_rec)
1595dcd0538fSMark Fasheh {
1596dcd0538fSMark Fasheh 	int i, insert_index, next_free, has_empty, num_bytes;
1597dcd0538fSMark Fasheh 	u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1598dcd0538fSMark Fasheh 	struct ocfs2_extent_rec *rec;
1599dcd0538fSMark Fasheh 
1600dcd0538fSMark Fasheh 	next_free = le16_to_cpu(el->l_next_free_rec);
1601dcd0538fSMark Fasheh 	has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1602dcd0538fSMark Fasheh 
1603dcd0538fSMark Fasheh 	BUG_ON(!next_free);
1604dcd0538fSMark Fasheh 
1605dcd0538fSMark Fasheh 	/* The tree code before us didn't allow enough room in the leaf. */
1606b1f3550fSJulia Lawall 	BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
1607dcd0538fSMark Fasheh 
1608dcd0538fSMark Fasheh 	/*
1609dcd0538fSMark Fasheh 	 * The easiest way to approach this is to just remove the
1610dcd0538fSMark Fasheh 	 * empty extent and temporarily decrement next_free.
1611dcd0538fSMark Fasheh 	 */
1612dcd0538fSMark Fasheh 	if (has_empty) {
1613dcd0538fSMark Fasheh 		/*
1614dcd0538fSMark Fasheh 		 * If next_free was 1 (only an empty extent), this
1615dcd0538fSMark Fasheh 		 * loop won't execute, which is fine. We still want
1616dcd0538fSMark Fasheh 		 * the decrement above to happen.
1617dcd0538fSMark Fasheh 		 */
1618dcd0538fSMark Fasheh 		for(i = 0; i < (next_free - 1); i++)
1619dcd0538fSMark Fasheh 			el->l_recs[i] = el->l_recs[i+1];
1620dcd0538fSMark Fasheh 
1621dcd0538fSMark Fasheh 		next_free--;
1622dcd0538fSMark Fasheh 	}
1623dcd0538fSMark Fasheh 
1624dcd0538fSMark Fasheh 	/*
1625dcd0538fSMark Fasheh 	 * Figure out what the new record index should be.
1626dcd0538fSMark Fasheh 	 */
1627dcd0538fSMark Fasheh 	for(i = 0; i < next_free; i++) {
1628dcd0538fSMark Fasheh 		rec = &el->l_recs[i];
1629dcd0538fSMark Fasheh 
1630dcd0538fSMark Fasheh 		if (insert_cpos < le32_to_cpu(rec->e_cpos))
1631dcd0538fSMark Fasheh 			break;
1632dcd0538fSMark Fasheh 	}
1633dcd0538fSMark Fasheh 	insert_index = i;
1634dcd0538fSMark Fasheh 
1635a09d09b8STao Ma 	trace_ocfs2_rotate_leaf(insert_cpos, insert_index,
1636a09d09b8STao Ma 				has_empty, next_free,
1637a09d09b8STao Ma 				le16_to_cpu(el->l_count));
1638dcd0538fSMark Fasheh 
1639dcd0538fSMark Fasheh 	BUG_ON(insert_index < 0);
1640dcd0538fSMark Fasheh 	BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1641dcd0538fSMark Fasheh 	BUG_ON(insert_index > next_free);
1642dcd0538fSMark Fasheh 
1643dcd0538fSMark Fasheh 	/*
1644dcd0538fSMark Fasheh 	 * No need to memmove if we're just adding to the tail.
1645dcd0538fSMark Fasheh 	 */
1646dcd0538fSMark Fasheh 	if (insert_index != next_free) {
1647dcd0538fSMark Fasheh 		BUG_ON(next_free >= le16_to_cpu(el->l_count));
1648dcd0538fSMark Fasheh 
1649dcd0538fSMark Fasheh 		num_bytes = next_free - insert_index;
1650dcd0538fSMark Fasheh 		num_bytes *= sizeof(struct ocfs2_extent_rec);
1651dcd0538fSMark Fasheh 		memmove(&el->l_recs[insert_index + 1],
1652dcd0538fSMark Fasheh 			&el->l_recs[insert_index],
1653dcd0538fSMark Fasheh 			num_bytes);
1654dcd0538fSMark Fasheh 	}
1655dcd0538fSMark Fasheh 
1656dcd0538fSMark Fasheh 	/*
1657dcd0538fSMark Fasheh 	 * Either we had an empty extent, and need to re-increment or
1658dcd0538fSMark Fasheh 	 * there was no empty extent on a non full rightmost leaf node,
1659dcd0538fSMark Fasheh 	 * in which case we still need to increment.
1660dcd0538fSMark Fasheh 	 */
1661dcd0538fSMark Fasheh 	next_free++;
1662dcd0538fSMark Fasheh 	el->l_next_free_rec = cpu_to_le16(next_free);
1663dcd0538fSMark Fasheh 	/*
1664dcd0538fSMark Fasheh 	 * Make sure none of the math above just messed up our tree.
1665dcd0538fSMark Fasheh 	 */
1666dcd0538fSMark Fasheh 	BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1667dcd0538fSMark Fasheh 
1668dcd0538fSMark Fasheh 	el->l_recs[insert_index] = *insert_rec;
1669dcd0538fSMark Fasheh 
1670dcd0538fSMark Fasheh }
1671dcd0538fSMark Fasheh 
1672328d5752SMark Fasheh static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1673328d5752SMark Fasheh {
1674328d5752SMark Fasheh 	int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1675328d5752SMark Fasheh 
1676328d5752SMark Fasheh 	BUG_ON(num_recs == 0);
1677328d5752SMark Fasheh 
1678328d5752SMark Fasheh 	if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1679328d5752SMark Fasheh 		num_recs--;
1680328d5752SMark Fasheh 		size = num_recs * sizeof(struct ocfs2_extent_rec);
1681328d5752SMark Fasheh 		memmove(&el->l_recs[0], &el->l_recs[1], size);
1682328d5752SMark Fasheh 		memset(&el->l_recs[num_recs], 0,
1683328d5752SMark Fasheh 		       sizeof(struct ocfs2_extent_rec));
1684328d5752SMark Fasheh 		el->l_next_free_rec = cpu_to_le16(num_recs);
1685328d5752SMark Fasheh 	}
1686328d5752SMark Fasheh }
1687328d5752SMark Fasheh 
1688dcd0538fSMark Fasheh /*
1689dcd0538fSMark Fasheh  * Create an empty extent record .
1690dcd0538fSMark Fasheh  *
1691dcd0538fSMark Fasheh  * l_next_free_rec may be updated.
1692dcd0538fSMark Fasheh  *
1693dcd0538fSMark Fasheh  * If an empty extent already exists do nothing.
1694dcd0538fSMark Fasheh  */
1695dcd0538fSMark Fasheh static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1696dcd0538fSMark Fasheh {
1697dcd0538fSMark Fasheh 	int next_free = le16_to_cpu(el->l_next_free_rec);
1698dcd0538fSMark Fasheh 
1699e48edee2SMark Fasheh 	BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1700e48edee2SMark Fasheh 
1701dcd0538fSMark Fasheh 	if (next_free == 0)
1702dcd0538fSMark Fasheh 		goto set_and_inc;
1703dcd0538fSMark Fasheh 
1704dcd0538fSMark Fasheh 	if (ocfs2_is_empty_extent(&el->l_recs[0]))
1705dcd0538fSMark Fasheh 		return;
1706dcd0538fSMark Fasheh 
1707dcd0538fSMark Fasheh 	mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1708dcd0538fSMark Fasheh 			"Asked to create an empty extent in a full list:\n"
1709dcd0538fSMark Fasheh 			"count = %u, tree depth = %u",
1710dcd0538fSMark Fasheh 			le16_to_cpu(el->l_count),
1711dcd0538fSMark Fasheh 			le16_to_cpu(el->l_tree_depth));
1712dcd0538fSMark Fasheh 
1713dcd0538fSMark Fasheh 	ocfs2_shift_records_right(el);
1714dcd0538fSMark Fasheh 
1715dcd0538fSMark Fasheh set_and_inc:
1716dcd0538fSMark Fasheh 	le16_add_cpu(&el->l_next_free_rec, 1);
1717dcd0538fSMark Fasheh 	memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1718dcd0538fSMark Fasheh }
1719dcd0538fSMark Fasheh 
1720dcd0538fSMark Fasheh /*
1721dcd0538fSMark Fasheh  * For a rotation which involves two leaf nodes, the "root node" is
1722dcd0538fSMark Fasheh  * the lowest level tree node which contains a path to both leafs. This
1723dcd0538fSMark Fasheh  * resulting set of information can be used to form a complete "subtree"
1724dcd0538fSMark Fasheh  *
1725dcd0538fSMark Fasheh  * This function is passed two full paths from the dinode down to a
1726dcd0538fSMark Fasheh  * pair of adjacent leaves. It's task is to figure out which path
1727dcd0538fSMark Fasheh  * index contains the subtree root - this can be the root index itself
1728dcd0538fSMark Fasheh  * in a worst-case rotation.
1729dcd0538fSMark Fasheh  *
1730dcd0538fSMark Fasheh  * The array index of the subtree root is passed back.
1731dcd0538fSMark Fasheh  */
173238a04e43STao Ma int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et,
1733dcd0538fSMark Fasheh 			    struct ocfs2_path *left,
1734dcd0538fSMark Fasheh 			    struct ocfs2_path *right)
1735dcd0538fSMark Fasheh {
1736dcd0538fSMark Fasheh 	int i = 0;
1737dcd0538fSMark Fasheh 
1738dcd0538fSMark Fasheh 	/*
1739dcd0538fSMark Fasheh 	 * Check that the caller passed in two paths from the same tree.
1740dcd0538fSMark Fasheh 	 */
1741dcd0538fSMark Fasheh 	BUG_ON(path_root_bh(left) != path_root_bh(right));
1742dcd0538fSMark Fasheh 
1743dcd0538fSMark Fasheh 	do {
1744dcd0538fSMark Fasheh 		i++;
1745dcd0538fSMark Fasheh 
1746dcd0538fSMark Fasheh 		/*
1747dcd0538fSMark Fasheh 		 * The caller didn't pass two adjacent paths.
1748dcd0538fSMark Fasheh 		 */
1749dcd0538fSMark Fasheh 		mlog_bug_on_msg(i > left->p_tree_depth,
17507dc02805SJoel Becker 				"Owner %llu, left depth %u, right depth %u\n"
1751dcd0538fSMark Fasheh 				"left leaf blk %llu, right leaf blk %llu\n",
17527dc02805SJoel Becker 				(unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
17537dc02805SJoel Becker 				left->p_tree_depth, right->p_tree_depth,
1754dcd0538fSMark Fasheh 				(unsigned long long)path_leaf_bh(left)->b_blocknr,
1755dcd0538fSMark Fasheh 				(unsigned long long)path_leaf_bh(right)->b_blocknr);
1756dcd0538fSMark Fasheh 	} while (left->p_node[i].bh->b_blocknr ==
1757dcd0538fSMark Fasheh 		 right->p_node[i].bh->b_blocknr);
1758dcd0538fSMark Fasheh 
1759dcd0538fSMark Fasheh 	return i - 1;
1760dcd0538fSMark Fasheh }
1761dcd0538fSMark Fasheh 
1762dcd0538fSMark Fasheh typedef void (path_insert_t)(void *, struct buffer_head *);
1763dcd0538fSMark Fasheh 
1764dcd0538fSMark Fasheh /*
1765dcd0538fSMark Fasheh  * Traverse a btree path in search of cpos, starting at root_el.
1766dcd0538fSMark Fasheh  *
1767dcd0538fSMark Fasheh  * This code can be called with a cpos larger than the tree, in which
1768dcd0538fSMark Fasheh  * case it will return the rightmost path.
1769dcd0538fSMark Fasheh  */
1770facdb77fSJoel Becker static int __ocfs2_find_path(struct ocfs2_caching_info *ci,
1771dcd0538fSMark Fasheh 			     struct ocfs2_extent_list *root_el, u32 cpos,
1772dcd0538fSMark Fasheh 			     path_insert_t *func, void *data)
1773dcd0538fSMark Fasheh {
1774dcd0538fSMark Fasheh 	int i, ret = 0;
1775dcd0538fSMark Fasheh 	u32 range;
1776dcd0538fSMark Fasheh 	u64 blkno;
1777dcd0538fSMark Fasheh 	struct buffer_head *bh = NULL;
1778dcd0538fSMark Fasheh 	struct ocfs2_extent_block *eb;
1779dcd0538fSMark Fasheh 	struct ocfs2_extent_list *el;
1780dcd0538fSMark Fasheh 	struct ocfs2_extent_rec *rec;
1781dcd0538fSMark Fasheh 
1782dcd0538fSMark Fasheh 	el = root_el;
1783dcd0538fSMark Fasheh 	while (el->l_tree_depth) {
1784dcd0538fSMark Fasheh 		if (le16_to_cpu(el->l_next_free_rec) == 0) {
1785facdb77fSJoel Becker 			ocfs2_error(ocfs2_metadata_cache_get_super(ci),
17867ecef14aSJoe Perches 				    "Owner %llu has empty extent list at depth %u\n",
1787facdb77fSJoel Becker 				    (unsigned long long)ocfs2_metadata_cache_owner(ci),
1788dcd0538fSMark Fasheh 				    le16_to_cpu(el->l_tree_depth));
1789dcd0538fSMark Fasheh 			ret = -EROFS;
1790dcd0538fSMark Fasheh 			goto out;
1791dcd0538fSMark Fasheh 
1792dcd0538fSMark Fasheh 		}
1793dcd0538fSMark Fasheh 
1794dcd0538fSMark Fasheh 		for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1795dcd0538fSMark Fasheh 			rec = &el->l_recs[i];
1796dcd0538fSMark Fasheh 
1797dcd0538fSMark Fasheh 			/*
1798dcd0538fSMark Fasheh 			 * In the case that cpos is off the allocation
1799dcd0538fSMark Fasheh 			 * tree, this should just wind up returning the
1800dcd0538fSMark Fasheh 			 * rightmost record.
1801dcd0538fSMark Fasheh 			 */
1802dcd0538fSMark Fasheh 			range = le32_to_cpu(rec->e_cpos) +
1803e48edee2SMark Fasheh 				ocfs2_rec_clusters(el, rec);
1804dcd0538fSMark Fasheh 			if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1805dcd0538fSMark Fasheh 			    break;
1806dcd0538fSMark Fasheh 		}
1807dcd0538fSMark Fasheh 
1808dcd0538fSMark Fasheh 		blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1809dcd0538fSMark Fasheh 		if (blkno == 0) {
1810facdb77fSJoel Becker 			ocfs2_error(ocfs2_metadata_cache_get_super(ci),
18117ecef14aSJoe Perches 				    "Owner %llu has bad blkno in extent list at depth %u (index %d)\n",
1812facdb77fSJoel Becker 				    (unsigned long long)ocfs2_metadata_cache_owner(ci),
1813dcd0538fSMark Fasheh 				    le16_to_cpu(el->l_tree_depth), i);
1814dcd0538fSMark Fasheh 			ret = -EROFS;
1815dcd0538fSMark Fasheh 			goto out;
1816dcd0538fSMark Fasheh 		}
1817dcd0538fSMark Fasheh 
1818dcd0538fSMark Fasheh 		brelse(bh);
1819dcd0538fSMark Fasheh 		bh = NULL;
1820facdb77fSJoel Becker 		ret = ocfs2_read_extent_block(ci, blkno, &bh);
1821dcd0538fSMark Fasheh 		if (ret) {
1822dcd0538fSMark Fasheh 			mlog_errno(ret);
1823dcd0538fSMark Fasheh 			goto out;
1824dcd0538fSMark Fasheh 		}
1825dcd0538fSMark Fasheh 
1826dcd0538fSMark Fasheh 		eb = (struct ocfs2_extent_block *) bh->b_data;
1827dcd0538fSMark Fasheh 		el = &eb->h_list;
1828dcd0538fSMark Fasheh 
1829dcd0538fSMark Fasheh 		if (le16_to_cpu(el->l_next_free_rec) >
1830dcd0538fSMark Fasheh 		    le16_to_cpu(el->l_count)) {
1831facdb77fSJoel Becker 			ocfs2_error(ocfs2_metadata_cache_get_super(ci),
18327ecef14aSJoe Perches 				    "Owner %llu has bad count in extent list at block %llu (next free=%u, count=%u)\n",
1833facdb77fSJoel Becker 				    (unsigned long long)ocfs2_metadata_cache_owner(ci),
1834dcd0538fSMark Fasheh 				    (unsigned long long)bh->b_blocknr,
1835dcd0538fSMark Fasheh 				    le16_to_cpu(el->l_next_free_rec),
1836dcd0538fSMark Fasheh 				    le16_to_cpu(el->l_count));
1837dcd0538fSMark Fasheh 			ret = -EROFS;
1838dcd0538fSMark Fasheh 			goto out;
1839dcd0538fSMark Fasheh 		}
1840dcd0538fSMark Fasheh 
1841dcd0538fSMark Fasheh 		if (func)
1842dcd0538fSMark Fasheh 			func(data, bh);
1843dcd0538fSMark Fasheh 	}
1844dcd0538fSMark Fasheh 
1845dcd0538fSMark Fasheh out:
1846dcd0538fSMark Fasheh 	/*
1847dcd0538fSMark Fasheh 	 * Catch any trailing bh that the loop didn't handle.
1848dcd0538fSMark Fasheh 	 */
1849dcd0538fSMark Fasheh 	brelse(bh);
1850dcd0538fSMark Fasheh 
1851dcd0538fSMark Fasheh 	return ret;
1852dcd0538fSMark Fasheh }
1853dcd0538fSMark Fasheh 
1854dcd0538fSMark Fasheh /*
1855dcd0538fSMark Fasheh  * Given an initialized path (that is, it has a valid root extent
1856dcd0538fSMark Fasheh  * list), this function will traverse the btree in search of the path
1857dcd0538fSMark Fasheh  * which would contain cpos.
1858dcd0538fSMark Fasheh  *
1859dcd0538fSMark Fasheh  * The path traveled is recorded in the path structure.
1860dcd0538fSMark Fasheh  *
1861dcd0538fSMark Fasheh  * Note that this will not do any comparisons on leaf node extent
1862dcd0538fSMark Fasheh  * records, so it will work fine in the case that we just added a tree
1863dcd0538fSMark Fasheh  * branch.
1864dcd0538fSMark Fasheh  */
1865dcd0538fSMark Fasheh struct find_path_data {
1866dcd0538fSMark Fasheh 	int index;
1867dcd0538fSMark Fasheh 	struct ocfs2_path *path;
1868dcd0538fSMark Fasheh };
1869dcd0538fSMark Fasheh static void find_path_ins(void *data, struct buffer_head *bh)
1870dcd0538fSMark Fasheh {
1871dcd0538fSMark Fasheh 	struct find_path_data *fp = data;
1872dcd0538fSMark Fasheh 
1873dcd0538fSMark Fasheh 	get_bh(bh);
1874dcd0538fSMark Fasheh 	ocfs2_path_insert_eb(fp->path, fp->index, bh);
1875dcd0538fSMark Fasheh 	fp->index++;
1876dcd0538fSMark Fasheh }
1877e2e9f608STao Ma int ocfs2_find_path(struct ocfs2_caching_info *ci,
1878facdb77fSJoel Becker 		    struct ocfs2_path *path, u32 cpos)
1879dcd0538fSMark Fasheh {
1880dcd0538fSMark Fasheh 	struct find_path_data data;
1881dcd0538fSMark Fasheh 
1882dcd0538fSMark Fasheh 	data.index = 1;
1883dcd0538fSMark Fasheh 	data.path = path;
1884facdb77fSJoel Becker 	return __ocfs2_find_path(ci, path_root_el(path), cpos,
1885dcd0538fSMark Fasheh 				 find_path_ins, &data);
1886dcd0538fSMark Fasheh }
1887dcd0538fSMark Fasheh 
1888dcd0538fSMark Fasheh static void find_leaf_ins(void *data, struct buffer_head *bh)
1889dcd0538fSMark Fasheh {
1890dcd0538fSMark Fasheh 	struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1891dcd0538fSMark Fasheh 	struct ocfs2_extent_list *el = &eb->h_list;
1892dcd0538fSMark Fasheh 	struct buffer_head **ret = data;
1893dcd0538fSMark Fasheh 
1894dcd0538fSMark Fasheh 	/* We want to retain only the leaf block. */
1895dcd0538fSMark Fasheh 	if (le16_to_cpu(el->l_tree_depth) == 0) {
1896dcd0538fSMark Fasheh 		get_bh(bh);
1897dcd0538fSMark Fasheh 		*ret = bh;
1898dcd0538fSMark Fasheh 	}
1899dcd0538fSMark Fasheh }
1900dcd0538fSMark Fasheh /*
1901dcd0538fSMark Fasheh  * Find the leaf block in the tree which would contain cpos. No
1902dcd0538fSMark Fasheh  * checking of the actual leaf is done.
1903dcd0538fSMark Fasheh  *
1904dcd0538fSMark Fasheh  * Some paths want to call this instead of allocating a path structure
1905dcd0538fSMark Fasheh  * and calling ocfs2_find_path().
1906dcd0538fSMark Fasheh  *
1907dcd0538fSMark Fasheh  * This function doesn't handle non btree extent lists.
1908dcd0538fSMark Fasheh  */
1909facdb77fSJoel Becker int ocfs2_find_leaf(struct ocfs2_caching_info *ci,
1910facdb77fSJoel Becker 		    struct ocfs2_extent_list *root_el, u32 cpos,
1911facdb77fSJoel Becker 		    struct buffer_head **leaf_bh)
1912dcd0538fSMark Fasheh {
1913dcd0538fSMark Fasheh 	int ret;
1914dcd0538fSMark Fasheh 	struct buffer_head *bh = NULL;
1915dcd0538fSMark Fasheh 
1916facdb77fSJoel Becker 	ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh);
1917dcd0538fSMark Fasheh 	if (ret) {
1918dcd0538fSMark Fasheh 		mlog_errno(ret);
1919dcd0538fSMark Fasheh 		goto out;
1920dcd0538fSMark Fasheh 	}
1921dcd0538fSMark Fasheh 
1922dcd0538fSMark Fasheh 	*leaf_bh = bh;
1923dcd0538fSMark Fasheh out:
1924dcd0538fSMark Fasheh 	return ret;
1925dcd0538fSMark Fasheh }
1926dcd0538fSMark Fasheh 
1927dcd0538fSMark Fasheh /*
1928dcd0538fSMark Fasheh  * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1929dcd0538fSMark Fasheh  *
1930dcd0538fSMark Fasheh  * Basically, we've moved stuff around at the bottom of the tree and
1931dcd0538fSMark Fasheh  * we need to fix up the extent records above the changes to reflect
1932dcd0538fSMark Fasheh  * the new changes.
1933dcd0538fSMark Fasheh  *
1934dcd0538fSMark Fasheh  * left_rec: the record on the left.
1935dcd0538fSMark Fasheh  * right_rec: the record to the right of left_rec
1936dcd0538fSMark Fasheh  * right_child_el: is the child list pointed to by right_rec
1937dcd0538fSMark Fasheh  *
1938dcd0538fSMark Fasheh  * By definition, this only works on interior nodes.
1939dcd0538fSMark Fasheh  */
1940dcd0538fSMark Fasheh static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1941dcd0538fSMark Fasheh 				  struct ocfs2_extent_rec *right_rec,
1942dcd0538fSMark Fasheh 				  struct ocfs2_extent_list *right_child_el)
1943dcd0538fSMark Fasheh {
1944dcd0538fSMark Fasheh 	u32 left_clusters, right_end;
1945dcd0538fSMark Fasheh 
1946dcd0538fSMark Fasheh 	/*
1947dcd0538fSMark Fasheh 	 * Interior nodes never have holes. Their cpos is the cpos of
1948dcd0538fSMark Fasheh 	 * the leftmost record in their child list. Their cluster
1949dcd0538fSMark Fasheh 	 * count covers the full theoretical range of their child list
1950dcd0538fSMark Fasheh 	 * - the range between their cpos and the cpos of the record
1951dcd0538fSMark Fasheh 	 * immediately to their right.
1952dcd0538fSMark Fasheh 	 */
1953dcd0538fSMark Fasheh 	left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
195482e12644STao Ma 	if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) {
195582e12644STao Ma 		BUG_ON(right_child_el->l_tree_depth);
1956328d5752SMark Fasheh 		BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1957328d5752SMark Fasheh 		left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1958328d5752SMark Fasheh 	}
1959dcd0538fSMark Fasheh 	left_clusters -= le32_to_cpu(left_rec->e_cpos);
1960e48edee2SMark Fasheh 	left_rec->e_int_clusters = cpu_to_le32(left_clusters);
1961dcd0538fSMark Fasheh 
1962dcd0538fSMark Fasheh 	/*
1963dcd0538fSMark Fasheh 	 * Calculate the rightmost cluster count boundary before
1964e48edee2SMark Fasheh 	 * moving cpos - we will need to adjust clusters after
1965dcd0538fSMark Fasheh 	 * updating e_cpos to keep the same highest cluster count.
1966dcd0538fSMark Fasheh 	 */
1967dcd0538fSMark Fasheh 	right_end = le32_to_cpu(right_rec->e_cpos);
1968e48edee2SMark Fasheh 	right_end += le32_to_cpu(right_rec->e_int_clusters);
1969dcd0538fSMark Fasheh 
1970dcd0538fSMark Fasheh 	right_rec->e_cpos = left_rec->e_cpos;
1971dcd0538fSMark Fasheh 	le32_add_cpu(&right_rec->e_cpos, left_clusters);
1972dcd0538fSMark Fasheh 
1973dcd0538fSMark Fasheh 	right_end -= le32_to_cpu(right_rec->e_cpos);
1974e48edee2SMark Fasheh 	right_rec->e_int_clusters = cpu_to_le32(right_end);
1975dcd0538fSMark Fasheh }
1976dcd0538fSMark Fasheh 
1977dcd0538fSMark Fasheh /*
1978dcd0538fSMark Fasheh  * Adjust the adjacent root node records involved in a
1979dcd0538fSMark Fasheh  * rotation. left_el_blkno is passed in as a key so that we can easily
1980dcd0538fSMark Fasheh  * find it's index in the root list.
1981dcd0538fSMark Fasheh  */
1982dcd0538fSMark Fasheh static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1983dcd0538fSMark Fasheh 				      struct ocfs2_extent_list *left_el,
1984dcd0538fSMark Fasheh 				      struct ocfs2_extent_list *right_el,
1985dcd0538fSMark Fasheh 				      u64 left_el_blkno)
1986dcd0538fSMark Fasheh {
1987dcd0538fSMark Fasheh 	int i;
1988dcd0538fSMark Fasheh 
1989dcd0538fSMark Fasheh 	BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1990dcd0538fSMark Fasheh 	       le16_to_cpu(left_el->l_tree_depth));
1991dcd0538fSMark Fasheh 
1992dcd0538fSMark Fasheh 	for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1993dcd0538fSMark Fasheh 		if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1994dcd0538fSMark Fasheh 			break;
1995dcd0538fSMark Fasheh 	}
1996dcd0538fSMark Fasheh 
1997dcd0538fSMark Fasheh 	/*
1998dcd0538fSMark Fasheh 	 * The path walking code should have never returned a root and
1999dcd0538fSMark Fasheh 	 * two paths which are not adjacent.
2000dcd0538fSMark Fasheh 	 */
2001dcd0538fSMark Fasheh 	BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
2002dcd0538fSMark Fasheh 
2003964f14a0SJun Piao 	ocfs2_adjust_adjacent_records(&root_el->l_recs[i],
2004dcd0538fSMark Fasheh 				      &root_el->l_recs[i + 1], right_el);
2005dcd0538fSMark Fasheh }
2006dcd0538fSMark Fasheh 
2007dcd0538fSMark Fasheh /*
2008dcd0538fSMark Fasheh  * We've changed a leaf block (in right_path) and need to reflect that
2009dcd0538fSMark Fasheh  * change back up the subtree.
2010dcd0538fSMark Fasheh  *
2011dcd0538fSMark Fasheh  * This happens in multiple places:
2012dcd0538fSMark Fasheh  *   - When we've moved an extent record from the left path leaf to the right
2013dcd0538fSMark Fasheh  *     path leaf to make room for an empty extent in the left path leaf.
2014dcd0538fSMark Fasheh  *   - When our insert into the right path leaf is at the leftmost edge
2015dcd0538fSMark Fasheh  *     and requires an update of the path immediately to it's left. This
2016dcd0538fSMark Fasheh  *     can occur at the end of some types of rotation and appending inserts.
2017677b9752STao Ma  *   - When we've adjusted the last extent record in the left path leaf and the
2018677b9752STao Ma  *     1st extent record in the right path leaf during cross extent block merge.
2019dcd0538fSMark Fasheh  */
20204619c73eSJoel Becker static void ocfs2_complete_edge_insert(handle_t *handle,
2021dcd0538fSMark Fasheh 				       struct ocfs2_path *left_path,
2022dcd0538fSMark Fasheh 				       struct ocfs2_path *right_path,
2023dcd0538fSMark Fasheh 				       int subtree_index)
2024dcd0538fSMark Fasheh {
2025ec20cec7SJoel Becker 	int i, idx;
2026dcd0538fSMark Fasheh 	struct ocfs2_extent_list *el, *left_el, *right_el;
2027dcd0538fSMark Fasheh 	struct ocfs2_extent_rec *left_rec, *right_rec;
2028dcd0538fSMark Fasheh 	struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2029dcd0538fSMark Fasheh 
2030dcd0538fSMark Fasheh 	/*
2031dcd0538fSMark Fasheh 	 * Update the counts and position values within all the
2032dcd0538fSMark Fasheh 	 * interior nodes to reflect the leaf rotation we just did.
2033dcd0538fSMark Fasheh 	 *
2034dcd0538fSMark Fasheh 	 * The root node is handled below the loop.
2035dcd0538fSMark Fasheh 	 *
2036dcd0538fSMark Fasheh 	 * We begin the loop with right_el and left_el pointing to the
2037dcd0538fSMark Fasheh 	 * leaf lists and work our way up.
2038dcd0538fSMark Fasheh 	 *
2039dcd0538fSMark Fasheh 	 * NOTE: within this loop, left_el and right_el always refer
2040dcd0538fSMark Fasheh 	 * to the *child* lists.
2041dcd0538fSMark Fasheh 	 */
2042dcd0538fSMark Fasheh 	left_el = path_leaf_el(left_path);
2043dcd0538fSMark Fasheh 	right_el = path_leaf_el(right_path);
2044dcd0538fSMark Fasheh 	for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
2045a09d09b8STao Ma 		trace_ocfs2_complete_edge_insert(i);
2046dcd0538fSMark Fasheh 
2047dcd0538fSMark Fasheh 		/*
2048dcd0538fSMark Fasheh 		 * One nice property of knowing that all of these
2049dcd0538fSMark Fasheh 		 * nodes are below the root is that we only deal with
2050dcd0538fSMark Fasheh 		 * the leftmost right node record and the rightmost
2051dcd0538fSMark Fasheh 		 * left node record.
2052dcd0538fSMark Fasheh 		 */
2053dcd0538fSMark Fasheh 		el = left_path->p_node[i].el;
2054dcd0538fSMark Fasheh 		idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
2055dcd0538fSMark Fasheh 		left_rec = &el->l_recs[idx];
2056dcd0538fSMark Fasheh 
2057dcd0538fSMark Fasheh 		el = right_path->p_node[i].el;
2058dcd0538fSMark Fasheh 		right_rec = &el->l_recs[0];
2059dcd0538fSMark Fasheh 
2060964f14a0SJun Piao 		ocfs2_adjust_adjacent_records(left_rec, right_rec, right_el);
2061dcd0538fSMark Fasheh 
2062ec20cec7SJoel Becker 		ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
2063ec20cec7SJoel Becker 		ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
2064dcd0538fSMark Fasheh 
2065dcd0538fSMark Fasheh 		/*
2066dcd0538fSMark Fasheh 		 * Setup our list pointers now so that the current
2067dcd0538fSMark Fasheh 		 * parents become children in the next iteration.
2068dcd0538fSMark Fasheh 		 */
2069dcd0538fSMark Fasheh 		left_el = left_path->p_node[i].el;
2070dcd0538fSMark Fasheh 		right_el = right_path->p_node[i].el;
2071dcd0538fSMark Fasheh 	}
2072dcd0538fSMark Fasheh 
2073dcd0538fSMark Fasheh 	/*
2074dcd0538fSMark Fasheh 	 * At the root node, adjust the two adjacent records which
2075dcd0538fSMark Fasheh 	 * begin our path to the leaves.
2076dcd0538fSMark Fasheh 	 */
2077dcd0538fSMark Fasheh 
2078dcd0538fSMark Fasheh 	el = left_path->p_node[subtree_index].el;
2079dcd0538fSMark Fasheh 	left_el = left_path->p_node[subtree_index + 1].el;
2080dcd0538fSMark Fasheh 	right_el = right_path->p_node[subtree_index + 1].el;
2081dcd0538fSMark Fasheh 
2082dcd0538fSMark Fasheh 	ocfs2_adjust_root_records(el, left_el, right_el,
2083dcd0538fSMark Fasheh 				  left_path->p_node[subtree_index + 1].bh->b_blocknr);
2084dcd0538fSMark Fasheh 
2085dcd0538fSMark Fasheh 	root_bh = left_path->p_node[subtree_index].bh;
2086dcd0538fSMark Fasheh 
2087ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, root_bh);
2088dcd0538fSMark Fasheh }
2089dcd0538fSMark Fasheh 
20905c601abaSJoel Becker static int ocfs2_rotate_subtree_right(handle_t *handle,
20915c601abaSJoel Becker 				      struct ocfs2_extent_tree *et,
2092dcd0538fSMark Fasheh 				      struct ocfs2_path *left_path,
2093dcd0538fSMark Fasheh 				      struct ocfs2_path *right_path,
2094dcd0538fSMark Fasheh 				      int subtree_index)
2095dcd0538fSMark Fasheh {
2096dcd0538fSMark Fasheh 	int ret, i;
2097dcd0538fSMark Fasheh 	struct buffer_head *right_leaf_bh;
2098dcd0538fSMark Fasheh 	struct buffer_head *left_leaf_bh = NULL;
2099dcd0538fSMark Fasheh 	struct buffer_head *root_bh;
2100dcd0538fSMark Fasheh 	struct ocfs2_extent_list *right_el, *left_el;
2101dcd0538fSMark Fasheh 	struct ocfs2_extent_rec move_rec;
2102dcd0538fSMark Fasheh 
2103dcd0538fSMark Fasheh 	left_leaf_bh = path_leaf_bh(left_path);
2104dcd0538fSMark Fasheh 	left_el = path_leaf_el(left_path);
2105dcd0538fSMark Fasheh 
2106dcd0538fSMark Fasheh 	if (left_el->l_next_free_rec != left_el->l_count) {
21075c601abaSJoel Becker 		ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
21087ecef14aSJoe Perches 			    "Inode %llu has non-full interior leaf node %llu (next free = %u)\n",
21095c601abaSJoel Becker 			    (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2110dcd0538fSMark Fasheh 			    (unsigned long long)left_leaf_bh->b_blocknr,
2111dcd0538fSMark Fasheh 			    le16_to_cpu(left_el->l_next_free_rec));
2112dcd0538fSMark Fasheh 		return -EROFS;
2113dcd0538fSMark Fasheh 	}
2114dcd0538fSMark Fasheh 
2115dcd0538fSMark Fasheh 	/*
2116dcd0538fSMark Fasheh 	 * This extent block may already have an empty record, so we
2117dcd0538fSMark Fasheh 	 * return early if so.
2118dcd0538fSMark Fasheh 	 */
2119dcd0538fSMark Fasheh 	if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
2120dcd0538fSMark Fasheh 		return 0;
2121dcd0538fSMark Fasheh 
2122dcd0538fSMark Fasheh 	root_bh = left_path->p_node[subtree_index].bh;
2123dcd0538fSMark Fasheh 	BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2124dcd0538fSMark Fasheh 
21255c601abaSJoel Becker 	ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
212613723d00SJoel Becker 					   subtree_index);
2127dcd0538fSMark Fasheh 	if (ret) {
2128dcd0538fSMark Fasheh 		mlog_errno(ret);
2129dcd0538fSMark Fasheh 		goto out;
2130dcd0538fSMark Fasheh 	}
2131dcd0538fSMark Fasheh 
2132dcd0538fSMark Fasheh 	for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
21335c601abaSJoel Becker 		ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
213413723d00SJoel Becker 						   right_path, i);
2135dcd0538fSMark Fasheh 		if (ret) {
2136dcd0538fSMark Fasheh 			mlog_errno(ret);
2137dcd0538fSMark Fasheh 			goto out;
2138dcd0538fSMark Fasheh 		}
2139dcd0538fSMark Fasheh 
21405c601abaSJoel Becker 		ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
214113723d00SJoel Becker 						   left_path, i);
2142dcd0538fSMark Fasheh 		if (ret) {
2143dcd0538fSMark Fasheh 			mlog_errno(ret);
2144dcd0538fSMark Fasheh 			goto out;
2145dcd0538fSMark Fasheh 		}
2146dcd0538fSMark Fasheh 	}
2147dcd0538fSMark Fasheh 
2148dcd0538fSMark Fasheh 	right_leaf_bh = path_leaf_bh(right_path);
2149dcd0538fSMark Fasheh 	right_el = path_leaf_el(right_path);
2150dcd0538fSMark Fasheh 
2151dcd0538fSMark Fasheh 	/* This is a code error, not a disk corruption. */
2152dcd0538fSMark Fasheh 	mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
2153dcd0538fSMark Fasheh 			"because rightmost leaf block %llu is empty\n",
21545c601abaSJoel Becker 			(unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2155dcd0538fSMark Fasheh 			(unsigned long long)right_leaf_bh->b_blocknr);
2156dcd0538fSMark Fasheh 
2157dcd0538fSMark Fasheh 	ocfs2_create_empty_extent(right_el);
2158dcd0538fSMark Fasheh 
2159ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, right_leaf_bh);
2160dcd0538fSMark Fasheh 
2161dcd0538fSMark Fasheh 	/* Do the copy now. */
2162dcd0538fSMark Fasheh 	i = le16_to_cpu(left_el->l_next_free_rec) - 1;
2163dcd0538fSMark Fasheh 	move_rec = left_el->l_recs[i];
2164dcd0538fSMark Fasheh 	right_el->l_recs[0] = move_rec;
2165dcd0538fSMark Fasheh 
2166dcd0538fSMark Fasheh 	/*
2167dcd0538fSMark Fasheh 	 * Clear out the record we just copied and shift everything
2168dcd0538fSMark Fasheh 	 * over, leaving an empty extent in the left leaf.
2169dcd0538fSMark Fasheh 	 *
2170dcd0538fSMark Fasheh 	 * We temporarily subtract from next_free_rec so that the
2171dcd0538fSMark Fasheh 	 * shift will lose the tail record (which is now defunct).
2172dcd0538fSMark Fasheh 	 */
2173dcd0538fSMark Fasheh 	le16_add_cpu(&left_el->l_next_free_rec, -1);
2174dcd0538fSMark Fasheh 	ocfs2_shift_records_right(left_el);
2175dcd0538fSMark Fasheh 	memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2176dcd0538fSMark Fasheh 	le16_add_cpu(&left_el->l_next_free_rec, 1);
2177dcd0538fSMark Fasheh 
2178ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, left_leaf_bh);
2179dcd0538fSMark Fasheh 
21804619c73eSJoel Becker 	ocfs2_complete_edge_insert(handle, left_path, right_path,
2181dcd0538fSMark Fasheh 				   subtree_index);
2182dcd0538fSMark Fasheh 
2183dcd0538fSMark Fasheh out:
2184dcd0538fSMark Fasheh 	return ret;
2185dcd0538fSMark Fasheh }
2186dcd0538fSMark Fasheh 
2187dcd0538fSMark Fasheh /*
2188dcd0538fSMark Fasheh  * Given a full path, determine what cpos value would return us a path
2189dcd0538fSMark Fasheh  * containing the leaf immediately to the left of the current one.
2190dcd0538fSMark Fasheh  *
2191dcd0538fSMark Fasheh  * Will return zero if the path passed in is already the leftmost path.
2192dcd0538fSMark Fasheh  */
2193ee149a7cSTristan Ye int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
2194dcd0538fSMark Fasheh 				  struct ocfs2_path *path, u32 *cpos)
2195dcd0538fSMark Fasheh {
2196dcd0538fSMark Fasheh 	int i, j, ret = 0;
2197dcd0538fSMark Fasheh 	u64 blkno;
2198dcd0538fSMark Fasheh 	struct ocfs2_extent_list *el;
2199dcd0538fSMark Fasheh 
2200e48edee2SMark Fasheh 	BUG_ON(path->p_tree_depth == 0);
2201e48edee2SMark Fasheh 
2202dcd0538fSMark Fasheh 	*cpos = 0;
2203dcd0538fSMark Fasheh 
2204dcd0538fSMark Fasheh 	blkno = path_leaf_bh(path)->b_blocknr;
2205dcd0538fSMark Fasheh 
2206dcd0538fSMark Fasheh 	/* Start at the tree node just above the leaf and work our way up. */
2207dcd0538fSMark Fasheh 	i = path->p_tree_depth - 1;
2208dcd0538fSMark Fasheh 	while (i >= 0) {
2209dcd0538fSMark Fasheh 		el = path->p_node[i].el;
2210dcd0538fSMark Fasheh 
2211dcd0538fSMark Fasheh 		/*
2212dcd0538fSMark Fasheh 		 * Find the extent record just before the one in our
2213dcd0538fSMark Fasheh 		 * path.
2214dcd0538fSMark Fasheh 		 */
2215dcd0538fSMark Fasheh 		for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2216dcd0538fSMark Fasheh 			if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2217dcd0538fSMark Fasheh 				if (j == 0) {
2218dcd0538fSMark Fasheh 					if (i == 0) {
2219dcd0538fSMark Fasheh 						/*
2220dcd0538fSMark Fasheh 						 * We've determined that the
2221dcd0538fSMark Fasheh 						 * path specified is already
2222dcd0538fSMark Fasheh 						 * the leftmost one - return a
2223dcd0538fSMark Fasheh 						 * cpos of zero.
2224dcd0538fSMark Fasheh 						 */
2225dcd0538fSMark Fasheh 						goto out;
2226dcd0538fSMark Fasheh 					}
2227dcd0538fSMark Fasheh 					/*
2228dcd0538fSMark Fasheh 					 * The leftmost record points to our
2229dcd0538fSMark Fasheh 					 * leaf - we need to travel up the
2230dcd0538fSMark Fasheh 					 * tree one level.
2231dcd0538fSMark Fasheh 					 */
2232dcd0538fSMark Fasheh 					goto next_node;
2233dcd0538fSMark Fasheh 				}
2234dcd0538fSMark Fasheh 
2235dcd0538fSMark Fasheh 				*cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
2236e48edee2SMark Fasheh 				*cpos = *cpos + ocfs2_rec_clusters(el,
2237e48edee2SMark Fasheh 							   &el->l_recs[j - 1]);
2238e48edee2SMark Fasheh 				*cpos = *cpos - 1;
2239dcd0538fSMark Fasheh 				goto out;
2240dcd0538fSMark Fasheh 			}
2241dcd0538fSMark Fasheh 		}
2242dcd0538fSMark Fasheh 
2243dcd0538fSMark Fasheh 		/*
2244dcd0538fSMark Fasheh 		 * If we got here, we never found a valid node where
2245dcd0538fSMark Fasheh 		 * the tree indicated one should be.
2246dcd0538fSMark Fasheh 		 */
22477ecef14aSJoe Perches 		ocfs2_error(sb, "Invalid extent tree at extent block %llu\n",
2248dcd0538fSMark Fasheh 			    (unsigned long long)blkno);
2249dcd0538fSMark Fasheh 		ret = -EROFS;
2250dcd0538fSMark Fasheh 		goto out;
2251dcd0538fSMark Fasheh 
2252dcd0538fSMark Fasheh next_node:
2253dcd0538fSMark Fasheh 		blkno = path->p_node[i].bh->b_blocknr;
2254dcd0538fSMark Fasheh 		i--;
2255dcd0538fSMark Fasheh 	}
2256dcd0538fSMark Fasheh 
2257dcd0538fSMark Fasheh out:
2258dcd0538fSMark Fasheh 	return ret;
2259dcd0538fSMark Fasheh }
2260dcd0538fSMark Fasheh 
2261328d5752SMark Fasheh /*
2262328d5752SMark Fasheh  * Extend the transaction by enough credits to complete the rotation,
2263328d5752SMark Fasheh  * and still leave at least the original number of credits allocated
2264328d5752SMark Fasheh  * to this transaction.
2265328d5752SMark Fasheh  */
2266dcd0538fSMark Fasheh static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
2267328d5752SMark Fasheh 					   int op_credits,
2268dcd0538fSMark Fasheh 					   struct ocfs2_path *path)
2269dcd0538fSMark Fasheh {
2270c901fb00STao Ma 	int ret = 0;
2271328d5752SMark Fasheh 	int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
2272dcd0538fSMark Fasheh 
2273c901fb00STao Ma 	if (handle->h_buffer_credits < credits)
2274c18b812dSTao Ma 		ret = ocfs2_extend_trans(handle,
2275c18b812dSTao Ma 					 credits - handle->h_buffer_credits);
2276c901fb00STao Ma 
2277c18b812dSTao Ma 	return ret;
2278dcd0538fSMark Fasheh }
2279dcd0538fSMark Fasheh 
2280dcd0538fSMark Fasheh /*
2281dcd0538fSMark Fasheh  * Trap the case where we're inserting into the theoretical range past
2282dcd0538fSMark Fasheh  * the _actual_ left leaf range. Otherwise, we'll rotate a record
2283dcd0538fSMark Fasheh  * whose cpos is less than ours into the right leaf.
2284dcd0538fSMark Fasheh  *
2285dcd0538fSMark Fasheh  * It's only necessary to look at the rightmost record of the left
2286dcd0538fSMark Fasheh  * leaf because the logic that calls us should ensure that the
2287dcd0538fSMark Fasheh  * theoretical ranges in the path components above the leaves are
2288dcd0538fSMark Fasheh  * correct.
2289dcd0538fSMark Fasheh  */
2290dcd0538fSMark Fasheh static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2291dcd0538fSMark Fasheh 						 u32 insert_cpos)
2292dcd0538fSMark Fasheh {
2293dcd0538fSMark Fasheh 	struct ocfs2_extent_list *left_el;
2294dcd0538fSMark Fasheh 	struct ocfs2_extent_rec *rec;
2295dcd0538fSMark Fasheh 	int next_free;
2296dcd0538fSMark Fasheh 
2297dcd0538fSMark Fasheh 	left_el = path_leaf_el(left_path);
2298dcd0538fSMark Fasheh 	next_free = le16_to_cpu(left_el->l_next_free_rec);
2299dcd0538fSMark Fasheh 	rec = &left_el->l_recs[next_free - 1];
2300dcd0538fSMark Fasheh 
2301dcd0538fSMark Fasheh 	if (insert_cpos > le32_to_cpu(rec->e_cpos))
2302dcd0538fSMark Fasheh 		return 1;
2303dcd0538fSMark Fasheh 	return 0;
2304dcd0538fSMark Fasheh }
2305dcd0538fSMark Fasheh 
2306328d5752SMark Fasheh static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2307328d5752SMark Fasheh {
2308328d5752SMark Fasheh 	int next_free = le16_to_cpu(el->l_next_free_rec);
2309328d5752SMark Fasheh 	unsigned int range;
2310328d5752SMark Fasheh 	struct ocfs2_extent_rec *rec;
2311328d5752SMark Fasheh 
2312328d5752SMark Fasheh 	if (next_free == 0)
2313328d5752SMark Fasheh 		return 0;
2314328d5752SMark Fasheh 
2315328d5752SMark Fasheh 	rec = &el->l_recs[0];
2316328d5752SMark Fasheh 	if (ocfs2_is_empty_extent(rec)) {
2317328d5752SMark Fasheh 		/* Empty list. */
2318328d5752SMark Fasheh 		if (next_free == 1)
2319328d5752SMark Fasheh 			return 0;
2320328d5752SMark Fasheh 		rec = &el->l_recs[1];
2321328d5752SMark Fasheh 	}
2322328d5752SMark Fasheh 
2323328d5752SMark Fasheh 	range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2324328d5752SMark Fasheh 	if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2325328d5752SMark Fasheh 		return 1;
2326328d5752SMark Fasheh 	return 0;
2327328d5752SMark Fasheh }
2328328d5752SMark Fasheh 
2329dcd0538fSMark Fasheh /*
2330dcd0538fSMark Fasheh  * Rotate all the records in a btree right one record, starting at insert_cpos.
2331dcd0538fSMark Fasheh  *
2332dcd0538fSMark Fasheh  * The path to the rightmost leaf should be passed in.
2333dcd0538fSMark Fasheh  *
2334dcd0538fSMark Fasheh  * The array is assumed to be large enough to hold an entire path (tree depth).
2335dcd0538fSMark Fasheh  *
2336af901ca1SAndré Goddard Rosa  * Upon successful return from this function:
2337dcd0538fSMark Fasheh  *
2338dcd0538fSMark Fasheh  * - The 'right_path' array will contain a path to the leaf block
2339dcd0538fSMark Fasheh  *   whose range contains e_cpos.
2340dcd0538fSMark Fasheh  * - That leaf block will have a single empty extent in list index 0.
2341dcd0538fSMark Fasheh  * - In the case that the rotation requires a post-insert update,
2342dcd0538fSMark Fasheh  *   *ret_left_path will contain a valid path which can be passed to
2343dcd0538fSMark Fasheh  *   ocfs2_insert_path().
2344dcd0538fSMark Fasheh  */
23451bbf0b8dSJoel Becker static int ocfs2_rotate_tree_right(handle_t *handle,
23465c601abaSJoel Becker 				   struct ocfs2_extent_tree *et,
2347328d5752SMark Fasheh 				   enum ocfs2_split_type split,
2348dcd0538fSMark Fasheh 				   u32 insert_cpos,
2349dcd0538fSMark Fasheh 				   struct ocfs2_path *right_path,
2350dcd0538fSMark Fasheh 				   struct ocfs2_path **ret_left_path)
2351dcd0538fSMark Fasheh {
2352328d5752SMark Fasheh 	int ret, start, orig_credits = handle->h_buffer_credits;
2353dcd0538fSMark Fasheh 	u32 cpos;
2354dcd0538fSMark Fasheh 	struct ocfs2_path *left_path = NULL;
23555c601abaSJoel Becker 	struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
2356dcd0538fSMark Fasheh 
2357dcd0538fSMark Fasheh 	*ret_left_path = NULL;
2358dcd0538fSMark Fasheh 
2359ffdd7a54SJoel Becker 	left_path = ocfs2_new_path_from_path(right_path);
2360dcd0538fSMark Fasheh 	if (!left_path) {
2361dcd0538fSMark Fasheh 		ret = -ENOMEM;
2362dcd0538fSMark Fasheh 		mlog_errno(ret);
2363dcd0538fSMark Fasheh 		goto out;
2364dcd0538fSMark Fasheh 	}
2365dcd0538fSMark Fasheh 
23665c601abaSJoel Becker 	ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
2367dcd0538fSMark Fasheh 	if (ret) {
2368dcd0538fSMark Fasheh 		mlog_errno(ret);
2369dcd0538fSMark Fasheh 		goto out;
2370dcd0538fSMark Fasheh 	}
2371dcd0538fSMark Fasheh 
2372a09d09b8STao Ma 	trace_ocfs2_rotate_tree_right(
2373a09d09b8STao Ma 		(unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2374a09d09b8STao Ma 		insert_cpos, cpos);
2375dcd0538fSMark Fasheh 
2376dcd0538fSMark Fasheh 	/*
2377dcd0538fSMark Fasheh 	 * What we want to do here is:
2378dcd0538fSMark Fasheh 	 *
2379dcd0538fSMark Fasheh 	 * 1) Start with the rightmost path.
2380dcd0538fSMark Fasheh 	 *
2381dcd0538fSMark Fasheh 	 * 2) Determine a path to the leaf block directly to the left
2382dcd0538fSMark Fasheh 	 *    of that leaf.
2383dcd0538fSMark Fasheh 	 *
2384dcd0538fSMark Fasheh 	 * 3) Determine the 'subtree root' - the lowest level tree node
2385dcd0538fSMark Fasheh 	 *    which contains a path to both leaves.
2386dcd0538fSMark Fasheh 	 *
2387dcd0538fSMark Fasheh 	 * 4) Rotate the subtree.
2388dcd0538fSMark Fasheh 	 *
2389dcd0538fSMark Fasheh 	 * 5) Find the next subtree by considering the left path to be
2390dcd0538fSMark Fasheh 	 *    the new right path.
2391dcd0538fSMark Fasheh 	 *
2392dcd0538fSMark Fasheh 	 * The check at the top of this while loop also accepts
2393dcd0538fSMark Fasheh 	 * insert_cpos == cpos because cpos is only a _theoretical_
2394dcd0538fSMark Fasheh 	 * value to get us the left path - insert_cpos might very well
2395dcd0538fSMark Fasheh 	 * be filling that hole.
2396dcd0538fSMark Fasheh 	 *
2397dcd0538fSMark Fasheh 	 * Stop at a cpos of '0' because we either started at the
2398dcd0538fSMark Fasheh 	 * leftmost branch (i.e., a tree with one branch and a
2399dcd0538fSMark Fasheh 	 * rotation inside of it), or we've gone as far as we can in
2400dcd0538fSMark Fasheh 	 * rotating subtrees.
2401dcd0538fSMark Fasheh 	 */
2402dcd0538fSMark Fasheh 	while (cpos && insert_cpos <= cpos) {
2403a09d09b8STao Ma 		trace_ocfs2_rotate_tree_right(
2404a09d09b8STao Ma 			(unsigned long long)
2405a09d09b8STao Ma 			ocfs2_metadata_cache_owner(et->et_ci),
2406dcd0538fSMark Fasheh 			insert_cpos, cpos);
2407dcd0538fSMark Fasheh 
24085c601abaSJoel Becker 		ret = ocfs2_find_path(et->et_ci, left_path, cpos);
2409dcd0538fSMark Fasheh 		if (ret) {
2410dcd0538fSMark Fasheh 			mlog_errno(ret);
2411dcd0538fSMark Fasheh 			goto out;
2412dcd0538fSMark Fasheh 		}
2413dcd0538fSMark Fasheh 
2414dcd0538fSMark Fasheh 		mlog_bug_on_msg(path_leaf_bh(left_path) ==
2415dcd0538fSMark Fasheh 				path_leaf_bh(right_path),
24165c601abaSJoel Becker 				"Owner %llu: error during insert of %u "
2417dcd0538fSMark Fasheh 				"(left path cpos %u) results in two identical "
2418dcd0538fSMark Fasheh 				"paths ending at %llu\n",
24195c601abaSJoel Becker 				(unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
24205c601abaSJoel Becker 				insert_cpos, cpos,
2421dcd0538fSMark Fasheh 				(unsigned long long)
2422dcd0538fSMark Fasheh 				path_leaf_bh(left_path)->b_blocknr);
2423dcd0538fSMark Fasheh 
2424328d5752SMark Fasheh 		if (split == SPLIT_NONE &&
2425328d5752SMark Fasheh 		    ocfs2_rotate_requires_path_adjustment(left_path,
2426dcd0538fSMark Fasheh 							  insert_cpos)) {
2427dcd0538fSMark Fasheh 
2428dcd0538fSMark Fasheh 			/*
2429dcd0538fSMark Fasheh 			 * We've rotated the tree as much as we
2430dcd0538fSMark Fasheh 			 * should. The rest is up to
2431dcd0538fSMark Fasheh 			 * ocfs2_insert_path() to complete, after the
2432dcd0538fSMark Fasheh 			 * record insertion. We indicate this
2433dcd0538fSMark Fasheh 			 * situation by returning the left path.
2434dcd0538fSMark Fasheh 			 *
2435dcd0538fSMark Fasheh 			 * The reason we don't adjust the records here
2436dcd0538fSMark Fasheh 			 * before the record insert is that an error
2437dcd0538fSMark Fasheh 			 * later might break the rule where a parent
2438dcd0538fSMark Fasheh 			 * record e_cpos will reflect the actual
2439dcd0538fSMark Fasheh 			 * e_cpos of the 1st nonempty record of the
2440dcd0538fSMark Fasheh 			 * child list.
2441dcd0538fSMark Fasheh 			 */
2442dcd0538fSMark Fasheh 			*ret_left_path = left_path;
2443dcd0538fSMark Fasheh 			goto out_ret_path;
2444dcd0538fSMark Fasheh 		}
2445dcd0538fSMark Fasheh 
24467dc02805SJoel Becker 		start = ocfs2_find_subtree_root(et, left_path, right_path);
2447dcd0538fSMark Fasheh 
2448a09d09b8STao Ma 		trace_ocfs2_rotate_subtree(start,
2449a09d09b8STao Ma 			(unsigned long long)
2450a09d09b8STao Ma 			right_path->p_node[start].bh->b_blocknr,
2451dcd0538fSMark Fasheh 			right_path->p_tree_depth);
2452dcd0538fSMark Fasheh 
2453dcd0538fSMark Fasheh 		ret = ocfs2_extend_rotate_transaction(handle, start,
2454328d5752SMark Fasheh 						      orig_credits, right_path);
2455dcd0538fSMark Fasheh 		if (ret) {
2456dcd0538fSMark Fasheh 			mlog_errno(ret);
2457dcd0538fSMark Fasheh 			goto out;
2458dcd0538fSMark Fasheh 		}
2459dcd0538fSMark Fasheh 
24605c601abaSJoel Becker 		ret = ocfs2_rotate_subtree_right(handle, et, left_path,
2461dcd0538fSMark Fasheh 						 right_path, start);
2462dcd0538fSMark Fasheh 		if (ret) {
2463dcd0538fSMark Fasheh 			mlog_errno(ret);
2464dcd0538fSMark Fasheh 			goto out;
2465dcd0538fSMark Fasheh 		}
2466dcd0538fSMark Fasheh 
2467328d5752SMark Fasheh 		if (split != SPLIT_NONE &&
2468328d5752SMark Fasheh 		    ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2469328d5752SMark Fasheh 						insert_cpos)) {
2470328d5752SMark Fasheh 			/*
2471328d5752SMark Fasheh 			 * A rotate moves the rightmost left leaf
2472328d5752SMark Fasheh 			 * record over to the leftmost right leaf
2473328d5752SMark Fasheh 			 * slot. If we're doing an extent split
2474328d5752SMark Fasheh 			 * instead of a real insert, then we have to
2475328d5752SMark Fasheh 			 * check that the extent to be split wasn't
2476328d5752SMark Fasheh 			 * just moved over. If it was, then we can
2477328d5752SMark Fasheh 			 * exit here, passing left_path back -
2478328d5752SMark Fasheh 			 * ocfs2_split_extent() is smart enough to
2479328d5752SMark Fasheh 			 * search both leaves.
2480328d5752SMark Fasheh 			 */
2481328d5752SMark Fasheh 			*ret_left_path = left_path;
2482328d5752SMark Fasheh 			goto out_ret_path;
2483328d5752SMark Fasheh 		}
2484328d5752SMark Fasheh 
2485dcd0538fSMark Fasheh 		/*
2486dcd0538fSMark Fasheh 		 * There is no need to re-read the next right path
2487dcd0538fSMark Fasheh 		 * as we know that it'll be our current left
2488dcd0538fSMark Fasheh 		 * path. Optimize by copying values instead.
2489dcd0538fSMark Fasheh 		 */
2490dcd0538fSMark Fasheh 		ocfs2_mv_path(right_path, left_path);
2491dcd0538fSMark Fasheh 
24925c601abaSJoel Becker 		ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
2493dcd0538fSMark Fasheh 		if (ret) {
2494dcd0538fSMark Fasheh 			mlog_errno(ret);
2495dcd0538fSMark Fasheh 			goto out;
2496dcd0538fSMark Fasheh 		}
2497dcd0538fSMark Fasheh 	}
2498dcd0538fSMark Fasheh 
2499dcd0538fSMark Fasheh out:
2500dcd0538fSMark Fasheh 	ocfs2_free_path(left_path);
2501dcd0538fSMark Fasheh 
2502dcd0538fSMark Fasheh out_ret_path:
2503dcd0538fSMark Fasheh 	return ret;
2504dcd0538fSMark Fasheh }
2505dcd0538fSMark Fasheh 
250609106baeSJoel Becker static int ocfs2_update_edge_lengths(handle_t *handle,
250709106baeSJoel Becker 				     struct ocfs2_extent_tree *et,
2508964f14a0SJun Piao 				     struct ocfs2_path *path)
2509328d5752SMark Fasheh {
25103c5e1068STao Ma 	int i, idx, ret;
2511328d5752SMark Fasheh 	struct ocfs2_extent_rec *rec;
2512328d5752SMark Fasheh 	struct ocfs2_extent_list *el;
2513328d5752SMark Fasheh 	struct ocfs2_extent_block *eb;
2514328d5752SMark Fasheh 	u32 range;
2515328d5752SMark Fasheh 
251609106baeSJoel Becker 	ret = ocfs2_journal_access_path(et->et_ci, handle, path);
25173c5e1068STao Ma 	if (ret) {
25183c5e1068STao Ma 		mlog_errno(ret);
25193c5e1068STao Ma 		goto out;
25203c5e1068STao Ma 	}
25213c5e1068STao Ma 
2522328d5752SMark Fasheh 	/* Path should always be rightmost. */
2523328d5752SMark Fasheh 	eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2524328d5752SMark Fasheh 	BUG_ON(eb->h_next_leaf_blk != 0ULL);
2525328d5752SMark Fasheh 
2526328d5752SMark Fasheh 	el = &eb->h_list;
2527328d5752SMark Fasheh 	BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2528328d5752SMark Fasheh 	idx = le16_to_cpu(el->l_next_free_rec) - 1;
2529328d5752SMark Fasheh 	rec = &el->l_recs[idx];
2530328d5752SMark Fasheh 	range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2531328d5752SMark Fasheh 
2532328d5752SMark Fasheh 	for (i = 0; i < path->p_tree_depth; i++) {
2533328d5752SMark Fasheh 		el = path->p_node[i].el;
2534328d5752SMark Fasheh 		idx = le16_to_cpu(el->l_next_free_rec) - 1;
2535328d5752SMark Fasheh 		rec = &el->l_recs[idx];
2536328d5752SMark Fasheh 
2537328d5752SMark Fasheh 		rec->e_int_clusters = cpu_to_le32(range);
2538328d5752SMark Fasheh 		le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2539328d5752SMark Fasheh 
2540328d5752SMark Fasheh 		ocfs2_journal_dirty(handle, path->p_node[i].bh);
2541328d5752SMark Fasheh 	}
25423c5e1068STao Ma out:
25433c5e1068STao Ma 	return ret;
2544328d5752SMark Fasheh }
2545328d5752SMark Fasheh 
25466641b0ceSJoel Becker static void ocfs2_unlink_path(handle_t *handle,
25476641b0ceSJoel Becker 			      struct ocfs2_extent_tree *et,
2548328d5752SMark Fasheh 			      struct ocfs2_cached_dealloc_ctxt *dealloc,
2549328d5752SMark Fasheh 			      struct ocfs2_path *path, int unlink_start)
2550328d5752SMark Fasheh {
2551328d5752SMark Fasheh 	int ret, i;
2552328d5752SMark Fasheh 	struct ocfs2_extent_block *eb;
2553328d5752SMark Fasheh 	struct ocfs2_extent_list *el;
2554328d5752SMark Fasheh 	struct buffer_head *bh;
2555328d5752SMark Fasheh 
2556328d5752SMark Fasheh 	for(i = unlink_start; i < path_num_items(path); i++) {
2557328d5752SMark Fasheh 		bh = path->p_node[i].bh;
2558328d5752SMark Fasheh 
2559328d5752SMark Fasheh 		eb = (struct ocfs2_extent_block *)bh->b_data;
2560328d5752SMark Fasheh 		/*
2561328d5752SMark Fasheh 		 * Not all nodes might have had their final count
2562328d5752SMark Fasheh 		 * decremented by the caller - handle this here.
2563328d5752SMark Fasheh 		 */
2564328d5752SMark Fasheh 		el = &eb->h_list;
2565328d5752SMark Fasheh 		if (le16_to_cpu(el->l_next_free_rec) > 1) {
2566328d5752SMark Fasheh 			mlog(ML_ERROR,
2567328d5752SMark Fasheh 			     "Inode %llu, attempted to remove extent block "
2568328d5752SMark Fasheh 			     "%llu with %u records\n",
25696641b0ceSJoel Becker 			     (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2570328d5752SMark Fasheh 			     (unsigned long long)le64_to_cpu(eb->h_blkno),
2571328d5752SMark Fasheh 			     le16_to_cpu(el->l_next_free_rec));
2572328d5752SMark Fasheh 
2573328d5752SMark Fasheh 			ocfs2_journal_dirty(handle, bh);
25746641b0ceSJoel Becker 			ocfs2_remove_from_cache(et->et_ci, bh);
2575328d5752SMark Fasheh 			continue;
2576328d5752SMark Fasheh 		}
2577328d5752SMark Fasheh 
2578328d5752SMark Fasheh 		el->l_next_free_rec = 0;
2579328d5752SMark Fasheh 		memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2580328d5752SMark Fasheh 
2581328d5752SMark Fasheh 		ocfs2_journal_dirty(handle, bh);
2582328d5752SMark Fasheh 
2583328d5752SMark Fasheh 		ret = ocfs2_cache_extent_block_free(dealloc, eb);
2584328d5752SMark Fasheh 		if (ret)
2585328d5752SMark Fasheh 			mlog_errno(ret);
2586328d5752SMark Fasheh 
25876641b0ceSJoel Becker 		ocfs2_remove_from_cache(et->et_ci, bh);
2588328d5752SMark Fasheh 	}
2589328d5752SMark Fasheh }
2590328d5752SMark Fasheh 
25916641b0ceSJoel Becker static void ocfs2_unlink_subtree(handle_t *handle,
25926641b0ceSJoel Becker 				 struct ocfs2_extent_tree *et,
2593328d5752SMark Fasheh 				 struct ocfs2_path *left_path,
2594328d5752SMark Fasheh 				 struct ocfs2_path *right_path,
2595328d5752SMark Fasheh 				 int subtree_index,
2596328d5752SMark Fasheh 				 struct ocfs2_cached_dealloc_ctxt *dealloc)
2597328d5752SMark Fasheh {
2598328d5752SMark Fasheh 	int i;
2599328d5752SMark Fasheh 	struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2600328d5752SMark Fasheh 	struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2601328d5752SMark Fasheh 	struct ocfs2_extent_block *eb;
2602328d5752SMark Fasheh 
2603328d5752SMark Fasheh 	eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2604328d5752SMark Fasheh 
2605328d5752SMark Fasheh 	for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2606328d5752SMark Fasheh 		if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2607328d5752SMark Fasheh 			break;
2608328d5752SMark Fasheh 
2609328d5752SMark Fasheh 	BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2610328d5752SMark Fasheh 
2611328d5752SMark Fasheh 	memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2612328d5752SMark Fasheh 	le16_add_cpu(&root_el->l_next_free_rec, -1);
2613328d5752SMark Fasheh 
2614328d5752SMark Fasheh 	eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2615328d5752SMark Fasheh 	eb->h_next_leaf_blk = 0;
2616328d5752SMark Fasheh 
2617328d5752SMark Fasheh 	ocfs2_journal_dirty(handle, root_bh);
2618328d5752SMark Fasheh 	ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2619328d5752SMark Fasheh 
26206641b0ceSJoel Becker 	ocfs2_unlink_path(handle, et, dealloc, right_path,
2621328d5752SMark Fasheh 			  subtree_index + 1);
2622328d5752SMark Fasheh }
2623328d5752SMark Fasheh 
26241e2dd63fSJoel Becker static int ocfs2_rotate_subtree_left(handle_t *handle,
26251e2dd63fSJoel Becker 				     struct ocfs2_extent_tree *et,
2626328d5752SMark Fasheh 				     struct ocfs2_path *left_path,
2627328d5752SMark Fasheh 				     struct ocfs2_path *right_path,
2628328d5752SMark Fasheh 				     int subtree_index,
2629328d5752SMark Fasheh 				     struct ocfs2_cached_dealloc_ctxt *dealloc,
26301e2dd63fSJoel Becker 				     int *deleted)
2631328d5752SMark Fasheh {
2632328d5752SMark Fasheh 	int ret, i, del_right_subtree = 0, right_has_empty = 0;
2633e7d4cb6bSTao Ma 	struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
2634328d5752SMark Fasheh 	struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2635328d5752SMark Fasheh 	struct ocfs2_extent_block *eb;
2636328d5752SMark Fasheh 
2637328d5752SMark Fasheh 	*deleted = 0;
2638328d5752SMark Fasheh 
2639328d5752SMark Fasheh 	right_leaf_el = path_leaf_el(right_path);
2640328d5752SMark Fasheh 	left_leaf_el = path_leaf_el(left_path);
2641328d5752SMark Fasheh 	root_bh = left_path->p_node[subtree_index].bh;
2642328d5752SMark Fasheh 	BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2643328d5752SMark Fasheh 
2644328d5752SMark Fasheh 	if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2645328d5752SMark Fasheh 		return 0;
2646328d5752SMark Fasheh 
2647328d5752SMark Fasheh 	eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2648328d5752SMark Fasheh 	if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2649328d5752SMark Fasheh 		/*
2650328d5752SMark Fasheh 		 * It's legal for us to proceed if the right leaf is
2651328d5752SMark Fasheh 		 * the rightmost one and it has an empty extent. There
2652328d5752SMark Fasheh 		 * are two cases to handle - whether the leaf will be
2653328d5752SMark Fasheh 		 * empty after removal or not. If the leaf isn't empty
2654328d5752SMark Fasheh 		 * then just remove the empty extent up front. The
2655328d5752SMark Fasheh 		 * next block will handle empty leaves by flagging
2656328d5752SMark Fasheh 		 * them for unlink.
2657328d5752SMark Fasheh 		 *
2658328d5752SMark Fasheh 		 * Non rightmost leaves will throw -EAGAIN and the
2659328d5752SMark Fasheh 		 * caller can manually move the subtree and retry.
2660328d5752SMark Fasheh 		 */
2661328d5752SMark Fasheh 
2662328d5752SMark Fasheh 		if (eb->h_next_leaf_blk != 0ULL)
2663328d5752SMark Fasheh 			return -EAGAIN;
2664328d5752SMark Fasheh 
2665328d5752SMark Fasheh 		if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
26661e2dd63fSJoel Becker 			ret = ocfs2_journal_access_eb(handle, et->et_ci,
2667328d5752SMark Fasheh 						      path_leaf_bh(right_path),
2668328d5752SMark Fasheh 						      OCFS2_JOURNAL_ACCESS_WRITE);
2669328d5752SMark Fasheh 			if (ret) {
2670328d5752SMark Fasheh 				mlog_errno(ret);
2671328d5752SMark Fasheh 				goto out;
2672328d5752SMark Fasheh 			}
2673328d5752SMark Fasheh 
2674328d5752SMark Fasheh 			ocfs2_remove_empty_extent(right_leaf_el);
2675328d5752SMark Fasheh 		} else
2676328d5752SMark Fasheh 			right_has_empty = 1;
2677328d5752SMark Fasheh 	}
2678328d5752SMark Fasheh 
2679328d5752SMark Fasheh 	if (eb->h_next_leaf_blk == 0ULL &&
2680328d5752SMark Fasheh 	    le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2681328d5752SMark Fasheh 		/*
2682328d5752SMark Fasheh 		 * We have to update i_last_eb_blk during the meta
2683328d5752SMark Fasheh 		 * data delete.
2684328d5752SMark Fasheh 		 */
2685d9a0a1f8SJoel Becker 		ret = ocfs2_et_root_journal_access(handle, et,
2686328d5752SMark Fasheh 						   OCFS2_JOURNAL_ACCESS_WRITE);
2687328d5752SMark Fasheh 		if (ret) {
2688328d5752SMark Fasheh 			mlog_errno(ret);
2689328d5752SMark Fasheh 			goto out;
2690328d5752SMark Fasheh 		}
2691328d5752SMark Fasheh 
2692328d5752SMark Fasheh 		del_right_subtree = 1;
2693328d5752SMark Fasheh 	}
2694328d5752SMark Fasheh 
2695328d5752SMark Fasheh 	/*
2696328d5752SMark Fasheh 	 * Getting here with an empty extent in the right path implies
2697328d5752SMark Fasheh 	 * that it's the rightmost path and will be deleted.
2698328d5752SMark Fasheh 	 */
2699328d5752SMark Fasheh 	BUG_ON(right_has_empty && !del_right_subtree);
2700328d5752SMark Fasheh 
27011e2dd63fSJoel Becker 	ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
270213723d00SJoel Becker 					   subtree_index);
2703328d5752SMark Fasheh 	if (ret) {
2704328d5752SMark Fasheh 		mlog_errno(ret);
2705328d5752SMark Fasheh 		goto out;
2706328d5752SMark Fasheh 	}
2707328d5752SMark Fasheh 
2708328d5752SMark Fasheh 	for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
27091e2dd63fSJoel Becker 		ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
271013723d00SJoel Becker 						   right_path, i);
2711328d5752SMark Fasheh 		if (ret) {
2712328d5752SMark Fasheh 			mlog_errno(ret);
2713328d5752SMark Fasheh 			goto out;
2714328d5752SMark Fasheh 		}
2715328d5752SMark Fasheh 
27161e2dd63fSJoel Becker 		ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
271713723d00SJoel Becker 						   left_path, i);
2718328d5752SMark Fasheh 		if (ret) {
2719328d5752SMark Fasheh 			mlog_errno(ret);
2720328d5752SMark Fasheh 			goto out;
2721328d5752SMark Fasheh 		}
2722328d5752SMark Fasheh 	}
2723328d5752SMark Fasheh 
2724328d5752SMark Fasheh 	if (!right_has_empty) {
2725328d5752SMark Fasheh 		/*
2726328d5752SMark Fasheh 		 * Only do this if we're moving a real
2727328d5752SMark Fasheh 		 * record. Otherwise, the action is delayed until
2728328d5752SMark Fasheh 		 * after removal of the right path in which case we
2729328d5752SMark Fasheh 		 * can do a simple shift to remove the empty extent.
2730328d5752SMark Fasheh 		 */
2731328d5752SMark Fasheh 		ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2732328d5752SMark Fasheh 		memset(&right_leaf_el->l_recs[0], 0,
2733328d5752SMark Fasheh 		       sizeof(struct ocfs2_extent_rec));
2734328d5752SMark Fasheh 	}
2735328d5752SMark Fasheh 	if (eb->h_next_leaf_blk == 0ULL) {
2736328d5752SMark Fasheh 		/*
2737328d5752SMark Fasheh 		 * Move recs over to get rid of empty extent, decrease
2738328d5752SMark Fasheh 		 * next_free. This is allowed to remove the last
2739328d5752SMark Fasheh 		 * extent in our leaf (setting l_next_free_rec to
2740328d5752SMark Fasheh 		 * zero) - the delete code below won't care.
2741328d5752SMark Fasheh 		 */
2742328d5752SMark Fasheh 		ocfs2_remove_empty_extent(right_leaf_el);
2743328d5752SMark Fasheh 	}
2744328d5752SMark Fasheh 
2745ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2746ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2747328d5752SMark Fasheh 
2748328d5752SMark Fasheh 	if (del_right_subtree) {
27496641b0ceSJoel Becker 		ocfs2_unlink_subtree(handle, et, left_path, right_path,
2750328d5752SMark Fasheh 				     subtree_index, dealloc);
2751964f14a0SJun Piao 		ret = ocfs2_update_edge_lengths(handle, et, left_path);
27523c5e1068STao Ma 		if (ret) {
27533c5e1068STao Ma 			mlog_errno(ret);
27543c5e1068STao Ma 			goto out;
27553c5e1068STao Ma 		}
2756328d5752SMark Fasheh 
2757328d5752SMark Fasheh 		eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
275835dc0aa3SJoel Becker 		ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
2759328d5752SMark Fasheh 
2760328d5752SMark Fasheh 		/*
2761328d5752SMark Fasheh 		 * Removal of the extent in the left leaf was skipped
2762328d5752SMark Fasheh 		 * above so we could delete the right path
2763328d5752SMark Fasheh 		 * 1st.
2764328d5752SMark Fasheh 		 */
2765328d5752SMark Fasheh 		if (right_has_empty)
2766328d5752SMark Fasheh 			ocfs2_remove_empty_extent(left_leaf_el);
2767328d5752SMark Fasheh 
2768ec20cec7SJoel Becker 		ocfs2_journal_dirty(handle, et_root_bh);
2769328d5752SMark Fasheh 
2770328d5752SMark Fasheh 		*deleted = 1;
2771328d5752SMark Fasheh 	} else
27724619c73eSJoel Becker 		ocfs2_complete_edge_insert(handle, left_path, right_path,
2773328d5752SMark Fasheh 					   subtree_index);
2774328d5752SMark Fasheh 
2775328d5752SMark Fasheh out:
2776328d5752SMark Fasheh 	return ret;
2777328d5752SMark Fasheh }
2778328d5752SMark Fasheh 
2779328d5752SMark Fasheh /*
2780328d5752SMark Fasheh  * Given a full path, determine what cpos value would return us a path
2781328d5752SMark Fasheh  * containing the leaf immediately to the right of the current one.
2782328d5752SMark Fasheh  *
2783328d5752SMark Fasheh  * Will return zero if the path passed in is already the rightmost path.
2784328d5752SMark Fasheh  *
2785328d5752SMark Fasheh  * This looks similar, but is subtly different to
2786328d5752SMark Fasheh  * ocfs2_find_cpos_for_left_leaf().
2787328d5752SMark Fasheh  */
278838a04e43STao Ma int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2789328d5752SMark Fasheh 				   struct ocfs2_path *path, u32 *cpos)
2790328d5752SMark Fasheh {
2791328d5752SMark Fasheh 	int i, j, ret = 0;
2792328d5752SMark Fasheh 	u64 blkno;
2793328d5752SMark Fasheh 	struct ocfs2_extent_list *el;
2794328d5752SMark Fasheh 
2795328d5752SMark Fasheh 	*cpos = 0;
2796328d5752SMark Fasheh 
2797328d5752SMark Fasheh 	if (path->p_tree_depth == 0)
2798328d5752SMark Fasheh 		return 0;
2799328d5752SMark Fasheh 
2800328d5752SMark Fasheh 	blkno = path_leaf_bh(path)->b_blocknr;
2801328d5752SMark Fasheh 
2802328d5752SMark Fasheh 	/* Start at the tree node just above the leaf and work our way up. */
2803328d5752SMark Fasheh 	i = path->p_tree_depth - 1;
2804328d5752SMark Fasheh 	while (i >= 0) {
2805328d5752SMark Fasheh 		int next_free;
2806328d5752SMark Fasheh 
2807328d5752SMark Fasheh 		el = path->p_node[i].el;
2808328d5752SMark Fasheh 
2809328d5752SMark Fasheh 		/*
2810328d5752SMark Fasheh 		 * Find the extent record just after the one in our
2811328d5752SMark Fasheh 		 * path.
2812328d5752SMark Fasheh 		 */
2813328d5752SMark Fasheh 		next_free = le16_to_cpu(el->l_next_free_rec);
2814328d5752SMark Fasheh 		for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2815328d5752SMark Fasheh 			if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2816328d5752SMark Fasheh 				if (j == (next_free - 1)) {
2817328d5752SMark Fasheh 					if (i == 0) {
2818328d5752SMark Fasheh 						/*
2819328d5752SMark Fasheh 						 * We've determined that the
2820328d5752SMark Fasheh 						 * path specified is already
2821328d5752SMark Fasheh 						 * the rightmost one - return a
2822328d5752SMark Fasheh 						 * cpos of zero.
2823328d5752SMark Fasheh 						 */
2824328d5752SMark Fasheh 						goto out;
2825328d5752SMark Fasheh 					}
2826328d5752SMark Fasheh 					/*
2827328d5752SMark Fasheh 					 * The rightmost record points to our
2828328d5752SMark Fasheh 					 * leaf - we need to travel up the
2829328d5752SMark Fasheh 					 * tree one level.
2830328d5752SMark Fasheh 					 */
2831328d5752SMark Fasheh 					goto next_node;
2832328d5752SMark Fasheh 				}
2833328d5752SMark Fasheh 
2834328d5752SMark Fasheh 				*cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2835328d5752SMark Fasheh 				goto out;
2836328d5752SMark Fasheh 			}
2837328d5752SMark Fasheh 		}
2838328d5752SMark Fasheh 
2839328d5752SMark Fasheh 		/*
2840328d5752SMark Fasheh 		 * If we got here, we never found a valid node where
2841328d5752SMark Fasheh 		 * the tree indicated one should be.
2842328d5752SMark Fasheh 		 */
28437ecef14aSJoe Perches 		ocfs2_error(sb, "Invalid extent tree at extent block %llu\n",
2844328d5752SMark Fasheh 			    (unsigned long long)blkno);
2845328d5752SMark Fasheh 		ret = -EROFS;
2846328d5752SMark Fasheh 		goto out;
2847328d5752SMark Fasheh 
2848328d5752SMark Fasheh next_node:
2849328d5752SMark Fasheh 		blkno = path->p_node[i].bh->b_blocknr;
2850328d5752SMark Fasheh 		i--;
2851328d5752SMark Fasheh 	}
2852328d5752SMark Fasheh 
2853328d5752SMark Fasheh out:
2854328d5752SMark Fasheh 	return ret;
2855328d5752SMark Fasheh }
2856328d5752SMark Fasheh 
285770f18c08SJoel Becker static int ocfs2_rotate_rightmost_leaf_left(handle_t *handle,
285870f18c08SJoel Becker 					    struct ocfs2_extent_tree *et,
285913723d00SJoel Becker 					    struct ocfs2_path *path)
2860328d5752SMark Fasheh {
2861328d5752SMark Fasheh 	int ret;
286213723d00SJoel Becker 	struct buffer_head *bh = path_leaf_bh(path);
286313723d00SJoel Becker 	struct ocfs2_extent_list *el = path_leaf_el(path);
2864328d5752SMark Fasheh 
2865328d5752SMark Fasheh 	if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2866328d5752SMark Fasheh 		return 0;
2867328d5752SMark Fasheh 
286870f18c08SJoel Becker 	ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
286913723d00SJoel Becker 					   path_num_items(path) - 1);
2870328d5752SMark Fasheh 	if (ret) {
2871328d5752SMark Fasheh 		mlog_errno(ret);
2872328d5752SMark Fasheh 		goto out;
2873328d5752SMark Fasheh 	}
2874328d5752SMark Fasheh 
2875328d5752SMark Fasheh 	ocfs2_remove_empty_extent(el);
2876ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, bh);
2877328d5752SMark Fasheh 
2878328d5752SMark Fasheh out:
2879328d5752SMark Fasheh 	return ret;
2880328d5752SMark Fasheh }
2881328d5752SMark Fasheh 
2882e46f74dcSJoel Becker static int __ocfs2_rotate_tree_left(handle_t *handle,
2883e46f74dcSJoel Becker 				    struct ocfs2_extent_tree *et,
2884e46f74dcSJoel Becker 				    int orig_credits,
2885328d5752SMark Fasheh 				    struct ocfs2_path *path,
2886328d5752SMark Fasheh 				    struct ocfs2_cached_dealloc_ctxt *dealloc,
2887e46f74dcSJoel Becker 				    struct ocfs2_path **empty_extent_path)
2888328d5752SMark Fasheh {
2889328d5752SMark Fasheh 	int ret, subtree_root, deleted;
2890328d5752SMark Fasheh 	u32 right_cpos;
2891328d5752SMark Fasheh 	struct ocfs2_path *left_path = NULL;
2892328d5752SMark Fasheh 	struct ocfs2_path *right_path = NULL;
2893e46f74dcSJoel Becker 	struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
2894328d5752SMark Fasheh 
2895099768b0SXue jiufei 	if (!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])))
2896099768b0SXue jiufei 		return 0;
2897328d5752SMark Fasheh 
2898328d5752SMark Fasheh 	*empty_extent_path = NULL;
2899328d5752SMark Fasheh 
2900e46f74dcSJoel Becker 	ret = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
2901328d5752SMark Fasheh 	if (ret) {
2902328d5752SMark Fasheh 		mlog_errno(ret);
2903328d5752SMark Fasheh 		goto out;
2904328d5752SMark Fasheh 	}
2905328d5752SMark Fasheh 
2906ffdd7a54SJoel Becker 	left_path = ocfs2_new_path_from_path(path);
2907328d5752SMark Fasheh 	if (!left_path) {
2908328d5752SMark Fasheh 		ret = -ENOMEM;
2909328d5752SMark Fasheh 		mlog_errno(ret);
2910328d5752SMark Fasheh 		goto out;
2911328d5752SMark Fasheh 	}
2912328d5752SMark Fasheh 
2913328d5752SMark Fasheh 	ocfs2_cp_path(left_path, path);
2914328d5752SMark Fasheh 
2915ffdd7a54SJoel Becker 	right_path = ocfs2_new_path_from_path(path);
2916328d5752SMark Fasheh 	if (!right_path) {
2917328d5752SMark Fasheh 		ret = -ENOMEM;
2918328d5752SMark Fasheh 		mlog_errno(ret);
2919328d5752SMark Fasheh 		goto out;
2920328d5752SMark Fasheh 	}
2921328d5752SMark Fasheh 
2922328d5752SMark Fasheh 	while (right_cpos) {
2923facdb77fSJoel Becker 		ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
2924328d5752SMark Fasheh 		if (ret) {
2925328d5752SMark Fasheh 			mlog_errno(ret);
2926328d5752SMark Fasheh 			goto out;
2927328d5752SMark Fasheh 		}
2928328d5752SMark Fasheh 
29297dc02805SJoel Becker 		subtree_root = ocfs2_find_subtree_root(et, left_path,
2930328d5752SMark Fasheh 						       right_path);
2931328d5752SMark Fasheh 
2932a09d09b8STao Ma 		trace_ocfs2_rotate_subtree(subtree_root,
2933328d5752SMark Fasheh 		     (unsigned long long)
2934328d5752SMark Fasheh 		     right_path->p_node[subtree_root].bh->b_blocknr,
2935328d5752SMark Fasheh 		     right_path->p_tree_depth);
2936328d5752SMark Fasheh 
293717215989SXue jiufei 		ret = ocfs2_extend_rotate_transaction(handle, 0,
2938328d5752SMark Fasheh 						      orig_credits, left_path);
2939328d5752SMark Fasheh 		if (ret) {
2940328d5752SMark Fasheh 			mlog_errno(ret);
2941328d5752SMark Fasheh 			goto out;
2942328d5752SMark Fasheh 		}
2943328d5752SMark Fasheh 
2944e8aed345SMark Fasheh 		/*
2945e8aed345SMark Fasheh 		 * Caller might still want to make changes to the
2946e8aed345SMark Fasheh 		 * tree root, so re-add it to the journal here.
2947e8aed345SMark Fasheh 		 */
2948e46f74dcSJoel Becker 		ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
294913723d00SJoel Becker 						   left_path, 0);
2950e8aed345SMark Fasheh 		if (ret) {
2951e8aed345SMark Fasheh 			mlog_errno(ret);
2952e8aed345SMark Fasheh 			goto out;
2953e8aed345SMark Fasheh 		}
2954e8aed345SMark Fasheh 
29551e2dd63fSJoel Becker 		ret = ocfs2_rotate_subtree_left(handle, et, left_path,
2956328d5752SMark Fasheh 						right_path, subtree_root,
29571e2dd63fSJoel Becker 						dealloc, &deleted);
2958328d5752SMark Fasheh 		if (ret == -EAGAIN) {
2959328d5752SMark Fasheh 			/*
2960328d5752SMark Fasheh 			 * The rotation has to temporarily stop due to
2961328d5752SMark Fasheh 			 * the right subtree having an empty
2962328d5752SMark Fasheh 			 * extent. Pass it back to the caller for a
2963328d5752SMark Fasheh 			 * fixup.
2964328d5752SMark Fasheh 			 */
2965328d5752SMark Fasheh 			*empty_extent_path = right_path;
2966328d5752SMark Fasheh 			right_path = NULL;
2967328d5752SMark Fasheh 			goto out;
2968328d5752SMark Fasheh 		}
2969328d5752SMark Fasheh 		if (ret) {
2970328d5752SMark Fasheh 			mlog_errno(ret);
2971328d5752SMark Fasheh 			goto out;
2972328d5752SMark Fasheh 		}
2973328d5752SMark Fasheh 
2974328d5752SMark Fasheh 		/*
2975328d5752SMark Fasheh 		 * The subtree rotate might have removed records on
2976328d5752SMark Fasheh 		 * the rightmost edge. If so, then rotation is
2977328d5752SMark Fasheh 		 * complete.
2978328d5752SMark Fasheh 		 */
2979328d5752SMark Fasheh 		if (deleted)
2980328d5752SMark Fasheh 			break;
2981328d5752SMark Fasheh 
2982328d5752SMark Fasheh 		ocfs2_mv_path(left_path, right_path);
2983328d5752SMark Fasheh 
2984e46f74dcSJoel Becker 		ret = ocfs2_find_cpos_for_right_leaf(sb, left_path,
2985328d5752SMark Fasheh 						     &right_cpos);
2986328d5752SMark Fasheh 		if (ret) {
2987328d5752SMark Fasheh 			mlog_errno(ret);
2988328d5752SMark Fasheh 			goto out;
2989328d5752SMark Fasheh 		}
2990328d5752SMark Fasheh 	}
2991328d5752SMark Fasheh 
2992328d5752SMark Fasheh out:
2993328d5752SMark Fasheh 	ocfs2_free_path(right_path);
2994328d5752SMark Fasheh 	ocfs2_free_path(left_path);
2995328d5752SMark Fasheh 
2996328d5752SMark Fasheh 	return ret;
2997328d5752SMark Fasheh }
2998328d5752SMark Fasheh 
299970f18c08SJoel Becker static int ocfs2_remove_rightmost_path(handle_t *handle,
300070f18c08SJoel Becker 				struct ocfs2_extent_tree *et,
3001328d5752SMark Fasheh 				struct ocfs2_path *path,
300270f18c08SJoel Becker 				struct ocfs2_cached_dealloc_ctxt *dealloc)
3003328d5752SMark Fasheh {
3004328d5752SMark Fasheh 	int ret, subtree_index;
3005328d5752SMark Fasheh 	u32 cpos;
3006328d5752SMark Fasheh 	struct ocfs2_path *left_path = NULL;
3007328d5752SMark Fasheh 	struct ocfs2_extent_block *eb;
3008328d5752SMark Fasheh 	struct ocfs2_extent_list *el;
3009328d5752SMark Fasheh 
30106136ca5fSJoel Becker 	ret = ocfs2_et_sanity_check(et);
3011e7d4cb6bSTao Ma 	if (ret)
3012e7d4cb6bSTao Ma 		goto out;
3013328d5752SMark Fasheh 
3014d9a0a1f8SJoel Becker 	ret = ocfs2_journal_access_path(et->et_ci, handle, path);
3015328d5752SMark Fasheh 	if (ret) {
3016328d5752SMark Fasheh 		mlog_errno(ret);
3017328d5752SMark Fasheh 		goto out;
3018328d5752SMark Fasheh 	}
3019328d5752SMark Fasheh 
30203d03a305SJoel Becker 	ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
30213d03a305SJoel Becker 					    path, &cpos);
3022328d5752SMark Fasheh 	if (ret) {
3023328d5752SMark Fasheh 		mlog_errno(ret);
3024328d5752SMark Fasheh 		goto out;
3025328d5752SMark Fasheh 	}
3026328d5752SMark Fasheh 
3027328d5752SMark Fasheh 	if (cpos) {
3028328d5752SMark Fasheh 		/*
3029328d5752SMark Fasheh 		 * We have a path to the left of this one - it needs
3030328d5752SMark Fasheh 		 * an update too.
3031328d5752SMark Fasheh 		 */
3032ffdd7a54SJoel Becker 		left_path = ocfs2_new_path_from_path(path);
3033328d5752SMark Fasheh 		if (!left_path) {
3034328d5752SMark Fasheh 			ret = -ENOMEM;
3035328d5752SMark Fasheh 			mlog_errno(ret);
3036328d5752SMark Fasheh 			goto out;
3037328d5752SMark Fasheh 		}
3038328d5752SMark Fasheh 
3039facdb77fSJoel Becker 		ret = ocfs2_find_path(et->et_ci, left_path, cpos);
3040328d5752SMark Fasheh 		if (ret) {
3041328d5752SMark Fasheh 			mlog_errno(ret);
3042328d5752SMark Fasheh 			goto out;
3043328d5752SMark Fasheh 		}
3044328d5752SMark Fasheh 
3045d9a0a1f8SJoel Becker 		ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
3046328d5752SMark Fasheh 		if (ret) {
3047328d5752SMark Fasheh 			mlog_errno(ret);
3048328d5752SMark Fasheh 			goto out;
3049328d5752SMark Fasheh 		}
3050328d5752SMark Fasheh 
30517dc02805SJoel Becker 		subtree_index = ocfs2_find_subtree_root(et, left_path, path);
3052328d5752SMark Fasheh 
30536641b0ceSJoel Becker 		ocfs2_unlink_subtree(handle, et, left_path, path,
3054328d5752SMark Fasheh 				     subtree_index, dealloc);
3055964f14a0SJun Piao 		ret = ocfs2_update_edge_lengths(handle, et, left_path);
30563c5e1068STao Ma 		if (ret) {
30573c5e1068STao Ma 			mlog_errno(ret);
30583c5e1068STao Ma 			goto out;
30593c5e1068STao Ma 		}
3060328d5752SMark Fasheh 
3061328d5752SMark Fasheh 		eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
306235dc0aa3SJoel Becker 		ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
3063328d5752SMark Fasheh 	} else {
3064328d5752SMark Fasheh 		/*
3065328d5752SMark Fasheh 		 * 'path' is also the leftmost path which
3066328d5752SMark Fasheh 		 * means it must be the only one. This gets
3067328d5752SMark Fasheh 		 * handled differently because we want to
306870f18c08SJoel Becker 		 * revert the root back to having extents
3069328d5752SMark Fasheh 		 * in-line.
3070328d5752SMark Fasheh 		 */
30716641b0ceSJoel Becker 		ocfs2_unlink_path(handle, et, dealloc, path, 1);
3072328d5752SMark Fasheh 
3073ce1d9ea6SJoel Becker 		el = et->et_root_el;
3074328d5752SMark Fasheh 		el->l_tree_depth = 0;
3075328d5752SMark Fasheh 		el->l_next_free_rec = 0;
3076328d5752SMark Fasheh 		memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3077328d5752SMark Fasheh 
307835dc0aa3SJoel Becker 		ocfs2_et_set_last_eb_blk(et, 0);
3079328d5752SMark Fasheh 	}
3080328d5752SMark Fasheh 
3081328d5752SMark Fasheh 	ocfs2_journal_dirty(handle, path_root_bh(path));
3082328d5752SMark Fasheh 
3083328d5752SMark Fasheh out:
3084328d5752SMark Fasheh 	ocfs2_free_path(left_path);
3085328d5752SMark Fasheh 	return ret;
3086328d5752SMark Fasheh }
3087328d5752SMark Fasheh 
3088d0c97d52SXue jiufei static int ocfs2_remove_rightmost_empty_extent(struct ocfs2_super *osb,
3089d0c97d52SXue jiufei 				struct ocfs2_extent_tree *et,
3090d0c97d52SXue jiufei 				struct ocfs2_path *path,
3091d0c97d52SXue jiufei 				struct ocfs2_cached_dealloc_ctxt *dealloc)
3092d0c97d52SXue jiufei {
3093d0c97d52SXue jiufei 	handle_t *handle;
3094d0c97d52SXue jiufei 	int ret;
3095d0c97d52SXue jiufei 	int credits = path->p_tree_depth * 2 + 1;
3096d0c97d52SXue jiufei 
3097d0c97d52SXue jiufei 	handle = ocfs2_start_trans(osb, credits);
3098d0c97d52SXue jiufei 	if (IS_ERR(handle)) {
3099d0c97d52SXue jiufei 		ret = PTR_ERR(handle);
3100d0c97d52SXue jiufei 		mlog_errno(ret);
3101d0c97d52SXue jiufei 		return ret;
3102d0c97d52SXue jiufei 	}
3103d0c97d52SXue jiufei 
3104d0c97d52SXue jiufei 	ret = ocfs2_remove_rightmost_path(handle, et, path, dealloc);
3105d0c97d52SXue jiufei 	if (ret)
3106d0c97d52SXue jiufei 		mlog_errno(ret);
3107d0c97d52SXue jiufei 
3108d0c97d52SXue jiufei 	ocfs2_commit_trans(osb, handle);
3109d0c97d52SXue jiufei 	return ret;
3110d0c97d52SXue jiufei }
3111d0c97d52SXue jiufei 
3112328d5752SMark Fasheh /*
3113328d5752SMark Fasheh  * Left rotation of btree records.
3114328d5752SMark Fasheh  *
3115328d5752SMark Fasheh  * In many ways, this is (unsurprisingly) the opposite of right
3116328d5752SMark Fasheh  * rotation. We start at some non-rightmost path containing an empty
3117328d5752SMark Fasheh  * extent in the leaf block. The code works its way to the rightmost
3118328d5752SMark Fasheh  * path by rotating records to the left in every subtree.
3119328d5752SMark Fasheh  *
3120328d5752SMark Fasheh  * This is used by any code which reduces the number of extent records
3121328d5752SMark Fasheh  * in a leaf. After removal, an empty record should be placed in the
3122328d5752SMark Fasheh  * leftmost list position.
3123328d5752SMark Fasheh  *
3124328d5752SMark Fasheh  * This won't handle a length update of the rightmost path records if
3125328d5752SMark Fasheh  * the rightmost tree leaf record is removed so the caller is
3126328d5752SMark Fasheh  * responsible for detecting and correcting that.
3127328d5752SMark Fasheh  */
312870f18c08SJoel Becker static int ocfs2_rotate_tree_left(handle_t *handle,
312970f18c08SJoel Becker 				  struct ocfs2_extent_tree *et,
3130328d5752SMark Fasheh 				  struct ocfs2_path *path,
313170f18c08SJoel Becker 				  struct ocfs2_cached_dealloc_ctxt *dealloc)
3132328d5752SMark Fasheh {
3133328d5752SMark Fasheh 	int ret, orig_credits = handle->h_buffer_credits;
3134328d5752SMark Fasheh 	struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
3135328d5752SMark Fasheh 	struct ocfs2_extent_block *eb;
3136328d5752SMark Fasheh 	struct ocfs2_extent_list *el;
3137328d5752SMark Fasheh 
3138328d5752SMark Fasheh 	el = path_leaf_el(path);
3139328d5752SMark Fasheh 	if (!ocfs2_is_empty_extent(&el->l_recs[0]))
3140328d5752SMark Fasheh 		return 0;
3141328d5752SMark Fasheh 
3142328d5752SMark Fasheh 	if (path->p_tree_depth == 0) {
3143328d5752SMark Fasheh rightmost_no_delete:
3144328d5752SMark Fasheh 		/*
3145e7d4cb6bSTao Ma 		 * Inline extents. This is trivially handled, so do
3146328d5752SMark Fasheh 		 * it up front.
3147328d5752SMark Fasheh 		 */
314870f18c08SJoel Becker 		ret = ocfs2_rotate_rightmost_leaf_left(handle, et, path);
3149328d5752SMark Fasheh 		if (ret)
3150328d5752SMark Fasheh 			mlog_errno(ret);
3151328d5752SMark Fasheh 		goto out;
3152328d5752SMark Fasheh 	}
3153328d5752SMark Fasheh 
3154328d5752SMark Fasheh 	/*
3155328d5752SMark Fasheh 	 * Handle rightmost branch now. There's several cases:
3156328d5752SMark Fasheh 	 *  1) simple rotation leaving records in there. That's trivial.
3157328d5752SMark Fasheh 	 *  2) rotation requiring a branch delete - there's no more
3158328d5752SMark Fasheh 	 *     records left. Two cases of this:
3159328d5752SMark Fasheh 	 *     a) There are branches to the left.
3160328d5752SMark Fasheh 	 *     b) This is also the leftmost (the only) branch.
3161328d5752SMark Fasheh 	 *
3162328d5752SMark Fasheh 	 *  1) is handled via ocfs2_rotate_rightmost_leaf_left()
3163328d5752SMark Fasheh 	 *  2a) we need the left branch so that we can update it with the unlink
316470f18c08SJoel Becker 	 *  2b) we need to bring the root back to inline extents.
3165328d5752SMark Fasheh 	 */
3166328d5752SMark Fasheh 
3167328d5752SMark Fasheh 	eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
3168328d5752SMark Fasheh 	el = &eb->h_list;
3169328d5752SMark Fasheh 	if (eb->h_next_leaf_blk == 0) {
3170328d5752SMark Fasheh 		/*
3171328d5752SMark Fasheh 		 * This gets a bit tricky if we're going to delete the
3172328d5752SMark Fasheh 		 * rightmost path. Get the other cases out of the way
3173328d5752SMark Fasheh 		 * 1st.
3174328d5752SMark Fasheh 		 */
3175328d5752SMark Fasheh 		if (le16_to_cpu(el->l_next_free_rec) > 1)
3176328d5752SMark Fasheh 			goto rightmost_no_delete;
3177328d5752SMark Fasheh 
3178328d5752SMark Fasheh 		if (le16_to_cpu(el->l_next_free_rec) == 0) {
3179328d5752SMark Fasheh 			ret = -EIO;
318070f18c08SJoel Becker 			ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
31817ecef14aSJoe Perches 				    "Owner %llu has empty extent block at %llu\n",
318270f18c08SJoel Becker 				    (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
3183328d5752SMark Fasheh 				    (unsigned long long)le64_to_cpu(eb->h_blkno));
3184328d5752SMark Fasheh 			goto out;
3185328d5752SMark Fasheh 		}
3186328d5752SMark Fasheh 
3187328d5752SMark Fasheh 		/*
3188328d5752SMark Fasheh 		 * XXX: The caller can not trust "path" any more after
3189328d5752SMark Fasheh 		 * this as it will have been deleted. What do we do?
3190328d5752SMark Fasheh 		 *
3191328d5752SMark Fasheh 		 * In theory the rotate-for-merge code will never get
3192328d5752SMark Fasheh 		 * here because it'll always ask for a rotate in a
3193328d5752SMark Fasheh 		 * nonempty list.
3194328d5752SMark Fasheh 		 */
3195328d5752SMark Fasheh 
319670f18c08SJoel Becker 		ret = ocfs2_remove_rightmost_path(handle, et, path,
319770f18c08SJoel Becker 						  dealloc);
3198328d5752SMark Fasheh 		if (ret)
3199328d5752SMark Fasheh 			mlog_errno(ret);
3200328d5752SMark Fasheh 		goto out;
3201328d5752SMark Fasheh 	}
3202328d5752SMark Fasheh 
3203328d5752SMark Fasheh 	/*
3204328d5752SMark Fasheh 	 * Now we can loop, remembering the path we get from -EAGAIN
3205328d5752SMark Fasheh 	 * and restarting from there.
3206328d5752SMark Fasheh 	 */
3207328d5752SMark Fasheh try_rotate:
3208e46f74dcSJoel Becker 	ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, path,
3209e46f74dcSJoel Becker 				       dealloc, &restart_path);
3210328d5752SMark Fasheh 	if (ret && ret != -EAGAIN) {
3211328d5752SMark Fasheh 		mlog_errno(ret);
3212328d5752SMark Fasheh 		goto out;
3213328d5752SMark Fasheh 	}
3214328d5752SMark Fasheh 
3215328d5752SMark Fasheh 	while (ret == -EAGAIN) {
3216328d5752SMark Fasheh 		tmp_path = restart_path;
3217328d5752SMark Fasheh 		restart_path = NULL;
3218328d5752SMark Fasheh 
3219e46f74dcSJoel Becker 		ret = __ocfs2_rotate_tree_left(handle, et, orig_credits,
3220328d5752SMark Fasheh 					       tmp_path, dealloc,
3221e46f74dcSJoel Becker 					       &restart_path);
3222328d5752SMark Fasheh 		if (ret && ret != -EAGAIN) {
3223328d5752SMark Fasheh 			mlog_errno(ret);
3224328d5752SMark Fasheh 			goto out;
3225328d5752SMark Fasheh 		}
3226328d5752SMark Fasheh 
3227328d5752SMark Fasheh 		ocfs2_free_path(tmp_path);
3228328d5752SMark Fasheh 		tmp_path = NULL;
3229328d5752SMark Fasheh 
3230328d5752SMark Fasheh 		if (ret == 0)
3231328d5752SMark Fasheh 			goto try_rotate;
3232328d5752SMark Fasheh 	}
3233328d5752SMark Fasheh 
3234328d5752SMark Fasheh out:
3235328d5752SMark Fasheh 	ocfs2_free_path(tmp_path);
3236328d5752SMark Fasheh 	ocfs2_free_path(restart_path);
3237328d5752SMark Fasheh 	return ret;
3238328d5752SMark Fasheh }
3239328d5752SMark Fasheh 
3240328d5752SMark Fasheh static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
3241328d5752SMark Fasheh 				int index)
3242328d5752SMark Fasheh {
3243328d5752SMark Fasheh 	struct ocfs2_extent_rec *rec = &el->l_recs[index];
3244328d5752SMark Fasheh 	unsigned int size;
3245328d5752SMark Fasheh 
3246328d5752SMark Fasheh 	if (rec->e_leaf_clusters == 0) {
3247328d5752SMark Fasheh 		/*
3248328d5752SMark Fasheh 		 * We consumed all of the merged-from record. An empty
3249328d5752SMark Fasheh 		 * extent cannot exist anywhere but the 1st array
3250328d5752SMark Fasheh 		 * position, so move things over if the merged-from
3251328d5752SMark Fasheh 		 * record doesn't occupy that position.
3252328d5752SMark Fasheh 		 *
3253328d5752SMark Fasheh 		 * This creates a new empty extent so the caller
3254328d5752SMark Fasheh 		 * should be smart enough to have removed any existing
3255328d5752SMark Fasheh 		 * ones.
3256328d5752SMark Fasheh 		 */
3257328d5752SMark Fasheh 		if (index > 0) {
3258328d5752SMark Fasheh 			BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3259328d5752SMark Fasheh 			size = index * sizeof(struct ocfs2_extent_rec);
3260328d5752SMark Fasheh 			memmove(&el->l_recs[1], &el->l_recs[0], size);
3261328d5752SMark Fasheh 		}
3262328d5752SMark Fasheh 
3263328d5752SMark Fasheh 		/*
3264328d5752SMark Fasheh 		 * Always memset - the caller doesn't check whether it
3265328d5752SMark Fasheh 		 * created an empty extent, so there could be junk in
3266328d5752SMark Fasheh 		 * the other fields.
3267328d5752SMark Fasheh 		 */
3268328d5752SMark Fasheh 		memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3269328d5752SMark Fasheh 	}
3270328d5752SMark Fasheh }
3271328d5752SMark Fasheh 
32724fe82c31SJoel Becker static int ocfs2_get_right_path(struct ocfs2_extent_tree *et,
3273677b9752STao Ma 				struct ocfs2_path *left_path,
3274677b9752STao Ma 				struct ocfs2_path **ret_right_path)
3275328d5752SMark Fasheh {
3276328d5752SMark Fasheh 	int ret;
3277677b9752STao Ma 	u32 right_cpos;
3278677b9752STao Ma 	struct ocfs2_path *right_path = NULL;
3279677b9752STao Ma 	struct ocfs2_extent_list *left_el;
3280677b9752STao Ma 
3281677b9752STao Ma 	*ret_right_path = NULL;
3282677b9752STao Ma 
3283677b9752STao Ma 	/* This function shouldn't be called for non-trees. */
3284677b9752STao Ma 	BUG_ON(left_path->p_tree_depth == 0);
3285677b9752STao Ma 
3286677b9752STao Ma 	left_el = path_leaf_el(left_path);
3287677b9752STao Ma 	BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3288677b9752STao Ma 
32894fe82c31SJoel Becker 	ret = ocfs2_find_cpos_for_right_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
32904fe82c31SJoel Becker 					     left_path, &right_cpos);
3291677b9752STao Ma 	if (ret) {
3292677b9752STao Ma 		mlog_errno(ret);
3293677b9752STao Ma 		goto out;
3294677b9752STao Ma 	}
3295677b9752STao Ma 
3296677b9752STao Ma 	/* This function shouldn't be called for the rightmost leaf. */
3297677b9752STao Ma 	BUG_ON(right_cpos == 0);
3298677b9752STao Ma 
3299ffdd7a54SJoel Becker 	right_path = ocfs2_new_path_from_path(left_path);
3300677b9752STao Ma 	if (!right_path) {
3301677b9752STao Ma 		ret = -ENOMEM;
3302677b9752STao Ma 		mlog_errno(ret);
3303677b9752STao Ma 		goto out;
3304677b9752STao Ma 	}
3305677b9752STao Ma 
33064fe82c31SJoel Becker 	ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
3307677b9752STao Ma 	if (ret) {
3308677b9752STao Ma 		mlog_errno(ret);
3309677b9752STao Ma 		goto out;
3310677b9752STao Ma 	}
3311677b9752STao Ma 
3312677b9752STao Ma 	*ret_right_path = right_path;
3313677b9752STao Ma out:
3314677b9752STao Ma 	if (ret)
3315677b9752STao Ma 		ocfs2_free_path(right_path);
3316677b9752STao Ma 	return ret;
3317677b9752STao Ma }
3318677b9752STao Ma 
3319677b9752STao Ma /*
3320677b9752STao Ma  * Remove split_rec clusters from the record at index and merge them
3321677b9752STao Ma  * onto the beginning of the record "next" to it.
3322677b9752STao Ma  * For index < l_count - 1, the next means the extent rec at index + 1.
3323677b9752STao Ma  * For index == l_count - 1, the "next" means the 1st extent rec of the
3324677b9752STao Ma  * next extent block.
3325677b9752STao Ma  */
33264fe82c31SJoel Becker static int ocfs2_merge_rec_right(struct ocfs2_path *left_path,
3327677b9752STao Ma 				 handle_t *handle,
33287dc02805SJoel Becker 				 struct ocfs2_extent_tree *et,
3329677b9752STao Ma 				 struct ocfs2_extent_rec *split_rec,
3330677b9752STao Ma 				 int index)
3331677b9752STao Ma {
3332677b9752STao Ma 	int ret, next_free, i;
3333328d5752SMark Fasheh 	unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3334328d5752SMark Fasheh 	struct ocfs2_extent_rec *left_rec;
3335328d5752SMark Fasheh 	struct ocfs2_extent_rec *right_rec;
3336677b9752STao Ma 	struct ocfs2_extent_list *right_el;
3337677b9752STao Ma 	struct ocfs2_path *right_path = NULL;
3338677b9752STao Ma 	int subtree_index = 0;
3339677b9752STao Ma 	struct ocfs2_extent_list *el = path_leaf_el(left_path);
3340677b9752STao Ma 	struct buffer_head *bh = path_leaf_bh(left_path);
3341677b9752STao Ma 	struct buffer_head *root_bh = NULL;
3342328d5752SMark Fasheh 
3343328d5752SMark Fasheh 	BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
3344328d5752SMark Fasheh 	left_rec = &el->l_recs[index];
3345677b9752STao Ma 
33469d8df6aaSAl Viro 	if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
3347677b9752STao Ma 	    le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3348677b9752STao Ma 		/* we meet with a cross extent block merge. */
33494fe82c31SJoel Becker 		ret = ocfs2_get_right_path(et, left_path, &right_path);
3350677b9752STao Ma 		if (ret) {
3351677b9752STao Ma 			mlog_errno(ret);
3352629a3b5fSMarkus Elfring 			return ret;
3353677b9752STao Ma 		}
3354677b9752STao Ma 
3355677b9752STao Ma 		right_el = path_leaf_el(right_path);
3356677b9752STao Ma 		next_free = le16_to_cpu(right_el->l_next_free_rec);
3357677b9752STao Ma 		BUG_ON(next_free <= 0);
3358677b9752STao Ma 		right_rec = &right_el->l_recs[0];
3359677b9752STao Ma 		if (ocfs2_is_empty_extent(right_rec)) {
33609d8df6aaSAl Viro 			BUG_ON(next_free <= 1);
3361677b9752STao Ma 			right_rec = &right_el->l_recs[1];
3362677b9752STao Ma 		}
3363677b9752STao Ma 
3364677b9752STao Ma 		BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3365677b9752STao Ma 		       le16_to_cpu(left_rec->e_leaf_clusters) !=
3366677b9752STao Ma 		       le32_to_cpu(right_rec->e_cpos));
3367677b9752STao Ma 
33687dc02805SJoel Becker 		subtree_index = ocfs2_find_subtree_root(et, left_path,
33697dc02805SJoel Becker 							right_path);
3370677b9752STao Ma 
3371677b9752STao Ma 		ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3372677b9752STao Ma 						      handle->h_buffer_credits,
3373677b9752STao Ma 						      right_path);
3374677b9752STao Ma 		if (ret) {
3375677b9752STao Ma 			mlog_errno(ret);
3376677b9752STao Ma 			goto out;
3377677b9752STao Ma 		}
3378677b9752STao Ma 
3379677b9752STao Ma 		root_bh = left_path->p_node[subtree_index].bh;
3380677b9752STao Ma 		BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3381677b9752STao Ma 
33827dc02805SJoel Becker 		ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
338313723d00SJoel Becker 						   subtree_index);
3384677b9752STao Ma 		if (ret) {
3385677b9752STao Ma 			mlog_errno(ret);
3386677b9752STao Ma 			goto out;
3387677b9752STao Ma 		}
3388677b9752STao Ma 
3389677b9752STao Ma 		for (i = subtree_index + 1;
3390677b9752STao Ma 		     i < path_num_items(right_path); i++) {
33917dc02805SJoel Becker 			ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
339213723d00SJoel Becker 							   right_path, i);
3393677b9752STao Ma 			if (ret) {
3394677b9752STao Ma 				mlog_errno(ret);
3395677b9752STao Ma 				goto out;
3396677b9752STao Ma 			}
3397677b9752STao Ma 
33987dc02805SJoel Becker 			ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
339913723d00SJoel Becker 							   left_path, i);
3400677b9752STao Ma 			if (ret) {
3401677b9752STao Ma 				mlog_errno(ret);
3402677b9752STao Ma 				goto out;
3403677b9752STao Ma 			}
3404677b9752STao Ma 		}
3405677b9752STao Ma 
3406677b9752STao Ma 	} else {
3407677b9752STao Ma 		BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3408328d5752SMark Fasheh 		right_rec = &el->l_recs[index + 1];
3409677b9752STao Ma 	}
3410328d5752SMark Fasheh 
34117dc02805SJoel Becker 	ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path,
341213723d00SJoel Becker 					   path_num_items(left_path) - 1);
3413328d5752SMark Fasheh 	if (ret) {
3414328d5752SMark Fasheh 		mlog_errno(ret);
3415328d5752SMark Fasheh 		goto out;
3416328d5752SMark Fasheh 	}
3417328d5752SMark Fasheh 
3418328d5752SMark Fasheh 	le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3419328d5752SMark Fasheh 
3420328d5752SMark Fasheh 	le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3421328d5752SMark Fasheh 	le64_add_cpu(&right_rec->e_blkno,
34227dc02805SJoel Becker 		     -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
34237dc02805SJoel Becker 					       split_clusters));
3424328d5752SMark Fasheh 	le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3425328d5752SMark Fasheh 
3426328d5752SMark Fasheh 	ocfs2_cleanup_merge(el, index);
3427328d5752SMark Fasheh 
3428ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, bh);
3429677b9752STao Ma 	if (right_path) {
3430ec20cec7SJoel Becker 		ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
34314619c73eSJoel Becker 		ocfs2_complete_edge_insert(handle, left_path, right_path,
34324619c73eSJoel Becker 					   subtree_index);
3433677b9752STao Ma 	}
3434328d5752SMark Fasheh out:
3435677b9752STao Ma 	ocfs2_free_path(right_path);
3436677b9752STao Ma 	return ret;
3437677b9752STao Ma }
3438677b9752STao Ma 
34394fe82c31SJoel Becker static int ocfs2_get_left_path(struct ocfs2_extent_tree *et,
3440677b9752STao Ma 			       struct ocfs2_path *right_path,
3441677b9752STao Ma 			       struct ocfs2_path **ret_left_path)
3442677b9752STao Ma {
3443677b9752STao Ma 	int ret;
3444677b9752STao Ma 	u32 left_cpos;
3445677b9752STao Ma 	struct ocfs2_path *left_path = NULL;
3446677b9752STao Ma 
3447677b9752STao Ma 	*ret_left_path = NULL;
3448677b9752STao Ma 
3449677b9752STao Ma 	/* This function shouldn't be called for non-trees. */
3450677b9752STao Ma 	BUG_ON(right_path->p_tree_depth == 0);
3451677b9752STao Ma 
34524fe82c31SJoel Becker 	ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3453677b9752STao Ma 					    right_path, &left_cpos);
3454677b9752STao Ma 	if (ret) {
3455677b9752STao Ma 		mlog_errno(ret);
3456677b9752STao Ma 		goto out;
3457677b9752STao Ma 	}
3458677b9752STao Ma 
3459677b9752STao Ma 	/* This function shouldn't be called for the leftmost leaf. */
3460677b9752STao Ma 	BUG_ON(left_cpos == 0);
3461677b9752STao Ma 
3462ffdd7a54SJoel Becker 	left_path = ocfs2_new_path_from_path(right_path);
3463677b9752STao Ma 	if (!left_path) {
3464677b9752STao Ma 		ret = -ENOMEM;
3465677b9752STao Ma 		mlog_errno(ret);
3466677b9752STao Ma 		goto out;
3467677b9752STao Ma 	}
3468677b9752STao Ma 
34694fe82c31SJoel Becker 	ret = ocfs2_find_path(et->et_ci, left_path, left_cpos);
3470677b9752STao Ma 	if (ret) {
3471677b9752STao Ma 		mlog_errno(ret);
3472677b9752STao Ma 		goto out;
3473677b9752STao Ma 	}
3474677b9752STao Ma 
3475677b9752STao Ma 	*ret_left_path = left_path;
3476677b9752STao Ma out:
3477677b9752STao Ma 	if (ret)
3478677b9752STao Ma 		ocfs2_free_path(left_path);
3479328d5752SMark Fasheh 	return ret;
3480328d5752SMark Fasheh }
3481328d5752SMark Fasheh 
3482328d5752SMark Fasheh /*
3483328d5752SMark Fasheh  * Remove split_rec clusters from the record at index and merge them
3484677b9752STao Ma  * onto the tail of the record "before" it.
3485677b9752STao Ma  * For index > 0, the "before" means the extent rec at index - 1.
3486677b9752STao Ma  *
3487677b9752STao Ma  * For index == 0, the "before" means the last record of the previous
3488677b9752STao Ma  * extent block. And there is also a situation that we may need to
3489677b9752STao Ma  * remove the rightmost leaf extent block in the right_path and change
3490677b9752STao Ma  * the right path to indicate the new rightmost path.
3491328d5752SMark Fasheh  */
34924fe82c31SJoel Becker static int ocfs2_merge_rec_left(struct ocfs2_path *right_path,
3493328d5752SMark Fasheh 				handle_t *handle,
34944fe82c31SJoel Becker 				struct ocfs2_extent_tree *et,
3495328d5752SMark Fasheh 				struct ocfs2_extent_rec *split_rec,
3496677b9752STao Ma 				struct ocfs2_cached_dealloc_ctxt *dealloc,
3497677b9752STao Ma 				int index)
3498328d5752SMark Fasheh {
3499677b9752STao Ma 	int ret, i, subtree_index = 0, has_empty_extent = 0;
3500328d5752SMark Fasheh 	unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3501328d5752SMark Fasheh 	struct ocfs2_extent_rec *left_rec;
3502328d5752SMark Fasheh 	struct ocfs2_extent_rec *right_rec;
3503677b9752STao Ma 	struct ocfs2_extent_list *el = path_leaf_el(right_path);
3504677b9752STao Ma 	struct buffer_head *bh = path_leaf_bh(right_path);
3505677b9752STao Ma 	struct buffer_head *root_bh = NULL;
3506677b9752STao Ma 	struct ocfs2_path *left_path = NULL;
3507677b9752STao Ma 	struct ocfs2_extent_list *left_el;
3508328d5752SMark Fasheh 
3509677b9752STao Ma 	BUG_ON(index < 0);
3510328d5752SMark Fasheh 
3511328d5752SMark Fasheh 	right_rec = &el->l_recs[index];
3512677b9752STao Ma 	if (index == 0) {
3513677b9752STao Ma 		/* we meet with a cross extent block merge. */
35144fe82c31SJoel Becker 		ret = ocfs2_get_left_path(et, right_path, &left_path);
3515677b9752STao Ma 		if (ret) {
3516677b9752STao Ma 			mlog_errno(ret);
3517992ef6e7SMarkus Elfring 			return ret;
3518677b9752STao Ma 		}
3519677b9752STao Ma 
3520677b9752STao Ma 		left_el = path_leaf_el(left_path);
3521677b9752STao Ma 		BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3522677b9752STao Ma 		       le16_to_cpu(left_el->l_count));
3523677b9752STao Ma 
3524677b9752STao Ma 		left_rec = &left_el->l_recs[
3525677b9752STao Ma 				le16_to_cpu(left_el->l_next_free_rec) - 1];
3526677b9752STao Ma 		BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3527677b9752STao Ma 		       le16_to_cpu(left_rec->e_leaf_clusters) !=
3528677b9752STao Ma 		       le32_to_cpu(split_rec->e_cpos));
3529677b9752STao Ma 
35307dc02805SJoel Becker 		subtree_index = ocfs2_find_subtree_root(et, left_path,
35317dc02805SJoel Becker 							right_path);
3532677b9752STao Ma 
3533677b9752STao Ma 		ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3534677b9752STao Ma 						      handle->h_buffer_credits,
3535677b9752STao Ma 						      left_path);
3536677b9752STao Ma 		if (ret) {
3537677b9752STao Ma 			mlog_errno(ret);
3538677b9752STao Ma 			goto out;
3539677b9752STao Ma 		}
3540677b9752STao Ma 
3541677b9752STao Ma 		root_bh = left_path->p_node[subtree_index].bh;
3542677b9752STao Ma 		BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3543677b9752STao Ma 
35444fe82c31SJoel Becker 		ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
354513723d00SJoel Becker 						   subtree_index);
3546677b9752STao Ma 		if (ret) {
3547677b9752STao Ma 			mlog_errno(ret);
3548677b9752STao Ma 			goto out;
3549677b9752STao Ma 		}
3550677b9752STao Ma 
3551677b9752STao Ma 		for (i = subtree_index + 1;
3552677b9752STao Ma 		     i < path_num_items(right_path); i++) {
35534fe82c31SJoel Becker 			ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
355413723d00SJoel Becker 							   right_path, i);
3555677b9752STao Ma 			if (ret) {
3556677b9752STao Ma 				mlog_errno(ret);
3557677b9752STao Ma 				goto out;
3558677b9752STao Ma 			}
3559677b9752STao Ma 
35604fe82c31SJoel Becker 			ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
356113723d00SJoel Becker 							   left_path, i);
3562677b9752STao Ma 			if (ret) {
3563677b9752STao Ma 				mlog_errno(ret);
3564677b9752STao Ma 				goto out;
3565677b9752STao Ma 			}
3566677b9752STao Ma 		}
3567677b9752STao Ma 	} else {
3568677b9752STao Ma 		left_rec = &el->l_recs[index - 1];
3569328d5752SMark Fasheh 		if (ocfs2_is_empty_extent(&el->l_recs[0]))
3570328d5752SMark Fasheh 			has_empty_extent = 1;
3571677b9752STao Ma 	}
3572328d5752SMark Fasheh 
35734fe82c31SJoel Becker 	ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
35749047beabSTao Ma 					   path_num_items(right_path) - 1);
3575328d5752SMark Fasheh 	if (ret) {
3576328d5752SMark Fasheh 		mlog_errno(ret);
3577328d5752SMark Fasheh 		goto out;
3578328d5752SMark Fasheh 	}
3579328d5752SMark Fasheh 
3580328d5752SMark Fasheh 	if (has_empty_extent && index == 1) {
3581328d5752SMark Fasheh 		/*
3582328d5752SMark Fasheh 		 * The easy case - we can just plop the record right in.
3583328d5752SMark Fasheh 		 */
3584328d5752SMark Fasheh 		*left_rec = *split_rec;
3585677b9752STao Ma 	} else
3586328d5752SMark Fasheh 		le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
3587328d5752SMark Fasheh 
3588328d5752SMark Fasheh 	le32_add_cpu(&right_rec->e_cpos, split_clusters);
3589328d5752SMark Fasheh 	le64_add_cpu(&right_rec->e_blkno,
35904fe82c31SJoel Becker 		     ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
35914fe82c31SJoel Becker 					      split_clusters));
3592328d5752SMark Fasheh 	le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3593328d5752SMark Fasheh 
3594328d5752SMark Fasheh 	ocfs2_cleanup_merge(el, index);
3595328d5752SMark Fasheh 
3596ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, bh);
3597677b9752STao Ma 	if (left_path) {
3598ec20cec7SJoel Becker 		ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3599677b9752STao Ma 
3600677b9752STao Ma 		/*
3601677b9752STao Ma 		 * In the situation that the right_rec is empty and the extent
3602677b9752STao Ma 		 * block is empty also,  ocfs2_complete_edge_insert can't handle
3603677b9752STao Ma 		 * it and we need to delete the right extent block.
3604677b9752STao Ma 		 */
3605677b9752STao Ma 		if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3606677b9752STao Ma 		    le16_to_cpu(el->l_next_free_rec) == 1) {
360717215989SXue jiufei 			/* extend credit for ocfs2_remove_rightmost_path */
360817215989SXue jiufei 			ret = ocfs2_extend_rotate_transaction(handle, 0,
360917215989SXue jiufei 					handle->h_buffer_credits,
361017215989SXue jiufei 					right_path);
361117215989SXue jiufei 			if (ret) {
361217215989SXue jiufei 				mlog_errno(ret);
361317215989SXue jiufei 				goto out;
361417215989SXue jiufei 			}
3615677b9752STao Ma 
361670f18c08SJoel Becker 			ret = ocfs2_remove_rightmost_path(handle, et,
3617e7d4cb6bSTao Ma 							  right_path,
361870f18c08SJoel Becker 							  dealloc);
3619677b9752STao Ma 			if (ret) {
3620677b9752STao Ma 				mlog_errno(ret);
3621677b9752STao Ma 				goto out;
3622677b9752STao Ma 			}
3623677b9752STao Ma 
3624677b9752STao Ma 			/* Now the rightmost extent block has been deleted.
3625677b9752STao Ma 			 * So we use the new rightmost path.
3626677b9752STao Ma 			 */
3627677b9752STao Ma 			ocfs2_mv_path(right_path, left_path);
3628677b9752STao Ma 			left_path = NULL;
3629677b9752STao Ma 		} else
36304619c73eSJoel Becker 			ocfs2_complete_edge_insert(handle, left_path,
3631677b9752STao Ma 						   right_path, subtree_index);
3632677b9752STao Ma 	}
3633328d5752SMark Fasheh out:
3634677b9752STao Ma 	ocfs2_free_path(left_path);
3635328d5752SMark Fasheh 	return ret;
3636328d5752SMark Fasheh }
3637328d5752SMark Fasheh 
3638c495dd24SJoel Becker static int ocfs2_try_to_merge_extent(handle_t *handle,
3639c495dd24SJoel Becker 				     struct ocfs2_extent_tree *et,
3640677b9752STao Ma 				     struct ocfs2_path *path,
3641328d5752SMark Fasheh 				     int split_index,
3642328d5752SMark Fasheh 				     struct ocfs2_extent_rec *split_rec,
3643328d5752SMark Fasheh 				     struct ocfs2_cached_dealloc_ctxt *dealloc,
3644c495dd24SJoel Becker 				     struct ocfs2_merge_ctxt *ctxt)
3645328d5752SMark Fasheh {
3646518d7269STao Mao 	int ret = 0;
3647677b9752STao Ma 	struct ocfs2_extent_list *el = path_leaf_el(path);
3648328d5752SMark Fasheh 	struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3649328d5752SMark Fasheh 
3650328d5752SMark Fasheh 	BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3651328d5752SMark Fasheh 
3652518d7269STao Mao 	if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
365317215989SXue jiufei 		/* extend credit for ocfs2_remove_rightmost_path */
365417215989SXue jiufei 		ret = ocfs2_extend_rotate_transaction(handle, 0,
365517215989SXue jiufei 				handle->h_buffer_credits,
365617215989SXue jiufei 				path);
365717215989SXue jiufei 		if (ret) {
365817215989SXue jiufei 			mlog_errno(ret);
365917215989SXue jiufei 			goto out;
366017215989SXue jiufei 		}
3661328d5752SMark Fasheh 		/*
3662328d5752SMark Fasheh 		 * The merge code will need to create an empty
3663328d5752SMark Fasheh 		 * extent to take the place of the newly
3664328d5752SMark Fasheh 		 * emptied slot. Remove any pre-existing empty
3665328d5752SMark Fasheh 		 * extents - having more than one in a leaf is
3666328d5752SMark Fasheh 		 * illegal.
3667328d5752SMark Fasheh 		 */
366870f18c08SJoel Becker 		ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
3669328d5752SMark Fasheh 		if (ret) {
3670328d5752SMark Fasheh 			mlog_errno(ret);
3671328d5752SMark Fasheh 			goto out;
3672328d5752SMark Fasheh 		}
3673328d5752SMark Fasheh 		split_index--;
3674328d5752SMark Fasheh 		rec = &el->l_recs[split_index];
3675328d5752SMark Fasheh 	}
3676328d5752SMark Fasheh 
3677328d5752SMark Fasheh 	if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3678328d5752SMark Fasheh 		/*
3679328d5752SMark Fasheh 		 * Left-right contig implies this.
3680328d5752SMark Fasheh 		 */
3681328d5752SMark Fasheh 		BUG_ON(!ctxt->c_split_covers_rec);
3682328d5752SMark Fasheh 
3683328d5752SMark Fasheh 		/*
3684328d5752SMark Fasheh 		 * Since the leftright insert always covers the entire
3685328d5752SMark Fasheh 		 * extent, this call will delete the insert record
3686328d5752SMark Fasheh 		 * entirely, resulting in an empty extent record added to
3687328d5752SMark Fasheh 		 * the extent block.
3688328d5752SMark Fasheh 		 *
3689328d5752SMark Fasheh 		 * Since the adding of an empty extent shifts
3690328d5752SMark Fasheh 		 * everything back to the right, there's no need to
3691328d5752SMark Fasheh 		 * update split_index here.
3692677b9752STao Ma 		 *
3693677b9752STao Ma 		 * When the split_index is zero, we need to merge it to the
3694677b9752STao Ma 		 * prevoius extent block. It is more efficient and easier
3695677b9752STao Ma 		 * if we do merge_right first and merge_left later.
3696328d5752SMark Fasheh 		 */
36974fe82c31SJoel Becker 		ret = ocfs2_merge_rec_right(path, handle, et, split_rec,
3698677b9752STao Ma 					    split_index);
3699328d5752SMark Fasheh 		if (ret) {
3700328d5752SMark Fasheh 			mlog_errno(ret);
3701328d5752SMark Fasheh 			goto out;
3702328d5752SMark Fasheh 		}
3703328d5752SMark Fasheh 
3704328d5752SMark Fasheh 		/*
3705328d5752SMark Fasheh 		 * We can only get this from logic error above.
3706328d5752SMark Fasheh 		 */
3707328d5752SMark Fasheh 		BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3708328d5752SMark Fasheh 
370917215989SXue jiufei 		/* extend credit for ocfs2_remove_rightmost_path */
371017215989SXue jiufei 		ret = ocfs2_extend_rotate_transaction(handle, 0,
371117215989SXue jiufei 					handle->h_buffer_credits,
371217215989SXue jiufei 					path);
371317215989SXue jiufei 		if (ret) {
371417215989SXue jiufei 			mlog_errno(ret);
371517215989SXue jiufei 			goto out;
371617215989SXue jiufei 		}
371717215989SXue jiufei 
3718677b9752STao Ma 		/* The merge left us with an empty extent, remove it. */
371970f18c08SJoel Becker 		ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
3720328d5752SMark Fasheh 		if (ret) {
3721328d5752SMark Fasheh 			mlog_errno(ret);
3722328d5752SMark Fasheh 			goto out;
3723328d5752SMark Fasheh 		}
3724677b9752STao Ma 
3725328d5752SMark Fasheh 		rec = &el->l_recs[split_index];
3726328d5752SMark Fasheh 
3727328d5752SMark Fasheh 		/*
3728328d5752SMark Fasheh 		 * Note that we don't pass split_rec here on purpose -
3729677b9752STao Ma 		 * we've merged it into the rec already.
3730328d5752SMark Fasheh 		 */
37314fe82c31SJoel Becker 		ret = ocfs2_merge_rec_left(path, handle, et, rec,
37324fe82c31SJoel Becker 					   dealloc, split_index);
3733677b9752STao Ma 
3734328d5752SMark Fasheh 		if (ret) {
3735328d5752SMark Fasheh 			mlog_errno(ret);
3736328d5752SMark Fasheh 			goto out;
3737328d5752SMark Fasheh 		}
3738328d5752SMark Fasheh 
373917215989SXue jiufei 		/* extend credit for ocfs2_remove_rightmost_path */
374017215989SXue jiufei 		ret = ocfs2_extend_rotate_transaction(handle, 0,
374117215989SXue jiufei 				handle->h_buffer_credits,
374217215989SXue jiufei 				path);
374317215989SXue jiufei 		if (ret) {
374417215989SXue jiufei 			mlog_errno(ret);
374517215989SXue jiufei 			goto out;
374617215989SXue jiufei 		}
374717215989SXue jiufei 
374870f18c08SJoel Becker 		ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
3749328d5752SMark Fasheh 		/*
3750328d5752SMark Fasheh 		 * Error from this last rotate is not critical, so
3751328d5752SMark Fasheh 		 * print but don't bubble it up.
3752328d5752SMark Fasheh 		 */
3753328d5752SMark Fasheh 		if (ret)
3754328d5752SMark Fasheh 			mlog_errno(ret);
3755328d5752SMark Fasheh 		ret = 0;
3756328d5752SMark Fasheh 	} else {
3757328d5752SMark Fasheh 		/*
3758328d5752SMark Fasheh 		 * Merge a record to the left or right.
3759328d5752SMark Fasheh 		 *
3760328d5752SMark Fasheh 		 * 'contig_type' is relative to the existing record,
3761328d5752SMark Fasheh 		 * so for example, if we're "right contig", it's to
3762328d5752SMark Fasheh 		 * the record on the left (hence the left merge).
3763328d5752SMark Fasheh 		 */
3764328d5752SMark Fasheh 		if (ctxt->c_contig_type == CONTIG_RIGHT) {
37654fe82c31SJoel Becker 			ret = ocfs2_merge_rec_left(path, handle, et,
37664fe82c31SJoel Becker 						   split_rec, dealloc,
3767328d5752SMark Fasheh 						   split_index);
3768328d5752SMark Fasheh 			if (ret) {
3769328d5752SMark Fasheh 				mlog_errno(ret);
3770328d5752SMark Fasheh 				goto out;
3771328d5752SMark Fasheh 			}
3772328d5752SMark Fasheh 		} else {
37734fe82c31SJoel Becker 			ret = ocfs2_merge_rec_right(path, handle,
37747dc02805SJoel Becker 						    et, split_rec,
3775328d5752SMark Fasheh 						    split_index);
3776328d5752SMark Fasheh 			if (ret) {
3777328d5752SMark Fasheh 				mlog_errno(ret);
3778328d5752SMark Fasheh 				goto out;
3779328d5752SMark Fasheh 			}
3780328d5752SMark Fasheh 		}
3781328d5752SMark Fasheh 
3782328d5752SMark Fasheh 		if (ctxt->c_split_covers_rec) {
378317215989SXue jiufei 			/* extend credit for ocfs2_remove_rightmost_path */
378417215989SXue jiufei 			ret = ocfs2_extend_rotate_transaction(handle, 0,
378517215989SXue jiufei 					handle->h_buffer_credits,
378617215989SXue jiufei 					path);
378717215989SXue jiufei 			if (ret) {
378817215989SXue jiufei 				mlog_errno(ret);
378917215989SXue jiufei 				ret = 0;
379017215989SXue jiufei 				goto out;
379117215989SXue jiufei 			}
379217215989SXue jiufei 
3793328d5752SMark Fasheh 			/*
3794328d5752SMark Fasheh 			 * The merge may have left an empty extent in
3795328d5752SMark Fasheh 			 * our leaf. Try to rotate it away.
3796328d5752SMark Fasheh 			 */
379770f18c08SJoel Becker 			ret = ocfs2_rotate_tree_left(handle, et, path,
379870f18c08SJoel Becker 						     dealloc);
3799328d5752SMark Fasheh 			if (ret)
3800328d5752SMark Fasheh 				mlog_errno(ret);
3801328d5752SMark Fasheh 			ret = 0;
3802328d5752SMark Fasheh 		}
3803328d5752SMark Fasheh 	}
3804328d5752SMark Fasheh 
3805328d5752SMark Fasheh out:
3806328d5752SMark Fasheh 	return ret;
3807328d5752SMark Fasheh }
3808328d5752SMark Fasheh 
3809328d5752SMark Fasheh static void ocfs2_subtract_from_rec(struct super_block *sb,
3810328d5752SMark Fasheh 				    enum ocfs2_split_type split,
3811328d5752SMark Fasheh 				    struct ocfs2_extent_rec *rec,
3812328d5752SMark Fasheh 				    struct ocfs2_extent_rec *split_rec)
3813328d5752SMark Fasheh {
3814328d5752SMark Fasheh 	u64 len_blocks;
3815328d5752SMark Fasheh 
3816328d5752SMark Fasheh 	len_blocks = ocfs2_clusters_to_blocks(sb,
3817328d5752SMark Fasheh 				le16_to_cpu(split_rec->e_leaf_clusters));
3818328d5752SMark Fasheh 
3819328d5752SMark Fasheh 	if (split == SPLIT_LEFT) {
3820328d5752SMark Fasheh 		/*
3821328d5752SMark Fasheh 		 * Region is on the left edge of the existing
3822328d5752SMark Fasheh 		 * record.
3823328d5752SMark Fasheh 		 */
3824328d5752SMark Fasheh 		le32_add_cpu(&rec->e_cpos,
3825328d5752SMark Fasheh 			     le16_to_cpu(split_rec->e_leaf_clusters));
3826328d5752SMark Fasheh 		le64_add_cpu(&rec->e_blkno, len_blocks);
3827328d5752SMark Fasheh 		le16_add_cpu(&rec->e_leaf_clusters,
3828328d5752SMark Fasheh 			     -le16_to_cpu(split_rec->e_leaf_clusters));
3829328d5752SMark Fasheh 	} else {
3830328d5752SMark Fasheh 		/*
3831328d5752SMark Fasheh 		 * Region is on the right edge of the existing
3832328d5752SMark Fasheh 		 * record.
3833328d5752SMark Fasheh 		 */
3834328d5752SMark Fasheh 		le16_add_cpu(&rec->e_leaf_clusters,
3835328d5752SMark Fasheh 			     -le16_to_cpu(split_rec->e_leaf_clusters));
3836328d5752SMark Fasheh 	}
3837328d5752SMark Fasheh }
3838328d5752SMark Fasheh 
3839dcd0538fSMark Fasheh /*
3840dcd0538fSMark Fasheh  * Do the final bits of extent record insertion at the target leaf
3841dcd0538fSMark Fasheh  * list. If this leaf is part of an allocation tree, it is assumed
3842dcd0538fSMark Fasheh  * that the tree above has been prepared.
3843dcd0538fSMark Fasheh  */
3844d5628623SJoel Becker static void ocfs2_insert_at_leaf(struct ocfs2_extent_tree *et,
3845d5628623SJoel Becker 				 struct ocfs2_extent_rec *insert_rec,
3846dcd0538fSMark Fasheh 				 struct ocfs2_extent_list *el,
3847d5628623SJoel Becker 				 struct ocfs2_insert_type *insert)
3848dcd0538fSMark Fasheh {
3849dcd0538fSMark Fasheh 	int i = insert->ins_contig_index;
3850dcd0538fSMark Fasheh 	unsigned int range;
3851dcd0538fSMark Fasheh 	struct ocfs2_extent_rec *rec;
3852dcd0538fSMark Fasheh 
3853e48edee2SMark Fasheh 	BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
3854dcd0538fSMark Fasheh 
3855328d5752SMark Fasheh 	if (insert->ins_split != SPLIT_NONE) {
3856328d5752SMark Fasheh 		i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3857328d5752SMark Fasheh 		BUG_ON(i == -1);
3858328d5752SMark Fasheh 		rec = &el->l_recs[i];
3859d5628623SJoel Becker 		ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
3860d5628623SJoel Becker 					insert->ins_split, rec,
3861328d5752SMark Fasheh 					insert_rec);
3862328d5752SMark Fasheh 		goto rotate;
3863328d5752SMark Fasheh 	}
3864328d5752SMark Fasheh 
3865dcd0538fSMark Fasheh 	/*
3866dcd0538fSMark Fasheh 	 * Contiguous insert - either left or right.
3867dcd0538fSMark Fasheh 	 */
3868dcd0538fSMark Fasheh 	if (insert->ins_contig != CONTIG_NONE) {
3869dcd0538fSMark Fasheh 		rec = &el->l_recs[i];
3870dcd0538fSMark Fasheh 		if (insert->ins_contig == CONTIG_LEFT) {
3871dcd0538fSMark Fasheh 			rec->e_blkno = insert_rec->e_blkno;
3872dcd0538fSMark Fasheh 			rec->e_cpos = insert_rec->e_cpos;
3873dcd0538fSMark Fasheh 		}
3874e48edee2SMark Fasheh 		le16_add_cpu(&rec->e_leaf_clusters,
3875e48edee2SMark Fasheh 			     le16_to_cpu(insert_rec->e_leaf_clusters));
3876dcd0538fSMark Fasheh 		return;
3877dcd0538fSMark Fasheh 	}
3878dcd0538fSMark Fasheh 
3879dcd0538fSMark Fasheh 	/*
3880dcd0538fSMark Fasheh 	 * Handle insert into an empty leaf.
3881dcd0538fSMark Fasheh 	 */
3882dcd0538fSMark Fasheh 	if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3883dcd0538fSMark Fasheh 	    ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3884dcd0538fSMark Fasheh 	     ocfs2_is_empty_extent(&el->l_recs[0]))) {
3885dcd0538fSMark Fasheh 		el->l_recs[0] = *insert_rec;
3886dcd0538fSMark Fasheh 		el->l_next_free_rec = cpu_to_le16(1);
3887dcd0538fSMark Fasheh 		return;
3888dcd0538fSMark Fasheh 	}
3889dcd0538fSMark Fasheh 
3890dcd0538fSMark Fasheh 	/*
3891dcd0538fSMark Fasheh 	 * Appending insert.
3892dcd0538fSMark Fasheh 	 */
3893dcd0538fSMark Fasheh 	if (insert->ins_appending == APPEND_TAIL) {
3894dcd0538fSMark Fasheh 		i = le16_to_cpu(el->l_next_free_rec) - 1;
3895dcd0538fSMark Fasheh 		rec = &el->l_recs[i];
3896e48edee2SMark Fasheh 		range = le32_to_cpu(rec->e_cpos)
3897e48edee2SMark Fasheh 			+ le16_to_cpu(rec->e_leaf_clusters);
3898dcd0538fSMark Fasheh 		BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3899dcd0538fSMark Fasheh 
3900dcd0538fSMark Fasheh 		mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3901dcd0538fSMark Fasheh 				le16_to_cpu(el->l_count),
3902d5628623SJoel Becker 				"owner %llu, depth %u, count %u, next free %u, "
3903dcd0538fSMark Fasheh 				"rec.cpos %u, rec.clusters %u, "
3904dcd0538fSMark Fasheh 				"insert.cpos %u, insert.clusters %u\n",
3905d5628623SJoel Becker 				ocfs2_metadata_cache_owner(et->et_ci),
3906dcd0538fSMark Fasheh 				le16_to_cpu(el->l_tree_depth),
3907dcd0538fSMark Fasheh 				le16_to_cpu(el->l_count),
3908dcd0538fSMark Fasheh 				le16_to_cpu(el->l_next_free_rec),
3909dcd0538fSMark Fasheh 				le32_to_cpu(el->l_recs[i].e_cpos),
3910e48edee2SMark Fasheh 				le16_to_cpu(el->l_recs[i].e_leaf_clusters),
3911dcd0538fSMark Fasheh 				le32_to_cpu(insert_rec->e_cpos),
3912e48edee2SMark Fasheh 				le16_to_cpu(insert_rec->e_leaf_clusters));
3913dcd0538fSMark Fasheh 		i++;
3914dcd0538fSMark Fasheh 		el->l_recs[i] = *insert_rec;
3915dcd0538fSMark Fasheh 		le16_add_cpu(&el->l_next_free_rec, 1);
3916dcd0538fSMark Fasheh 		return;
3917dcd0538fSMark Fasheh 	}
3918dcd0538fSMark Fasheh 
3919328d5752SMark Fasheh rotate:
3920dcd0538fSMark Fasheh 	/*
3921dcd0538fSMark Fasheh 	 * Ok, we have to rotate.
3922dcd0538fSMark Fasheh 	 *
3923dcd0538fSMark Fasheh 	 * At this point, it is safe to assume that inserting into an
3924dcd0538fSMark Fasheh 	 * empty leaf and appending to a leaf have both been handled
3925dcd0538fSMark Fasheh 	 * above.
3926dcd0538fSMark Fasheh 	 *
3927dcd0538fSMark Fasheh 	 * This leaf needs to have space, either by the empty 1st
3928dcd0538fSMark Fasheh 	 * extent record, or by virtue of an l_next_rec < l_count.
3929dcd0538fSMark Fasheh 	 */
3930dcd0538fSMark Fasheh 	ocfs2_rotate_leaf(el, insert_rec);
3931dcd0538fSMark Fasheh }
3932dcd0538fSMark Fasheh 
3933d401dc12SJoel Becker static void ocfs2_adjust_rightmost_records(handle_t *handle,
3934d401dc12SJoel Becker 					   struct ocfs2_extent_tree *et,
3935328d5752SMark Fasheh 					   struct ocfs2_path *path,
3936328d5752SMark Fasheh 					   struct ocfs2_extent_rec *insert_rec)
3937328d5752SMark Fasheh {
3938d22aa615SChangwei Ge 	int i, next_free;
3939328d5752SMark Fasheh 	struct buffer_head *bh;
3940328d5752SMark Fasheh 	struct ocfs2_extent_list *el;
3941328d5752SMark Fasheh 	struct ocfs2_extent_rec *rec;
3942328d5752SMark Fasheh 
3943328d5752SMark Fasheh 	/*
3944328d5752SMark Fasheh 	 * Update everything except the leaf block.
3945328d5752SMark Fasheh 	 */
3946328d5752SMark Fasheh 	for (i = 0; i < path->p_tree_depth; i++) {
3947328d5752SMark Fasheh 		bh = path->p_node[i].bh;
3948328d5752SMark Fasheh 		el = path->p_node[i].el;
3949328d5752SMark Fasheh 
3950328d5752SMark Fasheh 		next_free = le16_to_cpu(el->l_next_free_rec);
3951328d5752SMark Fasheh 		if (next_free == 0) {
3952d401dc12SJoel Becker 			ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
39537ecef14aSJoe Perches 				    "Owner %llu has a bad extent list\n",
3954d401dc12SJoel Becker 				    (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
3955328d5752SMark Fasheh 			return;
3956328d5752SMark Fasheh 		}
3957328d5752SMark Fasheh 
3958328d5752SMark Fasheh 		rec = &el->l_recs[next_free - 1];
3959328d5752SMark Fasheh 
3960328d5752SMark Fasheh 		rec->e_int_clusters = insert_rec->e_cpos;
3961328d5752SMark Fasheh 		le32_add_cpu(&rec->e_int_clusters,
3962328d5752SMark Fasheh 			     le16_to_cpu(insert_rec->e_leaf_clusters));
3963328d5752SMark Fasheh 		le32_add_cpu(&rec->e_int_clusters,
3964328d5752SMark Fasheh 			     -le32_to_cpu(rec->e_cpos));
3965328d5752SMark Fasheh 
3966ec20cec7SJoel Becker 		ocfs2_journal_dirty(handle, bh);
3967328d5752SMark Fasheh 	}
3968328d5752SMark Fasheh }
3969328d5752SMark Fasheh 
3970d401dc12SJoel Becker static int ocfs2_append_rec_to_path(handle_t *handle,
3971d401dc12SJoel Becker 				    struct ocfs2_extent_tree *et,
3972dcd0538fSMark Fasheh 				    struct ocfs2_extent_rec *insert_rec,
3973dcd0538fSMark Fasheh 				    struct ocfs2_path *right_path,
3974dcd0538fSMark Fasheh 				    struct ocfs2_path **ret_left_path)
3975dcd0538fSMark Fasheh {
3976328d5752SMark Fasheh 	int ret, next_free;
3977dcd0538fSMark Fasheh 	struct ocfs2_extent_list *el;
3978dcd0538fSMark Fasheh 	struct ocfs2_path *left_path = NULL;
3979dcd0538fSMark Fasheh 
3980dcd0538fSMark Fasheh 	*ret_left_path = NULL;
3981dcd0538fSMark Fasheh 
3982dcd0538fSMark Fasheh 	/*
3983e48edee2SMark Fasheh 	 * This shouldn't happen for non-trees. The extent rec cluster
3984e48edee2SMark Fasheh 	 * count manipulation below only works for interior nodes.
3985e48edee2SMark Fasheh 	 */
3986e48edee2SMark Fasheh 	BUG_ON(right_path->p_tree_depth == 0);
3987e48edee2SMark Fasheh 
3988e48edee2SMark Fasheh 	/*
3989dcd0538fSMark Fasheh 	 * If our appending insert is at the leftmost edge of a leaf,
3990dcd0538fSMark Fasheh 	 * then we might need to update the rightmost records of the
3991dcd0538fSMark Fasheh 	 * neighboring path.
3992dcd0538fSMark Fasheh 	 */
3993dcd0538fSMark Fasheh 	el = path_leaf_el(right_path);
3994dcd0538fSMark Fasheh 	next_free = le16_to_cpu(el->l_next_free_rec);
3995dcd0538fSMark Fasheh 	if (next_free == 0 ||
3996dcd0538fSMark Fasheh 	    (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3997dcd0538fSMark Fasheh 		u32 left_cpos;
3998dcd0538fSMark Fasheh 
3999d401dc12SJoel Becker 		ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
4000d401dc12SJoel Becker 						    right_path, &left_cpos);
4001dcd0538fSMark Fasheh 		if (ret) {
4002dcd0538fSMark Fasheh 			mlog_errno(ret);
4003dcd0538fSMark Fasheh 			goto out;
4004dcd0538fSMark Fasheh 		}
4005dcd0538fSMark Fasheh 
4006a09d09b8STao Ma 		trace_ocfs2_append_rec_to_path(
4007a09d09b8STao Ma 			(unsigned long long)
4008a09d09b8STao Ma 			ocfs2_metadata_cache_owner(et->et_ci),
4009a09d09b8STao Ma 			le32_to_cpu(insert_rec->e_cpos),
4010dcd0538fSMark Fasheh 			left_cpos);
4011dcd0538fSMark Fasheh 
4012dcd0538fSMark Fasheh 		/*
4013dcd0538fSMark Fasheh 		 * No need to worry if the append is already in the
4014dcd0538fSMark Fasheh 		 * leftmost leaf.
4015dcd0538fSMark Fasheh 		 */
4016dcd0538fSMark Fasheh 		if (left_cpos) {
4017ffdd7a54SJoel Becker 			left_path = ocfs2_new_path_from_path(right_path);
4018dcd0538fSMark Fasheh 			if (!left_path) {
4019dcd0538fSMark Fasheh 				ret = -ENOMEM;
4020dcd0538fSMark Fasheh 				mlog_errno(ret);
4021dcd0538fSMark Fasheh 				goto out;
4022dcd0538fSMark Fasheh 			}
4023dcd0538fSMark Fasheh 
4024d401dc12SJoel Becker 			ret = ocfs2_find_path(et->et_ci, left_path,
4025facdb77fSJoel Becker 					      left_cpos);
4026dcd0538fSMark Fasheh 			if (ret) {
4027dcd0538fSMark Fasheh 				mlog_errno(ret);
4028dcd0538fSMark Fasheh 				goto out;
4029dcd0538fSMark Fasheh 			}
4030dcd0538fSMark Fasheh 
4031dcd0538fSMark Fasheh 			/*
4032dcd0538fSMark Fasheh 			 * ocfs2_insert_path() will pass the left_path to the
4033dcd0538fSMark Fasheh 			 * journal for us.
4034dcd0538fSMark Fasheh 			 */
4035dcd0538fSMark Fasheh 		}
4036dcd0538fSMark Fasheh 	}
4037dcd0538fSMark Fasheh 
4038d401dc12SJoel Becker 	ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
4039dcd0538fSMark Fasheh 	if (ret) {
4040dcd0538fSMark Fasheh 		mlog_errno(ret);
4041dcd0538fSMark Fasheh 		goto out;
4042dcd0538fSMark Fasheh 	}
4043dcd0538fSMark Fasheh 
4044d401dc12SJoel Becker 	ocfs2_adjust_rightmost_records(handle, et, right_path, insert_rec);
4045dcd0538fSMark Fasheh 
4046dcd0538fSMark Fasheh 	*ret_left_path = left_path;
4047dcd0538fSMark Fasheh 	ret = 0;
4048dcd0538fSMark Fasheh out:
4049dcd0538fSMark Fasheh 	if (ret != 0)
4050dcd0538fSMark Fasheh 		ocfs2_free_path(left_path);
4051dcd0538fSMark Fasheh 
4052dcd0538fSMark Fasheh 	return ret;
4053dcd0538fSMark Fasheh }
4054dcd0538fSMark Fasheh 
4055c38e52bbSJoel Becker static void ocfs2_split_record(struct ocfs2_extent_tree *et,
4056328d5752SMark Fasheh 			       struct ocfs2_path *left_path,
4057328d5752SMark Fasheh 			       struct ocfs2_path *right_path,
4058328d5752SMark Fasheh 			       struct ocfs2_extent_rec *split_rec,
4059328d5752SMark Fasheh 			       enum ocfs2_split_type split)
4060328d5752SMark Fasheh {
4061328d5752SMark Fasheh 	int index;
4062328d5752SMark Fasheh 	u32 cpos = le32_to_cpu(split_rec->e_cpos);
4063328d5752SMark Fasheh 	struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
4064328d5752SMark Fasheh 	struct ocfs2_extent_rec *rec, *tmprec;
4065328d5752SMark Fasheh 
4066c19a28e1SFernando Carrijo 	right_el = path_leaf_el(right_path);
4067328d5752SMark Fasheh 	if (left_path)
4068328d5752SMark Fasheh 		left_el = path_leaf_el(left_path);
4069328d5752SMark Fasheh 
4070328d5752SMark Fasheh 	el = right_el;
4071328d5752SMark Fasheh 	insert_el = right_el;
4072328d5752SMark Fasheh 	index = ocfs2_search_extent_list(el, cpos);
4073328d5752SMark Fasheh 	if (index != -1) {
4074328d5752SMark Fasheh 		if (index == 0 && left_path) {
4075328d5752SMark Fasheh 			BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
4076328d5752SMark Fasheh 
4077328d5752SMark Fasheh 			/*
4078328d5752SMark Fasheh 			 * This typically means that the record
4079328d5752SMark Fasheh 			 * started in the left path but moved to the
4080328d5752SMark Fasheh 			 * right as a result of rotation. We either
4081328d5752SMark Fasheh 			 * move the existing record to the left, or we
4082328d5752SMark Fasheh 			 * do the later insert there.
4083328d5752SMark Fasheh 			 *
4084328d5752SMark Fasheh 			 * In this case, the left path should always
4085328d5752SMark Fasheh 			 * exist as the rotate code will have passed
4086328d5752SMark Fasheh 			 * it back for a post-insert update.
4087328d5752SMark Fasheh 			 */
4088328d5752SMark Fasheh 
4089328d5752SMark Fasheh 			if (split == SPLIT_LEFT) {
4090328d5752SMark Fasheh 				/*
4091328d5752SMark Fasheh 				 * It's a left split. Since we know
4092328d5752SMark Fasheh 				 * that the rotate code gave us an
4093328d5752SMark Fasheh 				 * empty extent in the left path, we
4094328d5752SMark Fasheh 				 * can just do the insert there.
4095328d5752SMark Fasheh 				 */
4096328d5752SMark Fasheh 				insert_el = left_el;
4097328d5752SMark Fasheh 			} else {
4098328d5752SMark Fasheh 				/*
4099328d5752SMark Fasheh 				 * Right split - we have to move the
4100328d5752SMark Fasheh 				 * existing record over to the left
4101328d5752SMark Fasheh 				 * leaf. The insert will be into the
4102328d5752SMark Fasheh 				 * newly created empty extent in the
4103328d5752SMark Fasheh 				 * right leaf.
4104328d5752SMark Fasheh 				 */
4105328d5752SMark Fasheh 				tmprec = &right_el->l_recs[index];
4106328d5752SMark Fasheh 				ocfs2_rotate_leaf(left_el, tmprec);
4107328d5752SMark Fasheh 				el = left_el;
4108328d5752SMark Fasheh 
4109328d5752SMark Fasheh 				memset(tmprec, 0, sizeof(*tmprec));
4110328d5752SMark Fasheh 				index = ocfs2_search_extent_list(left_el, cpos);
4111328d5752SMark Fasheh 				BUG_ON(index == -1);
4112328d5752SMark Fasheh 			}
4113328d5752SMark Fasheh 		}
4114328d5752SMark Fasheh 	} else {
4115328d5752SMark Fasheh 		BUG_ON(!left_path);
4116328d5752SMark Fasheh 		BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
4117328d5752SMark Fasheh 		/*
4118328d5752SMark Fasheh 		 * Left path is easy - we can just allow the insert to
4119328d5752SMark Fasheh 		 * happen.
4120328d5752SMark Fasheh 		 */
4121328d5752SMark Fasheh 		el = left_el;
4122328d5752SMark Fasheh 		insert_el = left_el;
4123328d5752SMark Fasheh 		index = ocfs2_search_extent_list(el, cpos);
4124328d5752SMark Fasheh 		BUG_ON(index == -1);
4125328d5752SMark Fasheh 	}
4126328d5752SMark Fasheh 
4127328d5752SMark Fasheh 	rec = &el->l_recs[index];
4128c38e52bbSJoel Becker 	ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
4129c38e52bbSJoel Becker 				split, rec, split_rec);
4130328d5752SMark Fasheh 	ocfs2_rotate_leaf(insert_el, split_rec);
4131328d5752SMark Fasheh }
4132328d5752SMark Fasheh 
4133dcd0538fSMark Fasheh /*
4134e7d4cb6bSTao Ma  * This function only does inserts on an allocation b-tree. For tree
4135e7d4cb6bSTao Ma  * depth = 0, ocfs2_insert_at_leaf() is called directly.
4136dcd0538fSMark Fasheh  *
4137dcd0538fSMark Fasheh  * right_path is the path we want to do the actual insert
4138dcd0538fSMark Fasheh  * in. left_path should only be passed in if we need to update that
4139dcd0538fSMark Fasheh  * portion of the tree after an edge insert.
4140dcd0538fSMark Fasheh  */
41413505bec0SJoel Becker static int ocfs2_insert_path(handle_t *handle,
41427dc02805SJoel Becker 			     struct ocfs2_extent_tree *et,
4143dcd0538fSMark Fasheh 			     struct ocfs2_path *left_path,
4144dcd0538fSMark Fasheh 			     struct ocfs2_path *right_path,
4145dcd0538fSMark Fasheh 			     struct ocfs2_extent_rec *insert_rec,
4146dcd0538fSMark Fasheh 			     struct ocfs2_insert_type *insert)
4147dcd0538fSMark Fasheh {
4148dcd0538fSMark Fasheh 	int ret, subtree_index;
4149dcd0538fSMark Fasheh 	struct buffer_head *leaf_bh = path_leaf_bh(right_path);
4150dcd0538fSMark Fasheh 
4151dcd0538fSMark Fasheh 	if (left_path) {
4152dcd0538fSMark Fasheh 		/*
4153dcd0538fSMark Fasheh 		 * There's a chance that left_path got passed back to
4154dcd0538fSMark Fasheh 		 * us without being accounted for in the
4155dcd0538fSMark Fasheh 		 * journal. Extend our transaction here to be sure we
4156dcd0538fSMark Fasheh 		 * can change those blocks.
4157dcd0538fSMark Fasheh 		 */
4158c901fb00STao Ma 		ret = ocfs2_extend_trans(handle, left_path->p_tree_depth);
4159dcd0538fSMark Fasheh 		if (ret < 0) {
4160dcd0538fSMark Fasheh 			mlog_errno(ret);
4161dcd0538fSMark Fasheh 			goto out;
4162dcd0538fSMark Fasheh 		}
4163dcd0538fSMark Fasheh 
41647dc02805SJoel Becker 		ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
4165dcd0538fSMark Fasheh 		if (ret < 0) {
4166dcd0538fSMark Fasheh 			mlog_errno(ret);
4167dcd0538fSMark Fasheh 			goto out;
4168dcd0538fSMark Fasheh 		}
4169dcd0538fSMark Fasheh 	}
4170dcd0538fSMark Fasheh 
4171e8aed345SMark Fasheh 	/*
4172e8aed345SMark Fasheh 	 * Pass both paths to the journal. The majority of inserts
4173e8aed345SMark Fasheh 	 * will be touching all components anyway.
4174e8aed345SMark Fasheh 	 */
41757dc02805SJoel Becker 	ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
4176e8aed345SMark Fasheh 	if (ret < 0) {
4177e8aed345SMark Fasheh 		mlog_errno(ret);
4178e8aed345SMark Fasheh 		goto out;
4179e8aed345SMark Fasheh 	}
4180e8aed345SMark Fasheh 
4181328d5752SMark Fasheh 	if (insert->ins_split != SPLIT_NONE) {
4182328d5752SMark Fasheh 		/*
4183328d5752SMark Fasheh 		 * We could call ocfs2_insert_at_leaf() for some types
4184c78bad11SJoe Perches 		 * of splits, but it's easier to just let one separate
4185328d5752SMark Fasheh 		 * function sort it all out.
4186328d5752SMark Fasheh 		 */
4187c38e52bbSJoel Becker 		ocfs2_split_record(et, left_path, right_path,
4188328d5752SMark Fasheh 				   insert_rec, insert->ins_split);
4189e8aed345SMark Fasheh 
4190e8aed345SMark Fasheh 		/*
4191e8aed345SMark Fasheh 		 * Split might have modified either leaf and we don't
4192e8aed345SMark Fasheh 		 * have a guarantee that the later edge insert will
4193e8aed345SMark Fasheh 		 * dirty this for us.
4194e8aed345SMark Fasheh 		 */
4195e8aed345SMark Fasheh 		if (left_path)
4196ec20cec7SJoel Becker 			ocfs2_journal_dirty(handle,
4197e8aed345SMark Fasheh 					    path_leaf_bh(left_path));
4198328d5752SMark Fasheh 	} else
4199d5628623SJoel Becker 		ocfs2_insert_at_leaf(et, insert_rec, path_leaf_el(right_path),
4200d5628623SJoel Becker 				     insert);
4201dcd0538fSMark Fasheh 
4202ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, leaf_bh);
4203dcd0538fSMark Fasheh 
4204dcd0538fSMark Fasheh 	if (left_path) {
4205dcd0538fSMark Fasheh 		/*
4206dcd0538fSMark Fasheh 		 * The rotate code has indicated that we need to fix
4207dcd0538fSMark Fasheh 		 * up portions of the tree after the insert.
4208dcd0538fSMark Fasheh 		 *
4209dcd0538fSMark Fasheh 		 * XXX: Should we extend the transaction here?
4210dcd0538fSMark Fasheh 		 */
42117dc02805SJoel Becker 		subtree_index = ocfs2_find_subtree_root(et, left_path,
4212dcd0538fSMark Fasheh 							right_path);
42134619c73eSJoel Becker 		ocfs2_complete_edge_insert(handle, left_path, right_path,
42144619c73eSJoel Becker 					   subtree_index);
4215dcd0538fSMark Fasheh 	}
4216dcd0538fSMark Fasheh 
4217dcd0538fSMark Fasheh 	ret = 0;
4218dcd0538fSMark Fasheh out:
4219dcd0538fSMark Fasheh 	return ret;
4220dcd0538fSMark Fasheh }
4221dcd0538fSMark Fasheh 
42223505bec0SJoel Becker static int ocfs2_do_insert_extent(handle_t *handle,
4223e7d4cb6bSTao Ma 				  struct ocfs2_extent_tree *et,
4224dcd0538fSMark Fasheh 				  struct ocfs2_extent_rec *insert_rec,
4225dcd0538fSMark Fasheh 				  struct ocfs2_insert_type *type)
4226dcd0538fSMark Fasheh {
4227dcd0538fSMark Fasheh 	int ret, rotate = 0;
4228dcd0538fSMark Fasheh 	u32 cpos;
4229dcd0538fSMark Fasheh 	struct ocfs2_path *right_path = NULL;
4230dcd0538fSMark Fasheh 	struct ocfs2_path *left_path = NULL;
4231dcd0538fSMark Fasheh 	struct ocfs2_extent_list *el;
4232dcd0538fSMark Fasheh 
4233ce1d9ea6SJoel Becker 	el = et->et_root_el;
4234dcd0538fSMark Fasheh 
4235d9a0a1f8SJoel Becker 	ret = ocfs2_et_root_journal_access(handle, et,
4236dcd0538fSMark Fasheh 					   OCFS2_JOURNAL_ACCESS_WRITE);
4237dcd0538fSMark Fasheh 	if (ret) {
4238dcd0538fSMark Fasheh 		mlog_errno(ret);
4239dcd0538fSMark Fasheh 		goto out;
4240dcd0538fSMark Fasheh 	}
4241dcd0538fSMark Fasheh 
4242dcd0538fSMark Fasheh 	if (le16_to_cpu(el->l_tree_depth) == 0) {
4243d5628623SJoel Becker 		ocfs2_insert_at_leaf(et, insert_rec, el, type);
4244dcd0538fSMark Fasheh 		goto out_update_clusters;
4245dcd0538fSMark Fasheh 	}
4246dcd0538fSMark Fasheh 
4247ffdd7a54SJoel Becker 	right_path = ocfs2_new_path_from_et(et);
4248dcd0538fSMark Fasheh 	if (!right_path) {
4249dcd0538fSMark Fasheh 		ret = -ENOMEM;
4250dcd0538fSMark Fasheh 		mlog_errno(ret);
4251dcd0538fSMark Fasheh 		goto out;
4252dcd0538fSMark Fasheh 	}
4253dcd0538fSMark Fasheh 
4254dcd0538fSMark Fasheh 	/*
4255dcd0538fSMark Fasheh 	 * Determine the path to start with. Rotations need the
4256dcd0538fSMark Fasheh 	 * rightmost path, everything else can go directly to the
4257dcd0538fSMark Fasheh 	 * target leaf.
4258dcd0538fSMark Fasheh 	 */
4259dcd0538fSMark Fasheh 	cpos = le32_to_cpu(insert_rec->e_cpos);
4260dcd0538fSMark Fasheh 	if (type->ins_appending == APPEND_NONE &&
4261dcd0538fSMark Fasheh 	    type->ins_contig == CONTIG_NONE) {
4262dcd0538fSMark Fasheh 		rotate = 1;
4263dcd0538fSMark Fasheh 		cpos = UINT_MAX;
4264dcd0538fSMark Fasheh 	}
4265dcd0538fSMark Fasheh 
4266facdb77fSJoel Becker 	ret = ocfs2_find_path(et->et_ci, right_path, cpos);
4267dcd0538fSMark Fasheh 	if (ret) {
4268dcd0538fSMark Fasheh 		mlog_errno(ret);
4269dcd0538fSMark Fasheh 		goto out;
4270dcd0538fSMark Fasheh 	}
4271dcd0538fSMark Fasheh 
4272dcd0538fSMark Fasheh 	/*
4273dcd0538fSMark Fasheh 	 * Rotations and appends need special treatment - they modify
4274dcd0538fSMark Fasheh 	 * parts of the tree's above them.
4275dcd0538fSMark Fasheh 	 *
4276dcd0538fSMark Fasheh 	 * Both might pass back a path immediate to the left of the
4277dcd0538fSMark Fasheh 	 * one being inserted to. This will be cause
4278dcd0538fSMark Fasheh 	 * ocfs2_insert_path() to modify the rightmost records of
4279dcd0538fSMark Fasheh 	 * left_path to account for an edge insert.
4280dcd0538fSMark Fasheh 	 *
4281dcd0538fSMark Fasheh 	 * XXX: When modifying this code, keep in mind that an insert
4282dcd0538fSMark Fasheh 	 * can wind up skipping both of these two special cases...
4283dcd0538fSMark Fasheh 	 */
4284dcd0538fSMark Fasheh 	if (rotate) {
42851bbf0b8dSJoel Becker 		ret = ocfs2_rotate_tree_right(handle, et, type->ins_split,
4286dcd0538fSMark Fasheh 					      le32_to_cpu(insert_rec->e_cpos),
4287dcd0538fSMark Fasheh 					      right_path, &left_path);
4288dcd0538fSMark Fasheh 		if (ret) {
4289dcd0538fSMark Fasheh 			mlog_errno(ret);
4290dcd0538fSMark Fasheh 			goto out;
4291dcd0538fSMark Fasheh 		}
4292e8aed345SMark Fasheh 
4293e8aed345SMark Fasheh 		/*
4294e8aed345SMark Fasheh 		 * ocfs2_rotate_tree_right() might have extended the
4295e8aed345SMark Fasheh 		 * transaction without re-journaling our tree root.
4296e8aed345SMark Fasheh 		 */
4297d9a0a1f8SJoel Becker 		ret = ocfs2_et_root_journal_access(handle, et,
4298e8aed345SMark Fasheh 						   OCFS2_JOURNAL_ACCESS_WRITE);
4299e8aed345SMark Fasheh 		if (ret) {
4300e8aed345SMark Fasheh 			mlog_errno(ret);
4301e8aed345SMark Fasheh 			goto out;
4302e8aed345SMark Fasheh 		}
4303dcd0538fSMark Fasheh 	} else if (type->ins_appending == APPEND_TAIL
4304dcd0538fSMark Fasheh 		   && type->ins_contig != CONTIG_LEFT) {
4305d401dc12SJoel Becker 		ret = ocfs2_append_rec_to_path(handle, et, insert_rec,
4306dcd0538fSMark Fasheh 					       right_path, &left_path);
4307dcd0538fSMark Fasheh 		if (ret) {
4308dcd0538fSMark Fasheh 			mlog_errno(ret);
4309dcd0538fSMark Fasheh 			goto out;
4310dcd0538fSMark Fasheh 		}
4311dcd0538fSMark Fasheh 	}
4312dcd0538fSMark Fasheh 
43133505bec0SJoel Becker 	ret = ocfs2_insert_path(handle, et, left_path, right_path,
4314dcd0538fSMark Fasheh 				insert_rec, type);
4315dcd0538fSMark Fasheh 	if (ret) {
4316dcd0538fSMark Fasheh 		mlog_errno(ret);
4317dcd0538fSMark Fasheh 		goto out;
4318dcd0538fSMark Fasheh 	}
4319dcd0538fSMark Fasheh 
4320dcd0538fSMark Fasheh out_update_clusters:
4321328d5752SMark Fasheh 	if (type->ins_split == SPLIT_NONE)
43226136ca5fSJoel Becker 		ocfs2_et_update_clusters(et,
4323e48edee2SMark Fasheh 					 le16_to_cpu(insert_rec->e_leaf_clusters));
4324dcd0538fSMark Fasheh 
4325ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, et->et_root_bh);
4326dcd0538fSMark Fasheh 
4327dcd0538fSMark Fasheh out:
4328dcd0538fSMark Fasheh 	ocfs2_free_path(left_path);
4329dcd0538fSMark Fasheh 	ocfs2_free_path(right_path);
4330dcd0538fSMark Fasheh 
4331dcd0538fSMark Fasheh 	return ret;
4332dcd0538fSMark Fasheh }
4333dcd0538fSMark Fasheh 
43349f99ad08SXue jiufei static int ocfs2_figure_merge_contig_type(struct ocfs2_extent_tree *et,
4335a2970291SJoel Becker 			       struct ocfs2_path *path,
4336328d5752SMark Fasheh 			       struct ocfs2_extent_list *el, int index,
43379f99ad08SXue jiufei 			       struct ocfs2_extent_rec *split_rec,
43389f99ad08SXue jiufei 			       struct ocfs2_merge_ctxt *ctxt)
4339328d5752SMark Fasheh {
43409f99ad08SXue jiufei 	int status = 0;
4341328d5752SMark Fasheh 	enum ocfs2_contig_type ret = CONTIG_NONE;
4342ad5a4d70STao Ma 	u32 left_cpos, right_cpos;
4343ad5a4d70STao Ma 	struct ocfs2_extent_rec *rec = NULL;
4344ad5a4d70STao Ma 	struct ocfs2_extent_list *new_el;
4345ad5a4d70STao Ma 	struct ocfs2_path *left_path = NULL, *right_path = NULL;
4346ad5a4d70STao Ma 	struct buffer_head *bh;
4347ad5a4d70STao Ma 	struct ocfs2_extent_block *eb;
4348a2970291SJoel Becker 	struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
4349ad5a4d70STao Ma 
4350ad5a4d70STao Ma 	if (index > 0) {
4351ad5a4d70STao Ma 		rec = &el->l_recs[index - 1];
4352ad5a4d70STao Ma 	} else if (path->p_tree_depth > 0) {
4353a2970291SJoel Becker 		status = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
4354ad5a4d70STao Ma 		if (status)
435506a269ccSMarkus Elfring 			goto exit;
4356ad5a4d70STao Ma 
4357ad5a4d70STao Ma 		if (left_cpos != 0) {
4358ffdd7a54SJoel Becker 			left_path = ocfs2_new_path_from_path(path);
43599f99ad08SXue jiufei 			if (!left_path) {
43609f99ad08SXue jiufei 				status = -ENOMEM;
43619f99ad08SXue jiufei 				mlog_errno(status);
436206a269ccSMarkus Elfring 				goto exit;
43639f99ad08SXue jiufei 			}
4364ad5a4d70STao Ma 
4365a2970291SJoel Becker 			status = ocfs2_find_path(et->et_ci, left_path,
4366a2970291SJoel Becker 						 left_cpos);
4367ad5a4d70STao Ma 			if (status)
436806a269ccSMarkus Elfring 				goto free_left_path;
4369ad5a4d70STao Ma 
4370ad5a4d70STao Ma 			new_el = path_leaf_el(left_path);
4371ad5a4d70STao Ma 
4372ad5a4d70STao Ma 			if (le16_to_cpu(new_el->l_next_free_rec) !=
4373ad5a4d70STao Ma 			    le16_to_cpu(new_el->l_count)) {
4374ad5a4d70STao Ma 				bh = path_leaf_bh(left_path);
4375ad5a4d70STao Ma 				eb = (struct ocfs2_extent_block *)bh->b_data;
4376a2970291SJoel Becker 				ocfs2_error(sb,
43777ecef14aSJoe Perches 					    "Extent block #%llu has an invalid l_next_free_rec of %d.  It should have matched the l_count of %d\n",
43785e96581aSJoel Becker 					    (unsigned long long)le64_to_cpu(eb->h_blkno),
43795e96581aSJoel Becker 					    le16_to_cpu(new_el->l_next_free_rec),
43805e96581aSJoel Becker 					    le16_to_cpu(new_el->l_count));
43815e96581aSJoel Becker 				status = -EINVAL;
438206a269ccSMarkus Elfring 				goto free_left_path;
4383ad5a4d70STao Ma 			}
4384ad5a4d70STao Ma 			rec = &new_el->l_recs[
4385ad5a4d70STao Ma 				le16_to_cpu(new_el->l_next_free_rec) - 1];
4386ad5a4d70STao Ma 		}
4387ad5a4d70STao Ma 	}
4388328d5752SMark Fasheh 
4389328d5752SMark Fasheh 	/*
4390328d5752SMark Fasheh 	 * We're careful to check for an empty extent record here -
4391328d5752SMark Fasheh 	 * the merge code will know what to do if it sees one.
4392328d5752SMark Fasheh 	 */
4393ad5a4d70STao Ma 	if (rec) {
4394328d5752SMark Fasheh 		if (index == 1 && ocfs2_is_empty_extent(rec)) {
4395328d5752SMark Fasheh 			if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4396328d5752SMark Fasheh 				ret = CONTIG_RIGHT;
4397328d5752SMark Fasheh 		} else {
4398853a3a14STao Ma 			ret = ocfs2_et_extent_contig(et, rec, split_rec);
4399328d5752SMark Fasheh 		}
4400328d5752SMark Fasheh 	}
4401328d5752SMark Fasheh 
4402ad5a4d70STao Ma 	rec = NULL;
4403ad5a4d70STao Ma 	if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4404ad5a4d70STao Ma 		rec = &el->l_recs[index + 1];
4405ad5a4d70STao Ma 	else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4406ad5a4d70STao Ma 		 path->p_tree_depth > 0) {
4407a2970291SJoel Becker 		status = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
4408ad5a4d70STao Ma 		if (status)
440906a269ccSMarkus Elfring 			goto free_left_path;
4410ad5a4d70STao Ma 
4411ad5a4d70STao Ma 		if (right_cpos == 0)
441206a269ccSMarkus Elfring 			goto free_left_path;
4413ad5a4d70STao Ma 
4414ffdd7a54SJoel Becker 		right_path = ocfs2_new_path_from_path(path);
44159f99ad08SXue jiufei 		if (!right_path) {
44169f99ad08SXue jiufei 			status = -ENOMEM;
44179f99ad08SXue jiufei 			mlog_errno(status);
441806a269ccSMarkus Elfring 			goto free_left_path;
44199f99ad08SXue jiufei 		}
4420ad5a4d70STao Ma 
4421a2970291SJoel Becker 		status = ocfs2_find_path(et->et_ci, right_path, right_cpos);
4422ad5a4d70STao Ma 		if (status)
442306a269ccSMarkus Elfring 			goto free_right_path;
4424ad5a4d70STao Ma 
4425ad5a4d70STao Ma 		new_el = path_leaf_el(right_path);
4426ad5a4d70STao Ma 		rec = &new_el->l_recs[0];
4427ad5a4d70STao Ma 		if (ocfs2_is_empty_extent(rec)) {
4428ad5a4d70STao Ma 			if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4429ad5a4d70STao Ma 				bh = path_leaf_bh(right_path);
4430ad5a4d70STao Ma 				eb = (struct ocfs2_extent_block *)bh->b_data;
4431a2970291SJoel Becker 				ocfs2_error(sb,
44327ecef14aSJoe Perches 					    "Extent block #%llu has an invalid l_next_free_rec of %d\n",
44335e96581aSJoel Becker 					    (unsigned long long)le64_to_cpu(eb->h_blkno),
44345e96581aSJoel Becker 					    le16_to_cpu(new_el->l_next_free_rec));
44355e96581aSJoel Becker 				status = -EINVAL;
443606a269ccSMarkus Elfring 				goto free_right_path;
4437ad5a4d70STao Ma 			}
4438ad5a4d70STao Ma 			rec = &new_el->l_recs[1];
4439ad5a4d70STao Ma 		}
4440ad5a4d70STao Ma 	}
4441ad5a4d70STao Ma 
4442ad5a4d70STao Ma 	if (rec) {
4443328d5752SMark Fasheh 		enum ocfs2_contig_type contig_type;
4444328d5752SMark Fasheh 
4445853a3a14STao Ma 		contig_type = ocfs2_et_extent_contig(et, rec, split_rec);
4446328d5752SMark Fasheh 
4447328d5752SMark Fasheh 		if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4448328d5752SMark Fasheh 			ret = CONTIG_LEFTRIGHT;
4449328d5752SMark Fasheh 		else if (ret == CONTIG_NONE)
4450328d5752SMark Fasheh 			ret = contig_type;
4451328d5752SMark Fasheh 	}
4452328d5752SMark Fasheh 
445306a269ccSMarkus Elfring free_right_path:
4454ad5a4d70STao Ma 	ocfs2_free_path(right_path);
445506a269ccSMarkus Elfring free_left_path:
445606a269ccSMarkus Elfring 	ocfs2_free_path(left_path);
445706a269ccSMarkus Elfring exit:
44589f99ad08SXue jiufei 	if (status == 0)
44599f99ad08SXue jiufei 		ctxt->c_contig_type = ret;
44609f99ad08SXue jiufei 
44619f99ad08SXue jiufei 	return status;
4462328d5752SMark Fasheh }
4463328d5752SMark Fasheh 
44641ef61b33SJoel Becker static void ocfs2_figure_contig_type(struct ocfs2_extent_tree *et,
4465dcd0538fSMark Fasheh 				     struct ocfs2_insert_type *insert,
4466dcd0538fSMark Fasheh 				     struct ocfs2_extent_list *el,
44671ef61b33SJoel Becker 				     struct ocfs2_extent_rec *insert_rec)
4468dcd0538fSMark Fasheh {
4469dcd0538fSMark Fasheh 	int i;
4470dcd0538fSMark Fasheh 	enum ocfs2_contig_type contig_type = CONTIG_NONE;
4471dcd0538fSMark Fasheh 
4472e48edee2SMark Fasheh 	BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4473e48edee2SMark Fasheh 
4474dcd0538fSMark Fasheh 	for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4475853a3a14STao Ma 		contig_type = ocfs2_et_extent_contig(et, &el->l_recs[i],
4476853a3a14STao Ma 						     insert_rec);
4477dcd0538fSMark Fasheh 		if (contig_type != CONTIG_NONE) {
4478dcd0538fSMark Fasheh 			insert->ins_contig_index = i;
4479dcd0538fSMark Fasheh 			break;
4480dcd0538fSMark Fasheh 		}
4481dcd0538fSMark Fasheh 	}
4482dcd0538fSMark Fasheh 	insert->ins_contig = contig_type;
4483ca12b7c4STao Ma 
4484ca12b7c4STao Ma 	if (insert->ins_contig != CONTIG_NONE) {
4485ca12b7c4STao Ma 		struct ocfs2_extent_rec *rec =
4486ca12b7c4STao Ma 				&el->l_recs[insert->ins_contig_index];
4487ca12b7c4STao Ma 		unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4488ca12b7c4STao Ma 				   le16_to_cpu(insert_rec->e_leaf_clusters);
4489ca12b7c4STao Ma 
4490ca12b7c4STao Ma 		/*
4491ca12b7c4STao Ma 		 * Caller might want us to limit the size of extents, don't
4492ca12b7c4STao Ma 		 * calculate contiguousness if we might exceed that limit.
4493ca12b7c4STao Ma 		 */
4494ce1d9ea6SJoel Becker 		if (et->et_max_leaf_clusters &&
4495ce1d9ea6SJoel Becker 		    (len > et->et_max_leaf_clusters))
4496ca12b7c4STao Ma 			insert->ins_contig = CONTIG_NONE;
4497ca12b7c4STao Ma 	}
4498dcd0538fSMark Fasheh }
4499dcd0538fSMark Fasheh 
4500dcd0538fSMark Fasheh /*
4501dcd0538fSMark Fasheh  * This should only be called against the righmost leaf extent list.
4502dcd0538fSMark Fasheh  *
4503dcd0538fSMark Fasheh  * ocfs2_figure_appending_type() will figure out whether we'll have to
4504dcd0538fSMark Fasheh  * insert at the tail of the rightmost leaf.
4505dcd0538fSMark Fasheh  *
4506e7d4cb6bSTao Ma  * This should also work against the root extent list for tree's with 0
4507e7d4cb6bSTao Ma  * depth. If we consider the root extent list to be the rightmost leaf node
4508dcd0538fSMark Fasheh  * then the logic here makes sense.
4509dcd0538fSMark Fasheh  */
4510dcd0538fSMark Fasheh static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4511dcd0538fSMark Fasheh 					struct ocfs2_extent_list *el,
4512dcd0538fSMark Fasheh 					struct ocfs2_extent_rec *insert_rec)
4513dcd0538fSMark Fasheh {
4514dcd0538fSMark Fasheh 	int i;
4515dcd0538fSMark Fasheh 	u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4516dcd0538fSMark Fasheh 	struct ocfs2_extent_rec *rec;
4517dcd0538fSMark Fasheh 
4518dcd0538fSMark Fasheh 	insert->ins_appending = APPEND_NONE;
4519dcd0538fSMark Fasheh 
4520e48edee2SMark Fasheh 	BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4521dcd0538fSMark Fasheh 
4522dcd0538fSMark Fasheh 	if (!el->l_next_free_rec)
4523dcd0538fSMark Fasheh 		goto set_tail_append;
4524dcd0538fSMark Fasheh 
4525dcd0538fSMark Fasheh 	if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4526dcd0538fSMark Fasheh 		/* Were all records empty? */
4527dcd0538fSMark Fasheh 		if (le16_to_cpu(el->l_next_free_rec) == 1)
4528dcd0538fSMark Fasheh 			goto set_tail_append;
4529dcd0538fSMark Fasheh 	}
4530dcd0538fSMark Fasheh 
4531dcd0538fSMark Fasheh 	i = le16_to_cpu(el->l_next_free_rec) - 1;
4532dcd0538fSMark Fasheh 	rec = &el->l_recs[i];
4533dcd0538fSMark Fasheh 
4534e48edee2SMark Fasheh 	if (cpos >=
4535e48edee2SMark Fasheh 	    (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
4536dcd0538fSMark Fasheh 		goto set_tail_append;
4537dcd0538fSMark Fasheh 
4538dcd0538fSMark Fasheh 	return;
4539dcd0538fSMark Fasheh 
4540dcd0538fSMark Fasheh set_tail_append:
4541dcd0538fSMark Fasheh 	insert->ins_appending = APPEND_TAIL;
4542dcd0538fSMark Fasheh }
4543dcd0538fSMark Fasheh 
4544dcd0538fSMark Fasheh /*
454525985edcSLucas De Marchi  * Helper function called at the beginning of an insert.
4546dcd0538fSMark Fasheh  *
4547dcd0538fSMark Fasheh  * This computes a few things that are commonly used in the process of
4548dcd0538fSMark Fasheh  * inserting into the btree:
4549dcd0538fSMark Fasheh  *   - Whether the new extent is contiguous with an existing one.
4550dcd0538fSMark Fasheh  *   - The current tree depth.
4551dcd0538fSMark Fasheh  *   - Whether the insert is an appending one.
4552dcd0538fSMark Fasheh  *   - The total # of free records in the tree.
4553dcd0538fSMark Fasheh  *
4554dcd0538fSMark Fasheh  * All of the information is stored on the ocfs2_insert_type
4555dcd0538fSMark Fasheh  * structure.
4556dcd0538fSMark Fasheh  */
4557627961b7SJoel Becker static int ocfs2_figure_insert_type(struct ocfs2_extent_tree *et,
4558dcd0538fSMark Fasheh 				    struct buffer_head **last_eb_bh,
4559dcd0538fSMark Fasheh 				    struct ocfs2_extent_rec *insert_rec,
4560c77534f6STao Mao 				    int *free_records,
4561dcd0538fSMark Fasheh 				    struct ocfs2_insert_type *insert)
4562dcd0538fSMark Fasheh {
4563dcd0538fSMark Fasheh 	int ret;
4564dcd0538fSMark Fasheh 	struct ocfs2_extent_block *eb;
4565dcd0538fSMark Fasheh 	struct ocfs2_extent_list *el;
4566dcd0538fSMark Fasheh 	struct ocfs2_path *path = NULL;
4567dcd0538fSMark Fasheh 	struct buffer_head *bh = NULL;
4568dcd0538fSMark Fasheh 
4569328d5752SMark Fasheh 	insert->ins_split = SPLIT_NONE;
4570328d5752SMark Fasheh 
4571ce1d9ea6SJoel Becker 	el = et->et_root_el;
4572dcd0538fSMark Fasheh 	insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4573dcd0538fSMark Fasheh 
4574dcd0538fSMark Fasheh 	if (el->l_tree_depth) {
4575dcd0538fSMark Fasheh 		/*
4576dcd0538fSMark Fasheh 		 * If we have tree depth, we read in the
4577dcd0538fSMark Fasheh 		 * rightmost extent block ahead of time as
4578dcd0538fSMark Fasheh 		 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4579dcd0538fSMark Fasheh 		 * may want it later.
4580dcd0538fSMark Fasheh 		 */
45813d03a305SJoel Becker 		ret = ocfs2_read_extent_block(et->et_ci,
45825e96581aSJoel Becker 					      ocfs2_et_get_last_eb_blk(et),
45835e96581aSJoel Becker 					      &bh);
4584dcd0538fSMark Fasheh 		if (ret) {
4585c1e8d35eSTao Ma 			mlog_errno(ret);
4586dcd0538fSMark Fasheh 			goto out;
4587dcd0538fSMark Fasheh 		}
4588dcd0538fSMark Fasheh 		eb = (struct ocfs2_extent_block *) bh->b_data;
4589dcd0538fSMark Fasheh 		el = &eb->h_list;
4590dcd0538fSMark Fasheh 	}
4591dcd0538fSMark Fasheh 
4592dcd0538fSMark Fasheh 	/*
4593dcd0538fSMark Fasheh 	 * Unless we have a contiguous insert, we'll need to know if
4594dcd0538fSMark Fasheh 	 * there is room left in our allocation tree for another
4595dcd0538fSMark Fasheh 	 * extent record.
4596dcd0538fSMark Fasheh 	 *
4597dcd0538fSMark Fasheh 	 * XXX: This test is simplistic, we can search for empty
4598dcd0538fSMark Fasheh 	 * extent records too.
4599dcd0538fSMark Fasheh 	 */
4600c77534f6STao Mao 	*free_records = le16_to_cpu(el->l_count) -
4601dcd0538fSMark Fasheh 		le16_to_cpu(el->l_next_free_rec);
4602dcd0538fSMark Fasheh 
4603dcd0538fSMark Fasheh 	if (!insert->ins_tree_depth) {
46041ef61b33SJoel Becker 		ocfs2_figure_contig_type(et, insert, el, insert_rec);
4605dcd0538fSMark Fasheh 		ocfs2_figure_appending_type(insert, el, insert_rec);
4606dcd0538fSMark Fasheh 		return 0;
4607dcd0538fSMark Fasheh 	}
4608dcd0538fSMark Fasheh 
4609ffdd7a54SJoel Becker 	path = ocfs2_new_path_from_et(et);
4610dcd0538fSMark Fasheh 	if (!path) {
4611dcd0538fSMark Fasheh 		ret = -ENOMEM;
4612dcd0538fSMark Fasheh 		mlog_errno(ret);
4613dcd0538fSMark Fasheh 		goto out;
4614dcd0538fSMark Fasheh 	}
4615dcd0538fSMark Fasheh 
4616dcd0538fSMark Fasheh 	/*
4617dcd0538fSMark Fasheh 	 * In the case that we're inserting past what the tree
4618dcd0538fSMark Fasheh 	 * currently accounts for, ocfs2_find_path() will return for
4619dcd0538fSMark Fasheh 	 * us the rightmost tree path. This is accounted for below in
4620dcd0538fSMark Fasheh 	 * the appending code.
4621dcd0538fSMark Fasheh 	 */
4622facdb77fSJoel Becker 	ret = ocfs2_find_path(et->et_ci, path, le32_to_cpu(insert_rec->e_cpos));
4623dcd0538fSMark Fasheh 	if (ret) {
4624dcd0538fSMark Fasheh 		mlog_errno(ret);
4625dcd0538fSMark Fasheh 		goto out;
4626dcd0538fSMark Fasheh 	}
4627dcd0538fSMark Fasheh 
4628dcd0538fSMark Fasheh 	el = path_leaf_el(path);
4629dcd0538fSMark Fasheh 
4630dcd0538fSMark Fasheh 	/*
4631dcd0538fSMark Fasheh 	 * Now that we have the path, there's two things we want to determine:
4632dcd0538fSMark Fasheh 	 * 1) Contiguousness (also set contig_index if this is so)
4633dcd0538fSMark Fasheh 	 *
4634dcd0538fSMark Fasheh 	 * 2) Are we doing an append? We can trivially break this up
4635dcd0538fSMark Fasheh          *     into two types of appends: simple record append, or a
4636dcd0538fSMark Fasheh          *     rotate inside the tail leaf.
4637dcd0538fSMark Fasheh 	 */
46381ef61b33SJoel Becker 	ocfs2_figure_contig_type(et, insert, el, insert_rec);
4639dcd0538fSMark Fasheh 
4640dcd0538fSMark Fasheh 	/*
4641dcd0538fSMark Fasheh 	 * The insert code isn't quite ready to deal with all cases of
4642dcd0538fSMark Fasheh 	 * left contiguousness. Specifically, if it's an insert into
4643dcd0538fSMark Fasheh 	 * the 1st record in a leaf, it will require the adjustment of
4644e48edee2SMark Fasheh 	 * cluster count on the last record of the path directly to it's
4645dcd0538fSMark Fasheh 	 * left. For now, just catch that case and fool the layers
4646dcd0538fSMark Fasheh 	 * above us. This works just fine for tree_depth == 0, which
4647dcd0538fSMark Fasheh 	 * is why we allow that above.
4648dcd0538fSMark Fasheh 	 */
4649dcd0538fSMark Fasheh 	if (insert->ins_contig == CONTIG_LEFT &&
4650dcd0538fSMark Fasheh 	    insert->ins_contig_index == 0)
4651dcd0538fSMark Fasheh 		insert->ins_contig = CONTIG_NONE;
4652dcd0538fSMark Fasheh 
4653dcd0538fSMark Fasheh 	/*
4654dcd0538fSMark Fasheh 	 * Ok, so we can simply compare against last_eb to figure out
4655dcd0538fSMark Fasheh 	 * whether the path doesn't exist. This will only happen in
4656dcd0538fSMark Fasheh 	 * the case that we're doing a tail append, so maybe we can
4657dcd0538fSMark Fasheh 	 * take advantage of that information somehow.
4658dcd0538fSMark Fasheh 	 */
465935dc0aa3SJoel Becker 	if (ocfs2_et_get_last_eb_blk(et) ==
4660e7d4cb6bSTao Ma 	    path_leaf_bh(path)->b_blocknr) {
4661dcd0538fSMark Fasheh 		/*
4662dcd0538fSMark Fasheh 		 * Ok, ocfs2_find_path() returned us the rightmost
4663dcd0538fSMark Fasheh 		 * tree path. This might be an appending insert. There are
4664dcd0538fSMark Fasheh 		 * two cases:
4665dcd0538fSMark Fasheh 		 *    1) We're doing a true append at the tail:
4666dcd0538fSMark Fasheh 		 *	-This might even be off the end of the leaf
4667dcd0538fSMark Fasheh 		 *    2) We're "appending" by rotating in the tail
4668dcd0538fSMark Fasheh 		 */
4669dcd0538fSMark Fasheh 		ocfs2_figure_appending_type(insert, el, insert_rec);
4670dcd0538fSMark Fasheh 	}
4671dcd0538fSMark Fasheh 
4672dcd0538fSMark Fasheh out:
4673dcd0538fSMark Fasheh 	ocfs2_free_path(path);
4674dcd0538fSMark Fasheh 
4675dcd0538fSMark Fasheh 	if (ret == 0)
4676dcd0538fSMark Fasheh 		*last_eb_bh = bh;
4677dcd0538fSMark Fasheh 	else
4678dcd0538fSMark Fasheh 		brelse(bh);
4679dcd0538fSMark Fasheh 	return ret;
4680dcd0538fSMark Fasheh }
4681dcd0538fSMark Fasheh 
4682dcd0538fSMark Fasheh /*
4683cc79d8c1SJoel Becker  * Insert an extent into a btree.
4684dcd0538fSMark Fasheh  *
4685cc79d8c1SJoel Becker  * The caller needs to update the owning btree's cluster count.
4686dcd0538fSMark Fasheh  */
4687cc79d8c1SJoel Becker int ocfs2_insert_extent(handle_t *handle,
4688f99b9b7cSJoel Becker 			struct ocfs2_extent_tree *et,
4689dcd0538fSMark Fasheh 			u32 cpos,
4690ccd979bdSMark Fasheh 			u64 start_blk,
4691ccd979bdSMark Fasheh 			u32 new_clusters,
46922ae99a60SMark Fasheh 			u8 flags,
4693f99b9b7cSJoel Becker 			struct ocfs2_alloc_context *meta_ac)
4694ccd979bdSMark Fasheh {
4695c3afcbb3SMark Fasheh 	int status;
4696c77534f6STao Mao 	int uninitialized_var(free_records);
4697ccd979bdSMark Fasheh 	struct buffer_head *last_eb_bh = NULL;
4698dcd0538fSMark Fasheh 	struct ocfs2_insert_type insert = {0, };
4699dcd0538fSMark Fasheh 	struct ocfs2_extent_rec rec;
4700ccd979bdSMark Fasheh 
4701a09d09b8STao Ma 	trace_ocfs2_insert_extent_start(
4702a09d09b8STao Ma 		(unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
4703a09d09b8STao Ma 		cpos, new_clusters);
4704ccd979bdSMark Fasheh 
4705e48edee2SMark Fasheh 	memset(&rec, 0, sizeof(rec));
4706dcd0538fSMark Fasheh 	rec.e_cpos = cpu_to_le32(cpos);
4707dcd0538fSMark Fasheh 	rec.e_blkno = cpu_to_le64(start_blk);
4708e48edee2SMark Fasheh 	rec.e_leaf_clusters = cpu_to_le16(new_clusters);
47092ae99a60SMark Fasheh 	rec.e_flags = flags;
47106136ca5fSJoel Becker 	status = ocfs2_et_insert_check(et, &rec);
47111e61ee79SJoel Becker 	if (status) {
47121e61ee79SJoel Becker 		mlog_errno(status);
47131e61ee79SJoel Becker 		goto bail;
47141e61ee79SJoel Becker 	}
4715ccd979bdSMark Fasheh 
4716627961b7SJoel Becker 	status = ocfs2_figure_insert_type(et, &last_eb_bh, &rec,
4717c77534f6STao Mao 					  &free_records, &insert);
4718ccd979bdSMark Fasheh 	if (status < 0) {
4719dcd0538fSMark Fasheh 		mlog_errno(status);
4720ccd979bdSMark Fasheh 		goto bail;
4721ccd979bdSMark Fasheh 	}
4722ccd979bdSMark Fasheh 
4723a09d09b8STao Ma 	trace_ocfs2_insert_extent(insert.ins_appending, insert.ins_contig,
4724a09d09b8STao Ma 				  insert.ins_contig_index, free_records,
4725a09d09b8STao Ma 				  insert.ins_tree_depth);
4726dcd0538fSMark Fasheh 
4727c77534f6STao Mao 	if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
4728d401dc12SJoel Becker 		status = ocfs2_grow_tree(handle, et,
4729328d5752SMark Fasheh 					 &insert.ins_tree_depth, &last_eb_bh,
4730ccd979bdSMark Fasheh 					 meta_ac);
4731c3afcbb3SMark Fasheh 		if (status) {
4732ccd979bdSMark Fasheh 			mlog_errno(status);
4733ccd979bdSMark Fasheh 			goto bail;
4734ccd979bdSMark Fasheh 		}
4735c3afcbb3SMark Fasheh 	}
4736ccd979bdSMark Fasheh 
4737dcd0538fSMark Fasheh 	/* Finally, we can add clusters. This might rotate the tree for us. */
47383505bec0SJoel Becker 	status = ocfs2_do_insert_extent(handle, et, &rec, &insert);
4739ccd979bdSMark Fasheh 	if (status < 0)
4740ccd979bdSMark Fasheh 		mlog_errno(status);
474192ba470cSJoel Becker 	else
474292ba470cSJoel Becker 		ocfs2_et_extent_map_insert(et, &rec);
4743ccd979bdSMark Fasheh 
4744ccd979bdSMark Fasheh bail:
4745ccd979bdSMark Fasheh 	brelse(last_eb_bh);
4746ccd979bdSMark Fasheh 
4747f56654c4STao Ma 	return status;
4748f56654c4STao Ma }
4749f56654c4STao Ma 
47500eb8d47eSTao Ma /*
47510eb8d47eSTao Ma  * Allcate and add clusters into the extent b-tree.
47520eb8d47eSTao Ma  * The new clusters(clusters_to_add) will be inserted at logical_offset.
4753f99b9b7cSJoel Becker  * The extent b-tree's root is specified by et, and
47540eb8d47eSTao Ma  * it is not limited to the file storage. Any extent tree can use this
47550eb8d47eSTao Ma  * function if it implements the proper ocfs2_extent_tree.
47560eb8d47eSTao Ma  */
4757cbee7e1aSJoel Becker int ocfs2_add_clusters_in_btree(handle_t *handle,
4758cbee7e1aSJoel Becker 				struct ocfs2_extent_tree *et,
47590eb8d47eSTao Ma 				u32 *logical_offset,
47600eb8d47eSTao Ma 				u32 clusters_to_add,
47610eb8d47eSTao Ma 				int mark_unwritten,
47620eb8d47eSTao Ma 				struct ocfs2_alloc_context *data_ac,
47630eb8d47eSTao Ma 				struct ocfs2_alloc_context *meta_ac,
4764f99b9b7cSJoel Becker 				enum ocfs2_alloc_restarted *reason_ret)
47650eb8d47eSTao Ma {
4766a09d09b8STao Ma 	int status = 0, err = 0;
4767fb951eb5SZongxun Wang 	int need_free = 0;
47680eb8d47eSTao Ma 	int free_extents;
47690eb8d47eSTao Ma 	enum ocfs2_alloc_restarted reason = RESTART_NONE;
47700eb8d47eSTao Ma 	u32 bit_off, num_bits;
47710eb8d47eSTao Ma 	u64 block;
47720eb8d47eSTao Ma 	u8 flags = 0;
4773cbee7e1aSJoel Becker 	struct ocfs2_super *osb =
4774cbee7e1aSJoel Becker 		OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
47750eb8d47eSTao Ma 
47760eb8d47eSTao Ma 	BUG_ON(!clusters_to_add);
47770eb8d47eSTao Ma 
47780eb8d47eSTao Ma 	if (mark_unwritten)
47790eb8d47eSTao Ma 		flags = OCFS2_EXT_UNWRITTEN;
47800eb8d47eSTao Ma 
4781964f14a0SJun Piao 	free_extents = ocfs2_num_free_extents(et);
47820eb8d47eSTao Ma 	if (free_extents < 0) {
47830eb8d47eSTao Ma 		status = free_extents;
47840eb8d47eSTao Ma 		mlog_errno(status);
47850eb8d47eSTao Ma 		goto leave;
47860eb8d47eSTao Ma 	}
47870eb8d47eSTao Ma 
47880eb8d47eSTao Ma 	/* there are two cases which could cause us to EAGAIN in the
47890eb8d47eSTao Ma 	 * we-need-more-metadata case:
47900eb8d47eSTao Ma 	 * 1) we haven't reserved *any*
47910eb8d47eSTao Ma 	 * 2) we are so fragmented, we've needed to add metadata too
47920eb8d47eSTao Ma 	 *    many times. */
47930eb8d47eSTao Ma 	if (!free_extents && !meta_ac) {
4794a09d09b8STao Ma 		err = -1;
47950eb8d47eSTao Ma 		status = -EAGAIN;
47960eb8d47eSTao Ma 		reason = RESTART_META;
47970eb8d47eSTao Ma 		goto leave;
47980eb8d47eSTao Ma 	} else if ((!free_extents)
47990eb8d47eSTao Ma 		   && (ocfs2_alloc_context_bits_left(meta_ac)
4800f99b9b7cSJoel Becker 		       < ocfs2_extend_meta_needed(et->et_root_el))) {
4801a09d09b8STao Ma 		err = -2;
48020eb8d47eSTao Ma 		status = -EAGAIN;
48030eb8d47eSTao Ma 		reason = RESTART_META;
48040eb8d47eSTao Ma 		goto leave;
48050eb8d47eSTao Ma 	}
48060eb8d47eSTao Ma 
48071ed9b777SJoel Becker 	status = __ocfs2_claim_clusters(handle, data_ac, 1,
48080eb8d47eSTao Ma 					clusters_to_add, &bit_off, &num_bits);
48090eb8d47eSTao Ma 	if (status < 0) {
48100eb8d47eSTao Ma 		if (status != -ENOSPC)
48110eb8d47eSTao Ma 			mlog_errno(status);
48120eb8d47eSTao Ma 		goto leave;
48130eb8d47eSTao Ma 	}
48140eb8d47eSTao Ma 
48150eb8d47eSTao Ma 	BUG_ON(num_bits > clusters_to_add);
48160eb8d47eSTao Ma 
481713723d00SJoel Becker 	/* reserve our write early -- insert_extent may update the tree root */
4818d9a0a1f8SJoel Becker 	status = ocfs2_et_root_journal_access(handle, et,
48190eb8d47eSTao Ma 					      OCFS2_JOURNAL_ACCESS_WRITE);
48200eb8d47eSTao Ma 	if (status < 0) {
48210eb8d47eSTao Ma 		mlog_errno(status);
4822fb951eb5SZongxun Wang 		need_free = 1;
4823fb951eb5SZongxun Wang 		goto bail;
48240eb8d47eSTao Ma 	}
48250eb8d47eSTao Ma 
48260eb8d47eSTao Ma 	block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4827a09d09b8STao Ma 	trace_ocfs2_add_clusters_in_btree(
4828a09d09b8STao Ma 	     (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
4829a09d09b8STao Ma 	     bit_off, num_bits);
4830cc79d8c1SJoel Becker 	status = ocfs2_insert_extent(handle, et, *logical_offset, block,
4831f56654c4STao Ma 				     num_bits, flags, meta_ac);
48320eb8d47eSTao Ma 	if (status < 0) {
48330eb8d47eSTao Ma 		mlog_errno(status);
4834fb951eb5SZongxun Wang 		need_free = 1;
4835fb951eb5SZongxun Wang 		goto bail;
48360eb8d47eSTao Ma 	}
48370eb8d47eSTao Ma 
4838ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, et->et_root_bh);
48390eb8d47eSTao Ma 
48400eb8d47eSTao Ma 	clusters_to_add -= num_bits;
48410eb8d47eSTao Ma 	*logical_offset += num_bits;
48420eb8d47eSTao Ma 
48430eb8d47eSTao Ma 	if (clusters_to_add) {
4844a09d09b8STao Ma 		err = clusters_to_add;
48450eb8d47eSTao Ma 		status = -EAGAIN;
48460eb8d47eSTao Ma 		reason = RESTART_TRANS;
48470eb8d47eSTao Ma 	}
48480eb8d47eSTao Ma 
4849fb951eb5SZongxun Wang bail:
4850fb951eb5SZongxun Wang 	if (need_free) {
4851fb951eb5SZongxun Wang 		if (data_ac->ac_which == OCFS2_AC_USE_LOCAL)
4852fb951eb5SZongxun Wang 			ocfs2_free_local_alloc_bits(osb, handle, data_ac,
4853fb951eb5SZongxun Wang 					bit_off, num_bits);
4854fb951eb5SZongxun Wang 		else
4855fb951eb5SZongxun Wang 			ocfs2_free_clusters(handle,
4856fb951eb5SZongxun Wang 					data_ac->ac_inode,
4857fb951eb5SZongxun Wang 					data_ac->ac_bh,
4858fb951eb5SZongxun Wang 					ocfs2_clusters_to_blocks(osb->sb, bit_off),
4859fb951eb5SZongxun Wang 					num_bits);
4860fb951eb5SZongxun Wang 	}
4861fb951eb5SZongxun Wang 
48620eb8d47eSTao Ma leave:
48630eb8d47eSTao Ma 	if (reason_ret)
48640eb8d47eSTao Ma 		*reason_ret = reason;
4865a09d09b8STao Ma 	trace_ocfs2_add_clusters_in_btree_ret(status, reason, err);
48660eb8d47eSTao Ma 	return status;
48670eb8d47eSTao Ma }
48680eb8d47eSTao Ma 
4869328d5752SMark Fasheh static void ocfs2_make_right_split_rec(struct super_block *sb,
4870328d5752SMark Fasheh 				       struct ocfs2_extent_rec *split_rec,
4871328d5752SMark Fasheh 				       u32 cpos,
4872328d5752SMark Fasheh 				       struct ocfs2_extent_rec *rec)
4873328d5752SMark Fasheh {
4874328d5752SMark Fasheh 	u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4875328d5752SMark Fasheh 	u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4876328d5752SMark Fasheh 
4877328d5752SMark Fasheh 	memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4878328d5752SMark Fasheh 
4879328d5752SMark Fasheh 	split_rec->e_cpos = cpu_to_le32(cpos);
4880328d5752SMark Fasheh 	split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4881328d5752SMark Fasheh 
4882328d5752SMark Fasheh 	split_rec->e_blkno = rec->e_blkno;
4883328d5752SMark Fasheh 	le64_add_cpu(&split_rec->e_blkno,
4884328d5752SMark Fasheh 		     ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4885328d5752SMark Fasheh 
4886328d5752SMark Fasheh 	split_rec->e_flags = rec->e_flags;
4887328d5752SMark Fasheh }
4888328d5752SMark Fasheh 
4889d231129fSJoel Becker static int ocfs2_split_and_insert(handle_t *handle,
4890e7d4cb6bSTao Ma 				  struct ocfs2_extent_tree *et,
4891d231129fSJoel Becker 				  struct ocfs2_path *path,
4892328d5752SMark Fasheh 				  struct buffer_head **last_eb_bh,
4893328d5752SMark Fasheh 				  int split_index,
4894328d5752SMark Fasheh 				  struct ocfs2_extent_rec *orig_split_rec,
4895328d5752SMark Fasheh 				  struct ocfs2_alloc_context *meta_ac)
4896328d5752SMark Fasheh {
4897328d5752SMark Fasheh 	int ret = 0, depth;
4898328d5752SMark Fasheh 	unsigned int insert_range, rec_range, do_leftright = 0;
4899328d5752SMark Fasheh 	struct ocfs2_extent_rec tmprec;
4900328d5752SMark Fasheh 	struct ocfs2_extent_list *rightmost_el;
4901328d5752SMark Fasheh 	struct ocfs2_extent_rec rec;
4902328d5752SMark Fasheh 	struct ocfs2_extent_rec split_rec = *orig_split_rec;
4903328d5752SMark Fasheh 	struct ocfs2_insert_type insert;
4904328d5752SMark Fasheh 	struct ocfs2_extent_block *eb;
4905328d5752SMark Fasheh 
4906328d5752SMark Fasheh leftright:
4907328d5752SMark Fasheh 	/*
4908328d5752SMark Fasheh 	 * Store a copy of the record on the stack - it might move
4909328d5752SMark Fasheh 	 * around as the tree is manipulated below.
4910328d5752SMark Fasheh 	 */
4911328d5752SMark Fasheh 	rec = path_leaf_el(path)->l_recs[split_index];
4912328d5752SMark Fasheh 
4913ce1d9ea6SJoel Becker 	rightmost_el = et->et_root_el;
4914328d5752SMark Fasheh 
4915328d5752SMark Fasheh 	depth = le16_to_cpu(rightmost_el->l_tree_depth);
4916328d5752SMark Fasheh 	if (depth) {
4917328d5752SMark Fasheh 		BUG_ON(!(*last_eb_bh));
4918328d5752SMark Fasheh 		eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4919328d5752SMark Fasheh 		rightmost_el = &eb->h_list;
4920328d5752SMark Fasheh 	}
4921328d5752SMark Fasheh 
4922328d5752SMark Fasheh 	if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4923328d5752SMark Fasheh 	    le16_to_cpu(rightmost_el->l_count)) {
4924d401dc12SJoel Becker 		ret = ocfs2_grow_tree(handle, et,
4925e7d4cb6bSTao Ma 				      &depth, last_eb_bh, meta_ac);
4926328d5752SMark Fasheh 		if (ret) {
4927328d5752SMark Fasheh 			mlog_errno(ret);
4928328d5752SMark Fasheh 			goto out;
4929328d5752SMark Fasheh 		}
4930328d5752SMark Fasheh 	}
4931328d5752SMark Fasheh 
4932328d5752SMark Fasheh 	memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4933328d5752SMark Fasheh 	insert.ins_appending = APPEND_NONE;
4934328d5752SMark Fasheh 	insert.ins_contig = CONTIG_NONE;
4935328d5752SMark Fasheh 	insert.ins_tree_depth = depth;
4936328d5752SMark Fasheh 
4937328d5752SMark Fasheh 	insert_range = le32_to_cpu(split_rec.e_cpos) +
4938328d5752SMark Fasheh 		le16_to_cpu(split_rec.e_leaf_clusters);
4939328d5752SMark Fasheh 	rec_range = le32_to_cpu(rec.e_cpos) +
4940328d5752SMark Fasheh 		le16_to_cpu(rec.e_leaf_clusters);
4941328d5752SMark Fasheh 
4942328d5752SMark Fasheh 	if (split_rec.e_cpos == rec.e_cpos) {
4943328d5752SMark Fasheh 		insert.ins_split = SPLIT_LEFT;
4944328d5752SMark Fasheh 	} else if (insert_range == rec_range) {
4945328d5752SMark Fasheh 		insert.ins_split = SPLIT_RIGHT;
4946328d5752SMark Fasheh 	} else {
4947328d5752SMark Fasheh 		/*
4948328d5752SMark Fasheh 		 * Left/right split. We fake this as a right split
4949328d5752SMark Fasheh 		 * first and then make a second pass as a left split.
4950328d5752SMark Fasheh 		 */
4951328d5752SMark Fasheh 		insert.ins_split = SPLIT_RIGHT;
4952328d5752SMark Fasheh 
4953d231129fSJoel Becker 		ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
4954d231129fSJoel Becker 					   &tmprec, insert_range, &rec);
4955328d5752SMark Fasheh 
4956328d5752SMark Fasheh 		split_rec = tmprec;
4957328d5752SMark Fasheh 
4958328d5752SMark Fasheh 		BUG_ON(do_leftright);
4959328d5752SMark Fasheh 		do_leftright = 1;
4960328d5752SMark Fasheh 	}
4961328d5752SMark Fasheh 
49623505bec0SJoel Becker 	ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
4963328d5752SMark Fasheh 	if (ret) {
4964328d5752SMark Fasheh 		mlog_errno(ret);
4965328d5752SMark Fasheh 		goto out;
4966328d5752SMark Fasheh 	}
4967328d5752SMark Fasheh 
4968328d5752SMark Fasheh 	if (do_leftright == 1) {
4969328d5752SMark Fasheh 		u32 cpos;
4970328d5752SMark Fasheh 		struct ocfs2_extent_list *el;
4971328d5752SMark Fasheh 
4972328d5752SMark Fasheh 		do_leftright++;
4973328d5752SMark Fasheh 		split_rec = *orig_split_rec;
4974328d5752SMark Fasheh 
4975328d5752SMark Fasheh 		ocfs2_reinit_path(path, 1);
4976328d5752SMark Fasheh 
4977328d5752SMark Fasheh 		cpos = le32_to_cpu(split_rec.e_cpos);
4978facdb77fSJoel Becker 		ret = ocfs2_find_path(et->et_ci, path, cpos);
4979328d5752SMark Fasheh 		if (ret) {
4980328d5752SMark Fasheh 			mlog_errno(ret);
4981328d5752SMark Fasheh 			goto out;
4982328d5752SMark Fasheh 		}
4983328d5752SMark Fasheh 
4984328d5752SMark Fasheh 		el = path_leaf_el(path);
4985328d5752SMark Fasheh 		split_index = ocfs2_search_extent_list(el, cpos);
4986981035b4SYingtai Xie 		if (split_index == -1) {
4987981035b4SYingtai Xie 			ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
49887ecef14aSJoe Perches 				    "Owner %llu has an extent at cpos %u which can no longer be found\n",
4989981035b4SYingtai Xie 				    (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
4990981035b4SYingtai Xie 				    cpos);
4991981035b4SYingtai Xie 			ret = -EROFS;
4992981035b4SYingtai Xie 			goto out;
4993981035b4SYingtai Xie 		}
4994328d5752SMark Fasheh 		goto leftright;
4995328d5752SMark Fasheh 	}
4996328d5752SMark Fasheh out:
4997328d5752SMark Fasheh 
4998328d5752SMark Fasheh 	return ret;
4999328d5752SMark Fasheh }
5000328d5752SMark Fasheh 
5001f3868d0fSJoel Becker static int ocfs2_replace_extent_rec(handle_t *handle,
5002f3868d0fSJoel Becker 				    struct ocfs2_extent_tree *et,
500347be12e4STao Ma 				    struct ocfs2_path *path,
500447be12e4STao Ma 				    struct ocfs2_extent_list *el,
500547be12e4STao Ma 				    int split_index,
500647be12e4STao Ma 				    struct ocfs2_extent_rec *split_rec)
500747be12e4STao Ma {
500847be12e4STao Ma 	int ret;
500947be12e4STao Ma 
5010f3868d0fSJoel Becker 	ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
501147be12e4STao Ma 					   path_num_items(path) - 1);
501247be12e4STao Ma 	if (ret) {
501347be12e4STao Ma 		mlog_errno(ret);
501447be12e4STao Ma 		goto out;
501547be12e4STao Ma 	}
501647be12e4STao Ma 
501747be12e4STao Ma 	el->l_recs[split_index] = *split_rec;
501847be12e4STao Ma 
501947be12e4STao Ma 	ocfs2_journal_dirty(handle, path_leaf_bh(path));
502047be12e4STao Ma out:
502147be12e4STao Ma 	return ret;
502247be12e4STao Ma }
502347be12e4STao Ma 
5024328d5752SMark Fasheh /*
5025555936bfSTao Ma  * Split part or all of the extent record at split_index in the leaf
5026555936bfSTao Ma  * pointed to by path. Merge with the contiguous extent record if needed.
5027328d5752SMark Fasheh  *
5028328d5752SMark Fasheh  * Care is taken to handle contiguousness so as to not grow the tree.
5029328d5752SMark Fasheh  *
5030328d5752SMark Fasheh  * meta_ac is not strictly necessary - we only truly need it if growth
5031328d5752SMark Fasheh  * of the tree is required. All other cases will degrade into a less
5032328d5752SMark Fasheh  * optimal tree layout.
5033328d5752SMark Fasheh  *
5034e7d4cb6bSTao Ma  * last_eb_bh should be the rightmost leaf block for any extent
5035e7d4cb6bSTao Ma  * btree. Since a split may grow the tree or a merge might shrink it,
5036e7d4cb6bSTao Ma  * the caller cannot trust the contents of that buffer after this call.
5037328d5752SMark Fasheh  *
5038328d5752SMark Fasheh  * This code is optimized for readability - several passes might be
5039328d5752SMark Fasheh  * made over certain portions of the tree. All of those blocks will
5040328d5752SMark Fasheh  * have been brought into cache (and pinned via the journal), so the
5041328d5752SMark Fasheh  * extra overhead is not expressed in terms of disk reads.
5042328d5752SMark Fasheh  */
5043e2e9f608STao Ma int ocfs2_split_extent(handle_t *handle,
5044e7d4cb6bSTao Ma 		       struct ocfs2_extent_tree *et,
5045328d5752SMark Fasheh 		       struct ocfs2_path *path,
5046328d5752SMark Fasheh 		       int split_index,
5047328d5752SMark Fasheh 		       struct ocfs2_extent_rec *split_rec,
5048328d5752SMark Fasheh 		       struct ocfs2_alloc_context *meta_ac,
5049328d5752SMark Fasheh 		       struct ocfs2_cached_dealloc_ctxt *dealloc)
5050328d5752SMark Fasheh {
5051328d5752SMark Fasheh 	int ret = 0;
5052328d5752SMark Fasheh 	struct ocfs2_extent_list *el = path_leaf_el(path);
5053e8aed345SMark Fasheh 	struct buffer_head *last_eb_bh = NULL;
5054328d5752SMark Fasheh 	struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
5055328d5752SMark Fasheh 	struct ocfs2_merge_ctxt ctxt;
5056328d5752SMark Fasheh 
5057328d5752SMark Fasheh 	if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
5058328d5752SMark Fasheh 	    ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
5059328d5752SMark Fasheh 	     (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
5060328d5752SMark Fasheh 		ret = -EIO;
5061328d5752SMark Fasheh 		mlog_errno(ret);
5062328d5752SMark Fasheh 		goto out;
5063328d5752SMark Fasheh 	}
5064328d5752SMark Fasheh 
50659f99ad08SXue jiufei 	ret = ocfs2_figure_merge_contig_type(et, path, el,
5066328d5752SMark Fasheh 					     split_index,
50679f99ad08SXue jiufei 					     split_rec,
50689f99ad08SXue jiufei 					     &ctxt);
50699f99ad08SXue jiufei 	if (ret) {
50709f99ad08SXue jiufei 		mlog_errno(ret);
50719f99ad08SXue jiufei 		goto out;
50729f99ad08SXue jiufei 	}
5073328d5752SMark Fasheh 
5074328d5752SMark Fasheh 	/*
5075328d5752SMark Fasheh 	 * The core merge / split code wants to know how much room is
5076a1cf076bSJoel Becker 	 * left in this allocation tree, so we pass the
5077328d5752SMark Fasheh 	 * rightmost extent list.
5078328d5752SMark Fasheh 	 */
5079328d5752SMark Fasheh 	if (path->p_tree_depth) {
5080328d5752SMark Fasheh 		struct ocfs2_extent_block *eb;
5081328d5752SMark Fasheh 
50823d03a305SJoel Becker 		ret = ocfs2_read_extent_block(et->et_ci,
50835e96581aSJoel Becker 					      ocfs2_et_get_last_eb_blk(et),
50840fcaa56aSJoel Becker 					      &last_eb_bh);
5085328d5752SMark Fasheh 		if (ret) {
5086c1e8d35eSTao Ma 			mlog_errno(ret);
5087328d5752SMark Fasheh 			goto out;
5088328d5752SMark Fasheh 		}
5089328d5752SMark Fasheh 
5090328d5752SMark Fasheh 		eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5091d22aa615SChangwei Ge 	}
5092328d5752SMark Fasheh 
5093328d5752SMark Fasheh 	if (rec->e_cpos == split_rec->e_cpos &&
5094328d5752SMark Fasheh 	    rec->e_leaf_clusters == split_rec->e_leaf_clusters)
5095328d5752SMark Fasheh 		ctxt.c_split_covers_rec = 1;
5096328d5752SMark Fasheh 	else
5097328d5752SMark Fasheh 		ctxt.c_split_covers_rec = 0;
5098328d5752SMark Fasheh 
5099328d5752SMark Fasheh 	ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
5100328d5752SMark Fasheh 
5101a09d09b8STao Ma 	trace_ocfs2_split_extent(split_index, ctxt.c_contig_type,
5102a09d09b8STao Ma 				 ctxt.c_has_empty_extent,
5103015452b1SMark Fasheh 				 ctxt.c_split_covers_rec);
5104328d5752SMark Fasheh 
5105328d5752SMark Fasheh 	if (ctxt.c_contig_type == CONTIG_NONE) {
5106328d5752SMark Fasheh 		if (ctxt.c_split_covers_rec)
5107f3868d0fSJoel Becker 			ret = ocfs2_replace_extent_rec(handle, et, path, el,
510847be12e4STao Ma 						       split_index, split_rec);
5109328d5752SMark Fasheh 		else
5110d231129fSJoel Becker 			ret = ocfs2_split_and_insert(handle, et, path,
5111328d5752SMark Fasheh 						     &last_eb_bh, split_index,
5112328d5752SMark Fasheh 						     split_rec, meta_ac);
5113328d5752SMark Fasheh 		if (ret)
5114328d5752SMark Fasheh 			mlog_errno(ret);
5115328d5752SMark Fasheh 	} else {
5116c495dd24SJoel Becker 		ret = ocfs2_try_to_merge_extent(handle, et, path,
5117328d5752SMark Fasheh 						split_index, split_rec,
5118c495dd24SJoel Becker 						dealloc, &ctxt);
5119328d5752SMark Fasheh 		if (ret)
5120328d5752SMark Fasheh 			mlog_errno(ret);
5121328d5752SMark Fasheh 	}
5122328d5752SMark Fasheh 
5123328d5752SMark Fasheh out:
5124328d5752SMark Fasheh 	brelse(last_eb_bh);
5125328d5752SMark Fasheh 	return ret;
5126328d5752SMark Fasheh }
5127328d5752SMark Fasheh 
5128328d5752SMark Fasheh /*
5129555936bfSTao Ma  * Change the flags of the already-existing extent at cpos for len clusters.
5130555936bfSTao Ma  *
5131555936bfSTao Ma  * new_flags: the flags we want to set.
5132555936bfSTao Ma  * clear_flags: the flags we want to clear.
5133555936bfSTao Ma  * phys: the new physical offset we want this new extent starts from.
5134328d5752SMark Fasheh  *
5135328d5752SMark Fasheh  * If the existing extent is larger than the request, initiate a
5136328d5752SMark Fasheh  * split. An attempt will be made at merging with adjacent extents.
5137328d5752SMark Fasheh  *
5138328d5752SMark Fasheh  * The caller is responsible for passing down meta_ac if we'll need it.
5139328d5752SMark Fasheh  */
51401aa75feaSTao Ma int ocfs2_change_extent_flag(handle_t *handle,
5141f99b9b7cSJoel Becker 			     struct ocfs2_extent_tree *et,
5142555936bfSTao Ma 			     u32 cpos, u32 len, u32 phys,
5143328d5752SMark Fasheh 			     struct ocfs2_alloc_context *meta_ac,
5144555936bfSTao Ma 			     struct ocfs2_cached_dealloc_ctxt *dealloc,
5145555936bfSTao Ma 			     int new_flags, int clear_flags)
5146328d5752SMark Fasheh {
5147328d5752SMark Fasheh 	int ret, index;
5148555936bfSTao Ma 	struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
5149555936bfSTao Ma 	u64 start_blkno = ocfs2_clusters_to_blocks(sb, phys);
5150328d5752SMark Fasheh 	struct ocfs2_extent_rec split_rec;
5151328d5752SMark Fasheh 	struct ocfs2_path *left_path = NULL;
5152328d5752SMark Fasheh 	struct ocfs2_extent_list *el;
5153555936bfSTao Ma 	struct ocfs2_extent_rec *rec;
5154328d5752SMark Fasheh 
5155ffdd7a54SJoel Becker 	left_path = ocfs2_new_path_from_et(et);
5156328d5752SMark Fasheh 	if (!left_path) {
5157328d5752SMark Fasheh 		ret = -ENOMEM;
5158328d5752SMark Fasheh 		mlog_errno(ret);
5159328d5752SMark Fasheh 		goto out;
5160328d5752SMark Fasheh 	}
5161328d5752SMark Fasheh 
5162facdb77fSJoel Becker 	ret = ocfs2_find_path(et->et_ci, left_path, cpos);
5163328d5752SMark Fasheh 	if (ret) {
5164328d5752SMark Fasheh 		mlog_errno(ret);
5165328d5752SMark Fasheh 		goto out;
5166328d5752SMark Fasheh 	}
5167328d5752SMark Fasheh 	el = path_leaf_el(left_path);
5168328d5752SMark Fasheh 
5169328d5752SMark Fasheh 	index = ocfs2_search_extent_list(el, cpos);
5170981035b4SYingtai Xie 	if (index == -1) {
5171555936bfSTao Ma 		ocfs2_error(sb,
51727ecef14aSJoe Perches 			    "Owner %llu has an extent at cpos %u which can no longer be found\n",
51737ecef14aSJoe Perches 			    (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
51747ecef14aSJoe Perches 			    cpos);
5175328d5752SMark Fasheh 		ret = -EROFS;
5176328d5752SMark Fasheh 		goto out;
5177328d5752SMark Fasheh 	}
5178328d5752SMark Fasheh 
5179555936bfSTao Ma 	ret = -EIO;
5180555936bfSTao Ma 	rec = &el->l_recs[index];
5181555936bfSTao Ma 	if (new_flags && (rec->e_flags & new_flags)) {
5182555936bfSTao Ma 		mlog(ML_ERROR, "Owner %llu tried to set %d flags on an "
518386544fbdSDarrick J. Wong 		     "extent that already had them\n",
5184555936bfSTao Ma 		     (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5185555936bfSTao Ma 		     new_flags);
5186555936bfSTao Ma 		goto out;
5187555936bfSTao Ma 	}
5188555936bfSTao Ma 
5189555936bfSTao Ma 	if (clear_flags && !(rec->e_flags & clear_flags)) {
5190555936bfSTao Ma 		mlog(ML_ERROR, "Owner %llu tried to clear %d flags on an "
519186544fbdSDarrick J. Wong 		     "extent that didn't have them\n",
5192555936bfSTao Ma 		     (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5193555936bfSTao Ma 		     clear_flags);
5194555936bfSTao Ma 		goto out;
5195555936bfSTao Ma 	}
5196555936bfSTao Ma 
5197328d5752SMark Fasheh 	memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
5198328d5752SMark Fasheh 	split_rec.e_cpos = cpu_to_le32(cpos);
5199328d5752SMark Fasheh 	split_rec.e_leaf_clusters = cpu_to_le16(len);
5200328d5752SMark Fasheh 	split_rec.e_blkno = cpu_to_le64(start_blkno);
5201555936bfSTao Ma 	split_rec.e_flags = rec->e_flags;
5202555936bfSTao Ma 	if (new_flags)
5203555936bfSTao Ma 		split_rec.e_flags |= new_flags;
5204555936bfSTao Ma 	if (clear_flags)
5205555936bfSTao Ma 		split_rec.e_flags &= ~clear_flags;
5206328d5752SMark Fasheh 
5207e2e9f608STao Ma 	ret = ocfs2_split_extent(handle, et, left_path,
5208e7d4cb6bSTao Ma 				 index, &split_rec, meta_ac,
5209e7d4cb6bSTao Ma 				 dealloc);
5210328d5752SMark Fasheh 	if (ret)
5211328d5752SMark Fasheh 		mlog_errno(ret);
5212328d5752SMark Fasheh 
5213328d5752SMark Fasheh out:
5214328d5752SMark Fasheh 	ocfs2_free_path(left_path);
5215328d5752SMark Fasheh 	return ret;
5216555936bfSTao Ma 
5217555936bfSTao Ma }
5218555936bfSTao Ma 
5219555936bfSTao Ma /*
5220555936bfSTao Ma  * Mark the already-existing extent at cpos as written for len clusters.
5221555936bfSTao Ma  * This removes the unwritten extent flag.
5222555936bfSTao Ma  *
5223555936bfSTao Ma  * If the existing extent is larger than the request, initiate a
5224555936bfSTao Ma  * split. An attempt will be made at merging with adjacent extents.
5225555936bfSTao Ma  *
5226555936bfSTao Ma  * The caller is responsible for passing down meta_ac if we'll need it.
5227555936bfSTao Ma  */
5228555936bfSTao Ma int ocfs2_mark_extent_written(struct inode *inode,
5229555936bfSTao Ma 			      struct ocfs2_extent_tree *et,
5230555936bfSTao Ma 			      handle_t *handle, u32 cpos, u32 len, u32 phys,
5231555936bfSTao Ma 			      struct ocfs2_alloc_context *meta_ac,
5232555936bfSTao Ma 			      struct ocfs2_cached_dealloc_ctxt *dealloc)
5233555936bfSTao Ma {
5234555936bfSTao Ma 	int ret;
5235555936bfSTao Ma 
5236a09d09b8STao Ma 	trace_ocfs2_mark_extent_written(
5237a09d09b8STao Ma 		(unsigned long long)OCFS2_I(inode)->ip_blkno,
5238a09d09b8STao Ma 		cpos, len, phys);
5239555936bfSTao Ma 
5240555936bfSTao Ma 	if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
52417ecef14aSJoe Perches 		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",
5242555936bfSTao Ma 			    (unsigned long long)OCFS2_I(inode)->ip_blkno);
5243555936bfSTao Ma 		ret = -EROFS;
5244555936bfSTao Ma 		goto out;
5245555936bfSTao Ma 	}
5246555936bfSTao Ma 
5247555936bfSTao Ma 	/*
5248555936bfSTao Ma 	 * XXX: This should be fixed up so that we just re-insert the
5249555936bfSTao Ma 	 * next extent records.
5250555936bfSTao Ma 	 */
5251555936bfSTao Ma 	ocfs2_et_extent_map_truncate(et, 0);
5252555936bfSTao Ma 
5253555936bfSTao Ma 	ret = ocfs2_change_extent_flag(handle, et, cpos,
5254555936bfSTao Ma 				       len, phys, meta_ac, dealloc,
5255555936bfSTao Ma 				       0, OCFS2_EXT_UNWRITTEN);
5256555936bfSTao Ma 	if (ret)
5257555936bfSTao Ma 		mlog_errno(ret);
5258555936bfSTao Ma 
5259555936bfSTao Ma out:
5260555936bfSTao Ma 	return ret;
5261328d5752SMark Fasheh }
5262328d5752SMark Fasheh 
5263dbdcf6a4SJoel Becker static int ocfs2_split_tree(handle_t *handle, struct ocfs2_extent_tree *et,
5264dbdcf6a4SJoel Becker 			    struct ocfs2_path *path,
5265d0c7d708SMark Fasheh 			    int index, u32 new_range,
5266d0c7d708SMark Fasheh 			    struct ocfs2_alloc_context *meta_ac)
5267d0c7d708SMark Fasheh {
5268c901fb00STao Ma 	int ret, depth, credits;
5269d0c7d708SMark Fasheh 	struct buffer_head *last_eb_bh = NULL;
5270d0c7d708SMark Fasheh 	struct ocfs2_extent_block *eb;
5271d0c7d708SMark Fasheh 	struct ocfs2_extent_list *rightmost_el, *el;
5272d0c7d708SMark Fasheh 	struct ocfs2_extent_rec split_rec;
5273d0c7d708SMark Fasheh 	struct ocfs2_extent_rec *rec;
5274d0c7d708SMark Fasheh 	struct ocfs2_insert_type insert;
5275d0c7d708SMark Fasheh 
5276d0c7d708SMark Fasheh 	/*
5277d0c7d708SMark Fasheh 	 * Setup the record to split before we grow the tree.
5278d0c7d708SMark Fasheh 	 */
5279d0c7d708SMark Fasheh 	el = path_leaf_el(path);
5280d0c7d708SMark Fasheh 	rec = &el->l_recs[index];
5281dbdcf6a4SJoel Becker 	ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
5282dbdcf6a4SJoel Becker 				   &split_rec, new_range, rec);
5283d0c7d708SMark Fasheh 
5284d0c7d708SMark Fasheh 	depth = path->p_tree_depth;
5285d0c7d708SMark Fasheh 	if (depth > 0) {
52863d03a305SJoel Becker 		ret = ocfs2_read_extent_block(et->et_ci,
52875e96581aSJoel Becker 					      ocfs2_et_get_last_eb_blk(et),
52880fcaa56aSJoel Becker 					      &last_eb_bh);
5289d0c7d708SMark Fasheh 		if (ret < 0) {
5290d0c7d708SMark Fasheh 			mlog_errno(ret);
5291d0c7d708SMark Fasheh 			goto out;
5292d0c7d708SMark Fasheh 		}
5293d0c7d708SMark Fasheh 
5294d0c7d708SMark Fasheh 		eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5295d0c7d708SMark Fasheh 		rightmost_el = &eb->h_list;
5296d0c7d708SMark Fasheh 	} else
5297d0c7d708SMark Fasheh 		rightmost_el = path_leaf_el(path);
5298d0c7d708SMark Fasheh 
5299c901fb00STao Ma 	credits = path->p_tree_depth +
5300ce1d9ea6SJoel Becker 		  ocfs2_extend_meta_needed(et->et_root_el);
5301d0c7d708SMark Fasheh 	ret = ocfs2_extend_trans(handle, credits);
5302d0c7d708SMark Fasheh 	if (ret) {
5303d0c7d708SMark Fasheh 		mlog_errno(ret);
5304d0c7d708SMark Fasheh 		goto out;
5305d0c7d708SMark Fasheh 	}
5306d0c7d708SMark Fasheh 
5307d0c7d708SMark Fasheh 	if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
5308d0c7d708SMark Fasheh 	    le16_to_cpu(rightmost_el->l_count)) {
5309d401dc12SJoel Becker 		ret = ocfs2_grow_tree(handle, et, &depth, &last_eb_bh,
5310d0c7d708SMark Fasheh 				      meta_ac);
5311d0c7d708SMark Fasheh 		if (ret) {
5312d0c7d708SMark Fasheh 			mlog_errno(ret);
5313d0c7d708SMark Fasheh 			goto out;
5314d0c7d708SMark Fasheh 		}
5315d0c7d708SMark Fasheh 	}
5316d0c7d708SMark Fasheh 
5317d0c7d708SMark Fasheh 	memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5318d0c7d708SMark Fasheh 	insert.ins_appending = APPEND_NONE;
5319d0c7d708SMark Fasheh 	insert.ins_contig = CONTIG_NONE;
5320d0c7d708SMark Fasheh 	insert.ins_split = SPLIT_RIGHT;
5321d0c7d708SMark Fasheh 	insert.ins_tree_depth = depth;
5322d0c7d708SMark Fasheh 
53233505bec0SJoel Becker 	ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
5324d0c7d708SMark Fasheh 	if (ret)
5325d0c7d708SMark Fasheh 		mlog_errno(ret);
5326d0c7d708SMark Fasheh 
5327d0c7d708SMark Fasheh out:
5328d0c7d708SMark Fasheh 	brelse(last_eb_bh);
5329d0c7d708SMark Fasheh 	return ret;
5330d0c7d708SMark Fasheh }
5331d0c7d708SMark Fasheh 
5332043beebbSJoel Becker static int ocfs2_truncate_rec(handle_t *handle,
5333043beebbSJoel Becker 			      struct ocfs2_extent_tree *et,
5334d0c7d708SMark Fasheh 			      struct ocfs2_path *path, int index,
5335d0c7d708SMark Fasheh 			      struct ocfs2_cached_dealloc_ctxt *dealloc,
5336043beebbSJoel Becker 			      u32 cpos, u32 len)
5337d0c7d708SMark Fasheh {
5338d0c7d708SMark Fasheh 	int ret;
5339d0c7d708SMark Fasheh 	u32 left_cpos, rec_range, trunc_range;
5340c14688eaSpiaojun 	int is_rightmost_tree_rec = 0;
5341043beebbSJoel Becker 	struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
5342d0c7d708SMark Fasheh 	struct ocfs2_path *left_path = NULL;
5343d0c7d708SMark Fasheh 	struct ocfs2_extent_list *el = path_leaf_el(path);
5344d0c7d708SMark Fasheh 	struct ocfs2_extent_rec *rec;
5345d0c7d708SMark Fasheh 	struct ocfs2_extent_block *eb;
5346d0c7d708SMark Fasheh 
5347d0c7d708SMark Fasheh 	if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
534817215989SXue jiufei 		/* extend credit for ocfs2_remove_rightmost_path */
534917215989SXue jiufei 		ret = ocfs2_extend_rotate_transaction(handle, 0,
535017215989SXue jiufei 				handle->h_buffer_credits,
535117215989SXue jiufei 				path);
535217215989SXue jiufei 		if (ret) {
535317215989SXue jiufei 			mlog_errno(ret);
535417215989SXue jiufei 			goto out;
535517215989SXue jiufei 		}
535617215989SXue jiufei 
535770f18c08SJoel Becker 		ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
5358d0c7d708SMark Fasheh 		if (ret) {
5359d0c7d708SMark Fasheh 			mlog_errno(ret);
5360d0c7d708SMark Fasheh 			goto out;
5361d0c7d708SMark Fasheh 		}
5362d0c7d708SMark Fasheh 
5363d0c7d708SMark Fasheh 		index--;
5364d0c7d708SMark Fasheh 	}
5365d0c7d708SMark Fasheh 
5366d0c7d708SMark Fasheh 	if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5367d0c7d708SMark Fasheh 	    path->p_tree_depth) {
5368d0c7d708SMark Fasheh 		/*
5369d0c7d708SMark Fasheh 		 * Check whether this is the rightmost tree record. If
5370d0c7d708SMark Fasheh 		 * we remove all of this record or part of its right
5371d0c7d708SMark Fasheh 		 * edge then an update of the record lengths above it
5372d0c7d708SMark Fasheh 		 * will be required.
5373d0c7d708SMark Fasheh 		 */
5374d0c7d708SMark Fasheh 		eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5375d0c7d708SMark Fasheh 		if (eb->h_next_leaf_blk == 0)
5376d0c7d708SMark Fasheh 			is_rightmost_tree_rec = 1;
5377d0c7d708SMark Fasheh 	}
5378d0c7d708SMark Fasheh 
5379d0c7d708SMark Fasheh 	rec = &el->l_recs[index];
5380d0c7d708SMark Fasheh 	if (index == 0 && path->p_tree_depth &&
5381d0c7d708SMark Fasheh 	    le32_to_cpu(rec->e_cpos) == cpos) {
5382d0c7d708SMark Fasheh 		/*
5383d0c7d708SMark Fasheh 		 * Changing the leftmost offset (via partial or whole
5384d0c7d708SMark Fasheh 		 * record truncate) of an interior (or rightmost) path
5385d0c7d708SMark Fasheh 		 * means we have to update the subtree that is formed
5386d0c7d708SMark Fasheh 		 * by this leaf and the one to it's left.
5387d0c7d708SMark Fasheh 		 *
5388d0c7d708SMark Fasheh 		 * There are two cases we can skip:
5389043beebbSJoel Becker 		 *   1) Path is the leftmost one in our btree.
5390d0c7d708SMark Fasheh 		 *   2) The leaf is rightmost and will be empty after
5391d0c7d708SMark Fasheh 		 *      we remove the extent record - the rotate code
5392d0c7d708SMark Fasheh 		 *      knows how to update the newly formed edge.
5393d0c7d708SMark Fasheh 		 */
5394d0c7d708SMark Fasheh 
5395043beebbSJoel Becker 		ret = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
5396d0c7d708SMark Fasheh 		if (ret) {
5397d0c7d708SMark Fasheh 			mlog_errno(ret);
5398d0c7d708SMark Fasheh 			goto out;
5399d0c7d708SMark Fasheh 		}
5400d0c7d708SMark Fasheh 
5401d0c7d708SMark Fasheh 		if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
5402ffdd7a54SJoel Becker 			left_path = ocfs2_new_path_from_path(path);
5403d0c7d708SMark Fasheh 			if (!left_path) {
5404d0c7d708SMark Fasheh 				ret = -ENOMEM;
5405d0c7d708SMark Fasheh 				mlog_errno(ret);
5406d0c7d708SMark Fasheh 				goto out;
5407d0c7d708SMark Fasheh 			}
5408d0c7d708SMark Fasheh 
5409facdb77fSJoel Becker 			ret = ocfs2_find_path(et->et_ci, left_path,
5410facdb77fSJoel Becker 					      left_cpos);
5411d0c7d708SMark Fasheh 			if (ret) {
5412d0c7d708SMark Fasheh 				mlog_errno(ret);
5413d0c7d708SMark Fasheh 				goto out;
5414d0c7d708SMark Fasheh 			}
5415d0c7d708SMark Fasheh 		}
5416d0c7d708SMark Fasheh 	}
5417d0c7d708SMark Fasheh 
5418d0c7d708SMark Fasheh 	ret = ocfs2_extend_rotate_transaction(handle, 0,
5419d0c7d708SMark Fasheh 					      handle->h_buffer_credits,
5420d0c7d708SMark Fasheh 					      path);
5421d0c7d708SMark Fasheh 	if (ret) {
5422d0c7d708SMark Fasheh 		mlog_errno(ret);
5423d0c7d708SMark Fasheh 		goto out;
5424d0c7d708SMark Fasheh 	}
5425d0c7d708SMark Fasheh 
5426d9a0a1f8SJoel Becker 	ret = ocfs2_journal_access_path(et->et_ci, handle, path);
5427d0c7d708SMark Fasheh 	if (ret) {
5428d0c7d708SMark Fasheh 		mlog_errno(ret);
5429d0c7d708SMark Fasheh 		goto out;
5430d0c7d708SMark Fasheh 	}
5431d0c7d708SMark Fasheh 
5432d9a0a1f8SJoel Becker 	ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
5433d0c7d708SMark Fasheh 	if (ret) {
5434d0c7d708SMark Fasheh 		mlog_errno(ret);
5435d0c7d708SMark Fasheh 		goto out;
5436d0c7d708SMark Fasheh 	}
5437d0c7d708SMark Fasheh 
5438d0c7d708SMark Fasheh 	rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5439d0c7d708SMark Fasheh 	trunc_range = cpos + len;
5440d0c7d708SMark Fasheh 
5441d0c7d708SMark Fasheh 	if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5442d0c7d708SMark Fasheh 		int next_free;
5443d0c7d708SMark Fasheh 
5444d0c7d708SMark Fasheh 		memset(rec, 0, sizeof(*rec));
5445d0c7d708SMark Fasheh 		ocfs2_cleanup_merge(el, index);
5446d0c7d708SMark Fasheh 
5447d0c7d708SMark Fasheh 		next_free = le16_to_cpu(el->l_next_free_rec);
5448d0c7d708SMark Fasheh 		if (is_rightmost_tree_rec && next_free > 1) {
5449d0c7d708SMark Fasheh 			/*
5450d0c7d708SMark Fasheh 			 * We skip the edge update if this path will
5451d0c7d708SMark Fasheh 			 * be deleted by the rotate code.
5452d0c7d708SMark Fasheh 			 */
5453d0c7d708SMark Fasheh 			rec = &el->l_recs[next_free - 1];
5454d401dc12SJoel Becker 			ocfs2_adjust_rightmost_records(handle, et, path,
5455d0c7d708SMark Fasheh 						       rec);
5456d0c7d708SMark Fasheh 		}
5457d0c7d708SMark Fasheh 	} else if (le32_to_cpu(rec->e_cpos) == cpos) {
5458d0c7d708SMark Fasheh 		/* Remove leftmost portion of the record. */
5459d0c7d708SMark Fasheh 		le32_add_cpu(&rec->e_cpos, len);
5460d0c7d708SMark Fasheh 		le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5461d0c7d708SMark Fasheh 		le16_add_cpu(&rec->e_leaf_clusters, -len);
5462d0c7d708SMark Fasheh 	} else if (rec_range == trunc_range) {
5463d0c7d708SMark Fasheh 		/* Remove rightmost portion of the record */
5464d0c7d708SMark Fasheh 		le16_add_cpu(&rec->e_leaf_clusters, -len);
5465d0c7d708SMark Fasheh 		if (is_rightmost_tree_rec)
5466d401dc12SJoel Becker 			ocfs2_adjust_rightmost_records(handle, et, path, rec);
5467d0c7d708SMark Fasheh 	} else {
5468d0c7d708SMark Fasheh 		/* Caller should have trapped this. */
5469043beebbSJoel Becker 		mlog(ML_ERROR, "Owner %llu: Invalid record truncate: (%u, %u) "
5470043beebbSJoel Becker 		     "(%u, %u)\n",
5471043beebbSJoel Becker 		     (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5472d0c7d708SMark Fasheh 		     le32_to_cpu(rec->e_cpos),
5473d0c7d708SMark Fasheh 		     le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5474d0c7d708SMark Fasheh 		BUG();
5475d0c7d708SMark Fasheh 	}
5476d0c7d708SMark Fasheh 
5477d0c7d708SMark Fasheh 	if (left_path) {
5478d0c7d708SMark Fasheh 		int subtree_index;
5479d0c7d708SMark Fasheh 
54807dc02805SJoel Becker 		subtree_index = ocfs2_find_subtree_root(et, left_path, path);
54814619c73eSJoel Becker 		ocfs2_complete_edge_insert(handle, left_path, path,
5482d0c7d708SMark Fasheh 					   subtree_index);
5483d0c7d708SMark Fasheh 	}
5484d0c7d708SMark Fasheh 
5485d0c7d708SMark Fasheh 	ocfs2_journal_dirty(handle, path_leaf_bh(path));
5486d0c7d708SMark Fasheh 
548770f18c08SJoel Becker 	ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
5488d0c7d708SMark Fasheh 	if (ret) {
5489d0c7d708SMark Fasheh 		mlog_errno(ret);
5490d0c7d708SMark Fasheh 		goto out;
5491d0c7d708SMark Fasheh 	}
5492d0c7d708SMark Fasheh 
5493d0c7d708SMark Fasheh out:
5494d0c7d708SMark Fasheh 	ocfs2_free_path(left_path);
5495d0c7d708SMark Fasheh 	return ret;
5496d0c7d708SMark Fasheh }
5497d0c7d708SMark Fasheh 
5498dbdcf6a4SJoel Becker int ocfs2_remove_extent(handle_t *handle,
5499f99b9b7cSJoel Becker 			struct ocfs2_extent_tree *et,
5500dbdcf6a4SJoel Becker 			u32 cpos, u32 len,
5501d0c7d708SMark Fasheh 			struct ocfs2_alloc_context *meta_ac,
5502f99b9b7cSJoel Becker 			struct ocfs2_cached_dealloc_ctxt *dealloc)
5503d0c7d708SMark Fasheh {
5504d0c7d708SMark Fasheh 	int ret, index;
5505d0c7d708SMark Fasheh 	u32 rec_range, trunc_range;
5506d0c7d708SMark Fasheh 	struct ocfs2_extent_rec *rec;
5507d0c7d708SMark Fasheh 	struct ocfs2_extent_list *el;
5508e7d4cb6bSTao Ma 	struct ocfs2_path *path = NULL;
5509d0c7d708SMark Fasheh 
55104c911eefSJoel Becker 	/*
55114c911eefSJoel Becker 	 * XXX: Why are we truncating to 0 instead of wherever this
55124c911eefSJoel Becker 	 * affects us?
55134c911eefSJoel Becker 	 */
55144c911eefSJoel Becker 	ocfs2_et_extent_map_truncate(et, 0);
5515d0c7d708SMark Fasheh 
5516ffdd7a54SJoel Becker 	path = ocfs2_new_path_from_et(et);
5517d0c7d708SMark Fasheh 	if (!path) {
5518d0c7d708SMark Fasheh 		ret = -ENOMEM;
5519d0c7d708SMark Fasheh 		mlog_errno(ret);
5520d0c7d708SMark Fasheh 		goto out;
5521d0c7d708SMark Fasheh 	}
5522d0c7d708SMark Fasheh 
5523facdb77fSJoel Becker 	ret = ocfs2_find_path(et->et_ci, path, cpos);
5524d0c7d708SMark Fasheh 	if (ret) {
5525d0c7d708SMark Fasheh 		mlog_errno(ret);
5526d0c7d708SMark Fasheh 		goto out;
5527d0c7d708SMark Fasheh 	}
5528d0c7d708SMark Fasheh 
5529d0c7d708SMark Fasheh 	el = path_leaf_el(path);
5530d0c7d708SMark Fasheh 	index = ocfs2_search_extent_list(el, cpos);
5531981035b4SYingtai Xie 	if (index == -1) {
5532dbdcf6a4SJoel Becker 		ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
55337ecef14aSJoe Perches 			    "Owner %llu has an extent at cpos %u which can no longer be found\n",
5534dbdcf6a4SJoel Becker 			    (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5535dbdcf6a4SJoel Becker 			    cpos);
5536d0c7d708SMark Fasheh 		ret = -EROFS;
5537d0c7d708SMark Fasheh 		goto out;
5538d0c7d708SMark Fasheh 	}
5539d0c7d708SMark Fasheh 
5540d0c7d708SMark Fasheh 	/*
5541d0c7d708SMark Fasheh 	 * We have 3 cases of extent removal:
5542d0c7d708SMark Fasheh 	 *   1) Range covers the entire extent rec
5543d0c7d708SMark Fasheh 	 *   2) Range begins or ends on one edge of the extent rec
5544d0c7d708SMark Fasheh 	 *   3) Range is in the middle of the extent rec (no shared edges)
5545d0c7d708SMark Fasheh 	 *
5546d0c7d708SMark Fasheh 	 * For case 1 we remove the extent rec and left rotate to
5547d0c7d708SMark Fasheh 	 * fill the hole.
5548d0c7d708SMark Fasheh 	 *
5549d0c7d708SMark Fasheh 	 * For case 2 we just shrink the existing extent rec, with a
5550d0c7d708SMark Fasheh 	 * tree update if the shrinking edge is also the edge of an
5551d0c7d708SMark Fasheh 	 * extent block.
5552d0c7d708SMark Fasheh 	 *
5553d0c7d708SMark Fasheh 	 * For case 3 we do a right split to turn the extent rec into
5554d0c7d708SMark Fasheh 	 * something case 2 can handle.
5555d0c7d708SMark Fasheh 	 */
5556d0c7d708SMark Fasheh 	rec = &el->l_recs[index];
5557d0c7d708SMark Fasheh 	rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5558d0c7d708SMark Fasheh 	trunc_range = cpos + len;
5559d0c7d708SMark Fasheh 
5560d0c7d708SMark Fasheh 	BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5561d0c7d708SMark Fasheh 
5562a09d09b8STao Ma 	trace_ocfs2_remove_extent(
5563dbdcf6a4SJoel Becker 		(unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5564a09d09b8STao Ma 		cpos, len, index, le32_to_cpu(rec->e_cpos),
5565a09d09b8STao Ma 		ocfs2_rec_clusters(el, rec));
5566d0c7d708SMark Fasheh 
5567d0c7d708SMark Fasheh 	if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
5568043beebbSJoel Becker 		ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5569043beebbSJoel Becker 					 cpos, len);
5570d0c7d708SMark Fasheh 		if (ret) {
5571d0c7d708SMark Fasheh 			mlog_errno(ret);
5572d0c7d708SMark Fasheh 			goto out;
5573d0c7d708SMark Fasheh 		}
5574d0c7d708SMark Fasheh 	} else {
5575dbdcf6a4SJoel Becker 		ret = ocfs2_split_tree(handle, et, path, index,
5576d0c7d708SMark Fasheh 				       trunc_range, meta_ac);
5577d0c7d708SMark Fasheh 		if (ret) {
5578d0c7d708SMark Fasheh 			mlog_errno(ret);
5579d0c7d708SMark Fasheh 			goto out;
5580d0c7d708SMark Fasheh 		}
5581d0c7d708SMark Fasheh 
5582d0c7d708SMark Fasheh 		/*
5583d0c7d708SMark Fasheh 		 * The split could have manipulated the tree enough to
5584d0c7d708SMark Fasheh 		 * move the record location, so we have to look for it again.
5585d0c7d708SMark Fasheh 		 */
5586d0c7d708SMark Fasheh 		ocfs2_reinit_path(path, 1);
5587d0c7d708SMark Fasheh 
5588facdb77fSJoel Becker 		ret = ocfs2_find_path(et->et_ci, path, cpos);
5589d0c7d708SMark Fasheh 		if (ret) {
5590d0c7d708SMark Fasheh 			mlog_errno(ret);
5591d0c7d708SMark Fasheh 			goto out;
5592d0c7d708SMark Fasheh 		}
5593d0c7d708SMark Fasheh 
5594d0c7d708SMark Fasheh 		el = path_leaf_el(path);
5595d0c7d708SMark Fasheh 		index = ocfs2_search_extent_list(el, cpos);
5596981035b4SYingtai Xie 		if (index == -1) {
5597dbdcf6a4SJoel Becker 			ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
55987ecef14aSJoe Perches 				    "Owner %llu: split at cpos %u lost record\n",
5599dbdcf6a4SJoel Becker 				    (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5600d0c7d708SMark Fasheh 				    cpos);
5601d0c7d708SMark Fasheh 			ret = -EROFS;
5602d0c7d708SMark Fasheh 			goto out;
5603d0c7d708SMark Fasheh 		}
5604d0c7d708SMark Fasheh 
5605d0c7d708SMark Fasheh 		/*
5606d0c7d708SMark Fasheh 		 * Double check our values here. If anything is fishy,
5607d0c7d708SMark Fasheh 		 * it's easier to catch it at the top level.
5608d0c7d708SMark Fasheh 		 */
5609d0c7d708SMark Fasheh 		rec = &el->l_recs[index];
5610d0c7d708SMark Fasheh 		rec_range = le32_to_cpu(rec->e_cpos) +
5611d0c7d708SMark Fasheh 			ocfs2_rec_clusters(el, rec);
5612d0c7d708SMark Fasheh 		if (rec_range != trunc_range) {
5613dbdcf6a4SJoel Becker 			ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
56147ecef14aSJoe Perches 				    "Owner %llu: error after split at cpos %u trunc len %u, existing record is (%u,%u)\n",
5615dbdcf6a4SJoel Becker 				    (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5616d0c7d708SMark Fasheh 				    cpos, len, le32_to_cpu(rec->e_cpos),
5617d0c7d708SMark Fasheh 				    ocfs2_rec_clusters(el, rec));
5618d0c7d708SMark Fasheh 			ret = -EROFS;
5619d0c7d708SMark Fasheh 			goto out;
5620d0c7d708SMark Fasheh 		}
5621d0c7d708SMark Fasheh 
5622043beebbSJoel Becker 		ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5623043beebbSJoel Becker 					 cpos, len);
5624d0c7d708SMark Fasheh 		if (ret) {
5625d0c7d708SMark Fasheh 			mlog_errno(ret);
5626d0c7d708SMark Fasheh 			goto out;
5627d0c7d708SMark Fasheh 		}
5628d0c7d708SMark Fasheh 	}
5629d0c7d708SMark Fasheh 
5630d0c7d708SMark Fasheh out:
5631d0c7d708SMark Fasheh 	ocfs2_free_path(path);
5632d0c7d708SMark Fasheh 	return ret;
5633d0c7d708SMark Fasheh }
5634d0c7d708SMark Fasheh 
563578f94673STristan Ye /*
563678f94673STristan Ye  * ocfs2_reserve_blocks_for_rec_trunc() would look basically the
563778f94673STristan Ye  * same as ocfs2_lock_alloctors(), except for it accepts a blocks
563878f94673STristan Ye  * number to reserve some extra blocks, and it only handles meta
563978f94673STristan Ye  * data allocations.
564078f94673STristan Ye  *
564178f94673STristan Ye  * Currently, only ocfs2_remove_btree_range() uses it for truncating
564278f94673STristan Ye  * and punching holes.
564378f94673STristan Ye  */
564478f94673STristan Ye static int ocfs2_reserve_blocks_for_rec_trunc(struct inode *inode,
564578f94673STristan Ye 					      struct ocfs2_extent_tree *et,
564678f94673STristan Ye 					      u32 extents_to_split,
564778f94673STristan Ye 					      struct ocfs2_alloc_context **ac,
564878f94673STristan Ye 					      int extra_blocks)
564978f94673STristan Ye {
565078f94673STristan Ye 	int ret = 0, num_free_extents;
565178f94673STristan Ye 	unsigned int max_recs_needed = 2 * extents_to_split;
565278f94673STristan Ye 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
565378f94673STristan Ye 
565478f94673STristan Ye 	*ac = NULL;
565578f94673STristan Ye 
5656964f14a0SJun Piao 	num_free_extents = ocfs2_num_free_extents(et);
565778f94673STristan Ye 	if (num_free_extents < 0) {
565878f94673STristan Ye 		ret = num_free_extents;
565978f94673STristan Ye 		mlog_errno(ret);
566078f94673STristan Ye 		goto out;
566178f94673STristan Ye 	}
566278f94673STristan Ye 
566378f94673STristan Ye 	if (!num_free_extents ||
566478f94673STristan Ye 	    (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed))
566578f94673STristan Ye 		extra_blocks += ocfs2_extend_meta_needed(et->et_root_el);
566678f94673STristan Ye 
566778f94673STristan Ye 	if (extra_blocks) {
566878f94673STristan Ye 		ret = ocfs2_reserve_new_metadata_blocks(osb, extra_blocks, ac);
566978f94673STristan Ye 		if (ret < 0) {
567078f94673STristan Ye 			if (ret != -ENOSPC)
567178f94673STristan Ye 				mlog_errno(ret);
567278f94673STristan Ye 			goto out;
567378f94673STristan Ye 		}
567478f94673STristan Ye 	}
567578f94673STristan Ye 
567678f94673STristan Ye out:
567778f94673STristan Ye 	if (ret) {
567878f94673STristan Ye 		if (*ac) {
567978f94673STristan Ye 			ocfs2_free_alloc_context(*ac);
568078f94673STristan Ye 			*ac = NULL;
568178f94673STristan Ye 		}
568278f94673STristan Ye 	}
568378f94673STristan Ye 
568478f94673STristan Ye 	return ret;
568578f94673STristan Ye }
568678f94673STristan Ye 
5687fecc0112SMark Fasheh int ocfs2_remove_btree_range(struct inode *inode,
5688fecc0112SMark Fasheh 			     struct ocfs2_extent_tree *et,
568978f94673STristan Ye 			     u32 cpos, u32 phys_cpos, u32 len, int flags,
569078f94673STristan Ye 			     struct ocfs2_cached_dealloc_ctxt *dealloc,
5691f62f12b3SJunxiao Bi 			     u64 refcount_loc, bool refcount_tree_locked)
5692fecc0112SMark Fasheh {
569378f94673STristan Ye 	int ret, credits = 0, extra_blocks = 0;
5694fecc0112SMark Fasheh 	u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
5695fecc0112SMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5696fecc0112SMark Fasheh 	struct inode *tl_inode = osb->osb_tl_inode;
5697fecc0112SMark Fasheh 	handle_t *handle;
5698fecc0112SMark Fasheh 	struct ocfs2_alloc_context *meta_ac = NULL;
569978f94673STristan Ye 	struct ocfs2_refcount_tree *ref_tree = NULL;
5700fecc0112SMark Fasheh 
570178f94673STristan Ye 	if ((flags & OCFS2_EXT_REFCOUNTED) && len) {
570284e40080SDarrick J. Wong 		BUG_ON(!ocfs2_is_refcount_inode(inode));
570378f94673STristan Ye 
5704f62f12b3SJunxiao Bi 		if (!refcount_tree_locked) {
570578f94673STristan Ye 			ret = ocfs2_lock_refcount_tree(osb, refcount_loc, 1,
570678f94673STristan Ye 						       &ref_tree, NULL);
570778f94673STristan Ye 			if (ret) {
570878f94673STristan Ye 				mlog_errno(ret);
570933add0e3SJoseph Qi 				goto bail;
571078f94673STristan Ye 			}
5711f62f12b3SJunxiao Bi 		}
571278f94673STristan Ye 
571378f94673STristan Ye 		ret = ocfs2_prepare_refcount_change_for_del(inode,
571478f94673STristan Ye 							    refcount_loc,
571578f94673STristan Ye 							    phys_blkno,
571678f94673STristan Ye 							    len,
571778f94673STristan Ye 							    &credits,
571878f94673STristan Ye 							    &extra_blocks);
571978f94673STristan Ye 		if (ret < 0) {
572078f94673STristan Ye 			mlog_errno(ret);
572133add0e3SJoseph Qi 			goto bail;
572278f94673STristan Ye 		}
572378f94673STristan Ye 	}
572478f94673STristan Ye 
572578f94673STristan Ye 	ret = ocfs2_reserve_blocks_for_rec_trunc(inode, et, 1, &meta_ac,
572678f94673STristan Ye 						 extra_blocks);
5727fecc0112SMark Fasheh 	if (ret) {
5728fecc0112SMark Fasheh 		mlog_errno(ret);
572933add0e3SJoseph Qi 		goto bail;
5730fecc0112SMark Fasheh 	}
5731fecc0112SMark Fasheh 
57325955102cSAl Viro 	inode_lock(tl_inode);
5733fecc0112SMark Fasheh 
5734fecc0112SMark Fasheh 	if (ocfs2_truncate_log_needs_flush(osb)) {
5735fecc0112SMark Fasheh 		ret = __ocfs2_flush_truncate_log(osb);
5736fecc0112SMark Fasheh 		if (ret < 0) {
5737fecc0112SMark Fasheh 			mlog_errno(ret);
5738fecc0112SMark Fasheh 			goto out;
5739fecc0112SMark Fasheh 		}
5740fecc0112SMark Fasheh 	}
5741fecc0112SMark Fasheh 
574278f94673STristan Ye 	handle = ocfs2_start_trans(osb,
574378f94673STristan Ye 			ocfs2_remove_extent_credits(osb->sb) + credits);
5744fecc0112SMark Fasheh 	if (IS_ERR(handle)) {
5745fecc0112SMark Fasheh 		ret = PTR_ERR(handle);
5746fecc0112SMark Fasheh 		mlog_errno(ret);
5747fecc0112SMark Fasheh 		goto out;
5748fecc0112SMark Fasheh 	}
5749fecc0112SMark Fasheh 
5750d9a0a1f8SJoel Becker 	ret = ocfs2_et_root_journal_access(handle, et,
5751fecc0112SMark Fasheh 					   OCFS2_JOURNAL_ACCESS_WRITE);
5752fecc0112SMark Fasheh 	if (ret) {
5753fecc0112SMark Fasheh 		mlog_errno(ret);
5754b8a0ae57SWengang Wang 		goto out_commit;
5755fecc0112SMark Fasheh 	}
5756fecc0112SMark Fasheh 
57575dd4056dSChristoph Hellwig 	dquot_free_space_nodirty(inode,
5758fd4ef231SMark Fasheh 				  ocfs2_clusters_to_bytes(inode->i_sb, len));
5759fd4ef231SMark Fasheh 
5760dbdcf6a4SJoel Becker 	ret = ocfs2_remove_extent(handle, et, cpos, len, meta_ac, dealloc);
5761fecc0112SMark Fasheh 	if (ret) {
5762fecc0112SMark Fasheh 		mlog_errno(ret);
5763fecc0112SMark Fasheh 		goto out_commit;
5764fecc0112SMark Fasheh 	}
5765fecc0112SMark Fasheh 
57666136ca5fSJoel Becker 	ocfs2_et_update_clusters(et, -len);
57676fdb702dSDarrick J. Wong 	ocfs2_update_inode_fsync_trans(handle, inode, 1);
5768fecc0112SMark Fasheh 
5769ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, et->et_root_bh);
5770fecc0112SMark Fasheh 
577178f94673STristan Ye 	if (phys_blkno) {
577278f94673STristan Ye 		if (flags & OCFS2_EXT_REFCOUNTED)
577378f94673STristan Ye 			ret = ocfs2_decrease_refcount(inode, handle,
577478f94673STristan Ye 					ocfs2_blocks_to_clusters(osb->sb,
577578f94673STristan Ye 								 phys_blkno),
577678f94673STristan Ye 					len, meta_ac,
577778f94673STristan Ye 					dealloc, 1);
577878f94673STristan Ye 		else
577978f94673STristan Ye 			ret = ocfs2_truncate_log_append(osb, handle,
578078f94673STristan Ye 							phys_blkno, len);
5781fecc0112SMark Fasheh 		if (ret)
5782fecc0112SMark Fasheh 			mlog_errno(ret);
5783fecc0112SMark Fasheh 
578478f94673STristan Ye 	}
578578f94673STristan Ye 
5786fecc0112SMark Fasheh out_commit:
5787fecc0112SMark Fasheh 	ocfs2_commit_trans(osb, handle);
5788fecc0112SMark Fasheh out:
57895955102cSAl Viro 	inode_unlock(tl_inode);
579033add0e3SJoseph Qi bail:
5791fecc0112SMark Fasheh 	if (meta_ac)
5792fecc0112SMark Fasheh 		ocfs2_free_alloc_context(meta_ac);
5793fecc0112SMark Fasheh 
579478f94673STristan Ye 	if (ref_tree)
579578f94673STristan Ye 		ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
579678f94673STristan Ye 
5797fecc0112SMark Fasheh 	return ret;
5798fecc0112SMark Fasheh }
5799fecc0112SMark Fasheh 
5800063c4561SMark Fasheh int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
5801ccd979bdSMark Fasheh {
5802ccd979bdSMark Fasheh 	struct buffer_head *tl_bh = osb->osb_tl_bh;
5803ccd979bdSMark Fasheh 	struct ocfs2_dinode *di;
5804ccd979bdSMark Fasheh 	struct ocfs2_truncate_log *tl;
5805ccd979bdSMark Fasheh 
5806ccd979bdSMark Fasheh 	di = (struct ocfs2_dinode *) tl_bh->b_data;
5807ccd979bdSMark Fasheh 	tl = &di->id2.i_dealloc;
5808ccd979bdSMark Fasheh 
5809ccd979bdSMark Fasheh 	mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5810ccd979bdSMark Fasheh 			"slot %d, invalid truncate log parameters: used = "
5811ccd979bdSMark Fasheh 			"%u, count = %u\n", osb->slot_num,
5812ccd979bdSMark Fasheh 			le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5813ccd979bdSMark Fasheh 	return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5814ccd979bdSMark Fasheh }
5815ccd979bdSMark Fasheh 
5816ccd979bdSMark Fasheh static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5817ccd979bdSMark Fasheh 					   unsigned int new_start)
5818ccd979bdSMark Fasheh {
5819ccd979bdSMark Fasheh 	unsigned int tail_index;
5820ccd979bdSMark Fasheh 	unsigned int current_tail;
5821ccd979bdSMark Fasheh 
5822ccd979bdSMark Fasheh 	/* No records, nothing to coalesce */
5823ccd979bdSMark Fasheh 	if (!le16_to_cpu(tl->tl_used))
5824ccd979bdSMark Fasheh 		return 0;
5825ccd979bdSMark Fasheh 
5826ccd979bdSMark Fasheh 	tail_index = le16_to_cpu(tl->tl_used) - 1;
5827ccd979bdSMark Fasheh 	current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5828ccd979bdSMark Fasheh 	current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5829ccd979bdSMark Fasheh 
5830ccd979bdSMark Fasheh 	return current_tail == new_start;
5831ccd979bdSMark Fasheh }
5832ccd979bdSMark Fasheh 
5833063c4561SMark Fasheh int ocfs2_truncate_log_append(struct ocfs2_super *osb,
58341fabe148SMark Fasheh 			      handle_t *handle,
5835ccd979bdSMark Fasheh 			      u64 start_blk,
5836ccd979bdSMark Fasheh 			      unsigned int num_clusters)
5837ccd979bdSMark Fasheh {
5838ccd979bdSMark Fasheh 	int status, index;
5839ccd979bdSMark Fasheh 	unsigned int start_cluster, tl_count;
5840ccd979bdSMark Fasheh 	struct inode *tl_inode = osb->osb_tl_inode;
5841ccd979bdSMark Fasheh 	struct buffer_head *tl_bh = osb->osb_tl_bh;
5842ccd979bdSMark Fasheh 	struct ocfs2_dinode *di;
5843ccd979bdSMark Fasheh 	struct ocfs2_truncate_log *tl;
5844ccd979bdSMark Fasheh 
58455955102cSAl Viro 	BUG_ON(inode_trylock(tl_inode));
5846ccd979bdSMark Fasheh 
5847ccd979bdSMark Fasheh 	start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5848ccd979bdSMark Fasheh 
5849ccd979bdSMark Fasheh 	di = (struct ocfs2_dinode *) tl_bh->b_data;
5850ccd979bdSMark Fasheh 
585110995aa2SJoel Becker 	/* tl_bh is loaded from ocfs2_truncate_log_init().  It's validated
585210995aa2SJoel Becker 	 * by the underlying call to ocfs2_read_inode_block(), so any
585310995aa2SJoel Becker 	 * corruption is a code bug */
585410995aa2SJoel Becker 	BUG_ON(!OCFS2_IS_VALID_DINODE(di));
585510995aa2SJoel Becker 
585610995aa2SJoel Becker 	tl = &di->id2.i_dealloc;
5857ccd979bdSMark Fasheh 	tl_count = le16_to_cpu(tl->tl_count);
5858ccd979bdSMark Fasheh 	mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5859ccd979bdSMark Fasheh 			tl_count == 0,
5860b0697053SMark Fasheh 			"Truncate record count on #%llu invalid "
5861b0697053SMark Fasheh 			"wanted %u, actual %u\n",
5862b0697053SMark Fasheh 			(unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
5863ccd979bdSMark Fasheh 			ocfs2_truncate_recs_per_inode(osb->sb),
5864ccd979bdSMark Fasheh 			le16_to_cpu(tl->tl_count));
5865ccd979bdSMark Fasheh 
5866ccd979bdSMark Fasheh 	/* Caller should have known to flush before calling us. */
5867ccd979bdSMark Fasheh 	index = le16_to_cpu(tl->tl_used);
5868ccd979bdSMark Fasheh 	if (index >= tl_count) {
5869ccd979bdSMark Fasheh 		status = -ENOSPC;
5870ccd979bdSMark Fasheh 		mlog_errno(status);
5871ccd979bdSMark Fasheh 		goto bail;
5872ccd979bdSMark Fasheh 	}
5873ccd979bdSMark Fasheh 
58740cf2f763SJoel Becker 	status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
5875ccd979bdSMark Fasheh 					 OCFS2_JOURNAL_ACCESS_WRITE);
5876ccd979bdSMark Fasheh 	if (status < 0) {
5877ccd979bdSMark Fasheh 		mlog_errno(status);
5878ccd979bdSMark Fasheh 		goto bail;
5879ccd979bdSMark Fasheh 	}
5880ccd979bdSMark Fasheh 
5881a09d09b8STao Ma 	trace_ocfs2_truncate_log_append(
5882a09d09b8STao Ma 		(unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index,
5883a09d09b8STao Ma 		start_cluster, num_clusters);
5884ccd979bdSMark Fasheh 	if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5885ccd979bdSMark Fasheh 		/*
5886ccd979bdSMark Fasheh 		 * Move index back to the record we are coalescing with.
5887ccd979bdSMark Fasheh 		 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5888ccd979bdSMark Fasheh 		 */
5889ccd979bdSMark Fasheh 		index--;
5890ccd979bdSMark Fasheh 
5891ccd979bdSMark Fasheh 		num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5892a09d09b8STao Ma 		trace_ocfs2_truncate_log_append(
5893a09d09b8STao Ma 			(unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
5894ccd979bdSMark Fasheh 			index, le32_to_cpu(tl->tl_recs[index].t_start),
5895ccd979bdSMark Fasheh 			num_clusters);
5896ccd979bdSMark Fasheh 	} else {
5897ccd979bdSMark Fasheh 		tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5898ccd979bdSMark Fasheh 		tl->tl_used = cpu_to_le16(index + 1);
5899ccd979bdSMark Fasheh 	}
5900ccd979bdSMark Fasheh 	tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5901ccd979bdSMark Fasheh 
5902ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, tl_bh);
5903ccd979bdSMark Fasheh 
590450308d81STao Ma 	osb->truncated_clusters += num_clusters;
5905ccd979bdSMark Fasheh bail:
5906ccd979bdSMark Fasheh 	return status;
5907ccd979bdSMark Fasheh }
5908ccd979bdSMark Fasheh 
5909ccd979bdSMark Fasheh static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
5910ccd979bdSMark Fasheh 					 struct inode *data_alloc_inode,
5911ccd979bdSMark Fasheh 					 struct buffer_head *data_alloc_bh)
5912ccd979bdSMark Fasheh {
5913ccd979bdSMark Fasheh 	int status = 0;
5914ccd979bdSMark Fasheh 	int i;
5915ccd979bdSMark Fasheh 	unsigned int num_clusters;
5916ccd979bdSMark Fasheh 	u64 start_blk;
5917ccd979bdSMark Fasheh 	struct ocfs2_truncate_rec rec;
5918ccd979bdSMark Fasheh 	struct ocfs2_dinode *di;
5919ccd979bdSMark Fasheh 	struct ocfs2_truncate_log *tl;
5920ccd979bdSMark Fasheh 	struct inode *tl_inode = osb->osb_tl_inode;
5921ccd979bdSMark Fasheh 	struct buffer_head *tl_bh = osb->osb_tl_bh;
59222b0ad008SJunxiao Bi 	handle_t *handle;
5923ccd979bdSMark Fasheh 
5924ccd979bdSMark Fasheh 	di = (struct ocfs2_dinode *) tl_bh->b_data;
5925ccd979bdSMark Fasheh 	tl = &di->id2.i_dealloc;
5926ccd979bdSMark Fasheh 	i = le16_to_cpu(tl->tl_used) - 1;
5927ccd979bdSMark Fasheh 	while (i >= 0) {
59282b0ad008SJunxiao Bi 		handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
59292b0ad008SJunxiao Bi 		if (IS_ERR(handle)) {
59302b0ad008SJunxiao Bi 			status = PTR_ERR(handle);
59312b0ad008SJunxiao Bi 			mlog_errno(status);
59322b0ad008SJunxiao Bi 			goto bail;
59332b0ad008SJunxiao Bi 		}
59342b0ad008SJunxiao Bi 
5935ccd979bdSMark Fasheh 		/* Caller has given us at least enough credits to
5936ccd979bdSMark Fasheh 		 * update the truncate log dinode */
59370cf2f763SJoel Becker 		status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
5938ccd979bdSMark Fasheh 						 OCFS2_JOURNAL_ACCESS_WRITE);
5939ccd979bdSMark Fasheh 		if (status < 0) {
5940ccd979bdSMark Fasheh 			mlog_errno(status);
5941ccd979bdSMark Fasheh 			goto bail;
5942ccd979bdSMark Fasheh 		}
5943ccd979bdSMark Fasheh 
5944ccd979bdSMark Fasheh 		tl->tl_used = cpu_to_le16(i);
5945ccd979bdSMark Fasheh 
5946ec20cec7SJoel Becker 		ocfs2_journal_dirty(handle, tl_bh);
5947ccd979bdSMark Fasheh 
5948ccd979bdSMark Fasheh 		rec = tl->tl_recs[i];
5949ccd979bdSMark Fasheh 		start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5950ccd979bdSMark Fasheh 						    le32_to_cpu(rec.t_start));
5951ccd979bdSMark Fasheh 		num_clusters = le32_to_cpu(rec.t_clusters);
5952ccd979bdSMark Fasheh 
5953ccd979bdSMark Fasheh 		/* if start_blk is not set, we ignore the record as
5954ccd979bdSMark Fasheh 		 * invalid. */
5955ccd979bdSMark Fasheh 		if (start_blk) {
5956a09d09b8STao Ma 			trace_ocfs2_replay_truncate_records(
5957a09d09b8STao Ma 				(unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
5958ccd979bdSMark Fasheh 				i, le32_to_cpu(rec.t_start), num_clusters);
5959ccd979bdSMark Fasheh 
5960ccd979bdSMark Fasheh 			status = ocfs2_free_clusters(handle, data_alloc_inode,
5961ccd979bdSMark Fasheh 						     data_alloc_bh, start_blk,
5962ccd979bdSMark Fasheh 						     num_clusters);
5963ccd979bdSMark Fasheh 			if (status < 0) {
5964ccd979bdSMark Fasheh 				mlog_errno(status);
5965ccd979bdSMark Fasheh 				goto bail;
5966ccd979bdSMark Fasheh 			}
5967ccd979bdSMark Fasheh 		}
5968102c2595SXue jiufei 
59692b0ad008SJunxiao Bi 		ocfs2_commit_trans(osb, handle);
5970ccd979bdSMark Fasheh 		i--;
5971ccd979bdSMark Fasheh 	}
5972ccd979bdSMark Fasheh 
597350308d81STao Ma 	osb->truncated_clusters = 0;
597450308d81STao Ma 
5975ccd979bdSMark Fasheh bail:
5976ccd979bdSMark Fasheh 	return status;
5977ccd979bdSMark Fasheh }
5978ccd979bdSMark Fasheh 
59791b1dcc1bSJes Sorensen /* Expects you to already be holding tl_inode->i_mutex */
5980063c4561SMark Fasheh int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5981ccd979bdSMark Fasheh {
5982ccd979bdSMark Fasheh 	int status;
5983ccd979bdSMark Fasheh 	unsigned int num_to_flush;
5984ccd979bdSMark Fasheh 	struct inode *tl_inode = osb->osb_tl_inode;
5985ccd979bdSMark Fasheh 	struct inode *data_alloc_inode = NULL;
5986ccd979bdSMark Fasheh 	struct buffer_head *tl_bh = osb->osb_tl_bh;
5987ccd979bdSMark Fasheh 	struct buffer_head *data_alloc_bh = NULL;
5988ccd979bdSMark Fasheh 	struct ocfs2_dinode *di;
5989ccd979bdSMark Fasheh 	struct ocfs2_truncate_log *tl;
5990ccd979bdSMark Fasheh 
59915955102cSAl Viro 	BUG_ON(inode_trylock(tl_inode));
5992ccd979bdSMark Fasheh 
5993ccd979bdSMark Fasheh 	di = (struct ocfs2_dinode *) tl_bh->b_data;
5994ccd979bdSMark Fasheh 
599510995aa2SJoel Becker 	/* tl_bh is loaded from ocfs2_truncate_log_init().  It's validated
599610995aa2SJoel Becker 	 * by the underlying call to ocfs2_read_inode_block(), so any
599710995aa2SJoel Becker 	 * corruption is a code bug */
599810995aa2SJoel Becker 	BUG_ON(!OCFS2_IS_VALID_DINODE(di));
599910995aa2SJoel Becker 
600010995aa2SJoel Becker 	tl = &di->id2.i_dealloc;
6001ccd979bdSMark Fasheh 	num_to_flush = le16_to_cpu(tl->tl_used);
6002a09d09b8STao Ma 	trace_ocfs2_flush_truncate_log(
6003a09d09b8STao Ma 		(unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
6004a09d09b8STao Ma 		num_to_flush);
6005ccd979bdSMark Fasheh 	if (!num_to_flush) {
6006ccd979bdSMark Fasheh 		status = 0;
6007e08dc8b9SMark Fasheh 		goto out;
6008ccd979bdSMark Fasheh 	}
6009ccd979bdSMark Fasheh 
6010ccd979bdSMark Fasheh 	data_alloc_inode = ocfs2_get_system_file_inode(osb,
6011ccd979bdSMark Fasheh 						       GLOBAL_BITMAP_SYSTEM_INODE,
6012ccd979bdSMark Fasheh 						       OCFS2_INVALID_SLOT);
6013ccd979bdSMark Fasheh 	if (!data_alloc_inode) {
6014ccd979bdSMark Fasheh 		status = -EINVAL;
6015ccd979bdSMark Fasheh 		mlog(ML_ERROR, "Could not get bitmap inode!\n");
6016e08dc8b9SMark Fasheh 		goto out;
6017ccd979bdSMark Fasheh 	}
6018ccd979bdSMark Fasheh 
60195955102cSAl Viro 	inode_lock(data_alloc_inode);
6020e08dc8b9SMark Fasheh 
6021e63aecb6SMark Fasheh 	status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
6022ccd979bdSMark Fasheh 	if (status < 0) {
6023ccd979bdSMark Fasheh 		mlog_errno(status);
6024e08dc8b9SMark Fasheh 		goto out_mutex;
6025ccd979bdSMark Fasheh 	}
6026ccd979bdSMark Fasheh 
60272b0ad008SJunxiao Bi 	status = ocfs2_replay_truncate_records(osb, data_alloc_inode,
6028ccd979bdSMark Fasheh 					       data_alloc_bh);
6029e08dc8b9SMark Fasheh 	if (status < 0)
6030ccd979bdSMark Fasheh 		mlog_errno(status);
6031ccd979bdSMark Fasheh 
6032e08dc8b9SMark Fasheh 	brelse(data_alloc_bh);
6033e63aecb6SMark Fasheh 	ocfs2_inode_unlock(data_alloc_inode, 1);
6034e08dc8b9SMark Fasheh 
6035e08dc8b9SMark Fasheh out_mutex:
60365955102cSAl Viro 	inode_unlock(data_alloc_inode);
6037ccd979bdSMark Fasheh 	iput(data_alloc_inode);
6038ccd979bdSMark Fasheh 
6039e08dc8b9SMark Fasheh out:
6040ccd979bdSMark Fasheh 	return status;
6041ccd979bdSMark Fasheh }
6042ccd979bdSMark Fasheh 
6043ccd979bdSMark Fasheh int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
6044ccd979bdSMark Fasheh {
6045ccd979bdSMark Fasheh 	int status;
6046ccd979bdSMark Fasheh 	struct inode *tl_inode = osb->osb_tl_inode;
6047ccd979bdSMark Fasheh 
60485955102cSAl Viro 	inode_lock(tl_inode);
6049ccd979bdSMark Fasheh 	status = __ocfs2_flush_truncate_log(osb);
60505955102cSAl Viro 	inode_unlock(tl_inode);
6051ccd979bdSMark Fasheh 
6052ccd979bdSMark Fasheh 	return status;
6053ccd979bdSMark Fasheh }
6054ccd979bdSMark Fasheh 
6055c4028958SDavid Howells static void ocfs2_truncate_log_worker(struct work_struct *work)
6056ccd979bdSMark Fasheh {
6057ccd979bdSMark Fasheh 	int status;
6058c4028958SDavid Howells 	struct ocfs2_super *osb =
6059c4028958SDavid Howells 		container_of(work, struct ocfs2_super,
6060c4028958SDavid Howells 			     osb_truncate_log_wq.work);
6061ccd979bdSMark Fasheh 
6062ccd979bdSMark Fasheh 	status = ocfs2_flush_truncate_log(osb);
6063ccd979bdSMark Fasheh 	if (status < 0)
6064ccd979bdSMark Fasheh 		mlog_errno(status);
60654d0ddb2cSTao Ma 	else
6066b89c5428STiger Yang 		ocfs2_init_steal_slots(osb);
6067ccd979bdSMark Fasheh }
6068ccd979bdSMark Fasheh 
6069ccd979bdSMark Fasheh #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
6070ccd979bdSMark Fasheh void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
6071ccd979bdSMark Fasheh 				       int cancel)
6072ccd979bdSMark Fasheh {
6073a9e9acaeSXue jiufei 	if (osb->osb_tl_inode &&
6074a9e9acaeSXue jiufei 			atomic_read(&osb->osb_tl_disable) == 0) {
6075ccd979bdSMark Fasheh 		/* We want to push off log flushes while truncates are
6076ccd979bdSMark Fasheh 		 * still running. */
6077ccd979bdSMark Fasheh 		if (cancel)
6078ccd979bdSMark Fasheh 			cancel_delayed_work(&osb->osb_truncate_log_wq);
6079ccd979bdSMark Fasheh 
608035ddf78eSjiangyiwen 		queue_delayed_work(osb->ocfs2_wq, &osb->osb_truncate_log_wq,
6081ccd979bdSMark Fasheh 				   OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
6082ccd979bdSMark Fasheh 	}
6083ccd979bdSMark Fasheh }
6084ccd979bdSMark Fasheh 
60852070ad1aSEric Ren /*
60862070ad1aSEric Ren  * Try to flush truncate logs if we can free enough clusters from it.
60872070ad1aSEric Ren  * As for return value, "< 0" means error, "0" no space and "1" means
60882070ad1aSEric Ren  * we have freed enough spaces and let the caller try to allocate again.
60892070ad1aSEric Ren  */
60902070ad1aSEric Ren int ocfs2_try_to_free_truncate_log(struct ocfs2_super *osb,
60912070ad1aSEric Ren 					unsigned int needed)
60922070ad1aSEric Ren {
60932070ad1aSEric Ren 	tid_t target;
60942070ad1aSEric Ren 	int ret = 0;
60952070ad1aSEric Ren 	unsigned int truncated_clusters;
60962070ad1aSEric Ren 
60972070ad1aSEric Ren 	inode_lock(osb->osb_tl_inode);
60982070ad1aSEric Ren 	truncated_clusters = osb->truncated_clusters;
60992070ad1aSEric Ren 	inode_unlock(osb->osb_tl_inode);
61002070ad1aSEric Ren 
61012070ad1aSEric Ren 	/*
61022070ad1aSEric Ren 	 * Check whether we can succeed in allocating if we free
61032070ad1aSEric Ren 	 * the truncate log.
61042070ad1aSEric Ren 	 */
61052070ad1aSEric Ren 	if (truncated_clusters < needed)
61062070ad1aSEric Ren 		goto out;
61072070ad1aSEric Ren 
61082070ad1aSEric Ren 	ret = ocfs2_flush_truncate_log(osb);
61092070ad1aSEric Ren 	if (ret) {
61102070ad1aSEric Ren 		mlog_errno(ret);
61112070ad1aSEric Ren 		goto out;
61122070ad1aSEric Ren 	}
61132070ad1aSEric Ren 
61142070ad1aSEric Ren 	if (jbd2_journal_start_commit(osb->journal->j_journal, &target)) {
61152070ad1aSEric Ren 		jbd2_log_wait_commit(osb->journal->j_journal, target);
61162070ad1aSEric Ren 		ret = 1;
61172070ad1aSEric Ren 	}
61182070ad1aSEric Ren out:
61192070ad1aSEric Ren 	return ret;
61202070ad1aSEric Ren }
61212070ad1aSEric Ren 
6122ccd979bdSMark Fasheh static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
6123ccd979bdSMark Fasheh 				       int slot_num,
6124ccd979bdSMark Fasheh 				       struct inode **tl_inode,
6125ccd979bdSMark Fasheh 				       struct buffer_head **tl_bh)
6126ccd979bdSMark Fasheh {
6127ccd979bdSMark Fasheh 	int status;
6128ccd979bdSMark Fasheh 	struct inode *inode = NULL;
6129ccd979bdSMark Fasheh 	struct buffer_head *bh = NULL;
6130ccd979bdSMark Fasheh 
6131ccd979bdSMark Fasheh 	inode = ocfs2_get_system_file_inode(osb,
6132ccd979bdSMark Fasheh 					   TRUNCATE_LOG_SYSTEM_INODE,
6133ccd979bdSMark Fasheh 					   slot_num);
6134ccd979bdSMark Fasheh 	if (!inode) {
6135ccd979bdSMark Fasheh 		status = -EINVAL;
6136ccd979bdSMark Fasheh 		mlog(ML_ERROR, "Could not get load truncate log inode!\n");
6137ccd979bdSMark Fasheh 		goto bail;
6138ccd979bdSMark Fasheh 	}
6139ccd979bdSMark Fasheh 
6140b657c95cSJoel Becker 	status = ocfs2_read_inode_block(inode, &bh);
6141ccd979bdSMark Fasheh 	if (status < 0) {
6142ccd979bdSMark Fasheh 		iput(inode);
6143ccd979bdSMark Fasheh 		mlog_errno(status);
6144ccd979bdSMark Fasheh 		goto bail;
6145ccd979bdSMark Fasheh 	}
6146ccd979bdSMark Fasheh 
6147ccd979bdSMark Fasheh 	*tl_inode = inode;
6148ccd979bdSMark Fasheh 	*tl_bh    = bh;
6149ccd979bdSMark Fasheh bail:
6150ccd979bdSMark Fasheh 	return status;
6151ccd979bdSMark Fasheh }
6152ccd979bdSMark Fasheh 
6153ccd979bdSMark Fasheh /* called during the 1st stage of node recovery. we stamp a clean
6154ccd979bdSMark Fasheh  * truncate log and pass back a copy for processing later. if the
6155ccd979bdSMark Fasheh  * truncate log does not require processing, a *tl_copy is set to
6156ccd979bdSMark Fasheh  * NULL. */
6157ccd979bdSMark Fasheh int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
6158ccd979bdSMark Fasheh 				      int slot_num,
6159ccd979bdSMark Fasheh 				      struct ocfs2_dinode **tl_copy)
6160ccd979bdSMark Fasheh {
6161ccd979bdSMark Fasheh 	int status;
6162ccd979bdSMark Fasheh 	struct inode *tl_inode = NULL;
6163ccd979bdSMark Fasheh 	struct buffer_head *tl_bh = NULL;
6164ccd979bdSMark Fasheh 	struct ocfs2_dinode *di;
6165ccd979bdSMark Fasheh 	struct ocfs2_truncate_log *tl;
6166ccd979bdSMark Fasheh 
6167ccd979bdSMark Fasheh 	*tl_copy = NULL;
6168ccd979bdSMark Fasheh 
6169a09d09b8STao Ma 	trace_ocfs2_begin_truncate_log_recovery(slot_num);
6170ccd979bdSMark Fasheh 
6171ccd979bdSMark Fasheh 	status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
6172ccd979bdSMark Fasheh 	if (status < 0) {
6173ccd979bdSMark Fasheh 		mlog_errno(status);
6174ccd979bdSMark Fasheh 		goto bail;
6175ccd979bdSMark Fasheh 	}
6176ccd979bdSMark Fasheh 
6177ccd979bdSMark Fasheh 	di = (struct ocfs2_dinode *) tl_bh->b_data;
6178ccd979bdSMark Fasheh 
617910995aa2SJoel Becker 	/* tl_bh is loaded from ocfs2_get_truncate_log_info().  It's
618010995aa2SJoel Becker 	 * validated by the underlying call to ocfs2_read_inode_block(),
618110995aa2SJoel Becker 	 * so any corruption is a code bug */
618210995aa2SJoel Becker 	BUG_ON(!OCFS2_IS_VALID_DINODE(di));
618310995aa2SJoel Becker 
618410995aa2SJoel Becker 	tl = &di->id2.i_dealloc;
6185ccd979bdSMark Fasheh 	if (le16_to_cpu(tl->tl_used)) {
6186a09d09b8STao Ma 		trace_ocfs2_truncate_log_recovery_num(le16_to_cpu(tl->tl_used));
6187ccd979bdSMark Fasheh 
6188ccd979bdSMark Fasheh 		*tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
6189ccd979bdSMark Fasheh 		if (!(*tl_copy)) {
6190ccd979bdSMark Fasheh 			status = -ENOMEM;
6191ccd979bdSMark Fasheh 			mlog_errno(status);
6192ccd979bdSMark Fasheh 			goto bail;
6193ccd979bdSMark Fasheh 		}
6194ccd979bdSMark Fasheh 
6195ccd979bdSMark Fasheh 		/* Assuming the write-out below goes well, this copy
6196ccd979bdSMark Fasheh 		 * will be passed back to recovery for processing. */
6197ccd979bdSMark Fasheh 		memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
6198ccd979bdSMark Fasheh 
6199ccd979bdSMark Fasheh 		/* All we need to do to clear the truncate log is set
6200ccd979bdSMark Fasheh 		 * tl_used. */
6201ccd979bdSMark Fasheh 		tl->tl_used = 0;
6202ccd979bdSMark Fasheh 
620313723d00SJoel Becker 		ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check);
62048cb471e8SJoel Becker 		status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode));
6205ccd979bdSMark Fasheh 		if (status < 0) {
6206ccd979bdSMark Fasheh 			mlog_errno(status);
6207ccd979bdSMark Fasheh 			goto bail;
6208ccd979bdSMark Fasheh 		}
6209ccd979bdSMark Fasheh 	}
6210ccd979bdSMark Fasheh 
6211ccd979bdSMark Fasheh bail:
6212ccd979bdSMark Fasheh 	iput(tl_inode);
6213ccd979bdSMark Fasheh 	brelse(tl_bh);
6214ccd979bdSMark Fasheh 
621546359295SJoseph Qi 	if (status < 0) {
6216ccd979bdSMark Fasheh 		kfree(*tl_copy);
6217ccd979bdSMark Fasheh 		*tl_copy = NULL;
6218c1e8d35eSTao Ma 		mlog_errno(status);
6219ccd979bdSMark Fasheh 	}
6220ccd979bdSMark Fasheh 
6221ccd979bdSMark Fasheh 	return status;
6222ccd979bdSMark Fasheh }
6223ccd979bdSMark Fasheh 
6224ccd979bdSMark Fasheh int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
6225ccd979bdSMark Fasheh 					 struct ocfs2_dinode *tl_copy)
6226ccd979bdSMark Fasheh {
6227ccd979bdSMark Fasheh 	int status = 0;
6228ccd979bdSMark Fasheh 	int i;
6229ccd979bdSMark Fasheh 	unsigned int clusters, num_recs, start_cluster;
6230ccd979bdSMark Fasheh 	u64 start_blk;
62311fabe148SMark Fasheh 	handle_t *handle;
6232ccd979bdSMark Fasheh 	struct inode *tl_inode = osb->osb_tl_inode;
6233ccd979bdSMark Fasheh 	struct ocfs2_truncate_log *tl;
6234ccd979bdSMark Fasheh 
6235ccd979bdSMark Fasheh 	if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
6236ccd979bdSMark Fasheh 		mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
6237ccd979bdSMark Fasheh 		return -EINVAL;
6238ccd979bdSMark Fasheh 	}
6239ccd979bdSMark Fasheh 
6240ccd979bdSMark Fasheh 	tl = &tl_copy->id2.i_dealloc;
6241ccd979bdSMark Fasheh 	num_recs = le16_to_cpu(tl->tl_used);
6242a09d09b8STao Ma 	trace_ocfs2_complete_truncate_log_recovery(
6243a09d09b8STao Ma 		(unsigned long long)le64_to_cpu(tl_copy->i_blkno),
6244a09d09b8STao Ma 		num_recs);
6245ccd979bdSMark Fasheh 
62465955102cSAl Viro 	inode_lock(tl_inode);
6247ccd979bdSMark Fasheh 	for(i = 0; i < num_recs; i++) {
6248ccd979bdSMark Fasheh 		if (ocfs2_truncate_log_needs_flush(osb)) {
6249ccd979bdSMark Fasheh 			status = __ocfs2_flush_truncate_log(osb);
6250ccd979bdSMark Fasheh 			if (status < 0) {
6251ccd979bdSMark Fasheh 				mlog_errno(status);
6252ccd979bdSMark Fasheh 				goto bail_up;
6253ccd979bdSMark Fasheh 			}
6254ccd979bdSMark Fasheh 		}
6255ccd979bdSMark Fasheh 
625665eff9ccSMark Fasheh 		handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6257ccd979bdSMark Fasheh 		if (IS_ERR(handle)) {
6258ccd979bdSMark Fasheh 			status = PTR_ERR(handle);
6259ccd979bdSMark Fasheh 			mlog_errno(status);
6260ccd979bdSMark Fasheh 			goto bail_up;
6261ccd979bdSMark Fasheh 		}
6262ccd979bdSMark Fasheh 
6263ccd979bdSMark Fasheh 		clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
6264ccd979bdSMark Fasheh 		start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
6265ccd979bdSMark Fasheh 		start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
6266ccd979bdSMark Fasheh 
6267ccd979bdSMark Fasheh 		status = ocfs2_truncate_log_append(osb, handle,
6268ccd979bdSMark Fasheh 						   start_blk, clusters);
626902dc1af4SMark Fasheh 		ocfs2_commit_trans(osb, handle);
6270ccd979bdSMark Fasheh 		if (status < 0) {
6271ccd979bdSMark Fasheh 			mlog_errno(status);
6272ccd979bdSMark Fasheh 			goto bail_up;
6273ccd979bdSMark Fasheh 		}
6274ccd979bdSMark Fasheh 	}
6275ccd979bdSMark Fasheh 
6276ccd979bdSMark Fasheh bail_up:
62775955102cSAl Viro 	inode_unlock(tl_inode);
6278ccd979bdSMark Fasheh 
6279ccd979bdSMark Fasheh 	return status;
6280ccd979bdSMark Fasheh }
6281ccd979bdSMark Fasheh 
6282ccd979bdSMark Fasheh void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
6283ccd979bdSMark Fasheh {
6284ccd979bdSMark Fasheh 	int status;
6285ccd979bdSMark Fasheh 	struct inode *tl_inode = osb->osb_tl_inode;
6286ccd979bdSMark Fasheh 
6287a9e9acaeSXue jiufei 	atomic_set(&osb->osb_tl_disable, 1);
6288a9e9acaeSXue jiufei 
6289ccd979bdSMark Fasheh 	if (tl_inode) {
6290ccd979bdSMark Fasheh 		cancel_delayed_work(&osb->osb_truncate_log_wq);
629135ddf78eSjiangyiwen 		flush_workqueue(osb->ocfs2_wq);
6292ccd979bdSMark Fasheh 
6293ccd979bdSMark Fasheh 		status = ocfs2_flush_truncate_log(osb);
6294ccd979bdSMark Fasheh 		if (status < 0)
6295ccd979bdSMark Fasheh 			mlog_errno(status);
6296ccd979bdSMark Fasheh 
6297ccd979bdSMark Fasheh 		brelse(osb->osb_tl_bh);
6298ccd979bdSMark Fasheh 		iput(osb->osb_tl_inode);
6299ccd979bdSMark Fasheh 	}
6300ccd979bdSMark Fasheh }
6301ccd979bdSMark Fasheh 
6302ccd979bdSMark Fasheh int ocfs2_truncate_log_init(struct ocfs2_super *osb)
6303ccd979bdSMark Fasheh {
6304ccd979bdSMark Fasheh 	int status;
6305ccd979bdSMark Fasheh 	struct inode *tl_inode = NULL;
6306ccd979bdSMark Fasheh 	struct buffer_head *tl_bh = NULL;
6307ccd979bdSMark Fasheh 
6308ccd979bdSMark Fasheh 	status = ocfs2_get_truncate_log_info(osb,
6309ccd979bdSMark Fasheh 					     osb->slot_num,
6310ccd979bdSMark Fasheh 					     &tl_inode,
6311ccd979bdSMark Fasheh 					     &tl_bh);
6312ccd979bdSMark Fasheh 	if (status < 0)
6313ccd979bdSMark Fasheh 		mlog_errno(status);
6314ccd979bdSMark Fasheh 
6315ccd979bdSMark Fasheh 	/* ocfs2_truncate_log_shutdown keys on the existence of
6316ccd979bdSMark Fasheh 	 * osb->osb_tl_inode so we don't set any of the osb variables
6317ccd979bdSMark Fasheh 	 * until we're sure all is well. */
6318c4028958SDavid Howells 	INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
6319c4028958SDavid Howells 			  ocfs2_truncate_log_worker);
6320a9e9acaeSXue jiufei 	atomic_set(&osb->osb_tl_disable, 0);
6321ccd979bdSMark Fasheh 	osb->osb_tl_bh    = tl_bh;
6322ccd979bdSMark Fasheh 	osb->osb_tl_inode = tl_inode;
6323ccd979bdSMark Fasheh 
6324ccd979bdSMark Fasheh 	return status;
6325ccd979bdSMark Fasheh }
6326ccd979bdSMark Fasheh 
63272b604351SMark Fasheh /*
63282b604351SMark Fasheh  * Delayed de-allocation of suballocator blocks.
63292b604351SMark Fasheh  *
63302b604351SMark Fasheh  * Some sets of block de-allocations might involve multiple suballocator inodes.
63312b604351SMark Fasheh  *
63322b604351SMark Fasheh  * The locking for this can get extremely complicated, especially when
63332b604351SMark Fasheh  * the suballocator inodes to delete from aren't known until deep
63342b604351SMark Fasheh  * within an unrelated codepath.
63352b604351SMark Fasheh  *
63362b604351SMark Fasheh  * ocfs2_extent_block structures are a good example of this - an inode
63372b604351SMark Fasheh  * btree could have been grown by any number of nodes each allocating
63382b604351SMark Fasheh  * out of their own suballoc inode.
63392b604351SMark Fasheh  *
63402b604351SMark Fasheh  * These structures allow the delay of block de-allocation until a
63412b604351SMark Fasheh  * later time, when locking of multiple cluster inodes won't cause
63422b604351SMark Fasheh  * deadlock.
63432b604351SMark Fasheh  */
63442b604351SMark Fasheh 
63452b604351SMark Fasheh /*
63462891d290STao Ma  * Describe a single bit freed from a suballocator.  For the block
63472891d290STao Ma  * suballocators, it represents one block.  For the global cluster
63482891d290STao Ma  * allocator, it represents some clusters and free_bit indicates
63492891d290STao Ma  * clusters number.
63502b604351SMark Fasheh  */
63512b604351SMark Fasheh struct ocfs2_cached_block_free {
63522b604351SMark Fasheh 	struct ocfs2_cached_block_free		*free_next;
635374380c47STao Ma 	u64					free_bg;
63542b604351SMark Fasheh 	u64					free_blk;
63552b604351SMark Fasheh 	unsigned int				free_bit;
63562b604351SMark Fasheh };
63572b604351SMark Fasheh 
63582b604351SMark Fasheh struct ocfs2_per_slot_free_list {
63592b604351SMark Fasheh 	struct ocfs2_per_slot_free_list		*f_next_suballocator;
63602b604351SMark Fasheh 	int					f_inode_type;
63612b604351SMark Fasheh 	int					f_slot;
63622b604351SMark Fasheh 	struct ocfs2_cached_block_free		*f_first;
63632b604351SMark Fasheh };
63642b604351SMark Fasheh 
63652891d290STao Ma static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
63662b604351SMark Fasheh 				    int sysfile_type,
63672b604351SMark Fasheh 				    int slot,
63682b604351SMark Fasheh 				    struct ocfs2_cached_block_free *head)
63692b604351SMark Fasheh {
63702b604351SMark Fasheh 	int ret;
63712b604351SMark Fasheh 	u64 bg_blkno;
63722b604351SMark Fasheh 	handle_t *handle;
63732b604351SMark Fasheh 	struct inode *inode;
63742b604351SMark Fasheh 	struct buffer_head *di_bh = NULL;
63752b604351SMark Fasheh 	struct ocfs2_cached_block_free *tmp;
63762b604351SMark Fasheh 
63772b604351SMark Fasheh 	inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
63782b604351SMark Fasheh 	if (!inode) {
63792b604351SMark Fasheh 		ret = -EINVAL;
63802b604351SMark Fasheh 		mlog_errno(ret);
63812b604351SMark Fasheh 		goto out;
63822b604351SMark Fasheh 	}
63832b604351SMark Fasheh 
63845955102cSAl Viro 	inode_lock(inode);
63852b604351SMark Fasheh 
6386e63aecb6SMark Fasheh 	ret = ocfs2_inode_lock(inode, &di_bh, 1);
63872b604351SMark Fasheh 	if (ret) {
63882b604351SMark Fasheh 		mlog_errno(ret);
63892b604351SMark Fasheh 		goto out_mutex;
63902b604351SMark Fasheh 	}
63912b604351SMark Fasheh 
6392d5bf1418SJunxiao Bi 	while (head) {
6393d5bf1418SJunxiao Bi 		if (head->free_bg)
6394d5bf1418SJunxiao Bi 			bg_blkno = head->free_bg;
6395d5bf1418SJunxiao Bi 		else
6396d5bf1418SJunxiao Bi 			bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
6397d5bf1418SJunxiao Bi 							      head->free_bit);
63982b604351SMark Fasheh 		handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
63992b604351SMark Fasheh 		if (IS_ERR(handle)) {
64002b604351SMark Fasheh 			ret = PTR_ERR(handle);
64012b604351SMark Fasheh 			mlog_errno(ret);
64022b604351SMark Fasheh 			goto out_unlock;
64032b604351SMark Fasheh 		}
64042b604351SMark Fasheh 
6405a09d09b8STao Ma 		trace_ocfs2_free_cached_blocks(
6406a09d09b8STao Ma 		     (unsigned long long)head->free_blk, head->free_bit);
64072b604351SMark Fasheh 
64082b604351SMark Fasheh 		ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
64092b604351SMark Fasheh 					       head->free_bit, bg_blkno, 1);
6410d5bf1418SJunxiao Bi 		if (ret)
64112b604351SMark Fasheh 			mlog_errno(ret);
64122b604351SMark Fasheh 
6413d5bf1418SJunxiao Bi 		ocfs2_commit_trans(osb, handle);
64142b604351SMark Fasheh 
64152b604351SMark Fasheh 		tmp = head;
64162b604351SMark Fasheh 		head = head->free_next;
64172b604351SMark Fasheh 		kfree(tmp);
64182b604351SMark Fasheh 	}
64192b604351SMark Fasheh 
64202b604351SMark Fasheh out_unlock:
6421e63aecb6SMark Fasheh 	ocfs2_inode_unlock(inode, 1);
64222b604351SMark Fasheh 	brelse(di_bh);
64232b604351SMark Fasheh out_mutex:
64245955102cSAl Viro 	inode_unlock(inode);
64252b604351SMark Fasheh 	iput(inode);
64262b604351SMark Fasheh out:
64272b604351SMark Fasheh 	while(head) {
64282b604351SMark Fasheh 		/* Premature exit may have left some dangling items. */
64292b604351SMark Fasheh 		tmp = head;
64302b604351SMark Fasheh 		head = head->free_next;
64312b604351SMark Fasheh 		kfree(tmp);
64322b604351SMark Fasheh 	}
64332b604351SMark Fasheh 
64342b604351SMark Fasheh 	return ret;
64352b604351SMark Fasheh }
64362b604351SMark Fasheh 
64372891d290STao Ma int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
64382891d290STao Ma 				u64 blkno, unsigned int bit)
64392891d290STao Ma {
64402891d290STao Ma 	int ret = 0;
64412891d290STao Ma 	struct ocfs2_cached_block_free *item;
64422891d290STao Ma 
644374380c47STao Ma 	item = kzalloc(sizeof(*item), GFP_NOFS);
64442891d290STao Ma 	if (item == NULL) {
64452891d290STao Ma 		ret = -ENOMEM;
64462891d290STao Ma 		mlog_errno(ret);
64472891d290STao Ma 		return ret;
64482891d290STao Ma 	}
64492891d290STao Ma 
6450a09d09b8STao Ma 	trace_ocfs2_cache_cluster_dealloc((unsigned long long)blkno, bit);
64512891d290STao Ma 
64522891d290STao Ma 	item->free_blk = blkno;
64532891d290STao Ma 	item->free_bit = bit;
64542891d290STao Ma 	item->free_next = ctxt->c_global_allocator;
64552891d290STao Ma 
64562891d290STao Ma 	ctxt->c_global_allocator = item;
64572891d290STao Ma 	return ret;
64582891d290STao Ma }
64592891d290STao Ma 
64602891d290STao Ma static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
64612891d290STao Ma 				      struct ocfs2_cached_block_free *head)
64622891d290STao Ma {
64632891d290STao Ma 	struct ocfs2_cached_block_free *tmp;
64642891d290STao Ma 	struct inode *tl_inode = osb->osb_tl_inode;
64652891d290STao Ma 	handle_t *handle;
64662891d290STao Ma 	int ret = 0;
64672891d290STao Ma 
64685955102cSAl Viro 	inode_lock(tl_inode);
64692891d290STao Ma 
64702891d290STao Ma 	while (head) {
64712891d290STao Ma 		if (ocfs2_truncate_log_needs_flush(osb)) {
64722891d290STao Ma 			ret = __ocfs2_flush_truncate_log(osb);
64732891d290STao Ma 			if (ret < 0) {
64742891d290STao Ma 				mlog_errno(ret);
64752891d290STao Ma 				break;
64762891d290STao Ma 			}
64772891d290STao Ma 		}
64782891d290STao Ma 
64792891d290STao Ma 		handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
64802891d290STao Ma 		if (IS_ERR(handle)) {
64812891d290STao Ma 			ret = PTR_ERR(handle);
64822891d290STao Ma 			mlog_errno(ret);
64832891d290STao Ma 			break;
64842891d290STao Ma 		}
64852891d290STao Ma 
64862891d290STao Ma 		ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
64872891d290STao Ma 						head->free_bit);
64882891d290STao Ma 
64892891d290STao Ma 		ocfs2_commit_trans(osb, handle);
64902891d290STao Ma 		tmp = head;
64912891d290STao Ma 		head = head->free_next;
64922891d290STao Ma 		kfree(tmp);
64932891d290STao Ma 
64942891d290STao Ma 		if (ret < 0) {
64952891d290STao Ma 			mlog_errno(ret);
64962891d290STao Ma 			break;
64972891d290STao Ma 		}
64982891d290STao Ma 	}
64992891d290STao Ma 
65005955102cSAl Viro 	inode_unlock(tl_inode);
65012891d290STao Ma 
65022891d290STao Ma 	while (head) {
65032891d290STao Ma 		/* Premature exit may have left some dangling items. */
65042891d290STao Ma 		tmp = head;
65052891d290STao Ma 		head = head->free_next;
65062891d290STao Ma 		kfree(tmp);
65072891d290STao Ma 	}
65082891d290STao Ma 
65092891d290STao Ma 	return ret;
65102891d290STao Ma }
65112891d290STao Ma 
65122b604351SMark Fasheh int ocfs2_run_deallocs(struct ocfs2_super *osb,
65132b604351SMark Fasheh 		       struct ocfs2_cached_dealloc_ctxt *ctxt)
65142b604351SMark Fasheh {
65152b604351SMark Fasheh 	int ret = 0, ret2;
65162b604351SMark Fasheh 	struct ocfs2_per_slot_free_list *fl;
65172b604351SMark Fasheh 
65182b604351SMark Fasheh 	if (!ctxt)
65192b604351SMark Fasheh 		return 0;
65202b604351SMark Fasheh 
65212b604351SMark Fasheh 	while (ctxt->c_first_suballocator) {
65222b604351SMark Fasheh 		fl = ctxt->c_first_suballocator;
65232b604351SMark Fasheh 
65242b604351SMark Fasheh 		if (fl->f_first) {
6525a09d09b8STao Ma 			trace_ocfs2_run_deallocs(fl->f_inode_type,
6526a09d09b8STao Ma 						 fl->f_slot);
65272891d290STao Ma 			ret2 = ocfs2_free_cached_blocks(osb,
65282891d290STao Ma 							fl->f_inode_type,
65292891d290STao Ma 							fl->f_slot,
65302891d290STao Ma 							fl->f_first);
65312b604351SMark Fasheh 			if (ret2)
65322b604351SMark Fasheh 				mlog_errno(ret2);
65332b604351SMark Fasheh 			if (!ret)
65342b604351SMark Fasheh 				ret = ret2;
65352b604351SMark Fasheh 		}
65362b604351SMark Fasheh 
65372b604351SMark Fasheh 		ctxt->c_first_suballocator = fl->f_next_suballocator;
65382b604351SMark Fasheh 		kfree(fl);
65392b604351SMark Fasheh 	}
65402b604351SMark Fasheh 
65412891d290STao Ma 	if (ctxt->c_global_allocator) {
65422891d290STao Ma 		ret2 = ocfs2_free_cached_clusters(osb,
65432891d290STao Ma 						  ctxt->c_global_allocator);
65442891d290STao Ma 		if (ret2)
65452891d290STao Ma 			mlog_errno(ret2);
65462891d290STao Ma 		if (!ret)
65472891d290STao Ma 			ret = ret2;
65482891d290STao Ma 
65492891d290STao Ma 		ctxt->c_global_allocator = NULL;
65502891d290STao Ma 	}
65512891d290STao Ma 
65522b604351SMark Fasheh 	return ret;
65532b604351SMark Fasheh }
65542b604351SMark Fasheh 
65552b604351SMark Fasheh static struct ocfs2_per_slot_free_list *
65562b604351SMark Fasheh ocfs2_find_per_slot_free_list(int type,
65572b604351SMark Fasheh 			      int slot,
65582b604351SMark Fasheh 			      struct ocfs2_cached_dealloc_ctxt *ctxt)
65592b604351SMark Fasheh {
65602b604351SMark Fasheh 	struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
65612b604351SMark Fasheh 
65622b604351SMark Fasheh 	while (fl) {
65632b604351SMark Fasheh 		if (fl->f_inode_type == type && fl->f_slot == slot)
65642b604351SMark Fasheh 			return fl;
65652b604351SMark Fasheh 
65662b604351SMark Fasheh 		fl = fl->f_next_suballocator;
65672b604351SMark Fasheh 	}
65682b604351SMark Fasheh 
65692b604351SMark Fasheh 	fl = kmalloc(sizeof(*fl), GFP_NOFS);
65702b604351SMark Fasheh 	if (fl) {
65712b604351SMark Fasheh 		fl->f_inode_type = type;
65722b604351SMark Fasheh 		fl->f_slot = slot;
65732b604351SMark Fasheh 		fl->f_first = NULL;
65742b604351SMark Fasheh 		fl->f_next_suballocator = ctxt->c_first_suballocator;
65752b604351SMark Fasheh 
65762b604351SMark Fasheh 		ctxt->c_first_suballocator = fl;
65772b604351SMark Fasheh 	}
65782b604351SMark Fasheh 	return fl;
65792b604351SMark Fasheh }
65802b604351SMark Fasheh 
65811823cb0bSTao Ma int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
658274380c47STao Ma 			      int type, int slot, u64 suballoc,
658374380c47STao Ma 			      u64 blkno, unsigned int bit)
65842b604351SMark Fasheh {
65852b604351SMark Fasheh 	int ret;
65862b604351SMark Fasheh 	struct ocfs2_per_slot_free_list *fl;
65872b604351SMark Fasheh 	struct ocfs2_cached_block_free *item;
65882b604351SMark Fasheh 
65892b604351SMark Fasheh 	fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
65902b604351SMark Fasheh 	if (fl == NULL) {
65912b604351SMark Fasheh 		ret = -ENOMEM;
65922b604351SMark Fasheh 		mlog_errno(ret);
65932b604351SMark Fasheh 		goto out;
65942b604351SMark Fasheh 	}
65952b604351SMark Fasheh 
659674380c47STao Ma 	item = kzalloc(sizeof(*item), GFP_NOFS);
65972b604351SMark Fasheh 	if (item == NULL) {
65982b604351SMark Fasheh 		ret = -ENOMEM;
65992b604351SMark Fasheh 		mlog_errno(ret);
66002b604351SMark Fasheh 		goto out;
66012b604351SMark Fasheh 	}
66022b604351SMark Fasheh 
6603a09d09b8STao Ma 	trace_ocfs2_cache_block_dealloc(type, slot,
6604a09d09b8STao Ma 					(unsigned long long)suballoc,
6605a09d09b8STao Ma 					(unsigned long long)blkno, bit);
66062b604351SMark Fasheh 
660774380c47STao Ma 	item->free_bg = suballoc;
66082b604351SMark Fasheh 	item->free_blk = blkno;
66092b604351SMark Fasheh 	item->free_bit = bit;
66102b604351SMark Fasheh 	item->free_next = fl->f_first;
66112b604351SMark Fasheh 
66122b604351SMark Fasheh 	fl->f_first = item;
66132b604351SMark Fasheh 
66142b604351SMark Fasheh 	ret = 0;
66152b604351SMark Fasheh out:
66162b604351SMark Fasheh 	return ret;
66172b604351SMark Fasheh }
66182b604351SMark Fasheh 
661959a5e416SMark Fasheh static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
662059a5e416SMark Fasheh 					 struct ocfs2_extent_block *eb)
662159a5e416SMark Fasheh {
662259a5e416SMark Fasheh 	return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
662359a5e416SMark Fasheh 					 le16_to_cpu(eb->h_suballoc_slot),
662474380c47STao Ma 					 le64_to_cpu(eb->h_suballoc_loc),
662559a5e416SMark Fasheh 					 le64_to_cpu(eb->h_blkno),
662659a5e416SMark Fasheh 					 le16_to_cpu(eb->h_suballoc_bit));
662759a5e416SMark Fasheh }
662859a5e416SMark Fasheh 
66292b4e30fbSJoel Becker static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
663060b11392SMark Fasheh {
663160b11392SMark Fasheh 	set_buffer_uptodate(bh);
663260b11392SMark Fasheh 	mark_buffer_dirty(bh);
663360b11392SMark Fasheh 	return 0;
663460b11392SMark Fasheh }
663560b11392SMark Fasheh 
66366f70fa51STao Ma void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
66371d410a6eSMark Fasheh 			      unsigned int from, unsigned int to,
66381d410a6eSMark Fasheh 			      struct page *page, int zero, u64 *phys)
663960b11392SMark Fasheh {
66401d410a6eSMark Fasheh 	int ret, partial = 0;
664160b11392SMark Fasheh 
66421d410a6eSMark Fasheh 	ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
664360b11392SMark Fasheh 	if (ret)
664460b11392SMark Fasheh 		mlog_errno(ret);
664560b11392SMark Fasheh 
66461d410a6eSMark Fasheh 	if (zero)
6647eebd2aa3SChristoph Lameter 		zero_user_segment(page, from, to);
664860b11392SMark Fasheh 
664960b11392SMark Fasheh 	/*
665060b11392SMark Fasheh 	 * Need to set the buffers we zero'd into uptodate
665160b11392SMark Fasheh 	 * here if they aren't - ocfs2_map_page_blocks()
665260b11392SMark Fasheh 	 * might've skipped some
665360b11392SMark Fasheh 	 */
665460b11392SMark Fasheh 	ret = walk_page_buffers(handle, page_buffers(page),
665560b11392SMark Fasheh 				from, to, &partial,
66562b4e30fbSJoel Becker 				ocfs2_zero_func);
66572b4e30fbSJoel Becker 	if (ret < 0)
66582b4e30fbSJoel Becker 		mlog_errno(ret);
66592b4e30fbSJoel Becker 	else if (ocfs2_should_order_data(inode)) {
66602b4e30fbSJoel Becker 		ret = ocfs2_jbd2_file_inode(handle, inode);
666160b11392SMark Fasheh 		if (ret < 0)
666260b11392SMark Fasheh 			mlog_errno(ret);
666360b11392SMark Fasheh 	}
666460b11392SMark Fasheh 
666560b11392SMark Fasheh 	if (!partial)
666660b11392SMark Fasheh 		SetPageUptodate(page);
666760b11392SMark Fasheh 
666860b11392SMark Fasheh 	flush_dcache_page(page);
66691d410a6eSMark Fasheh }
66701d410a6eSMark Fasheh 
66711d410a6eSMark Fasheh static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
66721d410a6eSMark Fasheh 				     loff_t end, struct page **pages,
66731d410a6eSMark Fasheh 				     int numpages, u64 phys, handle_t *handle)
66741d410a6eSMark Fasheh {
66751d410a6eSMark Fasheh 	int i;
66761d410a6eSMark Fasheh 	struct page *page;
667709cbfeafSKirill A. Shutemov 	unsigned int from, to = PAGE_SIZE;
66781d410a6eSMark Fasheh 	struct super_block *sb = inode->i_sb;
66791d410a6eSMark Fasheh 
66801d410a6eSMark Fasheh 	BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
66811d410a6eSMark Fasheh 
66821d410a6eSMark Fasheh 	if (numpages == 0)
66831d410a6eSMark Fasheh 		goto out;
66841d410a6eSMark Fasheh 
668509cbfeafSKirill A. Shutemov 	to = PAGE_SIZE;
66861d410a6eSMark Fasheh 	for(i = 0; i < numpages; i++) {
66871d410a6eSMark Fasheh 		page = pages[i];
66881d410a6eSMark Fasheh 
668909cbfeafSKirill A. Shutemov 		from = start & (PAGE_SIZE - 1);
669009cbfeafSKirill A. Shutemov 		if ((end >> PAGE_SHIFT) == page->index)
669109cbfeafSKirill A. Shutemov 			to = end & (PAGE_SIZE - 1);
66921d410a6eSMark Fasheh 
669309cbfeafSKirill A. Shutemov 		BUG_ON(from > PAGE_SIZE);
669409cbfeafSKirill A. Shutemov 		BUG_ON(to > PAGE_SIZE);
66951d410a6eSMark Fasheh 
66961d410a6eSMark Fasheh 		ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
66971d410a6eSMark Fasheh 					 &phys);
669860b11392SMark Fasheh 
669909cbfeafSKirill A. Shutemov 		start = (page->index + 1) << PAGE_SHIFT;
670060b11392SMark Fasheh 	}
670160b11392SMark Fasheh out:
67021d410a6eSMark Fasheh 	if (pages)
67031d410a6eSMark Fasheh 		ocfs2_unlock_and_free_pages(pages, numpages);
670460b11392SMark Fasheh }
670560b11392SMark Fasheh 
67066f70fa51STao Ma int ocfs2_grab_pages(struct inode *inode, loff_t start, loff_t end,
67071d410a6eSMark Fasheh 		     struct page **pages, int *num)
670860b11392SMark Fasheh {
67091d410a6eSMark Fasheh 	int numpages, ret = 0;
671060b11392SMark Fasheh 	struct address_space *mapping = inode->i_mapping;
671160b11392SMark Fasheh 	unsigned long index;
671235edec1dSMark Fasheh 	loff_t last_page_bytes;
671360b11392SMark Fasheh 
671435edec1dSMark Fasheh 	BUG_ON(start > end);
671560b11392SMark Fasheh 
67161d410a6eSMark Fasheh 	numpages = 0;
671735edec1dSMark Fasheh 	last_page_bytes = PAGE_ALIGN(end);
671809cbfeafSKirill A. Shutemov 	index = start >> PAGE_SHIFT;
671960b11392SMark Fasheh 	do {
67209b4c0ff3SJan Kara 		pages[numpages] = find_or_create_page(mapping, index, GFP_NOFS);
672160b11392SMark Fasheh 		if (!pages[numpages]) {
672260b11392SMark Fasheh 			ret = -ENOMEM;
672360b11392SMark Fasheh 			mlog_errno(ret);
672460b11392SMark Fasheh 			goto out;
672560b11392SMark Fasheh 		}
672660b11392SMark Fasheh 
672760b11392SMark Fasheh 		numpages++;
672860b11392SMark Fasheh 		index++;
672909cbfeafSKirill A. Shutemov 	} while (index < (last_page_bytes >> PAGE_SHIFT));
673060b11392SMark Fasheh 
673160b11392SMark Fasheh out:
673260b11392SMark Fasheh 	if (ret != 0) {
67331d410a6eSMark Fasheh 		if (pages)
67341d410a6eSMark Fasheh 			ocfs2_unlock_and_free_pages(pages, numpages);
673560b11392SMark Fasheh 		numpages = 0;
673660b11392SMark Fasheh 	}
673760b11392SMark Fasheh 
673860b11392SMark Fasheh 	*num = numpages;
673960b11392SMark Fasheh 
674060b11392SMark Fasheh 	return ret;
674160b11392SMark Fasheh }
674260b11392SMark Fasheh 
67436f70fa51STao Ma static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
67446f70fa51STao Ma 				struct page **pages, int *num)
67456f70fa51STao Ma {
67466f70fa51STao Ma 	struct super_block *sb = inode->i_sb;
67476f70fa51STao Ma 
67486f70fa51STao Ma 	BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
67496f70fa51STao Ma 	       (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
67506f70fa51STao Ma 
67516f70fa51STao Ma 	return ocfs2_grab_pages(inode, start, end, pages, num);
67526f70fa51STao Ma }
67536f70fa51STao Ma 
675460b11392SMark Fasheh /*
675560b11392SMark Fasheh  * Zero the area past i_size but still within an allocated
675660b11392SMark Fasheh  * cluster. This avoids exposing nonzero data on subsequent file
675760b11392SMark Fasheh  * extends.
675860b11392SMark Fasheh  *
675960b11392SMark Fasheh  * We need to call this before i_size is updated on the inode because
676060b11392SMark Fasheh  * otherwise block_write_full_page() will skip writeout of pages past
676160b11392SMark Fasheh  * i_size. The new_i_size parameter is passed for this reason.
676260b11392SMark Fasheh  */
676335edec1dSMark Fasheh int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
676435edec1dSMark Fasheh 				  u64 range_start, u64 range_end)
676560b11392SMark Fasheh {
67661d410a6eSMark Fasheh 	int ret = 0, numpages;
676760b11392SMark Fasheh 	struct page **pages = NULL;
676860b11392SMark Fasheh 	u64 phys;
67691d410a6eSMark Fasheh 	unsigned int ext_flags;
67701d410a6eSMark Fasheh 	struct super_block *sb = inode->i_sb;
677160b11392SMark Fasheh 
677260b11392SMark Fasheh 	/*
677360b11392SMark Fasheh 	 * File systems which don't support sparse files zero on every
677460b11392SMark Fasheh 	 * extend.
677560b11392SMark Fasheh 	 */
67761d410a6eSMark Fasheh 	if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
677760b11392SMark Fasheh 		return 0;
677860b11392SMark Fasheh 
67791d410a6eSMark Fasheh 	pages = kcalloc(ocfs2_pages_per_cluster(sb),
678060b11392SMark Fasheh 			sizeof(struct page *), GFP_NOFS);
678160b11392SMark Fasheh 	if (pages == NULL) {
678260b11392SMark Fasheh 		ret = -ENOMEM;
678360b11392SMark Fasheh 		mlog_errno(ret);
678460b11392SMark Fasheh 		goto out;
678560b11392SMark Fasheh 	}
678660b11392SMark Fasheh 
67871d410a6eSMark Fasheh 	if (range_start == range_end)
67881d410a6eSMark Fasheh 		goto out;
67891d410a6eSMark Fasheh 
67901d410a6eSMark Fasheh 	ret = ocfs2_extent_map_get_blocks(inode,
67911d410a6eSMark Fasheh 					  range_start >> sb->s_blocksize_bits,
67921d410a6eSMark Fasheh 					  &phys, NULL, &ext_flags);
679360b11392SMark Fasheh 	if (ret) {
679460b11392SMark Fasheh 		mlog_errno(ret);
679560b11392SMark Fasheh 		goto out;
679660b11392SMark Fasheh 	}
679760b11392SMark Fasheh 
67981d410a6eSMark Fasheh 	/*
67991d410a6eSMark Fasheh 	 * Tail is a hole, or is marked unwritten. In either case, we
68001d410a6eSMark Fasheh 	 * can count on read and write to return/push zero's.
68011d410a6eSMark Fasheh 	 */
68021d410a6eSMark Fasheh 	if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
680360b11392SMark Fasheh 		goto out;
680460b11392SMark Fasheh 
68051d410a6eSMark Fasheh 	ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
68061d410a6eSMark Fasheh 				   &numpages);
68071d410a6eSMark Fasheh 	if (ret) {
68081d410a6eSMark Fasheh 		mlog_errno(ret);
68091d410a6eSMark Fasheh 		goto out;
68101d410a6eSMark Fasheh 	}
68111d410a6eSMark Fasheh 
681235edec1dSMark Fasheh 	ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
681335edec1dSMark Fasheh 				 numpages, phys, handle);
681460b11392SMark Fasheh 
681560b11392SMark Fasheh 	/*
681660b11392SMark Fasheh 	 * Initiate writeout of the pages we zero'd here. We don't
681760b11392SMark Fasheh 	 * wait on them - the truncate_inode_pages() call later will
681860b11392SMark Fasheh 	 * do that for us.
681960b11392SMark Fasheh 	 */
68202cfd30adSChristoph Hellwig 	ret = filemap_fdatawrite_range(inode->i_mapping, range_start,
68212cfd30adSChristoph Hellwig 				       range_end - 1);
682260b11392SMark Fasheh 	if (ret)
682360b11392SMark Fasheh 		mlog_errno(ret);
682460b11392SMark Fasheh 
682560b11392SMark Fasheh out:
682660b11392SMark Fasheh 	kfree(pages);
682760b11392SMark Fasheh 
682860b11392SMark Fasheh 	return ret;
682960b11392SMark Fasheh }
683060b11392SMark Fasheh 
6831fdd77704STiger Yang static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
6832fdd77704STiger Yang 					     struct ocfs2_dinode *di)
68331afc32b9SMark Fasheh {
68341afc32b9SMark Fasheh 	unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
6835fdd77704STiger Yang 	unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
68361afc32b9SMark Fasheh 
6837fdd77704STiger Yang 	if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
6838fdd77704STiger Yang 		memset(&di->id2, 0, blocksize -
6839fdd77704STiger Yang 				    offsetof(struct ocfs2_dinode, id2) -
6840fdd77704STiger Yang 				    xattrsize);
6841fdd77704STiger Yang 	else
6842fdd77704STiger Yang 		memset(&di->id2, 0, blocksize -
6843fdd77704STiger Yang 				    offsetof(struct ocfs2_dinode, id2));
68441afc32b9SMark Fasheh }
68451afc32b9SMark Fasheh 
68465b6a3a2bSMark Fasheh void ocfs2_dinode_new_extent_list(struct inode *inode,
68475b6a3a2bSMark Fasheh 				  struct ocfs2_dinode *di)
68485b6a3a2bSMark Fasheh {
6849fdd77704STiger Yang 	ocfs2_zero_dinode_id2_with_xattr(inode, di);
68505b6a3a2bSMark Fasheh 	di->id2.i_list.l_tree_depth = 0;
68515b6a3a2bSMark Fasheh 	di->id2.i_list.l_next_free_rec = 0;
6852fdd77704STiger Yang 	di->id2.i_list.l_count = cpu_to_le16(
6853fdd77704STiger Yang 		ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
68545b6a3a2bSMark Fasheh }
68555b6a3a2bSMark Fasheh 
68561afc32b9SMark Fasheh void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
68571afc32b9SMark Fasheh {
68581afc32b9SMark Fasheh 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
68591afc32b9SMark Fasheh 	struct ocfs2_inline_data *idata = &di->id2.i_data;
68601afc32b9SMark Fasheh 
68611afc32b9SMark Fasheh 	spin_lock(&oi->ip_lock);
68621afc32b9SMark Fasheh 	oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
68631afc32b9SMark Fasheh 	di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
68641afc32b9SMark Fasheh 	spin_unlock(&oi->ip_lock);
68651afc32b9SMark Fasheh 
68661afc32b9SMark Fasheh 	/*
68671afc32b9SMark Fasheh 	 * We clear the entire i_data structure here so that all
68681afc32b9SMark Fasheh 	 * fields can be properly initialized.
68691afc32b9SMark Fasheh 	 */
6870fdd77704STiger Yang 	ocfs2_zero_dinode_id2_with_xattr(inode, di);
68711afc32b9SMark Fasheh 
6872fdd77704STiger Yang 	idata->id_count = cpu_to_le16(
6873fdd77704STiger Yang 			ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
68741afc32b9SMark Fasheh }
68751afc32b9SMark Fasheh 
68761afc32b9SMark Fasheh int ocfs2_convert_inline_data_to_extents(struct inode *inode,
68771afc32b9SMark Fasheh 					 struct buffer_head *di_bh)
68781afc32b9SMark Fasheh {
68791afc32b9SMark Fasheh 	int ret, i, has_data, num_pages = 0;
6880fb951eb5SZongxun Wang 	int need_free = 0;
6881fb951eb5SZongxun Wang 	u32 bit_off, num;
68821afc32b9SMark Fasheh 	handle_t *handle;
68831afc32b9SMark Fasheh 	u64 uninitialized_var(block);
68841afc32b9SMark Fasheh 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
68851afc32b9SMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
68861afc32b9SMark Fasheh 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
68871afc32b9SMark Fasheh 	struct ocfs2_alloc_context *data_ac = NULL;
68881afc32b9SMark Fasheh 	struct page **pages = NULL;
68891afc32b9SMark Fasheh 	loff_t end = osb->s_clustersize;
6890f99b9b7cSJoel Becker 	struct ocfs2_extent_tree et;
6891a90714c1SJan Kara 	int did_quota = 0;
68921afc32b9SMark Fasheh 
68931afc32b9SMark Fasheh 	has_data = i_size_read(inode) ? 1 : 0;
68941afc32b9SMark Fasheh 
68951afc32b9SMark Fasheh 	if (has_data) {
68961afc32b9SMark Fasheh 		pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
68971afc32b9SMark Fasheh 				sizeof(struct page *), GFP_NOFS);
68981afc32b9SMark Fasheh 		if (pages == NULL) {
68991afc32b9SMark Fasheh 			ret = -ENOMEM;
69001afc32b9SMark Fasheh 			mlog_errno(ret);
69013cc79b79SMarkus Elfring 			return ret;
69021afc32b9SMark Fasheh 		}
69031afc32b9SMark Fasheh 
69041afc32b9SMark Fasheh 		ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
69051afc32b9SMark Fasheh 		if (ret) {
69061afc32b9SMark Fasheh 			mlog_errno(ret);
69073cc79b79SMarkus Elfring 			goto free_pages;
69081afc32b9SMark Fasheh 		}
69091afc32b9SMark Fasheh 	}
69101afc32b9SMark Fasheh 
6911a90714c1SJan Kara 	handle = ocfs2_start_trans(osb,
6912a90714c1SJan Kara 				   ocfs2_inline_to_extents_credits(osb->sb));
69131afc32b9SMark Fasheh 	if (IS_ERR(handle)) {
69141afc32b9SMark Fasheh 		ret = PTR_ERR(handle);
69151afc32b9SMark Fasheh 		mlog_errno(ret);
691615eba0feSalex chen 		goto out;
69171afc32b9SMark Fasheh 	}
69181afc32b9SMark Fasheh 
69190cf2f763SJoel Becker 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
69201afc32b9SMark Fasheh 				      OCFS2_JOURNAL_ACCESS_WRITE);
69211afc32b9SMark Fasheh 	if (ret) {
69221afc32b9SMark Fasheh 		mlog_errno(ret);
69231afc32b9SMark Fasheh 		goto out_commit;
69241afc32b9SMark Fasheh 	}
69251afc32b9SMark Fasheh 
69261afc32b9SMark Fasheh 	if (has_data) {
69271afc32b9SMark Fasheh 		unsigned int page_end;
69281afc32b9SMark Fasheh 		u64 phys;
69291afc32b9SMark Fasheh 
69305dd4056dSChristoph Hellwig 		ret = dquot_alloc_space_nodirty(inode,
69315dd4056dSChristoph Hellwig 				       ocfs2_clusters_to_bytes(osb->sb, 1));
69325dd4056dSChristoph Hellwig 		if (ret)
6933a90714c1SJan Kara 			goto out_commit;
6934a90714c1SJan Kara 		did_quota = 1;
6935a90714c1SJan Kara 
69364fe370afSMark Fasheh 		data_ac->ac_resv = &OCFS2_I(inode)->ip_la_data_resv;
69374fe370afSMark Fasheh 
69381ed9b777SJoel Becker 		ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off,
69391afc32b9SMark Fasheh 					   &num);
69401afc32b9SMark Fasheh 		if (ret) {
69411afc32b9SMark Fasheh 			mlog_errno(ret);
69421afc32b9SMark Fasheh 			goto out_commit;
69431afc32b9SMark Fasheh 		}
69441afc32b9SMark Fasheh 
69451afc32b9SMark Fasheh 		/*
69461afc32b9SMark Fasheh 		 * Save two copies, one for insert, and one that can
69471afc32b9SMark Fasheh 		 * be changed by ocfs2_map_and_dirty_page() below.
69481afc32b9SMark Fasheh 		 */
69491afc32b9SMark Fasheh 		block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
69501afc32b9SMark Fasheh 
69511afc32b9SMark Fasheh 		/*
69521afc32b9SMark Fasheh 		 * Non sparse file systems zero on extend, so no need
69531afc32b9SMark Fasheh 		 * to do that now.
69541afc32b9SMark Fasheh 		 */
69551afc32b9SMark Fasheh 		if (!ocfs2_sparse_alloc(osb) &&
695609cbfeafSKirill A. Shutemov 		    PAGE_SIZE < osb->s_clustersize)
695709cbfeafSKirill A. Shutemov 			end = PAGE_SIZE;
69581afc32b9SMark Fasheh 
69591afc32b9SMark Fasheh 		ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
69601afc32b9SMark Fasheh 		if (ret) {
69611afc32b9SMark Fasheh 			mlog_errno(ret);
6962fb951eb5SZongxun Wang 			need_free = 1;
69631afc32b9SMark Fasheh 			goto out_commit;
69641afc32b9SMark Fasheh 		}
69651afc32b9SMark Fasheh 
69661afc32b9SMark Fasheh 		/*
69671afc32b9SMark Fasheh 		 * This should populate the 1st page for us and mark
69681afc32b9SMark Fasheh 		 * it up to date.
69691afc32b9SMark Fasheh 		 */
69701afc32b9SMark Fasheh 		ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
69711afc32b9SMark Fasheh 		if (ret) {
69721afc32b9SMark Fasheh 			mlog_errno(ret);
6973fb951eb5SZongxun Wang 			need_free = 1;
697415eba0feSalex chen 			goto out_unlock;
69751afc32b9SMark Fasheh 		}
69761afc32b9SMark Fasheh 
697709cbfeafSKirill A. Shutemov 		page_end = PAGE_SIZE;
697809cbfeafSKirill A. Shutemov 		if (PAGE_SIZE > osb->s_clustersize)
69791afc32b9SMark Fasheh 			page_end = osb->s_clustersize;
69801afc32b9SMark Fasheh 
69811afc32b9SMark Fasheh 		for (i = 0; i < num_pages; i++)
69821afc32b9SMark Fasheh 			ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
69831afc32b9SMark Fasheh 						 pages[i], i > 0, &phys);
69841afc32b9SMark Fasheh 	}
69851afc32b9SMark Fasheh 
69861afc32b9SMark Fasheh 	spin_lock(&oi->ip_lock);
69871afc32b9SMark Fasheh 	oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
69881afc32b9SMark Fasheh 	di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
69891afc32b9SMark Fasheh 	spin_unlock(&oi->ip_lock);
69901afc32b9SMark Fasheh 
69912931cdcbSDarrick J. Wong 	ocfs2_update_inode_fsync_trans(handle, inode, 1);
69925b6a3a2bSMark Fasheh 	ocfs2_dinode_new_extent_list(inode, di);
69931afc32b9SMark Fasheh 
69941afc32b9SMark Fasheh 	ocfs2_journal_dirty(handle, di_bh);
69951afc32b9SMark Fasheh 
69961afc32b9SMark Fasheh 	if (has_data) {
69971afc32b9SMark Fasheh 		/*
69981afc32b9SMark Fasheh 		 * An error at this point should be extremely rare. If
69991afc32b9SMark Fasheh 		 * this proves to be false, we could always re-build
70001afc32b9SMark Fasheh 		 * the in-inode data from our pages.
70011afc32b9SMark Fasheh 		 */
70025e404e9eSJoel Becker 		ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
7003cc79d8c1SJoel Becker 		ret = ocfs2_insert_extent(handle, &et, 0, block, 1, 0, NULL);
70041afc32b9SMark Fasheh 		if (ret) {
70051afc32b9SMark Fasheh 			mlog_errno(ret);
7006fb951eb5SZongxun Wang 			need_free = 1;
700715eba0feSalex chen 			goto out_unlock;
70081afc32b9SMark Fasheh 		}
70091afc32b9SMark Fasheh 
70101afc32b9SMark Fasheh 		inode->i_blocks = ocfs2_inode_sector_count(inode);
70111afc32b9SMark Fasheh 	}
70121afc32b9SMark Fasheh 
701315eba0feSalex chen out_unlock:
701415eba0feSalex chen 	if (pages)
701515eba0feSalex chen 		ocfs2_unlock_and_free_pages(pages, num_pages);
701615eba0feSalex chen 
70171afc32b9SMark Fasheh out_commit:
7018a90714c1SJan Kara 	if (ret < 0 && did_quota)
70195dd4056dSChristoph Hellwig 		dquot_free_space_nodirty(inode,
7020a90714c1SJan Kara 					  ocfs2_clusters_to_bytes(osb->sb, 1));
7021a90714c1SJan Kara 
7022fb951eb5SZongxun Wang 	if (need_free) {
7023fb951eb5SZongxun Wang 		if (data_ac->ac_which == OCFS2_AC_USE_LOCAL)
7024fb951eb5SZongxun Wang 			ocfs2_free_local_alloc_bits(osb, handle, data_ac,
7025fb951eb5SZongxun Wang 					bit_off, num);
7026fb951eb5SZongxun Wang 		else
7027fb951eb5SZongxun Wang 			ocfs2_free_clusters(handle,
7028fb951eb5SZongxun Wang 					data_ac->ac_inode,
7029fb951eb5SZongxun Wang 					data_ac->ac_bh,
7030fb951eb5SZongxun Wang 					ocfs2_clusters_to_blocks(osb->sb, bit_off),
7031fb951eb5SZongxun Wang 					num);
7032fb951eb5SZongxun Wang 	}
7033fb951eb5SZongxun Wang 
70341afc32b9SMark Fasheh 	ocfs2_commit_trans(osb, handle);
70351afc32b9SMark Fasheh 
703615eba0feSalex chen out:
70371afc32b9SMark Fasheh 	if (data_ac)
70381afc32b9SMark Fasheh 		ocfs2_free_alloc_context(data_ac);
70393cc79b79SMarkus Elfring free_pages:
70401afc32b9SMark Fasheh 	kfree(pages);
70411afc32b9SMark Fasheh 	return ret;
70421afc32b9SMark Fasheh }
70431afc32b9SMark Fasheh 
7044ccd979bdSMark Fasheh /*
7045ccd979bdSMark Fasheh  * It is expected, that by the time you call this function,
7046ccd979bdSMark Fasheh  * inode->i_size and fe->i_size have been adjusted.
7047ccd979bdSMark Fasheh  *
7048ccd979bdSMark Fasheh  * WARNING: This will kfree the truncate context
7049ccd979bdSMark Fasheh  */
7050ccd979bdSMark Fasheh int ocfs2_commit_truncate(struct ocfs2_super *osb,
7051ccd979bdSMark Fasheh 			  struct inode *inode,
705278f94673STristan Ye 			  struct buffer_head *di_bh)
7053ccd979bdSMark Fasheh {
705478f94673STristan Ye 	int status = 0, i, flags = 0;
705578f94673STristan Ye 	u32 new_highest_cpos, range, trunc_cpos, trunc_len, phys_cpos, coff;
7056bcbbb24aSTao Ma 	u64 blkno = 0;
7057ccd979bdSMark Fasheh 	struct ocfs2_extent_list *el;
705878f94673STristan Ye 	struct ocfs2_extent_rec *rec;
7059dcd0538fSMark Fasheh 	struct ocfs2_path *path = NULL;
706078f94673STristan Ye 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
706178f94673STristan Ye 	struct ocfs2_extent_list *root_el = &(di->id2.i_list);
706278f94673STristan Ye 	u64 refcount_loc = le64_to_cpu(di->i_refcount_loc);
706378f94673STristan Ye 	struct ocfs2_extent_tree et;
706478f94673STristan Ye 	struct ocfs2_cached_dealloc_ctxt dealloc;
7065f62f12b3SJunxiao Bi 	struct ocfs2_refcount_tree *ref_tree = NULL;
7066ccd979bdSMark Fasheh 
706778f94673STristan Ye 	ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
706878f94673STristan Ye 	ocfs2_init_dealloc_ctxt(&dealloc);
706978f94673STristan Ye 
7070dcd0538fSMark Fasheh 	new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
7071ccd979bdSMark Fasheh 						     i_size_read(inode));
7072ccd979bdSMark Fasheh 
707378f94673STristan Ye 	path = ocfs2_new_path(di_bh, &di->id2.i_list,
707413723d00SJoel Becker 			      ocfs2_journal_access_di);
7075dcd0538fSMark Fasheh 	if (!path) {
7076dcd0538fSMark Fasheh 		status = -ENOMEM;
7077ccd979bdSMark Fasheh 		mlog_errno(status);
7078ccd979bdSMark Fasheh 		goto bail;
7079ccd979bdSMark Fasheh 	}
708083418978SMark Fasheh 
708183418978SMark Fasheh 	ocfs2_extent_map_trunc(inode, new_highest_cpos);
708283418978SMark Fasheh 
7083dcd0538fSMark Fasheh start:
7084dcd0538fSMark Fasheh 	/*
70853a0782d0SMark Fasheh 	 * Check that we still have allocation to delete.
70863a0782d0SMark Fasheh 	 */
70873a0782d0SMark Fasheh 	if (OCFS2_I(inode)->ip_clusters == 0) {
70883a0782d0SMark Fasheh 		status = 0;
70893a0782d0SMark Fasheh 		goto bail;
70903a0782d0SMark Fasheh 	}
70913a0782d0SMark Fasheh 
70923a0782d0SMark Fasheh 	/*
7093dcd0538fSMark Fasheh 	 * Truncate always works against the rightmost tree branch.
7094dcd0538fSMark Fasheh 	 */
7095facdb77fSJoel Becker 	status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX);
7096dcd0538fSMark Fasheh 	if (status) {
7097dcd0538fSMark Fasheh 		mlog_errno(status);
7098ccd979bdSMark Fasheh 		goto bail;
7099ccd979bdSMark Fasheh 	}
7100ccd979bdSMark Fasheh 
7101a09d09b8STao Ma 	trace_ocfs2_commit_truncate(
7102a09d09b8STao Ma 		(unsigned long long)OCFS2_I(inode)->ip_blkno,
7103a09d09b8STao Ma 		new_highest_cpos,
7104a09d09b8STao Ma 		OCFS2_I(inode)->ip_clusters,
7105a09d09b8STao Ma 		path->p_tree_depth);
7106dcd0538fSMark Fasheh 
7107dcd0538fSMark Fasheh 	/*
7108dcd0538fSMark Fasheh 	 * By now, el will point to the extent list on the bottom most
7109dcd0538fSMark Fasheh 	 * portion of this tree. Only the tail record is considered in
7110dcd0538fSMark Fasheh 	 * each pass.
7111dcd0538fSMark Fasheh 	 *
7112dcd0538fSMark Fasheh 	 * We handle the following cases, in order:
7113dcd0538fSMark Fasheh 	 * - empty extent: delete the remaining branch
7114dcd0538fSMark Fasheh 	 * - remove the entire record
7115dcd0538fSMark Fasheh 	 * - remove a partial record
7116dcd0538fSMark Fasheh 	 * - no record needs to be removed (truncate has completed)
7117dcd0538fSMark Fasheh 	 */
7118dcd0538fSMark Fasheh 	el = path_leaf_el(path);
71193a0782d0SMark Fasheh 	if (le16_to_cpu(el->l_next_free_rec) == 0) {
71203a0782d0SMark Fasheh 		ocfs2_error(inode->i_sb,
71213a0782d0SMark Fasheh 			    "Inode %llu has empty extent block at %llu\n",
71223a0782d0SMark Fasheh 			    (unsigned long long)OCFS2_I(inode)->ip_blkno,
71233a0782d0SMark Fasheh 			    (unsigned long long)path_leaf_bh(path)->b_blocknr);
71243a0782d0SMark Fasheh 		status = -EROFS;
71253a0782d0SMark Fasheh 		goto bail;
71263a0782d0SMark Fasheh 	}
71273a0782d0SMark Fasheh 
7128ccd979bdSMark Fasheh 	i = le16_to_cpu(el->l_next_free_rec) - 1;
712978f94673STristan Ye 	rec = &el->l_recs[i];
713078f94673STristan Ye 	flags = rec->e_flags;
713178f94673STristan Ye 	range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
713278f94673STristan Ye 
713378f94673STristan Ye 	if (i == 0 && ocfs2_is_empty_extent(rec)) {
713478f94673STristan Ye 		/*
713578f94673STristan Ye 		 * Lower levels depend on this never happening, but it's best
713678f94673STristan Ye 		 * to check it up here before changing the tree.
713778f94673STristan Ye 		*/
713878f94673STristan Ye 		if (root_el->l_tree_depth && rec->e_int_clusters == 0) {
7139d0c97d52SXue jiufei 			mlog(ML_ERROR, "Inode %lu has an empty "
714078f94673STristan Ye 				    "extent record, depth %u\n", inode->i_ino,
714178f94673STristan Ye 				    le16_to_cpu(root_el->l_tree_depth));
7142d0c97d52SXue jiufei 			status = ocfs2_remove_rightmost_empty_extent(osb,
7143d0c97d52SXue jiufei 					&et, path, &dealloc);
7144d0c97d52SXue jiufei 			if (status) {
7145d0c97d52SXue jiufei 				mlog_errno(status);
714678f94673STristan Ye 				goto bail;
714778f94673STristan Ye 			}
7148d0c97d52SXue jiufei 
7149d0c97d52SXue jiufei 			ocfs2_reinit_path(path, 1);
7150d0c97d52SXue jiufei 			goto start;
7151d0c97d52SXue jiufei 		} else {
715278f94673STristan Ye 			trunc_cpos = le32_to_cpu(rec->e_cpos);
715378f94673STristan Ye 			trunc_len = 0;
715478f94673STristan Ye 			blkno = 0;
7155d0c97d52SXue jiufei 		}
715678f94673STristan Ye 	} else if (le32_to_cpu(rec->e_cpos) >= new_highest_cpos) {
715778f94673STristan Ye 		/*
715878f94673STristan Ye 		 * Truncate entire record.
715978f94673STristan Ye 		 */
716078f94673STristan Ye 		trunc_cpos = le32_to_cpu(rec->e_cpos);
716178f94673STristan Ye 		trunc_len = ocfs2_rec_clusters(el, rec);
716278f94673STristan Ye 		blkno = le64_to_cpu(rec->e_blkno);
7163dcd0538fSMark Fasheh 	} else if (range > new_highest_cpos) {
716478f94673STristan Ye 		/*
716578f94673STristan Ye 		 * Partial truncate. it also should be
716678f94673STristan Ye 		 * the last truncate we're doing.
716778f94673STristan Ye 		 */
716878f94673STristan Ye 		trunc_cpos = new_highest_cpos;
716978f94673STristan Ye 		trunc_len = range - new_highest_cpos;
717078f94673STristan Ye 		coff = new_highest_cpos - le32_to_cpu(rec->e_cpos);
717178f94673STristan Ye 		blkno = le64_to_cpu(rec->e_blkno) +
717278f94673STristan Ye 				ocfs2_clusters_to_blocks(inode->i_sb, coff);
7173dcd0538fSMark Fasheh 	} else {
717478f94673STristan Ye 		/*
717578f94673STristan Ye 		 * Truncate completed, leave happily.
717678f94673STristan Ye 		 */
7177dcd0538fSMark Fasheh 		status = 0;
7178dcd0538fSMark Fasheh 		goto bail;
7179dcd0538fSMark Fasheh 	}
7180ccd979bdSMark Fasheh 
718178f94673STristan Ye 	phys_cpos = ocfs2_blocks_to_clusters(inode->i_sb, blkno);
7182dcd0538fSMark Fasheh 
7183f62f12b3SJunxiao Bi 	if ((flags & OCFS2_EXT_REFCOUNTED) && trunc_len && !ref_tree) {
7184f62f12b3SJunxiao Bi 		status = ocfs2_lock_refcount_tree(osb, refcount_loc, 1,
7185f62f12b3SJunxiao Bi 				&ref_tree, NULL);
7186f62f12b3SJunxiao Bi 		if (status) {
7187f62f12b3SJunxiao Bi 			mlog_errno(status);
7188f62f12b3SJunxiao Bi 			goto bail;
7189f62f12b3SJunxiao Bi 		}
7190f62f12b3SJunxiao Bi 	}
7191f62f12b3SJunxiao Bi 
719278f94673STristan Ye 	status = ocfs2_remove_btree_range(inode, &et, trunc_cpos,
719378f94673STristan Ye 					  phys_cpos, trunc_len, flags, &dealloc,
7194f62f12b3SJunxiao Bi 					  refcount_loc, true);
7195bcbbb24aSTao Ma 	if (status < 0) {
7196bcbbb24aSTao Ma 		mlog_errno(status);
7197bcbbb24aSTao Ma 		goto bail;
7198bcbbb24aSTao Ma 	}
7199ccd979bdSMark Fasheh 
7200dcd0538fSMark Fasheh 	ocfs2_reinit_path(path, 1);
7201dcd0538fSMark Fasheh 
7202dcd0538fSMark Fasheh 	/*
72033a0782d0SMark Fasheh 	 * The check above will catch the case where we've truncated
72043a0782d0SMark Fasheh 	 * away all allocation.
7205dcd0538fSMark Fasheh 	 */
7206ccd979bdSMark Fasheh 	goto start;
72073a0782d0SMark Fasheh 
7208ccd979bdSMark Fasheh bail:
7209f62f12b3SJunxiao Bi 	if (ref_tree)
7210f62f12b3SJunxiao Bi 		ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
7211ccd979bdSMark Fasheh 
7212ccd979bdSMark Fasheh 	ocfs2_schedule_truncate_log_flush(osb, 1);
7213ccd979bdSMark Fasheh 
721478f94673STristan Ye 	ocfs2_run_deallocs(osb, &dealloc);
721559a5e416SMark Fasheh 
7216dcd0538fSMark Fasheh 	ocfs2_free_path(path);
7217ccd979bdSMark Fasheh 
7218ccd979bdSMark Fasheh 	return status;
7219ccd979bdSMark Fasheh }
7220ccd979bdSMark Fasheh 
7221ccd979bdSMark Fasheh /*
72221afc32b9SMark Fasheh  * 'start' is inclusive, 'end' is not.
72231afc32b9SMark Fasheh  */
72241afc32b9SMark Fasheh int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
72251afc32b9SMark Fasheh 			  unsigned int start, unsigned int end, int trunc)
72261afc32b9SMark Fasheh {
72271afc32b9SMark Fasheh 	int ret;
72281afc32b9SMark Fasheh 	unsigned int numbytes;
72291afc32b9SMark Fasheh 	handle_t *handle;
72301afc32b9SMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
72311afc32b9SMark Fasheh 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
72321afc32b9SMark Fasheh 	struct ocfs2_inline_data *idata = &di->id2.i_data;
72331afc32b9SMark Fasheh 
72341afc32b9SMark Fasheh 	if (end > i_size_read(inode))
72351afc32b9SMark Fasheh 		end = i_size_read(inode);
72361afc32b9SMark Fasheh 
7237d62e74beSYounger Liu 	BUG_ON(start > end);
72381afc32b9SMark Fasheh 
72391afc32b9SMark Fasheh 	if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
72401afc32b9SMark Fasheh 	    !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
72411afc32b9SMark Fasheh 	    !ocfs2_supports_inline_data(osb)) {
72421afc32b9SMark Fasheh 		ocfs2_error(inode->i_sb,
72437ecef14aSJoe Perches 			    "Inline data flags for inode %llu don't agree! Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
72441afc32b9SMark Fasheh 			    (unsigned long long)OCFS2_I(inode)->ip_blkno,
72451afc32b9SMark Fasheh 			    le16_to_cpu(di->i_dyn_features),
72461afc32b9SMark Fasheh 			    OCFS2_I(inode)->ip_dyn_features,
72471afc32b9SMark Fasheh 			    osb->s_feature_incompat);
72481afc32b9SMark Fasheh 		ret = -EROFS;
72491afc32b9SMark Fasheh 		goto out;
72501afc32b9SMark Fasheh 	}
72511afc32b9SMark Fasheh 
72521afc32b9SMark Fasheh 	handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
72531afc32b9SMark Fasheh 	if (IS_ERR(handle)) {
72541afc32b9SMark Fasheh 		ret = PTR_ERR(handle);
72551afc32b9SMark Fasheh 		mlog_errno(ret);
72561afc32b9SMark Fasheh 		goto out;
72571afc32b9SMark Fasheh 	}
72581afc32b9SMark Fasheh 
72590cf2f763SJoel Becker 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
72601afc32b9SMark Fasheh 				      OCFS2_JOURNAL_ACCESS_WRITE);
72611afc32b9SMark Fasheh 	if (ret) {
72621afc32b9SMark Fasheh 		mlog_errno(ret);
72631afc32b9SMark Fasheh 		goto out_commit;
72641afc32b9SMark Fasheh 	}
72651afc32b9SMark Fasheh 
72661afc32b9SMark Fasheh 	numbytes = end - start;
72671afc32b9SMark Fasheh 	memset(idata->id_data + start, 0, numbytes);
72681afc32b9SMark Fasheh 
72691afc32b9SMark Fasheh 	/*
72701afc32b9SMark Fasheh 	 * No need to worry about the data page here - it's been
72711afc32b9SMark Fasheh 	 * truncated already and inline data doesn't need it for
72721afc32b9SMark Fasheh 	 * pushing zero's to disk, so we'll let readpage pick it up
72731afc32b9SMark Fasheh 	 * later.
72741afc32b9SMark Fasheh 	 */
72751afc32b9SMark Fasheh 	if (trunc) {
72761afc32b9SMark Fasheh 		i_size_write(inode, start);
72771afc32b9SMark Fasheh 		di->i_size = cpu_to_le64(start);
72781afc32b9SMark Fasheh 	}
72791afc32b9SMark Fasheh 
72801afc32b9SMark Fasheh 	inode->i_blocks = ocfs2_inode_sector_count(inode);
7281078cd827SDeepa Dinamani 	inode->i_ctime = inode->i_mtime = current_time(inode);
72821afc32b9SMark Fasheh 
72831afc32b9SMark Fasheh 	di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
72841afc32b9SMark Fasheh 	di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
72851afc32b9SMark Fasheh 
72866fdb702dSDarrick J. Wong 	ocfs2_update_inode_fsync_trans(handle, inode, 1);
72871afc32b9SMark Fasheh 	ocfs2_journal_dirty(handle, di_bh);
72881afc32b9SMark Fasheh 
72891afc32b9SMark Fasheh out_commit:
72901afc32b9SMark Fasheh 	ocfs2_commit_trans(osb, handle);
72911afc32b9SMark Fasheh 
72921afc32b9SMark Fasheh out:
72931afc32b9SMark Fasheh 	return ret;
72941afc32b9SMark Fasheh }
7295e80de36dSTao Ma 
7296e80de36dSTao Ma static int ocfs2_trim_extent(struct super_block *sb,
7297e80de36dSTao Ma 			     struct ocfs2_group_desc *gd,
7298105ddc93SAshish Samant 			     u64 group, u32 start, u32 count)
7299e80de36dSTao Ma {
7300e80de36dSTao Ma 	u64 discard, bcount;
7301105ddc93SAshish Samant 	struct ocfs2_super *osb = OCFS2_SB(sb);
7302e80de36dSTao Ma 
7303e80de36dSTao Ma 	bcount = ocfs2_clusters_to_blocks(sb, count);
7304105ddc93SAshish Samant 	discard = ocfs2_clusters_to_blocks(sb, start);
7305105ddc93SAshish Samant 
7306105ddc93SAshish Samant 	/*
7307105ddc93SAshish Samant 	 * For the first cluster group, the gd->bg_blkno is not at the start
7308105ddc93SAshish Samant 	 * of the group, but at an offset from the start. If we add it while
7309105ddc93SAshish Samant 	 * calculating discard for first group, we will wrongly start fstrim a
7310105ddc93SAshish Samant 	 * few blocks after the desried start block and the range can cross
7311105ddc93SAshish Samant 	 * over into the next cluster group. So, add it only if this is not
7312105ddc93SAshish Samant 	 * the first cluster group.
7313105ddc93SAshish Samant 	 */
7314105ddc93SAshish Samant 	if (group != osb->first_cluster_group_blkno)
7315105ddc93SAshish Samant 		discard += le64_to_cpu(gd->bg_blkno);
7316e80de36dSTao Ma 
731710fca35fSTao Ma 	trace_ocfs2_trim_extent(sb, (unsigned long long)discard, bcount);
731810fca35fSTao Ma 
7319e80de36dSTao Ma 	return sb_issue_discard(sb, discard, bcount, GFP_NOFS, 0);
7320e80de36dSTao Ma }
7321e80de36dSTao Ma 
7322e80de36dSTao Ma static int ocfs2_trim_group(struct super_block *sb,
7323105ddc93SAshish Samant 			    struct ocfs2_group_desc *gd, u64 group,
7324e80de36dSTao Ma 			    u32 start, u32 max, u32 minbits)
7325e80de36dSTao Ma {
7326e80de36dSTao Ma 	int ret = 0, count = 0, next;
7327e80de36dSTao Ma 	void *bitmap = gd->bg_bitmap;
7328e80de36dSTao Ma 
7329e80de36dSTao Ma 	if (le16_to_cpu(gd->bg_free_bits_count) < minbits)
7330e80de36dSTao Ma 		return 0;
7331e80de36dSTao Ma 
733210fca35fSTao Ma 	trace_ocfs2_trim_group((unsigned long long)le64_to_cpu(gd->bg_blkno),
733310fca35fSTao Ma 			       start, max, minbits);
733410fca35fSTao Ma 
7335e80de36dSTao Ma 	while (start < max) {
7336e80de36dSTao Ma 		start = ocfs2_find_next_zero_bit(bitmap, max, start);
7337e80de36dSTao Ma 		if (start >= max)
7338e80de36dSTao Ma 			break;
7339e80de36dSTao Ma 		next = ocfs2_find_next_bit(bitmap, max, start);
7340e80de36dSTao Ma 
7341e80de36dSTao Ma 		if ((next - start) >= minbits) {
7342105ddc93SAshish Samant 			ret = ocfs2_trim_extent(sb, gd, group,
7343e80de36dSTao Ma 						start, next - start);
7344e80de36dSTao Ma 			if (ret < 0) {
7345e80de36dSTao Ma 				mlog_errno(ret);
7346e80de36dSTao Ma 				break;
7347e80de36dSTao Ma 			}
7348e80de36dSTao Ma 			count += next - start;
7349e80de36dSTao Ma 		}
7350e80de36dSTao Ma 		start = next + 1;
7351e80de36dSTao Ma 
7352e80de36dSTao Ma 		if (fatal_signal_pending(current)) {
7353e80de36dSTao Ma 			count = -ERESTARTSYS;
7354e80de36dSTao Ma 			break;
7355e80de36dSTao Ma 		}
7356e80de36dSTao Ma 
7357e80de36dSTao Ma 		if ((le16_to_cpu(gd->bg_free_bits_count) - count) < minbits)
7358e80de36dSTao Ma 			break;
7359e80de36dSTao Ma 	}
7360e80de36dSTao Ma 
7361e80de36dSTao Ma 	if (ret < 0)
7362e80de36dSTao Ma 		count = ret;
7363e80de36dSTao Ma 
7364e80de36dSTao Ma 	return count;
7365e80de36dSTao Ma }
7366e80de36dSTao Ma 
7367e80de36dSTao Ma int ocfs2_trim_fs(struct super_block *sb, struct fstrim_range *range)
7368e80de36dSTao Ma {
7369e80de36dSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(sb);
7370e80de36dSTao Ma 	u64 start, len, trimmed, first_group, last_group, group;
7371e80de36dSTao Ma 	int ret, cnt;
7372e80de36dSTao Ma 	u32 first_bit, last_bit, minlen;
7373e80de36dSTao Ma 	struct buffer_head *main_bm_bh = NULL;
7374e80de36dSTao Ma 	struct inode *main_bm_inode = NULL;
7375e80de36dSTao Ma 	struct buffer_head *gd_bh = NULL;
7376e80de36dSTao Ma 	struct ocfs2_dinode *main_bm;
7377e80de36dSTao Ma 	struct ocfs2_group_desc *gd = NULL;
7378e80de36dSTao Ma 
7379e80de36dSTao Ma 	start = range->start >> osb->s_clustersize_bits;
7380e80de36dSTao Ma 	len = range->len >> osb->s_clustersize_bits;
7381e80de36dSTao Ma 	minlen = range->minlen >> osb->s_clustersize_bits;
7382e80de36dSTao Ma 
7383aa89762cSJie Liu 	if (minlen >= osb->bitmap_cpg || range->len < sb->s_blocksize)
7384e80de36dSTao Ma 		return -EINVAL;
7385e80de36dSTao Ma 
7386e80de36dSTao Ma 	main_bm_inode = ocfs2_get_system_file_inode(osb,
7387e80de36dSTao Ma 						    GLOBAL_BITMAP_SYSTEM_INODE,
7388e80de36dSTao Ma 						    OCFS2_INVALID_SLOT);
7389e80de36dSTao Ma 	if (!main_bm_inode) {
7390e80de36dSTao Ma 		ret = -EIO;
7391e80de36dSTao Ma 		mlog_errno(ret);
7392e80de36dSTao Ma 		goto out;
7393e80de36dSTao Ma 	}
7394e80de36dSTao Ma 
73955955102cSAl Viro 	inode_lock(main_bm_inode);
7396e80de36dSTao Ma 
7397e80de36dSTao Ma 	ret = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 0);
7398e80de36dSTao Ma 	if (ret < 0) {
7399e80de36dSTao Ma 		mlog_errno(ret);
7400e80de36dSTao Ma 		goto out_mutex;
7401e80de36dSTao Ma 	}
7402e80de36dSTao Ma 	main_bm = (struct ocfs2_dinode *)main_bm_bh->b_data;
7403e80de36dSTao Ma 
7404e80de36dSTao Ma 	if (start >= le32_to_cpu(main_bm->i_clusters)) {
7405e80de36dSTao Ma 		ret = -EINVAL;
7406e80de36dSTao Ma 		goto out_unlock;
7407e80de36dSTao Ma 	}
7408e80de36dSTao Ma 
7409aa89762cSJie Liu 	len = range->len >> osb->s_clustersize_bits;
7410e80de36dSTao Ma 	if (start + len > le32_to_cpu(main_bm->i_clusters))
7411e80de36dSTao Ma 		len = le32_to_cpu(main_bm->i_clusters) - start;
7412e80de36dSTao Ma 
741310fca35fSTao Ma 	trace_ocfs2_trim_fs(start, len, minlen);
741410fca35fSTao Ma 
7415e80de36dSTao Ma 	/* Determine first and last group to examine based on start and len */
7416e80de36dSTao Ma 	first_group = ocfs2_which_cluster_group(main_bm_inode, start);
7417e80de36dSTao Ma 	if (first_group == osb->first_cluster_group_blkno)
7418e80de36dSTao Ma 		first_bit = start;
7419e80de36dSTao Ma 	else
7420e80de36dSTao Ma 		first_bit = start - ocfs2_blocks_to_clusters(sb, first_group);
7421e80de36dSTao Ma 	last_group = ocfs2_which_cluster_group(main_bm_inode, start + len - 1);
7422e80de36dSTao Ma 	last_bit = osb->bitmap_cpg;
7423e80de36dSTao Ma 
7424aa89762cSJie Liu 	trimmed = 0;
7425e80de36dSTao Ma 	for (group = first_group; group <= last_group;) {
7426e80de36dSTao Ma 		if (first_bit + len >= osb->bitmap_cpg)
7427e80de36dSTao Ma 			last_bit = osb->bitmap_cpg;
7428e80de36dSTao Ma 		else
7429e80de36dSTao Ma 			last_bit = first_bit + len;
7430e80de36dSTao Ma 
7431e80de36dSTao Ma 		ret = ocfs2_read_group_descriptor(main_bm_inode,
7432e80de36dSTao Ma 						  main_bm, group,
7433e80de36dSTao Ma 						  &gd_bh);
7434e80de36dSTao Ma 		if (ret < 0) {
7435e80de36dSTao Ma 			mlog_errno(ret);
7436e80de36dSTao Ma 			break;
7437e80de36dSTao Ma 		}
7438e80de36dSTao Ma 
7439e80de36dSTao Ma 		gd = (struct ocfs2_group_desc *)gd_bh->b_data;
7440105ddc93SAshish Samant 		cnt = ocfs2_trim_group(sb, gd, group,
7441105ddc93SAshish Samant 				       first_bit, last_bit, minlen);
7442e80de36dSTao Ma 		brelse(gd_bh);
7443e80de36dSTao Ma 		gd_bh = NULL;
7444e80de36dSTao Ma 		if (cnt < 0) {
7445e80de36dSTao Ma 			ret = cnt;
7446e80de36dSTao Ma 			mlog_errno(ret);
7447e80de36dSTao Ma 			break;
7448e80de36dSTao Ma 		}
7449e80de36dSTao Ma 
7450e80de36dSTao Ma 		trimmed += cnt;
7451e80de36dSTao Ma 		len -= osb->bitmap_cpg - first_bit;
7452e80de36dSTao Ma 		first_bit = 0;
7453e80de36dSTao Ma 		if (group == osb->first_cluster_group_blkno)
7454e80de36dSTao Ma 			group = ocfs2_clusters_to_blocks(sb, osb->bitmap_cpg);
7455e80de36dSTao Ma 		else
7456e80de36dSTao Ma 			group += ocfs2_clusters_to_blocks(sb, osb->bitmap_cpg);
7457e80de36dSTao Ma 	}
7458e80de36dSTao Ma 	range->len = trimmed * sb->s_blocksize;
7459e80de36dSTao Ma out_unlock:
7460e80de36dSTao Ma 	ocfs2_inode_unlock(main_bm_inode, 0);
7461e80de36dSTao Ma 	brelse(main_bm_bh);
7462e80de36dSTao Ma out_mutex:
74635955102cSAl Viro 	inode_unlock(main_bm_inode);
7464e80de36dSTao Ma 	iput(main_bm_inode);
7465e80de36dSTao Ma out:
7466e80de36dSTao Ma 	return ret;
7467e80de36dSTao Ma }
7468