xref: /openbmc/linux/fs/ext4/extents.c (revision dbf9d7da)
1a86c6181SAlex Tomas /*
2a86c6181SAlex Tomas  * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
3a86c6181SAlex Tomas  * Written by Alex Tomas <alex@clusterfs.com>
4a86c6181SAlex Tomas  *
5a86c6181SAlex Tomas  * Architecture independence:
6a86c6181SAlex Tomas  *   Copyright (c) 2005, Bull S.A.
7a86c6181SAlex Tomas  *   Written by Pierre Peiffer <pierre.peiffer@bull.net>
8a86c6181SAlex Tomas  *
9a86c6181SAlex Tomas  * This program is free software; you can redistribute it and/or modify
10a86c6181SAlex Tomas  * it under the terms of the GNU General Public License version 2 as
11a86c6181SAlex Tomas  * published by the Free Software Foundation.
12a86c6181SAlex Tomas  *
13a86c6181SAlex Tomas  * This program is distributed in the hope that it will be useful,
14a86c6181SAlex Tomas  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15a86c6181SAlex Tomas  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16a86c6181SAlex Tomas  * GNU General Public License for more details.
17a86c6181SAlex Tomas  *
18a86c6181SAlex Tomas  * You should have received a copy of the GNU General Public Licens
19a86c6181SAlex Tomas  * along with this program; if not, write to the Free Software
20a86c6181SAlex Tomas  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-
21a86c6181SAlex Tomas  */
22a86c6181SAlex Tomas 
23a86c6181SAlex Tomas /*
24a86c6181SAlex Tomas  * Extents support for EXT4
25a86c6181SAlex Tomas  *
26a86c6181SAlex Tomas  * TODO:
27a86c6181SAlex Tomas  *   - ext4*_error() should be used in some situations
28a86c6181SAlex Tomas  *   - analyze all BUG()/BUG_ON(), use -EIO where appropriate
29a86c6181SAlex Tomas  *   - smart tree reduction
30a86c6181SAlex Tomas  */
31a86c6181SAlex Tomas 
32a86c6181SAlex Tomas #include <linux/module.h>
33a86c6181SAlex Tomas #include <linux/fs.h>
34a86c6181SAlex Tomas #include <linux/time.h>
35a86c6181SAlex Tomas #include <linux/ext4_jbd2.h>
36cd02ff0bSMingming Cao #include <linux/jbd2.h>
37a86c6181SAlex Tomas #include <linux/highuid.h>
38a86c6181SAlex Tomas #include <linux/pagemap.h>
39a86c6181SAlex Tomas #include <linux/quotaops.h>
40a86c6181SAlex Tomas #include <linux/string.h>
41a86c6181SAlex Tomas #include <linux/slab.h>
42a2df2a63SAmit Arora #include <linux/falloc.h>
43a86c6181SAlex Tomas #include <linux/ext4_fs_extents.h>
44a86c6181SAlex Tomas #include <asm/uaccess.h>
45a86c6181SAlex Tomas 
46a86c6181SAlex Tomas 
47d0d856e8SRandy Dunlap /*
48d0d856e8SRandy Dunlap  * ext_pblock:
49d0d856e8SRandy Dunlap  * combine low and high parts of physical block number into ext4_fsblk_t
50d0d856e8SRandy Dunlap  */
5109b88252SAvantika Mathur static ext4_fsblk_t ext_pblock(struct ext4_extent *ex)
52f65e6fbaSAlex Tomas {
53f65e6fbaSAlex Tomas 	ext4_fsblk_t block;
54f65e6fbaSAlex Tomas 
55b377611dSAneesh Kumar K.V 	block = le32_to_cpu(ex->ee_start_lo);
56f65e6fbaSAlex Tomas 	block |= ((ext4_fsblk_t) le16_to_cpu(ex->ee_start_hi) << 31) << 1;
57f65e6fbaSAlex Tomas 	return block;
58f65e6fbaSAlex Tomas }
59f65e6fbaSAlex Tomas 
60d0d856e8SRandy Dunlap /*
61d0d856e8SRandy Dunlap  * idx_pblock:
62d0d856e8SRandy Dunlap  * combine low and high parts of a leaf physical block number into ext4_fsblk_t
63d0d856e8SRandy Dunlap  */
64c14c6fd5SAneesh Kumar K.V ext4_fsblk_t idx_pblock(struct ext4_extent_idx *ix)
65f65e6fbaSAlex Tomas {
66f65e6fbaSAlex Tomas 	ext4_fsblk_t block;
67f65e6fbaSAlex Tomas 
68d8dd0b45SAneesh Kumar K.V 	block = le32_to_cpu(ix->ei_leaf_lo);
69f65e6fbaSAlex Tomas 	block |= ((ext4_fsblk_t) le16_to_cpu(ix->ei_leaf_hi) << 31) << 1;
70f65e6fbaSAlex Tomas 	return block;
71f65e6fbaSAlex Tomas }
72f65e6fbaSAlex Tomas 
73d0d856e8SRandy Dunlap /*
74d0d856e8SRandy Dunlap  * ext4_ext_store_pblock:
75d0d856e8SRandy Dunlap  * stores a large physical block number into an extent struct,
76d0d856e8SRandy Dunlap  * breaking it into parts
77d0d856e8SRandy Dunlap  */
78c14c6fd5SAneesh Kumar K.V void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb)
79f65e6fbaSAlex Tomas {
80b377611dSAneesh Kumar K.V 	ex->ee_start_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff));
81f65e6fbaSAlex Tomas 	ex->ee_start_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
82f65e6fbaSAlex Tomas }
83f65e6fbaSAlex Tomas 
84d0d856e8SRandy Dunlap /*
85d0d856e8SRandy Dunlap  * ext4_idx_store_pblock:
86d0d856e8SRandy Dunlap  * stores a large physical block number into an index struct,
87d0d856e8SRandy Dunlap  * breaking it into parts
88d0d856e8SRandy Dunlap  */
8909b88252SAvantika Mathur static void ext4_idx_store_pblock(struct ext4_extent_idx *ix, ext4_fsblk_t pb)
90f65e6fbaSAlex Tomas {
91d8dd0b45SAneesh Kumar K.V 	ix->ei_leaf_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff));
92f65e6fbaSAlex Tomas 	ix->ei_leaf_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
93f65e6fbaSAlex Tomas }
94f65e6fbaSAlex Tomas 
95a86c6181SAlex Tomas static handle_t *ext4_ext_journal_restart(handle_t *handle, int needed)
96a86c6181SAlex Tomas {
97a86c6181SAlex Tomas 	int err;
98a86c6181SAlex Tomas 
99a86c6181SAlex Tomas 	if (handle->h_buffer_credits > needed)
100a86c6181SAlex Tomas 		return handle;
101a86c6181SAlex Tomas 	if (!ext4_journal_extend(handle, needed))
102a86c6181SAlex Tomas 		return handle;
103a86c6181SAlex Tomas 	err = ext4_journal_restart(handle, needed);
104a86c6181SAlex Tomas 
105a86c6181SAlex Tomas 	return handle;
106a86c6181SAlex Tomas }
107a86c6181SAlex Tomas 
108a86c6181SAlex Tomas /*
109a86c6181SAlex Tomas  * could return:
110a86c6181SAlex Tomas  *  - EROFS
111a86c6181SAlex Tomas  *  - ENOMEM
112a86c6181SAlex Tomas  */
113a86c6181SAlex Tomas static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
114a86c6181SAlex Tomas 				struct ext4_ext_path *path)
115a86c6181SAlex Tomas {
116a86c6181SAlex Tomas 	if (path->p_bh) {
117a86c6181SAlex Tomas 		/* path points to block */
118a86c6181SAlex Tomas 		return ext4_journal_get_write_access(handle, path->p_bh);
119a86c6181SAlex Tomas 	}
120a86c6181SAlex Tomas 	/* path points to leaf/index in inode body */
121a86c6181SAlex Tomas 	/* we use in-core data, no need to protect them */
122a86c6181SAlex Tomas 	return 0;
123a86c6181SAlex Tomas }
124a86c6181SAlex Tomas 
125a86c6181SAlex Tomas /*
126a86c6181SAlex Tomas  * could return:
127a86c6181SAlex Tomas  *  - EROFS
128a86c6181SAlex Tomas  *  - ENOMEM
129a86c6181SAlex Tomas  *  - EIO
130a86c6181SAlex Tomas  */
131a86c6181SAlex Tomas static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
132a86c6181SAlex Tomas 				struct ext4_ext_path *path)
133a86c6181SAlex Tomas {
134a86c6181SAlex Tomas 	int err;
135a86c6181SAlex Tomas 	if (path->p_bh) {
136a86c6181SAlex Tomas 		/* path points to block */
137a86c6181SAlex Tomas 		err = ext4_journal_dirty_metadata(handle, path->p_bh);
138a86c6181SAlex Tomas 	} else {
139a86c6181SAlex Tomas 		/* path points to leaf/index in inode body */
140a86c6181SAlex Tomas 		err = ext4_mark_inode_dirty(handle, inode);
141a86c6181SAlex Tomas 	}
142a86c6181SAlex Tomas 	return err;
143a86c6181SAlex Tomas }
144a86c6181SAlex Tomas 
145f65e6fbaSAlex Tomas static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
146a86c6181SAlex Tomas 			      struct ext4_ext_path *path,
147725d26d3SAneesh Kumar K.V 			      ext4_lblk_t block)
148a86c6181SAlex Tomas {
149a86c6181SAlex Tomas 	struct ext4_inode_info *ei = EXT4_I(inode);
150f65e6fbaSAlex Tomas 	ext4_fsblk_t bg_start;
151f65e6fbaSAlex Tomas 	ext4_grpblk_t colour;
152a86c6181SAlex Tomas 	int depth;
153a86c6181SAlex Tomas 
154a86c6181SAlex Tomas 	if (path) {
155a86c6181SAlex Tomas 		struct ext4_extent *ex;
156a86c6181SAlex Tomas 		depth = path->p_depth;
157a86c6181SAlex Tomas 
158a86c6181SAlex Tomas 		/* try to predict block placement */
1597e028976SAvantika Mathur 		ex = path[depth].p_ext;
1607e028976SAvantika Mathur 		if (ex)
161f65e6fbaSAlex Tomas 			return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block));
162a86c6181SAlex Tomas 
163d0d856e8SRandy Dunlap 		/* it looks like index is empty;
164d0d856e8SRandy Dunlap 		 * try to find starting block from index itself */
165a86c6181SAlex Tomas 		if (path[depth].p_bh)
166a86c6181SAlex Tomas 			return path[depth].p_bh->b_blocknr;
167a86c6181SAlex Tomas 	}
168a86c6181SAlex Tomas 
169a86c6181SAlex Tomas 	/* OK. use inode's group */
170a86c6181SAlex Tomas 	bg_start = (ei->i_block_group * EXT4_BLOCKS_PER_GROUP(inode->i_sb)) +
171a86c6181SAlex Tomas 		le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_first_data_block);
172a86c6181SAlex Tomas 	colour = (current->pid % 16) *
173a86c6181SAlex Tomas 			(EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
174a86c6181SAlex Tomas 	return bg_start + colour + block;
175a86c6181SAlex Tomas }
176a86c6181SAlex Tomas 
177f65e6fbaSAlex Tomas static ext4_fsblk_t
178a86c6181SAlex Tomas ext4_ext_new_block(handle_t *handle, struct inode *inode,
179a86c6181SAlex Tomas 			struct ext4_ext_path *path,
180a86c6181SAlex Tomas 			struct ext4_extent *ex, int *err)
181a86c6181SAlex Tomas {
182f65e6fbaSAlex Tomas 	ext4_fsblk_t goal, newblock;
183a86c6181SAlex Tomas 
184a86c6181SAlex Tomas 	goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
185a86c6181SAlex Tomas 	newblock = ext4_new_block(handle, inode, goal, err);
186a86c6181SAlex Tomas 	return newblock;
187a86c6181SAlex Tomas }
188a86c6181SAlex Tomas 
18909b88252SAvantika Mathur static int ext4_ext_space_block(struct inode *inode)
190a86c6181SAlex Tomas {
191a86c6181SAlex Tomas 	int size;
192a86c6181SAlex Tomas 
193a86c6181SAlex Tomas 	size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
194a86c6181SAlex Tomas 			/ sizeof(struct ext4_extent);
195bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST
196a86c6181SAlex Tomas 	if (size > 6)
197a86c6181SAlex Tomas 		size = 6;
198a86c6181SAlex Tomas #endif
199a86c6181SAlex Tomas 	return size;
200a86c6181SAlex Tomas }
201a86c6181SAlex Tomas 
20209b88252SAvantika Mathur static int ext4_ext_space_block_idx(struct inode *inode)
203a86c6181SAlex Tomas {
204a86c6181SAlex Tomas 	int size;
205a86c6181SAlex Tomas 
206a86c6181SAlex Tomas 	size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
207a86c6181SAlex Tomas 			/ sizeof(struct ext4_extent_idx);
208bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST
209a86c6181SAlex Tomas 	if (size > 5)
210a86c6181SAlex Tomas 		size = 5;
211a86c6181SAlex Tomas #endif
212a86c6181SAlex Tomas 	return size;
213a86c6181SAlex Tomas }
214a86c6181SAlex Tomas 
21509b88252SAvantika Mathur static int ext4_ext_space_root(struct inode *inode)
216a86c6181SAlex Tomas {
217a86c6181SAlex Tomas 	int size;
218a86c6181SAlex Tomas 
219a86c6181SAlex Tomas 	size = sizeof(EXT4_I(inode)->i_data);
220a86c6181SAlex Tomas 	size -= sizeof(struct ext4_extent_header);
221a86c6181SAlex Tomas 	size /= sizeof(struct ext4_extent);
222bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST
223a86c6181SAlex Tomas 	if (size > 3)
224a86c6181SAlex Tomas 		size = 3;
225a86c6181SAlex Tomas #endif
226a86c6181SAlex Tomas 	return size;
227a86c6181SAlex Tomas }
228a86c6181SAlex Tomas 
22909b88252SAvantika Mathur static int ext4_ext_space_root_idx(struct inode *inode)
230a86c6181SAlex Tomas {
231a86c6181SAlex Tomas 	int size;
232a86c6181SAlex Tomas 
233a86c6181SAlex Tomas 	size = sizeof(EXT4_I(inode)->i_data);
234a86c6181SAlex Tomas 	size -= sizeof(struct ext4_extent_header);
235a86c6181SAlex Tomas 	size /= sizeof(struct ext4_extent_idx);
236bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST
237a86c6181SAlex Tomas 	if (size > 4)
238a86c6181SAlex Tomas 		size = 4;
239a86c6181SAlex Tomas #endif
240a86c6181SAlex Tomas 	return size;
241a86c6181SAlex Tomas }
242a86c6181SAlex Tomas 
243c29c0ae7SAlex Tomas static int
244c29c0ae7SAlex Tomas ext4_ext_max_entries(struct inode *inode, int depth)
245c29c0ae7SAlex Tomas {
246c29c0ae7SAlex Tomas 	int max;
247c29c0ae7SAlex Tomas 
248c29c0ae7SAlex Tomas 	if (depth == ext_depth(inode)) {
249c29c0ae7SAlex Tomas 		if (depth == 0)
250c29c0ae7SAlex Tomas 			max = ext4_ext_space_root(inode);
251c29c0ae7SAlex Tomas 		else
252c29c0ae7SAlex Tomas 			max = ext4_ext_space_root_idx(inode);
253c29c0ae7SAlex Tomas 	} else {
254c29c0ae7SAlex Tomas 		if (depth == 0)
255c29c0ae7SAlex Tomas 			max = ext4_ext_space_block(inode);
256c29c0ae7SAlex Tomas 		else
257c29c0ae7SAlex Tomas 			max = ext4_ext_space_block_idx(inode);
258c29c0ae7SAlex Tomas 	}
259c29c0ae7SAlex Tomas 
260c29c0ae7SAlex Tomas 	return max;
261c29c0ae7SAlex Tomas }
262c29c0ae7SAlex Tomas 
263c29c0ae7SAlex Tomas static int __ext4_ext_check_header(const char *function, struct inode *inode,
264c29c0ae7SAlex Tomas 					struct ext4_extent_header *eh,
265c29c0ae7SAlex Tomas 					int depth)
266c29c0ae7SAlex Tomas {
267c29c0ae7SAlex Tomas 	const char *error_msg;
268c29c0ae7SAlex Tomas 	int max = 0;
269c29c0ae7SAlex Tomas 
270c29c0ae7SAlex Tomas 	if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
271c29c0ae7SAlex Tomas 		error_msg = "invalid magic";
272c29c0ae7SAlex Tomas 		goto corrupted;
273c29c0ae7SAlex Tomas 	}
274c29c0ae7SAlex Tomas 	if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
275c29c0ae7SAlex Tomas 		error_msg = "unexpected eh_depth";
276c29c0ae7SAlex Tomas 		goto corrupted;
277c29c0ae7SAlex Tomas 	}
278c29c0ae7SAlex Tomas 	if (unlikely(eh->eh_max == 0)) {
279c29c0ae7SAlex Tomas 		error_msg = "invalid eh_max";
280c29c0ae7SAlex Tomas 		goto corrupted;
281c29c0ae7SAlex Tomas 	}
282c29c0ae7SAlex Tomas 	max = ext4_ext_max_entries(inode, depth);
283c29c0ae7SAlex Tomas 	if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
284c29c0ae7SAlex Tomas 		error_msg = "too large eh_max";
285c29c0ae7SAlex Tomas 		goto corrupted;
286c29c0ae7SAlex Tomas 	}
287c29c0ae7SAlex Tomas 	if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
288c29c0ae7SAlex Tomas 		error_msg = "invalid eh_entries";
289c29c0ae7SAlex Tomas 		goto corrupted;
290c29c0ae7SAlex Tomas 	}
291c29c0ae7SAlex Tomas 	return 0;
292c29c0ae7SAlex Tomas 
293c29c0ae7SAlex Tomas corrupted:
294c29c0ae7SAlex Tomas 	ext4_error(inode->i_sb, function,
295c29c0ae7SAlex Tomas 			"bad header in inode #%lu: %s - magic %x, "
296c29c0ae7SAlex Tomas 			"entries %u, max %u(%u), depth %u(%u)",
297c29c0ae7SAlex Tomas 			inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic),
298c29c0ae7SAlex Tomas 			le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
299c29c0ae7SAlex Tomas 			max, le16_to_cpu(eh->eh_depth), depth);
300c29c0ae7SAlex Tomas 
301c29c0ae7SAlex Tomas 	return -EIO;
302c29c0ae7SAlex Tomas }
303c29c0ae7SAlex Tomas 
304c29c0ae7SAlex Tomas #define ext4_ext_check_header(inode, eh, depth)	\
305c29c0ae7SAlex Tomas 	__ext4_ext_check_header(__FUNCTION__, inode, eh, depth)
306c29c0ae7SAlex Tomas 
307a86c6181SAlex Tomas #ifdef EXT_DEBUG
308a86c6181SAlex Tomas static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
309a86c6181SAlex Tomas {
310a86c6181SAlex Tomas 	int k, l = path->p_depth;
311a86c6181SAlex Tomas 
312a86c6181SAlex Tomas 	ext_debug("path:");
313a86c6181SAlex Tomas 	for (k = 0; k <= l; k++, path++) {
314a86c6181SAlex Tomas 		if (path->p_idx) {
3152ae02107SMingming Cao 		  ext_debug("  %d->%llu", le32_to_cpu(path->p_idx->ei_block),
316f65e6fbaSAlex Tomas 			    idx_pblock(path->p_idx));
317a86c6181SAlex Tomas 		} else if (path->p_ext) {
3182ae02107SMingming Cao 			ext_debug("  %d:%d:%llu ",
319a86c6181SAlex Tomas 				  le32_to_cpu(path->p_ext->ee_block),
320a2df2a63SAmit Arora 				  ext4_ext_get_actual_len(path->p_ext),
321f65e6fbaSAlex Tomas 				  ext_pblock(path->p_ext));
322a86c6181SAlex Tomas 		} else
323a86c6181SAlex Tomas 			ext_debug("  []");
324a86c6181SAlex Tomas 	}
325a86c6181SAlex Tomas 	ext_debug("\n");
326a86c6181SAlex Tomas }
327a86c6181SAlex Tomas 
328a86c6181SAlex Tomas static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
329a86c6181SAlex Tomas {
330a86c6181SAlex Tomas 	int depth = ext_depth(inode);
331a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
332a86c6181SAlex Tomas 	struct ext4_extent *ex;
333a86c6181SAlex Tomas 	int i;
334a86c6181SAlex Tomas 
335a86c6181SAlex Tomas 	if (!path)
336a86c6181SAlex Tomas 		return;
337a86c6181SAlex Tomas 
338a86c6181SAlex Tomas 	eh = path[depth].p_hdr;
339a86c6181SAlex Tomas 	ex = EXT_FIRST_EXTENT(eh);
340a86c6181SAlex Tomas 
341a86c6181SAlex Tomas 	for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
3422ae02107SMingming Cao 		ext_debug("%d:%d:%llu ", le32_to_cpu(ex->ee_block),
343a2df2a63SAmit Arora 			  ext4_ext_get_actual_len(ex), ext_pblock(ex));
344a86c6181SAlex Tomas 	}
345a86c6181SAlex Tomas 	ext_debug("\n");
346a86c6181SAlex Tomas }
347a86c6181SAlex Tomas #else
348a86c6181SAlex Tomas #define ext4_ext_show_path(inode,path)
349a86c6181SAlex Tomas #define ext4_ext_show_leaf(inode,path)
350a86c6181SAlex Tomas #endif
351a86c6181SAlex Tomas 
352a86c6181SAlex Tomas static void ext4_ext_drop_refs(struct ext4_ext_path *path)
353a86c6181SAlex Tomas {
354a86c6181SAlex Tomas 	int depth = path->p_depth;
355a86c6181SAlex Tomas 	int i;
356a86c6181SAlex Tomas 
357a86c6181SAlex Tomas 	for (i = 0; i <= depth; i++, path++)
358a86c6181SAlex Tomas 		if (path->p_bh) {
359a86c6181SAlex Tomas 			brelse(path->p_bh);
360a86c6181SAlex Tomas 			path->p_bh = NULL;
361a86c6181SAlex Tomas 		}
362a86c6181SAlex Tomas }
363a86c6181SAlex Tomas 
364a86c6181SAlex Tomas /*
365d0d856e8SRandy Dunlap  * ext4_ext_binsearch_idx:
366d0d856e8SRandy Dunlap  * binary search for the closest index of the given block
367c29c0ae7SAlex Tomas  * the header must be checked before calling this
368a86c6181SAlex Tomas  */
369a86c6181SAlex Tomas static void
370725d26d3SAneesh Kumar K.V ext4_ext_binsearch_idx(struct inode *inode,
371725d26d3SAneesh Kumar K.V 			struct ext4_ext_path *path, ext4_lblk_t block)
372a86c6181SAlex Tomas {
373a86c6181SAlex Tomas 	struct ext4_extent_header *eh = path->p_hdr;
374a86c6181SAlex Tomas 	struct ext4_extent_idx *r, *l, *m;
375a86c6181SAlex Tomas 
376a86c6181SAlex Tomas 
377bba90743SEric Sandeen 	ext_debug("binsearch for %u(idx):  ", block);
378a86c6181SAlex Tomas 
379a86c6181SAlex Tomas 	l = EXT_FIRST_INDEX(eh) + 1;
380e9f410b1SDmitry Monakhov 	r = EXT_LAST_INDEX(eh);
381a86c6181SAlex Tomas 	while (l <= r) {
382a86c6181SAlex Tomas 		m = l + (r - l) / 2;
383a86c6181SAlex Tomas 		if (block < le32_to_cpu(m->ei_block))
384a86c6181SAlex Tomas 			r = m - 1;
385a86c6181SAlex Tomas 		else
386a86c6181SAlex Tomas 			l = m + 1;
38726d535edSDmitry Monakhov 		ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
38826d535edSDmitry Monakhov 				m, le32_to_cpu(m->ei_block),
38926d535edSDmitry Monakhov 				r, le32_to_cpu(r->ei_block));
390a86c6181SAlex Tomas 	}
391a86c6181SAlex Tomas 
392a86c6181SAlex Tomas 	path->p_idx = l - 1;
393f65e6fbaSAlex Tomas 	ext_debug("  -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
39426d535edSDmitry Monakhov 		  idx_pblock(path->p_idx));
395a86c6181SAlex Tomas 
396a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH
397a86c6181SAlex Tomas 	{
398a86c6181SAlex Tomas 		struct ext4_extent_idx *chix, *ix;
399a86c6181SAlex Tomas 		int k;
400a86c6181SAlex Tomas 
401a86c6181SAlex Tomas 		chix = ix = EXT_FIRST_INDEX(eh);
402a86c6181SAlex Tomas 		for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
403a86c6181SAlex Tomas 		  if (k != 0 &&
404a86c6181SAlex Tomas 		      le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
405a86c6181SAlex Tomas 				printk("k=%d, ix=0x%p, first=0x%p\n", k,
406a86c6181SAlex Tomas 					ix, EXT_FIRST_INDEX(eh));
407a86c6181SAlex Tomas 				printk("%u <= %u\n",
408a86c6181SAlex Tomas 				       le32_to_cpu(ix->ei_block),
409a86c6181SAlex Tomas 				       le32_to_cpu(ix[-1].ei_block));
410a86c6181SAlex Tomas 			}
411a86c6181SAlex Tomas 			BUG_ON(k && le32_to_cpu(ix->ei_block)
412a86c6181SAlex Tomas 					   <= le32_to_cpu(ix[-1].ei_block));
413a86c6181SAlex Tomas 			if (block < le32_to_cpu(ix->ei_block))
414a86c6181SAlex Tomas 				break;
415a86c6181SAlex Tomas 			chix = ix;
416a86c6181SAlex Tomas 		}
417a86c6181SAlex Tomas 		BUG_ON(chix != path->p_idx);
418a86c6181SAlex Tomas 	}
419a86c6181SAlex Tomas #endif
420a86c6181SAlex Tomas 
421a86c6181SAlex Tomas }
422a86c6181SAlex Tomas 
423a86c6181SAlex Tomas /*
424d0d856e8SRandy Dunlap  * ext4_ext_binsearch:
425d0d856e8SRandy Dunlap  * binary search for closest extent of the given block
426c29c0ae7SAlex Tomas  * the header must be checked before calling this
427a86c6181SAlex Tomas  */
428a86c6181SAlex Tomas static void
429725d26d3SAneesh Kumar K.V ext4_ext_binsearch(struct inode *inode,
430725d26d3SAneesh Kumar K.V 		struct ext4_ext_path *path, ext4_lblk_t block)
431a86c6181SAlex Tomas {
432a86c6181SAlex Tomas 	struct ext4_extent_header *eh = path->p_hdr;
433a86c6181SAlex Tomas 	struct ext4_extent *r, *l, *m;
434a86c6181SAlex Tomas 
435a86c6181SAlex Tomas 	if (eh->eh_entries == 0) {
436a86c6181SAlex Tomas 		/*
437d0d856e8SRandy Dunlap 		 * this leaf is empty:
438a86c6181SAlex Tomas 		 * we get such a leaf in split/add case
439a86c6181SAlex Tomas 		 */
440a86c6181SAlex Tomas 		return;
441a86c6181SAlex Tomas 	}
442a86c6181SAlex Tomas 
443bba90743SEric Sandeen 	ext_debug("binsearch for %u:  ", block);
444a86c6181SAlex Tomas 
445a86c6181SAlex Tomas 	l = EXT_FIRST_EXTENT(eh) + 1;
446e9f410b1SDmitry Monakhov 	r = EXT_LAST_EXTENT(eh);
447a86c6181SAlex Tomas 
448a86c6181SAlex Tomas 	while (l <= r) {
449a86c6181SAlex Tomas 		m = l + (r - l) / 2;
450a86c6181SAlex Tomas 		if (block < le32_to_cpu(m->ee_block))
451a86c6181SAlex Tomas 			r = m - 1;
452a86c6181SAlex Tomas 		else
453a86c6181SAlex Tomas 			l = m + 1;
45426d535edSDmitry Monakhov 		ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
45526d535edSDmitry Monakhov 				m, le32_to_cpu(m->ee_block),
45626d535edSDmitry Monakhov 				r, le32_to_cpu(r->ee_block));
457a86c6181SAlex Tomas 	}
458a86c6181SAlex Tomas 
459a86c6181SAlex Tomas 	path->p_ext = l - 1;
4602ae02107SMingming Cao 	ext_debug("  -> %d:%llu:%d ",
461a86c6181SAlex Tomas 			le32_to_cpu(path->p_ext->ee_block),
462f65e6fbaSAlex Tomas 			ext_pblock(path->p_ext),
463a2df2a63SAmit Arora 			ext4_ext_get_actual_len(path->p_ext));
464a86c6181SAlex Tomas 
465a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH
466a86c6181SAlex Tomas 	{
467a86c6181SAlex Tomas 		struct ext4_extent *chex, *ex;
468a86c6181SAlex Tomas 		int k;
469a86c6181SAlex Tomas 
470a86c6181SAlex Tomas 		chex = ex = EXT_FIRST_EXTENT(eh);
471a86c6181SAlex Tomas 		for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
472a86c6181SAlex Tomas 			BUG_ON(k && le32_to_cpu(ex->ee_block)
473a86c6181SAlex Tomas 					  <= le32_to_cpu(ex[-1].ee_block));
474a86c6181SAlex Tomas 			if (block < le32_to_cpu(ex->ee_block))
475a86c6181SAlex Tomas 				break;
476a86c6181SAlex Tomas 			chex = ex;
477a86c6181SAlex Tomas 		}
478a86c6181SAlex Tomas 		BUG_ON(chex != path->p_ext);
479a86c6181SAlex Tomas 	}
480a86c6181SAlex Tomas #endif
481a86c6181SAlex Tomas 
482a86c6181SAlex Tomas }
483a86c6181SAlex Tomas 
484a86c6181SAlex Tomas int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
485a86c6181SAlex Tomas {
486a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
487a86c6181SAlex Tomas 
488a86c6181SAlex Tomas 	eh = ext_inode_hdr(inode);
489a86c6181SAlex Tomas 	eh->eh_depth = 0;
490a86c6181SAlex Tomas 	eh->eh_entries = 0;
491a86c6181SAlex Tomas 	eh->eh_magic = EXT4_EXT_MAGIC;
492a86c6181SAlex Tomas 	eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode));
493a86c6181SAlex Tomas 	ext4_mark_inode_dirty(handle, inode);
494a86c6181SAlex Tomas 	ext4_ext_invalidate_cache(inode);
495a86c6181SAlex Tomas 	return 0;
496a86c6181SAlex Tomas }
497a86c6181SAlex Tomas 
498a86c6181SAlex Tomas struct ext4_ext_path *
499725d26d3SAneesh Kumar K.V ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
500725d26d3SAneesh Kumar K.V 					struct ext4_ext_path *path)
501a86c6181SAlex Tomas {
502a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
503a86c6181SAlex Tomas 	struct buffer_head *bh;
504a86c6181SAlex Tomas 	short int depth, i, ppos = 0, alloc = 0;
505a86c6181SAlex Tomas 
506a86c6181SAlex Tomas 	eh = ext_inode_hdr(inode);
507c29c0ae7SAlex Tomas 	depth = ext_depth(inode);
508c29c0ae7SAlex Tomas 	if (ext4_ext_check_header(inode, eh, depth))
509a86c6181SAlex Tomas 		return ERR_PTR(-EIO);
510a86c6181SAlex Tomas 
511a86c6181SAlex Tomas 
512a86c6181SAlex Tomas 	/* account possible depth increase */
513a86c6181SAlex Tomas 	if (!path) {
5145d4958f9SAvantika Mathur 		path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
515a86c6181SAlex Tomas 				GFP_NOFS);
516a86c6181SAlex Tomas 		if (!path)
517a86c6181SAlex Tomas 			return ERR_PTR(-ENOMEM);
518a86c6181SAlex Tomas 		alloc = 1;
519a86c6181SAlex Tomas 	}
520a86c6181SAlex Tomas 	path[0].p_hdr = eh;
521a86c6181SAlex Tomas 
522c29c0ae7SAlex Tomas 	i = depth;
523a86c6181SAlex Tomas 	/* walk through the tree */
524a86c6181SAlex Tomas 	while (i) {
525a86c6181SAlex Tomas 		ext_debug("depth %d: num %d, max %d\n",
526a86c6181SAlex Tomas 			  ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
527c29c0ae7SAlex Tomas 
528a86c6181SAlex Tomas 		ext4_ext_binsearch_idx(inode, path + ppos, block);
529f65e6fbaSAlex Tomas 		path[ppos].p_block = idx_pblock(path[ppos].p_idx);
530a86c6181SAlex Tomas 		path[ppos].p_depth = i;
531a86c6181SAlex Tomas 		path[ppos].p_ext = NULL;
532a86c6181SAlex Tomas 
533a86c6181SAlex Tomas 		bh = sb_bread(inode->i_sb, path[ppos].p_block);
534a86c6181SAlex Tomas 		if (!bh)
535a86c6181SAlex Tomas 			goto err;
536a86c6181SAlex Tomas 
537a86c6181SAlex Tomas 		eh = ext_block_hdr(bh);
538a86c6181SAlex Tomas 		ppos++;
539a86c6181SAlex Tomas 		BUG_ON(ppos > depth);
540a86c6181SAlex Tomas 		path[ppos].p_bh = bh;
541a86c6181SAlex Tomas 		path[ppos].p_hdr = eh;
542a86c6181SAlex Tomas 		i--;
543a86c6181SAlex Tomas 
544c29c0ae7SAlex Tomas 		if (ext4_ext_check_header(inode, eh, i))
545a86c6181SAlex Tomas 			goto err;
546a86c6181SAlex Tomas 	}
547a86c6181SAlex Tomas 
548a86c6181SAlex Tomas 	path[ppos].p_depth = i;
549a86c6181SAlex Tomas 	path[ppos].p_hdr = eh;
550a86c6181SAlex Tomas 	path[ppos].p_ext = NULL;
551a86c6181SAlex Tomas 	path[ppos].p_idx = NULL;
552a86c6181SAlex Tomas 
553a86c6181SAlex Tomas 	/* find extent */
554a86c6181SAlex Tomas 	ext4_ext_binsearch(inode, path + ppos, block);
555a86c6181SAlex Tomas 
556a86c6181SAlex Tomas 	ext4_ext_show_path(inode, path);
557a86c6181SAlex Tomas 
558a86c6181SAlex Tomas 	return path;
559a86c6181SAlex Tomas 
560a86c6181SAlex Tomas err:
561a86c6181SAlex Tomas 	ext4_ext_drop_refs(path);
562a86c6181SAlex Tomas 	if (alloc)
563a86c6181SAlex Tomas 		kfree(path);
564a86c6181SAlex Tomas 	return ERR_PTR(-EIO);
565a86c6181SAlex Tomas }
566a86c6181SAlex Tomas 
567a86c6181SAlex Tomas /*
568d0d856e8SRandy Dunlap  * ext4_ext_insert_index:
569d0d856e8SRandy Dunlap  * insert new index [@logical;@ptr] into the block at @curp;
570d0d856e8SRandy Dunlap  * check where to insert: before @curp or after @curp
571a86c6181SAlex Tomas  */
572a86c6181SAlex Tomas static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
573a86c6181SAlex Tomas 				struct ext4_ext_path *curp,
574f65e6fbaSAlex Tomas 				int logical, ext4_fsblk_t ptr)
575a86c6181SAlex Tomas {
576a86c6181SAlex Tomas 	struct ext4_extent_idx *ix;
577a86c6181SAlex Tomas 	int len, err;
578a86c6181SAlex Tomas 
5797e028976SAvantika Mathur 	err = ext4_ext_get_access(handle, inode, curp);
5807e028976SAvantika Mathur 	if (err)
581a86c6181SAlex Tomas 		return err;
582a86c6181SAlex Tomas 
583a86c6181SAlex Tomas 	BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block));
584a86c6181SAlex Tomas 	len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
585a86c6181SAlex Tomas 	if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
586a86c6181SAlex Tomas 		/* insert after */
587a86c6181SAlex Tomas 		if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
588a86c6181SAlex Tomas 			len = (len - 1) * sizeof(struct ext4_extent_idx);
589a86c6181SAlex Tomas 			len = len < 0 ? 0 : len;
59026d535edSDmitry Monakhov 			ext_debug("insert new index %d after: %llu. "
591a86c6181SAlex Tomas 					"move %d from 0x%p to 0x%p\n",
592a86c6181SAlex Tomas 					logical, ptr, len,
593a86c6181SAlex Tomas 					(curp->p_idx + 1), (curp->p_idx + 2));
594a86c6181SAlex Tomas 			memmove(curp->p_idx + 2, curp->p_idx + 1, len);
595a86c6181SAlex Tomas 		}
596a86c6181SAlex Tomas 		ix = curp->p_idx + 1;
597a86c6181SAlex Tomas 	} else {
598a86c6181SAlex Tomas 		/* insert before */
599a86c6181SAlex Tomas 		len = len * sizeof(struct ext4_extent_idx);
600a86c6181SAlex Tomas 		len = len < 0 ? 0 : len;
60126d535edSDmitry Monakhov 		ext_debug("insert new index %d before: %llu. "
602a86c6181SAlex Tomas 				"move %d from 0x%p to 0x%p\n",
603a86c6181SAlex Tomas 				logical, ptr, len,
604a86c6181SAlex Tomas 				curp->p_idx, (curp->p_idx + 1));
605a86c6181SAlex Tomas 		memmove(curp->p_idx + 1, curp->p_idx, len);
606a86c6181SAlex Tomas 		ix = curp->p_idx;
607a86c6181SAlex Tomas 	}
608a86c6181SAlex Tomas 
609a86c6181SAlex Tomas 	ix->ei_block = cpu_to_le32(logical);
610f65e6fbaSAlex Tomas 	ext4_idx_store_pblock(ix, ptr);
611a86c6181SAlex Tomas 	curp->p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(curp->p_hdr->eh_entries)+1);
612a86c6181SAlex Tomas 
613a86c6181SAlex Tomas 	BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries)
614a86c6181SAlex Tomas 			     > le16_to_cpu(curp->p_hdr->eh_max));
615a86c6181SAlex Tomas 	BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr));
616a86c6181SAlex Tomas 
617a86c6181SAlex Tomas 	err = ext4_ext_dirty(handle, inode, curp);
618a86c6181SAlex Tomas 	ext4_std_error(inode->i_sb, err);
619a86c6181SAlex Tomas 
620a86c6181SAlex Tomas 	return err;
621a86c6181SAlex Tomas }
622a86c6181SAlex Tomas 
623a86c6181SAlex Tomas /*
624d0d856e8SRandy Dunlap  * ext4_ext_split:
625d0d856e8SRandy Dunlap  * inserts new subtree into the path, using free index entry
626d0d856e8SRandy Dunlap  * at depth @at:
627a86c6181SAlex Tomas  * - allocates all needed blocks (new leaf and all intermediate index blocks)
628a86c6181SAlex Tomas  * - makes decision where to split
629d0d856e8SRandy Dunlap  * - moves remaining extents and index entries (right to the split point)
630a86c6181SAlex Tomas  *   into the newly allocated blocks
631d0d856e8SRandy Dunlap  * - initializes subtree
632a86c6181SAlex Tomas  */
633a86c6181SAlex Tomas static int ext4_ext_split(handle_t *handle, struct inode *inode,
634a86c6181SAlex Tomas 				struct ext4_ext_path *path,
635a86c6181SAlex Tomas 				struct ext4_extent *newext, int at)
636a86c6181SAlex Tomas {
637a86c6181SAlex Tomas 	struct buffer_head *bh = NULL;
638a86c6181SAlex Tomas 	int depth = ext_depth(inode);
639a86c6181SAlex Tomas 	struct ext4_extent_header *neh;
640a86c6181SAlex Tomas 	struct ext4_extent_idx *fidx;
641a86c6181SAlex Tomas 	struct ext4_extent *ex;
642a86c6181SAlex Tomas 	int i = at, k, m, a;
643f65e6fbaSAlex Tomas 	ext4_fsblk_t newblock, oldblock;
644a86c6181SAlex Tomas 	__le32 border;
645f65e6fbaSAlex Tomas 	ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
646a86c6181SAlex Tomas 	int err = 0;
647a86c6181SAlex Tomas 
648a86c6181SAlex Tomas 	/* make decision: where to split? */
649d0d856e8SRandy Dunlap 	/* FIXME: now decision is simplest: at current extent */
650a86c6181SAlex Tomas 
651d0d856e8SRandy Dunlap 	/* if current leaf will be split, then we should use
652a86c6181SAlex Tomas 	 * border from split point */
653a86c6181SAlex Tomas 	BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr));
654a86c6181SAlex Tomas 	if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
655a86c6181SAlex Tomas 		border = path[depth].p_ext[1].ee_block;
656d0d856e8SRandy Dunlap 		ext_debug("leaf will be split."
657a86c6181SAlex Tomas 				" next leaf starts at %d\n",
658a86c6181SAlex Tomas 				  le32_to_cpu(border));
659a86c6181SAlex Tomas 	} else {
660a86c6181SAlex Tomas 		border = newext->ee_block;
661a86c6181SAlex Tomas 		ext_debug("leaf will be added."
662a86c6181SAlex Tomas 				" next leaf starts at %d\n",
663a86c6181SAlex Tomas 				le32_to_cpu(border));
664a86c6181SAlex Tomas 	}
665a86c6181SAlex Tomas 
666a86c6181SAlex Tomas 	/*
667d0d856e8SRandy Dunlap 	 * If error occurs, then we break processing
668d0d856e8SRandy Dunlap 	 * and mark filesystem read-only. index won't
669a86c6181SAlex Tomas 	 * be inserted and tree will be in consistent
670d0d856e8SRandy Dunlap 	 * state. Next mount will repair buffers too.
671a86c6181SAlex Tomas 	 */
672a86c6181SAlex Tomas 
673a86c6181SAlex Tomas 	/*
674d0d856e8SRandy Dunlap 	 * Get array to track all allocated blocks.
675d0d856e8SRandy Dunlap 	 * We need this to handle errors and free blocks
676d0d856e8SRandy Dunlap 	 * upon them.
677a86c6181SAlex Tomas 	 */
6785d4958f9SAvantika Mathur 	ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
679a86c6181SAlex Tomas 	if (!ablocks)
680a86c6181SAlex Tomas 		return -ENOMEM;
681a86c6181SAlex Tomas 
682a86c6181SAlex Tomas 	/* allocate all needed blocks */
683a86c6181SAlex Tomas 	ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
684a86c6181SAlex Tomas 	for (a = 0; a < depth - at; a++) {
685a86c6181SAlex Tomas 		newblock = ext4_ext_new_block(handle, inode, path, newext, &err);
686a86c6181SAlex Tomas 		if (newblock == 0)
687a86c6181SAlex Tomas 			goto cleanup;
688a86c6181SAlex Tomas 		ablocks[a] = newblock;
689a86c6181SAlex Tomas 	}
690a86c6181SAlex Tomas 
691a86c6181SAlex Tomas 	/* initialize new leaf */
692a86c6181SAlex Tomas 	newblock = ablocks[--a];
693a86c6181SAlex Tomas 	BUG_ON(newblock == 0);
694a86c6181SAlex Tomas 	bh = sb_getblk(inode->i_sb, newblock);
695a86c6181SAlex Tomas 	if (!bh) {
696a86c6181SAlex Tomas 		err = -EIO;
697a86c6181SAlex Tomas 		goto cleanup;
698a86c6181SAlex Tomas 	}
699a86c6181SAlex Tomas 	lock_buffer(bh);
700a86c6181SAlex Tomas 
7017e028976SAvantika Mathur 	err = ext4_journal_get_create_access(handle, bh);
7027e028976SAvantika Mathur 	if (err)
703a86c6181SAlex Tomas 		goto cleanup;
704a86c6181SAlex Tomas 
705a86c6181SAlex Tomas 	neh = ext_block_hdr(bh);
706a86c6181SAlex Tomas 	neh->eh_entries = 0;
707a86c6181SAlex Tomas 	neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
708a86c6181SAlex Tomas 	neh->eh_magic = EXT4_EXT_MAGIC;
709a86c6181SAlex Tomas 	neh->eh_depth = 0;
710a86c6181SAlex Tomas 	ex = EXT_FIRST_EXTENT(neh);
711a86c6181SAlex Tomas 
712d0d856e8SRandy Dunlap 	/* move remainder of path[depth] to the new leaf */
713a86c6181SAlex Tomas 	BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max);
714a86c6181SAlex Tomas 	/* start copy from next extent */
715a86c6181SAlex Tomas 	/* TODO: we could do it by single memmove */
716a86c6181SAlex Tomas 	m = 0;
717a86c6181SAlex Tomas 	path[depth].p_ext++;
718a86c6181SAlex Tomas 	while (path[depth].p_ext <=
719a86c6181SAlex Tomas 			EXT_MAX_EXTENT(path[depth].p_hdr)) {
7202ae02107SMingming Cao 		ext_debug("move %d:%llu:%d in new leaf %llu\n",
721a86c6181SAlex Tomas 				le32_to_cpu(path[depth].p_ext->ee_block),
722f65e6fbaSAlex Tomas 				ext_pblock(path[depth].p_ext),
723a2df2a63SAmit Arora 				ext4_ext_get_actual_len(path[depth].p_ext),
724a86c6181SAlex Tomas 				newblock);
725a86c6181SAlex Tomas 		/*memmove(ex++, path[depth].p_ext++,
726a86c6181SAlex Tomas 				sizeof(struct ext4_extent));
727a86c6181SAlex Tomas 		neh->eh_entries++;*/
728a86c6181SAlex Tomas 		path[depth].p_ext++;
729a86c6181SAlex Tomas 		m++;
730a86c6181SAlex Tomas 	}
731a86c6181SAlex Tomas 	if (m) {
732a86c6181SAlex Tomas 		memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
733a86c6181SAlex Tomas 		neh->eh_entries = cpu_to_le16(le16_to_cpu(neh->eh_entries)+m);
734a86c6181SAlex Tomas 	}
735a86c6181SAlex Tomas 
736a86c6181SAlex Tomas 	set_buffer_uptodate(bh);
737a86c6181SAlex Tomas 	unlock_buffer(bh);
738a86c6181SAlex Tomas 
7397e028976SAvantika Mathur 	err = ext4_journal_dirty_metadata(handle, bh);
7407e028976SAvantika Mathur 	if (err)
741a86c6181SAlex Tomas 		goto cleanup;
742a86c6181SAlex Tomas 	brelse(bh);
743a86c6181SAlex Tomas 	bh = NULL;
744a86c6181SAlex Tomas 
745a86c6181SAlex Tomas 	/* correct old leaf */
746a86c6181SAlex Tomas 	if (m) {
7477e028976SAvantika Mathur 		err = ext4_ext_get_access(handle, inode, path + depth);
7487e028976SAvantika Mathur 		if (err)
749a86c6181SAlex Tomas 			goto cleanup;
750a86c6181SAlex Tomas 		path[depth].p_hdr->eh_entries =
751a86c6181SAlex Tomas 		     cpu_to_le16(le16_to_cpu(path[depth].p_hdr->eh_entries)-m);
7527e028976SAvantika Mathur 		err = ext4_ext_dirty(handle, inode, path + depth);
7537e028976SAvantika Mathur 		if (err)
754a86c6181SAlex Tomas 			goto cleanup;
755a86c6181SAlex Tomas 
756a86c6181SAlex Tomas 	}
757a86c6181SAlex Tomas 
758a86c6181SAlex Tomas 	/* create intermediate indexes */
759a86c6181SAlex Tomas 	k = depth - at - 1;
760a86c6181SAlex Tomas 	BUG_ON(k < 0);
761a86c6181SAlex Tomas 	if (k)
762a86c6181SAlex Tomas 		ext_debug("create %d intermediate indices\n", k);
763a86c6181SAlex Tomas 	/* insert new index into current index block */
764a86c6181SAlex Tomas 	/* current depth stored in i var */
765a86c6181SAlex Tomas 	i = depth - 1;
766a86c6181SAlex Tomas 	while (k--) {
767a86c6181SAlex Tomas 		oldblock = newblock;
768a86c6181SAlex Tomas 		newblock = ablocks[--a];
769bba90743SEric Sandeen 		bh = sb_getblk(inode->i_sb, newblock);
770a86c6181SAlex Tomas 		if (!bh) {
771a86c6181SAlex Tomas 			err = -EIO;
772a86c6181SAlex Tomas 			goto cleanup;
773a86c6181SAlex Tomas 		}
774a86c6181SAlex Tomas 		lock_buffer(bh);
775a86c6181SAlex Tomas 
7767e028976SAvantika Mathur 		err = ext4_journal_get_create_access(handle, bh);
7777e028976SAvantika Mathur 		if (err)
778a86c6181SAlex Tomas 			goto cleanup;
779a86c6181SAlex Tomas 
780a86c6181SAlex Tomas 		neh = ext_block_hdr(bh);
781a86c6181SAlex Tomas 		neh->eh_entries = cpu_to_le16(1);
782a86c6181SAlex Tomas 		neh->eh_magic = EXT4_EXT_MAGIC;
783a86c6181SAlex Tomas 		neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
784a86c6181SAlex Tomas 		neh->eh_depth = cpu_to_le16(depth - i);
785a86c6181SAlex Tomas 		fidx = EXT_FIRST_INDEX(neh);
786a86c6181SAlex Tomas 		fidx->ei_block = border;
787f65e6fbaSAlex Tomas 		ext4_idx_store_pblock(fidx, oldblock);
788a86c6181SAlex Tomas 
789bba90743SEric Sandeen 		ext_debug("int.index at %d (block %llu): %u -> %llu\n",
790bba90743SEric Sandeen 				i, newblock, le32_to_cpu(border), oldblock);
791a86c6181SAlex Tomas 		/* copy indexes */
792a86c6181SAlex Tomas 		m = 0;
793a86c6181SAlex Tomas 		path[i].p_idx++;
794a86c6181SAlex Tomas 
795a86c6181SAlex Tomas 		ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
796a86c6181SAlex Tomas 				EXT_MAX_INDEX(path[i].p_hdr));
797a86c6181SAlex Tomas 		BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) !=
798a86c6181SAlex Tomas 				EXT_LAST_INDEX(path[i].p_hdr));
799a86c6181SAlex Tomas 		while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
80026d535edSDmitry Monakhov 			ext_debug("%d: move %d:%llu in new index %llu\n", i,
801a86c6181SAlex Tomas 					le32_to_cpu(path[i].p_idx->ei_block),
802f65e6fbaSAlex Tomas 					idx_pblock(path[i].p_idx),
803a86c6181SAlex Tomas 					newblock);
804a86c6181SAlex Tomas 			/*memmove(++fidx, path[i].p_idx++,
805a86c6181SAlex Tomas 					sizeof(struct ext4_extent_idx));
806a86c6181SAlex Tomas 			neh->eh_entries++;
807a86c6181SAlex Tomas 			BUG_ON(neh->eh_entries > neh->eh_max);*/
808a86c6181SAlex Tomas 			path[i].p_idx++;
809a86c6181SAlex Tomas 			m++;
810a86c6181SAlex Tomas 		}
811a86c6181SAlex Tomas 		if (m) {
812a86c6181SAlex Tomas 			memmove(++fidx, path[i].p_idx - m,
813a86c6181SAlex Tomas 				sizeof(struct ext4_extent_idx) * m);
814a86c6181SAlex Tomas 			neh->eh_entries =
815a86c6181SAlex Tomas 				cpu_to_le16(le16_to_cpu(neh->eh_entries) + m);
816a86c6181SAlex Tomas 		}
817a86c6181SAlex Tomas 		set_buffer_uptodate(bh);
818a86c6181SAlex Tomas 		unlock_buffer(bh);
819a86c6181SAlex Tomas 
8207e028976SAvantika Mathur 		err = ext4_journal_dirty_metadata(handle, bh);
8217e028976SAvantika Mathur 		if (err)
822a86c6181SAlex Tomas 			goto cleanup;
823a86c6181SAlex Tomas 		brelse(bh);
824a86c6181SAlex Tomas 		bh = NULL;
825a86c6181SAlex Tomas 
826a86c6181SAlex Tomas 		/* correct old index */
827a86c6181SAlex Tomas 		if (m) {
828a86c6181SAlex Tomas 			err = ext4_ext_get_access(handle, inode, path + i);
829a86c6181SAlex Tomas 			if (err)
830a86c6181SAlex Tomas 				goto cleanup;
831a86c6181SAlex Tomas 			path[i].p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(path[i].p_hdr->eh_entries)-m);
832a86c6181SAlex Tomas 			err = ext4_ext_dirty(handle, inode, path + i);
833a86c6181SAlex Tomas 			if (err)
834a86c6181SAlex Tomas 				goto cleanup;
835a86c6181SAlex Tomas 		}
836a86c6181SAlex Tomas 
837a86c6181SAlex Tomas 		i--;
838a86c6181SAlex Tomas 	}
839a86c6181SAlex Tomas 
840a86c6181SAlex Tomas 	/* insert new index */
841a86c6181SAlex Tomas 	err = ext4_ext_insert_index(handle, inode, path + at,
842a86c6181SAlex Tomas 				    le32_to_cpu(border), newblock);
843a86c6181SAlex Tomas 
844a86c6181SAlex Tomas cleanup:
845a86c6181SAlex Tomas 	if (bh) {
846a86c6181SAlex Tomas 		if (buffer_locked(bh))
847a86c6181SAlex Tomas 			unlock_buffer(bh);
848a86c6181SAlex Tomas 		brelse(bh);
849a86c6181SAlex Tomas 	}
850a86c6181SAlex Tomas 
851a86c6181SAlex Tomas 	if (err) {
852a86c6181SAlex Tomas 		/* free all allocated blocks in error case */
853a86c6181SAlex Tomas 		for (i = 0; i < depth; i++) {
854a86c6181SAlex Tomas 			if (!ablocks[i])
855a86c6181SAlex Tomas 				continue;
856c9de560dSAlex Tomas 			ext4_free_blocks(handle, inode, ablocks[i], 1, 1);
857a86c6181SAlex Tomas 		}
858a86c6181SAlex Tomas 	}
859a86c6181SAlex Tomas 	kfree(ablocks);
860a86c6181SAlex Tomas 
861a86c6181SAlex Tomas 	return err;
862a86c6181SAlex Tomas }
863a86c6181SAlex Tomas 
864a86c6181SAlex Tomas /*
865d0d856e8SRandy Dunlap  * ext4_ext_grow_indepth:
866d0d856e8SRandy Dunlap  * implements tree growing procedure:
867a86c6181SAlex Tomas  * - allocates new block
868a86c6181SAlex Tomas  * - moves top-level data (index block or leaf) into the new block
869d0d856e8SRandy Dunlap  * - initializes new top-level, creating index that points to the
870a86c6181SAlex Tomas  *   just created block
871a86c6181SAlex Tomas  */
872a86c6181SAlex Tomas static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
873a86c6181SAlex Tomas 					struct ext4_ext_path *path,
874a86c6181SAlex Tomas 					struct ext4_extent *newext)
875a86c6181SAlex Tomas {
876a86c6181SAlex Tomas 	struct ext4_ext_path *curp = path;
877a86c6181SAlex Tomas 	struct ext4_extent_header *neh;
878a86c6181SAlex Tomas 	struct ext4_extent_idx *fidx;
879a86c6181SAlex Tomas 	struct buffer_head *bh;
880f65e6fbaSAlex Tomas 	ext4_fsblk_t newblock;
881a86c6181SAlex Tomas 	int err = 0;
882a86c6181SAlex Tomas 
883a86c6181SAlex Tomas 	newblock = ext4_ext_new_block(handle, inode, path, newext, &err);
884a86c6181SAlex Tomas 	if (newblock == 0)
885a86c6181SAlex Tomas 		return err;
886a86c6181SAlex Tomas 
887a86c6181SAlex Tomas 	bh = sb_getblk(inode->i_sb, newblock);
888a86c6181SAlex Tomas 	if (!bh) {
889a86c6181SAlex Tomas 		err = -EIO;
890a86c6181SAlex Tomas 		ext4_std_error(inode->i_sb, err);
891a86c6181SAlex Tomas 		return err;
892a86c6181SAlex Tomas 	}
893a86c6181SAlex Tomas 	lock_buffer(bh);
894a86c6181SAlex Tomas 
8957e028976SAvantika Mathur 	err = ext4_journal_get_create_access(handle, bh);
8967e028976SAvantika Mathur 	if (err) {
897a86c6181SAlex Tomas 		unlock_buffer(bh);
898a86c6181SAlex Tomas 		goto out;
899a86c6181SAlex Tomas 	}
900a86c6181SAlex Tomas 
901a86c6181SAlex Tomas 	/* move top-level index/leaf into new block */
902a86c6181SAlex Tomas 	memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
903a86c6181SAlex Tomas 
904a86c6181SAlex Tomas 	/* set size of new block */
905a86c6181SAlex Tomas 	neh = ext_block_hdr(bh);
906a86c6181SAlex Tomas 	/* old root could have indexes or leaves
907a86c6181SAlex Tomas 	 * so calculate e_max right way */
908a86c6181SAlex Tomas 	if (ext_depth(inode))
909a86c6181SAlex Tomas 	  neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
910a86c6181SAlex Tomas 	else
911a86c6181SAlex Tomas 	  neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
912a86c6181SAlex Tomas 	neh->eh_magic = EXT4_EXT_MAGIC;
913a86c6181SAlex Tomas 	set_buffer_uptodate(bh);
914a86c6181SAlex Tomas 	unlock_buffer(bh);
915a86c6181SAlex Tomas 
9167e028976SAvantika Mathur 	err = ext4_journal_dirty_metadata(handle, bh);
9177e028976SAvantika Mathur 	if (err)
918a86c6181SAlex Tomas 		goto out;
919a86c6181SAlex Tomas 
920a86c6181SAlex Tomas 	/* create index in new top-level index: num,max,pointer */
9217e028976SAvantika Mathur 	err = ext4_ext_get_access(handle, inode, curp);
9227e028976SAvantika Mathur 	if (err)
923a86c6181SAlex Tomas 		goto out;
924a86c6181SAlex Tomas 
925a86c6181SAlex Tomas 	curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
926a86c6181SAlex Tomas 	curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode));
927a86c6181SAlex Tomas 	curp->p_hdr->eh_entries = cpu_to_le16(1);
928a86c6181SAlex Tomas 	curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
929e9f410b1SDmitry Monakhov 
930e9f410b1SDmitry Monakhov 	if (path[0].p_hdr->eh_depth)
931e9f410b1SDmitry Monakhov 		curp->p_idx->ei_block =
932e9f410b1SDmitry Monakhov 			EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
933e9f410b1SDmitry Monakhov 	else
934e9f410b1SDmitry Monakhov 		curp->p_idx->ei_block =
935e9f410b1SDmitry Monakhov 			EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
936f65e6fbaSAlex Tomas 	ext4_idx_store_pblock(curp->p_idx, newblock);
937a86c6181SAlex Tomas 
938a86c6181SAlex Tomas 	neh = ext_inode_hdr(inode);
939a86c6181SAlex Tomas 	fidx = EXT_FIRST_INDEX(neh);
9402ae02107SMingming Cao 	ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
941a86c6181SAlex Tomas 		  le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
942f65e6fbaSAlex Tomas 		  le32_to_cpu(fidx->ei_block), idx_pblock(fidx));
943a86c6181SAlex Tomas 
944a86c6181SAlex Tomas 	neh->eh_depth = cpu_to_le16(path->p_depth + 1);
945a86c6181SAlex Tomas 	err = ext4_ext_dirty(handle, inode, curp);
946a86c6181SAlex Tomas out:
947a86c6181SAlex Tomas 	brelse(bh);
948a86c6181SAlex Tomas 
949a86c6181SAlex Tomas 	return err;
950a86c6181SAlex Tomas }
951a86c6181SAlex Tomas 
952a86c6181SAlex Tomas /*
953d0d856e8SRandy Dunlap  * ext4_ext_create_new_leaf:
954d0d856e8SRandy Dunlap  * finds empty index and adds new leaf.
955d0d856e8SRandy Dunlap  * if no free index is found, then it requests in-depth growing.
956a86c6181SAlex Tomas  */
957a86c6181SAlex Tomas static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
958a86c6181SAlex Tomas 					struct ext4_ext_path *path,
959a86c6181SAlex Tomas 					struct ext4_extent *newext)
960a86c6181SAlex Tomas {
961a86c6181SAlex Tomas 	struct ext4_ext_path *curp;
962a86c6181SAlex Tomas 	int depth, i, err = 0;
963a86c6181SAlex Tomas 
964a86c6181SAlex Tomas repeat:
965a86c6181SAlex Tomas 	i = depth = ext_depth(inode);
966a86c6181SAlex Tomas 
967a86c6181SAlex Tomas 	/* walk up to the tree and look for free index entry */
968a86c6181SAlex Tomas 	curp = path + depth;
969a86c6181SAlex Tomas 	while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
970a86c6181SAlex Tomas 		i--;
971a86c6181SAlex Tomas 		curp--;
972a86c6181SAlex Tomas 	}
973a86c6181SAlex Tomas 
974d0d856e8SRandy Dunlap 	/* we use already allocated block for index block,
975d0d856e8SRandy Dunlap 	 * so subsequent data blocks should be contiguous */
976a86c6181SAlex Tomas 	if (EXT_HAS_FREE_INDEX(curp)) {
977a86c6181SAlex Tomas 		/* if we found index with free entry, then use that
978a86c6181SAlex Tomas 		 * entry: create all needed subtree and add new leaf */
979a86c6181SAlex Tomas 		err = ext4_ext_split(handle, inode, path, newext, i);
980a86c6181SAlex Tomas 
981a86c6181SAlex Tomas 		/* refill path */
982a86c6181SAlex Tomas 		ext4_ext_drop_refs(path);
983a86c6181SAlex Tomas 		path = ext4_ext_find_extent(inode,
984725d26d3SAneesh Kumar K.V 				    (ext4_lblk_t)le32_to_cpu(newext->ee_block),
985a86c6181SAlex Tomas 				    path);
986a86c6181SAlex Tomas 		if (IS_ERR(path))
987a86c6181SAlex Tomas 			err = PTR_ERR(path);
988a86c6181SAlex Tomas 	} else {
989a86c6181SAlex Tomas 		/* tree is full, time to grow in depth */
990a86c6181SAlex Tomas 		err = ext4_ext_grow_indepth(handle, inode, path, newext);
991a86c6181SAlex Tomas 		if (err)
992a86c6181SAlex Tomas 			goto out;
993a86c6181SAlex Tomas 
994a86c6181SAlex Tomas 		/* refill path */
995a86c6181SAlex Tomas 		ext4_ext_drop_refs(path);
996a86c6181SAlex Tomas 		path = ext4_ext_find_extent(inode,
997725d26d3SAneesh Kumar K.V 				   (ext4_lblk_t)le32_to_cpu(newext->ee_block),
998a86c6181SAlex Tomas 				    path);
999a86c6181SAlex Tomas 		if (IS_ERR(path)) {
1000a86c6181SAlex Tomas 			err = PTR_ERR(path);
1001a86c6181SAlex Tomas 			goto out;
1002a86c6181SAlex Tomas 		}
1003a86c6181SAlex Tomas 
1004a86c6181SAlex Tomas 		/*
1005d0d856e8SRandy Dunlap 		 * only first (depth 0 -> 1) produces free space;
1006d0d856e8SRandy Dunlap 		 * in all other cases we have to split the grown tree
1007a86c6181SAlex Tomas 		 */
1008a86c6181SAlex Tomas 		depth = ext_depth(inode);
1009a86c6181SAlex Tomas 		if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
1010d0d856e8SRandy Dunlap 			/* now we need to split */
1011a86c6181SAlex Tomas 			goto repeat;
1012a86c6181SAlex Tomas 		}
1013a86c6181SAlex Tomas 	}
1014a86c6181SAlex Tomas 
1015a86c6181SAlex Tomas out:
1016a86c6181SAlex Tomas 	return err;
1017a86c6181SAlex Tomas }
1018a86c6181SAlex Tomas 
1019a86c6181SAlex Tomas /*
10201988b51eSAlex Tomas  * search the closest allocated block to the left for *logical
10211988b51eSAlex Tomas  * and returns it at @logical + it's physical address at @phys
10221988b51eSAlex Tomas  * if *logical is the smallest allocated block, the function
10231988b51eSAlex Tomas  * returns 0 at @phys
10241988b51eSAlex Tomas  * return value contains 0 (success) or error code
10251988b51eSAlex Tomas  */
10261988b51eSAlex Tomas int
10271988b51eSAlex Tomas ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path,
10281988b51eSAlex Tomas 			ext4_lblk_t *logical, ext4_fsblk_t *phys)
10291988b51eSAlex Tomas {
10301988b51eSAlex Tomas 	struct ext4_extent_idx *ix;
10311988b51eSAlex Tomas 	struct ext4_extent *ex;
10321988b51eSAlex Tomas 	int depth;
10331988b51eSAlex Tomas 
10341988b51eSAlex Tomas 	BUG_ON(path == NULL);
10351988b51eSAlex Tomas 	depth = path->p_depth;
10361988b51eSAlex Tomas 	*phys = 0;
10371988b51eSAlex Tomas 
10381988b51eSAlex Tomas 	if (depth == 0 && path->p_ext == NULL)
10391988b51eSAlex Tomas 		return 0;
10401988b51eSAlex Tomas 
10411988b51eSAlex Tomas 	/* usually extent in the path covers blocks smaller
10421988b51eSAlex Tomas 	 * then *logical, but it can be that extent is the
10431988b51eSAlex Tomas 	 * first one in the file */
10441988b51eSAlex Tomas 
10451988b51eSAlex Tomas 	ex = path[depth].p_ext;
10461988b51eSAlex Tomas 	if (*logical < le32_to_cpu(ex->ee_block)) {
10471988b51eSAlex Tomas 		BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
10481988b51eSAlex Tomas 		while (--depth >= 0) {
10491988b51eSAlex Tomas 			ix = path[depth].p_idx;
10501988b51eSAlex Tomas 			BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
10511988b51eSAlex Tomas 		}
10521988b51eSAlex Tomas 		return 0;
10531988b51eSAlex Tomas 	}
10541988b51eSAlex Tomas 
10551988b51eSAlex Tomas 	BUG_ON(*logical < le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len));
10561988b51eSAlex Tomas 
10571988b51eSAlex Tomas 	*logical = le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len) - 1;
10581988b51eSAlex Tomas 	*phys = ext_pblock(ex) + le16_to_cpu(ex->ee_len) - 1;
10591988b51eSAlex Tomas 	return 0;
10601988b51eSAlex Tomas }
10611988b51eSAlex Tomas 
10621988b51eSAlex Tomas /*
10631988b51eSAlex Tomas  * search the closest allocated block to the right for *logical
10641988b51eSAlex Tomas  * and returns it at @logical + it's physical address at @phys
10651988b51eSAlex Tomas  * if *logical is the smallest allocated block, the function
10661988b51eSAlex Tomas  * returns 0 at @phys
10671988b51eSAlex Tomas  * return value contains 0 (success) or error code
10681988b51eSAlex Tomas  */
10691988b51eSAlex Tomas int
10701988b51eSAlex Tomas ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path,
10711988b51eSAlex Tomas 			ext4_lblk_t *logical, ext4_fsblk_t *phys)
10721988b51eSAlex Tomas {
10731988b51eSAlex Tomas 	struct buffer_head *bh = NULL;
10741988b51eSAlex Tomas 	struct ext4_extent_header *eh;
10751988b51eSAlex Tomas 	struct ext4_extent_idx *ix;
10761988b51eSAlex Tomas 	struct ext4_extent *ex;
10771988b51eSAlex Tomas 	ext4_fsblk_t block;
10781988b51eSAlex Tomas 	int depth;
10791988b51eSAlex Tomas 
10801988b51eSAlex Tomas 	BUG_ON(path == NULL);
10811988b51eSAlex Tomas 	depth = path->p_depth;
10821988b51eSAlex Tomas 	*phys = 0;
10831988b51eSAlex Tomas 
10841988b51eSAlex Tomas 	if (depth == 0 && path->p_ext == NULL)
10851988b51eSAlex Tomas 		return 0;
10861988b51eSAlex Tomas 
10871988b51eSAlex Tomas 	/* usually extent in the path covers blocks smaller
10881988b51eSAlex Tomas 	 * then *logical, but it can be that extent is the
10891988b51eSAlex Tomas 	 * first one in the file */
10901988b51eSAlex Tomas 
10911988b51eSAlex Tomas 	ex = path[depth].p_ext;
10921988b51eSAlex Tomas 	if (*logical < le32_to_cpu(ex->ee_block)) {
10931988b51eSAlex Tomas 		BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
10941988b51eSAlex Tomas 		while (--depth >= 0) {
10951988b51eSAlex Tomas 			ix = path[depth].p_idx;
10961988b51eSAlex Tomas 			BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
10971988b51eSAlex Tomas 		}
10981988b51eSAlex Tomas 		*logical = le32_to_cpu(ex->ee_block);
10991988b51eSAlex Tomas 		*phys = ext_pblock(ex);
11001988b51eSAlex Tomas 		return 0;
11011988b51eSAlex Tomas 	}
11021988b51eSAlex Tomas 
11031988b51eSAlex Tomas 	BUG_ON(*logical < le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len));
11041988b51eSAlex Tomas 
11051988b51eSAlex Tomas 	if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
11061988b51eSAlex Tomas 		/* next allocated block in this leaf */
11071988b51eSAlex Tomas 		ex++;
11081988b51eSAlex Tomas 		*logical = le32_to_cpu(ex->ee_block);
11091988b51eSAlex Tomas 		*phys = ext_pblock(ex);
11101988b51eSAlex Tomas 		return 0;
11111988b51eSAlex Tomas 	}
11121988b51eSAlex Tomas 
11131988b51eSAlex Tomas 	/* go up and search for index to the right */
11141988b51eSAlex Tomas 	while (--depth >= 0) {
11151988b51eSAlex Tomas 		ix = path[depth].p_idx;
11161988b51eSAlex Tomas 		if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
11171988b51eSAlex Tomas 			break;
11181988b51eSAlex Tomas 	}
11191988b51eSAlex Tomas 
11201988b51eSAlex Tomas 	if (depth < 0) {
11211988b51eSAlex Tomas 		/* we've gone up to the root and
11221988b51eSAlex Tomas 		 * found no index to the right */
11231988b51eSAlex Tomas 		return 0;
11241988b51eSAlex Tomas 	}
11251988b51eSAlex Tomas 
11261988b51eSAlex Tomas 	/* we've found index to the right, let's
11271988b51eSAlex Tomas 	 * follow it and find the closest allocated
11281988b51eSAlex Tomas 	 * block to the right */
11291988b51eSAlex Tomas 	ix++;
11301988b51eSAlex Tomas 	block = idx_pblock(ix);
11311988b51eSAlex Tomas 	while (++depth < path->p_depth) {
11321988b51eSAlex Tomas 		bh = sb_bread(inode->i_sb, block);
11331988b51eSAlex Tomas 		if (bh == NULL)
11341988b51eSAlex Tomas 			return -EIO;
11351988b51eSAlex Tomas 		eh = ext_block_hdr(bh);
11361988b51eSAlex Tomas 		if (ext4_ext_check_header(inode, eh, depth)) {
11371988b51eSAlex Tomas 			put_bh(bh);
11381988b51eSAlex Tomas 			return -EIO;
11391988b51eSAlex Tomas 		}
11401988b51eSAlex Tomas 		ix = EXT_FIRST_INDEX(eh);
11411988b51eSAlex Tomas 		block = idx_pblock(ix);
11421988b51eSAlex Tomas 		put_bh(bh);
11431988b51eSAlex Tomas 	}
11441988b51eSAlex Tomas 
11451988b51eSAlex Tomas 	bh = sb_bread(inode->i_sb, block);
11461988b51eSAlex Tomas 	if (bh == NULL)
11471988b51eSAlex Tomas 		return -EIO;
11481988b51eSAlex Tomas 	eh = ext_block_hdr(bh);
11491988b51eSAlex Tomas 	if (ext4_ext_check_header(inode, eh, path->p_depth - depth)) {
11501988b51eSAlex Tomas 		put_bh(bh);
11511988b51eSAlex Tomas 		return -EIO;
11521988b51eSAlex Tomas 	}
11531988b51eSAlex Tomas 	ex = EXT_FIRST_EXTENT(eh);
11541988b51eSAlex Tomas 	*logical = le32_to_cpu(ex->ee_block);
11551988b51eSAlex Tomas 	*phys = ext_pblock(ex);
11561988b51eSAlex Tomas 	put_bh(bh);
11571988b51eSAlex Tomas 	return 0;
11581988b51eSAlex Tomas 
11591988b51eSAlex Tomas }
11601988b51eSAlex Tomas 
11611988b51eSAlex Tomas /*
1162d0d856e8SRandy Dunlap  * ext4_ext_next_allocated_block:
1163d0d856e8SRandy Dunlap  * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1164d0d856e8SRandy Dunlap  * NOTE: it considers block number from index entry as
1165d0d856e8SRandy Dunlap  * allocated block. Thus, index entries have to be consistent
1166d0d856e8SRandy Dunlap  * with leaves.
1167a86c6181SAlex Tomas  */
1168725d26d3SAneesh Kumar K.V static ext4_lblk_t
1169a86c6181SAlex Tomas ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1170a86c6181SAlex Tomas {
1171a86c6181SAlex Tomas 	int depth;
1172a86c6181SAlex Tomas 
1173a86c6181SAlex Tomas 	BUG_ON(path == NULL);
1174a86c6181SAlex Tomas 	depth = path->p_depth;
1175a86c6181SAlex Tomas 
1176a86c6181SAlex Tomas 	if (depth == 0 && path->p_ext == NULL)
1177a86c6181SAlex Tomas 		return EXT_MAX_BLOCK;
1178a86c6181SAlex Tomas 
1179a86c6181SAlex Tomas 	while (depth >= 0) {
1180a86c6181SAlex Tomas 		if (depth == path->p_depth) {
1181a86c6181SAlex Tomas 			/* leaf */
1182a86c6181SAlex Tomas 			if (path[depth].p_ext !=
1183a86c6181SAlex Tomas 					EXT_LAST_EXTENT(path[depth].p_hdr))
1184a86c6181SAlex Tomas 			  return le32_to_cpu(path[depth].p_ext[1].ee_block);
1185a86c6181SAlex Tomas 		} else {
1186a86c6181SAlex Tomas 			/* index */
1187a86c6181SAlex Tomas 			if (path[depth].p_idx !=
1188a86c6181SAlex Tomas 					EXT_LAST_INDEX(path[depth].p_hdr))
1189a86c6181SAlex Tomas 			  return le32_to_cpu(path[depth].p_idx[1].ei_block);
1190a86c6181SAlex Tomas 		}
1191a86c6181SAlex Tomas 		depth--;
1192a86c6181SAlex Tomas 	}
1193a86c6181SAlex Tomas 
1194a86c6181SAlex Tomas 	return EXT_MAX_BLOCK;
1195a86c6181SAlex Tomas }
1196a86c6181SAlex Tomas 
1197a86c6181SAlex Tomas /*
1198d0d856e8SRandy Dunlap  * ext4_ext_next_leaf_block:
1199a86c6181SAlex Tomas  * returns first allocated block from next leaf or EXT_MAX_BLOCK
1200a86c6181SAlex Tomas  */
1201725d26d3SAneesh Kumar K.V static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode,
1202a86c6181SAlex Tomas 					struct ext4_ext_path *path)
1203a86c6181SAlex Tomas {
1204a86c6181SAlex Tomas 	int depth;
1205a86c6181SAlex Tomas 
1206a86c6181SAlex Tomas 	BUG_ON(path == NULL);
1207a86c6181SAlex Tomas 	depth = path->p_depth;
1208a86c6181SAlex Tomas 
1209a86c6181SAlex Tomas 	/* zero-tree has no leaf blocks at all */
1210a86c6181SAlex Tomas 	if (depth == 0)
1211a86c6181SAlex Tomas 		return EXT_MAX_BLOCK;
1212a86c6181SAlex Tomas 
1213a86c6181SAlex Tomas 	/* go to index block */
1214a86c6181SAlex Tomas 	depth--;
1215a86c6181SAlex Tomas 
1216a86c6181SAlex Tomas 	while (depth >= 0) {
1217a86c6181SAlex Tomas 		if (path[depth].p_idx !=
1218a86c6181SAlex Tomas 				EXT_LAST_INDEX(path[depth].p_hdr))
1219725d26d3SAneesh Kumar K.V 			return (ext4_lblk_t)
1220725d26d3SAneesh Kumar K.V 				le32_to_cpu(path[depth].p_idx[1].ei_block);
1221a86c6181SAlex Tomas 		depth--;
1222a86c6181SAlex Tomas 	}
1223a86c6181SAlex Tomas 
1224a86c6181SAlex Tomas 	return EXT_MAX_BLOCK;
1225a86c6181SAlex Tomas }
1226a86c6181SAlex Tomas 
1227a86c6181SAlex Tomas /*
1228d0d856e8SRandy Dunlap  * ext4_ext_correct_indexes:
1229d0d856e8SRandy Dunlap  * if leaf gets modified and modified extent is first in the leaf,
1230d0d856e8SRandy Dunlap  * then we have to correct all indexes above.
1231a86c6181SAlex Tomas  * TODO: do we need to correct tree in all cases?
1232a86c6181SAlex Tomas  */
12331d03ec98SAneesh Kumar K.V static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
1234a86c6181SAlex Tomas 				struct ext4_ext_path *path)
1235a86c6181SAlex Tomas {
1236a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
1237a86c6181SAlex Tomas 	int depth = ext_depth(inode);
1238a86c6181SAlex Tomas 	struct ext4_extent *ex;
1239a86c6181SAlex Tomas 	__le32 border;
1240a86c6181SAlex Tomas 	int k, err = 0;
1241a86c6181SAlex Tomas 
1242a86c6181SAlex Tomas 	eh = path[depth].p_hdr;
1243a86c6181SAlex Tomas 	ex = path[depth].p_ext;
1244a86c6181SAlex Tomas 	BUG_ON(ex == NULL);
1245a86c6181SAlex Tomas 	BUG_ON(eh == NULL);
1246a86c6181SAlex Tomas 
1247a86c6181SAlex Tomas 	if (depth == 0) {
1248a86c6181SAlex Tomas 		/* there is no tree at all */
1249a86c6181SAlex Tomas 		return 0;
1250a86c6181SAlex Tomas 	}
1251a86c6181SAlex Tomas 
1252a86c6181SAlex Tomas 	if (ex != EXT_FIRST_EXTENT(eh)) {
1253a86c6181SAlex Tomas 		/* we correct tree if first leaf got modified only */
1254a86c6181SAlex Tomas 		return 0;
1255a86c6181SAlex Tomas 	}
1256a86c6181SAlex Tomas 
1257a86c6181SAlex Tomas 	/*
1258d0d856e8SRandy Dunlap 	 * TODO: we need correction if border is smaller than current one
1259a86c6181SAlex Tomas 	 */
1260a86c6181SAlex Tomas 	k = depth - 1;
1261a86c6181SAlex Tomas 	border = path[depth].p_ext->ee_block;
12627e028976SAvantika Mathur 	err = ext4_ext_get_access(handle, inode, path + k);
12637e028976SAvantika Mathur 	if (err)
1264a86c6181SAlex Tomas 		return err;
1265a86c6181SAlex Tomas 	path[k].p_idx->ei_block = border;
12667e028976SAvantika Mathur 	err = ext4_ext_dirty(handle, inode, path + k);
12677e028976SAvantika Mathur 	if (err)
1268a86c6181SAlex Tomas 		return err;
1269a86c6181SAlex Tomas 
1270a86c6181SAlex Tomas 	while (k--) {
1271a86c6181SAlex Tomas 		/* change all left-side indexes */
1272a86c6181SAlex Tomas 		if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1273a86c6181SAlex Tomas 			break;
12747e028976SAvantika Mathur 		err = ext4_ext_get_access(handle, inode, path + k);
12757e028976SAvantika Mathur 		if (err)
1276a86c6181SAlex Tomas 			break;
1277a86c6181SAlex Tomas 		path[k].p_idx->ei_block = border;
12787e028976SAvantika Mathur 		err = ext4_ext_dirty(handle, inode, path + k);
12797e028976SAvantika Mathur 		if (err)
1280a86c6181SAlex Tomas 			break;
1281a86c6181SAlex Tomas 	}
1282a86c6181SAlex Tomas 
1283a86c6181SAlex Tomas 	return err;
1284a86c6181SAlex Tomas }
1285a86c6181SAlex Tomas 
128609b88252SAvantika Mathur static int
1287a86c6181SAlex Tomas ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1288a86c6181SAlex Tomas 				struct ext4_extent *ex2)
1289a86c6181SAlex Tomas {
1290749269faSAmit Arora 	unsigned short ext1_ee_len, ext2_ee_len, max_len;
1291a2df2a63SAmit Arora 
1292a2df2a63SAmit Arora 	/*
1293a2df2a63SAmit Arora 	 * Make sure that either both extents are uninitialized, or
1294a2df2a63SAmit Arora 	 * both are _not_.
1295a2df2a63SAmit Arora 	 */
1296a2df2a63SAmit Arora 	if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1297a2df2a63SAmit Arora 		return 0;
1298a2df2a63SAmit Arora 
1299749269faSAmit Arora 	if (ext4_ext_is_uninitialized(ex1))
1300749269faSAmit Arora 		max_len = EXT_UNINIT_MAX_LEN;
1301749269faSAmit Arora 	else
1302749269faSAmit Arora 		max_len = EXT_INIT_MAX_LEN;
1303749269faSAmit Arora 
1304a2df2a63SAmit Arora 	ext1_ee_len = ext4_ext_get_actual_len(ex1);
1305a2df2a63SAmit Arora 	ext2_ee_len = ext4_ext_get_actual_len(ex2);
1306a2df2a63SAmit Arora 
1307a2df2a63SAmit Arora 	if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
130863f57933SAndrew Morton 			le32_to_cpu(ex2->ee_block))
1309a86c6181SAlex Tomas 		return 0;
1310a86c6181SAlex Tomas 
1311471d4011SSuparna Bhattacharya 	/*
1312471d4011SSuparna Bhattacharya 	 * To allow future support for preallocated extents to be added
1313471d4011SSuparna Bhattacharya 	 * as an RO_COMPAT feature, refuse to merge to extents if
1314d0d856e8SRandy Dunlap 	 * this can result in the top bit of ee_len being set.
1315471d4011SSuparna Bhattacharya 	 */
1316749269faSAmit Arora 	if (ext1_ee_len + ext2_ee_len > max_len)
1317471d4011SSuparna Bhattacharya 		return 0;
1318bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST
1319a86c6181SAlex Tomas 	if (le16_to_cpu(ex1->ee_len) >= 4)
1320a86c6181SAlex Tomas 		return 0;
1321a86c6181SAlex Tomas #endif
1322a86c6181SAlex Tomas 
1323a2df2a63SAmit Arora 	if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2))
1324a86c6181SAlex Tomas 		return 1;
1325a86c6181SAlex Tomas 	return 0;
1326a86c6181SAlex Tomas }
1327a86c6181SAlex Tomas 
1328a86c6181SAlex Tomas /*
132956055d3aSAmit Arora  * This function tries to merge the "ex" extent to the next extent in the tree.
133056055d3aSAmit Arora  * It always tries to merge towards right. If you want to merge towards
133156055d3aSAmit Arora  * left, pass "ex - 1" as argument instead of "ex".
133256055d3aSAmit Arora  * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
133356055d3aSAmit Arora  * 1 if they got merged.
133456055d3aSAmit Arora  */
133556055d3aSAmit Arora int ext4_ext_try_to_merge(struct inode *inode,
133656055d3aSAmit Arora 			  struct ext4_ext_path *path,
133756055d3aSAmit Arora 			  struct ext4_extent *ex)
133856055d3aSAmit Arora {
133956055d3aSAmit Arora 	struct ext4_extent_header *eh;
134056055d3aSAmit Arora 	unsigned int depth, len;
134156055d3aSAmit Arora 	int merge_done = 0;
134256055d3aSAmit Arora 	int uninitialized = 0;
134356055d3aSAmit Arora 
134456055d3aSAmit Arora 	depth = ext_depth(inode);
134556055d3aSAmit Arora 	BUG_ON(path[depth].p_hdr == NULL);
134656055d3aSAmit Arora 	eh = path[depth].p_hdr;
134756055d3aSAmit Arora 
134856055d3aSAmit Arora 	while (ex < EXT_LAST_EXTENT(eh)) {
134956055d3aSAmit Arora 		if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
135056055d3aSAmit Arora 			break;
135156055d3aSAmit Arora 		/* merge with next extent! */
135256055d3aSAmit Arora 		if (ext4_ext_is_uninitialized(ex))
135356055d3aSAmit Arora 			uninitialized = 1;
135456055d3aSAmit Arora 		ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
135556055d3aSAmit Arora 				+ ext4_ext_get_actual_len(ex + 1));
135656055d3aSAmit Arora 		if (uninitialized)
135756055d3aSAmit Arora 			ext4_ext_mark_uninitialized(ex);
135856055d3aSAmit Arora 
135956055d3aSAmit Arora 		if (ex + 1 < EXT_LAST_EXTENT(eh)) {
136056055d3aSAmit Arora 			len = (EXT_LAST_EXTENT(eh) - ex - 1)
136156055d3aSAmit Arora 				* sizeof(struct ext4_extent);
136256055d3aSAmit Arora 			memmove(ex + 1, ex + 2, len);
136356055d3aSAmit Arora 		}
136456055d3aSAmit Arora 		eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries) - 1);
136556055d3aSAmit Arora 		merge_done = 1;
136656055d3aSAmit Arora 		WARN_ON(eh->eh_entries == 0);
136756055d3aSAmit Arora 		if (!eh->eh_entries)
136856055d3aSAmit Arora 			ext4_error(inode->i_sb, "ext4_ext_try_to_merge",
136956055d3aSAmit Arora 			   "inode#%lu, eh->eh_entries = 0!", inode->i_ino);
137056055d3aSAmit Arora 	}
137156055d3aSAmit Arora 
137256055d3aSAmit Arora 	return merge_done;
137356055d3aSAmit Arora }
137456055d3aSAmit Arora 
137556055d3aSAmit Arora /*
137625d14f98SAmit Arora  * check if a portion of the "newext" extent overlaps with an
137725d14f98SAmit Arora  * existing extent.
137825d14f98SAmit Arora  *
137925d14f98SAmit Arora  * If there is an overlap discovered, it updates the length of the newext
138025d14f98SAmit Arora  * such that there will be no overlap, and then returns 1.
138125d14f98SAmit Arora  * If there is no overlap found, it returns 0.
138225d14f98SAmit Arora  */
138325d14f98SAmit Arora unsigned int ext4_ext_check_overlap(struct inode *inode,
138425d14f98SAmit Arora 				    struct ext4_extent *newext,
138525d14f98SAmit Arora 				    struct ext4_ext_path *path)
138625d14f98SAmit Arora {
1387725d26d3SAneesh Kumar K.V 	ext4_lblk_t b1, b2;
138825d14f98SAmit Arora 	unsigned int depth, len1;
138925d14f98SAmit Arora 	unsigned int ret = 0;
139025d14f98SAmit Arora 
139125d14f98SAmit Arora 	b1 = le32_to_cpu(newext->ee_block);
1392a2df2a63SAmit Arora 	len1 = ext4_ext_get_actual_len(newext);
139325d14f98SAmit Arora 	depth = ext_depth(inode);
139425d14f98SAmit Arora 	if (!path[depth].p_ext)
139525d14f98SAmit Arora 		goto out;
139625d14f98SAmit Arora 	b2 = le32_to_cpu(path[depth].p_ext->ee_block);
139725d14f98SAmit Arora 
139825d14f98SAmit Arora 	/*
139925d14f98SAmit Arora 	 * get the next allocated block if the extent in the path
140025d14f98SAmit Arora 	 * is before the requested block(s)
140125d14f98SAmit Arora 	 */
140225d14f98SAmit Arora 	if (b2 < b1) {
140325d14f98SAmit Arora 		b2 = ext4_ext_next_allocated_block(path);
140425d14f98SAmit Arora 		if (b2 == EXT_MAX_BLOCK)
140525d14f98SAmit Arora 			goto out;
140625d14f98SAmit Arora 	}
140725d14f98SAmit Arora 
1408725d26d3SAneesh Kumar K.V 	/* check for wrap through zero on extent logical start block*/
140925d14f98SAmit Arora 	if (b1 + len1 < b1) {
141025d14f98SAmit Arora 		len1 = EXT_MAX_BLOCK - b1;
141125d14f98SAmit Arora 		newext->ee_len = cpu_to_le16(len1);
141225d14f98SAmit Arora 		ret = 1;
141325d14f98SAmit Arora 	}
141425d14f98SAmit Arora 
141525d14f98SAmit Arora 	/* check for overlap */
141625d14f98SAmit Arora 	if (b1 + len1 > b2) {
141725d14f98SAmit Arora 		newext->ee_len = cpu_to_le16(b2 - b1);
141825d14f98SAmit Arora 		ret = 1;
141925d14f98SAmit Arora 	}
142025d14f98SAmit Arora out:
142125d14f98SAmit Arora 	return ret;
142225d14f98SAmit Arora }
142325d14f98SAmit Arora 
142425d14f98SAmit Arora /*
1425d0d856e8SRandy Dunlap  * ext4_ext_insert_extent:
1426d0d856e8SRandy Dunlap  * tries to merge requsted extent into the existing extent or
1427d0d856e8SRandy Dunlap  * inserts requested extent as new one into the tree,
1428d0d856e8SRandy Dunlap  * creating new leaf in the no-space case.
1429a86c6181SAlex Tomas  */
1430a86c6181SAlex Tomas int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1431a86c6181SAlex Tomas 				struct ext4_ext_path *path,
1432a86c6181SAlex Tomas 				struct ext4_extent *newext)
1433a86c6181SAlex Tomas {
1434a86c6181SAlex Tomas 	struct ext4_extent_header * eh;
1435a86c6181SAlex Tomas 	struct ext4_extent *ex, *fex;
1436a86c6181SAlex Tomas 	struct ext4_extent *nearex; /* nearest extent */
1437a86c6181SAlex Tomas 	struct ext4_ext_path *npath = NULL;
1438725d26d3SAneesh Kumar K.V 	int depth, len, err;
1439725d26d3SAneesh Kumar K.V 	ext4_lblk_t next;
1440a2df2a63SAmit Arora 	unsigned uninitialized = 0;
1441a86c6181SAlex Tomas 
1442a2df2a63SAmit Arora 	BUG_ON(ext4_ext_get_actual_len(newext) == 0);
1443a86c6181SAlex Tomas 	depth = ext_depth(inode);
1444a86c6181SAlex Tomas 	ex = path[depth].p_ext;
1445a86c6181SAlex Tomas 	BUG_ON(path[depth].p_hdr == NULL);
1446a86c6181SAlex Tomas 
1447a86c6181SAlex Tomas 	/* try to insert block into found extent and return */
1448a86c6181SAlex Tomas 	if (ex && ext4_can_extents_be_merged(inode, ex, newext)) {
14492ae02107SMingming Cao 		ext_debug("append %d block to %d:%d (from %llu)\n",
1450a2df2a63SAmit Arora 				ext4_ext_get_actual_len(newext),
1451a86c6181SAlex Tomas 				le32_to_cpu(ex->ee_block),
1452a2df2a63SAmit Arora 				ext4_ext_get_actual_len(ex), ext_pblock(ex));
14537e028976SAvantika Mathur 		err = ext4_ext_get_access(handle, inode, path + depth);
14547e028976SAvantika Mathur 		if (err)
1455a86c6181SAlex Tomas 			return err;
1456a2df2a63SAmit Arora 
1457a2df2a63SAmit Arora 		/*
1458a2df2a63SAmit Arora 		 * ext4_can_extents_be_merged should have checked that either
1459a2df2a63SAmit Arora 		 * both extents are uninitialized, or both aren't. Thus we
1460a2df2a63SAmit Arora 		 * need to check only one of them here.
1461a2df2a63SAmit Arora 		 */
1462a2df2a63SAmit Arora 		if (ext4_ext_is_uninitialized(ex))
1463a2df2a63SAmit Arora 			uninitialized = 1;
1464a2df2a63SAmit Arora 		ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1465a2df2a63SAmit Arora 					+ ext4_ext_get_actual_len(newext));
1466a2df2a63SAmit Arora 		if (uninitialized)
1467a2df2a63SAmit Arora 			ext4_ext_mark_uninitialized(ex);
1468a86c6181SAlex Tomas 		eh = path[depth].p_hdr;
1469a86c6181SAlex Tomas 		nearex = ex;
1470a86c6181SAlex Tomas 		goto merge;
1471a86c6181SAlex Tomas 	}
1472a86c6181SAlex Tomas 
1473a86c6181SAlex Tomas repeat:
1474a86c6181SAlex Tomas 	depth = ext_depth(inode);
1475a86c6181SAlex Tomas 	eh = path[depth].p_hdr;
1476a86c6181SAlex Tomas 	if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1477a86c6181SAlex Tomas 		goto has_space;
1478a86c6181SAlex Tomas 
1479a86c6181SAlex Tomas 	/* probably next leaf has space for us? */
1480a86c6181SAlex Tomas 	fex = EXT_LAST_EXTENT(eh);
1481a86c6181SAlex Tomas 	next = ext4_ext_next_leaf_block(inode, path);
1482a86c6181SAlex Tomas 	if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
1483a86c6181SAlex Tomas 	    && next != EXT_MAX_BLOCK) {
1484a86c6181SAlex Tomas 		ext_debug("next leaf block - %d\n", next);
1485a86c6181SAlex Tomas 		BUG_ON(npath != NULL);
1486a86c6181SAlex Tomas 		npath = ext4_ext_find_extent(inode, next, NULL);
1487a86c6181SAlex Tomas 		if (IS_ERR(npath))
1488a86c6181SAlex Tomas 			return PTR_ERR(npath);
1489a86c6181SAlex Tomas 		BUG_ON(npath->p_depth != path->p_depth);
1490a86c6181SAlex Tomas 		eh = npath[depth].p_hdr;
1491a86c6181SAlex Tomas 		if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
1492a86c6181SAlex Tomas 			ext_debug("next leaf isnt full(%d)\n",
1493a86c6181SAlex Tomas 				  le16_to_cpu(eh->eh_entries));
1494a86c6181SAlex Tomas 			path = npath;
1495a86c6181SAlex Tomas 			goto repeat;
1496a86c6181SAlex Tomas 		}
1497a86c6181SAlex Tomas 		ext_debug("next leaf has no free space(%d,%d)\n",
1498a86c6181SAlex Tomas 			  le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1499a86c6181SAlex Tomas 	}
1500a86c6181SAlex Tomas 
1501a86c6181SAlex Tomas 	/*
1502d0d856e8SRandy Dunlap 	 * There is no free space in the found leaf.
1503d0d856e8SRandy Dunlap 	 * We're gonna add a new leaf in the tree.
1504a86c6181SAlex Tomas 	 */
1505a86c6181SAlex Tomas 	err = ext4_ext_create_new_leaf(handle, inode, path, newext);
1506a86c6181SAlex Tomas 	if (err)
1507a86c6181SAlex Tomas 		goto cleanup;
1508a86c6181SAlex Tomas 	depth = ext_depth(inode);
1509a86c6181SAlex Tomas 	eh = path[depth].p_hdr;
1510a86c6181SAlex Tomas 
1511a86c6181SAlex Tomas has_space:
1512a86c6181SAlex Tomas 	nearex = path[depth].p_ext;
1513a86c6181SAlex Tomas 
15147e028976SAvantika Mathur 	err = ext4_ext_get_access(handle, inode, path + depth);
15157e028976SAvantika Mathur 	if (err)
1516a86c6181SAlex Tomas 		goto cleanup;
1517a86c6181SAlex Tomas 
1518a86c6181SAlex Tomas 	if (!nearex) {
1519a86c6181SAlex Tomas 		/* there is no extent in this leaf, create first one */
15202ae02107SMingming Cao 		ext_debug("first extent in the leaf: %d:%llu:%d\n",
1521a86c6181SAlex Tomas 				le32_to_cpu(newext->ee_block),
1522f65e6fbaSAlex Tomas 				ext_pblock(newext),
1523a2df2a63SAmit Arora 				ext4_ext_get_actual_len(newext));
1524a86c6181SAlex Tomas 		path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1525a86c6181SAlex Tomas 	} else if (le32_to_cpu(newext->ee_block)
1526a86c6181SAlex Tomas 			   > le32_to_cpu(nearex->ee_block)) {
1527a86c6181SAlex Tomas /*		BUG_ON(newext->ee_block == nearex->ee_block); */
1528a86c6181SAlex Tomas 		if (nearex != EXT_LAST_EXTENT(eh)) {
1529a86c6181SAlex Tomas 			len = EXT_MAX_EXTENT(eh) - nearex;
1530a86c6181SAlex Tomas 			len = (len - 1) * sizeof(struct ext4_extent);
1531a86c6181SAlex Tomas 			len = len < 0 ? 0 : len;
15322ae02107SMingming Cao 			ext_debug("insert %d:%llu:%d after: nearest 0x%p, "
1533a86c6181SAlex Tomas 					"move %d from 0x%p to 0x%p\n",
1534a86c6181SAlex Tomas 					le32_to_cpu(newext->ee_block),
1535f65e6fbaSAlex Tomas 					ext_pblock(newext),
1536a2df2a63SAmit Arora 					ext4_ext_get_actual_len(newext),
1537a86c6181SAlex Tomas 					nearex, len, nearex + 1, nearex + 2);
1538a86c6181SAlex Tomas 			memmove(nearex + 2, nearex + 1, len);
1539a86c6181SAlex Tomas 		}
1540a86c6181SAlex Tomas 		path[depth].p_ext = nearex + 1;
1541a86c6181SAlex Tomas 	} else {
1542a86c6181SAlex Tomas 		BUG_ON(newext->ee_block == nearex->ee_block);
1543a86c6181SAlex Tomas 		len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1544a86c6181SAlex Tomas 		len = len < 0 ? 0 : len;
15452ae02107SMingming Cao 		ext_debug("insert %d:%llu:%d before: nearest 0x%p, "
1546a86c6181SAlex Tomas 				"move %d from 0x%p to 0x%p\n",
1547a86c6181SAlex Tomas 				le32_to_cpu(newext->ee_block),
1548f65e6fbaSAlex Tomas 				ext_pblock(newext),
1549a2df2a63SAmit Arora 				ext4_ext_get_actual_len(newext),
1550a86c6181SAlex Tomas 				nearex, len, nearex + 1, nearex + 2);
1551a86c6181SAlex Tomas 		memmove(nearex + 1, nearex, len);
1552a86c6181SAlex Tomas 		path[depth].p_ext = nearex;
1553a86c6181SAlex Tomas 	}
1554a86c6181SAlex Tomas 
1555a86c6181SAlex Tomas 	eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)+1);
1556a86c6181SAlex Tomas 	nearex = path[depth].p_ext;
1557a86c6181SAlex Tomas 	nearex->ee_block = newext->ee_block;
1558b377611dSAneesh Kumar K.V 	ext4_ext_store_pblock(nearex, ext_pblock(newext));
1559a86c6181SAlex Tomas 	nearex->ee_len = newext->ee_len;
1560a86c6181SAlex Tomas 
1561a86c6181SAlex Tomas merge:
1562a86c6181SAlex Tomas 	/* try to merge extents to the right */
156356055d3aSAmit Arora 	ext4_ext_try_to_merge(inode, path, nearex);
1564a86c6181SAlex Tomas 
1565a86c6181SAlex Tomas 	/* try to merge extents to the left */
1566a86c6181SAlex Tomas 
1567a86c6181SAlex Tomas 	/* time to correct all indexes above */
1568a86c6181SAlex Tomas 	err = ext4_ext_correct_indexes(handle, inode, path);
1569a86c6181SAlex Tomas 	if (err)
1570a86c6181SAlex Tomas 		goto cleanup;
1571a86c6181SAlex Tomas 
1572a86c6181SAlex Tomas 	err = ext4_ext_dirty(handle, inode, path + depth);
1573a86c6181SAlex Tomas 
1574a86c6181SAlex Tomas cleanup:
1575a86c6181SAlex Tomas 	if (npath) {
1576a86c6181SAlex Tomas 		ext4_ext_drop_refs(npath);
1577a86c6181SAlex Tomas 		kfree(npath);
1578a86c6181SAlex Tomas 	}
1579a86c6181SAlex Tomas 	ext4_ext_tree_changed(inode);
1580a86c6181SAlex Tomas 	ext4_ext_invalidate_cache(inode);
1581a86c6181SAlex Tomas 	return err;
1582a86c6181SAlex Tomas }
1583a86c6181SAlex Tomas 
158409b88252SAvantika Mathur static void
1585725d26d3SAneesh Kumar K.V ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
1586dd54567aSMingming Cao 			__u32 len, ext4_fsblk_t start, int type)
1587a86c6181SAlex Tomas {
1588a86c6181SAlex Tomas 	struct ext4_ext_cache *cex;
1589a86c6181SAlex Tomas 	BUG_ON(len == 0);
1590a86c6181SAlex Tomas 	cex = &EXT4_I(inode)->i_cached_extent;
1591a86c6181SAlex Tomas 	cex->ec_type = type;
1592a86c6181SAlex Tomas 	cex->ec_block = block;
1593a86c6181SAlex Tomas 	cex->ec_len = len;
1594a86c6181SAlex Tomas 	cex->ec_start = start;
1595a86c6181SAlex Tomas }
1596a86c6181SAlex Tomas 
1597a86c6181SAlex Tomas /*
1598d0d856e8SRandy Dunlap  * ext4_ext_put_gap_in_cache:
1599d0d856e8SRandy Dunlap  * calculate boundaries of the gap that the requested block fits into
1600a86c6181SAlex Tomas  * and cache this gap
1601a86c6181SAlex Tomas  */
160209b88252SAvantika Mathur static void
1603a86c6181SAlex Tomas ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
1604725d26d3SAneesh Kumar K.V 				ext4_lblk_t block)
1605a86c6181SAlex Tomas {
1606a86c6181SAlex Tomas 	int depth = ext_depth(inode);
1607725d26d3SAneesh Kumar K.V 	unsigned long len;
1608725d26d3SAneesh Kumar K.V 	ext4_lblk_t lblock;
1609a86c6181SAlex Tomas 	struct ext4_extent *ex;
1610a86c6181SAlex Tomas 
1611a86c6181SAlex Tomas 	ex = path[depth].p_ext;
1612a86c6181SAlex Tomas 	if (ex == NULL) {
1613a86c6181SAlex Tomas 		/* there is no extent yet, so gap is [0;-] */
1614a86c6181SAlex Tomas 		lblock = 0;
1615a86c6181SAlex Tomas 		len = EXT_MAX_BLOCK;
1616a86c6181SAlex Tomas 		ext_debug("cache gap(whole file):");
1617a86c6181SAlex Tomas 	} else if (block < le32_to_cpu(ex->ee_block)) {
1618a86c6181SAlex Tomas 		lblock = block;
1619a86c6181SAlex Tomas 		len = le32_to_cpu(ex->ee_block) - block;
1620bba90743SEric Sandeen 		ext_debug("cache gap(before): %u [%u:%u]",
1621bba90743SEric Sandeen 				block,
1622bba90743SEric Sandeen 				le32_to_cpu(ex->ee_block),
1623bba90743SEric Sandeen 				 ext4_ext_get_actual_len(ex));
1624a86c6181SAlex Tomas 	} else if (block >= le32_to_cpu(ex->ee_block)
1625a2df2a63SAmit Arora 			+ ext4_ext_get_actual_len(ex)) {
1626725d26d3SAneesh Kumar K.V 		ext4_lblk_t next;
1627a86c6181SAlex Tomas 		lblock = le32_to_cpu(ex->ee_block)
1628a2df2a63SAmit Arora 			+ ext4_ext_get_actual_len(ex);
1629725d26d3SAneesh Kumar K.V 
1630725d26d3SAneesh Kumar K.V 		next = ext4_ext_next_allocated_block(path);
1631bba90743SEric Sandeen 		ext_debug("cache gap(after): [%u:%u] %u",
1632bba90743SEric Sandeen 				le32_to_cpu(ex->ee_block),
1633bba90743SEric Sandeen 				ext4_ext_get_actual_len(ex),
1634bba90743SEric Sandeen 				block);
1635725d26d3SAneesh Kumar K.V 		BUG_ON(next == lblock);
1636725d26d3SAneesh Kumar K.V 		len = next - lblock;
1637a86c6181SAlex Tomas 	} else {
1638a86c6181SAlex Tomas 		lblock = len = 0;
1639a86c6181SAlex Tomas 		BUG();
1640a86c6181SAlex Tomas 	}
1641a86c6181SAlex Tomas 
1642bba90743SEric Sandeen 	ext_debug(" -> %u:%lu\n", lblock, len);
1643a86c6181SAlex Tomas 	ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
1644a86c6181SAlex Tomas }
1645a86c6181SAlex Tomas 
164609b88252SAvantika Mathur static int
1647725d26d3SAneesh Kumar K.V ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
1648a86c6181SAlex Tomas 			struct ext4_extent *ex)
1649a86c6181SAlex Tomas {
1650a86c6181SAlex Tomas 	struct ext4_ext_cache *cex;
1651a86c6181SAlex Tomas 
1652a86c6181SAlex Tomas 	cex = &EXT4_I(inode)->i_cached_extent;
1653a86c6181SAlex Tomas 
1654a86c6181SAlex Tomas 	/* has cache valid data? */
1655a86c6181SAlex Tomas 	if (cex->ec_type == EXT4_EXT_CACHE_NO)
1656a86c6181SAlex Tomas 		return EXT4_EXT_CACHE_NO;
1657a86c6181SAlex Tomas 
1658a86c6181SAlex Tomas 	BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
1659a86c6181SAlex Tomas 			cex->ec_type != EXT4_EXT_CACHE_EXTENT);
1660a86c6181SAlex Tomas 	if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
1661a86c6181SAlex Tomas 		ex->ee_block = cpu_to_le32(cex->ec_block);
1662f65e6fbaSAlex Tomas 		ext4_ext_store_pblock(ex, cex->ec_start);
1663a86c6181SAlex Tomas 		ex->ee_len = cpu_to_le16(cex->ec_len);
1664bba90743SEric Sandeen 		ext_debug("%u cached by %u:%u:%llu\n",
1665bba90743SEric Sandeen 				block,
1666bba90743SEric Sandeen 				cex->ec_block, cex->ec_len, cex->ec_start);
1667a86c6181SAlex Tomas 		return cex->ec_type;
1668a86c6181SAlex Tomas 	}
1669a86c6181SAlex Tomas 
1670a86c6181SAlex Tomas 	/* not in cache */
1671a86c6181SAlex Tomas 	return EXT4_EXT_CACHE_NO;
1672a86c6181SAlex Tomas }
1673a86c6181SAlex Tomas 
1674a86c6181SAlex Tomas /*
1675d0d856e8SRandy Dunlap  * ext4_ext_rm_idx:
1676d0d856e8SRandy Dunlap  * removes index from the index block.
1677d0d856e8SRandy Dunlap  * It's used in truncate case only, thus all requests are for
1678d0d856e8SRandy Dunlap  * last index in the block only.
1679a86c6181SAlex Tomas  */
16801d03ec98SAneesh Kumar K.V static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
1681a86c6181SAlex Tomas 			struct ext4_ext_path *path)
1682a86c6181SAlex Tomas {
1683a86c6181SAlex Tomas 	struct buffer_head *bh;
1684a86c6181SAlex Tomas 	int err;
1685f65e6fbaSAlex Tomas 	ext4_fsblk_t leaf;
1686a86c6181SAlex Tomas 
1687a86c6181SAlex Tomas 	/* free index block */
1688a86c6181SAlex Tomas 	path--;
1689f65e6fbaSAlex Tomas 	leaf = idx_pblock(path->p_idx);
1690a86c6181SAlex Tomas 	BUG_ON(path->p_hdr->eh_entries == 0);
16917e028976SAvantika Mathur 	err = ext4_ext_get_access(handle, inode, path);
16927e028976SAvantika Mathur 	if (err)
1693a86c6181SAlex Tomas 		return err;
1694a86c6181SAlex Tomas 	path->p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(path->p_hdr->eh_entries)-1);
16957e028976SAvantika Mathur 	err = ext4_ext_dirty(handle, inode, path);
16967e028976SAvantika Mathur 	if (err)
1697a86c6181SAlex Tomas 		return err;
16982ae02107SMingming Cao 	ext_debug("index is empty, remove it, free block %llu\n", leaf);
1699a86c6181SAlex Tomas 	bh = sb_find_get_block(inode->i_sb, leaf);
1700a86c6181SAlex Tomas 	ext4_forget(handle, 1, inode, bh, leaf);
1701c9de560dSAlex Tomas 	ext4_free_blocks(handle, inode, leaf, 1, 1);
1702a86c6181SAlex Tomas 	return err;
1703a86c6181SAlex Tomas }
1704a86c6181SAlex Tomas 
1705a86c6181SAlex Tomas /*
1706d0d856e8SRandy Dunlap  * ext4_ext_calc_credits_for_insert:
1707d0d856e8SRandy Dunlap  * This routine returns max. credits that the extent tree can consume.
1708a86c6181SAlex Tomas  * It should be OK for low-performance paths like ->writepage()
1709d0d856e8SRandy Dunlap  * To allow many writing processes to fit into a single transaction,
17100e855ac8SAneesh Kumar K.V  * the caller should calculate credits under i_data_sem and
1711d0d856e8SRandy Dunlap  * pass the actual path.
1712a86c6181SAlex Tomas  */
171309b88252SAvantika Mathur int ext4_ext_calc_credits_for_insert(struct inode *inode,
1714a86c6181SAlex Tomas 						struct ext4_ext_path *path)
1715a86c6181SAlex Tomas {
1716a86c6181SAlex Tomas 	int depth, needed;
1717a86c6181SAlex Tomas 
1718a86c6181SAlex Tomas 	if (path) {
1719a86c6181SAlex Tomas 		/* probably there is space in leaf? */
1720a86c6181SAlex Tomas 		depth = ext_depth(inode);
1721a86c6181SAlex Tomas 		if (le16_to_cpu(path[depth].p_hdr->eh_entries)
1722a86c6181SAlex Tomas 				< le16_to_cpu(path[depth].p_hdr->eh_max))
1723a86c6181SAlex Tomas 			return 1;
1724a86c6181SAlex Tomas 	}
1725a86c6181SAlex Tomas 
1726a86c6181SAlex Tomas 	/*
1727d0d856e8SRandy Dunlap 	 * given 32-bit logical block (4294967296 blocks), max. tree
1728a86c6181SAlex Tomas 	 * can be 4 levels in depth -- 4 * 340^4 == 53453440000.
1729d0d856e8SRandy Dunlap 	 * Let's also add one more level for imbalance.
1730a86c6181SAlex Tomas 	 */
1731a86c6181SAlex Tomas 	depth = 5;
1732a86c6181SAlex Tomas 
1733a86c6181SAlex Tomas 	/* allocation of new data block(s) */
1734a86c6181SAlex Tomas 	needed = 2;
1735a86c6181SAlex Tomas 
1736a86c6181SAlex Tomas 	/*
1737d0d856e8SRandy Dunlap 	 * tree can be full, so it would need to grow in depth:
1738feb18927SJohann Lombardi 	 * we need one credit to modify old root, credits for
1739feb18927SJohann Lombardi 	 * new root will be added in split accounting
1740a86c6181SAlex Tomas 	 */
1741feb18927SJohann Lombardi 	needed += 1;
1742a86c6181SAlex Tomas 
1743a86c6181SAlex Tomas 	/*
1744d0d856e8SRandy Dunlap 	 * Index split can happen, we would need:
1745a86c6181SAlex Tomas 	 *    allocate intermediate indexes (bitmap + group)
1746a86c6181SAlex Tomas 	 *  + change two blocks at each level, but root (already included)
1747a86c6181SAlex Tomas 	 */
1748feb18927SJohann Lombardi 	needed += (depth * 2) + (depth * 2);
1749a86c6181SAlex Tomas 
1750a86c6181SAlex Tomas 	/* any allocation modifies superblock */
1751a86c6181SAlex Tomas 	needed += 1;
1752a86c6181SAlex Tomas 
1753a86c6181SAlex Tomas 	return needed;
1754a86c6181SAlex Tomas }
1755a86c6181SAlex Tomas 
1756a86c6181SAlex Tomas static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
1757a86c6181SAlex Tomas 				struct ext4_extent *ex,
1758725d26d3SAneesh Kumar K.V 				ext4_lblk_t from, ext4_lblk_t to)
1759a86c6181SAlex Tomas {
1760a86c6181SAlex Tomas 	struct buffer_head *bh;
1761a2df2a63SAmit Arora 	unsigned short ee_len =  ext4_ext_get_actual_len(ex);
1762c9de560dSAlex Tomas 	int i, metadata = 0;
1763a86c6181SAlex Tomas 
1764c9de560dSAlex Tomas 	if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
1765c9de560dSAlex Tomas 		metadata = 1;
1766a86c6181SAlex Tomas #ifdef EXTENTS_STATS
1767a86c6181SAlex Tomas 	{
1768a86c6181SAlex Tomas 		struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1769a86c6181SAlex Tomas 		spin_lock(&sbi->s_ext_stats_lock);
1770a86c6181SAlex Tomas 		sbi->s_ext_blocks += ee_len;
1771a86c6181SAlex Tomas 		sbi->s_ext_extents++;
1772a86c6181SAlex Tomas 		if (ee_len < sbi->s_ext_min)
1773a86c6181SAlex Tomas 			sbi->s_ext_min = ee_len;
1774a86c6181SAlex Tomas 		if (ee_len > sbi->s_ext_max)
1775a86c6181SAlex Tomas 			sbi->s_ext_max = ee_len;
1776a86c6181SAlex Tomas 		if (ext_depth(inode) > sbi->s_depth_max)
1777a86c6181SAlex Tomas 			sbi->s_depth_max = ext_depth(inode);
1778a86c6181SAlex Tomas 		spin_unlock(&sbi->s_ext_stats_lock);
1779a86c6181SAlex Tomas 	}
1780a86c6181SAlex Tomas #endif
1781a86c6181SAlex Tomas 	if (from >= le32_to_cpu(ex->ee_block)
1782a2df2a63SAmit Arora 	    && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
1783a86c6181SAlex Tomas 		/* tail removal */
1784725d26d3SAneesh Kumar K.V 		ext4_lblk_t num;
1785f65e6fbaSAlex Tomas 		ext4_fsblk_t start;
1786725d26d3SAneesh Kumar K.V 
1787a2df2a63SAmit Arora 		num = le32_to_cpu(ex->ee_block) + ee_len - from;
1788a2df2a63SAmit Arora 		start = ext_pblock(ex) + ee_len - num;
1789725d26d3SAneesh Kumar K.V 		ext_debug("free last %u blocks starting %llu\n", num, start);
1790a86c6181SAlex Tomas 		for (i = 0; i < num; i++) {
1791a86c6181SAlex Tomas 			bh = sb_find_get_block(inode->i_sb, start + i);
1792a86c6181SAlex Tomas 			ext4_forget(handle, 0, inode, bh, start + i);
1793a86c6181SAlex Tomas 		}
1794c9de560dSAlex Tomas 		ext4_free_blocks(handle, inode, start, num, metadata);
1795a86c6181SAlex Tomas 	} else if (from == le32_to_cpu(ex->ee_block)
1796a2df2a63SAmit Arora 		   && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
1797725d26d3SAneesh Kumar K.V 		printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n",
1798a2df2a63SAmit Arora 			from, to, le32_to_cpu(ex->ee_block), ee_len);
1799a86c6181SAlex Tomas 	} else {
1800725d26d3SAneesh Kumar K.V 		printk(KERN_INFO "strange request: removal(2) "
1801725d26d3SAneesh Kumar K.V 				"%u-%u from %u:%u\n",
1802a2df2a63SAmit Arora 				from, to, le32_to_cpu(ex->ee_block), ee_len);
1803a86c6181SAlex Tomas 	}
1804a86c6181SAlex Tomas 	return 0;
1805a86c6181SAlex Tomas }
1806a86c6181SAlex Tomas 
1807a86c6181SAlex Tomas static int
1808a86c6181SAlex Tomas ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
1809725d26d3SAneesh Kumar K.V 		struct ext4_ext_path *path, ext4_lblk_t start)
1810a86c6181SAlex Tomas {
1811a86c6181SAlex Tomas 	int err = 0, correct_index = 0;
1812a86c6181SAlex Tomas 	int depth = ext_depth(inode), credits;
1813a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
1814725d26d3SAneesh Kumar K.V 	ext4_lblk_t a, b, block;
1815725d26d3SAneesh Kumar K.V 	unsigned num;
1816725d26d3SAneesh Kumar K.V 	ext4_lblk_t ex_ee_block;
1817a86c6181SAlex Tomas 	unsigned short ex_ee_len;
1818a2df2a63SAmit Arora 	unsigned uninitialized = 0;
1819a86c6181SAlex Tomas 	struct ext4_extent *ex;
1820a86c6181SAlex Tomas 
1821c29c0ae7SAlex Tomas 	/* the header must be checked already in ext4_ext_remove_space() */
1822725d26d3SAneesh Kumar K.V 	ext_debug("truncate since %u in leaf\n", start);
1823a86c6181SAlex Tomas 	if (!path[depth].p_hdr)
1824a86c6181SAlex Tomas 		path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
1825a86c6181SAlex Tomas 	eh = path[depth].p_hdr;
1826a86c6181SAlex Tomas 	BUG_ON(eh == NULL);
1827a86c6181SAlex Tomas 
1828a86c6181SAlex Tomas 	/* find where to start removing */
1829a86c6181SAlex Tomas 	ex = EXT_LAST_EXTENT(eh);
1830a86c6181SAlex Tomas 
1831a86c6181SAlex Tomas 	ex_ee_block = le32_to_cpu(ex->ee_block);
1832a2df2a63SAmit Arora 	if (ext4_ext_is_uninitialized(ex))
1833a2df2a63SAmit Arora 		uninitialized = 1;
1834a2df2a63SAmit Arora 	ex_ee_len = ext4_ext_get_actual_len(ex);
1835a86c6181SAlex Tomas 
1836a86c6181SAlex Tomas 	while (ex >= EXT_FIRST_EXTENT(eh) &&
1837a86c6181SAlex Tomas 			ex_ee_block + ex_ee_len > start) {
1838a86c6181SAlex Tomas 		ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len);
1839a86c6181SAlex Tomas 		path[depth].p_ext = ex;
1840a86c6181SAlex Tomas 
1841a86c6181SAlex Tomas 		a = ex_ee_block > start ? ex_ee_block : start;
1842a86c6181SAlex Tomas 		b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
1843a86c6181SAlex Tomas 			ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
1844a86c6181SAlex Tomas 
1845a86c6181SAlex Tomas 		ext_debug("  border %u:%u\n", a, b);
1846a86c6181SAlex Tomas 
1847a86c6181SAlex Tomas 		if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
1848a86c6181SAlex Tomas 			block = 0;
1849a86c6181SAlex Tomas 			num = 0;
1850a86c6181SAlex Tomas 			BUG();
1851a86c6181SAlex Tomas 		} else if (a != ex_ee_block) {
1852a86c6181SAlex Tomas 			/* remove tail of the extent */
1853a86c6181SAlex Tomas 			block = ex_ee_block;
1854a86c6181SAlex Tomas 			num = a - block;
1855a86c6181SAlex Tomas 		} else if (b != ex_ee_block + ex_ee_len - 1) {
1856a86c6181SAlex Tomas 			/* remove head of the extent */
1857a86c6181SAlex Tomas 			block = a;
1858a86c6181SAlex Tomas 			num = b - a;
1859a86c6181SAlex Tomas 			/* there is no "make a hole" API yet */
1860a86c6181SAlex Tomas 			BUG();
1861a86c6181SAlex Tomas 		} else {
1862a86c6181SAlex Tomas 			/* remove whole extent: excellent! */
1863a86c6181SAlex Tomas 			block = ex_ee_block;
1864a86c6181SAlex Tomas 			num = 0;
1865a86c6181SAlex Tomas 			BUG_ON(a != ex_ee_block);
1866a86c6181SAlex Tomas 			BUG_ON(b != ex_ee_block + ex_ee_len - 1);
1867a86c6181SAlex Tomas 		}
1868a86c6181SAlex Tomas 
1869d0d856e8SRandy Dunlap 		/* at present, extent can't cross block group: */
1870a86c6181SAlex Tomas 		/* leaf + bitmap + group desc + sb + inode */
1871a86c6181SAlex Tomas 		credits = 5;
1872a86c6181SAlex Tomas 		if (ex == EXT_FIRST_EXTENT(eh)) {
1873a86c6181SAlex Tomas 			correct_index = 1;
1874a86c6181SAlex Tomas 			credits += (ext_depth(inode)) + 1;
1875a86c6181SAlex Tomas 		}
1876a86c6181SAlex Tomas #ifdef CONFIG_QUOTA
1877a86c6181SAlex Tomas 		credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
1878a86c6181SAlex Tomas #endif
1879a86c6181SAlex Tomas 
1880a86c6181SAlex Tomas 		handle = ext4_ext_journal_restart(handle, credits);
1881a86c6181SAlex Tomas 		if (IS_ERR(handle)) {
1882a86c6181SAlex Tomas 			err = PTR_ERR(handle);
1883a86c6181SAlex Tomas 			goto out;
1884a86c6181SAlex Tomas 		}
1885a86c6181SAlex Tomas 
1886a86c6181SAlex Tomas 		err = ext4_ext_get_access(handle, inode, path + depth);
1887a86c6181SAlex Tomas 		if (err)
1888a86c6181SAlex Tomas 			goto out;
1889a86c6181SAlex Tomas 
1890a86c6181SAlex Tomas 		err = ext4_remove_blocks(handle, inode, ex, a, b);
1891a86c6181SAlex Tomas 		if (err)
1892a86c6181SAlex Tomas 			goto out;
1893a86c6181SAlex Tomas 
1894a86c6181SAlex Tomas 		if (num == 0) {
1895d0d856e8SRandy Dunlap 			/* this extent is removed; mark slot entirely unused */
1896f65e6fbaSAlex Tomas 			ext4_ext_store_pblock(ex, 0);
1897a86c6181SAlex Tomas 			eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)-1);
1898a86c6181SAlex Tomas 		}
1899a86c6181SAlex Tomas 
1900a86c6181SAlex Tomas 		ex->ee_block = cpu_to_le32(block);
1901a86c6181SAlex Tomas 		ex->ee_len = cpu_to_le16(num);
1902749269faSAmit Arora 		/*
1903749269faSAmit Arora 		 * Do not mark uninitialized if all the blocks in the
1904749269faSAmit Arora 		 * extent have been removed.
1905749269faSAmit Arora 		 */
1906749269faSAmit Arora 		if (uninitialized && num)
1907a2df2a63SAmit Arora 			ext4_ext_mark_uninitialized(ex);
1908a86c6181SAlex Tomas 
1909a86c6181SAlex Tomas 		err = ext4_ext_dirty(handle, inode, path + depth);
1910a86c6181SAlex Tomas 		if (err)
1911a86c6181SAlex Tomas 			goto out;
1912a86c6181SAlex Tomas 
19132ae02107SMingming Cao 		ext_debug("new extent: %u:%u:%llu\n", block, num,
1914f65e6fbaSAlex Tomas 				ext_pblock(ex));
1915a86c6181SAlex Tomas 		ex--;
1916a86c6181SAlex Tomas 		ex_ee_block = le32_to_cpu(ex->ee_block);
1917a2df2a63SAmit Arora 		ex_ee_len = ext4_ext_get_actual_len(ex);
1918a86c6181SAlex Tomas 	}
1919a86c6181SAlex Tomas 
1920a86c6181SAlex Tomas 	if (correct_index && eh->eh_entries)
1921a86c6181SAlex Tomas 		err = ext4_ext_correct_indexes(handle, inode, path);
1922a86c6181SAlex Tomas 
1923a86c6181SAlex Tomas 	/* if this leaf is free, then we should
1924a86c6181SAlex Tomas 	 * remove it from index block above */
1925a86c6181SAlex Tomas 	if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
1926a86c6181SAlex Tomas 		err = ext4_ext_rm_idx(handle, inode, path + depth);
1927a86c6181SAlex Tomas 
1928a86c6181SAlex Tomas out:
1929a86c6181SAlex Tomas 	return err;
1930a86c6181SAlex Tomas }
1931a86c6181SAlex Tomas 
1932a86c6181SAlex Tomas /*
1933d0d856e8SRandy Dunlap  * ext4_ext_more_to_rm:
1934d0d856e8SRandy Dunlap  * returns 1 if current index has to be freed (even partial)
1935a86c6181SAlex Tomas  */
193609b88252SAvantika Mathur static int
1937a86c6181SAlex Tomas ext4_ext_more_to_rm(struct ext4_ext_path *path)
1938a86c6181SAlex Tomas {
1939a86c6181SAlex Tomas 	BUG_ON(path->p_idx == NULL);
1940a86c6181SAlex Tomas 
1941a86c6181SAlex Tomas 	if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
1942a86c6181SAlex Tomas 		return 0;
1943a86c6181SAlex Tomas 
1944a86c6181SAlex Tomas 	/*
1945d0d856e8SRandy Dunlap 	 * if truncate on deeper level happened, it wasn't partial,
1946a86c6181SAlex Tomas 	 * so we have to consider current index for truncation
1947a86c6181SAlex Tomas 	 */
1948a86c6181SAlex Tomas 	if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
1949a86c6181SAlex Tomas 		return 0;
1950a86c6181SAlex Tomas 	return 1;
1951a86c6181SAlex Tomas }
1952a86c6181SAlex Tomas 
19531d03ec98SAneesh Kumar K.V static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
1954a86c6181SAlex Tomas {
1955a86c6181SAlex Tomas 	struct super_block *sb = inode->i_sb;
1956a86c6181SAlex Tomas 	int depth = ext_depth(inode);
1957a86c6181SAlex Tomas 	struct ext4_ext_path *path;
1958a86c6181SAlex Tomas 	handle_t *handle;
1959a86c6181SAlex Tomas 	int i = 0, err = 0;
1960a86c6181SAlex Tomas 
1961725d26d3SAneesh Kumar K.V 	ext_debug("truncate since %u\n", start);
1962a86c6181SAlex Tomas 
1963a86c6181SAlex Tomas 	/* probably first extent we're gonna free will be last in block */
1964a86c6181SAlex Tomas 	handle = ext4_journal_start(inode, depth + 1);
1965a86c6181SAlex Tomas 	if (IS_ERR(handle))
1966a86c6181SAlex Tomas 		return PTR_ERR(handle);
1967a86c6181SAlex Tomas 
1968a86c6181SAlex Tomas 	ext4_ext_invalidate_cache(inode);
1969a86c6181SAlex Tomas 
1970a86c6181SAlex Tomas 	/*
1971d0d856e8SRandy Dunlap 	 * We start scanning from right side, freeing all the blocks
1972d0d856e8SRandy Dunlap 	 * after i_size and walking into the tree depth-wise.
1973a86c6181SAlex Tomas 	 */
19745d4958f9SAvantika Mathur 	path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_KERNEL);
1975a86c6181SAlex Tomas 	if (path == NULL) {
1976a86c6181SAlex Tomas 		ext4_journal_stop(handle);
1977a86c6181SAlex Tomas 		return -ENOMEM;
1978a86c6181SAlex Tomas 	}
1979a86c6181SAlex Tomas 	path[0].p_hdr = ext_inode_hdr(inode);
1980c29c0ae7SAlex Tomas 	if (ext4_ext_check_header(inode, path[0].p_hdr, depth)) {
1981a86c6181SAlex Tomas 		err = -EIO;
1982a86c6181SAlex Tomas 		goto out;
1983a86c6181SAlex Tomas 	}
1984a86c6181SAlex Tomas 	path[0].p_depth = depth;
1985a86c6181SAlex Tomas 
1986a86c6181SAlex Tomas 	while (i >= 0 && err == 0) {
1987a86c6181SAlex Tomas 		if (i == depth) {
1988a86c6181SAlex Tomas 			/* this is leaf block */
1989a86c6181SAlex Tomas 			err = ext4_ext_rm_leaf(handle, inode, path, start);
1990d0d856e8SRandy Dunlap 			/* root level has p_bh == NULL, brelse() eats this */
1991a86c6181SAlex Tomas 			brelse(path[i].p_bh);
1992a86c6181SAlex Tomas 			path[i].p_bh = NULL;
1993a86c6181SAlex Tomas 			i--;
1994a86c6181SAlex Tomas 			continue;
1995a86c6181SAlex Tomas 		}
1996a86c6181SAlex Tomas 
1997a86c6181SAlex Tomas 		/* this is index block */
1998a86c6181SAlex Tomas 		if (!path[i].p_hdr) {
1999a86c6181SAlex Tomas 			ext_debug("initialize header\n");
2000a86c6181SAlex Tomas 			path[i].p_hdr = ext_block_hdr(path[i].p_bh);
2001a86c6181SAlex Tomas 		}
2002a86c6181SAlex Tomas 
2003a86c6181SAlex Tomas 		if (!path[i].p_idx) {
2004d0d856e8SRandy Dunlap 			/* this level hasn't been touched yet */
2005a86c6181SAlex Tomas 			path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2006a86c6181SAlex Tomas 			path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2007a86c6181SAlex Tomas 			ext_debug("init index ptr: hdr 0x%p, num %d\n",
2008a86c6181SAlex Tomas 				  path[i].p_hdr,
2009a86c6181SAlex Tomas 				  le16_to_cpu(path[i].p_hdr->eh_entries));
2010a86c6181SAlex Tomas 		} else {
2011d0d856e8SRandy Dunlap 			/* we were already here, see at next index */
2012a86c6181SAlex Tomas 			path[i].p_idx--;
2013a86c6181SAlex Tomas 		}
2014a86c6181SAlex Tomas 
2015a86c6181SAlex Tomas 		ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2016a86c6181SAlex Tomas 				i, EXT_FIRST_INDEX(path[i].p_hdr),
2017a86c6181SAlex Tomas 				path[i].p_idx);
2018a86c6181SAlex Tomas 		if (ext4_ext_more_to_rm(path + i)) {
2019c29c0ae7SAlex Tomas 			struct buffer_head *bh;
2020a86c6181SAlex Tomas 			/* go to the next level */
20212ae02107SMingming Cao 			ext_debug("move to level %d (block %llu)\n",
2022f65e6fbaSAlex Tomas 				  i + 1, idx_pblock(path[i].p_idx));
2023a86c6181SAlex Tomas 			memset(path + i + 1, 0, sizeof(*path));
2024c29c0ae7SAlex Tomas 			bh = sb_bread(sb, idx_pblock(path[i].p_idx));
2025c29c0ae7SAlex Tomas 			if (!bh) {
2026a86c6181SAlex Tomas 				/* should we reset i_size? */
2027a86c6181SAlex Tomas 				err = -EIO;
2028a86c6181SAlex Tomas 				break;
2029a86c6181SAlex Tomas 			}
2030c29c0ae7SAlex Tomas 			if (WARN_ON(i + 1 > depth)) {
2031c29c0ae7SAlex Tomas 				err = -EIO;
2032c29c0ae7SAlex Tomas 				break;
2033c29c0ae7SAlex Tomas 			}
2034c29c0ae7SAlex Tomas 			if (ext4_ext_check_header(inode, ext_block_hdr(bh),
2035c29c0ae7SAlex Tomas 							depth - i - 1)) {
2036c29c0ae7SAlex Tomas 				err = -EIO;
2037c29c0ae7SAlex Tomas 				break;
2038c29c0ae7SAlex Tomas 			}
2039c29c0ae7SAlex Tomas 			path[i + 1].p_bh = bh;
2040a86c6181SAlex Tomas 
2041d0d856e8SRandy Dunlap 			/* save actual number of indexes since this
2042d0d856e8SRandy Dunlap 			 * number is changed at the next iteration */
2043a86c6181SAlex Tomas 			path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2044a86c6181SAlex Tomas 			i++;
2045a86c6181SAlex Tomas 		} else {
2046d0d856e8SRandy Dunlap 			/* we finished processing this index, go up */
2047a86c6181SAlex Tomas 			if (path[i].p_hdr->eh_entries == 0 && i > 0) {
2048d0d856e8SRandy Dunlap 				/* index is empty, remove it;
2049a86c6181SAlex Tomas 				 * handle must be already prepared by the
2050a86c6181SAlex Tomas 				 * truncatei_leaf() */
2051a86c6181SAlex Tomas 				err = ext4_ext_rm_idx(handle, inode, path + i);
2052a86c6181SAlex Tomas 			}
2053d0d856e8SRandy Dunlap 			/* root level has p_bh == NULL, brelse() eats this */
2054a86c6181SAlex Tomas 			brelse(path[i].p_bh);
2055a86c6181SAlex Tomas 			path[i].p_bh = NULL;
2056a86c6181SAlex Tomas 			i--;
2057a86c6181SAlex Tomas 			ext_debug("return to level %d\n", i);
2058a86c6181SAlex Tomas 		}
2059a86c6181SAlex Tomas 	}
2060a86c6181SAlex Tomas 
2061a86c6181SAlex Tomas 	/* TODO: flexible tree reduction should be here */
2062a86c6181SAlex Tomas 	if (path->p_hdr->eh_entries == 0) {
2063a86c6181SAlex Tomas 		/*
2064d0d856e8SRandy Dunlap 		 * truncate to zero freed all the tree,
2065d0d856e8SRandy Dunlap 		 * so we need to correct eh_depth
2066a86c6181SAlex Tomas 		 */
2067a86c6181SAlex Tomas 		err = ext4_ext_get_access(handle, inode, path);
2068a86c6181SAlex Tomas 		if (err == 0) {
2069a86c6181SAlex Tomas 			ext_inode_hdr(inode)->eh_depth = 0;
2070a86c6181SAlex Tomas 			ext_inode_hdr(inode)->eh_max =
2071a86c6181SAlex Tomas 				cpu_to_le16(ext4_ext_space_root(inode));
2072a86c6181SAlex Tomas 			err = ext4_ext_dirty(handle, inode, path);
2073a86c6181SAlex Tomas 		}
2074a86c6181SAlex Tomas 	}
2075a86c6181SAlex Tomas out:
2076a86c6181SAlex Tomas 	ext4_ext_tree_changed(inode);
2077a86c6181SAlex Tomas 	ext4_ext_drop_refs(path);
2078a86c6181SAlex Tomas 	kfree(path);
2079a86c6181SAlex Tomas 	ext4_journal_stop(handle);
2080a86c6181SAlex Tomas 
2081a86c6181SAlex Tomas 	return err;
2082a86c6181SAlex Tomas }
2083a86c6181SAlex Tomas 
2084a86c6181SAlex Tomas /*
2085a86c6181SAlex Tomas  * called at mount time
2086a86c6181SAlex Tomas  */
2087a86c6181SAlex Tomas void ext4_ext_init(struct super_block *sb)
2088a86c6181SAlex Tomas {
2089a86c6181SAlex Tomas 	/*
2090a86c6181SAlex Tomas 	 * possible initialization would be here
2091a86c6181SAlex Tomas 	 */
2092a86c6181SAlex Tomas 
2093a86c6181SAlex Tomas 	if (test_opt(sb, EXTENTS)) {
2094a86c6181SAlex Tomas 		printk("EXT4-fs: file extents enabled");
2095bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST
2096bbf2f9fbSRobert P. J. Day 		printk(", aggressive tests");
2097a86c6181SAlex Tomas #endif
2098a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH
2099a86c6181SAlex Tomas 		printk(", check binsearch");
2100a86c6181SAlex Tomas #endif
2101a86c6181SAlex Tomas #ifdef EXTENTS_STATS
2102a86c6181SAlex Tomas 		printk(", stats");
2103a86c6181SAlex Tomas #endif
2104a86c6181SAlex Tomas 		printk("\n");
2105a86c6181SAlex Tomas #ifdef EXTENTS_STATS
2106a86c6181SAlex Tomas 		spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2107a86c6181SAlex Tomas 		EXT4_SB(sb)->s_ext_min = 1 << 30;
2108a86c6181SAlex Tomas 		EXT4_SB(sb)->s_ext_max = 0;
2109a86c6181SAlex Tomas #endif
2110a86c6181SAlex Tomas 	}
2111a86c6181SAlex Tomas }
2112a86c6181SAlex Tomas 
2113a86c6181SAlex Tomas /*
2114a86c6181SAlex Tomas  * called at umount time
2115a86c6181SAlex Tomas  */
2116a86c6181SAlex Tomas void ext4_ext_release(struct super_block *sb)
2117a86c6181SAlex Tomas {
2118a86c6181SAlex Tomas 	if (!test_opt(sb, EXTENTS))
2119a86c6181SAlex Tomas 		return;
2120a86c6181SAlex Tomas 
2121a86c6181SAlex Tomas #ifdef EXTENTS_STATS
2122a86c6181SAlex Tomas 	if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2123a86c6181SAlex Tomas 		struct ext4_sb_info *sbi = EXT4_SB(sb);
2124a86c6181SAlex Tomas 		printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2125a86c6181SAlex Tomas 			sbi->s_ext_blocks, sbi->s_ext_extents,
2126a86c6181SAlex Tomas 			sbi->s_ext_blocks / sbi->s_ext_extents);
2127a86c6181SAlex Tomas 		printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2128a86c6181SAlex Tomas 			sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2129a86c6181SAlex Tomas 	}
2130a86c6181SAlex Tomas #endif
2131a86c6181SAlex Tomas }
2132a86c6181SAlex Tomas 
213356055d3aSAmit Arora /*
213456055d3aSAmit Arora  * This function is called by ext4_ext_get_blocks() if someone tries to write
213556055d3aSAmit Arora  * to an uninitialized extent. It may result in splitting the uninitialized
213656055d3aSAmit Arora  * extent into multiple extents (upto three - one initialized and two
213756055d3aSAmit Arora  * uninitialized).
213856055d3aSAmit Arora  * There are three possibilities:
213956055d3aSAmit Arora  *   a> There is no split required: Entire extent should be initialized
214056055d3aSAmit Arora  *   b> Splits in two extents: Write is happening at either end of the extent
214156055d3aSAmit Arora  *   c> Splits in three extents: Somone is writing in middle of the extent
214256055d3aSAmit Arora  */
2143725d26d3SAneesh Kumar K.V static int ext4_ext_convert_to_initialized(handle_t *handle,
2144725d26d3SAneesh Kumar K.V 						struct inode *inode,
214556055d3aSAmit Arora 						struct ext4_ext_path *path,
2146725d26d3SAneesh Kumar K.V 						ext4_lblk_t iblock,
214756055d3aSAmit Arora 						unsigned long max_blocks)
214856055d3aSAmit Arora {
214956055d3aSAmit Arora 	struct ext4_extent *ex, newex;
215056055d3aSAmit Arora 	struct ext4_extent *ex1 = NULL;
215156055d3aSAmit Arora 	struct ext4_extent *ex2 = NULL;
215256055d3aSAmit Arora 	struct ext4_extent *ex3 = NULL;
215356055d3aSAmit Arora 	struct ext4_extent_header *eh;
2154725d26d3SAneesh Kumar K.V 	ext4_lblk_t ee_block;
2155725d26d3SAneesh Kumar K.V 	unsigned int allocated, ee_len, depth;
215656055d3aSAmit Arora 	ext4_fsblk_t newblock;
215756055d3aSAmit Arora 	int err = 0;
215856055d3aSAmit Arora 	int ret = 0;
215956055d3aSAmit Arora 
216056055d3aSAmit Arora 	depth = ext_depth(inode);
216156055d3aSAmit Arora 	eh = path[depth].p_hdr;
216256055d3aSAmit Arora 	ex = path[depth].p_ext;
216356055d3aSAmit Arora 	ee_block = le32_to_cpu(ex->ee_block);
216456055d3aSAmit Arora 	ee_len = ext4_ext_get_actual_len(ex);
216556055d3aSAmit Arora 	allocated = ee_len - (iblock - ee_block);
216656055d3aSAmit Arora 	newblock = iblock - ee_block + ext_pblock(ex);
216756055d3aSAmit Arora 	ex2 = ex;
216856055d3aSAmit Arora 
216956055d3aSAmit Arora 	/* ex1: ee_block to iblock - 1 : uninitialized */
217056055d3aSAmit Arora 	if (iblock > ee_block) {
217156055d3aSAmit Arora 		ex1 = ex;
217256055d3aSAmit Arora 		ex1->ee_len = cpu_to_le16(iblock - ee_block);
217356055d3aSAmit Arora 		ext4_ext_mark_uninitialized(ex1);
217456055d3aSAmit Arora 		ex2 = &newex;
217556055d3aSAmit Arora 	}
217656055d3aSAmit Arora 	/*
217756055d3aSAmit Arora 	 * for sanity, update the length of the ex2 extent before
217856055d3aSAmit Arora 	 * we insert ex3, if ex1 is NULL. This is to avoid temporary
217956055d3aSAmit Arora 	 * overlap of blocks.
218056055d3aSAmit Arora 	 */
218156055d3aSAmit Arora 	if (!ex1 && allocated > max_blocks)
218256055d3aSAmit Arora 		ex2->ee_len = cpu_to_le16(max_blocks);
218356055d3aSAmit Arora 	/* ex3: to ee_block + ee_len : uninitialised */
218456055d3aSAmit Arora 	if (allocated > max_blocks) {
218556055d3aSAmit Arora 		unsigned int newdepth;
218656055d3aSAmit Arora 		ex3 = &newex;
218756055d3aSAmit Arora 		ex3->ee_block = cpu_to_le32(iblock + max_blocks);
218856055d3aSAmit Arora 		ext4_ext_store_pblock(ex3, newblock + max_blocks);
218956055d3aSAmit Arora 		ex3->ee_len = cpu_to_le16(allocated - max_blocks);
219056055d3aSAmit Arora 		ext4_ext_mark_uninitialized(ex3);
219156055d3aSAmit Arora 		err = ext4_ext_insert_extent(handle, inode, path, ex3);
219256055d3aSAmit Arora 		if (err)
219356055d3aSAmit Arora 			goto out;
219456055d3aSAmit Arora 		/*
219556055d3aSAmit Arora 		 * The depth, and hence eh & ex might change
219656055d3aSAmit Arora 		 * as part of the insert above.
219756055d3aSAmit Arora 		 */
219856055d3aSAmit Arora 		newdepth = ext_depth(inode);
219956055d3aSAmit Arora 		if (newdepth != depth) {
220056055d3aSAmit Arora 			depth = newdepth;
220156055d3aSAmit Arora 			path = ext4_ext_find_extent(inode, iblock, NULL);
220256055d3aSAmit Arora 			if (IS_ERR(path)) {
220356055d3aSAmit Arora 				err = PTR_ERR(path);
220456055d3aSAmit Arora 				path = NULL;
220556055d3aSAmit Arora 				goto out;
220656055d3aSAmit Arora 			}
220756055d3aSAmit Arora 			eh = path[depth].p_hdr;
220856055d3aSAmit Arora 			ex = path[depth].p_ext;
220956055d3aSAmit Arora 			if (ex2 != &newex)
221056055d3aSAmit Arora 				ex2 = ex;
221156055d3aSAmit Arora 		}
221256055d3aSAmit Arora 		allocated = max_blocks;
221356055d3aSAmit Arora 	}
221456055d3aSAmit Arora 	/*
221556055d3aSAmit Arora 	 * If there was a change of depth as part of the
221656055d3aSAmit Arora 	 * insertion of ex3 above, we need to update the length
221756055d3aSAmit Arora 	 * of the ex1 extent again here
221856055d3aSAmit Arora 	 */
221956055d3aSAmit Arora 	if (ex1 && ex1 != ex) {
222056055d3aSAmit Arora 		ex1 = ex;
222156055d3aSAmit Arora 		ex1->ee_len = cpu_to_le16(iblock - ee_block);
222256055d3aSAmit Arora 		ext4_ext_mark_uninitialized(ex1);
222356055d3aSAmit Arora 		ex2 = &newex;
222456055d3aSAmit Arora 	}
222556055d3aSAmit Arora 	/* ex2: iblock to iblock + maxblocks-1 : initialised */
222656055d3aSAmit Arora 	ex2->ee_block = cpu_to_le32(iblock);
222756055d3aSAmit Arora 	ext4_ext_store_pblock(ex2, newblock);
222856055d3aSAmit Arora 	ex2->ee_len = cpu_to_le16(allocated);
222956055d3aSAmit Arora 	if (ex2 != ex)
223056055d3aSAmit Arora 		goto insert;
223156055d3aSAmit Arora 	err = ext4_ext_get_access(handle, inode, path + depth);
223256055d3aSAmit Arora 	if (err)
223356055d3aSAmit Arora 		goto out;
223456055d3aSAmit Arora 	/*
223556055d3aSAmit Arora 	 * New (initialized) extent starts from the first block
223656055d3aSAmit Arora 	 * in the current extent. i.e., ex2 == ex
223756055d3aSAmit Arora 	 * We have to see if it can be merged with the extent
223856055d3aSAmit Arora 	 * on the left.
223956055d3aSAmit Arora 	 */
224056055d3aSAmit Arora 	if (ex2 > EXT_FIRST_EXTENT(eh)) {
224156055d3aSAmit Arora 		/*
224256055d3aSAmit Arora 		 * To merge left, pass "ex2 - 1" to try_to_merge(),
224356055d3aSAmit Arora 		 * since it merges towards right _only_.
224456055d3aSAmit Arora 		 */
224556055d3aSAmit Arora 		ret = ext4_ext_try_to_merge(inode, path, ex2 - 1);
224656055d3aSAmit Arora 		if (ret) {
224756055d3aSAmit Arora 			err = ext4_ext_correct_indexes(handle, inode, path);
224856055d3aSAmit Arora 			if (err)
224956055d3aSAmit Arora 				goto out;
225056055d3aSAmit Arora 			depth = ext_depth(inode);
225156055d3aSAmit Arora 			ex2--;
225256055d3aSAmit Arora 		}
225356055d3aSAmit Arora 	}
225456055d3aSAmit Arora 	/*
225556055d3aSAmit Arora 	 * Try to Merge towards right. This might be required
225656055d3aSAmit Arora 	 * only when the whole extent is being written to.
225756055d3aSAmit Arora 	 * i.e. ex2 == ex and ex3 == NULL.
225856055d3aSAmit Arora 	 */
225956055d3aSAmit Arora 	if (!ex3) {
226056055d3aSAmit Arora 		ret = ext4_ext_try_to_merge(inode, path, ex2);
226156055d3aSAmit Arora 		if (ret) {
226256055d3aSAmit Arora 			err = ext4_ext_correct_indexes(handle, inode, path);
226356055d3aSAmit Arora 			if (err)
226456055d3aSAmit Arora 				goto out;
226556055d3aSAmit Arora 		}
226656055d3aSAmit Arora 	}
226756055d3aSAmit Arora 	/* Mark modified extent as dirty */
226856055d3aSAmit Arora 	err = ext4_ext_dirty(handle, inode, path + depth);
226956055d3aSAmit Arora 	goto out;
227056055d3aSAmit Arora insert:
227156055d3aSAmit Arora 	err = ext4_ext_insert_extent(handle, inode, path, &newex);
227256055d3aSAmit Arora out:
227356055d3aSAmit Arora 	return err ? err : allocated;
227456055d3aSAmit Arora }
227556055d3aSAmit Arora 
2276c278bfecSAneesh Kumar K.V /*
2277c278bfecSAneesh Kumar K.V  * Need to be called with
22780e855ac8SAneesh Kumar K.V  * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
22790e855ac8SAneesh Kumar K.V  * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
2280c278bfecSAneesh Kumar K.V  */
2281f65e6fbaSAlex Tomas int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
2282725d26d3SAneesh Kumar K.V 			ext4_lblk_t iblock,
2283a86c6181SAlex Tomas 			unsigned long max_blocks, struct buffer_head *bh_result,
2284a86c6181SAlex Tomas 			int create, int extend_disksize)
2285a86c6181SAlex Tomas {
2286a86c6181SAlex Tomas 	struct ext4_ext_path *path = NULL;
228756055d3aSAmit Arora 	struct ext4_extent_header *eh;
2288a86c6181SAlex Tomas 	struct ext4_extent newex, *ex;
2289f65e6fbaSAlex Tomas 	ext4_fsblk_t goal, newblock;
229056055d3aSAmit Arora 	int err = 0, depth, ret;
2291a86c6181SAlex Tomas 	unsigned long allocated = 0;
2292c9de560dSAlex Tomas 	struct ext4_allocation_request ar;
2293a86c6181SAlex Tomas 
2294a86c6181SAlex Tomas 	__clear_bit(BH_New, &bh_result->b_state);
2295bba90743SEric Sandeen 	ext_debug("blocks %u/%lu requested for inode %u\n",
2296bba90743SEric Sandeen 			iblock, max_blocks, inode->i_ino);
2297a86c6181SAlex Tomas 
2298a86c6181SAlex Tomas 	/* check in cache */
22997e028976SAvantika Mathur 	goal = ext4_ext_in_cache(inode, iblock, &newex);
23007e028976SAvantika Mathur 	if (goal) {
2301a86c6181SAlex Tomas 		if (goal == EXT4_EXT_CACHE_GAP) {
2302a86c6181SAlex Tomas 			if (!create) {
230356055d3aSAmit Arora 				/*
230456055d3aSAmit Arora 				 * block isn't allocated yet and
230556055d3aSAmit Arora 				 * user doesn't want to allocate it
230656055d3aSAmit Arora 				 */
2307a86c6181SAlex Tomas 				goto out2;
2308a86c6181SAlex Tomas 			}
2309a86c6181SAlex Tomas 			/* we should allocate requested block */
2310a86c6181SAlex Tomas 		} else if (goal == EXT4_EXT_CACHE_EXTENT) {
2311a86c6181SAlex Tomas 			/* block is already allocated */
2312a86c6181SAlex Tomas 			newblock = iblock
2313a86c6181SAlex Tomas 				   - le32_to_cpu(newex.ee_block)
2314f65e6fbaSAlex Tomas 				   + ext_pblock(&newex);
2315d0d856e8SRandy Dunlap 			/* number of remaining blocks in the extent */
2316a86c6181SAlex Tomas 			allocated = le16_to_cpu(newex.ee_len) -
2317a86c6181SAlex Tomas 					(iblock - le32_to_cpu(newex.ee_block));
2318a86c6181SAlex Tomas 			goto out;
2319a86c6181SAlex Tomas 		} else {
2320a86c6181SAlex Tomas 			BUG();
2321a86c6181SAlex Tomas 		}
2322a86c6181SAlex Tomas 	}
2323a86c6181SAlex Tomas 
2324a86c6181SAlex Tomas 	/* find extent for this block */
2325a86c6181SAlex Tomas 	path = ext4_ext_find_extent(inode, iblock, NULL);
2326a86c6181SAlex Tomas 	if (IS_ERR(path)) {
2327a86c6181SAlex Tomas 		err = PTR_ERR(path);
2328a86c6181SAlex Tomas 		path = NULL;
2329a86c6181SAlex Tomas 		goto out2;
2330a86c6181SAlex Tomas 	}
2331a86c6181SAlex Tomas 
2332a86c6181SAlex Tomas 	depth = ext_depth(inode);
2333a86c6181SAlex Tomas 
2334a86c6181SAlex Tomas 	/*
2335d0d856e8SRandy Dunlap 	 * consistent leaf must not be empty;
2336d0d856e8SRandy Dunlap 	 * this situation is possible, though, _during_ tree modification;
2337a86c6181SAlex Tomas 	 * this is why assert can't be put in ext4_ext_find_extent()
2338a86c6181SAlex Tomas 	 */
2339a86c6181SAlex Tomas 	BUG_ON(path[depth].p_ext == NULL && depth != 0);
234056055d3aSAmit Arora 	eh = path[depth].p_hdr;
2341a86c6181SAlex Tomas 
23427e028976SAvantika Mathur 	ex = path[depth].p_ext;
23437e028976SAvantika Mathur 	if (ex) {
2344725d26d3SAneesh Kumar K.V 		ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
2345f65e6fbaSAlex Tomas 		ext4_fsblk_t ee_start = ext_pblock(ex);
2346a2df2a63SAmit Arora 		unsigned short ee_len;
2347471d4011SSuparna Bhattacharya 
2348471d4011SSuparna Bhattacharya 		/*
2349471d4011SSuparna Bhattacharya 		 * Uninitialized extents are treated as holes, except that
235056055d3aSAmit Arora 		 * we split out initialized portions during a write.
2351471d4011SSuparna Bhattacharya 		 */
2352a2df2a63SAmit Arora 		ee_len = ext4_ext_get_actual_len(ex);
2353d0d856e8SRandy Dunlap 		/* if found extent covers block, simply return it */
2354a86c6181SAlex Tomas 		if (iblock >= ee_block && iblock < ee_block + ee_len) {
2355a86c6181SAlex Tomas 			newblock = iblock - ee_block + ee_start;
2356d0d856e8SRandy Dunlap 			/* number of remaining blocks in the extent */
2357a86c6181SAlex Tomas 			allocated = ee_len - (iblock - ee_block);
2358bba90743SEric Sandeen 			ext_debug("%u fit into %lu:%d -> %llu\n", iblock,
2359a86c6181SAlex Tomas 					ee_block, ee_len, newblock);
236056055d3aSAmit Arora 
2361a2df2a63SAmit Arora 			/* Do not put uninitialized extent in the cache */
236256055d3aSAmit Arora 			if (!ext4_ext_is_uninitialized(ex)) {
2363a2df2a63SAmit Arora 				ext4_ext_put_in_cache(inode, ee_block,
2364a2df2a63SAmit Arora 							ee_len, ee_start,
2365a2df2a63SAmit Arora 							EXT4_EXT_CACHE_EXTENT);
2366a86c6181SAlex Tomas 				goto out;
2367a86c6181SAlex Tomas 			}
236856055d3aSAmit Arora 			if (create == EXT4_CREATE_UNINITIALIZED_EXT)
236956055d3aSAmit Arora 				goto out;
237056055d3aSAmit Arora 			if (!create)
237156055d3aSAmit Arora 				goto out2;
237256055d3aSAmit Arora 
237356055d3aSAmit Arora 			ret = ext4_ext_convert_to_initialized(handle, inode,
237456055d3aSAmit Arora 								path, iblock,
237556055d3aSAmit Arora 								max_blocks);
2376dbf9d7daSDmitry Monakhov 			if (ret <= 0) {
2377dbf9d7daSDmitry Monakhov 				err = ret;
237856055d3aSAmit Arora 				goto out2;
2379dbf9d7daSDmitry Monakhov 			} else
238056055d3aSAmit Arora 				allocated = ret;
238156055d3aSAmit Arora 			goto outnew;
238256055d3aSAmit Arora 		}
2383a86c6181SAlex Tomas 	}
2384a86c6181SAlex Tomas 
2385a86c6181SAlex Tomas 	/*
2386d0d856e8SRandy Dunlap 	 * requested block isn't allocated yet;
2387a86c6181SAlex Tomas 	 * we couldn't try to create block if create flag is zero
2388a86c6181SAlex Tomas 	 */
2389a86c6181SAlex Tomas 	if (!create) {
239056055d3aSAmit Arora 		/*
239156055d3aSAmit Arora 		 * put just found gap into cache to speed up
239256055d3aSAmit Arora 		 * subsequent requests
239356055d3aSAmit Arora 		 */
2394a86c6181SAlex Tomas 		ext4_ext_put_gap_in_cache(inode, path, iblock);
2395a86c6181SAlex Tomas 		goto out2;
2396a86c6181SAlex Tomas 	}
2397a86c6181SAlex Tomas 	/*
2398a86c6181SAlex Tomas 	 * Okay, we need to do block allocation.  Lazily initialize the block
2399d0d856e8SRandy Dunlap 	 * allocation info here if necessary.
2400a86c6181SAlex Tomas 	 */
2401a86c6181SAlex Tomas 	if (S_ISREG(inode->i_mode) && (!EXT4_I(inode)->i_block_alloc_info))
2402a86c6181SAlex Tomas 		ext4_init_block_alloc_info(inode);
2403a86c6181SAlex Tomas 
2404c9de560dSAlex Tomas 	/* find neighbour allocated blocks */
2405c9de560dSAlex Tomas 	ar.lleft = iblock;
2406c9de560dSAlex Tomas 	err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
2407c9de560dSAlex Tomas 	if (err)
2408c9de560dSAlex Tomas 		goto out2;
2409c9de560dSAlex Tomas 	ar.lright = iblock;
2410c9de560dSAlex Tomas 	err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright);
2411c9de560dSAlex Tomas 	if (err)
2412c9de560dSAlex Tomas 		goto out2;
241325d14f98SAmit Arora 
2414749269faSAmit Arora 	/*
2415749269faSAmit Arora 	 * See if request is beyond maximum number of blocks we can have in
2416749269faSAmit Arora 	 * a single extent. For an initialized extent this limit is
2417749269faSAmit Arora 	 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
2418749269faSAmit Arora 	 * EXT_UNINIT_MAX_LEN.
2419749269faSAmit Arora 	 */
2420749269faSAmit Arora 	if (max_blocks > EXT_INIT_MAX_LEN &&
2421749269faSAmit Arora 	    create != EXT4_CREATE_UNINITIALIZED_EXT)
2422749269faSAmit Arora 		max_blocks = EXT_INIT_MAX_LEN;
2423749269faSAmit Arora 	else if (max_blocks > EXT_UNINIT_MAX_LEN &&
2424749269faSAmit Arora 		 create == EXT4_CREATE_UNINITIALIZED_EXT)
2425749269faSAmit Arora 		max_blocks = EXT_UNINIT_MAX_LEN;
2426749269faSAmit Arora 
242725d14f98SAmit Arora 	/* Check if we can really insert (iblock)::(iblock+max_blocks) extent */
242825d14f98SAmit Arora 	newex.ee_block = cpu_to_le32(iblock);
242925d14f98SAmit Arora 	newex.ee_len = cpu_to_le16(max_blocks);
243025d14f98SAmit Arora 	err = ext4_ext_check_overlap(inode, &newex, path);
243125d14f98SAmit Arora 	if (err)
243225d14f98SAmit Arora 		allocated = le16_to_cpu(newex.ee_len);
243325d14f98SAmit Arora 	else
2434a86c6181SAlex Tomas 		allocated = max_blocks;
2435c9de560dSAlex Tomas 
2436c9de560dSAlex Tomas 	/* allocate new block */
2437c9de560dSAlex Tomas 	ar.inode = inode;
2438c9de560dSAlex Tomas 	ar.goal = ext4_ext_find_goal(inode, path, iblock);
2439c9de560dSAlex Tomas 	ar.logical = iblock;
2440c9de560dSAlex Tomas 	ar.len = allocated;
2441c9de560dSAlex Tomas 	if (S_ISREG(inode->i_mode))
2442c9de560dSAlex Tomas 		ar.flags = EXT4_MB_HINT_DATA;
2443c9de560dSAlex Tomas 	else
2444c9de560dSAlex Tomas 		/* disable in-core preallocation for non-regular files */
2445c9de560dSAlex Tomas 		ar.flags = 0;
2446c9de560dSAlex Tomas 	newblock = ext4_mb_new_blocks(handle, &ar, &err);
2447a86c6181SAlex Tomas 	if (!newblock)
2448a86c6181SAlex Tomas 		goto out2;
24492ae02107SMingming Cao 	ext_debug("allocate new block: goal %llu, found %llu/%lu\n",
2450a86c6181SAlex Tomas 			goal, newblock, allocated);
2451a86c6181SAlex Tomas 
2452a86c6181SAlex Tomas 	/* try to insert new extent into found leaf and return */
2453f65e6fbaSAlex Tomas 	ext4_ext_store_pblock(&newex, newblock);
2454c9de560dSAlex Tomas 	newex.ee_len = cpu_to_le16(ar.len);
2455a2df2a63SAmit Arora 	if (create == EXT4_CREATE_UNINITIALIZED_EXT)  /* Mark uninitialized */
2456a2df2a63SAmit Arora 		ext4_ext_mark_uninitialized(&newex);
2457a86c6181SAlex Tomas 	err = ext4_ext_insert_extent(handle, inode, path, &newex);
2458315054f0SAlex Tomas 	if (err) {
2459315054f0SAlex Tomas 		/* free data blocks we just allocated */
2460c9de560dSAlex Tomas 		/* not a good idea to call discard here directly,
2461c9de560dSAlex Tomas 		 * but otherwise we'd need to call it every free() */
2462c9de560dSAlex Tomas 		ext4_mb_discard_inode_preallocations(inode);
2463315054f0SAlex Tomas 		ext4_free_blocks(handle, inode, ext_pblock(&newex),
2464c9de560dSAlex Tomas 					le16_to_cpu(newex.ee_len), 0);
2465a86c6181SAlex Tomas 		goto out2;
2466315054f0SAlex Tomas 	}
2467a86c6181SAlex Tomas 
2468a86c6181SAlex Tomas 	if (extend_disksize && inode->i_size > EXT4_I(inode)->i_disksize)
2469a86c6181SAlex Tomas 		EXT4_I(inode)->i_disksize = inode->i_size;
2470a86c6181SAlex Tomas 
2471a86c6181SAlex Tomas 	/* previous routine could use block we allocated */
2472f65e6fbaSAlex Tomas 	newblock = ext_pblock(&newex);
2473c9de560dSAlex Tomas 	allocated = le16_to_cpu(newex.ee_len);
247456055d3aSAmit Arora outnew:
2475a86c6181SAlex Tomas 	__set_bit(BH_New, &bh_result->b_state);
2476a86c6181SAlex Tomas 
2477a2df2a63SAmit Arora 	/* Cache only when it is _not_ an uninitialized extent */
2478a2df2a63SAmit Arora 	if (create != EXT4_CREATE_UNINITIALIZED_EXT)
2479a86c6181SAlex Tomas 		ext4_ext_put_in_cache(inode, iblock, allocated, newblock,
2480a86c6181SAlex Tomas 						EXT4_EXT_CACHE_EXTENT);
2481a86c6181SAlex Tomas out:
2482a86c6181SAlex Tomas 	if (allocated > max_blocks)
2483a86c6181SAlex Tomas 		allocated = max_blocks;
2484a86c6181SAlex Tomas 	ext4_ext_show_leaf(inode, path);
2485a86c6181SAlex Tomas 	__set_bit(BH_Mapped, &bh_result->b_state);
2486a86c6181SAlex Tomas 	bh_result->b_bdev = inode->i_sb->s_bdev;
2487a86c6181SAlex Tomas 	bh_result->b_blocknr = newblock;
2488a86c6181SAlex Tomas out2:
2489a86c6181SAlex Tomas 	if (path) {
2490a86c6181SAlex Tomas 		ext4_ext_drop_refs(path);
2491a86c6181SAlex Tomas 		kfree(path);
2492a86c6181SAlex Tomas 	}
2493a86c6181SAlex Tomas 	return err ? err : allocated;
2494a86c6181SAlex Tomas }
2495a86c6181SAlex Tomas 
2496a86c6181SAlex Tomas void ext4_ext_truncate(struct inode * inode, struct page *page)
2497a86c6181SAlex Tomas {
2498a86c6181SAlex Tomas 	struct address_space *mapping = inode->i_mapping;
2499a86c6181SAlex Tomas 	struct super_block *sb = inode->i_sb;
2500725d26d3SAneesh Kumar K.V 	ext4_lblk_t last_block;
2501a86c6181SAlex Tomas 	handle_t *handle;
2502a86c6181SAlex Tomas 	int err = 0;
2503a86c6181SAlex Tomas 
2504a86c6181SAlex Tomas 	/*
2505a86c6181SAlex Tomas 	 * probably first extent we're gonna free will be last in block
2506a86c6181SAlex Tomas 	 */
2507a86c6181SAlex Tomas 	err = ext4_writepage_trans_blocks(inode) + 3;
2508a86c6181SAlex Tomas 	handle = ext4_journal_start(inode, err);
2509a86c6181SAlex Tomas 	if (IS_ERR(handle)) {
2510a86c6181SAlex Tomas 		if (page) {
2511a86c6181SAlex Tomas 			clear_highpage(page);
2512a86c6181SAlex Tomas 			flush_dcache_page(page);
2513a86c6181SAlex Tomas 			unlock_page(page);
2514a86c6181SAlex Tomas 			page_cache_release(page);
2515a86c6181SAlex Tomas 		}
2516a86c6181SAlex Tomas 		return;
2517a86c6181SAlex Tomas 	}
2518a86c6181SAlex Tomas 
2519a86c6181SAlex Tomas 	if (page)
2520a86c6181SAlex Tomas 		ext4_block_truncate_page(handle, page, mapping, inode->i_size);
2521a86c6181SAlex Tomas 
25220e855ac8SAneesh Kumar K.V 	down_write(&EXT4_I(inode)->i_data_sem);
2523a86c6181SAlex Tomas 	ext4_ext_invalidate_cache(inode);
2524a86c6181SAlex Tomas 
2525c9de560dSAlex Tomas 	ext4_mb_discard_inode_preallocations(inode);
2526c9de560dSAlex Tomas 
2527a86c6181SAlex Tomas 	/*
2528d0d856e8SRandy Dunlap 	 * TODO: optimization is possible here.
2529d0d856e8SRandy Dunlap 	 * Probably we need not scan at all,
2530d0d856e8SRandy Dunlap 	 * because page truncation is enough.
2531a86c6181SAlex Tomas 	 */
2532a86c6181SAlex Tomas 	if (ext4_orphan_add(handle, inode))
2533a86c6181SAlex Tomas 		goto out_stop;
2534a86c6181SAlex Tomas 
2535a86c6181SAlex Tomas 	/* we have to know where to truncate from in crash case */
2536a86c6181SAlex Tomas 	EXT4_I(inode)->i_disksize = inode->i_size;
2537a86c6181SAlex Tomas 	ext4_mark_inode_dirty(handle, inode);
2538a86c6181SAlex Tomas 
2539a86c6181SAlex Tomas 	last_block = (inode->i_size + sb->s_blocksize - 1)
2540a86c6181SAlex Tomas 			>> EXT4_BLOCK_SIZE_BITS(sb);
2541a86c6181SAlex Tomas 	err = ext4_ext_remove_space(inode, last_block);
2542a86c6181SAlex Tomas 
2543a86c6181SAlex Tomas 	/* In a multi-transaction truncate, we only make the final
254456055d3aSAmit Arora 	 * transaction synchronous.
254556055d3aSAmit Arora 	 */
2546a86c6181SAlex Tomas 	if (IS_SYNC(inode))
2547a86c6181SAlex Tomas 		handle->h_sync = 1;
2548a86c6181SAlex Tomas 
2549a86c6181SAlex Tomas out_stop:
2550a86c6181SAlex Tomas 	/*
2551d0d856e8SRandy Dunlap 	 * If this was a simple ftruncate() and the file will remain alive,
2552a86c6181SAlex Tomas 	 * then we need to clear up the orphan record which we created above.
2553a86c6181SAlex Tomas 	 * However, if this was a real unlink then we were called by
2554a86c6181SAlex Tomas 	 * ext4_delete_inode(), and we allow that function to clean up the
2555a86c6181SAlex Tomas 	 * orphan info for us.
2556a86c6181SAlex Tomas 	 */
2557a86c6181SAlex Tomas 	if (inode->i_nlink)
2558a86c6181SAlex Tomas 		ext4_orphan_del(handle, inode);
2559a86c6181SAlex Tomas 
25600e855ac8SAneesh Kumar K.V 	up_write(&EXT4_I(inode)->i_data_sem);
2561a86c6181SAlex Tomas 	ext4_journal_stop(handle);
2562a86c6181SAlex Tomas }
2563a86c6181SAlex Tomas 
2564a86c6181SAlex Tomas /*
2565d0d856e8SRandy Dunlap  * ext4_ext_writepage_trans_blocks:
2566d0d856e8SRandy Dunlap  * calculate max number of blocks we could modify
2567a86c6181SAlex Tomas  * in order to allocate new block for an inode
2568a86c6181SAlex Tomas  */
2569a86c6181SAlex Tomas int ext4_ext_writepage_trans_blocks(struct inode *inode, int num)
2570a86c6181SAlex Tomas {
2571a86c6181SAlex Tomas 	int needed;
2572a86c6181SAlex Tomas 
2573a86c6181SAlex Tomas 	needed = ext4_ext_calc_credits_for_insert(inode, NULL);
2574a86c6181SAlex Tomas 
2575d0d856e8SRandy Dunlap 	/* caller wants to allocate num blocks, but note it includes sb */
2576a86c6181SAlex Tomas 	needed = needed * num - (num - 1);
2577a86c6181SAlex Tomas 
2578a86c6181SAlex Tomas #ifdef CONFIG_QUOTA
2579a86c6181SAlex Tomas 	needed += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
2580a86c6181SAlex Tomas #endif
2581a86c6181SAlex Tomas 
2582a86c6181SAlex Tomas 	return needed;
2583a86c6181SAlex Tomas }
2584a2df2a63SAmit Arora 
2585a2df2a63SAmit Arora /*
2586a2df2a63SAmit Arora  * preallocate space for a file. This implements ext4's fallocate inode
2587a2df2a63SAmit Arora  * operation, which gets called from sys_fallocate system call.
2588a2df2a63SAmit Arora  * For block-mapped files, posix_fallocate should fall back to the method
2589a2df2a63SAmit Arora  * of writing zeroes to the required new blocks (the same behavior which is
2590a2df2a63SAmit Arora  * expected for file systems which do not support fallocate() system call).
2591a2df2a63SAmit Arora  */
2592a2df2a63SAmit Arora long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
2593a2df2a63SAmit Arora {
2594a2df2a63SAmit Arora 	handle_t *handle;
2595725d26d3SAneesh Kumar K.V 	ext4_lblk_t block;
2596725d26d3SAneesh Kumar K.V 	unsigned long max_blocks;
2597a2df2a63SAmit Arora 	ext4_fsblk_t nblocks = 0;
2598a2df2a63SAmit Arora 	int ret = 0;
2599a2df2a63SAmit Arora 	int ret2 = 0;
2600a2df2a63SAmit Arora 	int retries = 0;
2601a2df2a63SAmit Arora 	struct buffer_head map_bh;
2602a2df2a63SAmit Arora 	unsigned int credits, blkbits = inode->i_blkbits;
2603a2df2a63SAmit Arora 
2604a2df2a63SAmit Arora 	/*
2605a2df2a63SAmit Arora 	 * currently supporting (pre)allocate mode for extent-based
2606a2df2a63SAmit Arora 	 * files _only_
2607a2df2a63SAmit Arora 	 */
2608a2df2a63SAmit Arora 	if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
2609a2df2a63SAmit Arora 		return -EOPNOTSUPP;
2610a2df2a63SAmit Arora 
2611a2df2a63SAmit Arora 	/* preallocation to directories is currently not supported */
2612a2df2a63SAmit Arora 	if (S_ISDIR(inode->i_mode))
2613a2df2a63SAmit Arora 		return -ENODEV;
2614a2df2a63SAmit Arora 
2615a2df2a63SAmit Arora 	block = offset >> blkbits;
2616a2df2a63SAmit Arora 	max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
2617a2df2a63SAmit Arora 			- block;
2618a2df2a63SAmit Arora 
2619a2df2a63SAmit Arora 	/*
2620a2df2a63SAmit Arora 	 * credits to insert 1 extent into extent tree + buffers to be able to
2621a2df2a63SAmit Arora 	 * modify 1 super block, 1 block bitmap and 1 group descriptor.
2622a2df2a63SAmit Arora 	 */
2623a2df2a63SAmit Arora 	credits = EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + 3;
26240e855ac8SAneesh Kumar K.V 	down_write((&EXT4_I(inode)->i_data_sem));
2625a2df2a63SAmit Arora retry:
2626a2df2a63SAmit Arora 	while (ret >= 0 && ret < max_blocks) {
2627a2df2a63SAmit Arora 		block = block + ret;
2628a2df2a63SAmit Arora 		max_blocks = max_blocks - ret;
2629a2df2a63SAmit Arora 		handle = ext4_journal_start(inode, credits);
2630a2df2a63SAmit Arora 		if (IS_ERR(handle)) {
2631a2df2a63SAmit Arora 			ret = PTR_ERR(handle);
2632a2df2a63SAmit Arora 			break;
2633a2df2a63SAmit Arora 		}
2634a2df2a63SAmit Arora 
2635a2df2a63SAmit Arora 		ret = ext4_ext_get_blocks(handle, inode, block,
2636a2df2a63SAmit Arora 					  max_blocks, &map_bh,
2637a2df2a63SAmit Arora 					  EXT4_CREATE_UNINITIALIZED_EXT, 0);
2638221879c9SAneesh Kumar K.V 		WARN_ON(ret <= 0);
2639221879c9SAneesh Kumar K.V 		if (ret <= 0) {
2640a2df2a63SAmit Arora 			ext4_error(inode->i_sb, "ext4_fallocate",
2641221879c9SAneesh Kumar K.V 				    "ext4_ext_get_blocks returned error: "
2642221879c9SAneesh Kumar K.V 				    "inode#%lu, block=%u, max_blocks=%lu",
2643bba90743SEric Sandeen 				    inode->i_ino, block, max_blocks);
2644a2df2a63SAmit Arora 			ret = -EIO;
2645a2df2a63SAmit Arora 			ext4_mark_inode_dirty(handle, inode);
2646a2df2a63SAmit Arora 			ret2 = ext4_journal_stop(handle);
2647a2df2a63SAmit Arora 			break;
2648a2df2a63SAmit Arora 		}
2649a2df2a63SAmit Arora 		if (ret > 0) {
2650a2df2a63SAmit Arora 			/* check wrap through sign-bit/zero here */
2651a2df2a63SAmit Arora 			if ((block + ret) < 0 || (block + ret) < block) {
2652a2df2a63SAmit Arora 				ret = -EIO;
2653a2df2a63SAmit Arora 				ext4_mark_inode_dirty(handle, inode);
2654a2df2a63SAmit Arora 				ret2 = ext4_journal_stop(handle);
2655a2df2a63SAmit Arora 				break;
2656a2df2a63SAmit Arora 			}
2657a2df2a63SAmit Arora 			if (buffer_new(&map_bh) && ((block + ret) >
2658a2df2a63SAmit Arora 			    (EXT4_BLOCK_ALIGN(i_size_read(inode), blkbits)
2659a2df2a63SAmit Arora 			    >> blkbits)))
2660a2df2a63SAmit Arora 					nblocks = nblocks + ret;
2661a2df2a63SAmit Arora 		}
2662a2df2a63SAmit Arora 
2663a2df2a63SAmit Arora 		/* Update ctime if new blocks get allocated */
2664a2df2a63SAmit Arora 		if (nblocks) {
2665a2df2a63SAmit Arora 			struct timespec now;
2666a2df2a63SAmit Arora 
2667a2df2a63SAmit Arora 			now = current_fs_time(inode->i_sb);
2668a2df2a63SAmit Arora 			if (!timespec_equal(&inode->i_ctime, &now))
2669a2df2a63SAmit Arora 				inode->i_ctime = now;
2670a2df2a63SAmit Arora 		}
2671a2df2a63SAmit Arora 
2672a2df2a63SAmit Arora 		ext4_mark_inode_dirty(handle, inode);
2673a2df2a63SAmit Arora 		ret2 = ext4_journal_stop(handle);
2674a2df2a63SAmit Arora 		if (ret2)
2675a2df2a63SAmit Arora 			break;
2676a2df2a63SAmit Arora 	}
2677a2df2a63SAmit Arora 
2678a2df2a63SAmit Arora 	if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
2679a2df2a63SAmit Arora 		goto retry;
2680a2df2a63SAmit Arora 
26810e855ac8SAneesh Kumar K.V 	up_write((&EXT4_I(inode)->i_data_sem));
2682a2df2a63SAmit Arora 	/*
2683a2df2a63SAmit Arora 	 * Time to update the file size.
2684a2df2a63SAmit Arora 	 * Update only when preallocation was requested beyond the file size.
2685a2df2a63SAmit Arora 	 */
2686a2df2a63SAmit Arora 	if (!(mode & FALLOC_FL_KEEP_SIZE) &&
2687a2df2a63SAmit Arora 	    (offset + len) > i_size_read(inode)) {
2688a2df2a63SAmit Arora 		if (ret > 0) {
2689a2df2a63SAmit Arora 			/*
2690a2df2a63SAmit Arora 			 * if no error, we assume preallocation succeeded
2691a2df2a63SAmit Arora 			 * completely
2692a2df2a63SAmit Arora 			 */
2693a2df2a63SAmit Arora 			mutex_lock(&inode->i_mutex);
2694a2df2a63SAmit Arora 			i_size_write(inode, offset + len);
2695a2df2a63SAmit Arora 			EXT4_I(inode)->i_disksize = i_size_read(inode);
2696a2df2a63SAmit Arora 			mutex_unlock(&inode->i_mutex);
2697a2df2a63SAmit Arora 		} else if (ret < 0 && nblocks) {
2698a2df2a63SAmit Arora 			/* Handle partial allocation scenario */
2699a2df2a63SAmit Arora 			loff_t newsize;
2700a2df2a63SAmit Arora 
2701a2df2a63SAmit Arora 			mutex_lock(&inode->i_mutex);
2702a2df2a63SAmit Arora 			newsize  = (nblocks << blkbits) + i_size_read(inode);
2703a2df2a63SAmit Arora 			i_size_write(inode, EXT4_BLOCK_ALIGN(newsize, blkbits));
2704a2df2a63SAmit Arora 			EXT4_I(inode)->i_disksize = i_size_read(inode);
2705a2df2a63SAmit Arora 			mutex_unlock(&inode->i_mutex);
2706a2df2a63SAmit Arora 		}
2707a2df2a63SAmit Arora 	}
2708a2df2a63SAmit Arora 
2709a2df2a63SAmit Arora 	return ret > 0 ? ret2 : ret;
2710a2df2a63SAmit Arora }
2711