xref: /openbmc/linux/fs/ocfs2/alloc.c (revision 811f933df1e55615fd0bb4818f31e3868a8e6e23)
1ccd979bdSMark Fasheh /* -*- mode: c; c-basic-offset: 8; -*-
2ccd979bdSMark Fasheh  * vim: noexpandtab sw=8 ts=8 sts=0:
3ccd979bdSMark Fasheh  *
4ccd979bdSMark Fasheh  * alloc.c
5ccd979bdSMark Fasheh  *
6ccd979bdSMark Fasheh  * Extent allocs and frees
7ccd979bdSMark Fasheh  *
8ccd979bdSMark Fasheh  * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
9ccd979bdSMark Fasheh  *
10ccd979bdSMark Fasheh  * This program is free software; you can redistribute it and/or
11ccd979bdSMark Fasheh  * modify it under the terms of the GNU General Public
12ccd979bdSMark Fasheh  * License as published by the Free Software Foundation; either
13ccd979bdSMark Fasheh  * version 2 of the License, or (at your option) any later version.
14ccd979bdSMark Fasheh  *
15ccd979bdSMark Fasheh  * This program is distributed in the hope that it will be useful,
16ccd979bdSMark Fasheh  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17ccd979bdSMark Fasheh  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18ccd979bdSMark Fasheh  * General Public License for more details.
19ccd979bdSMark Fasheh  *
20ccd979bdSMark Fasheh  * You should have received a copy of the GNU General Public
21ccd979bdSMark Fasheh  * License along with this program; if not, write to the
22ccd979bdSMark Fasheh  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23ccd979bdSMark Fasheh  * Boston, MA 021110-1307, USA.
24ccd979bdSMark Fasheh  */
25ccd979bdSMark Fasheh 
26ccd979bdSMark Fasheh #include <linux/fs.h>
27ccd979bdSMark Fasheh #include <linux/types.h>
28ccd979bdSMark Fasheh #include <linux/slab.h>
29ccd979bdSMark Fasheh #include <linux/highmem.h>
3060b11392SMark Fasheh #include <linux/swap.h>
31ccd979bdSMark Fasheh 
32ccd979bdSMark Fasheh #define MLOG_MASK_PREFIX ML_DISK_ALLOC
33ccd979bdSMark Fasheh #include <cluster/masklog.h>
34ccd979bdSMark Fasheh 
35ccd979bdSMark Fasheh #include "ocfs2.h"
36ccd979bdSMark Fasheh 
37ccd979bdSMark Fasheh #include "alloc.h"
3860b11392SMark Fasheh #include "aops.h"
39ccd979bdSMark Fasheh #include "dlmglue.h"
40ccd979bdSMark Fasheh #include "extent_map.h"
41ccd979bdSMark Fasheh #include "inode.h"
42ccd979bdSMark Fasheh #include "journal.h"
43ccd979bdSMark Fasheh #include "localalloc.h"
44ccd979bdSMark Fasheh #include "suballoc.h"
45ccd979bdSMark Fasheh #include "sysfile.h"
46ccd979bdSMark Fasheh #include "file.h"
47ccd979bdSMark Fasheh #include "super.h"
48ccd979bdSMark Fasheh #include "uptodate.h"
49ccd979bdSMark Fasheh 
50ccd979bdSMark Fasheh #include "buffer_head_io.h"
51ccd979bdSMark Fasheh 
52ccd979bdSMark Fasheh static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
5359a5e416SMark Fasheh static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
5459a5e416SMark Fasheh 					 struct ocfs2_extent_block *eb);
55ccd979bdSMark Fasheh 
56dcd0538fSMark Fasheh /*
57dcd0538fSMark Fasheh  * Structures which describe a path through a btree, and functions to
58dcd0538fSMark Fasheh  * manipulate them.
59dcd0538fSMark Fasheh  *
60dcd0538fSMark Fasheh  * The idea here is to be as generic as possible with the tree
61dcd0538fSMark Fasheh  * manipulation code.
62dcd0538fSMark Fasheh  */
63dcd0538fSMark Fasheh struct ocfs2_path_item {
64dcd0538fSMark Fasheh 	struct buffer_head		*bh;
65dcd0538fSMark Fasheh 	struct ocfs2_extent_list	*el;
66dcd0538fSMark Fasheh };
67dcd0538fSMark Fasheh 
68dcd0538fSMark Fasheh #define OCFS2_MAX_PATH_DEPTH	5
69dcd0538fSMark Fasheh 
70dcd0538fSMark Fasheh struct ocfs2_path {
71dcd0538fSMark Fasheh 	int			p_tree_depth;
72dcd0538fSMark Fasheh 	struct ocfs2_path_item	p_node[OCFS2_MAX_PATH_DEPTH];
73dcd0538fSMark Fasheh };
74dcd0538fSMark Fasheh 
75dcd0538fSMark Fasheh #define path_root_bh(_path) ((_path)->p_node[0].bh)
76dcd0538fSMark Fasheh #define path_root_el(_path) ((_path)->p_node[0].el)
77dcd0538fSMark Fasheh #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
78dcd0538fSMark Fasheh #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
79dcd0538fSMark Fasheh #define path_num_items(_path) ((_path)->p_tree_depth + 1)
80dcd0538fSMark Fasheh 
81dcd0538fSMark Fasheh /*
82dcd0538fSMark Fasheh  * Reset the actual path elements so that we can re-use the structure
83dcd0538fSMark Fasheh  * to build another path. Generally, this involves freeing the buffer
84dcd0538fSMark Fasheh  * heads.
85dcd0538fSMark Fasheh  */
86dcd0538fSMark Fasheh static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
87dcd0538fSMark Fasheh {
88dcd0538fSMark Fasheh 	int i, start = 0, depth = 0;
89dcd0538fSMark Fasheh 	struct ocfs2_path_item *node;
90dcd0538fSMark Fasheh 
91dcd0538fSMark Fasheh 	if (keep_root)
92dcd0538fSMark Fasheh 		start = 1;
93dcd0538fSMark Fasheh 
94dcd0538fSMark Fasheh 	for(i = start; i < path_num_items(path); i++) {
95dcd0538fSMark Fasheh 		node = &path->p_node[i];
96dcd0538fSMark Fasheh 
97dcd0538fSMark Fasheh 		brelse(node->bh);
98dcd0538fSMark Fasheh 		node->bh = NULL;
99dcd0538fSMark Fasheh 		node->el = NULL;
100dcd0538fSMark Fasheh 	}
101dcd0538fSMark Fasheh 
102dcd0538fSMark Fasheh 	/*
103dcd0538fSMark Fasheh 	 * Tree depth may change during truncate, or insert. If we're
104dcd0538fSMark Fasheh 	 * keeping the root extent list, then make sure that our path
105dcd0538fSMark Fasheh 	 * structure reflects the proper depth.
106dcd0538fSMark Fasheh 	 */
107dcd0538fSMark Fasheh 	if (keep_root)
108dcd0538fSMark Fasheh 		depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
109dcd0538fSMark Fasheh 
110dcd0538fSMark Fasheh 	path->p_tree_depth = depth;
111dcd0538fSMark Fasheh }
112dcd0538fSMark Fasheh 
113dcd0538fSMark Fasheh static void ocfs2_free_path(struct ocfs2_path *path)
114dcd0538fSMark Fasheh {
115dcd0538fSMark Fasheh 	if (path) {
116dcd0538fSMark Fasheh 		ocfs2_reinit_path(path, 0);
117dcd0538fSMark Fasheh 		kfree(path);
118dcd0538fSMark Fasheh 	}
119dcd0538fSMark Fasheh }
120dcd0538fSMark Fasheh 
121dcd0538fSMark Fasheh /*
122328d5752SMark Fasheh  * All the elements of src into dest. After this call, src could be freed
123328d5752SMark Fasheh  * without affecting dest.
124328d5752SMark Fasheh  *
125328d5752SMark Fasheh  * Both paths should have the same root. Any non-root elements of dest
126328d5752SMark Fasheh  * will be freed.
127328d5752SMark Fasheh  */
128328d5752SMark Fasheh static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
129328d5752SMark Fasheh {
130328d5752SMark Fasheh 	int i;
131328d5752SMark Fasheh 
132328d5752SMark Fasheh 	BUG_ON(path_root_bh(dest) != path_root_bh(src));
133328d5752SMark Fasheh 	BUG_ON(path_root_el(dest) != path_root_el(src));
134328d5752SMark Fasheh 
135328d5752SMark Fasheh 	ocfs2_reinit_path(dest, 1);
136328d5752SMark Fasheh 
137328d5752SMark Fasheh 	for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
138328d5752SMark Fasheh 		dest->p_node[i].bh = src->p_node[i].bh;
139328d5752SMark Fasheh 		dest->p_node[i].el = src->p_node[i].el;
140328d5752SMark Fasheh 
141328d5752SMark Fasheh 		if (dest->p_node[i].bh)
142328d5752SMark Fasheh 			get_bh(dest->p_node[i].bh);
143328d5752SMark Fasheh 	}
144328d5752SMark Fasheh }
145328d5752SMark Fasheh 
146328d5752SMark Fasheh /*
147dcd0538fSMark Fasheh  * Make the *dest path the same as src and re-initialize src path to
148dcd0538fSMark Fasheh  * have a root only.
149dcd0538fSMark Fasheh  */
150dcd0538fSMark Fasheh static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
151dcd0538fSMark Fasheh {
152dcd0538fSMark Fasheh 	int i;
153dcd0538fSMark Fasheh 
154dcd0538fSMark Fasheh 	BUG_ON(path_root_bh(dest) != path_root_bh(src));
155dcd0538fSMark Fasheh 
156dcd0538fSMark Fasheh 	for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
157dcd0538fSMark Fasheh 		brelse(dest->p_node[i].bh);
158dcd0538fSMark Fasheh 
159dcd0538fSMark Fasheh 		dest->p_node[i].bh = src->p_node[i].bh;
160dcd0538fSMark Fasheh 		dest->p_node[i].el = src->p_node[i].el;
161dcd0538fSMark Fasheh 
162dcd0538fSMark Fasheh 		src->p_node[i].bh = NULL;
163dcd0538fSMark Fasheh 		src->p_node[i].el = NULL;
164dcd0538fSMark Fasheh 	}
165dcd0538fSMark Fasheh }
166dcd0538fSMark Fasheh 
167dcd0538fSMark Fasheh /*
168dcd0538fSMark Fasheh  * Insert an extent block at given index.
169dcd0538fSMark Fasheh  *
170dcd0538fSMark Fasheh  * This will not take an additional reference on eb_bh.
171dcd0538fSMark Fasheh  */
172dcd0538fSMark Fasheh static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
173dcd0538fSMark Fasheh 					struct buffer_head *eb_bh)
174dcd0538fSMark Fasheh {
175dcd0538fSMark Fasheh 	struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
176dcd0538fSMark Fasheh 
177dcd0538fSMark Fasheh 	/*
178dcd0538fSMark Fasheh 	 * Right now, no root bh is an extent block, so this helps
179dcd0538fSMark Fasheh 	 * catch code errors with dinode trees. The assertion can be
180dcd0538fSMark Fasheh 	 * safely removed if we ever need to insert extent block
181dcd0538fSMark Fasheh 	 * structures at the root.
182dcd0538fSMark Fasheh 	 */
183dcd0538fSMark Fasheh 	BUG_ON(index == 0);
184dcd0538fSMark Fasheh 
185dcd0538fSMark Fasheh 	path->p_node[index].bh = eb_bh;
186dcd0538fSMark Fasheh 	path->p_node[index].el = &eb->h_list;
187dcd0538fSMark Fasheh }
188dcd0538fSMark Fasheh 
189dcd0538fSMark Fasheh static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
190dcd0538fSMark Fasheh 					 struct ocfs2_extent_list *root_el)
191dcd0538fSMark Fasheh {
192dcd0538fSMark Fasheh 	struct ocfs2_path *path;
193dcd0538fSMark Fasheh 
194dcd0538fSMark Fasheh 	BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
195dcd0538fSMark Fasheh 
196dcd0538fSMark Fasheh 	path = kzalloc(sizeof(*path), GFP_NOFS);
197dcd0538fSMark Fasheh 	if (path) {
198dcd0538fSMark Fasheh 		path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
199dcd0538fSMark Fasheh 		get_bh(root_bh);
200dcd0538fSMark Fasheh 		path_root_bh(path) = root_bh;
201dcd0538fSMark Fasheh 		path_root_el(path) = root_el;
202dcd0538fSMark Fasheh 	}
203dcd0538fSMark Fasheh 
204dcd0538fSMark Fasheh 	return path;
205dcd0538fSMark Fasheh }
206dcd0538fSMark Fasheh 
207dcd0538fSMark Fasheh /*
208dcd0538fSMark Fasheh  * Allocate and initialize a new path based on a disk inode tree.
209dcd0538fSMark Fasheh  */
210dcd0538fSMark Fasheh static struct ocfs2_path *ocfs2_new_inode_path(struct buffer_head *di_bh)
211dcd0538fSMark Fasheh {
212dcd0538fSMark Fasheh 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
213dcd0538fSMark Fasheh 	struct ocfs2_extent_list *el = &di->id2.i_list;
214dcd0538fSMark Fasheh 
215dcd0538fSMark Fasheh 	return ocfs2_new_path(di_bh, el);
216dcd0538fSMark Fasheh }
217dcd0538fSMark Fasheh 
218dcd0538fSMark Fasheh /*
219dcd0538fSMark Fasheh  * Convenience function to journal all components in a path.
220dcd0538fSMark Fasheh  */
221dcd0538fSMark Fasheh static int ocfs2_journal_access_path(struct inode *inode, handle_t *handle,
222dcd0538fSMark Fasheh 				     struct ocfs2_path *path)
223dcd0538fSMark Fasheh {
224dcd0538fSMark Fasheh 	int i, ret = 0;
225dcd0538fSMark Fasheh 
226dcd0538fSMark Fasheh 	if (!path)
227dcd0538fSMark Fasheh 		goto out;
228dcd0538fSMark Fasheh 
229dcd0538fSMark Fasheh 	for(i = 0; i < path_num_items(path); i++) {
230dcd0538fSMark Fasheh 		ret = ocfs2_journal_access(handle, inode, path->p_node[i].bh,
231dcd0538fSMark Fasheh 					   OCFS2_JOURNAL_ACCESS_WRITE);
232dcd0538fSMark Fasheh 		if (ret < 0) {
233dcd0538fSMark Fasheh 			mlog_errno(ret);
234dcd0538fSMark Fasheh 			goto out;
235dcd0538fSMark Fasheh 		}
236dcd0538fSMark Fasheh 	}
237dcd0538fSMark Fasheh 
238dcd0538fSMark Fasheh out:
239dcd0538fSMark Fasheh 	return ret;
240dcd0538fSMark Fasheh }
241dcd0538fSMark Fasheh 
242328d5752SMark Fasheh /*
243328d5752SMark Fasheh  * Return the index of the extent record which contains cluster #v_cluster.
244328d5752SMark Fasheh  * -1 is returned if it was not found.
245328d5752SMark Fasheh  *
246328d5752SMark Fasheh  * Should work fine on interior and exterior nodes.
247328d5752SMark Fasheh  */
248328d5752SMark Fasheh int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
249328d5752SMark Fasheh {
250328d5752SMark Fasheh 	int ret = -1;
251328d5752SMark Fasheh 	int i;
252328d5752SMark Fasheh 	struct ocfs2_extent_rec *rec;
253328d5752SMark Fasheh 	u32 rec_end, rec_start, clusters;
254328d5752SMark Fasheh 
255328d5752SMark Fasheh 	for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
256328d5752SMark Fasheh 		rec = &el->l_recs[i];
257328d5752SMark Fasheh 
258328d5752SMark Fasheh 		rec_start = le32_to_cpu(rec->e_cpos);
259328d5752SMark Fasheh 		clusters = ocfs2_rec_clusters(el, rec);
260328d5752SMark Fasheh 
261328d5752SMark Fasheh 		rec_end = rec_start + clusters;
262328d5752SMark Fasheh 
263328d5752SMark Fasheh 		if (v_cluster >= rec_start && v_cluster < rec_end) {
264328d5752SMark Fasheh 			ret = i;
265328d5752SMark Fasheh 			break;
266328d5752SMark Fasheh 		}
267328d5752SMark Fasheh 	}
268328d5752SMark Fasheh 
269328d5752SMark Fasheh 	return ret;
270328d5752SMark Fasheh }
271328d5752SMark Fasheh 
272dcd0538fSMark Fasheh enum ocfs2_contig_type {
273dcd0538fSMark Fasheh 	CONTIG_NONE = 0,
274dcd0538fSMark Fasheh 	CONTIG_LEFT,
275328d5752SMark Fasheh 	CONTIG_RIGHT,
276328d5752SMark Fasheh 	CONTIG_LEFTRIGHT,
277dcd0538fSMark Fasheh };
278dcd0538fSMark Fasheh 
279e48edee2SMark Fasheh 
280e48edee2SMark Fasheh /*
281e48edee2SMark Fasheh  * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
282e48edee2SMark Fasheh  * ocfs2_extent_contig only work properly against leaf nodes!
283e48edee2SMark Fasheh  */
284dcd0538fSMark Fasheh static int ocfs2_block_extent_contig(struct super_block *sb,
285ccd979bdSMark Fasheh 				     struct ocfs2_extent_rec *ext,
286ccd979bdSMark Fasheh 				     u64 blkno)
287ccd979bdSMark Fasheh {
288e48edee2SMark Fasheh 	u64 blk_end = le64_to_cpu(ext->e_blkno);
289e48edee2SMark Fasheh 
290e48edee2SMark Fasheh 	blk_end += ocfs2_clusters_to_blocks(sb,
291e48edee2SMark Fasheh 				    le16_to_cpu(ext->e_leaf_clusters));
292e48edee2SMark Fasheh 
293e48edee2SMark Fasheh 	return blkno == blk_end;
294ccd979bdSMark Fasheh }
295ccd979bdSMark Fasheh 
296dcd0538fSMark Fasheh static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
297dcd0538fSMark Fasheh 				  struct ocfs2_extent_rec *right)
298dcd0538fSMark Fasheh {
299e48edee2SMark Fasheh 	u32 left_range;
300e48edee2SMark Fasheh 
301e48edee2SMark Fasheh 	left_range = le32_to_cpu(left->e_cpos) +
302e48edee2SMark Fasheh 		le16_to_cpu(left->e_leaf_clusters);
303e48edee2SMark Fasheh 
304e48edee2SMark Fasheh 	return (left_range == le32_to_cpu(right->e_cpos));
305dcd0538fSMark Fasheh }
306dcd0538fSMark Fasheh 
307dcd0538fSMark Fasheh static enum ocfs2_contig_type
308dcd0538fSMark Fasheh 	ocfs2_extent_contig(struct inode *inode,
309dcd0538fSMark Fasheh 			    struct ocfs2_extent_rec *ext,
310dcd0538fSMark Fasheh 			    struct ocfs2_extent_rec *insert_rec)
311dcd0538fSMark Fasheh {
312dcd0538fSMark Fasheh 	u64 blkno = le64_to_cpu(insert_rec->e_blkno);
313dcd0538fSMark Fasheh 
314328d5752SMark Fasheh 	/*
315328d5752SMark Fasheh 	 * Refuse to coalesce extent records with different flag
316328d5752SMark Fasheh 	 * fields - we don't want to mix unwritten extents with user
317328d5752SMark Fasheh 	 * data.
318328d5752SMark Fasheh 	 */
319328d5752SMark Fasheh 	if (ext->e_flags != insert_rec->e_flags)
320328d5752SMark Fasheh 		return CONTIG_NONE;
321328d5752SMark Fasheh 
322dcd0538fSMark Fasheh 	if (ocfs2_extents_adjacent(ext, insert_rec) &&
323dcd0538fSMark Fasheh 	    ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
324dcd0538fSMark Fasheh 			return CONTIG_RIGHT;
325dcd0538fSMark Fasheh 
326dcd0538fSMark Fasheh 	blkno = le64_to_cpu(ext->e_blkno);
327dcd0538fSMark Fasheh 	if (ocfs2_extents_adjacent(insert_rec, ext) &&
328dcd0538fSMark Fasheh 	    ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
329dcd0538fSMark Fasheh 		return CONTIG_LEFT;
330dcd0538fSMark Fasheh 
331dcd0538fSMark Fasheh 	return CONTIG_NONE;
332dcd0538fSMark Fasheh }
333dcd0538fSMark Fasheh 
334dcd0538fSMark Fasheh /*
335dcd0538fSMark Fasheh  * NOTE: We can have pretty much any combination of contiguousness and
336dcd0538fSMark Fasheh  * appending.
337dcd0538fSMark Fasheh  *
338dcd0538fSMark Fasheh  * The usefulness of APPEND_TAIL is more in that it lets us know that
339dcd0538fSMark Fasheh  * we'll have to update the path to that leaf.
340dcd0538fSMark Fasheh  */
341dcd0538fSMark Fasheh enum ocfs2_append_type {
342dcd0538fSMark Fasheh 	APPEND_NONE = 0,
343dcd0538fSMark Fasheh 	APPEND_TAIL,
344dcd0538fSMark Fasheh };
345dcd0538fSMark Fasheh 
346328d5752SMark Fasheh enum ocfs2_split_type {
347328d5752SMark Fasheh 	SPLIT_NONE = 0,
348328d5752SMark Fasheh 	SPLIT_LEFT,
349328d5752SMark Fasheh 	SPLIT_RIGHT,
350328d5752SMark Fasheh };
351328d5752SMark Fasheh 
352dcd0538fSMark Fasheh struct ocfs2_insert_type {
353328d5752SMark Fasheh 	enum ocfs2_split_type	ins_split;
354dcd0538fSMark Fasheh 	enum ocfs2_append_type	ins_appending;
355dcd0538fSMark Fasheh 	enum ocfs2_contig_type	ins_contig;
356dcd0538fSMark Fasheh 	int			ins_contig_index;
357dcd0538fSMark Fasheh 	int			ins_tree_depth;
358dcd0538fSMark Fasheh };
359dcd0538fSMark Fasheh 
360328d5752SMark Fasheh struct ocfs2_merge_ctxt {
361328d5752SMark Fasheh 	enum ocfs2_contig_type	c_contig_type;
362328d5752SMark Fasheh 	int			c_has_empty_extent;
363328d5752SMark Fasheh 	int			c_split_covers_rec;
364328d5752SMark Fasheh };
365328d5752SMark Fasheh 
366ccd979bdSMark Fasheh /*
367ccd979bdSMark Fasheh  * How many free extents have we got before we need more meta data?
368ccd979bdSMark Fasheh  */
369ccd979bdSMark Fasheh int ocfs2_num_free_extents(struct ocfs2_super *osb,
370ccd979bdSMark Fasheh 			   struct inode *inode,
371231b87d1STao Ma 			   struct buffer_head *bh)
372ccd979bdSMark Fasheh {
373ccd979bdSMark Fasheh 	int retval;
374ccd979bdSMark Fasheh 	struct ocfs2_extent_list *el;
375ccd979bdSMark Fasheh 	struct ocfs2_extent_block *eb;
376ccd979bdSMark Fasheh 	struct buffer_head *eb_bh = NULL;
377231b87d1STao Ma 	struct ocfs2_dinode *fe = (struct ocfs2_dinode *)bh->b_data;
378ccd979bdSMark Fasheh 
379ccd979bdSMark Fasheh 	mlog_entry_void();
380ccd979bdSMark Fasheh 
381ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_DINODE(fe)) {
382ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
383ccd979bdSMark Fasheh 		retval = -EIO;
384ccd979bdSMark Fasheh 		goto bail;
385ccd979bdSMark Fasheh 	}
386ccd979bdSMark Fasheh 
387ccd979bdSMark Fasheh 	if (fe->i_last_eb_blk) {
388ccd979bdSMark Fasheh 		retval = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
389ccd979bdSMark Fasheh 					  &eb_bh, OCFS2_BH_CACHED, inode);
390ccd979bdSMark Fasheh 		if (retval < 0) {
391ccd979bdSMark Fasheh 			mlog_errno(retval);
392ccd979bdSMark Fasheh 			goto bail;
393ccd979bdSMark Fasheh 		}
394ccd979bdSMark Fasheh 		eb = (struct ocfs2_extent_block *) eb_bh->b_data;
395ccd979bdSMark Fasheh 		el = &eb->h_list;
396ccd979bdSMark Fasheh 	} else
397ccd979bdSMark Fasheh 		el = &fe->id2.i_list;
398ccd979bdSMark Fasheh 
399ccd979bdSMark Fasheh 	BUG_ON(el->l_tree_depth != 0);
400ccd979bdSMark Fasheh 
401ccd979bdSMark Fasheh 	retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
402ccd979bdSMark Fasheh bail:
403ccd979bdSMark Fasheh 	if (eb_bh)
404ccd979bdSMark Fasheh 		brelse(eb_bh);
405ccd979bdSMark Fasheh 
406ccd979bdSMark Fasheh 	mlog_exit(retval);
407ccd979bdSMark Fasheh 	return retval;
408ccd979bdSMark Fasheh }
409ccd979bdSMark Fasheh 
410ccd979bdSMark Fasheh /* expects array to already be allocated
411ccd979bdSMark Fasheh  *
412ccd979bdSMark Fasheh  * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
413ccd979bdSMark Fasheh  * l_count for you
414ccd979bdSMark Fasheh  */
415ccd979bdSMark Fasheh static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
4161fabe148SMark Fasheh 				     handle_t *handle,
417ccd979bdSMark Fasheh 				     struct inode *inode,
418ccd979bdSMark Fasheh 				     int wanted,
419ccd979bdSMark Fasheh 				     struct ocfs2_alloc_context *meta_ac,
420ccd979bdSMark Fasheh 				     struct buffer_head *bhs[])
421ccd979bdSMark Fasheh {
422ccd979bdSMark Fasheh 	int count, status, i;
423ccd979bdSMark Fasheh 	u16 suballoc_bit_start;
424ccd979bdSMark Fasheh 	u32 num_got;
425ccd979bdSMark Fasheh 	u64 first_blkno;
426ccd979bdSMark Fasheh 	struct ocfs2_extent_block *eb;
427ccd979bdSMark Fasheh 
428ccd979bdSMark Fasheh 	mlog_entry_void();
429ccd979bdSMark Fasheh 
430ccd979bdSMark Fasheh 	count = 0;
431ccd979bdSMark Fasheh 	while (count < wanted) {
432ccd979bdSMark Fasheh 		status = ocfs2_claim_metadata(osb,
433ccd979bdSMark Fasheh 					      handle,
434ccd979bdSMark Fasheh 					      meta_ac,
435ccd979bdSMark Fasheh 					      wanted - count,
436ccd979bdSMark Fasheh 					      &suballoc_bit_start,
437ccd979bdSMark Fasheh 					      &num_got,
438ccd979bdSMark Fasheh 					      &first_blkno);
439ccd979bdSMark Fasheh 		if (status < 0) {
440ccd979bdSMark Fasheh 			mlog_errno(status);
441ccd979bdSMark Fasheh 			goto bail;
442ccd979bdSMark Fasheh 		}
443ccd979bdSMark Fasheh 
444ccd979bdSMark Fasheh 		for(i = count;  i < (num_got + count); i++) {
445ccd979bdSMark Fasheh 			bhs[i] = sb_getblk(osb->sb, first_blkno);
446ccd979bdSMark Fasheh 			if (bhs[i] == NULL) {
447ccd979bdSMark Fasheh 				status = -EIO;
448ccd979bdSMark Fasheh 				mlog_errno(status);
449ccd979bdSMark Fasheh 				goto bail;
450ccd979bdSMark Fasheh 			}
451ccd979bdSMark Fasheh 			ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
452ccd979bdSMark Fasheh 
453ccd979bdSMark Fasheh 			status = ocfs2_journal_access(handle, inode, bhs[i],
454ccd979bdSMark Fasheh 						      OCFS2_JOURNAL_ACCESS_CREATE);
455ccd979bdSMark Fasheh 			if (status < 0) {
456ccd979bdSMark Fasheh 				mlog_errno(status);
457ccd979bdSMark Fasheh 				goto bail;
458ccd979bdSMark Fasheh 			}
459ccd979bdSMark Fasheh 
460ccd979bdSMark Fasheh 			memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
461ccd979bdSMark Fasheh 			eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
462ccd979bdSMark Fasheh 			/* Ok, setup the minimal stuff here. */
463ccd979bdSMark Fasheh 			strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
464ccd979bdSMark Fasheh 			eb->h_blkno = cpu_to_le64(first_blkno);
465ccd979bdSMark Fasheh 			eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
466ccd979bdSMark Fasheh 			eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
467ccd979bdSMark Fasheh 			eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
468ccd979bdSMark Fasheh 			eb->h_list.l_count =
469ccd979bdSMark Fasheh 				cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
470ccd979bdSMark Fasheh 
471ccd979bdSMark Fasheh 			suballoc_bit_start++;
472ccd979bdSMark Fasheh 			first_blkno++;
473ccd979bdSMark Fasheh 
474ccd979bdSMark Fasheh 			/* We'll also be dirtied by the caller, so
475ccd979bdSMark Fasheh 			 * this isn't absolutely necessary. */
476ccd979bdSMark Fasheh 			status = ocfs2_journal_dirty(handle, bhs[i]);
477ccd979bdSMark Fasheh 			if (status < 0) {
478ccd979bdSMark Fasheh 				mlog_errno(status);
479ccd979bdSMark Fasheh 				goto bail;
480ccd979bdSMark Fasheh 			}
481ccd979bdSMark Fasheh 		}
482ccd979bdSMark Fasheh 
483ccd979bdSMark Fasheh 		count += num_got;
484ccd979bdSMark Fasheh 	}
485ccd979bdSMark Fasheh 
486ccd979bdSMark Fasheh 	status = 0;
487ccd979bdSMark Fasheh bail:
488ccd979bdSMark Fasheh 	if (status < 0) {
489ccd979bdSMark Fasheh 		for(i = 0; i < wanted; i++) {
490ccd979bdSMark Fasheh 			if (bhs[i])
491ccd979bdSMark Fasheh 				brelse(bhs[i]);
492ccd979bdSMark Fasheh 			bhs[i] = NULL;
493ccd979bdSMark Fasheh 		}
494ccd979bdSMark Fasheh 	}
495ccd979bdSMark Fasheh 	mlog_exit(status);
496ccd979bdSMark Fasheh 	return status;
497ccd979bdSMark Fasheh }
498ccd979bdSMark Fasheh 
499ccd979bdSMark Fasheh /*
500dcd0538fSMark Fasheh  * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
501dcd0538fSMark Fasheh  *
502dcd0538fSMark Fasheh  * Returns the sum of the rightmost extent rec logical offset and
503dcd0538fSMark Fasheh  * cluster count.
504dcd0538fSMark Fasheh  *
505dcd0538fSMark Fasheh  * ocfs2_add_branch() uses this to determine what logical cluster
506dcd0538fSMark Fasheh  * value should be populated into the leftmost new branch records.
507dcd0538fSMark Fasheh  *
508dcd0538fSMark Fasheh  * ocfs2_shift_tree_depth() uses this to determine the # clusters
509dcd0538fSMark Fasheh  * value for the new topmost tree record.
510dcd0538fSMark Fasheh  */
511dcd0538fSMark Fasheh static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list  *el)
512dcd0538fSMark Fasheh {
513dcd0538fSMark Fasheh 	int i;
514dcd0538fSMark Fasheh 
515dcd0538fSMark Fasheh 	i = le16_to_cpu(el->l_next_free_rec) - 1;
516dcd0538fSMark Fasheh 
517dcd0538fSMark Fasheh 	return le32_to_cpu(el->l_recs[i].e_cpos) +
518e48edee2SMark Fasheh 		ocfs2_rec_clusters(el, &el->l_recs[i]);
519dcd0538fSMark Fasheh }
520dcd0538fSMark Fasheh 
521dcd0538fSMark Fasheh /*
522ccd979bdSMark Fasheh  * Add an entire tree branch to our inode. eb_bh is the extent block
523ccd979bdSMark Fasheh  * to start at, if we don't want to start the branch at the dinode
524ccd979bdSMark Fasheh  * structure.
525ccd979bdSMark Fasheh  *
526ccd979bdSMark Fasheh  * last_eb_bh is required as we have to update it's next_leaf pointer
527ccd979bdSMark Fasheh  * for the new last extent block.
528ccd979bdSMark Fasheh  *
529ccd979bdSMark Fasheh  * the new branch will be 'empty' in the sense that every block will
530e48edee2SMark Fasheh  * contain a single record with cluster count == 0.
531ccd979bdSMark Fasheh  */
532ccd979bdSMark Fasheh static int ocfs2_add_branch(struct ocfs2_super *osb,
5331fabe148SMark Fasheh 			    handle_t *handle,
534ccd979bdSMark Fasheh 			    struct inode *inode,
535ccd979bdSMark Fasheh 			    struct buffer_head *fe_bh,
536ccd979bdSMark Fasheh 			    struct buffer_head *eb_bh,
537328d5752SMark Fasheh 			    struct buffer_head **last_eb_bh,
538ccd979bdSMark Fasheh 			    struct ocfs2_alloc_context *meta_ac)
539ccd979bdSMark Fasheh {
540ccd979bdSMark Fasheh 	int status, new_blocks, i;
541ccd979bdSMark Fasheh 	u64 next_blkno, new_last_eb_blk;
542ccd979bdSMark Fasheh 	struct buffer_head *bh;
543ccd979bdSMark Fasheh 	struct buffer_head **new_eb_bhs = NULL;
544ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe;
545ccd979bdSMark Fasheh 	struct ocfs2_extent_block *eb;
546ccd979bdSMark Fasheh 	struct ocfs2_extent_list  *eb_el;
547ccd979bdSMark Fasheh 	struct ocfs2_extent_list  *el;
548dcd0538fSMark Fasheh 	u32 new_cpos;
549ccd979bdSMark Fasheh 
550ccd979bdSMark Fasheh 	mlog_entry_void();
551ccd979bdSMark Fasheh 
552328d5752SMark Fasheh 	BUG_ON(!last_eb_bh || !*last_eb_bh);
553ccd979bdSMark Fasheh 
554ccd979bdSMark Fasheh 	fe = (struct ocfs2_dinode *) fe_bh->b_data;
555ccd979bdSMark Fasheh 
556ccd979bdSMark Fasheh 	if (eb_bh) {
557ccd979bdSMark Fasheh 		eb = (struct ocfs2_extent_block *) eb_bh->b_data;
558ccd979bdSMark Fasheh 		el = &eb->h_list;
559ccd979bdSMark Fasheh 	} else
560ccd979bdSMark Fasheh 		el = &fe->id2.i_list;
561ccd979bdSMark Fasheh 
562ccd979bdSMark Fasheh 	/* we never add a branch to a leaf. */
563ccd979bdSMark Fasheh 	BUG_ON(!el->l_tree_depth);
564ccd979bdSMark Fasheh 
565ccd979bdSMark Fasheh 	new_blocks = le16_to_cpu(el->l_tree_depth);
566ccd979bdSMark Fasheh 
567ccd979bdSMark Fasheh 	/* allocate the number of new eb blocks we need */
568ccd979bdSMark Fasheh 	new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
569ccd979bdSMark Fasheh 			     GFP_KERNEL);
570ccd979bdSMark Fasheh 	if (!new_eb_bhs) {
571ccd979bdSMark Fasheh 		status = -ENOMEM;
572ccd979bdSMark Fasheh 		mlog_errno(status);
573ccd979bdSMark Fasheh 		goto bail;
574ccd979bdSMark Fasheh 	}
575ccd979bdSMark Fasheh 
576ccd979bdSMark Fasheh 	status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks,
577ccd979bdSMark Fasheh 					   meta_ac, new_eb_bhs);
578ccd979bdSMark Fasheh 	if (status < 0) {
579ccd979bdSMark Fasheh 		mlog_errno(status);
580ccd979bdSMark Fasheh 		goto bail;
581ccd979bdSMark Fasheh 	}
582ccd979bdSMark Fasheh 
583328d5752SMark Fasheh 	eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
584dcd0538fSMark Fasheh 	new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
585dcd0538fSMark Fasheh 
586ccd979bdSMark Fasheh 	/* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
587ccd979bdSMark Fasheh 	 * linked with the rest of the tree.
588ccd979bdSMark Fasheh 	 * conversly, new_eb_bhs[0] is the new bottommost leaf.
589ccd979bdSMark Fasheh 	 *
590ccd979bdSMark Fasheh 	 * when we leave the loop, new_last_eb_blk will point to the
591ccd979bdSMark Fasheh 	 * newest leaf, and next_blkno will point to the topmost extent
592ccd979bdSMark Fasheh 	 * block. */
593ccd979bdSMark Fasheh 	next_blkno = new_last_eb_blk = 0;
594ccd979bdSMark Fasheh 	for(i = 0; i < new_blocks; i++) {
595ccd979bdSMark Fasheh 		bh = new_eb_bhs[i];
596ccd979bdSMark Fasheh 		eb = (struct ocfs2_extent_block *) bh->b_data;
597ccd979bdSMark Fasheh 		if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
598ccd979bdSMark Fasheh 			OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
599ccd979bdSMark Fasheh 			status = -EIO;
600ccd979bdSMark Fasheh 			goto bail;
601ccd979bdSMark Fasheh 		}
602ccd979bdSMark Fasheh 		eb_el = &eb->h_list;
603ccd979bdSMark Fasheh 
604ccd979bdSMark Fasheh 		status = ocfs2_journal_access(handle, inode, bh,
605ccd979bdSMark Fasheh 					      OCFS2_JOURNAL_ACCESS_CREATE);
606ccd979bdSMark Fasheh 		if (status < 0) {
607ccd979bdSMark Fasheh 			mlog_errno(status);
608ccd979bdSMark Fasheh 			goto bail;
609ccd979bdSMark Fasheh 		}
610ccd979bdSMark Fasheh 
611ccd979bdSMark Fasheh 		eb->h_next_leaf_blk = 0;
612ccd979bdSMark Fasheh 		eb_el->l_tree_depth = cpu_to_le16(i);
613ccd979bdSMark Fasheh 		eb_el->l_next_free_rec = cpu_to_le16(1);
614dcd0538fSMark Fasheh 		/*
615dcd0538fSMark Fasheh 		 * This actually counts as an empty extent as
616dcd0538fSMark Fasheh 		 * c_clusters == 0
617dcd0538fSMark Fasheh 		 */
618dcd0538fSMark Fasheh 		eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
619ccd979bdSMark Fasheh 		eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
620e48edee2SMark Fasheh 		/*
621e48edee2SMark Fasheh 		 * eb_el isn't always an interior node, but even leaf
622e48edee2SMark Fasheh 		 * nodes want a zero'd flags and reserved field so
623e48edee2SMark Fasheh 		 * this gets the whole 32 bits regardless of use.
624e48edee2SMark Fasheh 		 */
625e48edee2SMark Fasheh 		eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
626ccd979bdSMark Fasheh 		if (!eb_el->l_tree_depth)
627ccd979bdSMark Fasheh 			new_last_eb_blk = le64_to_cpu(eb->h_blkno);
628ccd979bdSMark Fasheh 
629ccd979bdSMark Fasheh 		status = ocfs2_journal_dirty(handle, bh);
630ccd979bdSMark Fasheh 		if (status < 0) {
631ccd979bdSMark Fasheh 			mlog_errno(status);
632ccd979bdSMark Fasheh 			goto bail;
633ccd979bdSMark Fasheh 		}
634ccd979bdSMark Fasheh 
635ccd979bdSMark Fasheh 		next_blkno = le64_to_cpu(eb->h_blkno);
636ccd979bdSMark Fasheh 	}
637ccd979bdSMark Fasheh 
638ccd979bdSMark Fasheh 	/* This is a bit hairy. We want to update up to three blocks
639ccd979bdSMark Fasheh 	 * here without leaving any of them in an inconsistent state
640ccd979bdSMark Fasheh 	 * in case of error. We don't have to worry about
641ccd979bdSMark Fasheh 	 * journal_dirty erroring as it won't unless we've aborted the
642ccd979bdSMark Fasheh 	 * handle (in which case we would never be here) so reserving
643ccd979bdSMark Fasheh 	 * the write with journal_access is all we need to do. */
644328d5752SMark Fasheh 	status = ocfs2_journal_access(handle, inode, *last_eb_bh,
645ccd979bdSMark Fasheh 				      OCFS2_JOURNAL_ACCESS_WRITE);
646ccd979bdSMark Fasheh 	if (status < 0) {
647ccd979bdSMark Fasheh 		mlog_errno(status);
648ccd979bdSMark Fasheh 		goto bail;
649ccd979bdSMark Fasheh 	}
650ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle, inode, fe_bh,
651ccd979bdSMark Fasheh 				      OCFS2_JOURNAL_ACCESS_WRITE);
652ccd979bdSMark Fasheh 	if (status < 0) {
653ccd979bdSMark Fasheh 		mlog_errno(status);
654ccd979bdSMark Fasheh 		goto bail;
655ccd979bdSMark Fasheh 	}
656ccd979bdSMark Fasheh 	if (eb_bh) {
657ccd979bdSMark Fasheh 		status = ocfs2_journal_access(handle, inode, eb_bh,
658ccd979bdSMark Fasheh 					      OCFS2_JOURNAL_ACCESS_WRITE);
659ccd979bdSMark Fasheh 		if (status < 0) {
660ccd979bdSMark Fasheh 			mlog_errno(status);
661ccd979bdSMark Fasheh 			goto bail;
662ccd979bdSMark Fasheh 		}
663ccd979bdSMark Fasheh 	}
664ccd979bdSMark Fasheh 
665ccd979bdSMark Fasheh 	/* Link the new branch into the rest of the tree (el will
666ccd979bdSMark Fasheh 	 * either be on the fe, or the extent block passed in. */
667ccd979bdSMark Fasheh 	i = le16_to_cpu(el->l_next_free_rec);
668ccd979bdSMark Fasheh 	el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
669dcd0538fSMark Fasheh 	el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
670e48edee2SMark Fasheh 	el->l_recs[i].e_int_clusters = 0;
671ccd979bdSMark Fasheh 	le16_add_cpu(&el->l_next_free_rec, 1);
672ccd979bdSMark Fasheh 
673ccd979bdSMark Fasheh 	/* fe needs a new last extent block pointer, as does the
674ccd979bdSMark Fasheh 	 * next_leaf on the previously last-extent-block. */
675ccd979bdSMark Fasheh 	fe->i_last_eb_blk = cpu_to_le64(new_last_eb_blk);
676ccd979bdSMark Fasheh 
677328d5752SMark Fasheh 	eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
678ccd979bdSMark Fasheh 	eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
679ccd979bdSMark Fasheh 
680328d5752SMark Fasheh 	status = ocfs2_journal_dirty(handle, *last_eb_bh);
681ccd979bdSMark Fasheh 	if (status < 0)
682ccd979bdSMark Fasheh 		mlog_errno(status);
683ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle, fe_bh);
684ccd979bdSMark Fasheh 	if (status < 0)
685ccd979bdSMark Fasheh 		mlog_errno(status);
686ccd979bdSMark Fasheh 	if (eb_bh) {
687ccd979bdSMark Fasheh 		status = ocfs2_journal_dirty(handle, eb_bh);
688ccd979bdSMark Fasheh 		if (status < 0)
689ccd979bdSMark Fasheh 			mlog_errno(status);
690ccd979bdSMark Fasheh 	}
691ccd979bdSMark Fasheh 
692328d5752SMark Fasheh 	/*
693328d5752SMark Fasheh 	 * Some callers want to track the rightmost leaf so pass it
694328d5752SMark Fasheh 	 * back here.
695328d5752SMark Fasheh 	 */
696328d5752SMark Fasheh 	brelse(*last_eb_bh);
697328d5752SMark Fasheh 	get_bh(new_eb_bhs[0]);
698328d5752SMark Fasheh 	*last_eb_bh = new_eb_bhs[0];
699328d5752SMark Fasheh 
700ccd979bdSMark Fasheh 	status = 0;
701ccd979bdSMark Fasheh bail:
702ccd979bdSMark Fasheh 	if (new_eb_bhs) {
703ccd979bdSMark Fasheh 		for (i = 0; i < new_blocks; i++)
704ccd979bdSMark Fasheh 			if (new_eb_bhs[i])
705ccd979bdSMark Fasheh 				brelse(new_eb_bhs[i]);
706ccd979bdSMark Fasheh 		kfree(new_eb_bhs);
707ccd979bdSMark Fasheh 	}
708ccd979bdSMark Fasheh 
709ccd979bdSMark Fasheh 	mlog_exit(status);
710ccd979bdSMark Fasheh 	return status;
711ccd979bdSMark Fasheh }
712ccd979bdSMark Fasheh 
713ccd979bdSMark Fasheh /*
714ccd979bdSMark Fasheh  * adds another level to the allocation tree.
715ccd979bdSMark Fasheh  * returns back the new extent block so you can add a branch to it
716ccd979bdSMark Fasheh  * after this call.
717ccd979bdSMark Fasheh  */
718ccd979bdSMark Fasheh static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
7191fabe148SMark Fasheh 				  handle_t *handle,
720ccd979bdSMark Fasheh 				  struct inode *inode,
721ccd979bdSMark Fasheh 				  struct buffer_head *fe_bh,
722ccd979bdSMark Fasheh 				  struct ocfs2_alloc_context *meta_ac,
723ccd979bdSMark Fasheh 				  struct buffer_head **ret_new_eb_bh)
724ccd979bdSMark Fasheh {
725ccd979bdSMark Fasheh 	int status, i;
726dcd0538fSMark Fasheh 	u32 new_clusters;
727ccd979bdSMark Fasheh 	struct buffer_head *new_eb_bh = NULL;
728ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe;
729ccd979bdSMark Fasheh 	struct ocfs2_extent_block *eb;
730ccd979bdSMark Fasheh 	struct ocfs2_extent_list  *fe_el;
731ccd979bdSMark Fasheh 	struct ocfs2_extent_list  *eb_el;
732ccd979bdSMark Fasheh 
733ccd979bdSMark Fasheh 	mlog_entry_void();
734ccd979bdSMark Fasheh 
735ccd979bdSMark Fasheh 	status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac,
736ccd979bdSMark Fasheh 					   &new_eb_bh);
737ccd979bdSMark Fasheh 	if (status < 0) {
738ccd979bdSMark Fasheh 		mlog_errno(status);
739ccd979bdSMark Fasheh 		goto bail;
740ccd979bdSMark Fasheh 	}
741ccd979bdSMark Fasheh 
742ccd979bdSMark Fasheh 	eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
743ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
744ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
745ccd979bdSMark Fasheh 		status = -EIO;
746ccd979bdSMark Fasheh 		goto bail;
747ccd979bdSMark Fasheh 	}
748ccd979bdSMark Fasheh 
749ccd979bdSMark Fasheh 	eb_el = &eb->h_list;
750ccd979bdSMark Fasheh 	fe = (struct ocfs2_dinode *) fe_bh->b_data;
751ccd979bdSMark Fasheh 	fe_el = &fe->id2.i_list;
752ccd979bdSMark Fasheh 
753ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle, inode, new_eb_bh,
754ccd979bdSMark Fasheh 				      OCFS2_JOURNAL_ACCESS_CREATE);
755ccd979bdSMark Fasheh 	if (status < 0) {
756ccd979bdSMark Fasheh 		mlog_errno(status);
757ccd979bdSMark Fasheh 		goto bail;
758ccd979bdSMark Fasheh 	}
759ccd979bdSMark Fasheh 
760ccd979bdSMark Fasheh 	/* copy the fe data into the new extent block */
761ccd979bdSMark Fasheh 	eb_el->l_tree_depth = fe_el->l_tree_depth;
762ccd979bdSMark Fasheh 	eb_el->l_next_free_rec = fe_el->l_next_free_rec;
763e48edee2SMark Fasheh 	for(i = 0; i < le16_to_cpu(fe_el->l_next_free_rec); i++)
764e48edee2SMark Fasheh 		eb_el->l_recs[i] = fe_el->l_recs[i];
765ccd979bdSMark Fasheh 
766ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle, new_eb_bh);
767ccd979bdSMark Fasheh 	if (status < 0) {
768ccd979bdSMark Fasheh 		mlog_errno(status);
769ccd979bdSMark Fasheh 		goto bail;
770ccd979bdSMark Fasheh 	}
771ccd979bdSMark Fasheh 
772ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle, inode, fe_bh,
773ccd979bdSMark Fasheh 				      OCFS2_JOURNAL_ACCESS_WRITE);
774ccd979bdSMark Fasheh 	if (status < 0) {
775ccd979bdSMark Fasheh 		mlog_errno(status);
776ccd979bdSMark Fasheh 		goto bail;
777ccd979bdSMark Fasheh 	}
778ccd979bdSMark Fasheh 
779dcd0538fSMark Fasheh 	new_clusters = ocfs2_sum_rightmost_rec(eb_el);
780dcd0538fSMark Fasheh 
781ccd979bdSMark Fasheh 	/* update fe now */
782ccd979bdSMark Fasheh 	le16_add_cpu(&fe_el->l_tree_depth, 1);
783ccd979bdSMark Fasheh 	fe_el->l_recs[0].e_cpos = 0;
784ccd979bdSMark Fasheh 	fe_el->l_recs[0].e_blkno = eb->h_blkno;
785e48edee2SMark Fasheh 	fe_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
786e48edee2SMark Fasheh 	for(i = 1; i < le16_to_cpu(fe_el->l_next_free_rec); i++)
787e48edee2SMark Fasheh 		memset(&fe_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
788ccd979bdSMark Fasheh 	fe_el->l_next_free_rec = cpu_to_le16(1);
789ccd979bdSMark Fasheh 
790ccd979bdSMark Fasheh 	/* If this is our 1st tree depth shift, then last_eb_blk
791ccd979bdSMark Fasheh 	 * becomes the allocated extent block */
792ccd979bdSMark Fasheh 	if (fe_el->l_tree_depth == cpu_to_le16(1))
793ccd979bdSMark Fasheh 		fe->i_last_eb_blk = eb->h_blkno;
794ccd979bdSMark Fasheh 
795ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle, fe_bh);
796ccd979bdSMark Fasheh 	if (status < 0) {
797ccd979bdSMark Fasheh 		mlog_errno(status);
798ccd979bdSMark Fasheh 		goto bail;
799ccd979bdSMark Fasheh 	}
800ccd979bdSMark Fasheh 
801ccd979bdSMark Fasheh 	*ret_new_eb_bh = new_eb_bh;
802ccd979bdSMark Fasheh 	new_eb_bh = NULL;
803ccd979bdSMark Fasheh 	status = 0;
804ccd979bdSMark Fasheh bail:
805ccd979bdSMark Fasheh 	if (new_eb_bh)
806ccd979bdSMark Fasheh 		brelse(new_eb_bh);
807ccd979bdSMark Fasheh 
808ccd979bdSMark Fasheh 	mlog_exit(status);
809ccd979bdSMark Fasheh 	return status;
810ccd979bdSMark Fasheh }
811ccd979bdSMark Fasheh 
812ccd979bdSMark Fasheh /*
813ccd979bdSMark Fasheh  * Should only be called when there is no space left in any of the
814ccd979bdSMark Fasheh  * leaf nodes. What we want to do is find the lowest tree depth
815ccd979bdSMark Fasheh  * non-leaf extent block with room for new records. There are three
816ccd979bdSMark Fasheh  * valid results of this search:
817ccd979bdSMark Fasheh  *
818ccd979bdSMark Fasheh  * 1) a lowest extent block is found, then we pass it back in
819ccd979bdSMark Fasheh  *    *lowest_eb_bh and return '0'
820ccd979bdSMark Fasheh  *
821ccd979bdSMark Fasheh  * 2) the search fails to find anything, but the dinode has room. We
822ccd979bdSMark Fasheh  *    pass NULL back in *lowest_eb_bh, but still return '0'
823ccd979bdSMark Fasheh  *
824ccd979bdSMark Fasheh  * 3) the search fails to find anything AND the dinode is full, in
825ccd979bdSMark Fasheh  *    which case we return > 0
826ccd979bdSMark Fasheh  *
827ccd979bdSMark Fasheh  * return status < 0 indicates an error.
828ccd979bdSMark Fasheh  */
829ccd979bdSMark Fasheh static int ocfs2_find_branch_target(struct ocfs2_super *osb,
830ccd979bdSMark Fasheh 				    struct inode *inode,
831ccd979bdSMark Fasheh 				    struct buffer_head *fe_bh,
832ccd979bdSMark Fasheh 				    struct buffer_head **target_bh)
833ccd979bdSMark Fasheh {
834ccd979bdSMark Fasheh 	int status = 0, i;
835ccd979bdSMark Fasheh 	u64 blkno;
836ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe;
837ccd979bdSMark Fasheh 	struct ocfs2_extent_block *eb;
838ccd979bdSMark Fasheh 	struct ocfs2_extent_list  *el;
839ccd979bdSMark Fasheh 	struct buffer_head *bh = NULL;
840ccd979bdSMark Fasheh 	struct buffer_head *lowest_bh = NULL;
841ccd979bdSMark Fasheh 
842ccd979bdSMark Fasheh 	mlog_entry_void();
843ccd979bdSMark Fasheh 
844ccd979bdSMark Fasheh 	*target_bh = NULL;
845ccd979bdSMark Fasheh 
846ccd979bdSMark Fasheh 	fe = (struct ocfs2_dinode *) fe_bh->b_data;
847ccd979bdSMark Fasheh 	el = &fe->id2.i_list;
848ccd979bdSMark Fasheh 
849ccd979bdSMark Fasheh 	while(le16_to_cpu(el->l_tree_depth) > 1) {
850ccd979bdSMark Fasheh 		if (le16_to_cpu(el->l_next_free_rec) == 0) {
851b0697053SMark Fasheh 			ocfs2_error(inode->i_sb, "Dinode %llu has empty "
852ccd979bdSMark Fasheh 				    "extent list (next_free_rec == 0)",
853b0697053SMark Fasheh 				    (unsigned long long)OCFS2_I(inode)->ip_blkno);
854ccd979bdSMark Fasheh 			status = -EIO;
855ccd979bdSMark Fasheh 			goto bail;
856ccd979bdSMark Fasheh 		}
857ccd979bdSMark Fasheh 		i = le16_to_cpu(el->l_next_free_rec) - 1;
858ccd979bdSMark Fasheh 		blkno = le64_to_cpu(el->l_recs[i].e_blkno);
859ccd979bdSMark Fasheh 		if (!blkno) {
860b0697053SMark Fasheh 			ocfs2_error(inode->i_sb, "Dinode %llu has extent "
861ccd979bdSMark Fasheh 				    "list where extent # %d has no physical "
862ccd979bdSMark Fasheh 				    "block start",
863b0697053SMark Fasheh 				    (unsigned long long)OCFS2_I(inode)->ip_blkno, i);
864ccd979bdSMark Fasheh 			status = -EIO;
865ccd979bdSMark Fasheh 			goto bail;
866ccd979bdSMark Fasheh 		}
867ccd979bdSMark Fasheh 
868ccd979bdSMark Fasheh 		if (bh) {
869ccd979bdSMark Fasheh 			brelse(bh);
870ccd979bdSMark Fasheh 			bh = NULL;
871ccd979bdSMark Fasheh 		}
872ccd979bdSMark Fasheh 
873ccd979bdSMark Fasheh 		status = ocfs2_read_block(osb, blkno, &bh, OCFS2_BH_CACHED,
874ccd979bdSMark Fasheh 					  inode);
875ccd979bdSMark Fasheh 		if (status < 0) {
876ccd979bdSMark Fasheh 			mlog_errno(status);
877ccd979bdSMark Fasheh 			goto bail;
878ccd979bdSMark Fasheh 		}
879ccd979bdSMark Fasheh 
880ccd979bdSMark Fasheh 		eb = (struct ocfs2_extent_block *) bh->b_data;
881ccd979bdSMark Fasheh 		if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
882ccd979bdSMark Fasheh 			OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
883ccd979bdSMark Fasheh 			status = -EIO;
884ccd979bdSMark Fasheh 			goto bail;
885ccd979bdSMark Fasheh 		}
886ccd979bdSMark Fasheh 		el = &eb->h_list;
887ccd979bdSMark Fasheh 
888ccd979bdSMark Fasheh 		if (le16_to_cpu(el->l_next_free_rec) <
889ccd979bdSMark Fasheh 		    le16_to_cpu(el->l_count)) {
890ccd979bdSMark Fasheh 			if (lowest_bh)
891ccd979bdSMark Fasheh 				brelse(lowest_bh);
892ccd979bdSMark Fasheh 			lowest_bh = bh;
893ccd979bdSMark Fasheh 			get_bh(lowest_bh);
894ccd979bdSMark Fasheh 		}
895ccd979bdSMark Fasheh 	}
896ccd979bdSMark Fasheh 
897ccd979bdSMark Fasheh 	/* If we didn't find one and the fe doesn't have any room,
898ccd979bdSMark Fasheh 	 * then return '1' */
899ccd979bdSMark Fasheh 	if (!lowest_bh
900ccd979bdSMark Fasheh 	    && (fe->id2.i_list.l_next_free_rec == fe->id2.i_list.l_count))
901ccd979bdSMark Fasheh 		status = 1;
902ccd979bdSMark Fasheh 
903ccd979bdSMark Fasheh 	*target_bh = lowest_bh;
904ccd979bdSMark Fasheh bail:
905ccd979bdSMark Fasheh 	if (bh)
906ccd979bdSMark Fasheh 		brelse(bh);
907ccd979bdSMark Fasheh 
908ccd979bdSMark Fasheh 	mlog_exit(status);
909ccd979bdSMark Fasheh 	return status;
910ccd979bdSMark Fasheh }
911ccd979bdSMark Fasheh 
912e48edee2SMark Fasheh /*
913c3afcbb3SMark Fasheh  * Grow a b-tree so that it has more records.
914c3afcbb3SMark Fasheh  *
915c3afcbb3SMark Fasheh  * We might shift the tree depth in which case existing paths should
916c3afcbb3SMark Fasheh  * be considered invalid.
917c3afcbb3SMark Fasheh  *
918c3afcbb3SMark Fasheh  * Tree depth after the grow is returned via *final_depth.
919328d5752SMark Fasheh  *
920328d5752SMark Fasheh  * *last_eb_bh will be updated by ocfs2_add_branch().
921c3afcbb3SMark Fasheh  */
922c3afcbb3SMark Fasheh static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
923c3afcbb3SMark Fasheh 			   struct buffer_head *di_bh, int *final_depth,
924328d5752SMark Fasheh 			   struct buffer_head **last_eb_bh,
925c3afcbb3SMark Fasheh 			   struct ocfs2_alloc_context *meta_ac)
926c3afcbb3SMark Fasheh {
927c3afcbb3SMark Fasheh 	int ret, shift;
928c3afcbb3SMark Fasheh 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
929c3afcbb3SMark Fasheh 	int depth = le16_to_cpu(di->id2.i_list.l_tree_depth);
930c3afcbb3SMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
931c3afcbb3SMark Fasheh 	struct buffer_head *bh = NULL;
932c3afcbb3SMark Fasheh 
933c3afcbb3SMark Fasheh 	BUG_ON(meta_ac == NULL);
934c3afcbb3SMark Fasheh 
935c3afcbb3SMark Fasheh 	shift = ocfs2_find_branch_target(osb, inode, di_bh, &bh);
936c3afcbb3SMark Fasheh 	if (shift < 0) {
937c3afcbb3SMark Fasheh 		ret = shift;
938c3afcbb3SMark Fasheh 		mlog_errno(ret);
939c3afcbb3SMark Fasheh 		goto out;
940c3afcbb3SMark Fasheh 	}
941c3afcbb3SMark Fasheh 
942c3afcbb3SMark Fasheh 	/* We traveled all the way to the bottom of the allocation tree
943c3afcbb3SMark Fasheh 	 * and didn't find room for any more extents - we need to add
944c3afcbb3SMark Fasheh 	 * another tree level */
945c3afcbb3SMark Fasheh 	if (shift) {
946c3afcbb3SMark Fasheh 		BUG_ON(bh);
947c3afcbb3SMark Fasheh 		mlog(0, "need to shift tree depth (current = %d)\n", depth);
948c3afcbb3SMark Fasheh 
949c3afcbb3SMark Fasheh 		/* ocfs2_shift_tree_depth will return us a buffer with
950c3afcbb3SMark Fasheh 		 * the new extent block (so we can pass that to
951c3afcbb3SMark Fasheh 		 * ocfs2_add_branch). */
952c3afcbb3SMark Fasheh 		ret = ocfs2_shift_tree_depth(osb, handle, inode, di_bh,
953c3afcbb3SMark Fasheh 					     meta_ac, &bh);
954c3afcbb3SMark Fasheh 		if (ret < 0) {
955c3afcbb3SMark Fasheh 			mlog_errno(ret);
956c3afcbb3SMark Fasheh 			goto out;
957c3afcbb3SMark Fasheh 		}
958c3afcbb3SMark Fasheh 		depth++;
959328d5752SMark Fasheh 		if (depth == 1) {
960328d5752SMark Fasheh 			/*
961328d5752SMark Fasheh 			 * Special case: we have room now if we shifted from
962328d5752SMark Fasheh 			 * tree_depth 0, so no more work needs to be done.
963328d5752SMark Fasheh 			 *
964328d5752SMark Fasheh 			 * We won't be calling add_branch, so pass
965328d5752SMark Fasheh 			 * back *last_eb_bh as the new leaf. At depth
966328d5752SMark Fasheh 			 * zero, it should always be null so there's
967328d5752SMark Fasheh 			 * no reason to brelse.
968328d5752SMark Fasheh 			 */
969328d5752SMark Fasheh 			BUG_ON(*last_eb_bh);
970328d5752SMark Fasheh 			get_bh(bh);
971328d5752SMark Fasheh 			*last_eb_bh = bh;
972c3afcbb3SMark Fasheh 			goto out;
973c3afcbb3SMark Fasheh 		}
974328d5752SMark Fasheh 	}
975c3afcbb3SMark Fasheh 
976c3afcbb3SMark Fasheh 	/* call ocfs2_add_branch to add the final part of the tree with
977c3afcbb3SMark Fasheh 	 * the new data. */
978c3afcbb3SMark Fasheh 	mlog(0, "add branch. bh = %p\n", bh);
979c3afcbb3SMark Fasheh 	ret = ocfs2_add_branch(osb, handle, inode, di_bh, bh, last_eb_bh,
980c3afcbb3SMark Fasheh 			       meta_ac);
981c3afcbb3SMark Fasheh 	if (ret < 0) {
982c3afcbb3SMark Fasheh 		mlog_errno(ret);
983c3afcbb3SMark Fasheh 		goto out;
984c3afcbb3SMark Fasheh 	}
985c3afcbb3SMark Fasheh 
986c3afcbb3SMark Fasheh out:
987c3afcbb3SMark Fasheh 	if (final_depth)
988c3afcbb3SMark Fasheh 		*final_depth = depth;
989c3afcbb3SMark Fasheh 	brelse(bh);
990c3afcbb3SMark Fasheh 	return ret;
991c3afcbb3SMark Fasheh }
992c3afcbb3SMark Fasheh 
993c3afcbb3SMark Fasheh /*
994dcd0538fSMark Fasheh  * This function will discard the rightmost extent record.
995dcd0538fSMark Fasheh  */
996dcd0538fSMark Fasheh static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
997dcd0538fSMark Fasheh {
998dcd0538fSMark Fasheh 	int next_free = le16_to_cpu(el->l_next_free_rec);
999dcd0538fSMark Fasheh 	int count = le16_to_cpu(el->l_count);
1000dcd0538fSMark Fasheh 	unsigned int num_bytes;
1001dcd0538fSMark Fasheh 
1002dcd0538fSMark Fasheh 	BUG_ON(!next_free);
1003dcd0538fSMark Fasheh 	/* This will cause us to go off the end of our extent list. */
1004dcd0538fSMark Fasheh 	BUG_ON(next_free >= count);
1005dcd0538fSMark Fasheh 
1006dcd0538fSMark Fasheh 	num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1007dcd0538fSMark Fasheh 
1008dcd0538fSMark Fasheh 	memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1009dcd0538fSMark Fasheh }
1010dcd0538fSMark Fasheh 
1011dcd0538fSMark Fasheh static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1012dcd0538fSMark Fasheh 			      struct ocfs2_extent_rec *insert_rec)
1013dcd0538fSMark Fasheh {
1014dcd0538fSMark Fasheh 	int i, insert_index, next_free, has_empty, num_bytes;
1015dcd0538fSMark Fasheh 	u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1016dcd0538fSMark Fasheh 	struct ocfs2_extent_rec *rec;
1017dcd0538fSMark Fasheh 
1018dcd0538fSMark Fasheh 	next_free = le16_to_cpu(el->l_next_free_rec);
1019dcd0538fSMark Fasheh 	has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1020dcd0538fSMark Fasheh 
1021dcd0538fSMark Fasheh 	BUG_ON(!next_free);
1022dcd0538fSMark Fasheh 
1023dcd0538fSMark Fasheh 	/* The tree code before us didn't allow enough room in the leaf. */
1024b1f3550fSJulia Lawall 	BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
1025dcd0538fSMark Fasheh 
1026dcd0538fSMark Fasheh 	/*
1027dcd0538fSMark Fasheh 	 * The easiest way to approach this is to just remove the
1028dcd0538fSMark Fasheh 	 * empty extent and temporarily decrement next_free.
1029dcd0538fSMark Fasheh 	 */
1030dcd0538fSMark Fasheh 	if (has_empty) {
1031dcd0538fSMark Fasheh 		/*
1032dcd0538fSMark Fasheh 		 * If next_free was 1 (only an empty extent), this
1033dcd0538fSMark Fasheh 		 * loop won't execute, which is fine. We still want
1034dcd0538fSMark Fasheh 		 * the decrement above to happen.
1035dcd0538fSMark Fasheh 		 */
1036dcd0538fSMark Fasheh 		for(i = 0; i < (next_free - 1); i++)
1037dcd0538fSMark Fasheh 			el->l_recs[i] = el->l_recs[i+1];
1038dcd0538fSMark Fasheh 
1039dcd0538fSMark Fasheh 		next_free--;
1040dcd0538fSMark Fasheh 	}
1041dcd0538fSMark Fasheh 
1042dcd0538fSMark Fasheh 	/*
1043dcd0538fSMark Fasheh 	 * Figure out what the new record index should be.
1044dcd0538fSMark Fasheh 	 */
1045dcd0538fSMark Fasheh 	for(i = 0; i < next_free; i++) {
1046dcd0538fSMark Fasheh 		rec = &el->l_recs[i];
1047dcd0538fSMark Fasheh 
1048dcd0538fSMark Fasheh 		if (insert_cpos < le32_to_cpu(rec->e_cpos))
1049dcd0538fSMark Fasheh 			break;
1050dcd0538fSMark Fasheh 	}
1051dcd0538fSMark Fasheh 	insert_index = i;
1052dcd0538fSMark Fasheh 
1053dcd0538fSMark Fasheh 	mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1054dcd0538fSMark Fasheh 	     insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1055dcd0538fSMark Fasheh 
1056dcd0538fSMark Fasheh 	BUG_ON(insert_index < 0);
1057dcd0538fSMark Fasheh 	BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1058dcd0538fSMark Fasheh 	BUG_ON(insert_index > next_free);
1059dcd0538fSMark Fasheh 
1060dcd0538fSMark Fasheh 	/*
1061dcd0538fSMark Fasheh 	 * No need to memmove if we're just adding to the tail.
1062dcd0538fSMark Fasheh 	 */
1063dcd0538fSMark Fasheh 	if (insert_index != next_free) {
1064dcd0538fSMark Fasheh 		BUG_ON(next_free >= le16_to_cpu(el->l_count));
1065dcd0538fSMark Fasheh 
1066dcd0538fSMark Fasheh 		num_bytes = next_free - insert_index;
1067dcd0538fSMark Fasheh 		num_bytes *= sizeof(struct ocfs2_extent_rec);
1068dcd0538fSMark Fasheh 		memmove(&el->l_recs[insert_index + 1],
1069dcd0538fSMark Fasheh 			&el->l_recs[insert_index],
1070dcd0538fSMark Fasheh 			num_bytes);
1071dcd0538fSMark Fasheh 	}
1072dcd0538fSMark Fasheh 
1073dcd0538fSMark Fasheh 	/*
1074dcd0538fSMark Fasheh 	 * Either we had an empty extent, and need to re-increment or
1075dcd0538fSMark Fasheh 	 * there was no empty extent on a non full rightmost leaf node,
1076dcd0538fSMark Fasheh 	 * in which case we still need to increment.
1077dcd0538fSMark Fasheh 	 */
1078dcd0538fSMark Fasheh 	next_free++;
1079dcd0538fSMark Fasheh 	el->l_next_free_rec = cpu_to_le16(next_free);
1080dcd0538fSMark Fasheh 	/*
1081dcd0538fSMark Fasheh 	 * Make sure none of the math above just messed up our tree.
1082dcd0538fSMark Fasheh 	 */
1083dcd0538fSMark Fasheh 	BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1084dcd0538fSMark Fasheh 
1085dcd0538fSMark Fasheh 	el->l_recs[insert_index] = *insert_rec;
1086dcd0538fSMark Fasheh 
1087dcd0538fSMark Fasheh }
1088dcd0538fSMark Fasheh 
1089328d5752SMark Fasheh static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1090328d5752SMark Fasheh {
1091328d5752SMark Fasheh 	int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1092328d5752SMark Fasheh 
1093328d5752SMark Fasheh 	BUG_ON(num_recs == 0);
1094328d5752SMark Fasheh 
1095328d5752SMark Fasheh 	if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1096328d5752SMark Fasheh 		num_recs--;
1097328d5752SMark Fasheh 		size = num_recs * sizeof(struct ocfs2_extent_rec);
1098328d5752SMark Fasheh 		memmove(&el->l_recs[0], &el->l_recs[1], size);
1099328d5752SMark Fasheh 		memset(&el->l_recs[num_recs], 0,
1100328d5752SMark Fasheh 		       sizeof(struct ocfs2_extent_rec));
1101328d5752SMark Fasheh 		el->l_next_free_rec = cpu_to_le16(num_recs);
1102328d5752SMark Fasheh 	}
1103328d5752SMark Fasheh }
1104328d5752SMark Fasheh 
1105dcd0538fSMark Fasheh /*
1106dcd0538fSMark Fasheh  * Create an empty extent record .
1107dcd0538fSMark Fasheh  *
1108dcd0538fSMark Fasheh  * l_next_free_rec may be updated.
1109dcd0538fSMark Fasheh  *
1110dcd0538fSMark Fasheh  * If an empty extent already exists do nothing.
1111dcd0538fSMark Fasheh  */
1112dcd0538fSMark Fasheh static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1113dcd0538fSMark Fasheh {
1114dcd0538fSMark Fasheh 	int next_free = le16_to_cpu(el->l_next_free_rec);
1115dcd0538fSMark Fasheh 
1116e48edee2SMark Fasheh 	BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1117e48edee2SMark Fasheh 
1118dcd0538fSMark Fasheh 	if (next_free == 0)
1119dcd0538fSMark Fasheh 		goto set_and_inc;
1120dcd0538fSMark Fasheh 
1121dcd0538fSMark Fasheh 	if (ocfs2_is_empty_extent(&el->l_recs[0]))
1122dcd0538fSMark Fasheh 		return;
1123dcd0538fSMark Fasheh 
1124dcd0538fSMark Fasheh 	mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1125dcd0538fSMark Fasheh 			"Asked to create an empty extent in a full list:\n"
1126dcd0538fSMark Fasheh 			"count = %u, tree depth = %u",
1127dcd0538fSMark Fasheh 			le16_to_cpu(el->l_count),
1128dcd0538fSMark Fasheh 			le16_to_cpu(el->l_tree_depth));
1129dcd0538fSMark Fasheh 
1130dcd0538fSMark Fasheh 	ocfs2_shift_records_right(el);
1131dcd0538fSMark Fasheh 
1132dcd0538fSMark Fasheh set_and_inc:
1133dcd0538fSMark Fasheh 	le16_add_cpu(&el->l_next_free_rec, 1);
1134dcd0538fSMark Fasheh 	memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1135dcd0538fSMark Fasheh }
1136dcd0538fSMark Fasheh 
1137dcd0538fSMark Fasheh /*
1138dcd0538fSMark Fasheh  * For a rotation which involves two leaf nodes, the "root node" is
1139dcd0538fSMark Fasheh  * the lowest level tree node which contains a path to both leafs. This
1140dcd0538fSMark Fasheh  * resulting set of information can be used to form a complete "subtree"
1141dcd0538fSMark Fasheh  *
1142dcd0538fSMark Fasheh  * This function is passed two full paths from the dinode down to a
1143dcd0538fSMark Fasheh  * pair of adjacent leaves. It's task is to figure out which path
1144dcd0538fSMark Fasheh  * index contains the subtree root - this can be the root index itself
1145dcd0538fSMark Fasheh  * in a worst-case rotation.
1146dcd0538fSMark Fasheh  *
1147dcd0538fSMark Fasheh  * The array index of the subtree root is passed back.
1148dcd0538fSMark Fasheh  */
1149dcd0538fSMark Fasheh static int ocfs2_find_subtree_root(struct inode *inode,
1150dcd0538fSMark Fasheh 				   struct ocfs2_path *left,
1151dcd0538fSMark Fasheh 				   struct ocfs2_path *right)
1152dcd0538fSMark Fasheh {
1153dcd0538fSMark Fasheh 	int i = 0;
1154dcd0538fSMark Fasheh 
1155dcd0538fSMark Fasheh 	/*
1156dcd0538fSMark Fasheh 	 * Check that the caller passed in two paths from the same tree.
1157dcd0538fSMark Fasheh 	 */
1158dcd0538fSMark Fasheh 	BUG_ON(path_root_bh(left) != path_root_bh(right));
1159dcd0538fSMark Fasheh 
1160dcd0538fSMark Fasheh 	do {
1161dcd0538fSMark Fasheh 		i++;
1162dcd0538fSMark Fasheh 
1163dcd0538fSMark Fasheh 		/*
1164dcd0538fSMark Fasheh 		 * The caller didn't pass two adjacent paths.
1165dcd0538fSMark Fasheh 		 */
1166dcd0538fSMark Fasheh 		mlog_bug_on_msg(i > left->p_tree_depth,
1167dcd0538fSMark Fasheh 				"Inode %lu, left depth %u, right depth %u\n"
1168dcd0538fSMark Fasheh 				"left leaf blk %llu, right leaf blk %llu\n",
1169dcd0538fSMark Fasheh 				inode->i_ino, left->p_tree_depth,
1170dcd0538fSMark Fasheh 				right->p_tree_depth,
1171dcd0538fSMark Fasheh 				(unsigned long long)path_leaf_bh(left)->b_blocknr,
1172dcd0538fSMark Fasheh 				(unsigned long long)path_leaf_bh(right)->b_blocknr);
1173dcd0538fSMark Fasheh 	} while (left->p_node[i].bh->b_blocknr ==
1174dcd0538fSMark Fasheh 		 right->p_node[i].bh->b_blocknr);
1175dcd0538fSMark Fasheh 
1176dcd0538fSMark Fasheh 	return i - 1;
1177dcd0538fSMark Fasheh }
1178dcd0538fSMark Fasheh 
1179dcd0538fSMark Fasheh typedef void (path_insert_t)(void *, struct buffer_head *);
1180dcd0538fSMark Fasheh 
1181dcd0538fSMark Fasheh /*
1182dcd0538fSMark Fasheh  * Traverse a btree path in search of cpos, starting at root_el.
1183dcd0538fSMark Fasheh  *
1184dcd0538fSMark Fasheh  * This code can be called with a cpos larger than the tree, in which
1185dcd0538fSMark Fasheh  * case it will return the rightmost path.
1186dcd0538fSMark Fasheh  */
1187dcd0538fSMark Fasheh static int __ocfs2_find_path(struct inode *inode,
1188dcd0538fSMark Fasheh 			     struct ocfs2_extent_list *root_el, u32 cpos,
1189dcd0538fSMark Fasheh 			     path_insert_t *func, void *data)
1190dcd0538fSMark Fasheh {
1191dcd0538fSMark Fasheh 	int i, ret = 0;
1192dcd0538fSMark Fasheh 	u32 range;
1193dcd0538fSMark Fasheh 	u64 blkno;
1194dcd0538fSMark Fasheh 	struct buffer_head *bh = NULL;
1195dcd0538fSMark Fasheh 	struct ocfs2_extent_block *eb;
1196dcd0538fSMark Fasheh 	struct ocfs2_extent_list *el;
1197dcd0538fSMark Fasheh 	struct ocfs2_extent_rec *rec;
1198dcd0538fSMark Fasheh 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
1199dcd0538fSMark Fasheh 
1200dcd0538fSMark Fasheh 	el = root_el;
1201dcd0538fSMark Fasheh 	while (el->l_tree_depth) {
1202dcd0538fSMark Fasheh 		if (le16_to_cpu(el->l_next_free_rec) == 0) {
1203dcd0538fSMark Fasheh 			ocfs2_error(inode->i_sb,
1204dcd0538fSMark Fasheh 				    "Inode %llu has empty extent list at "
1205dcd0538fSMark Fasheh 				    "depth %u\n",
1206dcd0538fSMark Fasheh 				    (unsigned long long)oi->ip_blkno,
1207dcd0538fSMark Fasheh 				    le16_to_cpu(el->l_tree_depth));
1208dcd0538fSMark Fasheh 			ret = -EROFS;
1209dcd0538fSMark Fasheh 			goto out;
1210dcd0538fSMark Fasheh 
1211dcd0538fSMark Fasheh 		}
1212dcd0538fSMark Fasheh 
1213dcd0538fSMark Fasheh 		for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1214dcd0538fSMark Fasheh 			rec = &el->l_recs[i];
1215dcd0538fSMark Fasheh 
1216dcd0538fSMark Fasheh 			/*
1217dcd0538fSMark Fasheh 			 * In the case that cpos is off the allocation
1218dcd0538fSMark Fasheh 			 * tree, this should just wind up returning the
1219dcd0538fSMark Fasheh 			 * rightmost record.
1220dcd0538fSMark Fasheh 			 */
1221dcd0538fSMark Fasheh 			range = le32_to_cpu(rec->e_cpos) +
1222e48edee2SMark Fasheh 				ocfs2_rec_clusters(el, rec);
1223dcd0538fSMark Fasheh 			if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1224dcd0538fSMark Fasheh 			    break;
1225dcd0538fSMark Fasheh 		}
1226dcd0538fSMark Fasheh 
1227dcd0538fSMark Fasheh 		blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1228dcd0538fSMark Fasheh 		if (blkno == 0) {
1229dcd0538fSMark Fasheh 			ocfs2_error(inode->i_sb,
1230dcd0538fSMark Fasheh 				    "Inode %llu has bad blkno in extent list "
1231dcd0538fSMark Fasheh 				    "at depth %u (index %d)\n",
1232dcd0538fSMark Fasheh 				    (unsigned long long)oi->ip_blkno,
1233dcd0538fSMark Fasheh 				    le16_to_cpu(el->l_tree_depth), i);
1234dcd0538fSMark Fasheh 			ret = -EROFS;
1235dcd0538fSMark Fasheh 			goto out;
1236dcd0538fSMark Fasheh 		}
1237dcd0538fSMark Fasheh 
1238dcd0538fSMark Fasheh 		brelse(bh);
1239dcd0538fSMark Fasheh 		bh = NULL;
1240dcd0538fSMark Fasheh 		ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
1241dcd0538fSMark Fasheh 				       &bh, OCFS2_BH_CACHED, inode);
1242dcd0538fSMark Fasheh 		if (ret) {
1243dcd0538fSMark Fasheh 			mlog_errno(ret);
1244dcd0538fSMark Fasheh 			goto out;
1245dcd0538fSMark Fasheh 		}
1246dcd0538fSMark Fasheh 
1247dcd0538fSMark Fasheh 		eb = (struct ocfs2_extent_block *) bh->b_data;
1248dcd0538fSMark Fasheh 		el = &eb->h_list;
1249dcd0538fSMark Fasheh 		if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
1250dcd0538fSMark Fasheh 			OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
1251dcd0538fSMark Fasheh 			ret = -EIO;
1252dcd0538fSMark Fasheh 			goto out;
1253dcd0538fSMark Fasheh 		}
1254dcd0538fSMark Fasheh 
1255dcd0538fSMark Fasheh 		if (le16_to_cpu(el->l_next_free_rec) >
1256dcd0538fSMark Fasheh 		    le16_to_cpu(el->l_count)) {
1257dcd0538fSMark Fasheh 			ocfs2_error(inode->i_sb,
1258dcd0538fSMark Fasheh 				    "Inode %llu has bad count in extent list "
1259dcd0538fSMark Fasheh 				    "at block %llu (next free=%u, count=%u)\n",
1260dcd0538fSMark Fasheh 				    (unsigned long long)oi->ip_blkno,
1261dcd0538fSMark Fasheh 				    (unsigned long long)bh->b_blocknr,
1262dcd0538fSMark Fasheh 				    le16_to_cpu(el->l_next_free_rec),
1263dcd0538fSMark Fasheh 				    le16_to_cpu(el->l_count));
1264dcd0538fSMark Fasheh 			ret = -EROFS;
1265dcd0538fSMark Fasheh 			goto out;
1266dcd0538fSMark Fasheh 		}
1267dcd0538fSMark Fasheh 
1268dcd0538fSMark Fasheh 		if (func)
1269dcd0538fSMark Fasheh 			func(data, bh);
1270dcd0538fSMark Fasheh 	}
1271dcd0538fSMark Fasheh 
1272dcd0538fSMark Fasheh out:
1273dcd0538fSMark Fasheh 	/*
1274dcd0538fSMark Fasheh 	 * Catch any trailing bh that the loop didn't handle.
1275dcd0538fSMark Fasheh 	 */
1276dcd0538fSMark Fasheh 	brelse(bh);
1277dcd0538fSMark Fasheh 
1278dcd0538fSMark Fasheh 	return ret;
1279dcd0538fSMark Fasheh }
1280dcd0538fSMark Fasheh 
1281dcd0538fSMark Fasheh /*
1282dcd0538fSMark Fasheh  * Given an initialized path (that is, it has a valid root extent
1283dcd0538fSMark Fasheh  * list), this function will traverse the btree in search of the path
1284dcd0538fSMark Fasheh  * which would contain cpos.
1285dcd0538fSMark Fasheh  *
1286dcd0538fSMark Fasheh  * The path traveled is recorded in the path structure.
1287dcd0538fSMark Fasheh  *
1288dcd0538fSMark Fasheh  * Note that this will not do any comparisons on leaf node extent
1289dcd0538fSMark Fasheh  * records, so it will work fine in the case that we just added a tree
1290dcd0538fSMark Fasheh  * branch.
1291dcd0538fSMark Fasheh  */
1292dcd0538fSMark Fasheh struct find_path_data {
1293dcd0538fSMark Fasheh 	int index;
1294dcd0538fSMark Fasheh 	struct ocfs2_path *path;
1295dcd0538fSMark Fasheh };
1296dcd0538fSMark Fasheh static void find_path_ins(void *data, struct buffer_head *bh)
1297dcd0538fSMark Fasheh {
1298dcd0538fSMark Fasheh 	struct find_path_data *fp = data;
1299dcd0538fSMark Fasheh 
1300dcd0538fSMark Fasheh 	get_bh(bh);
1301dcd0538fSMark Fasheh 	ocfs2_path_insert_eb(fp->path, fp->index, bh);
1302dcd0538fSMark Fasheh 	fp->index++;
1303dcd0538fSMark Fasheh }
1304dcd0538fSMark Fasheh static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path,
1305dcd0538fSMark Fasheh 			   u32 cpos)
1306dcd0538fSMark Fasheh {
1307dcd0538fSMark Fasheh 	struct find_path_data data;
1308dcd0538fSMark Fasheh 
1309dcd0538fSMark Fasheh 	data.index = 1;
1310dcd0538fSMark Fasheh 	data.path = path;
1311dcd0538fSMark Fasheh 	return __ocfs2_find_path(inode, path_root_el(path), cpos,
1312dcd0538fSMark Fasheh 				 find_path_ins, &data);
1313dcd0538fSMark Fasheh }
1314dcd0538fSMark Fasheh 
1315dcd0538fSMark Fasheh static void find_leaf_ins(void *data, struct buffer_head *bh)
1316dcd0538fSMark Fasheh {
1317dcd0538fSMark Fasheh 	struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1318dcd0538fSMark Fasheh 	struct ocfs2_extent_list *el = &eb->h_list;
1319dcd0538fSMark Fasheh 	struct buffer_head **ret = data;
1320dcd0538fSMark Fasheh 
1321dcd0538fSMark Fasheh 	/* We want to retain only the leaf block. */
1322dcd0538fSMark Fasheh 	if (le16_to_cpu(el->l_tree_depth) == 0) {
1323dcd0538fSMark Fasheh 		get_bh(bh);
1324dcd0538fSMark Fasheh 		*ret = bh;
1325dcd0538fSMark Fasheh 	}
1326dcd0538fSMark Fasheh }
1327dcd0538fSMark Fasheh /*
1328dcd0538fSMark Fasheh  * Find the leaf block in the tree which would contain cpos. No
1329dcd0538fSMark Fasheh  * checking of the actual leaf is done.
1330dcd0538fSMark Fasheh  *
1331dcd0538fSMark Fasheh  * Some paths want to call this instead of allocating a path structure
1332dcd0538fSMark Fasheh  * and calling ocfs2_find_path().
1333dcd0538fSMark Fasheh  *
1334dcd0538fSMark Fasheh  * This function doesn't handle non btree extent lists.
1335dcd0538fSMark Fasheh  */
1336363041a5SMark Fasheh int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el,
1337363041a5SMark Fasheh 		    u32 cpos, struct buffer_head **leaf_bh)
1338dcd0538fSMark Fasheh {
1339dcd0538fSMark Fasheh 	int ret;
1340dcd0538fSMark Fasheh 	struct buffer_head *bh = NULL;
1341dcd0538fSMark Fasheh 
1342dcd0538fSMark Fasheh 	ret = __ocfs2_find_path(inode, root_el, cpos, find_leaf_ins, &bh);
1343dcd0538fSMark Fasheh 	if (ret) {
1344dcd0538fSMark Fasheh 		mlog_errno(ret);
1345dcd0538fSMark Fasheh 		goto out;
1346dcd0538fSMark Fasheh 	}
1347dcd0538fSMark Fasheh 
1348dcd0538fSMark Fasheh 	*leaf_bh = bh;
1349dcd0538fSMark Fasheh out:
1350dcd0538fSMark Fasheh 	return ret;
1351dcd0538fSMark Fasheh }
1352dcd0538fSMark Fasheh 
1353dcd0538fSMark Fasheh /*
1354dcd0538fSMark Fasheh  * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1355dcd0538fSMark Fasheh  *
1356dcd0538fSMark Fasheh  * Basically, we've moved stuff around at the bottom of the tree and
1357dcd0538fSMark Fasheh  * we need to fix up the extent records above the changes to reflect
1358dcd0538fSMark Fasheh  * the new changes.
1359dcd0538fSMark Fasheh  *
1360dcd0538fSMark Fasheh  * left_rec: the record on the left.
1361dcd0538fSMark Fasheh  * left_child_el: is the child list pointed to by left_rec
1362dcd0538fSMark Fasheh  * right_rec: the record to the right of left_rec
1363dcd0538fSMark Fasheh  * right_child_el: is the child list pointed to by right_rec
1364dcd0538fSMark Fasheh  *
1365dcd0538fSMark Fasheh  * By definition, this only works on interior nodes.
1366dcd0538fSMark Fasheh  */
1367dcd0538fSMark Fasheh static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1368dcd0538fSMark Fasheh 				  struct ocfs2_extent_list *left_child_el,
1369dcd0538fSMark Fasheh 				  struct ocfs2_extent_rec *right_rec,
1370dcd0538fSMark Fasheh 				  struct ocfs2_extent_list *right_child_el)
1371dcd0538fSMark Fasheh {
1372dcd0538fSMark Fasheh 	u32 left_clusters, right_end;
1373dcd0538fSMark Fasheh 
1374dcd0538fSMark Fasheh 	/*
1375dcd0538fSMark Fasheh 	 * Interior nodes never have holes. Their cpos is the cpos of
1376dcd0538fSMark Fasheh 	 * the leftmost record in their child list. Their cluster
1377dcd0538fSMark Fasheh 	 * count covers the full theoretical range of their child list
1378dcd0538fSMark Fasheh 	 * - the range between their cpos and the cpos of the record
1379dcd0538fSMark Fasheh 	 * immediately to their right.
1380dcd0538fSMark Fasheh 	 */
1381dcd0538fSMark Fasheh 	left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
1382328d5752SMark Fasheh 	if (ocfs2_is_empty_extent(&right_child_el->l_recs[0])) {
1383328d5752SMark Fasheh 		BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1384328d5752SMark Fasheh 		left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1385328d5752SMark Fasheh 	}
1386dcd0538fSMark Fasheh 	left_clusters -= le32_to_cpu(left_rec->e_cpos);
1387e48edee2SMark Fasheh 	left_rec->e_int_clusters = cpu_to_le32(left_clusters);
1388dcd0538fSMark Fasheh 
1389dcd0538fSMark Fasheh 	/*
1390dcd0538fSMark Fasheh 	 * Calculate the rightmost cluster count boundary before
1391e48edee2SMark Fasheh 	 * moving cpos - we will need to adjust clusters after
1392dcd0538fSMark Fasheh 	 * updating e_cpos to keep the same highest cluster count.
1393dcd0538fSMark Fasheh 	 */
1394dcd0538fSMark Fasheh 	right_end = le32_to_cpu(right_rec->e_cpos);
1395e48edee2SMark Fasheh 	right_end += le32_to_cpu(right_rec->e_int_clusters);
1396dcd0538fSMark Fasheh 
1397dcd0538fSMark Fasheh 	right_rec->e_cpos = left_rec->e_cpos;
1398dcd0538fSMark Fasheh 	le32_add_cpu(&right_rec->e_cpos, left_clusters);
1399dcd0538fSMark Fasheh 
1400dcd0538fSMark Fasheh 	right_end -= le32_to_cpu(right_rec->e_cpos);
1401e48edee2SMark Fasheh 	right_rec->e_int_clusters = cpu_to_le32(right_end);
1402dcd0538fSMark Fasheh }
1403dcd0538fSMark Fasheh 
1404dcd0538fSMark Fasheh /*
1405dcd0538fSMark Fasheh  * Adjust the adjacent root node records involved in a
1406dcd0538fSMark Fasheh  * rotation. left_el_blkno is passed in as a key so that we can easily
1407dcd0538fSMark Fasheh  * find it's index in the root list.
1408dcd0538fSMark Fasheh  */
1409dcd0538fSMark Fasheh static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1410dcd0538fSMark Fasheh 				      struct ocfs2_extent_list *left_el,
1411dcd0538fSMark Fasheh 				      struct ocfs2_extent_list *right_el,
1412dcd0538fSMark Fasheh 				      u64 left_el_blkno)
1413dcd0538fSMark Fasheh {
1414dcd0538fSMark Fasheh 	int i;
1415dcd0538fSMark Fasheh 
1416dcd0538fSMark Fasheh 	BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1417dcd0538fSMark Fasheh 	       le16_to_cpu(left_el->l_tree_depth));
1418dcd0538fSMark Fasheh 
1419dcd0538fSMark Fasheh 	for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1420dcd0538fSMark Fasheh 		if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1421dcd0538fSMark Fasheh 			break;
1422dcd0538fSMark Fasheh 	}
1423dcd0538fSMark Fasheh 
1424dcd0538fSMark Fasheh 	/*
1425dcd0538fSMark Fasheh 	 * The path walking code should have never returned a root and
1426dcd0538fSMark Fasheh 	 * two paths which are not adjacent.
1427dcd0538fSMark Fasheh 	 */
1428dcd0538fSMark Fasheh 	BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1429dcd0538fSMark Fasheh 
1430dcd0538fSMark Fasheh 	ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1431dcd0538fSMark Fasheh 				      &root_el->l_recs[i + 1], right_el);
1432dcd0538fSMark Fasheh }
1433dcd0538fSMark Fasheh 
1434dcd0538fSMark Fasheh /*
1435dcd0538fSMark Fasheh  * We've changed a leaf block (in right_path) and need to reflect that
1436dcd0538fSMark Fasheh  * change back up the subtree.
1437dcd0538fSMark Fasheh  *
1438dcd0538fSMark Fasheh  * This happens in multiple places:
1439dcd0538fSMark Fasheh  *   - When we've moved an extent record from the left path leaf to the right
1440dcd0538fSMark Fasheh  *     path leaf to make room for an empty extent in the left path leaf.
1441dcd0538fSMark Fasheh  *   - When our insert into the right path leaf is at the leftmost edge
1442dcd0538fSMark Fasheh  *     and requires an update of the path immediately to it's left. This
1443dcd0538fSMark Fasheh  *     can occur at the end of some types of rotation and appending inserts.
1444677b9752STao Ma  *   - When we've adjusted the last extent record in the left path leaf and the
1445677b9752STao Ma  *     1st extent record in the right path leaf during cross extent block merge.
1446dcd0538fSMark Fasheh  */
1447dcd0538fSMark Fasheh static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
1448dcd0538fSMark Fasheh 				       struct ocfs2_path *left_path,
1449dcd0538fSMark Fasheh 				       struct ocfs2_path *right_path,
1450dcd0538fSMark Fasheh 				       int subtree_index)
1451dcd0538fSMark Fasheh {
1452dcd0538fSMark Fasheh 	int ret, i, idx;
1453dcd0538fSMark Fasheh 	struct ocfs2_extent_list *el, *left_el, *right_el;
1454dcd0538fSMark Fasheh 	struct ocfs2_extent_rec *left_rec, *right_rec;
1455dcd0538fSMark Fasheh 	struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
1456dcd0538fSMark Fasheh 
1457dcd0538fSMark Fasheh 	/*
1458dcd0538fSMark Fasheh 	 * Update the counts and position values within all the
1459dcd0538fSMark Fasheh 	 * interior nodes to reflect the leaf rotation we just did.
1460dcd0538fSMark Fasheh 	 *
1461dcd0538fSMark Fasheh 	 * The root node is handled below the loop.
1462dcd0538fSMark Fasheh 	 *
1463dcd0538fSMark Fasheh 	 * We begin the loop with right_el and left_el pointing to the
1464dcd0538fSMark Fasheh 	 * leaf lists and work our way up.
1465dcd0538fSMark Fasheh 	 *
1466dcd0538fSMark Fasheh 	 * NOTE: within this loop, left_el and right_el always refer
1467dcd0538fSMark Fasheh 	 * to the *child* lists.
1468dcd0538fSMark Fasheh 	 */
1469dcd0538fSMark Fasheh 	left_el = path_leaf_el(left_path);
1470dcd0538fSMark Fasheh 	right_el = path_leaf_el(right_path);
1471dcd0538fSMark Fasheh 	for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
1472dcd0538fSMark Fasheh 		mlog(0, "Adjust records at index %u\n", i);
1473dcd0538fSMark Fasheh 
1474dcd0538fSMark Fasheh 		/*
1475dcd0538fSMark Fasheh 		 * One nice property of knowing that all of these
1476dcd0538fSMark Fasheh 		 * nodes are below the root is that we only deal with
1477dcd0538fSMark Fasheh 		 * the leftmost right node record and the rightmost
1478dcd0538fSMark Fasheh 		 * left node record.
1479dcd0538fSMark Fasheh 		 */
1480dcd0538fSMark Fasheh 		el = left_path->p_node[i].el;
1481dcd0538fSMark Fasheh 		idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
1482dcd0538fSMark Fasheh 		left_rec = &el->l_recs[idx];
1483dcd0538fSMark Fasheh 
1484dcd0538fSMark Fasheh 		el = right_path->p_node[i].el;
1485dcd0538fSMark Fasheh 		right_rec = &el->l_recs[0];
1486dcd0538fSMark Fasheh 
1487dcd0538fSMark Fasheh 		ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
1488dcd0538fSMark Fasheh 					      right_el);
1489dcd0538fSMark Fasheh 
1490dcd0538fSMark Fasheh 		ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
1491dcd0538fSMark Fasheh 		if (ret)
1492dcd0538fSMark Fasheh 			mlog_errno(ret);
1493dcd0538fSMark Fasheh 
1494dcd0538fSMark Fasheh 		ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
1495dcd0538fSMark Fasheh 		if (ret)
1496dcd0538fSMark Fasheh 			mlog_errno(ret);
1497dcd0538fSMark Fasheh 
1498dcd0538fSMark Fasheh 		/*
1499dcd0538fSMark Fasheh 		 * Setup our list pointers now so that the current
1500dcd0538fSMark Fasheh 		 * parents become children in the next iteration.
1501dcd0538fSMark Fasheh 		 */
1502dcd0538fSMark Fasheh 		left_el = left_path->p_node[i].el;
1503dcd0538fSMark Fasheh 		right_el = right_path->p_node[i].el;
1504dcd0538fSMark Fasheh 	}
1505dcd0538fSMark Fasheh 
1506dcd0538fSMark Fasheh 	/*
1507dcd0538fSMark Fasheh 	 * At the root node, adjust the two adjacent records which
1508dcd0538fSMark Fasheh 	 * begin our path to the leaves.
1509dcd0538fSMark Fasheh 	 */
1510dcd0538fSMark Fasheh 
1511dcd0538fSMark Fasheh 	el = left_path->p_node[subtree_index].el;
1512dcd0538fSMark Fasheh 	left_el = left_path->p_node[subtree_index + 1].el;
1513dcd0538fSMark Fasheh 	right_el = right_path->p_node[subtree_index + 1].el;
1514dcd0538fSMark Fasheh 
1515dcd0538fSMark Fasheh 	ocfs2_adjust_root_records(el, left_el, right_el,
1516dcd0538fSMark Fasheh 				  left_path->p_node[subtree_index + 1].bh->b_blocknr);
1517dcd0538fSMark Fasheh 
1518dcd0538fSMark Fasheh 	root_bh = left_path->p_node[subtree_index].bh;
1519dcd0538fSMark Fasheh 
1520dcd0538fSMark Fasheh 	ret = ocfs2_journal_dirty(handle, root_bh);
1521dcd0538fSMark Fasheh 	if (ret)
1522dcd0538fSMark Fasheh 		mlog_errno(ret);
1523dcd0538fSMark Fasheh }
1524dcd0538fSMark Fasheh 
1525dcd0538fSMark Fasheh static int ocfs2_rotate_subtree_right(struct inode *inode,
1526dcd0538fSMark Fasheh 				      handle_t *handle,
1527dcd0538fSMark Fasheh 				      struct ocfs2_path *left_path,
1528dcd0538fSMark Fasheh 				      struct ocfs2_path *right_path,
1529dcd0538fSMark Fasheh 				      int subtree_index)
1530dcd0538fSMark Fasheh {
1531dcd0538fSMark Fasheh 	int ret, i;
1532dcd0538fSMark Fasheh 	struct buffer_head *right_leaf_bh;
1533dcd0538fSMark Fasheh 	struct buffer_head *left_leaf_bh = NULL;
1534dcd0538fSMark Fasheh 	struct buffer_head *root_bh;
1535dcd0538fSMark Fasheh 	struct ocfs2_extent_list *right_el, *left_el;
1536dcd0538fSMark Fasheh 	struct ocfs2_extent_rec move_rec;
1537dcd0538fSMark Fasheh 
1538dcd0538fSMark Fasheh 	left_leaf_bh = path_leaf_bh(left_path);
1539dcd0538fSMark Fasheh 	left_el = path_leaf_el(left_path);
1540dcd0538fSMark Fasheh 
1541dcd0538fSMark Fasheh 	if (left_el->l_next_free_rec != left_el->l_count) {
1542dcd0538fSMark Fasheh 		ocfs2_error(inode->i_sb,
1543dcd0538fSMark Fasheh 			    "Inode %llu has non-full interior leaf node %llu"
1544dcd0538fSMark Fasheh 			    "(next free = %u)",
1545dcd0538fSMark Fasheh 			    (unsigned long long)OCFS2_I(inode)->ip_blkno,
1546dcd0538fSMark Fasheh 			    (unsigned long long)left_leaf_bh->b_blocknr,
1547dcd0538fSMark Fasheh 			    le16_to_cpu(left_el->l_next_free_rec));
1548dcd0538fSMark Fasheh 		return -EROFS;
1549dcd0538fSMark Fasheh 	}
1550dcd0538fSMark Fasheh 
1551dcd0538fSMark Fasheh 	/*
1552dcd0538fSMark Fasheh 	 * This extent block may already have an empty record, so we
1553dcd0538fSMark Fasheh 	 * return early if so.
1554dcd0538fSMark Fasheh 	 */
1555dcd0538fSMark Fasheh 	if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
1556dcd0538fSMark Fasheh 		return 0;
1557dcd0538fSMark Fasheh 
1558dcd0538fSMark Fasheh 	root_bh = left_path->p_node[subtree_index].bh;
1559dcd0538fSMark Fasheh 	BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
1560dcd0538fSMark Fasheh 
1561dcd0538fSMark Fasheh 	ret = ocfs2_journal_access(handle, inode, root_bh,
1562dcd0538fSMark Fasheh 				   OCFS2_JOURNAL_ACCESS_WRITE);
1563dcd0538fSMark Fasheh 	if (ret) {
1564dcd0538fSMark Fasheh 		mlog_errno(ret);
1565dcd0538fSMark Fasheh 		goto out;
1566dcd0538fSMark Fasheh 	}
1567dcd0538fSMark Fasheh 
1568dcd0538fSMark Fasheh 	for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
1569dcd0538fSMark Fasheh 		ret = ocfs2_journal_access(handle, inode,
1570dcd0538fSMark Fasheh 					   right_path->p_node[i].bh,
1571dcd0538fSMark Fasheh 					   OCFS2_JOURNAL_ACCESS_WRITE);
1572dcd0538fSMark Fasheh 		if (ret) {
1573dcd0538fSMark Fasheh 			mlog_errno(ret);
1574dcd0538fSMark Fasheh 			goto out;
1575dcd0538fSMark Fasheh 		}
1576dcd0538fSMark Fasheh 
1577dcd0538fSMark Fasheh 		ret = ocfs2_journal_access(handle, inode,
1578dcd0538fSMark Fasheh 					   left_path->p_node[i].bh,
1579dcd0538fSMark Fasheh 					   OCFS2_JOURNAL_ACCESS_WRITE);
1580dcd0538fSMark Fasheh 		if (ret) {
1581dcd0538fSMark Fasheh 			mlog_errno(ret);
1582dcd0538fSMark Fasheh 			goto out;
1583dcd0538fSMark Fasheh 		}
1584dcd0538fSMark Fasheh 	}
1585dcd0538fSMark Fasheh 
1586dcd0538fSMark Fasheh 	right_leaf_bh = path_leaf_bh(right_path);
1587dcd0538fSMark Fasheh 	right_el = path_leaf_el(right_path);
1588dcd0538fSMark Fasheh 
1589dcd0538fSMark Fasheh 	/* This is a code error, not a disk corruption. */
1590dcd0538fSMark Fasheh 	mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
1591dcd0538fSMark Fasheh 			"because rightmost leaf block %llu is empty\n",
1592dcd0538fSMark Fasheh 			(unsigned long long)OCFS2_I(inode)->ip_blkno,
1593dcd0538fSMark Fasheh 			(unsigned long long)right_leaf_bh->b_blocknr);
1594dcd0538fSMark Fasheh 
1595dcd0538fSMark Fasheh 	ocfs2_create_empty_extent(right_el);
1596dcd0538fSMark Fasheh 
1597dcd0538fSMark Fasheh 	ret = ocfs2_journal_dirty(handle, right_leaf_bh);
1598dcd0538fSMark Fasheh 	if (ret) {
1599dcd0538fSMark Fasheh 		mlog_errno(ret);
1600dcd0538fSMark Fasheh 		goto out;
1601dcd0538fSMark Fasheh 	}
1602dcd0538fSMark Fasheh 
1603dcd0538fSMark Fasheh 	/* Do the copy now. */
1604dcd0538fSMark Fasheh 	i = le16_to_cpu(left_el->l_next_free_rec) - 1;
1605dcd0538fSMark Fasheh 	move_rec = left_el->l_recs[i];
1606dcd0538fSMark Fasheh 	right_el->l_recs[0] = move_rec;
1607dcd0538fSMark Fasheh 
1608dcd0538fSMark Fasheh 	/*
1609dcd0538fSMark Fasheh 	 * Clear out the record we just copied and shift everything
1610dcd0538fSMark Fasheh 	 * over, leaving an empty extent in the left leaf.
1611dcd0538fSMark Fasheh 	 *
1612dcd0538fSMark Fasheh 	 * We temporarily subtract from next_free_rec so that the
1613dcd0538fSMark Fasheh 	 * shift will lose the tail record (which is now defunct).
1614dcd0538fSMark Fasheh 	 */
1615dcd0538fSMark Fasheh 	le16_add_cpu(&left_el->l_next_free_rec, -1);
1616dcd0538fSMark Fasheh 	ocfs2_shift_records_right(left_el);
1617dcd0538fSMark Fasheh 	memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1618dcd0538fSMark Fasheh 	le16_add_cpu(&left_el->l_next_free_rec, 1);
1619dcd0538fSMark Fasheh 
1620dcd0538fSMark Fasheh 	ret = ocfs2_journal_dirty(handle, left_leaf_bh);
1621dcd0538fSMark Fasheh 	if (ret) {
1622dcd0538fSMark Fasheh 		mlog_errno(ret);
1623dcd0538fSMark Fasheh 		goto out;
1624dcd0538fSMark Fasheh 	}
1625dcd0538fSMark Fasheh 
1626dcd0538fSMark Fasheh 	ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
1627dcd0538fSMark Fasheh 				subtree_index);
1628dcd0538fSMark Fasheh 
1629dcd0538fSMark Fasheh out:
1630dcd0538fSMark Fasheh 	return ret;
1631dcd0538fSMark Fasheh }
1632dcd0538fSMark Fasheh 
1633dcd0538fSMark Fasheh /*
1634dcd0538fSMark Fasheh  * Given a full path, determine what cpos value would return us a path
1635dcd0538fSMark Fasheh  * containing the leaf immediately to the left of the current one.
1636dcd0538fSMark Fasheh  *
1637dcd0538fSMark Fasheh  * Will return zero if the path passed in is already the leftmost path.
1638dcd0538fSMark Fasheh  */
1639dcd0538fSMark Fasheh static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
1640dcd0538fSMark Fasheh 					 struct ocfs2_path *path, u32 *cpos)
1641dcd0538fSMark Fasheh {
1642dcd0538fSMark Fasheh 	int i, j, ret = 0;
1643dcd0538fSMark Fasheh 	u64 blkno;
1644dcd0538fSMark Fasheh 	struct ocfs2_extent_list *el;
1645dcd0538fSMark Fasheh 
1646e48edee2SMark Fasheh 	BUG_ON(path->p_tree_depth == 0);
1647e48edee2SMark Fasheh 
1648dcd0538fSMark Fasheh 	*cpos = 0;
1649dcd0538fSMark Fasheh 
1650dcd0538fSMark Fasheh 	blkno = path_leaf_bh(path)->b_blocknr;
1651dcd0538fSMark Fasheh 
1652dcd0538fSMark Fasheh 	/* Start at the tree node just above the leaf and work our way up. */
1653dcd0538fSMark Fasheh 	i = path->p_tree_depth - 1;
1654dcd0538fSMark Fasheh 	while (i >= 0) {
1655dcd0538fSMark Fasheh 		el = path->p_node[i].el;
1656dcd0538fSMark Fasheh 
1657dcd0538fSMark Fasheh 		/*
1658dcd0538fSMark Fasheh 		 * Find the extent record just before the one in our
1659dcd0538fSMark Fasheh 		 * path.
1660dcd0538fSMark Fasheh 		 */
1661dcd0538fSMark Fasheh 		for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
1662dcd0538fSMark Fasheh 			if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
1663dcd0538fSMark Fasheh 				if (j == 0) {
1664dcd0538fSMark Fasheh 					if (i == 0) {
1665dcd0538fSMark Fasheh 						/*
1666dcd0538fSMark Fasheh 						 * We've determined that the
1667dcd0538fSMark Fasheh 						 * path specified is already
1668dcd0538fSMark Fasheh 						 * the leftmost one - return a
1669dcd0538fSMark Fasheh 						 * cpos of zero.
1670dcd0538fSMark Fasheh 						 */
1671dcd0538fSMark Fasheh 						goto out;
1672dcd0538fSMark Fasheh 					}
1673dcd0538fSMark Fasheh 					/*
1674dcd0538fSMark Fasheh 					 * The leftmost record points to our
1675dcd0538fSMark Fasheh 					 * leaf - we need to travel up the
1676dcd0538fSMark Fasheh 					 * tree one level.
1677dcd0538fSMark Fasheh 					 */
1678dcd0538fSMark Fasheh 					goto next_node;
1679dcd0538fSMark Fasheh 				}
1680dcd0538fSMark Fasheh 
1681dcd0538fSMark Fasheh 				*cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
1682e48edee2SMark Fasheh 				*cpos = *cpos + ocfs2_rec_clusters(el,
1683e48edee2SMark Fasheh 							   &el->l_recs[j - 1]);
1684e48edee2SMark Fasheh 				*cpos = *cpos - 1;
1685dcd0538fSMark Fasheh 				goto out;
1686dcd0538fSMark Fasheh 			}
1687dcd0538fSMark Fasheh 		}
1688dcd0538fSMark Fasheh 
1689dcd0538fSMark Fasheh 		/*
1690dcd0538fSMark Fasheh 		 * If we got here, we never found a valid node where
1691dcd0538fSMark Fasheh 		 * the tree indicated one should be.
1692dcd0538fSMark Fasheh 		 */
1693dcd0538fSMark Fasheh 		ocfs2_error(sb,
1694dcd0538fSMark Fasheh 			    "Invalid extent tree at extent block %llu\n",
1695dcd0538fSMark Fasheh 			    (unsigned long long)blkno);
1696dcd0538fSMark Fasheh 		ret = -EROFS;
1697dcd0538fSMark Fasheh 		goto out;
1698dcd0538fSMark Fasheh 
1699dcd0538fSMark Fasheh next_node:
1700dcd0538fSMark Fasheh 		blkno = path->p_node[i].bh->b_blocknr;
1701dcd0538fSMark Fasheh 		i--;
1702dcd0538fSMark Fasheh 	}
1703dcd0538fSMark Fasheh 
1704dcd0538fSMark Fasheh out:
1705dcd0538fSMark Fasheh 	return ret;
1706dcd0538fSMark Fasheh }
1707dcd0538fSMark Fasheh 
1708328d5752SMark Fasheh /*
1709328d5752SMark Fasheh  * Extend the transaction by enough credits to complete the rotation,
1710328d5752SMark Fasheh  * and still leave at least the original number of credits allocated
1711328d5752SMark Fasheh  * to this transaction.
1712328d5752SMark Fasheh  */
1713dcd0538fSMark Fasheh static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
1714328d5752SMark Fasheh 					   int op_credits,
1715dcd0538fSMark Fasheh 					   struct ocfs2_path *path)
1716dcd0538fSMark Fasheh {
1717328d5752SMark Fasheh 	int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
1718dcd0538fSMark Fasheh 
1719dcd0538fSMark Fasheh 	if (handle->h_buffer_credits < credits)
1720dcd0538fSMark Fasheh 		return ocfs2_extend_trans(handle, credits);
1721dcd0538fSMark Fasheh 
1722dcd0538fSMark Fasheh 	return 0;
1723dcd0538fSMark Fasheh }
1724dcd0538fSMark Fasheh 
1725dcd0538fSMark Fasheh /*
1726dcd0538fSMark Fasheh  * Trap the case where we're inserting into the theoretical range past
1727dcd0538fSMark Fasheh  * the _actual_ left leaf range. Otherwise, we'll rotate a record
1728dcd0538fSMark Fasheh  * whose cpos is less than ours into the right leaf.
1729dcd0538fSMark Fasheh  *
1730dcd0538fSMark Fasheh  * It's only necessary to look at the rightmost record of the left
1731dcd0538fSMark Fasheh  * leaf because the logic that calls us should ensure that the
1732dcd0538fSMark Fasheh  * theoretical ranges in the path components above the leaves are
1733dcd0538fSMark Fasheh  * correct.
1734dcd0538fSMark Fasheh  */
1735dcd0538fSMark Fasheh static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
1736dcd0538fSMark Fasheh 						 u32 insert_cpos)
1737dcd0538fSMark Fasheh {
1738dcd0538fSMark Fasheh 	struct ocfs2_extent_list *left_el;
1739dcd0538fSMark Fasheh 	struct ocfs2_extent_rec *rec;
1740dcd0538fSMark Fasheh 	int next_free;
1741dcd0538fSMark Fasheh 
1742dcd0538fSMark Fasheh 	left_el = path_leaf_el(left_path);
1743dcd0538fSMark Fasheh 	next_free = le16_to_cpu(left_el->l_next_free_rec);
1744dcd0538fSMark Fasheh 	rec = &left_el->l_recs[next_free - 1];
1745dcd0538fSMark Fasheh 
1746dcd0538fSMark Fasheh 	if (insert_cpos > le32_to_cpu(rec->e_cpos))
1747dcd0538fSMark Fasheh 		return 1;
1748dcd0538fSMark Fasheh 	return 0;
1749dcd0538fSMark Fasheh }
1750dcd0538fSMark Fasheh 
1751328d5752SMark Fasheh static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
1752328d5752SMark Fasheh {
1753328d5752SMark Fasheh 	int next_free = le16_to_cpu(el->l_next_free_rec);
1754328d5752SMark Fasheh 	unsigned int range;
1755328d5752SMark Fasheh 	struct ocfs2_extent_rec *rec;
1756328d5752SMark Fasheh 
1757328d5752SMark Fasheh 	if (next_free == 0)
1758328d5752SMark Fasheh 		return 0;
1759328d5752SMark Fasheh 
1760328d5752SMark Fasheh 	rec = &el->l_recs[0];
1761328d5752SMark Fasheh 	if (ocfs2_is_empty_extent(rec)) {
1762328d5752SMark Fasheh 		/* Empty list. */
1763328d5752SMark Fasheh 		if (next_free == 1)
1764328d5752SMark Fasheh 			return 0;
1765328d5752SMark Fasheh 		rec = &el->l_recs[1];
1766328d5752SMark Fasheh 	}
1767328d5752SMark Fasheh 
1768328d5752SMark Fasheh 	range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
1769328d5752SMark Fasheh 	if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1770328d5752SMark Fasheh 		return 1;
1771328d5752SMark Fasheh 	return 0;
1772328d5752SMark Fasheh }
1773328d5752SMark Fasheh 
1774dcd0538fSMark Fasheh /*
1775dcd0538fSMark Fasheh  * Rotate all the records in a btree right one record, starting at insert_cpos.
1776dcd0538fSMark Fasheh  *
1777dcd0538fSMark Fasheh  * The path to the rightmost leaf should be passed in.
1778dcd0538fSMark Fasheh  *
1779dcd0538fSMark Fasheh  * The array is assumed to be large enough to hold an entire path (tree depth).
1780dcd0538fSMark Fasheh  *
1781dcd0538fSMark Fasheh  * Upon succesful return from this function:
1782dcd0538fSMark Fasheh  *
1783dcd0538fSMark Fasheh  * - The 'right_path' array will contain a path to the leaf block
1784dcd0538fSMark Fasheh  *   whose range contains e_cpos.
1785dcd0538fSMark Fasheh  * - That leaf block will have a single empty extent in list index 0.
1786dcd0538fSMark Fasheh  * - In the case that the rotation requires a post-insert update,
1787dcd0538fSMark Fasheh  *   *ret_left_path will contain a valid path which can be passed to
1788dcd0538fSMark Fasheh  *   ocfs2_insert_path().
1789dcd0538fSMark Fasheh  */
1790dcd0538fSMark Fasheh static int ocfs2_rotate_tree_right(struct inode *inode,
1791dcd0538fSMark Fasheh 				   handle_t *handle,
1792328d5752SMark Fasheh 				   enum ocfs2_split_type split,
1793dcd0538fSMark Fasheh 				   u32 insert_cpos,
1794dcd0538fSMark Fasheh 				   struct ocfs2_path *right_path,
1795dcd0538fSMark Fasheh 				   struct ocfs2_path **ret_left_path)
1796dcd0538fSMark Fasheh {
1797328d5752SMark Fasheh 	int ret, start, orig_credits = handle->h_buffer_credits;
1798dcd0538fSMark Fasheh 	u32 cpos;
1799dcd0538fSMark Fasheh 	struct ocfs2_path *left_path = NULL;
1800dcd0538fSMark Fasheh 
1801dcd0538fSMark Fasheh 	*ret_left_path = NULL;
1802dcd0538fSMark Fasheh 
1803dcd0538fSMark Fasheh 	left_path = ocfs2_new_path(path_root_bh(right_path),
1804dcd0538fSMark Fasheh 				   path_root_el(right_path));
1805dcd0538fSMark Fasheh 	if (!left_path) {
1806dcd0538fSMark Fasheh 		ret = -ENOMEM;
1807dcd0538fSMark Fasheh 		mlog_errno(ret);
1808dcd0538fSMark Fasheh 		goto out;
1809dcd0538fSMark Fasheh 	}
1810dcd0538fSMark Fasheh 
1811dcd0538fSMark Fasheh 	ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos);
1812dcd0538fSMark Fasheh 	if (ret) {
1813dcd0538fSMark Fasheh 		mlog_errno(ret);
1814dcd0538fSMark Fasheh 		goto out;
1815dcd0538fSMark Fasheh 	}
1816dcd0538fSMark Fasheh 
1817dcd0538fSMark Fasheh 	mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
1818dcd0538fSMark Fasheh 
1819dcd0538fSMark Fasheh 	/*
1820dcd0538fSMark Fasheh 	 * What we want to do here is:
1821dcd0538fSMark Fasheh 	 *
1822dcd0538fSMark Fasheh 	 * 1) Start with the rightmost path.
1823dcd0538fSMark Fasheh 	 *
1824dcd0538fSMark Fasheh 	 * 2) Determine a path to the leaf block directly to the left
1825dcd0538fSMark Fasheh 	 *    of that leaf.
1826dcd0538fSMark Fasheh 	 *
1827dcd0538fSMark Fasheh 	 * 3) Determine the 'subtree root' - the lowest level tree node
1828dcd0538fSMark Fasheh 	 *    which contains a path to both leaves.
1829dcd0538fSMark Fasheh 	 *
1830dcd0538fSMark Fasheh 	 * 4) Rotate the subtree.
1831dcd0538fSMark Fasheh 	 *
1832dcd0538fSMark Fasheh 	 * 5) Find the next subtree by considering the left path to be
1833dcd0538fSMark Fasheh 	 *    the new right path.
1834dcd0538fSMark Fasheh 	 *
1835dcd0538fSMark Fasheh 	 * The check at the top of this while loop also accepts
1836dcd0538fSMark Fasheh 	 * insert_cpos == cpos because cpos is only a _theoretical_
1837dcd0538fSMark Fasheh 	 * value to get us the left path - insert_cpos might very well
1838dcd0538fSMark Fasheh 	 * be filling that hole.
1839dcd0538fSMark Fasheh 	 *
1840dcd0538fSMark Fasheh 	 * Stop at a cpos of '0' because we either started at the
1841dcd0538fSMark Fasheh 	 * leftmost branch (i.e., a tree with one branch and a
1842dcd0538fSMark Fasheh 	 * rotation inside of it), or we've gone as far as we can in
1843dcd0538fSMark Fasheh 	 * rotating subtrees.
1844dcd0538fSMark Fasheh 	 */
1845dcd0538fSMark Fasheh 	while (cpos && insert_cpos <= cpos) {
1846dcd0538fSMark Fasheh 		mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
1847dcd0538fSMark Fasheh 		     insert_cpos, cpos);
1848dcd0538fSMark Fasheh 
1849dcd0538fSMark Fasheh 		ret = ocfs2_find_path(inode, left_path, cpos);
1850dcd0538fSMark Fasheh 		if (ret) {
1851dcd0538fSMark Fasheh 			mlog_errno(ret);
1852dcd0538fSMark Fasheh 			goto out;
1853dcd0538fSMark Fasheh 		}
1854dcd0538fSMark Fasheh 
1855dcd0538fSMark Fasheh 		mlog_bug_on_msg(path_leaf_bh(left_path) ==
1856dcd0538fSMark Fasheh 				path_leaf_bh(right_path),
1857dcd0538fSMark Fasheh 				"Inode %lu: error during insert of %u "
1858dcd0538fSMark Fasheh 				"(left path cpos %u) results in two identical "
1859dcd0538fSMark Fasheh 				"paths ending at %llu\n",
1860dcd0538fSMark Fasheh 				inode->i_ino, insert_cpos, cpos,
1861dcd0538fSMark Fasheh 				(unsigned long long)
1862dcd0538fSMark Fasheh 				path_leaf_bh(left_path)->b_blocknr);
1863dcd0538fSMark Fasheh 
1864328d5752SMark Fasheh 		if (split == SPLIT_NONE &&
1865328d5752SMark Fasheh 		    ocfs2_rotate_requires_path_adjustment(left_path,
1866dcd0538fSMark Fasheh 							  insert_cpos)) {
1867dcd0538fSMark Fasheh 
1868dcd0538fSMark Fasheh 			/*
1869dcd0538fSMark Fasheh 			 * We've rotated the tree as much as we
1870dcd0538fSMark Fasheh 			 * should. The rest is up to
1871dcd0538fSMark Fasheh 			 * ocfs2_insert_path() to complete, after the
1872dcd0538fSMark Fasheh 			 * record insertion. We indicate this
1873dcd0538fSMark Fasheh 			 * situation by returning the left path.
1874dcd0538fSMark Fasheh 			 *
1875dcd0538fSMark Fasheh 			 * The reason we don't adjust the records here
1876dcd0538fSMark Fasheh 			 * before the record insert is that an error
1877dcd0538fSMark Fasheh 			 * later might break the rule where a parent
1878dcd0538fSMark Fasheh 			 * record e_cpos will reflect the actual
1879dcd0538fSMark Fasheh 			 * e_cpos of the 1st nonempty record of the
1880dcd0538fSMark Fasheh 			 * child list.
1881dcd0538fSMark Fasheh 			 */
1882dcd0538fSMark Fasheh 			*ret_left_path = left_path;
1883dcd0538fSMark Fasheh 			goto out_ret_path;
1884dcd0538fSMark Fasheh 		}
1885dcd0538fSMark Fasheh 
1886dcd0538fSMark Fasheh 		start = ocfs2_find_subtree_root(inode, left_path, right_path);
1887dcd0538fSMark Fasheh 
1888dcd0538fSMark Fasheh 		mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
1889dcd0538fSMark Fasheh 		     start,
1890dcd0538fSMark Fasheh 		     (unsigned long long) right_path->p_node[start].bh->b_blocknr,
1891dcd0538fSMark Fasheh 		     right_path->p_tree_depth);
1892dcd0538fSMark Fasheh 
1893dcd0538fSMark Fasheh 		ret = ocfs2_extend_rotate_transaction(handle, start,
1894328d5752SMark Fasheh 						      orig_credits, right_path);
1895dcd0538fSMark Fasheh 		if (ret) {
1896dcd0538fSMark Fasheh 			mlog_errno(ret);
1897dcd0538fSMark Fasheh 			goto out;
1898dcd0538fSMark Fasheh 		}
1899dcd0538fSMark Fasheh 
1900dcd0538fSMark Fasheh 		ret = ocfs2_rotate_subtree_right(inode, handle, left_path,
1901dcd0538fSMark Fasheh 						 right_path, start);
1902dcd0538fSMark Fasheh 		if (ret) {
1903dcd0538fSMark Fasheh 			mlog_errno(ret);
1904dcd0538fSMark Fasheh 			goto out;
1905dcd0538fSMark Fasheh 		}
1906dcd0538fSMark Fasheh 
1907328d5752SMark Fasheh 		if (split != SPLIT_NONE &&
1908328d5752SMark Fasheh 		    ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
1909328d5752SMark Fasheh 						insert_cpos)) {
1910328d5752SMark Fasheh 			/*
1911328d5752SMark Fasheh 			 * A rotate moves the rightmost left leaf
1912328d5752SMark Fasheh 			 * record over to the leftmost right leaf
1913328d5752SMark Fasheh 			 * slot. If we're doing an extent split
1914328d5752SMark Fasheh 			 * instead of a real insert, then we have to
1915328d5752SMark Fasheh 			 * check that the extent to be split wasn't
1916328d5752SMark Fasheh 			 * just moved over. If it was, then we can
1917328d5752SMark Fasheh 			 * exit here, passing left_path back -
1918328d5752SMark Fasheh 			 * ocfs2_split_extent() is smart enough to
1919328d5752SMark Fasheh 			 * search both leaves.
1920328d5752SMark Fasheh 			 */
1921328d5752SMark Fasheh 			*ret_left_path = left_path;
1922328d5752SMark Fasheh 			goto out_ret_path;
1923328d5752SMark Fasheh 		}
1924328d5752SMark Fasheh 
1925dcd0538fSMark Fasheh 		/*
1926dcd0538fSMark Fasheh 		 * There is no need to re-read the next right path
1927dcd0538fSMark Fasheh 		 * as we know that it'll be our current left
1928dcd0538fSMark Fasheh 		 * path. Optimize by copying values instead.
1929dcd0538fSMark Fasheh 		 */
1930dcd0538fSMark Fasheh 		ocfs2_mv_path(right_path, left_path);
1931dcd0538fSMark Fasheh 
1932dcd0538fSMark Fasheh 		ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
1933dcd0538fSMark Fasheh 						    &cpos);
1934dcd0538fSMark Fasheh 		if (ret) {
1935dcd0538fSMark Fasheh 			mlog_errno(ret);
1936dcd0538fSMark Fasheh 			goto out;
1937dcd0538fSMark Fasheh 		}
1938dcd0538fSMark Fasheh 	}
1939dcd0538fSMark Fasheh 
1940dcd0538fSMark Fasheh out:
1941dcd0538fSMark Fasheh 	ocfs2_free_path(left_path);
1942dcd0538fSMark Fasheh 
1943dcd0538fSMark Fasheh out_ret_path:
1944dcd0538fSMark Fasheh 	return ret;
1945dcd0538fSMark Fasheh }
1946dcd0538fSMark Fasheh 
1947328d5752SMark Fasheh static void ocfs2_update_edge_lengths(struct inode *inode, handle_t *handle,
1948328d5752SMark Fasheh 				      struct ocfs2_path *path)
1949328d5752SMark Fasheh {
1950328d5752SMark Fasheh 	int i, idx;
1951328d5752SMark Fasheh 	struct ocfs2_extent_rec *rec;
1952328d5752SMark Fasheh 	struct ocfs2_extent_list *el;
1953328d5752SMark Fasheh 	struct ocfs2_extent_block *eb;
1954328d5752SMark Fasheh 	u32 range;
1955328d5752SMark Fasheh 
1956328d5752SMark Fasheh 	/* Path should always be rightmost. */
1957328d5752SMark Fasheh 	eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
1958328d5752SMark Fasheh 	BUG_ON(eb->h_next_leaf_blk != 0ULL);
1959328d5752SMark Fasheh 
1960328d5752SMark Fasheh 	el = &eb->h_list;
1961328d5752SMark Fasheh 	BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
1962328d5752SMark Fasheh 	idx = le16_to_cpu(el->l_next_free_rec) - 1;
1963328d5752SMark Fasheh 	rec = &el->l_recs[idx];
1964328d5752SMark Fasheh 	range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
1965328d5752SMark Fasheh 
1966328d5752SMark Fasheh 	for (i = 0; i < path->p_tree_depth; i++) {
1967328d5752SMark Fasheh 		el = path->p_node[i].el;
1968328d5752SMark Fasheh 		idx = le16_to_cpu(el->l_next_free_rec) - 1;
1969328d5752SMark Fasheh 		rec = &el->l_recs[idx];
1970328d5752SMark Fasheh 
1971328d5752SMark Fasheh 		rec->e_int_clusters = cpu_to_le32(range);
1972328d5752SMark Fasheh 		le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
1973328d5752SMark Fasheh 
1974328d5752SMark Fasheh 		ocfs2_journal_dirty(handle, path->p_node[i].bh);
1975328d5752SMark Fasheh 	}
1976328d5752SMark Fasheh }
1977328d5752SMark Fasheh 
1978328d5752SMark Fasheh static void ocfs2_unlink_path(struct inode *inode, handle_t *handle,
1979328d5752SMark Fasheh 			      struct ocfs2_cached_dealloc_ctxt *dealloc,
1980328d5752SMark Fasheh 			      struct ocfs2_path *path, int unlink_start)
1981328d5752SMark Fasheh {
1982328d5752SMark Fasheh 	int ret, i;
1983328d5752SMark Fasheh 	struct ocfs2_extent_block *eb;
1984328d5752SMark Fasheh 	struct ocfs2_extent_list *el;
1985328d5752SMark Fasheh 	struct buffer_head *bh;
1986328d5752SMark Fasheh 
1987328d5752SMark Fasheh 	for(i = unlink_start; i < path_num_items(path); i++) {
1988328d5752SMark Fasheh 		bh = path->p_node[i].bh;
1989328d5752SMark Fasheh 
1990328d5752SMark Fasheh 		eb = (struct ocfs2_extent_block *)bh->b_data;
1991328d5752SMark Fasheh 		/*
1992328d5752SMark Fasheh 		 * Not all nodes might have had their final count
1993328d5752SMark Fasheh 		 * decremented by the caller - handle this here.
1994328d5752SMark Fasheh 		 */
1995328d5752SMark Fasheh 		el = &eb->h_list;
1996328d5752SMark Fasheh 		if (le16_to_cpu(el->l_next_free_rec) > 1) {
1997328d5752SMark Fasheh 			mlog(ML_ERROR,
1998328d5752SMark Fasheh 			     "Inode %llu, attempted to remove extent block "
1999328d5752SMark Fasheh 			     "%llu with %u records\n",
2000328d5752SMark Fasheh 			     (unsigned long long)OCFS2_I(inode)->ip_blkno,
2001328d5752SMark Fasheh 			     (unsigned long long)le64_to_cpu(eb->h_blkno),
2002328d5752SMark Fasheh 			     le16_to_cpu(el->l_next_free_rec));
2003328d5752SMark Fasheh 
2004328d5752SMark Fasheh 			ocfs2_journal_dirty(handle, bh);
2005328d5752SMark Fasheh 			ocfs2_remove_from_cache(inode, bh);
2006328d5752SMark Fasheh 			continue;
2007328d5752SMark Fasheh 		}
2008328d5752SMark Fasheh 
2009328d5752SMark Fasheh 		el->l_next_free_rec = 0;
2010328d5752SMark Fasheh 		memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2011328d5752SMark Fasheh 
2012328d5752SMark Fasheh 		ocfs2_journal_dirty(handle, bh);
2013328d5752SMark Fasheh 
2014328d5752SMark Fasheh 		ret = ocfs2_cache_extent_block_free(dealloc, eb);
2015328d5752SMark Fasheh 		if (ret)
2016328d5752SMark Fasheh 			mlog_errno(ret);
2017328d5752SMark Fasheh 
2018328d5752SMark Fasheh 		ocfs2_remove_from_cache(inode, bh);
2019328d5752SMark Fasheh 	}
2020328d5752SMark Fasheh }
2021328d5752SMark Fasheh 
2022328d5752SMark Fasheh static void ocfs2_unlink_subtree(struct inode *inode, handle_t *handle,
2023328d5752SMark Fasheh 				 struct ocfs2_path *left_path,
2024328d5752SMark Fasheh 				 struct ocfs2_path *right_path,
2025328d5752SMark Fasheh 				 int subtree_index,
2026328d5752SMark Fasheh 				 struct ocfs2_cached_dealloc_ctxt *dealloc)
2027328d5752SMark Fasheh {
2028328d5752SMark Fasheh 	int i;
2029328d5752SMark Fasheh 	struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2030328d5752SMark Fasheh 	struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2031328d5752SMark Fasheh 	struct ocfs2_extent_list *el;
2032328d5752SMark Fasheh 	struct ocfs2_extent_block *eb;
2033328d5752SMark Fasheh 
2034328d5752SMark Fasheh 	el = path_leaf_el(left_path);
2035328d5752SMark Fasheh 
2036328d5752SMark Fasheh 	eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2037328d5752SMark Fasheh 
2038328d5752SMark Fasheh 	for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2039328d5752SMark Fasheh 		if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2040328d5752SMark Fasheh 			break;
2041328d5752SMark Fasheh 
2042328d5752SMark Fasheh 	BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2043328d5752SMark Fasheh 
2044328d5752SMark Fasheh 	memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2045328d5752SMark Fasheh 	le16_add_cpu(&root_el->l_next_free_rec, -1);
2046328d5752SMark Fasheh 
2047328d5752SMark Fasheh 	eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2048328d5752SMark Fasheh 	eb->h_next_leaf_blk = 0;
2049328d5752SMark Fasheh 
2050328d5752SMark Fasheh 	ocfs2_journal_dirty(handle, root_bh);
2051328d5752SMark Fasheh 	ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2052328d5752SMark Fasheh 
2053328d5752SMark Fasheh 	ocfs2_unlink_path(inode, handle, dealloc, right_path,
2054328d5752SMark Fasheh 			  subtree_index + 1);
2055328d5752SMark Fasheh }
2056328d5752SMark Fasheh 
2057328d5752SMark Fasheh static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
2058328d5752SMark Fasheh 				     struct ocfs2_path *left_path,
2059328d5752SMark Fasheh 				     struct ocfs2_path *right_path,
2060328d5752SMark Fasheh 				     int subtree_index,
2061328d5752SMark Fasheh 				     struct ocfs2_cached_dealloc_ctxt *dealloc,
2062328d5752SMark Fasheh 				     int *deleted)
2063328d5752SMark Fasheh {
2064328d5752SMark Fasheh 	int ret, i, del_right_subtree = 0, right_has_empty = 0;
2065328d5752SMark Fasheh 	struct buffer_head *root_bh, *di_bh = path_root_bh(right_path);
2066328d5752SMark Fasheh 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2067328d5752SMark Fasheh 	struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2068328d5752SMark Fasheh 	struct ocfs2_extent_block *eb;
2069328d5752SMark Fasheh 
2070328d5752SMark Fasheh 	*deleted = 0;
2071328d5752SMark Fasheh 
2072328d5752SMark Fasheh 	right_leaf_el = path_leaf_el(right_path);
2073328d5752SMark Fasheh 	left_leaf_el = path_leaf_el(left_path);
2074328d5752SMark Fasheh 	root_bh = left_path->p_node[subtree_index].bh;
2075328d5752SMark Fasheh 	BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2076328d5752SMark Fasheh 
2077328d5752SMark Fasheh 	if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2078328d5752SMark Fasheh 		return 0;
2079328d5752SMark Fasheh 
2080328d5752SMark Fasheh 	eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2081328d5752SMark Fasheh 	if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2082328d5752SMark Fasheh 		/*
2083328d5752SMark Fasheh 		 * It's legal for us to proceed if the right leaf is
2084328d5752SMark Fasheh 		 * the rightmost one and it has an empty extent. There
2085328d5752SMark Fasheh 		 * are two cases to handle - whether the leaf will be
2086328d5752SMark Fasheh 		 * empty after removal or not. If the leaf isn't empty
2087328d5752SMark Fasheh 		 * then just remove the empty extent up front. The
2088328d5752SMark Fasheh 		 * next block will handle empty leaves by flagging
2089328d5752SMark Fasheh 		 * them for unlink.
2090328d5752SMark Fasheh 		 *
2091328d5752SMark Fasheh 		 * Non rightmost leaves will throw -EAGAIN and the
2092328d5752SMark Fasheh 		 * caller can manually move the subtree and retry.
2093328d5752SMark Fasheh 		 */
2094328d5752SMark Fasheh 
2095328d5752SMark Fasheh 		if (eb->h_next_leaf_blk != 0ULL)
2096328d5752SMark Fasheh 			return -EAGAIN;
2097328d5752SMark Fasheh 
2098328d5752SMark Fasheh 		if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
2099328d5752SMark Fasheh 			ret = ocfs2_journal_access(handle, inode,
2100328d5752SMark Fasheh 						   path_leaf_bh(right_path),
2101328d5752SMark Fasheh 						   OCFS2_JOURNAL_ACCESS_WRITE);
2102328d5752SMark Fasheh 			if (ret) {
2103328d5752SMark Fasheh 				mlog_errno(ret);
2104328d5752SMark Fasheh 				goto out;
2105328d5752SMark Fasheh 			}
2106328d5752SMark Fasheh 
2107328d5752SMark Fasheh 			ocfs2_remove_empty_extent(right_leaf_el);
2108328d5752SMark Fasheh 		} else
2109328d5752SMark Fasheh 			right_has_empty = 1;
2110328d5752SMark Fasheh 	}
2111328d5752SMark Fasheh 
2112328d5752SMark Fasheh 	if (eb->h_next_leaf_blk == 0ULL &&
2113328d5752SMark Fasheh 	    le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2114328d5752SMark Fasheh 		/*
2115328d5752SMark Fasheh 		 * We have to update i_last_eb_blk during the meta
2116328d5752SMark Fasheh 		 * data delete.
2117328d5752SMark Fasheh 		 */
2118328d5752SMark Fasheh 		ret = ocfs2_journal_access(handle, inode, di_bh,
2119328d5752SMark Fasheh 					   OCFS2_JOURNAL_ACCESS_WRITE);
2120328d5752SMark Fasheh 		if (ret) {
2121328d5752SMark Fasheh 			mlog_errno(ret);
2122328d5752SMark Fasheh 			goto out;
2123328d5752SMark Fasheh 		}
2124328d5752SMark Fasheh 
2125328d5752SMark Fasheh 		del_right_subtree = 1;
2126328d5752SMark Fasheh 	}
2127328d5752SMark Fasheh 
2128328d5752SMark Fasheh 	/*
2129328d5752SMark Fasheh 	 * Getting here with an empty extent in the right path implies
2130328d5752SMark Fasheh 	 * that it's the rightmost path and will be deleted.
2131328d5752SMark Fasheh 	 */
2132328d5752SMark Fasheh 	BUG_ON(right_has_empty && !del_right_subtree);
2133328d5752SMark Fasheh 
2134328d5752SMark Fasheh 	ret = ocfs2_journal_access(handle, inode, root_bh,
2135328d5752SMark Fasheh 				   OCFS2_JOURNAL_ACCESS_WRITE);
2136328d5752SMark Fasheh 	if (ret) {
2137328d5752SMark Fasheh 		mlog_errno(ret);
2138328d5752SMark Fasheh 		goto out;
2139328d5752SMark Fasheh 	}
2140328d5752SMark Fasheh 
2141328d5752SMark Fasheh 	for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2142328d5752SMark Fasheh 		ret = ocfs2_journal_access(handle, inode,
2143328d5752SMark Fasheh 					   right_path->p_node[i].bh,
2144328d5752SMark Fasheh 					   OCFS2_JOURNAL_ACCESS_WRITE);
2145328d5752SMark Fasheh 		if (ret) {
2146328d5752SMark Fasheh 			mlog_errno(ret);
2147328d5752SMark Fasheh 			goto out;
2148328d5752SMark Fasheh 		}
2149328d5752SMark Fasheh 
2150328d5752SMark Fasheh 		ret = ocfs2_journal_access(handle, inode,
2151328d5752SMark Fasheh 					   left_path->p_node[i].bh,
2152328d5752SMark Fasheh 					   OCFS2_JOURNAL_ACCESS_WRITE);
2153328d5752SMark Fasheh 		if (ret) {
2154328d5752SMark Fasheh 			mlog_errno(ret);
2155328d5752SMark Fasheh 			goto out;
2156328d5752SMark Fasheh 		}
2157328d5752SMark Fasheh 	}
2158328d5752SMark Fasheh 
2159328d5752SMark Fasheh 	if (!right_has_empty) {
2160328d5752SMark Fasheh 		/*
2161328d5752SMark Fasheh 		 * Only do this if we're moving a real
2162328d5752SMark Fasheh 		 * record. Otherwise, the action is delayed until
2163328d5752SMark Fasheh 		 * after removal of the right path in which case we
2164328d5752SMark Fasheh 		 * can do a simple shift to remove the empty extent.
2165328d5752SMark Fasheh 		 */
2166328d5752SMark Fasheh 		ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2167328d5752SMark Fasheh 		memset(&right_leaf_el->l_recs[0], 0,
2168328d5752SMark Fasheh 		       sizeof(struct ocfs2_extent_rec));
2169328d5752SMark Fasheh 	}
2170328d5752SMark Fasheh 	if (eb->h_next_leaf_blk == 0ULL) {
2171328d5752SMark Fasheh 		/*
2172328d5752SMark Fasheh 		 * Move recs over to get rid of empty extent, decrease
2173328d5752SMark Fasheh 		 * next_free. This is allowed to remove the last
2174328d5752SMark Fasheh 		 * extent in our leaf (setting l_next_free_rec to
2175328d5752SMark Fasheh 		 * zero) - the delete code below won't care.
2176328d5752SMark Fasheh 		 */
2177328d5752SMark Fasheh 		ocfs2_remove_empty_extent(right_leaf_el);
2178328d5752SMark Fasheh 	}
2179328d5752SMark Fasheh 
2180328d5752SMark Fasheh 	ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2181328d5752SMark Fasheh 	if (ret)
2182328d5752SMark Fasheh 		mlog_errno(ret);
2183328d5752SMark Fasheh 	ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2184328d5752SMark Fasheh 	if (ret)
2185328d5752SMark Fasheh 		mlog_errno(ret);
2186328d5752SMark Fasheh 
2187328d5752SMark Fasheh 	if (del_right_subtree) {
2188328d5752SMark Fasheh 		ocfs2_unlink_subtree(inode, handle, left_path, right_path,
2189328d5752SMark Fasheh 				     subtree_index, dealloc);
2190328d5752SMark Fasheh 		ocfs2_update_edge_lengths(inode, handle, left_path);
2191328d5752SMark Fasheh 
2192328d5752SMark Fasheh 		eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2193328d5752SMark Fasheh 		di->i_last_eb_blk = eb->h_blkno;
2194328d5752SMark Fasheh 
2195328d5752SMark Fasheh 		/*
2196328d5752SMark Fasheh 		 * Removal of the extent in the left leaf was skipped
2197328d5752SMark Fasheh 		 * above so we could delete the right path
2198328d5752SMark Fasheh 		 * 1st.
2199328d5752SMark Fasheh 		 */
2200328d5752SMark Fasheh 		if (right_has_empty)
2201328d5752SMark Fasheh 			ocfs2_remove_empty_extent(left_leaf_el);
2202328d5752SMark Fasheh 
2203328d5752SMark Fasheh 		ret = ocfs2_journal_dirty(handle, di_bh);
2204328d5752SMark Fasheh 		if (ret)
2205328d5752SMark Fasheh 			mlog_errno(ret);
2206328d5752SMark Fasheh 
2207328d5752SMark Fasheh 		*deleted = 1;
2208328d5752SMark Fasheh 	} else
2209328d5752SMark Fasheh 		ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
2210328d5752SMark Fasheh 					   subtree_index);
2211328d5752SMark Fasheh 
2212328d5752SMark Fasheh out:
2213328d5752SMark Fasheh 	return ret;
2214328d5752SMark Fasheh }
2215328d5752SMark Fasheh 
2216328d5752SMark Fasheh /*
2217328d5752SMark Fasheh  * Given a full path, determine what cpos value would return us a path
2218328d5752SMark Fasheh  * containing the leaf immediately to the right of the current one.
2219328d5752SMark Fasheh  *
2220328d5752SMark Fasheh  * Will return zero if the path passed in is already the rightmost path.
2221328d5752SMark Fasheh  *
2222328d5752SMark Fasheh  * This looks similar, but is subtly different to
2223328d5752SMark Fasheh  * ocfs2_find_cpos_for_left_leaf().
2224328d5752SMark Fasheh  */
2225328d5752SMark Fasheh static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2226328d5752SMark Fasheh 					  struct ocfs2_path *path, u32 *cpos)
2227328d5752SMark Fasheh {
2228328d5752SMark Fasheh 	int i, j, ret = 0;
2229328d5752SMark Fasheh 	u64 blkno;
2230328d5752SMark Fasheh 	struct ocfs2_extent_list *el;
2231328d5752SMark Fasheh 
2232328d5752SMark Fasheh 	*cpos = 0;
2233328d5752SMark Fasheh 
2234328d5752SMark Fasheh 	if (path->p_tree_depth == 0)
2235328d5752SMark Fasheh 		return 0;
2236328d5752SMark Fasheh 
2237328d5752SMark Fasheh 	blkno = path_leaf_bh(path)->b_blocknr;
2238328d5752SMark Fasheh 
2239328d5752SMark Fasheh 	/* Start at the tree node just above the leaf and work our way up. */
2240328d5752SMark Fasheh 	i = path->p_tree_depth - 1;
2241328d5752SMark Fasheh 	while (i >= 0) {
2242328d5752SMark Fasheh 		int next_free;
2243328d5752SMark Fasheh 
2244328d5752SMark Fasheh 		el = path->p_node[i].el;
2245328d5752SMark Fasheh 
2246328d5752SMark Fasheh 		/*
2247328d5752SMark Fasheh 		 * Find the extent record just after the one in our
2248328d5752SMark Fasheh 		 * path.
2249328d5752SMark Fasheh 		 */
2250328d5752SMark Fasheh 		next_free = le16_to_cpu(el->l_next_free_rec);
2251328d5752SMark Fasheh 		for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2252328d5752SMark Fasheh 			if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2253328d5752SMark Fasheh 				if (j == (next_free - 1)) {
2254328d5752SMark Fasheh 					if (i == 0) {
2255328d5752SMark Fasheh 						/*
2256328d5752SMark Fasheh 						 * We've determined that the
2257328d5752SMark Fasheh 						 * path specified is already
2258328d5752SMark Fasheh 						 * the rightmost one - return a
2259328d5752SMark Fasheh 						 * cpos of zero.
2260328d5752SMark Fasheh 						 */
2261328d5752SMark Fasheh 						goto out;
2262328d5752SMark Fasheh 					}
2263328d5752SMark Fasheh 					/*
2264328d5752SMark Fasheh 					 * The rightmost record points to our
2265328d5752SMark Fasheh 					 * leaf - we need to travel up the
2266328d5752SMark Fasheh 					 * tree one level.
2267328d5752SMark Fasheh 					 */
2268328d5752SMark Fasheh 					goto next_node;
2269328d5752SMark Fasheh 				}
2270328d5752SMark Fasheh 
2271328d5752SMark Fasheh 				*cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2272328d5752SMark Fasheh 				goto out;
2273328d5752SMark Fasheh 			}
2274328d5752SMark Fasheh 		}
2275328d5752SMark Fasheh 
2276328d5752SMark Fasheh 		/*
2277328d5752SMark Fasheh 		 * If we got here, we never found a valid node where
2278328d5752SMark Fasheh 		 * the tree indicated one should be.
2279328d5752SMark Fasheh 		 */
2280328d5752SMark Fasheh 		ocfs2_error(sb,
2281328d5752SMark Fasheh 			    "Invalid extent tree at extent block %llu\n",
2282328d5752SMark Fasheh 			    (unsigned long long)blkno);
2283328d5752SMark Fasheh 		ret = -EROFS;
2284328d5752SMark Fasheh 		goto out;
2285328d5752SMark Fasheh 
2286328d5752SMark Fasheh next_node:
2287328d5752SMark Fasheh 		blkno = path->p_node[i].bh->b_blocknr;
2288328d5752SMark Fasheh 		i--;
2289328d5752SMark Fasheh 	}
2290328d5752SMark Fasheh 
2291328d5752SMark Fasheh out:
2292328d5752SMark Fasheh 	return ret;
2293328d5752SMark Fasheh }
2294328d5752SMark Fasheh 
2295328d5752SMark Fasheh static int ocfs2_rotate_rightmost_leaf_left(struct inode *inode,
2296328d5752SMark Fasheh 					    handle_t *handle,
2297328d5752SMark Fasheh 					    struct buffer_head *bh,
2298328d5752SMark Fasheh 					    struct ocfs2_extent_list *el)
2299328d5752SMark Fasheh {
2300328d5752SMark Fasheh 	int ret;
2301328d5752SMark Fasheh 
2302328d5752SMark Fasheh 	if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2303328d5752SMark Fasheh 		return 0;
2304328d5752SMark Fasheh 
2305328d5752SMark Fasheh 	ret = ocfs2_journal_access(handle, inode, bh,
2306328d5752SMark Fasheh 				   OCFS2_JOURNAL_ACCESS_WRITE);
2307328d5752SMark Fasheh 	if (ret) {
2308328d5752SMark Fasheh 		mlog_errno(ret);
2309328d5752SMark Fasheh 		goto out;
2310328d5752SMark Fasheh 	}
2311328d5752SMark Fasheh 
2312328d5752SMark Fasheh 	ocfs2_remove_empty_extent(el);
2313328d5752SMark Fasheh 
2314328d5752SMark Fasheh 	ret = ocfs2_journal_dirty(handle, bh);
2315328d5752SMark Fasheh 	if (ret)
2316328d5752SMark Fasheh 		mlog_errno(ret);
2317328d5752SMark Fasheh 
2318328d5752SMark Fasheh out:
2319328d5752SMark Fasheh 	return ret;
2320328d5752SMark Fasheh }
2321328d5752SMark Fasheh 
2322328d5752SMark Fasheh static int __ocfs2_rotate_tree_left(struct inode *inode,
2323328d5752SMark Fasheh 				    handle_t *handle, int orig_credits,
2324328d5752SMark Fasheh 				    struct ocfs2_path *path,
2325328d5752SMark Fasheh 				    struct ocfs2_cached_dealloc_ctxt *dealloc,
2326328d5752SMark Fasheh 				    struct ocfs2_path **empty_extent_path)
2327328d5752SMark Fasheh {
2328328d5752SMark Fasheh 	int ret, subtree_root, deleted;
2329328d5752SMark Fasheh 	u32 right_cpos;
2330328d5752SMark Fasheh 	struct ocfs2_path *left_path = NULL;
2331328d5752SMark Fasheh 	struct ocfs2_path *right_path = NULL;
2332328d5752SMark Fasheh 
2333328d5752SMark Fasheh 	BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2334328d5752SMark Fasheh 
2335328d5752SMark Fasheh 	*empty_extent_path = NULL;
2336328d5752SMark Fasheh 
2337328d5752SMark Fasheh 	ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, path,
2338328d5752SMark Fasheh 					     &right_cpos);
2339328d5752SMark Fasheh 	if (ret) {
2340328d5752SMark Fasheh 		mlog_errno(ret);
2341328d5752SMark Fasheh 		goto out;
2342328d5752SMark Fasheh 	}
2343328d5752SMark Fasheh 
2344328d5752SMark Fasheh 	left_path = ocfs2_new_path(path_root_bh(path),
2345328d5752SMark Fasheh 				   path_root_el(path));
2346328d5752SMark Fasheh 	if (!left_path) {
2347328d5752SMark Fasheh 		ret = -ENOMEM;
2348328d5752SMark Fasheh 		mlog_errno(ret);
2349328d5752SMark Fasheh 		goto out;
2350328d5752SMark Fasheh 	}
2351328d5752SMark Fasheh 
2352328d5752SMark Fasheh 	ocfs2_cp_path(left_path, path);
2353328d5752SMark Fasheh 
2354328d5752SMark Fasheh 	right_path = ocfs2_new_path(path_root_bh(path),
2355328d5752SMark Fasheh 				    path_root_el(path));
2356328d5752SMark Fasheh 	if (!right_path) {
2357328d5752SMark Fasheh 		ret = -ENOMEM;
2358328d5752SMark Fasheh 		mlog_errno(ret);
2359328d5752SMark Fasheh 		goto out;
2360328d5752SMark Fasheh 	}
2361328d5752SMark Fasheh 
2362328d5752SMark Fasheh 	while (right_cpos) {
2363328d5752SMark Fasheh 		ret = ocfs2_find_path(inode, right_path, right_cpos);
2364328d5752SMark Fasheh 		if (ret) {
2365328d5752SMark Fasheh 			mlog_errno(ret);
2366328d5752SMark Fasheh 			goto out;
2367328d5752SMark Fasheh 		}
2368328d5752SMark Fasheh 
2369328d5752SMark Fasheh 		subtree_root = ocfs2_find_subtree_root(inode, left_path,
2370328d5752SMark Fasheh 						       right_path);
2371328d5752SMark Fasheh 
2372328d5752SMark Fasheh 		mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2373328d5752SMark Fasheh 		     subtree_root,
2374328d5752SMark Fasheh 		     (unsigned long long)
2375328d5752SMark Fasheh 		     right_path->p_node[subtree_root].bh->b_blocknr,
2376328d5752SMark Fasheh 		     right_path->p_tree_depth);
2377328d5752SMark Fasheh 
2378328d5752SMark Fasheh 		ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2379328d5752SMark Fasheh 						      orig_credits, left_path);
2380328d5752SMark Fasheh 		if (ret) {
2381328d5752SMark Fasheh 			mlog_errno(ret);
2382328d5752SMark Fasheh 			goto out;
2383328d5752SMark Fasheh 		}
2384328d5752SMark Fasheh 
2385e8aed345SMark Fasheh 		/*
2386e8aed345SMark Fasheh 		 * Caller might still want to make changes to the
2387e8aed345SMark Fasheh 		 * tree root, so re-add it to the journal here.
2388e8aed345SMark Fasheh 		 */
2389e8aed345SMark Fasheh 		ret = ocfs2_journal_access(handle, inode,
2390e8aed345SMark Fasheh 					   path_root_bh(left_path),
2391e8aed345SMark Fasheh 					   OCFS2_JOURNAL_ACCESS_WRITE);
2392e8aed345SMark Fasheh 		if (ret) {
2393e8aed345SMark Fasheh 			mlog_errno(ret);
2394e8aed345SMark Fasheh 			goto out;
2395e8aed345SMark Fasheh 		}
2396e8aed345SMark Fasheh 
2397328d5752SMark Fasheh 		ret = ocfs2_rotate_subtree_left(inode, handle, left_path,
2398328d5752SMark Fasheh 						right_path, subtree_root,
2399328d5752SMark Fasheh 						dealloc, &deleted);
2400328d5752SMark Fasheh 		if (ret == -EAGAIN) {
2401328d5752SMark Fasheh 			/*
2402328d5752SMark Fasheh 			 * The rotation has to temporarily stop due to
2403328d5752SMark Fasheh 			 * the right subtree having an empty
2404328d5752SMark Fasheh 			 * extent. Pass it back to the caller for a
2405328d5752SMark Fasheh 			 * fixup.
2406328d5752SMark Fasheh 			 */
2407328d5752SMark Fasheh 			*empty_extent_path = right_path;
2408328d5752SMark Fasheh 			right_path = NULL;
2409328d5752SMark Fasheh 			goto out;
2410328d5752SMark Fasheh 		}
2411328d5752SMark Fasheh 		if (ret) {
2412328d5752SMark Fasheh 			mlog_errno(ret);
2413328d5752SMark Fasheh 			goto out;
2414328d5752SMark Fasheh 		}
2415328d5752SMark Fasheh 
2416328d5752SMark Fasheh 		/*
2417328d5752SMark Fasheh 		 * The subtree rotate might have removed records on
2418328d5752SMark Fasheh 		 * the rightmost edge. If so, then rotation is
2419328d5752SMark Fasheh 		 * complete.
2420328d5752SMark Fasheh 		 */
2421328d5752SMark Fasheh 		if (deleted)
2422328d5752SMark Fasheh 			break;
2423328d5752SMark Fasheh 
2424328d5752SMark Fasheh 		ocfs2_mv_path(left_path, right_path);
2425328d5752SMark Fasheh 
2426328d5752SMark Fasheh 		ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
2427328d5752SMark Fasheh 						     &right_cpos);
2428328d5752SMark Fasheh 		if (ret) {
2429328d5752SMark Fasheh 			mlog_errno(ret);
2430328d5752SMark Fasheh 			goto out;
2431328d5752SMark Fasheh 		}
2432328d5752SMark Fasheh 	}
2433328d5752SMark Fasheh 
2434328d5752SMark Fasheh out:
2435328d5752SMark Fasheh 	ocfs2_free_path(right_path);
2436328d5752SMark Fasheh 	ocfs2_free_path(left_path);
2437328d5752SMark Fasheh 
2438328d5752SMark Fasheh 	return ret;
2439328d5752SMark Fasheh }
2440328d5752SMark Fasheh 
2441328d5752SMark Fasheh static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
2442328d5752SMark Fasheh 				       struct ocfs2_path *path,
2443328d5752SMark Fasheh 				       struct ocfs2_cached_dealloc_ctxt *dealloc)
2444328d5752SMark Fasheh {
2445328d5752SMark Fasheh 	int ret, subtree_index;
2446328d5752SMark Fasheh 	u32 cpos;
2447328d5752SMark Fasheh 	struct ocfs2_path *left_path = NULL;
2448328d5752SMark Fasheh 	struct ocfs2_dinode *di;
2449328d5752SMark Fasheh 	struct ocfs2_extent_block *eb;
2450328d5752SMark Fasheh 	struct ocfs2_extent_list *el;
2451328d5752SMark Fasheh 
2452328d5752SMark Fasheh 	/*
2453328d5752SMark Fasheh 	 * XXX: This code assumes that the root is an inode, which is
2454328d5752SMark Fasheh 	 * true for now but may change as tree code gets generic.
2455328d5752SMark Fasheh 	 */
2456328d5752SMark Fasheh 	di = (struct ocfs2_dinode *)path_root_bh(path)->b_data;
2457328d5752SMark Fasheh 	if (!OCFS2_IS_VALID_DINODE(di)) {
2458328d5752SMark Fasheh 		ret = -EIO;
2459328d5752SMark Fasheh 		ocfs2_error(inode->i_sb,
2460328d5752SMark Fasheh 			    "Inode %llu has invalid path root",
2461328d5752SMark Fasheh 			    (unsigned long long)OCFS2_I(inode)->ip_blkno);
2462328d5752SMark Fasheh 		goto out;
2463328d5752SMark Fasheh 	}
2464328d5752SMark Fasheh 
2465328d5752SMark Fasheh 	/*
2466328d5752SMark Fasheh 	 * There's two ways we handle this depending on
2467328d5752SMark Fasheh 	 * whether path is the only existing one.
2468328d5752SMark Fasheh 	 */
2469328d5752SMark Fasheh 	ret = ocfs2_extend_rotate_transaction(handle, 0,
2470328d5752SMark Fasheh 					      handle->h_buffer_credits,
2471328d5752SMark Fasheh 					      path);
2472328d5752SMark Fasheh 	if (ret) {
2473328d5752SMark Fasheh 		mlog_errno(ret);
2474328d5752SMark Fasheh 		goto out;
2475328d5752SMark Fasheh 	}
2476328d5752SMark Fasheh 
2477328d5752SMark Fasheh 	ret = ocfs2_journal_access_path(inode, handle, path);
2478328d5752SMark Fasheh 	if (ret) {
2479328d5752SMark Fasheh 		mlog_errno(ret);
2480328d5752SMark Fasheh 		goto out;
2481328d5752SMark Fasheh 	}
2482328d5752SMark Fasheh 
2483328d5752SMark Fasheh 	ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
2484328d5752SMark Fasheh 	if (ret) {
2485328d5752SMark Fasheh 		mlog_errno(ret);
2486328d5752SMark Fasheh 		goto out;
2487328d5752SMark Fasheh 	}
2488328d5752SMark Fasheh 
2489328d5752SMark Fasheh 	if (cpos) {
2490328d5752SMark Fasheh 		/*
2491328d5752SMark Fasheh 		 * We have a path to the left of this one - it needs
2492328d5752SMark Fasheh 		 * an update too.
2493328d5752SMark Fasheh 		 */
2494328d5752SMark Fasheh 		left_path = ocfs2_new_path(path_root_bh(path),
2495328d5752SMark Fasheh 					   path_root_el(path));
2496328d5752SMark Fasheh 		if (!left_path) {
2497328d5752SMark Fasheh 			ret = -ENOMEM;
2498328d5752SMark Fasheh 			mlog_errno(ret);
2499328d5752SMark Fasheh 			goto out;
2500328d5752SMark Fasheh 		}
2501328d5752SMark Fasheh 
2502328d5752SMark Fasheh 		ret = ocfs2_find_path(inode, left_path, cpos);
2503328d5752SMark Fasheh 		if (ret) {
2504328d5752SMark Fasheh 			mlog_errno(ret);
2505328d5752SMark Fasheh 			goto out;
2506328d5752SMark Fasheh 		}
2507328d5752SMark Fasheh 
2508328d5752SMark Fasheh 		ret = ocfs2_journal_access_path(inode, handle, left_path);
2509328d5752SMark Fasheh 		if (ret) {
2510328d5752SMark Fasheh 			mlog_errno(ret);
2511328d5752SMark Fasheh 			goto out;
2512328d5752SMark Fasheh 		}
2513328d5752SMark Fasheh 
2514328d5752SMark Fasheh 		subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
2515328d5752SMark Fasheh 
2516328d5752SMark Fasheh 		ocfs2_unlink_subtree(inode, handle, left_path, path,
2517328d5752SMark Fasheh 				     subtree_index, dealloc);
2518328d5752SMark Fasheh 		ocfs2_update_edge_lengths(inode, handle, left_path);
2519328d5752SMark Fasheh 
2520328d5752SMark Fasheh 		eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2521328d5752SMark Fasheh 		di->i_last_eb_blk = eb->h_blkno;
2522328d5752SMark Fasheh 	} else {
2523328d5752SMark Fasheh 		/*
2524328d5752SMark Fasheh 		 * 'path' is also the leftmost path which
2525328d5752SMark Fasheh 		 * means it must be the only one. This gets
2526328d5752SMark Fasheh 		 * handled differently because we want to
2527328d5752SMark Fasheh 		 * revert the inode back to having extents
2528328d5752SMark Fasheh 		 * in-line.
2529328d5752SMark Fasheh 		 */
2530328d5752SMark Fasheh 		ocfs2_unlink_path(inode, handle, dealloc, path, 1);
2531328d5752SMark Fasheh 
2532328d5752SMark Fasheh 		el = &di->id2.i_list;
2533328d5752SMark Fasheh 		el->l_tree_depth = 0;
2534328d5752SMark Fasheh 		el->l_next_free_rec = 0;
2535328d5752SMark Fasheh 		memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2536328d5752SMark Fasheh 
2537328d5752SMark Fasheh 		di->i_last_eb_blk = 0;
2538328d5752SMark Fasheh 	}
2539328d5752SMark Fasheh 
2540328d5752SMark Fasheh 	ocfs2_journal_dirty(handle, path_root_bh(path));
2541328d5752SMark Fasheh 
2542328d5752SMark Fasheh out:
2543328d5752SMark Fasheh 	ocfs2_free_path(left_path);
2544328d5752SMark Fasheh 	return ret;
2545328d5752SMark Fasheh }
2546328d5752SMark Fasheh 
2547328d5752SMark Fasheh /*
2548328d5752SMark Fasheh  * Left rotation of btree records.
2549328d5752SMark Fasheh  *
2550328d5752SMark Fasheh  * In many ways, this is (unsurprisingly) the opposite of right
2551328d5752SMark Fasheh  * rotation. We start at some non-rightmost path containing an empty
2552328d5752SMark Fasheh  * extent in the leaf block. The code works its way to the rightmost
2553328d5752SMark Fasheh  * path by rotating records to the left in every subtree.
2554328d5752SMark Fasheh  *
2555328d5752SMark Fasheh  * This is used by any code which reduces the number of extent records
2556328d5752SMark Fasheh  * in a leaf. After removal, an empty record should be placed in the
2557328d5752SMark Fasheh  * leftmost list position.
2558328d5752SMark Fasheh  *
2559328d5752SMark Fasheh  * This won't handle a length update of the rightmost path records if
2560328d5752SMark Fasheh  * the rightmost tree leaf record is removed so the caller is
2561328d5752SMark Fasheh  * responsible for detecting and correcting that.
2562328d5752SMark Fasheh  */
2563328d5752SMark Fasheh static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle,
2564328d5752SMark Fasheh 				  struct ocfs2_path *path,
2565328d5752SMark Fasheh 				  struct ocfs2_cached_dealloc_ctxt *dealloc)
2566328d5752SMark Fasheh {
2567328d5752SMark Fasheh 	int ret, orig_credits = handle->h_buffer_credits;
2568328d5752SMark Fasheh 	struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
2569328d5752SMark Fasheh 	struct ocfs2_extent_block *eb;
2570328d5752SMark Fasheh 	struct ocfs2_extent_list *el;
2571328d5752SMark Fasheh 
2572328d5752SMark Fasheh 	el = path_leaf_el(path);
2573328d5752SMark Fasheh 	if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2574328d5752SMark Fasheh 		return 0;
2575328d5752SMark Fasheh 
2576328d5752SMark Fasheh 	if (path->p_tree_depth == 0) {
2577328d5752SMark Fasheh rightmost_no_delete:
2578328d5752SMark Fasheh 		/*
2579328d5752SMark Fasheh 		 * In-inode extents. This is trivially handled, so do
2580328d5752SMark Fasheh 		 * it up front.
2581328d5752SMark Fasheh 		 */
2582328d5752SMark Fasheh 		ret = ocfs2_rotate_rightmost_leaf_left(inode, handle,
2583328d5752SMark Fasheh 						       path_leaf_bh(path),
2584328d5752SMark Fasheh 						       path_leaf_el(path));
2585328d5752SMark Fasheh 		if (ret)
2586328d5752SMark Fasheh 			mlog_errno(ret);
2587328d5752SMark Fasheh 		goto out;
2588328d5752SMark Fasheh 	}
2589328d5752SMark Fasheh 
2590328d5752SMark Fasheh 	/*
2591328d5752SMark Fasheh 	 * Handle rightmost branch now. There's several cases:
2592328d5752SMark Fasheh 	 *  1) simple rotation leaving records in there. That's trivial.
2593328d5752SMark Fasheh 	 *  2) rotation requiring a branch delete - there's no more
2594328d5752SMark Fasheh 	 *     records left. Two cases of this:
2595328d5752SMark Fasheh 	 *     a) There are branches to the left.
2596328d5752SMark Fasheh 	 *     b) This is also the leftmost (the only) branch.
2597328d5752SMark Fasheh 	 *
2598328d5752SMark Fasheh 	 *  1) is handled via ocfs2_rotate_rightmost_leaf_left()
2599328d5752SMark Fasheh 	 *  2a) we need the left branch so that we can update it with the unlink
2600328d5752SMark Fasheh 	 *  2b) we need to bring the inode back to inline extents.
2601328d5752SMark Fasheh 	 */
2602328d5752SMark Fasheh 
2603328d5752SMark Fasheh 	eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2604328d5752SMark Fasheh 	el = &eb->h_list;
2605328d5752SMark Fasheh 	if (eb->h_next_leaf_blk == 0) {
2606328d5752SMark Fasheh 		/*
2607328d5752SMark Fasheh 		 * This gets a bit tricky if we're going to delete the
2608328d5752SMark Fasheh 		 * rightmost path. Get the other cases out of the way
2609328d5752SMark Fasheh 		 * 1st.
2610328d5752SMark Fasheh 		 */
2611328d5752SMark Fasheh 		if (le16_to_cpu(el->l_next_free_rec) > 1)
2612328d5752SMark Fasheh 			goto rightmost_no_delete;
2613328d5752SMark Fasheh 
2614328d5752SMark Fasheh 		if (le16_to_cpu(el->l_next_free_rec) == 0) {
2615328d5752SMark Fasheh 			ret = -EIO;
2616328d5752SMark Fasheh 			ocfs2_error(inode->i_sb,
2617328d5752SMark Fasheh 				    "Inode %llu has empty extent block at %llu",
2618328d5752SMark Fasheh 				    (unsigned long long)OCFS2_I(inode)->ip_blkno,
2619328d5752SMark Fasheh 				    (unsigned long long)le64_to_cpu(eb->h_blkno));
2620328d5752SMark Fasheh 			goto out;
2621328d5752SMark Fasheh 		}
2622328d5752SMark Fasheh 
2623328d5752SMark Fasheh 		/*
2624328d5752SMark Fasheh 		 * XXX: The caller can not trust "path" any more after
2625328d5752SMark Fasheh 		 * this as it will have been deleted. What do we do?
2626328d5752SMark Fasheh 		 *
2627328d5752SMark Fasheh 		 * In theory the rotate-for-merge code will never get
2628328d5752SMark Fasheh 		 * here because it'll always ask for a rotate in a
2629328d5752SMark Fasheh 		 * nonempty list.
2630328d5752SMark Fasheh 		 */
2631328d5752SMark Fasheh 
2632328d5752SMark Fasheh 		ret = ocfs2_remove_rightmost_path(inode, handle, path,
2633328d5752SMark Fasheh 						  dealloc);
2634328d5752SMark Fasheh 		if (ret)
2635328d5752SMark Fasheh 			mlog_errno(ret);
2636328d5752SMark Fasheh 		goto out;
2637328d5752SMark Fasheh 	}
2638328d5752SMark Fasheh 
2639328d5752SMark Fasheh 	/*
2640328d5752SMark Fasheh 	 * Now we can loop, remembering the path we get from -EAGAIN
2641328d5752SMark Fasheh 	 * and restarting from there.
2642328d5752SMark Fasheh 	 */
2643328d5752SMark Fasheh try_rotate:
2644328d5752SMark Fasheh 	ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path,
2645328d5752SMark Fasheh 				       dealloc, &restart_path);
2646328d5752SMark Fasheh 	if (ret && ret != -EAGAIN) {
2647328d5752SMark Fasheh 		mlog_errno(ret);
2648328d5752SMark Fasheh 		goto out;
2649328d5752SMark Fasheh 	}
2650328d5752SMark Fasheh 
2651328d5752SMark Fasheh 	while (ret == -EAGAIN) {
2652328d5752SMark Fasheh 		tmp_path = restart_path;
2653328d5752SMark Fasheh 		restart_path = NULL;
2654328d5752SMark Fasheh 
2655328d5752SMark Fasheh 		ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits,
2656328d5752SMark Fasheh 					       tmp_path, dealloc,
2657328d5752SMark Fasheh 					       &restart_path);
2658328d5752SMark Fasheh 		if (ret && ret != -EAGAIN) {
2659328d5752SMark Fasheh 			mlog_errno(ret);
2660328d5752SMark Fasheh 			goto out;
2661328d5752SMark Fasheh 		}
2662328d5752SMark Fasheh 
2663328d5752SMark Fasheh 		ocfs2_free_path(tmp_path);
2664328d5752SMark Fasheh 		tmp_path = NULL;
2665328d5752SMark Fasheh 
2666328d5752SMark Fasheh 		if (ret == 0)
2667328d5752SMark Fasheh 			goto try_rotate;
2668328d5752SMark Fasheh 	}
2669328d5752SMark Fasheh 
2670328d5752SMark Fasheh out:
2671328d5752SMark Fasheh 	ocfs2_free_path(tmp_path);
2672328d5752SMark Fasheh 	ocfs2_free_path(restart_path);
2673328d5752SMark Fasheh 	return ret;
2674328d5752SMark Fasheh }
2675328d5752SMark Fasheh 
2676328d5752SMark Fasheh static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
2677328d5752SMark Fasheh 				int index)
2678328d5752SMark Fasheh {
2679328d5752SMark Fasheh 	struct ocfs2_extent_rec *rec = &el->l_recs[index];
2680328d5752SMark Fasheh 	unsigned int size;
2681328d5752SMark Fasheh 
2682328d5752SMark Fasheh 	if (rec->e_leaf_clusters == 0) {
2683328d5752SMark Fasheh 		/*
2684328d5752SMark Fasheh 		 * We consumed all of the merged-from record. An empty
2685328d5752SMark Fasheh 		 * extent cannot exist anywhere but the 1st array
2686328d5752SMark Fasheh 		 * position, so move things over if the merged-from
2687328d5752SMark Fasheh 		 * record doesn't occupy that position.
2688328d5752SMark Fasheh 		 *
2689328d5752SMark Fasheh 		 * This creates a new empty extent so the caller
2690328d5752SMark Fasheh 		 * should be smart enough to have removed any existing
2691328d5752SMark Fasheh 		 * ones.
2692328d5752SMark Fasheh 		 */
2693328d5752SMark Fasheh 		if (index > 0) {
2694328d5752SMark Fasheh 			BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
2695328d5752SMark Fasheh 			size = index * sizeof(struct ocfs2_extent_rec);
2696328d5752SMark Fasheh 			memmove(&el->l_recs[1], &el->l_recs[0], size);
2697328d5752SMark Fasheh 		}
2698328d5752SMark Fasheh 
2699328d5752SMark Fasheh 		/*
2700328d5752SMark Fasheh 		 * Always memset - the caller doesn't check whether it
2701328d5752SMark Fasheh 		 * created an empty extent, so there could be junk in
2702328d5752SMark Fasheh 		 * the other fields.
2703328d5752SMark Fasheh 		 */
2704328d5752SMark Fasheh 		memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2705328d5752SMark Fasheh 	}
2706328d5752SMark Fasheh }
2707328d5752SMark Fasheh 
2708677b9752STao Ma static int ocfs2_get_right_path(struct inode *inode,
2709677b9752STao Ma 				struct ocfs2_path *left_path,
2710677b9752STao Ma 				struct ocfs2_path **ret_right_path)
2711328d5752SMark Fasheh {
2712328d5752SMark Fasheh 	int ret;
2713677b9752STao Ma 	u32 right_cpos;
2714677b9752STao Ma 	struct ocfs2_path *right_path = NULL;
2715677b9752STao Ma 	struct ocfs2_extent_list *left_el;
2716677b9752STao Ma 
2717677b9752STao Ma 	*ret_right_path = NULL;
2718677b9752STao Ma 
2719677b9752STao Ma 	/* This function shouldn't be called for non-trees. */
2720677b9752STao Ma 	BUG_ON(left_path->p_tree_depth == 0);
2721677b9752STao Ma 
2722677b9752STao Ma 	left_el = path_leaf_el(left_path);
2723677b9752STao Ma 	BUG_ON(left_el->l_next_free_rec != left_el->l_count);
2724677b9752STao Ma 
2725677b9752STao Ma 	ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
2726677b9752STao Ma 					     &right_cpos);
2727677b9752STao Ma 	if (ret) {
2728677b9752STao Ma 		mlog_errno(ret);
2729677b9752STao Ma 		goto out;
2730677b9752STao Ma 	}
2731677b9752STao Ma 
2732677b9752STao Ma 	/* This function shouldn't be called for the rightmost leaf. */
2733677b9752STao Ma 	BUG_ON(right_cpos == 0);
2734677b9752STao Ma 
2735677b9752STao Ma 	right_path = ocfs2_new_path(path_root_bh(left_path),
2736677b9752STao Ma 				    path_root_el(left_path));
2737677b9752STao Ma 	if (!right_path) {
2738677b9752STao Ma 		ret = -ENOMEM;
2739677b9752STao Ma 		mlog_errno(ret);
2740677b9752STao Ma 		goto out;
2741677b9752STao Ma 	}
2742677b9752STao Ma 
2743677b9752STao Ma 	ret = ocfs2_find_path(inode, right_path, right_cpos);
2744677b9752STao Ma 	if (ret) {
2745677b9752STao Ma 		mlog_errno(ret);
2746677b9752STao Ma 		goto out;
2747677b9752STao Ma 	}
2748677b9752STao Ma 
2749677b9752STao Ma 	*ret_right_path = right_path;
2750677b9752STao Ma out:
2751677b9752STao Ma 	if (ret)
2752677b9752STao Ma 		ocfs2_free_path(right_path);
2753677b9752STao Ma 	return ret;
2754677b9752STao Ma }
2755677b9752STao Ma 
2756677b9752STao Ma /*
2757677b9752STao Ma  * Remove split_rec clusters from the record at index and merge them
2758677b9752STao Ma  * onto the beginning of the record "next" to it.
2759677b9752STao Ma  * For index < l_count - 1, the next means the extent rec at index + 1.
2760677b9752STao Ma  * For index == l_count - 1, the "next" means the 1st extent rec of the
2761677b9752STao Ma  * next extent block.
2762677b9752STao Ma  */
2763677b9752STao Ma static int ocfs2_merge_rec_right(struct inode *inode,
2764677b9752STao Ma 				 struct ocfs2_path *left_path,
2765677b9752STao Ma 				 handle_t *handle,
2766677b9752STao Ma 				 struct ocfs2_extent_rec *split_rec,
2767677b9752STao Ma 				 int index)
2768677b9752STao Ma {
2769677b9752STao Ma 	int ret, next_free, i;
2770328d5752SMark Fasheh 	unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
2771328d5752SMark Fasheh 	struct ocfs2_extent_rec *left_rec;
2772328d5752SMark Fasheh 	struct ocfs2_extent_rec *right_rec;
2773677b9752STao Ma 	struct ocfs2_extent_list *right_el;
2774677b9752STao Ma 	struct ocfs2_path *right_path = NULL;
2775677b9752STao Ma 	int subtree_index = 0;
2776677b9752STao Ma 	struct ocfs2_extent_list *el = path_leaf_el(left_path);
2777677b9752STao Ma 	struct buffer_head *bh = path_leaf_bh(left_path);
2778677b9752STao Ma 	struct buffer_head *root_bh = NULL;
2779328d5752SMark Fasheh 
2780328d5752SMark Fasheh 	BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
2781328d5752SMark Fasheh 	left_rec = &el->l_recs[index];
2782677b9752STao Ma 
27839d8df6aaSAl Viro 	if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
2784677b9752STao Ma 	    le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
2785677b9752STao Ma 		/* we meet with a cross extent block merge. */
2786677b9752STao Ma 		ret = ocfs2_get_right_path(inode, left_path, &right_path);
2787677b9752STao Ma 		if (ret) {
2788677b9752STao Ma 			mlog_errno(ret);
2789677b9752STao Ma 			goto out;
2790677b9752STao Ma 		}
2791677b9752STao Ma 
2792677b9752STao Ma 		right_el = path_leaf_el(right_path);
2793677b9752STao Ma 		next_free = le16_to_cpu(right_el->l_next_free_rec);
2794677b9752STao Ma 		BUG_ON(next_free <= 0);
2795677b9752STao Ma 		right_rec = &right_el->l_recs[0];
2796677b9752STao Ma 		if (ocfs2_is_empty_extent(right_rec)) {
27979d8df6aaSAl Viro 			BUG_ON(next_free <= 1);
2798677b9752STao Ma 			right_rec = &right_el->l_recs[1];
2799677b9752STao Ma 		}
2800677b9752STao Ma 
2801677b9752STao Ma 		BUG_ON(le32_to_cpu(left_rec->e_cpos) +
2802677b9752STao Ma 		       le16_to_cpu(left_rec->e_leaf_clusters) !=
2803677b9752STao Ma 		       le32_to_cpu(right_rec->e_cpos));
2804677b9752STao Ma 
2805677b9752STao Ma 		subtree_index = ocfs2_find_subtree_root(inode,
2806677b9752STao Ma 							left_path, right_path);
2807677b9752STao Ma 
2808677b9752STao Ma 		ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
2809677b9752STao Ma 						      handle->h_buffer_credits,
2810677b9752STao Ma 						      right_path);
2811677b9752STao Ma 		if (ret) {
2812677b9752STao Ma 			mlog_errno(ret);
2813677b9752STao Ma 			goto out;
2814677b9752STao Ma 		}
2815677b9752STao Ma 
2816677b9752STao Ma 		root_bh = left_path->p_node[subtree_index].bh;
2817677b9752STao Ma 		BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2818677b9752STao Ma 
2819677b9752STao Ma 		ret = ocfs2_journal_access(handle, inode, root_bh,
2820677b9752STao Ma 					   OCFS2_JOURNAL_ACCESS_WRITE);
2821677b9752STao Ma 		if (ret) {
2822677b9752STao Ma 			mlog_errno(ret);
2823677b9752STao Ma 			goto out;
2824677b9752STao Ma 		}
2825677b9752STao Ma 
2826677b9752STao Ma 		for (i = subtree_index + 1;
2827677b9752STao Ma 		     i < path_num_items(right_path); i++) {
2828677b9752STao Ma 			ret = ocfs2_journal_access(handle, inode,
2829677b9752STao Ma 						   right_path->p_node[i].bh,
2830677b9752STao Ma 						   OCFS2_JOURNAL_ACCESS_WRITE);
2831677b9752STao Ma 			if (ret) {
2832677b9752STao Ma 				mlog_errno(ret);
2833677b9752STao Ma 				goto out;
2834677b9752STao Ma 			}
2835677b9752STao Ma 
2836677b9752STao Ma 			ret = ocfs2_journal_access(handle, inode,
2837677b9752STao Ma 						   left_path->p_node[i].bh,
2838677b9752STao Ma 						   OCFS2_JOURNAL_ACCESS_WRITE);
2839677b9752STao Ma 			if (ret) {
2840677b9752STao Ma 				mlog_errno(ret);
2841677b9752STao Ma 				goto out;
2842677b9752STao Ma 			}
2843677b9752STao Ma 		}
2844677b9752STao Ma 
2845677b9752STao Ma 	} else {
2846677b9752STao Ma 		BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
2847328d5752SMark Fasheh 		right_rec = &el->l_recs[index + 1];
2848677b9752STao Ma 	}
2849328d5752SMark Fasheh 
2850328d5752SMark Fasheh 	ret = ocfs2_journal_access(handle, inode, bh,
2851328d5752SMark Fasheh 				   OCFS2_JOURNAL_ACCESS_WRITE);
2852328d5752SMark Fasheh 	if (ret) {
2853328d5752SMark Fasheh 		mlog_errno(ret);
2854328d5752SMark Fasheh 		goto out;
2855328d5752SMark Fasheh 	}
2856328d5752SMark Fasheh 
2857328d5752SMark Fasheh 	le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
2858328d5752SMark Fasheh 
2859328d5752SMark Fasheh 	le32_add_cpu(&right_rec->e_cpos, -split_clusters);
2860328d5752SMark Fasheh 	le64_add_cpu(&right_rec->e_blkno,
2861328d5752SMark Fasheh 		     -ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
2862328d5752SMark Fasheh 	le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
2863328d5752SMark Fasheh 
2864328d5752SMark Fasheh 	ocfs2_cleanup_merge(el, index);
2865328d5752SMark Fasheh 
2866328d5752SMark Fasheh 	ret = ocfs2_journal_dirty(handle, bh);
2867328d5752SMark Fasheh 	if (ret)
2868328d5752SMark Fasheh 		mlog_errno(ret);
2869328d5752SMark Fasheh 
2870677b9752STao Ma 	if (right_path) {
2871677b9752STao Ma 		ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2872677b9752STao Ma 		if (ret)
2873677b9752STao Ma 			mlog_errno(ret);
2874677b9752STao Ma 
2875677b9752STao Ma 		ocfs2_complete_edge_insert(inode, handle, left_path,
2876677b9752STao Ma 					   right_path, subtree_index);
2877677b9752STao Ma 	}
2878328d5752SMark Fasheh out:
2879677b9752STao Ma 	if (right_path)
2880677b9752STao Ma 		ocfs2_free_path(right_path);
2881677b9752STao Ma 	return ret;
2882677b9752STao Ma }
2883677b9752STao Ma 
2884677b9752STao Ma static int ocfs2_get_left_path(struct inode *inode,
2885677b9752STao Ma 			       struct ocfs2_path *right_path,
2886677b9752STao Ma 			       struct ocfs2_path **ret_left_path)
2887677b9752STao Ma {
2888677b9752STao Ma 	int ret;
2889677b9752STao Ma 	u32 left_cpos;
2890677b9752STao Ma 	struct ocfs2_path *left_path = NULL;
2891677b9752STao Ma 
2892677b9752STao Ma 	*ret_left_path = NULL;
2893677b9752STao Ma 
2894677b9752STao Ma 	/* This function shouldn't be called for non-trees. */
2895677b9752STao Ma 	BUG_ON(right_path->p_tree_depth == 0);
2896677b9752STao Ma 
2897677b9752STao Ma 	ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
2898677b9752STao Ma 					    right_path, &left_cpos);
2899677b9752STao Ma 	if (ret) {
2900677b9752STao Ma 		mlog_errno(ret);
2901677b9752STao Ma 		goto out;
2902677b9752STao Ma 	}
2903677b9752STao Ma 
2904677b9752STao Ma 	/* This function shouldn't be called for the leftmost leaf. */
2905677b9752STao Ma 	BUG_ON(left_cpos == 0);
2906677b9752STao Ma 
2907677b9752STao Ma 	left_path = ocfs2_new_path(path_root_bh(right_path),
2908677b9752STao Ma 				   path_root_el(right_path));
2909677b9752STao Ma 	if (!left_path) {
2910677b9752STao Ma 		ret = -ENOMEM;
2911677b9752STao Ma 		mlog_errno(ret);
2912677b9752STao Ma 		goto out;
2913677b9752STao Ma 	}
2914677b9752STao Ma 
2915677b9752STao Ma 	ret = ocfs2_find_path(inode, left_path, left_cpos);
2916677b9752STao Ma 	if (ret) {
2917677b9752STao Ma 		mlog_errno(ret);
2918677b9752STao Ma 		goto out;
2919677b9752STao Ma 	}
2920677b9752STao Ma 
2921677b9752STao Ma 	*ret_left_path = left_path;
2922677b9752STao Ma out:
2923677b9752STao Ma 	if (ret)
2924677b9752STao Ma 		ocfs2_free_path(left_path);
2925328d5752SMark Fasheh 	return ret;
2926328d5752SMark Fasheh }
2927328d5752SMark Fasheh 
2928328d5752SMark Fasheh /*
2929328d5752SMark Fasheh  * Remove split_rec clusters from the record at index and merge them
2930677b9752STao Ma  * onto the tail of the record "before" it.
2931677b9752STao Ma  * For index > 0, the "before" means the extent rec at index - 1.
2932677b9752STao Ma  *
2933677b9752STao Ma  * For index == 0, the "before" means the last record of the previous
2934677b9752STao Ma  * extent block. And there is also a situation that we may need to
2935677b9752STao Ma  * remove the rightmost leaf extent block in the right_path and change
2936677b9752STao Ma  * the right path to indicate the new rightmost path.
2937328d5752SMark Fasheh  */
2938677b9752STao Ma static int ocfs2_merge_rec_left(struct inode *inode,
2939677b9752STao Ma 				struct ocfs2_path *right_path,
2940328d5752SMark Fasheh 				handle_t *handle,
2941328d5752SMark Fasheh 				struct ocfs2_extent_rec *split_rec,
2942677b9752STao Ma 				struct ocfs2_cached_dealloc_ctxt *dealloc,
2943677b9752STao Ma 				int index)
2944328d5752SMark Fasheh {
2945677b9752STao Ma 	int ret, i, subtree_index = 0, has_empty_extent = 0;
2946328d5752SMark Fasheh 	unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
2947328d5752SMark Fasheh 	struct ocfs2_extent_rec *left_rec;
2948328d5752SMark Fasheh 	struct ocfs2_extent_rec *right_rec;
2949677b9752STao Ma 	struct ocfs2_extent_list *el = path_leaf_el(right_path);
2950677b9752STao Ma 	struct buffer_head *bh = path_leaf_bh(right_path);
2951677b9752STao Ma 	struct buffer_head *root_bh = NULL;
2952677b9752STao Ma 	struct ocfs2_path *left_path = NULL;
2953677b9752STao Ma 	struct ocfs2_extent_list *left_el;
2954328d5752SMark Fasheh 
2955677b9752STao Ma 	BUG_ON(index < 0);
2956328d5752SMark Fasheh 
2957328d5752SMark Fasheh 	right_rec = &el->l_recs[index];
2958677b9752STao Ma 	if (index == 0) {
2959677b9752STao Ma 		/* we meet with a cross extent block merge. */
2960677b9752STao Ma 		ret = ocfs2_get_left_path(inode, right_path, &left_path);
2961677b9752STao Ma 		if (ret) {
2962677b9752STao Ma 			mlog_errno(ret);
2963677b9752STao Ma 			goto out;
2964677b9752STao Ma 		}
2965677b9752STao Ma 
2966677b9752STao Ma 		left_el = path_leaf_el(left_path);
2967677b9752STao Ma 		BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
2968677b9752STao Ma 		       le16_to_cpu(left_el->l_count));
2969677b9752STao Ma 
2970677b9752STao Ma 		left_rec = &left_el->l_recs[
2971677b9752STao Ma 				le16_to_cpu(left_el->l_next_free_rec) - 1];
2972677b9752STao Ma 		BUG_ON(le32_to_cpu(left_rec->e_cpos) +
2973677b9752STao Ma 		       le16_to_cpu(left_rec->e_leaf_clusters) !=
2974677b9752STao Ma 		       le32_to_cpu(split_rec->e_cpos));
2975677b9752STao Ma 
2976677b9752STao Ma 		subtree_index = ocfs2_find_subtree_root(inode,
2977677b9752STao Ma 							left_path, right_path);
2978677b9752STao Ma 
2979677b9752STao Ma 		ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
2980677b9752STao Ma 						      handle->h_buffer_credits,
2981677b9752STao Ma 						      left_path);
2982677b9752STao Ma 		if (ret) {
2983677b9752STao Ma 			mlog_errno(ret);
2984677b9752STao Ma 			goto out;
2985677b9752STao Ma 		}
2986677b9752STao Ma 
2987677b9752STao Ma 		root_bh = left_path->p_node[subtree_index].bh;
2988677b9752STao Ma 		BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2989677b9752STao Ma 
2990677b9752STao Ma 		ret = ocfs2_journal_access(handle, inode, root_bh,
2991677b9752STao Ma 					   OCFS2_JOURNAL_ACCESS_WRITE);
2992677b9752STao Ma 		if (ret) {
2993677b9752STao Ma 			mlog_errno(ret);
2994677b9752STao Ma 			goto out;
2995677b9752STao Ma 		}
2996677b9752STao Ma 
2997677b9752STao Ma 		for (i = subtree_index + 1;
2998677b9752STao Ma 		     i < path_num_items(right_path); i++) {
2999677b9752STao Ma 			ret = ocfs2_journal_access(handle, inode,
3000677b9752STao Ma 						   right_path->p_node[i].bh,
3001677b9752STao Ma 						   OCFS2_JOURNAL_ACCESS_WRITE);
3002677b9752STao Ma 			if (ret) {
3003677b9752STao Ma 				mlog_errno(ret);
3004677b9752STao Ma 				goto out;
3005677b9752STao Ma 			}
3006677b9752STao Ma 
3007677b9752STao Ma 			ret = ocfs2_journal_access(handle, inode,
3008677b9752STao Ma 						   left_path->p_node[i].bh,
3009677b9752STao Ma 						   OCFS2_JOURNAL_ACCESS_WRITE);
3010677b9752STao Ma 			if (ret) {
3011677b9752STao Ma 				mlog_errno(ret);
3012677b9752STao Ma 				goto out;
3013677b9752STao Ma 			}
3014677b9752STao Ma 		}
3015677b9752STao Ma 	} else {
3016677b9752STao Ma 		left_rec = &el->l_recs[index - 1];
3017328d5752SMark Fasheh 		if (ocfs2_is_empty_extent(&el->l_recs[0]))
3018328d5752SMark Fasheh 			has_empty_extent = 1;
3019677b9752STao Ma 	}
3020328d5752SMark Fasheh 
3021328d5752SMark Fasheh 	ret = ocfs2_journal_access(handle, inode, bh,
3022328d5752SMark Fasheh 				   OCFS2_JOURNAL_ACCESS_WRITE);
3023328d5752SMark Fasheh 	if (ret) {
3024328d5752SMark Fasheh 		mlog_errno(ret);
3025328d5752SMark Fasheh 		goto out;
3026328d5752SMark Fasheh 	}
3027328d5752SMark Fasheh 
3028328d5752SMark Fasheh 	if (has_empty_extent && index == 1) {
3029328d5752SMark Fasheh 		/*
3030328d5752SMark Fasheh 		 * The easy case - we can just plop the record right in.
3031328d5752SMark Fasheh 		 */
3032328d5752SMark Fasheh 		*left_rec = *split_rec;
3033328d5752SMark Fasheh 
3034328d5752SMark Fasheh 		has_empty_extent = 0;
3035677b9752STao Ma 	} else
3036328d5752SMark Fasheh 		le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
3037328d5752SMark Fasheh 
3038328d5752SMark Fasheh 	le32_add_cpu(&right_rec->e_cpos, split_clusters);
3039328d5752SMark Fasheh 	le64_add_cpu(&right_rec->e_blkno,
3040328d5752SMark Fasheh 		     ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3041328d5752SMark Fasheh 	le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3042328d5752SMark Fasheh 
3043328d5752SMark Fasheh 	ocfs2_cleanup_merge(el, index);
3044328d5752SMark Fasheh 
3045328d5752SMark Fasheh 	ret = ocfs2_journal_dirty(handle, bh);
3046328d5752SMark Fasheh 	if (ret)
3047328d5752SMark Fasheh 		mlog_errno(ret);
3048328d5752SMark Fasheh 
3049677b9752STao Ma 	if (left_path) {
3050677b9752STao Ma 		ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3051677b9752STao Ma 		if (ret)
3052677b9752STao Ma 			mlog_errno(ret);
3053677b9752STao Ma 
3054677b9752STao Ma 		/*
3055677b9752STao Ma 		 * In the situation that the right_rec is empty and the extent
3056677b9752STao Ma 		 * block is empty also,  ocfs2_complete_edge_insert can't handle
3057677b9752STao Ma 		 * it and we need to delete the right extent block.
3058677b9752STao Ma 		 */
3059677b9752STao Ma 		if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3060677b9752STao Ma 		    le16_to_cpu(el->l_next_free_rec) == 1) {
3061677b9752STao Ma 
3062677b9752STao Ma 			ret = ocfs2_remove_rightmost_path(inode, handle,
3063677b9752STao Ma 							  right_path, dealloc);
3064677b9752STao Ma 			if (ret) {
3065677b9752STao Ma 				mlog_errno(ret);
3066677b9752STao Ma 				goto out;
3067677b9752STao Ma 			}
3068677b9752STao Ma 
3069677b9752STao Ma 			/* Now the rightmost extent block has been deleted.
3070677b9752STao Ma 			 * So we use the new rightmost path.
3071677b9752STao Ma 			 */
3072677b9752STao Ma 			ocfs2_mv_path(right_path, left_path);
3073677b9752STao Ma 			left_path = NULL;
3074677b9752STao Ma 		} else
3075677b9752STao Ma 			ocfs2_complete_edge_insert(inode, handle, left_path,
3076677b9752STao Ma 						   right_path, subtree_index);
3077677b9752STao Ma 	}
3078328d5752SMark Fasheh out:
3079677b9752STao Ma 	if (left_path)
3080677b9752STao Ma 		ocfs2_free_path(left_path);
3081328d5752SMark Fasheh 	return ret;
3082328d5752SMark Fasheh }
3083328d5752SMark Fasheh 
3084328d5752SMark Fasheh static int ocfs2_try_to_merge_extent(struct inode *inode,
3085328d5752SMark Fasheh 				     handle_t *handle,
3086677b9752STao Ma 				     struct ocfs2_path *path,
3087328d5752SMark Fasheh 				     int split_index,
3088328d5752SMark Fasheh 				     struct ocfs2_extent_rec *split_rec,
3089328d5752SMark Fasheh 				     struct ocfs2_cached_dealloc_ctxt *dealloc,
3090328d5752SMark Fasheh 				     struct ocfs2_merge_ctxt *ctxt)
3091328d5752SMark Fasheh 
3092328d5752SMark Fasheh {
3093518d7269STao Mao 	int ret = 0;
3094677b9752STao Ma 	struct ocfs2_extent_list *el = path_leaf_el(path);
3095328d5752SMark Fasheh 	struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3096328d5752SMark Fasheh 
3097328d5752SMark Fasheh 	BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3098328d5752SMark Fasheh 
3099518d7269STao Mao 	if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3100328d5752SMark Fasheh 		/*
3101328d5752SMark Fasheh 		 * The merge code will need to create an empty
3102328d5752SMark Fasheh 		 * extent to take the place of the newly
3103328d5752SMark Fasheh 		 * emptied slot. Remove any pre-existing empty
3104328d5752SMark Fasheh 		 * extents - having more than one in a leaf is
3105328d5752SMark Fasheh 		 * illegal.
3106328d5752SMark Fasheh 		 */
3107677b9752STao Ma 		ret = ocfs2_rotate_tree_left(inode, handle, path,
3108328d5752SMark Fasheh 					     dealloc);
3109328d5752SMark Fasheh 		if (ret) {
3110328d5752SMark Fasheh 			mlog_errno(ret);
3111328d5752SMark Fasheh 			goto out;
3112328d5752SMark Fasheh 		}
3113328d5752SMark Fasheh 		split_index--;
3114328d5752SMark Fasheh 		rec = &el->l_recs[split_index];
3115328d5752SMark Fasheh 	}
3116328d5752SMark Fasheh 
3117328d5752SMark Fasheh 	if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3118328d5752SMark Fasheh 		/*
3119328d5752SMark Fasheh 		 * Left-right contig implies this.
3120328d5752SMark Fasheh 		 */
3121328d5752SMark Fasheh 		BUG_ON(!ctxt->c_split_covers_rec);
3122328d5752SMark Fasheh 
3123328d5752SMark Fasheh 		/*
3124328d5752SMark Fasheh 		 * Since the leftright insert always covers the entire
3125328d5752SMark Fasheh 		 * extent, this call will delete the insert record
3126328d5752SMark Fasheh 		 * entirely, resulting in an empty extent record added to
3127328d5752SMark Fasheh 		 * the extent block.
3128328d5752SMark Fasheh 		 *
3129328d5752SMark Fasheh 		 * Since the adding of an empty extent shifts
3130328d5752SMark Fasheh 		 * everything back to the right, there's no need to
3131328d5752SMark Fasheh 		 * update split_index here.
3132677b9752STao Ma 		 *
3133677b9752STao Ma 		 * When the split_index is zero, we need to merge it to the
3134677b9752STao Ma 		 * prevoius extent block. It is more efficient and easier
3135677b9752STao Ma 		 * if we do merge_right first and merge_left later.
3136328d5752SMark Fasheh 		 */
3137677b9752STao Ma 		ret = ocfs2_merge_rec_right(inode, path,
3138677b9752STao Ma 					    handle, split_rec,
3139677b9752STao Ma 					    split_index);
3140328d5752SMark Fasheh 		if (ret) {
3141328d5752SMark Fasheh 			mlog_errno(ret);
3142328d5752SMark Fasheh 			goto out;
3143328d5752SMark Fasheh 		}
3144328d5752SMark Fasheh 
3145328d5752SMark Fasheh 		/*
3146328d5752SMark Fasheh 		 * We can only get this from logic error above.
3147328d5752SMark Fasheh 		 */
3148328d5752SMark Fasheh 		BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3149328d5752SMark Fasheh 
3150677b9752STao Ma 		/* The merge left us with an empty extent, remove it. */
3151677b9752STao Ma 		ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc);
3152328d5752SMark Fasheh 		if (ret) {
3153328d5752SMark Fasheh 			mlog_errno(ret);
3154328d5752SMark Fasheh 			goto out;
3155328d5752SMark Fasheh 		}
3156677b9752STao Ma 
3157328d5752SMark Fasheh 		rec = &el->l_recs[split_index];
3158328d5752SMark Fasheh 
3159328d5752SMark Fasheh 		/*
3160328d5752SMark Fasheh 		 * Note that we don't pass split_rec here on purpose -
3161677b9752STao Ma 		 * we've merged it into the rec already.
3162328d5752SMark Fasheh 		 */
3163677b9752STao Ma 		ret = ocfs2_merge_rec_left(inode, path,
3164677b9752STao Ma 					   handle, rec,
3165677b9752STao Ma 					   dealloc,
3166677b9752STao Ma 					   split_index);
3167677b9752STao Ma 
3168328d5752SMark Fasheh 		if (ret) {
3169328d5752SMark Fasheh 			mlog_errno(ret);
3170328d5752SMark Fasheh 			goto out;
3171328d5752SMark Fasheh 		}
3172328d5752SMark Fasheh 
3173677b9752STao Ma 		ret = ocfs2_rotate_tree_left(inode, handle, path,
3174328d5752SMark Fasheh 					     dealloc);
3175328d5752SMark Fasheh 		/*
3176328d5752SMark Fasheh 		 * Error from this last rotate is not critical, so
3177328d5752SMark Fasheh 		 * print but don't bubble it up.
3178328d5752SMark Fasheh 		 */
3179328d5752SMark Fasheh 		if (ret)
3180328d5752SMark Fasheh 			mlog_errno(ret);
3181328d5752SMark Fasheh 		ret = 0;
3182328d5752SMark Fasheh 	} else {
3183328d5752SMark Fasheh 		/*
3184328d5752SMark Fasheh 		 * Merge a record to the left or right.
3185328d5752SMark Fasheh 		 *
3186328d5752SMark Fasheh 		 * 'contig_type' is relative to the existing record,
3187328d5752SMark Fasheh 		 * so for example, if we're "right contig", it's to
3188328d5752SMark Fasheh 		 * the record on the left (hence the left merge).
3189328d5752SMark Fasheh 		 */
3190328d5752SMark Fasheh 		if (ctxt->c_contig_type == CONTIG_RIGHT) {
3191328d5752SMark Fasheh 			ret = ocfs2_merge_rec_left(inode,
3192677b9752STao Ma 						   path,
3193677b9752STao Ma 						   handle, split_rec,
3194677b9752STao Ma 						   dealloc,
3195328d5752SMark Fasheh 						   split_index);
3196328d5752SMark Fasheh 			if (ret) {
3197328d5752SMark Fasheh 				mlog_errno(ret);
3198328d5752SMark Fasheh 				goto out;
3199328d5752SMark Fasheh 			}
3200328d5752SMark Fasheh 		} else {
3201328d5752SMark Fasheh 			ret = ocfs2_merge_rec_right(inode,
3202677b9752STao Ma 						    path,
3203677b9752STao Ma 						    handle, split_rec,
3204328d5752SMark Fasheh 						    split_index);
3205328d5752SMark Fasheh 			if (ret) {
3206328d5752SMark Fasheh 				mlog_errno(ret);
3207328d5752SMark Fasheh 				goto out;
3208328d5752SMark Fasheh 			}
3209328d5752SMark Fasheh 		}
3210328d5752SMark Fasheh 
3211328d5752SMark Fasheh 		if (ctxt->c_split_covers_rec) {
3212328d5752SMark Fasheh 			/*
3213328d5752SMark Fasheh 			 * The merge may have left an empty extent in
3214328d5752SMark Fasheh 			 * our leaf. Try to rotate it away.
3215328d5752SMark Fasheh 			 */
3216677b9752STao Ma 			ret = ocfs2_rotate_tree_left(inode, handle, path,
3217328d5752SMark Fasheh 						     dealloc);
3218328d5752SMark Fasheh 			if (ret)
3219328d5752SMark Fasheh 				mlog_errno(ret);
3220328d5752SMark Fasheh 			ret = 0;
3221328d5752SMark Fasheh 		}
3222328d5752SMark Fasheh 	}
3223328d5752SMark Fasheh 
3224328d5752SMark Fasheh out:
3225328d5752SMark Fasheh 	return ret;
3226328d5752SMark Fasheh }
3227328d5752SMark Fasheh 
3228328d5752SMark Fasheh static void ocfs2_subtract_from_rec(struct super_block *sb,
3229328d5752SMark Fasheh 				    enum ocfs2_split_type split,
3230328d5752SMark Fasheh 				    struct ocfs2_extent_rec *rec,
3231328d5752SMark Fasheh 				    struct ocfs2_extent_rec *split_rec)
3232328d5752SMark Fasheh {
3233328d5752SMark Fasheh 	u64 len_blocks;
3234328d5752SMark Fasheh 
3235328d5752SMark Fasheh 	len_blocks = ocfs2_clusters_to_blocks(sb,
3236328d5752SMark Fasheh 				le16_to_cpu(split_rec->e_leaf_clusters));
3237328d5752SMark Fasheh 
3238328d5752SMark Fasheh 	if (split == SPLIT_LEFT) {
3239328d5752SMark Fasheh 		/*
3240328d5752SMark Fasheh 		 * Region is on the left edge of the existing
3241328d5752SMark Fasheh 		 * record.
3242328d5752SMark Fasheh 		 */
3243328d5752SMark Fasheh 		le32_add_cpu(&rec->e_cpos,
3244328d5752SMark Fasheh 			     le16_to_cpu(split_rec->e_leaf_clusters));
3245328d5752SMark Fasheh 		le64_add_cpu(&rec->e_blkno, len_blocks);
3246328d5752SMark Fasheh 		le16_add_cpu(&rec->e_leaf_clusters,
3247328d5752SMark Fasheh 			     -le16_to_cpu(split_rec->e_leaf_clusters));
3248328d5752SMark Fasheh 	} else {
3249328d5752SMark Fasheh 		/*
3250328d5752SMark Fasheh 		 * Region is on the right edge of the existing
3251328d5752SMark Fasheh 		 * record.
3252328d5752SMark Fasheh 		 */
3253328d5752SMark Fasheh 		le16_add_cpu(&rec->e_leaf_clusters,
3254328d5752SMark Fasheh 			     -le16_to_cpu(split_rec->e_leaf_clusters));
3255328d5752SMark Fasheh 	}
3256328d5752SMark Fasheh }
3257328d5752SMark Fasheh 
3258dcd0538fSMark Fasheh /*
3259dcd0538fSMark Fasheh  * Do the final bits of extent record insertion at the target leaf
3260dcd0538fSMark Fasheh  * list. If this leaf is part of an allocation tree, it is assumed
3261dcd0538fSMark Fasheh  * that the tree above has been prepared.
3262dcd0538fSMark Fasheh  */
3263dcd0538fSMark Fasheh static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
3264dcd0538fSMark Fasheh 				 struct ocfs2_extent_list *el,
3265dcd0538fSMark Fasheh 				 struct ocfs2_insert_type *insert,
3266dcd0538fSMark Fasheh 				 struct inode *inode)
3267dcd0538fSMark Fasheh {
3268dcd0538fSMark Fasheh 	int i = insert->ins_contig_index;
3269dcd0538fSMark Fasheh 	unsigned int range;
3270dcd0538fSMark Fasheh 	struct ocfs2_extent_rec *rec;
3271dcd0538fSMark Fasheh 
3272e48edee2SMark Fasheh 	BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
3273dcd0538fSMark Fasheh 
3274328d5752SMark Fasheh 	if (insert->ins_split != SPLIT_NONE) {
3275328d5752SMark Fasheh 		i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3276328d5752SMark Fasheh 		BUG_ON(i == -1);
3277328d5752SMark Fasheh 		rec = &el->l_recs[i];
3278328d5752SMark Fasheh 		ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec,
3279328d5752SMark Fasheh 					insert_rec);
3280328d5752SMark Fasheh 		goto rotate;
3281328d5752SMark Fasheh 	}
3282328d5752SMark Fasheh 
3283dcd0538fSMark Fasheh 	/*
3284dcd0538fSMark Fasheh 	 * Contiguous insert - either left or right.
3285dcd0538fSMark Fasheh 	 */
3286dcd0538fSMark Fasheh 	if (insert->ins_contig != CONTIG_NONE) {
3287dcd0538fSMark Fasheh 		rec = &el->l_recs[i];
3288dcd0538fSMark Fasheh 		if (insert->ins_contig == CONTIG_LEFT) {
3289dcd0538fSMark Fasheh 			rec->e_blkno = insert_rec->e_blkno;
3290dcd0538fSMark Fasheh 			rec->e_cpos = insert_rec->e_cpos;
3291dcd0538fSMark Fasheh 		}
3292e48edee2SMark Fasheh 		le16_add_cpu(&rec->e_leaf_clusters,
3293e48edee2SMark Fasheh 			     le16_to_cpu(insert_rec->e_leaf_clusters));
3294dcd0538fSMark Fasheh 		return;
3295dcd0538fSMark Fasheh 	}
3296dcd0538fSMark Fasheh 
3297dcd0538fSMark Fasheh 	/*
3298dcd0538fSMark Fasheh 	 * Handle insert into an empty leaf.
3299dcd0538fSMark Fasheh 	 */
3300dcd0538fSMark Fasheh 	if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3301dcd0538fSMark Fasheh 	    ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3302dcd0538fSMark Fasheh 	     ocfs2_is_empty_extent(&el->l_recs[0]))) {
3303dcd0538fSMark Fasheh 		el->l_recs[0] = *insert_rec;
3304dcd0538fSMark Fasheh 		el->l_next_free_rec = cpu_to_le16(1);
3305dcd0538fSMark Fasheh 		return;
3306dcd0538fSMark Fasheh 	}
3307dcd0538fSMark Fasheh 
3308dcd0538fSMark Fasheh 	/*
3309dcd0538fSMark Fasheh 	 * Appending insert.
3310dcd0538fSMark Fasheh 	 */
3311dcd0538fSMark Fasheh 	if (insert->ins_appending == APPEND_TAIL) {
3312dcd0538fSMark Fasheh 		i = le16_to_cpu(el->l_next_free_rec) - 1;
3313dcd0538fSMark Fasheh 		rec = &el->l_recs[i];
3314e48edee2SMark Fasheh 		range = le32_to_cpu(rec->e_cpos)
3315e48edee2SMark Fasheh 			+ le16_to_cpu(rec->e_leaf_clusters);
3316dcd0538fSMark Fasheh 		BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3317dcd0538fSMark Fasheh 
3318dcd0538fSMark Fasheh 		mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3319dcd0538fSMark Fasheh 				le16_to_cpu(el->l_count),
3320dcd0538fSMark Fasheh 				"inode %lu, depth %u, count %u, next free %u, "
3321dcd0538fSMark Fasheh 				"rec.cpos %u, rec.clusters %u, "
3322dcd0538fSMark Fasheh 				"insert.cpos %u, insert.clusters %u\n",
3323dcd0538fSMark Fasheh 				inode->i_ino,
3324dcd0538fSMark Fasheh 				le16_to_cpu(el->l_tree_depth),
3325dcd0538fSMark Fasheh 				le16_to_cpu(el->l_count),
3326dcd0538fSMark Fasheh 				le16_to_cpu(el->l_next_free_rec),
3327dcd0538fSMark Fasheh 				le32_to_cpu(el->l_recs[i].e_cpos),
3328e48edee2SMark Fasheh 				le16_to_cpu(el->l_recs[i].e_leaf_clusters),
3329dcd0538fSMark Fasheh 				le32_to_cpu(insert_rec->e_cpos),
3330e48edee2SMark Fasheh 				le16_to_cpu(insert_rec->e_leaf_clusters));
3331dcd0538fSMark Fasheh 		i++;
3332dcd0538fSMark Fasheh 		el->l_recs[i] = *insert_rec;
3333dcd0538fSMark Fasheh 		le16_add_cpu(&el->l_next_free_rec, 1);
3334dcd0538fSMark Fasheh 		return;
3335dcd0538fSMark Fasheh 	}
3336dcd0538fSMark Fasheh 
3337328d5752SMark Fasheh rotate:
3338dcd0538fSMark Fasheh 	/*
3339dcd0538fSMark Fasheh 	 * Ok, we have to rotate.
3340dcd0538fSMark Fasheh 	 *
3341dcd0538fSMark Fasheh 	 * At this point, it is safe to assume that inserting into an
3342dcd0538fSMark Fasheh 	 * empty leaf and appending to a leaf have both been handled
3343dcd0538fSMark Fasheh 	 * above.
3344dcd0538fSMark Fasheh 	 *
3345dcd0538fSMark Fasheh 	 * This leaf needs to have space, either by the empty 1st
3346dcd0538fSMark Fasheh 	 * extent record, or by virtue of an l_next_rec < l_count.
3347dcd0538fSMark Fasheh 	 */
3348dcd0538fSMark Fasheh 	ocfs2_rotate_leaf(el, insert_rec);
3349dcd0538fSMark Fasheh }
3350dcd0538fSMark Fasheh 
3351dcd0538fSMark Fasheh static inline void ocfs2_update_dinode_clusters(struct inode *inode,
3352dcd0538fSMark Fasheh 						struct ocfs2_dinode *di,
3353dcd0538fSMark Fasheh 						u32 clusters)
3354dcd0538fSMark Fasheh {
3355dcd0538fSMark Fasheh 	le32_add_cpu(&di->i_clusters, clusters);
3356dcd0538fSMark Fasheh 	spin_lock(&OCFS2_I(inode)->ip_lock);
3357dcd0538fSMark Fasheh 	OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters);
3358dcd0538fSMark Fasheh 	spin_unlock(&OCFS2_I(inode)->ip_lock);
3359dcd0538fSMark Fasheh }
3360dcd0538fSMark Fasheh 
3361328d5752SMark Fasheh static void ocfs2_adjust_rightmost_records(struct inode *inode,
3362328d5752SMark Fasheh 					   handle_t *handle,
3363328d5752SMark Fasheh 					   struct ocfs2_path *path,
3364328d5752SMark Fasheh 					   struct ocfs2_extent_rec *insert_rec)
3365328d5752SMark Fasheh {
3366328d5752SMark Fasheh 	int ret, i, next_free;
3367328d5752SMark Fasheh 	struct buffer_head *bh;
3368328d5752SMark Fasheh 	struct ocfs2_extent_list *el;
3369328d5752SMark Fasheh 	struct ocfs2_extent_rec *rec;
3370328d5752SMark Fasheh 
3371328d5752SMark Fasheh 	/*
3372328d5752SMark Fasheh 	 * Update everything except the leaf block.
3373328d5752SMark Fasheh 	 */
3374328d5752SMark Fasheh 	for (i = 0; i < path->p_tree_depth; i++) {
3375328d5752SMark Fasheh 		bh = path->p_node[i].bh;
3376328d5752SMark Fasheh 		el = path->p_node[i].el;
3377328d5752SMark Fasheh 
3378328d5752SMark Fasheh 		next_free = le16_to_cpu(el->l_next_free_rec);
3379328d5752SMark Fasheh 		if (next_free == 0) {
3380328d5752SMark Fasheh 			ocfs2_error(inode->i_sb,
3381328d5752SMark Fasheh 				    "Dinode %llu has a bad extent list",
3382328d5752SMark Fasheh 				    (unsigned long long)OCFS2_I(inode)->ip_blkno);
3383328d5752SMark Fasheh 			ret = -EIO;
3384328d5752SMark Fasheh 			return;
3385328d5752SMark Fasheh 		}
3386328d5752SMark Fasheh 
3387328d5752SMark Fasheh 		rec = &el->l_recs[next_free - 1];
3388328d5752SMark Fasheh 
3389328d5752SMark Fasheh 		rec->e_int_clusters = insert_rec->e_cpos;
3390328d5752SMark Fasheh 		le32_add_cpu(&rec->e_int_clusters,
3391328d5752SMark Fasheh 			     le16_to_cpu(insert_rec->e_leaf_clusters));
3392328d5752SMark Fasheh 		le32_add_cpu(&rec->e_int_clusters,
3393328d5752SMark Fasheh 			     -le32_to_cpu(rec->e_cpos));
3394328d5752SMark Fasheh 
3395328d5752SMark Fasheh 		ret = ocfs2_journal_dirty(handle, bh);
3396328d5752SMark Fasheh 		if (ret)
3397328d5752SMark Fasheh 			mlog_errno(ret);
3398328d5752SMark Fasheh 
3399328d5752SMark Fasheh 	}
3400328d5752SMark Fasheh }
3401328d5752SMark Fasheh 
3402dcd0538fSMark Fasheh static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
3403dcd0538fSMark Fasheh 				    struct ocfs2_extent_rec *insert_rec,
3404dcd0538fSMark Fasheh 				    struct ocfs2_path *right_path,
3405dcd0538fSMark Fasheh 				    struct ocfs2_path **ret_left_path)
3406dcd0538fSMark Fasheh {
3407328d5752SMark Fasheh 	int ret, next_free;
3408dcd0538fSMark Fasheh 	struct ocfs2_extent_list *el;
3409dcd0538fSMark Fasheh 	struct ocfs2_path *left_path = NULL;
3410dcd0538fSMark Fasheh 
3411dcd0538fSMark Fasheh 	*ret_left_path = NULL;
3412dcd0538fSMark Fasheh 
3413dcd0538fSMark Fasheh 	/*
3414e48edee2SMark Fasheh 	 * This shouldn't happen for non-trees. The extent rec cluster
3415e48edee2SMark Fasheh 	 * count manipulation below only works for interior nodes.
3416e48edee2SMark Fasheh 	 */
3417e48edee2SMark Fasheh 	BUG_ON(right_path->p_tree_depth == 0);
3418e48edee2SMark Fasheh 
3419e48edee2SMark Fasheh 	/*
3420dcd0538fSMark Fasheh 	 * If our appending insert is at the leftmost edge of a leaf,
3421dcd0538fSMark Fasheh 	 * then we might need to update the rightmost records of the
3422dcd0538fSMark Fasheh 	 * neighboring path.
3423dcd0538fSMark Fasheh 	 */
3424dcd0538fSMark Fasheh 	el = path_leaf_el(right_path);
3425dcd0538fSMark Fasheh 	next_free = le16_to_cpu(el->l_next_free_rec);
3426dcd0538fSMark Fasheh 	if (next_free == 0 ||
3427dcd0538fSMark Fasheh 	    (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3428dcd0538fSMark Fasheh 		u32 left_cpos;
3429dcd0538fSMark Fasheh 
3430dcd0538fSMark Fasheh 		ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
3431dcd0538fSMark Fasheh 						    &left_cpos);
3432dcd0538fSMark Fasheh 		if (ret) {
3433dcd0538fSMark Fasheh 			mlog_errno(ret);
3434dcd0538fSMark Fasheh 			goto out;
3435dcd0538fSMark Fasheh 		}
3436dcd0538fSMark Fasheh 
3437dcd0538fSMark Fasheh 		mlog(0, "Append may need a left path update. cpos: %u, "
3438dcd0538fSMark Fasheh 		     "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3439dcd0538fSMark Fasheh 		     left_cpos);
3440dcd0538fSMark Fasheh 
3441dcd0538fSMark Fasheh 		/*
3442dcd0538fSMark Fasheh 		 * No need to worry if the append is already in the
3443dcd0538fSMark Fasheh 		 * leftmost leaf.
3444dcd0538fSMark Fasheh 		 */
3445dcd0538fSMark Fasheh 		if (left_cpos) {
3446dcd0538fSMark Fasheh 			left_path = ocfs2_new_path(path_root_bh(right_path),
3447dcd0538fSMark Fasheh 						   path_root_el(right_path));
3448dcd0538fSMark Fasheh 			if (!left_path) {
3449dcd0538fSMark Fasheh 				ret = -ENOMEM;
3450dcd0538fSMark Fasheh 				mlog_errno(ret);
3451dcd0538fSMark Fasheh 				goto out;
3452dcd0538fSMark Fasheh 			}
3453dcd0538fSMark Fasheh 
3454dcd0538fSMark Fasheh 			ret = ocfs2_find_path(inode, left_path, left_cpos);
3455dcd0538fSMark Fasheh 			if (ret) {
3456dcd0538fSMark Fasheh 				mlog_errno(ret);
3457dcd0538fSMark Fasheh 				goto out;
3458dcd0538fSMark Fasheh 			}
3459dcd0538fSMark Fasheh 
3460dcd0538fSMark Fasheh 			/*
3461dcd0538fSMark Fasheh 			 * ocfs2_insert_path() will pass the left_path to the
3462dcd0538fSMark Fasheh 			 * journal for us.
3463dcd0538fSMark Fasheh 			 */
3464dcd0538fSMark Fasheh 		}
3465dcd0538fSMark Fasheh 	}
3466dcd0538fSMark Fasheh 
3467dcd0538fSMark Fasheh 	ret = ocfs2_journal_access_path(inode, handle, right_path);
3468dcd0538fSMark Fasheh 	if (ret) {
3469dcd0538fSMark Fasheh 		mlog_errno(ret);
3470dcd0538fSMark Fasheh 		goto out;
3471dcd0538fSMark Fasheh 	}
3472dcd0538fSMark Fasheh 
3473328d5752SMark Fasheh 	ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec);
3474dcd0538fSMark Fasheh 
3475dcd0538fSMark Fasheh 	*ret_left_path = left_path;
3476dcd0538fSMark Fasheh 	ret = 0;
3477dcd0538fSMark Fasheh out:
3478dcd0538fSMark Fasheh 	if (ret != 0)
3479dcd0538fSMark Fasheh 		ocfs2_free_path(left_path);
3480dcd0538fSMark Fasheh 
3481dcd0538fSMark Fasheh 	return ret;
3482dcd0538fSMark Fasheh }
3483dcd0538fSMark Fasheh 
3484328d5752SMark Fasheh static void ocfs2_split_record(struct inode *inode,
3485328d5752SMark Fasheh 			       struct ocfs2_path *left_path,
3486328d5752SMark Fasheh 			       struct ocfs2_path *right_path,
3487328d5752SMark Fasheh 			       struct ocfs2_extent_rec *split_rec,
3488328d5752SMark Fasheh 			       enum ocfs2_split_type split)
3489328d5752SMark Fasheh {
3490328d5752SMark Fasheh 	int index;
3491328d5752SMark Fasheh 	u32 cpos = le32_to_cpu(split_rec->e_cpos);
3492328d5752SMark Fasheh 	struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
3493328d5752SMark Fasheh 	struct ocfs2_extent_rec *rec, *tmprec;
3494328d5752SMark Fasheh 
3495328d5752SMark Fasheh 	right_el = path_leaf_el(right_path);;
3496328d5752SMark Fasheh 	if (left_path)
3497328d5752SMark Fasheh 		left_el = path_leaf_el(left_path);
3498328d5752SMark Fasheh 
3499328d5752SMark Fasheh 	el = right_el;
3500328d5752SMark Fasheh 	insert_el = right_el;
3501328d5752SMark Fasheh 	index = ocfs2_search_extent_list(el, cpos);
3502328d5752SMark Fasheh 	if (index != -1) {
3503328d5752SMark Fasheh 		if (index == 0 && left_path) {
3504328d5752SMark Fasheh 			BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3505328d5752SMark Fasheh 
3506328d5752SMark Fasheh 			/*
3507328d5752SMark Fasheh 			 * This typically means that the record
3508328d5752SMark Fasheh 			 * started in the left path but moved to the
3509328d5752SMark Fasheh 			 * right as a result of rotation. We either
3510328d5752SMark Fasheh 			 * move the existing record to the left, or we
3511328d5752SMark Fasheh 			 * do the later insert there.
3512328d5752SMark Fasheh 			 *
3513328d5752SMark Fasheh 			 * In this case, the left path should always
3514328d5752SMark Fasheh 			 * exist as the rotate code will have passed
3515328d5752SMark Fasheh 			 * it back for a post-insert update.
3516328d5752SMark Fasheh 			 */
3517328d5752SMark Fasheh 
3518328d5752SMark Fasheh 			if (split == SPLIT_LEFT) {
3519328d5752SMark Fasheh 				/*
3520328d5752SMark Fasheh 				 * It's a left split. Since we know
3521328d5752SMark Fasheh 				 * that the rotate code gave us an
3522328d5752SMark Fasheh 				 * empty extent in the left path, we
3523328d5752SMark Fasheh 				 * can just do the insert there.
3524328d5752SMark Fasheh 				 */
3525328d5752SMark Fasheh 				insert_el = left_el;
3526328d5752SMark Fasheh 			} else {
3527328d5752SMark Fasheh 				/*
3528328d5752SMark Fasheh 				 * Right split - we have to move the
3529328d5752SMark Fasheh 				 * existing record over to the left
3530328d5752SMark Fasheh 				 * leaf. The insert will be into the
3531328d5752SMark Fasheh 				 * newly created empty extent in the
3532328d5752SMark Fasheh 				 * right leaf.
3533328d5752SMark Fasheh 				 */
3534328d5752SMark Fasheh 				tmprec = &right_el->l_recs[index];
3535328d5752SMark Fasheh 				ocfs2_rotate_leaf(left_el, tmprec);
3536328d5752SMark Fasheh 				el = left_el;
3537328d5752SMark Fasheh 
3538328d5752SMark Fasheh 				memset(tmprec, 0, sizeof(*tmprec));
3539328d5752SMark Fasheh 				index = ocfs2_search_extent_list(left_el, cpos);
3540328d5752SMark Fasheh 				BUG_ON(index == -1);
3541328d5752SMark Fasheh 			}
3542328d5752SMark Fasheh 		}
3543328d5752SMark Fasheh 	} else {
3544328d5752SMark Fasheh 		BUG_ON(!left_path);
3545328d5752SMark Fasheh 		BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
3546328d5752SMark Fasheh 		/*
3547328d5752SMark Fasheh 		 * Left path is easy - we can just allow the insert to
3548328d5752SMark Fasheh 		 * happen.
3549328d5752SMark Fasheh 		 */
3550328d5752SMark Fasheh 		el = left_el;
3551328d5752SMark Fasheh 		insert_el = left_el;
3552328d5752SMark Fasheh 		index = ocfs2_search_extent_list(el, cpos);
3553328d5752SMark Fasheh 		BUG_ON(index == -1);
3554328d5752SMark Fasheh 	}
3555328d5752SMark Fasheh 
3556328d5752SMark Fasheh 	rec = &el->l_recs[index];
3557328d5752SMark Fasheh 	ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec);
3558328d5752SMark Fasheh 	ocfs2_rotate_leaf(insert_el, split_rec);
3559328d5752SMark Fasheh }
3560328d5752SMark Fasheh 
3561dcd0538fSMark Fasheh /*
3562dcd0538fSMark Fasheh  * This function only does inserts on an allocation b-tree. For dinode
3563dcd0538fSMark Fasheh  * lists, ocfs2_insert_at_leaf() is called directly.
3564dcd0538fSMark Fasheh  *
3565dcd0538fSMark Fasheh  * right_path is the path we want to do the actual insert
3566dcd0538fSMark Fasheh  * in. left_path should only be passed in if we need to update that
3567dcd0538fSMark Fasheh  * portion of the tree after an edge insert.
3568dcd0538fSMark Fasheh  */
3569dcd0538fSMark Fasheh static int ocfs2_insert_path(struct inode *inode,
3570dcd0538fSMark Fasheh 			     handle_t *handle,
3571dcd0538fSMark Fasheh 			     struct ocfs2_path *left_path,
3572dcd0538fSMark Fasheh 			     struct ocfs2_path *right_path,
3573dcd0538fSMark Fasheh 			     struct ocfs2_extent_rec *insert_rec,
3574dcd0538fSMark Fasheh 			     struct ocfs2_insert_type *insert)
3575dcd0538fSMark Fasheh {
3576dcd0538fSMark Fasheh 	int ret, subtree_index;
3577dcd0538fSMark Fasheh 	struct buffer_head *leaf_bh = path_leaf_bh(right_path);
3578dcd0538fSMark Fasheh 
3579dcd0538fSMark Fasheh 	if (left_path) {
3580dcd0538fSMark Fasheh 		int credits = handle->h_buffer_credits;
3581dcd0538fSMark Fasheh 
3582dcd0538fSMark Fasheh 		/*
3583dcd0538fSMark Fasheh 		 * There's a chance that left_path got passed back to
3584dcd0538fSMark Fasheh 		 * us without being accounted for in the
3585dcd0538fSMark Fasheh 		 * journal. Extend our transaction here to be sure we
3586dcd0538fSMark Fasheh 		 * can change those blocks.
3587dcd0538fSMark Fasheh 		 */
3588dcd0538fSMark Fasheh 		credits += left_path->p_tree_depth;
3589dcd0538fSMark Fasheh 
3590dcd0538fSMark Fasheh 		ret = ocfs2_extend_trans(handle, credits);
3591dcd0538fSMark Fasheh 		if (ret < 0) {
3592dcd0538fSMark Fasheh 			mlog_errno(ret);
3593dcd0538fSMark Fasheh 			goto out;
3594dcd0538fSMark Fasheh 		}
3595dcd0538fSMark Fasheh 
3596dcd0538fSMark Fasheh 		ret = ocfs2_journal_access_path(inode, handle, left_path);
3597dcd0538fSMark Fasheh 		if (ret < 0) {
3598dcd0538fSMark Fasheh 			mlog_errno(ret);
3599dcd0538fSMark Fasheh 			goto out;
3600dcd0538fSMark Fasheh 		}
3601dcd0538fSMark Fasheh 	}
3602dcd0538fSMark Fasheh 
3603e8aed345SMark Fasheh 	/*
3604e8aed345SMark Fasheh 	 * Pass both paths to the journal. The majority of inserts
3605e8aed345SMark Fasheh 	 * will be touching all components anyway.
3606e8aed345SMark Fasheh 	 */
3607e8aed345SMark Fasheh 	ret = ocfs2_journal_access_path(inode, handle, right_path);
3608e8aed345SMark Fasheh 	if (ret < 0) {
3609e8aed345SMark Fasheh 		mlog_errno(ret);
3610e8aed345SMark Fasheh 		goto out;
3611e8aed345SMark Fasheh 	}
3612e8aed345SMark Fasheh 
3613328d5752SMark Fasheh 	if (insert->ins_split != SPLIT_NONE) {
3614328d5752SMark Fasheh 		/*
3615328d5752SMark Fasheh 		 * We could call ocfs2_insert_at_leaf() for some types
3616c78bad11SJoe Perches 		 * of splits, but it's easier to just let one separate
3617328d5752SMark Fasheh 		 * function sort it all out.
3618328d5752SMark Fasheh 		 */
3619328d5752SMark Fasheh 		ocfs2_split_record(inode, left_path, right_path,
3620328d5752SMark Fasheh 				   insert_rec, insert->ins_split);
3621e8aed345SMark Fasheh 
3622e8aed345SMark Fasheh 		/*
3623e8aed345SMark Fasheh 		 * Split might have modified either leaf and we don't
3624e8aed345SMark Fasheh 		 * have a guarantee that the later edge insert will
3625e8aed345SMark Fasheh 		 * dirty this for us.
3626e8aed345SMark Fasheh 		 */
3627e8aed345SMark Fasheh 		if (left_path)
3628e8aed345SMark Fasheh 			ret = ocfs2_journal_dirty(handle,
3629e8aed345SMark Fasheh 						  path_leaf_bh(left_path));
3630e8aed345SMark Fasheh 			if (ret)
3631e8aed345SMark Fasheh 				mlog_errno(ret);
3632328d5752SMark Fasheh 	} else
3633328d5752SMark Fasheh 		ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path),
3634328d5752SMark Fasheh 				     insert, inode);
3635dcd0538fSMark Fasheh 
3636dcd0538fSMark Fasheh 	ret = ocfs2_journal_dirty(handle, leaf_bh);
3637dcd0538fSMark Fasheh 	if (ret)
3638dcd0538fSMark Fasheh 		mlog_errno(ret);
3639dcd0538fSMark Fasheh 
3640dcd0538fSMark Fasheh 	if (left_path) {
3641dcd0538fSMark Fasheh 		/*
3642dcd0538fSMark Fasheh 		 * The rotate code has indicated that we need to fix
3643dcd0538fSMark Fasheh 		 * up portions of the tree after the insert.
3644dcd0538fSMark Fasheh 		 *
3645dcd0538fSMark Fasheh 		 * XXX: Should we extend the transaction here?
3646dcd0538fSMark Fasheh 		 */
3647dcd0538fSMark Fasheh 		subtree_index = ocfs2_find_subtree_root(inode, left_path,
3648dcd0538fSMark Fasheh 							right_path);
3649dcd0538fSMark Fasheh 		ocfs2_complete_edge_insert(inode, handle, left_path,
3650dcd0538fSMark Fasheh 					   right_path, subtree_index);
3651dcd0538fSMark Fasheh 	}
3652dcd0538fSMark Fasheh 
3653dcd0538fSMark Fasheh 	ret = 0;
3654dcd0538fSMark Fasheh out:
3655dcd0538fSMark Fasheh 	return ret;
3656dcd0538fSMark Fasheh }
3657dcd0538fSMark Fasheh 
3658dcd0538fSMark Fasheh static int ocfs2_do_insert_extent(struct inode *inode,
3659dcd0538fSMark Fasheh 				  handle_t *handle,
3660dcd0538fSMark Fasheh 				  struct buffer_head *di_bh,
3661dcd0538fSMark Fasheh 				  struct ocfs2_extent_rec *insert_rec,
3662dcd0538fSMark Fasheh 				  struct ocfs2_insert_type *type)
3663dcd0538fSMark Fasheh {
3664dcd0538fSMark Fasheh 	int ret, rotate = 0;
3665dcd0538fSMark Fasheh 	u32 cpos;
3666dcd0538fSMark Fasheh 	struct ocfs2_path *right_path = NULL;
3667dcd0538fSMark Fasheh 	struct ocfs2_path *left_path = NULL;
3668dcd0538fSMark Fasheh 	struct ocfs2_dinode *di;
3669dcd0538fSMark Fasheh 	struct ocfs2_extent_list *el;
3670dcd0538fSMark Fasheh 
3671dcd0538fSMark Fasheh 	di = (struct ocfs2_dinode *) di_bh->b_data;
3672dcd0538fSMark Fasheh 	el = &di->id2.i_list;
3673dcd0538fSMark Fasheh 
3674dcd0538fSMark Fasheh 	ret = ocfs2_journal_access(handle, inode, di_bh,
3675dcd0538fSMark Fasheh 				   OCFS2_JOURNAL_ACCESS_WRITE);
3676dcd0538fSMark Fasheh 	if (ret) {
3677dcd0538fSMark Fasheh 		mlog_errno(ret);
3678dcd0538fSMark Fasheh 		goto out;
3679dcd0538fSMark Fasheh 	}
3680dcd0538fSMark Fasheh 
3681dcd0538fSMark Fasheh 	if (le16_to_cpu(el->l_tree_depth) == 0) {
3682dcd0538fSMark Fasheh 		ocfs2_insert_at_leaf(insert_rec, el, type, inode);
3683dcd0538fSMark Fasheh 		goto out_update_clusters;
3684dcd0538fSMark Fasheh 	}
3685dcd0538fSMark Fasheh 
3686dcd0538fSMark Fasheh 	right_path = ocfs2_new_inode_path(di_bh);
3687dcd0538fSMark Fasheh 	if (!right_path) {
3688dcd0538fSMark Fasheh 		ret = -ENOMEM;
3689dcd0538fSMark Fasheh 		mlog_errno(ret);
3690dcd0538fSMark Fasheh 		goto out;
3691dcd0538fSMark Fasheh 	}
3692dcd0538fSMark Fasheh 
3693dcd0538fSMark Fasheh 	/*
3694dcd0538fSMark Fasheh 	 * Determine the path to start with. Rotations need the
3695dcd0538fSMark Fasheh 	 * rightmost path, everything else can go directly to the
3696dcd0538fSMark Fasheh 	 * target leaf.
3697dcd0538fSMark Fasheh 	 */
3698dcd0538fSMark Fasheh 	cpos = le32_to_cpu(insert_rec->e_cpos);
3699dcd0538fSMark Fasheh 	if (type->ins_appending == APPEND_NONE &&
3700dcd0538fSMark Fasheh 	    type->ins_contig == CONTIG_NONE) {
3701dcd0538fSMark Fasheh 		rotate = 1;
3702dcd0538fSMark Fasheh 		cpos = UINT_MAX;
3703dcd0538fSMark Fasheh 	}
3704dcd0538fSMark Fasheh 
3705dcd0538fSMark Fasheh 	ret = ocfs2_find_path(inode, right_path, cpos);
3706dcd0538fSMark Fasheh 	if (ret) {
3707dcd0538fSMark Fasheh 		mlog_errno(ret);
3708dcd0538fSMark Fasheh 		goto out;
3709dcd0538fSMark Fasheh 	}
3710dcd0538fSMark Fasheh 
3711dcd0538fSMark Fasheh 	/*
3712dcd0538fSMark Fasheh 	 * Rotations and appends need special treatment - they modify
3713dcd0538fSMark Fasheh 	 * parts of the tree's above them.
3714dcd0538fSMark Fasheh 	 *
3715dcd0538fSMark Fasheh 	 * Both might pass back a path immediate to the left of the
3716dcd0538fSMark Fasheh 	 * one being inserted to. This will be cause
3717dcd0538fSMark Fasheh 	 * ocfs2_insert_path() to modify the rightmost records of
3718dcd0538fSMark Fasheh 	 * left_path to account for an edge insert.
3719dcd0538fSMark Fasheh 	 *
3720dcd0538fSMark Fasheh 	 * XXX: When modifying this code, keep in mind that an insert
3721dcd0538fSMark Fasheh 	 * can wind up skipping both of these two special cases...
3722dcd0538fSMark Fasheh 	 */
3723dcd0538fSMark Fasheh 	if (rotate) {
3724328d5752SMark Fasheh 		ret = ocfs2_rotate_tree_right(inode, handle, type->ins_split,
3725dcd0538fSMark Fasheh 					      le32_to_cpu(insert_rec->e_cpos),
3726dcd0538fSMark Fasheh 					      right_path, &left_path);
3727dcd0538fSMark Fasheh 		if (ret) {
3728dcd0538fSMark Fasheh 			mlog_errno(ret);
3729dcd0538fSMark Fasheh 			goto out;
3730dcd0538fSMark Fasheh 		}
3731e8aed345SMark Fasheh 
3732e8aed345SMark Fasheh 		/*
3733e8aed345SMark Fasheh 		 * ocfs2_rotate_tree_right() might have extended the
3734e8aed345SMark Fasheh 		 * transaction without re-journaling our tree root.
3735e8aed345SMark Fasheh 		 */
3736e8aed345SMark Fasheh 		ret = ocfs2_journal_access(handle, inode, di_bh,
3737e8aed345SMark Fasheh 					   OCFS2_JOURNAL_ACCESS_WRITE);
3738e8aed345SMark Fasheh 		if (ret) {
3739e8aed345SMark Fasheh 			mlog_errno(ret);
3740e8aed345SMark Fasheh 			goto out;
3741e8aed345SMark Fasheh 		}
3742dcd0538fSMark Fasheh 	} else if (type->ins_appending == APPEND_TAIL
3743dcd0538fSMark Fasheh 		   && type->ins_contig != CONTIG_LEFT) {
3744dcd0538fSMark Fasheh 		ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
3745dcd0538fSMark Fasheh 					       right_path, &left_path);
3746dcd0538fSMark Fasheh 		if (ret) {
3747dcd0538fSMark Fasheh 			mlog_errno(ret);
3748dcd0538fSMark Fasheh 			goto out;
3749dcd0538fSMark Fasheh 		}
3750dcd0538fSMark Fasheh 	}
3751dcd0538fSMark Fasheh 
3752dcd0538fSMark Fasheh 	ret = ocfs2_insert_path(inode, handle, left_path, right_path,
3753dcd0538fSMark Fasheh 				insert_rec, type);
3754dcd0538fSMark Fasheh 	if (ret) {
3755dcd0538fSMark Fasheh 		mlog_errno(ret);
3756dcd0538fSMark Fasheh 		goto out;
3757dcd0538fSMark Fasheh 	}
3758dcd0538fSMark Fasheh 
3759dcd0538fSMark Fasheh out_update_clusters:
3760328d5752SMark Fasheh 	if (type->ins_split == SPLIT_NONE)
3761dcd0538fSMark Fasheh 		ocfs2_update_dinode_clusters(inode, di,
3762e48edee2SMark Fasheh 					     le16_to_cpu(insert_rec->e_leaf_clusters));
3763dcd0538fSMark Fasheh 
3764dcd0538fSMark Fasheh 	ret = ocfs2_journal_dirty(handle, di_bh);
3765dcd0538fSMark Fasheh 	if (ret)
3766dcd0538fSMark Fasheh 		mlog_errno(ret);
3767dcd0538fSMark Fasheh 
3768dcd0538fSMark Fasheh out:
3769dcd0538fSMark Fasheh 	ocfs2_free_path(left_path);
3770dcd0538fSMark Fasheh 	ocfs2_free_path(right_path);
3771dcd0538fSMark Fasheh 
3772dcd0538fSMark Fasheh 	return ret;
3773dcd0538fSMark Fasheh }
3774dcd0538fSMark Fasheh 
3775328d5752SMark Fasheh static enum ocfs2_contig_type
3776ad5a4d70STao Ma ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
3777328d5752SMark Fasheh 			       struct ocfs2_extent_list *el, int index,
3778328d5752SMark Fasheh 			       struct ocfs2_extent_rec *split_rec)
3779328d5752SMark Fasheh {
3780ad5a4d70STao Ma 	int status;
3781328d5752SMark Fasheh 	enum ocfs2_contig_type ret = CONTIG_NONE;
3782ad5a4d70STao Ma 	u32 left_cpos, right_cpos;
3783ad5a4d70STao Ma 	struct ocfs2_extent_rec *rec = NULL;
3784ad5a4d70STao Ma 	struct ocfs2_extent_list *new_el;
3785ad5a4d70STao Ma 	struct ocfs2_path *left_path = NULL, *right_path = NULL;
3786ad5a4d70STao Ma 	struct buffer_head *bh;
3787ad5a4d70STao Ma 	struct ocfs2_extent_block *eb;
3788ad5a4d70STao Ma 
3789ad5a4d70STao Ma 	if (index > 0) {
3790ad5a4d70STao Ma 		rec = &el->l_recs[index - 1];
3791ad5a4d70STao Ma 	} else if (path->p_tree_depth > 0) {
3792ad5a4d70STao Ma 		status = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
3793ad5a4d70STao Ma 						       path, &left_cpos);
3794ad5a4d70STao Ma 		if (status)
3795ad5a4d70STao Ma 			goto out;
3796ad5a4d70STao Ma 
3797ad5a4d70STao Ma 		if (left_cpos != 0) {
3798ad5a4d70STao Ma 			left_path = ocfs2_new_path(path_root_bh(path),
3799ad5a4d70STao Ma 						   path_root_el(path));
3800ad5a4d70STao Ma 			if (!left_path)
3801ad5a4d70STao Ma 				goto out;
3802ad5a4d70STao Ma 
3803ad5a4d70STao Ma 			status = ocfs2_find_path(inode, left_path, left_cpos);
3804ad5a4d70STao Ma 			if (status)
3805ad5a4d70STao Ma 				goto out;
3806ad5a4d70STao Ma 
3807ad5a4d70STao Ma 			new_el = path_leaf_el(left_path);
3808ad5a4d70STao Ma 
3809ad5a4d70STao Ma 			if (le16_to_cpu(new_el->l_next_free_rec) !=
3810ad5a4d70STao Ma 			    le16_to_cpu(new_el->l_count)) {
3811ad5a4d70STao Ma 				bh = path_leaf_bh(left_path);
3812ad5a4d70STao Ma 				eb = (struct ocfs2_extent_block *)bh->b_data;
3813ad5a4d70STao Ma 				OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb,
3814ad5a4d70STao Ma 								 eb);
3815ad5a4d70STao Ma 				goto out;
3816ad5a4d70STao Ma 			}
3817ad5a4d70STao Ma 			rec = &new_el->l_recs[
3818ad5a4d70STao Ma 				le16_to_cpu(new_el->l_next_free_rec) - 1];
3819ad5a4d70STao Ma 		}
3820ad5a4d70STao Ma 	}
3821328d5752SMark Fasheh 
3822328d5752SMark Fasheh 	/*
3823328d5752SMark Fasheh 	 * We're careful to check for an empty extent record here -
3824328d5752SMark Fasheh 	 * the merge code will know what to do if it sees one.
3825328d5752SMark Fasheh 	 */
3826ad5a4d70STao Ma 	if (rec) {
3827328d5752SMark Fasheh 		if (index == 1 && ocfs2_is_empty_extent(rec)) {
3828328d5752SMark Fasheh 			if (split_rec->e_cpos == el->l_recs[index].e_cpos)
3829328d5752SMark Fasheh 				ret = CONTIG_RIGHT;
3830328d5752SMark Fasheh 		} else {
3831328d5752SMark Fasheh 			ret = ocfs2_extent_contig(inode, rec, split_rec);
3832328d5752SMark Fasheh 		}
3833328d5752SMark Fasheh 	}
3834328d5752SMark Fasheh 
3835ad5a4d70STao Ma 	rec = NULL;
3836ad5a4d70STao Ma 	if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
3837ad5a4d70STao Ma 		rec = &el->l_recs[index + 1];
3838ad5a4d70STao Ma 	else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
3839ad5a4d70STao Ma 		 path->p_tree_depth > 0) {
3840ad5a4d70STao Ma 		status = ocfs2_find_cpos_for_right_leaf(inode->i_sb,
3841ad5a4d70STao Ma 							path, &right_cpos);
3842ad5a4d70STao Ma 		if (status)
3843ad5a4d70STao Ma 			goto out;
3844ad5a4d70STao Ma 
3845ad5a4d70STao Ma 		if (right_cpos == 0)
3846ad5a4d70STao Ma 			goto out;
3847ad5a4d70STao Ma 
3848ad5a4d70STao Ma 		right_path = ocfs2_new_path(path_root_bh(path),
3849ad5a4d70STao Ma 					    path_root_el(path));
3850ad5a4d70STao Ma 		if (!right_path)
3851ad5a4d70STao Ma 			goto out;
3852ad5a4d70STao Ma 
3853ad5a4d70STao Ma 		status = ocfs2_find_path(inode, right_path, right_cpos);
3854ad5a4d70STao Ma 		if (status)
3855ad5a4d70STao Ma 			goto out;
3856ad5a4d70STao Ma 
3857ad5a4d70STao Ma 		new_el = path_leaf_el(right_path);
3858ad5a4d70STao Ma 		rec = &new_el->l_recs[0];
3859ad5a4d70STao Ma 		if (ocfs2_is_empty_extent(rec)) {
3860ad5a4d70STao Ma 			if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
3861ad5a4d70STao Ma 				bh = path_leaf_bh(right_path);
3862ad5a4d70STao Ma 				eb = (struct ocfs2_extent_block *)bh->b_data;
3863ad5a4d70STao Ma 				OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb,
3864ad5a4d70STao Ma 								 eb);
3865ad5a4d70STao Ma 				goto out;
3866ad5a4d70STao Ma 			}
3867ad5a4d70STao Ma 			rec = &new_el->l_recs[1];
3868ad5a4d70STao Ma 		}
3869ad5a4d70STao Ma 	}
3870ad5a4d70STao Ma 
3871ad5a4d70STao Ma 	if (rec) {
3872328d5752SMark Fasheh 		enum ocfs2_contig_type contig_type;
3873328d5752SMark Fasheh 
3874328d5752SMark Fasheh 		contig_type = ocfs2_extent_contig(inode, rec, split_rec);
3875328d5752SMark Fasheh 
3876328d5752SMark Fasheh 		if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
3877328d5752SMark Fasheh 			ret = CONTIG_LEFTRIGHT;
3878328d5752SMark Fasheh 		else if (ret == CONTIG_NONE)
3879328d5752SMark Fasheh 			ret = contig_type;
3880328d5752SMark Fasheh 	}
3881328d5752SMark Fasheh 
3882ad5a4d70STao Ma out:
3883ad5a4d70STao Ma 	if (left_path)
3884ad5a4d70STao Ma 		ocfs2_free_path(left_path);
3885ad5a4d70STao Ma 	if (right_path)
3886ad5a4d70STao Ma 		ocfs2_free_path(right_path);
3887ad5a4d70STao Ma 
3888328d5752SMark Fasheh 	return ret;
3889328d5752SMark Fasheh }
3890328d5752SMark Fasheh 
3891dcd0538fSMark Fasheh static void ocfs2_figure_contig_type(struct inode *inode,
3892dcd0538fSMark Fasheh 				     struct ocfs2_insert_type *insert,
3893dcd0538fSMark Fasheh 				     struct ocfs2_extent_list *el,
3894dcd0538fSMark Fasheh 				     struct ocfs2_extent_rec *insert_rec)
3895dcd0538fSMark Fasheh {
3896dcd0538fSMark Fasheh 	int i;
3897dcd0538fSMark Fasheh 	enum ocfs2_contig_type contig_type = CONTIG_NONE;
3898dcd0538fSMark Fasheh 
3899e48edee2SMark Fasheh 	BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
3900e48edee2SMark Fasheh 
3901dcd0538fSMark Fasheh 	for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
3902dcd0538fSMark Fasheh 		contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
3903dcd0538fSMark Fasheh 						  insert_rec);
3904dcd0538fSMark Fasheh 		if (contig_type != CONTIG_NONE) {
3905dcd0538fSMark Fasheh 			insert->ins_contig_index = i;
3906dcd0538fSMark Fasheh 			break;
3907dcd0538fSMark Fasheh 		}
3908dcd0538fSMark Fasheh 	}
3909dcd0538fSMark Fasheh 	insert->ins_contig = contig_type;
3910dcd0538fSMark Fasheh }
3911dcd0538fSMark Fasheh 
3912dcd0538fSMark Fasheh /*
3913dcd0538fSMark Fasheh  * This should only be called against the righmost leaf extent list.
3914dcd0538fSMark Fasheh  *
3915dcd0538fSMark Fasheh  * ocfs2_figure_appending_type() will figure out whether we'll have to
3916dcd0538fSMark Fasheh  * insert at the tail of the rightmost leaf.
3917dcd0538fSMark Fasheh  *
3918dcd0538fSMark Fasheh  * This should also work against the dinode list for tree's with 0
3919dcd0538fSMark Fasheh  * depth. If we consider the dinode list to be the rightmost leaf node
3920dcd0538fSMark Fasheh  * then the logic here makes sense.
3921dcd0538fSMark Fasheh  */
3922dcd0538fSMark Fasheh static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
3923dcd0538fSMark Fasheh 					struct ocfs2_extent_list *el,
3924dcd0538fSMark Fasheh 					struct ocfs2_extent_rec *insert_rec)
3925dcd0538fSMark Fasheh {
3926dcd0538fSMark Fasheh 	int i;
3927dcd0538fSMark Fasheh 	u32 cpos = le32_to_cpu(insert_rec->e_cpos);
3928dcd0538fSMark Fasheh 	struct ocfs2_extent_rec *rec;
3929dcd0538fSMark Fasheh 
3930dcd0538fSMark Fasheh 	insert->ins_appending = APPEND_NONE;
3931dcd0538fSMark Fasheh 
3932e48edee2SMark Fasheh 	BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
3933dcd0538fSMark Fasheh 
3934dcd0538fSMark Fasheh 	if (!el->l_next_free_rec)
3935dcd0538fSMark Fasheh 		goto set_tail_append;
3936dcd0538fSMark Fasheh 
3937dcd0538fSMark Fasheh 	if (ocfs2_is_empty_extent(&el->l_recs[0])) {
3938dcd0538fSMark Fasheh 		/* Were all records empty? */
3939dcd0538fSMark Fasheh 		if (le16_to_cpu(el->l_next_free_rec) == 1)
3940dcd0538fSMark Fasheh 			goto set_tail_append;
3941dcd0538fSMark Fasheh 	}
3942dcd0538fSMark Fasheh 
3943dcd0538fSMark Fasheh 	i = le16_to_cpu(el->l_next_free_rec) - 1;
3944dcd0538fSMark Fasheh 	rec = &el->l_recs[i];
3945dcd0538fSMark Fasheh 
3946e48edee2SMark Fasheh 	if (cpos >=
3947e48edee2SMark Fasheh 	    (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
3948dcd0538fSMark Fasheh 		goto set_tail_append;
3949dcd0538fSMark Fasheh 
3950dcd0538fSMark Fasheh 	return;
3951dcd0538fSMark Fasheh 
3952dcd0538fSMark Fasheh set_tail_append:
3953dcd0538fSMark Fasheh 	insert->ins_appending = APPEND_TAIL;
3954dcd0538fSMark Fasheh }
3955dcd0538fSMark Fasheh 
3956dcd0538fSMark Fasheh /*
3957dcd0538fSMark Fasheh  * Helper function called at the begining of an insert.
3958dcd0538fSMark Fasheh  *
3959dcd0538fSMark Fasheh  * This computes a few things that are commonly used in the process of
3960dcd0538fSMark Fasheh  * inserting into the btree:
3961dcd0538fSMark Fasheh  *   - Whether the new extent is contiguous with an existing one.
3962dcd0538fSMark Fasheh  *   - The current tree depth.
3963dcd0538fSMark Fasheh  *   - Whether the insert is an appending one.
3964dcd0538fSMark Fasheh  *   - The total # of free records in the tree.
3965dcd0538fSMark Fasheh  *
3966dcd0538fSMark Fasheh  * All of the information is stored on the ocfs2_insert_type
3967dcd0538fSMark Fasheh  * structure.
3968dcd0538fSMark Fasheh  */
3969dcd0538fSMark Fasheh static int ocfs2_figure_insert_type(struct inode *inode,
3970dcd0538fSMark Fasheh 				    struct buffer_head *di_bh,
3971dcd0538fSMark Fasheh 				    struct buffer_head **last_eb_bh,
3972dcd0538fSMark Fasheh 				    struct ocfs2_extent_rec *insert_rec,
3973c77534f6STao Mao 				    int *free_records,
3974dcd0538fSMark Fasheh 				    struct ocfs2_insert_type *insert)
3975dcd0538fSMark Fasheh {
3976dcd0538fSMark Fasheh 	int ret;
3977dcd0538fSMark Fasheh 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3978dcd0538fSMark Fasheh 	struct ocfs2_extent_block *eb;
3979dcd0538fSMark Fasheh 	struct ocfs2_extent_list *el;
3980dcd0538fSMark Fasheh 	struct ocfs2_path *path = NULL;
3981dcd0538fSMark Fasheh 	struct buffer_head *bh = NULL;
3982dcd0538fSMark Fasheh 
3983328d5752SMark Fasheh 	insert->ins_split = SPLIT_NONE;
3984328d5752SMark Fasheh 
3985dcd0538fSMark Fasheh 	el = &di->id2.i_list;
3986dcd0538fSMark Fasheh 	insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
3987dcd0538fSMark Fasheh 
3988dcd0538fSMark Fasheh 	if (el->l_tree_depth) {
3989dcd0538fSMark Fasheh 		/*
3990dcd0538fSMark Fasheh 		 * If we have tree depth, we read in the
3991dcd0538fSMark Fasheh 		 * rightmost extent block ahead of time as
3992dcd0538fSMark Fasheh 		 * ocfs2_figure_insert_type() and ocfs2_add_branch()
3993dcd0538fSMark Fasheh 		 * may want it later.
3994dcd0538fSMark Fasheh 		 */
3995dcd0538fSMark Fasheh 		ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
3996dcd0538fSMark Fasheh 				       le64_to_cpu(di->i_last_eb_blk), &bh,
3997dcd0538fSMark Fasheh 				       OCFS2_BH_CACHED, inode);
3998dcd0538fSMark Fasheh 		if (ret) {
3999dcd0538fSMark Fasheh 			mlog_exit(ret);
4000dcd0538fSMark Fasheh 			goto out;
4001dcd0538fSMark Fasheh 		}
4002dcd0538fSMark Fasheh 		eb = (struct ocfs2_extent_block *) bh->b_data;
4003dcd0538fSMark Fasheh 		el = &eb->h_list;
4004dcd0538fSMark Fasheh 	}
4005dcd0538fSMark Fasheh 
4006dcd0538fSMark Fasheh 	/*
4007dcd0538fSMark Fasheh 	 * Unless we have a contiguous insert, we'll need to know if
4008dcd0538fSMark Fasheh 	 * there is room left in our allocation tree for another
4009dcd0538fSMark Fasheh 	 * extent record.
4010dcd0538fSMark Fasheh 	 *
4011dcd0538fSMark Fasheh 	 * XXX: This test is simplistic, we can search for empty
4012dcd0538fSMark Fasheh 	 * extent records too.
4013dcd0538fSMark Fasheh 	 */
4014c77534f6STao Mao 	*free_records = le16_to_cpu(el->l_count) -
4015dcd0538fSMark Fasheh 		le16_to_cpu(el->l_next_free_rec);
4016dcd0538fSMark Fasheh 
4017dcd0538fSMark Fasheh 	if (!insert->ins_tree_depth) {
4018dcd0538fSMark Fasheh 		ocfs2_figure_contig_type(inode, insert, el, insert_rec);
4019dcd0538fSMark Fasheh 		ocfs2_figure_appending_type(insert, el, insert_rec);
4020dcd0538fSMark Fasheh 		return 0;
4021dcd0538fSMark Fasheh 	}
4022dcd0538fSMark Fasheh 
4023dcd0538fSMark Fasheh 	path = ocfs2_new_inode_path(di_bh);
4024dcd0538fSMark Fasheh 	if (!path) {
4025dcd0538fSMark Fasheh 		ret = -ENOMEM;
4026dcd0538fSMark Fasheh 		mlog_errno(ret);
4027dcd0538fSMark Fasheh 		goto out;
4028dcd0538fSMark Fasheh 	}
4029dcd0538fSMark Fasheh 
4030dcd0538fSMark Fasheh 	/*
4031dcd0538fSMark Fasheh 	 * In the case that we're inserting past what the tree
4032dcd0538fSMark Fasheh 	 * currently accounts for, ocfs2_find_path() will return for
4033dcd0538fSMark Fasheh 	 * us the rightmost tree path. This is accounted for below in
4034dcd0538fSMark Fasheh 	 * the appending code.
4035dcd0538fSMark Fasheh 	 */
4036dcd0538fSMark Fasheh 	ret = ocfs2_find_path(inode, path, le32_to_cpu(insert_rec->e_cpos));
4037dcd0538fSMark Fasheh 	if (ret) {
4038dcd0538fSMark Fasheh 		mlog_errno(ret);
4039dcd0538fSMark Fasheh 		goto out;
4040dcd0538fSMark Fasheh 	}
4041dcd0538fSMark Fasheh 
4042dcd0538fSMark Fasheh 	el = path_leaf_el(path);
4043dcd0538fSMark Fasheh 
4044dcd0538fSMark Fasheh 	/*
4045dcd0538fSMark Fasheh 	 * Now that we have the path, there's two things we want to determine:
4046dcd0538fSMark Fasheh 	 * 1) Contiguousness (also set contig_index if this is so)
4047dcd0538fSMark Fasheh 	 *
4048dcd0538fSMark Fasheh 	 * 2) Are we doing an append? We can trivially break this up
4049dcd0538fSMark Fasheh          *     into two types of appends: simple record append, or a
4050dcd0538fSMark Fasheh          *     rotate inside the tail leaf.
4051dcd0538fSMark Fasheh 	 */
4052dcd0538fSMark Fasheh 	ocfs2_figure_contig_type(inode, insert, el, insert_rec);
4053dcd0538fSMark Fasheh 
4054dcd0538fSMark Fasheh 	/*
4055dcd0538fSMark Fasheh 	 * The insert code isn't quite ready to deal with all cases of
4056dcd0538fSMark Fasheh 	 * left contiguousness. Specifically, if it's an insert into
4057dcd0538fSMark Fasheh 	 * the 1st record in a leaf, it will require the adjustment of
4058e48edee2SMark Fasheh 	 * cluster count on the last record of the path directly to it's
4059dcd0538fSMark Fasheh 	 * left. For now, just catch that case and fool the layers
4060dcd0538fSMark Fasheh 	 * above us. This works just fine for tree_depth == 0, which
4061dcd0538fSMark Fasheh 	 * is why we allow that above.
4062dcd0538fSMark Fasheh 	 */
4063dcd0538fSMark Fasheh 	if (insert->ins_contig == CONTIG_LEFT &&
4064dcd0538fSMark Fasheh 	    insert->ins_contig_index == 0)
4065dcd0538fSMark Fasheh 		insert->ins_contig = CONTIG_NONE;
4066dcd0538fSMark Fasheh 
4067dcd0538fSMark Fasheh 	/*
4068dcd0538fSMark Fasheh 	 * Ok, so we can simply compare against last_eb to figure out
4069dcd0538fSMark Fasheh 	 * whether the path doesn't exist. This will only happen in
4070dcd0538fSMark Fasheh 	 * the case that we're doing a tail append, so maybe we can
4071dcd0538fSMark Fasheh 	 * take advantage of that information somehow.
4072dcd0538fSMark Fasheh 	 */
4073dcd0538fSMark Fasheh 	if (le64_to_cpu(di->i_last_eb_blk) == path_leaf_bh(path)->b_blocknr) {
4074dcd0538fSMark Fasheh 		/*
4075dcd0538fSMark Fasheh 		 * Ok, ocfs2_find_path() returned us the rightmost
4076dcd0538fSMark Fasheh 		 * tree path. This might be an appending insert. There are
4077dcd0538fSMark Fasheh 		 * two cases:
4078dcd0538fSMark Fasheh 		 *    1) We're doing a true append at the tail:
4079dcd0538fSMark Fasheh 		 *	-This might even be off the end of the leaf
4080dcd0538fSMark Fasheh 		 *    2) We're "appending" by rotating in the tail
4081dcd0538fSMark Fasheh 		 */
4082dcd0538fSMark Fasheh 		ocfs2_figure_appending_type(insert, el, insert_rec);
4083dcd0538fSMark Fasheh 	}
4084dcd0538fSMark Fasheh 
4085dcd0538fSMark Fasheh out:
4086dcd0538fSMark Fasheh 	ocfs2_free_path(path);
4087dcd0538fSMark Fasheh 
4088dcd0538fSMark Fasheh 	if (ret == 0)
4089dcd0538fSMark Fasheh 		*last_eb_bh = bh;
4090dcd0538fSMark Fasheh 	else
4091dcd0538fSMark Fasheh 		brelse(bh);
4092dcd0538fSMark Fasheh 	return ret;
4093dcd0538fSMark Fasheh }
4094dcd0538fSMark Fasheh 
4095dcd0538fSMark Fasheh /*
4096dcd0538fSMark Fasheh  * Insert an extent into an inode btree.
4097dcd0538fSMark Fasheh  *
4098dcd0538fSMark Fasheh  * The caller needs to update fe->i_clusters
4099dcd0538fSMark Fasheh  */
4100ccd979bdSMark Fasheh int ocfs2_insert_extent(struct ocfs2_super *osb,
41011fabe148SMark Fasheh 			handle_t *handle,
4102ccd979bdSMark Fasheh 			struct inode *inode,
4103ccd979bdSMark Fasheh 			struct buffer_head *fe_bh,
4104dcd0538fSMark Fasheh 			u32 cpos,
4105ccd979bdSMark Fasheh 			u64 start_blk,
4106ccd979bdSMark Fasheh 			u32 new_clusters,
41072ae99a60SMark Fasheh 			u8 flags,
4108ccd979bdSMark Fasheh 			struct ocfs2_alloc_context *meta_ac)
4109ccd979bdSMark Fasheh {
4110c3afcbb3SMark Fasheh 	int status;
4111c77534f6STao Mao 	int uninitialized_var(free_records);
4112ccd979bdSMark Fasheh 	struct buffer_head *last_eb_bh = NULL;
4113dcd0538fSMark Fasheh 	struct ocfs2_insert_type insert = {0, };
4114dcd0538fSMark Fasheh 	struct ocfs2_extent_rec rec;
4115ccd979bdSMark Fasheh 
41161afc32b9SMark Fasheh 	BUG_ON(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL);
41171afc32b9SMark Fasheh 
4118dcd0538fSMark Fasheh 	mlog(0, "add %u clusters at position %u to inode %llu\n",
4119dcd0538fSMark Fasheh 	     new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4120ccd979bdSMark Fasheh 
4121dcd0538fSMark Fasheh 	mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
4122dcd0538fSMark Fasheh 			(OCFS2_I(inode)->ip_clusters != cpos),
4123dcd0538fSMark Fasheh 			"Device %s, asking for sparse allocation: inode %llu, "
4124dcd0538fSMark Fasheh 			"cpos %u, clusters %u\n",
4125dcd0538fSMark Fasheh 			osb->dev_str,
4126dcd0538fSMark Fasheh 			(unsigned long long)OCFS2_I(inode)->ip_blkno, cpos,
4127dcd0538fSMark Fasheh 			OCFS2_I(inode)->ip_clusters);
4128ccd979bdSMark Fasheh 
4129e48edee2SMark Fasheh 	memset(&rec, 0, sizeof(rec));
4130dcd0538fSMark Fasheh 	rec.e_cpos = cpu_to_le32(cpos);
4131dcd0538fSMark Fasheh 	rec.e_blkno = cpu_to_le64(start_blk);
4132e48edee2SMark Fasheh 	rec.e_leaf_clusters = cpu_to_le16(new_clusters);
41332ae99a60SMark Fasheh 	rec.e_flags = flags;
4134ccd979bdSMark Fasheh 
4135dcd0538fSMark Fasheh 	status = ocfs2_figure_insert_type(inode, fe_bh, &last_eb_bh, &rec,
4136c77534f6STao Mao 					  &free_records, &insert);
4137ccd979bdSMark Fasheh 	if (status < 0) {
4138dcd0538fSMark Fasheh 		mlog_errno(status);
4139ccd979bdSMark Fasheh 		goto bail;
4140ccd979bdSMark Fasheh 	}
4141ccd979bdSMark Fasheh 
4142dcd0538fSMark Fasheh 	mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4143dcd0538fSMark Fasheh 	     "Insert.contig_index: %d, Insert.free_records: %d, "
4144dcd0538fSMark Fasheh 	     "Insert.tree_depth: %d\n",
4145dcd0538fSMark Fasheh 	     insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
4146c77534f6STao Mao 	     free_records, insert.ins_tree_depth);
4147dcd0538fSMark Fasheh 
4148c77534f6STao Mao 	if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
4149c3afcbb3SMark Fasheh 		status = ocfs2_grow_tree(inode, handle, fe_bh,
4150328d5752SMark Fasheh 					 &insert.ins_tree_depth, &last_eb_bh,
4151ccd979bdSMark Fasheh 					 meta_ac);
4152c3afcbb3SMark Fasheh 		if (status) {
4153ccd979bdSMark Fasheh 			mlog_errno(status);
4154ccd979bdSMark Fasheh 			goto bail;
4155ccd979bdSMark Fasheh 		}
4156c3afcbb3SMark Fasheh 	}
4157ccd979bdSMark Fasheh 
4158dcd0538fSMark Fasheh 	/* Finally, we can add clusters. This might rotate the tree for us. */
4159dcd0538fSMark Fasheh 	status = ocfs2_do_insert_extent(inode, handle, fe_bh, &rec, &insert);
4160ccd979bdSMark Fasheh 	if (status < 0)
4161ccd979bdSMark Fasheh 		mlog_errno(status);
416283418978SMark Fasheh 	else
416383418978SMark Fasheh 		ocfs2_extent_map_insert_rec(inode, &rec);
4164ccd979bdSMark Fasheh 
4165ccd979bdSMark Fasheh bail:
4166ccd979bdSMark Fasheh 	if (last_eb_bh)
4167ccd979bdSMark Fasheh 		brelse(last_eb_bh);
4168ccd979bdSMark Fasheh 
4169ccd979bdSMark Fasheh 	mlog_exit(status);
4170ccd979bdSMark Fasheh 	return status;
4171ccd979bdSMark Fasheh }
4172ccd979bdSMark Fasheh 
4173328d5752SMark Fasheh static void ocfs2_make_right_split_rec(struct super_block *sb,
4174328d5752SMark Fasheh 				       struct ocfs2_extent_rec *split_rec,
4175328d5752SMark Fasheh 				       u32 cpos,
4176328d5752SMark Fasheh 				       struct ocfs2_extent_rec *rec)
4177328d5752SMark Fasheh {
4178328d5752SMark Fasheh 	u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4179328d5752SMark Fasheh 	u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4180328d5752SMark Fasheh 
4181328d5752SMark Fasheh 	memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4182328d5752SMark Fasheh 
4183328d5752SMark Fasheh 	split_rec->e_cpos = cpu_to_le32(cpos);
4184328d5752SMark Fasheh 	split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4185328d5752SMark Fasheh 
4186328d5752SMark Fasheh 	split_rec->e_blkno = rec->e_blkno;
4187328d5752SMark Fasheh 	le64_add_cpu(&split_rec->e_blkno,
4188328d5752SMark Fasheh 		     ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4189328d5752SMark Fasheh 
4190328d5752SMark Fasheh 	split_rec->e_flags = rec->e_flags;
4191328d5752SMark Fasheh }
4192328d5752SMark Fasheh 
4193328d5752SMark Fasheh static int ocfs2_split_and_insert(struct inode *inode,
4194328d5752SMark Fasheh 				  handle_t *handle,
4195328d5752SMark Fasheh 				  struct ocfs2_path *path,
4196328d5752SMark Fasheh 				  struct buffer_head *di_bh,
4197328d5752SMark Fasheh 				  struct buffer_head **last_eb_bh,
4198328d5752SMark Fasheh 				  int split_index,
4199328d5752SMark Fasheh 				  struct ocfs2_extent_rec *orig_split_rec,
4200328d5752SMark Fasheh 				  struct ocfs2_alloc_context *meta_ac)
4201328d5752SMark Fasheh {
4202328d5752SMark Fasheh 	int ret = 0, depth;
4203328d5752SMark Fasheh 	unsigned int insert_range, rec_range, do_leftright = 0;
4204328d5752SMark Fasheh 	struct ocfs2_extent_rec tmprec;
4205328d5752SMark Fasheh 	struct ocfs2_extent_list *rightmost_el;
4206328d5752SMark Fasheh 	struct ocfs2_extent_rec rec;
4207328d5752SMark Fasheh 	struct ocfs2_extent_rec split_rec = *orig_split_rec;
4208328d5752SMark Fasheh 	struct ocfs2_insert_type insert;
4209328d5752SMark Fasheh 	struct ocfs2_extent_block *eb;
4210328d5752SMark Fasheh 	struct ocfs2_dinode *di;
4211328d5752SMark Fasheh 
4212328d5752SMark Fasheh leftright:
4213328d5752SMark Fasheh 	/*
4214328d5752SMark Fasheh 	 * Store a copy of the record on the stack - it might move
4215328d5752SMark Fasheh 	 * around as the tree is manipulated below.
4216328d5752SMark Fasheh 	 */
4217328d5752SMark Fasheh 	rec = path_leaf_el(path)->l_recs[split_index];
4218328d5752SMark Fasheh 
4219328d5752SMark Fasheh 	di = (struct ocfs2_dinode *)di_bh->b_data;
4220328d5752SMark Fasheh 	rightmost_el = &di->id2.i_list;
4221328d5752SMark Fasheh 
4222328d5752SMark Fasheh 	depth = le16_to_cpu(rightmost_el->l_tree_depth);
4223328d5752SMark Fasheh 	if (depth) {
4224328d5752SMark Fasheh 		BUG_ON(!(*last_eb_bh));
4225328d5752SMark Fasheh 		eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4226328d5752SMark Fasheh 		rightmost_el = &eb->h_list;
4227328d5752SMark Fasheh 	}
4228328d5752SMark Fasheh 
4229328d5752SMark Fasheh 	if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4230328d5752SMark Fasheh 	    le16_to_cpu(rightmost_el->l_count)) {
4231328d5752SMark Fasheh 		ret = ocfs2_grow_tree(inode, handle, di_bh, &depth, last_eb_bh,
4232328d5752SMark Fasheh 				      meta_ac);
4233328d5752SMark Fasheh 		if (ret) {
4234328d5752SMark Fasheh 			mlog_errno(ret);
4235328d5752SMark Fasheh 			goto out;
4236328d5752SMark Fasheh 		}
4237328d5752SMark Fasheh 	}
4238328d5752SMark Fasheh 
4239328d5752SMark Fasheh 	memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4240328d5752SMark Fasheh 	insert.ins_appending = APPEND_NONE;
4241328d5752SMark Fasheh 	insert.ins_contig = CONTIG_NONE;
4242328d5752SMark Fasheh 	insert.ins_tree_depth = depth;
4243328d5752SMark Fasheh 
4244328d5752SMark Fasheh 	insert_range = le32_to_cpu(split_rec.e_cpos) +
4245328d5752SMark Fasheh 		le16_to_cpu(split_rec.e_leaf_clusters);
4246328d5752SMark Fasheh 	rec_range = le32_to_cpu(rec.e_cpos) +
4247328d5752SMark Fasheh 		le16_to_cpu(rec.e_leaf_clusters);
4248328d5752SMark Fasheh 
4249328d5752SMark Fasheh 	if (split_rec.e_cpos == rec.e_cpos) {
4250328d5752SMark Fasheh 		insert.ins_split = SPLIT_LEFT;
4251328d5752SMark Fasheh 	} else if (insert_range == rec_range) {
4252328d5752SMark Fasheh 		insert.ins_split = SPLIT_RIGHT;
4253328d5752SMark Fasheh 	} else {
4254328d5752SMark Fasheh 		/*
4255328d5752SMark Fasheh 		 * Left/right split. We fake this as a right split
4256328d5752SMark Fasheh 		 * first and then make a second pass as a left split.
4257328d5752SMark Fasheh 		 */
4258328d5752SMark Fasheh 		insert.ins_split = SPLIT_RIGHT;
4259328d5752SMark Fasheh 
4260328d5752SMark Fasheh 		ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range,
4261328d5752SMark Fasheh 					   &rec);
4262328d5752SMark Fasheh 
4263328d5752SMark Fasheh 		split_rec = tmprec;
4264328d5752SMark Fasheh 
4265328d5752SMark Fasheh 		BUG_ON(do_leftright);
4266328d5752SMark Fasheh 		do_leftright = 1;
4267328d5752SMark Fasheh 	}
4268328d5752SMark Fasheh 
4269328d5752SMark Fasheh 	ret = ocfs2_do_insert_extent(inode, handle, di_bh, &split_rec,
4270328d5752SMark Fasheh 				     &insert);
4271328d5752SMark Fasheh 	if (ret) {
4272328d5752SMark Fasheh 		mlog_errno(ret);
4273328d5752SMark Fasheh 		goto out;
4274328d5752SMark Fasheh 	}
4275328d5752SMark Fasheh 
4276328d5752SMark Fasheh 	if (do_leftright == 1) {
4277328d5752SMark Fasheh 		u32 cpos;
4278328d5752SMark Fasheh 		struct ocfs2_extent_list *el;
4279328d5752SMark Fasheh 
4280328d5752SMark Fasheh 		do_leftright++;
4281328d5752SMark Fasheh 		split_rec = *orig_split_rec;
4282328d5752SMark Fasheh 
4283328d5752SMark Fasheh 		ocfs2_reinit_path(path, 1);
4284328d5752SMark Fasheh 
4285328d5752SMark Fasheh 		cpos = le32_to_cpu(split_rec.e_cpos);
4286328d5752SMark Fasheh 		ret = ocfs2_find_path(inode, path, cpos);
4287328d5752SMark Fasheh 		if (ret) {
4288328d5752SMark Fasheh 			mlog_errno(ret);
4289328d5752SMark Fasheh 			goto out;
4290328d5752SMark Fasheh 		}
4291328d5752SMark Fasheh 
4292328d5752SMark Fasheh 		el = path_leaf_el(path);
4293328d5752SMark Fasheh 		split_index = ocfs2_search_extent_list(el, cpos);
4294328d5752SMark Fasheh 		goto leftright;
4295328d5752SMark Fasheh 	}
4296328d5752SMark Fasheh out:
4297328d5752SMark Fasheh 
4298328d5752SMark Fasheh 	return ret;
4299328d5752SMark Fasheh }
4300328d5752SMark Fasheh 
4301328d5752SMark Fasheh /*
4302328d5752SMark Fasheh  * Mark part or all of the extent record at split_index in the leaf
4303328d5752SMark Fasheh  * pointed to by path as written. This removes the unwritten
4304328d5752SMark Fasheh  * extent flag.
4305328d5752SMark Fasheh  *
4306328d5752SMark Fasheh  * Care is taken to handle contiguousness so as to not grow the tree.
4307328d5752SMark Fasheh  *
4308328d5752SMark Fasheh  * meta_ac is not strictly necessary - we only truly need it if growth
4309328d5752SMark Fasheh  * of the tree is required. All other cases will degrade into a less
4310328d5752SMark Fasheh  * optimal tree layout.
4311328d5752SMark Fasheh  *
4312328d5752SMark Fasheh  * last_eb_bh should be the rightmost leaf block for any inode with a
4313328d5752SMark Fasheh  * btree. Since a split may grow the tree or a merge might shrink it, the caller cannot trust the contents of that buffer after this call.
4314328d5752SMark Fasheh  *
4315328d5752SMark Fasheh  * This code is optimized for readability - several passes might be
4316328d5752SMark Fasheh  * made over certain portions of the tree. All of those blocks will
4317328d5752SMark Fasheh  * have been brought into cache (and pinned via the journal), so the
4318328d5752SMark Fasheh  * extra overhead is not expressed in terms of disk reads.
4319328d5752SMark Fasheh  */
4320328d5752SMark Fasheh static int __ocfs2_mark_extent_written(struct inode *inode,
4321328d5752SMark Fasheh 				       struct buffer_head *di_bh,
4322328d5752SMark Fasheh 				       handle_t *handle,
4323328d5752SMark Fasheh 				       struct ocfs2_path *path,
4324328d5752SMark Fasheh 				       int split_index,
4325328d5752SMark Fasheh 				       struct ocfs2_extent_rec *split_rec,
4326328d5752SMark Fasheh 				       struct ocfs2_alloc_context *meta_ac,
4327328d5752SMark Fasheh 				       struct ocfs2_cached_dealloc_ctxt *dealloc)
4328328d5752SMark Fasheh {
4329328d5752SMark Fasheh 	int ret = 0;
4330328d5752SMark Fasheh 	struct ocfs2_extent_list *el = path_leaf_el(path);
4331e8aed345SMark Fasheh 	struct buffer_head *last_eb_bh = NULL;
4332328d5752SMark Fasheh 	struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
4333328d5752SMark Fasheh 	struct ocfs2_merge_ctxt ctxt;
4334328d5752SMark Fasheh 	struct ocfs2_extent_list *rightmost_el;
4335328d5752SMark Fasheh 
43363cf0c507SRoel Kluin 	if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) {
4337328d5752SMark Fasheh 		ret = -EIO;
4338328d5752SMark Fasheh 		mlog_errno(ret);
4339328d5752SMark Fasheh 		goto out;
4340328d5752SMark Fasheh 	}
4341328d5752SMark Fasheh 
4342328d5752SMark Fasheh 	if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
4343328d5752SMark Fasheh 	    ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
4344328d5752SMark Fasheh 	     (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
4345328d5752SMark Fasheh 		ret = -EIO;
4346328d5752SMark Fasheh 		mlog_errno(ret);
4347328d5752SMark Fasheh 		goto out;
4348328d5752SMark Fasheh 	}
4349328d5752SMark Fasheh 
4350ad5a4d70STao Ma 	ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, path, el,
4351328d5752SMark Fasheh 							    split_index,
4352328d5752SMark Fasheh 							    split_rec);
4353328d5752SMark Fasheh 
4354328d5752SMark Fasheh 	/*
4355328d5752SMark Fasheh 	 * The core merge / split code wants to know how much room is
4356328d5752SMark Fasheh 	 * left in this inodes allocation tree, so we pass the
4357328d5752SMark Fasheh 	 * rightmost extent list.
4358328d5752SMark Fasheh 	 */
4359328d5752SMark Fasheh 	if (path->p_tree_depth) {
4360328d5752SMark Fasheh 		struct ocfs2_extent_block *eb;
4361328d5752SMark Fasheh 		struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
4362328d5752SMark Fasheh 
4363328d5752SMark Fasheh 		ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
4364328d5752SMark Fasheh 				       le64_to_cpu(di->i_last_eb_blk),
4365328d5752SMark Fasheh 				       &last_eb_bh, OCFS2_BH_CACHED, inode);
4366328d5752SMark Fasheh 		if (ret) {
4367328d5752SMark Fasheh 			mlog_exit(ret);
4368328d5752SMark Fasheh 			goto out;
4369328d5752SMark Fasheh 		}
4370328d5752SMark Fasheh 
4371328d5752SMark Fasheh 		eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
4372328d5752SMark Fasheh 		if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
4373328d5752SMark Fasheh 			OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
4374328d5752SMark Fasheh 			ret = -EROFS;
4375328d5752SMark Fasheh 			goto out;
4376328d5752SMark Fasheh 		}
4377328d5752SMark Fasheh 
4378328d5752SMark Fasheh 		rightmost_el = &eb->h_list;
4379328d5752SMark Fasheh 	} else
4380328d5752SMark Fasheh 		rightmost_el = path_root_el(path);
4381328d5752SMark Fasheh 
4382328d5752SMark Fasheh 	if (rec->e_cpos == split_rec->e_cpos &&
4383328d5752SMark Fasheh 	    rec->e_leaf_clusters == split_rec->e_leaf_clusters)
4384328d5752SMark Fasheh 		ctxt.c_split_covers_rec = 1;
4385328d5752SMark Fasheh 	else
4386328d5752SMark Fasheh 		ctxt.c_split_covers_rec = 0;
4387328d5752SMark Fasheh 
4388328d5752SMark Fasheh 	ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
4389328d5752SMark Fasheh 
4390015452b1SMark Fasheh 	mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
4391015452b1SMark Fasheh 	     split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
4392015452b1SMark Fasheh 	     ctxt.c_split_covers_rec);
4393328d5752SMark Fasheh 
4394328d5752SMark Fasheh 	if (ctxt.c_contig_type == CONTIG_NONE) {
4395328d5752SMark Fasheh 		if (ctxt.c_split_covers_rec)
4396328d5752SMark Fasheh 			el->l_recs[split_index] = *split_rec;
4397328d5752SMark Fasheh 		else
4398328d5752SMark Fasheh 			ret = ocfs2_split_and_insert(inode, handle, path, di_bh,
4399328d5752SMark Fasheh 						     &last_eb_bh, split_index,
4400328d5752SMark Fasheh 						     split_rec, meta_ac);
4401328d5752SMark Fasheh 		if (ret)
4402328d5752SMark Fasheh 			mlog_errno(ret);
4403328d5752SMark Fasheh 	} else {
4404328d5752SMark Fasheh 		ret = ocfs2_try_to_merge_extent(inode, handle, path,
4405328d5752SMark Fasheh 						split_index, split_rec,
4406328d5752SMark Fasheh 						dealloc, &ctxt);
4407328d5752SMark Fasheh 		if (ret)
4408328d5752SMark Fasheh 			mlog_errno(ret);
4409328d5752SMark Fasheh 	}
4410328d5752SMark Fasheh 
4411328d5752SMark Fasheh out:
4412328d5752SMark Fasheh 	brelse(last_eb_bh);
4413328d5752SMark Fasheh 	return ret;
4414328d5752SMark Fasheh }
4415328d5752SMark Fasheh 
4416328d5752SMark Fasheh /*
4417328d5752SMark Fasheh  * Mark the already-existing extent at cpos as written for len clusters.
4418328d5752SMark Fasheh  *
4419328d5752SMark Fasheh  * If the existing extent is larger than the request, initiate a
4420328d5752SMark Fasheh  * split. An attempt will be made at merging with adjacent extents.
4421328d5752SMark Fasheh  *
4422328d5752SMark Fasheh  * The caller is responsible for passing down meta_ac if we'll need it.
4423328d5752SMark Fasheh  */
4424328d5752SMark Fasheh int ocfs2_mark_extent_written(struct inode *inode, struct buffer_head *di_bh,
4425328d5752SMark Fasheh 			      handle_t *handle, u32 cpos, u32 len, u32 phys,
4426328d5752SMark Fasheh 			      struct ocfs2_alloc_context *meta_ac,
4427328d5752SMark Fasheh 			      struct ocfs2_cached_dealloc_ctxt *dealloc)
4428328d5752SMark Fasheh {
4429328d5752SMark Fasheh 	int ret, index;
4430328d5752SMark Fasheh 	u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
4431328d5752SMark Fasheh 	struct ocfs2_extent_rec split_rec;
4432328d5752SMark Fasheh 	struct ocfs2_path *left_path = NULL;
4433328d5752SMark Fasheh 	struct ocfs2_extent_list *el;
4434328d5752SMark Fasheh 
4435328d5752SMark Fasheh 	mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
4436328d5752SMark Fasheh 	     inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
4437328d5752SMark Fasheh 
4438328d5752SMark Fasheh 	if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
4439328d5752SMark Fasheh 		ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
4440328d5752SMark Fasheh 			    "that are being written to, but the feature bit "
4441328d5752SMark Fasheh 			    "is not set in the super block.",
4442328d5752SMark Fasheh 			    (unsigned long long)OCFS2_I(inode)->ip_blkno);
4443328d5752SMark Fasheh 		ret = -EROFS;
4444328d5752SMark Fasheh 		goto out;
4445328d5752SMark Fasheh 	}
4446328d5752SMark Fasheh 
4447328d5752SMark Fasheh 	/*
4448328d5752SMark Fasheh 	 * XXX: This should be fixed up so that we just re-insert the
4449328d5752SMark Fasheh 	 * next extent records.
4450328d5752SMark Fasheh 	 */
4451328d5752SMark Fasheh 	ocfs2_extent_map_trunc(inode, 0);
4452328d5752SMark Fasheh 
4453328d5752SMark Fasheh 	left_path = ocfs2_new_inode_path(di_bh);
4454328d5752SMark Fasheh 	if (!left_path) {
4455328d5752SMark Fasheh 		ret = -ENOMEM;
4456328d5752SMark Fasheh 		mlog_errno(ret);
4457328d5752SMark Fasheh 		goto out;
4458328d5752SMark Fasheh 	}
4459328d5752SMark Fasheh 
4460328d5752SMark Fasheh 	ret = ocfs2_find_path(inode, left_path, cpos);
4461328d5752SMark Fasheh 	if (ret) {
4462328d5752SMark Fasheh 		mlog_errno(ret);
4463328d5752SMark Fasheh 		goto out;
4464328d5752SMark Fasheh 	}
4465328d5752SMark Fasheh 	el = path_leaf_el(left_path);
4466328d5752SMark Fasheh 
4467328d5752SMark Fasheh 	index = ocfs2_search_extent_list(el, cpos);
4468328d5752SMark Fasheh 	if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
4469328d5752SMark Fasheh 		ocfs2_error(inode->i_sb,
4470328d5752SMark Fasheh 			    "Inode %llu has an extent at cpos %u which can no "
4471328d5752SMark Fasheh 			    "longer be found.\n",
4472328d5752SMark Fasheh 			    (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
4473328d5752SMark Fasheh 		ret = -EROFS;
4474328d5752SMark Fasheh 		goto out;
4475328d5752SMark Fasheh 	}
4476328d5752SMark Fasheh 
4477328d5752SMark Fasheh 	memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
4478328d5752SMark Fasheh 	split_rec.e_cpos = cpu_to_le32(cpos);
4479328d5752SMark Fasheh 	split_rec.e_leaf_clusters = cpu_to_le16(len);
4480328d5752SMark Fasheh 	split_rec.e_blkno = cpu_to_le64(start_blkno);
4481328d5752SMark Fasheh 	split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
4482328d5752SMark Fasheh 	split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
4483328d5752SMark Fasheh 
4484328d5752SMark Fasheh 	ret = __ocfs2_mark_extent_written(inode, di_bh, handle, left_path,
4485328d5752SMark Fasheh 					  index, &split_rec, meta_ac, dealloc);
4486328d5752SMark Fasheh 	if (ret)
4487328d5752SMark Fasheh 		mlog_errno(ret);
4488328d5752SMark Fasheh 
4489328d5752SMark Fasheh out:
4490328d5752SMark Fasheh 	ocfs2_free_path(left_path);
4491328d5752SMark Fasheh 	return ret;
4492328d5752SMark Fasheh }
4493328d5752SMark Fasheh 
4494d0c7d708SMark Fasheh static int ocfs2_split_tree(struct inode *inode, struct buffer_head *di_bh,
4495d0c7d708SMark Fasheh 			    handle_t *handle, struct ocfs2_path *path,
4496d0c7d708SMark Fasheh 			    int index, u32 new_range,
4497d0c7d708SMark Fasheh 			    struct ocfs2_alloc_context *meta_ac)
4498d0c7d708SMark Fasheh {
4499d0c7d708SMark Fasheh 	int ret, depth, credits = handle->h_buffer_credits;
4500d0c7d708SMark Fasheh 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
4501d0c7d708SMark Fasheh 	struct buffer_head *last_eb_bh = NULL;
4502d0c7d708SMark Fasheh 	struct ocfs2_extent_block *eb;
4503d0c7d708SMark Fasheh 	struct ocfs2_extent_list *rightmost_el, *el;
4504d0c7d708SMark Fasheh 	struct ocfs2_extent_rec split_rec;
4505d0c7d708SMark Fasheh 	struct ocfs2_extent_rec *rec;
4506d0c7d708SMark Fasheh 	struct ocfs2_insert_type insert;
4507d0c7d708SMark Fasheh 
4508d0c7d708SMark Fasheh 	/*
4509d0c7d708SMark Fasheh 	 * Setup the record to split before we grow the tree.
4510d0c7d708SMark Fasheh 	 */
4511d0c7d708SMark Fasheh 	el = path_leaf_el(path);
4512d0c7d708SMark Fasheh 	rec = &el->l_recs[index];
4513d0c7d708SMark Fasheh 	ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec);
4514d0c7d708SMark Fasheh 
4515d0c7d708SMark Fasheh 	depth = path->p_tree_depth;
4516d0c7d708SMark Fasheh 	if (depth > 0) {
4517d0c7d708SMark Fasheh 		ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
4518d0c7d708SMark Fasheh 				       le64_to_cpu(di->i_last_eb_blk),
4519d0c7d708SMark Fasheh 				       &last_eb_bh, OCFS2_BH_CACHED, inode);
4520d0c7d708SMark Fasheh 		if (ret < 0) {
4521d0c7d708SMark Fasheh 			mlog_errno(ret);
4522d0c7d708SMark Fasheh 			goto out;
4523d0c7d708SMark Fasheh 		}
4524d0c7d708SMark Fasheh 
4525d0c7d708SMark Fasheh 		eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
4526d0c7d708SMark Fasheh 		rightmost_el = &eb->h_list;
4527d0c7d708SMark Fasheh 	} else
4528d0c7d708SMark Fasheh 		rightmost_el = path_leaf_el(path);
4529d0c7d708SMark Fasheh 
4530*811f933dSTao Ma 	credits += path->p_tree_depth +
4531*811f933dSTao Ma 		   ocfs2_extend_meta_needed(&di->id2.i_list);
4532d0c7d708SMark Fasheh 	ret = ocfs2_extend_trans(handle, credits);
4533d0c7d708SMark Fasheh 	if (ret) {
4534d0c7d708SMark Fasheh 		mlog_errno(ret);
4535d0c7d708SMark Fasheh 		goto out;
4536d0c7d708SMark Fasheh 	}
4537d0c7d708SMark Fasheh 
4538d0c7d708SMark Fasheh 	if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4539d0c7d708SMark Fasheh 	    le16_to_cpu(rightmost_el->l_count)) {
4540d0c7d708SMark Fasheh 		ret = ocfs2_grow_tree(inode, handle, di_bh, &depth, &last_eb_bh,
4541d0c7d708SMark Fasheh 				      meta_ac);
4542d0c7d708SMark Fasheh 		if (ret) {
4543d0c7d708SMark Fasheh 			mlog_errno(ret);
4544d0c7d708SMark Fasheh 			goto out;
4545d0c7d708SMark Fasheh 		}
4546d0c7d708SMark Fasheh 	}
4547d0c7d708SMark Fasheh 
4548d0c7d708SMark Fasheh 	memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4549d0c7d708SMark Fasheh 	insert.ins_appending = APPEND_NONE;
4550d0c7d708SMark Fasheh 	insert.ins_contig = CONTIG_NONE;
4551d0c7d708SMark Fasheh 	insert.ins_split = SPLIT_RIGHT;
4552d0c7d708SMark Fasheh 	insert.ins_tree_depth = depth;
4553d0c7d708SMark Fasheh 
4554d0c7d708SMark Fasheh 	ret = ocfs2_do_insert_extent(inode, handle, di_bh, &split_rec, &insert);
4555d0c7d708SMark Fasheh 	if (ret)
4556d0c7d708SMark Fasheh 		mlog_errno(ret);
4557d0c7d708SMark Fasheh 
4558d0c7d708SMark Fasheh out:
4559d0c7d708SMark Fasheh 	brelse(last_eb_bh);
4560d0c7d708SMark Fasheh 	return ret;
4561d0c7d708SMark Fasheh }
4562d0c7d708SMark Fasheh 
4563d0c7d708SMark Fasheh static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
4564d0c7d708SMark Fasheh 			      struct ocfs2_path *path, int index,
4565d0c7d708SMark Fasheh 			      struct ocfs2_cached_dealloc_ctxt *dealloc,
4566d0c7d708SMark Fasheh 			      u32 cpos, u32 len)
4567d0c7d708SMark Fasheh {
4568d0c7d708SMark Fasheh 	int ret;
4569d0c7d708SMark Fasheh 	u32 left_cpos, rec_range, trunc_range;
4570d0c7d708SMark Fasheh 	int wants_rotate = 0, is_rightmost_tree_rec = 0;
4571d0c7d708SMark Fasheh 	struct super_block *sb = inode->i_sb;
4572d0c7d708SMark Fasheh 	struct ocfs2_path *left_path = NULL;
4573d0c7d708SMark Fasheh 	struct ocfs2_extent_list *el = path_leaf_el(path);
4574d0c7d708SMark Fasheh 	struct ocfs2_extent_rec *rec;
4575d0c7d708SMark Fasheh 	struct ocfs2_extent_block *eb;
4576d0c7d708SMark Fasheh 
4577d0c7d708SMark Fasheh 	if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
4578d0c7d708SMark Fasheh 		ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc);
4579d0c7d708SMark Fasheh 		if (ret) {
4580d0c7d708SMark Fasheh 			mlog_errno(ret);
4581d0c7d708SMark Fasheh 			goto out;
4582d0c7d708SMark Fasheh 		}
4583d0c7d708SMark Fasheh 
4584d0c7d708SMark Fasheh 		index--;
4585d0c7d708SMark Fasheh 	}
4586d0c7d708SMark Fasheh 
4587d0c7d708SMark Fasheh 	if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
4588d0c7d708SMark Fasheh 	    path->p_tree_depth) {
4589d0c7d708SMark Fasheh 		/*
4590d0c7d708SMark Fasheh 		 * Check whether this is the rightmost tree record. If
4591d0c7d708SMark Fasheh 		 * we remove all of this record or part of its right
4592d0c7d708SMark Fasheh 		 * edge then an update of the record lengths above it
4593d0c7d708SMark Fasheh 		 * will be required.
4594d0c7d708SMark Fasheh 		 */
4595d0c7d708SMark Fasheh 		eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
4596d0c7d708SMark Fasheh 		if (eb->h_next_leaf_blk == 0)
4597d0c7d708SMark Fasheh 			is_rightmost_tree_rec = 1;
4598d0c7d708SMark Fasheh 	}
4599d0c7d708SMark Fasheh 
4600d0c7d708SMark Fasheh 	rec = &el->l_recs[index];
4601d0c7d708SMark Fasheh 	if (index == 0 && path->p_tree_depth &&
4602d0c7d708SMark Fasheh 	    le32_to_cpu(rec->e_cpos) == cpos) {
4603d0c7d708SMark Fasheh 		/*
4604d0c7d708SMark Fasheh 		 * Changing the leftmost offset (via partial or whole
4605d0c7d708SMark Fasheh 		 * record truncate) of an interior (or rightmost) path
4606d0c7d708SMark Fasheh 		 * means we have to update the subtree that is formed
4607d0c7d708SMark Fasheh 		 * by this leaf and the one to it's left.
4608d0c7d708SMark Fasheh 		 *
4609d0c7d708SMark Fasheh 		 * There are two cases we can skip:
4610d0c7d708SMark Fasheh 		 *   1) Path is the leftmost one in our inode tree.
4611d0c7d708SMark Fasheh 		 *   2) The leaf is rightmost and will be empty after
4612d0c7d708SMark Fasheh 		 *      we remove the extent record - the rotate code
4613d0c7d708SMark Fasheh 		 *      knows how to update the newly formed edge.
4614d0c7d708SMark Fasheh 		 */
4615d0c7d708SMark Fasheh 
4616d0c7d708SMark Fasheh 		ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path,
4617d0c7d708SMark Fasheh 						    &left_cpos);
4618d0c7d708SMark Fasheh 		if (ret) {
4619d0c7d708SMark Fasheh 			mlog_errno(ret);
4620d0c7d708SMark Fasheh 			goto out;
4621d0c7d708SMark Fasheh 		}
4622d0c7d708SMark Fasheh 
4623d0c7d708SMark Fasheh 		if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
4624d0c7d708SMark Fasheh 			left_path = ocfs2_new_path(path_root_bh(path),
4625d0c7d708SMark Fasheh 						   path_root_el(path));
4626d0c7d708SMark Fasheh 			if (!left_path) {
4627d0c7d708SMark Fasheh 				ret = -ENOMEM;
4628d0c7d708SMark Fasheh 				mlog_errno(ret);
4629d0c7d708SMark Fasheh 				goto out;
4630d0c7d708SMark Fasheh 			}
4631d0c7d708SMark Fasheh 
4632d0c7d708SMark Fasheh 			ret = ocfs2_find_path(inode, left_path, left_cpos);
4633d0c7d708SMark Fasheh 			if (ret) {
4634d0c7d708SMark Fasheh 				mlog_errno(ret);
4635d0c7d708SMark Fasheh 				goto out;
4636d0c7d708SMark Fasheh 			}
4637d0c7d708SMark Fasheh 		}
4638d0c7d708SMark Fasheh 	}
4639d0c7d708SMark Fasheh 
4640d0c7d708SMark Fasheh 	ret = ocfs2_extend_rotate_transaction(handle, 0,
4641d0c7d708SMark Fasheh 					      handle->h_buffer_credits,
4642d0c7d708SMark Fasheh 					      path);
4643d0c7d708SMark Fasheh 	if (ret) {
4644d0c7d708SMark Fasheh 		mlog_errno(ret);
4645d0c7d708SMark Fasheh 		goto out;
4646d0c7d708SMark Fasheh 	}
4647d0c7d708SMark Fasheh 
4648d0c7d708SMark Fasheh 	ret = ocfs2_journal_access_path(inode, handle, path);
4649d0c7d708SMark Fasheh 	if (ret) {
4650d0c7d708SMark Fasheh 		mlog_errno(ret);
4651d0c7d708SMark Fasheh 		goto out;
4652d0c7d708SMark Fasheh 	}
4653d0c7d708SMark Fasheh 
4654d0c7d708SMark Fasheh 	ret = ocfs2_journal_access_path(inode, handle, left_path);
4655d0c7d708SMark Fasheh 	if (ret) {
4656d0c7d708SMark Fasheh 		mlog_errno(ret);
4657d0c7d708SMark Fasheh 		goto out;
4658d0c7d708SMark Fasheh 	}
4659d0c7d708SMark Fasheh 
4660d0c7d708SMark Fasheh 	rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
4661d0c7d708SMark Fasheh 	trunc_range = cpos + len;
4662d0c7d708SMark Fasheh 
4663d0c7d708SMark Fasheh 	if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
4664d0c7d708SMark Fasheh 		int next_free;
4665d0c7d708SMark Fasheh 
4666d0c7d708SMark Fasheh 		memset(rec, 0, sizeof(*rec));
4667d0c7d708SMark Fasheh 		ocfs2_cleanup_merge(el, index);
4668d0c7d708SMark Fasheh 		wants_rotate = 1;
4669d0c7d708SMark Fasheh 
4670d0c7d708SMark Fasheh 		next_free = le16_to_cpu(el->l_next_free_rec);
4671d0c7d708SMark Fasheh 		if (is_rightmost_tree_rec && next_free > 1) {
4672d0c7d708SMark Fasheh 			/*
4673d0c7d708SMark Fasheh 			 * We skip the edge update if this path will
4674d0c7d708SMark Fasheh 			 * be deleted by the rotate code.
4675d0c7d708SMark Fasheh 			 */
4676d0c7d708SMark Fasheh 			rec = &el->l_recs[next_free - 1];
4677d0c7d708SMark Fasheh 			ocfs2_adjust_rightmost_records(inode, handle, path,
4678d0c7d708SMark Fasheh 						       rec);
4679d0c7d708SMark Fasheh 		}
4680d0c7d708SMark Fasheh 	} else if (le32_to_cpu(rec->e_cpos) == cpos) {
4681d0c7d708SMark Fasheh 		/* Remove leftmost portion of the record. */
4682d0c7d708SMark Fasheh 		le32_add_cpu(&rec->e_cpos, len);
4683d0c7d708SMark Fasheh 		le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
4684d0c7d708SMark Fasheh 		le16_add_cpu(&rec->e_leaf_clusters, -len);
4685d0c7d708SMark Fasheh 	} else if (rec_range == trunc_range) {
4686d0c7d708SMark Fasheh 		/* Remove rightmost portion of the record */
4687d0c7d708SMark Fasheh 		le16_add_cpu(&rec->e_leaf_clusters, -len);
4688d0c7d708SMark Fasheh 		if (is_rightmost_tree_rec)
4689d0c7d708SMark Fasheh 			ocfs2_adjust_rightmost_records(inode, handle, path, rec);
4690d0c7d708SMark Fasheh 	} else {
4691d0c7d708SMark Fasheh 		/* Caller should have trapped this. */
4692d0c7d708SMark Fasheh 		mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) "
4693d0c7d708SMark Fasheh 		     "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno,
4694d0c7d708SMark Fasheh 		     le32_to_cpu(rec->e_cpos),
4695d0c7d708SMark Fasheh 		     le16_to_cpu(rec->e_leaf_clusters), cpos, len);
4696d0c7d708SMark Fasheh 		BUG();
4697d0c7d708SMark Fasheh 	}
4698d0c7d708SMark Fasheh 
4699d0c7d708SMark Fasheh 	if (left_path) {
4700d0c7d708SMark Fasheh 		int subtree_index;
4701d0c7d708SMark Fasheh 
4702d0c7d708SMark Fasheh 		subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
4703d0c7d708SMark Fasheh 		ocfs2_complete_edge_insert(inode, handle, left_path, path,
4704d0c7d708SMark Fasheh 					   subtree_index);
4705d0c7d708SMark Fasheh 	}
4706d0c7d708SMark Fasheh 
4707d0c7d708SMark Fasheh 	ocfs2_journal_dirty(handle, path_leaf_bh(path));
4708d0c7d708SMark Fasheh 
4709d0c7d708SMark Fasheh 	ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc);
4710d0c7d708SMark Fasheh 	if (ret) {
4711d0c7d708SMark Fasheh 		mlog_errno(ret);
4712d0c7d708SMark Fasheh 		goto out;
4713d0c7d708SMark Fasheh 	}
4714d0c7d708SMark Fasheh 
4715d0c7d708SMark Fasheh out:
4716d0c7d708SMark Fasheh 	ocfs2_free_path(left_path);
4717d0c7d708SMark Fasheh 	return ret;
4718d0c7d708SMark Fasheh }
4719d0c7d708SMark Fasheh 
4720063c4561SMark Fasheh int ocfs2_remove_extent(struct inode *inode, struct buffer_head *di_bh,
4721d0c7d708SMark Fasheh 			u32 cpos, u32 len, handle_t *handle,
4722d0c7d708SMark Fasheh 			struct ocfs2_alloc_context *meta_ac,
4723d0c7d708SMark Fasheh 			struct ocfs2_cached_dealloc_ctxt *dealloc)
4724d0c7d708SMark Fasheh {
4725d0c7d708SMark Fasheh 	int ret, index;
4726d0c7d708SMark Fasheh 	u32 rec_range, trunc_range;
4727d0c7d708SMark Fasheh 	struct ocfs2_extent_rec *rec;
4728d0c7d708SMark Fasheh 	struct ocfs2_extent_list *el;
4729d0c7d708SMark Fasheh 	struct ocfs2_path *path;
4730d0c7d708SMark Fasheh 
4731d0c7d708SMark Fasheh 	ocfs2_extent_map_trunc(inode, 0);
4732d0c7d708SMark Fasheh 
4733d0c7d708SMark Fasheh 	path = ocfs2_new_inode_path(di_bh);
4734d0c7d708SMark Fasheh 	if (!path) {
4735d0c7d708SMark Fasheh 		ret = -ENOMEM;
4736d0c7d708SMark Fasheh 		mlog_errno(ret);
4737d0c7d708SMark Fasheh 		goto out;
4738d0c7d708SMark Fasheh 	}
4739d0c7d708SMark Fasheh 
4740d0c7d708SMark Fasheh 	ret = ocfs2_find_path(inode, path, cpos);
4741d0c7d708SMark Fasheh 	if (ret) {
4742d0c7d708SMark Fasheh 		mlog_errno(ret);
4743d0c7d708SMark Fasheh 		goto out;
4744d0c7d708SMark Fasheh 	}
4745d0c7d708SMark Fasheh 
4746d0c7d708SMark Fasheh 	el = path_leaf_el(path);
4747d0c7d708SMark Fasheh 	index = ocfs2_search_extent_list(el, cpos);
4748d0c7d708SMark Fasheh 	if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
4749d0c7d708SMark Fasheh 		ocfs2_error(inode->i_sb,
4750d0c7d708SMark Fasheh 			    "Inode %llu has an extent at cpos %u which can no "
4751d0c7d708SMark Fasheh 			    "longer be found.\n",
4752d0c7d708SMark Fasheh 			    (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
4753d0c7d708SMark Fasheh 		ret = -EROFS;
4754d0c7d708SMark Fasheh 		goto out;
4755d0c7d708SMark Fasheh 	}
4756d0c7d708SMark Fasheh 
4757d0c7d708SMark Fasheh 	/*
4758d0c7d708SMark Fasheh 	 * We have 3 cases of extent removal:
4759d0c7d708SMark Fasheh 	 *   1) Range covers the entire extent rec
4760d0c7d708SMark Fasheh 	 *   2) Range begins or ends on one edge of the extent rec
4761d0c7d708SMark Fasheh 	 *   3) Range is in the middle of the extent rec (no shared edges)
4762d0c7d708SMark Fasheh 	 *
4763d0c7d708SMark Fasheh 	 * For case 1 we remove the extent rec and left rotate to
4764d0c7d708SMark Fasheh 	 * fill the hole.
4765d0c7d708SMark Fasheh 	 *
4766d0c7d708SMark Fasheh 	 * For case 2 we just shrink the existing extent rec, with a
4767d0c7d708SMark Fasheh 	 * tree update if the shrinking edge is also the edge of an
4768d0c7d708SMark Fasheh 	 * extent block.
4769d0c7d708SMark Fasheh 	 *
4770d0c7d708SMark Fasheh 	 * For case 3 we do a right split to turn the extent rec into
4771d0c7d708SMark Fasheh 	 * something case 2 can handle.
4772d0c7d708SMark Fasheh 	 */
4773d0c7d708SMark Fasheh 	rec = &el->l_recs[index];
4774d0c7d708SMark Fasheh 	rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
4775d0c7d708SMark Fasheh 	trunc_range = cpos + len;
4776d0c7d708SMark Fasheh 
4777d0c7d708SMark Fasheh 	BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
4778d0c7d708SMark Fasheh 
4779d0c7d708SMark Fasheh 	mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
4780d0c7d708SMark Fasheh 	     "(cpos %u, len %u)\n",
4781d0c7d708SMark Fasheh 	     (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index,
4782d0c7d708SMark Fasheh 	     le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
4783d0c7d708SMark Fasheh 
4784d0c7d708SMark Fasheh 	if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
4785d0c7d708SMark Fasheh 		ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
4786d0c7d708SMark Fasheh 					 cpos, len);
4787d0c7d708SMark Fasheh 		if (ret) {
4788d0c7d708SMark Fasheh 			mlog_errno(ret);
4789d0c7d708SMark Fasheh 			goto out;
4790d0c7d708SMark Fasheh 		}
4791d0c7d708SMark Fasheh 	} else {
4792d0c7d708SMark Fasheh 		ret = ocfs2_split_tree(inode, di_bh, handle, path, index,
4793d0c7d708SMark Fasheh 				       trunc_range, meta_ac);
4794d0c7d708SMark Fasheh 		if (ret) {
4795d0c7d708SMark Fasheh 			mlog_errno(ret);
4796d0c7d708SMark Fasheh 			goto out;
4797d0c7d708SMark Fasheh 		}
4798d0c7d708SMark Fasheh 
4799d0c7d708SMark Fasheh 		/*
4800d0c7d708SMark Fasheh 		 * The split could have manipulated the tree enough to
4801d0c7d708SMark Fasheh 		 * move the record location, so we have to look for it again.
4802d0c7d708SMark Fasheh 		 */
4803d0c7d708SMark Fasheh 		ocfs2_reinit_path(path, 1);
4804d0c7d708SMark Fasheh 
4805d0c7d708SMark Fasheh 		ret = ocfs2_find_path(inode, path, cpos);
4806d0c7d708SMark Fasheh 		if (ret) {
4807d0c7d708SMark Fasheh 			mlog_errno(ret);
4808d0c7d708SMark Fasheh 			goto out;
4809d0c7d708SMark Fasheh 		}
4810d0c7d708SMark Fasheh 
4811d0c7d708SMark Fasheh 		el = path_leaf_el(path);
4812d0c7d708SMark Fasheh 		index = ocfs2_search_extent_list(el, cpos);
4813d0c7d708SMark Fasheh 		if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
4814d0c7d708SMark Fasheh 			ocfs2_error(inode->i_sb,
4815d0c7d708SMark Fasheh 				    "Inode %llu: split at cpos %u lost record.",
4816d0c7d708SMark Fasheh 				    (unsigned long long)OCFS2_I(inode)->ip_blkno,
4817d0c7d708SMark Fasheh 				    cpos);
4818d0c7d708SMark Fasheh 			ret = -EROFS;
4819d0c7d708SMark Fasheh 			goto out;
4820d0c7d708SMark Fasheh 		}
4821d0c7d708SMark Fasheh 
4822d0c7d708SMark Fasheh 		/*
4823d0c7d708SMark Fasheh 		 * Double check our values here. If anything is fishy,
4824d0c7d708SMark Fasheh 		 * it's easier to catch it at the top level.
4825d0c7d708SMark Fasheh 		 */
4826d0c7d708SMark Fasheh 		rec = &el->l_recs[index];
4827d0c7d708SMark Fasheh 		rec_range = le32_to_cpu(rec->e_cpos) +
4828d0c7d708SMark Fasheh 			ocfs2_rec_clusters(el, rec);
4829d0c7d708SMark Fasheh 		if (rec_range != trunc_range) {
4830d0c7d708SMark Fasheh 			ocfs2_error(inode->i_sb,
4831d0c7d708SMark Fasheh 				    "Inode %llu: error after split at cpos %u"
4832d0c7d708SMark Fasheh 				    "trunc len %u, existing record is (%u,%u)",
4833d0c7d708SMark Fasheh 				    (unsigned long long)OCFS2_I(inode)->ip_blkno,
4834d0c7d708SMark Fasheh 				    cpos, len, le32_to_cpu(rec->e_cpos),
4835d0c7d708SMark Fasheh 				    ocfs2_rec_clusters(el, rec));
4836d0c7d708SMark Fasheh 			ret = -EROFS;
4837d0c7d708SMark Fasheh 			goto out;
4838d0c7d708SMark Fasheh 		}
4839d0c7d708SMark Fasheh 
4840d0c7d708SMark Fasheh 		ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
4841d0c7d708SMark Fasheh 					 cpos, len);
4842d0c7d708SMark Fasheh 		if (ret) {
4843d0c7d708SMark Fasheh 			mlog_errno(ret);
4844d0c7d708SMark Fasheh 			goto out;
4845d0c7d708SMark Fasheh 		}
4846d0c7d708SMark Fasheh 	}
4847d0c7d708SMark Fasheh 
4848d0c7d708SMark Fasheh out:
4849d0c7d708SMark Fasheh 	ocfs2_free_path(path);
4850d0c7d708SMark Fasheh 	return ret;
4851d0c7d708SMark Fasheh }
4852d0c7d708SMark Fasheh 
4853063c4561SMark Fasheh int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
4854ccd979bdSMark Fasheh {
4855ccd979bdSMark Fasheh 	struct buffer_head *tl_bh = osb->osb_tl_bh;
4856ccd979bdSMark Fasheh 	struct ocfs2_dinode *di;
4857ccd979bdSMark Fasheh 	struct ocfs2_truncate_log *tl;
4858ccd979bdSMark Fasheh 
4859ccd979bdSMark Fasheh 	di = (struct ocfs2_dinode *) tl_bh->b_data;
4860ccd979bdSMark Fasheh 	tl = &di->id2.i_dealloc;
4861ccd979bdSMark Fasheh 
4862ccd979bdSMark Fasheh 	mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
4863ccd979bdSMark Fasheh 			"slot %d, invalid truncate log parameters: used = "
4864ccd979bdSMark Fasheh 			"%u, count = %u\n", osb->slot_num,
4865ccd979bdSMark Fasheh 			le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
4866ccd979bdSMark Fasheh 	return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
4867ccd979bdSMark Fasheh }
4868ccd979bdSMark Fasheh 
4869ccd979bdSMark Fasheh static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
4870ccd979bdSMark Fasheh 					   unsigned int new_start)
4871ccd979bdSMark Fasheh {
4872ccd979bdSMark Fasheh 	unsigned int tail_index;
4873ccd979bdSMark Fasheh 	unsigned int current_tail;
4874ccd979bdSMark Fasheh 
4875ccd979bdSMark Fasheh 	/* No records, nothing to coalesce */
4876ccd979bdSMark Fasheh 	if (!le16_to_cpu(tl->tl_used))
4877ccd979bdSMark Fasheh 		return 0;
4878ccd979bdSMark Fasheh 
4879ccd979bdSMark Fasheh 	tail_index = le16_to_cpu(tl->tl_used) - 1;
4880ccd979bdSMark Fasheh 	current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
4881ccd979bdSMark Fasheh 	current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
4882ccd979bdSMark Fasheh 
4883ccd979bdSMark Fasheh 	return current_tail == new_start;
4884ccd979bdSMark Fasheh }
4885ccd979bdSMark Fasheh 
4886063c4561SMark Fasheh int ocfs2_truncate_log_append(struct ocfs2_super *osb,
48871fabe148SMark Fasheh 			      handle_t *handle,
4888ccd979bdSMark Fasheh 			      u64 start_blk,
4889ccd979bdSMark Fasheh 			      unsigned int num_clusters)
4890ccd979bdSMark Fasheh {
4891ccd979bdSMark Fasheh 	int status, index;
4892ccd979bdSMark Fasheh 	unsigned int start_cluster, tl_count;
4893ccd979bdSMark Fasheh 	struct inode *tl_inode = osb->osb_tl_inode;
4894ccd979bdSMark Fasheh 	struct buffer_head *tl_bh = osb->osb_tl_bh;
4895ccd979bdSMark Fasheh 	struct ocfs2_dinode *di;
4896ccd979bdSMark Fasheh 	struct ocfs2_truncate_log *tl;
4897ccd979bdSMark Fasheh 
4898b0697053SMark Fasheh 	mlog_entry("start_blk = %llu, num_clusters = %u\n",
4899b0697053SMark Fasheh 		   (unsigned long long)start_blk, num_clusters);
4900ccd979bdSMark Fasheh 
49011b1dcc1bSJes Sorensen 	BUG_ON(mutex_trylock(&tl_inode->i_mutex));
4902ccd979bdSMark Fasheh 
4903ccd979bdSMark Fasheh 	start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
4904ccd979bdSMark Fasheh 
4905ccd979bdSMark Fasheh 	di = (struct ocfs2_dinode *) tl_bh->b_data;
4906ccd979bdSMark Fasheh 	tl = &di->id2.i_dealloc;
4907ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_DINODE(di)) {
4908ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
4909ccd979bdSMark Fasheh 		status = -EIO;
4910ccd979bdSMark Fasheh 		goto bail;
4911ccd979bdSMark Fasheh 	}
4912ccd979bdSMark Fasheh 
4913ccd979bdSMark Fasheh 	tl_count = le16_to_cpu(tl->tl_count);
4914ccd979bdSMark Fasheh 	mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
4915ccd979bdSMark Fasheh 			tl_count == 0,
4916b0697053SMark Fasheh 			"Truncate record count on #%llu invalid "
4917b0697053SMark Fasheh 			"wanted %u, actual %u\n",
4918b0697053SMark Fasheh 			(unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
4919ccd979bdSMark Fasheh 			ocfs2_truncate_recs_per_inode(osb->sb),
4920ccd979bdSMark Fasheh 			le16_to_cpu(tl->tl_count));
4921ccd979bdSMark Fasheh 
4922ccd979bdSMark Fasheh 	/* Caller should have known to flush before calling us. */
4923ccd979bdSMark Fasheh 	index = le16_to_cpu(tl->tl_used);
4924ccd979bdSMark Fasheh 	if (index >= tl_count) {
4925ccd979bdSMark Fasheh 		status = -ENOSPC;
4926ccd979bdSMark Fasheh 		mlog_errno(status);
4927ccd979bdSMark Fasheh 		goto bail;
4928ccd979bdSMark Fasheh 	}
4929ccd979bdSMark Fasheh 
4930ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle, tl_inode, tl_bh,
4931ccd979bdSMark Fasheh 				      OCFS2_JOURNAL_ACCESS_WRITE);
4932ccd979bdSMark Fasheh 	if (status < 0) {
4933ccd979bdSMark Fasheh 		mlog_errno(status);
4934ccd979bdSMark Fasheh 		goto bail;
4935ccd979bdSMark Fasheh 	}
4936ccd979bdSMark Fasheh 
4937ccd979bdSMark Fasheh 	mlog(0, "Log truncate of %u clusters starting at cluster %u to "
4938b0697053SMark Fasheh 	     "%llu (index = %d)\n", num_clusters, start_cluster,
4939b0697053SMark Fasheh 	     (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
4940ccd979bdSMark Fasheh 
4941ccd979bdSMark Fasheh 	if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
4942ccd979bdSMark Fasheh 		/*
4943ccd979bdSMark Fasheh 		 * Move index back to the record we are coalescing with.
4944ccd979bdSMark Fasheh 		 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
4945ccd979bdSMark Fasheh 		 */
4946ccd979bdSMark Fasheh 		index--;
4947ccd979bdSMark Fasheh 
4948ccd979bdSMark Fasheh 		num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
4949ccd979bdSMark Fasheh 		mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
4950ccd979bdSMark Fasheh 		     index, le32_to_cpu(tl->tl_recs[index].t_start),
4951ccd979bdSMark Fasheh 		     num_clusters);
4952ccd979bdSMark Fasheh 	} else {
4953ccd979bdSMark Fasheh 		tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
4954ccd979bdSMark Fasheh 		tl->tl_used = cpu_to_le16(index + 1);
4955ccd979bdSMark Fasheh 	}
4956ccd979bdSMark Fasheh 	tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
4957ccd979bdSMark Fasheh 
4958ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle, tl_bh);
4959ccd979bdSMark Fasheh 	if (status < 0) {
4960ccd979bdSMark Fasheh 		mlog_errno(status);
4961ccd979bdSMark Fasheh 		goto bail;
4962ccd979bdSMark Fasheh 	}
4963ccd979bdSMark Fasheh 
4964ccd979bdSMark Fasheh bail:
4965ccd979bdSMark Fasheh 	mlog_exit(status);
4966ccd979bdSMark Fasheh 	return status;
4967ccd979bdSMark Fasheh }
4968ccd979bdSMark Fasheh 
4969ccd979bdSMark Fasheh static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
49701fabe148SMark Fasheh 					 handle_t *handle,
4971ccd979bdSMark Fasheh 					 struct inode *data_alloc_inode,
4972ccd979bdSMark Fasheh 					 struct buffer_head *data_alloc_bh)
4973ccd979bdSMark Fasheh {
4974ccd979bdSMark Fasheh 	int status = 0;
4975ccd979bdSMark Fasheh 	int i;
4976ccd979bdSMark Fasheh 	unsigned int num_clusters;
4977ccd979bdSMark Fasheh 	u64 start_blk;
4978ccd979bdSMark Fasheh 	struct ocfs2_truncate_rec rec;
4979ccd979bdSMark Fasheh 	struct ocfs2_dinode *di;
4980ccd979bdSMark Fasheh 	struct ocfs2_truncate_log *tl;
4981ccd979bdSMark Fasheh 	struct inode *tl_inode = osb->osb_tl_inode;
4982ccd979bdSMark Fasheh 	struct buffer_head *tl_bh = osb->osb_tl_bh;
4983ccd979bdSMark Fasheh 
4984ccd979bdSMark Fasheh 	mlog_entry_void();
4985ccd979bdSMark Fasheh 
4986ccd979bdSMark Fasheh 	di = (struct ocfs2_dinode *) tl_bh->b_data;
4987ccd979bdSMark Fasheh 	tl = &di->id2.i_dealloc;
4988ccd979bdSMark Fasheh 	i = le16_to_cpu(tl->tl_used) - 1;
4989ccd979bdSMark Fasheh 	while (i >= 0) {
4990ccd979bdSMark Fasheh 		/* Caller has given us at least enough credits to
4991ccd979bdSMark Fasheh 		 * update the truncate log dinode */
4992ccd979bdSMark Fasheh 		status = ocfs2_journal_access(handle, tl_inode, tl_bh,
4993ccd979bdSMark Fasheh 					      OCFS2_JOURNAL_ACCESS_WRITE);
4994ccd979bdSMark Fasheh 		if (status < 0) {
4995ccd979bdSMark Fasheh 			mlog_errno(status);
4996ccd979bdSMark Fasheh 			goto bail;
4997ccd979bdSMark Fasheh 		}
4998ccd979bdSMark Fasheh 
4999ccd979bdSMark Fasheh 		tl->tl_used = cpu_to_le16(i);
5000ccd979bdSMark Fasheh 
5001ccd979bdSMark Fasheh 		status = ocfs2_journal_dirty(handle, tl_bh);
5002ccd979bdSMark Fasheh 		if (status < 0) {
5003ccd979bdSMark Fasheh 			mlog_errno(status);
5004ccd979bdSMark Fasheh 			goto bail;
5005ccd979bdSMark Fasheh 		}
5006ccd979bdSMark Fasheh 
5007ccd979bdSMark Fasheh 		/* TODO: Perhaps we can calculate the bulk of the
5008ccd979bdSMark Fasheh 		 * credits up front rather than extending like
5009ccd979bdSMark Fasheh 		 * this. */
5010ccd979bdSMark Fasheh 		status = ocfs2_extend_trans(handle,
5011ccd979bdSMark Fasheh 					    OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5012ccd979bdSMark Fasheh 		if (status < 0) {
5013ccd979bdSMark Fasheh 			mlog_errno(status);
5014ccd979bdSMark Fasheh 			goto bail;
5015ccd979bdSMark Fasheh 		}
5016ccd979bdSMark Fasheh 
5017ccd979bdSMark Fasheh 		rec = tl->tl_recs[i];
5018ccd979bdSMark Fasheh 		start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5019ccd979bdSMark Fasheh 						    le32_to_cpu(rec.t_start));
5020ccd979bdSMark Fasheh 		num_clusters = le32_to_cpu(rec.t_clusters);
5021ccd979bdSMark Fasheh 
5022ccd979bdSMark Fasheh 		/* if start_blk is not set, we ignore the record as
5023ccd979bdSMark Fasheh 		 * invalid. */
5024ccd979bdSMark Fasheh 		if (start_blk) {
5025ccd979bdSMark Fasheh 			mlog(0, "free record %d, start = %u, clusters = %u\n",
5026ccd979bdSMark Fasheh 			     i, le32_to_cpu(rec.t_start), num_clusters);
5027ccd979bdSMark Fasheh 
5028ccd979bdSMark Fasheh 			status = ocfs2_free_clusters(handle, data_alloc_inode,
5029ccd979bdSMark Fasheh 						     data_alloc_bh, start_blk,
5030ccd979bdSMark Fasheh 						     num_clusters);
5031ccd979bdSMark Fasheh 			if (status < 0) {
5032ccd979bdSMark Fasheh 				mlog_errno(status);
5033ccd979bdSMark Fasheh 				goto bail;
5034ccd979bdSMark Fasheh 			}
5035ccd979bdSMark Fasheh 		}
5036ccd979bdSMark Fasheh 		i--;
5037ccd979bdSMark Fasheh 	}
5038ccd979bdSMark Fasheh 
5039ccd979bdSMark Fasheh bail:
5040ccd979bdSMark Fasheh 	mlog_exit(status);
5041ccd979bdSMark Fasheh 	return status;
5042ccd979bdSMark Fasheh }
5043ccd979bdSMark Fasheh 
50441b1dcc1bSJes Sorensen /* Expects you to already be holding tl_inode->i_mutex */
5045063c4561SMark Fasheh int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5046ccd979bdSMark Fasheh {
5047ccd979bdSMark Fasheh 	int status;
5048ccd979bdSMark Fasheh 	unsigned int num_to_flush;
50491fabe148SMark Fasheh 	handle_t *handle;
5050ccd979bdSMark Fasheh 	struct inode *tl_inode = osb->osb_tl_inode;
5051ccd979bdSMark Fasheh 	struct inode *data_alloc_inode = NULL;
5052ccd979bdSMark Fasheh 	struct buffer_head *tl_bh = osb->osb_tl_bh;
5053ccd979bdSMark Fasheh 	struct buffer_head *data_alloc_bh = NULL;
5054ccd979bdSMark Fasheh 	struct ocfs2_dinode *di;
5055ccd979bdSMark Fasheh 	struct ocfs2_truncate_log *tl;
5056ccd979bdSMark Fasheh 
5057ccd979bdSMark Fasheh 	mlog_entry_void();
5058ccd979bdSMark Fasheh 
50591b1dcc1bSJes Sorensen 	BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5060ccd979bdSMark Fasheh 
5061ccd979bdSMark Fasheh 	di = (struct ocfs2_dinode *) tl_bh->b_data;
5062ccd979bdSMark Fasheh 	tl = &di->id2.i_dealloc;
5063ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_DINODE(di)) {
5064ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
5065ccd979bdSMark Fasheh 		status = -EIO;
5066e08dc8b9SMark Fasheh 		goto out;
5067ccd979bdSMark Fasheh 	}
5068ccd979bdSMark Fasheh 
5069ccd979bdSMark Fasheh 	num_to_flush = le16_to_cpu(tl->tl_used);
5070b0697053SMark Fasheh 	mlog(0, "Flush %u records from truncate log #%llu\n",
5071b0697053SMark Fasheh 	     num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
5072ccd979bdSMark Fasheh 	if (!num_to_flush) {
5073ccd979bdSMark Fasheh 		status = 0;
5074e08dc8b9SMark Fasheh 		goto out;
5075ccd979bdSMark Fasheh 	}
5076ccd979bdSMark Fasheh 
5077ccd979bdSMark Fasheh 	data_alloc_inode = ocfs2_get_system_file_inode(osb,
5078ccd979bdSMark Fasheh 						       GLOBAL_BITMAP_SYSTEM_INODE,
5079ccd979bdSMark Fasheh 						       OCFS2_INVALID_SLOT);
5080ccd979bdSMark Fasheh 	if (!data_alloc_inode) {
5081ccd979bdSMark Fasheh 		status = -EINVAL;
5082ccd979bdSMark Fasheh 		mlog(ML_ERROR, "Could not get bitmap inode!\n");
5083e08dc8b9SMark Fasheh 		goto out;
5084ccd979bdSMark Fasheh 	}
5085ccd979bdSMark Fasheh 
5086e08dc8b9SMark Fasheh 	mutex_lock(&data_alloc_inode->i_mutex);
5087e08dc8b9SMark Fasheh 
5088e63aecb6SMark Fasheh 	status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
5089ccd979bdSMark Fasheh 	if (status < 0) {
5090ccd979bdSMark Fasheh 		mlog_errno(status);
5091e08dc8b9SMark Fasheh 		goto out_mutex;
5092ccd979bdSMark Fasheh 	}
5093ccd979bdSMark Fasheh 
509465eff9ccSMark Fasheh 	handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
5095ccd979bdSMark Fasheh 	if (IS_ERR(handle)) {
5096ccd979bdSMark Fasheh 		status = PTR_ERR(handle);
5097ccd979bdSMark Fasheh 		mlog_errno(status);
5098e08dc8b9SMark Fasheh 		goto out_unlock;
5099ccd979bdSMark Fasheh 	}
5100ccd979bdSMark Fasheh 
5101ccd979bdSMark Fasheh 	status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5102ccd979bdSMark Fasheh 					       data_alloc_bh);
5103e08dc8b9SMark Fasheh 	if (status < 0)
5104ccd979bdSMark Fasheh 		mlog_errno(status);
5105ccd979bdSMark Fasheh 
510602dc1af4SMark Fasheh 	ocfs2_commit_trans(osb, handle);
5107ccd979bdSMark Fasheh 
5108e08dc8b9SMark Fasheh out_unlock:
5109e08dc8b9SMark Fasheh 	brelse(data_alloc_bh);
5110e63aecb6SMark Fasheh 	ocfs2_inode_unlock(data_alloc_inode, 1);
5111e08dc8b9SMark Fasheh 
5112e08dc8b9SMark Fasheh out_mutex:
5113e08dc8b9SMark Fasheh 	mutex_unlock(&data_alloc_inode->i_mutex);
5114ccd979bdSMark Fasheh 	iput(data_alloc_inode);
5115ccd979bdSMark Fasheh 
5116e08dc8b9SMark Fasheh out:
5117ccd979bdSMark Fasheh 	mlog_exit(status);
5118ccd979bdSMark Fasheh 	return status;
5119ccd979bdSMark Fasheh }
5120ccd979bdSMark Fasheh 
5121ccd979bdSMark Fasheh int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5122ccd979bdSMark Fasheh {
5123ccd979bdSMark Fasheh 	int status;
5124ccd979bdSMark Fasheh 	struct inode *tl_inode = osb->osb_tl_inode;
5125ccd979bdSMark Fasheh 
51261b1dcc1bSJes Sorensen 	mutex_lock(&tl_inode->i_mutex);
5127ccd979bdSMark Fasheh 	status = __ocfs2_flush_truncate_log(osb);
51281b1dcc1bSJes Sorensen 	mutex_unlock(&tl_inode->i_mutex);
5129ccd979bdSMark Fasheh 
5130ccd979bdSMark Fasheh 	return status;
5131ccd979bdSMark Fasheh }
5132ccd979bdSMark Fasheh 
5133c4028958SDavid Howells static void ocfs2_truncate_log_worker(struct work_struct *work)
5134ccd979bdSMark Fasheh {
5135ccd979bdSMark Fasheh 	int status;
5136c4028958SDavid Howells 	struct ocfs2_super *osb =
5137c4028958SDavid Howells 		container_of(work, struct ocfs2_super,
5138c4028958SDavid Howells 			     osb_truncate_log_wq.work);
5139ccd979bdSMark Fasheh 
5140ccd979bdSMark Fasheh 	mlog_entry_void();
5141ccd979bdSMark Fasheh 
5142ccd979bdSMark Fasheh 	status = ocfs2_flush_truncate_log(osb);
5143ccd979bdSMark Fasheh 	if (status < 0)
5144ccd979bdSMark Fasheh 		mlog_errno(status);
51454d0ddb2cSTao Ma 	else
51464d0ddb2cSTao Ma 		ocfs2_init_inode_steal_slot(osb);
5147ccd979bdSMark Fasheh 
5148ccd979bdSMark Fasheh 	mlog_exit(status);
5149ccd979bdSMark Fasheh }
5150ccd979bdSMark Fasheh 
5151ccd979bdSMark Fasheh #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5152ccd979bdSMark Fasheh void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
5153ccd979bdSMark Fasheh 				       int cancel)
5154ccd979bdSMark Fasheh {
5155ccd979bdSMark Fasheh 	if (osb->osb_tl_inode) {
5156ccd979bdSMark Fasheh 		/* We want to push off log flushes while truncates are
5157ccd979bdSMark Fasheh 		 * still running. */
5158ccd979bdSMark Fasheh 		if (cancel)
5159ccd979bdSMark Fasheh 			cancel_delayed_work(&osb->osb_truncate_log_wq);
5160ccd979bdSMark Fasheh 
5161ccd979bdSMark Fasheh 		queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
5162ccd979bdSMark Fasheh 				   OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
5163ccd979bdSMark Fasheh 	}
5164ccd979bdSMark Fasheh }
5165ccd979bdSMark Fasheh 
5166ccd979bdSMark Fasheh static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
5167ccd979bdSMark Fasheh 				       int slot_num,
5168ccd979bdSMark Fasheh 				       struct inode **tl_inode,
5169ccd979bdSMark Fasheh 				       struct buffer_head **tl_bh)
5170ccd979bdSMark Fasheh {
5171ccd979bdSMark Fasheh 	int status;
5172ccd979bdSMark Fasheh 	struct inode *inode = NULL;
5173ccd979bdSMark Fasheh 	struct buffer_head *bh = NULL;
5174ccd979bdSMark Fasheh 
5175ccd979bdSMark Fasheh 	inode = ocfs2_get_system_file_inode(osb,
5176ccd979bdSMark Fasheh 					   TRUNCATE_LOG_SYSTEM_INODE,
5177ccd979bdSMark Fasheh 					   slot_num);
5178ccd979bdSMark Fasheh 	if (!inode) {
5179ccd979bdSMark Fasheh 		status = -EINVAL;
5180ccd979bdSMark Fasheh 		mlog(ML_ERROR, "Could not get load truncate log inode!\n");
5181ccd979bdSMark Fasheh 		goto bail;
5182ccd979bdSMark Fasheh 	}
5183ccd979bdSMark Fasheh 
5184ccd979bdSMark Fasheh 	status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
5185ccd979bdSMark Fasheh 				  OCFS2_BH_CACHED, inode);
5186ccd979bdSMark Fasheh 	if (status < 0) {
5187ccd979bdSMark Fasheh 		iput(inode);
5188ccd979bdSMark Fasheh 		mlog_errno(status);
5189ccd979bdSMark Fasheh 		goto bail;
5190ccd979bdSMark Fasheh 	}
5191ccd979bdSMark Fasheh 
5192ccd979bdSMark Fasheh 	*tl_inode = inode;
5193ccd979bdSMark Fasheh 	*tl_bh    = bh;
5194ccd979bdSMark Fasheh bail:
5195ccd979bdSMark Fasheh 	mlog_exit(status);
5196ccd979bdSMark Fasheh 	return status;
5197ccd979bdSMark Fasheh }
5198ccd979bdSMark Fasheh 
5199ccd979bdSMark Fasheh /* called during the 1st stage of node recovery. we stamp a clean
5200ccd979bdSMark Fasheh  * truncate log and pass back a copy for processing later. if the
5201ccd979bdSMark Fasheh  * truncate log does not require processing, a *tl_copy is set to
5202ccd979bdSMark Fasheh  * NULL. */
5203ccd979bdSMark Fasheh int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
5204ccd979bdSMark Fasheh 				      int slot_num,
5205ccd979bdSMark Fasheh 				      struct ocfs2_dinode **tl_copy)
5206ccd979bdSMark Fasheh {
5207ccd979bdSMark Fasheh 	int status;
5208ccd979bdSMark Fasheh 	struct inode *tl_inode = NULL;
5209ccd979bdSMark Fasheh 	struct buffer_head *tl_bh = NULL;
5210ccd979bdSMark Fasheh 	struct ocfs2_dinode *di;
5211ccd979bdSMark Fasheh 	struct ocfs2_truncate_log *tl;
5212ccd979bdSMark Fasheh 
5213ccd979bdSMark Fasheh 	*tl_copy = NULL;
5214ccd979bdSMark Fasheh 
5215ccd979bdSMark Fasheh 	mlog(0, "recover truncate log from slot %d\n", slot_num);
5216ccd979bdSMark Fasheh 
5217ccd979bdSMark Fasheh 	status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
5218ccd979bdSMark Fasheh 	if (status < 0) {
5219ccd979bdSMark Fasheh 		mlog_errno(status);
5220ccd979bdSMark Fasheh 		goto bail;
5221ccd979bdSMark Fasheh 	}
5222ccd979bdSMark Fasheh 
5223ccd979bdSMark Fasheh 	di = (struct ocfs2_dinode *) tl_bh->b_data;
5224ccd979bdSMark Fasheh 	tl = &di->id2.i_dealloc;
5225ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_DINODE(di)) {
5226ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_DINODE(tl_inode->i_sb, di);
5227ccd979bdSMark Fasheh 		status = -EIO;
5228ccd979bdSMark Fasheh 		goto bail;
5229ccd979bdSMark Fasheh 	}
5230ccd979bdSMark Fasheh 
5231ccd979bdSMark Fasheh 	if (le16_to_cpu(tl->tl_used)) {
5232ccd979bdSMark Fasheh 		mlog(0, "We'll have %u logs to recover\n",
5233ccd979bdSMark Fasheh 		     le16_to_cpu(tl->tl_used));
5234ccd979bdSMark Fasheh 
5235ccd979bdSMark Fasheh 		*tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
5236ccd979bdSMark Fasheh 		if (!(*tl_copy)) {
5237ccd979bdSMark Fasheh 			status = -ENOMEM;
5238ccd979bdSMark Fasheh 			mlog_errno(status);
5239ccd979bdSMark Fasheh 			goto bail;
5240ccd979bdSMark Fasheh 		}
5241ccd979bdSMark Fasheh 
5242ccd979bdSMark Fasheh 		/* Assuming the write-out below goes well, this copy
5243ccd979bdSMark Fasheh 		 * will be passed back to recovery for processing. */
5244ccd979bdSMark Fasheh 		memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
5245ccd979bdSMark Fasheh 
5246ccd979bdSMark Fasheh 		/* All we need to do to clear the truncate log is set
5247ccd979bdSMark Fasheh 		 * tl_used. */
5248ccd979bdSMark Fasheh 		tl->tl_used = 0;
5249ccd979bdSMark Fasheh 
5250ccd979bdSMark Fasheh 		status = ocfs2_write_block(osb, tl_bh, tl_inode);
5251ccd979bdSMark Fasheh 		if (status < 0) {
5252ccd979bdSMark Fasheh 			mlog_errno(status);
5253ccd979bdSMark Fasheh 			goto bail;
5254ccd979bdSMark Fasheh 		}
5255ccd979bdSMark Fasheh 	}
5256ccd979bdSMark Fasheh 
5257ccd979bdSMark Fasheh bail:
5258ccd979bdSMark Fasheh 	if (tl_inode)
5259ccd979bdSMark Fasheh 		iput(tl_inode);
5260ccd979bdSMark Fasheh 	if (tl_bh)
5261ccd979bdSMark Fasheh 		brelse(tl_bh);
5262ccd979bdSMark Fasheh 
5263ccd979bdSMark Fasheh 	if (status < 0 && (*tl_copy)) {
5264ccd979bdSMark Fasheh 		kfree(*tl_copy);
5265ccd979bdSMark Fasheh 		*tl_copy = NULL;
5266ccd979bdSMark Fasheh 	}
5267ccd979bdSMark Fasheh 
5268ccd979bdSMark Fasheh 	mlog_exit(status);
5269ccd979bdSMark Fasheh 	return status;
5270ccd979bdSMark Fasheh }
5271ccd979bdSMark Fasheh 
5272ccd979bdSMark Fasheh int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
5273ccd979bdSMark Fasheh 					 struct ocfs2_dinode *tl_copy)
5274ccd979bdSMark Fasheh {
5275ccd979bdSMark Fasheh 	int status = 0;
5276ccd979bdSMark Fasheh 	int i;
5277ccd979bdSMark Fasheh 	unsigned int clusters, num_recs, start_cluster;
5278ccd979bdSMark Fasheh 	u64 start_blk;
52791fabe148SMark Fasheh 	handle_t *handle;
5280ccd979bdSMark Fasheh 	struct inode *tl_inode = osb->osb_tl_inode;
5281ccd979bdSMark Fasheh 	struct ocfs2_truncate_log *tl;
5282ccd979bdSMark Fasheh 
5283ccd979bdSMark Fasheh 	mlog_entry_void();
5284ccd979bdSMark Fasheh 
5285ccd979bdSMark Fasheh 	if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
5286ccd979bdSMark Fasheh 		mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
5287ccd979bdSMark Fasheh 		return -EINVAL;
5288ccd979bdSMark Fasheh 	}
5289ccd979bdSMark Fasheh 
5290ccd979bdSMark Fasheh 	tl = &tl_copy->id2.i_dealloc;
5291ccd979bdSMark Fasheh 	num_recs = le16_to_cpu(tl->tl_used);
5292b0697053SMark Fasheh 	mlog(0, "cleanup %u records from %llu\n", num_recs,
52931ca1a111SMark Fasheh 	     (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
5294ccd979bdSMark Fasheh 
52951b1dcc1bSJes Sorensen 	mutex_lock(&tl_inode->i_mutex);
5296ccd979bdSMark Fasheh 	for(i = 0; i < num_recs; i++) {
5297ccd979bdSMark Fasheh 		if (ocfs2_truncate_log_needs_flush(osb)) {
5298ccd979bdSMark Fasheh 			status = __ocfs2_flush_truncate_log(osb);
5299ccd979bdSMark Fasheh 			if (status < 0) {
5300ccd979bdSMark Fasheh 				mlog_errno(status);
5301ccd979bdSMark Fasheh 				goto bail_up;
5302ccd979bdSMark Fasheh 			}
5303ccd979bdSMark Fasheh 		}
5304ccd979bdSMark Fasheh 
530565eff9ccSMark Fasheh 		handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
5306ccd979bdSMark Fasheh 		if (IS_ERR(handle)) {
5307ccd979bdSMark Fasheh 			status = PTR_ERR(handle);
5308ccd979bdSMark Fasheh 			mlog_errno(status);
5309ccd979bdSMark Fasheh 			goto bail_up;
5310ccd979bdSMark Fasheh 		}
5311ccd979bdSMark Fasheh 
5312ccd979bdSMark Fasheh 		clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
5313ccd979bdSMark Fasheh 		start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
5314ccd979bdSMark Fasheh 		start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
5315ccd979bdSMark Fasheh 
5316ccd979bdSMark Fasheh 		status = ocfs2_truncate_log_append(osb, handle,
5317ccd979bdSMark Fasheh 						   start_blk, clusters);
531802dc1af4SMark Fasheh 		ocfs2_commit_trans(osb, handle);
5319ccd979bdSMark Fasheh 		if (status < 0) {
5320ccd979bdSMark Fasheh 			mlog_errno(status);
5321ccd979bdSMark Fasheh 			goto bail_up;
5322ccd979bdSMark Fasheh 		}
5323ccd979bdSMark Fasheh 	}
5324ccd979bdSMark Fasheh 
5325ccd979bdSMark Fasheh bail_up:
53261b1dcc1bSJes Sorensen 	mutex_unlock(&tl_inode->i_mutex);
5327ccd979bdSMark Fasheh 
5328ccd979bdSMark Fasheh 	mlog_exit(status);
5329ccd979bdSMark Fasheh 	return status;
5330ccd979bdSMark Fasheh }
5331ccd979bdSMark Fasheh 
5332ccd979bdSMark Fasheh void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
5333ccd979bdSMark Fasheh {
5334ccd979bdSMark Fasheh 	int status;
5335ccd979bdSMark Fasheh 	struct inode *tl_inode = osb->osb_tl_inode;
5336ccd979bdSMark Fasheh 
5337ccd979bdSMark Fasheh 	mlog_entry_void();
5338ccd979bdSMark Fasheh 
5339ccd979bdSMark Fasheh 	if (tl_inode) {
5340ccd979bdSMark Fasheh 		cancel_delayed_work(&osb->osb_truncate_log_wq);
5341ccd979bdSMark Fasheh 		flush_workqueue(ocfs2_wq);
5342ccd979bdSMark Fasheh 
5343ccd979bdSMark Fasheh 		status = ocfs2_flush_truncate_log(osb);
5344ccd979bdSMark Fasheh 		if (status < 0)
5345ccd979bdSMark Fasheh 			mlog_errno(status);
5346ccd979bdSMark Fasheh 
5347ccd979bdSMark Fasheh 		brelse(osb->osb_tl_bh);
5348ccd979bdSMark Fasheh 		iput(osb->osb_tl_inode);
5349ccd979bdSMark Fasheh 	}
5350ccd979bdSMark Fasheh 
5351ccd979bdSMark Fasheh 	mlog_exit_void();
5352ccd979bdSMark Fasheh }
5353ccd979bdSMark Fasheh 
5354ccd979bdSMark Fasheh int ocfs2_truncate_log_init(struct ocfs2_super *osb)
5355ccd979bdSMark Fasheh {
5356ccd979bdSMark Fasheh 	int status;
5357ccd979bdSMark Fasheh 	struct inode *tl_inode = NULL;
5358ccd979bdSMark Fasheh 	struct buffer_head *tl_bh = NULL;
5359ccd979bdSMark Fasheh 
5360ccd979bdSMark Fasheh 	mlog_entry_void();
5361ccd979bdSMark Fasheh 
5362ccd979bdSMark Fasheh 	status = ocfs2_get_truncate_log_info(osb,
5363ccd979bdSMark Fasheh 					     osb->slot_num,
5364ccd979bdSMark Fasheh 					     &tl_inode,
5365ccd979bdSMark Fasheh 					     &tl_bh);
5366ccd979bdSMark Fasheh 	if (status < 0)
5367ccd979bdSMark Fasheh 		mlog_errno(status);
5368ccd979bdSMark Fasheh 
5369ccd979bdSMark Fasheh 	/* ocfs2_truncate_log_shutdown keys on the existence of
5370ccd979bdSMark Fasheh 	 * osb->osb_tl_inode so we don't set any of the osb variables
5371ccd979bdSMark Fasheh 	 * until we're sure all is well. */
5372c4028958SDavid Howells 	INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
5373c4028958SDavid Howells 			  ocfs2_truncate_log_worker);
5374ccd979bdSMark Fasheh 	osb->osb_tl_bh    = tl_bh;
5375ccd979bdSMark Fasheh 	osb->osb_tl_inode = tl_inode;
5376ccd979bdSMark Fasheh 
5377ccd979bdSMark Fasheh 	mlog_exit(status);
5378ccd979bdSMark Fasheh 	return status;
5379ccd979bdSMark Fasheh }
5380ccd979bdSMark Fasheh 
53812b604351SMark Fasheh /*
53822b604351SMark Fasheh  * Delayed de-allocation of suballocator blocks.
53832b604351SMark Fasheh  *
53842b604351SMark Fasheh  * Some sets of block de-allocations might involve multiple suballocator inodes.
53852b604351SMark Fasheh  *
53862b604351SMark Fasheh  * The locking for this can get extremely complicated, especially when
53872b604351SMark Fasheh  * the suballocator inodes to delete from aren't known until deep
53882b604351SMark Fasheh  * within an unrelated codepath.
53892b604351SMark Fasheh  *
53902b604351SMark Fasheh  * ocfs2_extent_block structures are a good example of this - an inode
53912b604351SMark Fasheh  * btree could have been grown by any number of nodes each allocating
53922b604351SMark Fasheh  * out of their own suballoc inode.
53932b604351SMark Fasheh  *
53942b604351SMark Fasheh  * These structures allow the delay of block de-allocation until a
53952b604351SMark Fasheh  * later time, when locking of multiple cluster inodes won't cause
53962b604351SMark Fasheh  * deadlock.
53972b604351SMark Fasheh  */
53982b604351SMark Fasheh 
53992b604351SMark Fasheh /*
54002b604351SMark Fasheh  * Describes a single block free from a suballocator
54012b604351SMark Fasheh  */
54022b604351SMark Fasheh struct ocfs2_cached_block_free {
54032b604351SMark Fasheh 	struct ocfs2_cached_block_free		*free_next;
54042b604351SMark Fasheh 	u64					free_blk;
54052b604351SMark Fasheh 	unsigned int				free_bit;
54062b604351SMark Fasheh };
54072b604351SMark Fasheh 
54082b604351SMark Fasheh struct ocfs2_per_slot_free_list {
54092b604351SMark Fasheh 	struct ocfs2_per_slot_free_list		*f_next_suballocator;
54102b604351SMark Fasheh 	int					f_inode_type;
54112b604351SMark Fasheh 	int					f_slot;
54122b604351SMark Fasheh 	struct ocfs2_cached_block_free		*f_first;
54132b604351SMark Fasheh };
54142b604351SMark Fasheh 
54152b604351SMark Fasheh static int ocfs2_free_cached_items(struct ocfs2_super *osb,
54162b604351SMark Fasheh 				   int sysfile_type,
54172b604351SMark Fasheh 				   int slot,
54182b604351SMark Fasheh 				   struct ocfs2_cached_block_free *head)
54192b604351SMark Fasheh {
54202b604351SMark Fasheh 	int ret;
54212b604351SMark Fasheh 	u64 bg_blkno;
54222b604351SMark Fasheh 	handle_t *handle;
54232b604351SMark Fasheh 	struct inode *inode;
54242b604351SMark Fasheh 	struct buffer_head *di_bh = NULL;
54252b604351SMark Fasheh 	struct ocfs2_cached_block_free *tmp;
54262b604351SMark Fasheh 
54272b604351SMark Fasheh 	inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
54282b604351SMark Fasheh 	if (!inode) {
54292b604351SMark Fasheh 		ret = -EINVAL;
54302b604351SMark Fasheh 		mlog_errno(ret);
54312b604351SMark Fasheh 		goto out;
54322b604351SMark Fasheh 	}
54332b604351SMark Fasheh 
54342b604351SMark Fasheh 	mutex_lock(&inode->i_mutex);
54352b604351SMark Fasheh 
5436e63aecb6SMark Fasheh 	ret = ocfs2_inode_lock(inode, &di_bh, 1);
54372b604351SMark Fasheh 	if (ret) {
54382b604351SMark Fasheh 		mlog_errno(ret);
54392b604351SMark Fasheh 		goto out_mutex;
54402b604351SMark Fasheh 	}
54412b604351SMark Fasheh 
54422b604351SMark Fasheh 	handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
54432b604351SMark Fasheh 	if (IS_ERR(handle)) {
54442b604351SMark Fasheh 		ret = PTR_ERR(handle);
54452b604351SMark Fasheh 		mlog_errno(ret);
54462b604351SMark Fasheh 		goto out_unlock;
54472b604351SMark Fasheh 	}
54482b604351SMark Fasheh 
54492b604351SMark Fasheh 	while (head) {
54502b604351SMark Fasheh 		bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
54512b604351SMark Fasheh 						      head->free_bit);
54522b604351SMark Fasheh 		mlog(0, "Free bit: (bit %u, blkno %llu)\n",
54532b604351SMark Fasheh 		     head->free_bit, (unsigned long long)head->free_blk);
54542b604351SMark Fasheh 
54552b604351SMark Fasheh 		ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
54562b604351SMark Fasheh 					       head->free_bit, bg_blkno, 1);
54572b604351SMark Fasheh 		if (ret) {
54582b604351SMark Fasheh 			mlog_errno(ret);
54592b604351SMark Fasheh 			goto out_journal;
54602b604351SMark Fasheh 		}
54612b604351SMark Fasheh 
54622b604351SMark Fasheh 		ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
54632b604351SMark Fasheh 		if (ret) {
54642b604351SMark Fasheh 			mlog_errno(ret);
54652b604351SMark Fasheh 			goto out_journal;
54662b604351SMark Fasheh 		}
54672b604351SMark Fasheh 
54682b604351SMark Fasheh 		tmp = head;
54692b604351SMark Fasheh 		head = head->free_next;
54702b604351SMark Fasheh 		kfree(tmp);
54712b604351SMark Fasheh 	}
54722b604351SMark Fasheh 
54732b604351SMark Fasheh out_journal:
54742b604351SMark Fasheh 	ocfs2_commit_trans(osb, handle);
54752b604351SMark Fasheh 
54762b604351SMark Fasheh out_unlock:
5477e63aecb6SMark Fasheh 	ocfs2_inode_unlock(inode, 1);
54782b604351SMark Fasheh 	brelse(di_bh);
54792b604351SMark Fasheh out_mutex:
54802b604351SMark Fasheh 	mutex_unlock(&inode->i_mutex);
54812b604351SMark Fasheh 	iput(inode);
54822b604351SMark Fasheh out:
54832b604351SMark Fasheh 	while(head) {
54842b604351SMark Fasheh 		/* Premature exit may have left some dangling items. */
54852b604351SMark Fasheh 		tmp = head;
54862b604351SMark Fasheh 		head = head->free_next;
54872b604351SMark Fasheh 		kfree(tmp);
54882b604351SMark Fasheh 	}
54892b604351SMark Fasheh 
54902b604351SMark Fasheh 	return ret;
54912b604351SMark Fasheh }
54922b604351SMark Fasheh 
54932b604351SMark Fasheh int ocfs2_run_deallocs(struct ocfs2_super *osb,
54942b604351SMark Fasheh 		       struct ocfs2_cached_dealloc_ctxt *ctxt)
54952b604351SMark Fasheh {
54962b604351SMark Fasheh 	int ret = 0, ret2;
54972b604351SMark Fasheh 	struct ocfs2_per_slot_free_list *fl;
54982b604351SMark Fasheh 
54992b604351SMark Fasheh 	if (!ctxt)
55002b604351SMark Fasheh 		return 0;
55012b604351SMark Fasheh 
55022b604351SMark Fasheh 	while (ctxt->c_first_suballocator) {
55032b604351SMark Fasheh 		fl = ctxt->c_first_suballocator;
55042b604351SMark Fasheh 
55052b604351SMark Fasheh 		if (fl->f_first) {
55062b604351SMark Fasheh 			mlog(0, "Free items: (type %u, slot %d)\n",
55072b604351SMark Fasheh 			     fl->f_inode_type, fl->f_slot);
55082b604351SMark Fasheh 			ret2 = ocfs2_free_cached_items(osb, fl->f_inode_type,
55092b604351SMark Fasheh 						       fl->f_slot, fl->f_first);
55102b604351SMark Fasheh 			if (ret2)
55112b604351SMark Fasheh 				mlog_errno(ret2);
55122b604351SMark Fasheh 			if (!ret)
55132b604351SMark Fasheh 				ret = ret2;
55142b604351SMark Fasheh 		}
55152b604351SMark Fasheh 
55162b604351SMark Fasheh 		ctxt->c_first_suballocator = fl->f_next_suballocator;
55172b604351SMark Fasheh 		kfree(fl);
55182b604351SMark Fasheh 	}
55192b604351SMark Fasheh 
55202b604351SMark Fasheh 	return ret;
55212b604351SMark Fasheh }
55222b604351SMark Fasheh 
55232b604351SMark Fasheh static struct ocfs2_per_slot_free_list *
55242b604351SMark Fasheh ocfs2_find_per_slot_free_list(int type,
55252b604351SMark Fasheh 			      int slot,
55262b604351SMark Fasheh 			      struct ocfs2_cached_dealloc_ctxt *ctxt)
55272b604351SMark Fasheh {
55282b604351SMark Fasheh 	struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
55292b604351SMark Fasheh 
55302b604351SMark Fasheh 	while (fl) {
55312b604351SMark Fasheh 		if (fl->f_inode_type == type && fl->f_slot == slot)
55322b604351SMark Fasheh 			return fl;
55332b604351SMark Fasheh 
55342b604351SMark Fasheh 		fl = fl->f_next_suballocator;
55352b604351SMark Fasheh 	}
55362b604351SMark Fasheh 
55372b604351SMark Fasheh 	fl = kmalloc(sizeof(*fl), GFP_NOFS);
55382b604351SMark Fasheh 	if (fl) {
55392b604351SMark Fasheh 		fl->f_inode_type = type;
55402b604351SMark Fasheh 		fl->f_slot = slot;
55412b604351SMark Fasheh 		fl->f_first = NULL;
55422b604351SMark Fasheh 		fl->f_next_suballocator = ctxt->c_first_suballocator;
55432b604351SMark Fasheh 
55442b604351SMark Fasheh 		ctxt->c_first_suballocator = fl;
55452b604351SMark Fasheh 	}
55462b604351SMark Fasheh 	return fl;
55472b604351SMark Fasheh }
55482b604351SMark Fasheh 
55492b604351SMark Fasheh static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
55502b604351SMark Fasheh 				     int type, int slot, u64 blkno,
55512b604351SMark Fasheh 				     unsigned int bit)
55522b604351SMark Fasheh {
55532b604351SMark Fasheh 	int ret;
55542b604351SMark Fasheh 	struct ocfs2_per_slot_free_list *fl;
55552b604351SMark Fasheh 	struct ocfs2_cached_block_free *item;
55562b604351SMark Fasheh 
55572b604351SMark Fasheh 	fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
55582b604351SMark Fasheh 	if (fl == NULL) {
55592b604351SMark Fasheh 		ret = -ENOMEM;
55602b604351SMark Fasheh 		mlog_errno(ret);
55612b604351SMark Fasheh 		goto out;
55622b604351SMark Fasheh 	}
55632b604351SMark Fasheh 
55642b604351SMark Fasheh 	item = kmalloc(sizeof(*item), GFP_NOFS);
55652b604351SMark Fasheh 	if (item == NULL) {
55662b604351SMark Fasheh 		ret = -ENOMEM;
55672b604351SMark Fasheh 		mlog_errno(ret);
55682b604351SMark Fasheh 		goto out;
55692b604351SMark Fasheh 	}
55702b604351SMark Fasheh 
55712b604351SMark Fasheh 	mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
55722b604351SMark Fasheh 	     type, slot, bit, (unsigned long long)blkno);
55732b604351SMark Fasheh 
55742b604351SMark Fasheh 	item->free_blk = blkno;
55752b604351SMark Fasheh 	item->free_bit = bit;
55762b604351SMark Fasheh 	item->free_next = fl->f_first;
55772b604351SMark Fasheh 
55782b604351SMark Fasheh 	fl->f_first = item;
55792b604351SMark Fasheh 
55802b604351SMark Fasheh 	ret = 0;
55812b604351SMark Fasheh out:
55822b604351SMark Fasheh 	return ret;
55832b604351SMark Fasheh }
55842b604351SMark Fasheh 
558559a5e416SMark Fasheh static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
558659a5e416SMark Fasheh 					 struct ocfs2_extent_block *eb)
558759a5e416SMark Fasheh {
558859a5e416SMark Fasheh 	return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
558959a5e416SMark Fasheh 					 le16_to_cpu(eb->h_suballoc_slot),
559059a5e416SMark Fasheh 					 le64_to_cpu(eb->h_blkno),
559159a5e416SMark Fasheh 					 le16_to_cpu(eb->h_suballoc_bit));
559259a5e416SMark Fasheh }
559359a5e416SMark Fasheh 
5594ccd979bdSMark Fasheh /* This function will figure out whether the currently last extent
5595ccd979bdSMark Fasheh  * block will be deleted, and if it will, what the new last extent
5596ccd979bdSMark Fasheh  * block will be so we can update his h_next_leaf_blk field, as well
5597ccd979bdSMark Fasheh  * as the dinodes i_last_eb_blk */
5598dcd0538fSMark Fasheh static int ocfs2_find_new_last_ext_blk(struct inode *inode,
55993a0782d0SMark Fasheh 				       unsigned int clusters_to_del,
5600dcd0538fSMark Fasheh 				       struct ocfs2_path *path,
5601ccd979bdSMark Fasheh 				       struct buffer_head **new_last_eb)
5602ccd979bdSMark Fasheh {
56033a0782d0SMark Fasheh 	int next_free, ret = 0;
5604dcd0538fSMark Fasheh 	u32 cpos;
56053a0782d0SMark Fasheh 	struct ocfs2_extent_rec *rec;
5606ccd979bdSMark Fasheh 	struct ocfs2_extent_block *eb;
5607ccd979bdSMark Fasheh 	struct ocfs2_extent_list *el;
5608ccd979bdSMark Fasheh 	struct buffer_head *bh = NULL;
5609ccd979bdSMark Fasheh 
5610ccd979bdSMark Fasheh 	*new_last_eb = NULL;
5611ccd979bdSMark Fasheh 
5612ccd979bdSMark Fasheh 	/* we have no tree, so of course, no last_eb. */
5613dcd0538fSMark Fasheh 	if (!path->p_tree_depth)
5614dcd0538fSMark Fasheh 		goto out;
5615ccd979bdSMark Fasheh 
5616ccd979bdSMark Fasheh 	/* trunc to zero special case - this makes tree_depth = 0
5617ccd979bdSMark Fasheh 	 * regardless of what it is.  */
56183a0782d0SMark Fasheh 	if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
5619dcd0538fSMark Fasheh 		goto out;
5620ccd979bdSMark Fasheh 
5621dcd0538fSMark Fasheh 	el = path_leaf_el(path);
5622ccd979bdSMark Fasheh 	BUG_ON(!el->l_next_free_rec);
5623ccd979bdSMark Fasheh 
56243a0782d0SMark Fasheh 	/*
56253a0782d0SMark Fasheh 	 * Make sure that this extent list will actually be empty
56263a0782d0SMark Fasheh 	 * after we clear away the data. We can shortcut out if
56273a0782d0SMark Fasheh 	 * there's more than one non-empty extent in the
56283a0782d0SMark Fasheh 	 * list. Otherwise, a check of the remaining extent is
56293a0782d0SMark Fasheh 	 * necessary.
56303a0782d0SMark Fasheh 	 */
56313a0782d0SMark Fasheh 	next_free = le16_to_cpu(el->l_next_free_rec);
56323a0782d0SMark Fasheh 	rec = NULL;
5633dcd0538fSMark Fasheh 	if (ocfs2_is_empty_extent(&el->l_recs[0])) {
56343a0782d0SMark Fasheh 		if (next_free > 2)
5635dcd0538fSMark Fasheh 			goto out;
56363a0782d0SMark Fasheh 
56373a0782d0SMark Fasheh 		/* We may have a valid extent in index 1, check it. */
56383a0782d0SMark Fasheh 		if (next_free == 2)
56393a0782d0SMark Fasheh 			rec = &el->l_recs[1];
56403a0782d0SMark Fasheh 
56413a0782d0SMark Fasheh 		/*
56423a0782d0SMark Fasheh 		 * Fall through - no more nonempty extents, so we want
56433a0782d0SMark Fasheh 		 * to delete this leaf.
56443a0782d0SMark Fasheh 		 */
56453a0782d0SMark Fasheh 	} else {
56463a0782d0SMark Fasheh 		if (next_free > 1)
5647dcd0538fSMark Fasheh 			goto out;
5648ccd979bdSMark Fasheh 
56493a0782d0SMark Fasheh 		rec = &el->l_recs[0];
56503a0782d0SMark Fasheh 	}
56513a0782d0SMark Fasheh 
56523a0782d0SMark Fasheh 	if (rec) {
56533a0782d0SMark Fasheh 		/*
56543a0782d0SMark Fasheh 		 * Check it we'll only be trimming off the end of this
56553a0782d0SMark Fasheh 		 * cluster.
56563a0782d0SMark Fasheh 		 */
5657e48edee2SMark Fasheh 		if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
56583a0782d0SMark Fasheh 			goto out;
56593a0782d0SMark Fasheh 	}
56603a0782d0SMark Fasheh 
5661dcd0538fSMark Fasheh 	ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
5662dcd0538fSMark Fasheh 	if (ret) {
5663dcd0538fSMark Fasheh 		mlog_errno(ret);
5664dcd0538fSMark Fasheh 		goto out;
5665ccd979bdSMark Fasheh 	}
5666ccd979bdSMark Fasheh 
5667dcd0538fSMark Fasheh 	ret = ocfs2_find_leaf(inode, path_root_el(path), cpos, &bh);
5668dcd0538fSMark Fasheh 	if (ret) {
5669dcd0538fSMark Fasheh 		mlog_errno(ret);
5670dcd0538fSMark Fasheh 		goto out;
5671ccd979bdSMark Fasheh 	}
5672dcd0538fSMark Fasheh 
5673ccd979bdSMark Fasheh 	eb = (struct ocfs2_extent_block *) bh->b_data;
5674ccd979bdSMark Fasheh 	el = &eb->h_list;
5675ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
5676ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
5677dcd0538fSMark Fasheh 		ret = -EROFS;
5678dcd0538fSMark Fasheh 		goto out;
5679ccd979bdSMark Fasheh 	}
5680ccd979bdSMark Fasheh 
5681ccd979bdSMark Fasheh 	*new_last_eb = bh;
5682ccd979bdSMark Fasheh 	get_bh(*new_last_eb);
5683dcd0538fSMark Fasheh 	mlog(0, "returning block %llu, (cpos: %u)\n",
5684dcd0538fSMark Fasheh 	     (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
5685dcd0538fSMark Fasheh out:
5686ccd979bdSMark Fasheh 	brelse(bh);
5687ccd979bdSMark Fasheh 
5688dcd0538fSMark Fasheh 	return ret;
5689ccd979bdSMark Fasheh }
5690ccd979bdSMark Fasheh 
56913a0782d0SMark Fasheh /*
56923a0782d0SMark Fasheh  * Trim some clusters off the rightmost edge of a tree. Only called
56933a0782d0SMark Fasheh  * during truncate.
56943a0782d0SMark Fasheh  *
56953a0782d0SMark Fasheh  * The caller needs to:
56963a0782d0SMark Fasheh  *   - start journaling of each path component.
56973a0782d0SMark Fasheh  *   - compute and fully set up any new last ext block
56983a0782d0SMark Fasheh  */
56993a0782d0SMark Fasheh static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
57003a0782d0SMark Fasheh 			   handle_t *handle, struct ocfs2_truncate_context *tc,
57013a0782d0SMark Fasheh 			   u32 clusters_to_del, u64 *delete_start)
57023a0782d0SMark Fasheh {
57033a0782d0SMark Fasheh 	int ret, i, index = path->p_tree_depth;
57043a0782d0SMark Fasheh 	u32 new_edge = 0;
57053a0782d0SMark Fasheh 	u64 deleted_eb = 0;
57063a0782d0SMark Fasheh 	struct buffer_head *bh;
57073a0782d0SMark Fasheh 	struct ocfs2_extent_list *el;
57083a0782d0SMark Fasheh 	struct ocfs2_extent_rec *rec;
57093a0782d0SMark Fasheh 
57103a0782d0SMark Fasheh 	*delete_start = 0;
57113a0782d0SMark Fasheh 
57123a0782d0SMark Fasheh 	while (index >= 0) {
57133a0782d0SMark Fasheh 		bh = path->p_node[index].bh;
57143a0782d0SMark Fasheh 		el = path->p_node[index].el;
57153a0782d0SMark Fasheh 
57163a0782d0SMark Fasheh 		mlog(0, "traveling tree (index = %d, block = %llu)\n",
57173a0782d0SMark Fasheh 		     index,  (unsigned long long)bh->b_blocknr);
57183a0782d0SMark Fasheh 
57193a0782d0SMark Fasheh 		BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
57203a0782d0SMark Fasheh 
57213a0782d0SMark Fasheh 		if (index !=
57223a0782d0SMark Fasheh 		    (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
57233a0782d0SMark Fasheh 			ocfs2_error(inode->i_sb,
57243a0782d0SMark Fasheh 				    "Inode %lu has invalid ext. block %llu",
57253a0782d0SMark Fasheh 				    inode->i_ino,
57263a0782d0SMark Fasheh 				    (unsigned long long)bh->b_blocknr);
57273a0782d0SMark Fasheh 			ret = -EROFS;
57283a0782d0SMark Fasheh 			goto out;
57293a0782d0SMark Fasheh 		}
57303a0782d0SMark Fasheh 
57313a0782d0SMark Fasheh find_tail_record:
57323a0782d0SMark Fasheh 		i = le16_to_cpu(el->l_next_free_rec) - 1;
57333a0782d0SMark Fasheh 		rec = &el->l_recs[i];
57343a0782d0SMark Fasheh 
57353a0782d0SMark Fasheh 		mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
57363a0782d0SMark Fasheh 		     "next = %u\n", i, le32_to_cpu(rec->e_cpos),
5737e48edee2SMark Fasheh 		     ocfs2_rec_clusters(el, rec),
57383a0782d0SMark Fasheh 		     (unsigned long long)le64_to_cpu(rec->e_blkno),
57393a0782d0SMark Fasheh 		     le16_to_cpu(el->l_next_free_rec));
57403a0782d0SMark Fasheh 
5741e48edee2SMark Fasheh 		BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
57423a0782d0SMark Fasheh 
57433a0782d0SMark Fasheh 		if (le16_to_cpu(el->l_tree_depth) == 0) {
57443a0782d0SMark Fasheh 			/*
57453a0782d0SMark Fasheh 			 * If the leaf block contains a single empty
57463a0782d0SMark Fasheh 			 * extent and no records, we can just remove
57473a0782d0SMark Fasheh 			 * the block.
57483a0782d0SMark Fasheh 			 */
57493a0782d0SMark Fasheh 			if (i == 0 && ocfs2_is_empty_extent(rec)) {
57503a0782d0SMark Fasheh 				memset(rec, 0,
57513a0782d0SMark Fasheh 				       sizeof(struct ocfs2_extent_rec));
57523a0782d0SMark Fasheh 				el->l_next_free_rec = cpu_to_le16(0);
57533a0782d0SMark Fasheh 
57543a0782d0SMark Fasheh 				goto delete;
57553a0782d0SMark Fasheh 			}
57563a0782d0SMark Fasheh 
57573a0782d0SMark Fasheh 			/*
57583a0782d0SMark Fasheh 			 * Remove any empty extents by shifting things
57593a0782d0SMark Fasheh 			 * left. That should make life much easier on
57603a0782d0SMark Fasheh 			 * the code below. This condition is rare
57613a0782d0SMark Fasheh 			 * enough that we shouldn't see a performance
57623a0782d0SMark Fasheh 			 * hit.
57633a0782d0SMark Fasheh 			 */
57643a0782d0SMark Fasheh 			if (ocfs2_is_empty_extent(&el->l_recs[0])) {
57653a0782d0SMark Fasheh 				le16_add_cpu(&el->l_next_free_rec, -1);
57663a0782d0SMark Fasheh 
57673a0782d0SMark Fasheh 				for(i = 0;
57683a0782d0SMark Fasheh 				    i < le16_to_cpu(el->l_next_free_rec); i++)
57693a0782d0SMark Fasheh 					el->l_recs[i] = el->l_recs[i + 1];
57703a0782d0SMark Fasheh 
57713a0782d0SMark Fasheh 				memset(&el->l_recs[i], 0,
57723a0782d0SMark Fasheh 				       sizeof(struct ocfs2_extent_rec));
57733a0782d0SMark Fasheh 
57743a0782d0SMark Fasheh 				/*
57753a0782d0SMark Fasheh 				 * We've modified our extent list. The
57763a0782d0SMark Fasheh 				 * simplest way to handle this change
57773a0782d0SMark Fasheh 				 * is to being the search from the
57783a0782d0SMark Fasheh 				 * start again.
57793a0782d0SMark Fasheh 				 */
57803a0782d0SMark Fasheh 				goto find_tail_record;
57813a0782d0SMark Fasheh 			}
57823a0782d0SMark Fasheh 
5783e48edee2SMark Fasheh 			le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
57843a0782d0SMark Fasheh 
57853a0782d0SMark Fasheh 			/*
57863a0782d0SMark Fasheh 			 * We'll use "new_edge" on our way back up the
57873a0782d0SMark Fasheh 			 * tree to know what our rightmost cpos is.
57883a0782d0SMark Fasheh 			 */
5789e48edee2SMark Fasheh 			new_edge = le16_to_cpu(rec->e_leaf_clusters);
57903a0782d0SMark Fasheh 			new_edge += le32_to_cpu(rec->e_cpos);
57913a0782d0SMark Fasheh 
57923a0782d0SMark Fasheh 			/*
57933a0782d0SMark Fasheh 			 * The caller will use this to delete data blocks.
57943a0782d0SMark Fasheh 			 */
57953a0782d0SMark Fasheh 			*delete_start = le64_to_cpu(rec->e_blkno)
57963a0782d0SMark Fasheh 				+ ocfs2_clusters_to_blocks(inode->i_sb,
5797e48edee2SMark Fasheh 					le16_to_cpu(rec->e_leaf_clusters));
57983a0782d0SMark Fasheh 
57993a0782d0SMark Fasheh 			/*
58003a0782d0SMark Fasheh 			 * If it's now empty, remove this record.
58013a0782d0SMark Fasheh 			 */
5802e48edee2SMark Fasheh 			if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
58033a0782d0SMark Fasheh 				memset(rec, 0,
58043a0782d0SMark Fasheh 				       sizeof(struct ocfs2_extent_rec));
58053a0782d0SMark Fasheh 				le16_add_cpu(&el->l_next_free_rec, -1);
58063a0782d0SMark Fasheh 			}
58073a0782d0SMark Fasheh 		} else {
58083a0782d0SMark Fasheh 			if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
58093a0782d0SMark Fasheh 				memset(rec, 0,
58103a0782d0SMark Fasheh 				       sizeof(struct ocfs2_extent_rec));
58113a0782d0SMark Fasheh 				le16_add_cpu(&el->l_next_free_rec, -1);
58123a0782d0SMark Fasheh 
58133a0782d0SMark Fasheh 				goto delete;
58143a0782d0SMark Fasheh 			}
58153a0782d0SMark Fasheh 
58163a0782d0SMark Fasheh 			/* Can this actually happen? */
58173a0782d0SMark Fasheh 			if (le16_to_cpu(el->l_next_free_rec) == 0)
58183a0782d0SMark Fasheh 				goto delete;
58193a0782d0SMark Fasheh 
58203a0782d0SMark Fasheh 			/*
58213a0782d0SMark Fasheh 			 * We never actually deleted any clusters
58223a0782d0SMark Fasheh 			 * because our leaf was empty. There's no
58233a0782d0SMark Fasheh 			 * reason to adjust the rightmost edge then.
58243a0782d0SMark Fasheh 			 */
58253a0782d0SMark Fasheh 			if (new_edge == 0)
58263a0782d0SMark Fasheh 				goto delete;
58273a0782d0SMark Fasheh 
5828e48edee2SMark Fasheh 			rec->e_int_clusters = cpu_to_le32(new_edge);
5829e48edee2SMark Fasheh 			le32_add_cpu(&rec->e_int_clusters,
58303a0782d0SMark Fasheh 				     -le32_to_cpu(rec->e_cpos));
58313a0782d0SMark Fasheh 
58323a0782d0SMark Fasheh 			 /*
58333a0782d0SMark Fasheh 			  * A deleted child record should have been
58343a0782d0SMark Fasheh 			  * caught above.
58353a0782d0SMark Fasheh 			  */
5836e48edee2SMark Fasheh 			 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
58373a0782d0SMark Fasheh 		}
58383a0782d0SMark Fasheh 
58393a0782d0SMark Fasheh delete:
58403a0782d0SMark Fasheh 		ret = ocfs2_journal_dirty(handle, bh);
58413a0782d0SMark Fasheh 		if (ret) {
58423a0782d0SMark Fasheh 			mlog_errno(ret);
58433a0782d0SMark Fasheh 			goto out;
58443a0782d0SMark Fasheh 		}
58453a0782d0SMark Fasheh 
58463a0782d0SMark Fasheh 		mlog(0, "extent list container %llu, after: record %d: "
58473a0782d0SMark Fasheh 		     "(%u, %u, %llu), next = %u.\n",
58483a0782d0SMark Fasheh 		     (unsigned long long)bh->b_blocknr, i,
5849e48edee2SMark Fasheh 		     le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
58503a0782d0SMark Fasheh 		     (unsigned long long)le64_to_cpu(rec->e_blkno),
58513a0782d0SMark Fasheh 		     le16_to_cpu(el->l_next_free_rec));
58523a0782d0SMark Fasheh 
58533a0782d0SMark Fasheh 		/*
58543a0782d0SMark Fasheh 		 * We must be careful to only attempt delete of an
58553a0782d0SMark Fasheh 		 * extent block (and not the root inode block).
58563a0782d0SMark Fasheh 		 */
58573a0782d0SMark Fasheh 		if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
58583a0782d0SMark Fasheh 			struct ocfs2_extent_block *eb =
58593a0782d0SMark Fasheh 				(struct ocfs2_extent_block *)bh->b_data;
58603a0782d0SMark Fasheh 
58613a0782d0SMark Fasheh 			/*
58623a0782d0SMark Fasheh 			 * Save this for use when processing the
58633a0782d0SMark Fasheh 			 * parent block.
58643a0782d0SMark Fasheh 			 */
58653a0782d0SMark Fasheh 			deleted_eb = le64_to_cpu(eb->h_blkno);
58663a0782d0SMark Fasheh 
58673a0782d0SMark Fasheh 			mlog(0, "deleting this extent block.\n");
58683a0782d0SMark Fasheh 
58693a0782d0SMark Fasheh 			ocfs2_remove_from_cache(inode, bh);
58703a0782d0SMark Fasheh 
5871e48edee2SMark Fasheh 			BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
58723a0782d0SMark Fasheh 			BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
58733a0782d0SMark Fasheh 			BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
58743a0782d0SMark Fasheh 
587559a5e416SMark Fasheh 			ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
58763a0782d0SMark Fasheh 			/* An error here is not fatal. */
58773a0782d0SMark Fasheh 			if (ret < 0)
58783a0782d0SMark Fasheh 				mlog_errno(ret);
58793a0782d0SMark Fasheh 		} else {
58803a0782d0SMark Fasheh 			deleted_eb = 0;
58813a0782d0SMark Fasheh 		}
58823a0782d0SMark Fasheh 
58833a0782d0SMark Fasheh 		index--;
58843a0782d0SMark Fasheh 	}
58853a0782d0SMark Fasheh 
58863a0782d0SMark Fasheh 	ret = 0;
58873a0782d0SMark Fasheh out:
58883a0782d0SMark Fasheh 	return ret;
58893a0782d0SMark Fasheh }
58903a0782d0SMark Fasheh 
5891ccd979bdSMark Fasheh static int ocfs2_do_truncate(struct ocfs2_super *osb,
5892ccd979bdSMark Fasheh 			     unsigned int clusters_to_del,
5893ccd979bdSMark Fasheh 			     struct inode *inode,
5894ccd979bdSMark Fasheh 			     struct buffer_head *fe_bh,
58951fabe148SMark Fasheh 			     handle_t *handle,
5896dcd0538fSMark Fasheh 			     struct ocfs2_truncate_context *tc,
5897dcd0538fSMark Fasheh 			     struct ocfs2_path *path)
5898ccd979bdSMark Fasheh {
58993a0782d0SMark Fasheh 	int status;
5900ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe;
5901ccd979bdSMark Fasheh 	struct ocfs2_extent_block *last_eb = NULL;
5902ccd979bdSMark Fasheh 	struct ocfs2_extent_list *el;
5903ccd979bdSMark Fasheh 	struct buffer_head *last_eb_bh = NULL;
5904ccd979bdSMark Fasheh 	u64 delete_blk = 0;
5905ccd979bdSMark Fasheh 
5906ccd979bdSMark Fasheh 	fe = (struct ocfs2_dinode *) fe_bh->b_data;
5907ccd979bdSMark Fasheh 
59083a0782d0SMark Fasheh 	status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
5909dcd0538fSMark Fasheh 					     path, &last_eb_bh);
5910ccd979bdSMark Fasheh 	if (status < 0) {
5911ccd979bdSMark Fasheh 		mlog_errno(status);
5912ccd979bdSMark Fasheh 		goto bail;
5913ccd979bdSMark Fasheh 	}
5914ccd979bdSMark Fasheh 
5915dcd0538fSMark Fasheh 	/*
5916dcd0538fSMark Fasheh 	 * Each component will be touched, so we might as well journal
5917dcd0538fSMark Fasheh 	 * here to avoid having to handle errors later.
5918dcd0538fSMark Fasheh 	 */
59193a0782d0SMark Fasheh 	status = ocfs2_journal_access_path(inode, handle, path);
5920ccd979bdSMark Fasheh 	if (status < 0) {
5921ccd979bdSMark Fasheh 		mlog_errno(status);
5922ccd979bdSMark Fasheh 		goto bail;
5923ccd979bdSMark Fasheh 	}
5924dcd0538fSMark Fasheh 
5925dcd0538fSMark Fasheh 	if (last_eb_bh) {
5926dcd0538fSMark Fasheh 		status = ocfs2_journal_access(handle, inode, last_eb_bh,
5927dcd0538fSMark Fasheh 					      OCFS2_JOURNAL_ACCESS_WRITE);
5928dcd0538fSMark Fasheh 		if (status < 0) {
5929dcd0538fSMark Fasheh 			mlog_errno(status);
5930dcd0538fSMark Fasheh 			goto bail;
5931dcd0538fSMark Fasheh 		}
5932dcd0538fSMark Fasheh 
5933dcd0538fSMark Fasheh 		last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5934dcd0538fSMark Fasheh 	}
5935dcd0538fSMark Fasheh 
5936ccd979bdSMark Fasheh 	el = &(fe->id2.i_list);
5937ccd979bdSMark Fasheh 
5938dcd0538fSMark Fasheh 	/*
5939dcd0538fSMark Fasheh 	 * Lower levels depend on this never happening, but it's best
5940dcd0538fSMark Fasheh 	 * to check it up here before changing the tree.
5941dcd0538fSMark Fasheh 	 */
5942e48edee2SMark Fasheh 	if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
5943dcd0538fSMark Fasheh 		ocfs2_error(inode->i_sb,
5944dcd0538fSMark Fasheh 			    "Inode %lu has an empty extent record, depth %u\n",
5945dcd0538fSMark Fasheh 			    inode->i_ino, le16_to_cpu(el->l_tree_depth));
59463a0782d0SMark Fasheh 		status = -EROFS;
5947dcd0538fSMark Fasheh 		goto bail;
5948dcd0538fSMark Fasheh 	}
5949dcd0538fSMark Fasheh 
5950ccd979bdSMark Fasheh 	spin_lock(&OCFS2_I(inode)->ip_lock);
5951ccd979bdSMark Fasheh 	OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
5952ccd979bdSMark Fasheh 				      clusters_to_del;
5953ccd979bdSMark Fasheh 	spin_unlock(&OCFS2_I(inode)->ip_lock);
5954ccd979bdSMark Fasheh 	le32_add_cpu(&fe->i_clusters, -clusters_to_del);
5955e535e2efSMark Fasheh 	inode->i_blocks = ocfs2_inode_sector_count(inode);
5956ccd979bdSMark Fasheh 
59573a0782d0SMark Fasheh 	status = ocfs2_trim_tree(inode, path, handle, tc,
59583a0782d0SMark Fasheh 				 clusters_to_del, &delete_blk);
59593a0782d0SMark Fasheh 	if (status) {
59603a0782d0SMark Fasheh 		mlog_errno(status);
59613a0782d0SMark Fasheh 		goto bail;
5962ccd979bdSMark Fasheh 	}
5963ccd979bdSMark Fasheh 
5964dcd0538fSMark Fasheh 	if (le32_to_cpu(fe->i_clusters) == 0) {
5965ccd979bdSMark Fasheh 		/* trunc to zero is a special case. */
5966ccd979bdSMark Fasheh 		el->l_tree_depth = 0;
5967ccd979bdSMark Fasheh 		fe->i_last_eb_blk = 0;
5968ccd979bdSMark Fasheh 	} else if (last_eb)
5969ccd979bdSMark Fasheh 		fe->i_last_eb_blk = last_eb->h_blkno;
5970ccd979bdSMark Fasheh 
5971ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle, fe_bh);
5972ccd979bdSMark Fasheh 	if (status < 0) {
5973ccd979bdSMark Fasheh 		mlog_errno(status);
5974ccd979bdSMark Fasheh 		goto bail;
5975ccd979bdSMark Fasheh 	}
5976ccd979bdSMark Fasheh 
5977ccd979bdSMark Fasheh 	if (last_eb) {
5978ccd979bdSMark Fasheh 		/* If there will be a new last extent block, then by
5979ccd979bdSMark Fasheh 		 * definition, there cannot be any leaves to the right of
5980ccd979bdSMark Fasheh 		 * him. */
5981ccd979bdSMark Fasheh 		last_eb->h_next_leaf_blk = 0;
5982ccd979bdSMark Fasheh 		status = ocfs2_journal_dirty(handle, last_eb_bh);
5983ccd979bdSMark Fasheh 		if (status < 0) {
5984ccd979bdSMark Fasheh 			mlog_errno(status);
5985ccd979bdSMark Fasheh 			goto bail;
5986ccd979bdSMark Fasheh 		}
5987ccd979bdSMark Fasheh 	}
5988ccd979bdSMark Fasheh 
59893a0782d0SMark Fasheh 	if (delete_blk) {
5990ccd979bdSMark Fasheh 		status = ocfs2_truncate_log_append(osb, handle, delete_blk,
5991ccd979bdSMark Fasheh 						   clusters_to_del);
5992ccd979bdSMark Fasheh 		if (status < 0) {
5993ccd979bdSMark Fasheh 			mlog_errno(status);
5994ccd979bdSMark Fasheh 			goto bail;
5995ccd979bdSMark Fasheh 		}
59963a0782d0SMark Fasheh 	}
5997ccd979bdSMark Fasheh 	status = 0;
5998ccd979bdSMark Fasheh bail:
5999dcd0538fSMark Fasheh 
6000ccd979bdSMark Fasheh 	mlog_exit(status);
6001ccd979bdSMark Fasheh 	return status;
6002ccd979bdSMark Fasheh }
6003ccd979bdSMark Fasheh 
600460b11392SMark Fasheh static int ocfs2_writeback_zero_func(handle_t *handle, struct buffer_head *bh)
600560b11392SMark Fasheh {
600660b11392SMark Fasheh 	set_buffer_uptodate(bh);
600760b11392SMark Fasheh 	mark_buffer_dirty(bh);
600860b11392SMark Fasheh 	return 0;
600960b11392SMark Fasheh }
601060b11392SMark Fasheh 
601160b11392SMark Fasheh static int ocfs2_ordered_zero_func(handle_t *handle, struct buffer_head *bh)
601260b11392SMark Fasheh {
601360b11392SMark Fasheh 	set_buffer_uptodate(bh);
601460b11392SMark Fasheh 	mark_buffer_dirty(bh);
601560b11392SMark Fasheh 	return ocfs2_journal_dirty_data(handle, bh);
601660b11392SMark Fasheh }
601760b11392SMark Fasheh 
60181d410a6eSMark Fasheh static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
60191d410a6eSMark Fasheh 				     unsigned int from, unsigned int to,
60201d410a6eSMark Fasheh 				     struct page *page, int zero, u64 *phys)
602160b11392SMark Fasheh {
60221d410a6eSMark Fasheh 	int ret, partial = 0;
602360b11392SMark Fasheh 
60241d410a6eSMark Fasheh 	ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
602560b11392SMark Fasheh 	if (ret)
602660b11392SMark Fasheh 		mlog_errno(ret);
602760b11392SMark Fasheh 
60281d410a6eSMark Fasheh 	if (zero)
6029eebd2aa3SChristoph Lameter 		zero_user_segment(page, from, to);
603060b11392SMark Fasheh 
603160b11392SMark Fasheh 	/*
603260b11392SMark Fasheh 	 * Need to set the buffers we zero'd into uptodate
603360b11392SMark Fasheh 	 * here if they aren't - ocfs2_map_page_blocks()
603460b11392SMark Fasheh 	 * might've skipped some
603560b11392SMark Fasheh 	 */
603660b11392SMark Fasheh 	if (ocfs2_should_order_data(inode)) {
603760b11392SMark Fasheh 		ret = walk_page_buffers(handle,
603860b11392SMark Fasheh 					page_buffers(page),
603960b11392SMark Fasheh 					from, to, &partial,
604060b11392SMark Fasheh 					ocfs2_ordered_zero_func);
604160b11392SMark Fasheh 		if (ret < 0)
604260b11392SMark Fasheh 			mlog_errno(ret);
604360b11392SMark Fasheh 	} else {
604460b11392SMark Fasheh 		ret = walk_page_buffers(handle, page_buffers(page),
604560b11392SMark Fasheh 					from, to, &partial,
604660b11392SMark Fasheh 					ocfs2_writeback_zero_func);
604760b11392SMark Fasheh 		if (ret < 0)
604860b11392SMark Fasheh 			mlog_errno(ret);
604960b11392SMark Fasheh 	}
605060b11392SMark Fasheh 
605160b11392SMark Fasheh 	if (!partial)
605260b11392SMark Fasheh 		SetPageUptodate(page);
605360b11392SMark Fasheh 
605460b11392SMark Fasheh 	flush_dcache_page(page);
60551d410a6eSMark Fasheh }
60561d410a6eSMark Fasheh 
60571d410a6eSMark Fasheh static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
60581d410a6eSMark Fasheh 				     loff_t end, struct page **pages,
60591d410a6eSMark Fasheh 				     int numpages, u64 phys, handle_t *handle)
60601d410a6eSMark Fasheh {
60611d410a6eSMark Fasheh 	int i;
60621d410a6eSMark Fasheh 	struct page *page;
60631d410a6eSMark Fasheh 	unsigned int from, to = PAGE_CACHE_SIZE;
60641d410a6eSMark Fasheh 	struct super_block *sb = inode->i_sb;
60651d410a6eSMark Fasheh 
60661d410a6eSMark Fasheh 	BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
60671d410a6eSMark Fasheh 
60681d410a6eSMark Fasheh 	if (numpages == 0)
60691d410a6eSMark Fasheh 		goto out;
60701d410a6eSMark Fasheh 
60711d410a6eSMark Fasheh 	to = PAGE_CACHE_SIZE;
60721d410a6eSMark Fasheh 	for(i = 0; i < numpages; i++) {
60731d410a6eSMark Fasheh 		page = pages[i];
60741d410a6eSMark Fasheh 
60751d410a6eSMark Fasheh 		from = start & (PAGE_CACHE_SIZE - 1);
60761d410a6eSMark Fasheh 		if ((end >> PAGE_CACHE_SHIFT) == page->index)
60771d410a6eSMark Fasheh 			to = end & (PAGE_CACHE_SIZE - 1);
60781d410a6eSMark Fasheh 
60791d410a6eSMark Fasheh 		BUG_ON(from > PAGE_CACHE_SIZE);
60801d410a6eSMark Fasheh 		BUG_ON(to > PAGE_CACHE_SIZE);
60811d410a6eSMark Fasheh 
60821d410a6eSMark Fasheh 		ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
60831d410a6eSMark Fasheh 					 &phys);
608460b11392SMark Fasheh 
608535edec1dSMark Fasheh 		start = (page->index + 1) << PAGE_CACHE_SHIFT;
608660b11392SMark Fasheh 	}
608760b11392SMark Fasheh out:
60881d410a6eSMark Fasheh 	if (pages)
60891d410a6eSMark Fasheh 		ocfs2_unlock_and_free_pages(pages, numpages);
609060b11392SMark Fasheh }
609160b11392SMark Fasheh 
609235edec1dSMark Fasheh static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
60931d410a6eSMark Fasheh 				struct page **pages, int *num)
609460b11392SMark Fasheh {
60951d410a6eSMark Fasheh 	int numpages, ret = 0;
609660b11392SMark Fasheh 	struct super_block *sb = inode->i_sb;
609760b11392SMark Fasheh 	struct address_space *mapping = inode->i_mapping;
609860b11392SMark Fasheh 	unsigned long index;
609935edec1dSMark Fasheh 	loff_t last_page_bytes;
610060b11392SMark Fasheh 
610135edec1dSMark Fasheh 	BUG_ON(start > end);
610260b11392SMark Fasheh 
610335edec1dSMark Fasheh 	BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
610435edec1dSMark Fasheh 	       (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
610535edec1dSMark Fasheh 
61061d410a6eSMark Fasheh 	numpages = 0;
610735edec1dSMark Fasheh 	last_page_bytes = PAGE_ALIGN(end);
610835edec1dSMark Fasheh 	index = start >> PAGE_CACHE_SHIFT;
610960b11392SMark Fasheh 	do {
611060b11392SMark Fasheh 		pages[numpages] = grab_cache_page(mapping, index);
611160b11392SMark Fasheh 		if (!pages[numpages]) {
611260b11392SMark Fasheh 			ret = -ENOMEM;
611360b11392SMark Fasheh 			mlog_errno(ret);
611460b11392SMark Fasheh 			goto out;
611560b11392SMark Fasheh 		}
611660b11392SMark Fasheh 
611760b11392SMark Fasheh 		numpages++;
611860b11392SMark Fasheh 		index++;
611935edec1dSMark Fasheh 	} while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
612060b11392SMark Fasheh 
612160b11392SMark Fasheh out:
612260b11392SMark Fasheh 	if (ret != 0) {
61231d410a6eSMark Fasheh 		if (pages)
61241d410a6eSMark Fasheh 			ocfs2_unlock_and_free_pages(pages, numpages);
612560b11392SMark Fasheh 		numpages = 0;
612660b11392SMark Fasheh 	}
612760b11392SMark Fasheh 
612860b11392SMark Fasheh 	*num = numpages;
612960b11392SMark Fasheh 
613060b11392SMark Fasheh 	return ret;
613160b11392SMark Fasheh }
613260b11392SMark Fasheh 
613360b11392SMark Fasheh /*
613460b11392SMark Fasheh  * Zero the area past i_size but still within an allocated
613560b11392SMark Fasheh  * cluster. This avoids exposing nonzero data on subsequent file
613660b11392SMark Fasheh  * extends.
613760b11392SMark Fasheh  *
613860b11392SMark Fasheh  * We need to call this before i_size is updated on the inode because
613960b11392SMark Fasheh  * otherwise block_write_full_page() will skip writeout of pages past
614060b11392SMark Fasheh  * i_size. The new_i_size parameter is passed for this reason.
614160b11392SMark Fasheh  */
614235edec1dSMark Fasheh int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
614335edec1dSMark Fasheh 				  u64 range_start, u64 range_end)
614460b11392SMark Fasheh {
61451d410a6eSMark Fasheh 	int ret = 0, numpages;
614660b11392SMark Fasheh 	struct page **pages = NULL;
614760b11392SMark Fasheh 	u64 phys;
61481d410a6eSMark Fasheh 	unsigned int ext_flags;
61491d410a6eSMark Fasheh 	struct super_block *sb = inode->i_sb;
615060b11392SMark Fasheh 
615160b11392SMark Fasheh 	/*
615260b11392SMark Fasheh 	 * File systems which don't support sparse files zero on every
615360b11392SMark Fasheh 	 * extend.
615460b11392SMark Fasheh 	 */
61551d410a6eSMark Fasheh 	if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
615660b11392SMark Fasheh 		return 0;
615760b11392SMark Fasheh 
61581d410a6eSMark Fasheh 	pages = kcalloc(ocfs2_pages_per_cluster(sb),
615960b11392SMark Fasheh 			sizeof(struct page *), GFP_NOFS);
616060b11392SMark Fasheh 	if (pages == NULL) {
616160b11392SMark Fasheh 		ret = -ENOMEM;
616260b11392SMark Fasheh 		mlog_errno(ret);
616360b11392SMark Fasheh 		goto out;
616460b11392SMark Fasheh 	}
616560b11392SMark Fasheh 
61661d410a6eSMark Fasheh 	if (range_start == range_end)
61671d410a6eSMark Fasheh 		goto out;
61681d410a6eSMark Fasheh 
61691d410a6eSMark Fasheh 	ret = ocfs2_extent_map_get_blocks(inode,
61701d410a6eSMark Fasheh 					  range_start >> sb->s_blocksize_bits,
61711d410a6eSMark Fasheh 					  &phys, NULL, &ext_flags);
617260b11392SMark Fasheh 	if (ret) {
617360b11392SMark Fasheh 		mlog_errno(ret);
617460b11392SMark Fasheh 		goto out;
617560b11392SMark Fasheh 	}
617660b11392SMark Fasheh 
61771d410a6eSMark Fasheh 	/*
61781d410a6eSMark Fasheh 	 * Tail is a hole, or is marked unwritten. In either case, we
61791d410a6eSMark Fasheh 	 * can count on read and write to return/push zero's.
61801d410a6eSMark Fasheh 	 */
61811d410a6eSMark Fasheh 	if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
618260b11392SMark Fasheh 		goto out;
618360b11392SMark Fasheh 
61841d410a6eSMark Fasheh 	ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
61851d410a6eSMark Fasheh 				   &numpages);
61861d410a6eSMark Fasheh 	if (ret) {
61871d410a6eSMark Fasheh 		mlog_errno(ret);
61881d410a6eSMark Fasheh 		goto out;
61891d410a6eSMark Fasheh 	}
61901d410a6eSMark Fasheh 
619135edec1dSMark Fasheh 	ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
619235edec1dSMark Fasheh 				 numpages, phys, handle);
619360b11392SMark Fasheh 
619460b11392SMark Fasheh 	/*
619560b11392SMark Fasheh 	 * Initiate writeout of the pages we zero'd here. We don't
619660b11392SMark Fasheh 	 * wait on them - the truncate_inode_pages() call later will
619760b11392SMark Fasheh 	 * do that for us.
619860b11392SMark Fasheh 	 */
619935edec1dSMark Fasheh 	ret = do_sync_mapping_range(inode->i_mapping, range_start,
620035edec1dSMark Fasheh 				    range_end - 1, SYNC_FILE_RANGE_WRITE);
620160b11392SMark Fasheh 	if (ret)
620260b11392SMark Fasheh 		mlog_errno(ret);
620360b11392SMark Fasheh 
620460b11392SMark Fasheh out:
620560b11392SMark Fasheh 	if (pages)
620660b11392SMark Fasheh 		kfree(pages);
620760b11392SMark Fasheh 
620860b11392SMark Fasheh 	return ret;
620960b11392SMark Fasheh }
621060b11392SMark Fasheh 
62111afc32b9SMark Fasheh static void ocfs2_zero_dinode_id2(struct inode *inode, struct ocfs2_dinode *di)
62121afc32b9SMark Fasheh {
62131afc32b9SMark Fasheh 	unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
62141afc32b9SMark Fasheh 
62151afc32b9SMark Fasheh 	memset(&di->id2, 0, blocksize - offsetof(struct ocfs2_dinode, id2));
62161afc32b9SMark Fasheh }
62171afc32b9SMark Fasheh 
62185b6a3a2bSMark Fasheh void ocfs2_dinode_new_extent_list(struct inode *inode,
62195b6a3a2bSMark Fasheh 				  struct ocfs2_dinode *di)
62205b6a3a2bSMark Fasheh {
62215b6a3a2bSMark Fasheh 	ocfs2_zero_dinode_id2(inode, di);
62225b6a3a2bSMark Fasheh 	di->id2.i_list.l_tree_depth = 0;
62235b6a3a2bSMark Fasheh 	di->id2.i_list.l_next_free_rec = 0;
62245b6a3a2bSMark Fasheh 	di->id2.i_list.l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(inode->i_sb));
62255b6a3a2bSMark Fasheh }
62265b6a3a2bSMark Fasheh 
62271afc32b9SMark Fasheh void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
62281afc32b9SMark Fasheh {
62291afc32b9SMark Fasheh 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
62301afc32b9SMark Fasheh 	struct ocfs2_inline_data *idata = &di->id2.i_data;
62311afc32b9SMark Fasheh 
62321afc32b9SMark Fasheh 	spin_lock(&oi->ip_lock);
62331afc32b9SMark Fasheh 	oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
62341afc32b9SMark Fasheh 	di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
62351afc32b9SMark Fasheh 	spin_unlock(&oi->ip_lock);
62361afc32b9SMark Fasheh 
62371afc32b9SMark Fasheh 	/*
62381afc32b9SMark Fasheh 	 * We clear the entire i_data structure here so that all
62391afc32b9SMark Fasheh 	 * fields can be properly initialized.
62401afc32b9SMark Fasheh 	 */
62411afc32b9SMark Fasheh 	ocfs2_zero_dinode_id2(inode, di);
62421afc32b9SMark Fasheh 
62431afc32b9SMark Fasheh 	idata->id_count = cpu_to_le16(ocfs2_max_inline_data(inode->i_sb));
62441afc32b9SMark Fasheh }
62451afc32b9SMark Fasheh 
62461afc32b9SMark Fasheh int ocfs2_convert_inline_data_to_extents(struct inode *inode,
62471afc32b9SMark Fasheh 					 struct buffer_head *di_bh)
62481afc32b9SMark Fasheh {
62491afc32b9SMark Fasheh 	int ret, i, has_data, num_pages = 0;
62501afc32b9SMark Fasheh 	handle_t *handle;
62511afc32b9SMark Fasheh 	u64 uninitialized_var(block);
62521afc32b9SMark Fasheh 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
62531afc32b9SMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
62541afc32b9SMark Fasheh 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
62551afc32b9SMark Fasheh 	struct ocfs2_alloc_context *data_ac = NULL;
62561afc32b9SMark Fasheh 	struct page **pages = NULL;
62571afc32b9SMark Fasheh 	loff_t end = osb->s_clustersize;
62581afc32b9SMark Fasheh 
62591afc32b9SMark Fasheh 	has_data = i_size_read(inode) ? 1 : 0;
62601afc32b9SMark Fasheh 
62611afc32b9SMark Fasheh 	if (has_data) {
62621afc32b9SMark Fasheh 		pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
62631afc32b9SMark Fasheh 				sizeof(struct page *), GFP_NOFS);
62641afc32b9SMark Fasheh 		if (pages == NULL) {
62651afc32b9SMark Fasheh 			ret = -ENOMEM;
62661afc32b9SMark Fasheh 			mlog_errno(ret);
62671afc32b9SMark Fasheh 			goto out;
62681afc32b9SMark Fasheh 		}
62691afc32b9SMark Fasheh 
62701afc32b9SMark Fasheh 		ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
62711afc32b9SMark Fasheh 		if (ret) {
62721afc32b9SMark Fasheh 			mlog_errno(ret);
62731afc32b9SMark Fasheh 			goto out;
62741afc32b9SMark Fasheh 		}
62751afc32b9SMark Fasheh 	}
62761afc32b9SMark Fasheh 
62771afc32b9SMark Fasheh 	handle = ocfs2_start_trans(osb, OCFS2_INLINE_TO_EXTENTS_CREDITS);
62781afc32b9SMark Fasheh 	if (IS_ERR(handle)) {
62791afc32b9SMark Fasheh 		ret = PTR_ERR(handle);
62801afc32b9SMark Fasheh 		mlog_errno(ret);
62811afc32b9SMark Fasheh 		goto out_unlock;
62821afc32b9SMark Fasheh 	}
62831afc32b9SMark Fasheh 
62841afc32b9SMark Fasheh 	ret = ocfs2_journal_access(handle, inode, di_bh,
62851afc32b9SMark Fasheh 				   OCFS2_JOURNAL_ACCESS_WRITE);
62861afc32b9SMark Fasheh 	if (ret) {
62871afc32b9SMark Fasheh 		mlog_errno(ret);
62881afc32b9SMark Fasheh 		goto out_commit;
62891afc32b9SMark Fasheh 	}
62901afc32b9SMark Fasheh 
62911afc32b9SMark Fasheh 	if (has_data) {
62921afc32b9SMark Fasheh 		u32 bit_off, num;
62931afc32b9SMark Fasheh 		unsigned int page_end;
62941afc32b9SMark Fasheh 		u64 phys;
62951afc32b9SMark Fasheh 
62961afc32b9SMark Fasheh 		ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
62971afc32b9SMark Fasheh 					   &num);
62981afc32b9SMark Fasheh 		if (ret) {
62991afc32b9SMark Fasheh 			mlog_errno(ret);
63001afc32b9SMark Fasheh 			goto out_commit;
63011afc32b9SMark Fasheh 		}
63021afc32b9SMark Fasheh 
63031afc32b9SMark Fasheh 		/*
63041afc32b9SMark Fasheh 		 * Save two copies, one for insert, and one that can
63051afc32b9SMark Fasheh 		 * be changed by ocfs2_map_and_dirty_page() below.
63061afc32b9SMark Fasheh 		 */
63071afc32b9SMark Fasheh 		block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
63081afc32b9SMark Fasheh 
63091afc32b9SMark Fasheh 		/*
63101afc32b9SMark Fasheh 		 * Non sparse file systems zero on extend, so no need
63111afc32b9SMark Fasheh 		 * to do that now.
63121afc32b9SMark Fasheh 		 */
63131afc32b9SMark Fasheh 		if (!ocfs2_sparse_alloc(osb) &&
63141afc32b9SMark Fasheh 		    PAGE_CACHE_SIZE < osb->s_clustersize)
63151afc32b9SMark Fasheh 			end = PAGE_CACHE_SIZE;
63161afc32b9SMark Fasheh 
63171afc32b9SMark Fasheh 		ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
63181afc32b9SMark Fasheh 		if (ret) {
63191afc32b9SMark Fasheh 			mlog_errno(ret);
63201afc32b9SMark Fasheh 			goto out_commit;
63211afc32b9SMark Fasheh 		}
63221afc32b9SMark Fasheh 
63231afc32b9SMark Fasheh 		/*
63241afc32b9SMark Fasheh 		 * This should populate the 1st page for us and mark
63251afc32b9SMark Fasheh 		 * it up to date.
63261afc32b9SMark Fasheh 		 */
63271afc32b9SMark Fasheh 		ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
63281afc32b9SMark Fasheh 		if (ret) {
63291afc32b9SMark Fasheh 			mlog_errno(ret);
63301afc32b9SMark Fasheh 			goto out_commit;
63311afc32b9SMark Fasheh 		}
63321afc32b9SMark Fasheh 
63331afc32b9SMark Fasheh 		page_end = PAGE_CACHE_SIZE;
63341afc32b9SMark Fasheh 		if (PAGE_CACHE_SIZE > osb->s_clustersize)
63351afc32b9SMark Fasheh 			page_end = osb->s_clustersize;
63361afc32b9SMark Fasheh 
63371afc32b9SMark Fasheh 		for (i = 0; i < num_pages; i++)
63381afc32b9SMark Fasheh 			ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
63391afc32b9SMark Fasheh 						 pages[i], i > 0, &phys);
63401afc32b9SMark Fasheh 	}
63411afc32b9SMark Fasheh 
63421afc32b9SMark Fasheh 	spin_lock(&oi->ip_lock);
63431afc32b9SMark Fasheh 	oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
63441afc32b9SMark Fasheh 	di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
63451afc32b9SMark Fasheh 	spin_unlock(&oi->ip_lock);
63461afc32b9SMark Fasheh 
63475b6a3a2bSMark Fasheh 	ocfs2_dinode_new_extent_list(inode, di);
63481afc32b9SMark Fasheh 
63491afc32b9SMark Fasheh 	ocfs2_journal_dirty(handle, di_bh);
63501afc32b9SMark Fasheh 
63511afc32b9SMark Fasheh 	if (has_data) {
63521afc32b9SMark Fasheh 		/*
63531afc32b9SMark Fasheh 		 * An error at this point should be extremely rare. If
63541afc32b9SMark Fasheh 		 * this proves to be false, we could always re-build
63551afc32b9SMark Fasheh 		 * the in-inode data from our pages.
63561afc32b9SMark Fasheh 		 */
63571afc32b9SMark Fasheh 		ret = ocfs2_insert_extent(osb, handle, inode, di_bh,
63581afc32b9SMark Fasheh 					  0, block, 1, 0, NULL);
63591afc32b9SMark Fasheh 		if (ret) {
63601afc32b9SMark Fasheh 			mlog_errno(ret);
63611afc32b9SMark Fasheh 			goto out_commit;
63621afc32b9SMark Fasheh 		}
63631afc32b9SMark Fasheh 
63641afc32b9SMark Fasheh 		inode->i_blocks = ocfs2_inode_sector_count(inode);
63651afc32b9SMark Fasheh 	}
63661afc32b9SMark Fasheh 
63671afc32b9SMark Fasheh out_commit:
63681afc32b9SMark Fasheh 	ocfs2_commit_trans(osb, handle);
63691afc32b9SMark Fasheh 
63701afc32b9SMark Fasheh out_unlock:
63711afc32b9SMark Fasheh 	if (data_ac)
63721afc32b9SMark Fasheh 		ocfs2_free_alloc_context(data_ac);
63731afc32b9SMark Fasheh 
63741afc32b9SMark Fasheh out:
63751afc32b9SMark Fasheh 	if (pages) {
63761afc32b9SMark Fasheh 		ocfs2_unlock_and_free_pages(pages, num_pages);
63771afc32b9SMark Fasheh 		kfree(pages);
63781afc32b9SMark Fasheh 	}
63791afc32b9SMark Fasheh 
63801afc32b9SMark Fasheh 	return ret;
63811afc32b9SMark Fasheh }
63821afc32b9SMark Fasheh 
6383ccd979bdSMark Fasheh /*
6384ccd979bdSMark Fasheh  * It is expected, that by the time you call this function,
6385ccd979bdSMark Fasheh  * inode->i_size and fe->i_size have been adjusted.
6386ccd979bdSMark Fasheh  *
6387ccd979bdSMark Fasheh  * WARNING: This will kfree the truncate context
6388ccd979bdSMark Fasheh  */
6389ccd979bdSMark Fasheh int ocfs2_commit_truncate(struct ocfs2_super *osb,
6390ccd979bdSMark Fasheh 			  struct inode *inode,
6391ccd979bdSMark Fasheh 			  struct buffer_head *fe_bh,
6392ccd979bdSMark Fasheh 			  struct ocfs2_truncate_context *tc)
6393ccd979bdSMark Fasheh {
6394ccd979bdSMark Fasheh 	int status, i, credits, tl_sem = 0;
6395dcd0538fSMark Fasheh 	u32 clusters_to_del, new_highest_cpos, range;
6396ccd979bdSMark Fasheh 	struct ocfs2_extent_list *el;
63971fabe148SMark Fasheh 	handle_t *handle = NULL;
6398ccd979bdSMark Fasheh 	struct inode *tl_inode = osb->osb_tl_inode;
6399dcd0538fSMark Fasheh 	struct ocfs2_path *path = NULL;
6400ccd979bdSMark Fasheh 
6401ccd979bdSMark Fasheh 	mlog_entry_void();
6402ccd979bdSMark Fasheh 
6403dcd0538fSMark Fasheh 	new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
6404ccd979bdSMark Fasheh 						     i_size_read(inode));
6405ccd979bdSMark Fasheh 
6406dcd0538fSMark Fasheh 	path = ocfs2_new_inode_path(fe_bh);
6407dcd0538fSMark Fasheh 	if (!path) {
6408dcd0538fSMark Fasheh 		status = -ENOMEM;
6409ccd979bdSMark Fasheh 		mlog_errno(status);
6410ccd979bdSMark Fasheh 		goto bail;
6411ccd979bdSMark Fasheh 	}
641283418978SMark Fasheh 
641383418978SMark Fasheh 	ocfs2_extent_map_trunc(inode, new_highest_cpos);
641483418978SMark Fasheh 
6415dcd0538fSMark Fasheh start:
6416dcd0538fSMark Fasheh 	/*
64173a0782d0SMark Fasheh 	 * Check that we still have allocation to delete.
64183a0782d0SMark Fasheh 	 */
64193a0782d0SMark Fasheh 	if (OCFS2_I(inode)->ip_clusters == 0) {
64203a0782d0SMark Fasheh 		status = 0;
64213a0782d0SMark Fasheh 		goto bail;
64223a0782d0SMark Fasheh 	}
64233a0782d0SMark Fasheh 
64243a0782d0SMark Fasheh 	/*
6425dcd0538fSMark Fasheh 	 * Truncate always works against the rightmost tree branch.
6426dcd0538fSMark Fasheh 	 */
6427dcd0538fSMark Fasheh 	status = ocfs2_find_path(inode, path, UINT_MAX);
6428dcd0538fSMark Fasheh 	if (status) {
6429dcd0538fSMark Fasheh 		mlog_errno(status);
6430ccd979bdSMark Fasheh 		goto bail;
6431ccd979bdSMark Fasheh 	}
6432ccd979bdSMark Fasheh 
6433dcd0538fSMark Fasheh 	mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
6434dcd0538fSMark Fasheh 	     OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
6435dcd0538fSMark Fasheh 
6436dcd0538fSMark Fasheh 	/*
6437dcd0538fSMark Fasheh 	 * By now, el will point to the extent list on the bottom most
6438dcd0538fSMark Fasheh 	 * portion of this tree. Only the tail record is considered in
6439dcd0538fSMark Fasheh 	 * each pass.
6440dcd0538fSMark Fasheh 	 *
6441dcd0538fSMark Fasheh 	 * We handle the following cases, in order:
6442dcd0538fSMark Fasheh 	 * - empty extent: delete the remaining branch
6443dcd0538fSMark Fasheh 	 * - remove the entire record
6444dcd0538fSMark Fasheh 	 * - remove a partial record
6445dcd0538fSMark Fasheh 	 * - no record needs to be removed (truncate has completed)
6446dcd0538fSMark Fasheh 	 */
6447dcd0538fSMark Fasheh 	el = path_leaf_el(path);
64483a0782d0SMark Fasheh 	if (le16_to_cpu(el->l_next_free_rec) == 0) {
64493a0782d0SMark Fasheh 		ocfs2_error(inode->i_sb,
64503a0782d0SMark Fasheh 			    "Inode %llu has empty extent block at %llu\n",
64513a0782d0SMark Fasheh 			    (unsigned long long)OCFS2_I(inode)->ip_blkno,
64523a0782d0SMark Fasheh 			    (unsigned long long)path_leaf_bh(path)->b_blocknr);
64533a0782d0SMark Fasheh 		status = -EROFS;
64543a0782d0SMark Fasheh 		goto bail;
64553a0782d0SMark Fasheh 	}
64563a0782d0SMark Fasheh 
6457ccd979bdSMark Fasheh 	i = le16_to_cpu(el->l_next_free_rec) - 1;
6458dcd0538fSMark Fasheh 	range = le32_to_cpu(el->l_recs[i].e_cpos) +
6459e48edee2SMark Fasheh 		ocfs2_rec_clusters(el, &el->l_recs[i]);
6460dcd0538fSMark Fasheh 	if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
6461dcd0538fSMark Fasheh 		clusters_to_del = 0;
6462dcd0538fSMark Fasheh 	} else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
6463e48edee2SMark Fasheh 		clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
6464dcd0538fSMark Fasheh 	} else if (range > new_highest_cpos) {
6465e48edee2SMark Fasheh 		clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
6466ccd979bdSMark Fasheh 				   le32_to_cpu(el->l_recs[i].e_cpos)) -
6467dcd0538fSMark Fasheh 				  new_highest_cpos;
6468dcd0538fSMark Fasheh 	} else {
6469dcd0538fSMark Fasheh 		status = 0;
6470dcd0538fSMark Fasheh 		goto bail;
6471dcd0538fSMark Fasheh 	}
6472ccd979bdSMark Fasheh 
6473dcd0538fSMark Fasheh 	mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
6474dcd0538fSMark Fasheh 	     clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
6475dcd0538fSMark Fasheh 
64761b1dcc1bSJes Sorensen 	mutex_lock(&tl_inode->i_mutex);
6477ccd979bdSMark Fasheh 	tl_sem = 1;
6478ccd979bdSMark Fasheh 	/* ocfs2_truncate_log_needs_flush guarantees us at least one
6479ccd979bdSMark Fasheh 	 * record is free for use. If there isn't any, we flush to get
6480ccd979bdSMark Fasheh 	 * an empty truncate log.  */
6481ccd979bdSMark Fasheh 	if (ocfs2_truncate_log_needs_flush(osb)) {
6482ccd979bdSMark Fasheh 		status = __ocfs2_flush_truncate_log(osb);
6483ccd979bdSMark Fasheh 		if (status < 0) {
6484ccd979bdSMark Fasheh 			mlog_errno(status);
6485ccd979bdSMark Fasheh 			goto bail;
6486ccd979bdSMark Fasheh 		}
6487ccd979bdSMark Fasheh 	}
6488ccd979bdSMark Fasheh 
6489ccd979bdSMark Fasheh 	credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
6490dcd0538fSMark Fasheh 						(struct ocfs2_dinode *)fe_bh->b_data,
6491dcd0538fSMark Fasheh 						el);
649265eff9ccSMark Fasheh 	handle = ocfs2_start_trans(osb, credits);
6493ccd979bdSMark Fasheh 	if (IS_ERR(handle)) {
6494ccd979bdSMark Fasheh 		status = PTR_ERR(handle);
6495ccd979bdSMark Fasheh 		handle = NULL;
6496ccd979bdSMark Fasheh 		mlog_errno(status);
6497ccd979bdSMark Fasheh 		goto bail;
6498ccd979bdSMark Fasheh 	}
6499ccd979bdSMark Fasheh 
6500dcd0538fSMark Fasheh 	status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
6501dcd0538fSMark Fasheh 				   tc, path);
6502ccd979bdSMark Fasheh 	if (status < 0) {
6503ccd979bdSMark Fasheh 		mlog_errno(status);
6504ccd979bdSMark Fasheh 		goto bail;
6505ccd979bdSMark Fasheh 	}
6506ccd979bdSMark Fasheh 
65071b1dcc1bSJes Sorensen 	mutex_unlock(&tl_inode->i_mutex);
6508ccd979bdSMark Fasheh 	tl_sem = 0;
6509ccd979bdSMark Fasheh 
651002dc1af4SMark Fasheh 	ocfs2_commit_trans(osb, handle);
6511ccd979bdSMark Fasheh 	handle = NULL;
6512ccd979bdSMark Fasheh 
6513dcd0538fSMark Fasheh 	ocfs2_reinit_path(path, 1);
6514dcd0538fSMark Fasheh 
6515dcd0538fSMark Fasheh 	/*
65163a0782d0SMark Fasheh 	 * The check above will catch the case where we've truncated
65173a0782d0SMark Fasheh 	 * away all allocation.
6518dcd0538fSMark Fasheh 	 */
6519ccd979bdSMark Fasheh 	goto start;
65203a0782d0SMark Fasheh 
6521ccd979bdSMark Fasheh bail:
6522ccd979bdSMark Fasheh 
6523ccd979bdSMark Fasheh 	ocfs2_schedule_truncate_log_flush(osb, 1);
6524ccd979bdSMark Fasheh 
6525ccd979bdSMark Fasheh 	if (tl_sem)
65261b1dcc1bSJes Sorensen 		mutex_unlock(&tl_inode->i_mutex);
6527ccd979bdSMark Fasheh 
6528ccd979bdSMark Fasheh 	if (handle)
652902dc1af4SMark Fasheh 		ocfs2_commit_trans(osb, handle);
6530ccd979bdSMark Fasheh 
653159a5e416SMark Fasheh 	ocfs2_run_deallocs(osb, &tc->tc_dealloc);
653259a5e416SMark Fasheh 
6533dcd0538fSMark Fasheh 	ocfs2_free_path(path);
6534ccd979bdSMark Fasheh 
6535ccd979bdSMark Fasheh 	/* This will drop the ext_alloc cluster lock for us */
6536ccd979bdSMark Fasheh 	ocfs2_free_truncate_context(tc);
6537ccd979bdSMark Fasheh 
6538ccd979bdSMark Fasheh 	mlog_exit(status);
6539ccd979bdSMark Fasheh 	return status;
6540ccd979bdSMark Fasheh }
6541ccd979bdSMark Fasheh 
6542ccd979bdSMark Fasheh /*
654359a5e416SMark Fasheh  * Expects the inode to already be locked.
6544ccd979bdSMark Fasheh  */
6545ccd979bdSMark Fasheh int ocfs2_prepare_truncate(struct ocfs2_super *osb,
6546ccd979bdSMark Fasheh 			   struct inode *inode,
6547ccd979bdSMark Fasheh 			   struct buffer_head *fe_bh,
6548ccd979bdSMark Fasheh 			   struct ocfs2_truncate_context **tc)
6549ccd979bdSMark Fasheh {
655059a5e416SMark Fasheh 	int status;
6551ccd979bdSMark Fasheh 	unsigned int new_i_clusters;
6552ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe;
6553ccd979bdSMark Fasheh 	struct ocfs2_extent_block *eb;
6554ccd979bdSMark Fasheh 	struct buffer_head *last_eb_bh = NULL;
6555ccd979bdSMark Fasheh 
6556ccd979bdSMark Fasheh 	mlog_entry_void();
6557ccd979bdSMark Fasheh 
6558ccd979bdSMark Fasheh 	*tc = NULL;
6559ccd979bdSMark Fasheh 
6560ccd979bdSMark Fasheh 	new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
6561ccd979bdSMark Fasheh 						  i_size_read(inode));
6562ccd979bdSMark Fasheh 	fe = (struct ocfs2_dinode *) fe_bh->b_data;
6563ccd979bdSMark Fasheh 
6564ccd979bdSMark Fasheh 	mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
65651ca1a111SMark Fasheh 	     "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
65661ca1a111SMark Fasheh 	     (unsigned long long)le64_to_cpu(fe->i_size));
6567ccd979bdSMark Fasheh 
6568cd861280SRobert P. J. Day 	*tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
6569ccd979bdSMark Fasheh 	if (!(*tc)) {
6570ccd979bdSMark Fasheh 		status = -ENOMEM;
6571ccd979bdSMark Fasheh 		mlog_errno(status);
6572ccd979bdSMark Fasheh 		goto bail;
6573ccd979bdSMark Fasheh 	}
657459a5e416SMark Fasheh 	ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
6575ccd979bdSMark Fasheh 
6576ccd979bdSMark Fasheh 	if (fe->id2.i_list.l_tree_depth) {
6577ccd979bdSMark Fasheh 		status = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
6578ccd979bdSMark Fasheh 					  &last_eb_bh, OCFS2_BH_CACHED, inode);
6579ccd979bdSMark Fasheh 		if (status < 0) {
6580ccd979bdSMark Fasheh 			mlog_errno(status);
6581ccd979bdSMark Fasheh 			goto bail;
6582ccd979bdSMark Fasheh 		}
6583ccd979bdSMark Fasheh 		eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6584ccd979bdSMark Fasheh 		if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
6585ccd979bdSMark Fasheh 			OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
6586ccd979bdSMark Fasheh 
6587ccd979bdSMark Fasheh 			brelse(last_eb_bh);
6588ccd979bdSMark Fasheh 			status = -EIO;
6589ccd979bdSMark Fasheh 			goto bail;
6590ccd979bdSMark Fasheh 		}
6591ccd979bdSMark Fasheh 	}
6592ccd979bdSMark Fasheh 
6593ccd979bdSMark Fasheh 	(*tc)->tc_last_eb_bh = last_eb_bh;
6594ccd979bdSMark Fasheh 
6595ccd979bdSMark Fasheh 	status = 0;
6596ccd979bdSMark Fasheh bail:
6597ccd979bdSMark Fasheh 	if (status < 0) {
6598ccd979bdSMark Fasheh 		if (*tc)
6599ccd979bdSMark Fasheh 			ocfs2_free_truncate_context(*tc);
6600ccd979bdSMark Fasheh 		*tc = NULL;
6601ccd979bdSMark Fasheh 	}
6602ccd979bdSMark Fasheh 	mlog_exit_void();
6603ccd979bdSMark Fasheh 	return status;
6604ccd979bdSMark Fasheh }
6605ccd979bdSMark Fasheh 
66061afc32b9SMark Fasheh /*
66071afc32b9SMark Fasheh  * 'start' is inclusive, 'end' is not.
66081afc32b9SMark Fasheh  */
66091afc32b9SMark Fasheh int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
66101afc32b9SMark Fasheh 			  unsigned int start, unsigned int end, int trunc)
66111afc32b9SMark Fasheh {
66121afc32b9SMark Fasheh 	int ret;
66131afc32b9SMark Fasheh 	unsigned int numbytes;
66141afc32b9SMark Fasheh 	handle_t *handle;
66151afc32b9SMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
66161afc32b9SMark Fasheh 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
66171afc32b9SMark Fasheh 	struct ocfs2_inline_data *idata = &di->id2.i_data;
66181afc32b9SMark Fasheh 
66191afc32b9SMark Fasheh 	if (end > i_size_read(inode))
66201afc32b9SMark Fasheh 		end = i_size_read(inode);
66211afc32b9SMark Fasheh 
66221afc32b9SMark Fasheh 	BUG_ON(start >= end);
66231afc32b9SMark Fasheh 
66241afc32b9SMark Fasheh 	if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
66251afc32b9SMark Fasheh 	    !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
66261afc32b9SMark Fasheh 	    !ocfs2_supports_inline_data(osb)) {
66271afc32b9SMark Fasheh 		ocfs2_error(inode->i_sb,
66281afc32b9SMark Fasheh 			    "Inline data flags for inode %llu don't agree! "
66291afc32b9SMark Fasheh 			    "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
66301afc32b9SMark Fasheh 			    (unsigned long long)OCFS2_I(inode)->ip_blkno,
66311afc32b9SMark Fasheh 			    le16_to_cpu(di->i_dyn_features),
66321afc32b9SMark Fasheh 			    OCFS2_I(inode)->ip_dyn_features,
66331afc32b9SMark Fasheh 			    osb->s_feature_incompat);
66341afc32b9SMark Fasheh 		ret = -EROFS;
66351afc32b9SMark Fasheh 		goto out;
66361afc32b9SMark Fasheh 	}
66371afc32b9SMark Fasheh 
66381afc32b9SMark Fasheh 	handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
66391afc32b9SMark Fasheh 	if (IS_ERR(handle)) {
66401afc32b9SMark Fasheh 		ret = PTR_ERR(handle);
66411afc32b9SMark Fasheh 		mlog_errno(ret);
66421afc32b9SMark Fasheh 		goto out;
66431afc32b9SMark Fasheh 	}
66441afc32b9SMark Fasheh 
66451afc32b9SMark Fasheh 	ret = ocfs2_journal_access(handle, inode, di_bh,
66461afc32b9SMark Fasheh 				   OCFS2_JOURNAL_ACCESS_WRITE);
66471afc32b9SMark Fasheh 	if (ret) {
66481afc32b9SMark Fasheh 		mlog_errno(ret);
66491afc32b9SMark Fasheh 		goto out_commit;
66501afc32b9SMark Fasheh 	}
66511afc32b9SMark Fasheh 
66521afc32b9SMark Fasheh 	numbytes = end - start;
66531afc32b9SMark Fasheh 	memset(idata->id_data + start, 0, numbytes);
66541afc32b9SMark Fasheh 
66551afc32b9SMark Fasheh 	/*
66561afc32b9SMark Fasheh 	 * No need to worry about the data page here - it's been
66571afc32b9SMark Fasheh 	 * truncated already and inline data doesn't need it for
66581afc32b9SMark Fasheh 	 * pushing zero's to disk, so we'll let readpage pick it up
66591afc32b9SMark Fasheh 	 * later.
66601afc32b9SMark Fasheh 	 */
66611afc32b9SMark Fasheh 	if (trunc) {
66621afc32b9SMark Fasheh 		i_size_write(inode, start);
66631afc32b9SMark Fasheh 		di->i_size = cpu_to_le64(start);
66641afc32b9SMark Fasheh 	}
66651afc32b9SMark Fasheh 
66661afc32b9SMark Fasheh 	inode->i_blocks = ocfs2_inode_sector_count(inode);
66671afc32b9SMark Fasheh 	inode->i_ctime = inode->i_mtime = CURRENT_TIME;
66681afc32b9SMark Fasheh 
66691afc32b9SMark Fasheh 	di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
66701afc32b9SMark Fasheh 	di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
66711afc32b9SMark Fasheh 
66721afc32b9SMark Fasheh 	ocfs2_journal_dirty(handle, di_bh);
66731afc32b9SMark Fasheh 
66741afc32b9SMark Fasheh out_commit:
66751afc32b9SMark Fasheh 	ocfs2_commit_trans(osb, handle);
66761afc32b9SMark Fasheh 
66771afc32b9SMark Fasheh out:
66781afc32b9SMark Fasheh 	return ret;
66791afc32b9SMark Fasheh }
66801afc32b9SMark Fasheh 
6681ccd979bdSMark Fasheh static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
6682ccd979bdSMark Fasheh {
668359a5e416SMark Fasheh 	/*
668459a5e416SMark Fasheh 	 * The caller is responsible for completing deallocation
668559a5e416SMark Fasheh 	 * before freeing the context.
668659a5e416SMark Fasheh 	 */
668759a5e416SMark Fasheh 	if (tc->tc_dealloc.c_first_suballocator != NULL)
668859a5e416SMark Fasheh 		mlog(ML_NOTICE,
668959a5e416SMark Fasheh 		     "Truncate completion has non-empty dealloc context\n");
6690ccd979bdSMark Fasheh 
6691ccd979bdSMark Fasheh 	if (tc->tc_last_eb_bh)
6692ccd979bdSMark Fasheh 		brelse(tc->tc_last_eb_bh);
6693ccd979bdSMark Fasheh 
6694ccd979bdSMark Fasheh 	kfree(tc);
6695ccd979bdSMark Fasheh }
6696