xref: /openbmc/linux/fs/ext4/extents.c (revision 56b19868aca856a7d7bf20c3a7a1030e4fd75b2b)
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  */
5209b88252SAvantika Mathur static 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 {
32956b19868SAneesh Kumar K.V 	ext4_fsblk_t block = ext_pblock(ext);
33056b19868SAneesh Kumar K.V 	int len = ext4_ext_get_actual_len(ext);
33156b19868SAneesh Kumar K.V 	struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
33256b19868SAneesh Kumar K.V 	if (unlikely(block < le32_to_cpu(es->s_first_data_block) ||
33356b19868SAneesh Kumar K.V 			((block + len) > ext4_blocks_count(es))))
33456b19868SAneesh Kumar K.V 		return 0;
33556b19868SAneesh Kumar K.V 	else
33656b19868SAneesh Kumar K.V 		return 1;
33756b19868SAneesh Kumar K.V }
33856b19868SAneesh Kumar K.V 
33956b19868SAneesh Kumar K.V static int ext4_valid_extent_idx(struct inode *inode,
34056b19868SAneesh Kumar K.V 				struct ext4_extent_idx *ext_idx)
34156b19868SAneesh Kumar K.V {
34256b19868SAneesh Kumar K.V 	ext4_fsblk_t block = idx_pblock(ext_idx);
34356b19868SAneesh Kumar K.V 	struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
34456b19868SAneesh Kumar K.V 	if (unlikely(block < le32_to_cpu(es->s_first_data_block) ||
34556b19868SAneesh Kumar K.V 			(block > ext4_blocks_count(es))))
34656b19868SAneesh Kumar K.V 		return 0;
34756b19868SAneesh Kumar K.V 	else
34856b19868SAneesh Kumar K.V 		return 1;
34956b19868SAneesh Kumar K.V }
35056b19868SAneesh Kumar K.V 
35156b19868SAneesh Kumar K.V static int ext4_valid_extent_entries(struct inode *inode,
35256b19868SAneesh Kumar K.V 				struct ext4_extent_header *eh,
35356b19868SAneesh Kumar K.V 				int depth)
35456b19868SAneesh Kumar K.V {
35556b19868SAneesh Kumar K.V 	struct ext4_extent *ext;
35656b19868SAneesh Kumar K.V 	struct ext4_extent_idx *ext_idx;
35756b19868SAneesh Kumar K.V 	unsigned short entries;
35856b19868SAneesh Kumar K.V 	if (eh->eh_entries == 0)
35956b19868SAneesh Kumar K.V 		return 1;
36056b19868SAneesh Kumar K.V 
36156b19868SAneesh Kumar K.V 	entries = le16_to_cpu(eh->eh_entries);
36256b19868SAneesh Kumar K.V 
36356b19868SAneesh Kumar K.V 	if (depth == 0) {
36456b19868SAneesh Kumar K.V 		/* leaf entries */
36556b19868SAneesh Kumar K.V 		ext = EXT_FIRST_EXTENT(eh);
36656b19868SAneesh Kumar K.V 		while (entries) {
36756b19868SAneesh Kumar K.V 			if (!ext4_valid_extent(inode, ext))
36856b19868SAneesh Kumar K.V 				return 0;
36956b19868SAneesh Kumar K.V 			ext++;
37056b19868SAneesh Kumar K.V 			entries--;
37156b19868SAneesh Kumar K.V 		}
37256b19868SAneesh Kumar K.V 	} else {
37356b19868SAneesh Kumar K.V 		ext_idx = EXT_FIRST_INDEX(eh);
37456b19868SAneesh Kumar K.V 		while (entries) {
37556b19868SAneesh Kumar K.V 			if (!ext4_valid_extent_idx(inode, ext_idx))
37656b19868SAneesh Kumar K.V 				return 0;
37756b19868SAneesh Kumar K.V 			ext_idx++;
37856b19868SAneesh Kumar K.V 			entries--;
37956b19868SAneesh Kumar K.V 		}
38056b19868SAneesh Kumar K.V 	}
38156b19868SAneesh Kumar K.V 	return 1;
38256b19868SAneesh Kumar K.V }
38356b19868SAneesh Kumar K.V 
38456b19868SAneesh Kumar K.V static int __ext4_ext_check(const char *function, struct inode *inode,
385c29c0ae7SAlex Tomas 					struct ext4_extent_header *eh,
386c29c0ae7SAlex Tomas 					int depth)
387c29c0ae7SAlex Tomas {
388c29c0ae7SAlex Tomas 	const char *error_msg;
389c29c0ae7SAlex Tomas 	int max = 0;
390c29c0ae7SAlex Tomas 
391c29c0ae7SAlex Tomas 	if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
392c29c0ae7SAlex Tomas 		error_msg = "invalid magic";
393c29c0ae7SAlex Tomas 		goto corrupted;
394c29c0ae7SAlex Tomas 	}
395c29c0ae7SAlex Tomas 	if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
396c29c0ae7SAlex Tomas 		error_msg = "unexpected eh_depth";
397c29c0ae7SAlex Tomas 		goto corrupted;
398c29c0ae7SAlex Tomas 	}
399c29c0ae7SAlex Tomas 	if (unlikely(eh->eh_max == 0)) {
400c29c0ae7SAlex Tomas 		error_msg = "invalid eh_max";
401c29c0ae7SAlex Tomas 		goto corrupted;
402c29c0ae7SAlex Tomas 	}
403c29c0ae7SAlex Tomas 	max = ext4_ext_max_entries(inode, depth);
404c29c0ae7SAlex Tomas 	if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
405c29c0ae7SAlex Tomas 		error_msg = "too large eh_max";
406c29c0ae7SAlex Tomas 		goto corrupted;
407c29c0ae7SAlex Tomas 	}
408c29c0ae7SAlex Tomas 	if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
409c29c0ae7SAlex Tomas 		error_msg = "invalid eh_entries";
410c29c0ae7SAlex Tomas 		goto corrupted;
411c29c0ae7SAlex Tomas 	}
41256b19868SAneesh Kumar K.V 	if (!ext4_valid_extent_entries(inode, eh, depth)) {
41356b19868SAneesh Kumar K.V 		error_msg = "invalid extent entries";
41456b19868SAneesh Kumar K.V 		goto corrupted;
41556b19868SAneesh Kumar K.V 	}
416c29c0ae7SAlex Tomas 	return 0;
417c29c0ae7SAlex Tomas 
418c29c0ae7SAlex Tomas corrupted:
419c29c0ae7SAlex Tomas 	ext4_error(inode->i_sb, function,
42056b19868SAneesh Kumar K.V 			"bad header/extent in inode #%lu: %s - magic %x, "
421c29c0ae7SAlex Tomas 			"entries %u, max %u(%u), depth %u(%u)",
422c29c0ae7SAlex Tomas 			inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic),
423c29c0ae7SAlex Tomas 			le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
424c29c0ae7SAlex Tomas 			max, le16_to_cpu(eh->eh_depth), depth);
425c29c0ae7SAlex Tomas 
426c29c0ae7SAlex Tomas 	return -EIO;
427c29c0ae7SAlex Tomas }
428c29c0ae7SAlex Tomas 
42956b19868SAneesh Kumar K.V #define ext4_ext_check(inode, eh, depth)	\
43056b19868SAneesh Kumar K.V 	__ext4_ext_check(__func__, inode, eh, depth)
431c29c0ae7SAlex Tomas 
432a86c6181SAlex Tomas #ifdef EXT_DEBUG
433a86c6181SAlex Tomas static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
434a86c6181SAlex Tomas {
435a86c6181SAlex Tomas 	int k, l = path->p_depth;
436a86c6181SAlex Tomas 
437a86c6181SAlex Tomas 	ext_debug("path:");
438a86c6181SAlex Tomas 	for (k = 0; k <= l; k++, path++) {
439a86c6181SAlex Tomas 		if (path->p_idx) {
4402ae02107SMingming Cao 		  ext_debug("  %d->%llu", le32_to_cpu(path->p_idx->ei_block),
441f65e6fbaSAlex Tomas 			    idx_pblock(path->p_idx));
442a86c6181SAlex Tomas 		} else if (path->p_ext) {
4432ae02107SMingming Cao 			ext_debug("  %d:%d:%llu ",
444a86c6181SAlex Tomas 				  le32_to_cpu(path->p_ext->ee_block),
445a2df2a63SAmit Arora 				  ext4_ext_get_actual_len(path->p_ext),
446f65e6fbaSAlex Tomas 				  ext_pblock(path->p_ext));
447a86c6181SAlex Tomas 		} else
448a86c6181SAlex Tomas 			ext_debug("  []");
449a86c6181SAlex Tomas 	}
450a86c6181SAlex Tomas 	ext_debug("\n");
451a86c6181SAlex Tomas }
452a86c6181SAlex Tomas 
453a86c6181SAlex Tomas static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
454a86c6181SAlex Tomas {
455a86c6181SAlex Tomas 	int depth = ext_depth(inode);
456a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
457a86c6181SAlex Tomas 	struct ext4_extent *ex;
458a86c6181SAlex Tomas 	int i;
459a86c6181SAlex Tomas 
460a86c6181SAlex Tomas 	if (!path)
461a86c6181SAlex Tomas 		return;
462a86c6181SAlex Tomas 
463a86c6181SAlex Tomas 	eh = path[depth].p_hdr;
464a86c6181SAlex Tomas 	ex = EXT_FIRST_EXTENT(eh);
465a86c6181SAlex Tomas 
466a86c6181SAlex Tomas 	for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
4672ae02107SMingming Cao 		ext_debug("%d:%d:%llu ", le32_to_cpu(ex->ee_block),
468a2df2a63SAmit Arora 			  ext4_ext_get_actual_len(ex), ext_pblock(ex));
469a86c6181SAlex Tomas 	}
470a86c6181SAlex Tomas 	ext_debug("\n");
471a86c6181SAlex Tomas }
472a86c6181SAlex Tomas #else
473a86c6181SAlex Tomas #define ext4_ext_show_path(inode, path)
474a86c6181SAlex Tomas #define ext4_ext_show_leaf(inode, path)
475a86c6181SAlex Tomas #endif
476a86c6181SAlex Tomas 
477b35905c1SAneesh Kumar K.V void ext4_ext_drop_refs(struct ext4_ext_path *path)
478a86c6181SAlex Tomas {
479a86c6181SAlex Tomas 	int depth = path->p_depth;
480a86c6181SAlex Tomas 	int i;
481a86c6181SAlex Tomas 
482a86c6181SAlex Tomas 	for (i = 0; i <= depth; i++, path++)
483a86c6181SAlex Tomas 		if (path->p_bh) {
484a86c6181SAlex Tomas 			brelse(path->p_bh);
485a86c6181SAlex Tomas 			path->p_bh = NULL;
486a86c6181SAlex Tomas 		}
487a86c6181SAlex Tomas }
488a86c6181SAlex Tomas 
489a86c6181SAlex Tomas /*
490d0d856e8SRandy Dunlap  * ext4_ext_binsearch_idx:
491d0d856e8SRandy Dunlap  * binary search for the closest index of the given block
492c29c0ae7SAlex Tomas  * the header must be checked before calling this
493a86c6181SAlex Tomas  */
494a86c6181SAlex Tomas static void
495725d26d3SAneesh Kumar K.V ext4_ext_binsearch_idx(struct inode *inode,
496725d26d3SAneesh Kumar K.V 			struct ext4_ext_path *path, ext4_lblk_t block)
497a86c6181SAlex Tomas {
498a86c6181SAlex Tomas 	struct ext4_extent_header *eh = path->p_hdr;
499a86c6181SAlex Tomas 	struct ext4_extent_idx *r, *l, *m;
500a86c6181SAlex Tomas 
501a86c6181SAlex Tomas 
502bba90743SEric Sandeen 	ext_debug("binsearch for %u(idx):  ", block);
503a86c6181SAlex Tomas 
504a86c6181SAlex Tomas 	l = EXT_FIRST_INDEX(eh) + 1;
505e9f410b1SDmitry Monakhov 	r = EXT_LAST_INDEX(eh);
506a86c6181SAlex Tomas 	while (l <= r) {
507a86c6181SAlex Tomas 		m = l + (r - l) / 2;
508a86c6181SAlex Tomas 		if (block < le32_to_cpu(m->ei_block))
509a86c6181SAlex Tomas 			r = m - 1;
510a86c6181SAlex Tomas 		else
511a86c6181SAlex Tomas 			l = m + 1;
51226d535edSDmitry Monakhov 		ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
51326d535edSDmitry Monakhov 				m, le32_to_cpu(m->ei_block),
51426d535edSDmitry Monakhov 				r, le32_to_cpu(r->ei_block));
515a86c6181SAlex Tomas 	}
516a86c6181SAlex Tomas 
517a86c6181SAlex Tomas 	path->p_idx = l - 1;
518f65e6fbaSAlex Tomas 	ext_debug("  -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
51926d535edSDmitry Monakhov 		  idx_pblock(path->p_idx));
520a86c6181SAlex Tomas 
521a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH
522a86c6181SAlex Tomas 	{
523a86c6181SAlex Tomas 		struct ext4_extent_idx *chix, *ix;
524a86c6181SAlex Tomas 		int k;
525a86c6181SAlex Tomas 
526a86c6181SAlex Tomas 		chix = ix = EXT_FIRST_INDEX(eh);
527a86c6181SAlex Tomas 		for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
528a86c6181SAlex Tomas 		  if (k != 0 &&
529a86c6181SAlex Tomas 		      le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
5304776004fSTheodore Ts'o 				printk(KERN_DEBUG "k=%d, ix=0x%p, "
5314776004fSTheodore Ts'o 				       "first=0x%p\n", k,
532a86c6181SAlex Tomas 				       ix, EXT_FIRST_INDEX(eh));
5334776004fSTheodore Ts'o 				printk(KERN_DEBUG "%u <= %u\n",
534a86c6181SAlex Tomas 				       le32_to_cpu(ix->ei_block),
535a86c6181SAlex Tomas 				       le32_to_cpu(ix[-1].ei_block));
536a86c6181SAlex Tomas 			}
537a86c6181SAlex Tomas 			BUG_ON(k && le32_to_cpu(ix->ei_block)
538a86c6181SAlex Tomas 					   <= le32_to_cpu(ix[-1].ei_block));
539a86c6181SAlex Tomas 			if (block < le32_to_cpu(ix->ei_block))
540a86c6181SAlex Tomas 				break;
541a86c6181SAlex Tomas 			chix = ix;
542a86c6181SAlex Tomas 		}
543a86c6181SAlex Tomas 		BUG_ON(chix != path->p_idx);
544a86c6181SAlex Tomas 	}
545a86c6181SAlex Tomas #endif
546a86c6181SAlex Tomas 
547a86c6181SAlex Tomas }
548a86c6181SAlex Tomas 
549a86c6181SAlex Tomas /*
550d0d856e8SRandy Dunlap  * ext4_ext_binsearch:
551d0d856e8SRandy Dunlap  * binary search for closest extent of the given block
552c29c0ae7SAlex Tomas  * the header must be checked before calling this
553a86c6181SAlex Tomas  */
554a86c6181SAlex Tomas static void
555725d26d3SAneesh Kumar K.V ext4_ext_binsearch(struct inode *inode,
556725d26d3SAneesh Kumar K.V 		struct ext4_ext_path *path, ext4_lblk_t block)
557a86c6181SAlex Tomas {
558a86c6181SAlex Tomas 	struct ext4_extent_header *eh = path->p_hdr;
559a86c6181SAlex Tomas 	struct ext4_extent *r, *l, *m;
560a86c6181SAlex Tomas 
561a86c6181SAlex Tomas 	if (eh->eh_entries == 0) {
562a86c6181SAlex Tomas 		/*
563d0d856e8SRandy Dunlap 		 * this leaf is empty:
564a86c6181SAlex Tomas 		 * we get such a leaf in split/add case
565a86c6181SAlex Tomas 		 */
566a86c6181SAlex Tomas 		return;
567a86c6181SAlex Tomas 	}
568a86c6181SAlex Tomas 
569bba90743SEric Sandeen 	ext_debug("binsearch for %u:  ", block);
570a86c6181SAlex Tomas 
571a86c6181SAlex Tomas 	l = EXT_FIRST_EXTENT(eh) + 1;
572e9f410b1SDmitry Monakhov 	r = EXT_LAST_EXTENT(eh);
573a86c6181SAlex Tomas 
574a86c6181SAlex Tomas 	while (l <= r) {
575a86c6181SAlex Tomas 		m = l + (r - l) / 2;
576a86c6181SAlex Tomas 		if (block < le32_to_cpu(m->ee_block))
577a86c6181SAlex Tomas 			r = m - 1;
578a86c6181SAlex Tomas 		else
579a86c6181SAlex Tomas 			l = m + 1;
58026d535edSDmitry Monakhov 		ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
58126d535edSDmitry Monakhov 				m, le32_to_cpu(m->ee_block),
58226d535edSDmitry Monakhov 				r, le32_to_cpu(r->ee_block));
583a86c6181SAlex Tomas 	}
584a86c6181SAlex Tomas 
585a86c6181SAlex Tomas 	path->p_ext = l - 1;
5862ae02107SMingming Cao 	ext_debug("  -> %d:%llu:%d ",
587a86c6181SAlex Tomas 			le32_to_cpu(path->p_ext->ee_block),
588f65e6fbaSAlex Tomas 			ext_pblock(path->p_ext),
589a2df2a63SAmit Arora 			ext4_ext_get_actual_len(path->p_ext));
590a86c6181SAlex Tomas 
591a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH
592a86c6181SAlex Tomas 	{
593a86c6181SAlex Tomas 		struct ext4_extent *chex, *ex;
594a86c6181SAlex Tomas 		int k;
595a86c6181SAlex Tomas 
596a86c6181SAlex Tomas 		chex = ex = EXT_FIRST_EXTENT(eh);
597a86c6181SAlex Tomas 		for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
598a86c6181SAlex Tomas 			BUG_ON(k && le32_to_cpu(ex->ee_block)
599a86c6181SAlex Tomas 					  <= le32_to_cpu(ex[-1].ee_block));
600a86c6181SAlex Tomas 			if (block < le32_to_cpu(ex->ee_block))
601a86c6181SAlex Tomas 				break;
602a86c6181SAlex Tomas 			chex = ex;
603a86c6181SAlex Tomas 		}
604a86c6181SAlex Tomas 		BUG_ON(chex != path->p_ext);
605a86c6181SAlex Tomas 	}
606a86c6181SAlex Tomas #endif
607a86c6181SAlex Tomas 
608a86c6181SAlex Tomas }
609a86c6181SAlex Tomas 
610a86c6181SAlex Tomas int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
611a86c6181SAlex Tomas {
612a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
613a86c6181SAlex Tomas 
614a86c6181SAlex Tomas 	eh = ext_inode_hdr(inode);
615a86c6181SAlex Tomas 	eh->eh_depth = 0;
616a86c6181SAlex Tomas 	eh->eh_entries = 0;
617a86c6181SAlex Tomas 	eh->eh_magic = EXT4_EXT_MAGIC;
618a86c6181SAlex Tomas 	eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode));
619a86c6181SAlex Tomas 	ext4_mark_inode_dirty(handle, inode);
620a86c6181SAlex Tomas 	ext4_ext_invalidate_cache(inode);
621a86c6181SAlex Tomas 	return 0;
622a86c6181SAlex Tomas }
623a86c6181SAlex Tomas 
624a86c6181SAlex Tomas struct ext4_ext_path *
625725d26d3SAneesh Kumar K.V ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
626725d26d3SAneesh Kumar K.V 					struct ext4_ext_path *path)
627a86c6181SAlex Tomas {
628a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
629a86c6181SAlex Tomas 	struct buffer_head *bh;
630a86c6181SAlex Tomas 	short int depth, i, ppos = 0, alloc = 0;
631a86c6181SAlex Tomas 
632a86c6181SAlex Tomas 	eh = ext_inode_hdr(inode);
633c29c0ae7SAlex Tomas 	depth = ext_depth(inode);
63456b19868SAneesh Kumar K.V 	if (ext4_ext_check(inode, eh, depth))
635a86c6181SAlex Tomas 		return ERR_PTR(-EIO);
636a86c6181SAlex Tomas 
637a86c6181SAlex Tomas 
638a86c6181SAlex Tomas 	/* account possible depth increase */
639a86c6181SAlex Tomas 	if (!path) {
6405d4958f9SAvantika Mathur 		path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
641a86c6181SAlex Tomas 				GFP_NOFS);
642a86c6181SAlex Tomas 		if (!path)
643a86c6181SAlex Tomas 			return ERR_PTR(-ENOMEM);
644a86c6181SAlex Tomas 		alloc = 1;
645a86c6181SAlex Tomas 	}
646a86c6181SAlex Tomas 	path[0].p_hdr = eh;
6471973adcbSShen Feng 	path[0].p_bh = NULL;
648a86c6181SAlex Tomas 
649c29c0ae7SAlex Tomas 	i = depth;
650a86c6181SAlex Tomas 	/* walk through the tree */
651a86c6181SAlex Tomas 	while (i) {
652a86c6181SAlex Tomas 		ext_debug("depth %d: num %d, max %d\n",
653a86c6181SAlex Tomas 			  ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
654c29c0ae7SAlex Tomas 
655a86c6181SAlex Tomas 		ext4_ext_binsearch_idx(inode, path + ppos, block);
656f65e6fbaSAlex Tomas 		path[ppos].p_block = idx_pblock(path[ppos].p_idx);
657a86c6181SAlex Tomas 		path[ppos].p_depth = i;
658a86c6181SAlex Tomas 		path[ppos].p_ext = NULL;
659a86c6181SAlex Tomas 
660a86c6181SAlex Tomas 		bh = sb_bread(inode->i_sb, path[ppos].p_block);
661a86c6181SAlex Tomas 		if (!bh)
662a86c6181SAlex Tomas 			goto err;
663a86c6181SAlex Tomas 
664a86c6181SAlex Tomas 		eh = ext_block_hdr(bh);
665a86c6181SAlex Tomas 		ppos++;
666a86c6181SAlex Tomas 		BUG_ON(ppos > depth);
667a86c6181SAlex Tomas 		path[ppos].p_bh = bh;
668a86c6181SAlex Tomas 		path[ppos].p_hdr = eh;
669a86c6181SAlex Tomas 		i--;
670a86c6181SAlex Tomas 
67156b19868SAneesh Kumar K.V 		if (ext4_ext_check(inode, eh, i))
672a86c6181SAlex Tomas 			goto err;
673a86c6181SAlex Tomas 	}
674a86c6181SAlex Tomas 
675a86c6181SAlex Tomas 	path[ppos].p_depth = i;
676a86c6181SAlex Tomas 	path[ppos].p_ext = NULL;
677a86c6181SAlex Tomas 	path[ppos].p_idx = NULL;
678a86c6181SAlex Tomas 
679a86c6181SAlex Tomas 	/* find extent */
680a86c6181SAlex Tomas 	ext4_ext_binsearch(inode, path + ppos, block);
6811973adcbSShen Feng 	/* if not an empty leaf */
6821973adcbSShen Feng 	if (path[ppos].p_ext)
6831973adcbSShen Feng 		path[ppos].p_block = ext_pblock(path[ppos].p_ext);
684a86c6181SAlex Tomas 
685a86c6181SAlex Tomas 	ext4_ext_show_path(inode, path);
686a86c6181SAlex Tomas 
687a86c6181SAlex Tomas 	return path;
688a86c6181SAlex Tomas 
689a86c6181SAlex Tomas err:
690a86c6181SAlex Tomas 	ext4_ext_drop_refs(path);
691a86c6181SAlex Tomas 	if (alloc)
692a86c6181SAlex Tomas 		kfree(path);
693a86c6181SAlex Tomas 	return ERR_PTR(-EIO);
694a86c6181SAlex Tomas }
695a86c6181SAlex Tomas 
696a86c6181SAlex Tomas /*
697d0d856e8SRandy Dunlap  * ext4_ext_insert_index:
698d0d856e8SRandy Dunlap  * insert new index [@logical;@ptr] into the block at @curp;
699d0d856e8SRandy Dunlap  * check where to insert: before @curp or after @curp
700a86c6181SAlex Tomas  */
701a86c6181SAlex Tomas static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
702a86c6181SAlex Tomas 				struct ext4_ext_path *curp,
703f65e6fbaSAlex Tomas 				int logical, ext4_fsblk_t ptr)
704a86c6181SAlex Tomas {
705a86c6181SAlex Tomas 	struct ext4_extent_idx *ix;
706a86c6181SAlex Tomas 	int len, err;
707a86c6181SAlex Tomas 
7087e028976SAvantika Mathur 	err = ext4_ext_get_access(handle, inode, curp);
7097e028976SAvantika Mathur 	if (err)
710a86c6181SAlex Tomas 		return err;
711a86c6181SAlex Tomas 
712a86c6181SAlex Tomas 	BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block));
713a86c6181SAlex Tomas 	len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
714a86c6181SAlex Tomas 	if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
715a86c6181SAlex Tomas 		/* insert after */
716a86c6181SAlex Tomas 		if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
717a86c6181SAlex Tomas 			len = (len - 1) * sizeof(struct ext4_extent_idx);
718a86c6181SAlex Tomas 			len = len < 0 ? 0 : len;
71926d535edSDmitry Monakhov 			ext_debug("insert new index %d after: %llu. "
720a86c6181SAlex Tomas 					"move %d from 0x%p to 0x%p\n",
721a86c6181SAlex Tomas 					logical, ptr, len,
722a86c6181SAlex Tomas 					(curp->p_idx + 1), (curp->p_idx + 2));
723a86c6181SAlex Tomas 			memmove(curp->p_idx + 2, curp->p_idx + 1, len);
724a86c6181SAlex Tomas 		}
725a86c6181SAlex Tomas 		ix = curp->p_idx + 1;
726a86c6181SAlex Tomas 	} else {
727a86c6181SAlex Tomas 		/* insert before */
728a86c6181SAlex Tomas 		len = len * sizeof(struct ext4_extent_idx);
729a86c6181SAlex Tomas 		len = len < 0 ? 0 : len;
73026d535edSDmitry Monakhov 		ext_debug("insert new index %d before: %llu. "
731a86c6181SAlex Tomas 				"move %d from 0x%p to 0x%p\n",
732a86c6181SAlex Tomas 				logical, ptr, len,
733a86c6181SAlex Tomas 				curp->p_idx, (curp->p_idx + 1));
734a86c6181SAlex Tomas 		memmove(curp->p_idx + 1, curp->p_idx, len);
735a86c6181SAlex Tomas 		ix = curp->p_idx;
736a86c6181SAlex Tomas 	}
737a86c6181SAlex Tomas 
738a86c6181SAlex Tomas 	ix->ei_block = cpu_to_le32(logical);
739f65e6fbaSAlex Tomas 	ext4_idx_store_pblock(ix, ptr);
740e8546d06SMarcin Slusarz 	le16_add_cpu(&curp->p_hdr->eh_entries, 1);
741a86c6181SAlex Tomas 
742a86c6181SAlex Tomas 	BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries)
743a86c6181SAlex Tomas 			     > le16_to_cpu(curp->p_hdr->eh_max));
744a86c6181SAlex Tomas 	BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr));
745a86c6181SAlex Tomas 
746a86c6181SAlex Tomas 	err = ext4_ext_dirty(handle, inode, curp);
747a86c6181SAlex Tomas 	ext4_std_error(inode->i_sb, err);
748a86c6181SAlex Tomas 
749a86c6181SAlex Tomas 	return err;
750a86c6181SAlex Tomas }
751a86c6181SAlex Tomas 
752a86c6181SAlex Tomas /*
753d0d856e8SRandy Dunlap  * ext4_ext_split:
754d0d856e8SRandy Dunlap  * inserts new subtree into the path, using free index entry
755d0d856e8SRandy Dunlap  * at depth @at:
756a86c6181SAlex Tomas  * - allocates all needed blocks (new leaf and all intermediate index blocks)
757a86c6181SAlex Tomas  * - makes decision where to split
758d0d856e8SRandy Dunlap  * - moves remaining extents and index entries (right to the split point)
759a86c6181SAlex Tomas  *   into the newly allocated blocks
760d0d856e8SRandy Dunlap  * - initializes subtree
761a86c6181SAlex Tomas  */
762a86c6181SAlex Tomas static int ext4_ext_split(handle_t *handle, struct inode *inode,
763a86c6181SAlex Tomas 				struct ext4_ext_path *path,
764a86c6181SAlex Tomas 				struct ext4_extent *newext, int at)
765a86c6181SAlex Tomas {
766a86c6181SAlex Tomas 	struct buffer_head *bh = NULL;
767a86c6181SAlex Tomas 	int depth = ext_depth(inode);
768a86c6181SAlex Tomas 	struct ext4_extent_header *neh;
769a86c6181SAlex Tomas 	struct ext4_extent_idx *fidx;
770a86c6181SAlex Tomas 	struct ext4_extent *ex;
771a86c6181SAlex Tomas 	int i = at, k, m, a;
772f65e6fbaSAlex Tomas 	ext4_fsblk_t newblock, oldblock;
773a86c6181SAlex Tomas 	__le32 border;
774f65e6fbaSAlex Tomas 	ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
775a86c6181SAlex Tomas 	int err = 0;
776a86c6181SAlex Tomas 
777a86c6181SAlex Tomas 	/* make decision: where to split? */
778d0d856e8SRandy Dunlap 	/* FIXME: now decision is simplest: at current extent */
779a86c6181SAlex Tomas 
780d0d856e8SRandy Dunlap 	/* if current leaf will be split, then we should use
781a86c6181SAlex Tomas 	 * border from split point */
782a86c6181SAlex Tomas 	BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr));
783a86c6181SAlex Tomas 	if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
784a86c6181SAlex Tomas 		border = path[depth].p_ext[1].ee_block;
785d0d856e8SRandy Dunlap 		ext_debug("leaf will be split."
786a86c6181SAlex Tomas 				" next leaf starts at %d\n",
787a86c6181SAlex Tomas 				  le32_to_cpu(border));
788a86c6181SAlex Tomas 	} else {
789a86c6181SAlex Tomas 		border = newext->ee_block;
790a86c6181SAlex Tomas 		ext_debug("leaf will be added."
791a86c6181SAlex Tomas 				" next leaf starts at %d\n",
792a86c6181SAlex Tomas 				le32_to_cpu(border));
793a86c6181SAlex Tomas 	}
794a86c6181SAlex Tomas 
795a86c6181SAlex Tomas 	/*
796d0d856e8SRandy Dunlap 	 * If error occurs, then we break processing
797d0d856e8SRandy Dunlap 	 * and mark filesystem read-only. index won't
798a86c6181SAlex Tomas 	 * be inserted and tree will be in consistent
799d0d856e8SRandy Dunlap 	 * state. Next mount will repair buffers too.
800a86c6181SAlex Tomas 	 */
801a86c6181SAlex Tomas 
802a86c6181SAlex Tomas 	/*
803d0d856e8SRandy Dunlap 	 * Get array to track all allocated blocks.
804d0d856e8SRandy Dunlap 	 * We need this to handle errors and free blocks
805d0d856e8SRandy Dunlap 	 * upon them.
806a86c6181SAlex Tomas 	 */
8075d4958f9SAvantika Mathur 	ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
808a86c6181SAlex Tomas 	if (!ablocks)
809a86c6181SAlex Tomas 		return -ENOMEM;
810a86c6181SAlex Tomas 
811a86c6181SAlex Tomas 	/* allocate all needed blocks */
812a86c6181SAlex Tomas 	ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
813a86c6181SAlex Tomas 	for (a = 0; a < depth - at; a++) {
814654b4908SAneesh Kumar K.V 		newblock = ext4_ext_new_meta_block(handle, inode, path,
815654b4908SAneesh Kumar K.V 						   newext, &err);
816a86c6181SAlex Tomas 		if (newblock == 0)
817a86c6181SAlex Tomas 			goto cleanup;
818a86c6181SAlex Tomas 		ablocks[a] = newblock;
819a86c6181SAlex Tomas 	}
820a86c6181SAlex Tomas 
821a86c6181SAlex Tomas 	/* initialize new leaf */
822a86c6181SAlex Tomas 	newblock = ablocks[--a];
823a86c6181SAlex Tomas 	BUG_ON(newblock == 0);
824a86c6181SAlex Tomas 	bh = sb_getblk(inode->i_sb, newblock);
825a86c6181SAlex Tomas 	if (!bh) {
826a86c6181SAlex Tomas 		err = -EIO;
827a86c6181SAlex Tomas 		goto cleanup;
828a86c6181SAlex Tomas 	}
829a86c6181SAlex Tomas 	lock_buffer(bh);
830a86c6181SAlex Tomas 
8317e028976SAvantika Mathur 	err = ext4_journal_get_create_access(handle, bh);
8327e028976SAvantika Mathur 	if (err)
833a86c6181SAlex Tomas 		goto cleanup;
834a86c6181SAlex Tomas 
835a86c6181SAlex Tomas 	neh = ext_block_hdr(bh);
836a86c6181SAlex Tomas 	neh->eh_entries = 0;
837a86c6181SAlex Tomas 	neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
838a86c6181SAlex Tomas 	neh->eh_magic = EXT4_EXT_MAGIC;
839a86c6181SAlex Tomas 	neh->eh_depth = 0;
840a86c6181SAlex Tomas 	ex = EXT_FIRST_EXTENT(neh);
841a86c6181SAlex Tomas 
842d0d856e8SRandy Dunlap 	/* move remainder of path[depth] to the new leaf */
843a86c6181SAlex Tomas 	BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max);
844a86c6181SAlex Tomas 	/* start copy from next extent */
845a86c6181SAlex Tomas 	/* TODO: we could do it by single memmove */
846a86c6181SAlex Tomas 	m = 0;
847a86c6181SAlex Tomas 	path[depth].p_ext++;
848a86c6181SAlex Tomas 	while (path[depth].p_ext <=
849a86c6181SAlex Tomas 			EXT_MAX_EXTENT(path[depth].p_hdr)) {
8502ae02107SMingming Cao 		ext_debug("move %d:%llu:%d in new leaf %llu\n",
851a86c6181SAlex Tomas 				le32_to_cpu(path[depth].p_ext->ee_block),
852f65e6fbaSAlex Tomas 				ext_pblock(path[depth].p_ext),
853a2df2a63SAmit Arora 				ext4_ext_get_actual_len(path[depth].p_ext),
854a86c6181SAlex Tomas 				newblock);
855a86c6181SAlex Tomas 		/*memmove(ex++, path[depth].p_ext++,
856a86c6181SAlex Tomas 				sizeof(struct ext4_extent));
857a86c6181SAlex Tomas 		neh->eh_entries++;*/
858a86c6181SAlex Tomas 		path[depth].p_ext++;
859a86c6181SAlex Tomas 		m++;
860a86c6181SAlex Tomas 	}
861a86c6181SAlex Tomas 	if (m) {
862a86c6181SAlex Tomas 		memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
863e8546d06SMarcin Slusarz 		le16_add_cpu(&neh->eh_entries, m);
864a86c6181SAlex Tomas 	}
865a86c6181SAlex Tomas 
866a86c6181SAlex Tomas 	set_buffer_uptodate(bh);
867a86c6181SAlex Tomas 	unlock_buffer(bh);
868a86c6181SAlex Tomas 
8690390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, inode, bh);
8707e028976SAvantika Mathur 	if (err)
871a86c6181SAlex Tomas 		goto cleanup;
872a86c6181SAlex Tomas 	brelse(bh);
873a86c6181SAlex Tomas 	bh = NULL;
874a86c6181SAlex Tomas 
875a86c6181SAlex Tomas 	/* correct old leaf */
876a86c6181SAlex Tomas 	if (m) {
8777e028976SAvantika Mathur 		err = ext4_ext_get_access(handle, inode, path + depth);
8787e028976SAvantika Mathur 		if (err)
879a86c6181SAlex Tomas 			goto cleanup;
880e8546d06SMarcin Slusarz 		le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
8817e028976SAvantika Mathur 		err = ext4_ext_dirty(handle, inode, path + depth);
8827e028976SAvantika Mathur 		if (err)
883a86c6181SAlex Tomas 			goto cleanup;
884a86c6181SAlex Tomas 
885a86c6181SAlex Tomas 	}
886a86c6181SAlex Tomas 
887a86c6181SAlex Tomas 	/* create intermediate indexes */
888a86c6181SAlex Tomas 	k = depth - at - 1;
889a86c6181SAlex Tomas 	BUG_ON(k < 0);
890a86c6181SAlex Tomas 	if (k)
891a86c6181SAlex Tomas 		ext_debug("create %d intermediate indices\n", k);
892a86c6181SAlex Tomas 	/* insert new index into current index block */
893a86c6181SAlex Tomas 	/* current depth stored in i var */
894a86c6181SAlex Tomas 	i = depth - 1;
895a86c6181SAlex Tomas 	while (k--) {
896a86c6181SAlex Tomas 		oldblock = newblock;
897a86c6181SAlex Tomas 		newblock = ablocks[--a];
898bba90743SEric Sandeen 		bh = sb_getblk(inode->i_sb, newblock);
899a86c6181SAlex Tomas 		if (!bh) {
900a86c6181SAlex Tomas 			err = -EIO;
901a86c6181SAlex Tomas 			goto cleanup;
902a86c6181SAlex Tomas 		}
903a86c6181SAlex Tomas 		lock_buffer(bh);
904a86c6181SAlex Tomas 
9057e028976SAvantika Mathur 		err = ext4_journal_get_create_access(handle, bh);
9067e028976SAvantika Mathur 		if (err)
907a86c6181SAlex Tomas 			goto cleanup;
908a86c6181SAlex Tomas 
909a86c6181SAlex Tomas 		neh = ext_block_hdr(bh);
910a86c6181SAlex Tomas 		neh->eh_entries = cpu_to_le16(1);
911a86c6181SAlex Tomas 		neh->eh_magic = EXT4_EXT_MAGIC;
912a86c6181SAlex Tomas 		neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
913a86c6181SAlex Tomas 		neh->eh_depth = cpu_to_le16(depth - i);
914a86c6181SAlex Tomas 		fidx = EXT_FIRST_INDEX(neh);
915a86c6181SAlex Tomas 		fidx->ei_block = border;
916f65e6fbaSAlex Tomas 		ext4_idx_store_pblock(fidx, oldblock);
917a86c6181SAlex Tomas 
918bba90743SEric Sandeen 		ext_debug("int.index at %d (block %llu): %u -> %llu\n",
919bba90743SEric Sandeen 				i, newblock, le32_to_cpu(border), oldblock);
920a86c6181SAlex Tomas 		/* copy indexes */
921a86c6181SAlex Tomas 		m = 0;
922a86c6181SAlex Tomas 		path[i].p_idx++;
923a86c6181SAlex Tomas 
924a86c6181SAlex Tomas 		ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
925a86c6181SAlex Tomas 				EXT_MAX_INDEX(path[i].p_hdr));
926a86c6181SAlex Tomas 		BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) !=
927a86c6181SAlex Tomas 				EXT_LAST_INDEX(path[i].p_hdr));
928a86c6181SAlex Tomas 		while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
92926d535edSDmitry Monakhov 			ext_debug("%d: move %d:%llu in new index %llu\n", i,
930a86c6181SAlex Tomas 					le32_to_cpu(path[i].p_idx->ei_block),
931f65e6fbaSAlex Tomas 					idx_pblock(path[i].p_idx),
932a86c6181SAlex Tomas 					newblock);
933a86c6181SAlex Tomas 			/*memmove(++fidx, path[i].p_idx++,
934a86c6181SAlex Tomas 					sizeof(struct ext4_extent_idx));
935a86c6181SAlex Tomas 			neh->eh_entries++;
936a86c6181SAlex Tomas 			BUG_ON(neh->eh_entries > neh->eh_max);*/
937a86c6181SAlex Tomas 			path[i].p_idx++;
938a86c6181SAlex Tomas 			m++;
939a86c6181SAlex Tomas 		}
940a86c6181SAlex Tomas 		if (m) {
941a86c6181SAlex Tomas 			memmove(++fidx, path[i].p_idx - m,
942a86c6181SAlex Tomas 				sizeof(struct ext4_extent_idx) * m);
943e8546d06SMarcin Slusarz 			le16_add_cpu(&neh->eh_entries, m);
944a86c6181SAlex Tomas 		}
945a86c6181SAlex Tomas 		set_buffer_uptodate(bh);
946a86c6181SAlex Tomas 		unlock_buffer(bh);
947a86c6181SAlex Tomas 
9480390131bSFrank Mayhar 		err = ext4_handle_dirty_metadata(handle, inode, bh);
9497e028976SAvantika Mathur 		if (err)
950a86c6181SAlex Tomas 			goto cleanup;
951a86c6181SAlex Tomas 		brelse(bh);
952a86c6181SAlex Tomas 		bh = NULL;
953a86c6181SAlex Tomas 
954a86c6181SAlex Tomas 		/* correct old index */
955a86c6181SAlex Tomas 		if (m) {
956a86c6181SAlex Tomas 			err = ext4_ext_get_access(handle, inode, path + i);
957a86c6181SAlex Tomas 			if (err)
958a86c6181SAlex Tomas 				goto cleanup;
959e8546d06SMarcin Slusarz 			le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
960a86c6181SAlex Tomas 			err = ext4_ext_dirty(handle, inode, path + i);
961a86c6181SAlex Tomas 			if (err)
962a86c6181SAlex Tomas 				goto cleanup;
963a86c6181SAlex Tomas 		}
964a86c6181SAlex Tomas 
965a86c6181SAlex Tomas 		i--;
966a86c6181SAlex Tomas 	}
967a86c6181SAlex Tomas 
968a86c6181SAlex Tomas 	/* insert new index */
969a86c6181SAlex Tomas 	err = ext4_ext_insert_index(handle, inode, path + at,
970a86c6181SAlex Tomas 				    le32_to_cpu(border), newblock);
971a86c6181SAlex Tomas 
972a86c6181SAlex Tomas cleanup:
973a86c6181SAlex Tomas 	if (bh) {
974a86c6181SAlex Tomas 		if (buffer_locked(bh))
975a86c6181SAlex Tomas 			unlock_buffer(bh);
976a86c6181SAlex Tomas 		brelse(bh);
977a86c6181SAlex Tomas 	}
978a86c6181SAlex Tomas 
979a86c6181SAlex Tomas 	if (err) {
980a86c6181SAlex Tomas 		/* free all allocated blocks in error case */
981a86c6181SAlex Tomas 		for (i = 0; i < depth; i++) {
982a86c6181SAlex Tomas 			if (!ablocks[i])
983a86c6181SAlex Tomas 				continue;
984c9de560dSAlex Tomas 			ext4_free_blocks(handle, inode, ablocks[i], 1, 1);
985a86c6181SAlex Tomas 		}
986a86c6181SAlex Tomas 	}
987a86c6181SAlex Tomas 	kfree(ablocks);
988a86c6181SAlex Tomas 
989a86c6181SAlex Tomas 	return err;
990a86c6181SAlex Tomas }
991a86c6181SAlex Tomas 
992a86c6181SAlex Tomas /*
993d0d856e8SRandy Dunlap  * ext4_ext_grow_indepth:
994d0d856e8SRandy Dunlap  * implements tree growing procedure:
995a86c6181SAlex Tomas  * - allocates new block
996a86c6181SAlex Tomas  * - moves top-level data (index block or leaf) into the new block
997d0d856e8SRandy Dunlap  * - initializes new top-level, creating index that points to the
998a86c6181SAlex Tomas  *   just created block
999a86c6181SAlex Tomas  */
1000a86c6181SAlex Tomas static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
1001a86c6181SAlex Tomas 					struct ext4_ext_path *path,
1002a86c6181SAlex Tomas 					struct ext4_extent *newext)
1003a86c6181SAlex Tomas {
1004a86c6181SAlex Tomas 	struct ext4_ext_path *curp = path;
1005a86c6181SAlex Tomas 	struct ext4_extent_header *neh;
1006a86c6181SAlex Tomas 	struct ext4_extent_idx *fidx;
1007a86c6181SAlex Tomas 	struct buffer_head *bh;
1008f65e6fbaSAlex Tomas 	ext4_fsblk_t newblock;
1009a86c6181SAlex Tomas 	int err = 0;
1010a86c6181SAlex Tomas 
1011654b4908SAneesh Kumar K.V 	newblock = ext4_ext_new_meta_block(handle, inode, path, newext, &err);
1012a86c6181SAlex Tomas 	if (newblock == 0)
1013a86c6181SAlex Tomas 		return err;
1014a86c6181SAlex Tomas 
1015a86c6181SAlex Tomas 	bh = sb_getblk(inode->i_sb, newblock);
1016a86c6181SAlex Tomas 	if (!bh) {
1017a86c6181SAlex Tomas 		err = -EIO;
1018a86c6181SAlex Tomas 		ext4_std_error(inode->i_sb, err);
1019a86c6181SAlex Tomas 		return err;
1020a86c6181SAlex Tomas 	}
1021a86c6181SAlex Tomas 	lock_buffer(bh);
1022a86c6181SAlex Tomas 
10237e028976SAvantika Mathur 	err = ext4_journal_get_create_access(handle, bh);
10247e028976SAvantika Mathur 	if (err) {
1025a86c6181SAlex Tomas 		unlock_buffer(bh);
1026a86c6181SAlex Tomas 		goto out;
1027a86c6181SAlex Tomas 	}
1028a86c6181SAlex Tomas 
1029a86c6181SAlex Tomas 	/* move top-level index/leaf into new block */
1030a86c6181SAlex Tomas 	memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
1031a86c6181SAlex Tomas 
1032a86c6181SAlex Tomas 	/* set size of new block */
1033a86c6181SAlex Tomas 	neh = ext_block_hdr(bh);
1034a86c6181SAlex Tomas 	/* old root could have indexes or leaves
1035a86c6181SAlex Tomas 	 * so calculate e_max right way */
1036a86c6181SAlex Tomas 	if (ext_depth(inode))
1037a86c6181SAlex Tomas 	  neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
1038a86c6181SAlex Tomas 	else
1039a86c6181SAlex Tomas 	  neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
1040a86c6181SAlex Tomas 	neh->eh_magic = EXT4_EXT_MAGIC;
1041a86c6181SAlex Tomas 	set_buffer_uptodate(bh);
1042a86c6181SAlex Tomas 	unlock_buffer(bh);
1043a86c6181SAlex Tomas 
10440390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, inode, bh);
10457e028976SAvantika Mathur 	if (err)
1046a86c6181SAlex Tomas 		goto out;
1047a86c6181SAlex Tomas 
1048a86c6181SAlex Tomas 	/* create index in new top-level index: num,max,pointer */
10497e028976SAvantika Mathur 	err = ext4_ext_get_access(handle, inode, curp);
10507e028976SAvantika Mathur 	if (err)
1051a86c6181SAlex Tomas 		goto out;
1052a86c6181SAlex Tomas 
1053a86c6181SAlex Tomas 	curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
1054a86c6181SAlex Tomas 	curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode));
1055a86c6181SAlex Tomas 	curp->p_hdr->eh_entries = cpu_to_le16(1);
1056a86c6181SAlex Tomas 	curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
1057e9f410b1SDmitry Monakhov 
1058e9f410b1SDmitry Monakhov 	if (path[0].p_hdr->eh_depth)
1059e9f410b1SDmitry Monakhov 		curp->p_idx->ei_block =
1060e9f410b1SDmitry Monakhov 			EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
1061e9f410b1SDmitry Monakhov 	else
1062e9f410b1SDmitry Monakhov 		curp->p_idx->ei_block =
1063e9f410b1SDmitry Monakhov 			EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
1064f65e6fbaSAlex Tomas 	ext4_idx_store_pblock(curp->p_idx, newblock);
1065a86c6181SAlex Tomas 
1066a86c6181SAlex Tomas 	neh = ext_inode_hdr(inode);
1067a86c6181SAlex Tomas 	fidx = EXT_FIRST_INDEX(neh);
10682ae02107SMingming Cao 	ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
1069a86c6181SAlex Tomas 		  le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
1070f65e6fbaSAlex Tomas 		  le32_to_cpu(fidx->ei_block), idx_pblock(fidx));
1071a86c6181SAlex Tomas 
1072a86c6181SAlex Tomas 	neh->eh_depth = cpu_to_le16(path->p_depth + 1);
1073a86c6181SAlex Tomas 	err = ext4_ext_dirty(handle, inode, curp);
1074a86c6181SAlex Tomas out:
1075a86c6181SAlex Tomas 	brelse(bh);
1076a86c6181SAlex Tomas 
1077a86c6181SAlex Tomas 	return err;
1078a86c6181SAlex Tomas }
1079a86c6181SAlex Tomas 
1080a86c6181SAlex Tomas /*
1081d0d856e8SRandy Dunlap  * ext4_ext_create_new_leaf:
1082d0d856e8SRandy Dunlap  * finds empty index and adds new leaf.
1083d0d856e8SRandy Dunlap  * if no free index is found, then it requests in-depth growing.
1084a86c6181SAlex Tomas  */
1085a86c6181SAlex Tomas static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
1086a86c6181SAlex Tomas 					struct ext4_ext_path *path,
1087a86c6181SAlex Tomas 					struct ext4_extent *newext)
1088a86c6181SAlex Tomas {
1089a86c6181SAlex Tomas 	struct ext4_ext_path *curp;
1090a86c6181SAlex Tomas 	int depth, i, err = 0;
1091a86c6181SAlex Tomas 
1092a86c6181SAlex Tomas repeat:
1093a86c6181SAlex Tomas 	i = depth = ext_depth(inode);
1094a86c6181SAlex Tomas 
1095a86c6181SAlex Tomas 	/* walk up to the tree and look for free index entry */
1096a86c6181SAlex Tomas 	curp = path + depth;
1097a86c6181SAlex Tomas 	while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1098a86c6181SAlex Tomas 		i--;
1099a86c6181SAlex Tomas 		curp--;
1100a86c6181SAlex Tomas 	}
1101a86c6181SAlex Tomas 
1102d0d856e8SRandy Dunlap 	/* we use already allocated block for index block,
1103d0d856e8SRandy Dunlap 	 * so subsequent data blocks should be contiguous */
1104a86c6181SAlex Tomas 	if (EXT_HAS_FREE_INDEX(curp)) {
1105a86c6181SAlex Tomas 		/* if we found index with free entry, then use that
1106a86c6181SAlex Tomas 		 * entry: create all needed subtree and add new leaf */
1107a86c6181SAlex Tomas 		err = ext4_ext_split(handle, inode, path, newext, i);
1108787e0981SShen Feng 		if (err)
1109787e0981SShen Feng 			goto out;
1110a86c6181SAlex Tomas 
1111a86c6181SAlex Tomas 		/* refill path */
1112a86c6181SAlex Tomas 		ext4_ext_drop_refs(path);
1113a86c6181SAlex Tomas 		path = ext4_ext_find_extent(inode,
1114725d26d3SAneesh Kumar K.V 				    (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1115a86c6181SAlex Tomas 				    path);
1116a86c6181SAlex Tomas 		if (IS_ERR(path))
1117a86c6181SAlex Tomas 			err = PTR_ERR(path);
1118a86c6181SAlex Tomas 	} else {
1119a86c6181SAlex Tomas 		/* tree is full, time to grow in depth */
1120a86c6181SAlex Tomas 		err = ext4_ext_grow_indepth(handle, inode, path, newext);
1121a86c6181SAlex Tomas 		if (err)
1122a86c6181SAlex Tomas 			goto out;
1123a86c6181SAlex Tomas 
1124a86c6181SAlex Tomas 		/* refill path */
1125a86c6181SAlex Tomas 		ext4_ext_drop_refs(path);
1126a86c6181SAlex Tomas 		path = ext4_ext_find_extent(inode,
1127725d26d3SAneesh Kumar K.V 				   (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1128a86c6181SAlex Tomas 				    path);
1129a86c6181SAlex Tomas 		if (IS_ERR(path)) {
1130a86c6181SAlex Tomas 			err = PTR_ERR(path);
1131a86c6181SAlex Tomas 			goto out;
1132a86c6181SAlex Tomas 		}
1133a86c6181SAlex Tomas 
1134a86c6181SAlex Tomas 		/*
1135d0d856e8SRandy Dunlap 		 * only first (depth 0 -> 1) produces free space;
1136d0d856e8SRandy Dunlap 		 * in all other cases we have to split the grown tree
1137a86c6181SAlex Tomas 		 */
1138a86c6181SAlex Tomas 		depth = ext_depth(inode);
1139a86c6181SAlex Tomas 		if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
1140d0d856e8SRandy Dunlap 			/* now we need to split */
1141a86c6181SAlex Tomas 			goto repeat;
1142a86c6181SAlex Tomas 		}
1143a86c6181SAlex Tomas 	}
1144a86c6181SAlex Tomas 
1145a86c6181SAlex Tomas out:
1146a86c6181SAlex Tomas 	return err;
1147a86c6181SAlex Tomas }
1148a86c6181SAlex Tomas 
1149a86c6181SAlex Tomas /*
11501988b51eSAlex Tomas  * search the closest allocated block to the left for *logical
11511988b51eSAlex Tomas  * and returns it at @logical + it's physical address at @phys
11521988b51eSAlex Tomas  * if *logical is the smallest allocated block, the function
11531988b51eSAlex Tomas  * returns 0 at @phys
11541988b51eSAlex Tomas  * return value contains 0 (success) or error code
11551988b51eSAlex Tomas  */
11561988b51eSAlex Tomas int
11571988b51eSAlex Tomas ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path,
11581988b51eSAlex Tomas 			ext4_lblk_t *logical, ext4_fsblk_t *phys)
11591988b51eSAlex Tomas {
11601988b51eSAlex Tomas 	struct ext4_extent_idx *ix;
11611988b51eSAlex Tomas 	struct ext4_extent *ex;
1162b939e376SAneesh Kumar K.V 	int depth, ee_len;
11631988b51eSAlex Tomas 
11641988b51eSAlex Tomas 	BUG_ON(path == NULL);
11651988b51eSAlex Tomas 	depth = path->p_depth;
11661988b51eSAlex Tomas 	*phys = 0;
11671988b51eSAlex Tomas 
11681988b51eSAlex Tomas 	if (depth == 0 && path->p_ext == NULL)
11691988b51eSAlex Tomas 		return 0;
11701988b51eSAlex Tomas 
11711988b51eSAlex Tomas 	/* usually extent in the path covers blocks smaller
11721988b51eSAlex Tomas 	 * then *logical, but it can be that extent is the
11731988b51eSAlex Tomas 	 * first one in the file */
11741988b51eSAlex Tomas 
11751988b51eSAlex Tomas 	ex = path[depth].p_ext;
1176b939e376SAneesh Kumar K.V 	ee_len = ext4_ext_get_actual_len(ex);
11771988b51eSAlex Tomas 	if (*logical < le32_to_cpu(ex->ee_block)) {
11781988b51eSAlex Tomas 		BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
11791988b51eSAlex Tomas 		while (--depth >= 0) {
11801988b51eSAlex Tomas 			ix = path[depth].p_idx;
11811988b51eSAlex Tomas 			BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
11821988b51eSAlex Tomas 		}
11831988b51eSAlex Tomas 		return 0;
11841988b51eSAlex Tomas 	}
11851988b51eSAlex Tomas 
1186b939e376SAneesh Kumar K.V 	BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
11871988b51eSAlex Tomas 
1188b939e376SAneesh Kumar K.V 	*logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
1189b939e376SAneesh Kumar K.V 	*phys = ext_pblock(ex) + ee_len - 1;
11901988b51eSAlex Tomas 	return 0;
11911988b51eSAlex Tomas }
11921988b51eSAlex Tomas 
11931988b51eSAlex Tomas /*
11941988b51eSAlex Tomas  * search the closest allocated block to the right for *logical
11951988b51eSAlex Tomas  * and returns it at @logical + it's physical address at @phys
11961988b51eSAlex Tomas  * if *logical is the smallest allocated block, the function
11971988b51eSAlex Tomas  * returns 0 at @phys
11981988b51eSAlex Tomas  * return value contains 0 (success) or error code
11991988b51eSAlex Tomas  */
12001988b51eSAlex Tomas int
12011988b51eSAlex Tomas ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path,
12021988b51eSAlex Tomas 			ext4_lblk_t *logical, ext4_fsblk_t *phys)
12031988b51eSAlex Tomas {
12041988b51eSAlex Tomas 	struct buffer_head *bh = NULL;
12051988b51eSAlex Tomas 	struct ext4_extent_header *eh;
12061988b51eSAlex Tomas 	struct ext4_extent_idx *ix;
12071988b51eSAlex Tomas 	struct ext4_extent *ex;
12081988b51eSAlex Tomas 	ext4_fsblk_t block;
1209*395a87bfSEric Sandeen 	int depth;	/* Note, NOT eh_depth; depth from top of tree */
1210*395a87bfSEric Sandeen 	int ee_len;
12111988b51eSAlex Tomas 
12121988b51eSAlex Tomas 	BUG_ON(path == NULL);
12131988b51eSAlex Tomas 	depth = path->p_depth;
12141988b51eSAlex Tomas 	*phys = 0;
12151988b51eSAlex Tomas 
12161988b51eSAlex Tomas 	if (depth == 0 && path->p_ext == NULL)
12171988b51eSAlex Tomas 		return 0;
12181988b51eSAlex Tomas 
12191988b51eSAlex Tomas 	/* usually extent in the path covers blocks smaller
12201988b51eSAlex Tomas 	 * then *logical, but it can be that extent is the
12211988b51eSAlex Tomas 	 * first one in the file */
12221988b51eSAlex Tomas 
12231988b51eSAlex Tomas 	ex = path[depth].p_ext;
1224b939e376SAneesh Kumar K.V 	ee_len = ext4_ext_get_actual_len(ex);
12251988b51eSAlex Tomas 	if (*logical < le32_to_cpu(ex->ee_block)) {
12261988b51eSAlex Tomas 		BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
12271988b51eSAlex Tomas 		while (--depth >= 0) {
12281988b51eSAlex Tomas 			ix = path[depth].p_idx;
12291988b51eSAlex Tomas 			BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
12301988b51eSAlex Tomas 		}
12311988b51eSAlex Tomas 		*logical = le32_to_cpu(ex->ee_block);
12321988b51eSAlex Tomas 		*phys = ext_pblock(ex);
12331988b51eSAlex Tomas 		return 0;
12341988b51eSAlex Tomas 	}
12351988b51eSAlex Tomas 
1236b939e376SAneesh Kumar K.V 	BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
12371988b51eSAlex Tomas 
12381988b51eSAlex Tomas 	if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
12391988b51eSAlex Tomas 		/* next allocated block in this leaf */
12401988b51eSAlex Tomas 		ex++;
12411988b51eSAlex Tomas 		*logical = le32_to_cpu(ex->ee_block);
12421988b51eSAlex Tomas 		*phys = ext_pblock(ex);
12431988b51eSAlex Tomas 		return 0;
12441988b51eSAlex Tomas 	}
12451988b51eSAlex Tomas 
12461988b51eSAlex Tomas 	/* go up and search for index to the right */
12471988b51eSAlex Tomas 	while (--depth >= 0) {
12481988b51eSAlex Tomas 		ix = path[depth].p_idx;
12491988b51eSAlex Tomas 		if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
125025f1ee3aSWu Fengguang 			goto got_index;
12511988b51eSAlex Tomas 	}
12521988b51eSAlex Tomas 
125325f1ee3aSWu Fengguang 	/* we've gone up to the root and found no index to the right */
12541988b51eSAlex Tomas 	return 0;
12551988b51eSAlex Tomas 
125625f1ee3aSWu Fengguang got_index:
12571988b51eSAlex Tomas 	/* we've found index to the right, let's
12581988b51eSAlex Tomas 	 * follow it and find the closest allocated
12591988b51eSAlex Tomas 	 * block to the right */
12601988b51eSAlex Tomas 	ix++;
12611988b51eSAlex Tomas 	block = idx_pblock(ix);
12621988b51eSAlex Tomas 	while (++depth < path->p_depth) {
12631988b51eSAlex Tomas 		bh = sb_bread(inode->i_sb, block);
12641988b51eSAlex Tomas 		if (bh == NULL)
12651988b51eSAlex Tomas 			return -EIO;
12661988b51eSAlex Tomas 		eh = ext_block_hdr(bh);
1267*395a87bfSEric Sandeen 		/* subtract from p_depth to get proper eh_depth */
126856b19868SAneesh Kumar K.V 		if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
12691988b51eSAlex Tomas 			put_bh(bh);
12701988b51eSAlex Tomas 			return -EIO;
12711988b51eSAlex Tomas 		}
12721988b51eSAlex Tomas 		ix = EXT_FIRST_INDEX(eh);
12731988b51eSAlex Tomas 		block = idx_pblock(ix);
12741988b51eSAlex Tomas 		put_bh(bh);
12751988b51eSAlex Tomas 	}
12761988b51eSAlex Tomas 
12771988b51eSAlex Tomas 	bh = sb_bread(inode->i_sb, block);
12781988b51eSAlex Tomas 	if (bh == NULL)
12791988b51eSAlex Tomas 		return -EIO;
12801988b51eSAlex Tomas 	eh = ext_block_hdr(bh);
128156b19868SAneesh Kumar K.V 	if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
12821988b51eSAlex Tomas 		put_bh(bh);
12831988b51eSAlex Tomas 		return -EIO;
12841988b51eSAlex Tomas 	}
12851988b51eSAlex Tomas 	ex = EXT_FIRST_EXTENT(eh);
12861988b51eSAlex Tomas 	*logical = le32_to_cpu(ex->ee_block);
12871988b51eSAlex Tomas 	*phys = ext_pblock(ex);
12881988b51eSAlex Tomas 	put_bh(bh);
12891988b51eSAlex Tomas 	return 0;
12901988b51eSAlex Tomas }
12911988b51eSAlex Tomas 
12921988b51eSAlex Tomas /*
1293d0d856e8SRandy Dunlap  * ext4_ext_next_allocated_block:
1294d0d856e8SRandy Dunlap  * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1295d0d856e8SRandy Dunlap  * NOTE: it considers block number from index entry as
1296d0d856e8SRandy Dunlap  * allocated block. Thus, index entries have to be consistent
1297d0d856e8SRandy Dunlap  * with leaves.
1298a86c6181SAlex Tomas  */
1299725d26d3SAneesh Kumar K.V static ext4_lblk_t
1300a86c6181SAlex Tomas ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1301a86c6181SAlex Tomas {
1302a86c6181SAlex Tomas 	int depth;
1303a86c6181SAlex Tomas 
1304a86c6181SAlex Tomas 	BUG_ON(path == NULL);
1305a86c6181SAlex Tomas 	depth = path->p_depth;
1306a86c6181SAlex Tomas 
1307a86c6181SAlex Tomas 	if (depth == 0 && path->p_ext == NULL)
1308a86c6181SAlex Tomas 		return EXT_MAX_BLOCK;
1309a86c6181SAlex Tomas 
1310a86c6181SAlex Tomas 	while (depth >= 0) {
1311a86c6181SAlex Tomas 		if (depth == path->p_depth) {
1312a86c6181SAlex Tomas 			/* leaf */
1313a86c6181SAlex Tomas 			if (path[depth].p_ext !=
1314a86c6181SAlex Tomas 					EXT_LAST_EXTENT(path[depth].p_hdr))
1315a86c6181SAlex Tomas 			  return le32_to_cpu(path[depth].p_ext[1].ee_block);
1316a86c6181SAlex Tomas 		} else {
1317a86c6181SAlex Tomas 			/* index */
1318a86c6181SAlex Tomas 			if (path[depth].p_idx !=
1319a86c6181SAlex Tomas 					EXT_LAST_INDEX(path[depth].p_hdr))
1320a86c6181SAlex Tomas 			  return le32_to_cpu(path[depth].p_idx[1].ei_block);
1321a86c6181SAlex Tomas 		}
1322a86c6181SAlex Tomas 		depth--;
1323a86c6181SAlex Tomas 	}
1324a86c6181SAlex Tomas 
1325a86c6181SAlex Tomas 	return EXT_MAX_BLOCK;
1326a86c6181SAlex Tomas }
1327a86c6181SAlex Tomas 
1328a86c6181SAlex Tomas /*
1329d0d856e8SRandy Dunlap  * ext4_ext_next_leaf_block:
1330a86c6181SAlex Tomas  * returns first allocated block from next leaf or EXT_MAX_BLOCK
1331a86c6181SAlex Tomas  */
1332725d26d3SAneesh Kumar K.V static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode,
1333a86c6181SAlex Tomas 					struct ext4_ext_path *path)
1334a86c6181SAlex Tomas {
1335a86c6181SAlex Tomas 	int depth;
1336a86c6181SAlex Tomas 
1337a86c6181SAlex Tomas 	BUG_ON(path == NULL);
1338a86c6181SAlex Tomas 	depth = path->p_depth;
1339a86c6181SAlex Tomas 
1340a86c6181SAlex Tomas 	/* zero-tree has no leaf blocks at all */
1341a86c6181SAlex Tomas 	if (depth == 0)
1342a86c6181SAlex Tomas 		return EXT_MAX_BLOCK;
1343a86c6181SAlex Tomas 
1344a86c6181SAlex Tomas 	/* go to index block */
1345a86c6181SAlex Tomas 	depth--;
1346a86c6181SAlex Tomas 
1347a86c6181SAlex Tomas 	while (depth >= 0) {
1348a86c6181SAlex Tomas 		if (path[depth].p_idx !=
1349a86c6181SAlex Tomas 				EXT_LAST_INDEX(path[depth].p_hdr))
1350725d26d3SAneesh Kumar K.V 			return (ext4_lblk_t)
1351725d26d3SAneesh Kumar K.V 				le32_to_cpu(path[depth].p_idx[1].ei_block);
1352a86c6181SAlex Tomas 		depth--;
1353a86c6181SAlex Tomas 	}
1354a86c6181SAlex Tomas 
1355a86c6181SAlex Tomas 	return EXT_MAX_BLOCK;
1356a86c6181SAlex Tomas }
1357a86c6181SAlex Tomas 
1358a86c6181SAlex Tomas /*
1359d0d856e8SRandy Dunlap  * ext4_ext_correct_indexes:
1360d0d856e8SRandy Dunlap  * if leaf gets modified and modified extent is first in the leaf,
1361d0d856e8SRandy Dunlap  * then we have to correct all indexes above.
1362a86c6181SAlex Tomas  * TODO: do we need to correct tree in all cases?
1363a86c6181SAlex Tomas  */
13641d03ec98SAneesh Kumar K.V static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
1365a86c6181SAlex Tomas 				struct ext4_ext_path *path)
1366a86c6181SAlex Tomas {
1367a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
1368a86c6181SAlex Tomas 	int depth = ext_depth(inode);
1369a86c6181SAlex Tomas 	struct ext4_extent *ex;
1370a86c6181SAlex Tomas 	__le32 border;
1371a86c6181SAlex Tomas 	int k, err = 0;
1372a86c6181SAlex Tomas 
1373a86c6181SAlex Tomas 	eh = path[depth].p_hdr;
1374a86c6181SAlex Tomas 	ex = path[depth].p_ext;
1375a86c6181SAlex Tomas 	BUG_ON(ex == NULL);
1376a86c6181SAlex Tomas 	BUG_ON(eh == NULL);
1377a86c6181SAlex Tomas 
1378a86c6181SAlex Tomas 	if (depth == 0) {
1379a86c6181SAlex Tomas 		/* there is no tree at all */
1380a86c6181SAlex Tomas 		return 0;
1381a86c6181SAlex Tomas 	}
1382a86c6181SAlex Tomas 
1383a86c6181SAlex Tomas 	if (ex != EXT_FIRST_EXTENT(eh)) {
1384a86c6181SAlex Tomas 		/* we correct tree if first leaf got modified only */
1385a86c6181SAlex Tomas 		return 0;
1386a86c6181SAlex Tomas 	}
1387a86c6181SAlex Tomas 
1388a86c6181SAlex Tomas 	/*
1389d0d856e8SRandy Dunlap 	 * TODO: we need correction if border is smaller than current one
1390a86c6181SAlex Tomas 	 */
1391a86c6181SAlex Tomas 	k = depth - 1;
1392a86c6181SAlex Tomas 	border = path[depth].p_ext->ee_block;
13937e028976SAvantika Mathur 	err = ext4_ext_get_access(handle, inode, path + k);
13947e028976SAvantika Mathur 	if (err)
1395a86c6181SAlex Tomas 		return err;
1396a86c6181SAlex Tomas 	path[k].p_idx->ei_block = border;
13977e028976SAvantika Mathur 	err = ext4_ext_dirty(handle, inode, path + k);
13987e028976SAvantika Mathur 	if (err)
1399a86c6181SAlex Tomas 		return err;
1400a86c6181SAlex Tomas 
1401a86c6181SAlex Tomas 	while (k--) {
1402a86c6181SAlex Tomas 		/* change all left-side indexes */
1403a86c6181SAlex Tomas 		if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1404a86c6181SAlex Tomas 			break;
14057e028976SAvantika Mathur 		err = ext4_ext_get_access(handle, inode, path + k);
14067e028976SAvantika Mathur 		if (err)
1407a86c6181SAlex Tomas 			break;
1408a86c6181SAlex Tomas 		path[k].p_idx->ei_block = border;
14097e028976SAvantika Mathur 		err = ext4_ext_dirty(handle, inode, path + k);
14107e028976SAvantika Mathur 		if (err)
1411a86c6181SAlex Tomas 			break;
1412a86c6181SAlex Tomas 	}
1413a86c6181SAlex Tomas 
1414a86c6181SAlex Tomas 	return err;
1415a86c6181SAlex Tomas }
1416a86c6181SAlex Tomas 
141709b88252SAvantika Mathur static int
1418a86c6181SAlex Tomas ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1419a86c6181SAlex Tomas 				struct ext4_extent *ex2)
1420a86c6181SAlex Tomas {
1421749269faSAmit Arora 	unsigned short ext1_ee_len, ext2_ee_len, max_len;
1422a2df2a63SAmit Arora 
1423a2df2a63SAmit Arora 	/*
1424a2df2a63SAmit Arora 	 * Make sure that either both extents are uninitialized, or
1425a2df2a63SAmit Arora 	 * both are _not_.
1426a2df2a63SAmit Arora 	 */
1427a2df2a63SAmit Arora 	if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1428a2df2a63SAmit Arora 		return 0;
1429a2df2a63SAmit Arora 
1430749269faSAmit Arora 	if (ext4_ext_is_uninitialized(ex1))
1431749269faSAmit Arora 		max_len = EXT_UNINIT_MAX_LEN;
1432749269faSAmit Arora 	else
1433749269faSAmit Arora 		max_len = EXT_INIT_MAX_LEN;
1434749269faSAmit Arora 
1435a2df2a63SAmit Arora 	ext1_ee_len = ext4_ext_get_actual_len(ex1);
1436a2df2a63SAmit Arora 	ext2_ee_len = ext4_ext_get_actual_len(ex2);
1437a2df2a63SAmit Arora 
1438a2df2a63SAmit Arora 	if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
143963f57933SAndrew Morton 			le32_to_cpu(ex2->ee_block))
1440a86c6181SAlex Tomas 		return 0;
1441a86c6181SAlex Tomas 
1442471d4011SSuparna Bhattacharya 	/*
1443471d4011SSuparna Bhattacharya 	 * To allow future support for preallocated extents to be added
1444471d4011SSuparna Bhattacharya 	 * as an RO_COMPAT feature, refuse to merge to extents if
1445d0d856e8SRandy Dunlap 	 * this can result in the top bit of ee_len being set.
1446471d4011SSuparna Bhattacharya 	 */
1447749269faSAmit Arora 	if (ext1_ee_len + ext2_ee_len > max_len)
1448471d4011SSuparna Bhattacharya 		return 0;
1449bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST
1450b939e376SAneesh Kumar K.V 	if (ext1_ee_len >= 4)
1451a86c6181SAlex Tomas 		return 0;
1452a86c6181SAlex Tomas #endif
1453a86c6181SAlex Tomas 
1454a2df2a63SAmit Arora 	if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2))
1455a86c6181SAlex Tomas 		return 1;
1456a86c6181SAlex Tomas 	return 0;
1457a86c6181SAlex Tomas }
1458a86c6181SAlex Tomas 
1459a86c6181SAlex Tomas /*
146056055d3aSAmit Arora  * This function tries to merge the "ex" extent to the next extent in the tree.
146156055d3aSAmit Arora  * It always tries to merge towards right. If you want to merge towards
146256055d3aSAmit Arora  * left, pass "ex - 1" as argument instead of "ex".
146356055d3aSAmit Arora  * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
146456055d3aSAmit Arora  * 1 if they got merged.
146556055d3aSAmit Arora  */
146656055d3aSAmit Arora int ext4_ext_try_to_merge(struct inode *inode,
146756055d3aSAmit Arora 			  struct ext4_ext_path *path,
146856055d3aSAmit Arora 			  struct ext4_extent *ex)
146956055d3aSAmit Arora {
147056055d3aSAmit Arora 	struct ext4_extent_header *eh;
147156055d3aSAmit Arora 	unsigned int depth, len;
147256055d3aSAmit Arora 	int merge_done = 0;
147356055d3aSAmit Arora 	int uninitialized = 0;
147456055d3aSAmit Arora 
147556055d3aSAmit Arora 	depth = ext_depth(inode);
147656055d3aSAmit Arora 	BUG_ON(path[depth].p_hdr == NULL);
147756055d3aSAmit Arora 	eh = path[depth].p_hdr;
147856055d3aSAmit Arora 
147956055d3aSAmit Arora 	while (ex < EXT_LAST_EXTENT(eh)) {
148056055d3aSAmit Arora 		if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
148156055d3aSAmit Arora 			break;
148256055d3aSAmit Arora 		/* merge with next extent! */
148356055d3aSAmit Arora 		if (ext4_ext_is_uninitialized(ex))
148456055d3aSAmit Arora 			uninitialized = 1;
148556055d3aSAmit Arora 		ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
148656055d3aSAmit Arora 				+ ext4_ext_get_actual_len(ex + 1));
148756055d3aSAmit Arora 		if (uninitialized)
148856055d3aSAmit Arora 			ext4_ext_mark_uninitialized(ex);
148956055d3aSAmit Arora 
149056055d3aSAmit Arora 		if (ex + 1 < EXT_LAST_EXTENT(eh)) {
149156055d3aSAmit Arora 			len = (EXT_LAST_EXTENT(eh) - ex - 1)
149256055d3aSAmit Arora 				* sizeof(struct ext4_extent);
149356055d3aSAmit Arora 			memmove(ex + 1, ex + 2, len);
149456055d3aSAmit Arora 		}
1495e8546d06SMarcin Slusarz 		le16_add_cpu(&eh->eh_entries, -1);
149656055d3aSAmit Arora 		merge_done = 1;
149756055d3aSAmit Arora 		WARN_ON(eh->eh_entries == 0);
149856055d3aSAmit Arora 		if (!eh->eh_entries)
149956055d3aSAmit Arora 			ext4_error(inode->i_sb, "ext4_ext_try_to_merge",
150056055d3aSAmit Arora 			   "inode#%lu, eh->eh_entries = 0!", inode->i_ino);
150156055d3aSAmit Arora 	}
150256055d3aSAmit Arora 
150356055d3aSAmit Arora 	return merge_done;
150456055d3aSAmit Arora }
150556055d3aSAmit Arora 
150656055d3aSAmit Arora /*
150725d14f98SAmit Arora  * check if a portion of the "newext" extent overlaps with an
150825d14f98SAmit Arora  * existing extent.
150925d14f98SAmit Arora  *
151025d14f98SAmit Arora  * If there is an overlap discovered, it updates the length of the newext
151125d14f98SAmit Arora  * such that there will be no overlap, and then returns 1.
151225d14f98SAmit Arora  * If there is no overlap found, it returns 0.
151325d14f98SAmit Arora  */
151425d14f98SAmit Arora unsigned int ext4_ext_check_overlap(struct inode *inode,
151525d14f98SAmit Arora 				    struct ext4_extent *newext,
151625d14f98SAmit Arora 				    struct ext4_ext_path *path)
151725d14f98SAmit Arora {
1518725d26d3SAneesh Kumar K.V 	ext4_lblk_t b1, b2;
151925d14f98SAmit Arora 	unsigned int depth, len1;
152025d14f98SAmit Arora 	unsigned int ret = 0;
152125d14f98SAmit Arora 
152225d14f98SAmit Arora 	b1 = le32_to_cpu(newext->ee_block);
1523a2df2a63SAmit Arora 	len1 = ext4_ext_get_actual_len(newext);
152425d14f98SAmit Arora 	depth = ext_depth(inode);
152525d14f98SAmit Arora 	if (!path[depth].p_ext)
152625d14f98SAmit Arora 		goto out;
152725d14f98SAmit Arora 	b2 = le32_to_cpu(path[depth].p_ext->ee_block);
152825d14f98SAmit Arora 
152925d14f98SAmit Arora 	/*
153025d14f98SAmit Arora 	 * get the next allocated block if the extent in the path
153125d14f98SAmit Arora 	 * is before the requested block(s)
153225d14f98SAmit Arora 	 */
153325d14f98SAmit Arora 	if (b2 < b1) {
153425d14f98SAmit Arora 		b2 = ext4_ext_next_allocated_block(path);
153525d14f98SAmit Arora 		if (b2 == EXT_MAX_BLOCK)
153625d14f98SAmit Arora 			goto out;
153725d14f98SAmit Arora 	}
153825d14f98SAmit Arora 
1539725d26d3SAneesh Kumar K.V 	/* check for wrap through zero on extent logical start block*/
154025d14f98SAmit Arora 	if (b1 + len1 < b1) {
154125d14f98SAmit Arora 		len1 = EXT_MAX_BLOCK - b1;
154225d14f98SAmit Arora 		newext->ee_len = cpu_to_le16(len1);
154325d14f98SAmit Arora 		ret = 1;
154425d14f98SAmit Arora 	}
154525d14f98SAmit Arora 
154625d14f98SAmit Arora 	/* check for overlap */
154725d14f98SAmit Arora 	if (b1 + len1 > b2) {
154825d14f98SAmit Arora 		newext->ee_len = cpu_to_le16(b2 - b1);
154925d14f98SAmit Arora 		ret = 1;
155025d14f98SAmit Arora 	}
155125d14f98SAmit Arora out:
155225d14f98SAmit Arora 	return ret;
155325d14f98SAmit Arora }
155425d14f98SAmit Arora 
155525d14f98SAmit Arora /*
1556d0d856e8SRandy Dunlap  * ext4_ext_insert_extent:
1557d0d856e8SRandy Dunlap  * tries to merge requsted extent into the existing extent or
1558d0d856e8SRandy Dunlap  * inserts requested extent as new one into the tree,
1559d0d856e8SRandy Dunlap  * creating new leaf in the no-space case.
1560a86c6181SAlex Tomas  */
1561a86c6181SAlex Tomas int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1562a86c6181SAlex Tomas 				struct ext4_ext_path *path,
1563a86c6181SAlex Tomas 				struct ext4_extent *newext)
1564a86c6181SAlex Tomas {
1565a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
1566a86c6181SAlex Tomas 	struct ext4_extent *ex, *fex;
1567a86c6181SAlex Tomas 	struct ext4_extent *nearex; /* nearest extent */
1568a86c6181SAlex Tomas 	struct ext4_ext_path *npath = NULL;
1569725d26d3SAneesh Kumar K.V 	int depth, len, err;
1570725d26d3SAneesh Kumar K.V 	ext4_lblk_t next;
1571a2df2a63SAmit Arora 	unsigned uninitialized = 0;
1572a86c6181SAlex Tomas 
1573a2df2a63SAmit Arora 	BUG_ON(ext4_ext_get_actual_len(newext) == 0);
1574a86c6181SAlex Tomas 	depth = ext_depth(inode);
1575a86c6181SAlex Tomas 	ex = path[depth].p_ext;
1576a86c6181SAlex Tomas 	BUG_ON(path[depth].p_hdr == NULL);
1577a86c6181SAlex Tomas 
1578a86c6181SAlex Tomas 	/* try to insert block into found extent and return */
1579a86c6181SAlex Tomas 	if (ex && ext4_can_extents_be_merged(inode, ex, newext)) {
15802ae02107SMingming Cao 		ext_debug("append %d block to %d:%d (from %llu)\n",
1581a2df2a63SAmit Arora 				ext4_ext_get_actual_len(newext),
1582a86c6181SAlex Tomas 				le32_to_cpu(ex->ee_block),
1583a2df2a63SAmit Arora 				ext4_ext_get_actual_len(ex), ext_pblock(ex));
15847e028976SAvantika Mathur 		err = ext4_ext_get_access(handle, inode, path + depth);
15857e028976SAvantika Mathur 		if (err)
1586a86c6181SAlex Tomas 			return err;
1587a2df2a63SAmit Arora 
1588a2df2a63SAmit Arora 		/*
1589a2df2a63SAmit Arora 		 * ext4_can_extents_be_merged should have checked that either
1590a2df2a63SAmit Arora 		 * both extents are uninitialized, or both aren't. Thus we
1591a2df2a63SAmit Arora 		 * need to check only one of them here.
1592a2df2a63SAmit Arora 		 */
1593a2df2a63SAmit Arora 		if (ext4_ext_is_uninitialized(ex))
1594a2df2a63SAmit Arora 			uninitialized = 1;
1595a2df2a63SAmit Arora 		ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1596a2df2a63SAmit Arora 					+ ext4_ext_get_actual_len(newext));
1597a2df2a63SAmit Arora 		if (uninitialized)
1598a2df2a63SAmit Arora 			ext4_ext_mark_uninitialized(ex);
1599a86c6181SAlex Tomas 		eh = path[depth].p_hdr;
1600a86c6181SAlex Tomas 		nearex = ex;
1601a86c6181SAlex Tomas 		goto merge;
1602a86c6181SAlex Tomas 	}
1603a86c6181SAlex Tomas 
1604a86c6181SAlex Tomas repeat:
1605a86c6181SAlex Tomas 	depth = ext_depth(inode);
1606a86c6181SAlex Tomas 	eh = path[depth].p_hdr;
1607a86c6181SAlex Tomas 	if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1608a86c6181SAlex Tomas 		goto has_space;
1609a86c6181SAlex Tomas 
1610a86c6181SAlex Tomas 	/* probably next leaf has space for us? */
1611a86c6181SAlex Tomas 	fex = EXT_LAST_EXTENT(eh);
1612a86c6181SAlex Tomas 	next = ext4_ext_next_leaf_block(inode, path);
1613a86c6181SAlex Tomas 	if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
1614a86c6181SAlex Tomas 	    && next != EXT_MAX_BLOCK) {
1615a86c6181SAlex Tomas 		ext_debug("next leaf block - %d\n", next);
1616a86c6181SAlex Tomas 		BUG_ON(npath != NULL);
1617a86c6181SAlex Tomas 		npath = ext4_ext_find_extent(inode, next, NULL);
1618a86c6181SAlex Tomas 		if (IS_ERR(npath))
1619a86c6181SAlex Tomas 			return PTR_ERR(npath);
1620a86c6181SAlex Tomas 		BUG_ON(npath->p_depth != path->p_depth);
1621a86c6181SAlex Tomas 		eh = npath[depth].p_hdr;
1622a86c6181SAlex Tomas 		if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
1623a86c6181SAlex Tomas 			ext_debug("next leaf isnt full(%d)\n",
1624a86c6181SAlex Tomas 				  le16_to_cpu(eh->eh_entries));
1625a86c6181SAlex Tomas 			path = npath;
1626a86c6181SAlex Tomas 			goto repeat;
1627a86c6181SAlex Tomas 		}
1628a86c6181SAlex Tomas 		ext_debug("next leaf has no free space(%d,%d)\n",
1629a86c6181SAlex Tomas 			  le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1630a86c6181SAlex Tomas 	}
1631a86c6181SAlex Tomas 
1632a86c6181SAlex Tomas 	/*
1633d0d856e8SRandy Dunlap 	 * There is no free space in the found leaf.
1634d0d856e8SRandy Dunlap 	 * We're gonna add a new leaf in the tree.
1635a86c6181SAlex Tomas 	 */
1636a86c6181SAlex Tomas 	err = ext4_ext_create_new_leaf(handle, inode, path, newext);
1637a86c6181SAlex Tomas 	if (err)
1638a86c6181SAlex Tomas 		goto cleanup;
1639a86c6181SAlex Tomas 	depth = ext_depth(inode);
1640a86c6181SAlex Tomas 	eh = path[depth].p_hdr;
1641a86c6181SAlex Tomas 
1642a86c6181SAlex Tomas has_space:
1643a86c6181SAlex Tomas 	nearex = path[depth].p_ext;
1644a86c6181SAlex Tomas 
16457e028976SAvantika Mathur 	err = ext4_ext_get_access(handle, inode, path + depth);
16467e028976SAvantika Mathur 	if (err)
1647a86c6181SAlex Tomas 		goto cleanup;
1648a86c6181SAlex Tomas 
1649a86c6181SAlex Tomas 	if (!nearex) {
1650a86c6181SAlex Tomas 		/* there is no extent in this leaf, create first one */
16512ae02107SMingming Cao 		ext_debug("first extent in the leaf: %d:%llu:%d\n",
1652a86c6181SAlex Tomas 				le32_to_cpu(newext->ee_block),
1653f65e6fbaSAlex Tomas 				ext_pblock(newext),
1654a2df2a63SAmit Arora 				ext4_ext_get_actual_len(newext));
1655a86c6181SAlex Tomas 		path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1656a86c6181SAlex Tomas 	} else if (le32_to_cpu(newext->ee_block)
1657a86c6181SAlex Tomas 			   > le32_to_cpu(nearex->ee_block)) {
1658a86c6181SAlex Tomas /*		BUG_ON(newext->ee_block == nearex->ee_block); */
1659a86c6181SAlex Tomas 		if (nearex != EXT_LAST_EXTENT(eh)) {
1660a86c6181SAlex Tomas 			len = EXT_MAX_EXTENT(eh) - nearex;
1661a86c6181SAlex Tomas 			len = (len - 1) * sizeof(struct ext4_extent);
1662a86c6181SAlex Tomas 			len = len < 0 ? 0 : len;
16632ae02107SMingming Cao 			ext_debug("insert %d:%llu:%d after: nearest 0x%p, "
1664a86c6181SAlex Tomas 					"move %d from 0x%p to 0x%p\n",
1665a86c6181SAlex Tomas 					le32_to_cpu(newext->ee_block),
1666f65e6fbaSAlex Tomas 					ext_pblock(newext),
1667a2df2a63SAmit Arora 					ext4_ext_get_actual_len(newext),
1668a86c6181SAlex Tomas 					nearex, len, nearex + 1, nearex + 2);
1669a86c6181SAlex Tomas 			memmove(nearex + 2, nearex + 1, len);
1670a86c6181SAlex Tomas 		}
1671a86c6181SAlex Tomas 		path[depth].p_ext = nearex + 1;
1672a86c6181SAlex Tomas 	} else {
1673a86c6181SAlex Tomas 		BUG_ON(newext->ee_block == nearex->ee_block);
1674a86c6181SAlex Tomas 		len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1675a86c6181SAlex Tomas 		len = len < 0 ? 0 : len;
16762ae02107SMingming Cao 		ext_debug("insert %d:%llu:%d before: nearest 0x%p, "
1677a86c6181SAlex Tomas 				"move %d from 0x%p to 0x%p\n",
1678a86c6181SAlex Tomas 				le32_to_cpu(newext->ee_block),
1679f65e6fbaSAlex Tomas 				ext_pblock(newext),
1680a2df2a63SAmit Arora 				ext4_ext_get_actual_len(newext),
1681a86c6181SAlex Tomas 				nearex, len, nearex + 1, nearex + 2);
1682a86c6181SAlex Tomas 		memmove(nearex + 1, nearex, len);
1683a86c6181SAlex Tomas 		path[depth].p_ext = nearex;
1684a86c6181SAlex Tomas 	}
1685a86c6181SAlex Tomas 
1686e8546d06SMarcin Slusarz 	le16_add_cpu(&eh->eh_entries, 1);
1687a86c6181SAlex Tomas 	nearex = path[depth].p_ext;
1688a86c6181SAlex Tomas 	nearex->ee_block = newext->ee_block;
1689b377611dSAneesh Kumar K.V 	ext4_ext_store_pblock(nearex, ext_pblock(newext));
1690a86c6181SAlex Tomas 	nearex->ee_len = newext->ee_len;
1691a86c6181SAlex Tomas 
1692a86c6181SAlex Tomas merge:
1693a86c6181SAlex Tomas 	/* try to merge extents to the right */
169456055d3aSAmit Arora 	ext4_ext_try_to_merge(inode, path, nearex);
1695a86c6181SAlex Tomas 
1696a86c6181SAlex Tomas 	/* try to merge extents to the left */
1697a86c6181SAlex Tomas 
1698a86c6181SAlex Tomas 	/* time to correct all indexes above */
1699a86c6181SAlex Tomas 	err = ext4_ext_correct_indexes(handle, inode, path);
1700a86c6181SAlex Tomas 	if (err)
1701a86c6181SAlex Tomas 		goto cleanup;
1702a86c6181SAlex Tomas 
1703a86c6181SAlex Tomas 	err = ext4_ext_dirty(handle, inode, path + depth);
1704a86c6181SAlex Tomas 
1705a86c6181SAlex Tomas cleanup:
1706a86c6181SAlex Tomas 	if (npath) {
1707a86c6181SAlex Tomas 		ext4_ext_drop_refs(npath);
1708a86c6181SAlex Tomas 		kfree(npath);
1709a86c6181SAlex Tomas 	}
1710a86c6181SAlex Tomas 	ext4_ext_invalidate_cache(inode);
1711a86c6181SAlex Tomas 	return err;
1712a86c6181SAlex Tomas }
1713a86c6181SAlex Tomas 
17146873fa0dSEric Sandeen int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block,
17156873fa0dSEric Sandeen 			ext4_lblk_t num, ext_prepare_callback func,
17166873fa0dSEric Sandeen 			void *cbdata)
17176873fa0dSEric Sandeen {
17186873fa0dSEric Sandeen 	struct ext4_ext_path *path = NULL;
17196873fa0dSEric Sandeen 	struct ext4_ext_cache cbex;
17206873fa0dSEric Sandeen 	struct ext4_extent *ex;
17216873fa0dSEric Sandeen 	ext4_lblk_t next, start = 0, end = 0;
17226873fa0dSEric Sandeen 	ext4_lblk_t last = block + num;
17236873fa0dSEric Sandeen 	int depth, exists, err = 0;
17246873fa0dSEric Sandeen 
17256873fa0dSEric Sandeen 	BUG_ON(func == NULL);
17266873fa0dSEric Sandeen 	BUG_ON(inode == NULL);
17276873fa0dSEric Sandeen 
17286873fa0dSEric Sandeen 	while (block < last && block != EXT_MAX_BLOCK) {
17296873fa0dSEric Sandeen 		num = last - block;
17306873fa0dSEric Sandeen 		/* find extent for this block */
17316873fa0dSEric Sandeen 		path = ext4_ext_find_extent(inode, block, path);
17326873fa0dSEric Sandeen 		if (IS_ERR(path)) {
17336873fa0dSEric Sandeen 			err = PTR_ERR(path);
17346873fa0dSEric Sandeen 			path = NULL;
17356873fa0dSEric Sandeen 			break;
17366873fa0dSEric Sandeen 		}
17376873fa0dSEric Sandeen 
17386873fa0dSEric Sandeen 		depth = ext_depth(inode);
17396873fa0dSEric Sandeen 		BUG_ON(path[depth].p_hdr == NULL);
17406873fa0dSEric Sandeen 		ex = path[depth].p_ext;
17416873fa0dSEric Sandeen 		next = ext4_ext_next_allocated_block(path);
17426873fa0dSEric Sandeen 
17436873fa0dSEric Sandeen 		exists = 0;
17446873fa0dSEric Sandeen 		if (!ex) {
17456873fa0dSEric Sandeen 			/* there is no extent yet, so try to allocate
17466873fa0dSEric Sandeen 			 * all requested space */
17476873fa0dSEric Sandeen 			start = block;
17486873fa0dSEric Sandeen 			end = block + num;
17496873fa0dSEric Sandeen 		} else if (le32_to_cpu(ex->ee_block) > block) {
17506873fa0dSEric Sandeen 			/* need to allocate space before found extent */
17516873fa0dSEric Sandeen 			start = block;
17526873fa0dSEric Sandeen 			end = le32_to_cpu(ex->ee_block);
17536873fa0dSEric Sandeen 			if (block + num < end)
17546873fa0dSEric Sandeen 				end = block + num;
17556873fa0dSEric Sandeen 		} else if (block >= le32_to_cpu(ex->ee_block)
17566873fa0dSEric Sandeen 					+ ext4_ext_get_actual_len(ex)) {
17576873fa0dSEric Sandeen 			/* need to allocate space after found extent */
17586873fa0dSEric Sandeen 			start = block;
17596873fa0dSEric Sandeen 			end = block + num;
17606873fa0dSEric Sandeen 			if (end >= next)
17616873fa0dSEric Sandeen 				end = next;
17626873fa0dSEric Sandeen 		} else if (block >= le32_to_cpu(ex->ee_block)) {
17636873fa0dSEric Sandeen 			/*
17646873fa0dSEric Sandeen 			 * some part of requested space is covered
17656873fa0dSEric Sandeen 			 * by found extent
17666873fa0dSEric Sandeen 			 */
17676873fa0dSEric Sandeen 			start = block;
17686873fa0dSEric Sandeen 			end = le32_to_cpu(ex->ee_block)
17696873fa0dSEric Sandeen 				+ ext4_ext_get_actual_len(ex);
17706873fa0dSEric Sandeen 			if (block + num < end)
17716873fa0dSEric Sandeen 				end = block + num;
17726873fa0dSEric Sandeen 			exists = 1;
17736873fa0dSEric Sandeen 		} else {
17746873fa0dSEric Sandeen 			BUG();
17756873fa0dSEric Sandeen 		}
17766873fa0dSEric Sandeen 		BUG_ON(end <= start);
17776873fa0dSEric Sandeen 
17786873fa0dSEric Sandeen 		if (!exists) {
17796873fa0dSEric Sandeen 			cbex.ec_block = start;
17806873fa0dSEric Sandeen 			cbex.ec_len = end - start;
17816873fa0dSEric Sandeen 			cbex.ec_start = 0;
17826873fa0dSEric Sandeen 			cbex.ec_type = EXT4_EXT_CACHE_GAP;
17836873fa0dSEric Sandeen 		} else {
17846873fa0dSEric Sandeen 			cbex.ec_block = le32_to_cpu(ex->ee_block);
17856873fa0dSEric Sandeen 			cbex.ec_len = ext4_ext_get_actual_len(ex);
17866873fa0dSEric Sandeen 			cbex.ec_start = ext_pblock(ex);
17876873fa0dSEric Sandeen 			cbex.ec_type = EXT4_EXT_CACHE_EXTENT;
17886873fa0dSEric Sandeen 		}
17896873fa0dSEric Sandeen 
17906873fa0dSEric Sandeen 		BUG_ON(cbex.ec_len == 0);
17916873fa0dSEric Sandeen 		err = func(inode, path, &cbex, ex, cbdata);
17926873fa0dSEric Sandeen 		ext4_ext_drop_refs(path);
17936873fa0dSEric Sandeen 
17946873fa0dSEric Sandeen 		if (err < 0)
17956873fa0dSEric Sandeen 			break;
17966873fa0dSEric Sandeen 
17976873fa0dSEric Sandeen 		if (err == EXT_REPEAT)
17986873fa0dSEric Sandeen 			continue;
17996873fa0dSEric Sandeen 		else if (err == EXT_BREAK) {
18006873fa0dSEric Sandeen 			err = 0;
18016873fa0dSEric Sandeen 			break;
18026873fa0dSEric Sandeen 		}
18036873fa0dSEric Sandeen 
18046873fa0dSEric Sandeen 		if (ext_depth(inode) != depth) {
18056873fa0dSEric Sandeen 			/* depth was changed. we have to realloc path */
18066873fa0dSEric Sandeen 			kfree(path);
18076873fa0dSEric Sandeen 			path = NULL;
18086873fa0dSEric Sandeen 		}
18096873fa0dSEric Sandeen 
18106873fa0dSEric Sandeen 		block = cbex.ec_block + cbex.ec_len;
18116873fa0dSEric Sandeen 	}
18126873fa0dSEric Sandeen 
18136873fa0dSEric Sandeen 	if (path) {
18146873fa0dSEric Sandeen 		ext4_ext_drop_refs(path);
18156873fa0dSEric Sandeen 		kfree(path);
18166873fa0dSEric Sandeen 	}
18176873fa0dSEric Sandeen 
18186873fa0dSEric Sandeen 	return err;
18196873fa0dSEric Sandeen }
18206873fa0dSEric Sandeen 
182109b88252SAvantika Mathur static void
1822725d26d3SAneesh Kumar K.V ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
1823dd54567aSMingming Cao 			__u32 len, ext4_fsblk_t start, int type)
1824a86c6181SAlex Tomas {
1825a86c6181SAlex Tomas 	struct ext4_ext_cache *cex;
1826a86c6181SAlex Tomas 	BUG_ON(len == 0);
1827a86c6181SAlex Tomas 	cex = &EXT4_I(inode)->i_cached_extent;
1828a86c6181SAlex Tomas 	cex->ec_type = type;
1829a86c6181SAlex Tomas 	cex->ec_block = block;
1830a86c6181SAlex Tomas 	cex->ec_len = len;
1831a86c6181SAlex Tomas 	cex->ec_start = start;
1832a86c6181SAlex Tomas }
1833a86c6181SAlex Tomas 
1834a86c6181SAlex Tomas /*
1835d0d856e8SRandy Dunlap  * ext4_ext_put_gap_in_cache:
1836d0d856e8SRandy Dunlap  * calculate boundaries of the gap that the requested block fits into
1837a86c6181SAlex Tomas  * and cache this gap
1838a86c6181SAlex Tomas  */
183909b88252SAvantika Mathur static void
1840a86c6181SAlex Tomas ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
1841725d26d3SAneesh Kumar K.V 				ext4_lblk_t block)
1842a86c6181SAlex Tomas {
1843a86c6181SAlex Tomas 	int depth = ext_depth(inode);
1844725d26d3SAneesh Kumar K.V 	unsigned long len;
1845725d26d3SAneesh Kumar K.V 	ext4_lblk_t lblock;
1846a86c6181SAlex Tomas 	struct ext4_extent *ex;
1847a86c6181SAlex Tomas 
1848a86c6181SAlex Tomas 	ex = path[depth].p_ext;
1849a86c6181SAlex Tomas 	if (ex == NULL) {
1850a86c6181SAlex Tomas 		/* there is no extent yet, so gap is [0;-] */
1851a86c6181SAlex Tomas 		lblock = 0;
1852a86c6181SAlex Tomas 		len = EXT_MAX_BLOCK;
1853a86c6181SAlex Tomas 		ext_debug("cache gap(whole file):");
1854a86c6181SAlex Tomas 	} else if (block < le32_to_cpu(ex->ee_block)) {
1855a86c6181SAlex Tomas 		lblock = block;
1856a86c6181SAlex Tomas 		len = le32_to_cpu(ex->ee_block) - block;
1857bba90743SEric Sandeen 		ext_debug("cache gap(before): %u [%u:%u]",
1858bba90743SEric Sandeen 				block,
1859bba90743SEric Sandeen 				le32_to_cpu(ex->ee_block),
1860bba90743SEric Sandeen 				 ext4_ext_get_actual_len(ex));
1861a86c6181SAlex Tomas 	} else if (block >= le32_to_cpu(ex->ee_block)
1862a2df2a63SAmit Arora 			+ ext4_ext_get_actual_len(ex)) {
1863725d26d3SAneesh Kumar K.V 		ext4_lblk_t next;
1864a86c6181SAlex Tomas 		lblock = le32_to_cpu(ex->ee_block)
1865a2df2a63SAmit Arora 			+ ext4_ext_get_actual_len(ex);
1866725d26d3SAneesh Kumar K.V 
1867725d26d3SAneesh Kumar K.V 		next = ext4_ext_next_allocated_block(path);
1868bba90743SEric Sandeen 		ext_debug("cache gap(after): [%u:%u] %u",
1869bba90743SEric Sandeen 				le32_to_cpu(ex->ee_block),
1870bba90743SEric Sandeen 				ext4_ext_get_actual_len(ex),
1871bba90743SEric Sandeen 				block);
1872725d26d3SAneesh Kumar K.V 		BUG_ON(next == lblock);
1873725d26d3SAneesh Kumar K.V 		len = next - lblock;
1874a86c6181SAlex Tomas 	} else {
1875a86c6181SAlex Tomas 		lblock = len = 0;
1876a86c6181SAlex Tomas 		BUG();
1877a86c6181SAlex Tomas 	}
1878a86c6181SAlex Tomas 
1879bba90743SEric Sandeen 	ext_debug(" -> %u:%lu\n", lblock, len);
1880a86c6181SAlex Tomas 	ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
1881a86c6181SAlex Tomas }
1882a86c6181SAlex Tomas 
188309b88252SAvantika Mathur static int
1884725d26d3SAneesh Kumar K.V ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
1885a86c6181SAlex Tomas 			struct ext4_extent *ex)
1886a86c6181SAlex Tomas {
1887a86c6181SAlex Tomas 	struct ext4_ext_cache *cex;
1888a86c6181SAlex Tomas 
1889a86c6181SAlex Tomas 	cex = &EXT4_I(inode)->i_cached_extent;
1890a86c6181SAlex Tomas 
1891a86c6181SAlex Tomas 	/* has cache valid data? */
1892a86c6181SAlex Tomas 	if (cex->ec_type == EXT4_EXT_CACHE_NO)
1893a86c6181SAlex Tomas 		return EXT4_EXT_CACHE_NO;
1894a86c6181SAlex Tomas 
1895a86c6181SAlex Tomas 	BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
1896a86c6181SAlex Tomas 			cex->ec_type != EXT4_EXT_CACHE_EXTENT);
1897a86c6181SAlex Tomas 	if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
1898a86c6181SAlex Tomas 		ex->ee_block = cpu_to_le32(cex->ec_block);
1899f65e6fbaSAlex Tomas 		ext4_ext_store_pblock(ex, cex->ec_start);
1900a86c6181SAlex Tomas 		ex->ee_len = cpu_to_le16(cex->ec_len);
1901bba90743SEric Sandeen 		ext_debug("%u cached by %u:%u:%llu\n",
1902bba90743SEric Sandeen 				block,
1903bba90743SEric Sandeen 				cex->ec_block, cex->ec_len, cex->ec_start);
1904a86c6181SAlex Tomas 		return cex->ec_type;
1905a86c6181SAlex Tomas 	}
1906a86c6181SAlex Tomas 
1907a86c6181SAlex Tomas 	/* not in cache */
1908a86c6181SAlex Tomas 	return EXT4_EXT_CACHE_NO;
1909a86c6181SAlex Tomas }
1910a86c6181SAlex Tomas 
1911a86c6181SAlex Tomas /*
1912d0d856e8SRandy Dunlap  * ext4_ext_rm_idx:
1913d0d856e8SRandy Dunlap  * removes index from the index block.
1914d0d856e8SRandy Dunlap  * It's used in truncate case only, thus all requests are for
1915d0d856e8SRandy Dunlap  * last index in the block only.
1916a86c6181SAlex Tomas  */
19171d03ec98SAneesh Kumar K.V static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
1918a86c6181SAlex Tomas 			struct ext4_ext_path *path)
1919a86c6181SAlex Tomas {
1920a86c6181SAlex Tomas 	struct buffer_head *bh;
1921a86c6181SAlex Tomas 	int err;
1922f65e6fbaSAlex Tomas 	ext4_fsblk_t leaf;
1923a86c6181SAlex Tomas 
1924a86c6181SAlex Tomas 	/* free index block */
1925a86c6181SAlex Tomas 	path--;
1926f65e6fbaSAlex Tomas 	leaf = idx_pblock(path->p_idx);
1927a86c6181SAlex Tomas 	BUG_ON(path->p_hdr->eh_entries == 0);
19287e028976SAvantika Mathur 	err = ext4_ext_get_access(handle, inode, path);
19297e028976SAvantika Mathur 	if (err)
1930a86c6181SAlex Tomas 		return err;
1931e8546d06SMarcin Slusarz 	le16_add_cpu(&path->p_hdr->eh_entries, -1);
19327e028976SAvantika Mathur 	err = ext4_ext_dirty(handle, inode, path);
19337e028976SAvantika Mathur 	if (err)
1934a86c6181SAlex Tomas 		return err;
19352ae02107SMingming Cao 	ext_debug("index is empty, remove it, free block %llu\n", leaf);
1936a86c6181SAlex Tomas 	bh = sb_find_get_block(inode->i_sb, leaf);
1937a86c6181SAlex Tomas 	ext4_forget(handle, 1, inode, bh, leaf);
1938c9de560dSAlex Tomas 	ext4_free_blocks(handle, inode, leaf, 1, 1);
1939a86c6181SAlex Tomas 	return err;
1940a86c6181SAlex Tomas }
1941a86c6181SAlex Tomas 
1942a86c6181SAlex Tomas /*
1943ee12b630SMingming Cao  * ext4_ext_calc_credits_for_single_extent:
1944ee12b630SMingming Cao  * This routine returns max. credits that needed to insert an extent
1945ee12b630SMingming Cao  * to the extent tree.
1946ee12b630SMingming Cao  * When pass the actual path, the caller should calculate credits
1947ee12b630SMingming Cao  * under i_data_sem.
1948a86c6181SAlex Tomas  */
1949525f4ed8SMingming Cao int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
1950a86c6181SAlex Tomas 						struct ext4_ext_path *path)
1951a86c6181SAlex Tomas {
1952a86c6181SAlex Tomas 	if (path) {
1953ee12b630SMingming Cao 		int depth = ext_depth(inode);
1954f3bd1f3fSMingming Cao 		int ret = 0;
1955ee12b630SMingming Cao 
1956a86c6181SAlex Tomas 		/* probably there is space in leaf? */
1957a86c6181SAlex Tomas 		if (le16_to_cpu(path[depth].p_hdr->eh_entries)
1958ee12b630SMingming Cao 				< le16_to_cpu(path[depth].p_hdr->eh_max)) {
1959ee12b630SMingming Cao 
1960ee12b630SMingming Cao 			/*
1961ee12b630SMingming Cao 			 *  There are some space in the leaf tree, no
1962ee12b630SMingming Cao 			 *  need to account for leaf block credit
1963ee12b630SMingming Cao 			 *
1964ee12b630SMingming Cao 			 *  bitmaps and block group descriptor blocks
1965ee12b630SMingming Cao 			 *  and other metadat blocks still need to be
1966ee12b630SMingming Cao 			 *  accounted.
1967ee12b630SMingming Cao 			 */
1968525f4ed8SMingming Cao 			/* 1 bitmap, 1 block group descriptor */
1969ee12b630SMingming Cao 			ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
1970ee12b630SMingming Cao 		}
1971ee12b630SMingming Cao 	}
1972ee12b630SMingming Cao 
1973525f4ed8SMingming Cao 	return ext4_chunk_trans_blocks(inode, nrblocks);
1974a86c6181SAlex Tomas }
1975a86c6181SAlex Tomas 
1976a86c6181SAlex Tomas /*
1977ee12b630SMingming Cao  * How many index/leaf blocks need to change/allocate to modify nrblocks?
1978ee12b630SMingming Cao  *
1979ee12b630SMingming Cao  * if nrblocks are fit in a single extent (chunk flag is 1), then
1980ee12b630SMingming Cao  * in the worse case, each tree level index/leaf need to be changed
1981ee12b630SMingming Cao  * if the tree split due to insert a new extent, then the old tree
1982ee12b630SMingming Cao  * index/leaf need to be updated too
1983ee12b630SMingming Cao  *
1984ee12b630SMingming Cao  * If the nrblocks are discontiguous, they could cause
1985ee12b630SMingming Cao  * the whole tree split more than once, but this is really rare.
1986a86c6181SAlex Tomas  */
1987525f4ed8SMingming Cao int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
1988ee12b630SMingming Cao {
1989ee12b630SMingming Cao 	int index;
1990ee12b630SMingming Cao 	int depth = ext_depth(inode);
1991a86c6181SAlex Tomas 
1992ee12b630SMingming Cao 	if (chunk)
1993ee12b630SMingming Cao 		index = depth * 2;
1994ee12b630SMingming Cao 	else
1995ee12b630SMingming Cao 		index = depth * 3;
1996a86c6181SAlex Tomas 
1997ee12b630SMingming Cao 	return index;
1998a86c6181SAlex Tomas }
1999a86c6181SAlex Tomas 
2000a86c6181SAlex Tomas static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
2001a86c6181SAlex Tomas 				struct ext4_extent *ex,
2002725d26d3SAneesh Kumar K.V 				ext4_lblk_t from, ext4_lblk_t to)
2003a86c6181SAlex Tomas {
2004a86c6181SAlex Tomas 	struct buffer_head *bh;
2005a2df2a63SAmit Arora 	unsigned short ee_len =  ext4_ext_get_actual_len(ex);
2006c9de560dSAlex Tomas 	int i, metadata = 0;
2007a86c6181SAlex Tomas 
2008c9de560dSAlex Tomas 	if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2009c9de560dSAlex Tomas 		metadata = 1;
2010a86c6181SAlex Tomas #ifdef EXTENTS_STATS
2011a86c6181SAlex Tomas 	{
2012a86c6181SAlex Tomas 		struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2013a86c6181SAlex Tomas 		spin_lock(&sbi->s_ext_stats_lock);
2014a86c6181SAlex Tomas 		sbi->s_ext_blocks += ee_len;
2015a86c6181SAlex Tomas 		sbi->s_ext_extents++;
2016a86c6181SAlex Tomas 		if (ee_len < sbi->s_ext_min)
2017a86c6181SAlex Tomas 			sbi->s_ext_min = ee_len;
2018a86c6181SAlex Tomas 		if (ee_len > sbi->s_ext_max)
2019a86c6181SAlex Tomas 			sbi->s_ext_max = ee_len;
2020a86c6181SAlex Tomas 		if (ext_depth(inode) > sbi->s_depth_max)
2021a86c6181SAlex Tomas 			sbi->s_depth_max = ext_depth(inode);
2022a86c6181SAlex Tomas 		spin_unlock(&sbi->s_ext_stats_lock);
2023a86c6181SAlex Tomas 	}
2024a86c6181SAlex Tomas #endif
2025a86c6181SAlex Tomas 	if (from >= le32_to_cpu(ex->ee_block)
2026a2df2a63SAmit Arora 	    && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
2027a86c6181SAlex Tomas 		/* tail removal */
2028725d26d3SAneesh Kumar K.V 		ext4_lblk_t num;
2029f65e6fbaSAlex Tomas 		ext4_fsblk_t start;
2030725d26d3SAneesh Kumar K.V 
2031a2df2a63SAmit Arora 		num = le32_to_cpu(ex->ee_block) + ee_len - from;
2032a2df2a63SAmit Arora 		start = ext_pblock(ex) + ee_len - num;
2033725d26d3SAneesh Kumar K.V 		ext_debug("free last %u blocks starting %llu\n", num, start);
2034a86c6181SAlex Tomas 		for (i = 0; i < num; i++) {
2035a86c6181SAlex Tomas 			bh = sb_find_get_block(inode->i_sb, start + i);
2036a86c6181SAlex Tomas 			ext4_forget(handle, 0, inode, bh, start + i);
2037a86c6181SAlex Tomas 		}
2038c9de560dSAlex Tomas 		ext4_free_blocks(handle, inode, start, num, metadata);
2039a86c6181SAlex Tomas 	} else if (from == le32_to_cpu(ex->ee_block)
2040a2df2a63SAmit Arora 		   && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
2041725d26d3SAneesh Kumar K.V 		printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n",
2042a2df2a63SAmit Arora 			from, to, le32_to_cpu(ex->ee_block), ee_len);
2043a86c6181SAlex Tomas 	} else {
2044725d26d3SAneesh Kumar K.V 		printk(KERN_INFO "strange request: removal(2) "
2045725d26d3SAneesh Kumar K.V 				"%u-%u from %u:%u\n",
2046a2df2a63SAmit Arora 				from, to, le32_to_cpu(ex->ee_block), ee_len);
2047a86c6181SAlex Tomas 	}
2048a86c6181SAlex Tomas 	return 0;
2049a86c6181SAlex Tomas }
2050a86c6181SAlex Tomas 
2051a86c6181SAlex Tomas static int
2052a86c6181SAlex Tomas ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
2053725d26d3SAneesh Kumar K.V 		struct ext4_ext_path *path, ext4_lblk_t start)
2054a86c6181SAlex Tomas {
2055a86c6181SAlex Tomas 	int err = 0, correct_index = 0;
2056a86c6181SAlex Tomas 	int depth = ext_depth(inode), credits;
2057a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
2058725d26d3SAneesh Kumar K.V 	ext4_lblk_t a, b, block;
2059725d26d3SAneesh Kumar K.V 	unsigned num;
2060725d26d3SAneesh Kumar K.V 	ext4_lblk_t ex_ee_block;
2061a86c6181SAlex Tomas 	unsigned short ex_ee_len;
2062a2df2a63SAmit Arora 	unsigned uninitialized = 0;
2063a86c6181SAlex Tomas 	struct ext4_extent *ex;
2064a86c6181SAlex Tomas 
2065c29c0ae7SAlex Tomas 	/* the header must be checked already in ext4_ext_remove_space() */
2066725d26d3SAneesh Kumar K.V 	ext_debug("truncate since %u in leaf\n", start);
2067a86c6181SAlex Tomas 	if (!path[depth].p_hdr)
2068a86c6181SAlex Tomas 		path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2069a86c6181SAlex Tomas 	eh = path[depth].p_hdr;
2070a86c6181SAlex Tomas 	BUG_ON(eh == NULL);
2071a86c6181SAlex Tomas 
2072a86c6181SAlex Tomas 	/* find where to start removing */
2073a86c6181SAlex Tomas 	ex = EXT_LAST_EXTENT(eh);
2074a86c6181SAlex Tomas 
2075a86c6181SAlex Tomas 	ex_ee_block = le32_to_cpu(ex->ee_block);
2076a2df2a63SAmit Arora 	if (ext4_ext_is_uninitialized(ex))
2077a2df2a63SAmit Arora 		uninitialized = 1;
2078a2df2a63SAmit Arora 	ex_ee_len = ext4_ext_get_actual_len(ex);
2079a86c6181SAlex Tomas 
2080a86c6181SAlex Tomas 	while (ex >= EXT_FIRST_EXTENT(eh) &&
2081a86c6181SAlex Tomas 			ex_ee_block + ex_ee_len > start) {
2082a86c6181SAlex Tomas 		ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len);
2083a86c6181SAlex Tomas 		path[depth].p_ext = ex;
2084a86c6181SAlex Tomas 
2085a86c6181SAlex Tomas 		a = ex_ee_block > start ? ex_ee_block : start;
2086a86c6181SAlex Tomas 		b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
2087a86c6181SAlex Tomas 			ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
2088a86c6181SAlex Tomas 
2089a86c6181SAlex Tomas 		ext_debug("  border %u:%u\n", a, b);
2090a86c6181SAlex Tomas 
2091a86c6181SAlex Tomas 		if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
2092a86c6181SAlex Tomas 			block = 0;
2093a86c6181SAlex Tomas 			num = 0;
2094a86c6181SAlex Tomas 			BUG();
2095a86c6181SAlex Tomas 		} else if (a != ex_ee_block) {
2096a86c6181SAlex Tomas 			/* remove tail of the extent */
2097a86c6181SAlex Tomas 			block = ex_ee_block;
2098a86c6181SAlex Tomas 			num = a - block;
2099a86c6181SAlex Tomas 		} else if (b != ex_ee_block + ex_ee_len - 1) {
2100a86c6181SAlex Tomas 			/* remove head of the extent */
2101a86c6181SAlex Tomas 			block = a;
2102a86c6181SAlex Tomas 			num = b - a;
2103a86c6181SAlex Tomas 			/* there is no "make a hole" API yet */
2104a86c6181SAlex Tomas 			BUG();
2105a86c6181SAlex Tomas 		} else {
2106a86c6181SAlex Tomas 			/* remove whole extent: excellent! */
2107a86c6181SAlex Tomas 			block = ex_ee_block;
2108a86c6181SAlex Tomas 			num = 0;
2109a86c6181SAlex Tomas 			BUG_ON(a != ex_ee_block);
2110a86c6181SAlex Tomas 			BUG_ON(b != ex_ee_block + ex_ee_len - 1);
2111a86c6181SAlex Tomas 		}
2112a86c6181SAlex Tomas 
211334071da7STheodore Ts'o 		/*
211434071da7STheodore Ts'o 		 * 3 for leaf, sb, and inode plus 2 (bmap and group
211534071da7STheodore Ts'o 		 * descriptor) for each block group; assume two block
211634071da7STheodore Ts'o 		 * groups plus ex_ee_len/blocks_per_block_group for
211734071da7STheodore Ts'o 		 * the worst case
211834071da7STheodore Ts'o 		 */
211934071da7STheodore Ts'o 		credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
2120a86c6181SAlex Tomas 		if (ex == EXT_FIRST_EXTENT(eh)) {
2121a86c6181SAlex Tomas 			correct_index = 1;
2122a86c6181SAlex Tomas 			credits += (ext_depth(inode)) + 1;
2123a86c6181SAlex Tomas 		}
2124a86c6181SAlex Tomas 		credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
2125a86c6181SAlex Tomas 
21269102e4faSShen Feng 		err = ext4_ext_journal_restart(handle, credits);
21279102e4faSShen Feng 		if (err)
2128a86c6181SAlex Tomas 			goto out;
2129a86c6181SAlex Tomas 
2130a86c6181SAlex Tomas 		err = ext4_ext_get_access(handle, inode, path + depth);
2131a86c6181SAlex Tomas 		if (err)
2132a86c6181SAlex Tomas 			goto out;
2133a86c6181SAlex Tomas 
2134a86c6181SAlex Tomas 		err = ext4_remove_blocks(handle, inode, ex, a, b);
2135a86c6181SAlex Tomas 		if (err)
2136a86c6181SAlex Tomas 			goto out;
2137a86c6181SAlex Tomas 
2138a86c6181SAlex Tomas 		if (num == 0) {
2139d0d856e8SRandy Dunlap 			/* this extent is removed; mark slot entirely unused */
2140f65e6fbaSAlex Tomas 			ext4_ext_store_pblock(ex, 0);
2141e8546d06SMarcin Slusarz 			le16_add_cpu(&eh->eh_entries, -1);
2142a86c6181SAlex Tomas 		}
2143a86c6181SAlex Tomas 
2144a86c6181SAlex Tomas 		ex->ee_block = cpu_to_le32(block);
2145a86c6181SAlex Tomas 		ex->ee_len = cpu_to_le16(num);
2146749269faSAmit Arora 		/*
2147749269faSAmit Arora 		 * Do not mark uninitialized if all the blocks in the
2148749269faSAmit Arora 		 * extent have been removed.
2149749269faSAmit Arora 		 */
2150749269faSAmit Arora 		if (uninitialized && num)
2151a2df2a63SAmit Arora 			ext4_ext_mark_uninitialized(ex);
2152a86c6181SAlex Tomas 
2153a86c6181SAlex Tomas 		err = ext4_ext_dirty(handle, inode, path + depth);
2154a86c6181SAlex Tomas 		if (err)
2155a86c6181SAlex Tomas 			goto out;
2156a86c6181SAlex Tomas 
21572ae02107SMingming Cao 		ext_debug("new extent: %u:%u:%llu\n", block, num,
2158f65e6fbaSAlex Tomas 				ext_pblock(ex));
2159a86c6181SAlex Tomas 		ex--;
2160a86c6181SAlex Tomas 		ex_ee_block = le32_to_cpu(ex->ee_block);
2161a2df2a63SAmit Arora 		ex_ee_len = ext4_ext_get_actual_len(ex);
2162a86c6181SAlex Tomas 	}
2163a86c6181SAlex Tomas 
2164a86c6181SAlex Tomas 	if (correct_index && eh->eh_entries)
2165a86c6181SAlex Tomas 		err = ext4_ext_correct_indexes(handle, inode, path);
2166a86c6181SAlex Tomas 
2167a86c6181SAlex Tomas 	/* if this leaf is free, then we should
2168a86c6181SAlex Tomas 	 * remove it from index block above */
2169a86c6181SAlex Tomas 	if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
2170a86c6181SAlex Tomas 		err = ext4_ext_rm_idx(handle, inode, path + depth);
2171a86c6181SAlex Tomas 
2172a86c6181SAlex Tomas out:
2173a86c6181SAlex Tomas 	return err;
2174a86c6181SAlex Tomas }
2175a86c6181SAlex Tomas 
2176a86c6181SAlex Tomas /*
2177d0d856e8SRandy Dunlap  * ext4_ext_more_to_rm:
2178d0d856e8SRandy Dunlap  * returns 1 if current index has to be freed (even partial)
2179a86c6181SAlex Tomas  */
218009b88252SAvantika Mathur static int
2181a86c6181SAlex Tomas ext4_ext_more_to_rm(struct ext4_ext_path *path)
2182a86c6181SAlex Tomas {
2183a86c6181SAlex Tomas 	BUG_ON(path->p_idx == NULL);
2184a86c6181SAlex Tomas 
2185a86c6181SAlex Tomas 	if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2186a86c6181SAlex Tomas 		return 0;
2187a86c6181SAlex Tomas 
2188a86c6181SAlex Tomas 	/*
2189d0d856e8SRandy Dunlap 	 * if truncate on deeper level happened, it wasn't partial,
2190a86c6181SAlex Tomas 	 * so we have to consider current index for truncation
2191a86c6181SAlex Tomas 	 */
2192a86c6181SAlex Tomas 	if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2193a86c6181SAlex Tomas 		return 0;
2194a86c6181SAlex Tomas 	return 1;
2195a86c6181SAlex Tomas }
2196a86c6181SAlex Tomas 
21971d03ec98SAneesh Kumar K.V static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
2198a86c6181SAlex Tomas {
2199a86c6181SAlex Tomas 	struct super_block *sb = inode->i_sb;
2200a86c6181SAlex Tomas 	int depth = ext_depth(inode);
2201a86c6181SAlex Tomas 	struct ext4_ext_path *path;
2202a86c6181SAlex Tomas 	handle_t *handle;
2203a86c6181SAlex Tomas 	int i = 0, err = 0;
2204a86c6181SAlex Tomas 
2205725d26d3SAneesh Kumar K.V 	ext_debug("truncate since %u\n", start);
2206a86c6181SAlex Tomas 
2207a86c6181SAlex Tomas 	/* probably first extent we're gonna free will be last in block */
2208a86c6181SAlex Tomas 	handle = ext4_journal_start(inode, depth + 1);
2209a86c6181SAlex Tomas 	if (IS_ERR(handle))
2210a86c6181SAlex Tomas 		return PTR_ERR(handle);
2211a86c6181SAlex Tomas 
2212a86c6181SAlex Tomas 	ext4_ext_invalidate_cache(inode);
2213a86c6181SAlex Tomas 
2214a86c6181SAlex Tomas 	/*
2215d0d856e8SRandy Dunlap 	 * We start scanning from right side, freeing all the blocks
2216d0d856e8SRandy Dunlap 	 * after i_size and walking into the tree depth-wise.
2217a86c6181SAlex Tomas 	 */
2218216553c4SJosef Bacik 	path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
2219a86c6181SAlex Tomas 	if (path == NULL) {
2220a86c6181SAlex Tomas 		ext4_journal_stop(handle);
2221a86c6181SAlex Tomas 		return -ENOMEM;
2222a86c6181SAlex Tomas 	}
2223a86c6181SAlex Tomas 	path[0].p_hdr = ext_inode_hdr(inode);
222456b19868SAneesh Kumar K.V 	if (ext4_ext_check(inode, path[0].p_hdr, depth)) {
2225a86c6181SAlex Tomas 		err = -EIO;
2226a86c6181SAlex Tomas 		goto out;
2227a86c6181SAlex Tomas 	}
2228a86c6181SAlex Tomas 	path[0].p_depth = depth;
2229a86c6181SAlex Tomas 
2230a86c6181SAlex Tomas 	while (i >= 0 && err == 0) {
2231a86c6181SAlex Tomas 		if (i == depth) {
2232a86c6181SAlex Tomas 			/* this is leaf block */
2233a86c6181SAlex Tomas 			err = ext4_ext_rm_leaf(handle, inode, path, start);
2234d0d856e8SRandy Dunlap 			/* root level has p_bh == NULL, brelse() eats this */
2235a86c6181SAlex Tomas 			brelse(path[i].p_bh);
2236a86c6181SAlex Tomas 			path[i].p_bh = NULL;
2237a86c6181SAlex Tomas 			i--;
2238a86c6181SAlex Tomas 			continue;
2239a86c6181SAlex Tomas 		}
2240a86c6181SAlex Tomas 
2241a86c6181SAlex Tomas 		/* this is index block */
2242a86c6181SAlex Tomas 		if (!path[i].p_hdr) {
2243a86c6181SAlex Tomas 			ext_debug("initialize header\n");
2244a86c6181SAlex Tomas 			path[i].p_hdr = ext_block_hdr(path[i].p_bh);
2245a86c6181SAlex Tomas 		}
2246a86c6181SAlex Tomas 
2247a86c6181SAlex Tomas 		if (!path[i].p_idx) {
2248d0d856e8SRandy Dunlap 			/* this level hasn't been touched yet */
2249a86c6181SAlex Tomas 			path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2250a86c6181SAlex Tomas 			path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2251a86c6181SAlex Tomas 			ext_debug("init index ptr: hdr 0x%p, num %d\n",
2252a86c6181SAlex Tomas 				  path[i].p_hdr,
2253a86c6181SAlex Tomas 				  le16_to_cpu(path[i].p_hdr->eh_entries));
2254a86c6181SAlex Tomas 		} else {
2255d0d856e8SRandy Dunlap 			/* we were already here, see at next index */
2256a86c6181SAlex Tomas 			path[i].p_idx--;
2257a86c6181SAlex Tomas 		}
2258a86c6181SAlex Tomas 
2259a86c6181SAlex Tomas 		ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2260a86c6181SAlex Tomas 				i, EXT_FIRST_INDEX(path[i].p_hdr),
2261a86c6181SAlex Tomas 				path[i].p_idx);
2262a86c6181SAlex Tomas 		if (ext4_ext_more_to_rm(path + i)) {
2263c29c0ae7SAlex Tomas 			struct buffer_head *bh;
2264a86c6181SAlex Tomas 			/* go to the next level */
22652ae02107SMingming Cao 			ext_debug("move to level %d (block %llu)\n",
2266f65e6fbaSAlex Tomas 				  i + 1, idx_pblock(path[i].p_idx));
2267a86c6181SAlex Tomas 			memset(path + i + 1, 0, sizeof(*path));
2268c29c0ae7SAlex Tomas 			bh = sb_bread(sb, idx_pblock(path[i].p_idx));
2269c29c0ae7SAlex Tomas 			if (!bh) {
2270a86c6181SAlex Tomas 				/* should we reset i_size? */
2271a86c6181SAlex Tomas 				err = -EIO;
2272a86c6181SAlex Tomas 				break;
2273a86c6181SAlex Tomas 			}
2274c29c0ae7SAlex Tomas 			if (WARN_ON(i + 1 > depth)) {
2275c29c0ae7SAlex Tomas 				err = -EIO;
2276c29c0ae7SAlex Tomas 				break;
2277c29c0ae7SAlex Tomas 			}
227856b19868SAneesh Kumar K.V 			if (ext4_ext_check(inode, ext_block_hdr(bh),
2279c29c0ae7SAlex Tomas 							depth - i - 1)) {
2280c29c0ae7SAlex Tomas 				err = -EIO;
2281c29c0ae7SAlex Tomas 				break;
2282c29c0ae7SAlex Tomas 			}
2283c29c0ae7SAlex Tomas 			path[i + 1].p_bh = bh;
2284a86c6181SAlex Tomas 
2285d0d856e8SRandy Dunlap 			/* save actual number of indexes since this
2286d0d856e8SRandy Dunlap 			 * number is changed at the next iteration */
2287a86c6181SAlex Tomas 			path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2288a86c6181SAlex Tomas 			i++;
2289a86c6181SAlex Tomas 		} else {
2290d0d856e8SRandy Dunlap 			/* we finished processing this index, go up */
2291a86c6181SAlex Tomas 			if (path[i].p_hdr->eh_entries == 0 && i > 0) {
2292d0d856e8SRandy Dunlap 				/* index is empty, remove it;
2293a86c6181SAlex Tomas 				 * handle must be already prepared by the
2294a86c6181SAlex Tomas 				 * truncatei_leaf() */
2295a86c6181SAlex Tomas 				err = ext4_ext_rm_idx(handle, inode, path + i);
2296a86c6181SAlex Tomas 			}
2297d0d856e8SRandy Dunlap 			/* root level has p_bh == NULL, brelse() eats this */
2298a86c6181SAlex Tomas 			brelse(path[i].p_bh);
2299a86c6181SAlex Tomas 			path[i].p_bh = NULL;
2300a86c6181SAlex Tomas 			i--;
2301a86c6181SAlex Tomas 			ext_debug("return to level %d\n", i);
2302a86c6181SAlex Tomas 		}
2303a86c6181SAlex Tomas 	}
2304a86c6181SAlex Tomas 
2305a86c6181SAlex Tomas 	/* TODO: flexible tree reduction should be here */
2306a86c6181SAlex Tomas 	if (path->p_hdr->eh_entries == 0) {
2307a86c6181SAlex Tomas 		/*
2308d0d856e8SRandy Dunlap 		 * truncate to zero freed all the tree,
2309d0d856e8SRandy Dunlap 		 * so we need to correct eh_depth
2310a86c6181SAlex Tomas 		 */
2311a86c6181SAlex Tomas 		err = ext4_ext_get_access(handle, inode, path);
2312a86c6181SAlex Tomas 		if (err == 0) {
2313a86c6181SAlex Tomas 			ext_inode_hdr(inode)->eh_depth = 0;
2314a86c6181SAlex Tomas 			ext_inode_hdr(inode)->eh_max =
2315a86c6181SAlex Tomas 				cpu_to_le16(ext4_ext_space_root(inode));
2316a86c6181SAlex Tomas 			err = ext4_ext_dirty(handle, inode, path);
2317a86c6181SAlex Tomas 		}
2318a86c6181SAlex Tomas 	}
2319a86c6181SAlex Tomas out:
2320a86c6181SAlex Tomas 	ext4_ext_drop_refs(path);
2321a86c6181SAlex Tomas 	kfree(path);
2322a86c6181SAlex Tomas 	ext4_journal_stop(handle);
2323a86c6181SAlex Tomas 
2324a86c6181SAlex Tomas 	return err;
2325a86c6181SAlex Tomas }
2326a86c6181SAlex Tomas 
2327a86c6181SAlex Tomas /*
2328a86c6181SAlex Tomas  * called at mount time
2329a86c6181SAlex Tomas  */
2330a86c6181SAlex Tomas void ext4_ext_init(struct super_block *sb)
2331a86c6181SAlex Tomas {
2332a86c6181SAlex Tomas 	/*
2333a86c6181SAlex Tomas 	 * possible initialization would be here
2334a86c6181SAlex Tomas 	 */
2335a86c6181SAlex Tomas 
233683982b6fSTheodore Ts'o 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
23374776004fSTheodore Ts'o 		printk(KERN_INFO "EXT4-fs: file extents enabled");
2338bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST
2339bbf2f9fbSRobert P. J. Day 		printk(", aggressive tests");
2340a86c6181SAlex Tomas #endif
2341a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH
2342a86c6181SAlex Tomas 		printk(", check binsearch");
2343a86c6181SAlex Tomas #endif
2344a86c6181SAlex Tomas #ifdef EXTENTS_STATS
2345a86c6181SAlex Tomas 		printk(", stats");
2346a86c6181SAlex Tomas #endif
2347a86c6181SAlex Tomas 		printk("\n");
2348a86c6181SAlex Tomas #ifdef EXTENTS_STATS
2349a86c6181SAlex Tomas 		spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2350a86c6181SAlex Tomas 		EXT4_SB(sb)->s_ext_min = 1 << 30;
2351a86c6181SAlex Tomas 		EXT4_SB(sb)->s_ext_max = 0;
2352a86c6181SAlex Tomas #endif
2353a86c6181SAlex Tomas 	}
2354a86c6181SAlex Tomas }
2355a86c6181SAlex Tomas 
2356a86c6181SAlex Tomas /*
2357a86c6181SAlex Tomas  * called at umount time
2358a86c6181SAlex Tomas  */
2359a86c6181SAlex Tomas void ext4_ext_release(struct super_block *sb)
2360a86c6181SAlex Tomas {
236183982b6fSTheodore Ts'o 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
2362a86c6181SAlex Tomas 		return;
2363a86c6181SAlex Tomas 
2364a86c6181SAlex Tomas #ifdef EXTENTS_STATS
2365a86c6181SAlex Tomas 	if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2366a86c6181SAlex Tomas 		struct ext4_sb_info *sbi = EXT4_SB(sb);
2367a86c6181SAlex Tomas 		printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2368a86c6181SAlex Tomas 			sbi->s_ext_blocks, sbi->s_ext_extents,
2369a86c6181SAlex Tomas 			sbi->s_ext_blocks / sbi->s_ext_extents);
2370a86c6181SAlex Tomas 		printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2371a86c6181SAlex Tomas 			sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2372a86c6181SAlex Tomas 	}
2373a86c6181SAlex Tomas #endif
2374a86c6181SAlex Tomas }
2375a86c6181SAlex Tomas 
2376093a088bSAneesh Kumar K.V static void bi_complete(struct bio *bio, int error)
2377093a088bSAneesh Kumar K.V {
2378093a088bSAneesh Kumar K.V 	complete((struct completion *)bio->bi_private);
2379093a088bSAneesh Kumar K.V }
2380093a088bSAneesh Kumar K.V 
2381093a088bSAneesh Kumar K.V /* FIXME!! we need to try to merge to left or right after zero-out  */
2382093a088bSAneesh Kumar K.V static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2383093a088bSAneesh Kumar K.V {
2384093a088bSAneesh Kumar K.V 	int ret = -EIO;
2385093a088bSAneesh Kumar K.V 	struct bio *bio;
2386093a088bSAneesh Kumar K.V 	int blkbits, blocksize;
2387093a088bSAneesh Kumar K.V 	sector_t ee_pblock;
2388093a088bSAneesh Kumar K.V 	struct completion event;
2389093a088bSAneesh Kumar K.V 	unsigned int ee_len, len, done, offset;
2390093a088bSAneesh Kumar K.V 
2391093a088bSAneesh Kumar K.V 
2392093a088bSAneesh Kumar K.V 	blkbits   = inode->i_blkbits;
2393093a088bSAneesh Kumar K.V 	blocksize = inode->i_sb->s_blocksize;
2394093a088bSAneesh Kumar K.V 	ee_len    = ext4_ext_get_actual_len(ex);
2395093a088bSAneesh Kumar K.V 	ee_pblock = ext_pblock(ex);
2396093a088bSAneesh Kumar K.V 
2397093a088bSAneesh Kumar K.V 	/* convert ee_pblock to 512 byte sectors */
2398093a088bSAneesh Kumar K.V 	ee_pblock = ee_pblock << (blkbits - 9);
2399093a088bSAneesh Kumar K.V 
2400093a088bSAneesh Kumar K.V 	while (ee_len > 0) {
2401093a088bSAneesh Kumar K.V 
2402093a088bSAneesh Kumar K.V 		if (ee_len > BIO_MAX_PAGES)
2403093a088bSAneesh Kumar K.V 			len = BIO_MAX_PAGES;
2404093a088bSAneesh Kumar K.V 		else
2405093a088bSAneesh Kumar K.V 			len = ee_len;
2406093a088bSAneesh Kumar K.V 
2407093a088bSAneesh Kumar K.V 		bio = bio_alloc(GFP_NOIO, len);
2408093a088bSAneesh Kumar K.V 		if (!bio)
2409093a088bSAneesh Kumar K.V 			return -ENOMEM;
2410093a088bSAneesh Kumar K.V 		bio->bi_sector = ee_pblock;
2411093a088bSAneesh Kumar K.V 		bio->bi_bdev   = inode->i_sb->s_bdev;
2412093a088bSAneesh Kumar K.V 
2413093a088bSAneesh Kumar K.V 		done = 0;
2414093a088bSAneesh Kumar K.V 		offset = 0;
2415093a088bSAneesh Kumar K.V 		while (done < len) {
2416093a088bSAneesh Kumar K.V 			ret = bio_add_page(bio, ZERO_PAGE(0),
2417093a088bSAneesh Kumar K.V 							blocksize, offset);
2418093a088bSAneesh Kumar K.V 			if (ret != blocksize) {
2419093a088bSAneesh Kumar K.V 				/*
2420093a088bSAneesh Kumar K.V 				 * We can't add any more pages because of
2421093a088bSAneesh Kumar K.V 				 * hardware limitations.  Start a new bio.
2422093a088bSAneesh Kumar K.V 				 */
2423093a088bSAneesh Kumar K.V 				break;
2424093a088bSAneesh Kumar K.V 			}
2425093a088bSAneesh Kumar K.V 			done++;
2426093a088bSAneesh Kumar K.V 			offset += blocksize;
2427093a088bSAneesh Kumar K.V 			if (offset >= PAGE_CACHE_SIZE)
2428093a088bSAneesh Kumar K.V 				offset = 0;
2429093a088bSAneesh Kumar K.V 		}
2430093a088bSAneesh Kumar K.V 
2431093a088bSAneesh Kumar K.V 		init_completion(&event);
2432093a088bSAneesh Kumar K.V 		bio->bi_private = &event;
2433093a088bSAneesh Kumar K.V 		bio->bi_end_io = bi_complete;
2434093a088bSAneesh Kumar K.V 		submit_bio(WRITE, bio);
2435093a088bSAneesh Kumar K.V 		wait_for_completion(&event);
2436093a088bSAneesh Kumar K.V 
2437093a088bSAneesh Kumar K.V 		if (test_bit(BIO_UPTODATE, &bio->bi_flags))
2438093a088bSAneesh Kumar K.V 			ret = 0;
2439093a088bSAneesh Kumar K.V 		else {
2440093a088bSAneesh Kumar K.V 			ret = -EIO;
2441093a088bSAneesh Kumar K.V 			break;
2442093a088bSAneesh Kumar K.V 		}
2443093a088bSAneesh Kumar K.V 		bio_put(bio);
2444093a088bSAneesh Kumar K.V 		ee_len    -= done;
2445093a088bSAneesh Kumar K.V 		ee_pblock += done  << (blkbits - 9);
2446093a088bSAneesh Kumar K.V 	}
2447093a088bSAneesh Kumar K.V 	return ret;
2448093a088bSAneesh Kumar K.V }
2449093a088bSAneesh Kumar K.V 
24503977c965SAneesh Kumar K.V #define EXT4_EXT_ZERO_LEN 7
24513977c965SAneesh Kumar K.V 
245256055d3aSAmit Arora /*
245356055d3aSAmit Arora  * This function is called by ext4_ext_get_blocks() if someone tries to write
245456055d3aSAmit Arora  * to an uninitialized extent. It may result in splitting the uninitialized
245556055d3aSAmit Arora  * extent into multiple extents (upto three - one initialized and two
245656055d3aSAmit Arora  * uninitialized).
245756055d3aSAmit Arora  * There are three possibilities:
245856055d3aSAmit Arora  *   a> There is no split required: Entire extent should be initialized
245956055d3aSAmit Arora  *   b> Splits in two extents: Write is happening at either end of the extent
246056055d3aSAmit Arora  *   c> Splits in three extents: Somone is writing in middle of the extent
246156055d3aSAmit Arora  */
2462725d26d3SAneesh Kumar K.V static int ext4_ext_convert_to_initialized(handle_t *handle,
2463725d26d3SAneesh Kumar K.V 						struct inode *inode,
246456055d3aSAmit Arora 						struct ext4_ext_path *path,
2465725d26d3SAneesh Kumar K.V 						ext4_lblk_t iblock,
2466498e5f24STheodore Ts'o 						unsigned int max_blocks)
246756055d3aSAmit Arora {
246895c3889cSAneesh Kumar K.V 	struct ext4_extent *ex, newex, orig_ex;
246956055d3aSAmit Arora 	struct ext4_extent *ex1 = NULL;
247056055d3aSAmit Arora 	struct ext4_extent *ex2 = NULL;
247156055d3aSAmit Arora 	struct ext4_extent *ex3 = NULL;
247256055d3aSAmit Arora 	struct ext4_extent_header *eh;
2473725d26d3SAneesh Kumar K.V 	ext4_lblk_t ee_block;
2474725d26d3SAneesh Kumar K.V 	unsigned int allocated, ee_len, depth;
247556055d3aSAmit Arora 	ext4_fsblk_t newblock;
247656055d3aSAmit Arora 	int err = 0;
247756055d3aSAmit Arora 	int ret = 0;
247856055d3aSAmit Arora 
247956055d3aSAmit Arora 	depth = ext_depth(inode);
248056055d3aSAmit Arora 	eh = path[depth].p_hdr;
248156055d3aSAmit Arora 	ex = path[depth].p_ext;
248256055d3aSAmit Arora 	ee_block = le32_to_cpu(ex->ee_block);
248356055d3aSAmit Arora 	ee_len = ext4_ext_get_actual_len(ex);
248456055d3aSAmit Arora 	allocated = ee_len - (iblock - ee_block);
248556055d3aSAmit Arora 	newblock = iblock - ee_block + ext_pblock(ex);
248656055d3aSAmit Arora 	ex2 = ex;
248795c3889cSAneesh Kumar K.V 	orig_ex.ee_block = ex->ee_block;
248895c3889cSAneesh Kumar K.V 	orig_ex.ee_len   = cpu_to_le16(ee_len);
248995c3889cSAneesh Kumar K.V 	ext4_ext_store_pblock(&orig_ex, ext_pblock(ex));
249056055d3aSAmit Arora 
24919df5643aSAneesh Kumar K.V 	err = ext4_ext_get_access(handle, inode, path + depth);
24929df5643aSAneesh Kumar K.V 	if (err)
24939df5643aSAneesh Kumar K.V 		goto out;
24943977c965SAneesh Kumar K.V 	/* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
24953977c965SAneesh Kumar K.V 	if (ee_len <= 2*EXT4_EXT_ZERO_LEN) {
24963977c965SAneesh Kumar K.V 		err =  ext4_ext_zeroout(inode, &orig_ex);
24973977c965SAneesh Kumar K.V 		if (err)
24983977c965SAneesh Kumar K.V 			goto fix_extent_len;
24993977c965SAneesh Kumar K.V 		/* update the extent length and mark as initialized */
25003977c965SAneesh Kumar K.V 		ex->ee_block = orig_ex.ee_block;
25013977c965SAneesh Kumar K.V 		ex->ee_len   = orig_ex.ee_len;
25023977c965SAneesh Kumar K.V 		ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
25033977c965SAneesh Kumar K.V 		ext4_ext_dirty(handle, inode, path + depth);
2504161e7b7cSAneesh Kumar K.V 		/* zeroed the full extent */
2505161e7b7cSAneesh Kumar K.V 		return allocated;
25063977c965SAneesh Kumar K.V 	}
25079df5643aSAneesh Kumar K.V 
250856055d3aSAmit Arora 	/* ex1: ee_block to iblock - 1 : uninitialized */
250956055d3aSAmit Arora 	if (iblock > ee_block) {
251056055d3aSAmit Arora 		ex1 = ex;
251156055d3aSAmit Arora 		ex1->ee_len = cpu_to_le16(iblock - ee_block);
251256055d3aSAmit Arora 		ext4_ext_mark_uninitialized(ex1);
251356055d3aSAmit Arora 		ex2 = &newex;
251456055d3aSAmit Arora 	}
251556055d3aSAmit Arora 	/*
251656055d3aSAmit Arora 	 * for sanity, update the length of the ex2 extent before
251756055d3aSAmit Arora 	 * we insert ex3, if ex1 is NULL. This is to avoid temporary
251856055d3aSAmit Arora 	 * overlap of blocks.
251956055d3aSAmit Arora 	 */
252056055d3aSAmit Arora 	if (!ex1 && allocated > max_blocks)
252156055d3aSAmit Arora 		ex2->ee_len = cpu_to_le16(max_blocks);
252256055d3aSAmit Arora 	/* ex3: to ee_block + ee_len : uninitialised */
252356055d3aSAmit Arora 	if (allocated > max_blocks) {
252456055d3aSAmit Arora 		unsigned int newdepth;
25253977c965SAneesh Kumar K.V 		/* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */
25263977c965SAneesh Kumar K.V 		if (allocated <= EXT4_EXT_ZERO_LEN) {
2527d03856bdSAneesh Kumar K.V 			/*
2528d03856bdSAneesh Kumar K.V 			 * iblock == ee_block is handled by the zerouout
2529d03856bdSAneesh Kumar K.V 			 * at the beginning.
2530d03856bdSAneesh Kumar K.V 			 * Mark first half uninitialized.
25313977c965SAneesh Kumar K.V 			 * Mark second half initialized and zero out the
25323977c965SAneesh Kumar K.V 			 * initialized extent
25333977c965SAneesh Kumar K.V 			 */
25343977c965SAneesh Kumar K.V 			ex->ee_block = orig_ex.ee_block;
25353977c965SAneesh Kumar K.V 			ex->ee_len   = cpu_to_le16(ee_len - allocated);
25363977c965SAneesh Kumar K.V 			ext4_ext_mark_uninitialized(ex);
25373977c965SAneesh Kumar K.V 			ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
25383977c965SAneesh Kumar K.V 			ext4_ext_dirty(handle, inode, path + depth);
25393977c965SAneesh Kumar K.V 
25403977c965SAneesh Kumar K.V 			ex3 = &newex;
25413977c965SAneesh Kumar K.V 			ex3->ee_block = cpu_to_le32(iblock);
25423977c965SAneesh Kumar K.V 			ext4_ext_store_pblock(ex3, newblock);
25433977c965SAneesh Kumar K.V 			ex3->ee_len = cpu_to_le16(allocated);
25443977c965SAneesh Kumar K.V 			err = ext4_ext_insert_extent(handle, inode, path, ex3);
25453977c965SAneesh Kumar K.V 			if (err == -ENOSPC) {
25463977c965SAneesh Kumar K.V 				err =  ext4_ext_zeroout(inode, &orig_ex);
25473977c965SAneesh Kumar K.V 				if (err)
25483977c965SAneesh Kumar K.V 					goto fix_extent_len;
25493977c965SAneesh Kumar K.V 				ex->ee_block = orig_ex.ee_block;
25503977c965SAneesh Kumar K.V 				ex->ee_len   = orig_ex.ee_len;
25513977c965SAneesh Kumar K.V 				ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
25523977c965SAneesh Kumar K.V 				ext4_ext_dirty(handle, inode, path + depth);
2553d03856bdSAneesh Kumar K.V 				/* blocks available from iblock */
2554161e7b7cSAneesh Kumar K.V 				return allocated;
25553977c965SAneesh Kumar K.V 
25563977c965SAneesh Kumar K.V 			} else if (err)
25573977c965SAneesh Kumar K.V 				goto fix_extent_len;
25583977c965SAneesh Kumar K.V 
2559161e7b7cSAneesh Kumar K.V 			/*
2560161e7b7cSAneesh Kumar K.V 			 * We need to zero out the second half because
2561161e7b7cSAneesh Kumar K.V 			 * an fallocate request can update file size and
2562161e7b7cSAneesh Kumar K.V 			 * converting the second half to initialized extent
2563161e7b7cSAneesh Kumar K.V 			 * implies that we can leak some junk data to user
2564161e7b7cSAneesh Kumar K.V 			 * space.
2565161e7b7cSAneesh Kumar K.V 			 */
2566161e7b7cSAneesh Kumar K.V 			err =  ext4_ext_zeroout(inode, ex3);
2567161e7b7cSAneesh Kumar K.V 			if (err) {
2568161e7b7cSAneesh Kumar K.V 				/*
2569161e7b7cSAneesh Kumar K.V 				 * We should actually mark the
2570161e7b7cSAneesh Kumar K.V 				 * second half as uninit and return error
2571161e7b7cSAneesh Kumar K.V 				 * Insert would have changed the extent
2572161e7b7cSAneesh Kumar K.V 				 */
2573161e7b7cSAneesh Kumar K.V 				depth = ext_depth(inode);
2574161e7b7cSAneesh Kumar K.V 				ext4_ext_drop_refs(path);
2575161e7b7cSAneesh Kumar K.V 				path = ext4_ext_find_extent(inode,
2576161e7b7cSAneesh Kumar K.V 								iblock, path);
2577161e7b7cSAneesh Kumar K.V 				if (IS_ERR(path)) {
2578161e7b7cSAneesh Kumar K.V 					err = PTR_ERR(path);
2579161e7b7cSAneesh Kumar K.V 					return err;
2580161e7b7cSAneesh Kumar K.V 				}
2581d03856bdSAneesh Kumar K.V 				/* get the second half extent details */
2582161e7b7cSAneesh Kumar K.V 				ex = path[depth].p_ext;
2583161e7b7cSAneesh Kumar K.V 				err = ext4_ext_get_access(handle, inode,
2584161e7b7cSAneesh Kumar K.V 								path + depth);
2585161e7b7cSAneesh Kumar K.V 				if (err)
2586161e7b7cSAneesh Kumar K.V 					return err;
2587161e7b7cSAneesh Kumar K.V 				ext4_ext_mark_uninitialized(ex);
2588161e7b7cSAneesh Kumar K.V 				ext4_ext_dirty(handle, inode, path + depth);
2589161e7b7cSAneesh Kumar K.V 				return err;
2590161e7b7cSAneesh Kumar K.V 			}
2591161e7b7cSAneesh Kumar K.V 
2592161e7b7cSAneesh Kumar K.V 			/* zeroed the second half */
25933977c965SAneesh Kumar K.V 			return allocated;
25943977c965SAneesh Kumar K.V 		}
259556055d3aSAmit Arora 		ex3 = &newex;
259656055d3aSAmit Arora 		ex3->ee_block = cpu_to_le32(iblock + max_blocks);
259756055d3aSAmit Arora 		ext4_ext_store_pblock(ex3, newblock + max_blocks);
259856055d3aSAmit Arora 		ex3->ee_len = cpu_to_le16(allocated - max_blocks);
259956055d3aSAmit Arora 		ext4_ext_mark_uninitialized(ex3);
260056055d3aSAmit Arora 		err = ext4_ext_insert_extent(handle, inode, path, ex3);
2601093a088bSAneesh Kumar K.V 		if (err == -ENOSPC) {
2602093a088bSAneesh Kumar K.V 			err =  ext4_ext_zeroout(inode, &orig_ex);
2603093a088bSAneesh Kumar K.V 			if (err)
2604093a088bSAneesh Kumar K.V 				goto fix_extent_len;
2605093a088bSAneesh Kumar K.V 			/* update the extent length and mark as initialized */
260695c3889cSAneesh Kumar K.V 			ex->ee_block = orig_ex.ee_block;
260795c3889cSAneesh Kumar K.V 			ex->ee_len   = orig_ex.ee_len;
260895c3889cSAneesh Kumar K.V 			ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
260995c3889cSAneesh Kumar K.V 			ext4_ext_dirty(handle, inode, path + depth);
2610161e7b7cSAneesh Kumar K.V 			/* zeroed the full extent */
2611d03856bdSAneesh Kumar K.V 			/* blocks available from iblock */
2612161e7b7cSAneesh Kumar K.V 			return allocated;
2613093a088bSAneesh Kumar K.V 
2614093a088bSAneesh Kumar K.V 		} else if (err)
2615093a088bSAneesh Kumar K.V 			goto fix_extent_len;
261656055d3aSAmit Arora 		/*
261756055d3aSAmit Arora 		 * The depth, and hence eh & ex might change
261856055d3aSAmit Arora 		 * as part of the insert above.
261956055d3aSAmit Arora 		 */
262056055d3aSAmit Arora 		newdepth = ext_depth(inode);
262195c3889cSAneesh Kumar K.V 		/*
262273ac36eaSColy Li 		 * update the extent length after successful insert of the
262395c3889cSAneesh Kumar K.V 		 * split extent
262495c3889cSAneesh Kumar K.V 		 */
262595c3889cSAneesh Kumar K.V 		orig_ex.ee_len = cpu_to_le16(ee_len -
262695c3889cSAneesh Kumar K.V 						ext4_ext_get_actual_len(ex3));
262756055d3aSAmit Arora 		depth = newdepth;
2628b35905c1SAneesh Kumar K.V 		ext4_ext_drop_refs(path);
2629b35905c1SAneesh Kumar K.V 		path = ext4_ext_find_extent(inode, iblock, path);
263056055d3aSAmit Arora 		if (IS_ERR(path)) {
263156055d3aSAmit Arora 			err = PTR_ERR(path);
263256055d3aSAmit Arora 			goto out;
263356055d3aSAmit Arora 		}
263456055d3aSAmit Arora 		eh = path[depth].p_hdr;
263556055d3aSAmit Arora 		ex = path[depth].p_ext;
263656055d3aSAmit Arora 		if (ex2 != &newex)
263756055d3aSAmit Arora 			ex2 = ex;
26389df5643aSAneesh Kumar K.V 
26399df5643aSAneesh Kumar K.V 		err = ext4_ext_get_access(handle, inode, path + depth);
26409df5643aSAneesh Kumar K.V 		if (err)
26419df5643aSAneesh Kumar K.V 			goto out;
2642d03856bdSAneesh Kumar K.V 
264356055d3aSAmit Arora 		allocated = max_blocks;
26443977c965SAneesh Kumar K.V 
26453977c965SAneesh Kumar K.V 		/* If extent has less than EXT4_EXT_ZERO_LEN and we are trying
26463977c965SAneesh Kumar K.V 		 * to insert a extent in the middle zerout directly
26473977c965SAneesh Kumar K.V 		 * otherwise give the extent a chance to merge to left
26483977c965SAneesh Kumar K.V 		 */
26493977c965SAneesh Kumar K.V 		if (le16_to_cpu(orig_ex.ee_len) <= EXT4_EXT_ZERO_LEN &&
26503977c965SAneesh Kumar K.V 							iblock != ee_block) {
26513977c965SAneesh Kumar K.V 			err =  ext4_ext_zeroout(inode, &orig_ex);
26523977c965SAneesh Kumar K.V 			if (err)
26533977c965SAneesh Kumar K.V 				goto fix_extent_len;
26543977c965SAneesh Kumar K.V 			/* update the extent length and mark as initialized */
26553977c965SAneesh Kumar K.V 			ex->ee_block = orig_ex.ee_block;
26563977c965SAneesh Kumar K.V 			ex->ee_len   = orig_ex.ee_len;
26573977c965SAneesh Kumar K.V 			ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
26583977c965SAneesh Kumar K.V 			ext4_ext_dirty(handle, inode, path + depth);
2659161e7b7cSAneesh Kumar K.V 			/* zero out the first half */
2660d03856bdSAneesh Kumar K.V 			/* blocks available from iblock */
2661161e7b7cSAneesh Kumar K.V 			return allocated;
26623977c965SAneesh Kumar K.V 		}
266356055d3aSAmit Arora 	}
266456055d3aSAmit Arora 	/*
266556055d3aSAmit Arora 	 * If there was a change of depth as part of the
266656055d3aSAmit Arora 	 * insertion of ex3 above, we need to update the length
266756055d3aSAmit Arora 	 * of the ex1 extent again here
266856055d3aSAmit Arora 	 */
266956055d3aSAmit Arora 	if (ex1 && ex1 != ex) {
267056055d3aSAmit Arora 		ex1 = ex;
267156055d3aSAmit Arora 		ex1->ee_len = cpu_to_le16(iblock - ee_block);
267256055d3aSAmit Arora 		ext4_ext_mark_uninitialized(ex1);
267356055d3aSAmit Arora 		ex2 = &newex;
267456055d3aSAmit Arora 	}
267556055d3aSAmit Arora 	/* ex2: iblock to iblock + maxblocks-1 : initialised */
267656055d3aSAmit Arora 	ex2->ee_block = cpu_to_le32(iblock);
267756055d3aSAmit Arora 	ext4_ext_store_pblock(ex2, newblock);
267856055d3aSAmit Arora 	ex2->ee_len = cpu_to_le16(allocated);
267956055d3aSAmit Arora 	if (ex2 != ex)
268056055d3aSAmit Arora 		goto insert;
268156055d3aSAmit Arora 	/*
268256055d3aSAmit Arora 	 * New (initialized) extent starts from the first block
268356055d3aSAmit Arora 	 * in the current extent. i.e., ex2 == ex
268456055d3aSAmit Arora 	 * We have to see if it can be merged with the extent
268556055d3aSAmit Arora 	 * on the left.
268656055d3aSAmit Arora 	 */
268756055d3aSAmit Arora 	if (ex2 > EXT_FIRST_EXTENT(eh)) {
268856055d3aSAmit Arora 		/*
268956055d3aSAmit Arora 		 * To merge left, pass "ex2 - 1" to try_to_merge(),
269056055d3aSAmit Arora 		 * since it merges towards right _only_.
269156055d3aSAmit Arora 		 */
269256055d3aSAmit Arora 		ret = ext4_ext_try_to_merge(inode, path, ex2 - 1);
269356055d3aSAmit Arora 		if (ret) {
269456055d3aSAmit Arora 			err = ext4_ext_correct_indexes(handle, inode, path);
269556055d3aSAmit Arora 			if (err)
269656055d3aSAmit Arora 				goto out;
269756055d3aSAmit Arora 			depth = ext_depth(inode);
269856055d3aSAmit Arora 			ex2--;
269956055d3aSAmit Arora 		}
270056055d3aSAmit Arora 	}
270156055d3aSAmit Arora 	/*
270256055d3aSAmit Arora 	 * Try to Merge towards right. This might be required
270356055d3aSAmit Arora 	 * only when the whole extent is being written to.
270456055d3aSAmit Arora 	 * i.e. ex2 == ex and ex3 == NULL.
270556055d3aSAmit Arora 	 */
270656055d3aSAmit Arora 	if (!ex3) {
270756055d3aSAmit Arora 		ret = ext4_ext_try_to_merge(inode, path, ex2);
270856055d3aSAmit Arora 		if (ret) {
270956055d3aSAmit Arora 			err = ext4_ext_correct_indexes(handle, inode, path);
271056055d3aSAmit Arora 			if (err)
271156055d3aSAmit Arora 				goto out;
271256055d3aSAmit Arora 		}
271356055d3aSAmit Arora 	}
271456055d3aSAmit Arora 	/* Mark modified extent as dirty */
271556055d3aSAmit Arora 	err = ext4_ext_dirty(handle, inode, path + depth);
271656055d3aSAmit Arora 	goto out;
271756055d3aSAmit Arora insert:
271856055d3aSAmit Arora 	err = ext4_ext_insert_extent(handle, inode, path, &newex);
2719093a088bSAneesh Kumar K.V 	if (err == -ENOSPC) {
2720093a088bSAneesh Kumar K.V 		err =  ext4_ext_zeroout(inode, &orig_ex);
2721093a088bSAneesh Kumar K.V 		if (err)
2722093a088bSAneesh Kumar K.V 			goto fix_extent_len;
2723093a088bSAneesh Kumar K.V 		/* update the extent length and mark as initialized */
2724093a088bSAneesh Kumar K.V 		ex->ee_block = orig_ex.ee_block;
2725093a088bSAneesh Kumar K.V 		ex->ee_len   = orig_ex.ee_len;
2726093a088bSAneesh Kumar K.V 		ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2727093a088bSAneesh Kumar K.V 		ext4_ext_dirty(handle, inode, path + depth);
2728161e7b7cSAneesh Kumar K.V 		/* zero out the first half */
2729161e7b7cSAneesh Kumar K.V 		return allocated;
2730093a088bSAneesh Kumar K.V 	} else if (err)
2731093a088bSAneesh Kumar K.V 		goto fix_extent_len;
2732093a088bSAneesh Kumar K.V out:
2733093a088bSAneesh Kumar K.V 	return err ? err : allocated;
2734093a088bSAneesh Kumar K.V 
2735093a088bSAneesh Kumar K.V fix_extent_len:
273695c3889cSAneesh Kumar K.V 	ex->ee_block = orig_ex.ee_block;
273795c3889cSAneesh Kumar K.V 	ex->ee_len   = orig_ex.ee_len;
273895c3889cSAneesh Kumar K.V 	ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
273995c3889cSAneesh Kumar K.V 	ext4_ext_mark_uninitialized(ex);
274095c3889cSAneesh Kumar K.V 	ext4_ext_dirty(handle, inode, path + depth);
2741093a088bSAneesh Kumar K.V 	return err;
274256055d3aSAmit Arora }
274356055d3aSAmit Arora 
2744c278bfecSAneesh Kumar K.V /*
2745f5ab0d1fSMingming Cao  * Block allocation/map/preallocation routine for extents based files
2746f5ab0d1fSMingming Cao  *
2747f5ab0d1fSMingming Cao  *
2748c278bfecSAneesh Kumar K.V  * Need to be called with
27490e855ac8SAneesh Kumar K.V  * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
27500e855ac8SAneesh Kumar K.V  * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
2751f5ab0d1fSMingming Cao  *
2752f5ab0d1fSMingming Cao  * return > 0, number of of blocks already mapped/allocated
2753f5ab0d1fSMingming Cao  *          if create == 0 and these are pre-allocated blocks
2754f5ab0d1fSMingming Cao  *          	buffer head is unmapped
2755f5ab0d1fSMingming Cao  *          otherwise blocks are mapped
2756f5ab0d1fSMingming Cao  *
2757f5ab0d1fSMingming Cao  * return = 0, if plain look up failed (blocks have not been allocated)
2758f5ab0d1fSMingming Cao  *          buffer head is unmapped
2759f5ab0d1fSMingming Cao  *
2760f5ab0d1fSMingming Cao  * return < 0, error case.
2761c278bfecSAneesh Kumar K.V  */
2762f65e6fbaSAlex Tomas int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
2763725d26d3SAneesh Kumar K.V 			ext4_lblk_t iblock,
2764498e5f24STheodore Ts'o 			unsigned int max_blocks, struct buffer_head *bh_result,
2765a86c6181SAlex Tomas 			int create, int extend_disksize)
2766a86c6181SAlex Tomas {
2767a86c6181SAlex Tomas 	struct ext4_ext_path *path = NULL;
276856055d3aSAmit Arora 	struct ext4_extent_header *eh;
2769a86c6181SAlex Tomas 	struct ext4_extent newex, *ex;
2770498e5f24STheodore Ts'o 	ext4_fsblk_t newblock;
2771498e5f24STheodore Ts'o 	int err = 0, depth, ret, cache_type;
2772498e5f24STheodore Ts'o 	unsigned int allocated = 0;
2773c9de560dSAlex Tomas 	struct ext4_allocation_request ar;
277461628a3fSMingming Cao 	loff_t disksize;
2775a86c6181SAlex Tomas 
2776a86c6181SAlex Tomas 	__clear_bit(BH_New, &bh_result->b_state);
2777498e5f24STheodore Ts'o 	ext_debug("blocks %u/%u requested for inode %u\n",
2778bba90743SEric Sandeen 			iblock, max_blocks, inode->i_ino);
2779a86c6181SAlex Tomas 
2780a86c6181SAlex Tomas 	/* check in cache */
2781498e5f24STheodore Ts'o 	cache_type = ext4_ext_in_cache(inode, iblock, &newex);
2782498e5f24STheodore Ts'o 	if (cache_type) {
2783498e5f24STheodore Ts'o 		if (cache_type == EXT4_EXT_CACHE_GAP) {
2784a86c6181SAlex Tomas 			if (!create) {
278556055d3aSAmit Arora 				/*
278656055d3aSAmit Arora 				 * block isn't allocated yet and
278756055d3aSAmit Arora 				 * user doesn't want to allocate it
278856055d3aSAmit Arora 				 */
2789a86c6181SAlex Tomas 				goto out2;
2790a86c6181SAlex Tomas 			}
2791a86c6181SAlex Tomas 			/* we should allocate requested block */
2792498e5f24STheodore Ts'o 		} else if (cache_type == EXT4_EXT_CACHE_EXTENT) {
2793a86c6181SAlex Tomas 			/* block is already allocated */
2794a86c6181SAlex Tomas 			newblock = iblock
2795a86c6181SAlex Tomas 				   - le32_to_cpu(newex.ee_block)
2796f65e6fbaSAlex Tomas 				   + ext_pblock(&newex);
2797d0d856e8SRandy Dunlap 			/* number of remaining blocks in the extent */
2798b939e376SAneesh Kumar K.V 			allocated = ext4_ext_get_actual_len(&newex) -
2799a86c6181SAlex Tomas 					(iblock - le32_to_cpu(newex.ee_block));
2800a86c6181SAlex Tomas 			goto out;
2801a86c6181SAlex Tomas 		} else {
2802a86c6181SAlex Tomas 			BUG();
2803a86c6181SAlex Tomas 		}
2804a86c6181SAlex Tomas 	}
2805a86c6181SAlex Tomas 
2806a86c6181SAlex Tomas 	/* find extent for this block */
2807a86c6181SAlex Tomas 	path = ext4_ext_find_extent(inode, iblock, NULL);
2808a86c6181SAlex Tomas 	if (IS_ERR(path)) {
2809a86c6181SAlex Tomas 		err = PTR_ERR(path);
2810a86c6181SAlex Tomas 		path = NULL;
2811a86c6181SAlex Tomas 		goto out2;
2812a86c6181SAlex Tomas 	}
2813a86c6181SAlex Tomas 
2814a86c6181SAlex Tomas 	depth = ext_depth(inode);
2815a86c6181SAlex Tomas 
2816a86c6181SAlex Tomas 	/*
2817d0d856e8SRandy Dunlap 	 * consistent leaf must not be empty;
2818d0d856e8SRandy Dunlap 	 * this situation is possible, though, _during_ tree modification;
2819a86c6181SAlex Tomas 	 * this is why assert can't be put in ext4_ext_find_extent()
2820a86c6181SAlex Tomas 	 */
2821a86c6181SAlex Tomas 	BUG_ON(path[depth].p_ext == NULL && depth != 0);
282256055d3aSAmit Arora 	eh = path[depth].p_hdr;
2823a86c6181SAlex Tomas 
28247e028976SAvantika Mathur 	ex = path[depth].p_ext;
28257e028976SAvantika Mathur 	if (ex) {
2826725d26d3SAneesh Kumar K.V 		ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
2827f65e6fbaSAlex Tomas 		ext4_fsblk_t ee_start = ext_pblock(ex);
2828a2df2a63SAmit Arora 		unsigned short ee_len;
2829471d4011SSuparna Bhattacharya 
2830471d4011SSuparna Bhattacharya 		/*
2831471d4011SSuparna Bhattacharya 		 * Uninitialized extents are treated as holes, except that
283256055d3aSAmit Arora 		 * we split out initialized portions during a write.
2833471d4011SSuparna Bhattacharya 		 */
2834a2df2a63SAmit Arora 		ee_len = ext4_ext_get_actual_len(ex);
2835d0d856e8SRandy Dunlap 		/* if found extent covers block, simply return it */
2836a86c6181SAlex Tomas 		if (iblock >= ee_block && iblock < ee_block + ee_len) {
2837a86c6181SAlex Tomas 			newblock = iblock - ee_block + ee_start;
2838d0d856e8SRandy Dunlap 			/* number of remaining blocks in the extent */
2839a86c6181SAlex Tomas 			allocated = ee_len - (iblock - ee_block);
2840bba90743SEric Sandeen 			ext_debug("%u fit into %lu:%d -> %llu\n", iblock,
2841a86c6181SAlex Tomas 					ee_block, ee_len, newblock);
284256055d3aSAmit Arora 
2843a2df2a63SAmit Arora 			/* Do not put uninitialized extent in the cache */
284456055d3aSAmit Arora 			if (!ext4_ext_is_uninitialized(ex)) {
2845a2df2a63SAmit Arora 				ext4_ext_put_in_cache(inode, ee_block,
2846a2df2a63SAmit Arora 							ee_len, ee_start,
2847a2df2a63SAmit Arora 							EXT4_EXT_CACHE_EXTENT);
2848a86c6181SAlex Tomas 				goto out;
2849a86c6181SAlex Tomas 			}
285056055d3aSAmit Arora 			if (create == EXT4_CREATE_UNINITIALIZED_EXT)
285156055d3aSAmit Arora 				goto out;
2852e067ba00SAneesh Kumar K.V 			if (!create) {
2853e067ba00SAneesh Kumar K.V 				/*
2854e067ba00SAneesh Kumar K.V 				 * We have blocks reserved already.  We
2855e067ba00SAneesh Kumar K.V 				 * return allocated blocks so that delalloc
2856e067ba00SAneesh Kumar K.V 				 * won't do block reservation for us.  But
2857e067ba00SAneesh Kumar K.V 				 * the buffer head will be unmapped so that
2858e067ba00SAneesh Kumar K.V 				 * a read from the block returns 0s.
2859e067ba00SAneesh Kumar K.V 				 */
2860e067ba00SAneesh Kumar K.V 				if (allocated > max_blocks)
2861e067ba00SAneesh Kumar K.V 					allocated = max_blocks;
2862953e622bSEric Sandeen 				set_buffer_unwritten(bh_result);
286356055d3aSAmit Arora 				goto out2;
2864e067ba00SAneesh Kumar K.V 			}
286556055d3aSAmit Arora 
286656055d3aSAmit Arora 			ret = ext4_ext_convert_to_initialized(handle, inode,
286756055d3aSAmit Arora 								path, iblock,
286856055d3aSAmit Arora 								max_blocks);
2869dbf9d7daSDmitry Monakhov 			if (ret <= 0) {
2870dbf9d7daSDmitry Monakhov 				err = ret;
287156055d3aSAmit Arora 				goto out2;
2872dbf9d7daSDmitry Monakhov 			} else
287356055d3aSAmit Arora 				allocated = ret;
287456055d3aSAmit Arora 			goto outnew;
287556055d3aSAmit Arora 		}
2876a86c6181SAlex Tomas 	}
2877a86c6181SAlex Tomas 
2878a86c6181SAlex Tomas 	/*
2879d0d856e8SRandy Dunlap 	 * requested block isn't allocated yet;
2880a86c6181SAlex Tomas 	 * we couldn't try to create block if create flag is zero
2881a86c6181SAlex Tomas 	 */
2882a86c6181SAlex Tomas 	if (!create) {
288356055d3aSAmit Arora 		/*
288456055d3aSAmit Arora 		 * put just found gap into cache to speed up
288556055d3aSAmit Arora 		 * subsequent requests
288656055d3aSAmit Arora 		 */
2887a86c6181SAlex Tomas 		ext4_ext_put_gap_in_cache(inode, path, iblock);
2888a86c6181SAlex Tomas 		goto out2;
2889a86c6181SAlex Tomas 	}
2890a86c6181SAlex Tomas 	/*
2891c2ea3fdeSTheodore Ts'o 	 * Okay, we need to do block allocation.
2892a86c6181SAlex Tomas 	 */
2893a86c6181SAlex Tomas 
2894c9de560dSAlex Tomas 	/* find neighbour allocated blocks */
2895c9de560dSAlex Tomas 	ar.lleft = iblock;
2896c9de560dSAlex Tomas 	err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
2897c9de560dSAlex Tomas 	if (err)
2898c9de560dSAlex Tomas 		goto out2;
2899c9de560dSAlex Tomas 	ar.lright = iblock;
2900c9de560dSAlex Tomas 	err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright);
2901c9de560dSAlex Tomas 	if (err)
2902c9de560dSAlex Tomas 		goto out2;
290325d14f98SAmit Arora 
2904749269faSAmit Arora 	/*
2905749269faSAmit Arora 	 * See if request is beyond maximum number of blocks we can have in
2906749269faSAmit Arora 	 * a single extent. For an initialized extent this limit is
2907749269faSAmit Arora 	 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
2908749269faSAmit Arora 	 * EXT_UNINIT_MAX_LEN.
2909749269faSAmit Arora 	 */
2910749269faSAmit Arora 	if (max_blocks > EXT_INIT_MAX_LEN &&
2911749269faSAmit Arora 	    create != EXT4_CREATE_UNINITIALIZED_EXT)
2912749269faSAmit Arora 		max_blocks = EXT_INIT_MAX_LEN;
2913749269faSAmit Arora 	else if (max_blocks > EXT_UNINIT_MAX_LEN &&
2914749269faSAmit Arora 		 create == EXT4_CREATE_UNINITIALIZED_EXT)
2915749269faSAmit Arora 		max_blocks = EXT_UNINIT_MAX_LEN;
2916749269faSAmit Arora 
291725d14f98SAmit Arora 	/* Check if we can really insert (iblock)::(iblock+max_blocks) extent */
291825d14f98SAmit Arora 	newex.ee_block = cpu_to_le32(iblock);
291925d14f98SAmit Arora 	newex.ee_len = cpu_to_le16(max_blocks);
292025d14f98SAmit Arora 	err = ext4_ext_check_overlap(inode, &newex, path);
292125d14f98SAmit Arora 	if (err)
2922b939e376SAneesh Kumar K.V 		allocated = ext4_ext_get_actual_len(&newex);
292325d14f98SAmit Arora 	else
2924a86c6181SAlex Tomas 		allocated = max_blocks;
2925c9de560dSAlex Tomas 
2926c9de560dSAlex Tomas 	/* allocate new block */
2927c9de560dSAlex Tomas 	ar.inode = inode;
2928c9de560dSAlex Tomas 	ar.goal = ext4_ext_find_goal(inode, path, iblock);
2929c9de560dSAlex Tomas 	ar.logical = iblock;
2930c9de560dSAlex Tomas 	ar.len = allocated;
2931c9de560dSAlex Tomas 	if (S_ISREG(inode->i_mode))
2932c9de560dSAlex Tomas 		ar.flags = EXT4_MB_HINT_DATA;
2933c9de560dSAlex Tomas 	else
2934c9de560dSAlex Tomas 		/* disable in-core preallocation for non-regular files */
2935c9de560dSAlex Tomas 		ar.flags = 0;
2936c9de560dSAlex Tomas 	newblock = ext4_mb_new_blocks(handle, &ar, &err);
2937a86c6181SAlex Tomas 	if (!newblock)
2938a86c6181SAlex Tomas 		goto out2;
29392ae02107SMingming Cao 	ext_debug("allocate new block: goal %llu, found %llu/%lu\n",
2940498e5f24STheodore Ts'o 		  ar.goal, newblock, allocated);
2941a86c6181SAlex Tomas 
2942a86c6181SAlex Tomas 	/* try to insert new extent into found leaf and return */
2943f65e6fbaSAlex Tomas 	ext4_ext_store_pblock(&newex, newblock);
2944c9de560dSAlex Tomas 	newex.ee_len = cpu_to_le16(ar.len);
2945a2df2a63SAmit Arora 	if (create == EXT4_CREATE_UNINITIALIZED_EXT)  /* Mark uninitialized */
2946a2df2a63SAmit Arora 		ext4_ext_mark_uninitialized(&newex);
2947a86c6181SAlex Tomas 	err = ext4_ext_insert_extent(handle, inode, path, &newex);
2948315054f0SAlex Tomas 	if (err) {
2949315054f0SAlex Tomas 		/* free data blocks we just allocated */
2950c9de560dSAlex Tomas 		/* not a good idea to call discard here directly,
2951c9de560dSAlex Tomas 		 * but otherwise we'd need to call it every free() */
2952c2ea3fdeSTheodore Ts'o 		ext4_discard_preallocations(inode);
2953315054f0SAlex Tomas 		ext4_free_blocks(handle, inode, ext_pblock(&newex),
2954b939e376SAneesh Kumar K.V 					ext4_ext_get_actual_len(&newex), 0);
2955a86c6181SAlex Tomas 		goto out2;
2956315054f0SAlex Tomas 	}
2957a86c6181SAlex Tomas 
2958a86c6181SAlex Tomas 	/* previous routine could use block we allocated */
2959f65e6fbaSAlex Tomas 	newblock = ext_pblock(&newex);
2960b939e376SAneesh Kumar K.V 	allocated = ext4_ext_get_actual_len(&newex);
296156055d3aSAmit Arora outnew:
296261628a3fSMingming Cao 	if (extend_disksize) {
296361628a3fSMingming Cao 		disksize = ((loff_t) iblock + ar.len) << inode->i_blkbits;
296461628a3fSMingming Cao 		if (disksize > i_size_read(inode))
296561628a3fSMingming Cao 			disksize = i_size_read(inode);
296661628a3fSMingming Cao 		if (disksize > EXT4_I(inode)->i_disksize)
296761628a3fSMingming Cao 			EXT4_I(inode)->i_disksize = disksize;
296861628a3fSMingming Cao 	}
2969a379cd1dSAneesh Kumar K.V 
2970953e622bSEric Sandeen 	set_buffer_new(bh_result);
2971a86c6181SAlex Tomas 
2972a2df2a63SAmit Arora 	/* Cache only when it is _not_ an uninitialized extent */
2973a2df2a63SAmit Arora 	if (create != EXT4_CREATE_UNINITIALIZED_EXT)
2974a86c6181SAlex Tomas 		ext4_ext_put_in_cache(inode, iblock, allocated, newblock,
2975a86c6181SAlex Tomas 						EXT4_EXT_CACHE_EXTENT);
2976a86c6181SAlex Tomas out:
2977a86c6181SAlex Tomas 	if (allocated > max_blocks)
2978a86c6181SAlex Tomas 		allocated = max_blocks;
2979a86c6181SAlex Tomas 	ext4_ext_show_leaf(inode, path);
2980953e622bSEric Sandeen 	set_buffer_mapped(bh_result);
2981a86c6181SAlex Tomas 	bh_result->b_bdev = inode->i_sb->s_bdev;
2982a86c6181SAlex Tomas 	bh_result->b_blocknr = newblock;
2983a86c6181SAlex Tomas out2:
2984a86c6181SAlex Tomas 	if (path) {
2985a86c6181SAlex Tomas 		ext4_ext_drop_refs(path);
2986a86c6181SAlex Tomas 		kfree(path);
2987a86c6181SAlex Tomas 	}
2988a86c6181SAlex Tomas 	return err ? err : allocated;
2989a86c6181SAlex Tomas }
2990a86c6181SAlex Tomas 
2991cf108bcaSJan Kara void ext4_ext_truncate(struct inode *inode)
2992a86c6181SAlex Tomas {
2993a86c6181SAlex Tomas 	struct address_space *mapping = inode->i_mapping;
2994a86c6181SAlex Tomas 	struct super_block *sb = inode->i_sb;
2995725d26d3SAneesh Kumar K.V 	ext4_lblk_t last_block;
2996a86c6181SAlex Tomas 	handle_t *handle;
2997a86c6181SAlex Tomas 	int err = 0;
2998a86c6181SAlex Tomas 
2999a86c6181SAlex Tomas 	/*
3000a86c6181SAlex Tomas 	 * probably first extent we're gonna free will be last in block
3001a86c6181SAlex Tomas 	 */
3002f3bd1f3fSMingming Cao 	err = ext4_writepage_trans_blocks(inode);
3003a86c6181SAlex Tomas 	handle = ext4_journal_start(inode, err);
3004cf108bcaSJan Kara 	if (IS_ERR(handle))
3005a86c6181SAlex Tomas 		return;
3006a86c6181SAlex Tomas 
3007cf108bcaSJan Kara 	if (inode->i_size & (sb->s_blocksize - 1))
3008cf108bcaSJan Kara 		ext4_block_truncate_page(handle, mapping, inode->i_size);
3009a86c6181SAlex Tomas 
30109ddfc3dcSJan Kara 	if (ext4_orphan_add(handle, inode))
30119ddfc3dcSJan Kara 		goto out_stop;
30129ddfc3dcSJan Kara 
30130e855ac8SAneesh Kumar K.V 	down_write(&EXT4_I(inode)->i_data_sem);
3014a86c6181SAlex Tomas 	ext4_ext_invalidate_cache(inode);
3015a86c6181SAlex Tomas 
3016c2ea3fdeSTheodore Ts'o 	ext4_discard_preallocations(inode);
3017c9de560dSAlex Tomas 
3018a86c6181SAlex Tomas 	/*
3019d0d856e8SRandy Dunlap 	 * TODO: optimization is possible here.
3020d0d856e8SRandy Dunlap 	 * Probably we need not scan at all,
3021d0d856e8SRandy Dunlap 	 * because page truncation is enough.
3022a86c6181SAlex Tomas 	 */
3023a86c6181SAlex Tomas 
3024a86c6181SAlex Tomas 	/* we have to know where to truncate from in crash case */
3025a86c6181SAlex Tomas 	EXT4_I(inode)->i_disksize = inode->i_size;
3026a86c6181SAlex Tomas 	ext4_mark_inode_dirty(handle, inode);
3027a86c6181SAlex Tomas 
3028a86c6181SAlex Tomas 	last_block = (inode->i_size + sb->s_blocksize - 1)
3029a86c6181SAlex Tomas 			>> EXT4_BLOCK_SIZE_BITS(sb);
3030a86c6181SAlex Tomas 	err = ext4_ext_remove_space(inode, last_block);
3031a86c6181SAlex Tomas 
3032a86c6181SAlex Tomas 	/* In a multi-transaction truncate, we only make the final
303356055d3aSAmit Arora 	 * transaction synchronous.
303456055d3aSAmit Arora 	 */
3035a86c6181SAlex Tomas 	if (IS_SYNC(inode))
30360390131bSFrank Mayhar 		ext4_handle_sync(handle);
3037a86c6181SAlex Tomas 
3038a86c6181SAlex Tomas out_stop:
30399ddfc3dcSJan Kara 	up_write(&EXT4_I(inode)->i_data_sem);
3040a86c6181SAlex Tomas 	/*
3041d0d856e8SRandy Dunlap 	 * If this was a simple ftruncate() and the file will remain alive,
3042a86c6181SAlex Tomas 	 * then we need to clear up the orphan record which we created above.
3043a86c6181SAlex Tomas 	 * However, if this was a real unlink then we were called by
3044a86c6181SAlex Tomas 	 * ext4_delete_inode(), and we allow that function to clean up the
3045a86c6181SAlex Tomas 	 * orphan info for us.
3046a86c6181SAlex Tomas 	 */
3047a86c6181SAlex Tomas 	if (inode->i_nlink)
3048a86c6181SAlex Tomas 		ext4_orphan_del(handle, inode);
3049a86c6181SAlex Tomas 
3050ef737728SSolofo Ramangalahy 	inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
3051ef737728SSolofo Ramangalahy 	ext4_mark_inode_dirty(handle, inode);
3052a86c6181SAlex Tomas 	ext4_journal_stop(handle);
3053a86c6181SAlex Tomas }
3054a86c6181SAlex Tomas 
3055fd28784aSAneesh Kumar K.V static void ext4_falloc_update_inode(struct inode *inode,
3056fd28784aSAneesh Kumar K.V 				int mode, loff_t new_size, int update_ctime)
3057fd28784aSAneesh Kumar K.V {
3058fd28784aSAneesh Kumar K.V 	struct timespec now;
3059fd28784aSAneesh Kumar K.V 
3060fd28784aSAneesh Kumar K.V 	if (update_ctime) {
3061fd28784aSAneesh Kumar K.V 		now = current_fs_time(inode->i_sb);
3062fd28784aSAneesh Kumar K.V 		if (!timespec_equal(&inode->i_ctime, &now))
3063fd28784aSAneesh Kumar K.V 			inode->i_ctime = now;
3064fd28784aSAneesh Kumar K.V 	}
3065fd28784aSAneesh Kumar K.V 	/*
3066fd28784aSAneesh Kumar K.V 	 * Update only when preallocation was requested beyond
3067fd28784aSAneesh Kumar K.V 	 * the file size.
3068fd28784aSAneesh Kumar K.V 	 */
3069cf17fea6SAneesh Kumar K.V 	if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3070cf17fea6SAneesh Kumar K.V 		if (new_size > i_size_read(inode))
3071fd28784aSAneesh Kumar K.V 			i_size_write(inode, new_size);
3072cf17fea6SAneesh Kumar K.V 		if (new_size > EXT4_I(inode)->i_disksize)
3073cf17fea6SAneesh Kumar K.V 			ext4_update_i_disksize(inode, new_size);
3074fd28784aSAneesh Kumar K.V 	}
3075fd28784aSAneesh Kumar K.V 
3076fd28784aSAneesh Kumar K.V }
3077fd28784aSAneesh Kumar K.V 
3078a2df2a63SAmit Arora /*
3079a2df2a63SAmit Arora  * preallocate space for a file. This implements ext4's fallocate inode
3080a2df2a63SAmit Arora  * operation, which gets called from sys_fallocate system call.
3081a2df2a63SAmit Arora  * For block-mapped files, posix_fallocate should fall back to the method
3082a2df2a63SAmit Arora  * of writing zeroes to the required new blocks (the same behavior which is
3083a2df2a63SAmit Arora  * expected for file systems which do not support fallocate() system call).
3084a2df2a63SAmit Arora  */
3085a2df2a63SAmit Arora long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
3086a2df2a63SAmit Arora {
3087a2df2a63SAmit Arora 	handle_t *handle;
3088725d26d3SAneesh Kumar K.V 	ext4_lblk_t block;
3089fd28784aSAneesh Kumar K.V 	loff_t new_size;
3090498e5f24STheodore Ts'o 	unsigned int max_blocks;
3091a2df2a63SAmit Arora 	int ret = 0;
3092a2df2a63SAmit Arora 	int ret2 = 0;
3093a2df2a63SAmit Arora 	int retries = 0;
3094a2df2a63SAmit Arora 	struct buffer_head map_bh;
3095a2df2a63SAmit Arora 	unsigned int credits, blkbits = inode->i_blkbits;
3096a2df2a63SAmit Arora 
3097a2df2a63SAmit Arora 	/*
3098a2df2a63SAmit Arora 	 * currently supporting (pre)allocate mode for extent-based
3099a2df2a63SAmit Arora 	 * files _only_
3100a2df2a63SAmit Arora 	 */
3101a2df2a63SAmit Arora 	if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
3102a2df2a63SAmit Arora 		return -EOPNOTSUPP;
3103a2df2a63SAmit Arora 
3104a2df2a63SAmit Arora 	/* preallocation to directories is currently not supported */
3105a2df2a63SAmit Arora 	if (S_ISDIR(inode->i_mode))
3106a2df2a63SAmit Arora 		return -ENODEV;
3107a2df2a63SAmit Arora 
3108a2df2a63SAmit Arora 	block = offset >> blkbits;
3109fd28784aSAneesh Kumar K.V 	/*
3110fd28784aSAneesh Kumar K.V 	 * We can't just convert len to max_blocks because
3111fd28784aSAneesh Kumar K.V 	 * If blocksize = 4096 offset = 3072 and len = 2048
3112fd28784aSAneesh Kumar K.V 	 */
3113a2df2a63SAmit Arora 	max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
3114a2df2a63SAmit Arora 							- block;
3115a2df2a63SAmit Arora 	/*
3116f3bd1f3fSMingming Cao 	 * credits to insert 1 extent into extent tree
3117a2df2a63SAmit Arora 	 */
3118f3bd1f3fSMingming Cao 	credits = ext4_chunk_trans_blocks(inode, max_blocks);
311955bd725aSAneesh Kumar K.V 	mutex_lock(&inode->i_mutex);
3120a2df2a63SAmit Arora retry:
3121a2df2a63SAmit Arora 	while (ret >= 0 && ret < max_blocks) {
3122a2df2a63SAmit Arora 		block = block + ret;
3123a2df2a63SAmit Arora 		max_blocks = max_blocks - ret;
3124a2df2a63SAmit Arora 		handle = ext4_journal_start(inode, credits);
3125a2df2a63SAmit Arora 		if (IS_ERR(handle)) {
3126a2df2a63SAmit Arora 			ret = PTR_ERR(handle);
3127a2df2a63SAmit Arora 			break;
3128a2df2a63SAmit Arora 		}
312955bd725aSAneesh Kumar K.V 		ret = ext4_get_blocks_wrap(handle, inode, block,
3130a2df2a63SAmit Arora 					  max_blocks, &map_bh,
3131d2a17637SMingming Cao 					  EXT4_CREATE_UNINITIALIZED_EXT, 0, 0);
3132221879c9SAneesh Kumar K.V 		if (ret <= 0) {
31332c98615dSAneesh Kumar K.V #ifdef EXT4FS_DEBUG
31342c98615dSAneesh Kumar K.V 			WARN_ON(ret <= 0);
31352c98615dSAneesh Kumar K.V 			printk(KERN_ERR "%s: ext4_ext_get_blocks "
31362c98615dSAneesh Kumar K.V 				    "returned error inode#%lu, block=%u, "
31379fd9784cSThadeu Lima de Souza Cascardo 				    "max_blocks=%u", __func__,
3138bba90743SEric Sandeen 				    inode->i_ino, block, max_blocks);
31392c98615dSAneesh Kumar K.V #endif
3140a2df2a63SAmit Arora 			ext4_mark_inode_dirty(handle, inode);
3141a2df2a63SAmit Arora 			ret2 = ext4_journal_stop(handle);
3142a2df2a63SAmit Arora 			break;
3143a2df2a63SAmit Arora 		}
3144fd28784aSAneesh Kumar K.V 		if ((block + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
3145fd28784aSAneesh Kumar K.V 						blkbits) >> blkbits))
3146fd28784aSAneesh Kumar K.V 			new_size = offset + len;
3147fd28784aSAneesh Kumar K.V 		else
3148fd28784aSAneesh Kumar K.V 			new_size = (block + ret) << blkbits;
3149a2df2a63SAmit Arora 
3150fd28784aSAneesh Kumar K.V 		ext4_falloc_update_inode(inode, mode, new_size,
3151fd28784aSAneesh Kumar K.V 						buffer_new(&map_bh));
3152a2df2a63SAmit Arora 		ext4_mark_inode_dirty(handle, inode);
3153a2df2a63SAmit Arora 		ret2 = ext4_journal_stop(handle);
3154a2df2a63SAmit Arora 		if (ret2)
3155a2df2a63SAmit Arora 			break;
3156a2df2a63SAmit Arora 	}
3157fd28784aSAneesh Kumar K.V 	if (ret == -ENOSPC &&
3158fd28784aSAneesh Kumar K.V 			ext4_should_retry_alloc(inode->i_sb, &retries)) {
3159fd28784aSAneesh Kumar K.V 		ret = 0;
3160a2df2a63SAmit Arora 		goto retry;
3161a2df2a63SAmit Arora 	}
316255bd725aSAneesh Kumar K.V 	mutex_unlock(&inode->i_mutex);
3163a2df2a63SAmit Arora 	return ret > 0 ? ret2 : ret;
3164a2df2a63SAmit Arora }
31656873fa0dSEric Sandeen 
31666873fa0dSEric Sandeen /*
31676873fa0dSEric Sandeen  * Callback function called for each extent to gather FIEMAP information.
31686873fa0dSEric Sandeen  */
31693a06d778SAneesh Kumar K.V static int ext4_ext_fiemap_cb(struct inode *inode, struct ext4_ext_path *path,
31706873fa0dSEric Sandeen 		       struct ext4_ext_cache *newex, struct ext4_extent *ex,
31716873fa0dSEric Sandeen 		       void *data)
31726873fa0dSEric Sandeen {
31736873fa0dSEric Sandeen 	struct fiemap_extent_info *fieinfo = data;
31746873fa0dSEric Sandeen 	unsigned long blksize_bits = inode->i_sb->s_blocksize_bits;
31756873fa0dSEric Sandeen 	__u64	logical;
31766873fa0dSEric Sandeen 	__u64	physical;
31776873fa0dSEric Sandeen 	__u64	length;
31786873fa0dSEric Sandeen 	__u32	flags = 0;
31796873fa0dSEric Sandeen 	int	error;
31806873fa0dSEric Sandeen 
31816873fa0dSEric Sandeen 	logical =  (__u64)newex->ec_block << blksize_bits;
31826873fa0dSEric Sandeen 
31836873fa0dSEric Sandeen 	if (newex->ec_type == EXT4_EXT_CACHE_GAP) {
31846873fa0dSEric Sandeen 		pgoff_t offset;
31856873fa0dSEric Sandeen 		struct page *page;
31866873fa0dSEric Sandeen 		struct buffer_head *bh = NULL;
31876873fa0dSEric Sandeen 
31886873fa0dSEric Sandeen 		offset = logical >> PAGE_SHIFT;
31896873fa0dSEric Sandeen 		page = find_get_page(inode->i_mapping, offset);
31906873fa0dSEric Sandeen 		if (!page || !page_has_buffers(page))
31916873fa0dSEric Sandeen 			return EXT_CONTINUE;
31926873fa0dSEric Sandeen 
31936873fa0dSEric Sandeen 		bh = page_buffers(page);
31946873fa0dSEric Sandeen 
31956873fa0dSEric Sandeen 		if (!bh)
31966873fa0dSEric Sandeen 			return EXT_CONTINUE;
31976873fa0dSEric Sandeen 
31986873fa0dSEric Sandeen 		if (buffer_delay(bh)) {
31996873fa0dSEric Sandeen 			flags |= FIEMAP_EXTENT_DELALLOC;
32006873fa0dSEric Sandeen 			page_cache_release(page);
32016873fa0dSEric Sandeen 		} else {
32026873fa0dSEric Sandeen 			page_cache_release(page);
32036873fa0dSEric Sandeen 			return EXT_CONTINUE;
32046873fa0dSEric Sandeen 		}
32056873fa0dSEric Sandeen 	}
32066873fa0dSEric Sandeen 
32076873fa0dSEric Sandeen 	physical = (__u64)newex->ec_start << blksize_bits;
32086873fa0dSEric Sandeen 	length =   (__u64)newex->ec_len << blksize_bits;
32096873fa0dSEric Sandeen 
32106873fa0dSEric Sandeen 	if (ex && ext4_ext_is_uninitialized(ex))
32116873fa0dSEric Sandeen 		flags |= FIEMAP_EXTENT_UNWRITTEN;
32126873fa0dSEric Sandeen 
32136873fa0dSEric Sandeen 	/*
32146873fa0dSEric Sandeen 	 * If this extent reaches EXT_MAX_BLOCK, it must be last.
32156873fa0dSEric Sandeen 	 *
32166873fa0dSEric Sandeen 	 * Or if ext4_ext_next_allocated_block is EXT_MAX_BLOCK,
32176873fa0dSEric Sandeen 	 * this also indicates no more allocated blocks.
32186873fa0dSEric Sandeen 	 *
32196873fa0dSEric Sandeen 	 * XXX this might miss a single-block extent at EXT_MAX_BLOCK
32206873fa0dSEric Sandeen 	 */
32216873fa0dSEric Sandeen 	if (logical + length - 1 == EXT_MAX_BLOCK ||
32226873fa0dSEric Sandeen 	    ext4_ext_next_allocated_block(path) == EXT_MAX_BLOCK)
32236873fa0dSEric Sandeen 		flags |= FIEMAP_EXTENT_LAST;
32246873fa0dSEric Sandeen 
32256873fa0dSEric Sandeen 	error = fiemap_fill_next_extent(fieinfo, logical, physical,
32266873fa0dSEric Sandeen 					length, flags);
32276873fa0dSEric Sandeen 	if (error < 0)
32286873fa0dSEric Sandeen 		return error;
32296873fa0dSEric Sandeen 	if (error == 1)
32306873fa0dSEric Sandeen 		return EXT_BREAK;
32316873fa0dSEric Sandeen 
32326873fa0dSEric Sandeen 	return EXT_CONTINUE;
32336873fa0dSEric Sandeen }
32346873fa0dSEric Sandeen 
32356873fa0dSEric Sandeen /* fiemap flags we can handle specified here */
32366873fa0dSEric Sandeen #define EXT4_FIEMAP_FLAGS	(FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
32376873fa0dSEric Sandeen 
32383a06d778SAneesh Kumar K.V static int ext4_xattr_fiemap(struct inode *inode,
32393a06d778SAneesh Kumar K.V 				struct fiemap_extent_info *fieinfo)
32406873fa0dSEric Sandeen {
32416873fa0dSEric Sandeen 	__u64 physical = 0;
32426873fa0dSEric Sandeen 	__u64 length;
32436873fa0dSEric Sandeen 	__u32 flags = FIEMAP_EXTENT_LAST;
32446873fa0dSEric Sandeen 	int blockbits = inode->i_sb->s_blocksize_bits;
32456873fa0dSEric Sandeen 	int error = 0;
32466873fa0dSEric Sandeen 
32476873fa0dSEric Sandeen 	/* in-inode? */
32486873fa0dSEric Sandeen 	if (EXT4_I(inode)->i_state & EXT4_STATE_XATTR) {
32496873fa0dSEric Sandeen 		struct ext4_iloc iloc;
32506873fa0dSEric Sandeen 		int offset;	/* offset of xattr in inode */
32516873fa0dSEric Sandeen 
32526873fa0dSEric Sandeen 		error = ext4_get_inode_loc(inode, &iloc);
32536873fa0dSEric Sandeen 		if (error)
32546873fa0dSEric Sandeen 			return error;
32556873fa0dSEric Sandeen 		physical = iloc.bh->b_blocknr << blockbits;
32566873fa0dSEric Sandeen 		offset = EXT4_GOOD_OLD_INODE_SIZE +
32576873fa0dSEric Sandeen 				EXT4_I(inode)->i_extra_isize;
32586873fa0dSEric Sandeen 		physical += offset;
32596873fa0dSEric Sandeen 		length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
32606873fa0dSEric Sandeen 		flags |= FIEMAP_EXTENT_DATA_INLINE;
32616873fa0dSEric Sandeen 	} else { /* external block */
32626873fa0dSEric Sandeen 		physical = EXT4_I(inode)->i_file_acl << blockbits;
32636873fa0dSEric Sandeen 		length = inode->i_sb->s_blocksize;
32646873fa0dSEric Sandeen 	}
32656873fa0dSEric Sandeen 
32666873fa0dSEric Sandeen 	if (physical)
32676873fa0dSEric Sandeen 		error = fiemap_fill_next_extent(fieinfo, 0, physical,
32686873fa0dSEric Sandeen 						length, flags);
32696873fa0dSEric Sandeen 	return (error < 0 ? error : 0);
32706873fa0dSEric Sandeen }
32716873fa0dSEric Sandeen 
32726873fa0dSEric Sandeen int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
32736873fa0dSEric Sandeen 		__u64 start, __u64 len)
32746873fa0dSEric Sandeen {
32756873fa0dSEric Sandeen 	ext4_lblk_t start_blk;
32766873fa0dSEric Sandeen 	ext4_lblk_t len_blks;
32776873fa0dSEric Sandeen 	int error = 0;
32786873fa0dSEric Sandeen 
32796873fa0dSEric Sandeen 	/* fallback to generic here if not in extents fmt */
32806873fa0dSEric Sandeen 	if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
32816873fa0dSEric Sandeen 		return generic_block_fiemap(inode, fieinfo, start, len,
32826873fa0dSEric Sandeen 			ext4_get_block);
32836873fa0dSEric Sandeen 
32846873fa0dSEric Sandeen 	if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
32856873fa0dSEric Sandeen 		return -EBADR;
32866873fa0dSEric Sandeen 
32876873fa0dSEric Sandeen 	if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
32886873fa0dSEric Sandeen 		error = ext4_xattr_fiemap(inode, fieinfo);
32896873fa0dSEric Sandeen 	} else {
32906873fa0dSEric Sandeen 		start_blk = start >> inode->i_sb->s_blocksize_bits;
32916873fa0dSEric Sandeen 		len_blks = len >> inode->i_sb->s_blocksize_bits;
32926873fa0dSEric Sandeen 
32936873fa0dSEric Sandeen 		/*
32946873fa0dSEric Sandeen 		 * Walk the extent tree gathering extent information.
32956873fa0dSEric Sandeen 		 * ext4_ext_fiemap_cb will push extents back to user.
32966873fa0dSEric Sandeen 		 */
32976873fa0dSEric Sandeen 		down_write(&EXT4_I(inode)->i_data_sem);
32986873fa0dSEric Sandeen 		error = ext4_ext_walk_space(inode, start_blk, len_blks,
32996873fa0dSEric Sandeen 					  ext4_ext_fiemap_cb, fieinfo);
33006873fa0dSEric Sandeen 		up_write(&EXT4_I(inode)->i_data_sem);
33016873fa0dSEric Sandeen 	}
33026873fa0dSEric Sandeen 
33036873fa0dSEric Sandeen 	return error;
33046873fa0dSEric Sandeen }
33056873fa0dSEric Sandeen 
3306