xref: /openbmc/linux/fs/ext4/extents.c (revision 748de6736c1e482e111f9d1b5a5d5b1787600cad)
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>
436873fa0dSEric Sandeen #include <linux/fiemap.h>
443dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
453dcf5451SChristoph Hellwig #include "ext4_extents.h"
46a86c6181SAlex Tomas 
47a86c6181SAlex Tomas 
48d0d856e8SRandy Dunlap /*
49d0d856e8SRandy Dunlap  * ext_pblock:
50d0d856e8SRandy Dunlap  * combine low and high parts of physical block number into ext4_fsblk_t
51d0d856e8SRandy Dunlap  */
52*748de673SAkira Fujita ext4_fsblk_t ext_pblock(struct ext4_extent *ex)
53f65e6fbaSAlex Tomas {
54f65e6fbaSAlex Tomas 	ext4_fsblk_t block;
55f65e6fbaSAlex Tomas 
56b377611dSAneesh Kumar K.V 	block = le32_to_cpu(ex->ee_start_lo);
57f65e6fbaSAlex Tomas 	block |= ((ext4_fsblk_t) le16_to_cpu(ex->ee_start_hi) << 31) << 1;
58f65e6fbaSAlex Tomas 	return block;
59f65e6fbaSAlex Tomas }
60f65e6fbaSAlex Tomas 
61d0d856e8SRandy Dunlap /*
62d0d856e8SRandy Dunlap  * idx_pblock:
63d0d856e8SRandy Dunlap  * combine low and high parts of a leaf physical block number into ext4_fsblk_t
64d0d856e8SRandy Dunlap  */
65c14c6fd5SAneesh Kumar K.V ext4_fsblk_t idx_pblock(struct ext4_extent_idx *ix)
66f65e6fbaSAlex Tomas {
67f65e6fbaSAlex Tomas 	ext4_fsblk_t block;
68f65e6fbaSAlex Tomas 
69d8dd0b45SAneesh Kumar K.V 	block = le32_to_cpu(ix->ei_leaf_lo);
70f65e6fbaSAlex Tomas 	block |= ((ext4_fsblk_t) le16_to_cpu(ix->ei_leaf_hi) << 31) << 1;
71f65e6fbaSAlex Tomas 	return block;
72f65e6fbaSAlex Tomas }
73f65e6fbaSAlex Tomas 
74d0d856e8SRandy Dunlap /*
75d0d856e8SRandy Dunlap  * ext4_ext_store_pblock:
76d0d856e8SRandy Dunlap  * stores a large physical block number into an extent struct,
77d0d856e8SRandy Dunlap  * breaking it into parts
78d0d856e8SRandy Dunlap  */
79c14c6fd5SAneesh Kumar K.V void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb)
80f65e6fbaSAlex Tomas {
81b377611dSAneesh Kumar K.V 	ex->ee_start_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff));
82f65e6fbaSAlex Tomas 	ex->ee_start_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
83f65e6fbaSAlex Tomas }
84f65e6fbaSAlex Tomas 
85d0d856e8SRandy Dunlap /*
86d0d856e8SRandy Dunlap  * ext4_idx_store_pblock:
87d0d856e8SRandy Dunlap  * stores a large physical block number into an index struct,
88d0d856e8SRandy Dunlap  * breaking it into parts
89d0d856e8SRandy Dunlap  */
9009b88252SAvantika Mathur static void ext4_idx_store_pblock(struct ext4_extent_idx *ix, ext4_fsblk_t pb)
91f65e6fbaSAlex Tomas {
92d8dd0b45SAneesh Kumar K.V 	ix->ei_leaf_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff));
93f65e6fbaSAlex Tomas 	ix->ei_leaf_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
94f65e6fbaSAlex Tomas }
95f65e6fbaSAlex Tomas 
969102e4faSShen Feng static int ext4_ext_journal_restart(handle_t *handle, int needed)
97a86c6181SAlex Tomas {
98a86c6181SAlex Tomas 	int err;
99a86c6181SAlex Tomas 
1000390131bSFrank Mayhar 	if (!ext4_handle_valid(handle))
1010390131bSFrank Mayhar 		return 0;
102a86c6181SAlex Tomas 	if (handle->h_buffer_credits > needed)
1039102e4faSShen Feng 		return 0;
1049102e4faSShen Feng 	err = ext4_journal_extend(handle, needed);
1050123c939STheodore Ts'o 	if (err <= 0)
1069102e4faSShen Feng 		return err;
1079102e4faSShen Feng 	return ext4_journal_restart(handle, needed);
108a86c6181SAlex Tomas }
109a86c6181SAlex Tomas 
110a86c6181SAlex Tomas /*
111a86c6181SAlex Tomas  * could return:
112a86c6181SAlex Tomas  *  - EROFS
113a86c6181SAlex Tomas  *  - ENOMEM
114a86c6181SAlex Tomas  */
115a86c6181SAlex Tomas static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
116a86c6181SAlex Tomas 				struct ext4_ext_path *path)
117a86c6181SAlex Tomas {
118a86c6181SAlex Tomas 	if (path->p_bh) {
119a86c6181SAlex Tomas 		/* path points to block */
120a86c6181SAlex Tomas 		return ext4_journal_get_write_access(handle, path->p_bh);
121a86c6181SAlex Tomas 	}
122a86c6181SAlex Tomas 	/* path points to leaf/index in inode body */
123a86c6181SAlex Tomas 	/* we use in-core data, no need to protect them */
124a86c6181SAlex Tomas 	return 0;
125a86c6181SAlex Tomas }
126a86c6181SAlex Tomas 
127a86c6181SAlex Tomas /*
128a86c6181SAlex Tomas  * could return:
129a86c6181SAlex Tomas  *  - EROFS
130a86c6181SAlex Tomas  *  - ENOMEM
131a86c6181SAlex Tomas  *  - EIO
132a86c6181SAlex Tomas  */
133a86c6181SAlex Tomas static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
134a86c6181SAlex Tomas 				struct ext4_ext_path *path)
135a86c6181SAlex Tomas {
136a86c6181SAlex Tomas 	int err;
137a86c6181SAlex Tomas 	if (path->p_bh) {
138a86c6181SAlex Tomas 		/* path points to block */
1390390131bSFrank Mayhar 		err = ext4_handle_dirty_metadata(handle, inode, path->p_bh);
140a86c6181SAlex Tomas 	} else {
141a86c6181SAlex Tomas 		/* path points to leaf/index in inode body */
142a86c6181SAlex Tomas 		err = ext4_mark_inode_dirty(handle, inode);
143a86c6181SAlex Tomas 	}
144a86c6181SAlex Tomas 	return err;
145a86c6181SAlex Tomas }
146a86c6181SAlex Tomas 
147f65e6fbaSAlex Tomas static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
148a86c6181SAlex Tomas 			      struct ext4_ext_path *path,
149725d26d3SAneesh Kumar K.V 			      ext4_lblk_t block)
150a86c6181SAlex Tomas {
151a86c6181SAlex Tomas 	struct ext4_inode_info *ei = EXT4_I(inode);
152f65e6fbaSAlex Tomas 	ext4_fsblk_t bg_start;
15374d3487fSValerie Clement 	ext4_fsblk_t last_block;
154f65e6fbaSAlex Tomas 	ext4_grpblk_t colour;
155a4912123STheodore Ts'o 	ext4_group_t block_group;
156a4912123STheodore Ts'o 	int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
157a86c6181SAlex Tomas 	int depth;
158a86c6181SAlex Tomas 
159a86c6181SAlex Tomas 	if (path) {
160a86c6181SAlex Tomas 		struct ext4_extent *ex;
161a86c6181SAlex Tomas 		depth = path->p_depth;
162a86c6181SAlex Tomas 
163a86c6181SAlex Tomas 		/* try to predict block placement */
1647e028976SAvantika Mathur 		ex = path[depth].p_ext;
1657e028976SAvantika Mathur 		if (ex)
166f65e6fbaSAlex Tomas 			return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block));
167a86c6181SAlex Tomas 
168d0d856e8SRandy Dunlap 		/* it looks like index is empty;
169d0d856e8SRandy Dunlap 		 * try to find starting block from index itself */
170a86c6181SAlex Tomas 		if (path[depth].p_bh)
171a86c6181SAlex Tomas 			return path[depth].p_bh->b_blocknr;
172a86c6181SAlex Tomas 	}
173a86c6181SAlex Tomas 
174a86c6181SAlex Tomas 	/* OK. use inode's group */
175a4912123STheodore Ts'o 	block_group = ei->i_block_group;
176a4912123STheodore Ts'o 	if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
177a4912123STheodore Ts'o 		/*
178a4912123STheodore Ts'o 		 * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
179a4912123STheodore Ts'o 		 * block groups per flexgroup, reserve the first block
180a4912123STheodore Ts'o 		 * group for directories and special files.  Regular
181a4912123STheodore Ts'o 		 * files will start at the second block group.  This
182a4912123STheodore Ts'o 		 * tends to speed up directory access and improves
183a4912123STheodore Ts'o 		 * fsck times.
184a4912123STheodore Ts'o 		 */
185a4912123STheodore Ts'o 		block_group &= ~(flex_size-1);
186a4912123STheodore Ts'o 		if (S_ISREG(inode->i_mode))
187a4912123STheodore Ts'o 			block_group++;
188a4912123STheodore Ts'o 	}
189a4912123STheodore Ts'o 	bg_start = (block_group * EXT4_BLOCKS_PER_GROUP(inode->i_sb)) +
190a86c6181SAlex Tomas 		le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_first_data_block);
19174d3487fSValerie Clement 	last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
19274d3487fSValerie Clement 
193a4912123STheodore Ts'o 	/*
194a4912123STheodore Ts'o 	 * If we are doing delayed allocation, we don't need take
195a4912123STheodore Ts'o 	 * colour into account.
196a4912123STheodore Ts'o 	 */
197a4912123STheodore Ts'o 	if (test_opt(inode->i_sb, DELALLOC))
198a4912123STheodore Ts'o 		return bg_start;
199a4912123STheodore Ts'o 
20074d3487fSValerie Clement 	if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
201a86c6181SAlex Tomas 		colour = (current->pid % 16) *
202a86c6181SAlex Tomas 			(EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
20374d3487fSValerie Clement 	else
20474d3487fSValerie Clement 		colour = (current->pid % 16) * ((last_block - bg_start) / 16);
205a86c6181SAlex Tomas 	return bg_start + colour + block;
206a86c6181SAlex Tomas }
207a86c6181SAlex Tomas 
208654b4908SAneesh Kumar K.V /*
209654b4908SAneesh Kumar K.V  * Allocation for a meta data block
210654b4908SAneesh Kumar K.V  */
211f65e6fbaSAlex Tomas static ext4_fsblk_t
212654b4908SAneesh Kumar K.V ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
213a86c6181SAlex Tomas 			struct ext4_ext_path *path,
214a86c6181SAlex Tomas 			struct ext4_extent *ex, int *err)
215a86c6181SAlex Tomas {
216f65e6fbaSAlex Tomas 	ext4_fsblk_t goal, newblock;
217a86c6181SAlex Tomas 
218a86c6181SAlex Tomas 	goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
21997df5d15STheodore Ts'o 	newblock = ext4_new_meta_blocks(handle, inode, goal, NULL, err);
220a86c6181SAlex Tomas 	return newblock;
221a86c6181SAlex Tomas }
222a86c6181SAlex Tomas 
22309b88252SAvantika Mathur static int ext4_ext_space_block(struct inode *inode)
224a86c6181SAlex Tomas {
225a86c6181SAlex Tomas 	int size;
226a86c6181SAlex Tomas 
227a86c6181SAlex Tomas 	size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
228a86c6181SAlex Tomas 			/ sizeof(struct ext4_extent);
229bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST
230a86c6181SAlex Tomas 	if (size > 6)
231a86c6181SAlex Tomas 		size = 6;
232a86c6181SAlex Tomas #endif
233a86c6181SAlex Tomas 	return size;
234a86c6181SAlex Tomas }
235a86c6181SAlex Tomas 
23609b88252SAvantika Mathur static int ext4_ext_space_block_idx(struct inode *inode)
237a86c6181SAlex Tomas {
238a86c6181SAlex Tomas 	int size;
239a86c6181SAlex Tomas 
240a86c6181SAlex Tomas 	size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
241a86c6181SAlex Tomas 			/ sizeof(struct ext4_extent_idx);
242bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST
243a86c6181SAlex Tomas 	if (size > 5)
244a86c6181SAlex Tomas 		size = 5;
245a86c6181SAlex Tomas #endif
246a86c6181SAlex Tomas 	return size;
247a86c6181SAlex Tomas }
248a86c6181SAlex Tomas 
24909b88252SAvantika Mathur static int ext4_ext_space_root(struct inode *inode)
250a86c6181SAlex Tomas {
251a86c6181SAlex Tomas 	int size;
252a86c6181SAlex Tomas 
253a86c6181SAlex Tomas 	size = sizeof(EXT4_I(inode)->i_data);
254a86c6181SAlex Tomas 	size -= sizeof(struct ext4_extent_header);
255a86c6181SAlex Tomas 	size /= sizeof(struct ext4_extent);
256bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST
257a86c6181SAlex Tomas 	if (size > 3)
258a86c6181SAlex Tomas 		size = 3;
259a86c6181SAlex Tomas #endif
260a86c6181SAlex Tomas 	return size;
261a86c6181SAlex Tomas }
262a86c6181SAlex Tomas 
26309b88252SAvantika Mathur static int ext4_ext_space_root_idx(struct inode *inode)
264a86c6181SAlex Tomas {
265a86c6181SAlex Tomas 	int size;
266a86c6181SAlex Tomas 
267a86c6181SAlex Tomas 	size = sizeof(EXT4_I(inode)->i_data);
268a86c6181SAlex Tomas 	size -= sizeof(struct ext4_extent_header);
269a86c6181SAlex Tomas 	size /= sizeof(struct ext4_extent_idx);
270bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST
271a86c6181SAlex Tomas 	if (size > 4)
272a86c6181SAlex Tomas 		size = 4;
273a86c6181SAlex Tomas #endif
274a86c6181SAlex Tomas 	return size;
275a86c6181SAlex Tomas }
276a86c6181SAlex Tomas 
277d2a17637SMingming Cao /*
278d2a17637SMingming Cao  * Calculate the number of metadata blocks needed
279d2a17637SMingming Cao  * to allocate @blocks
280d2a17637SMingming Cao  * Worse case is one block per extent
281d2a17637SMingming Cao  */
282d2a17637SMingming Cao int ext4_ext_calc_metadata_amount(struct inode *inode, int blocks)
283d2a17637SMingming Cao {
284d2a17637SMingming Cao 	int lcap, icap, rcap, leafs, idxs, num;
285d2a17637SMingming Cao 	int newextents = blocks;
286d2a17637SMingming Cao 
287d2a17637SMingming Cao 	rcap = ext4_ext_space_root_idx(inode);
288d2a17637SMingming Cao 	lcap = ext4_ext_space_block(inode);
289d2a17637SMingming Cao 	icap = ext4_ext_space_block_idx(inode);
290d2a17637SMingming Cao 
291d2a17637SMingming Cao 	/* number of new leaf blocks needed */
292d2a17637SMingming Cao 	num = leafs = (newextents + lcap - 1) / lcap;
293d2a17637SMingming Cao 
294d2a17637SMingming Cao 	/*
295d2a17637SMingming Cao 	 * Worse case, we need separate index block(s)
296d2a17637SMingming Cao 	 * to link all new leaf blocks
297d2a17637SMingming Cao 	 */
298d2a17637SMingming Cao 	idxs = (leafs + icap - 1) / icap;
299d2a17637SMingming Cao 	do {
300d2a17637SMingming Cao 		num += idxs;
301d2a17637SMingming Cao 		idxs = (idxs + icap - 1) / icap;
302d2a17637SMingming Cao 	} while (idxs > rcap);
303d2a17637SMingming Cao 
304d2a17637SMingming Cao 	return num;
305d2a17637SMingming Cao }
306d2a17637SMingming Cao 
307c29c0ae7SAlex Tomas static int
308c29c0ae7SAlex Tomas ext4_ext_max_entries(struct inode *inode, int depth)
309c29c0ae7SAlex Tomas {
310c29c0ae7SAlex Tomas 	int max;
311c29c0ae7SAlex Tomas 
312c29c0ae7SAlex Tomas 	if (depth == ext_depth(inode)) {
313c29c0ae7SAlex Tomas 		if (depth == 0)
314c29c0ae7SAlex Tomas 			max = ext4_ext_space_root(inode);
315c29c0ae7SAlex Tomas 		else
316c29c0ae7SAlex Tomas 			max = ext4_ext_space_root_idx(inode);
317c29c0ae7SAlex Tomas 	} else {
318c29c0ae7SAlex Tomas 		if (depth == 0)
319c29c0ae7SAlex Tomas 			max = ext4_ext_space_block(inode);
320c29c0ae7SAlex Tomas 		else
321c29c0ae7SAlex Tomas 			max = ext4_ext_space_block_idx(inode);
322c29c0ae7SAlex Tomas 	}
323c29c0ae7SAlex Tomas 
324c29c0ae7SAlex Tomas 	return max;
325c29c0ae7SAlex Tomas }
326c29c0ae7SAlex Tomas 
32756b19868SAneesh Kumar K.V static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
32856b19868SAneesh Kumar K.V {
3296fd058f7STheodore Ts'o 	ext4_fsblk_t block = ext_pblock(ext);
33056b19868SAneesh Kumar K.V 	int len = ext4_ext_get_actual_len(ext);
331e84a26ceSTheodore Ts'o 
3326fd058f7STheodore Ts'o 	return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
33356b19868SAneesh Kumar K.V }
33456b19868SAneesh Kumar K.V 
33556b19868SAneesh Kumar K.V static int ext4_valid_extent_idx(struct inode *inode,
33656b19868SAneesh Kumar K.V 				struct ext4_extent_idx *ext_idx)
33756b19868SAneesh Kumar K.V {
3386fd058f7STheodore Ts'o 	ext4_fsblk_t block = idx_pblock(ext_idx);
339e84a26ceSTheodore Ts'o 
3406fd058f7STheodore Ts'o 	return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
34156b19868SAneesh Kumar K.V }
34256b19868SAneesh Kumar K.V 
34356b19868SAneesh Kumar K.V static int ext4_valid_extent_entries(struct inode *inode,
34456b19868SAneesh Kumar K.V 				struct ext4_extent_header *eh,
34556b19868SAneesh Kumar K.V 				int depth)
34656b19868SAneesh Kumar K.V {
34756b19868SAneesh Kumar K.V 	struct ext4_extent *ext;
34856b19868SAneesh Kumar K.V 	struct ext4_extent_idx *ext_idx;
34956b19868SAneesh Kumar K.V 	unsigned short entries;
35056b19868SAneesh Kumar K.V 	if (eh->eh_entries == 0)
35156b19868SAneesh Kumar K.V 		return 1;
35256b19868SAneesh Kumar K.V 
35356b19868SAneesh Kumar K.V 	entries = le16_to_cpu(eh->eh_entries);
35456b19868SAneesh Kumar K.V 
35556b19868SAneesh Kumar K.V 	if (depth == 0) {
35656b19868SAneesh Kumar K.V 		/* leaf entries */
35756b19868SAneesh Kumar K.V 		ext = EXT_FIRST_EXTENT(eh);
35856b19868SAneesh Kumar K.V 		while (entries) {
35956b19868SAneesh Kumar K.V 			if (!ext4_valid_extent(inode, ext))
36056b19868SAneesh Kumar K.V 				return 0;
36156b19868SAneesh Kumar K.V 			ext++;
36256b19868SAneesh Kumar K.V 			entries--;
36356b19868SAneesh Kumar K.V 		}
36456b19868SAneesh Kumar K.V 	} else {
36556b19868SAneesh Kumar K.V 		ext_idx = EXT_FIRST_INDEX(eh);
36656b19868SAneesh Kumar K.V 		while (entries) {
36756b19868SAneesh Kumar K.V 			if (!ext4_valid_extent_idx(inode, ext_idx))
36856b19868SAneesh Kumar K.V 				return 0;
36956b19868SAneesh Kumar K.V 			ext_idx++;
37056b19868SAneesh Kumar K.V 			entries--;
37156b19868SAneesh Kumar K.V 		}
37256b19868SAneesh Kumar K.V 	}
37356b19868SAneesh Kumar K.V 	return 1;
37456b19868SAneesh Kumar K.V }
37556b19868SAneesh Kumar K.V 
37656b19868SAneesh Kumar K.V static int __ext4_ext_check(const char *function, struct inode *inode,
377c29c0ae7SAlex Tomas 					struct ext4_extent_header *eh,
378c29c0ae7SAlex Tomas 					int depth)
379c29c0ae7SAlex Tomas {
380c29c0ae7SAlex Tomas 	const char *error_msg;
381c29c0ae7SAlex Tomas 	int max = 0;
382c29c0ae7SAlex Tomas 
383c29c0ae7SAlex Tomas 	if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
384c29c0ae7SAlex Tomas 		error_msg = "invalid magic";
385c29c0ae7SAlex Tomas 		goto corrupted;
386c29c0ae7SAlex Tomas 	}
387c29c0ae7SAlex Tomas 	if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
388c29c0ae7SAlex Tomas 		error_msg = "unexpected eh_depth";
389c29c0ae7SAlex Tomas 		goto corrupted;
390c29c0ae7SAlex Tomas 	}
391c29c0ae7SAlex Tomas 	if (unlikely(eh->eh_max == 0)) {
392c29c0ae7SAlex Tomas 		error_msg = "invalid eh_max";
393c29c0ae7SAlex Tomas 		goto corrupted;
394c29c0ae7SAlex Tomas 	}
395c29c0ae7SAlex Tomas 	max = ext4_ext_max_entries(inode, depth);
396c29c0ae7SAlex Tomas 	if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
397c29c0ae7SAlex Tomas 		error_msg = "too large eh_max";
398c29c0ae7SAlex Tomas 		goto corrupted;
399c29c0ae7SAlex Tomas 	}
400c29c0ae7SAlex Tomas 	if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
401c29c0ae7SAlex Tomas 		error_msg = "invalid eh_entries";
402c29c0ae7SAlex Tomas 		goto corrupted;
403c29c0ae7SAlex Tomas 	}
40456b19868SAneesh Kumar K.V 	if (!ext4_valid_extent_entries(inode, eh, depth)) {
40556b19868SAneesh Kumar K.V 		error_msg = "invalid extent entries";
40656b19868SAneesh Kumar K.V 		goto corrupted;
40756b19868SAneesh Kumar K.V 	}
408c29c0ae7SAlex Tomas 	return 0;
409c29c0ae7SAlex Tomas 
410c29c0ae7SAlex Tomas corrupted:
411c29c0ae7SAlex Tomas 	ext4_error(inode->i_sb, function,
41256b19868SAneesh Kumar K.V 			"bad header/extent in inode #%lu: %s - magic %x, "
413c29c0ae7SAlex Tomas 			"entries %u, max %u(%u), depth %u(%u)",
414c29c0ae7SAlex Tomas 			inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic),
415c29c0ae7SAlex Tomas 			le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
416c29c0ae7SAlex Tomas 			max, le16_to_cpu(eh->eh_depth), depth);
417c29c0ae7SAlex Tomas 
418c29c0ae7SAlex Tomas 	return -EIO;
419c29c0ae7SAlex Tomas }
420c29c0ae7SAlex Tomas 
42156b19868SAneesh Kumar K.V #define ext4_ext_check(inode, eh, depth)	\
42256b19868SAneesh Kumar K.V 	__ext4_ext_check(__func__, inode, eh, depth)
423c29c0ae7SAlex Tomas 
4247a262f7cSAneesh Kumar K.V int ext4_ext_check_inode(struct inode *inode)
4257a262f7cSAneesh Kumar K.V {
4267a262f7cSAneesh Kumar K.V 	return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode));
4277a262f7cSAneesh Kumar K.V }
4287a262f7cSAneesh Kumar K.V 
429a86c6181SAlex Tomas #ifdef EXT_DEBUG
430a86c6181SAlex Tomas static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
431a86c6181SAlex Tomas {
432a86c6181SAlex Tomas 	int k, l = path->p_depth;
433a86c6181SAlex Tomas 
434a86c6181SAlex Tomas 	ext_debug("path:");
435a86c6181SAlex Tomas 	for (k = 0; k <= l; k++, path++) {
436a86c6181SAlex Tomas 		if (path->p_idx) {
4372ae02107SMingming Cao 		  ext_debug("  %d->%llu", le32_to_cpu(path->p_idx->ei_block),
438f65e6fbaSAlex Tomas 			    idx_pblock(path->p_idx));
439a86c6181SAlex Tomas 		} else if (path->p_ext) {
4402ae02107SMingming Cao 			ext_debug("  %d:%d:%llu ",
441a86c6181SAlex Tomas 				  le32_to_cpu(path->p_ext->ee_block),
442a2df2a63SAmit Arora 				  ext4_ext_get_actual_len(path->p_ext),
443f65e6fbaSAlex Tomas 				  ext_pblock(path->p_ext));
444a86c6181SAlex Tomas 		} else
445a86c6181SAlex Tomas 			ext_debug("  []");
446a86c6181SAlex Tomas 	}
447a86c6181SAlex Tomas 	ext_debug("\n");
448a86c6181SAlex Tomas }
449a86c6181SAlex Tomas 
450a86c6181SAlex Tomas static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
451a86c6181SAlex Tomas {
452a86c6181SAlex Tomas 	int depth = ext_depth(inode);
453a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
454a86c6181SAlex Tomas 	struct ext4_extent *ex;
455a86c6181SAlex Tomas 	int i;
456a86c6181SAlex Tomas 
457a86c6181SAlex Tomas 	if (!path)
458a86c6181SAlex Tomas 		return;
459a86c6181SAlex Tomas 
460a86c6181SAlex Tomas 	eh = path[depth].p_hdr;
461a86c6181SAlex Tomas 	ex = EXT_FIRST_EXTENT(eh);
462a86c6181SAlex Tomas 
463a86c6181SAlex Tomas 	for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
4642ae02107SMingming Cao 		ext_debug("%d:%d:%llu ", le32_to_cpu(ex->ee_block),
465a2df2a63SAmit Arora 			  ext4_ext_get_actual_len(ex), ext_pblock(ex));
466a86c6181SAlex Tomas 	}
467a86c6181SAlex Tomas 	ext_debug("\n");
468a86c6181SAlex Tomas }
469a86c6181SAlex Tomas #else
470a86c6181SAlex Tomas #define ext4_ext_show_path(inode, path)
471a86c6181SAlex Tomas #define ext4_ext_show_leaf(inode, path)
472a86c6181SAlex Tomas #endif
473a86c6181SAlex Tomas 
474b35905c1SAneesh Kumar K.V void ext4_ext_drop_refs(struct ext4_ext_path *path)
475a86c6181SAlex Tomas {
476a86c6181SAlex Tomas 	int depth = path->p_depth;
477a86c6181SAlex Tomas 	int i;
478a86c6181SAlex Tomas 
479a86c6181SAlex Tomas 	for (i = 0; i <= depth; i++, path++)
480a86c6181SAlex Tomas 		if (path->p_bh) {
481a86c6181SAlex Tomas 			brelse(path->p_bh);
482a86c6181SAlex Tomas 			path->p_bh = NULL;
483a86c6181SAlex Tomas 		}
484a86c6181SAlex Tomas }
485a86c6181SAlex Tomas 
486a86c6181SAlex Tomas /*
487d0d856e8SRandy Dunlap  * ext4_ext_binsearch_idx:
488d0d856e8SRandy Dunlap  * binary search for the closest index of the given block
489c29c0ae7SAlex Tomas  * the header must be checked before calling this
490a86c6181SAlex Tomas  */
491a86c6181SAlex Tomas static void
492725d26d3SAneesh Kumar K.V ext4_ext_binsearch_idx(struct inode *inode,
493725d26d3SAneesh Kumar K.V 			struct ext4_ext_path *path, ext4_lblk_t block)
494a86c6181SAlex Tomas {
495a86c6181SAlex Tomas 	struct ext4_extent_header *eh = path->p_hdr;
496a86c6181SAlex Tomas 	struct ext4_extent_idx *r, *l, *m;
497a86c6181SAlex Tomas 
498a86c6181SAlex Tomas 
499bba90743SEric Sandeen 	ext_debug("binsearch for %u(idx):  ", block);
500a86c6181SAlex Tomas 
501a86c6181SAlex Tomas 	l = EXT_FIRST_INDEX(eh) + 1;
502e9f410b1SDmitry Monakhov 	r = EXT_LAST_INDEX(eh);
503a86c6181SAlex Tomas 	while (l <= r) {
504a86c6181SAlex Tomas 		m = l + (r - l) / 2;
505a86c6181SAlex Tomas 		if (block < le32_to_cpu(m->ei_block))
506a86c6181SAlex Tomas 			r = m - 1;
507a86c6181SAlex Tomas 		else
508a86c6181SAlex Tomas 			l = m + 1;
50926d535edSDmitry Monakhov 		ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
51026d535edSDmitry Monakhov 				m, le32_to_cpu(m->ei_block),
51126d535edSDmitry Monakhov 				r, le32_to_cpu(r->ei_block));
512a86c6181SAlex Tomas 	}
513a86c6181SAlex Tomas 
514a86c6181SAlex Tomas 	path->p_idx = l - 1;
515f65e6fbaSAlex Tomas 	ext_debug("  -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
51626d535edSDmitry Monakhov 		  idx_pblock(path->p_idx));
517a86c6181SAlex Tomas 
518a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH
519a86c6181SAlex Tomas 	{
520a86c6181SAlex Tomas 		struct ext4_extent_idx *chix, *ix;
521a86c6181SAlex Tomas 		int k;
522a86c6181SAlex Tomas 
523a86c6181SAlex Tomas 		chix = ix = EXT_FIRST_INDEX(eh);
524a86c6181SAlex Tomas 		for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
525a86c6181SAlex Tomas 		  if (k != 0 &&
526a86c6181SAlex Tomas 		      le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
5274776004fSTheodore Ts'o 				printk(KERN_DEBUG "k=%d, ix=0x%p, "
5284776004fSTheodore Ts'o 				       "first=0x%p\n", k,
529a86c6181SAlex Tomas 				       ix, EXT_FIRST_INDEX(eh));
5304776004fSTheodore Ts'o 				printk(KERN_DEBUG "%u <= %u\n",
531a86c6181SAlex Tomas 				       le32_to_cpu(ix->ei_block),
532a86c6181SAlex Tomas 				       le32_to_cpu(ix[-1].ei_block));
533a86c6181SAlex Tomas 			}
534a86c6181SAlex Tomas 			BUG_ON(k && le32_to_cpu(ix->ei_block)
535a86c6181SAlex Tomas 					   <= le32_to_cpu(ix[-1].ei_block));
536a86c6181SAlex Tomas 			if (block < le32_to_cpu(ix->ei_block))
537a86c6181SAlex Tomas 				break;
538a86c6181SAlex Tomas 			chix = ix;
539a86c6181SAlex Tomas 		}
540a86c6181SAlex Tomas 		BUG_ON(chix != path->p_idx);
541a86c6181SAlex Tomas 	}
542a86c6181SAlex Tomas #endif
543a86c6181SAlex Tomas 
544a86c6181SAlex Tomas }
545a86c6181SAlex Tomas 
546a86c6181SAlex Tomas /*
547d0d856e8SRandy Dunlap  * ext4_ext_binsearch:
548d0d856e8SRandy Dunlap  * binary search for closest extent of the given block
549c29c0ae7SAlex Tomas  * the header must be checked before calling this
550a86c6181SAlex Tomas  */
551a86c6181SAlex Tomas static void
552725d26d3SAneesh Kumar K.V ext4_ext_binsearch(struct inode *inode,
553725d26d3SAneesh Kumar K.V 		struct ext4_ext_path *path, ext4_lblk_t block)
554a86c6181SAlex Tomas {
555a86c6181SAlex Tomas 	struct ext4_extent_header *eh = path->p_hdr;
556a86c6181SAlex Tomas 	struct ext4_extent *r, *l, *m;
557a86c6181SAlex Tomas 
558a86c6181SAlex Tomas 	if (eh->eh_entries == 0) {
559a86c6181SAlex Tomas 		/*
560d0d856e8SRandy Dunlap 		 * this leaf is empty:
561a86c6181SAlex Tomas 		 * we get such a leaf in split/add case
562a86c6181SAlex Tomas 		 */
563a86c6181SAlex Tomas 		return;
564a86c6181SAlex Tomas 	}
565a86c6181SAlex Tomas 
566bba90743SEric Sandeen 	ext_debug("binsearch for %u:  ", block);
567a86c6181SAlex Tomas 
568a86c6181SAlex Tomas 	l = EXT_FIRST_EXTENT(eh) + 1;
569e9f410b1SDmitry Monakhov 	r = EXT_LAST_EXTENT(eh);
570a86c6181SAlex Tomas 
571a86c6181SAlex Tomas 	while (l <= r) {
572a86c6181SAlex Tomas 		m = l + (r - l) / 2;
573a86c6181SAlex Tomas 		if (block < le32_to_cpu(m->ee_block))
574a86c6181SAlex Tomas 			r = m - 1;
575a86c6181SAlex Tomas 		else
576a86c6181SAlex Tomas 			l = m + 1;
57726d535edSDmitry Monakhov 		ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
57826d535edSDmitry Monakhov 				m, le32_to_cpu(m->ee_block),
57926d535edSDmitry Monakhov 				r, le32_to_cpu(r->ee_block));
580a86c6181SAlex Tomas 	}
581a86c6181SAlex Tomas 
582a86c6181SAlex Tomas 	path->p_ext = l - 1;
5832ae02107SMingming Cao 	ext_debug("  -> %d:%llu:%d ",
584a86c6181SAlex Tomas 			le32_to_cpu(path->p_ext->ee_block),
585f65e6fbaSAlex Tomas 			ext_pblock(path->p_ext),
586a2df2a63SAmit Arora 			ext4_ext_get_actual_len(path->p_ext));
587a86c6181SAlex Tomas 
588a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH
589a86c6181SAlex Tomas 	{
590a86c6181SAlex Tomas 		struct ext4_extent *chex, *ex;
591a86c6181SAlex Tomas 		int k;
592a86c6181SAlex Tomas 
593a86c6181SAlex Tomas 		chex = ex = EXT_FIRST_EXTENT(eh);
594a86c6181SAlex Tomas 		for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
595a86c6181SAlex Tomas 			BUG_ON(k && le32_to_cpu(ex->ee_block)
596a86c6181SAlex Tomas 					  <= le32_to_cpu(ex[-1].ee_block));
597a86c6181SAlex Tomas 			if (block < le32_to_cpu(ex->ee_block))
598a86c6181SAlex Tomas 				break;
599a86c6181SAlex Tomas 			chex = ex;
600a86c6181SAlex Tomas 		}
601a86c6181SAlex Tomas 		BUG_ON(chex != path->p_ext);
602a86c6181SAlex Tomas 	}
603a86c6181SAlex Tomas #endif
604a86c6181SAlex Tomas 
605a86c6181SAlex Tomas }
606a86c6181SAlex Tomas 
607a86c6181SAlex Tomas int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
608a86c6181SAlex Tomas {
609a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
610a86c6181SAlex Tomas 
611a86c6181SAlex Tomas 	eh = ext_inode_hdr(inode);
612a86c6181SAlex Tomas 	eh->eh_depth = 0;
613a86c6181SAlex Tomas 	eh->eh_entries = 0;
614a86c6181SAlex Tomas 	eh->eh_magic = EXT4_EXT_MAGIC;
615a86c6181SAlex Tomas 	eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode));
616a86c6181SAlex Tomas 	ext4_mark_inode_dirty(handle, inode);
617a86c6181SAlex Tomas 	ext4_ext_invalidate_cache(inode);
618a86c6181SAlex Tomas 	return 0;
619a86c6181SAlex Tomas }
620a86c6181SAlex Tomas 
621a86c6181SAlex Tomas struct ext4_ext_path *
622725d26d3SAneesh Kumar K.V ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
623725d26d3SAneesh Kumar K.V 					struct ext4_ext_path *path)
624a86c6181SAlex Tomas {
625a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
626a86c6181SAlex Tomas 	struct buffer_head *bh;
627a86c6181SAlex Tomas 	short int depth, i, ppos = 0, alloc = 0;
628a86c6181SAlex Tomas 
629a86c6181SAlex Tomas 	eh = ext_inode_hdr(inode);
630c29c0ae7SAlex Tomas 	depth = ext_depth(inode);
631a86c6181SAlex Tomas 
632a86c6181SAlex Tomas 	/* account possible depth increase */
633a86c6181SAlex Tomas 	if (!path) {
6345d4958f9SAvantika Mathur 		path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
635a86c6181SAlex Tomas 				GFP_NOFS);
636a86c6181SAlex Tomas 		if (!path)
637a86c6181SAlex Tomas 			return ERR_PTR(-ENOMEM);
638a86c6181SAlex Tomas 		alloc = 1;
639a86c6181SAlex Tomas 	}
640a86c6181SAlex Tomas 	path[0].p_hdr = eh;
6411973adcbSShen Feng 	path[0].p_bh = NULL;
642a86c6181SAlex Tomas 
643c29c0ae7SAlex Tomas 	i = depth;
644a86c6181SAlex Tomas 	/* walk through the tree */
645a86c6181SAlex Tomas 	while (i) {
6467a262f7cSAneesh Kumar K.V 		int need_to_validate = 0;
6477a262f7cSAneesh Kumar K.V 
648a86c6181SAlex Tomas 		ext_debug("depth %d: num %d, max %d\n",
649a86c6181SAlex Tomas 			  ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
650c29c0ae7SAlex Tomas 
651a86c6181SAlex Tomas 		ext4_ext_binsearch_idx(inode, path + ppos, block);
652f65e6fbaSAlex Tomas 		path[ppos].p_block = idx_pblock(path[ppos].p_idx);
653a86c6181SAlex Tomas 		path[ppos].p_depth = i;
654a86c6181SAlex Tomas 		path[ppos].p_ext = NULL;
655a86c6181SAlex Tomas 
6567a262f7cSAneesh Kumar K.V 		bh = sb_getblk(inode->i_sb, path[ppos].p_block);
6577a262f7cSAneesh Kumar K.V 		if (unlikely(!bh))
658a86c6181SAlex Tomas 			goto err;
6597a262f7cSAneesh Kumar K.V 		if (!bh_uptodate_or_lock(bh)) {
6607a262f7cSAneesh Kumar K.V 			if (bh_submit_read(bh) < 0) {
6617a262f7cSAneesh Kumar K.V 				put_bh(bh);
6627a262f7cSAneesh Kumar K.V 				goto err;
6637a262f7cSAneesh Kumar K.V 			}
6647a262f7cSAneesh Kumar K.V 			/* validate the extent entries */
6657a262f7cSAneesh Kumar K.V 			need_to_validate = 1;
6667a262f7cSAneesh Kumar K.V 		}
667a86c6181SAlex Tomas 		eh = ext_block_hdr(bh);
668a86c6181SAlex Tomas 		ppos++;
669a86c6181SAlex Tomas 		BUG_ON(ppos > depth);
670a86c6181SAlex Tomas 		path[ppos].p_bh = bh;
671a86c6181SAlex Tomas 		path[ppos].p_hdr = eh;
672a86c6181SAlex Tomas 		i--;
673a86c6181SAlex Tomas 
6747a262f7cSAneesh Kumar K.V 		if (need_to_validate && ext4_ext_check(inode, eh, i))
675a86c6181SAlex Tomas 			goto err;
676a86c6181SAlex Tomas 	}
677a86c6181SAlex Tomas 
678a86c6181SAlex Tomas 	path[ppos].p_depth = i;
679a86c6181SAlex Tomas 	path[ppos].p_ext = NULL;
680a86c6181SAlex Tomas 	path[ppos].p_idx = NULL;
681a86c6181SAlex Tomas 
682a86c6181SAlex Tomas 	/* find extent */
683a86c6181SAlex Tomas 	ext4_ext_binsearch(inode, path + ppos, block);
6841973adcbSShen Feng 	/* if not an empty leaf */
6851973adcbSShen Feng 	if (path[ppos].p_ext)
6861973adcbSShen Feng 		path[ppos].p_block = ext_pblock(path[ppos].p_ext);
687a86c6181SAlex Tomas 
688a86c6181SAlex Tomas 	ext4_ext_show_path(inode, path);
689a86c6181SAlex Tomas 
690a86c6181SAlex Tomas 	return path;
691a86c6181SAlex Tomas 
692a86c6181SAlex Tomas err:
693a86c6181SAlex Tomas 	ext4_ext_drop_refs(path);
694a86c6181SAlex Tomas 	if (alloc)
695a86c6181SAlex Tomas 		kfree(path);
696a86c6181SAlex Tomas 	return ERR_PTR(-EIO);
697a86c6181SAlex Tomas }
698a86c6181SAlex Tomas 
699a86c6181SAlex Tomas /*
700d0d856e8SRandy Dunlap  * ext4_ext_insert_index:
701d0d856e8SRandy Dunlap  * insert new index [@logical;@ptr] into the block at @curp;
702d0d856e8SRandy Dunlap  * check where to insert: before @curp or after @curp
703a86c6181SAlex Tomas  */
704a86c6181SAlex Tomas static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
705a86c6181SAlex Tomas 				struct ext4_ext_path *curp,
706f65e6fbaSAlex Tomas 				int logical, ext4_fsblk_t ptr)
707a86c6181SAlex Tomas {
708a86c6181SAlex Tomas 	struct ext4_extent_idx *ix;
709a86c6181SAlex Tomas 	int len, err;
710a86c6181SAlex Tomas 
7117e028976SAvantika Mathur 	err = ext4_ext_get_access(handle, inode, curp);
7127e028976SAvantika Mathur 	if (err)
713a86c6181SAlex Tomas 		return err;
714a86c6181SAlex Tomas 
715a86c6181SAlex Tomas 	BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block));
716a86c6181SAlex Tomas 	len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
717a86c6181SAlex Tomas 	if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
718a86c6181SAlex Tomas 		/* insert after */
719a86c6181SAlex Tomas 		if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
720a86c6181SAlex Tomas 			len = (len - 1) * sizeof(struct ext4_extent_idx);
721a86c6181SAlex Tomas 			len = len < 0 ? 0 : len;
72226d535edSDmitry Monakhov 			ext_debug("insert new index %d after: %llu. "
723a86c6181SAlex Tomas 					"move %d from 0x%p to 0x%p\n",
724a86c6181SAlex Tomas 					logical, ptr, len,
725a86c6181SAlex Tomas 					(curp->p_idx + 1), (curp->p_idx + 2));
726a86c6181SAlex Tomas 			memmove(curp->p_idx + 2, curp->p_idx + 1, len);
727a86c6181SAlex Tomas 		}
728a86c6181SAlex Tomas 		ix = curp->p_idx + 1;
729a86c6181SAlex Tomas 	} else {
730a86c6181SAlex Tomas 		/* insert before */
731a86c6181SAlex Tomas 		len = len * sizeof(struct ext4_extent_idx);
732a86c6181SAlex Tomas 		len = len < 0 ? 0 : len;
73326d535edSDmitry Monakhov 		ext_debug("insert new index %d before: %llu. "
734a86c6181SAlex Tomas 				"move %d from 0x%p to 0x%p\n",
735a86c6181SAlex Tomas 				logical, ptr, len,
736a86c6181SAlex Tomas 				curp->p_idx, (curp->p_idx + 1));
737a86c6181SAlex Tomas 		memmove(curp->p_idx + 1, curp->p_idx, len);
738a86c6181SAlex Tomas 		ix = curp->p_idx;
739a86c6181SAlex Tomas 	}
740a86c6181SAlex Tomas 
741a86c6181SAlex Tomas 	ix->ei_block = cpu_to_le32(logical);
742f65e6fbaSAlex Tomas 	ext4_idx_store_pblock(ix, ptr);
743e8546d06SMarcin Slusarz 	le16_add_cpu(&curp->p_hdr->eh_entries, 1);
744a86c6181SAlex Tomas 
745a86c6181SAlex Tomas 	BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries)
746a86c6181SAlex Tomas 			     > le16_to_cpu(curp->p_hdr->eh_max));
747a86c6181SAlex Tomas 	BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr));
748a86c6181SAlex Tomas 
749a86c6181SAlex Tomas 	err = ext4_ext_dirty(handle, inode, curp);
750a86c6181SAlex Tomas 	ext4_std_error(inode->i_sb, err);
751a86c6181SAlex Tomas 
752a86c6181SAlex Tomas 	return err;
753a86c6181SAlex Tomas }
754a86c6181SAlex Tomas 
755a86c6181SAlex Tomas /*
756d0d856e8SRandy Dunlap  * ext4_ext_split:
757d0d856e8SRandy Dunlap  * inserts new subtree into the path, using free index entry
758d0d856e8SRandy Dunlap  * at depth @at:
759a86c6181SAlex Tomas  * - allocates all needed blocks (new leaf and all intermediate index blocks)
760a86c6181SAlex Tomas  * - makes decision where to split
761d0d856e8SRandy Dunlap  * - moves remaining extents and index entries (right to the split point)
762a86c6181SAlex Tomas  *   into the newly allocated blocks
763d0d856e8SRandy Dunlap  * - initializes subtree
764a86c6181SAlex Tomas  */
765a86c6181SAlex Tomas static int ext4_ext_split(handle_t *handle, struct inode *inode,
766a86c6181SAlex Tomas 				struct ext4_ext_path *path,
767a86c6181SAlex Tomas 				struct ext4_extent *newext, int at)
768a86c6181SAlex Tomas {
769a86c6181SAlex Tomas 	struct buffer_head *bh = NULL;
770a86c6181SAlex Tomas 	int depth = ext_depth(inode);
771a86c6181SAlex Tomas 	struct ext4_extent_header *neh;
772a86c6181SAlex Tomas 	struct ext4_extent_idx *fidx;
773a86c6181SAlex Tomas 	struct ext4_extent *ex;
774a86c6181SAlex Tomas 	int i = at, k, m, a;
775f65e6fbaSAlex Tomas 	ext4_fsblk_t newblock, oldblock;
776a86c6181SAlex Tomas 	__le32 border;
777f65e6fbaSAlex Tomas 	ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
778a86c6181SAlex Tomas 	int err = 0;
779a86c6181SAlex Tomas 
780a86c6181SAlex Tomas 	/* make decision: where to split? */
781d0d856e8SRandy Dunlap 	/* FIXME: now decision is simplest: at current extent */
782a86c6181SAlex Tomas 
783d0d856e8SRandy Dunlap 	/* if current leaf will be split, then we should use
784a86c6181SAlex Tomas 	 * border from split point */
785a86c6181SAlex Tomas 	BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr));
786a86c6181SAlex Tomas 	if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
787a86c6181SAlex Tomas 		border = path[depth].p_ext[1].ee_block;
788d0d856e8SRandy Dunlap 		ext_debug("leaf will be split."
789a86c6181SAlex Tomas 				" next leaf starts at %d\n",
790a86c6181SAlex Tomas 				  le32_to_cpu(border));
791a86c6181SAlex Tomas 	} else {
792a86c6181SAlex Tomas 		border = newext->ee_block;
793a86c6181SAlex Tomas 		ext_debug("leaf will be added."
794a86c6181SAlex Tomas 				" next leaf starts at %d\n",
795a86c6181SAlex Tomas 				le32_to_cpu(border));
796a86c6181SAlex Tomas 	}
797a86c6181SAlex Tomas 
798a86c6181SAlex Tomas 	/*
799d0d856e8SRandy Dunlap 	 * If error occurs, then we break processing
800d0d856e8SRandy Dunlap 	 * and mark filesystem read-only. index won't
801a86c6181SAlex Tomas 	 * be inserted and tree will be in consistent
802d0d856e8SRandy Dunlap 	 * state. Next mount will repair buffers too.
803a86c6181SAlex Tomas 	 */
804a86c6181SAlex Tomas 
805a86c6181SAlex Tomas 	/*
806d0d856e8SRandy Dunlap 	 * Get array to track all allocated blocks.
807d0d856e8SRandy Dunlap 	 * We need this to handle errors and free blocks
808d0d856e8SRandy Dunlap 	 * upon them.
809a86c6181SAlex Tomas 	 */
8105d4958f9SAvantika Mathur 	ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
811a86c6181SAlex Tomas 	if (!ablocks)
812a86c6181SAlex Tomas 		return -ENOMEM;
813a86c6181SAlex Tomas 
814a86c6181SAlex Tomas 	/* allocate all needed blocks */
815a86c6181SAlex Tomas 	ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
816a86c6181SAlex Tomas 	for (a = 0; a < depth - at; a++) {
817654b4908SAneesh Kumar K.V 		newblock = ext4_ext_new_meta_block(handle, inode, path,
818654b4908SAneesh Kumar K.V 						   newext, &err);
819a86c6181SAlex Tomas 		if (newblock == 0)
820a86c6181SAlex Tomas 			goto cleanup;
821a86c6181SAlex Tomas 		ablocks[a] = newblock;
822a86c6181SAlex Tomas 	}
823a86c6181SAlex Tomas 
824a86c6181SAlex Tomas 	/* initialize new leaf */
825a86c6181SAlex Tomas 	newblock = ablocks[--a];
826a86c6181SAlex Tomas 	BUG_ON(newblock == 0);
827a86c6181SAlex Tomas 	bh = sb_getblk(inode->i_sb, newblock);
828a86c6181SAlex Tomas 	if (!bh) {
829a86c6181SAlex Tomas 		err = -EIO;
830a86c6181SAlex Tomas 		goto cleanup;
831a86c6181SAlex Tomas 	}
832a86c6181SAlex Tomas 	lock_buffer(bh);
833a86c6181SAlex Tomas 
8347e028976SAvantika Mathur 	err = ext4_journal_get_create_access(handle, bh);
8357e028976SAvantika Mathur 	if (err)
836a86c6181SAlex Tomas 		goto cleanup;
837a86c6181SAlex Tomas 
838a86c6181SAlex Tomas 	neh = ext_block_hdr(bh);
839a86c6181SAlex Tomas 	neh->eh_entries = 0;
840a86c6181SAlex Tomas 	neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
841a86c6181SAlex Tomas 	neh->eh_magic = EXT4_EXT_MAGIC;
842a86c6181SAlex Tomas 	neh->eh_depth = 0;
843a86c6181SAlex Tomas 	ex = EXT_FIRST_EXTENT(neh);
844a86c6181SAlex Tomas 
845d0d856e8SRandy Dunlap 	/* move remainder of path[depth] to the new leaf */
846a86c6181SAlex Tomas 	BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max);
847a86c6181SAlex Tomas 	/* start copy from next extent */
848a86c6181SAlex Tomas 	/* TODO: we could do it by single memmove */
849a86c6181SAlex Tomas 	m = 0;
850a86c6181SAlex Tomas 	path[depth].p_ext++;
851a86c6181SAlex Tomas 	while (path[depth].p_ext <=
852a86c6181SAlex Tomas 			EXT_MAX_EXTENT(path[depth].p_hdr)) {
8532ae02107SMingming Cao 		ext_debug("move %d:%llu:%d in new leaf %llu\n",
854a86c6181SAlex Tomas 				le32_to_cpu(path[depth].p_ext->ee_block),
855f65e6fbaSAlex Tomas 				ext_pblock(path[depth].p_ext),
856a2df2a63SAmit Arora 				ext4_ext_get_actual_len(path[depth].p_ext),
857a86c6181SAlex Tomas 				newblock);
858a86c6181SAlex Tomas 		/*memmove(ex++, path[depth].p_ext++,
859a86c6181SAlex Tomas 				sizeof(struct ext4_extent));
860a86c6181SAlex Tomas 		neh->eh_entries++;*/
861a86c6181SAlex Tomas 		path[depth].p_ext++;
862a86c6181SAlex Tomas 		m++;
863a86c6181SAlex Tomas 	}
864a86c6181SAlex Tomas 	if (m) {
865a86c6181SAlex Tomas 		memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
866e8546d06SMarcin Slusarz 		le16_add_cpu(&neh->eh_entries, m);
867a86c6181SAlex Tomas 	}
868a86c6181SAlex Tomas 
869a86c6181SAlex Tomas 	set_buffer_uptodate(bh);
870a86c6181SAlex Tomas 	unlock_buffer(bh);
871a86c6181SAlex Tomas 
8720390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, inode, bh);
8737e028976SAvantika Mathur 	if (err)
874a86c6181SAlex Tomas 		goto cleanup;
875a86c6181SAlex Tomas 	brelse(bh);
876a86c6181SAlex Tomas 	bh = NULL;
877a86c6181SAlex Tomas 
878a86c6181SAlex Tomas 	/* correct old leaf */
879a86c6181SAlex Tomas 	if (m) {
8807e028976SAvantika Mathur 		err = ext4_ext_get_access(handle, inode, path + depth);
8817e028976SAvantika Mathur 		if (err)
882a86c6181SAlex Tomas 			goto cleanup;
883e8546d06SMarcin Slusarz 		le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
8847e028976SAvantika Mathur 		err = ext4_ext_dirty(handle, inode, path + depth);
8857e028976SAvantika Mathur 		if (err)
886a86c6181SAlex Tomas 			goto cleanup;
887a86c6181SAlex Tomas 
888a86c6181SAlex Tomas 	}
889a86c6181SAlex Tomas 
890a86c6181SAlex Tomas 	/* create intermediate indexes */
891a86c6181SAlex Tomas 	k = depth - at - 1;
892a86c6181SAlex Tomas 	BUG_ON(k < 0);
893a86c6181SAlex Tomas 	if (k)
894a86c6181SAlex Tomas 		ext_debug("create %d intermediate indices\n", k);
895a86c6181SAlex Tomas 	/* insert new index into current index block */
896a86c6181SAlex Tomas 	/* current depth stored in i var */
897a86c6181SAlex Tomas 	i = depth - 1;
898a86c6181SAlex Tomas 	while (k--) {
899a86c6181SAlex Tomas 		oldblock = newblock;
900a86c6181SAlex Tomas 		newblock = ablocks[--a];
901bba90743SEric Sandeen 		bh = sb_getblk(inode->i_sb, newblock);
902a86c6181SAlex Tomas 		if (!bh) {
903a86c6181SAlex Tomas 			err = -EIO;
904a86c6181SAlex Tomas 			goto cleanup;
905a86c6181SAlex Tomas 		}
906a86c6181SAlex Tomas 		lock_buffer(bh);
907a86c6181SAlex Tomas 
9087e028976SAvantika Mathur 		err = ext4_journal_get_create_access(handle, bh);
9097e028976SAvantika Mathur 		if (err)
910a86c6181SAlex Tomas 			goto cleanup;
911a86c6181SAlex Tomas 
912a86c6181SAlex Tomas 		neh = ext_block_hdr(bh);
913a86c6181SAlex Tomas 		neh->eh_entries = cpu_to_le16(1);
914a86c6181SAlex Tomas 		neh->eh_magic = EXT4_EXT_MAGIC;
915a86c6181SAlex Tomas 		neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
916a86c6181SAlex Tomas 		neh->eh_depth = cpu_to_le16(depth - i);
917a86c6181SAlex Tomas 		fidx = EXT_FIRST_INDEX(neh);
918a86c6181SAlex Tomas 		fidx->ei_block = border;
919f65e6fbaSAlex Tomas 		ext4_idx_store_pblock(fidx, oldblock);
920a86c6181SAlex Tomas 
921bba90743SEric Sandeen 		ext_debug("int.index at %d (block %llu): %u -> %llu\n",
922bba90743SEric Sandeen 				i, newblock, le32_to_cpu(border), oldblock);
923a86c6181SAlex Tomas 		/* copy indexes */
924a86c6181SAlex Tomas 		m = 0;
925a86c6181SAlex Tomas 		path[i].p_idx++;
926a86c6181SAlex Tomas 
927a86c6181SAlex Tomas 		ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
928a86c6181SAlex Tomas 				EXT_MAX_INDEX(path[i].p_hdr));
929a86c6181SAlex Tomas 		BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) !=
930a86c6181SAlex Tomas 				EXT_LAST_INDEX(path[i].p_hdr));
931a86c6181SAlex Tomas 		while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
93226d535edSDmitry Monakhov 			ext_debug("%d: move %d:%llu in new index %llu\n", i,
933a86c6181SAlex Tomas 					le32_to_cpu(path[i].p_idx->ei_block),
934f65e6fbaSAlex Tomas 					idx_pblock(path[i].p_idx),
935a86c6181SAlex Tomas 					newblock);
936a86c6181SAlex Tomas 			/*memmove(++fidx, path[i].p_idx++,
937a86c6181SAlex Tomas 					sizeof(struct ext4_extent_idx));
938a86c6181SAlex Tomas 			neh->eh_entries++;
939a86c6181SAlex Tomas 			BUG_ON(neh->eh_entries > neh->eh_max);*/
940a86c6181SAlex Tomas 			path[i].p_idx++;
941a86c6181SAlex Tomas 			m++;
942a86c6181SAlex Tomas 		}
943a86c6181SAlex Tomas 		if (m) {
944a86c6181SAlex Tomas 			memmove(++fidx, path[i].p_idx - m,
945a86c6181SAlex Tomas 				sizeof(struct ext4_extent_idx) * m);
946e8546d06SMarcin Slusarz 			le16_add_cpu(&neh->eh_entries, m);
947a86c6181SAlex Tomas 		}
948a86c6181SAlex Tomas 		set_buffer_uptodate(bh);
949a86c6181SAlex Tomas 		unlock_buffer(bh);
950a86c6181SAlex Tomas 
9510390131bSFrank Mayhar 		err = ext4_handle_dirty_metadata(handle, inode, bh);
9527e028976SAvantika Mathur 		if (err)
953a86c6181SAlex Tomas 			goto cleanup;
954a86c6181SAlex Tomas 		brelse(bh);
955a86c6181SAlex Tomas 		bh = NULL;
956a86c6181SAlex Tomas 
957a86c6181SAlex Tomas 		/* correct old index */
958a86c6181SAlex Tomas 		if (m) {
959a86c6181SAlex Tomas 			err = ext4_ext_get_access(handle, inode, path + i);
960a86c6181SAlex Tomas 			if (err)
961a86c6181SAlex Tomas 				goto cleanup;
962e8546d06SMarcin Slusarz 			le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
963a86c6181SAlex Tomas 			err = ext4_ext_dirty(handle, inode, path + i);
964a86c6181SAlex Tomas 			if (err)
965a86c6181SAlex Tomas 				goto cleanup;
966a86c6181SAlex Tomas 		}
967a86c6181SAlex Tomas 
968a86c6181SAlex Tomas 		i--;
969a86c6181SAlex Tomas 	}
970a86c6181SAlex Tomas 
971a86c6181SAlex Tomas 	/* insert new index */
972a86c6181SAlex Tomas 	err = ext4_ext_insert_index(handle, inode, path + at,
973a86c6181SAlex Tomas 				    le32_to_cpu(border), newblock);
974a86c6181SAlex Tomas 
975a86c6181SAlex Tomas cleanup:
976a86c6181SAlex Tomas 	if (bh) {
977a86c6181SAlex Tomas 		if (buffer_locked(bh))
978a86c6181SAlex Tomas 			unlock_buffer(bh);
979a86c6181SAlex Tomas 		brelse(bh);
980a86c6181SAlex Tomas 	}
981a86c6181SAlex Tomas 
982a86c6181SAlex Tomas 	if (err) {
983a86c6181SAlex Tomas 		/* free all allocated blocks in error case */
984a86c6181SAlex Tomas 		for (i = 0; i < depth; i++) {
985a86c6181SAlex Tomas 			if (!ablocks[i])
986a86c6181SAlex Tomas 				continue;
987c9de560dSAlex Tomas 			ext4_free_blocks(handle, inode, ablocks[i], 1, 1);
988a86c6181SAlex Tomas 		}
989a86c6181SAlex Tomas 	}
990a86c6181SAlex Tomas 	kfree(ablocks);
991a86c6181SAlex Tomas 
992a86c6181SAlex Tomas 	return err;
993a86c6181SAlex Tomas }
994a86c6181SAlex Tomas 
995a86c6181SAlex Tomas /*
996d0d856e8SRandy Dunlap  * ext4_ext_grow_indepth:
997d0d856e8SRandy Dunlap  * implements tree growing procedure:
998a86c6181SAlex Tomas  * - allocates new block
999a86c6181SAlex Tomas  * - moves top-level data (index block or leaf) into the new block
1000d0d856e8SRandy Dunlap  * - initializes new top-level, creating index that points to the
1001a86c6181SAlex Tomas  *   just created block
1002a86c6181SAlex Tomas  */
1003a86c6181SAlex Tomas static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
1004a86c6181SAlex Tomas 					struct ext4_ext_path *path,
1005a86c6181SAlex Tomas 					struct ext4_extent *newext)
1006a86c6181SAlex Tomas {
1007a86c6181SAlex Tomas 	struct ext4_ext_path *curp = path;
1008a86c6181SAlex Tomas 	struct ext4_extent_header *neh;
1009a86c6181SAlex Tomas 	struct ext4_extent_idx *fidx;
1010a86c6181SAlex Tomas 	struct buffer_head *bh;
1011f65e6fbaSAlex Tomas 	ext4_fsblk_t newblock;
1012a86c6181SAlex Tomas 	int err = 0;
1013a86c6181SAlex Tomas 
1014654b4908SAneesh Kumar K.V 	newblock = ext4_ext_new_meta_block(handle, inode, path, newext, &err);
1015a86c6181SAlex Tomas 	if (newblock == 0)
1016a86c6181SAlex Tomas 		return err;
1017a86c6181SAlex Tomas 
1018a86c6181SAlex Tomas 	bh = sb_getblk(inode->i_sb, newblock);
1019a86c6181SAlex Tomas 	if (!bh) {
1020a86c6181SAlex Tomas 		err = -EIO;
1021a86c6181SAlex Tomas 		ext4_std_error(inode->i_sb, err);
1022a86c6181SAlex Tomas 		return err;
1023a86c6181SAlex Tomas 	}
1024a86c6181SAlex Tomas 	lock_buffer(bh);
1025a86c6181SAlex Tomas 
10267e028976SAvantika Mathur 	err = ext4_journal_get_create_access(handle, bh);
10277e028976SAvantika Mathur 	if (err) {
1028a86c6181SAlex Tomas 		unlock_buffer(bh);
1029a86c6181SAlex Tomas 		goto out;
1030a86c6181SAlex Tomas 	}
1031a86c6181SAlex Tomas 
1032a86c6181SAlex Tomas 	/* move top-level index/leaf into new block */
1033a86c6181SAlex Tomas 	memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
1034a86c6181SAlex Tomas 
1035a86c6181SAlex Tomas 	/* set size of new block */
1036a86c6181SAlex Tomas 	neh = ext_block_hdr(bh);
1037a86c6181SAlex Tomas 	/* old root could have indexes or leaves
1038a86c6181SAlex Tomas 	 * so calculate e_max right way */
1039a86c6181SAlex Tomas 	if (ext_depth(inode))
1040a86c6181SAlex Tomas 	  neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
1041a86c6181SAlex Tomas 	else
1042a86c6181SAlex Tomas 	  neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
1043a86c6181SAlex Tomas 	neh->eh_magic = EXT4_EXT_MAGIC;
1044a86c6181SAlex Tomas 	set_buffer_uptodate(bh);
1045a86c6181SAlex Tomas 	unlock_buffer(bh);
1046a86c6181SAlex Tomas 
10470390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, inode, bh);
10487e028976SAvantika Mathur 	if (err)
1049a86c6181SAlex Tomas 		goto out;
1050a86c6181SAlex Tomas 
1051a86c6181SAlex Tomas 	/* create index in new top-level index: num,max,pointer */
10527e028976SAvantika Mathur 	err = ext4_ext_get_access(handle, inode, curp);
10537e028976SAvantika Mathur 	if (err)
1054a86c6181SAlex Tomas 		goto out;
1055a86c6181SAlex Tomas 
1056a86c6181SAlex Tomas 	curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
1057a86c6181SAlex Tomas 	curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode));
1058a86c6181SAlex Tomas 	curp->p_hdr->eh_entries = cpu_to_le16(1);
1059a86c6181SAlex Tomas 	curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
1060e9f410b1SDmitry Monakhov 
1061e9f410b1SDmitry Monakhov 	if (path[0].p_hdr->eh_depth)
1062e9f410b1SDmitry Monakhov 		curp->p_idx->ei_block =
1063e9f410b1SDmitry Monakhov 			EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
1064e9f410b1SDmitry Monakhov 	else
1065e9f410b1SDmitry Monakhov 		curp->p_idx->ei_block =
1066e9f410b1SDmitry Monakhov 			EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
1067f65e6fbaSAlex Tomas 	ext4_idx_store_pblock(curp->p_idx, newblock);
1068a86c6181SAlex Tomas 
1069a86c6181SAlex Tomas 	neh = ext_inode_hdr(inode);
1070a86c6181SAlex Tomas 	fidx = EXT_FIRST_INDEX(neh);
10712ae02107SMingming Cao 	ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
1072a86c6181SAlex Tomas 		  le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
1073f65e6fbaSAlex Tomas 		  le32_to_cpu(fidx->ei_block), idx_pblock(fidx));
1074a86c6181SAlex Tomas 
1075a86c6181SAlex Tomas 	neh->eh_depth = cpu_to_le16(path->p_depth + 1);
1076a86c6181SAlex Tomas 	err = ext4_ext_dirty(handle, inode, curp);
1077a86c6181SAlex Tomas out:
1078a86c6181SAlex Tomas 	brelse(bh);
1079a86c6181SAlex Tomas 
1080a86c6181SAlex Tomas 	return err;
1081a86c6181SAlex Tomas }
1082a86c6181SAlex Tomas 
1083a86c6181SAlex Tomas /*
1084d0d856e8SRandy Dunlap  * ext4_ext_create_new_leaf:
1085d0d856e8SRandy Dunlap  * finds empty index and adds new leaf.
1086d0d856e8SRandy Dunlap  * if no free index is found, then it requests in-depth growing.
1087a86c6181SAlex Tomas  */
1088a86c6181SAlex Tomas static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
1089a86c6181SAlex Tomas 					struct ext4_ext_path *path,
1090a86c6181SAlex Tomas 					struct ext4_extent *newext)
1091a86c6181SAlex Tomas {
1092a86c6181SAlex Tomas 	struct ext4_ext_path *curp;
1093a86c6181SAlex Tomas 	int depth, i, err = 0;
1094a86c6181SAlex Tomas 
1095a86c6181SAlex Tomas repeat:
1096a86c6181SAlex Tomas 	i = depth = ext_depth(inode);
1097a86c6181SAlex Tomas 
1098a86c6181SAlex Tomas 	/* walk up to the tree and look for free index entry */
1099a86c6181SAlex Tomas 	curp = path + depth;
1100a86c6181SAlex Tomas 	while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1101a86c6181SAlex Tomas 		i--;
1102a86c6181SAlex Tomas 		curp--;
1103a86c6181SAlex Tomas 	}
1104a86c6181SAlex Tomas 
1105d0d856e8SRandy Dunlap 	/* we use already allocated block for index block,
1106d0d856e8SRandy Dunlap 	 * so subsequent data blocks should be contiguous */
1107a86c6181SAlex Tomas 	if (EXT_HAS_FREE_INDEX(curp)) {
1108a86c6181SAlex Tomas 		/* if we found index with free entry, then use that
1109a86c6181SAlex Tomas 		 * entry: create all needed subtree and add new leaf */
1110a86c6181SAlex Tomas 		err = ext4_ext_split(handle, inode, path, newext, i);
1111787e0981SShen Feng 		if (err)
1112787e0981SShen Feng 			goto out;
1113a86c6181SAlex Tomas 
1114a86c6181SAlex Tomas 		/* refill path */
1115a86c6181SAlex Tomas 		ext4_ext_drop_refs(path);
1116a86c6181SAlex Tomas 		path = ext4_ext_find_extent(inode,
1117725d26d3SAneesh Kumar K.V 				    (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1118a86c6181SAlex Tomas 				    path);
1119a86c6181SAlex Tomas 		if (IS_ERR(path))
1120a86c6181SAlex Tomas 			err = PTR_ERR(path);
1121a86c6181SAlex Tomas 	} else {
1122a86c6181SAlex Tomas 		/* tree is full, time to grow in depth */
1123a86c6181SAlex Tomas 		err = ext4_ext_grow_indepth(handle, inode, path, newext);
1124a86c6181SAlex Tomas 		if (err)
1125a86c6181SAlex Tomas 			goto out;
1126a86c6181SAlex Tomas 
1127a86c6181SAlex Tomas 		/* refill path */
1128a86c6181SAlex Tomas 		ext4_ext_drop_refs(path);
1129a86c6181SAlex Tomas 		path = ext4_ext_find_extent(inode,
1130725d26d3SAneesh Kumar K.V 				   (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1131a86c6181SAlex Tomas 				    path);
1132a86c6181SAlex Tomas 		if (IS_ERR(path)) {
1133a86c6181SAlex Tomas 			err = PTR_ERR(path);
1134a86c6181SAlex Tomas 			goto out;
1135a86c6181SAlex Tomas 		}
1136a86c6181SAlex Tomas 
1137a86c6181SAlex Tomas 		/*
1138d0d856e8SRandy Dunlap 		 * only first (depth 0 -> 1) produces free space;
1139d0d856e8SRandy Dunlap 		 * in all other cases we have to split the grown tree
1140a86c6181SAlex Tomas 		 */
1141a86c6181SAlex Tomas 		depth = ext_depth(inode);
1142a86c6181SAlex Tomas 		if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
1143d0d856e8SRandy Dunlap 			/* now we need to split */
1144a86c6181SAlex Tomas 			goto repeat;
1145a86c6181SAlex Tomas 		}
1146a86c6181SAlex Tomas 	}
1147a86c6181SAlex Tomas 
1148a86c6181SAlex Tomas out:
1149a86c6181SAlex Tomas 	return err;
1150a86c6181SAlex Tomas }
1151a86c6181SAlex Tomas 
1152a86c6181SAlex Tomas /*
11531988b51eSAlex Tomas  * search the closest allocated block to the left for *logical
11541988b51eSAlex Tomas  * and returns it at @logical + it's physical address at @phys
11551988b51eSAlex Tomas  * if *logical is the smallest allocated block, the function
11561988b51eSAlex Tomas  * returns 0 at @phys
11571988b51eSAlex Tomas  * return value contains 0 (success) or error code
11581988b51eSAlex Tomas  */
11591988b51eSAlex Tomas int
11601988b51eSAlex Tomas ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path,
11611988b51eSAlex Tomas 			ext4_lblk_t *logical, ext4_fsblk_t *phys)
11621988b51eSAlex Tomas {
11631988b51eSAlex Tomas 	struct ext4_extent_idx *ix;
11641988b51eSAlex Tomas 	struct ext4_extent *ex;
1165b939e376SAneesh Kumar K.V 	int depth, ee_len;
11661988b51eSAlex Tomas 
11671988b51eSAlex Tomas 	BUG_ON(path == NULL);
11681988b51eSAlex Tomas 	depth = path->p_depth;
11691988b51eSAlex Tomas 	*phys = 0;
11701988b51eSAlex Tomas 
11711988b51eSAlex Tomas 	if (depth == 0 && path->p_ext == NULL)
11721988b51eSAlex Tomas 		return 0;
11731988b51eSAlex Tomas 
11741988b51eSAlex Tomas 	/* usually extent in the path covers blocks smaller
11751988b51eSAlex Tomas 	 * then *logical, but it can be that extent is the
11761988b51eSAlex Tomas 	 * first one in the file */
11771988b51eSAlex Tomas 
11781988b51eSAlex Tomas 	ex = path[depth].p_ext;
1179b939e376SAneesh Kumar K.V 	ee_len = ext4_ext_get_actual_len(ex);
11801988b51eSAlex Tomas 	if (*logical < le32_to_cpu(ex->ee_block)) {
11811988b51eSAlex Tomas 		BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
11821988b51eSAlex Tomas 		while (--depth >= 0) {
11831988b51eSAlex Tomas 			ix = path[depth].p_idx;
11841988b51eSAlex Tomas 			BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
11851988b51eSAlex Tomas 		}
11861988b51eSAlex Tomas 		return 0;
11871988b51eSAlex Tomas 	}
11881988b51eSAlex Tomas 
1189b939e376SAneesh Kumar K.V 	BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
11901988b51eSAlex Tomas 
1191b939e376SAneesh Kumar K.V 	*logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
1192b939e376SAneesh Kumar K.V 	*phys = ext_pblock(ex) + ee_len - 1;
11931988b51eSAlex Tomas 	return 0;
11941988b51eSAlex Tomas }
11951988b51eSAlex Tomas 
11961988b51eSAlex Tomas /*
11971988b51eSAlex Tomas  * search the closest allocated block to the right for *logical
11981988b51eSAlex Tomas  * and returns it at @logical + it's physical address at @phys
11991988b51eSAlex Tomas  * if *logical is the smallest allocated block, the function
12001988b51eSAlex Tomas  * returns 0 at @phys
12011988b51eSAlex Tomas  * return value contains 0 (success) or error code
12021988b51eSAlex Tomas  */
12031988b51eSAlex Tomas int
12041988b51eSAlex Tomas ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path,
12051988b51eSAlex Tomas 			ext4_lblk_t *logical, ext4_fsblk_t *phys)
12061988b51eSAlex Tomas {
12071988b51eSAlex Tomas 	struct buffer_head *bh = NULL;
12081988b51eSAlex Tomas 	struct ext4_extent_header *eh;
12091988b51eSAlex Tomas 	struct ext4_extent_idx *ix;
12101988b51eSAlex Tomas 	struct ext4_extent *ex;
12111988b51eSAlex Tomas 	ext4_fsblk_t block;
1212395a87bfSEric Sandeen 	int depth;	/* Note, NOT eh_depth; depth from top of tree */
1213395a87bfSEric Sandeen 	int ee_len;
12141988b51eSAlex Tomas 
12151988b51eSAlex Tomas 	BUG_ON(path == NULL);
12161988b51eSAlex Tomas 	depth = path->p_depth;
12171988b51eSAlex Tomas 	*phys = 0;
12181988b51eSAlex Tomas 
12191988b51eSAlex Tomas 	if (depth == 0 && path->p_ext == NULL)
12201988b51eSAlex Tomas 		return 0;
12211988b51eSAlex Tomas 
12221988b51eSAlex Tomas 	/* usually extent in the path covers blocks smaller
12231988b51eSAlex Tomas 	 * then *logical, but it can be that extent is the
12241988b51eSAlex Tomas 	 * first one in the file */
12251988b51eSAlex Tomas 
12261988b51eSAlex Tomas 	ex = path[depth].p_ext;
1227b939e376SAneesh Kumar K.V 	ee_len = ext4_ext_get_actual_len(ex);
12281988b51eSAlex Tomas 	if (*logical < le32_to_cpu(ex->ee_block)) {
12291988b51eSAlex Tomas 		BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
12301988b51eSAlex Tomas 		while (--depth >= 0) {
12311988b51eSAlex Tomas 			ix = path[depth].p_idx;
12321988b51eSAlex Tomas 			BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
12331988b51eSAlex Tomas 		}
12341988b51eSAlex Tomas 		*logical = le32_to_cpu(ex->ee_block);
12351988b51eSAlex Tomas 		*phys = ext_pblock(ex);
12361988b51eSAlex Tomas 		return 0;
12371988b51eSAlex Tomas 	}
12381988b51eSAlex Tomas 
1239b939e376SAneesh Kumar K.V 	BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
12401988b51eSAlex Tomas 
12411988b51eSAlex Tomas 	if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
12421988b51eSAlex Tomas 		/* next allocated block in this leaf */
12431988b51eSAlex Tomas 		ex++;
12441988b51eSAlex Tomas 		*logical = le32_to_cpu(ex->ee_block);
12451988b51eSAlex Tomas 		*phys = ext_pblock(ex);
12461988b51eSAlex Tomas 		return 0;
12471988b51eSAlex Tomas 	}
12481988b51eSAlex Tomas 
12491988b51eSAlex Tomas 	/* go up and search for index to the right */
12501988b51eSAlex Tomas 	while (--depth >= 0) {
12511988b51eSAlex Tomas 		ix = path[depth].p_idx;
12521988b51eSAlex Tomas 		if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
125325f1ee3aSWu Fengguang 			goto got_index;
12541988b51eSAlex Tomas 	}
12551988b51eSAlex Tomas 
125625f1ee3aSWu Fengguang 	/* we've gone up to the root and found no index to the right */
12571988b51eSAlex Tomas 	return 0;
12581988b51eSAlex Tomas 
125925f1ee3aSWu Fengguang got_index:
12601988b51eSAlex Tomas 	/* we've found index to the right, let's
12611988b51eSAlex Tomas 	 * follow it and find the closest allocated
12621988b51eSAlex Tomas 	 * block to the right */
12631988b51eSAlex Tomas 	ix++;
12641988b51eSAlex Tomas 	block = idx_pblock(ix);
12651988b51eSAlex Tomas 	while (++depth < path->p_depth) {
12661988b51eSAlex Tomas 		bh = sb_bread(inode->i_sb, block);
12671988b51eSAlex Tomas 		if (bh == NULL)
12681988b51eSAlex Tomas 			return -EIO;
12691988b51eSAlex Tomas 		eh = ext_block_hdr(bh);
1270395a87bfSEric Sandeen 		/* subtract from p_depth to get proper eh_depth */
127156b19868SAneesh Kumar K.V 		if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
12721988b51eSAlex Tomas 			put_bh(bh);
12731988b51eSAlex Tomas 			return -EIO;
12741988b51eSAlex Tomas 		}
12751988b51eSAlex Tomas 		ix = EXT_FIRST_INDEX(eh);
12761988b51eSAlex Tomas 		block = idx_pblock(ix);
12771988b51eSAlex Tomas 		put_bh(bh);
12781988b51eSAlex Tomas 	}
12791988b51eSAlex Tomas 
12801988b51eSAlex Tomas 	bh = sb_bread(inode->i_sb, block);
12811988b51eSAlex Tomas 	if (bh == NULL)
12821988b51eSAlex Tomas 		return -EIO;
12831988b51eSAlex Tomas 	eh = ext_block_hdr(bh);
128456b19868SAneesh Kumar K.V 	if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
12851988b51eSAlex Tomas 		put_bh(bh);
12861988b51eSAlex Tomas 		return -EIO;
12871988b51eSAlex Tomas 	}
12881988b51eSAlex Tomas 	ex = EXT_FIRST_EXTENT(eh);
12891988b51eSAlex Tomas 	*logical = le32_to_cpu(ex->ee_block);
12901988b51eSAlex Tomas 	*phys = ext_pblock(ex);
12911988b51eSAlex Tomas 	put_bh(bh);
12921988b51eSAlex Tomas 	return 0;
12931988b51eSAlex Tomas }
12941988b51eSAlex Tomas 
12951988b51eSAlex Tomas /*
1296d0d856e8SRandy Dunlap  * ext4_ext_next_allocated_block:
1297d0d856e8SRandy Dunlap  * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1298d0d856e8SRandy Dunlap  * NOTE: it considers block number from index entry as
1299d0d856e8SRandy Dunlap  * allocated block. Thus, index entries have to be consistent
1300d0d856e8SRandy Dunlap  * with leaves.
1301a86c6181SAlex Tomas  */
1302725d26d3SAneesh Kumar K.V static ext4_lblk_t
1303a86c6181SAlex Tomas ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1304a86c6181SAlex Tomas {
1305a86c6181SAlex Tomas 	int depth;
1306a86c6181SAlex Tomas 
1307a86c6181SAlex Tomas 	BUG_ON(path == NULL);
1308a86c6181SAlex Tomas 	depth = path->p_depth;
1309a86c6181SAlex Tomas 
1310a86c6181SAlex Tomas 	if (depth == 0 && path->p_ext == NULL)
1311a86c6181SAlex Tomas 		return EXT_MAX_BLOCK;
1312a86c6181SAlex Tomas 
1313a86c6181SAlex Tomas 	while (depth >= 0) {
1314a86c6181SAlex Tomas 		if (depth == path->p_depth) {
1315a86c6181SAlex Tomas 			/* leaf */
1316a86c6181SAlex Tomas 			if (path[depth].p_ext !=
1317a86c6181SAlex Tomas 					EXT_LAST_EXTENT(path[depth].p_hdr))
1318a86c6181SAlex Tomas 			  return le32_to_cpu(path[depth].p_ext[1].ee_block);
1319a86c6181SAlex Tomas 		} else {
1320a86c6181SAlex Tomas 			/* index */
1321a86c6181SAlex Tomas 			if (path[depth].p_idx !=
1322a86c6181SAlex Tomas 					EXT_LAST_INDEX(path[depth].p_hdr))
1323a86c6181SAlex Tomas 			  return le32_to_cpu(path[depth].p_idx[1].ei_block);
1324a86c6181SAlex Tomas 		}
1325a86c6181SAlex Tomas 		depth--;
1326a86c6181SAlex Tomas 	}
1327a86c6181SAlex Tomas 
1328a86c6181SAlex Tomas 	return EXT_MAX_BLOCK;
1329a86c6181SAlex Tomas }
1330a86c6181SAlex Tomas 
1331a86c6181SAlex Tomas /*
1332d0d856e8SRandy Dunlap  * ext4_ext_next_leaf_block:
1333a86c6181SAlex Tomas  * returns first allocated block from next leaf or EXT_MAX_BLOCK
1334a86c6181SAlex Tomas  */
1335725d26d3SAneesh Kumar K.V static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode,
1336a86c6181SAlex Tomas 					struct ext4_ext_path *path)
1337a86c6181SAlex Tomas {
1338a86c6181SAlex Tomas 	int depth;
1339a86c6181SAlex Tomas 
1340a86c6181SAlex Tomas 	BUG_ON(path == NULL);
1341a86c6181SAlex Tomas 	depth = path->p_depth;
1342a86c6181SAlex Tomas 
1343a86c6181SAlex Tomas 	/* zero-tree has no leaf blocks at all */
1344a86c6181SAlex Tomas 	if (depth == 0)
1345a86c6181SAlex Tomas 		return EXT_MAX_BLOCK;
1346a86c6181SAlex Tomas 
1347a86c6181SAlex Tomas 	/* go to index block */
1348a86c6181SAlex Tomas 	depth--;
1349a86c6181SAlex Tomas 
1350a86c6181SAlex Tomas 	while (depth >= 0) {
1351a86c6181SAlex Tomas 		if (path[depth].p_idx !=
1352a86c6181SAlex Tomas 				EXT_LAST_INDEX(path[depth].p_hdr))
1353725d26d3SAneesh Kumar K.V 			return (ext4_lblk_t)
1354725d26d3SAneesh Kumar K.V 				le32_to_cpu(path[depth].p_idx[1].ei_block);
1355a86c6181SAlex Tomas 		depth--;
1356a86c6181SAlex Tomas 	}
1357a86c6181SAlex Tomas 
1358a86c6181SAlex Tomas 	return EXT_MAX_BLOCK;
1359a86c6181SAlex Tomas }
1360a86c6181SAlex Tomas 
1361a86c6181SAlex Tomas /*
1362d0d856e8SRandy Dunlap  * ext4_ext_correct_indexes:
1363d0d856e8SRandy Dunlap  * if leaf gets modified and modified extent is first in the leaf,
1364d0d856e8SRandy Dunlap  * then we have to correct all indexes above.
1365a86c6181SAlex Tomas  * TODO: do we need to correct tree in all cases?
1366a86c6181SAlex Tomas  */
13671d03ec98SAneesh Kumar K.V static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
1368a86c6181SAlex Tomas 				struct ext4_ext_path *path)
1369a86c6181SAlex Tomas {
1370a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
1371a86c6181SAlex Tomas 	int depth = ext_depth(inode);
1372a86c6181SAlex Tomas 	struct ext4_extent *ex;
1373a86c6181SAlex Tomas 	__le32 border;
1374a86c6181SAlex Tomas 	int k, err = 0;
1375a86c6181SAlex Tomas 
1376a86c6181SAlex Tomas 	eh = path[depth].p_hdr;
1377a86c6181SAlex Tomas 	ex = path[depth].p_ext;
1378a86c6181SAlex Tomas 	BUG_ON(ex == NULL);
1379a86c6181SAlex Tomas 	BUG_ON(eh == NULL);
1380a86c6181SAlex Tomas 
1381a86c6181SAlex Tomas 	if (depth == 0) {
1382a86c6181SAlex Tomas 		/* there is no tree at all */
1383a86c6181SAlex Tomas 		return 0;
1384a86c6181SAlex Tomas 	}
1385a86c6181SAlex Tomas 
1386a86c6181SAlex Tomas 	if (ex != EXT_FIRST_EXTENT(eh)) {
1387a86c6181SAlex Tomas 		/* we correct tree if first leaf got modified only */
1388a86c6181SAlex Tomas 		return 0;
1389a86c6181SAlex Tomas 	}
1390a86c6181SAlex Tomas 
1391a86c6181SAlex Tomas 	/*
1392d0d856e8SRandy Dunlap 	 * TODO: we need correction if border is smaller than current one
1393a86c6181SAlex Tomas 	 */
1394a86c6181SAlex Tomas 	k = depth - 1;
1395a86c6181SAlex Tomas 	border = path[depth].p_ext->ee_block;
13967e028976SAvantika Mathur 	err = ext4_ext_get_access(handle, inode, path + k);
13977e028976SAvantika Mathur 	if (err)
1398a86c6181SAlex Tomas 		return err;
1399a86c6181SAlex Tomas 	path[k].p_idx->ei_block = border;
14007e028976SAvantika Mathur 	err = ext4_ext_dirty(handle, inode, path + k);
14017e028976SAvantika Mathur 	if (err)
1402a86c6181SAlex Tomas 		return err;
1403a86c6181SAlex Tomas 
1404a86c6181SAlex Tomas 	while (k--) {
1405a86c6181SAlex Tomas 		/* change all left-side indexes */
1406a86c6181SAlex Tomas 		if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1407a86c6181SAlex Tomas 			break;
14087e028976SAvantika Mathur 		err = ext4_ext_get_access(handle, inode, path + k);
14097e028976SAvantika Mathur 		if (err)
1410a86c6181SAlex Tomas 			break;
1411a86c6181SAlex Tomas 		path[k].p_idx->ei_block = border;
14127e028976SAvantika Mathur 		err = ext4_ext_dirty(handle, inode, path + k);
14137e028976SAvantika Mathur 		if (err)
1414a86c6181SAlex Tomas 			break;
1415a86c6181SAlex Tomas 	}
1416a86c6181SAlex Tomas 
1417a86c6181SAlex Tomas 	return err;
1418a86c6181SAlex Tomas }
1419a86c6181SAlex Tomas 
1420*748de673SAkira Fujita int
1421a86c6181SAlex Tomas ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1422a86c6181SAlex Tomas 				struct ext4_extent *ex2)
1423a86c6181SAlex Tomas {
1424749269faSAmit Arora 	unsigned short ext1_ee_len, ext2_ee_len, max_len;
1425a2df2a63SAmit Arora 
1426a2df2a63SAmit Arora 	/*
1427a2df2a63SAmit Arora 	 * Make sure that either both extents are uninitialized, or
1428a2df2a63SAmit Arora 	 * both are _not_.
1429a2df2a63SAmit Arora 	 */
1430a2df2a63SAmit Arora 	if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1431a2df2a63SAmit Arora 		return 0;
1432a2df2a63SAmit Arora 
1433749269faSAmit Arora 	if (ext4_ext_is_uninitialized(ex1))
1434749269faSAmit Arora 		max_len = EXT_UNINIT_MAX_LEN;
1435749269faSAmit Arora 	else
1436749269faSAmit Arora 		max_len = EXT_INIT_MAX_LEN;
1437749269faSAmit Arora 
1438a2df2a63SAmit Arora 	ext1_ee_len = ext4_ext_get_actual_len(ex1);
1439a2df2a63SAmit Arora 	ext2_ee_len = ext4_ext_get_actual_len(ex2);
1440a2df2a63SAmit Arora 
1441a2df2a63SAmit Arora 	if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
144263f57933SAndrew Morton 			le32_to_cpu(ex2->ee_block))
1443a86c6181SAlex Tomas 		return 0;
1444a86c6181SAlex Tomas 
1445471d4011SSuparna Bhattacharya 	/*
1446471d4011SSuparna Bhattacharya 	 * To allow future support for preallocated extents to be added
1447471d4011SSuparna Bhattacharya 	 * as an RO_COMPAT feature, refuse to merge to extents if
1448d0d856e8SRandy Dunlap 	 * this can result in the top bit of ee_len being set.
1449471d4011SSuparna Bhattacharya 	 */
1450749269faSAmit Arora 	if (ext1_ee_len + ext2_ee_len > max_len)
1451471d4011SSuparna Bhattacharya 		return 0;
1452bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST
1453b939e376SAneesh Kumar K.V 	if (ext1_ee_len >= 4)
1454a86c6181SAlex Tomas 		return 0;
1455a86c6181SAlex Tomas #endif
1456a86c6181SAlex Tomas 
1457a2df2a63SAmit Arora 	if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2))
1458a86c6181SAlex Tomas 		return 1;
1459a86c6181SAlex Tomas 	return 0;
1460a86c6181SAlex Tomas }
1461a86c6181SAlex Tomas 
1462a86c6181SAlex Tomas /*
146356055d3aSAmit Arora  * This function tries to merge the "ex" extent to the next extent in the tree.
146456055d3aSAmit Arora  * It always tries to merge towards right. If you want to merge towards
146556055d3aSAmit Arora  * left, pass "ex - 1" as argument instead of "ex".
146656055d3aSAmit Arora  * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
146756055d3aSAmit Arora  * 1 if they got merged.
146856055d3aSAmit Arora  */
146956055d3aSAmit Arora int ext4_ext_try_to_merge(struct inode *inode,
147056055d3aSAmit Arora 			  struct ext4_ext_path *path,
147156055d3aSAmit Arora 			  struct ext4_extent *ex)
147256055d3aSAmit Arora {
147356055d3aSAmit Arora 	struct ext4_extent_header *eh;
147456055d3aSAmit Arora 	unsigned int depth, len;
147556055d3aSAmit Arora 	int merge_done = 0;
147656055d3aSAmit Arora 	int uninitialized = 0;
147756055d3aSAmit Arora 
147856055d3aSAmit Arora 	depth = ext_depth(inode);
147956055d3aSAmit Arora 	BUG_ON(path[depth].p_hdr == NULL);
148056055d3aSAmit Arora 	eh = path[depth].p_hdr;
148156055d3aSAmit Arora 
148256055d3aSAmit Arora 	while (ex < EXT_LAST_EXTENT(eh)) {
148356055d3aSAmit Arora 		if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
148456055d3aSAmit Arora 			break;
148556055d3aSAmit Arora 		/* merge with next extent! */
148656055d3aSAmit Arora 		if (ext4_ext_is_uninitialized(ex))
148756055d3aSAmit Arora 			uninitialized = 1;
148856055d3aSAmit Arora 		ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
148956055d3aSAmit Arora 				+ ext4_ext_get_actual_len(ex + 1));
149056055d3aSAmit Arora 		if (uninitialized)
149156055d3aSAmit Arora 			ext4_ext_mark_uninitialized(ex);
149256055d3aSAmit Arora 
149356055d3aSAmit Arora 		if (ex + 1 < EXT_LAST_EXTENT(eh)) {
149456055d3aSAmit Arora 			len = (EXT_LAST_EXTENT(eh) - ex - 1)
149556055d3aSAmit Arora 				* sizeof(struct ext4_extent);
149656055d3aSAmit Arora 			memmove(ex + 1, ex + 2, len);
149756055d3aSAmit Arora 		}
1498e8546d06SMarcin Slusarz 		le16_add_cpu(&eh->eh_entries, -1);
149956055d3aSAmit Arora 		merge_done = 1;
150056055d3aSAmit Arora 		WARN_ON(eh->eh_entries == 0);
150156055d3aSAmit Arora 		if (!eh->eh_entries)
150256055d3aSAmit Arora 			ext4_error(inode->i_sb, "ext4_ext_try_to_merge",
150356055d3aSAmit Arora 			   "inode#%lu, eh->eh_entries = 0!", inode->i_ino);
150456055d3aSAmit Arora 	}
150556055d3aSAmit Arora 
150656055d3aSAmit Arora 	return merge_done;
150756055d3aSAmit Arora }
150856055d3aSAmit Arora 
150956055d3aSAmit Arora /*
151025d14f98SAmit Arora  * check if a portion of the "newext" extent overlaps with an
151125d14f98SAmit Arora  * existing extent.
151225d14f98SAmit Arora  *
151325d14f98SAmit Arora  * If there is an overlap discovered, it updates the length of the newext
151425d14f98SAmit Arora  * such that there will be no overlap, and then returns 1.
151525d14f98SAmit Arora  * If there is no overlap found, it returns 0.
151625d14f98SAmit Arora  */
151725d14f98SAmit Arora unsigned int ext4_ext_check_overlap(struct inode *inode,
151825d14f98SAmit Arora 				    struct ext4_extent *newext,
151925d14f98SAmit Arora 				    struct ext4_ext_path *path)
152025d14f98SAmit Arora {
1521725d26d3SAneesh Kumar K.V 	ext4_lblk_t b1, b2;
152225d14f98SAmit Arora 	unsigned int depth, len1;
152325d14f98SAmit Arora 	unsigned int ret = 0;
152425d14f98SAmit Arora 
152525d14f98SAmit Arora 	b1 = le32_to_cpu(newext->ee_block);
1526a2df2a63SAmit Arora 	len1 = ext4_ext_get_actual_len(newext);
152725d14f98SAmit Arora 	depth = ext_depth(inode);
152825d14f98SAmit Arora 	if (!path[depth].p_ext)
152925d14f98SAmit Arora 		goto out;
153025d14f98SAmit Arora 	b2 = le32_to_cpu(path[depth].p_ext->ee_block);
153125d14f98SAmit Arora 
153225d14f98SAmit Arora 	/*
153325d14f98SAmit Arora 	 * get the next allocated block if the extent in the path
153425d14f98SAmit Arora 	 * is before the requested block(s)
153525d14f98SAmit Arora 	 */
153625d14f98SAmit Arora 	if (b2 < b1) {
153725d14f98SAmit Arora 		b2 = ext4_ext_next_allocated_block(path);
153825d14f98SAmit Arora 		if (b2 == EXT_MAX_BLOCK)
153925d14f98SAmit Arora 			goto out;
154025d14f98SAmit Arora 	}
154125d14f98SAmit Arora 
1542725d26d3SAneesh Kumar K.V 	/* check for wrap through zero on extent logical start block*/
154325d14f98SAmit Arora 	if (b1 + len1 < b1) {
154425d14f98SAmit Arora 		len1 = EXT_MAX_BLOCK - b1;
154525d14f98SAmit Arora 		newext->ee_len = cpu_to_le16(len1);
154625d14f98SAmit Arora 		ret = 1;
154725d14f98SAmit Arora 	}
154825d14f98SAmit Arora 
154925d14f98SAmit Arora 	/* check for overlap */
155025d14f98SAmit Arora 	if (b1 + len1 > b2) {
155125d14f98SAmit Arora 		newext->ee_len = cpu_to_le16(b2 - b1);
155225d14f98SAmit Arora 		ret = 1;
155325d14f98SAmit Arora 	}
155425d14f98SAmit Arora out:
155525d14f98SAmit Arora 	return ret;
155625d14f98SAmit Arora }
155725d14f98SAmit Arora 
155825d14f98SAmit Arora /*
1559d0d856e8SRandy Dunlap  * ext4_ext_insert_extent:
1560d0d856e8SRandy Dunlap  * tries to merge requsted extent into the existing extent or
1561d0d856e8SRandy Dunlap  * inserts requested extent as new one into the tree,
1562d0d856e8SRandy Dunlap  * creating new leaf in the no-space case.
1563a86c6181SAlex Tomas  */
1564a86c6181SAlex Tomas int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1565a86c6181SAlex Tomas 				struct ext4_ext_path *path,
1566a86c6181SAlex Tomas 				struct ext4_extent *newext)
1567a86c6181SAlex Tomas {
1568a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
1569a86c6181SAlex Tomas 	struct ext4_extent *ex, *fex;
1570a86c6181SAlex Tomas 	struct ext4_extent *nearex; /* nearest extent */
1571a86c6181SAlex Tomas 	struct ext4_ext_path *npath = NULL;
1572725d26d3SAneesh Kumar K.V 	int depth, len, err;
1573725d26d3SAneesh Kumar K.V 	ext4_lblk_t next;
1574a2df2a63SAmit Arora 	unsigned uninitialized = 0;
1575a86c6181SAlex Tomas 
1576a2df2a63SAmit Arora 	BUG_ON(ext4_ext_get_actual_len(newext) == 0);
1577a86c6181SAlex Tomas 	depth = ext_depth(inode);
1578a86c6181SAlex Tomas 	ex = path[depth].p_ext;
1579a86c6181SAlex Tomas 	BUG_ON(path[depth].p_hdr == NULL);
1580a86c6181SAlex Tomas 
1581a86c6181SAlex Tomas 	/* try to insert block into found extent and return */
1582a86c6181SAlex Tomas 	if (ex && ext4_can_extents_be_merged(inode, ex, newext)) {
15832ae02107SMingming Cao 		ext_debug("append %d block to %d:%d (from %llu)\n",
1584a2df2a63SAmit Arora 				ext4_ext_get_actual_len(newext),
1585a86c6181SAlex Tomas 				le32_to_cpu(ex->ee_block),
1586a2df2a63SAmit Arora 				ext4_ext_get_actual_len(ex), ext_pblock(ex));
15877e028976SAvantika Mathur 		err = ext4_ext_get_access(handle, inode, path + depth);
15887e028976SAvantika Mathur 		if (err)
1589a86c6181SAlex Tomas 			return err;
1590a2df2a63SAmit Arora 
1591a2df2a63SAmit Arora 		/*
1592a2df2a63SAmit Arora 		 * ext4_can_extents_be_merged should have checked that either
1593a2df2a63SAmit Arora 		 * both extents are uninitialized, or both aren't. Thus we
1594a2df2a63SAmit Arora 		 * need to check only one of them here.
1595a2df2a63SAmit Arora 		 */
1596a2df2a63SAmit Arora 		if (ext4_ext_is_uninitialized(ex))
1597a2df2a63SAmit Arora 			uninitialized = 1;
1598a2df2a63SAmit Arora 		ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1599a2df2a63SAmit Arora 					+ ext4_ext_get_actual_len(newext));
1600a2df2a63SAmit Arora 		if (uninitialized)
1601a2df2a63SAmit Arora 			ext4_ext_mark_uninitialized(ex);
1602a86c6181SAlex Tomas 		eh = path[depth].p_hdr;
1603a86c6181SAlex Tomas 		nearex = ex;
1604a86c6181SAlex Tomas 		goto merge;
1605a86c6181SAlex Tomas 	}
1606a86c6181SAlex Tomas 
1607a86c6181SAlex Tomas repeat:
1608a86c6181SAlex Tomas 	depth = ext_depth(inode);
1609a86c6181SAlex Tomas 	eh = path[depth].p_hdr;
1610a86c6181SAlex Tomas 	if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1611a86c6181SAlex Tomas 		goto has_space;
1612a86c6181SAlex Tomas 
1613a86c6181SAlex Tomas 	/* probably next leaf has space for us? */
1614a86c6181SAlex Tomas 	fex = EXT_LAST_EXTENT(eh);
1615a86c6181SAlex Tomas 	next = ext4_ext_next_leaf_block(inode, path);
1616a86c6181SAlex Tomas 	if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
1617a86c6181SAlex Tomas 	    && next != EXT_MAX_BLOCK) {
1618a86c6181SAlex Tomas 		ext_debug("next leaf block - %d\n", next);
1619a86c6181SAlex Tomas 		BUG_ON(npath != NULL);
1620a86c6181SAlex Tomas 		npath = ext4_ext_find_extent(inode, next, NULL);
1621a86c6181SAlex Tomas 		if (IS_ERR(npath))
1622a86c6181SAlex Tomas 			return PTR_ERR(npath);
1623a86c6181SAlex Tomas 		BUG_ON(npath->p_depth != path->p_depth);
1624a86c6181SAlex Tomas 		eh = npath[depth].p_hdr;
1625a86c6181SAlex Tomas 		if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
1626a86c6181SAlex Tomas 			ext_debug("next leaf isnt full(%d)\n",
1627a86c6181SAlex Tomas 				  le16_to_cpu(eh->eh_entries));
1628a86c6181SAlex Tomas 			path = npath;
1629a86c6181SAlex Tomas 			goto repeat;
1630a86c6181SAlex Tomas 		}
1631a86c6181SAlex Tomas 		ext_debug("next leaf has no free space(%d,%d)\n",
1632a86c6181SAlex Tomas 			  le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1633a86c6181SAlex Tomas 	}
1634a86c6181SAlex Tomas 
1635a86c6181SAlex Tomas 	/*
1636d0d856e8SRandy Dunlap 	 * There is no free space in the found leaf.
1637d0d856e8SRandy Dunlap 	 * We're gonna add a new leaf in the tree.
1638a86c6181SAlex Tomas 	 */
1639a86c6181SAlex Tomas 	err = ext4_ext_create_new_leaf(handle, inode, path, newext);
1640a86c6181SAlex Tomas 	if (err)
1641a86c6181SAlex Tomas 		goto cleanup;
1642a86c6181SAlex Tomas 	depth = ext_depth(inode);
1643a86c6181SAlex Tomas 	eh = path[depth].p_hdr;
1644a86c6181SAlex Tomas 
1645a86c6181SAlex Tomas has_space:
1646a86c6181SAlex Tomas 	nearex = path[depth].p_ext;
1647a86c6181SAlex Tomas 
16487e028976SAvantika Mathur 	err = ext4_ext_get_access(handle, inode, path + depth);
16497e028976SAvantika Mathur 	if (err)
1650a86c6181SAlex Tomas 		goto cleanup;
1651a86c6181SAlex Tomas 
1652a86c6181SAlex Tomas 	if (!nearex) {
1653a86c6181SAlex Tomas 		/* there is no extent in this leaf, create first one */
16542ae02107SMingming Cao 		ext_debug("first extent in the leaf: %d:%llu:%d\n",
1655a86c6181SAlex Tomas 				le32_to_cpu(newext->ee_block),
1656f65e6fbaSAlex Tomas 				ext_pblock(newext),
1657a2df2a63SAmit Arora 				ext4_ext_get_actual_len(newext));
1658a86c6181SAlex Tomas 		path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1659a86c6181SAlex Tomas 	} else if (le32_to_cpu(newext->ee_block)
1660a86c6181SAlex Tomas 			   > le32_to_cpu(nearex->ee_block)) {
1661a86c6181SAlex Tomas /*		BUG_ON(newext->ee_block == nearex->ee_block); */
1662a86c6181SAlex Tomas 		if (nearex != EXT_LAST_EXTENT(eh)) {
1663a86c6181SAlex Tomas 			len = EXT_MAX_EXTENT(eh) - nearex;
1664a86c6181SAlex Tomas 			len = (len - 1) * sizeof(struct ext4_extent);
1665a86c6181SAlex Tomas 			len = len < 0 ? 0 : len;
16662ae02107SMingming Cao 			ext_debug("insert %d:%llu:%d after: nearest 0x%p, "
1667a86c6181SAlex Tomas 					"move %d from 0x%p to 0x%p\n",
1668a86c6181SAlex Tomas 					le32_to_cpu(newext->ee_block),
1669f65e6fbaSAlex Tomas 					ext_pblock(newext),
1670a2df2a63SAmit Arora 					ext4_ext_get_actual_len(newext),
1671a86c6181SAlex Tomas 					nearex, len, nearex + 1, nearex + 2);
1672a86c6181SAlex Tomas 			memmove(nearex + 2, nearex + 1, len);
1673a86c6181SAlex Tomas 		}
1674a86c6181SAlex Tomas 		path[depth].p_ext = nearex + 1;
1675a86c6181SAlex Tomas 	} else {
1676a86c6181SAlex Tomas 		BUG_ON(newext->ee_block == nearex->ee_block);
1677a86c6181SAlex Tomas 		len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1678a86c6181SAlex Tomas 		len = len < 0 ? 0 : len;
16792ae02107SMingming Cao 		ext_debug("insert %d:%llu:%d before: nearest 0x%p, "
1680a86c6181SAlex Tomas 				"move %d from 0x%p to 0x%p\n",
1681a86c6181SAlex Tomas 				le32_to_cpu(newext->ee_block),
1682f65e6fbaSAlex Tomas 				ext_pblock(newext),
1683a2df2a63SAmit Arora 				ext4_ext_get_actual_len(newext),
1684a86c6181SAlex Tomas 				nearex, len, nearex + 1, nearex + 2);
1685a86c6181SAlex Tomas 		memmove(nearex + 1, nearex, len);
1686a86c6181SAlex Tomas 		path[depth].p_ext = nearex;
1687a86c6181SAlex Tomas 	}
1688a86c6181SAlex Tomas 
1689e8546d06SMarcin Slusarz 	le16_add_cpu(&eh->eh_entries, 1);
1690a86c6181SAlex Tomas 	nearex = path[depth].p_ext;
1691a86c6181SAlex Tomas 	nearex->ee_block = newext->ee_block;
1692b377611dSAneesh Kumar K.V 	ext4_ext_store_pblock(nearex, ext_pblock(newext));
1693a86c6181SAlex Tomas 	nearex->ee_len = newext->ee_len;
1694a86c6181SAlex Tomas 
1695a86c6181SAlex Tomas merge:
1696a86c6181SAlex Tomas 	/* try to merge extents to the right */
169756055d3aSAmit Arora 	ext4_ext_try_to_merge(inode, path, nearex);
1698a86c6181SAlex Tomas 
1699a86c6181SAlex Tomas 	/* try to merge extents to the left */
1700a86c6181SAlex Tomas 
1701a86c6181SAlex Tomas 	/* time to correct all indexes above */
1702a86c6181SAlex Tomas 	err = ext4_ext_correct_indexes(handle, inode, path);
1703a86c6181SAlex Tomas 	if (err)
1704a86c6181SAlex Tomas 		goto cleanup;
1705a86c6181SAlex Tomas 
1706a86c6181SAlex Tomas 	err = ext4_ext_dirty(handle, inode, path + depth);
1707a86c6181SAlex Tomas 
1708a86c6181SAlex Tomas cleanup:
1709a86c6181SAlex Tomas 	if (npath) {
1710a86c6181SAlex Tomas 		ext4_ext_drop_refs(npath);
1711a86c6181SAlex Tomas 		kfree(npath);
1712a86c6181SAlex Tomas 	}
1713a86c6181SAlex Tomas 	ext4_ext_invalidate_cache(inode);
1714a86c6181SAlex Tomas 	return err;
1715a86c6181SAlex Tomas }
1716a86c6181SAlex Tomas 
17176873fa0dSEric Sandeen int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block,
17186873fa0dSEric Sandeen 			ext4_lblk_t num, ext_prepare_callback func,
17196873fa0dSEric Sandeen 			void *cbdata)
17206873fa0dSEric Sandeen {
17216873fa0dSEric Sandeen 	struct ext4_ext_path *path = NULL;
17226873fa0dSEric Sandeen 	struct ext4_ext_cache cbex;
17236873fa0dSEric Sandeen 	struct ext4_extent *ex;
17246873fa0dSEric Sandeen 	ext4_lblk_t next, start = 0, end = 0;
17256873fa0dSEric Sandeen 	ext4_lblk_t last = block + num;
17266873fa0dSEric Sandeen 	int depth, exists, err = 0;
17276873fa0dSEric Sandeen 
17286873fa0dSEric Sandeen 	BUG_ON(func == NULL);
17296873fa0dSEric Sandeen 	BUG_ON(inode == NULL);
17306873fa0dSEric Sandeen 
17316873fa0dSEric Sandeen 	while (block < last && block != EXT_MAX_BLOCK) {
17326873fa0dSEric Sandeen 		num = last - block;
17336873fa0dSEric Sandeen 		/* find extent for this block */
17346873fa0dSEric Sandeen 		path = ext4_ext_find_extent(inode, block, path);
17356873fa0dSEric Sandeen 		if (IS_ERR(path)) {
17366873fa0dSEric Sandeen 			err = PTR_ERR(path);
17376873fa0dSEric Sandeen 			path = NULL;
17386873fa0dSEric Sandeen 			break;
17396873fa0dSEric Sandeen 		}
17406873fa0dSEric Sandeen 
17416873fa0dSEric Sandeen 		depth = ext_depth(inode);
17426873fa0dSEric Sandeen 		BUG_ON(path[depth].p_hdr == NULL);
17436873fa0dSEric Sandeen 		ex = path[depth].p_ext;
17446873fa0dSEric Sandeen 		next = ext4_ext_next_allocated_block(path);
17456873fa0dSEric Sandeen 
17466873fa0dSEric Sandeen 		exists = 0;
17476873fa0dSEric Sandeen 		if (!ex) {
17486873fa0dSEric Sandeen 			/* there is no extent yet, so try to allocate
17496873fa0dSEric Sandeen 			 * all requested space */
17506873fa0dSEric Sandeen 			start = block;
17516873fa0dSEric Sandeen 			end = block + num;
17526873fa0dSEric Sandeen 		} else if (le32_to_cpu(ex->ee_block) > block) {
17536873fa0dSEric Sandeen 			/* need to allocate space before found extent */
17546873fa0dSEric Sandeen 			start = block;
17556873fa0dSEric Sandeen 			end = le32_to_cpu(ex->ee_block);
17566873fa0dSEric Sandeen 			if (block + num < end)
17576873fa0dSEric Sandeen 				end = block + num;
17586873fa0dSEric Sandeen 		} else if (block >= le32_to_cpu(ex->ee_block)
17596873fa0dSEric Sandeen 					+ ext4_ext_get_actual_len(ex)) {
17606873fa0dSEric Sandeen 			/* need to allocate space after found extent */
17616873fa0dSEric Sandeen 			start = block;
17626873fa0dSEric Sandeen 			end = block + num;
17636873fa0dSEric Sandeen 			if (end >= next)
17646873fa0dSEric Sandeen 				end = next;
17656873fa0dSEric Sandeen 		} else if (block >= le32_to_cpu(ex->ee_block)) {
17666873fa0dSEric Sandeen 			/*
17676873fa0dSEric Sandeen 			 * some part of requested space is covered
17686873fa0dSEric Sandeen 			 * by found extent
17696873fa0dSEric Sandeen 			 */
17706873fa0dSEric Sandeen 			start = block;
17716873fa0dSEric Sandeen 			end = le32_to_cpu(ex->ee_block)
17726873fa0dSEric Sandeen 				+ ext4_ext_get_actual_len(ex);
17736873fa0dSEric Sandeen 			if (block + num < end)
17746873fa0dSEric Sandeen 				end = block + num;
17756873fa0dSEric Sandeen 			exists = 1;
17766873fa0dSEric Sandeen 		} else {
17776873fa0dSEric Sandeen 			BUG();
17786873fa0dSEric Sandeen 		}
17796873fa0dSEric Sandeen 		BUG_ON(end <= start);
17806873fa0dSEric Sandeen 
17816873fa0dSEric Sandeen 		if (!exists) {
17826873fa0dSEric Sandeen 			cbex.ec_block = start;
17836873fa0dSEric Sandeen 			cbex.ec_len = end - start;
17846873fa0dSEric Sandeen 			cbex.ec_start = 0;
17856873fa0dSEric Sandeen 			cbex.ec_type = EXT4_EXT_CACHE_GAP;
17866873fa0dSEric Sandeen 		} else {
17876873fa0dSEric Sandeen 			cbex.ec_block = le32_to_cpu(ex->ee_block);
17886873fa0dSEric Sandeen 			cbex.ec_len = ext4_ext_get_actual_len(ex);
17896873fa0dSEric Sandeen 			cbex.ec_start = ext_pblock(ex);
17906873fa0dSEric Sandeen 			cbex.ec_type = EXT4_EXT_CACHE_EXTENT;
17916873fa0dSEric Sandeen 		}
17926873fa0dSEric Sandeen 
17936873fa0dSEric Sandeen 		BUG_ON(cbex.ec_len == 0);
17946873fa0dSEric Sandeen 		err = func(inode, path, &cbex, ex, cbdata);
17956873fa0dSEric Sandeen 		ext4_ext_drop_refs(path);
17966873fa0dSEric Sandeen 
17976873fa0dSEric Sandeen 		if (err < 0)
17986873fa0dSEric Sandeen 			break;
17996873fa0dSEric Sandeen 
18006873fa0dSEric Sandeen 		if (err == EXT_REPEAT)
18016873fa0dSEric Sandeen 			continue;
18026873fa0dSEric Sandeen 		else if (err == EXT_BREAK) {
18036873fa0dSEric Sandeen 			err = 0;
18046873fa0dSEric Sandeen 			break;
18056873fa0dSEric Sandeen 		}
18066873fa0dSEric Sandeen 
18076873fa0dSEric Sandeen 		if (ext_depth(inode) != depth) {
18086873fa0dSEric Sandeen 			/* depth was changed. we have to realloc path */
18096873fa0dSEric Sandeen 			kfree(path);
18106873fa0dSEric Sandeen 			path = NULL;
18116873fa0dSEric Sandeen 		}
18126873fa0dSEric Sandeen 
18136873fa0dSEric Sandeen 		block = cbex.ec_block + cbex.ec_len;
18146873fa0dSEric Sandeen 	}
18156873fa0dSEric Sandeen 
18166873fa0dSEric Sandeen 	if (path) {
18176873fa0dSEric Sandeen 		ext4_ext_drop_refs(path);
18186873fa0dSEric Sandeen 		kfree(path);
18196873fa0dSEric Sandeen 	}
18206873fa0dSEric Sandeen 
18216873fa0dSEric Sandeen 	return err;
18226873fa0dSEric Sandeen }
18236873fa0dSEric Sandeen 
182409b88252SAvantika Mathur static void
1825725d26d3SAneesh Kumar K.V ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
1826dd54567aSMingming Cao 			__u32 len, ext4_fsblk_t start, int type)
1827a86c6181SAlex Tomas {
1828a86c6181SAlex Tomas 	struct ext4_ext_cache *cex;
1829a86c6181SAlex Tomas 	BUG_ON(len == 0);
18302ec0ae3aSTheodore Ts'o 	spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1831a86c6181SAlex Tomas 	cex = &EXT4_I(inode)->i_cached_extent;
1832a86c6181SAlex Tomas 	cex->ec_type = type;
1833a86c6181SAlex Tomas 	cex->ec_block = block;
1834a86c6181SAlex Tomas 	cex->ec_len = len;
1835a86c6181SAlex Tomas 	cex->ec_start = start;
18362ec0ae3aSTheodore Ts'o 	spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1837a86c6181SAlex Tomas }
1838a86c6181SAlex Tomas 
1839a86c6181SAlex Tomas /*
1840d0d856e8SRandy Dunlap  * ext4_ext_put_gap_in_cache:
1841d0d856e8SRandy Dunlap  * calculate boundaries of the gap that the requested block fits into
1842a86c6181SAlex Tomas  * and cache this gap
1843a86c6181SAlex Tomas  */
184409b88252SAvantika Mathur static void
1845a86c6181SAlex Tomas ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
1846725d26d3SAneesh Kumar K.V 				ext4_lblk_t block)
1847a86c6181SAlex Tomas {
1848a86c6181SAlex Tomas 	int depth = ext_depth(inode);
1849725d26d3SAneesh Kumar K.V 	unsigned long len;
1850725d26d3SAneesh Kumar K.V 	ext4_lblk_t lblock;
1851a86c6181SAlex Tomas 	struct ext4_extent *ex;
1852a86c6181SAlex Tomas 
1853a86c6181SAlex Tomas 	ex = path[depth].p_ext;
1854a86c6181SAlex Tomas 	if (ex == NULL) {
1855a86c6181SAlex Tomas 		/* there is no extent yet, so gap is [0;-] */
1856a86c6181SAlex Tomas 		lblock = 0;
1857a86c6181SAlex Tomas 		len = EXT_MAX_BLOCK;
1858a86c6181SAlex Tomas 		ext_debug("cache gap(whole file):");
1859a86c6181SAlex Tomas 	} else if (block < le32_to_cpu(ex->ee_block)) {
1860a86c6181SAlex Tomas 		lblock = block;
1861a86c6181SAlex Tomas 		len = le32_to_cpu(ex->ee_block) - block;
1862bba90743SEric Sandeen 		ext_debug("cache gap(before): %u [%u:%u]",
1863bba90743SEric Sandeen 				block,
1864bba90743SEric Sandeen 				le32_to_cpu(ex->ee_block),
1865bba90743SEric Sandeen 				 ext4_ext_get_actual_len(ex));
1866a86c6181SAlex Tomas 	} else if (block >= le32_to_cpu(ex->ee_block)
1867a2df2a63SAmit Arora 			+ ext4_ext_get_actual_len(ex)) {
1868725d26d3SAneesh Kumar K.V 		ext4_lblk_t next;
1869a86c6181SAlex Tomas 		lblock = le32_to_cpu(ex->ee_block)
1870a2df2a63SAmit Arora 			+ ext4_ext_get_actual_len(ex);
1871725d26d3SAneesh Kumar K.V 
1872725d26d3SAneesh Kumar K.V 		next = ext4_ext_next_allocated_block(path);
1873bba90743SEric Sandeen 		ext_debug("cache gap(after): [%u:%u] %u",
1874bba90743SEric Sandeen 				le32_to_cpu(ex->ee_block),
1875bba90743SEric Sandeen 				ext4_ext_get_actual_len(ex),
1876bba90743SEric Sandeen 				block);
1877725d26d3SAneesh Kumar K.V 		BUG_ON(next == lblock);
1878725d26d3SAneesh Kumar K.V 		len = next - lblock;
1879a86c6181SAlex Tomas 	} else {
1880a86c6181SAlex Tomas 		lblock = len = 0;
1881a86c6181SAlex Tomas 		BUG();
1882a86c6181SAlex Tomas 	}
1883a86c6181SAlex Tomas 
1884bba90743SEric Sandeen 	ext_debug(" -> %u:%lu\n", lblock, len);
1885a86c6181SAlex Tomas 	ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
1886a86c6181SAlex Tomas }
1887a86c6181SAlex Tomas 
188809b88252SAvantika Mathur static int
1889725d26d3SAneesh Kumar K.V ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
1890a86c6181SAlex Tomas 			struct ext4_extent *ex)
1891a86c6181SAlex Tomas {
1892a86c6181SAlex Tomas 	struct ext4_ext_cache *cex;
18932ec0ae3aSTheodore Ts'o 	int ret = EXT4_EXT_CACHE_NO;
1894a86c6181SAlex Tomas 
18952ec0ae3aSTheodore Ts'o 	/*
18962ec0ae3aSTheodore Ts'o 	 * We borrow i_block_reservation_lock to protect i_cached_extent
18972ec0ae3aSTheodore Ts'o 	 */
18982ec0ae3aSTheodore Ts'o 	spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1899a86c6181SAlex Tomas 	cex = &EXT4_I(inode)->i_cached_extent;
1900a86c6181SAlex Tomas 
1901a86c6181SAlex Tomas 	/* has cache valid data? */
1902a86c6181SAlex Tomas 	if (cex->ec_type == EXT4_EXT_CACHE_NO)
19032ec0ae3aSTheodore Ts'o 		goto errout;
1904a86c6181SAlex Tomas 
1905a86c6181SAlex Tomas 	BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
1906a86c6181SAlex Tomas 			cex->ec_type != EXT4_EXT_CACHE_EXTENT);
1907a86c6181SAlex Tomas 	if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
1908a86c6181SAlex Tomas 		ex->ee_block = cpu_to_le32(cex->ec_block);
1909f65e6fbaSAlex Tomas 		ext4_ext_store_pblock(ex, cex->ec_start);
1910a86c6181SAlex Tomas 		ex->ee_len = cpu_to_le16(cex->ec_len);
1911bba90743SEric Sandeen 		ext_debug("%u cached by %u:%u:%llu\n",
1912bba90743SEric Sandeen 				block,
1913bba90743SEric Sandeen 				cex->ec_block, cex->ec_len, cex->ec_start);
19142ec0ae3aSTheodore Ts'o 		ret = cex->ec_type;
1915a86c6181SAlex Tomas 	}
19162ec0ae3aSTheodore Ts'o errout:
19172ec0ae3aSTheodore Ts'o 	spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
19182ec0ae3aSTheodore Ts'o 	return ret;
1919a86c6181SAlex Tomas }
1920a86c6181SAlex Tomas 
1921a86c6181SAlex Tomas /*
1922d0d856e8SRandy Dunlap  * ext4_ext_rm_idx:
1923d0d856e8SRandy Dunlap  * removes index from the index block.
1924d0d856e8SRandy Dunlap  * It's used in truncate case only, thus all requests are for
1925d0d856e8SRandy Dunlap  * last index in the block only.
1926a86c6181SAlex Tomas  */
19271d03ec98SAneesh Kumar K.V static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
1928a86c6181SAlex Tomas 			struct ext4_ext_path *path)
1929a86c6181SAlex Tomas {
1930a86c6181SAlex Tomas 	struct buffer_head *bh;
1931a86c6181SAlex Tomas 	int err;
1932f65e6fbaSAlex Tomas 	ext4_fsblk_t leaf;
1933a86c6181SAlex Tomas 
1934a86c6181SAlex Tomas 	/* free index block */
1935a86c6181SAlex Tomas 	path--;
1936f65e6fbaSAlex Tomas 	leaf = idx_pblock(path->p_idx);
1937a86c6181SAlex Tomas 	BUG_ON(path->p_hdr->eh_entries == 0);
19387e028976SAvantika Mathur 	err = ext4_ext_get_access(handle, inode, path);
19397e028976SAvantika Mathur 	if (err)
1940a86c6181SAlex Tomas 		return err;
1941e8546d06SMarcin Slusarz 	le16_add_cpu(&path->p_hdr->eh_entries, -1);
19427e028976SAvantika Mathur 	err = ext4_ext_dirty(handle, inode, path);
19437e028976SAvantika Mathur 	if (err)
1944a86c6181SAlex Tomas 		return err;
19452ae02107SMingming Cao 	ext_debug("index is empty, remove it, free block %llu\n", leaf);
1946a86c6181SAlex Tomas 	bh = sb_find_get_block(inode->i_sb, leaf);
1947a86c6181SAlex Tomas 	ext4_forget(handle, 1, inode, bh, leaf);
1948c9de560dSAlex Tomas 	ext4_free_blocks(handle, inode, leaf, 1, 1);
1949a86c6181SAlex Tomas 	return err;
1950a86c6181SAlex Tomas }
1951a86c6181SAlex Tomas 
1952a86c6181SAlex Tomas /*
1953ee12b630SMingming Cao  * ext4_ext_calc_credits_for_single_extent:
1954ee12b630SMingming Cao  * This routine returns max. credits that needed to insert an extent
1955ee12b630SMingming Cao  * to the extent tree.
1956ee12b630SMingming Cao  * When pass the actual path, the caller should calculate credits
1957ee12b630SMingming Cao  * under i_data_sem.
1958a86c6181SAlex Tomas  */
1959525f4ed8SMingming Cao int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
1960a86c6181SAlex Tomas 						struct ext4_ext_path *path)
1961a86c6181SAlex Tomas {
1962a86c6181SAlex Tomas 	if (path) {
1963ee12b630SMingming Cao 		int depth = ext_depth(inode);
1964f3bd1f3fSMingming Cao 		int ret = 0;
1965ee12b630SMingming Cao 
1966a86c6181SAlex Tomas 		/* probably there is space in leaf? */
1967a86c6181SAlex Tomas 		if (le16_to_cpu(path[depth].p_hdr->eh_entries)
1968ee12b630SMingming Cao 				< le16_to_cpu(path[depth].p_hdr->eh_max)) {
1969ee12b630SMingming Cao 
1970ee12b630SMingming Cao 			/*
1971ee12b630SMingming Cao 			 *  There are some space in the leaf tree, no
1972ee12b630SMingming Cao 			 *  need to account for leaf block credit
1973ee12b630SMingming Cao 			 *
1974ee12b630SMingming Cao 			 *  bitmaps and block group descriptor blocks
1975ee12b630SMingming Cao 			 *  and other metadat blocks still need to be
1976ee12b630SMingming Cao 			 *  accounted.
1977ee12b630SMingming Cao 			 */
1978525f4ed8SMingming Cao 			/* 1 bitmap, 1 block group descriptor */
1979ee12b630SMingming Cao 			ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
1980ee12b630SMingming Cao 		}
1981ee12b630SMingming Cao 	}
1982ee12b630SMingming Cao 
1983525f4ed8SMingming Cao 	return ext4_chunk_trans_blocks(inode, nrblocks);
1984a86c6181SAlex Tomas }
1985a86c6181SAlex Tomas 
1986a86c6181SAlex Tomas /*
1987ee12b630SMingming Cao  * How many index/leaf blocks need to change/allocate to modify nrblocks?
1988ee12b630SMingming Cao  *
1989ee12b630SMingming Cao  * if nrblocks are fit in a single extent (chunk flag is 1), then
1990ee12b630SMingming Cao  * in the worse case, each tree level index/leaf need to be changed
1991ee12b630SMingming Cao  * if the tree split due to insert a new extent, then the old tree
1992ee12b630SMingming Cao  * index/leaf need to be updated too
1993ee12b630SMingming Cao  *
1994ee12b630SMingming Cao  * If the nrblocks are discontiguous, they could cause
1995ee12b630SMingming Cao  * the whole tree split more than once, but this is really rare.
1996a86c6181SAlex Tomas  */
1997525f4ed8SMingming Cao int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
1998ee12b630SMingming Cao {
1999ee12b630SMingming Cao 	int index;
2000ee12b630SMingming Cao 	int depth = ext_depth(inode);
2001a86c6181SAlex Tomas 
2002ee12b630SMingming Cao 	if (chunk)
2003ee12b630SMingming Cao 		index = depth * 2;
2004ee12b630SMingming Cao 	else
2005ee12b630SMingming Cao 		index = depth * 3;
2006a86c6181SAlex Tomas 
2007ee12b630SMingming Cao 	return index;
2008a86c6181SAlex Tomas }
2009a86c6181SAlex Tomas 
2010a86c6181SAlex Tomas static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
2011a86c6181SAlex Tomas 				struct ext4_extent *ex,
2012725d26d3SAneesh Kumar K.V 				ext4_lblk_t from, ext4_lblk_t to)
2013a86c6181SAlex Tomas {
2014a86c6181SAlex Tomas 	struct buffer_head *bh;
2015a2df2a63SAmit Arora 	unsigned short ee_len =  ext4_ext_get_actual_len(ex);
2016c9de560dSAlex Tomas 	int i, metadata = 0;
2017a86c6181SAlex Tomas 
2018c9de560dSAlex Tomas 	if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2019c9de560dSAlex Tomas 		metadata = 1;
2020a86c6181SAlex Tomas #ifdef EXTENTS_STATS
2021a86c6181SAlex Tomas 	{
2022a86c6181SAlex Tomas 		struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2023a86c6181SAlex Tomas 		spin_lock(&sbi->s_ext_stats_lock);
2024a86c6181SAlex Tomas 		sbi->s_ext_blocks += ee_len;
2025a86c6181SAlex Tomas 		sbi->s_ext_extents++;
2026a86c6181SAlex Tomas 		if (ee_len < sbi->s_ext_min)
2027a86c6181SAlex Tomas 			sbi->s_ext_min = ee_len;
2028a86c6181SAlex Tomas 		if (ee_len > sbi->s_ext_max)
2029a86c6181SAlex Tomas 			sbi->s_ext_max = ee_len;
2030a86c6181SAlex Tomas 		if (ext_depth(inode) > sbi->s_depth_max)
2031a86c6181SAlex Tomas 			sbi->s_depth_max = ext_depth(inode);
2032a86c6181SAlex Tomas 		spin_unlock(&sbi->s_ext_stats_lock);
2033a86c6181SAlex Tomas 	}
2034a86c6181SAlex Tomas #endif
2035a86c6181SAlex Tomas 	if (from >= le32_to_cpu(ex->ee_block)
2036a2df2a63SAmit Arora 	    && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
2037a86c6181SAlex Tomas 		/* tail removal */
2038725d26d3SAneesh Kumar K.V 		ext4_lblk_t num;
2039f65e6fbaSAlex Tomas 		ext4_fsblk_t start;
2040725d26d3SAneesh Kumar K.V 
2041a2df2a63SAmit Arora 		num = le32_to_cpu(ex->ee_block) + ee_len - from;
2042a2df2a63SAmit Arora 		start = ext_pblock(ex) + ee_len - num;
2043725d26d3SAneesh Kumar K.V 		ext_debug("free last %u blocks starting %llu\n", num, start);
2044a86c6181SAlex Tomas 		for (i = 0; i < num; i++) {
2045a86c6181SAlex Tomas 			bh = sb_find_get_block(inode->i_sb, start + i);
2046a86c6181SAlex Tomas 			ext4_forget(handle, 0, inode, bh, start + i);
2047a86c6181SAlex Tomas 		}
2048c9de560dSAlex Tomas 		ext4_free_blocks(handle, inode, start, num, metadata);
2049a86c6181SAlex Tomas 	} else if (from == le32_to_cpu(ex->ee_block)
2050a2df2a63SAmit Arora 		   && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
2051725d26d3SAneesh Kumar K.V 		printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n",
2052a2df2a63SAmit Arora 			from, to, le32_to_cpu(ex->ee_block), ee_len);
2053a86c6181SAlex Tomas 	} else {
2054725d26d3SAneesh Kumar K.V 		printk(KERN_INFO "strange request: removal(2) "
2055725d26d3SAneesh Kumar K.V 				"%u-%u from %u:%u\n",
2056a2df2a63SAmit Arora 				from, to, le32_to_cpu(ex->ee_block), ee_len);
2057a86c6181SAlex Tomas 	}
2058a86c6181SAlex Tomas 	return 0;
2059a86c6181SAlex Tomas }
2060a86c6181SAlex Tomas 
2061a86c6181SAlex Tomas static int
2062a86c6181SAlex Tomas ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
2063725d26d3SAneesh Kumar K.V 		struct ext4_ext_path *path, ext4_lblk_t start)
2064a86c6181SAlex Tomas {
2065a86c6181SAlex Tomas 	int err = 0, correct_index = 0;
2066a86c6181SAlex Tomas 	int depth = ext_depth(inode), credits;
2067a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
2068725d26d3SAneesh Kumar K.V 	ext4_lblk_t a, b, block;
2069725d26d3SAneesh Kumar K.V 	unsigned num;
2070725d26d3SAneesh Kumar K.V 	ext4_lblk_t ex_ee_block;
2071a86c6181SAlex Tomas 	unsigned short ex_ee_len;
2072a2df2a63SAmit Arora 	unsigned uninitialized = 0;
2073a86c6181SAlex Tomas 	struct ext4_extent *ex;
2074a86c6181SAlex Tomas 
2075c29c0ae7SAlex Tomas 	/* the header must be checked already in ext4_ext_remove_space() */
2076725d26d3SAneesh Kumar K.V 	ext_debug("truncate since %u in leaf\n", start);
2077a86c6181SAlex Tomas 	if (!path[depth].p_hdr)
2078a86c6181SAlex Tomas 		path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2079a86c6181SAlex Tomas 	eh = path[depth].p_hdr;
2080a86c6181SAlex Tomas 	BUG_ON(eh == NULL);
2081a86c6181SAlex Tomas 
2082a86c6181SAlex Tomas 	/* find where to start removing */
2083a86c6181SAlex Tomas 	ex = EXT_LAST_EXTENT(eh);
2084a86c6181SAlex Tomas 
2085a86c6181SAlex Tomas 	ex_ee_block = le32_to_cpu(ex->ee_block);
2086a2df2a63SAmit Arora 	ex_ee_len = ext4_ext_get_actual_len(ex);
2087a86c6181SAlex Tomas 
2088a86c6181SAlex Tomas 	while (ex >= EXT_FIRST_EXTENT(eh) &&
2089a86c6181SAlex Tomas 			ex_ee_block + ex_ee_len > start) {
2090a41f2071SAneesh Kumar K.V 
2091a41f2071SAneesh Kumar K.V 		if (ext4_ext_is_uninitialized(ex))
2092a41f2071SAneesh Kumar K.V 			uninitialized = 1;
2093a41f2071SAneesh Kumar K.V 		else
2094a41f2071SAneesh Kumar K.V 			uninitialized = 0;
2095a41f2071SAneesh Kumar K.V 
2096a86c6181SAlex Tomas 		ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len);
2097a86c6181SAlex Tomas 		path[depth].p_ext = ex;
2098a86c6181SAlex Tomas 
2099a86c6181SAlex Tomas 		a = ex_ee_block > start ? ex_ee_block : start;
2100a86c6181SAlex Tomas 		b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
2101a86c6181SAlex Tomas 			ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
2102a86c6181SAlex Tomas 
2103a86c6181SAlex Tomas 		ext_debug("  border %u:%u\n", a, b);
2104a86c6181SAlex Tomas 
2105a86c6181SAlex Tomas 		if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
2106a86c6181SAlex Tomas 			block = 0;
2107a86c6181SAlex Tomas 			num = 0;
2108a86c6181SAlex Tomas 			BUG();
2109a86c6181SAlex Tomas 		} else if (a != ex_ee_block) {
2110a86c6181SAlex Tomas 			/* remove tail of the extent */
2111a86c6181SAlex Tomas 			block = ex_ee_block;
2112a86c6181SAlex Tomas 			num = a - block;
2113a86c6181SAlex Tomas 		} else if (b != ex_ee_block + ex_ee_len - 1) {
2114a86c6181SAlex Tomas 			/* remove head of the extent */
2115a86c6181SAlex Tomas 			block = a;
2116a86c6181SAlex Tomas 			num = b - a;
2117a86c6181SAlex Tomas 			/* there is no "make a hole" API yet */
2118a86c6181SAlex Tomas 			BUG();
2119a86c6181SAlex Tomas 		} else {
2120a86c6181SAlex Tomas 			/* remove whole extent: excellent! */
2121a86c6181SAlex Tomas 			block = ex_ee_block;
2122a86c6181SAlex Tomas 			num = 0;
2123a86c6181SAlex Tomas 			BUG_ON(a != ex_ee_block);
2124a86c6181SAlex Tomas 			BUG_ON(b != ex_ee_block + ex_ee_len - 1);
2125a86c6181SAlex Tomas 		}
2126a86c6181SAlex Tomas 
212734071da7STheodore Ts'o 		/*
212834071da7STheodore Ts'o 		 * 3 for leaf, sb, and inode plus 2 (bmap and group
212934071da7STheodore Ts'o 		 * descriptor) for each block group; assume two block
213034071da7STheodore Ts'o 		 * groups plus ex_ee_len/blocks_per_block_group for
213134071da7STheodore Ts'o 		 * the worst case
213234071da7STheodore Ts'o 		 */
213334071da7STheodore Ts'o 		credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
2134a86c6181SAlex Tomas 		if (ex == EXT_FIRST_EXTENT(eh)) {
2135a86c6181SAlex Tomas 			correct_index = 1;
2136a86c6181SAlex Tomas 			credits += (ext_depth(inode)) + 1;
2137a86c6181SAlex Tomas 		}
2138a86c6181SAlex Tomas 		credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
2139a86c6181SAlex Tomas 
21409102e4faSShen Feng 		err = ext4_ext_journal_restart(handle, credits);
21419102e4faSShen Feng 		if (err)
2142a86c6181SAlex Tomas 			goto out;
2143a86c6181SAlex Tomas 
2144a86c6181SAlex Tomas 		err = ext4_ext_get_access(handle, inode, path + depth);
2145a86c6181SAlex Tomas 		if (err)
2146a86c6181SAlex Tomas 			goto out;
2147a86c6181SAlex Tomas 
2148a86c6181SAlex Tomas 		err = ext4_remove_blocks(handle, inode, ex, a, b);
2149a86c6181SAlex Tomas 		if (err)
2150a86c6181SAlex Tomas 			goto out;
2151a86c6181SAlex Tomas 
2152a86c6181SAlex Tomas 		if (num == 0) {
2153d0d856e8SRandy Dunlap 			/* this extent is removed; mark slot entirely unused */
2154f65e6fbaSAlex Tomas 			ext4_ext_store_pblock(ex, 0);
2155e8546d06SMarcin Slusarz 			le16_add_cpu(&eh->eh_entries, -1);
2156a86c6181SAlex Tomas 		}
2157a86c6181SAlex Tomas 
2158a86c6181SAlex Tomas 		ex->ee_block = cpu_to_le32(block);
2159a86c6181SAlex Tomas 		ex->ee_len = cpu_to_le16(num);
2160749269faSAmit Arora 		/*
2161749269faSAmit Arora 		 * Do not mark uninitialized if all the blocks in the
2162749269faSAmit Arora 		 * extent have been removed.
2163749269faSAmit Arora 		 */
2164749269faSAmit Arora 		if (uninitialized && num)
2165a2df2a63SAmit Arora 			ext4_ext_mark_uninitialized(ex);
2166a86c6181SAlex Tomas 
2167a86c6181SAlex Tomas 		err = ext4_ext_dirty(handle, inode, path + depth);
2168a86c6181SAlex Tomas 		if (err)
2169a86c6181SAlex Tomas 			goto out;
2170a86c6181SAlex Tomas 
21712ae02107SMingming Cao 		ext_debug("new extent: %u:%u:%llu\n", block, num,
2172f65e6fbaSAlex Tomas 				ext_pblock(ex));
2173a86c6181SAlex Tomas 		ex--;
2174a86c6181SAlex Tomas 		ex_ee_block = le32_to_cpu(ex->ee_block);
2175a2df2a63SAmit Arora 		ex_ee_len = ext4_ext_get_actual_len(ex);
2176a86c6181SAlex Tomas 	}
2177a86c6181SAlex Tomas 
2178a86c6181SAlex Tomas 	if (correct_index && eh->eh_entries)
2179a86c6181SAlex Tomas 		err = ext4_ext_correct_indexes(handle, inode, path);
2180a86c6181SAlex Tomas 
2181a86c6181SAlex Tomas 	/* if this leaf is free, then we should
2182a86c6181SAlex Tomas 	 * remove it from index block above */
2183a86c6181SAlex Tomas 	if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
2184a86c6181SAlex Tomas 		err = ext4_ext_rm_idx(handle, inode, path + depth);
2185a86c6181SAlex Tomas 
2186a86c6181SAlex Tomas out:
2187a86c6181SAlex Tomas 	return err;
2188a86c6181SAlex Tomas }
2189a86c6181SAlex Tomas 
2190a86c6181SAlex Tomas /*
2191d0d856e8SRandy Dunlap  * ext4_ext_more_to_rm:
2192d0d856e8SRandy Dunlap  * returns 1 if current index has to be freed (even partial)
2193a86c6181SAlex Tomas  */
219409b88252SAvantika Mathur static int
2195a86c6181SAlex Tomas ext4_ext_more_to_rm(struct ext4_ext_path *path)
2196a86c6181SAlex Tomas {
2197a86c6181SAlex Tomas 	BUG_ON(path->p_idx == NULL);
2198a86c6181SAlex Tomas 
2199a86c6181SAlex Tomas 	if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2200a86c6181SAlex Tomas 		return 0;
2201a86c6181SAlex Tomas 
2202a86c6181SAlex Tomas 	/*
2203d0d856e8SRandy Dunlap 	 * if truncate on deeper level happened, it wasn't partial,
2204a86c6181SAlex Tomas 	 * so we have to consider current index for truncation
2205a86c6181SAlex Tomas 	 */
2206a86c6181SAlex Tomas 	if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2207a86c6181SAlex Tomas 		return 0;
2208a86c6181SAlex Tomas 	return 1;
2209a86c6181SAlex Tomas }
2210a86c6181SAlex Tomas 
22111d03ec98SAneesh Kumar K.V static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
2212a86c6181SAlex Tomas {
2213a86c6181SAlex Tomas 	struct super_block *sb = inode->i_sb;
2214a86c6181SAlex Tomas 	int depth = ext_depth(inode);
2215a86c6181SAlex Tomas 	struct ext4_ext_path *path;
2216a86c6181SAlex Tomas 	handle_t *handle;
2217a86c6181SAlex Tomas 	int i = 0, err = 0;
2218a86c6181SAlex Tomas 
2219725d26d3SAneesh Kumar K.V 	ext_debug("truncate since %u\n", start);
2220a86c6181SAlex Tomas 
2221a86c6181SAlex Tomas 	/* probably first extent we're gonna free will be last in block */
2222a86c6181SAlex Tomas 	handle = ext4_journal_start(inode, depth + 1);
2223a86c6181SAlex Tomas 	if (IS_ERR(handle))
2224a86c6181SAlex Tomas 		return PTR_ERR(handle);
2225a86c6181SAlex Tomas 
2226a86c6181SAlex Tomas 	ext4_ext_invalidate_cache(inode);
2227a86c6181SAlex Tomas 
2228a86c6181SAlex Tomas 	/*
2229d0d856e8SRandy Dunlap 	 * We start scanning from right side, freeing all the blocks
2230d0d856e8SRandy Dunlap 	 * after i_size and walking into the tree depth-wise.
2231a86c6181SAlex Tomas 	 */
2232216553c4SJosef Bacik 	path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
2233a86c6181SAlex Tomas 	if (path == NULL) {
2234a86c6181SAlex Tomas 		ext4_journal_stop(handle);
2235a86c6181SAlex Tomas 		return -ENOMEM;
2236a86c6181SAlex Tomas 	}
2237a86c6181SAlex Tomas 	path[0].p_hdr = ext_inode_hdr(inode);
223856b19868SAneesh Kumar K.V 	if (ext4_ext_check(inode, path[0].p_hdr, depth)) {
2239a86c6181SAlex Tomas 		err = -EIO;
2240a86c6181SAlex Tomas 		goto out;
2241a86c6181SAlex Tomas 	}
2242a86c6181SAlex Tomas 	path[0].p_depth = depth;
2243a86c6181SAlex Tomas 
2244a86c6181SAlex Tomas 	while (i >= 0 && err == 0) {
2245a86c6181SAlex Tomas 		if (i == depth) {
2246a86c6181SAlex Tomas 			/* this is leaf block */
2247a86c6181SAlex Tomas 			err = ext4_ext_rm_leaf(handle, inode, path, start);
2248d0d856e8SRandy Dunlap 			/* root level has p_bh == NULL, brelse() eats this */
2249a86c6181SAlex Tomas 			brelse(path[i].p_bh);
2250a86c6181SAlex Tomas 			path[i].p_bh = NULL;
2251a86c6181SAlex Tomas 			i--;
2252a86c6181SAlex Tomas 			continue;
2253a86c6181SAlex Tomas 		}
2254a86c6181SAlex Tomas 
2255a86c6181SAlex Tomas 		/* this is index block */
2256a86c6181SAlex Tomas 		if (!path[i].p_hdr) {
2257a86c6181SAlex Tomas 			ext_debug("initialize header\n");
2258a86c6181SAlex Tomas 			path[i].p_hdr = ext_block_hdr(path[i].p_bh);
2259a86c6181SAlex Tomas 		}
2260a86c6181SAlex Tomas 
2261a86c6181SAlex Tomas 		if (!path[i].p_idx) {
2262d0d856e8SRandy Dunlap 			/* this level hasn't been touched yet */
2263a86c6181SAlex Tomas 			path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2264a86c6181SAlex Tomas 			path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2265a86c6181SAlex Tomas 			ext_debug("init index ptr: hdr 0x%p, num %d\n",
2266a86c6181SAlex Tomas 				  path[i].p_hdr,
2267a86c6181SAlex Tomas 				  le16_to_cpu(path[i].p_hdr->eh_entries));
2268a86c6181SAlex Tomas 		} else {
2269d0d856e8SRandy Dunlap 			/* we were already here, see at next index */
2270a86c6181SAlex Tomas 			path[i].p_idx--;
2271a86c6181SAlex Tomas 		}
2272a86c6181SAlex Tomas 
2273a86c6181SAlex Tomas 		ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2274a86c6181SAlex Tomas 				i, EXT_FIRST_INDEX(path[i].p_hdr),
2275a86c6181SAlex Tomas 				path[i].p_idx);
2276a86c6181SAlex Tomas 		if (ext4_ext_more_to_rm(path + i)) {
2277c29c0ae7SAlex Tomas 			struct buffer_head *bh;
2278a86c6181SAlex Tomas 			/* go to the next level */
22792ae02107SMingming Cao 			ext_debug("move to level %d (block %llu)\n",
2280f65e6fbaSAlex Tomas 				  i + 1, idx_pblock(path[i].p_idx));
2281a86c6181SAlex Tomas 			memset(path + i + 1, 0, sizeof(*path));
2282c29c0ae7SAlex Tomas 			bh = sb_bread(sb, idx_pblock(path[i].p_idx));
2283c29c0ae7SAlex Tomas 			if (!bh) {
2284a86c6181SAlex Tomas 				/* should we reset i_size? */
2285a86c6181SAlex Tomas 				err = -EIO;
2286a86c6181SAlex Tomas 				break;
2287a86c6181SAlex Tomas 			}
2288c29c0ae7SAlex Tomas 			if (WARN_ON(i + 1 > depth)) {
2289c29c0ae7SAlex Tomas 				err = -EIO;
2290c29c0ae7SAlex Tomas 				break;
2291c29c0ae7SAlex Tomas 			}
229256b19868SAneesh Kumar K.V 			if (ext4_ext_check(inode, ext_block_hdr(bh),
2293c29c0ae7SAlex Tomas 							depth - i - 1)) {
2294c29c0ae7SAlex Tomas 				err = -EIO;
2295c29c0ae7SAlex Tomas 				break;
2296c29c0ae7SAlex Tomas 			}
2297c29c0ae7SAlex Tomas 			path[i + 1].p_bh = bh;
2298a86c6181SAlex Tomas 
2299d0d856e8SRandy Dunlap 			/* save actual number of indexes since this
2300d0d856e8SRandy Dunlap 			 * number is changed at the next iteration */
2301a86c6181SAlex Tomas 			path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2302a86c6181SAlex Tomas 			i++;
2303a86c6181SAlex Tomas 		} else {
2304d0d856e8SRandy Dunlap 			/* we finished processing this index, go up */
2305a86c6181SAlex Tomas 			if (path[i].p_hdr->eh_entries == 0 && i > 0) {
2306d0d856e8SRandy Dunlap 				/* index is empty, remove it;
2307a86c6181SAlex Tomas 				 * handle must be already prepared by the
2308a86c6181SAlex Tomas 				 * truncatei_leaf() */
2309a86c6181SAlex Tomas 				err = ext4_ext_rm_idx(handle, inode, path + i);
2310a86c6181SAlex Tomas 			}
2311d0d856e8SRandy Dunlap 			/* root level has p_bh == NULL, brelse() eats this */
2312a86c6181SAlex Tomas 			brelse(path[i].p_bh);
2313a86c6181SAlex Tomas 			path[i].p_bh = NULL;
2314a86c6181SAlex Tomas 			i--;
2315a86c6181SAlex Tomas 			ext_debug("return to level %d\n", i);
2316a86c6181SAlex Tomas 		}
2317a86c6181SAlex Tomas 	}
2318a86c6181SAlex Tomas 
2319a86c6181SAlex Tomas 	/* TODO: flexible tree reduction should be here */
2320a86c6181SAlex Tomas 	if (path->p_hdr->eh_entries == 0) {
2321a86c6181SAlex Tomas 		/*
2322d0d856e8SRandy Dunlap 		 * truncate to zero freed all the tree,
2323d0d856e8SRandy Dunlap 		 * so we need to correct eh_depth
2324a86c6181SAlex Tomas 		 */
2325a86c6181SAlex Tomas 		err = ext4_ext_get_access(handle, inode, path);
2326a86c6181SAlex Tomas 		if (err == 0) {
2327a86c6181SAlex Tomas 			ext_inode_hdr(inode)->eh_depth = 0;
2328a86c6181SAlex Tomas 			ext_inode_hdr(inode)->eh_max =
2329a86c6181SAlex Tomas 				cpu_to_le16(ext4_ext_space_root(inode));
2330a86c6181SAlex Tomas 			err = ext4_ext_dirty(handle, inode, path);
2331a86c6181SAlex Tomas 		}
2332a86c6181SAlex Tomas 	}
2333a86c6181SAlex Tomas out:
2334a86c6181SAlex Tomas 	ext4_ext_drop_refs(path);
2335a86c6181SAlex Tomas 	kfree(path);
2336a86c6181SAlex Tomas 	ext4_journal_stop(handle);
2337a86c6181SAlex Tomas 
2338a86c6181SAlex Tomas 	return err;
2339a86c6181SAlex Tomas }
2340a86c6181SAlex Tomas 
2341a86c6181SAlex Tomas /*
2342a86c6181SAlex Tomas  * called at mount time
2343a86c6181SAlex Tomas  */
2344a86c6181SAlex Tomas void ext4_ext_init(struct super_block *sb)
2345a86c6181SAlex Tomas {
2346a86c6181SAlex Tomas 	/*
2347a86c6181SAlex Tomas 	 * possible initialization would be here
2348a86c6181SAlex Tomas 	 */
2349a86c6181SAlex Tomas 
235083982b6fSTheodore Ts'o 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
23514776004fSTheodore Ts'o 		printk(KERN_INFO "EXT4-fs: file extents enabled");
2352bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST
2353bbf2f9fbSRobert P. J. Day 		printk(", aggressive tests");
2354a86c6181SAlex Tomas #endif
2355a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH
2356a86c6181SAlex Tomas 		printk(", check binsearch");
2357a86c6181SAlex Tomas #endif
2358a86c6181SAlex Tomas #ifdef EXTENTS_STATS
2359a86c6181SAlex Tomas 		printk(", stats");
2360a86c6181SAlex Tomas #endif
2361a86c6181SAlex Tomas 		printk("\n");
2362a86c6181SAlex Tomas #ifdef EXTENTS_STATS
2363a86c6181SAlex Tomas 		spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2364a86c6181SAlex Tomas 		EXT4_SB(sb)->s_ext_min = 1 << 30;
2365a86c6181SAlex Tomas 		EXT4_SB(sb)->s_ext_max = 0;
2366a86c6181SAlex Tomas #endif
2367a86c6181SAlex Tomas 	}
2368a86c6181SAlex Tomas }
2369a86c6181SAlex Tomas 
2370a86c6181SAlex Tomas /*
2371a86c6181SAlex Tomas  * called at umount time
2372a86c6181SAlex Tomas  */
2373a86c6181SAlex Tomas void ext4_ext_release(struct super_block *sb)
2374a86c6181SAlex Tomas {
237583982b6fSTheodore Ts'o 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
2376a86c6181SAlex Tomas 		return;
2377a86c6181SAlex Tomas 
2378a86c6181SAlex Tomas #ifdef EXTENTS_STATS
2379a86c6181SAlex Tomas 	if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2380a86c6181SAlex Tomas 		struct ext4_sb_info *sbi = EXT4_SB(sb);
2381a86c6181SAlex Tomas 		printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2382a86c6181SAlex Tomas 			sbi->s_ext_blocks, sbi->s_ext_extents,
2383a86c6181SAlex Tomas 			sbi->s_ext_blocks / sbi->s_ext_extents);
2384a86c6181SAlex Tomas 		printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2385a86c6181SAlex Tomas 			sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2386a86c6181SAlex Tomas 	}
2387a86c6181SAlex Tomas #endif
2388a86c6181SAlex Tomas }
2389a86c6181SAlex Tomas 
2390093a088bSAneesh Kumar K.V static void bi_complete(struct bio *bio, int error)
2391093a088bSAneesh Kumar K.V {
2392093a088bSAneesh Kumar K.V 	complete((struct completion *)bio->bi_private);
2393093a088bSAneesh Kumar K.V }
2394093a088bSAneesh Kumar K.V 
2395093a088bSAneesh Kumar K.V /* FIXME!! we need to try to merge to left or right after zero-out  */
2396093a088bSAneesh Kumar K.V static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2397093a088bSAneesh Kumar K.V {
2398093a088bSAneesh Kumar K.V 	int ret = -EIO;
2399093a088bSAneesh Kumar K.V 	struct bio *bio;
2400093a088bSAneesh Kumar K.V 	int blkbits, blocksize;
2401093a088bSAneesh Kumar K.V 	sector_t ee_pblock;
2402093a088bSAneesh Kumar K.V 	struct completion event;
2403093a088bSAneesh Kumar K.V 	unsigned int ee_len, len, done, offset;
2404093a088bSAneesh Kumar K.V 
2405093a088bSAneesh Kumar K.V 
2406093a088bSAneesh Kumar K.V 	blkbits   = inode->i_blkbits;
2407093a088bSAneesh Kumar K.V 	blocksize = inode->i_sb->s_blocksize;
2408093a088bSAneesh Kumar K.V 	ee_len    = ext4_ext_get_actual_len(ex);
2409093a088bSAneesh Kumar K.V 	ee_pblock = ext_pblock(ex);
2410093a088bSAneesh Kumar K.V 
2411093a088bSAneesh Kumar K.V 	/* convert ee_pblock to 512 byte sectors */
2412093a088bSAneesh Kumar K.V 	ee_pblock = ee_pblock << (blkbits - 9);
2413093a088bSAneesh Kumar K.V 
2414093a088bSAneesh Kumar K.V 	while (ee_len > 0) {
2415093a088bSAneesh Kumar K.V 
2416093a088bSAneesh Kumar K.V 		if (ee_len > BIO_MAX_PAGES)
2417093a088bSAneesh Kumar K.V 			len = BIO_MAX_PAGES;
2418093a088bSAneesh Kumar K.V 		else
2419093a088bSAneesh Kumar K.V 			len = ee_len;
2420093a088bSAneesh Kumar K.V 
2421093a088bSAneesh Kumar K.V 		bio = bio_alloc(GFP_NOIO, len);
2422093a088bSAneesh Kumar K.V 		bio->bi_sector = ee_pblock;
2423093a088bSAneesh Kumar K.V 		bio->bi_bdev   = inode->i_sb->s_bdev;
2424093a088bSAneesh Kumar K.V 
2425093a088bSAneesh Kumar K.V 		done = 0;
2426093a088bSAneesh Kumar K.V 		offset = 0;
2427093a088bSAneesh Kumar K.V 		while (done < len) {
2428093a088bSAneesh Kumar K.V 			ret = bio_add_page(bio, ZERO_PAGE(0),
2429093a088bSAneesh Kumar K.V 							blocksize, offset);
2430093a088bSAneesh Kumar K.V 			if (ret != blocksize) {
2431093a088bSAneesh Kumar K.V 				/*
2432093a088bSAneesh Kumar K.V 				 * We can't add any more pages because of
2433093a088bSAneesh Kumar K.V 				 * hardware limitations.  Start a new bio.
2434093a088bSAneesh Kumar K.V 				 */
2435093a088bSAneesh Kumar K.V 				break;
2436093a088bSAneesh Kumar K.V 			}
2437093a088bSAneesh Kumar K.V 			done++;
2438093a088bSAneesh Kumar K.V 			offset += blocksize;
2439093a088bSAneesh Kumar K.V 			if (offset >= PAGE_CACHE_SIZE)
2440093a088bSAneesh Kumar K.V 				offset = 0;
2441093a088bSAneesh Kumar K.V 		}
2442093a088bSAneesh Kumar K.V 
2443093a088bSAneesh Kumar K.V 		init_completion(&event);
2444093a088bSAneesh Kumar K.V 		bio->bi_private = &event;
2445093a088bSAneesh Kumar K.V 		bio->bi_end_io = bi_complete;
2446093a088bSAneesh Kumar K.V 		submit_bio(WRITE, bio);
2447093a088bSAneesh Kumar K.V 		wait_for_completion(&event);
2448093a088bSAneesh Kumar K.V 
2449093a088bSAneesh Kumar K.V 		if (test_bit(BIO_UPTODATE, &bio->bi_flags))
2450093a088bSAneesh Kumar K.V 			ret = 0;
2451093a088bSAneesh Kumar K.V 		else {
2452093a088bSAneesh Kumar K.V 			ret = -EIO;
2453093a088bSAneesh Kumar K.V 			break;
2454093a088bSAneesh Kumar K.V 		}
2455093a088bSAneesh Kumar K.V 		bio_put(bio);
2456093a088bSAneesh Kumar K.V 		ee_len    -= done;
2457093a088bSAneesh Kumar K.V 		ee_pblock += done  << (blkbits - 9);
2458093a088bSAneesh Kumar K.V 	}
2459093a088bSAneesh Kumar K.V 	return ret;
2460093a088bSAneesh Kumar K.V }
2461093a088bSAneesh Kumar K.V 
24623977c965SAneesh Kumar K.V #define EXT4_EXT_ZERO_LEN 7
24633977c965SAneesh Kumar K.V 
246456055d3aSAmit Arora /*
246556055d3aSAmit Arora  * This function is called by ext4_ext_get_blocks() if someone tries to write
246656055d3aSAmit Arora  * to an uninitialized extent. It may result in splitting the uninitialized
246756055d3aSAmit Arora  * extent into multiple extents (upto three - one initialized and two
246856055d3aSAmit Arora  * uninitialized).
246956055d3aSAmit Arora  * There are three possibilities:
247056055d3aSAmit Arora  *   a> There is no split required: Entire extent should be initialized
247156055d3aSAmit Arora  *   b> Splits in two extents: Write is happening at either end of the extent
247256055d3aSAmit Arora  *   c> Splits in three extents: Somone is writing in middle of the extent
247356055d3aSAmit Arora  */
2474725d26d3SAneesh Kumar K.V static int ext4_ext_convert_to_initialized(handle_t *handle,
2475725d26d3SAneesh Kumar K.V 						struct inode *inode,
247656055d3aSAmit Arora 						struct ext4_ext_path *path,
2477725d26d3SAneesh Kumar K.V 						ext4_lblk_t iblock,
2478498e5f24STheodore Ts'o 						unsigned int max_blocks)
247956055d3aSAmit Arora {
248095c3889cSAneesh Kumar K.V 	struct ext4_extent *ex, newex, orig_ex;
248156055d3aSAmit Arora 	struct ext4_extent *ex1 = NULL;
248256055d3aSAmit Arora 	struct ext4_extent *ex2 = NULL;
248356055d3aSAmit Arora 	struct ext4_extent *ex3 = NULL;
248456055d3aSAmit Arora 	struct ext4_extent_header *eh;
2485725d26d3SAneesh Kumar K.V 	ext4_lblk_t ee_block;
2486725d26d3SAneesh Kumar K.V 	unsigned int allocated, ee_len, depth;
248756055d3aSAmit Arora 	ext4_fsblk_t newblock;
248856055d3aSAmit Arora 	int err = 0;
248956055d3aSAmit Arora 	int ret = 0;
249056055d3aSAmit Arora 
249156055d3aSAmit Arora 	depth = ext_depth(inode);
249256055d3aSAmit Arora 	eh = path[depth].p_hdr;
249356055d3aSAmit Arora 	ex = path[depth].p_ext;
249456055d3aSAmit Arora 	ee_block = le32_to_cpu(ex->ee_block);
249556055d3aSAmit Arora 	ee_len = ext4_ext_get_actual_len(ex);
249656055d3aSAmit Arora 	allocated = ee_len - (iblock - ee_block);
249756055d3aSAmit Arora 	newblock = iblock - ee_block + ext_pblock(ex);
249856055d3aSAmit Arora 	ex2 = ex;
249995c3889cSAneesh Kumar K.V 	orig_ex.ee_block = ex->ee_block;
250095c3889cSAneesh Kumar K.V 	orig_ex.ee_len   = cpu_to_le16(ee_len);
250195c3889cSAneesh Kumar K.V 	ext4_ext_store_pblock(&orig_ex, ext_pblock(ex));
250256055d3aSAmit Arora 
25039df5643aSAneesh Kumar K.V 	err = ext4_ext_get_access(handle, inode, path + depth);
25049df5643aSAneesh Kumar K.V 	if (err)
25059df5643aSAneesh Kumar K.V 		goto out;
25063977c965SAneesh Kumar K.V 	/* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
25073977c965SAneesh Kumar K.V 	if (ee_len <= 2*EXT4_EXT_ZERO_LEN) {
25083977c965SAneesh Kumar K.V 		err =  ext4_ext_zeroout(inode, &orig_ex);
25093977c965SAneesh Kumar K.V 		if (err)
25103977c965SAneesh Kumar K.V 			goto fix_extent_len;
25113977c965SAneesh Kumar K.V 		/* update the extent length and mark as initialized */
25123977c965SAneesh Kumar K.V 		ex->ee_block = orig_ex.ee_block;
25133977c965SAneesh Kumar K.V 		ex->ee_len   = orig_ex.ee_len;
25143977c965SAneesh Kumar K.V 		ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
25153977c965SAneesh Kumar K.V 		ext4_ext_dirty(handle, inode, path + depth);
2516161e7b7cSAneesh Kumar K.V 		/* zeroed the full extent */
2517161e7b7cSAneesh Kumar K.V 		return allocated;
25183977c965SAneesh Kumar K.V 	}
25199df5643aSAneesh Kumar K.V 
252056055d3aSAmit Arora 	/* ex1: ee_block to iblock - 1 : uninitialized */
252156055d3aSAmit Arora 	if (iblock > ee_block) {
252256055d3aSAmit Arora 		ex1 = ex;
252356055d3aSAmit Arora 		ex1->ee_len = cpu_to_le16(iblock - ee_block);
252456055d3aSAmit Arora 		ext4_ext_mark_uninitialized(ex1);
252556055d3aSAmit Arora 		ex2 = &newex;
252656055d3aSAmit Arora 	}
252756055d3aSAmit Arora 	/*
252856055d3aSAmit Arora 	 * for sanity, update the length of the ex2 extent before
252956055d3aSAmit Arora 	 * we insert ex3, if ex1 is NULL. This is to avoid temporary
253056055d3aSAmit Arora 	 * overlap of blocks.
253156055d3aSAmit Arora 	 */
253256055d3aSAmit Arora 	if (!ex1 && allocated > max_blocks)
253356055d3aSAmit Arora 		ex2->ee_len = cpu_to_le16(max_blocks);
253456055d3aSAmit Arora 	/* ex3: to ee_block + ee_len : uninitialised */
253556055d3aSAmit Arora 	if (allocated > max_blocks) {
253656055d3aSAmit Arora 		unsigned int newdepth;
25373977c965SAneesh Kumar K.V 		/* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */
25383977c965SAneesh Kumar K.V 		if (allocated <= EXT4_EXT_ZERO_LEN) {
2539d03856bdSAneesh Kumar K.V 			/*
2540d03856bdSAneesh Kumar K.V 			 * iblock == ee_block is handled by the zerouout
2541d03856bdSAneesh Kumar K.V 			 * at the beginning.
2542d03856bdSAneesh Kumar K.V 			 * Mark first half uninitialized.
25433977c965SAneesh Kumar K.V 			 * Mark second half initialized and zero out the
25443977c965SAneesh Kumar K.V 			 * initialized extent
25453977c965SAneesh Kumar K.V 			 */
25463977c965SAneesh Kumar K.V 			ex->ee_block = orig_ex.ee_block;
25473977c965SAneesh Kumar K.V 			ex->ee_len   = cpu_to_le16(ee_len - allocated);
25483977c965SAneesh Kumar K.V 			ext4_ext_mark_uninitialized(ex);
25493977c965SAneesh Kumar K.V 			ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
25503977c965SAneesh Kumar K.V 			ext4_ext_dirty(handle, inode, path + depth);
25513977c965SAneesh Kumar K.V 
25523977c965SAneesh Kumar K.V 			ex3 = &newex;
25533977c965SAneesh Kumar K.V 			ex3->ee_block = cpu_to_le32(iblock);
25543977c965SAneesh Kumar K.V 			ext4_ext_store_pblock(ex3, newblock);
25553977c965SAneesh Kumar K.V 			ex3->ee_len = cpu_to_le16(allocated);
25563977c965SAneesh Kumar K.V 			err = ext4_ext_insert_extent(handle, inode, path, ex3);
25573977c965SAneesh Kumar K.V 			if (err == -ENOSPC) {
25583977c965SAneesh Kumar K.V 				err =  ext4_ext_zeroout(inode, &orig_ex);
25593977c965SAneesh Kumar K.V 				if (err)
25603977c965SAneesh Kumar K.V 					goto fix_extent_len;
25613977c965SAneesh Kumar K.V 				ex->ee_block = orig_ex.ee_block;
25623977c965SAneesh Kumar K.V 				ex->ee_len   = orig_ex.ee_len;
25633977c965SAneesh Kumar K.V 				ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
25643977c965SAneesh Kumar K.V 				ext4_ext_dirty(handle, inode, path + depth);
2565d03856bdSAneesh Kumar K.V 				/* blocks available from iblock */
2566161e7b7cSAneesh Kumar K.V 				return allocated;
25673977c965SAneesh Kumar K.V 
25683977c965SAneesh Kumar K.V 			} else if (err)
25693977c965SAneesh Kumar K.V 				goto fix_extent_len;
25703977c965SAneesh Kumar K.V 
2571161e7b7cSAneesh Kumar K.V 			/*
2572161e7b7cSAneesh Kumar K.V 			 * We need to zero out the second half because
2573161e7b7cSAneesh Kumar K.V 			 * an fallocate request can update file size and
2574161e7b7cSAneesh Kumar K.V 			 * converting the second half to initialized extent
2575161e7b7cSAneesh Kumar K.V 			 * implies that we can leak some junk data to user
2576161e7b7cSAneesh Kumar K.V 			 * space.
2577161e7b7cSAneesh Kumar K.V 			 */
2578161e7b7cSAneesh Kumar K.V 			err =  ext4_ext_zeroout(inode, ex3);
2579161e7b7cSAneesh Kumar K.V 			if (err) {
2580161e7b7cSAneesh Kumar K.V 				/*
2581161e7b7cSAneesh Kumar K.V 				 * We should actually mark the
2582161e7b7cSAneesh Kumar K.V 				 * second half as uninit and return error
2583161e7b7cSAneesh Kumar K.V 				 * Insert would have changed the extent
2584161e7b7cSAneesh Kumar K.V 				 */
2585161e7b7cSAneesh Kumar K.V 				depth = ext_depth(inode);
2586161e7b7cSAneesh Kumar K.V 				ext4_ext_drop_refs(path);
2587161e7b7cSAneesh Kumar K.V 				path = ext4_ext_find_extent(inode,
2588161e7b7cSAneesh Kumar K.V 								iblock, path);
2589161e7b7cSAneesh Kumar K.V 				if (IS_ERR(path)) {
2590161e7b7cSAneesh Kumar K.V 					err = PTR_ERR(path);
2591161e7b7cSAneesh Kumar K.V 					return err;
2592161e7b7cSAneesh Kumar K.V 				}
2593d03856bdSAneesh Kumar K.V 				/* get the second half extent details */
2594161e7b7cSAneesh Kumar K.V 				ex = path[depth].p_ext;
2595161e7b7cSAneesh Kumar K.V 				err = ext4_ext_get_access(handle, inode,
2596161e7b7cSAneesh Kumar K.V 								path + depth);
2597161e7b7cSAneesh Kumar K.V 				if (err)
2598161e7b7cSAneesh Kumar K.V 					return err;
2599161e7b7cSAneesh Kumar K.V 				ext4_ext_mark_uninitialized(ex);
2600161e7b7cSAneesh Kumar K.V 				ext4_ext_dirty(handle, inode, path + depth);
2601161e7b7cSAneesh Kumar K.V 				return err;
2602161e7b7cSAneesh Kumar K.V 			}
2603161e7b7cSAneesh Kumar K.V 
2604161e7b7cSAneesh Kumar K.V 			/* zeroed the second half */
26053977c965SAneesh Kumar K.V 			return allocated;
26063977c965SAneesh Kumar K.V 		}
260756055d3aSAmit Arora 		ex3 = &newex;
260856055d3aSAmit Arora 		ex3->ee_block = cpu_to_le32(iblock + max_blocks);
260956055d3aSAmit Arora 		ext4_ext_store_pblock(ex3, newblock + max_blocks);
261056055d3aSAmit Arora 		ex3->ee_len = cpu_to_le16(allocated - max_blocks);
261156055d3aSAmit Arora 		ext4_ext_mark_uninitialized(ex3);
261256055d3aSAmit Arora 		err = ext4_ext_insert_extent(handle, inode, path, ex3);
2613093a088bSAneesh Kumar K.V 		if (err == -ENOSPC) {
2614093a088bSAneesh Kumar K.V 			err =  ext4_ext_zeroout(inode, &orig_ex);
2615093a088bSAneesh Kumar K.V 			if (err)
2616093a088bSAneesh Kumar K.V 				goto fix_extent_len;
2617093a088bSAneesh Kumar K.V 			/* update the extent length and mark as initialized */
261895c3889cSAneesh Kumar K.V 			ex->ee_block = orig_ex.ee_block;
261995c3889cSAneesh Kumar K.V 			ex->ee_len   = orig_ex.ee_len;
262095c3889cSAneesh Kumar K.V 			ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
262195c3889cSAneesh Kumar K.V 			ext4_ext_dirty(handle, inode, path + depth);
2622161e7b7cSAneesh Kumar K.V 			/* zeroed the full extent */
2623d03856bdSAneesh Kumar K.V 			/* blocks available from iblock */
2624161e7b7cSAneesh Kumar K.V 			return allocated;
2625093a088bSAneesh Kumar K.V 
2626093a088bSAneesh Kumar K.V 		} else if (err)
2627093a088bSAneesh Kumar K.V 			goto fix_extent_len;
262856055d3aSAmit Arora 		/*
262956055d3aSAmit Arora 		 * The depth, and hence eh & ex might change
263056055d3aSAmit Arora 		 * as part of the insert above.
263156055d3aSAmit Arora 		 */
263256055d3aSAmit Arora 		newdepth = ext_depth(inode);
263395c3889cSAneesh Kumar K.V 		/*
263473ac36eaSColy Li 		 * update the extent length after successful insert of the
263595c3889cSAneesh Kumar K.V 		 * split extent
263695c3889cSAneesh Kumar K.V 		 */
263795c3889cSAneesh Kumar K.V 		orig_ex.ee_len = cpu_to_le16(ee_len -
263895c3889cSAneesh Kumar K.V 						ext4_ext_get_actual_len(ex3));
263956055d3aSAmit Arora 		depth = newdepth;
2640b35905c1SAneesh Kumar K.V 		ext4_ext_drop_refs(path);
2641b35905c1SAneesh Kumar K.V 		path = ext4_ext_find_extent(inode, iblock, path);
264256055d3aSAmit Arora 		if (IS_ERR(path)) {
264356055d3aSAmit Arora 			err = PTR_ERR(path);
264456055d3aSAmit Arora 			goto out;
264556055d3aSAmit Arora 		}
264656055d3aSAmit Arora 		eh = path[depth].p_hdr;
264756055d3aSAmit Arora 		ex = path[depth].p_ext;
264856055d3aSAmit Arora 		if (ex2 != &newex)
264956055d3aSAmit Arora 			ex2 = ex;
26509df5643aSAneesh Kumar K.V 
26519df5643aSAneesh Kumar K.V 		err = ext4_ext_get_access(handle, inode, path + depth);
26529df5643aSAneesh Kumar K.V 		if (err)
26539df5643aSAneesh Kumar K.V 			goto out;
2654d03856bdSAneesh Kumar K.V 
265556055d3aSAmit Arora 		allocated = max_blocks;
26563977c965SAneesh Kumar K.V 
26573977c965SAneesh Kumar K.V 		/* If extent has less than EXT4_EXT_ZERO_LEN and we are trying
26583977c965SAneesh Kumar K.V 		 * to insert a extent in the middle zerout directly
26593977c965SAneesh Kumar K.V 		 * otherwise give the extent a chance to merge to left
26603977c965SAneesh Kumar K.V 		 */
26613977c965SAneesh Kumar K.V 		if (le16_to_cpu(orig_ex.ee_len) <= EXT4_EXT_ZERO_LEN &&
26623977c965SAneesh Kumar K.V 							iblock != ee_block) {
26633977c965SAneesh Kumar K.V 			err =  ext4_ext_zeroout(inode, &orig_ex);
26643977c965SAneesh Kumar K.V 			if (err)
26653977c965SAneesh Kumar K.V 				goto fix_extent_len;
26663977c965SAneesh Kumar K.V 			/* update the extent length and mark as initialized */
26673977c965SAneesh Kumar K.V 			ex->ee_block = orig_ex.ee_block;
26683977c965SAneesh Kumar K.V 			ex->ee_len   = orig_ex.ee_len;
26693977c965SAneesh Kumar K.V 			ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
26703977c965SAneesh Kumar K.V 			ext4_ext_dirty(handle, inode, path + depth);
2671161e7b7cSAneesh Kumar K.V 			/* zero out the first half */
2672d03856bdSAneesh Kumar K.V 			/* blocks available from iblock */
2673161e7b7cSAneesh Kumar K.V 			return allocated;
26743977c965SAneesh Kumar K.V 		}
267556055d3aSAmit Arora 	}
267656055d3aSAmit Arora 	/*
267756055d3aSAmit Arora 	 * If there was a change of depth as part of the
267856055d3aSAmit Arora 	 * insertion of ex3 above, we need to update the length
267956055d3aSAmit Arora 	 * of the ex1 extent again here
268056055d3aSAmit Arora 	 */
268156055d3aSAmit Arora 	if (ex1 && ex1 != ex) {
268256055d3aSAmit Arora 		ex1 = ex;
268356055d3aSAmit Arora 		ex1->ee_len = cpu_to_le16(iblock - ee_block);
268456055d3aSAmit Arora 		ext4_ext_mark_uninitialized(ex1);
268556055d3aSAmit Arora 		ex2 = &newex;
268656055d3aSAmit Arora 	}
268756055d3aSAmit Arora 	/* ex2: iblock to iblock + maxblocks-1 : initialised */
268856055d3aSAmit Arora 	ex2->ee_block = cpu_to_le32(iblock);
268956055d3aSAmit Arora 	ext4_ext_store_pblock(ex2, newblock);
269056055d3aSAmit Arora 	ex2->ee_len = cpu_to_le16(allocated);
269156055d3aSAmit Arora 	if (ex2 != ex)
269256055d3aSAmit Arora 		goto insert;
269356055d3aSAmit Arora 	/*
269456055d3aSAmit Arora 	 * New (initialized) extent starts from the first block
269556055d3aSAmit Arora 	 * in the current extent. i.e., ex2 == ex
269656055d3aSAmit Arora 	 * We have to see if it can be merged with the extent
269756055d3aSAmit Arora 	 * on the left.
269856055d3aSAmit Arora 	 */
269956055d3aSAmit Arora 	if (ex2 > EXT_FIRST_EXTENT(eh)) {
270056055d3aSAmit Arora 		/*
270156055d3aSAmit Arora 		 * To merge left, pass "ex2 - 1" to try_to_merge(),
270256055d3aSAmit Arora 		 * since it merges towards right _only_.
270356055d3aSAmit Arora 		 */
270456055d3aSAmit Arora 		ret = ext4_ext_try_to_merge(inode, path, ex2 - 1);
270556055d3aSAmit Arora 		if (ret) {
270656055d3aSAmit Arora 			err = ext4_ext_correct_indexes(handle, inode, path);
270756055d3aSAmit Arora 			if (err)
270856055d3aSAmit Arora 				goto out;
270956055d3aSAmit Arora 			depth = ext_depth(inode);
271056055d3aSAmit Arora 			ex2--;
271156055d3aSAmit Arora 		}
271256055d3aSAmit Arora 	}
271356055d3aSAmit Arora 	/*
271456055d3aSAmit Arora 	 * Try to Merge towards right. This might be required
271556055d3aSAmit Arora 	 * only when the whole extent is being written to.
271656055d3aSAmit Arora 	 * i.e. ex2 == ex and ex3 == NULL.
271756055d3aSAmit Arora 	 */
271856055d3aSAmit Arora 	if (!ex3) {
271956055d3aSAmit Arora 		ret = ext4_ext_try_to_merge(inode, path, ex2);
272056055d3aSAmit Arora 		if (ret) {
272156055d3aSAmit Arora 			err = ext4_ext_correct_indexes(handle, inode, path);
272256055d3aSAmit Arora 			if (err)
272356055d3aSAmit Arora 				goto out;
272456055d3aSAmit Arora 		}
272556055d3aSAmit Arora 	}
272656055d3aSAmit Arora 	/* Mark modified extent as dirty */
272756055d3aSAmit Arora 	err = ext4_ext_dirty(handle, inode, path + depth);
272856055d3aSAmit Arora 	goto out;
272956055d3aSAmit Arora insert:
273056055d3aSAmit Arora 	err = ext4_ext_insert_extent(handle, inode, path, &newex);
2731093a088bSAneesh Kumar K.V 	if (err == -ENOSPC) {
2732093a088bSAneesh Kumar K.V 		err =  ext4_ext_zeroout(inode, &orig_ex);
2733093a088bSAneesh Kumar K.V 		if (err)
2734093a088bSAneesh Kumar K.V 			goto fix_extent_len;
2735093a088bSAneesh Kumar K.V 		/* update the extent length and mark as initialized */
2736093a088bSAneesh Kumar K.V 		ex->ee_block = orig_ex.ee_block;
2737093a088bSAneesh Kumar K.V 		ex->ee_len   = orig_ex.ee_len;
2738093a088bSAneesh Kumar K.V 		ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2739093a088bSAneesh Kumar K.V 		ext4_ext_dirty(handle, inode, path + depth);
2740161e7b7cSAneesh Kumar K.V 		/* zero out the first half */
2741161e7b7cSAneesh Kumar K.V 		return allocated;
2742093a088bSAneesh Kumar K.V 	} else if (err)
2743093a088bSAneesh Kumar K.V 		goto fix_extent_len;
2744093a088bSAneesh Kumar K.V out:
2745093a088bSAneesh Kumar K.V 	return err ? err : allocated;
2746093a088bSAneesh Kumar K.V 
2747093a088bSAneesh Kumar K.V fix_extent_len:
274895c3889cSAneesh Kumar K.V 	ex->ee_block = orig_ex.ee_block;
274995c3889cSAneesh Kumar K.V 	ex->ee_len   = orig_ex.ee_len;
275095c3889cSAneesh Kumar K.V 	ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
275195c3889cSAneesh Kumar K.V 	ext4_ext_mark_uninitialized(ex);
275295c3889cSAneesh Kumar K.V 	ext4_ext_dirty(handle, inode, path + depth);
2753093a088bSAneesh Kumar K.V 	return err;
275456055d3aSAmit Arora }
275556055d3aSAmit Arora 
2756c278bfecSAneesh Kumar K.V /*
2757f5ab0d1fSMingming Cao  * Block allocation/map/preallocation routine for extents based files
2758f5ab0d1fSMingming Cao  *
2759f5ab0d1fSMingming Cao  *
2760c278bfecSAneesh Kumar K.V  * Need to be called with
27610e855ac8SAneesh Kumar K.V  * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
27620e855ac8SAneesh Kumar K.V  * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
2763f5ab0d1fSMingming Cao  *
2764f5ab0d1fSMingming Cao  * return > 0, number of of blocks already mapped/allocated
2765f5ab0d1fSMingming Cao  *          if create == 0 and these are pre-allocated blocks
2766f5ab0d1fSMingming Cao  *          	buffer head is unmapped
2767f5ab0d1fSMingming Cao  *          otherwise blocks are mapped
2768f5ab0d1fSMingming Cao  *
2769f5ab0d1fSMingming Cao  * return = 0, if plain look up failed (blocks have not been allocated)
2770f5ab0d1fSMingming Cao  *          buffer head is unmapped
2771f5ab0d1fSMingming Cao  *
2772f5ab0d1fSMingming Cao  * return < 0, error case.
2773c278bfecSAneesh Kumar K.V  */
2774f65e6fbaSAlex Tomas int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
2775725d26d3SAneesh Kumar K.V 			ext4_lblk_t iblock,
2776498e5f24STheodore Ts'o 			unsigned int max_blocks, struct buffer_head *bh_result,
2777c2177057STheodore Ts'o 			int flags)
2778a86c6181SAlex Tomas {
2779a86c6181SAlex Tomas 	struct ext4_ext_path *path = NULL;
278056055d3aSAmit Arora 	struct ext4_extent_header *eh;
2781a86c6181SAlex Tomas 	struct ext4_extent newex, *ex;
2782498e5f24STheodore Ts'o 	ext4_fsblk_t newblock;
2783498e5f24STheodore Ts'o 	int err = 0, depth, ret, cache_type;
2784498e5f24STheodore Ts'o 	unsigned int allocated = 0;
2785c9de560dSAlex Tomas 	struct ext4_allocation_request ar;
2786a86c6181SAlex Tomas 
2787a86c6181SAlex Tomas 	__clear_bit(BH_New, &bh_result->b_state);
2788498e5f24STheodore Ts'o 	ext_debug("blocks %u/%u requested for inode %u\n",
2789bba90743SEric Sandeen 			iblock, max_blocks, inode->i_ino);
2790a86c6181SAlex Tomas 
2791a86c6181SAlex Tomas 	/* check in cache */
2792498e5f24STheodore Ts'o 	cache_type = ext4_ext_in_cache(inode, iblock, &newex);
2793498e5f24STheodore Ts'o 	if (cache_type) {
2794498e5f24STheodore Ts'o 		if (cache_type == EXT4_EXT_CACHE_GAP) {
2795c2177057STheodore Ts'o 			if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
279656055d3aSAmit Arora 				/*
279756055d3aSAmit Arora 				 * block isn't allocated yet and
279856055d3aSAmit Arora 				 * user doesn't want to allocate it
279956055d3aSAmit Arora 				 */
2800a86c6181SAlex Tomas 				goto out2;
2801a86c6181SAlex Tomas 			}
2802a86c6181SAlex Tomas 			/* we should allocate requested block */
2803498e5f24STheodore Ts'o 		} else if (cache_type == EXT4_EXT_CACHE_EXTENT) {
2804a86c6181SAlex Tomas 			/* block is already allocated */
2805a86c6181SAlex Tomas 			newblock = iblock
2806a86c6181SAlex Tomas 				   - le32_to_cpu(newex.ee_block)
2807f65e6fbaSAlex Tomas 				   + ext_pblock(&newex);
2808d0d856e8SRandy Dunlap 			/* number of remaining blocks in the extent */
2809b939e376SAneesh Kumar K.V 			allocated = ext4_ext_get_actual_len(&newex) -
2810a86c6181SAlex Tomas 					(iblock - le32_to_cpu(newex.ee_block));
2811a86c6181SAlex Tomas 			goto out;
2812a86c6181SAlex Tomas 		} else {
2813a86c6181SAlex Tomas 			BUG();
2814a86c6181SAlex Tomas 		}
2815a86c6181SAlex Tomas 	}
2816a86c6181SAlex Tomas 
2817a86c6181SAlex Tomas 	/* find extent for this block */
2818a86c6181SAlex Tomas 	path = ext4_ext_find_extent(inode, iblock, NULL);
2819a86c6181SAlex Tomas 	if (IS_ERR(path)) {
2820a86c6181SAlex Tomas 		err = PTR_ERR(path);
2821a86c6181SAlex Tomas 		path = NULL;
2822a86c6181SAlex Tomas 		goto out2;
2823a86c6181SAlex Tomas 	}
2824a86c6181SAlex Tomas 
2825a86c6181SAlex Tomas 	depth = ext_depth(inode);
2826a86c6181SAlex Tomas 
2827a86c6181SAlex Tomas 	/*
2828d0d856e8SRandy Dunlap 	 * consistent leaf must not be empty;
2829d0d856e8SRandy Dunlap 	 * this situation is possible, though, _during_ tree modification;
2830a86c6181SAlex Tomas 	 * this is why assert can't be put in ext4_ext_find_extent()
2831a86c6181SAlex Tomas 	 */
2832a86c6181SAlex Tomas 	BUG_ON(path[depth].p_ext == NULL && depth != 0);
283356055d3aSAmit Arora 	eh = path[depth].p_hdr;
2834a86c6181SAlex Tomas 
28357e028976SAvantika Mathur 	ex = path[depth].p_ext;
28367e028976SAvantika Mathur 	if (ex) {
2837725d26d3SAneesh Kumar K.V 		ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
2838f65e6fbaSAlex Tomas 		ext4_fsblk_t ee_start = ext_pblock(ex);
2839a2df2a63SAmit Arora 		unsigned short ee_len;
2840471d4011SSuparna Bhattacharya 
2841471d4011SSuparna Bhattacharya 		/*
2842471d4011SSuparna Bhattacharya 		 * Uninitialized extents are treated as holes, except that
284356055d3aSAmit Arora 		 * we split out initialized portions during a write.
2844471d4011SSuparna Bhattacharya 		 */
2845a2df2a63SAmit Arora 		ee_len = ext4_ext_get_actual_len(ex);
2846d0d856e8SRandy Dunlap 		/* if found extent covers block, simply return it */
2847a86c6181SAlex Tomas 		if (iblock >= ee_block && iblock < ee_block + ee_len) {
2848a86c6181SAlex Tomas 			newblock = iblock - ee_block + ee_start;
2849d0d856e8SRandy Dunlap 			/* number of remaining blocks in the extent */
2850a86c6181SAlex Tomas 			allocated = ee_len - (iblock - ee_block);
2851bba90743SEric Sandeen 			ext_debug("%u fit into %lu:%d -> %llu\n", iblock,
2852a86c6181SAlex Tomas 					ee_block, ee_len, newblock);
285356055d3aSAmit Arora 
2854a2df2a63SAmit Arora 			/* Do not put uninitialized extent in the cache */
285556055d3aSAmit Arora 			if (!ext4_ext_is_uninitialized(ex)) {
2856a2df2a63SAmit Arora 				ext4_ext_put_in_cache(inode, ee_block,
2857a2df2a63SAmit Arora 							ee_len, ee_start,
2858a2df2a63SAmit Arora 							EXT4_EXT_CACHE_EXTENT);
2859a86c6181SAlex Tomas 				goto out;
2860a86c6181SAlex Tomas 			}
2861c2177057STheodore Ts'o 			if (flags & EXT4_GET_BLOCKS_UNINIT_EXT)
286256055d3aSAmit Arora 				goto out;
2863c2177057STheodore Ts'o 			if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
286429fa89d0SAneesh Kumar K.V 				if (allocated > max_blocks)
286529fa89d0SAneesh Kumar K.V 					allocated = max_blocks;
2866e067ba00SAneesh Kumar K.V 				/*
2867e067ba00SAneesh Kumar K.V 				 * We have blocks reserved already.  We
2868e067ba00SAneesh Kumar K.V 				 * return allocated blocks so that delalloc
2869e067ba00SAneesh Kumar K.V 				 * won't do block reservation for us.  But
2870e067ba00SAneesh Kumar K.V 				 * the buffer head will be unmapped so that
2871e067ba00SAneesh Kumar K.V 				 * a read from the block returns 0s.
2872e067ba00SAneesh Kumar K.V 				 */
2873953e622bSEric Sandeen 				set_buffer_unwritten(bh_result);
28749c1ee184SAneesh Kumar K.V 				bh_result->b_bdev = inode->i_sb->s_bdev;
28759c1ee184SAneesh Kumar K.V 				bh_result->b_blocknr = newblock;
287656055d3aSAmit Arora 				goto out2;
2877e067ba00SAneesh Kumar K.V 			}
287856055d3aSAmit Arora 
287956055d3aSAmit Arora 			ret = ext4_ext_convert_to_initialized(handle, inode,
288056055d3aSAmit Arora 								path, iblock,
288156055d3aSAmit Arora 								max_blocks);
2882dbf9d7daSDmitry Monakhov 			if (ret <= 0) {
2883dbf9d7daSDmitry Monakhov 				err = ret;
288456055d3aSAmit Arora 				goto out2;
2885dbf9d7daSDmitry Monakhov 			} else
288656055d3aSAmit Arora 				allocated = ret;
288756055d3aSAmit Arora 			goto outnew;
288856055d3aSAmit Arora 		}
2889a86c6181SAlex Tomas 	}
2890a86c6181SAlex Tomas 
2891a86c6181SAlex Tomas 	/*
2892d0d856e8SRandy Dunlap 	 * requested block isn't allocated yet;
2893a86c6181SAlex Tomas 	 * we couldn't try to create block if create flag is zero
2894a86c6181SAlex Tomas 	 */
2895c2177057STheodore Ts'o 	if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
289656055d3aSAmit Arora 		/*
289756055d3aSAmit Arora 		 * put just found gap into cache to speed up
289856055d3aSAmit Arora 		 * subsequent requests
289956055d3aSAmit Arora 		 */
2900a86c6181SAlex Tomas 		ext4_ext_put_gap_in_cache(inode, path, iblock);
2901a86c6181SAlex Tomas 		goto out2;
2902a86c6181SAlex Tomas 	}
2903a86c6181SAlex Tomas 	/*
2904c2ea3fdeSTheodore Ts'o 	 * Okay, we need to do block allocation.
2905a86c6181SAlex Tomas 	 */
2906a86c6181SAlex Tomas 
2907c9de560dSAlex Tomas 	/* find neighbour allocated blocks */
2908c9de560dSAlex Tomas 	ar.lleft = iblock;
2909c9de560dSAlex Tomas 	err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
2910c9de560dSAlex Tomas 	if (err)
2911c9de560dSAlex Tomas 		goto out2;
2912c9de560dSAlex Tomas 	ar.lright = iblock;
2913c9de560dSAlex Tomas 	err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright);
2914c9de560dSAlex Tomas 	if (err)
2915c9de560dSAlex Tomas 		goto out2;
291625d14f98SAmit Arora 
2917749269faSAmit Arora 	/*
2918749269faSAmit Arora 	 * See if request is beyond maximum number of blocks we can have in
2919749269faSAmit Arora 	 * a single extent. For an initialized extent this limit is
2920749269faSAmit Arora 	 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
2921749269faSAmit Arora 	 * EXT_UNINIT_MAX_LEN.
2922749269faSAmit Arora 	 */
2923749269faSAmit Arora 	if (max_blocks > EXT_INIT_MAX_LEN &&
2924c2177057STheodore Ts'o 	    !(flags & EXT4_GET_BLOCKS_UNINIT_EXT))
2925749269faSAmit Arora 		max_blocks = EXT_INIT_MAX_LEN;
2926749269faSAmit Arora 	else if (max_blocks > EXT_UNINIT_MAX_LEN &&
2927c2177057STheodore Ts'o 		 (flags & EXT4_GET_BLOCKS_UNINIT_EXT))
2928749269faSAmit Arora 		max_blocks = EXT_UNINIT_MAX_LEN;
2929749269faSAmit Arora 
293025d14f98SAmit Arora 	/* Check if we can really insert (iblock)::(iblock+max_blocks) extent */
293125d14f98SAmit Arora 	newex.ee_block = cpu_to_le32(iblock);
293225d14f98SAmit Arora 	newex.ee_len = cpu_to_le16(max_blocks);
293325d14f98SAmit Arora 	err = ext4_ext_check_overlap(inode, &newex, path);
293425d14f98SAmit Arora 	if (err)
2935b939e376SAneesh Kumar K.V 		allocated = ext4_ext_get_actual_len(&newex);
293625d14f98SAmit Arora 	else
2937a86c6181SAlex Tomas 		allocated = max_blocks;
2938c9de560dSAlex Tomas 
2939c9de560dSAlex Tomas 	/* allocate new block */
2940c9de560dSAlex Tomas 	ar.inode = inode;
2941c9de560dSAlex Tomas 	ar.goal = ext4_ext_find_goal(inode, path, iblock);
2942c9de560dSAlex Tomas 	ar.logical = iblock;
2943c9de560dSAlex Tomas 	ar.len = allocated;
2944c9de560dSAlex Tomas 	if (S_ISREG(inode->i_mode))
2945c9de560dSAlex Tomas 		ar.flags = EXT4_MB_HINT_DATA;
2946c9de560dSAlex Tomas 	else
2947c9de560dSAlex Tomas 		/* disable in-core preallocation for non-regular files */
2948c9de560dSAlex Tomas 		ar.flags = 0;
2949c9de560dSAlex Tomas 	newblock = ext4_mb_new_blocks(handle, &ar, &err);
2950a86c6181SAlex Tomas 	if (!newblock)
2951a86c6181SAlex Tomas 		goto out2;
29522ae02107SMingming Cao 	ext_debug("allocate new block: goal %llu, found %llu/%lu\n",
2953498e5f24STheodore Ts'o 		  ar.goal, newblock, allocated);
2954a86c6181SAlex Tomas 
2955a86c6181SAlex Tomas 	/* try to insert new extent into found leaf and return */
2956f65e6fbaSAlex Tomas 	ext4_ext_store_pblock(&newex, newblock);
2957c9de560dSAlex Tomas 	newex.ee_len = cpu_to_le16(ar.len);
2958c2177057STheodore Ts'o 	if (flags & EXT4_GET_BLOCKS_UNINIT_EXT)  /* Mark uninitialized */
2959a2df2a63SAmit Arora 		ext4_ext_mark_uninitialized(&newex);
2960a86c6181SAlex Tomas 	err = ext4_ext_insert_extent(handle, inode, path, &newex);
2961315054f0SAlex Tomas 	if (err) {
2962315054f0SAlex Tomas 		/* free data blocks we just allocated */
2963c9de560dSAlex Tomas 		/* not a good idea to call discard here directly,
2964c9de560dSAlex Tomas 		 * but otherwise we'd need to call it every free() */
2965c2ea3fdeSTheodore Ts'o 		ext4_discard_preallocations(inode);
2966315054f0SAlex Tomas 		ext4_free_blocks(handle, inode, ext_pblock(&newex),
2967b939e376SAneesh Kumar K.V 					ext4_ext_get_actual_len(&newex), 0);
2968a86c6181SAlex Tomas 		goto out2;
2969315054f0SAlex Tomas 	}
2970a86c6181SAlex Tomas 
2971a86c6181SAlex Tomas 	/* previous routine could use block we allocated */
2972f65e6fbaSAlex Tomas 	newblock = ext_pblock(&newex);
2973b939e376SAneesh Kumar K.V 	allocated = ext4_ext_get_actual_len(&newex);
297456055d3aSAmit Arora outnew:
2975953e622bSEric Sandeen 	set_buffer_new(bh_result);
2976a86c6181SAlex Tomas 
2977a2df2a63SAmit Arora 	/* Cache only when it is _not_ an uninitialized extent */
2978c2177057STheodore Ts'o 	if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0)
2979a86c6181SAlex Tomas 		ext4_ext_put_in_cache(inode, iblock, allocated, newblock,
2980a86c6181SAlex Tomas 						EXT4_EXT_CACHE_EXTENT);
2981a86c6181SAlex Tomas out:
2982a86c6181SAlex Tomas 	if (allocated > max_blocks)
2983a86c6181SAlex Tomas 		allocated = max_blocks;
2984a86c6181SAlex Tomas 	ext4_ext_show_leaf(inode, path);
2985953e622bSEric Sandeen 	set_buffer_mapped(bh_result);
2986a86c6181SAlex Tomas 	bh_result->b_bdev = inode->i_sb->s_bdev;
2987a86c6181SAlex Tomas 	bh_result->b_blocknr = newblock;
2988a86c6181SAlex Tomas out2:
2989a86c6181SAlex Tomas 	if (path) {
2990a86c6181SAlex Tomas 		ext4_ext_drop_refs(path);
2991a86c6181SAlex Tomas 		kfree(path);
2992a86c6181SAlex Tomas 	}
2993a86c6181SAlex Tomas 	return err ? err : allocated;
2994a86c6181SAlex Tomas }
2995a86c6181SAlex Tomas 
2996cf108bcaSJan Kara void ext4_ext_truncate(struct inode *inode)
2997a86c6181SAlex Tomas {
2998a86c6181SAlex Tomas 	struct address_space *mapping = inode->i_mapping;
2999a86c6181SAlex Tomas 	struct super_block *sb = inode->i_sb;
3000725d26d3SAneesh Kumar K.V 	ext4_lblk_t last_block;
3001a86c6181SAlex Tomas 	handle_t *handle;
3002a86c6181SAlex Tomas 	int err = 0;
3003a86c6181SAlex Tomas 
3004a86c6181SAlex Tomas 	/*
3005a86c6181SAlex Tomas 	 * probably first extent we're gonna free will be last in block
3006a86c6181SAlex Tomas 	 */
3007f3bd1f3fSMingming Cao 	err = ext4_writepage_trans_blocks(inode);
3008a86c6181SAlex Tomas 	handle = ext4_journal_start(inode, err);
3009cf108bcaSJan Kara 	if (IS_ERR(handle))
3010a86c6181SAlex Tomas 		return;
3011a86c6181SAlex Tomas 
3012cf108bcaSJan Kara 	if (inode->i_size & (sb->s_blocksize - 1))
3013cf108bcaSJan Kara 		ext4_block_truncate_page(handle, mapping, inode->i_size);
3014a86c6181SAlex Tomas 
30159ddfc3dcSJan Kara 	if (ext4_orphan_add(handle, inode))
30169ddfc3dcSJan Kara 		goto out_stop;
30179ddfc3dcSJan Kara 
30180e855ac8SAneesh Kumar K.V 	down_write(&EXT4_I(inode)->i_data_sem);
3019a86c6181SAlex Tomas 	ext4_ext_invalidate_cache(inode);
3020a86c6181SAlex Tomas 
3021c2ea3fdeSTheodore Ts'o 	ext4_discard_preallocations(inode);
3022c9de560dSAlex Tomas 
3023a86c6181SAlex Tomas 	/*
3024d0d856e8SRandy Dunlap 	 * TODO: optimization is possible here.
3025d0d856e8SRandy Dunlap 	 * Probably we need not scan at all,
3026d0d856e8SRandy Dunlap 	 * because page truncation is enough.
3027a86c6181SAlex Tomas 	 */
3028a86c6181SAlex Tomas 
3029a86c6181SAlex Tomas 	/* we have to know where to truncate from in crash case */
3030a86c6181SAlex Tomas 	EXT4_I(inode)->i_disksize = inode->i_size;
3031a86c6181SAlex Tomas 	ext4_mark_inode_dirty(handle, inode);
3032a86c6181SAlex Tomas 
3033a86c6181SAlex Tomas 	last_block = (inode->i_size + sb->s_blocksize - 1)
3034a86c6181SAlex Tomas 			>> EXT4_BLOCK_SIZE_BITS(sb);
3035a86c6181SAlex Tomas 	err = ext4_ext_remove_space(inode, last_block);
3036a86c6181SAlex Tomas 
3037a86c6181SAlex Tomas 	/* In a multi-transaction truncate, we only make the final
303856055d3aSAmit Arora 	 * transaction synchronous.
303956055d3aSAmit Arora 	 */
3040a86c6181SAlex Tomas 	if (IS_SYNC(inode))
30410390131bSFrank Mayhar 		ext4_handle_sync(handle);
3042a86c6181SAlex Tomas 
3043a86c6181SAlex Tomas out_stop:
30449ddfc3dcSJan Kara 	up_write(&EXT4_I(inode)->i_data_sem);
3045a86c6181SAlex Tomas 	/*
3046d0d856e8SRandy Dunlap 	 * If this was a simple ftruncate() and the file will remain alive,
3047a86c6181SAlex Tomas 	 * then we need to clear up the orphan record which we created above.
3048a86c6181SAlex Tomas 	 * However, if this was a real unlink then we were called by
3049a86c6181SAlex Tomas 	 * ext4_delete_inode(), and we allow that function to clean up the
3050a86c6181SAlex Tomas 	 * orphan info for us.
3051a86c6181SAlex Tomas 	 */
3052a86c6181SAlex Tomas 	if (inode->i_nlink)
3053a86c6181SAlex Tomas 		ext4_orphan_del(handle, inode);
3054a86c6181SAlex Tomas 
3055ef737728SSolofo Ramangalahy 	inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
3056ef737728SSolofo Ramangalahy 	ext4_mark_inode_dirty(handle, inode);
3057a86c6181SAlex Tomas 	ext4_journal_stop(handle);
3058a86c6181SAlex Tomas }
3059a86c6181SAlex Tomas 
3060fd28784aSAneesh Kumar K.V static void ext4_falloc_update_inode(struct inode *inode,
3061fd28784aSAneesh Kumar K.V 				int mode, loff_t new_size, int update_ctime)
3062fd28784aSAneesh Kumar K.V {
3063fd28784aSAneesh Kumar K.V 	struct timespec now;
3064fd28784aSAneesh Kumar K.V 
3065fd28784aSAneesh Kumar K.V 	if (update_ctime) {
3066fd28784aSAneesh Kumar K.V 		now = current_fs_time(inode->i_sb);
3067fd28784aSAneesh Kumar K.V 		if (!timespec_equal(&inode->i_ctime, &now))
3068fd28784aSAneesh Kumar K.V 			inode->i_ctime = now;
3069fd28784aSAneesh Kumar K.V 	}
3070fd28784aSAneesh Kumar K.V 	/*
3071fd28784aSAneesh Kumar K.V 	 * Update only when preallocation was requested beyond
3072fd28784aSAneesh Kumar K.V 	 * the file size.
3073fd28784aSAneesh Kumar K.V 	 */
3074cf17fea6SAneesh Kumar K.V 	if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3075cf17fea6SAneesh Kumar K.V 		if (new_size > i_size_read(inode))
3076fd28784aSAneesh Kumar K.V 			i_size_write(inode, new_size);
3077cf17fea6SAneesh Kumar K.V 		if (new_size > EXT4_I(inode)->i_disksize)
3078cf17fea6SAneesh Kumar K.V 			ext4_update_i_disksize(inode, new_size);
3079fd28784aSAneesh Kumar K.V 	}
3080fd28784aSAneesh Kumar K.V 
3081fd28784aSAneesh Kumar K.V }
3082fd28784aSAneesh Kumar K.V 
3083a2df2a63SAmit Arora /*
3084a2df2a63SAmit Arora  * preallocate space for a file. This implements ext4's fallocate inode
3085a2df2a63SAmit Arora  * operation, which gets called from sys_fallocate system call.
3086a2df2a63SAmit Arora  * For block-mapped files, posix_fallocate should fall back to the method
3087a2df2a63SAmit Arora  * of writing zeroes to the required new blocks (the same behavior which is
3088a2df2a63SAmit Arora  * expected for file systems which do not support fallocate() system call).
3089a2df2a63SAmit Arora  */
3090a2df2a63SAmit Arora long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
3091a2df2a63SAmit Arora {
3092a2df2a63SAmit Arora 	handle_t *handle;
3093725d26d3SAneesh Kumar K.V 	ext4_lblk_t block;
3094fd28784aSAneesh Kumar K.V 	loff_t new_size;
3095498e5f24STheodore Ts'o 	unsigned int max_blocks;
3096a2df2a63SAmit Arora 	int ret = 0;
3097a2df2a63SAmit Arora 	int ret2 = 0;
3098a2df2a63SAmit Arora 	int retries = 0;
3099a2df2a63SAmit Arora 	struct buffer_head map_bh;
3100a2df2a63SAmit Arora 	unsigned int credits, blkbits = inode->i_blkbits;
3101a2df2a63SAmit Arora 
3102a2df2a63SAmit Arora 	/*
3103a2df2a63SAmit Arora 	 * currently supporting (pre)allocate mode for extent-based
3104a2df2a63SAmit Arora 	 * files _only_
3105a2df2a63SAmit Arora 	 */
3106a2df2a63SAmit Arora 	if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
3107a2df2a63SAmit Arora 		return -EOPNOTSUPP;
3108a2df2a63SAmit Arora 
3109a2df2a63SAmit Arora 	/* preallocation to directories is currently not supported */
3110a2df2a63SAmit Arora 	if (S_ISDIR(inode->i_mode))
3111a2df2a63SAmit Arora 		return -ENODEV;
3112a2df2a63SAmit Arora 
3113a2df2a63SAmit Arora 	block = offset >> blkbits;
3114fd28784aSAneesh Kumar K.V 	/*
3115fd28784aSAneesh Kumar K.V 	 * We can't just convert len to max_blocks because
3116fd28784aSAneesh Kumar K.V 	 * If blocksize = 4096 offset = 3072 and len = 2048
3117fd28784aSAneesh Kumar K.V 	 */
3118a2df2a63SAmit Arora 	max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
3119a2df2a63SAmit Arora 							- block;
3120a2df2a63SAmit Arora 	/*
3121f3bd1f3fSMingming Cao 	 * credits to insert 1 extent into extent tree
3122a2df2a63SAmit Arora 	 */
3123f3bd1f3fSMingming Cao 	credits = ext4_chunk_trans_blocks(inode, max_blocks);
312455bd725aSAneesh Kumar K.V 	mutex_lock(&inode->i_mutex);
3125a2df2a63SAmit Arora retry:
3126a2df2a63SAmit Arora 	while (ret >= 0 && ret < max_blocks) {
3127a2df2a63SAmit Arora 		block = block + ret;
3128a2df2a63SAmit Arora 		max_blocks = max_blocks - ret;
3129a2df2a63SAmit Arora 		handle = ext4_journal_start(inode, credits);
3130a2df2a63SAmit Arora 		if (IS_ERR(handle)) {
3131a2df2a63SAmit Arora 			ret = PTR_ERR(handle);
3132a2df2a63SAmit Arora 			break;
3133a2df2a63SAmit Arora 		}
313479ffab34SAneesh Kumar K.V 		map_bh.b_state = 0;
313512b7ac17STheodore Ts'o 		ret = ext4_get_blocks(handle, inode, block,
3136a2df2a63SAmit Arora 				      max_blocks, &map_bh,
3137c2177057STheodore Ts'o 				      EXT4_GET_BLOCKS_CREATE_UNINIT_EXT);
3138221879c9SAneesh Kumar K.V 		if (ret <= 0) {
31392c98615dSAneesh Kumar K.V #ifdef EXT4FS_DEBUG
31402c98615dSAneesh Kumar K.V 			WARN_ON(ret <= 0);
31412c98615dSAneesh Kumar K.V 			printk(KERN_ERR "%s: ext4_ext_get_blocks "
31422c98615dSAneesh Kumar K.V 				    "returned error inode#%lu, block=%u, "
31439fd9784cSThadeu Lima de Souza Cascardo 				    "max_blocks=%u", __func__,
3144bba90743SEric Sandeen 				    inode->i_ino, block, max_blocks);
31452c98615dSAneesh Kumar K.V #endif
3146a2df2a63SAmit Arora 			ext4_mark_inode_dirty(handle, inode);
3147a2df2a63SAmit Arora 			ret2 = ext4_journal_stop(handle);
3148a2df2a63SAmit Arora 			break;
3149a2df2a63SAmit Arora 		}
3150fd28784aSAneesh Kumar K.V 		if ((block + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
3151fd28784aSAneesh Kumar K.V 						blkbits) >> blkbits))
3152fd28784aSAneesh Kumar K.V 			new_size = offset + len;
3153fd28784aSAneesh Kumar K.V 		else
3154fd28784aSAneesh Kumar K.V 			new_size = (block + ret) << blkbits;
3155a2df2a63SAmit Arora 
3156fd28784aSAneesh Kumar K.V 		ext4_falloc_update_inode(inode, mode, new_size,
3157fd28784aSAneesh Kumar K.V 						buffer_new(&map_bh));
3158a2df2a63SAmit Arora 		ext4_mark_inode_dirty(handle, inode);
3159a2df2a63SAmit Arora 		ret2 = ext4_journal_stop(handle);
3160a2df2a63SAmit Arora 		if (ret2)
3161a2df2a63SAmit Arora 			break;
3162a2df2a63SAmit Arora 	}
3163fd28784aSAneesh Kumar K.V 	if (ret == -ENOSPC &&
3164fd28784aSAneesh Kumar K.V 			ext4_should_retry_alloc(inode->i_sb, &retries)) {
3165fd28784aSAneesh Kumar K.V 		ret = 0;
3166a2df2a63SAmit Arora 		goto retry;
3167a2df2a63SAmit Arora 	}
316855bd725aSAneesh Kumar K.V 	mutex_unlock(&inode->i_mutex);
3169a2df2a63SAmit Arora 	return ret > 0 ? ret2 : ret;
3170a2df2a63SAmit Arora }
31716873fa0dSEric Sandeen 
31726873fa0dSEric Sandeen /*
31736873fa0dSEric Sandeen  * Callback function called for each extent to gather FIEMAP information.
31746873fa0dSEric Sandeen  */
31753a06d778SAneesh Kumar K.V static int ext4_ext_fiemap_cb(struct inode *inode, struct ext4_ext_path *path,
31766873fa0dSEric Sandeen 		       struct ext4_ext_cache *newex, struct ext4_extent *ex,
31776873fa0dSEric Sandeen 		       void *data)
31786873fa0dSEric Sandeen {
31796873fa0dSEric Sandeen 	struct fiemap_extent_info *fieinfo = data;
3180c9877b20SEric Sandeen 	unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
31816873fa0dSEric Sandeen 	__u64	logical;
31826873fa0dSEric Sandeen 	__u64	physical;
31836873fa0dSEric Sandeen 	__u64	length;
31846873fa0dSEric Sandeen 	__u32	flags = 0;
31856873fa0dSEric Sandeen 	int	error;
31866873fa0dSEric Sandeen 
31876873fa0dSEric Sandeen 	logical =  (__u64)newex->ec_block << blksize_bits;
31886873fa0dSEric Sandeen 
31896873fa0dSEric Sandeen 	if (newex->ec_type == EXT4_EXT_CACHE_GAP) {
31906873fa0dSEric Sandeen 		pgoff_t offset;
31916873fa0dSEric Sandeen 		struct page *page;
31926873fa0dSEric Sandeen 		struct buffer_head *bh = NULL;
31936873fa0dSEric Sandeen 
31946873fa0dSEric Sandeen 		offset = logical >> PAGE_SHIFT;
31956873fa0dSEric Sandeen 		page = find_get_page(inode->i_mapping, offset);
31966873fa0dSEric Sandeen 		if (!page || !page_has_buffers(page))
31976873fa0dSEric Sandeen 			return EXT_CONTINUE;
31986873fa0dSEric Sandeen 
31996873fa0dSEric Sandeen 		bh = page_buffers(page);
32006873fa0dSEric Sandeen 
32016873fa0dSEric Sandeen 		if (!bh)
32026873fa0dSEric Sandeen 			return EXT_CONTINUE;
32036873fa0dSEric Sandeen 
32046873fa0dSEric Sandeen 		if (buffer_delay(bh)) {
32056873fa0dSEric Sandeen 			flags |= FIEMAP_EXTENT_DELALLOC;
32066873fa0dSEric Sandeen 			page_cache_release(page);
32076873fa0dSEric Sandeen 		} else {
32086873fa0dSEric Sandeen 			page_cache_release(page);
32096873fa0dSEric Sandeen 			return EXT_CONTINUE;
32106873fa0dSEric Sandeen 		}
32116873fa0dSEric Sandeen 	}
32126873fa0dSEric Sandeen 
32136873fa0dSEric Sandeen 	physical = (__u64)newex->ec_start << blksize_bits;
32146873fa0dSEric Sandeen 	length =   (__u64)newex->ec_len << blksize_bits;
32156873fa0dSEric Sandeen 
32166873fa0dSEric Sandeen 	if (ex && ext4_ext_is_uninitialized(ex))
32176873fa0dSEric Sandeen 		flags |= FIEMAP_EXTENT_UNWRITTEN;
32186873fa0dSEric Sandeen 
32196873fa0dSEric Sandeen 	/*
32206873fa0dSEric Sandeen 	 * If this extent reaches EXT_MAX_BLOCK, it must be last.
32216873fa0dSEric Sandeen 	 *
32226873fa0dSEric Sandeen 	 * Or if ext4_ext_next_allocated_block is EXT_MAX_BLOCK,
32236873fa0dSEric Sandeen 	 * this also indicates no more allocated blocks.
32246873fa0dSEric Sandeen 	 *
32256873fa0dSEric Sandeen 	 * XXX this might miss a single-block extent at EXT_MAX_BLOCK
32266873fa0dSEric Sandeen 	 */
3227c9877b20SEric Sandeen 	if (ext4_ext_next_allocated_block(path) == EXT_MAX_BLOCK ||
3228eefd7f03STheodore Ts'o 	    newex->ec_block + newex->ec_len - 1 == EXT_MAX_BLOCK) {
3229eefd7f03STheodore Ts'o 		loff_t size = i_size_read(inode);
3230eefd7f03STheodore Ts'o 		loff_t bs = EXT4_BLOCK_SIZE(inode->i_sb);
3231eefd7f03STheodore Ts'o 
32326873fa0dSEric Sandeen 		flags |= FIEMAP_EXTENT_LAST;
3233eefd7f03STheodore Ts'o 		if ((flags & FIEMAP_EXTENT_DELALLOC) &&
3234eefd7f03STheodore Ts'o 		    logical+length > size)
3235eefd7f03STheodore Ts'o 			length = (size - logical + bs - 1) & ~(bs-1);
3236eefd7f03STheodore Ts'o 	}
32376873fa0dSEric Sandeen 
32386873fa0dSEric Sandeen 	error = fiemap_fill_next_extent(fieinfo, logical, physical,
32396873fa0dSEric Sandeen 					length, flags);
32406873fa0dSEric Sandeen 	if (error < 0)
32416873fa0dSEric Sandeen 		return error;
32426873fa0dSEric Sandeen 	if (error == 1)
32436873fa0dSEric Sandeen 		return EXT_BREAK;
32446873fa0dSEric Sandeen 
32456873fa0dSEric Sandeen 	return EXT_CONTINUE;
32466873fa0dSEric Sandeen }
32476873fa0dSEric Sandeen 
32486873fa0dSEric Sandeen /* fiemap flags we can handle specified here */
32496873fa0dSEric Sandeen #define EXT4_FIEMAP_FLAGS	(FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
32506873fa0dSEric Sandeen 
32513a06d778SAneesh Kumar K.V static int ext4_xattr_fiemap(struct inode *inode,
32523a06d778SAneesh Kumar K.V 				struct fiemap_extent_info *fieinfo)
32536873fa0dSEric Sandeen {
32546873fa0dSEric Sandeen 	__u64 physical = 0;
32556873fa0dSEric Sandeen 	__u64 length;
32566873fa0dSEric Sandeen 	__u32 flags = FIEMAP_EXTENT_LAST;
32576873fa0dSEric Sandeen 	int blockbits = inode->i_sb->s_blocksize_bits;
32586873fa0dSEric Sandeen 	int error = 0;
32596873fa0dSEric Sandeen 
32606873fa0dSEric Sandeen 	/* in-inode? */
32616873fa0dSEric Sandeen 	if (EXT4_I(inode)->i_state & EXT4_STATE_XATTR) {
32626873fa0dSEric Sandeen 		struct ext4_iloc iloc;
32636873fa0dSEric Sandeen 		int offset;	/* offset of xattr in inode */
32646873fa0dSEric Sandeen 
32656873fa0dSEric Sandeen 		error = ext4_get_inode_loc(inode, &iloc);
32666873fa0dSEric Sandeen 		if (error)
32676873fa0dSEric Sandeen 			return error;
32686873fa0dSEric Sandeen 		physical = iloc.bh->b_blocknr << blockbits;
32696873fa0dSEric Sandeen 		offset = EXT4_GOOD_OLD_INODE_SIZE +
32706873fa0dSEric Sandeen 				EXT4_I(inode)->i_extra_isize;
32716873fa0dSEric Sandeen 		physical += offset;
32726873fa0dSEric Sandeen 		length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
32736873fa0dSEric Sandeen 		flags |= FIEMAP_EXTENT_DATA_INLINE;
32746873fa0dSEric Sandeen 	} else { /* external block */
32756873fa0dSEric Sandeen 		physical = EXT4_I(inode)->i_file_acl << blockbits;
32766873fa0dSEric Sandeen 		length = inode->i_sb->s_blocksize;
32776873fa0dSEric Sandeen 	}
32786873fa0dSEric Sandeen 
32796873fa0dSEric Sandeen 	if (physical)
32806873fa0dSEric Sandeen 		error = fiemap_fill_next_extent(fieinfo, 0, physical,
32816873fa0dSEric Sandeen 						length, flags);
32826873fa0dSEric Sandeen 	return (error < 0 ? error : 0);
32836873fa0dSEric Sandeen }
32846873fa0dSEric Sandeen 
32856873fa0dSEric Sandeen int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
32866873fa0dSEric Sandeen 		__u64 start, __u64 len)
32876873fa0dSEric Sandeen {
32886873fa0dSEric Sandeen 	ext4_lblk_t start_blk;
32896873fa0dSEric Sandeen 	ext4_lblk_t len_blks;
32906873fa0dSEric Sandeen 	int error = 0;
32916873fa0dSEric Sandeen 
32926873fa0dSEric Sandeen 	/* fallback to generic here if not in extents fmt */
32936873fa0dSEric Sandeen 	if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
32946873fa0dSEric Sandeen 		return generic_block_fiemap(inode, fieinfo, start, len,
32956873fa0dSEric Sandeen 			ext4_get_block);
32966873fa0dSEric Sandeen 
32976873fa0dSEric Sandeen 	if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
32986873fa0dSEric Sandeen 		return -EBADR;
32996873fa0dSEric Sandeen 
33006873fa0dSEric Sandeen 	if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
33016873fa0dSEric Sandeen 		error = ext4_xattr_fiemap(inode, fieinfo);
33026873fa0dSEric Sandeen 	} else {
33036873fa0dSEric Sandeen 		start_blk = start >> inode->i_sb->s_blocksize_bits;
33046873fa0dSEric Sandeen 		len_blks = len >> inode->i_sb->s_blocksize_bits;
33056873fa0dSEric Sandeen 
33066873fa0dSEric Sandeen 		/*
33076873fa0dSEric Sandeen 		 * Walk the extent tree gathering extent information.
33086873fa0dSEric Sandeen 		 * ext4_ext_fiemap_cb will push extents back to user.
33096873fa0dSEric Sandeen 		 */
33100568c518STheodore Ts'o 		down_read(&EXT4_I(inode)->i_data_sem);
33116873fa0dSEric Sandeen 		error = ext4_ext_walk_space(inode, start_blk, len_blks,
33126873fa0dSEric Sandeen 					  ext4_ext_fiemap_cb, fieinfo);
33130568c518STheodore Ts'o 		up_read(&EXT4_I(inode)->i_data_sem);
33146873fa0dSEric Sandeen 	}
33156873fa0dSEric Sandeen 
33166873fa0dSEric Sandeen 	return error;
33176873fa0dSEric Sandeen }
33186873fa0dSEric Sandeen 
3319