xref: /openbmc/linux/fs/ext4/extents.c (revision ee12b630687d510f6f4b6d4acdc4e267fd4adeda)
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>
35cd02ff0bSMingming Cao #include <linux/jbd2.h>
36a86c6181SAlex Tomas #include <linux/highuid.h>
37a86c6181SAlex Tomas #include <linux/pagemap.h>
38a86c6181SAlex Tomas #include <linux/quotaops.h>
39a86c6181SAlex Tomas #include <linux/string.h>
40a86c6181SAlex Tomas #include <linux/slab.h>
41a2df2a63SAmit Arora #include <linux/falloc.h>
42a86c6181SAlex Tomas #include <asm/uaccess.h>
433dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
443dcf5451SChristoph Hellwig #include "ext4_extents.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 
959102e4faSShen Feng static int 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)
1009102e4faSShen Feng 		return 0;
1019102e4faSShen Feng 	err = ext4_journal_extend(handle, needed);
1020123c939STheodore Ts'o 	if (err <= 0)
1039102e4faSShen Feng 		return err;
1049102e4faSShen Feng 	return ext4_journal_restart(handle, needed);
105a86c6181SAlex Tomas }
106a86c6181SAlex Tomas 
107a86c6181SAlex Tomas /*
108a86c6181SAlex Tomas  * could return:
109a86c6181SAlex Tomas  *  - EROFS
110a86c6181SAlex Tomas  *  - ENOMEM
111a86c6181SAlex Tomas  */
112a86c6181SAlex Tomas static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
113a86c6181SAlex Tomas 				struct ext4_ext_path *path)
114a86c6181SAlex Tomas {
115a86c6181SAlex Tomas 	if (path->p_bh) {
116a86c6181SAlex Tomas 		/* path points to block */
117a86c6181SAlex Tomas 		return ext4_journal_get_write_access(handle, path->p_bh);
118a86c6181SAlex Tomas 	}
119a86c6181SAlex Tomas 	/* path points to leaf/index in inode body */
120a86c6181SAlex Tomas 	/* we use in-core data, no need to protect them */
121a86c6181SAlex Tomas 	return 0;
122a86c6181SAlex Tomas }
123a86c6181SAlex Tomas 
124a86c6181SAlex Tomas /*
125a86c6181SAlex Tomas  * could return:
126a86c6181SAlex Tomas  *  - EROFS
127a86c6181SAlex Tomas  *  - ENOMEM
128a86c6181SAlex Tomas  *  - EIO
129a86c6181SAlex Tomas  */
130a86c6181SAlex Tomas static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
131a86c6181SAlex Tomas 				struct ext4_ext_path *path)
132a86c6181SAlex Tomas {
133a86c6181SAlex Tomas 	int err;
134a86c6181SAlex Tomas 	if (path->p_bh) {
135a86c6181SAlex Tomas 		/* path points to block */
136a86c6181SAlex Tomas 		err = ext4_journal_dirty_metadata(handle, path->p_bh);
137a86c6181SAlex Tomas 	} else {
138a86c6181SAlex Tomas 		/* path points to leaf/index in inode body */
139a86c6181SAlex Tomas 		err = ext4_mark_inode_dirty(handle, inode);
140a86c6181SAlex Tomas 	}
141a86c6181SAlex Tomas 	return err;
142a86c6181SAlex Tomas }
143a86c6181SAlex Tomas 
144f65e6fbaSAlex Tomas static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
145a86c6181SAlex Tomas 			      struct ext4_ext_path *path,
146725d26d3SAneesh Kumar K.V 			      ext4_lblk_t block)
147a86c6181SAlex Tomas {
148a86c6181SAlex Tomas 	struct ext4_inode_info *ei = EXT4_I(inode);
149f65e6fbaSAlex Tomas 	ext4_fsblk_t bg_start;
15074d3487fSValerie Clement 	ext4_fsblk_t last_block;
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);
17274d3487fSValerie Clement 	last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
17374d3487fSValerie Clement 
17474d3487fSValerie Clement 	if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
175a86c6181SAlex Tomas 		colour = (current->pid % 16) *
176a86c6181SAlex Tomas 			(EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
17774d3487fSValerie Clement 	else
17874d3487fSValerie Clement 		colour = (current->pid % 16) * ((last_block - bg_start) / 16);
179a86c6181SAlex Tomas 	return bg_start + colour + block;
180a86c6181SAlex Tomas }
181a86c6181SAlex Tomas 
182654b4908SAneesh Kumar K.V /*
183654b4908SAneesh Kumar K.V  * Allocation for a meta data block
184654b4908SAneesh Kumar K.V  */
185f65e6fbaSAlex Tomas static ext4_fsblk_t
186654b4908SAneesh Kumar K.V ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
187a86c6181SAlex Tomas 			struct ext4_ext_path *path,
188a86c6181SAlex Tomas 			struct ext4_extent *ex, int *err)
189a86c6181SAlex Tomas {
190f65e6fbaSAlex Tomas 	ext4_fsblk_t goal, newblock;
191a86c6181SAlex Tomas 
192a86c6181SAlex Tomas 	goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
1937061eba7SAneesh Kumar K.V 	newblock = ext4_new_meta_block(handle, inode, goal, err);
194a86c6181SAlex Tomas 	return newblock;
195a86c6181SAlex Tomas }
196a86c6181SAlex Tomas 
19709b88252SAvantika Mathur static int ext4_ext_space_block(struct inode *inode)
198a86c6181SAlex Tomas {
199a86c6181SAlex Tomas 	int size;
200a86c6181SAlex Tomas 
201a86c6181SAlex Tomas 	size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
202a86c6181SAlex Tomas 			/ sizeof(struct ext4_extent);
203bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST
204a86c6181SAlex Tomas 	if (size > 6)
205a86c6181SAlex Tomas 		size = 6;
206a86c6181SAlex Tomas #endif
207a86c6181SAlex Tomas 	return size;
208a86c6181SAlex Tomas }
209a86c6181SAlex Tomas 
21009b88252SAvantika Mathur static int ext4_ext_space_block_idx(struct inode *inode)
211a86c6181SAlex Tomas {
212a86c6181SAlex Tomas 	int size;
213a86c6181SAlex Tomas 
214a86c6181SAlex Tomas 	size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
215a86c6181SAlex Tomas 			/ sizeof(struct ext4_extent_idx);
216bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST
217a86c6181SAlex Tomas 	if (size > 5)
218a86c6181SAlex Tomas 		size = 5;
219a86c6181SAlex Tomas #endif
220a86c6181SAlex Tomas 	return size;
221a86c6181SAlex Tomas }
222a86c6181SAlex Tomas 
22309b88252SAvantika Mathur static int ext4_ext_space_root(struct inode *inode)
224a86c6181SAlex Tomas {
225a86c6181SAlex Tomas 	int size;
226a86c6181SAlex Tomas 
227a86c6181SAlex Tomas 	size = sizeof(EXT4_I(inode)->i_data);
228a86c6181SAlex Tomas 	size -= sizeof(struct ext4_extent_header);
229a86c6181SAlex Tomas 	size /= sizeof(struct ext4_extent);
230bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST
231a86c6181SAlex Tomas 	if (size > 3)
232a86c6181SAlex Tomas 		size = 3;
233a86c6181SAlex Tomas #endif
234a86c6181SAlex Tomas 	return size;
235a86c6181SAlex Tomas }
236a86c6181SAlex Tomas 
23709b88252SAvantika Mathur static int ext4_ext_space_root_idx(struct inode *inode)
238a86c6181SAlex Tomas {
239a86c6181SAlex Tomas 	int size;
240a86c6181SAlex Tomas 
241a86c6181SAlex Tomas 	size = sizeof(EXT4_I(inode)->i_data);
242a86c6181SAlex Tomas 	size -= sizeof(struct ext4_extent_header);
243a86c6181SAlex Tomas 	size /= sizeof(struct ext4_extent_idx);
244bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST
245a86c6181SAlex Tomas 	if (size > 4)
246a86c6181SAlex Tomas 		size = 4;
247a86c6181SAlex Tomas #endif
248a86c6181SAlex Tomas 	return size;
249a86c6181SAlex Tomas }
250a86c6181SAlex Tomas 
251d2a17637SMingming Cao /*
252d2a17637SMingming Cao  * Calculate the number of metadata blocks needed
253d2a17637SMingming Cao  * to allocate @blocks
254d2a17637SMingming Cao  * Worse case is one block per extent
255d2a17637SMingming Cao  */
256d2a17637SMingming Cao int ext4_ext_calc_metadata_amount(struct inode *inode, int blocks)
257d2a17637SMingming Cao {
258d2a17637SMingming Cao 	int lcap, icap, rcap, leafs, idxs, num;
259d2a17637SMingming Cao 	int newextents = blocks;
260d2a17637SMingming Cao 
261d2a17637SMingming Cao 	rcap = ext4_ext_space_root_idx(inode);
262d2a17637SMingming Cao 	lcap = ext4_ext_space_block(inode);
263d2a17637SMingming Cao 	icap = ext4_ext_space_block_idx(inode);
264d2a17637SMingming Cao 
265d2a17637SMingming Cao 	/* number of new leaf blocks needed */
266d2a17637SMingming Cao 	num = leafs = (newextents + lcap - 1) / lcap;
267d2a17637SMingming Cao 
268d2a17637SMingming Cao 	/*
269d2a17637SMingming Cao 	 * Worse case, we need separate index block(s)
270d2a17637SMingming Cao 	 * to link all new leaf blocks
271d2a17637SMingming Cao 	 */
272d2a17637SMingming Cao 	idxs = (leafs + icap - 1) / icap;
273d2a17637SMingming Cao 	do {
274d2a17637SMingming Cao 		num += idxs;
275d2a17637SMingming Cao 		idxs = (idxs + icap - 1) / icap;
276d2a17637SMingming Cao 	} while (idxs > rcap);
277d2a17637SMingming Cao 
278d2a17637SMingming Cao 	return num;
279d2a17637SMingming Cao }
280d2a17637SMingming Cao 
281c29c0ae7SAlex Tomas static int
282c29c0ae7SAlex Tomas ext4_ext_max_entries(struct inode *inode, int depth)
283c29c0ae7SAlex Tomas {
284c29c0ae7SAlex Tomas 	int max;
285c29c0ae7SAlex Tomas 
286c29c0ae7SAlex Tomas 	if (depth == ext_depth(inode)) {
287c29c0ae7SAlex Tomas 		if (depth == 0)
288c29c0ae7SAlex Tomas 			max = ext4_ext_space_root(inode);
289c29c0ae7SAlex Tomas 		else
290c29c0ae7SAlex Tomas 			max = ext4_ext_space_root_idx(inode);
291c29c0ae7SAlex Tomas 	} else {
292c29c0ae7SAlex Tomas 		if (depth == 0)
293c29c0ae7SAlex Tomas 			max = ext4_ext_space_block(inode);
294c29c0ae7SAlex Tomas 		else
295c29c0ae7SAlex Tomas 			max = ext4_ext_space_block_idx(inode);
296c29c0ae7SAlex Tomas 	}
297c29c0ae7SAlex Tomas 
298c29c0ae7SAlex Tomas 	return max;
299c29c0ae7SAlex Tomas }
300c29c0ae7SAlex Tomas 
301c29c0ae7SAlex Tomas static int __ext4_ext_check_header(const char *function, struct inode *inode,
302c29c0ae7SAlex Tomas 					struct ext4_extent_header *eh,
303c29c0ae7SAlex Tomas 					int depth)
304c29c0ae7SAlex Tomas {
305c29c0ae7SAlex Tomas 	const char *error_msg;
306c29c0ae7SAlex Tomas 	int max = 0;
307c29c0ae7SAlex Tomas 
308c29c0ae7SAlex Tomas 	if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
309c29c0ae7SAlex Tomas 		error_msg = "invalid magic";
310c29c0ae7SAlex Tomas 		goto corrupted;
311c29c0ae7SAlex Tomas 	}
312c29c0ae7SAlex Tomas 	if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
313c29c0ae7SAlex Tomas 		error_msg = "unexpected eh_depth";
314c29c0ae7SAlex Tomas 		goto corrupted;
315c29c0ae7SAlex Tomas 	}
316c29c0ae7SAlex Tomas 	if (unlikely(eh->eh_max == 0)) {
317c29c0ae7SAlex Tomas 		error_msg = "invalid eh_max";
318c29c0ae7SAlex Tomas 		goto corrupted;
319c29c0ae7SAlex Tomas 	}
320c29c0ae7SAlex Tomas 	max = ext4_ext_max_entries(inode, depth);
321c29c0ae7SAlex Tomas 	if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
322c29c0ae7SAlex Tomas 		error_msg = "too large eh_max";
323c29c0ae7SAlex Tomas 		goto corrupted;
324c29c0ae7SAlex Tomas 	}
325c29c0ae7SAlex Tomas 	if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
326c29c0ae7SAlex Tomas 		error_msg = "invalid eh_entries";
327c29c0ae7SAlex Tomas 		goto corrupted;
328c29c0ae7SAlex Tomas 	}
329c29c0ae7SAlex Tomas 	return 0;
330c29c0ae7SAlex Tomas 
331c29c0ae7SAlex Tomas corrupted:
332c29c0ae7SAlex Tomas 	ext4_error(inode->i_sb, function,
333c29c0ae7SAlex Tomas 			"bad header in inode #%lu: %s - magic %x, "
334c29c0ae7SAlex Tomas 			"entries %u, max %u(%u), depth %u(%u)",
335c29c0ae7SAlex Tomas 			inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic),
336c29c0ae7SAlex Tomas 			le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
337c29c0ae7SAlex Tomas 			max, le16_to_cpu(eh->eh_depth), depth);
338c29c0ae7SAlex Tomas 
339c29c0ae7SAlex Tomas 	return -EIO;
340c29c0ae7SAlex Tomas }
341c29c0ae7SAlex Tomas 
342c29c0ae7SAlex Tomas #define ext4_ext_check_header(inode, eh, depth)	\
34346e665e9SHarvey Harrison 	__ext4_ext_check_header(__func__, inode, eh, depth)
344c29c0ae7SAlex Tomas 
345a86c6181SAlex Tomas #ifdef EXT_DEBUG
346a86c6181SAlex Tomas static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
347a86c6181SAlex Tomas {
348a86c6181SAlex Tomas 	int k, l = path->p_depth;
349a86c6181SAlex Tomas 
350a86c6181SAlex Tomas 	ext_debug("path:");
351a86c6181SAlex Tomas 	for (k = 0; k <= l; k++, path++) {
352a86c6181SAlex Tomas 		if (path->p_idx) {
3532ae02107SMingming Cao 		  ext_debug("  %d->%llu", le32_to_cpu(path->p_idx->ei_block),
354f65e6fbaSAlex Tomas 			    idx_pblock(path->p_idx));
355a86c6181SAlex Tomas 		} else if (path->p_ext) {
3562ae02107SMingming Cao 			ext_debug("  %d:%d:%llu ",
357a86c6181SAlex Tomas 				  le32_to_cpu(path->p_ext->ee_block),
358a2df2a63SAmit Arora 				  ext4_ext_get_actual_len(path->p_ext),
359f65e6fbaSAlex Tomas 				  ext_pblock(path->p_ext));
360a86c6181SAlex Tomas 		} else
361a86c6181SAlex Tomas 			ext_debug("  []");
362a86c6181SAlex Tomas 	}
363a86c6181SAlex Tomas 	ext_debug("\n");
364a86c6181SAlex Tomas }
365a86c6181SAlex Tomas 
366a86c6181SAlex Tomas static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
367a86c6181SAlex Tomas {
368a86c6181SAlex Tomas 	int depth = ext_depth(inode);
369a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
370a86c6181SAlex Tomas 	struct ext4_extent *ex;
371a86c6181SAlex Tomas 	int i;
372a86c6181SAlex Tomas 
373a86c6181SAlex Tomas 	if (!path)
374a86c6181SAlex Tomas 		return;
375a86c6181SAlex Tomas 
376a86c6181SAlex Tomas 	eh = path[depth].p_hdr;
377a86c6181SAlex Tomas 	ex = EXT_FIRST_EXTENT(eh);
378a86c6181SAlex Tomas 
379a86c6181SAlex Tomas 	for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
3802ae02107SMingming Cao 		ext_debug("%d:%d:%llu ", le32_to_cpu(ex->ee_block),
381a2df2a63SAmit Arora 			  ext4_ext_get_actual_len(ex), ext_pblock(ex));
382a86c6181SAlex Tomas 	}
383a86c6181SAlex Tomas 	ext_debug("\n");
384a86c6181SAlex Tomas }
385a86c6181SAlex Tomas #else
386a86c6181SAlex Tomas #define ext4_ext_show_path(inode,path)
387a86c6181SAlex Tomas #define ext4_ext_show_leaf(inode,path)
388a86c6181SAlex Tomas #endif
389a86c6181SAlex Tomas 
390b35905c1SAneesh Kumar K.V void ext4_ext_drop_refs(struct ext4_ext_path *path)
391a86c6181SAlex Tomas {
392a86c6181SAlex Tomas 	int depth = path->p_depth;
393a86c6181SAlex Tomas 	int i;
394a86c6181SAlex Tomas 
395a86c6181SAlex Tomas 	for (i = 0; i <= depth; i++, path++)
396a86c6181SAlex Tomas 		if (path->p_bh) {
397a86c6181SAlex Tomas 			brelse(path->p_bh);
398a86c6181SAlex Tomas 			path->p_bh = NULL;
399a86c6181SAlex Tomas 		}
400a86c6181SAlex Tomas }
401a86c6181SAlex Tomas 
402a86c6181SAlex Tomas /*
403d0d856e8SRandy Dunlap  * ext4_ext_binsearch_idx:
404d0d856e8SRandy Dunlap  * binary search for the closest index of the given block
405c29c0ae7SAlex Tomas  * the header must be checked before calling this
406a86c6181SAlex Tomas  */
407a86c6181SAlex Tomas static void
408725d26d3SAneesh Kumar K.V ext4_ext_binsearch_idx(struct inode *inode,
409725d26d3SAneesh Kumar K.V 			struct ext4_ext_path *path, ext4_lblk_t block)
410a86c6181SAlex Tomas {
411a86c6181SAlex Tomas 	struct ext4_extent_header *eh = path->p_hdr;
412a86c6181SAlex Tomas 	struct ext4_extent_idx *r, *l, *m;
413a86c6181SAlex Tomas 
414a86c6181SAlex Tomas 
415bba90743SEric Sandeen 	ext_debug("binsearch for %u(idx):  ", block);
416a86c6181SAlex Tomas 
417a86c6181SAlex Tomas 	l = EXT_FIRST_INDEX(eh) + 1;
418e9f410b1SDmitry Monakhov 	r = EXT_LAST_INDEX(eh);
419a86c6181SAlex Tomas 	while (l <= r) {
420a86c6181SAlex Tomas 		m = l + (r - l) / 2;
421a86c6181SAlex Tomas 		if (block < le32_to_cpu(m->ei_block))
422a86c6181SAlex Tomas 			r = m - 1;
423a86c6181SAlex Tomas 		else
424a86c6181SAlex Tomas 			l = m + 1;
42526d535edSDmitry Monakhov 		ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
42626d535edSDmitry Monakhov 				m, le32_to_cpu(m->ei_block),
42726d535edSDmitry Monakhov 				r, le32_to_cpu(r->ei_block));
428a86c6181SAlex Tomas 	}
429a86c6181SAlex Tomas 
430a86c6181SAlex Tomas 	path->p_idx = l - 1;
431f65e6fbaSAlex Tomas 	ext_debug("  -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
43226d535edSDmitry Monakhov 		  idx_pblock(path->p_idx));
433a86c6181SAlex Tomas 
434a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH
435a86c6181SAlex Tomas 	{
436a86c6181SAlex Tomas 		struct ext4_extent_idx *chix, *ix;
437a86c6181SAlex Tomas 		int k;
438a86c6181SAlex Tomas 
439a86c6181SAlex Tomas 		chix = ix = EXT_FIRST_INDEX(eh);
440a86c6181SAlex Tomas 		for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
441a86c6181SAlex Tomas 		  if (k != 0 &&
442a86c6181SAlex Tomas 		      le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
443a86c6181SAlex Tomas 				printk("k=%d, ix=0x%p, first=0x%p\n", k,
444a86c6181SAlex Tomas 					ix, EXT_FIRST_INDEX(eh));
445a86c6181SAlex Tomas 				printk("%u <= %u\n",
446a86c6181SAlex Tomas 				       le32_to_cpu(ix->ei_block),
447a86c6181SAlex Tomas 				       le32_to_cpu(ix[-1].ei_block));
448a86c6181SAlex Tomas 			}
449a86c6181SAlex Tomas 			BUG_ON(k && le32_to_cpu(ix->ei_block)
450a86c6181SAlex Tomas 					   <= le32_to_cpu(ix[-1].ei_block));
451a86c6181SAlex Tomas 			if (block < le32_to_cpu(ix->ei_block))
452a86c6181SAlex Tomas 				break;
453a86c6181SAlex Tomas 			chix = ix;
454a86c6181SAlex Tomas 		}
455a86c6181SAlex Tomas 		BUG_ON(chix != path->p_idx);
456a86c6181SAlex Tomas 	}
457a86c6181SAlex Tomas #endif
458a86c6181SAlex Tomas 
459a86c6181SAlex Tomas }
460a86c6181SAlex Tomas 
461a86c6181SAlex Tomas /*
462d0d856e8SRandy Dunlap  * ext4_ext_binsearch:
463d0d856e8SRandy Dunlap  * binary search for closest extent of the given block
464c29c0ae7SAlex Tomas  * the header must be checked before calling this
465a86c6181SAlex Tomas  */
466a86c6181SAlex Tomas static void
467725d26d3SAneesh Kumar K.V ext4_ext_binsearch(struct inode *inode,
468725d26d3SAneesh Kumar K.V 		struct ext4_ext_path *path, ext4_lblk_t block)
469a86c6181SAlex Tomas {
470a86c6181SAlex Tomas 	struct ext4_extent_header *eh = path->p_hdr;
471a86c6181SAlex Tomas 	struct ext4_extent *r, *l, *m;
472a86c6181SAlex Tomas 
473a86c6181SAlex Tomas 	if (eh->eh_entries == 0) {
474a86c6181SAlex Tomas 		/*
475d0d856e8SRandy Dunlap 		 * this leaf is empty:
476a86c6181SAlex Tomas 		 * we get such a leaf in split/add case
477a86c6181SAlex Tomas 		 */
478a86c6181SAlex Tomas 		return;
479a86c6181SAlex Tomas 	}
480a86c6181SAlex Tomas 
481bba90743SEric Sandeen 	ext_debug("binsearch for %u:  ", block);
482a86c6181SAlex Tomas 
483a86c6181SAlex Tomas 	l = EXT_FIRST_EXTENT(eh) + 1;
484e9f410b1SDmitry Monakhov 	r = EXT_LAST_EXTENT(eh);
485a86c6181SAlex Tomas 
486a86c6181SAlex Tomas 	while (l <= r) {
487a86c6181SAlex Tomas 		m = l + (r - l) / 2;
488a86c6181SAlex Tomas 		if (block < le32_to_cpu(m->ee_block))
489a86c6181SAlex Tomas 			r = m - 1;
490a86c6181SAlex Tomas 		else
491a86c6181SAlex Tomas 			l = m + 1;
49226d535edSDmitry Monakhov 		ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
49326d535edSDmitry Monakhov 				m, le32_to_cpu(m->ee_block),
49426d535edSDmitry Monakhov 				r, le32_to_cpu(r->ee_block));
495a86c6181SAlex Tomas 	}
496a86c6181SAlex Tomas 
497a86c6181SAlex Tomas 	path->p_ext = l - 1;
4982ae02107SMingming Cao 	ext_debug("  -> %d:%llu:%d ",
499a86c6181SAlex Tomas 			le32_to_cpu(path->p_ext->ee_block),
500f65e6fbaSAlex Tomas 			ext_pblock(path->p_ext),
501a2df2a63SAmit Arora 			ext4_ext_get_actual_len(path->p_ext));
502a86c6181SAlex Tomas 
503a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH
504a86c6181SAlex Tomas 	{
505a86c6181SAlex Tomas 		struct ext4_extent *chex, *ex;
506a86c6181SAlex Tomas 		int k;
507a86c6181SAlex Tomas 
508a86c6181SAlex Tomas 		chex = ex = EXT_FIRST_EXTENT(eh);
509a86c6181SAlex Tomas 		for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
510a86c6181SAlex Tomas 			BUG_ON(k && le32_to_cpu(ex->ee_block)
511a86c6181SAlex Tomas 					  <= le32_to_cpu(ex[-1].ee_block));
512a86c6181SAlex Tomas 			if (block < le32_to_cpu(ex->ee_block))
513a86c6181SAlex Tomas 				break;
514a86c6181SAlex Tomas 			chex = ex;
515a86c6181SAlex Tomas 		}
516a86c6181SAlex Tomas 		BUG_ON(chex != path->p_ext);
517a86c6181SAlex Tomas 	}
518a86c6181SAlex Tomas #endif
519a86c6181SAlex Tomas 
520a86c6181SAlex Tomas }
521a86c6181SAlex Tomas 
522a86c6181SAlex Tomas int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
523a86c6181SAlex Tomas {
524a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
525a86c6181SAlex Tomas 
526a86c6181SAlex Tomas 	eh = ext_inode_hdr(inode);
527a86c6181SAlex Tomas 	eh->eh_depth = 0;
528a86c6181SAlex Tomas 	eh->eh_entries = 0;
529a86c6181SAlex Tomas 	eh->eh_magic = EXT4_EXT_MAGIC;
530a86c6181SAlex Tomas 	eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode));
531a86c6181SAlex Tomas 	ext4_mark_inode_dirty(handle, inode);
532a86c6181SAlex Tomas 	ext4_ext_invalidate_cache(inode);
533a86c6181SAlex Tomas 	return 0;
534a86c6181SAlex Tomas }
535a86c6181SAlex Tomas 
536a86c6181SAlex Tomas struct ext4_ext_path *
537725d26d3SAneesh Kumar K.V ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
538725d26d3SAneesh Kumar K.V 					struct ext4_ext_path *path)
539a86c6181SAlex Tomas {
540a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
541a86c6181SAlex Tomas 	struct buffer_head *bh;
542a86c6181SAlex Tomas 	short int depth, i, ppos = 0, alloc = 0;
543a86c6181SAlex Tomas 
544a86c6181SAlex Tomas 	eh = ext_inode_hdr(inode);
545c29c0ae7SAlex Tomas 	depth = ext_depth(inode);
546c29c0ae7SAlex Tomas 	if (ext4_ext_check_header(inode, eh, depth))
547a86c6181SAlex Tomas 		return ERR_PTR(-EIO);
548a86c6181SAlex Tomas 
549a86c6181SAlex Tomas 
550a86c6181SAlex Tomas 	/* account possible depth increase */
551a86c6181SAlex Tomas 	if (!path) {
5525d4958f9SAvantika Mathur 		path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
553a86c6181SAlex Tomas 				GFP_NOFS);
554a86c6181SAlex Tomas 		if (!path)
555a86c6181SAlex Tomas 			return ERR_PTR(-ENOMEM);
556a86c6181SAlex Tomas 		alloc = 1;
557a86c6181SAlex Tomas 	}
558a86c6181SAlex Tomas 	path[0].p_hdr = eh;
5591973adcbSShen Feng 	path[0].p_bh = NULL;
560a86c6181SAlex Tomas 
561c29c0ae7SAlex Tomas 	i = depth;
562a86c6181SAlex Tomas 	/* walk through the tree */
563a86c6181SAlex Tomas 	while (i) {
564a86c6181SAlex Tomas 		ext_debug("depth %d: num %d, max %d\n",
565a86c6181SAlex Tomas 			  ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
566c29c0ae7SAlex Tomas 
567a86c6181SAlex Tomas 		ext4_ext_binsearch_idx(inode, path + ppos, block);
568f65e6fbaSAlex Tomas 		path[ppos].p_block = idx_pblock(path[ppos].p_idx);
569a86c6181SAlex Tomas 		path[ppos].p_depth = i;
570a86c6181SAlex Tomas 		path[ppos].p_ext = NULL;
571a86c6181SAlex Tomas 
572a86c6181SAlex Tomas 		bh = sb_bread(inode->i_sb, path[ppos].p_block);
573a86c6181SAlex Tomas 		if (!bh)
574a86c6181SAlex Tomas 			goto err;
575a86c6181SAlex Tomas 
576a86c6181SAlex Tomas 		eh = ext_block_hdr(bh);
577a86c6181SAlex Tomas 		ppos++;
578a86c6181SAlex Tomas 		BUG_ON(ppos > depth);
579a86c6181SAlex Tomas 		path[ppos].p_bh = bh;
580a86c6181SAlex Tomas 		path[ppos].p_hdr = eh;
581a86c6181SAlex Tomas 		i--;
582a86c6181SAlex Tomas 
583c29c0ae7SAlex Tomas 		if (ext4_ext_check_header(inode, eh, i))
584a86c6181SAlex Tomas 			goto err;
585a86c6181SAlex Tomas 	}
586a86c6181SAlex Tomas 
587a86c6181SAlex Tomas 	path[ppos].p_depth = i;
588a86c6181SAlex Tomas 	path[ppos].p_ext = NULL;
589a86c6181SAlex Tomas 	path[ppos].p_idx = NULL;
590a86c6181SAlex Tomas 
591a86c6181SAlex Tomas 	/* find extent */
592a86c6181SAlex Tomas 	ext4_ext_binsearch(inode, path + ppos, block);
5931973adcbSShen Feng 	/* if not an empty leaf */
5941973adcbSShen Feng 	if (path[ppos].p_ext)
5951973adcbSShen Feng 		path[ppos].p_block = ext_pblock(path[ppos].p_ext);
596a86c6181SAlex Tomas 
597a86c6181SAlex Tomas 	ext4_ext_show_path(inode, path);
598a86c6181SAlex Tomas 
599a86c6181SAlex Tomas 	return path;
600a86c6181SAlex Tomas 
601a86c6181SAlex Tomas err:
602a86c6181SAlex Tomas 	ext4_ext_drop_refs(path);
603a86c6181SAlex Tomas 	if (alloc)
604a86c6181SAlex Tomas 		kfree(path);
605a86c6181SAlex Tomas 	return ERR_PTR(-EIO);
606a86c6181SAlex Tomas }
607a86c6181SAlex Tomas 
608a86c6181SAlex Tomas /*
609d0d856e8SRandy Dunlap  * ext4_ext_insert_index:
610d0d856e8SRandy Dunlap  * insert new index [@logical;@ptr] into the block at @curp;
611d0d856e8SRandy Dunlap  * check where to insert: before @curp or after @curp
612a86c6181SAlex Tomas  */
613a86c6181SAlex Tomas static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
614a86c6181SAlex Tomas 				struct ext4_ext_path *curp,
615f65e6fbaSAlex Tomas 				int logical, ext4_fsblk_t ptr)
616a86c6181SAlex Tomas {
617a86c6181SAlex Tomas 	struct ext4_extent_idx *ix;
618a86c6181SAlex Tomas 	int len, err;
619a86c6181SAlex Tomas 
6207e028976SAvantika Mathur 	err = ext4_ext_get_access(handle, inode, curp);
6217e028976SAvantika Mathur 	if (err)
622a86c6181SAlex Tomas 		return err;
623a86c6181SAlex Tomas 
624a86c6181SAlex Tomas 	BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block));
625a86c6181SAlex Tomas 	len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
626a86c6181SAlex Tomas 	if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
627a86c6181SAlex Tomas 		/* insert after */
628a86c6181SAlex Tomas 		if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
629a86c6181SAlex Tomas 			len = (len - 1) * sizeof(struct ext4_extent_idx);
630a86c6181SAlex Tomas 			len = len < 0 ? 0 : len;
63126d535edSDmitry Monakhov 			ext_debug("insert new index %d after: %llu. "
632a86c6181SAlex Tomas 					"move %d from 0x%p to 0x%p\n",
633a86c6181SAlex Tomas 					logical, ptr, len,
634a86c6181SAlex Tomas 					(curp->p_idx + 1), (curp->p_idx + 2));
635a86c6181SAlex Tomas 			memmove(curp->p_idx + 2, curp->p_idx + 1, len);
636a86c6181SAlex Tomas 		}
637a86c6181SAlex Tomas 		ix = curp->p_idx + 1;
638a86c6181SAlex Tomas 	} else {
639a86c6181SAlex Tomas 		/* insert before */
640a86c6181SAlex Tomas 		len = len * sizeof(struct ext4_extent_idx);
641a86c6181SAlex Tomas 		len = len < 0 ? 0 : len;
64226d535edSDmitry Monakhov 		ext_debug("insert new index %d before: %llu. "
643a86c6181SAlex Tomas 				"move %d from 0x%p to 0x%p\n",
644a86c6181SAlex Tomas 				logical, ptr, len,
645a86c6181SAlex Tomas 				curp->p_idx, (curp->p_idx + 1));
646a86c6181SAlex Tomas 		memmove(curp->p_idx + 1, curp->p_idx, len);
647a86c6181SAlex Tomas 		ix = curp->p_idx;
648a86c6181SAlex Tomas 	}
649a86c6181SAlex Tomas 
650a86c6181SAlex Tomas 	ix->ei_block = cpu_to_le32(logical);
651f65e6fbaSAlex Tomas 	ext4_idx_store_pblock(ix, ptr);
652e8546d06SMarcin Slusarz 	le16_add_cpu(&curp->p_hdr->eh_entries, 1);
653a86c6181SAlex Tomas 
654a86c6181SAlex Tomas 	BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries)
655a86c6181SAlex Tomas 			     > le16_to_cpu(curp->p_hdr->eh_max));
656a86c6181SAlex Tomas 	BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr));
657a86c6181SAlex Tomas 
658a86c6181SAlex Tomas 	err = ext4_ext_dirty(handle, inode, curp);
659a86c6181SAlex Tomas 	ext4_std_error(inode->i_sb, err);
660a86c6181SAlex Tomas 
661a86c6181SAlex Tomas 	return err;
662a86c6181SAlex Tomas }
663a86c6181SAlex Tomas 
664a86c6181SAlex Tomas /*
665d0d856e8SRandy Dunlap  * ext4_ext_split:
666d0d856e8SRandy Dunlap  * inserts new subtree into the path, using free index entry
667d0d856e8SRandy Dunlap  * at depth @at:
668a86c6181SAlex Tomas  * - allocates all needed blocks (new leaf and all intermediate index blocks)
669a86c6181SAlex Tomas  * - makes decision where to split
670d0d856e8SRandy Dunlap  * - moves remaining extents and index entries (right to the split point)
671a86c6181SAlex Tomas  *   into the newly allocated blocks
672d0d856e8SRandy Dunlap  * - initializes subtree
673a86c6181SAlex Tomas  */
674a86c6181SAlex Tomas static int ext4_ext_split(handle_t *handle, struct inode *inode,
675a86c6181SAlex Tomas 				struct ext4_ext_path *path,
676a86c6181SAlex Tomas 				struct ext4_extent *newext, int at)
677a86c6181SAlex Tomas {
678a86c6181SAlex Tomas 	struct buffer_head *bh = NULL;
679a86c6181SAlex Tomas 	int depth = ext_depth(inode);
680a86c6181SAlex Tomas 	struct ext4_extent_header *neh;
681a86c6181SAlex Tomas 	struct ext4_extent_idx *fidx;
682a86c6181SAlex Tomas 	struct ext4_extent *ex;
683a86c6181SAlex Tomas 	int i = at, k, m, a;
684f65e6fbaSAlex Tomas 	ext4_fsblk_t newblock, oldblock;
685a86c6181SAlex Tomas 	__le32 border;
686f65e6fbaSAlex Tomas 	ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
687a86c6181SAlex Tomas 	int err = 0;
688a86c6181SAlex Tomas 
689a86c6181SAlex Tomas 	/* make decision: where to split? */
690d0d856e8SRandy Dunlap 	/* FIXME: now decision is simplest: at current extent */
691a86c6181SAlex Tomas 
692d0d856e8SRandy Dunlap 	/* if current leaf will be split, then we should use
693a86c6181SAlex Tomas 	 * border from split point */
694a86c6181SAlex Tomas 	BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr));
695a86c6181SAlex Tomas 	if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
696a86c6181SAlex Tomas 		border = path[depth].p_ext[1].ee_block;
697d0d856e8SRandy Dunlap 		ext_debug("leaf will be split."
698a86c6181SAlex Tomas 				" next leaf starts at %d\n",
699a86c6181SAlex Tomas 				  le32_to_cpu(border));
700a86c6181SAlex Tomas 	} else {
701a86c6181SAlex Tomas 		border = newext->ee_block;
702a86c6181SAlex Tomas 		ext_debug("leaf will be added."
703a86c6181SAlex Tomas 				" next leaf starts at %d\n",
704a86c6181SAlex Tomas 				le32_to_cpu(border));
705a86c6181SAlex Tomas 	}
706a86c6181SAlex Tomas 
707a86c6181SAlex Tomas 	/*
708d0d856e8SRandy Dunlap 	 * If error occurs, then we break processing
709d0d856e8SRandy Dunlap 	 * and mark filesystem read-only. index won't
710a86c6181SAlex Tomas 	 * be inserted and tree will be in consistent
711d0d856e8SRandy Dunlap 	 * state. Next mount will repair buffers too.
712a86c6181SAlex Tomas 	 */
713a86c6181SAlex Tomas 
714a86c6181SAlex Tomas 	/*
715d0d856e8SRandy Dunlap 	 * Get array to track all allocated blocks.
716d0d856e8SRandy Dunlap 	 * We need this to handle errors and free blocks
717d0d856e8SRandy Dunlap 	 * upon them.
718a86c6181SAlex Tomas 	 */
7195d4958f9SAvantika Mathur 	ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
720a86c6181SAlex Tomas 	if (!ablocks)
721a86c6181SAlex Tomas 		return -ENOMEM;
722a86c6181SAlex Tomas 
723a86c6181SAlex Tomas 	/* allocate all needed blocks */
724a86c6181SAlex Tomas 	ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
725a86c6181SAlex Tomas 	for (a = 0; a < depth - at; a++) {
726654b4908SAneesh Kumar K.V 		newblock = ext4_ext_new_meta_block(handle, inode, path,
727654b4908SAneesh Kumar K.V 						   newext, &err);
728a86c6181SAlex Tomas 		if (newblock == 0)
729a86c6181SAlex Tomas 			goto cleanup;
730a86c6181SAlex Tomas 		ablocks[a] = newblock;
731a86c6181SAlex Tomas 	}
732a86c6181SAlex Tomas 
733a86c6181SAlex Tomas 	/* initialize new leaf */
734a86c6181SAlex Tomas 	newblock = ablocks[--a];
735a86c6181SAlex Tomas 	BUG_ON(newblock == 0);
736a86c6181SAlex Tomas 	bh = sb_getblk(inode->i_sb, newblock);
737a86c6181SAlex Tomas 	if (!bh) {
738a86c6181SAlex Tomas 		err = -EIO;
739a86c6181SAlex Tomas 		goto cleanup;
740a86c6181SAlex Tomas 	}
741a86c6181SAlex Tomas 	lock_buffer(bh);
742a86c6181SAlex Tomas 
7437e028976SAvantika Mathur 	err = ext4_journal_get_create_access(handle, bh);
7447e028976SAvantika Mathur 	if (err)
745a86c6181SAlex Tomas 		goto cleanup;
746a86c6181SAlex Tomas 
747a86c6181SAlex Tomas 	neh = ext_block_hdr(bh);
748a86c6181SAlex Tomas 	neh->eh_entries = 0;
749a86c6181SAlex Tomas 	neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
750a86c6181SAlex Tomas 	neh->eh_magic = EXT4_EXT_MAGIC;
751a86c6181SAlex Tomas 	neh->eh_depth = 0;
752a86c6181SAlex Tomas 	ex = EXT_FIRST_EXTENT(neh);
753a86c6181SAlex Tomas 
754d0d856e8SRandy Dunlap 	/* move remainder of path[depth] to the new leaf */
755a86c6181SAlex Tomas 	BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max);
756a86c6181SAlex Tomas 	/* start copy from next extent */
757a86c6181SAlex Tomas 	/* TODO: we could do it by single memmove */
758a86c6181SAlex Tomas 	m = 0;
759a86c6181SAlex Tomas 	path[depth].p_ext++;
760a86c6181SAlex Tomas 	while (path[depth].p_ext <=
761a86c6181SAlex Tomas 			EXT_MAX_EXTENT(path[depth].p_hdr)) {
7622ae02107SMingming Cao 		ext_debug("move %d:%llu:%d in new leaf %llu\n",
763a86c6181SAlex Tomas 				le32_to_cpu(path[depth].p_ext->ee_block),
764f65e6fbaSAlex Tomas 				ext_pblock(path[depth].p_ext),
765a2df2a63SAmit Arora 				ext4_ext_get_actual_len(path[depth].p_ext),
766a86c6181SAlex Tomas 				newblock);
767a86c6181SAlex Tomas 		/*memmove(ex++, path[depth].p_ext++,
768a86c6181SAlex Tomas 				sizeof(struct ext4_extent));
769a86c6181SAlex Tomas 		neh->eh_entries++;*/
770a86c6181SAlex Tomas 		path[depth].p_ext++;
771a86c6181SAlex Tomas 		m++;
772a86c6181SAlex Tomas 	}
773a86c6181SAlex Tomas 	if (m) {
774a86c6181SAlex Tomas 		memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
775e8546d06SMarcin Slusarz 		le16_add_cpu(&neh->eh_entries, m);
776a86c6181SAlex Tomas 	}
777a86c6181SAlex Tomas 
778a86c6181SAlex Tomas 	set_buffer_uptodate(bh);
779a86c6181SAlex Tomas 	unlock_buffer(bh);
780a86c6181SAlex Tomas 
7817e028976SAvantika Mathur 	err = ext4_journal_dirty_metadata(handle, bh);
7827e028976SAvantika Mathur 	if (err)
783a86c6181SAlex Tomas 		goto cleanup;
784a86c6181SAlex Tomas 	brelse(bh);
785a86c6181SAlex Tomas 	bh = NULL;
786a86c6181SAlex Tomas 
787a86c6181SAlex Tomas 	/* correct old leaf */
788a86c6181SAlex Tomas 	if (m) {
7897e028976SAvantika Mathur 		err = ext4_ext_get_access(handle, inode, path + depth);
7907e028976SAvantika Mathur 		if (err)
791a86c6181SAlex Tomas 			goto cleanup;
792e8546d06SMarcin Slusarz 		le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
7937e028976SAvantika Mathur 		err = ext4_ext_dirty(handle, inode, path + depth);
7947e028976SAvantika Mathur 		if (err)
795a86c6181SAlex Tomas 			goto cleanup;
796a86c6181SAlex Tomas 
797a86c6181SAlex Tomas 	}
798a86c6181SAlex Tomas 
799a86c6181SAlex Tomas 	/* create intermediate indexes */
800a86c6181SAlex Tomas 	k = depth - at - 1;
801a86c6181SAlex Tomas 	BUG_ON(k < 0);
802a86c6181SAlex Tomas 	if (k)
803a86c6181SAlex Tomas 		ext_debug("create %d intermediate indices\n", k);
804a86c6181SAlex Tomas 	/* insert new index into current index block */
805a86c6181SAlex Tomas 	/* current depth stored in i var */
806a86c6181SAlex Tomas 	i = depth - 1;
807a86c6181SAlex Tomas 	while (k--) {
808a86c6181SAlex Tomas 		oldblock = newblock;
809a86c6181SAlex Tomas 		newblock = ablocks[--a];
810bba90743SEric Sandeen 		bh = sb_getblk(inode->i_sb, newblock);
811a86c6181SAlex Tomas 		if (!bh) {
812a86c6181SAlex Tomas 			err = -EIO;
813a86c6181SAlex Tomas 			goto cleanup;
814a86c6181SAlex Tomas 		}
815a86c6181SAlex Tomas 		lock_buffer(bh);
816a86c6181SAlex Tomas 
8177e028976SAvantika Mathur 		err = ext4_journal_get_create_access(handle, bh);
8187e028976SAvantika Mathur 		if (err)
819a86c6181SAlex Tomas 			goto cleanup;
820a86c6181SAlex Tomas 
821a86c6181SAlex Tomas 		neh = ext_block_hdr(bh);
822a86c6181SAlex Tomas 		neh->eh_entries = cpu_to_le16(1);
823a86c6181SAlex Tomas 		neh->eh_magic = EXT4_EXT_MAGIC;
824a86c6181SAlex Tomas 		neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
825a86c6181SAlex Tomas 		neh->eh_depth = cpu_to_le16(depth - i);
826a86c6181SAlex Tomas 		fidx = EXT_FIRST_INDEX(neh);
827a86c6181SAlex Tomas 		fidx->ei_block = border;
828f65e6fbaSAlex Tomas 		ext4_idx_store_pblock(fidx, oldblock);
829a86c6181SAlex Tomas 
830bba90743SEric Sandeen 		ext_debug("int.index at %d (block %llu): %u -> %llu\n",
831bba90743SEric Sandeen 				i, newblock, le32_to_cpu(border), oldblock);
832a86c6181SAlex Tomas 		/* copy indexes */
833a86c6181SAlex Tomas 		m = 0;
834a86c6181SAlex Tomas 		path[i].p_idx++;
835a86c6181SAlex Tomas 
836a86c6181SAlex Tomas 		ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
837a86c6181SAlex Tomas 				EXT_MAX_INDEX(path[i].p_hdr));
838a86c6181SAlex Tomas 		BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) !=
839a86c6181SAlex Tomas 				EXT_LAST_INDEX(path[i].p_hdr));
840a86c6181SAlex Tomas 		while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
84126d535edSDmitry Monakhov 			ext_debug("%d: move %d:%llu in new index %llu\n", i,
842a86c6181SAlex Tomas 					le32_to_cpu(path[i].p_idx->ei_block),
843f65e6fbaSAlex Tomas 					idx_pblock(path[i].p_idx),
844a86c6181SAlex Tomas 					newblock);
845a86c6181SAlex Tomas 			/*memmove(++fidx, path[i].p_idx++,
846a86c6181SAlex Tomas 					sizeof(struct ext4_extent_idx));
847a86c6181SAlex Tomas 			neh->eh_entries++;
848a86c6181SAlex Tomas 			BUG_ON(neh->eh_entries > neh->eh_max);*/
849a86c6181SAlex Tomas 			path[i].p_idx++;
850a86c6181SAlex Tomas 			m++;
851a86c6181SAlex Tomas 		}
852a86c6181SAlex Tomas 		if (m) {
853a86c6181SAlex Tomas 			memmove(++fidx, path[i].p_idx - m,
854a86c6181SAlex Tomas 				sizeof(struct ext4_extent_idx) * m);
855e8546d06SMarcin Slusarz 			le16_add_cpu(&neh->eh_entries, m);
856a86c6181SAlex Tomas 		}
857a86c6181SAlex Tomas 		set_buffer_uptodate(bh);
858a86c6181SAlex Tomas 		unlock_buffer(bh);
859a86c6181SAlex Tomas 
8607e028976SAvantika Mathur 		err = ext4_journal_dirty_metadata(handle, bh);
8617e028976SAvantika Mathur 		if (err)
862a86c6181SAlex Tomas 			goto cleanup;
863a86c6181SAlex Tomas 		brelse(bh);
864a86c6181SAlex Tomas 		bh = NULL;
865a86c6181SAlex Tomas 
866a86c6181SAlex Tomas 		/* correct old index */
867a86c6181SAlex Tomas 		if (m) {
868a86c6181SAlex Tomas 			err = ext4_ext_get_access(handle, inode, path + i);
869a86c6181SAlex Tomas 			if (err)
870a86c6181SAlex Tomas 				goto cleanup;
871e8546d06SMarcin Slusarz 			le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
872a86c6181SAlex Tomas 			err = ext4_ext_dirty(handle, inode, path + i);
873a86c6181SAlex Tomas 			if (err)
874a86c6181SAlex Tomas 				goto cleanup;
875a86c6181SAlex Tomas 		}
876a86c6181SAlex Tomas 
877a86c6181SAlex Tomas 		i--;
878a86c6181SAlex Tomas 	}
879a86c6181SAlex Tomas 
880a86c6181SAlex Tomas 	/* insert new index */
881a86c6181SAlex Tomas 	err = ext4_ext_insert_index(handle, inode, path + at,
882a86c6181SAlex Tomas 				    le32_to_cpu(border), newblock);
883a86c6181SAlex Tomas 
884a86c6181SAlex Tomas cleanup:
885a86c6181SAlex Tomas 	if (bh) {
886a86c6181SAlex Tomas 		if (buffer_locked(bh))
887a86c6181SAlex Tomas 			unlock_buffer(bh);
888a86c6181SAlex Tomas 		brelse(bh);
889a86c6181SAlex Tomas 	}
890a86c6181SAlex Tomas 
891a86c6181SAlex Tomas 	if (err) {
892a86c6181SAlex Tomas 		/* free all allocated blocks in error case */
893a86c6181SAlex Tomas 		for (i = 0; i < depth; i++) {
894a86c6181SAlex Tomas 			if (!ablocks[i])
895a86c6181SAlex Tomas 				continue;
896c9de560dSAlex Tomas 			ext4_free_blocks(handle, inode, ablocks[i], 1, 1);
897a86c6181SAlex Tomas 		}
898a86c6181SAlex Tomas 	}
899a86c6181SAlex Tomas 	kfree(ablocks);
900a86c6181SAlex Tomas 
901a86c6181SAlex Tomas 	return err;
902a86c6181SAlex Tomas }
903a86c6181SAlex Tomas 
904a86c6181SAlex Tomas /*
905d0d856e8SRandy Dunlap  * ext4_ext_grow_indepth:
906d0d856e8SRandy Dunlap  * implements tree growing procedure:
907a86c6181SAlex Tomas  * - allocates new block
908a86c6181SAlex Tomas  * - moves top-level data (index block or leaf) into the new block
909d0d856e8SRandy Dunlap  * - initializes new top-level, creating index that points to the
910a86c6181SAlex Tomas  *   just created block
911a86c6181SAlex Tomas  */
912a86c6181SAlex Tomas static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
913a86c6181SAlex Tomas 					struct ext4_ext_path *path,
914a86c6181SAlex Tomas 					struct ext4_extent *newext)
915a86c6181SAlex Tomas {
916a86c6181SAlex Tomas 	struct ext4_ext_path *curp = path;
917a86c6181SAlex Tomas 	struct ext4_extent_header *neh;
918a86c6181SAlex Tomas 	struct ext4_extent_idx *fidx;
919a86c6181SAlex Tomas 	struct buffer_head *bh;
920f65e6fbaSAlex Tomas 	ext4_fsblk_t newblock;
921a86c6181SAlex Tomas 	int err = 0;
922a86c6181SAlex Tomas 
923654b4908SAneesh Kumar K.V 	newblock = ext4_ext_new_meta_block(handle, inode, path, newext, &err);
924a86c6181SAlex Tomas 	if (newblock == 0)
925a86c6181SAlex Tomas 		return err;
926a86c6181SAlex Tomas 
927a86c6181SAlex Tomas 	bh = sb_getblk(inode->i_sb, newblock);
928a86c6181SAlex Tomas 	if (!bh) {
929a86c6181SAlex Tomas 		err = -EIO;
930a86c6181SAlex Tomas 		ext4_std_error(inode->i_sb, err);
931a86c6181SAlex Tomas 		return err;
932a86c6181SAlex Tomas 	}
933a86c6181SAlex Tomas 	lock_buffer(bh);
934a86c6181SAlex Tomas 
9357e028976SAvantika Mathur 	err = ext4_journal_get_create_access(handle, bh);
9367e028976SAvantika Mathur 	if (err) {
937a86c6181SAlex Tomas 		unlock_buffer(bh);
938a86c6181SAlex Tomas 		goto out;
939a86c6181SAlex Tomas 	}
940a86c6181SAlex Tomas 
941a86c6181SAlex Tomas 	/* move top-level index/leaf into new block */
942a86c6181SAlex Tomas 	memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
943a86c6181SAlex Tomas 
944a86c6181SAlex Tomas 	/* set size of new block */
945a86c6181SAlex Tomas 	neh = ext_block_hdr(bh);
946a86c6181SAlex Tomas 	/* old root could have indexes or leaves
947a86c6181SAlex Tomas 	 * so calculate e_max right way */
948a86c6181SAlex Tomas 	if (ext_depth(inode))
949a86c6181SAlex Tomas 	  neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
950a86c6181SAlex Tomas 	else
951a86c6181SAlex Tomas 	  neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
952a86c6181SAlex Tomas 	neh->eh_magic = EXT4_EXT_MAGIC;
953a86c6181SAlex Tomas 	set_buffer_uptodate(bh);
954a86c6181SAlex Tomas 	unlock_buffer(bh);
955a86c6181SAlex Tomas 
9567e028976SAvantika Mathur 	err = ext4_journal_dirty_metadata(handle, bh);
9577e028976SAvantika Mathur 	if (err)
958a86c6181SAlex Tomas 		goto out;
959a86c6181SAlex Tomas 
960a86c6181SAlex Tomas 	/* create index in new top-level index: num,max,pointer */
9617e028976SAvantika Mathur 	err = ext4_ext_get_access(handle, inode, curp);
9627e028976SAvantika Mathur 	if (err)
963a86c6181SAlex Tomas 		goto out;
964a86c6181SAlex Tomas 
965a86c6181SAlex Tomas 	curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
966a86c6181SAlex Tomas 	curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode));
967a86c6181SAlex Tomas 	curp->p_hdr->eh_entries = cpu_to_le16(1);
968a86c6181SAlex Tomas 	curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
969e9f410b1SDmitry Monakhov 
970e9f410b1SDmitry Monakhov 	if (path[0].p_hdr->eh_depth)
971e9f410b1SDmitry Monakhov 		curp->p_idx->ei_block =
972e9f410b1SDmitry Monakhov 			EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
973e9f410b1SDmitry Monakhov 	else
974e9f410b1SDmitry Monakhov 		curp->p_idx->ei_block =
975e9f410b1SDmitry Monakhov 			EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
976f65e6fbaSAlex Tomas 	ext4_idx_store_pblock(curp->p_idx, newblock);
977a86c6181SAlex Tomas 
978a86c6181SAlex Tomas 	neh = ext_inode_hdr(inode);
979a86c6181SAlex Tomas 	fidx = EXT_FIRST_INDEX(neh);
9802ae02107SMingming Cao 	ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
981a86c6181SAlex Tomas 		  le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
982f65e6fbaSAlex Tomas 		  le32_to_cpu(fidx->ei_block), idx_pblock(fidx));
983a86c6181SAlex Tomas 
984a86c6181SAlex Tomas 	neh->eh_depth = cpu_to_le16(path->p_depth + 1);
985a86c6181SAlex Tomas 	err = ext4_ext_dirty(handle, inode, curp);
986a86c6181SAlex Tomas out:
987a86c6181SAlex Tomas 	brelse(bh);
988a86c6181SAlex Tomas 
989a86c6181SAlex Tomas 	return err;
990a86c6181SAlex Tomas }
991a86c6181SAlex Tomas 
992a86c6181SAlex Tomas /*
993d0d856e8SRandy Dunlap  * ext4_ext_create_new_leaf:
994d0d856e8SRandy Dunlap  * finds empty index and adds new leaf.
995d0d856e8SRandy Dunlap  * if no free index is found, then it requests in-depth growing.
996a86c6181SAlex Tomas  */
997a86c6181SAlex Tomas static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
998a86c6181SAlex Tomas 					struct ext4_ext_path *path,
999a86c6181SAlex Tomas 					struct ext4_extent *newext)
1000a86c6181SAlex Tomas {
1001a86c6181SAlex Tomas 	struct ext4_ext_path *curp;
1002a86c6181SAlex Tomas 	int depth, i, err = 0;
1003a86c6181SAlex Tomas 
1004a86c6181SAlex Tomas repeat:
1005a86c6181SAlex Tomas 	i = depth = ext_depth(inode);
1006a86c6181SAlex Tomas 
1007a86c6181SAlex Tomas 	/* walk up to the tree and look for free index entry */
1008a86c6181SAlex Tomas 	curp = path + depth;
1009a86c6181SAlex Tomas 	while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1010a86c6181SAlex Tomas 		i--;
1011a86c6181SAlex Tomas 		curp--;
1012a86c6181SAlex Tomas 	}
1013a86c6181SAlex Tomas 
1014d0d856e8SRandy Dunlap 	/* we use already allocated block for index block,
1015d0d856e8SRandy Dunlap 	 * so subsequent data blocks should be contiguous */
1016a86c6181SAlex Tomas 	if (EXT_HAS_FREE_INDEX(curp)) {
1017a86c6181SAlex Tomas 		/* if we found index with free entry, then use that
1018a86c6181SAlex Tomas 		 * entry: create all needed subtree and add new leaf */
1019a86c6181SAlex Tomas 		err = ext4_ext_split(handle, inode, path, newext, i);
1020787e0981SShen Feng 		if (err)
1021787e0981SShen Feng 			goto out;
1022a86c6181SAlex Tomas 
1023a86c6181SAlex Tomas 		/* refill path */
1024a86c6181SAlex Tomas 		ext4_ext_drop_refs(path);
1025a86c6181SAlex Tomas 		path = ext4_ext_find_extent(inode,
1026725d26d3SAneesh Kumar K.V 				    (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1027a86c6181SAlex Tomas 				    path);
1028a86c6181SAlex Tomas 		if (IS_ERR(path))
1029a86c6181SAlex Tomas 			err = PTR_ERR(path);
1030a86c6181SAlex Tomas 	} else {
1031a86c6181SAlex Tomas 		/* tree is full, time to grow in depth */
1032a86c6181SAlex Tomas 		err = ext4_ext_grow_indepth(handle, inode, path, newext);
1033a86c6181SAlex Tomas 		if (err)
1034a86c6181SAlex Tomas 			goto out;
1035a86c6181SAlex Tomas 
1036a86c6181SAlex Tomas 		/* refill path */
1037a86c6181SAlex Tomas 		ext4_ext_drop_refs(path);
1038a86c6181SAlex Tomas 		path = ext4_ext_find_extent(inode,
1039725d26d3SAneesh Kumar K.V 				   (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1040a86c6181SAlex Tomas 				    path);
1041a86c6181SAlex Tomas 		if (IS_ERR(path)) {
1042a86c6181SAlex Tomas 			err = PTR_ERR(path);
1043a86c6181SAlex Tomas 			goto out;
1044a86c6181SAlex Tomas 		}
1045a86c6181SAlex Tomas 
1046a86c6181SAlex Tomas 		/*
1047d0d856e8SRandy Dunlap 		 * only first (depth 0 -> 1) produces free space;
1048d0d856e8SRandy Dunlap 		 * in all other cases we have to split the grown tree
1049a86c6181SAlex Tomas 		 */
1050a86c6181SAlex Tomas 		depth = ext_depth(inode);
1051a86c6181SAlex Tomas 		if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
1052d0d856e8SRandy Dunlap 			/* now we need to split */
1053a86c6181SAlex Tomas 			goto repeat;
1054a86c6181SAlex Tomas 		}
1055a86c6181SAlex Tomas 	}
1056a86c6181SAlex Tomas 
1057a86c6181SAlex Tomas out:
1058a86c6181SAlex Tomas 	return err;
1059a86c6181SAlex Tomas }
1060a86c6181SAlex Tomas 
1061a86c6181SAlex Tomas /*
10621988b51eSAlex Tomas  * search the closest allocated block to the left for *logical
10631988b51eSAlex Tomas  * and returns it at @logical + it's physical address at @phys
10641988b51eSAlex Tomas  * if *logical is the smallest allocated block, the function
10651988b51eSAlex Tomas  * returns 0 at @phys
10661988b51eSAlex Tomas  * return value contains 0 (success) or error code
10671988b51eSAlex Tomas  */
10681988b51eSAlex Tomas int
10691988b51eSAlex Tomas ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path,
10701988b51eSAlex Tomas 			ext4_lblk_t *logical, ext4_fsblk_t *phys)
10711988b51eSAlex Tomas {
10721988b51eSAlex Tomas 	struct ext4_extent_idx *ix;
10731988b51eSAlex Tomas 	struct ext4_extent *ex;
1074b939e376SAneesh Kumar K.V 	int depth, ee_len;
10751988b51eSAlex Tomas 
10761988b51eSAlex Tomas 	BUG_ON(path == NULL);
10771988b51eSAlex Tomas 	depth = path->p_depth;
10781988b51eSAlex Tomas 	*phys = 0;
10791988b51eSAlex Tomas 
10801988b51eSAlex Tomas 	if (depth == 0 && path->p_ext == NULL)
10811988b51eSAlex Tomas 		return 0;
10821988b51eSAlex Tomas 
10831988b51eSAlex Tomas 	/* usually extent in the path covers blocks smaller
10841988b51eSAlex Tomas 	 * then *logical, but it can be that extent is the
10851988b51eSAlex Tomas 	 * first one in the file */
10861988b51eSAlex Tomas 
10871988b51eSAlex Tomas 	ex = path[depth].p_ext;
1088b939e376SAneesh Kumar K.V 	ee_len = ext4_ext_get_actual_len(ex);
10891988b51eSAlex Tomas 	if (*logical < le32_to_cpu(ex->ee_block)) {
10901988b51eSAlex Tomas 		BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
10911988b51eSAlex Tomas 		while (--depth >= 0) {
10921988b51eSAlex Tomas 			ix = path[depth].p_idx;
10931988b51eSAlex Tomas 			BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
10941988b51eSAlex Tomas 		}
10951988b51eSAlex Tomas 		return 0;
10961988b51eSAlex Tomas 	}
10971988b51eSAlex Tomas 
1098b939e376SAneesh Kumar K.V 	BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
10991988b51eSAlex Tomas 
1100b939e376SAneesh Kumar K.V 	*logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
1101b939e376SAneesh Kumar K.V 	*phys = ext_pblock(ex) + ee_len - 1;
11021988b51eSAlex Tomas 	return 0;
11031988b51eSAlex Tomas }
11041988b51eSAlex Tomas 
11051988b51eSAlex Tomas /*
11061988b51eSAlex Tomas  * search the closest allocated block to the right for *logical
11071988b51eSAlex Tomas  * and returns it at @logical + it's physical address at @phys
11081988b51eSAlex Tomas  * if *logical is the smallest allocated block, the function
11091988b51eSAlex Tomas  * returns 0 at @phys
11101988b51eSAlex Tomas  * return value contains 0 (success) or error code
11111988b51eSAlex Tomas  */
11121988b51eSAlex Tomas int
11131988b51eSAlex Tomas ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path,
11141988b51eSAlex Tomas 			ext4_lblk_t *logical, ext4_fsblk_t *phys)
11151988b51eSAlex Tomas {
11161988b51eSAlex Tomas 	struct buffer_head *bh = NULL;
11171988b51eSAlex Tomas 	struct ext4_extent_header *eh;
11181988b51eSAlex Tomas 	struct ext4_extent_idx *ix;
11191988b51eSAlex Tomas 	struct ext4_extent *ex;
11201988b51eSAlex Tomas 	ext4_fsblk_t block;
1121b939e376SAneesh Kumar K.V 	int depth, ee_len;
11221988b51eSAlex Tomas 
11231988b51eSAlex Tomas 	BUG_ON(path == NULL);
11241988b51eSAlex Tomas 	depth = path->p_depth;
11251988b51eSAlex Tomas 	*phys = 0;
11261988b51eSAlex Tomas 
11271988b51eSAlex Tomas 	if (depth == 0 && path->p_ext == NULL)
11281988b51eSAlex Tomas 		return 0;
11291988b51eSAlex Tomas 
11301988b51eSAlex Tomas 	/* usually extent in the path covers blocks smaller
11311988b51eSAlex Tomas 	 * then *logical, but it can be that extent is the
11321988b51eSAlex Tomas 	 * first one in the file */
11331988b51eSAlex Tomas 
11341988b51eSAlex Tomas 	ex = path[depth].p_ext;
1135b939e376SAneesh Kumar K.V 	ee_len = ext4_ext_get_actual_len(ex);
11361988b51eSAlex Tomas 	if (*logical < le32_to_cpu(ex->ee_block)) {
11371988b51eSAlex Tomas 		BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
11381988b51eSAlex Tomas 		while (--depth >= 0) {
11391988b51eSAlex Tomas 			ix = path[depth].p_idx;
11401988b51eSAlex Tomas 			BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
11411988b51eSAlex Tomas 		}
11421988b51eSAlex Tomas 		*logical = le32_to_cpu(ex->ee_block);
11431988b51eSAlex Tomas 		*phys = ext_pblock(ex);
11441988b51eSAlex Tomas 		return 0;
11451988b51eSAlex Tomas 	}
11461988b51eSAlex Tomas 
1147b939e376SAneesh Kumar K.V 	BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
11481988b51eSAlex Tomas 
11491988b51eSAlex Tomas 	if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
11501988b51eSAlex Tomas 		/* next allocated block in this leaf */
11511988b51eSAlex Tomas 		ex++;
11521988b51eSAlex Tomas 		*logical = le32_to_cpu(ex->ee_block);
11531988b51eSAlex Tomas 		*phys = ext_pblock(ex);
11541988b51eSAlex Tomas 		return 0;
11551988b51eSAlex Tomas 	}
11561988b51eSAlex Tomas 
11571988b51eSAlex Tomas 	/* go up and search for index to the right */
11581988b51eSAlex Tomas 	while (--depth >= 0) {
11591988b51eSAlex Tomas 		ix = path[depth].p_idx;
11601988b51eSAlex Tomas 		if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
11611988b51eSAlex Tomas 			break;
11621988b51eSAlex Tomas 	}
11631988b51eSAlex Tomas 
11641988b51eSAlex Tomas 	if (depth < 0) {
11651988b51eSAlex Tomas 		/* we've gone up to the root and
11661988b51eSAlex Tomas 		 * found no index to the right */
11671988b51eSAlex Tomas 		return 0;
11681988b51eSAlex Tomas 	}
11691988b51eSAlex Tomas 
11701988b51eSAlex Tomas 	/* we've found index to the right, let's
11711988b51eSAlex Tomas 	 * follow it and find the closest allocated
11721988b51eSAlex Tomas 	 * block to the right */
11731988b51eSAlex Tomas 	ix++;
11741988b51eSAlex Tomas 	block = idx_pblock(ix);
11751988b51eSAlex Tomas 	while (++depth < path->p_depth) {
11761988b51eSAlex Tomas 		bh = sb_bread(inode->i_sb, block);
11771988b51eSAlex Tomas 		if (bh == NULL)
11781988b51eSAlex Tomas 			return -EIO;
11791988b51eSAlex Tomas 		eh = ext_block_hdr(bh);
11801988b51eSAlex Tomas 		if (ext4_ext_check_header(inode, eh, depth)) {
11811988b51eSAlex Tomas 			put_bh(bh);
11821988b51eSAlex Tomas 			return -EIO;
11831988b51eSAlex Tomas 		}
11841988b51eSAlex Tomas 		ix = EXT_FIRST_INDEX(eh);
11851988b51eSAlex Tomas 		block = idx_pblock(ix);
11861988b51eSAlex Tomas 		put_bh(bh);
11871988b51eSAlex Tomas 	}
11881988b51eSAlex Tomas 
11891988b51eSAlex Tomas 	bh = sb_bread(inode->i_sb, block);
11901988b51eSAlex Tomas 	if (bh == NULL)
11911988b51eSAlex Tomas 		return -EIO;
11921988b51eSAlex Tomas 	eh = ext_block_hdr(bh);
11931988b51eSAlex Tomas 	if (ext4_ext_check_header(inode, eh, path->p_depth - depth)) {
11941988b51eSAlex Tomas 		put_bh(bh);
11951988b51eSAlex Tomas 		return -EIO;
11961988b51eSAlex Tomas 	}
11971988b51eSAlex Tomas 	ex = EXT_FIRST_EXTENT(eh);
11981988b51eSAlex Tomas 	*logical = le32_to_cpu(ex->ee_block);
11991988b51eSAlex Tomas 	*phys = ext_pblock(ex);
12001988b51eSAlex Tomas 	put_bh(bh);
12011988b51eSAlex Tomas 	return 0;
12021988b51eSAlex Tomas 
12031988b51eSAlex Tomas }
12041988b51eSAlex Tomas 
12051988b51eSAlex Tomas /*
1206d0d856e8SRandy Dunlap  * ext4_ext_next_allocated_block:
1207d0d856e8SRandy Dunlap  * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1208d0d856e8SRandy Dunlap  * NOTE: it considers block number from index entry as
1209d0d856e8SRandy Dunlap  * allocated block. Thus, index entries have to be consistent
1210d0d856e8SRandy Dunlap  * with leaves.
1211a86c6181SAlex Tomas  */
1212725d26d3SAneesh Kumar K.V static ext4_lblk_t
1213a86c6181SAlex Tomas ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1214a86c6181SAlex Tomas {
1215a86c6181SAlex Tomas 	int depth;
1216a86c6181SAlex Tomas 
1217a86c6181SAlex Tomas 	BUG_ON(path == NULL);
1218a86c6181SAlex Tomas 	depth = path->p_depth;
1219a86c6181SAlex Tomas 
1220a86c6181SAlex Tomas 	if (depth == 0 && path->p_ext == NULL)
1221a86c6181SAlex Tomas 		return EXT_MAX_BLOCK;
1222a86c6181SAlex Tomas 
1223a86c6181SAlex Tomas 	while (depth >= 0) {
1224a86c6181SAlex Tomas 		if (depth == path->p_depth) {
1225a86c6181SAlex Tomas 			/* leaf */
1226a86c6181SAlex Tomas 			if (path[depth].p_ext !=
1227a86c6181SAlex Tomas 					EXT_LAST_EXTENT(path[depth].p_hdr))
1228a86c6181SAlex Tomas 			  return le32_to_cpu(path[depth].p_ext[1].ee_block);
1229a86c6181SAlex Tomas 		} else {
1230a86c6181SAlex Tomas 			/* index */
1231a86c6181SAlex Tomas 			if (path[depth].p_idx !=
1232a86c6181SAlex Tomas 					EXT_LAST_INDEX(path[depth].p_hdr))
1233a86c6181SAlex Tomas 			  return le32_to_cpu(path[depth].p_idx[1].ei_block);
1234a86c6181SAlex Tomas 		}
1235a86c6181SAlex Tomas 		depth--;
1236a86c6181SAlex Tomas 	}
1237a86c6181SAlex Tomas 
1238a86c6181SAlex Tomas 	return EXT_MAX_BLOCK;
1239a86c6181SAlex Tomas }
1240a86c6181SAlex Tomas 
1241a86c6181SAlex Tomas /*
1242d0d856e8SRandy Dunlap  * ext4_ext_next_leaf_block:
1243a86c6181SAlex Tomas  * returns first allocated block from next leaf or EXT_MAX_BLOCK
1244a86c6181SAlex Tomas  */
1245725d26d3SAneesh Kumar K.V static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode,
1246a86c6181SAlex Tomas 					struct ext4_ext_path *path)
1247a86c6181SAlex Tomas {
1248a86c6181SAlex Tomas 	int depth;
1249a86c6181SAlex Tomas 
1250a86c6181SAlex Tomas 	BUG_ON(path == NULL);
1251a86c6181SAlex Tomas 	depth = path->p_depth;
1252a86c6181SAlex Tomas 
1253a86c6181SAlex Tomas 	/* zero-tree has no leaf blocks at all */
1254a86c6181SAlex Tomas 	if (depth == 0)
1255a86c6181SAlex Tomas 		return EXT_MAX_BLOCK;
1256a86c6181SAlex Tomas 
1257a86c6181SAlex Tomas 	/* go to index block */
1258a86c6181SAlex Tomas 	depth--;
1259a86c6181SAlex Tomas 
1260a86c6181SAlex Tomas 	while (depth >= 0) {
1261a86c6181SAlex Tomas 		if (path[depth].p_idx !=
1262a86c6181SAlex Tomas 				EXT_LAST_INDEX(path[depth].p_hdr))
1263725d26d3SAneesh Kumar K.V 			return (ext4_lblk_t)
1264725d26d3SAneesh Kumar K.V 				le32_to_cpu(path[depth].p_idx[1].ei_block);
1265a86c6181SAlex Tomas 		depth--;
1266a86c6181SAlex Tomas 	}
1267a86c6181SAlex Tomas 
1268a86c6181SAlex Tomas 	return EXT_MAX_BLOCK;
1269a86c6181SAlex Tomas }
1270a86c6181SAlex Tomas 
1271a86c6181SAlex Tomas /*
1272d0d856e8SRandy Dunlap  * ext4_ext_correct_indexes:
1273d0d856e8SRandy Dunlap  * if leaf gets modified and modified extent is first in the leaf,
1274d0d856e8SRandy Dunlap  * then we have to correct all indexes above.
1275a86c6181SAlex Tomas  * TODO: do we need to correct tree in all cases?
1276a86c6181SAlex Tomas  */
12771d03ec98SAneesh Kumar K.V static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
1278a86c6181SAlex Tomas 				struct ext4_ext_path *path)
1279a86c6181SAlex Tomas {
1280a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
1281a86c6181SAlex Tomas 	int depth = ext_depth(inode);
1282a86c6181SAlex Tomas 	struct ext4_extent *ex;
1283a86c6181SAlex Tomas 	__le32 border;
1284a86c6181SAlex Tomas 	int k, err = 0;
1285a86c6181SAlex Tomas 
1286a86c6181SAlex Tomas 	eh = path[depth].p_hdr;
1287a86c6181SAlex Tomas 	ex = path[depth].p_ext;
1288a86c6181SAlex Tomas 	BUG_ON(ex == NULL);
1289a86c6181SAlex Tomas 	BUG_ON(eh == NULL);
1290a86c6181SAlex Tomas 
1291a86c6181SAlex Tomas 	if (depth == 0) {
1292a86c6181SAlex Tomas 		/* there is no tree at all */
1293a86c6181SAlex Tomas 		return 0;
1294a86c6181SAlex Tomas 	}
1295a86c6181SAlex Tomas 
1296a86c6181SAlex Tomas 	if (ex != EXT_FIRST_EXTENT(eh)) {
1297a86c6181SAlex Tomas 		/* we correct tree if first leaf got modified only */
1298a86c6181SAlex Tomas 		return 0;
1299a86c6181SAlex Tomas 	}
1300a86c6181SAlex Tomas 
1301a86c6181SAlex Tomas 	/*
1302d0d856e8SRandy Dunlap 	 * TODO: we need correction if border is smaller than current one
1303a86c6181SAlex Tomas 	 */
1304a86c6181SAlex Tomas 	k = depth - 1;
1305a86c6181SAlex Tomas 	border = path[depth].p_ext->ee_block;
13067e028976SAvantika Mathur 	err = ext4_ext_get_access(handle, inode, path + k);
13077e028976SAvantika Mathur 	if (err)
1308a86c6181SAlex Tomas 		return err;
1309a86c6181SAlex Tomas 	path[k].p_idx->ei_block = border;
13107e028976SAvantika Mathur 	err = ext4_ext_dirty(handle, inode, path + k);
13117e028976SAvantika Mathur 	if (err)
1312a86c6181SAlex Tomas 		return err;
1313a86c6181SAlex Tomas 
1314a86c6181SAlex Tomas 	while (k--) {
1315a86c6181SAlex Tomas 		/* change all left-side indexes */
1316a86c6181SAlex Tomas 		if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1317a86c6181SAlex Tomas 			break;
13187e028976SAvantika Mathur 		err = ext4_ext_get_access(handle, inode, path + k);
13197e028976SAvantika Mathur 		if (err)
1320a86c6181SAlex Tomas 			break;
1321a86c6181SAlex Tomas 		path[k].p_idx->ei_block = border;
13227e028976SAvantika Mathur 		err = ext4_ext_dirty(handle, inode, path + k);
13237e028976SAvantika Mathur 		if (err)
1324a86c6181SAlex Tomas 			break;
1325a86c6181SAlex Tomas 	}
1326a86c6181SAlex Tomas 
1327a86c6181SAlex Tomas 	return err;
1328a86c6181SAlex Tomas }
1329a86c6181SAlex Tomas 
133009b88252SAvantika Mathur static int
1331a86c6181SAlex Tomas ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1332a86c6181SAlex Tomas 				struct ext4_extent *ex2)
1333a86c6181SAlex Tomas {
1334749269faSAmit Arora 	unsigned short ext1_ee_len, ext2_ee_len, max_len;
1335a2df2a63SAmit Arora 
1336a2df2a63SAmit Arora 	/*
1337a2df2a63SAmit Arora 	 * Make sure that either both extents are uninitialized, or
1338a2df2a63SAmit Arora 	 * both are _not_.
1339a2df2a63SAmit Arora 	 */
1340a2df2a63SAmit Arora 	if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1341a2df2a63SAmit Arora 		return 0;
1342a2df2a63SAmit Arora 
1343749269faSAmit Arora 	if (ext4_ext_is_uninitialized(ex1))
1344749269faSAmit Arora 		max_len = EXT_UNINIT_MAX_LEN;
1345749269faSAmit Arora 	else
1346749269faSAmit Arora 		max_len = EXT_INIT_MAX_LEN;
1347749269faSAmit Arora 
1348a2df2a63SAmit Arora 	ext1_ee_len = ext4_ext_get_actual_len(ex1);
1349a2df2a63SAmit Arora 	ext2_ee_len = ext4_ext_get_actual_len(ex2);
1350a2df2a63SAmit Arora 
1351a2df2a63SAmit Arora 	if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
135263f57933SAndrew Morton 			le32_to_cpu(ex2->ee_block))
1353a86c6181SAlex Tomas 		return 0;
1354a86c6181SAlex Tomas 
1355471d4011SSuparna Bhattacharya 	/*
1356471d4011SSuparna Bhattacharya 	 * To allow future support for preallocated extents to be added
1357471d4011SSuparna Bhattacharya 	 * as an RO_COMPAT feature, refuse to merge to extents if
1358d0d856e8SRandy Dunlap 	 * this can result in the top bit of ee_len being set.
1359471d4011SSuparna Bhattacharya 	 */
1360749269faSAmit Arora 	if (ext1_ee_len + ext2_ee_len > max_len)
1361471d4011SSuparna Bhattacharya 		return 0;
1362bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST
1363b939e376SAneesh Kumar K.V 	if (ext1_ee_len >= 4)
1364a86c6181SAlex Tomas 		return 0;
1365a86c6181SAlex Tomas #endif
1366a86c6181SAlex Tomas 
1367a2df2a63SAmit Arora 	if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2))
1368a86c6181SAlex Tomas 		return 1;
1369a86c6181SAlex Tomas 	return 0;
1370a86c6181SAlex Tomas }
1371a86c6181SAlex Tomas 
1372a86c6181SAlex Tomas /*
137356055d3aSAmit Arora  * This function tries to merge the "ex" extent to the next extent in the tree.
137456055d3aSAmit Arora  * It always tries to merge towards right. If you want to merge towards
137556055d3aSAmit Arora  * left, pass "ex - 1" as argument instead of "ex".
137656055d3aSAmit Arora  * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
137756055d3aSAmit Arora  * 1 if they got merged.
137856055d3aSAmit Arora  */
137956055d3aSAmit Arora int ext4_ext_try_to_merge(struct inode *inode,
138056055d3aSAmit Arora 			  struct ext4_ext_path *path,
138156055d3aSAmit Arora 			  struct ext4_extent *ex)
138256055d3aSAmit Arora {
138356055d3aSAmit Arora 	struct ext4_extent_header *eh;
138456055d3aSAmit Arora 	unsigned int depth, len;
138556055d3aSAmit Arora 	int merge_done = 0;
138656055d3aSAmit Arora 	int uninitialized = 0;
138756055d3aSAmit Arora 
138856055d3aSAmit Arora 	depth = ext_depth(inode);
138956055d3aSAmit Arora 	BUG_ON(path[depth].p_hdr == NULL);
139056055d3aSAmit Arora 	eh = path[depth].p_hdr;
139156055d3aSAmit Arora 
139256055d3aSAmit Arora 	while (ex < EXT_LAST_EXTENT(eh)) {
139356055d3aSAmit Arora 		if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
139456055d3aSAmit Arora 			break;
139556055d3aSAmit Arora 		/* merge with next extent! */
139656055d3aSAmit Arora 		if (ext4_ext_is_uninitialized(ex))
139756055d3aSAmit Arora 			uninitialized = 1;
139856055d3aSAmit Arora 		ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
139956055d3aSAmit Arora 				+ ext4_ext_get_actual_len(ex + 1));
140056055d3aSAmit Arora 		if (uninitialized)
140156055d3aSAmit Arora 			ext4_ext_mark_uninitialized(ex);
140256055d3aSAmit Arora 
140356055d3aSAmit Arora 		if (ex + 1 < EXT_LAST_EXTENT(eh)) {
140456055d3aSAmit Arora 			len = (EXT_LAST_EXTENT(eh) - ex - 1)
140556055d3aSAmit Arora 				* sizeof(struct ext4_extent);
140656055d3aSAmit Arora 			memmove(ex + 1, ex + 2, len);
140756055d3aSAmit Arora 		}
1408e8546d06SMarcin Slusarz 		le16_add_cpu(&eh->eh_entries, -1);
140956055d3aSAmit Arora 		merge_done = 1;
141056055d3aSAmit Arora 		WARN_ON(eh->eh_entries == 0);
141156055d3aSAmit Arora 		if (!eh->eh_entries)
141256055d3aSAmit Arora 			ext4_error(inode->i_sb, "ext4_ext_try_to_merge",
141356055d3aSAmit Arora 			   "inode#%lu, eh->eh_entries = 0!", inode->i_ino);
141456055d3aSAmit Arora 	}
141556055d3aSAmit Arora 
141656055d3aSAmit Arora 	return merge_done;
141756055d3aSAmit Arora }
141856055d3aSAmit Arora 
141956055d3aSAmit Arora /*
142025d14f98SAmit Arora  * check if a portion of the "newext" extent overlaps with an
142125d14f98SAmit Arora  * existing extent.
142225d14f98SAmit Arora  *
142325d14f98SAmit Arora  * If there is an overlap discovered, it updates the length of the newext
142425d14f98SAmit Arora  * such that there will be no overlap, and then returns 1.
142525d14f98SAmit Arora  * If there is no overlap found, it returns 0.
142625d14f98SAmit Arora  */
142725d14f98SAmit Arora unsigned int ext4_ext_check_overlap(struct inode *inode,
142825d14f98SAmit Arora 				    struct ext4_extent *newext,
142925d14f98SAmit Arora 				    struct ext4_ext_path *path)
143025d14f98SAmit Arora {
1431725d26d3SAneesh Kumar K.V 	ext4_lblk_t b1, b2;
143225d14f98SAmit Arora 	unsigned int depth, len1;
143325d14f98SAmit Arora 	unsigned int ret = 0;
143425d14f98SAmit Arora 
143525d14f98SAmit Arora 	b1 = le32_to_cpu(newext->ee_block);
1436a2df2a63SAmit Arora 	len1 = ext4_ext_get_actual_len(newext);
143725d14f98SAmit Arora 	depth = ext_depth(inode);
143825d14f98SAmit Arora 	if (!path[depth].p_ext)
143925d14f98SAmit Arora 		goto out;
144025d14f98SAmit Arora 	b2 = le32_to_cpu(path[depth].p_ext->ee_block);
144125d14f98SAmit Arora 
144225d14f98SAmit Arora 	/*
144325d14f98SAmit Arora 	 * get the next allocated block if the extent in the path
144425d14f98SAmit Arora 	 * is before the requested block(s)
144525d14f98SAmit Arora 	 */
144625d14f98SAmit Arora 	if (b2 < b1) {
144725d14f98SAmit Arora 		b2 = ext4_ext_next_allocated_block(path);
144825d14f98SAmit Arora 		if (b2 == EXT_MAX_BLOCK)
144925d14f98SAmit Arora 			goto out;
145025d14f98SAmit Arora 	}
145125d14f98SAmit Arora 
1452725d26d3SAneesh Kumar K.V 	/* check for wrap through zero on extent logical start block*/
145325d14f98SAmit Arora 	if (b1 + len1 < b1) {
145425d14f98SAmit Arora 		len1 = EXT_MAX_BLOCK - b1;
145525d14f98SAmit Arora 		newext->ee_len = cpu_to_le16(len1);
145625d14f98SAmit Arora 		ret = 1;
145725d14f98SAmit Arora 	}
145825d14f98SAmit Arora 
145925d14f98SAmit Arora 	/* check for overlap */
146025d14f98SAmit Arora 	if (b1 + len1 > b2) {
146125d14f98SAmit Arora 		newext->ee_len = cpu_to_le16(b2 - b1);
146225d14f98SAmit Arora 		ret = 1;
146325d14f98SAmit Arora 	}
146425d14f98SAmit Arora out:
146525d14f98SAmit Arora 	return ret;
146625d14f98SAmit Arora }
146725d14f98SAmit Arora 
146825d14f98SAmit Arora /*
1469d0d856e8SRandy Dunlap  * ext4_ext_insert_extent:
1470d0d856e8SRandy Dunlap  * tries to merge requsted extent into the existing extent or
1471d0d856e8SRandy Dunlap  * inserts requested extent as new one into the tree,
1472d0d856e8SRandy Dunlap  * creating new leaf in the no-space case.
1473a86c6181SAlex Tomas  */
1474a86c6181SAlex Tomas int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1475a86c6181SAlex Tomas 				struct ext4_ext_path *path,
1476a86c6181SAlex Tomas 				struct ext4_extent *newext)
1477a86c6181SAlex Tomas {
1478a86c6181SAlex Tomas 	struct ext4_extent_header * eh;
1479a86c6181SAlex Tomas 	struct ext4_extent *ex, *fex;
1480a86c6181SAlex Tomas 	struct ext4_extent *nearex; /* nearest extent */
1481a86c6181SAlex Tomas 	struct ext4_ext_path *npath = NULL;
1482725d26d3SAneesh Kumar K.V 	int depth, len, err;
1483725d26d3SAneesh Kumar K.V 	ext4_lblk_t next;
1484a2df2a63SAmit Arora 	unsigned uninitialized = 0;
1485a86c6181SAlex Tomas 
1486a2df2a63SAmit Arora 	BUG_ON(ext4_ext_get_actual_len(newext) == 0);
1487a86c6181SAlex Tomas 	depth = ext_depth(inode);
1488a86c6181SAlex Tomas 	ex = path[depth].p_ext;
1489a86c6181SAlex Tomas 	BUG_ON(path[depth].p_hdr == NULL);
1490a86c6181SAlex Tomas 
1491a86c6181SAlex Tomas 	/* try to insert block into found extent and return */
1492a86c6181SAlex Tomas 	if (ex && ext4_can_extents_be_merged(inode, ex, newext)) {
14932ae02107SMingming Cao 		ext_debug("append %d block to %d:%d (from %llu)\n",
1494a2df2a63SAmit Arora 				ext4_ext_get_actual_len(newext),
1495a86c6181SAlex Tomas 				le32_to_cpu(ex->ee_block),
1496a2df2a63SAmit Arora 				ext4_ext_get_actual_len(ex), ext_pblock(ex));
14977e028976SAvantika Mathur 		err = ext4_ext_get_access(handle, inode, path + depth);
14987e028976SAvantika Mathur 		if (err)
1499a86c6181SAlex Tomas 			return err;
1500a2df2a63SAmit Arora 
1501a2df2a63SAmit Arora 		/*
1502a2df2a63SAmit Arora 		 * ext4_can_extents_be_merged should have checked that either
1503a2df2a63SAmit Arora 		 * both extents are uninitialized, or both aren't. Thus we
1504a2df2a63SAmit Arora 		 * need to check only one of them here.
1505a2df2a63SAmit Arora 		 */
1506a2df2a63SAmit Arora 		if (ext4_ext_is_uninitialized(ex))
1507a2df2a63SAmit Arora 			uninitialized = 1;
1508a2df2a63SAmit Arora 		ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1509a2df2a63SAmit Arora 					+ ext4_ext_get_actual_len(newext));
1510a2df2a63SAmit Arora 		if (uninitialized)
1511a2df2a63SAmit Arora 			ext4_ext_mark_uninitialized(ex);
1512a86c6181SAlex Tomas 		eh = path[depth].p_hdr;
1513a86c6181SAlex Tomas 		nearex = ex;
1514a86c6181SAlex Tomas 		goto merge;
1515a86c6181SAlex Tomas 	}
1516a86c6181SAlex Tomas 
1517a86c6181SAlex Tomas repeat:
1518a86c6181SAlex Tomas 	depth = ext_depth(inode);
1519a86c6181SAlex Tomas 	eh = path[depth].p_hdr;
1520a86c6181SAlex Tomas 	if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1521a86c6181SAlex Tomas 		goto has_space;
1522a86c6181SAlex Tomas 
1523a86c6181SAlex Tomas 	/* probably next leaf has space for us? */
1524a86c6181SAlex Tomas 	fex = EXT_LAST_EXTENT(eh);
1525a86c6181SAlex Tomas 	next = ext4_ext_next_leaf_block(inode, path);
1526a86c6181SAlex Tomas 	if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
1527a86c6181SAlex Tomas 	    && next != EXT_MAX_BLOCK) {
1528a86c6181SAlex Tomas 		ext_debug("next leaf block - %d\n", next);
1529a86c6181SAlex Tomas 		BUG_ON(npath != NULL);
1530a86c6181SAlex Tomas 		npath = ext4_ext_find_extent(inode, next, NULL);
1531a86c6181SAlex Tomas 		if (IS_ERR(npath))
1532a86c6181SAlex Tomas 			return PTR_ERR(npath);
1533a86c6181SAlex Tomas 		BUG_ON(npath->p_depth != path->p_depth);
1534a86c6181SAlex Tomas 		eh = npath[depth].p_hdr;
1535a86c6181SAlex Tomas 		if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
1536a86c6181SAlex Tomas 			ext_debug("next leaf isnt full(%d)\n",
1537a86c6181SAlex Tomas 				  le16_to_cpu(eh->eh_entries));
1538a86c6181SAlex Tomas 			path = npath;
1539a86c6181SAlex Tomas 			goto repeat;
1540a86c6181SAlex Tomas 		}
1541a86c6181SAlex Tomas 		ext_debug("next leaf has no free space(%d,%d)\n",
1542a86c6181SAlex Tomas 			  le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1543a86c6181SAlex Tomas 	}
1544a86c6181SAlex Tomas 
1545a86c6181SAlex Tomas 	/*
1546d0d856e8SRandy Dunlap 	 * There is no free space in the found leaf.
1547d0d856e8SRandy Dunlap 	 * We're gonna add a new leaf in the tree.
1548a86c6181SAlex Tomas 	 */
1549a86c6181SAlex Tomas 	err = ext4_ext_create_new_leaf(handle, inode, path, newext);
1550a86c6181SAlex Tomas 	if (err)
1551a86c6181SAlex Tomas 		goto cleanup;
1552a86c6181SAlex Tomas 	depth = ext_depth(inode);
1553a86c6181SAlex Tomas 	eh = path[depth].p_hdr;
1554a86c6181SAlex Tomas 
1555a86c6181SAlex Tomas has_space:
1556a86c6181SAlex Tomas 	nearex = path[depth].p_ext;
1557a86c6181SAlex Tomas 
15587e028976SAvantika Mathur 	err = ext4_ext_get_access(handle, inode, path + depth);
15597e028976SAvantika Mathur 	if (err)
1560a86c6181SAlex Tomas 		goto cleanup;
1561a86c6181SAlex Tomas 
1562a86c6181SAlex Tomas 	if (!nearex) {
1563a86c6181SAlex Tomas 		/* there is no extent in this leaf, create first one */
15642ae02107SMingming Cao 		ext_debug("first extent in the leaf: %d:%llu:%d\n",
1565a86c6181SAlex Tomas 				le32_to_cpu(newext->ee_block),
1566f65e6fbaSAlex Tomas 				ext_pblock(newext),
1567a2df2a63SAmit Arora 				ext4_ext_get_actual_len(newext));
1568a86c6181SAlex Tomas 		path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1569a86c6181SAlex Tomas 	} else if (le32_to_cpu(newext->ee_block)
1570a86c6181SAlex Tomas 			   > le32_to_cpu(nearex->ee_block)) {
1571a86c6181SAlex Tomas /*		BUG_ON(newext->ee_block == nearex->ee_block); */
1572a86c6181SAlex Tomas 		if (nearex != EXT_LAST_EXTENT(eh)) {
1573a86c6181SAlex Tomas 			len = EXT_MAX_EXTENT(eh) - nearex;
1574a86c6181SAlex Tomas 			len = (len - 1) * sizeof(struct ext4_extent);
1575a86c6181SAlex Tomas 			len = len < 0 ? 0 : len;
15762ae02107SMingming Cao 			ext_debug("insert %d:%llu:%d after: nearest 0x%p, "
1577a86c6181SAlex Tomas 					"move %d from 0x%p to 0x%p\n",
1578a86c6181SAlex Tomas 					le32_to_cpu(newext->ee_block),
1579f65e6fbaSAlex Tomas 					ext_pblock(newext),
1580a2df2a63SAmit Arora 					ext4_ext_get_actual_len(newext),
1581a86c6181SAlex Tomas 					nearex, len, nearex + 1, nearex + 2);
1582a86c6181SAlex Tomas 			memmove(nearex + 2, nearex + 1, len);
1583a86c6181SAlex Tomas 		}
1584a86c6181SAlex Tomas 		path[depth].p_ext = nearex + 1;
1585a86c6181SAlex Tomas 	} else {
1586a86c6181SAlex Tomas 		BUG_ON(newext->ee_block == nearex->ee_block);
1587a86c6181SAlex Tomas 		len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1588a86c6181SAlex Tomas 		len = len < 0 ? 0 : len;
15892ae02107SMingming Cao 		ext_debug("insert %d:%llu:%d before: nearest 0x%p, "
1590a86c6181SAlex Tomas 				"move %d from 0x%p to 0x%p\n",
1591a86c6181SAlex Tomas 				le32_to_cpu(newext->ee_block),
1592f65e6fbaSAlex Tomas 				ext_pblock(newext),
1593a2df2a63SAmit Arora 				ext4_ext_get_actual_len(newext),
1594a86c6181SAlex Tomas 				nearex, len, nearex + 1, nearex + 2);
1595a86c6181SAlex Tomas 		memmove(nearex + 1, nearex, len);
1596a86c6181SAlex Tomas 		path[depth].p_ext = nearex;
1597a86c6181SAlex Tomas 	}
1598a86c6181SAlex Tomas 
1599e8546d06SMarcin Slusarz 	le16_add_cpu(&eh->eh_entries, 1);
1600a86c6181SAlex Tomas 	nearex = path[depth].p_ext;
1601a86c6181SAlex Tomas 	nearex->ee_block = newext->ee_block;
1602b377611dSAneesh Kumar K.V 	ext4_ext_store_pblock(nearex, ext_pblock(newext));
1603a86c6181SAlex Tomas 	nearex->ee_len = newext->ee_len;
1604a86c6181SAlex Tomas 
1605a86c6181SAlex Tomas merge:
1606a86c6181SAlex Tomas 	/* try to merge extents to the right */
160756055d3aSAmit Arora 	ext4_ext_try_to_merge(inode, path, nearex);
1608a86c6181SAlex Tomas 
1609a86c6181SAlex Tomas 	/* try to merge extents to the left */
1610a86c6181SAlex Tomas 
1611a86c6181SAlex Tomas 	/* time to correct all indexes above */
1612a86c6181SAlex Tomas 	err = ext4_ext_correct_indexes(handle, inode, path);
1613a86c6181SAlex Tomas 	if (err)
1614a86c6181SAlex Tomas 		goto cleanup;
1615a86c6181SAlex Tomas 
1616a86c6181SAlex Tomas 	err = ext4_ext_dirty(handle, inode, path + depth);
1617a86c6181SAlex Tomas 
1618a86c6181SAlex Tomas cleanup:
1619a86c6181SAlex Tomas 	if (npath) {
1620a86c6181SAlex Tomas 		ext4_ext_drop_refs(npath);
1621a86c6181SAlex Tomas 		kfree(npath);
1622a86c6181SAlex Tomas 	}
1623a86c6181SAlex Tomas 	ext4_ext_tree_changed(inode);
1624a86c6181SAlex Tomas 	ext4_ext_invalidate_cache(inode);
1625a86c6181SAlex Tomas 	return err;
1626a86c6181SAlex Tomas }
1627a86c6181SAlex Tomas 
162809b88252SAvantika Mathur static void
1629725d26d3SAneesh Kumar K.V ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
1630dd54567aSMingming Cao 			__u32 len, ext4_fsblk_t start, int type)
1631a86c6181SAlex Tomas {
1632a86c6181SAlex Tomas 	struct ext4_ext_cache *cex;
1633a86c6181SAlex Tomas 	BUG_ON(len == 0);
1634a86c6181SAlex Tomas 	cex = &EXT4_I(inode)->i_cached_extent;
1635a86c6181SAlex Tomas 	cex->ec_type = type;
1636a86c6181SAlex Tomas 	cex->ec_block = block;
1637a86c6181SAlex Tomas 	cex->ec_len = len;
1638a86c6181SAlex Tomas 	cex->ec_start = start;
1639a86c6181SAlex Tomas }
1640a86c6181SAlex Tomas 
1641a86c6181SAlex Tomas /*
1642d0d856e8SRandy Dunlap  * ext4_ext_put_gap_in_cache:
1643d0d856e8SRandy Dunlap  * calculate boundaries of the gap that the requested block fits into
1644a86c6181SAlex Tomas  * and cache this gap
1645a86c6181SAlex Tomas  */
164609b88252SAvantika Mathur static void
1647a86c6181SAlex Tomas ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
1648725d26d3SAneesh Kumar K.V 				ext4_lblk_t block)
1649a86c6181SAlex Tomas {
1650a86c6181SAlex Tomas 	int depth = ext_depth(inode);
1651725d26d3SAneesh Kumar K.V 	unsigned long len;
1652725d26d3SAneesh Kumar K.V 	ext4_lblk_t lblock;
1653a86c6181SAlex Tomas 	struct ext4_extent *ex;
1654a86c6181SAlex Tomas 
1655a86c6181SAlex Tomas 	ex = path[depth].p_ext;
1656a86c6181SAlex Tomas 	if (ex == NULL) {
1657a86c6181SAlex Tomas 		/* there is no extent yet, so gap is [0;-] */
1658a86c6181SAlex Tomas 		lblock = 0;
1659a86c6181SAlex Tomas 		len = EXT_MAX_BLOCK;
1660a86c6181SAlex Tomas 		ext_debug("cache gap(whole file):");
1661a86c6181SAlex Tomas 	} else if (block < le32_to_cpu(ex->ee_block)) {
1662a86c6181SAlex Tomas 		lblock = block;
1663a86c6181SAlex Tomas 		len = le32_to_cpu(ex->ee_block) - block;
1664bba90743SEric Sandeen 		ext_debug("cache gap(before): %u [%u:%u]",
1665bba90743SEric Sandeen 				block,
1666bba90743SEric Sandeen 				le32_to_cpu(ex->ee_block),
1667bba90743SEric Sandeen 				 ext4_ext_get_actual_len(ex));
1668a86c6181SAlex Tomas 	} else if (block >= le32_to_cpu(ex->ee_block)
1669a2df2a63SAmit Arora 			+ ext4_ext_get_actual_len(ex)) {
1670725d26d3SAneesh Kumar K.V 		ext4_lblk_t next;
1671a86c6181SAlex Tomas 		lblock = le32_to_cpu(ex->ee_block)
1672a2df2a63SAmit Arora 			+ ext4_ext_get_actual_len(ex);
1673725d26d3SAneesh Kumar K.V 
1674725d26d3SAneesh Kumar K.V 		next = ext4_ext_next_allocated_block(path);
1675bba90743SEric Sandeen 		ext_debug("cache gap(after): [%u:%u] %u",
1676bba90743SEric Sandeen 				le32_to_cpu(ex->ee_block),
1677bba90743SEric Sandeen 				ext4_ext_get_actual_len(ex),
1678bba90743SEric Sandeen 				block);
1679725d26d3SAneesh Kumar K.V 		BUG_ON(next == lblock);
1680725d26d3SAneesh Kumar K.V 		len = next - lblock;
1681a86c6181SAlex Tomas 	} else {
1682a86c6181SAlex Tomas 		lblock = len = 0;
1683a86c6181SAlex Tomas 		BUG();
1684a86c6181SAlex Tomas 	}
1685a86c6181SAlex Tomas 
1686bba90743SEric Sandeen 	ext_debug(" -> %u:%lu\n", lblock, len);
1687a86c6181SAlex Tomas 	ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
1688a86c6181SAlex Tomas }
1689a86c6181SAlex Tomas 
169009b88252SAvantika Mathur static int
1691725d26d3SAneesh Kumar K.V ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
1692a86c6181SAlex Tomas 			struct ext4_extent *ex)
1693a86c6181SAlex Tomas {
1694a86c6181SAlex Tomas 	struct ext4_ext_cache *cex;
1695a86c6181SAlex Tomas 
1696a86c6181SAlex Tomas 	cex = &EXT4_I(inode)->i_cached_extent;
1697a86c6181SAlex Tomas 
1698a86c6181SAlex Tomas 	/* has cache valid data? */
1699a86c6181SAlex Tomas 	if (cex->ec_type == EXT4_EXT_CACHE_NO)
1700a86c6181SAlex Tomas 		return EXT4_EXT_CACHE_NO;
1701a86c6181SAlex Tomas 
1702a86c6181SAlex Tomas 	BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
1703a86c6181SAlex Tomas 			cex->ec_type != EXT4_EXT_CACHE_EXTENT);
1704a86c6181SAlex Tomas 	if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
1705a86c6181SAlex Tomas 		ex->ee_block = cpu_to_le32(cex->ec_block);
1706f65e6fbaSAlex Tomas 		ext4_ext_store_pblock(ex, cex->ec_start);
1707a86c6181SAlex Tomas 		ex->ee_len = cpu_to_le16(cex->ec_len);
1708bba90743SEric Sandeen 		ext_debug("%u cached by %u:%u:%llu\n",
1709bba90743SEric Sandeen 				block,
1710bba90743SEric Sandeen 				cex->ec_block, cex->ec_len, cex->ec_start);
1711a86c6181SAlex Tomas 		return cex->ec_type;
1712a86c6181SAlex Tomas 	}
1713a86c6181SAlex Tomas 
1714a86c6181SAlex Tomas 	/* not in cache */
1715a86c6181SAlex Tomas 	return EXT4_EXT_CACHE_NO;
1716a86c6181SAlex Tomas }
1717a86c6181SAlex Tomas 
1718a86c6181SAlex Tomas /*
1719d0d856e8SRandy Dunlap  * ext4_ext_rm_idx:
1720d0d856e8SRandy Dunlap  * removes index from the index block.
1721d0d856e8SRandy Dunlap  * It's used in truncate case only, thus all requests are for
1722d0d856e8SRandy Dunlap  * last index in the block only.
1723a86c6181SAlex Tomas  */
17241d03ec98SAneesh Kumar K.V static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
1725a86c6181SAlex Tomas 			struct ext4_ext_path *path)
1726a86c6181SAlex Tomas {
1727a86c6181SAlex Tomas 	struct buffer_head *bh;
1728a86c6181SAlex Tomas 	int err;
1729f65e6fbaSAlex Tomas 	ext4_fsblk_t leaf;
1730a86c6181SAlex Tomas 
1731a86c6181SAlex Tomas 	/* free index block */
1732a86c6181SAlex Tomas 	path--;
1733f65e6fbaSAlex Tomas 	leaf = idx_pblock(path->p_idx);
1734a86c6181SAlex Tomas 	BUG_ON(path->p_hdr->eh_entries == 0);
17357e028976SAvantika Mathur 	err = ext4_ext_get_access(handle, inode, path);
17367e028976SAvantika Mathur 	if (err)
1737a86c6181SAlex Tomas 		return err;
1738e8546d06SMarcin Slusarz 	le16_add_cpu(&path->p_hdr->eh_entries, -1);
17397e028976SAvantika Mathur 	err = ext4_ext_dirty(handle, inode, path);
17407e028976SAvantika Mathur 	if (err)
1741a86c6181SAlex Tomas 		return err;
17422ae02107SMingming Cao 	ext_debug("index is empty, remove it, free block %llu\n", leaf);
1743a86c6181SAlex Tomas 	bh = sb_find_get_block(inode->i_sb, leaf);
1744a86c6181SAlex Tomas 	ext4_forget(handle, 1, inode, bh, leaf);
1745c9de560dSAlex Tomas 	ext4_free_blocks(handle, inode, leaf, 1, 1);
1746a86c6181SAlex Tomas 	return err;
1747a86c6181SAlex Tomas }
1748a86c6181SAlex Tomas 
1749a86c6181SAlex Tomas /*
1750*ee12b630SMingming Cao  * ext4_ext_calc_credits_for_single_extent:
1751*ee12b630SMingming Cao  * This routine returns max. credits that needed to insert an extent
1752*ee12b630SMingming Cao  * to the extent tree.
1753*ee12b630SMingming Cao  * When pass the actual path, the caller should calculate credits
1754*ee12b630SMingming Cao  * under i_data_sem.
1755a86c6181SAlex Tomas  */
1756*ee12b630SMingming Cao int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int num,
1757a86c6181SAlex Tomas 						struct ext4_ext_path *path)
1758a86c6181SAlex Tomas {
1759a86c6181SAlex Tomas 	if (path) {
1760*ee12b630SMingming Cao 		int depth = ext_depth(inode);
1761*ee12b630SMingming Cao 		int ret;
1762*ee12b630SMingming Cao 
1763a86c6181SAlex Tomas 		/* probably there is space in leaf? */
1764a86c6181SAlex Tomas 		if (le16_to_cpu(path[depth].p_hdr->eh_entries)
1765*ee12b630SMingming Cao 				< le16_to_cpu(path[depth].p_hdr->eh_max)) {
1766*ee12b630SMingming Cao 
1767*ee12b630SMingming Cao 			/*
1768*ee12b630SMingming Cao 			 *  There are some space in the leaf tree, no
1769*ee12b630SMingming Cao 			 *  need to account for leaf block credit
1770*ee12b630SMingming Cao 			 *
1771*ee12b630SMingming Cao 			 *  bitmaps and block group descriptor blocks
1772*ee12b630SMingming Cao 			 *  and other metadat blocks still need to be
1773*ee12b630SMingming Cao 			 *  accounted.
1774*ee12b630SMingming Cao 			 */
1775*ee12b630SMingming Cao 			/* 1 one bitmap, 1 block group descriptor */
1776*ee12b630SMingming Cao 			ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
1777*ee12b630SMingming Cao 		}
1778*ee12b630SMingming Cao 	}
1779*ee12b630SMingming Cao 
1780*ee12b630SMingming Cao 	return ext4_meta_trans_blocks(inode, num, 1);
1781a86c6181SAlex Tomas }
1782a86c6181SAlex Tomas 
1783a86c6181SAlex Tomas /*
1784*ee12b630SMingming Cao  * How many index/leaf blocks need to change/allocate to modify nrblocks?
1785*ee12b630SMingming Cao  *
1786*ee12b630SMingming Cao  * if nrblocks are fit in a single extent (chunk flag is 1), then
1787*ee12b630SMingming Cao  * in the worse case, each tree level index/leaf need to be changed
1788*ee12b630SMingming Cao  * if the tree split due to insert a new extent, then the old tree
1789*ee12b630SMingming Cao  * index/leaf need to be updated too
1790*ee12b630SMingming Cao  *
1791*ee12b630SMingming Cao  * If the nrblocks are discontiguous, they could cause
1792*ee12b630SMingming Cao  * the whole tree split more than once, but this is really rare.
1793a86c6181SAlex Tomas  */
1794*ee12b630SMingming Cao int ext4_ext_index_trans_blocks(struct inode *inode, int num, int chunk)
1795*ee12b630SMingming Cao {
1796*ee12b630SMingming Cao 	int index;
1797*ee12b630SMingming Cao 	int depth = ext_depth(inode);
1798a86c6181SAlex Tomas 
1799*ee12b630SMingming Cao 	if (chunk)
1800*ee12b630SMingming Cao 		index = depth * 2;
1801*ee12b630SMingming Cao 	else
1802*ee12b630SMingming Cao 		index = depth * 3;
1803a86c6181SAlex Tomas 
1804*ee12b630SMingming Cao 	return index;
1805a86c6181SAlex Tomas }
1806a86c6181SAlex Tomas 
1807a86c6181SAlex Tomas static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
1808a86c6181SAlex Tomas 				struct ext4_extent *ex,
1809725d26d3SAneesh Kumar K.V 				ext4_lblk_t from, ext4_lblk_t to)
1810a86c6181SAlex Tomas {
1811a86c6181SAlex Tomas 	struct buffer_head *bh;
1812a2df2a63SAmit Arora 	unsigned short ee_len =  ext4_ext_get_actual_len(ex);
1813c9de560dSAlex Tomas 	int i, metadata = 0;
1814a86c6181SAlex Tomas 
1815c9de560dSAlex Tomas 	if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
1816c9de560dSAlex Tomas 		metadata = 1;
1817a86c6181SAlex Tomas #ifdef EXTENTS_STATS
1818a86c6181SAlex Tomas 	{
1819a86c6181SAlex Tomas 		struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1820a86c6181SAlex Tomas 		spin_lock(&sbi->s_ext_stats_lock);
1821a86c6181SAlex Tomas 		sbi->s_ext_blocks += ee_len;
1822a86c6181SAlex Tomas 		sbi->s_ext_extents++;
1823a86c6181SAlex Tomas 		if (ee_len < sbi->s_ext_min)
1824a86c6181SAlex Tomas 			sbi->s_ext_min = ee_len;
1825a86c6181SAlex Tomas 		if (ee_len > sbi->s_ext_max)
1826a86c6181SAlex Tomas 			sbi->s_ext_max = ee_len;
1827a86c6181SAlex Tomas 		if (ext_depth(inode) > sbi->s_depth_max)
1828a86c6181SAlex Tomas 			sbi->s_depth_max = ext_depth(inode);
1829a86c6181SAlex Tomas 		spin_unlock(&sbi->s_ext_stats_lock);
1830a86c6181SAlex Tomas 	}
1831a86c6181SAlex Tomas #endif
1832a86c6181SAlex Tomas 	if (from >= le32_to_cpu(ex->ee_block)
1833a2df2a63SAmit Arora 	    && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
1834a86c6181SAlex Tomas 		/* tail removal */
1835725d26d3SAneesh Kumar K.V 		ext4_lblk_t num;
1836f65e6fbaSAlex Tomas 		ext4_fsblk_t start;
1837725d26d3SAneesh Kumar K.V 
1838a2df2a63SAmit Arora 		num = le32_to_cpu(ex->ee_block) + ee_len - from;
1839a2df2a63SAmit Arora 		start = ext_pblock(ex) + ee_len - num;
1840725d26d3SAneesh Kumar K.V 		ext_debug("free last %u blocks starting %llu\n", num, start);
1841a86c6181SAlex Tomas 		for (i = 0; i < num; i++) {
1842a86c6181SAlex Tomas 			bh = sb_find_get_block(inode->i_sb, start + i);
1843a86c6181SAlex Tomas 			ext4_forget(handle, 0, inode, bh, start + i);
1844a86c6181SAlex Tomas 		}
1845c9de560dSAlex Tomas 		ext4_free_blocks(handle, inode, start, num, metadata);
1846a86c6181SAlex Tomas 	} else if (from == le32_to_cpu(ex->ee_block)
1847a2df2a63SAmit Arora 		   && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
1848725d26d3SAneesh Kumar K.V 		printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n",
1849a2df2a63SAmit Arora 			from, to, le32_to_cpu(ex->ee_block), ee_len);
1850a86c6181SAlex Tomas 	} else {
1851725d26d3SAneesh Kumar K.V 		printk(KERN_INFO "strange request: removal(2) "
1852725d26d3SAneesh Kumar K.V 				"%u-%u from %u:%u\n",
1853a2df2a63SAmit Arora 				from, to, le32_to_cpu(ex->ee_block), ee_len);
1854a86c6181SAlex Tomas 	}
1855a86c6181SAlex Tomas 	return 0;
1856a86c6181SAlex Tomas }
1857a86c6181SAlex Tomas 
1858a86c6181SAlex Tomas static int
1859a86c6181SAlex Tomas ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
1860725d26d3SAneesh Kumar K.V 		struct ext4_ext_path *path, ext4_lblk_t start)
1861a86c6181SAlex Tomas {
1862a86c6181SAlex Tomas 	int err = 0, correct_index = 0;
1863a86c6181SAlex Tomas 	int depth = ext_depth(inode), credits;
1864a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
1865725d26d3SAneesh Kumar K.V 	ext4_lblk_t a, b, block;
1866725d26d3SAneesh Kumar K.V 	unsigned num;
1867725d26d3SAneesh Kumar K.V 	ext4_lblk_t ex_ee_block;
1868a86c6181SAlex Tomas 	unsigned short ex_ee_len;
1869a2df2a63SAmit Arora 	unsigned uninitialized = 0;
1870a86c6181SAlex Tomas 	struct ext4_extent *ex;
1871a86c6181SAlex Tomas 
1872c29c0ae7SAlex Tomas 	/* the header must be checked already in ext4_ext_remove_space() */
1873725d26d3SAneesh Kumar K.V 	ext_debug("truncate since %u in leaf\n", start);
1874a86c6181SAlex Tomas 	if (!path[depth].p_hdr)
1875a86c6181SAlex Tomas 		path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
1876a86c6181SAlex Tomas 	eh = path[depth].p_hdr;
1877a86c6181SAlex Tomas 	BUG_ON(eh == NULL);
1878a86c6181SAlex Tomas 
1879a86c6181SAlex Tomas 	/* find where to start removing */
1880a86c6181SAlex Tomas 	ex = EXT_LAST_EXTENT(eh);
1881a86c6181SAlex Tomas 
1882a86c6181SAlex Tomas 	ex_ee_block = le32_to_cpu(ex->ee_block);
1883a2df2a63SAmit Arora 	if (ext4_ext_is_uninitialized(ex))
1884a2df2a63SAmit Arora 		uninitialized = 1;
1885a2df2a63SAmit Arora 	ex_ee_len = ext4_ext_get_actual_len(ex);
1886a86c6181SAlex Tomas 
1887a86c6181SAlex Tomas 	while (ex >= EXT_FIRST_EXTENT(eh) &&
1888a86c6181SAlex Tomas 			ex_ee_block + ex_ee_len > start) {
1889a86c6181SAlex Tomas 		ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len);
1890a86c6181SAlex Tomas 		path[depth].p_ext = ex;
1891a86c6181SAlex Tomas 
1892a86c6181SAlex Tomas 		a = ex_ee_block > start ? ex_ee_block : start;
1893a86c6181SAlex Tomas 		b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
1894a86c6181SAlex Tomas 			ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
1895a86c6181SAlex Tomas 
1896a86c6181SAlex Tomas 		ext_debug("  border %u:%u\n", a, b);
1897a86c6181SAlex Tomas 
1898a86c6181SAlex Tomas 		if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
1899a86c6181SAlex Tomas 			block = 0;
1900a86c6181SAlex Tomas 			num = 0;
1901a86c6181SAlex Tomas 			BUG();
1902a86c6181SAlex Tomas 		} else if (a != ex_ee_block) {
1903a86c6181SAlex Tomas 			/* remove tail of the extent */
1904a86c6181SAlex Tomas 			block = ex_ee_block;
1905a86c6181SAlex Tomas 			num = a - block;
1906a86c6181SAlex Tomas 		} else if (b != ex_ee_block + ex_ee_len - 1) {
1907a86c6181SAlex Tomas 			/* remove head of the extent */
1908a86c6181SAlex Tomas 			block = a;
1909a86c6181SAlex Tomas 			num = b - a;
1910a86c6181SAlex Tomas 			/* there is no "make a hole" API yet */
1911a86c6181SAlex Tomas 			BUG();
1912a86c6181SAlex Tomas 		} else {
1913a86c6181SAlex Tomas 			/* remove whole extent: excellent! */
1914a86c6181SAlex Tomas 			block = ex_ee_block;
1915a86c6181SAlex Tomas 			num = 0;
1916a86c6181SAlex Tomas 			BUG_ON(a != ex_ee_block);
1917a86c6181SAlex Tomas 			BUG_ON(b != ex_ee_block + ex_ee_len - 1);
1918a86c6181SAlex Tomas 		}
1919a86c6181SAlex Tomas 
192034071da7STheodore Ts'o 		/*
192134071da7STheodore Ts'o 		 * 3 for leaf, sb, and inode plus 2 (bmap and group
192234071da7STheodore Ts'o 		 * descriptor) for each block group; assume two block
192334071da7STheodore Ts'o 		 * groups plus ex_ee_len/blocks_per_block_group for
192434071da7STheodore Ts'o 		 * the worst case
192534071da7STheodore Ts'o 		 */
192634071da7STheodore Ts'o 		credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
1927a86c6181SAlex Tomas 		if (ex == EXT_FIRST_EXTENT(eh)) {
1928a86c6181SAlex Tomas 			correct_index = 1;
1929a86c6181SAlex Tomas 			credits += (ext_depth(inode)) + 1;
1930a86c6181SAlex Tomas 		}
1931a86c6181SAlex Tomas 		credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
1932a86c6181SAlex Tomas 
19339102e4faSShen Feng 		err = ext4_ext_journal_restart(handle, credits);
19349102e4faSShen Feng 		if (err)
1935a86c6181SAlex Tomas 			goto out;
1936a86c6181SAlex Tomas 
1937a86c6181SAlex Tomas 		err = ext4_ext_get_access(handle, inode, path + depth);
1938a86c6181SAlex Tomas 		if (err)
1939a86c6181SAlex Tomas 			goto out;
1940a86c6181SAlex Tomas 
1941a86c6181SAlex Tomas 		err = ext4_remove_blocks(handle, inode, ex, a, b);
1942a86c6181SAlex Tomas 		if (err)
1943a86c6181SAlex Tomas 			goto out;
1944a86c6181SAlex Tomas 
1945a86c6181SAlex Tomas 		if (num == 0) {
1946d0d856e8SRandy Dunlap 			/* this extent is removed; mark slot entirely unused */
1947f65e6fbaSAlex Tomas 			ext4_ext_store_pblock(ex, 0);
1948e8546d06SMarcin Slusarz 			le16_add_cpu(&eh->eh_entries, -1);
1949a86c6181SAlex Tomas 		}
1950a86c6181SAlex Tomas 
1951a86c6181SAlex Tomas 		ex->ee_block = cpu_to_le32(block);
1952a86c6181SAlex Tomas 		ex->ee_len = cpu_to_le16(num);
1953749269faSAmit Arora 		/*
1954749269faSAmit Arora 		 * Do not mark uninitialized if all the blocks in the
1955749269faSAmit Arora 		 * extent have been removed.
1956749269faSAmit Arora 		 */
1957749269faSAmit Arora 		if (uninitialized && num)
1958a2df2a63SAmit Arora 			ext4_ext_mark_uninitialized(ex);
1959a86c6181SAlex Tomas 
1960a86c6181SAlex Tomas 		err = ext4_ext_dirty(handle, inode, path + depth);
1961a86c6181SAlex Tomas 		if (err)
1962a86c6181SAlex Tomas 			goto out;
1963a86c6181SAlex Tomas 
19642ae02107SMingming Cao 		ext_debug("new extent: %u:%u:%llu\n", block, num,
1965f65e6fbaSAlex Tomas 				ext_pblock(ex));
1966a86c6181SAlex Tomas 		ex--;
1967a86c6181SAlex Tomas 		ex_ee_block = le32_to_cpu(ex->ee_block);
1968a2df2a63SAmit Arora 		ex_ee_len = ext4_ext_get_actual_len(ex);
1969a86c6181SAlex Tomas 	}
1970a86c6181SAlex Tomas 
1971a86c6181SAlex Tomas 	if (correct_index && eh->eh_entries)
1972a86c6181SAlex Tomas 		err = ext4_ext_correct_indexes(handle, inode, path);
1973a86c6181SAlex Tomas 
1974a86c6181SAlex Tomas 	/* if this leaf is free, then we should
1975a86c6181SAlex Tomas 	 * remove it from index block above */
1976a86c6181SAlex Tomas 	if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
1977a86c6181SAlex Tomas 		err = ext4_ext_rm_idx(handle, inode, path + depth);
1978a86c6181SAlex Tomas 
1979a86c6181SAlex Tomas out:
1980a86c6181SAlex Tomas 	return err;
1981a86c6181SAlex Tomas }
1982a86c6181SAlex Tomas 
1983a86c6181SAlex Tomas /*
1984d0d856e8SRandy Dunlap  * ext4_ext_more_to_rm:
1985d0d856e8SRandy Dunlap  * returns 1 if current index has to be freed (even partial)
1986a86c6181SAlex Tomas  */
198709b88252SAvantika Mathur static int
1988a86c6181SAlex Tomas ext4_ext_more_to_rm(struct ext4_ext_path *path)
1989a86c6181SAlex Tomas {
1990a86c6181SAlex Tomas 	BUG_ON(path->p_idx == NULL);
1991a86c6181SAlex Tomas 
1992a86c6181SAlex Tomas 	if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
1993a86c6181SAlex Tomas 		return 0;
1994a86c6181SAlex Tomas 
1995a86c6181SAlex Tomas 	/*
1996d0d856e8SRandy Dunlap 	 * if truncate on deeper level happened, it wasn't partial,
1997a86c6181SAlex Tomas 	 * so we have to consider current index for truncation
1998a86c6181SAlex Tomas 	 */
1999a86c6181SAlex Tomas 	if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2000a86c6181SAlex Tomas 		return 0;
2001a86c6181SAlex Tomas 	return 1;
2002a86c6181SAlex Tomas }
2003a86c6181SAlex Tomas 
20041d03ec98SAneesh Kumar K.V static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
2005a86c6181SAlex Tomas {
2006a86c6181SAlex Tomas 	struct super_block *sb = inode->i_sb;
2007a86c6181SAlex Tomas 	int depth = ext_depth(inode);
2008a86c6181SAlex Tomas 	struct ext4_ext_path *path;
2009a86c6181SAlex Tomas 	handle_t *handle;
2010a86c6181SAlex Tomas 	int i = 0, err = 0;
2011a86c6181SAlex Tomas 
2012725d26d3SAneesh Kumar K.V 	ext_debug("truncate since %u\n", start);
2013a86c6181SAlex Tomas 
2014a86c6181SAlex Tomas 	/* probably first extent we're gonna free will be last in block */
2015a86c6181SAlex Tomas 	handle = ext4_journal_start(inode, depth + 1);
2016a86c6181SAlex Tomas 	if (IS_ERR(handle))
2017a86c6181SAlex Tomas 		return PTR_ERR(handle);
2018a86c6181SAlex Tomas 
2019a86c6181SAlex Tomas 	ext4_ext_invalidate_cache(inode);
2020a86c6181SAlex Tomas 
2021a86c6181SAlex Tomas 	/*
2022d0d856e8SRandy Dunlap 	 * We start scanning from right side, freeing all the blocks
2023d0d856e8SRandy Dunlap 	 * after i_size and walking into the tree depth-wise.
2024a86c6181SAlex Tomas 	 */
2025216553c4SJosef Bacik 	path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
2026a86c6181SAlex Tomas 	if (path == NULL) {
2027a86c6181SAlex Tomas 		ext4_journal_stop(handle);
2028a86c6181SAlex Tomas 		return -ENOMEM;
2029a86c6181SAlex Tomas 	}
2030a86c6181SAlex Tomas 	path[0].p_hdr = ext_inode_hdr(inode);
2031c29c0ae7SAlex Tomas 	if (ext4_ext_check_header(inode, path[0].p_hdr, depth)) {
2032a86c6181SAlex Tomas 		err = -EIO;
2033a86c6181SAlex Tomas 		goto out;
2034a86c6181SAlex Tomas 	}
2035a86c6181SAlex Tomas 	path[0].p_depth = depth;
2036a86c6181SAlex Tomas 
2037a86c6181SAlex Tomas 	while (i >= 0 && err == 0) {
2038a86c6181SAlex Tomas 		if (i == depth) {
2039a86c6181SAlex Tomas 			/* this is leaf block */
2040a86c6181SAlex Tomas 			err = ext4_ext_rm_leaf(handle, inode, path, start);
2041d0d856e8SRandy Dunlap 			/* root level has p_bh == NULL, brelse() eats this */
2042a86c6181SAlex Tomas 			brelse(path[i].p_bh);
2043a86c6181SAlex Tomas 			path[i].p_bh = NULL;
2044a86c6181SAlex Tomas 			i--;
2045a86c6181SAlex Tomas 			continue;
2046a86c6181SAlex Tomas 		}
2047a86c6181SAlex Tomas 
2048a86c6181SAlex Tomas 		/* this is index block */
2049a86c6181SAlex Tomas 		if (!path[i].p_hdr) {
2050a86c6181SAlex Tomas 			ext_debug("initialize header\n");
2051a86c6181SAlex Tomas 			path[i].p_hdr = ext_block_hdr(path[i].p_bh);
2052a86c6181SAlex Tomas 		}
2053a86c6181SAlex Tomas 
2054a86c6181SAlex Tomas 		if (!path[i].p_idx) {
2055d0d856e8SRandy Dunlap 			/* this level hasn't been touched yet */
2056a86c6181SAlex Tomas 			path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2057a86c6181SAlex Tomas 			path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2058a86c6181SAlex Tomas 			ext_debug("init index ptr: hdr 0x%p, num %d\n",
2059a86c6181SAlex Tomas 				  path[i].p_hdr,
2060a86c6181SAlex Tomas 				  le16_to_cpu(path[i].p_hdr->eh_entries));
2061a86c6181SAlex Tomas 		} else {
2062d0d856e8SRandy Dunlap 			/* we were already here, see at next index */
2063a86c6181SAlex Tomas 			path[i].p_idx--;
2064a86c6181SAlex Tomas 		}
2065a86c6181SAlex Tomas 
2066a86c6181SAlex Tomas 		ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2067a86c6181SAlex Tomas 				i, EXT_FIRST_INDEX(path[i].p_hdr),
2068a86c6181SAlex Tomas 				path[i].p_idx);
2069a86c6181SAlex Tomas 		if (ext4_ext_more_to_rm(path + i)) {
2070c29c0ae7SAlex Tomas 			struct buffer_head *bh;
2071a86c6181SAlex Tomas 			/* go to the next level */
20722ae02107SMingming Cao 			ext_debug("move to level %d (block %llu)\n",
2073f65e6fbaSAlex Tomas 				  i + 1, idx_pblock(path[i].p_idx));
2074a86c6181SAlex Tomas 			memset(path + i + 1, 0, sizeof(*path));
2075c29c0ae7SAlex Tomas 			bh = sb_bread(sb, idx_pblock(path[i].p_idx));
2076c29c0ae7SAlex Tomas 			if (!bh) {
2077a86c6181SAlex Tomas 				/* should we reset i_size? */
2078a86c6181SAlex Tomas 				err = -EIO;
2079a86c6181SAlex Tomas 				break;
2080a86c6181SAlex Tomas 			}
2081c29c0ae7SAlex Tomas 			if (WARN_ON(i + 1 > depth)) {
2082c29c0ae7SAlex Tomas 				err = -EIO;
2083c29c0ae7SAlex Tomas 				break;
2084c29c0ae7SAlex Tomas 			}
2085c29c0ae7SAlex Tomas 			if (ext4_ext_check_header(inode, ext_block_hdr(bh),
2086c29c0ae7SAlex Tomas 							depth - i - 1)) {
2087c29c0ae7SAlex Tomas 				err = -EIO;
2088c29c0ae7SAlex Tomas 				break;
2089c29c0ae7SAlex Tomas 			}
2090c29c0ae7SAlex Tomas 			path[i + 1].p_bh = bh;
2091a86c6181SAlex Tomas 
2092d0d856e8SRandy Dunlap 			/* save actual number of indexes since this
2093d0d856e8SRandy Dunlap 			 * number is changed at the next iteration */
2094a86c6181SAlex Tomas 			path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2095a86c6181SAlex Tomas 			i++;
2096a86c6181SAlex Tomas 		} else {
2097d0d856e8SRandy Dunlap 			/* we finished processing this index, go up */
2098a86c6181SAlex Tomas 			if (path[i].p_hdr->eh_entries == 0 && i > 0) {
2099d0d856e8SRandy Dunlap 				/* index is empty, remove it;
2100a86c6181SAlex Tomas 				 * handle must be already prepared by the
2101a86c6181SAlex Tomas 				 * truncatei_leaf() */
2102a86c6181SAlex Tomas 				err = ext4_ext_rm_idx(handle, inode, path + i);
2103a86c6181SAlex Tomas 			}
2104d0d856e8SRandy Dunlap 			/* root level has p_bh == NULL, brelse() eats this */
2105a86c6181SAlex Tomas 			brelse(path[i].p_bh);
2106a86c6181SAlex Tomas 			path[i].p_bh = NULL;
2107a86c6181SAlex Tomas 			i--;
2108a86c6181SAlex Tomas 			ext_debug("return to level %d\n", i);
2109a86c6181SAlex Tomas 		}
2110a86c6181SAlex Tomas 	}
2111a86c6181SAlex Tomas 
2112a86c6181SAlex Tomas 	/* TODO: flexible tree reduction should be here */
2113a86c6181SAlex Tomas 	if (path->p_hdr->eh_entries == 0) {
2114a86c6181SAlex Tomas 		/*
2115d0d856e8SRandy Dunlap 		 * truncate to zero freed all the tree,
2116d0d856e8SRandy Dunlap 		 * so we need to correct eh_depth
2117a86c6181SAlex Tomas 		 */
2118a86c6181SAlex Tomas 		err = ext4_ext_get_access(handle, inode, path);
2119a86c6181SAlex Tomas 		if (err == 0) {
2120a86c6181SAlex Tomas 			ext_inode_hdr(inode)->eh_depth = 0;
2121a86c6181SAlex Tomas 			ext_inode_hdr(inode)->eh_max =
2122a86c6181SAlex Tomas 				cpu_to_le16(ext4_ext_space_root(inode));
2123a86c6181SAlex Tomas 			err = ext4_ext_dirty(handle, inode, path);
2124a86c6181SAlex Tomas 		}
2125a86c6181SAlex Tomas 	}
2126a86c6181SAlex Tomas out:
2127a86c6181SAlex Tomas 	ext4_ext_tree_changed(inode);
2128a86c6181SAlex Tomas 	ext4_ext_drop_refs(path);
2129a86c6181SAlex Tomas 	kfree(path);
2130a86c6181SAlex Tomas 	ext4_journal_stop(handle);
2131a86c6181SAlex Tomas 
2132a86c6181SAlex Tomas 	return err;
2133a86c6181SAlex Tomas }
2134a86c6181SAlex Tomas 
2135a86c6181SAlex Tomas /*
2136a86c6181SAlex Tomas  * called at mount time
2137a86c6181SAlex Tomas  */
2138a86c6181SAlex Tomas void ext4_ext_init(struct super_block *sb)
2139a86c6181SAlex Tomas {
2140a86c6181SAlex Tomas 	/*
2141a86c6181SAlex Tomas 	 * possible initialization would be here
2142a86c6181SAlex Tomas 	 */
2143a86c6181SAlex Tomas 
2144a86c6181SAlex Tomas 	if (test_opt(sb, EXTENTS)) {
2145a86c6181SAlex Tomas 		printk("EXT4-fs: file extents enabled");
2146bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST
2147bbf2f9fbSRobert P. J. Day 		printk(", aggressive tests");
2148a86c6181SAlex Tomas #endif
2149a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH
2150a86c6181SAlex Tomas 		printk(", check binsearch");
2151a86c6181SAlex Tomas #endif
2152a86c6181SAlex Tomas #ifdef EXTENTS_STATS
2153a86c6181SAlex Tomas 		printk(", stats");
2154a86c6181SAlex Tomas #endif
2155a86c6181SAlex Tomas 		printk("\n");
2156a86c6181SAlex Tomas #ifdef EXTENTS_STATS
2157a86c6181SAlex Tomas 		spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2158a86c6181SAlex Tomas 		EXT4_SB(sb)->s_ext_min = 1 << 30;
2159a86c6181SAlex Tomas 		EXT4_SB(sb)->s_ext_max = 0;
2160a86c6181SAlex Tomas #endif
2161a86c6181SAlex Tomas 	}
2162a86c6181SAlex Tomas }
2163a86c6181SAlex Tomas 
2164a86c6181SAlex Tomas /*
2165a86c6181SAlex Tomas  * called at umount time
2166a86c6181SAlex Tomas  */
2167a86c6181SAlex Tomas void ext4_ext_release(struct super_block *sb)
2168a86c6181SAlex Tomas {
2169a86c6181SAlex Tomas 	if (!test_opt(sb, EXTENTS))
2170a86c6181SAlex Tomas 		return;
2171a86c6181SAlex Tomas 
2172a86c6181SAlex Tomas #ifdef EXTENTS_STATS
2173a86c6181SAlex Tomas 	if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2174a86c6181SAlex Tomas 		struct ext4_sb_info *sbi = EXT4_SB(sb);
2175a86c6181SAlex Tomas 		printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2176a86c6181SAlex Tomas 			sbi->s_ext_blocks, sbi->s_ext_extents,
2177a86c6181SAlex Tomas 			sbi->s_ext_blocks / sbi->s_ext_extents);
2178a86c6181SAlex Tomas 		printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2179a86c6181SAlex Tomas 			sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2180a86c6181SAlex Tomas 	}
2181a86c6181SAlex Tomas #endif
2182a86c6181SAlex Tomas }
2183a86c6181SAlex Tomas 
2184093a088bSAneesh Kumar K.V static void bi_complete(struct bio *bio, int error)
2185093a088bSAneesh Kumar K.V {
2186093a088bSAneesh Kumar K.V 	complete((struct completion *)bio->bi_private);
2187093a088bSAneesh Kumar K.V }
2188093a088bSAneesh Kumar K.V 
2189093a088bSAneesh Kumar K.V /* FIXME!! we need to try to merge to left or right after zero-out  */
2190093a088bSAneesh Kumar K.V static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2191093a088bSAneesh Kumar K.V {
2192093a088bSAneesh Kumar K.V 	int ret = -EIO;
2193093a088bSAneesh Kumar K.V 	struct bio *bio;
2194093a088bSAneesh Kumar K.V 	int blkbits, blocksize;
2195093a088bSAneesh Kumar K.V 	sector_t ee_pblock;
2196093a088bSAneesh Kumar K.V 	struct completion event;
2197093a088bSAneesh Kumar K.V 	unsigned int ee_len, len, done, offset;
2198093a088bSAneesh Kumar K.V 
2199093a088bSAneesh Kumar K.V 
2200093a088bSAneesh Kumar K.V 	blkbits   = inode->i_blkbits;
2201093a088bSAneesh Kumar K.V 	blocksize = inode->i_sb->s_blocksize;
2202093a088bSAneesh Kumar K.V 	ee_len    = ext4_ext_get_actual_len(ex);
2203093a088bSAneesh Kumar K.V 	ee_pblock = ext_pblock(ex);
2204093a088bSAneesh Kumar K.V 
2205093a088bSAneesh Kumar K.V 	/* convert ee_pblock to 512 byte sectors */
2206093a088bSAneesh Kumar K.V 	ee_pblock = ee_pblock << (blkbits - 9);
2207093a088bSAneesh Kumar K.V 
2208093a088bSAneesh Kumar K.V 	while (ee_len > 0) {
2209093a088bSAneesh Kumar K.V 
2210093a088bSAneesh Kumar K.V 		if (ee_len > BIO_MAX_PAGES)
2211093a088bSAneesh Kumar K.V 			len = BIO_MAX_PAGES;
2212093a088bSAneesh Kumar K.V 		else
2213093a088bSAneesh Kumar K.V 			len = ee_len;
2214093a088bSAneesh Kumar K.V 
2215093a088bSAneesh Kumar K.V 		bio = bio_alloc(GFP_NOIO, len);
2216093a088bSAneesh Kumar K.V 		if (!bio)
2217093a088bSAneesh Kumar K.V 			return -ENOMEM;
2218093a088bSAneesh Kumar K.V 		bio->bi_sector = ee_pblock;
2219093a088bSAneesh Kumar K.V 		bio->bi_bdev   = inode->i_sb->s_bdev;
2220093a088bSAneesh Kumar K.V 
2221093a088bSAneesh Kumar K.V 		done = 0;
2222093a088bSAneesh Kumar K.V 		offset = 0;
2223093a088bSAneesh Kumar K.V 		while (done < len) {
2224093a088bSAneesh Kumar K.V 			ret = bio_add_page(bio, ZERO_PAGE(0),
2225093a088bSAneesh Kumar K.V 							blocksize, offset);
2226093a088bSAneesh Kumar K.V 			if (ret != blocksize) {
2227093a088bSAneesh Kumar K.V 				/*
2228093a088bSAneesh Kumar K.V 				 * We can't add any more pages because of
2229093a088bSAneesh Kumar K.V 				 * hardware limitations.  Start a new bio.
2230093a088bSAneesh Kumar K.V 				 */
2231093a088bSAneesh Kumar K.V 				break;
2232093a088bSAneesh Kumar K.V 			}
2233093a088bSAneesh Kumar K.V 			done++;
2234093a088bSAneesh Kumar K.V 			offset += blocksize;
2235093a088bSAneesh Kumar K.V 			if (offset >= PAGE_CACHE_SIZE)
2236093a088bSAneesh Kumar K.V 				offset = 0;
2237093a088bSAneesh Kumar K.V 		}
2238093a088bSAneesh Kumar K.V 
2239093a088bSAneesh Kumar K.V 		init_completion(&event);
2240093a088bSAneesh Kumar K.V 		bio->bi_private = &event;
2241093a088bSAneesh Kumar K.V 		bio->bi_end_io = bi_complete;
2242093a088bSAneesh Kumar K.V 		submit_bio(WRITE, bio);
2243093a088bSAneesh Kumar K.V 		wait_for_completion(&event);
2244093a088bSAneesh Kumar K.V 
2245093a088bSAneesh Kumar K.V 		if (test_bit(BIO_UPTODATE, &bio->bi_flags))
2246093a088bSAneesh Kumar K.V 			ret = 0;
2247093a088bSAneesh Kumar K.V 		else {
2248093a088bSAneesh Kumar K.V 			ret = -EIO;
2249093a088bSAneesh Kumar K.V 			break;
2250093a088bSAneesh Kumar K.V 		}
2251093a088bSAneesh Kumar K.V 		bio_put(bio);
2252093a088bSAneesh Kumar K.V 		ee_len    -= done;
2253093a088bSAneesh Kumar K.V 		ee_pblock += done  << (blkbits - 9);
2254093a088bSAneesh Kumar K.V 	}
2255093a088bSAneesh Kumar K.V 	return ret;
2256093a088bSAneesh Kumar K.V }
2257093a088bSAneesh Kumar K.V 
22583977c965SAneesh Kumar K.V #define EXT4_EXT_ZERO_LEN 7
22593977c965SAneesh Kumar K.V 
226056055d3aSAmit Arora /*
226156055d3aSAmit Arora  * This function is called by ext4_ext_get_blocks() if someone tries to write
226256055d3aSAmit Arora  * to an uninitialized extent. It may result in splitting the uninitialized
226356055d3aSAmit Arora  * extent into multiple extents (upto three - one initialized and two
226456055d3aSAmit Arora  * uninitialized).
226556055d3aSAmit Arora  * There are three possibilities:
226656055d3aSAmit Arora  *   a> There is no split required: Entire extent should be initialized
226756055d3aSAmit Arora  *   b> Splits in two extents: Write is happening at either end of the extent
226856055d3aSAmit Arora  *   c> Splits in three extents: Somone is writing in middle of the extent
226956055d3aSAmit Arora  */
2270725d26d3SAneesh Kumar K.V static int ext4_ext_convert_to_initialized(handle_t *handle,
2271725d26d3SAneesh Kumar K.V 						struct inode *inode,
227256055d3aSAmit Arora 						struct ext4_ext_path *path,
2273725d26d3SAneesh Kumar K.V 						ext4_lblk_t iblock,
227456055d3aSAmit Arora 						unsigned long max_blocks)
227556055d3aSAmit Arora {
227695c3889cSAneesh Kumar K.V 	struct ext4_extent *ex, newex, orig_ex;
227756055d3aSAmit Arora 	struct ext4_extent *ex1 = NULL;
227856055d3aSAmit Arora 	struct ext4_extent *ex2 = NULL;
227956055d3aSAmit Arora 	struct ext4_extent *ex3 = NULL;
228056055d3aSAmit Arora 	struct ext4_extent_header *eh;
2281725d26d3SAneesh Kumar K.V 	ext4_lblk_t ee_block;
2282725d26d3SAneesh Kumar K.V 	unsigned int allocated, ee_len, depth;
228356055d3aSAmit Arora 	ext4_fsblk_t newblock;
228456055d3aSAmit Arora 	int err = 0;
228556055d3aSAmit Arora 	int ret = 0;
228656055d3aSAmit Arora 
228756055d3aSAmit Arora 	depth = ext_depth(inode);
228856055d3aSAmit Arora 	eh = path[depth].p_hdr;
228956055d3aSAmit Arora 	ex = path[depth].p_ext;
229056055d3aSAmit Arora 	ee_block = le32_to_cpu(ex->ee_block);
229156055d3aSAmit Arora 	ee_len = ext4_ext_get_actual_len(ex);
229256055d3aSAmit Arora 	allocated = ee_len - (iblock - ee_block);
229356055d3aSAmit Arora 	newblock = iblock - ee_block + ext_pblock(ex);
229456055d3aSAmit Arora 	ex2 = ex;
229595c3889cSAneesh Kumar K.V 	orig_ex.ee_block = ex->ee_block;
229695c3889cSAneesh Kumar K.V 	orig_ex.ee_len   = cpu_to_le16(ee_len);
229795c3889cSAneesh Kumar K.V 	ext4_ext_store_pblock(&orig_ex, ext_pblock(ex));
229856055d3aSAmit Arora 
22999df5643aSAneesh Kumar K.V 	err = ext4_ext_get_access(handle, inode, path + depth);
23009df5643aSAneesh Kumar K.V 	if (err)
23019df5643aSAneesh Kumar K.V 		goto out;
23023977c965SAneesh Kumar K.V 	/* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
23033977c965SAneesh Kumar K.V 	if (ee_len <= 2*EXT4_EXT_ZERO_LEN) {
23043977c965SAneesh Kumar K.V 		err =  ext4_ext_zeroout(inode, &orig_ex);
23053977c965SAneesh Kumar K.V 		if (err)
23063977c965SAneesh Kumar K.V 			goto fix_extent_len;
23073977c965SAneesh Kumar K.V 		/* update the extent length and mark as initialized */
23083977c965SAneesh Kumar K.V 		ex->ee_block = orig_ex.ee_block;
23093977c965SAneesh Kumar K.V 		ex->ee_len   = orig_ex.ee_len;
23103977c965SAneesh Kumar K.V 		ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
23113977c965SAneesh Kumar K.V 		ext4_ext_dirty(handle, inode, path + depth);
2312161e7b7cSAneesh Kumar K.V 		/* zeroed the full extent */
2313161e7b7cSAneesh Kumar K.V 		return allocated;
23143977c965SAneesh Kumar K.V 	}
23159df5643aSAneesh Kumar K.V 
231656055d3aSAmit Arora 	/* ex1: ee_block to iblock - 1 : uninitialized */
231756055d3aSAmit Arora 	if (iblock > ee_block) {
231856055d3aSAmit Arora 		ex1 = ex;
231956055d3aSAmit Arora 		ex1->ee_len = cpu_to_le16(iblock - ee_block);
232056055d3aSAmit Arora 		ext4_ext_mark_uninitialized(ex1);
232156055d3aSAmit Arora 		ex2 = &newex;
232256055d3aSAmit Arora 	}
232356055d3aSAmit Arora 	/*
232456055d3aSAmit Arora 	 * for sanity, update the length of the ex2 extent before
232556055d3aSAmit Arora 	 * we insert ex3, if ex1 is NULL. This is to avoid temporary
232656055d3aSAmit Arora 	 * overlap of blocks.
232756055d3aSAmit Arora 	 */
232856055d3aSAmit Arora 	if (!ex1 && allocated > max_blocks)
232956055d3aSAmit Arora 		ex2->ee_len = cpu_to_le16(max_blocks);
233056055d3aSAmit Arora 	/* ex3: to ee_block + ee_len : uninitialised */
233156055d3aSAmit Arora 	if (allocated > max_blocks) {
233256055d3aSAmit Arora 		unsigned int newdepth;
23333977c965SAneesh Kumar K.V 		/* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */
23343977c965SAneesh Kumar K.V 		if (allocated <= EXT4_EXT_ZERO_LEN) {
2335d03856bdSAneesh Kumar K.V 			/*
2336d03856bdSAneesh Kumar K.V 			 * iblock == ee_block is handled by the zerouout
2337d03856bdSAneesh Kumar K.V 			 * at the beginning.
2338d03856bdSAneesh Kumar K.V 			 * Mark first half uninitialized.
23393977c965SAneesh Kumar K.V 			 * Mark second half initialized and zero out the
23403977c965SAneesh Kumar K.V 			 * initialized extent
23413977c965SAneesh Kumar K.V 			 */
23423977c965SAneesh Kumar K.V 			ex->ee_block = orig_ex.ee_block;
23433977c965SAneesh Kumar K.V 			ex->ee_len   = cpu_to_le16(ee_len - allocated);
23443977c965SAneesh Kumar K.V 			ext4_ext_mark_uninitialized(ex);
23453977c965SAneesh Kumar K.V 			ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
23463977c965SAneesh Kumar K.V 			ext4_ext_dirty(handle, inode, path + depth);
23473977c965SAneesh Kumar K.V 
23483977c965SAneesh Kumar K.V 			ex3 = &newex;
23493977c965SAneesh Kumar K.V 			ex3->ee_block = cpu_to_le32(iblock);
23503977c965SAneesh Kumar K.V 			ext4_ext_store_pblock(ex3, newblock);
23513977c965SAneesh Kumar K.V 			ex3->ee_len = cpu_to_le16(allocated);
23523977c965SAneesh Kumar K.V 			err = ext4_ext_insert_extent(handle, inode, path, ex3);
23533977c965SAneesh Kumar K.V 			if (err == -ENOSPC) {
23543977c965SAneesh Kumar K.V 				err =  ext4_ext_zeroout(inode, &orig_ex);
23553977c965SAneesh Kumar K.V 				if (err)
23563977c965SAneesh Kumar K.V 					goto fix_extent_len;
23573977c965SAneesh Kumar K.V 				ex->ee_block = orig_ex.ee_block;
23583977c965SAneesh Kumar K.V 				ex->ee_len   = orig_ex.ee_len;
23593977c965SAneesh Kumar K.V 				ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
23603977c965SAneesh Kumar K.V 				ext4_ext_dirty(handle, inode, path + depth);
2361d03856bdSAneesh Kumar K.V 				/* blocks available from iblock */
2362161e7b7cSAneesh Kumar K.V 				return allocated;
23633977c965SAneesh Kumar K.V 
23643977c965SAneesh Kumar K.V 			} else if (err)
23653977c965SAneesh Kumar K.V 				goto fix_extent_len;
23663977c965SAneesh Kumar K.V 
2367161e7b7cSAneesh Kumar K.V 			/*
2368161e7b7cSAneesh Kumar K.V 			 * We need to zero out the second half because
2369161e7b7cSAneesh Kumar K.V 			 * an fallocate request can update file size and
2370161e7b7cSAneesh Kumar K.V 			 * converting the second half to initialized extent
2371161e7b7cSAneesh Kumar K.V 			 * implies that we can leak some junk data to user
2372161e7b7cSAneesh Kumar K.V 			 * space.
2373161e7b7cSAneesh Kumar K.V 			 */
2374161e7b7cSAneesh Kumar K.V 			err =  ext4_ext_zeroout(inode, ex3);
2375161e7b7cSAneesh Kumar K.V 			if (err) {
2376161e7b7cSAneesh Kumar K.V 				/*
2377161e7b7cSAneesh Kumar K.V 				 * We should actually mark the
2378161e7b7cSAneesh Kumar K.V 				 * second half as uninit and return error
2379161e7b7cSAneesh Kumar K.V 				 * Insert would have changed the extent
2380161e7b7cSAneesh Kumar K.V 				 */
2381161e7b7cSAneesh Kumar K.V 				depth = ext_depth(inode);
2382161e7b7cSAneesh Kumar K.V 				ext4_ext_drop_refs(path);
2383161e7b7cSAneesh Kumar K.V 				path = ext4_ext_find_extent(inode,
2384161e7b7cSAneesh Kumar K.V 								iblock, path);
2385161e7b7cSAneesh Kumar K.V 				if (IS_ERR(path)) {
2386161e7b7cSAneesh Kumar K.V 					err = PTR_ERR(path);
2387161e7b7cSAneesh Kumar K.V 					return err;
2388161e7b7cSAneesh Kumar K.V 				}
2389d03856bdSAneesh Kumar K.V 				/* get the second half extent details */
2390161e7b7cSAneesh Kumar K.V 				ex = path[depth].p_ext;
2391161e7b7cSAneesh Kumar K.V 				err = ext4_ext_get_access(handle, inode,
2392161e7b7cSAneesh Kumar K.V 								path + depth);
2393161e7b7cSAneesh Kumar K.V 				if (err)
2394161e7b7cSAneesh Kumar K.V 					return err;
2395161e7b7cSAneesh Kumar K.V 				ext4_ext_mark_uninitialized(ex);
2396161e7b7cSAneesh Kumar K.V 				ext4_ext_dirty(handle, inode, path + depth);
2397161e7b7cSAneesh Kumar K.V 				return err;
2398161e7b7cSAneesh Kumar K.V 			}
2399161e7b7cSAneesh Kumar K.V 
2400161e7b7cSAneesh Kumar K.V 			/* zeroed the second half */
24013977c965SAneesh Kumar K.V 			return allocated;
24023977c965SAneesh Kumar K.V 		}
240356055d3aSAmit Arora 		ex3 = &newex;
240456055d3aSAmit Arora 		ex3->ee_block = cpu_to_le32(iblock + max_blocks);
240556055d3aSAmit Arora 		ext4_ext_store_pblock(ex3, newblock + max_blocks);
240656055d3aSAmit Arora 		ex3->ee_len = cpu_to_le16(allocated - max_blocks);
240756055d3aSAmit Arora 		ext4_ext_mark_uninitialized(ex3);
240856055d3aSAmit Arora 		err = ext4_ext_insert_extent(handle, inode, path, ex3);
2409093a088bSAneesh Kumar K.V 		if (err == -ENOSPC) {
2410093a088bSAneesh Kumar K.V 			err =  ext4_ext_zeroout(inode, &orig_ex);
2411093a088bSAneesh Kumar K.V 			if (err)
2412093a088bSAneesh Kumar K.V 				goto fix_extent_len;
2413093a088bSAneesh Kumar K.V 			/* update the extent length and mark as initialized */
241495c3889cSAneesh Kumar K.V 			ex->ee_block = orig_ex.ee_block;
241595c3889cSAneesh Kumar K.V 			ex->ee_len   = orig_ex.ee_len;
241695c3889cSAneesh Kumar K.V 			ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
241795c3889cSAneesh Kumar K.V 			ext4_ext_dirty(handle, inode, path + depth);
2418161e7b7cSAneesh Kumar K.V 			/* zeroed the full extent */
2419d03856bdSAneesh Kumar K.V 			/* blocks available from iblock */
2420161e7b7cSAneesh Kumar K.V 			return allocated;
2421093a088bSAneesh Kumar K.V 
2422093a088bSAneesh Kumar K.V 		} else if (err)
2423093a088bSAneesh Kumar K.V 			goto fix_extent_len;
242456055d3aSAmit Arora 		/*
242556055d3aSAmit Arora 		 * The depth, and hence eh & ex might change
242656055d3aSAmit Arora 		 * as part of the insert above.
242756055d3aSAmit Arora 		 */
242856055d3aSAmit Arora 		newdepth = ext_depth(inode);
242995c3889cSAneesh Kumar K.V 		/*
243095c3889cSAneesh Kumar K.V 		 * update the extent length after successfull insert of the
243195c3889cSAneesh Kumar K.V 		 * split extent
243295c3889cSAneesh Kumar K.V 		 */
243395c3889cSAneesh Kumar K.V 		orig_ex.ee_len = cpu_to_le16(ee_len -
243495c3889cSAneesh Kumar K.V 						ext4_ext_get_actual_len(ex3));
243556055d3aSAmit Arora 		depth = newdepth;
2436b35905c1SAneesh Kumar K.V 		ext4_ext_drop_refs(path);
2437b35905c1SAneesh Kumar K.V 		path = ext4_ext_find_extent(inode, iblock, path);
243856055d3aSAmit Arora 		if (IS_ERR(path)) {
243956055d3aSAmit Arora 			err = PTR_ERR(path);
244056055d3aSAmit Arora 			goto out;
244156055d3aSAmit Arora 		}
244256055d3aSAmit Arora 		eh = path[depth].p_hdr;
244356055d3aSAmit Arora 		ex = path[depth].p_ext;
244456055d3aSAmit Arora 		if (ex2 != &newex)
244556055d3aSAmit Arora 			ex2 = ex;
24469df5643aSAneesh Kumar K.V 
24479df5643aSAneesh Kumar K.V 		err = ext4_ext_get_access(handle, inode, path + depth);
24489df5643aSAneesh Kumar K.V 		if (err)
24499df5643aSAneesh Kumar K.V 			goto out;
2450d03856bdSAneesh Kumar K.V 
245156055d3aSAmit Arora 		allocated = max_blocks;
24523977c965SAneesh Kumar K.V 
24533977c965SAneesh Kumar K.V 		/* If extent has less than EXT4_EXT_ZERO_LEN and we are trying
24543977c965SAneesh Kumar K.V 		 * to insert a extent in the middle zerout directly
24553977c965SAneesh Kumar K.V 		 * otherwise give the extent a chance to merge to left
24563977c965SAneesh Kumar K.V 		 */
24573977c965SAneesh Kumar K.V 		if (le16_to_cpu(orig_ex.ee_len) <= EXT4_EXT_ZERO_LEN &&
24583977c965SAneesh Kumar K.V 							iblock != ee_block) {
24593977c965SAneesh Kumar K.V 			err =  ext4_ext_zeroout(inode, &orig_ex);
24603977c965SAneesh Kumar K.V 			if (err)
24613977c965SAneesh Kumar K.V 				goto fix_extent_len;
24623977c965SAneesh Kumar K.V 			/* update the extent length and mark as initialized */
24633977c965SAneesh Kumar K.V 			ex->ee_block = orig_ex.ee_block;
24643977c965SAneesh Kumar K.V 			ex->ee_len   = orig_ex.ee_len;
24653977c965SAneesh Kumar K.V 			ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
24663977c965SAneesh Kumar K.V 			ext4_ext_dirty(handle, inode, path + depth);
2467161e7b7cSAneesh Kumar K.V 			/* zero out the first half */
2468d03856bdSAneesh Kumar K.V 			/* blocks available from iblock */
2469161e7b7cSAneesh Kumar K.V 			return allocated;
24703977c965SAneesh Kumar K.V 		}
247156055d3aSAmit Arora 	}
247256055d3aSAmit Arora 	/*
247356055d3aSAmit Arora 	 * If there was a change of depth as part of the
247456055d3aSAmit Arora 	 * insertion of ex3 above, we need to update the length
247556055d3aSAmit Arora 	 * of the ex1 extent again here
247656055d3aSAmit Arora 	 */
247756055d3aSAmit Arora 	if (ex1 && ex1 != ex) {
247856055d3aSAmit Arora 		ex1 = ex;
247956055d3aSAmit Arora 		ex1->ee_len = cpu_to_le16(iblock - ee_block);
248056055d3aSAmit Arora 		ext4_ext_mark_uninitialized(ex1);
248156055d3aSAmit Arora 		ex2 = &newex;
248256055d3aSAmit Arora 	}
248356055d3aSAmit Arora 	/* ex2: iblock to iblock + maxblocks-1 : initialised */
248456055d3aSAmit Arora 	ex2->ee_block = cpu_to_le32(iblock);
248556055d3aSAmit Arora 	ext4_ext_store_pblock(ex2, newblock);
248656055d3aSAmit Arora 	ex2->ee_len = cpu_to_le16(allocated);
248756055d3aSAmit Arora 	if (ex2 != ex)
248856055d3aSAmit Arora 		goto insert;
248956055d3aSAmit Arora 	/*
249056055d3aSAmit Arora 	 * New (initialized) extent starts from the first block
249156055d3aSAmit Arora 	 * in the current extent. i.e., ex2 == ex
249256055d3aSAmit Arora 	 * We have to see if it can be merged with the extent
249356055d3aSAmit Arora 	 * on the left.
249456055d3aSAmit Arora 	 */
249556055d3aSAmit Arora 	if (ex2 > EXT_FIRST_EXTENT(eh)) {
249656055d3aSAmit Arora 		/*
249756055d3aSAmit Arora 		 * To merge left, pass "ex2 - 1" to try_to_merge(),
249856055d3aSAmit Arora 		 * since it merges towards right _only_.
249956055d3aSAmit Arora 		 */
250056055d3aSAmit Arora 		ret = ext4_ext_try_to_merge(inode, path, ex2 - 1);
250156055d3aSAmit Arora 		if (ret) {
250256055d3aSAmit Arora 			err = ext4_ext_correct_indexes(handle, inode, path);
250356055d3aSAmit Arora 			if (err)
250456055d3aSAmit Arora 				goto out;
250556055d3aSAmit Arora 			depth = ext_depth(inode);
250656055d3aSAmit Arora 			ex2--;
250756055d3aSAmit Arora 		}
250856055d3aSAmit Arora 	}
250956055d3aSAmit Arora 	/*
251056055d3aSAmit Arora 	 * Try to Merge towards right. This might be required
251156055d3aSAmit Arora 	 * only when the whole extent is being written to.
251256055d3aSAmit Arora 	 * i.e. ex2 == ex and ex3 == NULL.
251356055d3aSAmit Arora 	 */
251456055d3aSAmit Arora 	if (!ex3) {
251556055d3aSAmit Arora 		ret = ext4_ext_try_to_merge(inode, path, ex2);
251656055d3aSAmit Arora 		if (ret) {
251756055d3aSAmit Arora 			err = ext4_ext_correct_indexes(handle, inode, path);
251856055d3aSAmit Arora 			if (err)
251956055d3aSAmit Arora 				goto out;
252056055d3aSAmit Arora 		}
252156055d3aSAmit Arora 	}
252256055d3aSAmit Arora 	/* Mark modified extent as dirty */
252356055d3aSAmit Arora 	err = ext4_ext_dirty(handle, inode, path + depth);
252456055d3aSAmit Arora 	goto out;
252556055d3aSAmit Arora insert:
252656055d3aSAmit Arora 	err = ext4_ext_insert_extent(handle, inode, path, &newex);
2527093a088bSAneesh Kumar K.V 	if (err == -ENOSPC) {
2528093a088bSAneesh Kumar K.V 		err =  ext4_ext_zeroout(inode, &orig_ex);
2529093a088bSAneesh Kumar K.V 		if (err)
2530093a088bSAneesh Kumar K.V 			goto fix_extent_len;
2531093a088bSAneesh Kumar K.V 		/* update the extent length and mark as initialized */
2532093a088bSAneesh Kumar K.V 		ex->ee_block = orig_ex.ee_block;
2533093a088bSAneesh Kumar K.V 		ex->ee_len   = orig_ex.ee_len;
2534093a088bSAneesh Kumar K.V 		ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2535093a088bSAneesh Kumar K.V 		ext4_ext_dirty(handle, inode, path + depth);
2536161e7b7cSAneesh Kumar K.V 		/* zero out the first half */
2537161e7b7cSAneesh Kumar K.V 		return allocated;
2538093a088bSAneesh Kumar K.V 	} else if (err)
2539093a088bSAneesh Kumar K.V 		goto fix_extent_len;
2540093a088bSAneesh Kumar K.V out:
2541093a088bSAneesh Kumar K.V 	return err ? err : allocated;
2542093a088bSAneesh Kumar K.V 
2543093a088bSAneesh Kumar K.V fix_extent_len:
254495c3889cSAneesh Kumar K.V 	ex->ee_block = orig_ex.ee_block;
254595c3889cSAneesh Kumar K.V 	ex->ee_len   = orig_ex.ee_len;
254695c3889cSAneesh Kumar K.V 	ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
254795c3889cSAneesh Kumar K.V 	ext4_ext_mark_uninitialized(ex);
254895c3889cSAneesh Kumar K.V 	ext4_ext_dirty(handle, inode, path + depth);
2549093a088bSAneesh Kumar K.V 	return err;
255056055d3aSAmit Arora }
255156055d3aSAmit Arora 
2552c278bfecSAneesh Kumar K.V /*
2553f5ab0d1fSMingming Cao  * Block allocation/map/preallocation routine for extents based files
2554f5ab0d1fSMingming Cao  *
2555f5ab0d1fSMingming Cao  *
2556c278bfecSAneesh Kumar K.V  * Need to be called with
25570e855ac8SAneesh Kumar K.V  * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
25580e855ac8SAneesh Kumar K.V  * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
2559f5ab0d1fSMingming Cao  *
2560f5ab0d1fSMingming Cao  * return > 0, number of of blocks already mapped/allocated
2561f5ab0d1fSMingming Cao  *          if create == 0 and these are pre-allocated blocks
2562f5ab0d1fSMingming Cao  *          	buffer head is unmapped
2563f5ab0d1fSMingming Cao  *          otherwise blocks are mapped
2564f5ab0d1fSMingming Cao  *
2565f5ab0d1fSMingming Cao  * return = 0, if plain look up failed (blocks have not been allocated)
2566f5ab0d1fSMingming Cao  *          buffer head is unmapped
2567f5ab0d1fSMingming Cao  *
2568f5ab0d1fSMingming Cao  * return < 0, error case.
2569c278bfecSAneesh Kumar K.V  */
2570f65e6fbaSAlex Tomas int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
2571725d26d3SAneesh Kumar K.V 			ext4_lblk_t iblock,
2572a86c6181SAlex Tomas 			unsigned long max_blocks, struct buffer_head *bh_result,
2573a86c6181SAlex Tomas 			int create, int extend_disksize)
2574a86c6181SAlex Tomas {
2575a86c6181SAlex Tomas 	struct ext4_ext_path *path = NULL;
257656055d3aSAmit Arora 	struct ext4_extent_header *eh;
2577a86c6181SAlex Tomas 	struct ext4_extent newex, *ex;
2578f65e6fbaSAlex Tomas 	ext4_fsblk_t goal, newblock;
257956055d3aSAmit Arora 	int err = 0, depth, ret;
2580a86c6181SAlex Tomas 	unsigned long allocated = 0;
2581c9de560dSAlex Tomas 	struct ext4_allocation_request ar;
258261628a3fSMingming Cao 	loff_t disksize;
2583a86c6181SAlex Tomas 
2584a86c6181SAlex Tomas 	__clear_bit(BH_New, &bh_result->b_state);
2585bba90743SEric Sandeen 	ext_debug("blocks %u/%lu requested for inode %u\n",
2586bba90743SEric Sandeen 			iblock, max_blocks, inode->i_ino);
2587a86c6181SAlex Tomas 
2588a86c6181SAlex Tomas 	/* check in cache */
25897e028976SAvantika Mathur 	goal = ext4_ext_in_cache(inode, iblock, &newex);
25907e028976SAvantika Mathur 	if (goal) {
2591a86c6181SAlex Tomas 		if (goal == EXT4_EXT_CACHE_GAP) {
2592a86c6181SAlex Tomas 			if (!create) {
259356055d3aSAmit Arora 				/*
259456055d3aSAmit Arora 				 * block isn't allocated yet and
259556055d3aSAmit Arora 				 * user doesn't want to allocate it
259656055d3aSAmit Arora 				 */
2597a86c6181SAlex Tomas 				goto out2;
2598a86c6181SAlex Tomas 			}
2599a86c6181SAlex Tomas 			/* we should allocate requested block */
2600a86c6181SAlex Tomas 		} else if (goal == EXT4_EXT_CACHE_EXTENT) {
2601a86c6181SAlex Tomas 			/* block is already allocated */
2602a86c6181SAlex Tomas 			newblock = iblock
2603a86c6181SAlex Tomas 				   - le32_to_cpu(newex.ee_block)
2604f65e6fbaSAlex Tomas 				   + ext_pblock(&newex);
2605d0d856e8SRandy Dunlap 			/* number of remaining blocks in the extent */
2606b939e376SAneesh Kumar K.V 			allocated = ext4_ext_get_actual_len(&newex) -
2607a86c6181SAlex Tomas 					(iblock - le32_to_cpu(newex.ee_block));
2608a86c6181SAlex Tomas 			goto out;
2609a86c6181SAlex Tomas 		} else {
2610a86c6181SAlex Tomas 			BUG();
2611a86c6181SAlex Tomas 		}
2612a86c6181SAlex Tomas 	}
2613a86c6181SAlex Tomas 
2614a86c6181SAlex Tomas 	/* find extent for this block */
2615a86c6181SAlex Tomas 	path = ext4_ext_find_extent(inode, iblock, NULL);
2616a86c6181SAlex Tomas 	if (IS_ERR(path)) {
2617a86c6181SAlex Tomas 		err = PTR_ERR(path);
2618a86c6181SAlex Tomas 		path = NULL;
2619a86c6181SAlex Tomas 		goto out2;
2620a86c6181SAlex Tomas 	}
2621a86c6181SAlex Tomas 
2622a86c6181SAlex Tomas 	depth = ext_depth(inode);
2623a86c6181SAlex Tomas 
2624a86c6181SAlex Tomas 	/*
2625d0d856e8SRandy Dunlap 	 * consistent leaf must not be empty;
2626d0d856e8SRandy Dunlap 	 * this situation is possible, though, _during_ tree modification;
2627a86c6181SAlex Tomas 	 * this is why assert can't be put in ext4_ext_find_extent()
2628a86c6181SAlex Tomas 	 */
2629a86c6181SAlex Tomas 	BUG_ON(path[depth].p_ext == NULL && depth != 0);
263056055d3aSAmit Arora 	eh = path[depth].p_hdr;
2631a86c6181SAlex Tomas 
26327e028976SAvantika Mathur 	ex = path[depth].p_ext;
26337e028976SAvantika Mathur 	if (ex) {
2634725d26d3SAneesh Kumar K.V 		ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
2635f65e6fbaSAlex Tomas 		ext4_fsblk_t ee_start = ext_pblock(ex);
2636a2df2a63SAmit Arora 		unsigned short ee_len;
2637471d4011SSuparna Bhattacharya 
2638471d4011SSuparna Bhattacharya 		/*
2639471d4011SSuparna Bhattacharya 		 * Uninitialized extents are treated as holes, except that
264056055d3aSAmit Arora 		 * we split out initialized portions during a write.
2641471d4011SSuparna Bhattacharya 		 */
2642a2df2a63SAmit Arora 		ee_len = ext4_ext_get_actual_len(ex);
2643d0d856e8SRandy Dunlap 		/* if found extent covers block, simply return it */
2644a86c6181SAlex Tomas 		if (iblock >= ee_block && iblock < ee_block + ee_len) {
2645a86c6181SAlex Tomas 			newblock = iblock - ee_block + ee_start;
2646d0d856e8SRandy Dunlap 			/* number of remaining blocks in the extent */
2647a86c6181SAlex Tomas 			allocated = ee_len - (iblock - ee_block);
2648bba90743SEric Sandeen 			ext_debug("%u fit into %lu:%d -> %llu\n", iblock,
2649a86c6181SAlex Tomas 					ee_block, ee_len, newblock);
265056055d3aSAmit Arora 
2651a2df2a63SAmit Arora 			/* Do not put uninitialized extent in the cache */
265256055d3aSAmit Arora 			if (!ext4_ext_is_uninitialized(ex)) {
2653a2df2a63SAmit Arora 				ext4_ext_put_in_cache(inode, ee_block,
2654a2df2a63SAmit Arora 							ee_len, ee_start,
2655a2df2a63SAmit Arora 							EXT4_EXT_CACHE_EXTENT);
2656a86c6181SAlex Tomas 				goto out;
2657a86c6181SAlex Tomas 			}
265856055d3aSAmit Arora 			if (create == EXT4_CREATE_UNINITIALIZED_EXT)
265956055d3aSAmit Arora 				goto out;
2660e067ba00SAneesh Kumar K.V 			if (!create) {
2661e067ba00SAneesh Kumar K.V 				/*
2662e067ba00SAneesh Kumar K.V 				 * We have blocks reserved already.  We
2663e067ba00SAneesh Kumar K.V 				 * return allocated blocks so that delalloc
2664e067ba00SAneesh Kumar K.V 				 * won't do block reservation for us.  But
2665e067ba00SAneesh Kumar K.V 				 * the buffer head will be unmapped so that
2666e067ba00SAneesh Kumar K.V 				 * a read from the block returns 0s.
2667e067ba00SAneesh Kumar K.V 				 */
2668e067ba00SAneesh Kumar K.V 				if (allocated > max_blocks)
2669e067ba00SAneesh Kumar K.V 					allocated = max_blocks;
2670953e622bSEric Sandeen 				set_buffer_unwritten(bh_result);
267156055d3aSAmit Arora 				goto out2;
2672e067ba00SAneesh Kumar K.V 			}
267356055d3aSAmit Arora 
267456055d3aSAmit Arora 			ret = ext4_ext_convert_to_initialized(handle, inode,
267556055d3aSAmit Arora 								path, iblock,
267656055d3aSAmit Arora 								max_blocks);
2677dbf9d7daSDmitry Monakhov 			if (ret <= 0) {
2678dbf9d7daSDmitry Monakhov 				err = ret;
267956055d3aSAmit Arora 				goto out2;
2680dbf9d7daSDmitry Monakhov 			} else
268156055d3aSAmit Arora 				allocated = ret;
268256055d3aSAmit Arora 			goto outnew;
268356055d3aSAmit Arora 		}
2684a86c6181SAlex Tomas 	}
2685a86c6181SAlex Tomas 
2686a86c6181SAlex Tomas 	/*
2687d0d856e8SRandy Dunlap 	 * requested block isn't allocated yet;
2688a86c6181SAlex Tomas 	 * we couldn't try to create block if create flag is zero
2689a86c6181SAlex Tomas 	 */
2690a86c6181SAlex Tomas 	if (!create) {
269156055d3aSAmit Arora 		/*
269256055d3aSAmit Arora 		 * put just found gap into cache to speed up
269356055d3aSAmit Arora 		 * subsequent requests
269456055d3aSAmit Arora 		 */
2695a86c6181SAlex Tomas 		ext4_ext_put_gap_in_cache(inode, path, iblock);
2696a86c6181SAlex Tomas 		goto out2;
2697a86c6181SAlex Tomas 	}
2698a86c6181SAlex Tomas 	/*
2699a86c6181SAlex Tomas 	 * Okay, we need to do block allocation.  Lazily initialize the block
2700d0d856e8SRandy Dunlap 	 * allocation info here if necessary.
2701a86c6181SAlex Tomas 	 */
2702a86c6181SAlex Tomas 	if (S_ISREG(inode->i_mode) && (!EXT4_I(inode)->i_block_alloc_info))
2703a86c6181SAlex Tomas 		ext4_init_block_alloc_info(inode);
2704a86c6181SAlex Tomas 
2705c9de560dSAlex Tomas 	/* find neighbour allocated blocks */
2706c9de560dSAlex Tomas 	ar.lleft = iblock;
2707c9de560dSAlex Tomas 	err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
2708c9de560dSAlex Tomas 	if (err)
2709c9de560dSAlex Tomas 		goto out2;
2710c9de560dSAlex Tomas 	ar.lright = iblock;
2711c9de560dSAlex Tomas 	err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright);
2712c9de560dSAlex Tomas 	if (err)
2713c9de560dSAlex Tomas 		goto out2;
271425d14f98SAmit Arora 
2715749269faSAmit Arora 	/*
2716749269faSAmit Arora 	 * See if request is beyond maximum number of blocks we can have in
2717749269faSAmit Arora 	 * a single extent. For an initialized extent this limit is
2718749269faSAmit Arora 	 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
2719749269faSAmit Arora 	 * EXT_UNINIT_MAX_LEN.
2720749269faSAmit Arora 	 */
2721749269faSAmit Arora 	if (max_blocks > EXT_INIT_MAX_LEN &&
2722749269faSAmit Arora 	    create != EXT4_CREATE_UNINITIALIZED_EXT)
2723749269faSAmit Arora 		max_blocks = EXT_INIT_MAX_LEN;
2724749269faSAmit Arora 	else if (max_blocks > EXT_UNINIT_MAX_LEN &&
2725749269faSAmit Arora 		 create == EXT4_CREATE_UNINITIALIZED_EXT)
2726749269faSAmit Arora 		max_blocks = EXT_UNINIT_MAX_LEN;
2727749269faSAmit Arora 
272825d14f98SAmit Arora 	/* Check if we can really insert (iblock)::(iblock+max_blocks) extent */
272925d14f98SAmit Arora 	newex.ee_block = cpu_to_le32(iblock);
273025d14f98SAmit Arora 	newex.ee_len = cpu_to_le16(max_blocks);
273125d14f98SAmit Arora 	err = ext4_ext_check_overlap(inode, &newex, path);
273225d14f98SAmit Arora 	if (err)
2733b939e376SAneesh Kumar K.V 		allocated = ext4_ext_get_actual_len(&newex);
273425d14f98SAmit Arora 	else
2735a86c6181SAlex Tomas 		allocated = max_blocks;
2736c9de560dSAlex Tomas 
2737c9de560dSAlex Tomas 	/* allocate new block */
2738c9de560dSAlex Tomas 	ar.inode = inode;
2739c9de560dSAlex Tomas 	ar.goal = ext4_ext_find_goal(inode, path, iblock);
2740c9de560dSAlex Tomas 	ar.logical = iblock;
2741c9de560dSAlex Tomas 	ar.len = allocated;
2742c9de560dSAlex Tomas 	if (S_ISREG(inode->i_mode))
2743c9de560dSAlex Tomas 		ar.flags = EXT4_MB_HINT_DATA;
2744c9de560dSAlex Tomas 	else
2745c9de560dSAlex Tomas 		/* disable in-core preallocation for non-regular files */
2746c9de560dSAlex Tomas 		ar.flags = 0;
2747c9de560dSAlex Tomas 	newblock = ext4_mb_new_blocks(handle, &ar, &err);
2748a86c6181SAlex Tomas 	if (!newblock)
2749a86c6181SAlex Tomas 		goto out2;
27502ae02107SMingming Cao 	ext_debug("allocate new block: goal %llu, found %llu/%lu\n",
2751a86c6181SAlex Tomas 			goal, newblock, allocated);
2752a86c6181SAlex Tomas 
2753a86c6181SAlex Tomas 	/* try to insert new extent into found leaf and return */
2754f65e6fbaSAlex Tomas 	ext4_ext_store_pblock(&newex, newblock);
2755c9de560dSAlex Tomas 	newex.ee_len = cpu_to_le16(ar.len);
2756a2df2a63SAmit Arora 	if (create == EXT4_CREATE_UNINITIALIZED_EXT)  /* Mark uninitialized */
2757a2df2a63SAmit Arora 		ext4_ext_mark_uninitialized(&newex);
2758a86c6181SAlex Tomas 	err = ext4_ext_insert_extent(handle, inode, path, &newex);
2759315054f0SAlex Tomas 	if (err) {
2760315054f0SAlex Tomas 		/* free data blocks we just allocated */
2761c9de560dSAlex Tomas 		/* not a good idea to call discard here directly,
2762c9de560dSAlex Tomas 		 * but otherwise we'd need to call it every free() */
2763c9de560dSAlex Tomas 		ext4_mb_discard_inode_preallocations(inode);
2764315054f0SAlex Tomas 		ext4_free_blocks(handle, inode, ext_pblock(&newex),
2765b939e376SAneesh Kumar K.V 					ext4_ext_get_actual_len(&newex), 0);
2766a86c6181SAlex Tomas 		goto out2;
2767315054f0SAlex Tomas 	}
2768a86c6181SAlex Tomas 
2769a86c6181SAlex Tomas 	/* previous routine could use block we allocated */
2770f65e6fbaSAlex Tomas 	newblock = ext_pblock(&newex);
2771b939e376SAneesh Kumar K.V 	allocated = ext4_ext_get_actual_len(&newex);
277256055d3aSAmit Arora outnew:
277361628a3fSMingming Cao 	if (extend_disksize) {
277461628a3fSMingming Cao 		disksize = ((loff_t) iblock + ar.len) << inode->i_blkbits;
277561628a3fSMingming Cao 		if (disksize > i_size_read(inode))
277661628a3fSMingming Cao 			disksize = i_size_read(inode);
277761628a3fSMingming Cao 		if (disksize > EXT4_I(inode)->i_disksize)
277861628a3fSMingming Cao 			EXT4_I(inode)->i_disksize = disksize;
277961628a3fSMingming Cao 	}
2780a379cd1dSAneesh Kumar K.V 
2781953e622bSEric Sandeen 	set_buffer_new(bh_result);
2782a86c6181SAlex Tomas 
2783a2df2a63SAmit Arora 	/* Cache only when it is _not_ an uninitialized extent */
2784a2df2a63SAmit Arora 	if (create != EXT4_CREATE_UNINITIALIZED_EXT)
2785a86c6181SAlex Tomas 		ext4_ext_put_in_cache(inode, iblock, allocated, newblock,
2786a86c6181SAlex Tomas 						EXT4_EXT_CACHE_EXTENT);
2787a86c6181SAlex Tomas out:
2788a86c6181SAlex Tomas 	if (allocated > max_blocks)
2789a86c6181SAlex Tomas 		allocated = max_blocks;
2790a86c6181SAlex Tomas 	ext4_ext_show_leaf(inode, path);
2791953e622bSEric Sandeen 	set_buffer_mapped(bh_result);
2792a86c6181SAlex Tomas 	bh_result->b_bdev = inode->i_sb->s_bdev;
2793a86c6181SAlex Tomas 	bh_result->b_blocknr = newblock;
2794a86c6181SAlex Tomas out2:
2795a86c6181SAlex Tomas 	if (path) {
2796a86c6181SAlex Tomas 		ext4_ext_drop_refs(path);
2797a86c6181SAlex Tomas 		kfree(path);
2798a86c6181SAlex Tomas 	}
2799a86c6181SAlex Tomas 	return err ? err : allocated;
2800a86c6181SAlex Tomas }
2801a86c6181SAlex Tomas 
2802cf108bcaSJan Kara void ext4_ext_truncate(struct inode *inode)
2803a86c6181SAlex Tomas {
2804a86c6181SAlex Tomas 	struct address_space *mapping = inode->i_mapping;
2805a86c6181SAlex Tomas 	struct super_block *sb = inode->i_sb;
2806725d26d3SAneesh Kumar K.V 	ext4_lblk_t last_block;
2807a86c6181SAlex Tomas 	handle_t *handle;
2808a86c6181SAlex Tomas 	int err = 0;
2809a86c6181SAlex Tomas 
2810a86c6181SAlex Tomas 	/*
2811a86c6181SAlex Tomas 	 * probably first extent we're gonna free will be last in block
2812a86c6181SAlex Tomas 	 */
2813a86c6181SAlex Tomas 	err = ext4_writepage_trans_blocks(inode) + 3;
2814a86c6181SAlex Tomas 	handle = ext4_journal_start(inode, err);
2815cf108bcaSJan Kara 	if (IS_ERR(handle))
2816a86c6181SAlex Tomas 		return;
2817a86c6181SAlex Tomas 
2818cf108bcaSJan Kara 	if (inode->i_size & (sb->s_blocksize - 1))
2819cf108bcaSJan Kara 		ext4_block_truncate_page(handle, mapping, inode->i_size);
2820a86c6181SAlex Tomas 
28219ddfc3dcSJan Kara 	if (ext4_orphan_add(handle, inode))
28229ddfc3dcSJan Kara 		goto out_stop;
28239ddfc3dcSJan Kara 
28240e855ac8SAneesh Kumar K.V 	down_write(&EXT4_I(inode)->i_data_sem);
2825a86c6181SAlex Tomas 	ext4_ext_invalidate_cache(inode);
2826a86c6181SAlex Tomas 
282788aa3cffSTheodore Ts'o 	ext4_discard_reservation(inode);
2828c9de560dSAlex Tomas 
2829a86c6181SAlex Tomas 	/*
2830d0d856e8SRandy Dunlap 	 * TODO: optimization is possible here.
2831d0d856e8SRandy Dunlap 	 * Probably we need not scan at all,
2832d0d856e8SRandy Dunlap 	 * because page truncation is enough.
2833a86c6181SAlex Tomas 	 */
2834a86c6181SAlex Tomas 
2835a86c6181SAlex Tomas 	/* we have to know where to truncate from in crash case */
2836a86c6181SAlex Tomas 	EXT4_I(inode)->i_disksize = inode->i_size;
2837a86c6181SAlex Tomas 	ext4_mark_inode_dirty(handle, inode);
2838a86c6181SAlex Tomas 
2839a86c6181SAlex Tomas 	last_block = (inode->i_size + sb->s_blocksize - 1)
2840a86c6181SAlex Tomas 			>> EXT4_BLOCK_SIZE_BITS(sb);
2841a86c6181SAlex Tomas 	err = ext4_ext_remove_space(inode, last_block);
2842a86c6181SAlex Tomas 
2843a86c6181SAlex Tomas 	/* In a multi-transaction truncate, we only make the final
284456055d3aSAmit Arora 	 * transaction synchronous.
284556055d3aSAmit Arora 	 */
2846a86c6181SAlex Tomas 	if (IS_SYNC(inode))
2847a86c6181SAlex Tomas 		handle->h_sync = 1;
2848a86c6181SAlex Tomas 
2849a86c6181SAlex Tomas out_stop:
28509ddfc3dcSJan Kara 	up_write(&EXT4_I(inode)->i_data_sem);
2851a86c6181SAlex Tomas 	/*
2852d0d856e8SRandy Dunlap 	 * If this was a simple ftruncate() and the file will remain alive,
2853a86c6181SAlex Tomas 	 * then we need to clear up the orphan record which we created above.
2854a86c6181SAlex Tomas 	 * However, if this was a real unlink then we were called by
2855a86c6181SAlex Tomas 	 * ext4_delete_inode(), and we allow that function to clean up the
2856a86c6181SAlex Tomas 	 * orphan info for us.
2857a86c6181SAlex Tomas 	 */
2858a86c6181SAlex Tomas 	if (inode->i_nlink)
2859a86c6181SAlex Tomas 		ext4_orphan_del(handle, inode);
2860a86c6181SAlex Tomas 
2861ef737728SSolofo Ramangalahy 	inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
2862ef737728SSolofo Ramangalahy 	ext4_mark_inode_dirty(handle, inode);
2863a86c6181SAlex Tomas 	ext4_journal_stop(handle);
2864a86c6181SAlex Tomas }
2865a86c6181SAlex Tomas 
2866fd28784aSAneesh Kumar K.V static void ext4_falloc_update_inode(struct inode *inode,
2867fd28784aSAneesh Kumar K.V 				int mode, loff_t new_size, int update_ctime)
2868fd28784aSAneesh Kumar K.V {
2869fd28784aSAneesh Kumar K.V 	struct timespec now;
2870fd28784aSAneesh Kumar K.V 
2871fd28784aSAneesh Kumar K.V 	if (update_ctime) {
2872fd28784aSAneesh Kumar K.V 		now = current_fs_time(inode->i_sb);
2873fd28784aSAneesh Kumar K.V 		if (!timespec_equal(&inode->i_ctime, &now))
2874fd28784aSAneesh Kumar K.V 			inode->i_ctime = now;
2875fd28784aSAneesh Kumar K.V 	}
2876fd28784aSAneesh Kumar K.V 	/*
2877fd28784aSAneesh Kumar K.V 	 * Update only when preallocation was requested beyond
2878fd28784aSAneesh Kumar K.V 	 * the file size.
2879fd28784aSAneesh Kumar K.V 	 */
2880fd28784aSAneesh Kumar K.V 	if (!(mode & FALLOC_FL_KEEP_SIZE) &&
2881fd28784aSAneesh Kumar K.V 				new_size > i_size_read(inode)) {
2882fd28784aSAneesh Kumar K.V 		i_size_write(inode, new_size);
2883fd28784aSAneesh Kumar K.V 		EXT4_I(inode)->i_disksize = new_size;
2884fd28784aSAneesh Kumar K.V 	}
2885fd28784aSAneesh Kumar K.V 
2886fd28784aSAneesh Kumar K.V }
2887fd28784aSAneesh Kumar K.V 
2888a2df2a63SAmit Arora /*
2889a2df2a63SAmit Arora  * preallocate space for a file. This implements ext4's fallocate inode
2890a2df2a63SAmit Arora  * operation, which gets called from sys_fallocate system call.
2891a2df2a63SAmit Arora  * For block-mapped files, posix_fallocate should fall back to the method
2892a2df2a63SAmit Arora  * of writing zeroes to the required new blocks (the same behavior which is
2893a2df2a63SAmit Arora  * expected for file systems which do not support fallocate() system call).
2894a2df2a63SAmit Arora  */
2895a2df2a63SAmit Arora long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
2896a2df2a63SAmit Arora {
2897a2df2a63SAmit Arora 	handle_t *handle;
2898725d26d3SAneesh Kumar K.V 	ext4_lblk_t block;
2899fd28784aSAneesh Kumar K.V 	loff_t new_size;
2900725d26d3SAneesh Kumar K.V 	unsigned long max_blocks;
2901a2df2a63SAmit Arora 	int ret = 0;
2902a2df2a63SAmit Arora 	int ret2 = 0;
2903a2df2a63SAmit Arora 	int retries = 0;
2904a2df2a63SAmit Arora 	struct buffer_head map_bh;
2905a2df2a63SAmit Arora 	unsigned int credits, blkbits = inode->i_blkbits;
2906a2df2a63SAmit Arora 
2907a2df2a63SAmit Arora 	/*
2908a2df2a63SAmit Arora 	 * currently supporting (pre)allocate mode for extent-based
2909a2df2a63SAmit Arora 	 * files _only_
2910a2df2a63SAmit Arora 	 */
2911a2df2a63SAmit Arora 	if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
2912a2df2a63SAmit Arora 		return -EOPNOTSUPP;
2913a2df2a63SAmit Arora 
2914a2df2a63SAmit Arora 	/* preallocation to directories is currently not supported */
2915a2df2a63SAmit Arora 	if (S_ISDIR(inode->i_mode))
2916a2df2a63SAmit Arora 		return -ENODEV;
2917a2df2a63SAmit Arora 
2918a2df2a63SAmit Arora 	block = offset >> blkbits;
2919fd28784aSAneesh Kumar K.V 	/*
2920fd28784aSAneesh Kumar K.V 	 * We can't just convert len to max_blocks because
2921fd28784aSAneesh Kumar K.V 	 * If blocksize = 4096 offset = 3072 and len = 2048
2922fd28784aSAneesh Kumar K.V 	 */
2923a2df2a63SAmit Arora 	max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
2924a2df2a63SAmit Arora 							- block;
2925a2df2a63SAmit Arora 	/*
2926a2df2a63SAmit Arora 	 * credits to insert 1 extent into extent tree + buffers to be able to
2927a2df2a63SAmit Arora 	 * modify 1 super block, 1 block bitmap and 1 group descriptor.
2928a2df2a63SAmit Arora 	 */
2929a2df2a63SAmit Arora 	credits = EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + 3;
293055bd725aSAneesh Kumar K.V 	mutex_lock(&inode->i_mutex);
2931a2df2a63SAmit Arora retry:
2932a2df2a63SAmit Arora 	while (ret >= 0 && ret < max_blocks) {
2933a2df2a63SAmit Arora 		block = block + ret;
2934a2df2a63SAmit Arora 		max_blocks = max_blocks - ret;
2935a2df2a63SAmit Arora 		handle = ext4_journal_start(inode, credits);
2936a2df2a63SAmit Arora 		if (IS_ERR(handle)) {
2937a2df2a63SAmit Arora 			ret = PTR_ERR(handle);
2938a2df2a63SAmit Arora 			break;
2939a2df2a63SAmit Arora 		}
294055bd725aSAneesh Kumar K.V 		ret = ext4_get_blocks_wrap(handle, inode, block,
2941a2df2a63SAmit Arora 					  max_blocks, &map_bh,
2942d2a17637SMingming Cao 					  EXT4_CREATE_UNINITIALIZED_EXT, 0, 0);
2943221879c9SAneesh Kumar K.V 		if (ret <= 0) {
29442c98615dSAneesh Kumar K.V #ifdef EXT4FS_DEBUG
29452c98615dSAneesh Kumar K.V 			WARN_ON(ret <= 0);
29462c98615dSAneesh Kumar K.V 			printk(KERN_ERR "%s: ext4_ext_get_blocks "
29472c98615dSAneesh Kumar K.V 				    "returned error inode#%lu, block=%u, "
29482c98615dSAneesh Kumar K.V 				    "max_blocks=%lu", __func__,
2949bba90743SEric Sandeen 				    inode->i_ino, block, max_blocks);
29502c98615dSAneesh Kumar K.V #endif
2951a2df2a63SAmit Arora 			ext4_mark_inode_dirty(handle, inode);
2952a2df2a63SAmit Arora 			ret2 = ext4_journal_stop(handle);
2953a2df2a63SAmit Arora 			break;
2954a2df2a63SAmit Arora 		}
2955fd28784aSAneesh Kumar K.V 		if ((block + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
2956fd28784aSAneesh Kumar K.V 						blkbits) >> blkbits))
2957fd28784aSAneesh Kumar K.V 			new_size = offset + len;
2958fd28784aSAneesh Kumar K.V 		else
2959fd28784aSAneesh Kumar K.V 			new_size = (block + ret) << blkbits;
2960a2df2a63SAmit Arora 
2961fd28784aSAneesh Kumar K.V 		ext4_falloc_update_inode(inode, mode, new_size,
2962fd28784aSAneesh Kumar K.V 						buffer_new(&map_bh));
2963a2df2a63SAmit Arora 		ext4_mark_inode_dirty(handle, inode);
2964a2df2a63SAmit Arora 		ret2 = ext4_journal_stop(handle);
2965a2df2a63SAmit Arora 		if (ret2)
2966a2df2a63SAmit Arora 			break;
2967a2df2a63SAmit Arora 	}
2968fd28784aSAneesh Kumar K.V 	if (ret == -ENOSPC &&
2969fd28784aSAneesh Kumar K.V 			ext4_should_retry_alloc(inode->i_sb, &retries)) {
2970fd28784aSAneesh Kumar K.V 		ret = 0;
2971a2df2a63SAmit Arora 		goto retry;
2972a2df2a63SAmit Arora 	}
297355bd725aSAneesh Kumar K.V 	mutex_unlock(&inode->i_mutex);
2974a2df2a63SAmit Arora 	return ret > 0 ? ret2 : ret;
2975a2df2a63SAmit Arora }
2976