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