xref: /openbmc/linux/fs/ext4/extents.c (revision bf89d16f)
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 
47487caeefSJan Kara static int ext4_ext_truncate_extend_restart(handle_t *handle,
48487caeefSJan Kara 					    struct inode *inode,
49487caeefSJan Kara 					    int needed)
50a86c6181SAlex Tomas {
51a86c6181SAlex Tomas 	int err;
52a86c6181SAlex Tomas 
530390131bSFrank Mayhar 	if (!ext4_handle_valid(handle))
540390131bSFrank Mayhar 		return 0;
55a86c6181SAlex Tomas 	if (handle->h_buffer_credits > needed)
569102e4faSShen Feng 		return 0;
579102e4faSShen Feng 	err = ext4_journal_extend(handle, needed);
580123c939STheodore Ts'o 	if (err <= 0)
599102e4faSShen Feng 		return err;
60487caeefSJan Kara 	err = ext4_truncate_restart_trans(handle, inode, needed);
610617b83fSDmitry Monakhov 	if (err == 0)
620617b83fSDmitry Monakhov 		err = -EAGAIN;
63487caeefSJan Kara 
64487caeefSJan Kara 	return err;
65a86c6181SAlex Tomas }
66a86c6181SAlex Tomas 
67a86c6181SAlex Tomas /*
68a86c6181SAlex Tomas  * could return:
69a86c6181SAlex Tomas  *  - EROFS
70a86c6181SAlex Tomas  *  - ENOMEM
71a86c6181SAlex Tomas  */
72a86c6181SAlex Tomas static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
73a86c6181SAlex Tomas 				struct ext4_ext_path *path)
74a86c6181SAlex Tomas {
75a86c6181SAlex Tomas 	if (path->p_bh) {
76a86c6181SAlex Tomas 		/* path points to block */
77a86c6181SAlex Tomas 		return ext4_journal_get_write_access(handle, path->p_bh);
78a86c6181SAlex Tomas 	}
79a86c6181SAlex Tomas 	/* path points to leaf/index in inode body */
80a86c6181SAlex Tomas 	/* we use in-core data, no need to protect them */
81a86c6181SAlex Tomas 	return 0;
82a86c6181SAlex Tomas }
83a86c6181SAlex Tomas 
84a86c6181SAlex Tomas /*
85a86c6181SAlex Tomas  * could return:
86a86c6181SAlex Tomas  *  - EROFS
87a86c6181SAlex Tomas  *  - ENOMEM
88a86c6181SAlex Tomas  *  - EIO
89a86c6181SAlex Tomas  */
90a86c6181SAlex Tomas static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
91a86c6181SAlex Tomas 				struct ext4_ext_path *path)
92a86c6181SAlex Tomas {
93a86c6181SAlex Tomas 	int err;
94a86c6181SAlex Tomas 	if (path->p_bh) {
95a86c6181SAlex Tomas 		/* path points to block */
960390131bSFrank Mayhar 		err = ext4_handle_dirty_metadata(handle, inode, path->p_bh);
97a86c6181SAlex Tomas 	} else {
98a86c6181SAlex Tomas 		/* path points to leaf/index in inode body */
99a86c6181SAlex Tomas 		err = ext4_mark_inode_dirty(handle, inode);
100a86c6181SAlex Tomas 	}
101a86c6181SAlex Tomas 	return err;
102a86c6181SAlex Tomas }
103a86c6181SAlex Tomas 
104f65e6fbaSAlex Tomas static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
105a86c6181SAlex Tomas 			      struct ext4_ext_path *path,
106725d26d3SAneesh Kumar K.V 			      ext4_lblk_t block)
107a86c6181SAlex Tomas {
108a86c6181SAlex Tomas 	struct ext4_inode_info *ei = EXT4_I(inode);
109f65e6fbaSAlex Tomas 	ext4_fsblk_t bg_start;
11074d3487fSValerie Clement 	ext4_fsblk_t last_block;
111f65e6fbaSAlex Tomas 	ext4_grpblk_t colour;
112a4912123STheodore Ts'o 	ext4_group_t block_group;
113a4912123STheodore Ts'o 	int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
114a86c6181SAlex Tomas 	int depth;
115a86c6181SAlex Tomas 
116a86c6181SAlex Tomas 	if (path) {
117a86c6181SAlex Tomas 		struct ext4_extent *ex;
118a86c6181SAlex Tomas 		depth = path->p_depth;
119a86c6181SAlex Tomas 
120a86c6181SAlex Tomas 		/* try to predict block placement */
1217e028976SAvantika Mathur 		ex = path[depth].p_ext;
1227e028976SAvantika Mathur 		if (ex)
123*bf89d16fSTheodore Ts'o 			return (ext4_ext_pblock(ex) +
124*bf89d16fSTheodore Ts'o 				(block - le32_to_cpu(ex->ee_block)));
125a86c6181SAlex Tomas 
126d0d856e8SRandy Dunlap 		/* it looks like index is empty;
127d0d856e8SRandy Dunlap 		 * try to find starting block from index itself */
128a86c6181SAlex Tomas 		if (path[depth].p_bh)
129a86c6181SAlex Tomas 			return path[depth].p_bh->b_blocknr;
130a86c6181SAlex Tomas 	}
131a86c6181SAlex Tomas 
132a86c6181SAlex Tomas 	/* OK. use inode's group */
133a4912123STheodore Ts'o 	block_group = ei->i_block_group;
134a4912123STheodore Ts'o 	if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
135a4912123STheodore Ts'o 		/*
136a4912123STheodore Ts'o 		 * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
137a4912123STheodore Ts'o 		 * block groups per flexgroup, reserve the first block
138a4912123STheodore Ts'o 		 * group for directories and special files.  Regular
139a4912123STheodore Ts'o 		 * files will start at the second block group.  This
140a4912123STheodore Ts'o 		 * tends to speed up directory access and improves
141a4912123STheodore Ts'o 		 * fsck times.
142a4912123STheodore Ts'o 		 */
143a4912123STheodore Ts'o 		block_group &= ~(flex_size-1);
144a4912123STheodore Ts'o 		if (S_ISREG(inode->i_mode))
145a4912123STheodore Ts'o 			block_group++;
146a4912123STheodore Ts'o 	}
1475661bd68SAkinobu Mita 	bg_start = ext4_group_first_block_no(inode->i_sb, block_group);
14874d3487fSValerie Clement 	last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
14974d3487fSValerie Clement 
150a4912123STheodore Ts'o 	/*
151a4912123STheodore Ts'o 	 * If we are doing delayed allocation, we don't need take
152a4912123STheodore Ts'o 	 * colour into account.
153a4912123STheodore Ts'o 	 */
154a4912123STheodore Ts'o 	if (test_opt(inode->i_sb, DELALLOC))
155a4912123STheodore Ts'o 		return bg_start;
156a4912123STheodore Ts'o 
15774d3487fSValerie Clement 	if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
158a86c6181SAlex Tomas 		colour = (current->pid % 16) *
159a86c6181SAlex Tomas 			(EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
16074d3487fSValerie Clement 	else
16174d3487fSValerie Clement 		colour = (current->pid % 16) * ((last_block - bg_start) / 16);
162a86c6181SAlex Tomas 	return bg_start + colour + block;
163a86c6181SAlex Tomas }
164a86c6181SAlex Tomas 
165654b4908SAneesh Kumar K.V /*
166654b4908SAneesh Kumar K.V  * Allocation for a meta data block
167654b4908SAneesh Kumar K.V  */
168f65e6fbaSAlex Tomas static ext4_fsblk_t
169654b4908SAneesh Kumar K.V ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
170a86c6181SAlex Tomas 			struct ext4_ext_path *path,
171a86c6181SAlex Tomas 			struct ext4_extent *ex, int *err)
172a86c6181SAlex Tomas {
173f65e6fbaSAlex Tomas 	ext4_fsblk_t goal, newblock;
174a86c6181SAlex Tomas 
175a86c6181SAlex Tomas 	goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
17697df5d15STheodore Ts'o 	newblock = ext4_new_meta_blocks(handle, inode, goal, NULL, err);
177a86c6181SAlex Tomas 	return newblock;
178a86c6181SAlex Tomas }
179a86c6181SAlex Tomas 
18055ad63bfSTheodore Ts'o static inline int ext4_ext_space_block(struct inode *inode, int check)
181a86c6181SAlex Tomas {
182a86c6181SAlex Tomas 	int size;
183a86c6181SAlex Tomas 
184a86c6181SAlex Tomas 	size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
185a86c6181SAlex Tomas 			/ sizeof(struct ext4_extent);
18655ad63bfSTheodore Ts'o 	if (!check) {
187bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST
188a86c6181SAlex Tomas 		if (size > 6)
189a86c6181SAlex Tomas 			size = 6;
190a86c6181SAlex Tomas #endif
19155ad63bfSTheodore Ts'o 	}
192a86c6181SAlex Tomas 	return size;
193a86c6181SAlex Tomas }
194a86c6181SAlex Tomas 
19555ad63bfSTheodore Ts'o static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
196a86c6181SAlex Tomas {
197a86c6181SAlex Tomas 	int size;
198a86c6181SAlex Tomas 
199a86c6181SAlex Tomas 	size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
200a86c6181SAlex Tomas 			/ sizeof(struct ext4_extent_idx);
20155ad63bfSTheodore Ts'o 	if (!check) {
202bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST
203a86c6181SAlex Tomas 		if (size > 5)
204a86c6181SAlex Tomas 			size = 5;
205a86c6181SAlex Tomas #endif
20655ad63bfSTheodore Ts'o 	}
207a86c6181SAlex Tomas 	return size;
208a86c6181SAlex Tomas }
209a86c6181SAlex Tomas 
21055ad63bfSTheodore Ts'o static inline int ext4_ext_space_root(struct inode *inode, int check)
211a86c6181SAlex Tomas {
212a86c6181SAlex Tomas 	int size;
213a86c6181SAlex Tomas 
214a86c6181SAlex Tomas 	size = sizeof(EXT4_I(inode)->i_data);
215a86c6181SAlex Tomas 	size -= sizeof(struct ext4_extent_header);
216a86c6181SAlex Tomas 	size /= sizeof(struct ext4_extent);
21755ad63bfSTheodore Ts'o 	if (!check) {
218bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST
219a86c6181SAlex Tomas 		if (size > 3)
220a86c6181SAlex Tomas 			size = 3;
221a86c6181SAlex Tomas #endif
22255ad63bfSTheodore Ts'o 	}
223a86c6181SAlex Tomas 	return size;
224a86c6181SAlex Tomas }
225a86c6181SAlex Tomas 
22655ad63bfSTheodore Ts'o static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
227a86c6181SAlex Tomas {
228a86c6181SAlex Tomas 	int size;
229a86c6181SAlex Tomas 
230a86c6181SAlex Tomas 	size = sizeof(EXT4_I(inode)->i_data);
231a86c6181SAlex Tomas 	size -= sizeof(struct ext4_extent_header);
232a86c6181SAlex Tomas 	size /= sizeof(struct ext4_extent_idx);
23355ad63bfSTheodore Ts'o 	if (!check) {
234bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST
235a86c6181SAlex Tomas 		if (size > 4)
236a86c6181SAlex Tomas 			size = 4;
237a86c6181SAlex Tomas #endif
23855ad63bfSTheodore Ts'o 	}
239a86c6181SAlex Tomas 	return size;
240a86c6181SAlex Tomas }
241a86c6181SAlex Tomas 
242d2a17637SMingming Cao /*
243d2a17637SMingming Cao  * Calculate the number of metadata blocks needed
244d2a17637SMingming Cao  * to allocate @blocks
245d2a17637SMingming Cao  * Worse case is one block per extent
246d2a17637SMingming Cao  */
2479d0be502STheodore Ts'o int ext4_ext_calc_metadata_amount(struct inode *inode, sector_t lblock)
248d2a17637SMingming Cao {
2499d0be502STheodore Ts'o 	struct ext4_inode_info *ei = EXT4_I(inode);
2509d0be502STheodore Ts'o 	int idxs, num = 0;
251d2a17637SMingming Cao 
2529d0be502STheodore Ts'o 	idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
2539d0be502STheodore Ts'o 		/ sizeof(struct ext4_extent_idx));
254d2a17637SMingming Cao 
255d2a17637SMingming Cao 	/*
2569d0be502STheodore Ts'o 	 * If the new delayed allocation block is contiguous with the
2579d0be502STheodore Ts'o 	 * previous da block, it can share index blocks with the
2589d0be502STheodore Ts'o 	 * previous block, so we only need to allocate a new index
2599d0be502STheodore Ts'o 	 * block every idxs leaf blocks.  At ldxs**2 blocks, we need
2609d0be502STheodore Ts'o 	 * an additional index block, and at ldxs**3 blocks, yet
2619d0be502STheodore Ts'o 	 * another index blocks.
262d2a17637SMingming Cao 	 */
2639d0be502STheodore Ts'o 	if (ei->i_da_metadata_calc_len &&
2649d0be502STheodore Ts'o 	    ei->i_da_metadata_calc_last_lblock+1 == lblock) {
2659d0be502STheodore Ts'o 		if ((ei->i_da_metadata_calc_len % idxs) == 0)
2669d0be502STheodore Ts'o 			num++;
2679d0be502STheodore Ts'o 		if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0)
2689d0be502STheodore Ts'o 			num++;
2699d0be502STheodore Ts'o 		if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) {
2709d0be502STheodore Ts'o 			num++;
2719d0be502STheodore Ts'o 			ei->i_da_metadata_calc_len = 0;
2729d0be502STheodore Ts'o 		} else
2739d0be502STheodore Ts'o 			ei->i_da_metadata_calc_len++;
2749d0be502STheodore Ts'o 		ei->i_da_metadata_calc_last_lblock++;
275d2a17637SMingming Cao 		return num;
276d2a17637SMingming Cao 	}
277d2a17637SMingming Cao 
2789d0be502STheodore Ts'o 	/*
2799d0be502STheodore Ts'o 	 * In the worst case we need a new set of index blocks at
2809d0be502STheodore Ts'o 	 * every level of the inode's extent tree.
2819d0be502STheodore Ts'o 	 */
2829d0be502STheodore Ts'o 	ei->i_da_metadata_calc_len = 1;
2839d0be502STheodore Ts'o 	ei->i_da_metadata_calc_last_lblock = lblock;
2849d0be502STheodore Ts'o 	return ext_depth(inode) + 1;
2859d0be502STheodore Ts'o }
2869d0be502STheodore Ts'o 
287c29c0ae7SAlex Tomas static int
288c29c0ae7SAlex Tomas ext4_ext_max_entries(struct inode *inode, int depth)
289c29c0ae7SAlex Tomas {
290c29c0ae7SAlex Tomas 	int max;
291c29c0ae7SAlex Tomas 
292c29c0ae7SAlex Tomas 	if (depth == ext_depth(inode)) {
293c29c0ae7SAlex Tomas 		if (depth == 0)
29455ad63bfSTheodore Ts'o 			max = ext4_ext_space_root(inode, 1);
295c29c0ae7SAlex Tomas 		else
29655ad63bfSTheodore Ts'o 			max = ext4_ext_space_root_idx(inode, 1);
297c29c0ae7SAlex Tomas 	} else {
298c29c0ae7SAlex Tomas 		if (depth == 0)
29955ad63bfSTheodore Ts'o 			max = ext4_ext_space_block(inode, 1);
300c29c0ae7SAlex Tomas 		else
30155ad63bfSTheodore Ts'o 			max = ext4_ext_space_block_idx(inode, 1);
302c29c0ae7SAlex Tomas 	}
303c29c0ae7SAlex Tomas 
304c29c0ae7SAlex Tomas 	return max;
305c29c0ae7SAlex Tomas }
306c29c0ae7SAlex Tomas 
30756b19868SAneesh Kumar K.V static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
30856b19868SAneesh Kumar K.V {
309*bf89d16fSTheodore Ts'o 	ext4_fsblk_t block = ext4_ext_pblock(ext);
31056b19868SAneesh Kumar K.V 	int len = ext4_ext_get_actual_len(ext);
311e84a26ceSTheodore Ts'o 
3126fd058f7STheodore Ts'o 	return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
31356b19868SAneesh Kumar K.V }
31456b19868SAneesh Kumar K.V 
31556b19868SAneesh Kumar K.V static int ext4_valid_extent_idx(struct inode *inode,
31656b19868SAneesh Kumar K.V 				struct ext4_extent_idx *ext_idx)
31756b19868SAneesh Kumar K.V {
318*bf89d16fSTheodore Ts'o 	ext4_fsblk_t block = ext4_idx_pblock(ext_idx);
319e84a26ceSTheodore Ts'o 
3206fd058f7STheodore Ts'o 	return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
32156b19868SAneesh Kumar K.V }
32256b19868SAneesh Kumar K.V 
32356b19868SAneesh Kumar K.V static int ext4_valid_extent_entries(struct inode *inode,
32456b19868SAneesh Kumar K.V 				struct ext4_extent_header *eh,
32556b19868SAneesh Kumar K.V 				int depth)
32656b19868SAneesh Kumar K.V {
32756b19868SAneesh Kumar K.V 	struct ext4_extent *ext;
32856b19868SAneesh Kumar K.V 	struct ext4_extent_idx *ext_idx;
32956b19868SAneesh Kumar K.V 	unsigned short entries;
33056b19868SAneesh Kumar K.V 	if (eh->eh_entries == 0)
33156b19868SAneesh Kumar K.V 		return 1;
33256b19868SAneesh Kumar K.V 
33356b19868SAneesh Kumar K.V 	entries = le16_to_cpu(eh->eh_entries);
33456b19868SAneesh Kumar K.V 
33556b19868SAneesh Kumar K.V 	if (depth == 0) {
33656b19868SAneesh Kumar K.V 		/* leaf entries */
33756b19868SAneesh Kumar K.V 		ext = EXT_FIRST_EXTENT(eh);
33856b19868SAneesh Kumar K.V 		while (entries) {
33956b19868SAneesh Kumar K.V 			if (!ext4_valid_extent(inode, ext))
34056b19868SAneesh Kumar K.V 				return 0;
34156b19868SAneesh Kumar K.V 			ext++;
34256b19868SAneesh Kumar K.V 			entries--;
34356b19868SAneesh Kumar K.V 		}
34456b19868SAneesh Kumar K.V 	} else {
34556b19868SAneesh Kumar K.V 		ext_idx = EXT_FIRST_INDEX(eh);
34656b19868SAneesh Kumar K.V 		while (entries) {
34756b19868SAneesh Kumar K.V 			if (!ext4_valid_extent_idx(inode, ext_idx))
34856b19868SAneesh Kumar K.V 				return 0;
34956b19868SAneesh Kumar K.V 			ext_idx++;
35056b19868SAneesh Kumar K.V 			entries--;
35156b19868SAneesh Kumar K.V 		}
35256b19868SAneesh Kumar K.V 	}
35356b19868SAneesh Kumar K.V 	return 1;
35456b19868SAneesh Kumar K.V }
35556b19868SAneesh Kumar K.V 
356c398eda0STheodore Ts'o static int __ext4_ext_check(const char *function, unsigned int line,
357c398eda0STheodore Ts'o 			    struct inode *inode, struct ext4_extent_header *eh,
358c29c0ae7SAlex Tomas 			    int depth)
359c29c0ae7SAlex Tomas {
360c29c0ae7SAlex Tomas 	const char *error_msg;
361c29c0ae7SAlex Tomas 	int max = 0;
362c29c0ae7SAlex Tomas 
363c29c0ae7SAlex Tomas 	if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
364c29c0ae7SAlex Tomas 		error_msg = "invalid magic";
365c29c0ae7SAlex Tomas 		goto corrupted;
366c29c0ae7SAlex Tomas 	}
367c29c0ae7SAlex Tomas 	if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
368c29c0ae7SAlex Tomas 		error_msg = "unexpected eh_depth";
369c29c0ae7SAlex Tomas 		goto corrupted;
370c29c0ae7SAlex Tomas 	}
371c29c0ae7SAlex Tomas 	if (unlikely(eh->eh_max == 0)) {
372c29c0ae7SAlex Tomas 		error_msg = "invalid eh_max";
373c29c0ae7SAlex Tomas 		goto corrupted;
374c29c0ae7SAlex Tomas 	}
375c29c0ae7SAlex Tomas 	max = ext4_ext_max_entries(inode, depth);
376c29c0ae7SAlex Tomas 	if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
377c29c0ae7SAlex Tomas 		error_msg = "too large eh_max";
378c29c0ae7SAlex Tomas 		goto corrupted;
379c29c0ae7SAlex Tomas 	}
380c29c0ae7SAlex Tomas 	if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
381c29c0ae7SAlex Tomas 		error_msg = "invalid eh_entries";
382c29c0ae7SAlex Tomas 		goto corrupted;
383c29c0ae7SAlex Tomas 	}
38456b19868SAneesh Kumar K.V 	if (!ext4_valid_extent_entries(inode, eh, depth)) {
38556b19868SAneesh Kumar K.V 		error_msg = "invalid extent entries";
38656b19868SAneesh Kumar K.V 		goto corrupted;
38756b19868SAneesh Kumar K.V 	}
388c29c0ae7SAlex Tomas 	return 0;
389c29c0ae7SAlex Tomas 
390c29c0ae7SAlex Tomas corrupted:
391c398eda0STheodore Ts'o 	ext4_error_inode(inode, function, line, 0,
39224676da4STheodore Ts'o 			"bad header/extent: %s - magic %x, "
393c29c0ae7SAlex Tomas 			"entries %u, max %u(%u), depth %u(%u)",
39424676da4STheodore Ts'o 			error_msg, le16_to_cpu(eh->eh_magic),
395c29c0ae7SAlex Tomas 			le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
396c29c0ae7SAlex Tomas 			max, le16_to_cpu(eh->eh_depth), depth);
397c29c0ae7SAlex Tomas 
398c29c0ae7SAlex Tomas 	return -EIO;
399c29c0ae7SAlex Tomas }
400c29c0ae7SAlex Tomas 
40156b19868SAneesh Kumar K.V #define ext4_ext_check(inode, eh, depth)	\
402c398eda0STheodore Ts'o 	__ext4_ext_check(__func__, __LINE__, inode, eh, depth)
403c29c0ae7SAlex Tomas 
4047a262f7cSAneesh Kumar K.V int ext4_ext_check_inode(struct inode *inode)
4057a262f7cSAneesh Kumar K.V {
4067a262f7cSAneesh Kumar K.V 	return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode));
4077a262f7cSAneesh Kumar K.V }
4087a262f7cSAneesh Kumar K.V 
409a86c6181SAlex Tomas #ifdef EXT_DEBUG
410a86c6181SAlex Tomas static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
411a86c6181SAlex Tomas {
412a86c6181SAlex Tomas 	int k, l = path->p_depth;
413a86c6181SAlex Tomas 
414a86c6181SAlex Tomas 	ext_debug("path:");
415a86c6181SAlex Tomas 	for (k = 0; k <= l; k++, path++) {
416a86c6181SAlex Tomas 		if (path->p_idx) {
4172ae02107SMingming Cao 		  ext_debug("  %d->%llu", le32_to_cpu(path->p_idx->ei_block),
418*bf89d16fSTheodore Ts'o 			    ext4_idx_pblock(path->p_idx));
419a86c6181SAlex Tomas 		} else if (path->p_ext) {
420553f9008SMingming 			ext_debug("  %d:[%d]%d:%llu ",
421a86c6181SAlex Tomas 				  le32_to_cpu(path->p_ext->ee_block),
422553f9008SMingming 				  ext4_ext_is_uninitialized(path->p_ext),
423a2df2a63SAmit Arora 				  ext4_ext_get_actual_len(path->p_ext),
424*bf89d16fSTheodore Ts'o 				  ext4_ext_pblock(path->p_ext));
425a86c6181SAlex Tomas 		} else
426a86c6181SAlex Tomas 			ext_debug("  []");
427a86c6181SAlex Tomas 	}
428a86c6181SAlex Tomas 	ext_debug("\n");
429a86c6181SAlex Tomas }
430a86c6181SAlex Tomas 
431a86c6181SAlex Tomas static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
432a86c6181SAlex Tomas {
433a86c6181SAlex Tomas 	int depth = ext_depth(inode);
434a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
435a86c6181SAlex Tomas 	struct ext4_extent *ex;
436a86c6181SAlex Tomas 	int i;
437a86c6181SAlex Tomas 
438a86c6181SAlex Tomas 	if (!path)
439a86c6181SAlex Tomas 		return;
440a86c6181SAlex Tomas 
441a86c6181SAlex Tomas 	eh = path[depth].p_hdr;
442a86c6181SAlex Tomas 	ex = EXT_FIRST_EXTENT(eh);
443a86c6181SAlex Tomas 
444553f9008SMingming 	ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
445553f9008SMingming 
446a86c6181SAlex Tomas 	for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
447553f9008SMingming 		ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
448553f9008SMingming 			  ext4_ext_is_uninitialized(ex),
449*bf89d16fSTheodore Ts'o 			  ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
450a86c6181SAlex Tomas 	}
451a86c6181SAlex Tomas 	ext_debug("\n");
452a86c6181SAlex Tomas }
453a86c6181SAlex Tomas #else
454a86c6181SAlex Tomas #define ext4_ext_show_path(inode, path)
455a86c6181SAlex Tomas #define ext4_ext_show_leaf(inode, path)
456a86c6181SAlex Tomas #endif
457a86c6181SAlex Tomas 
458b35905c1SAneesh Kumar K.V void ext4_ext_drop_refs(struct ext4_ext_path *path)
459a86c6181SAlex Tomas {
460a86c6181SAlex Tomas 	int depth = path->p_depth;
461a86c6181SAlex Tomas 	int i;
462a86c6181SAlex Tomas 
463a86c6181SAlex Tomas 	for (i = 0; i <= depth; i++, path++)
464a86c6181SAlex Tomas 		if (path->p_bh) {
465a86c6181SAlex Tomas 			brelse(path->p_bh);
466a86c6181SAlex Tomas 			path->p_bh = NULL;
467a86c6181SAlex Tomas 		}
468a86c6181SAlex Tomas }
469a86c6181SAlex Tomas 
470a86c6181SAlex Tomas /*
471d0d856e8SRandy Dunlap  * ext4_ext_binsearch_idx:
472d0d856e8SRandy Dunlap  * binary search for the closest index of the given block
473c29c0ae7SAlex Tomas  * the header must be checked before calling this
474a86c6181SAlex Tomas  */
475a86c6181SAlex Tomas static void
476725d26d3SAneesh Kumar K.V ext4_ext_binsearch_idx(struct inode *inode,
477725d26d3SAneesh Kumar K.V 			struct ext4_ext_path *path, ext4_lblk_t block)
478a86c6181SAlex Tomas {
479a86c6181SAlex Tomas 	struct ext4_extent_header *eh = path->p_hdr;
480a86c6181SAlex Tomas 	struct ext4_extent_idx *r, *l, *m;
481a86c6181SAlex Tomas 
482a86c6181SAlex Tomas 
483bba90743SEric Sandeen 	ext_debug("binsearch for %u(idx):  ", block);
484a86c6181SAlex Tomas 
485a86c6181SAlex Tomas 	l = EXT_FIRST_INDEX(eh) + 1;
486e9f410b1SDmitry Monakhov 	r = EXT_LAST_INDEX(eh);
487a86c6181SAlex Tomas 	while (l <= r) {
488a86c6181SAlex Tomas 		m = l + (r - l) / 2;
489a86c6181SAlex Tomas 		if (block < le32_to_cpu(m->ei_block))
490a86c6181SAlex Tomas 			r = m - 1;
491a86c6181SAlex Tomas 		else
492a86c6181SAlex Tomas 			l = m + 1;
49326d535edSDmitry Monakhov 		ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
49426d535edSDmitry Monakhov 				m, le32_to_cpu(m->ei_block),
49526d535edSDmitry Monakhov 				r, le32_to_cpu(r->ei_block));
496a86c6181SAlex Tomas 	}
497a86c6181SAlex Tomas 
498a86c6181SAlex Tomas 	path->p_idx = l - 1;
499f65e6fbaSAlex Tomas 	ext_debug("  -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
500*bf89d16fSTheodore Ts'o 		  ext4_idx_pblock(path->p_idx));
501a86c6181SAlex Tomas 
502a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH
503a86c6181SAlex Tomas 	{
504a86c6181SAlex Tomas 		struct ext4_extent_idx *chix, *ix;
505a86c6181SAlex Tomas 		int k;
506a86c6181SAlex Tomas 
507a86c6181SAlex Tomas 		chix = ix = EXT_FIRST_INDEX(eh);
508a86c6181SAlex Tomas 		for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
509a86c6181SAlex Tomas 		  if (k != 0 &&
510a86c6181SAlex Tomas 		      le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
5114776004fSTheodore Ts'o 				printk(KERN_DEBUG "k=%d, ix=0x%p, "
5124776004fSTheodore Ts'o 				       "first=0x%p\n", k,
513a86c6181SAlex Tomas 				       ix, EXT_FIRST_INDEX(eh));
5144776004fSTheodore Ts'o 				printk(KERN_DEBUG "%u <= %u\n",
515a86c6181SAlex Tomas 				       le32_to_cpu(ix->ei_block),
516a86c6181SAlex Tomas 				       le32_to_cpu(ix[-1].ei_block));
517a86c6181SAlex Tomas 			}
518a86c6181SAlex Tomas 			BUG_ON(k && le32_to_cpu(ix->ei_block)
519a86c6181SAlex Tomas 					   <= le32_to_cpu(ix[-1].ei_block));
520a86c6181SAlex Tomas 			if (block < le32_to_cpu(ix->ei_block))
521a86c6181SAlex Tomas 				break;
522a86c6181SAlex Tomas 			chix = ix;
523a86c6181SAlex Tomas 		}
524a86c6181SAlex Tomas 		BUG_ON(chix != path->p_idx);
525a86c6181SAlex Tomas 	}
526a86c6181SAlex Tomas #endif
527a86c6181SAlex Tomas 
528a86c6181SAlex Tomas }
529a86c6181SAlex Tomas 
530a86c6181SAlex Tomas /*
531d0d856e8SRandy Dunlap  * ext4_ext_binsearch:
532d0d856e8SRandy Dunlap  * binary search for closest extent of the given block
533c29c0ae7SAlex Tomas  * the header must be checked before calling this
534a86c6181SAlex Tomas  */
535a86c6181SAlex Tomas static void
536725d26d3SAneesh Kumar K.V ext4_ext_binsearch(struct inode *inode,
537725d26d3SAneesh Kumar K.V 		struct ext4_ext_path *path, ext4_lblk_t block)
538a86c6181SAlex Tomas {
539a86c6181SAlex Tomas 	struct ext4_extent_header *eh = path->p_hdr;
540a86c6181SAlex Tomas 	struct ext4_extent *r, *l, *m;
541a86c6181SAlex Tomas 
542a86c6181SAlex Tomas 	if (eh->eh_entries == 0) {
543a86c6181SAlex Tomas 		/*
544d0d856e8SRandy Dunlap 		 * this leaf is empty:
545a86c6181SAlex Tomas 		 * we get such a leaf in split/add case
546a86c6181SAlex Tomas 		 */
547a86c6181SAlex Tomas 		return;
548a86c6181SAlex Tomas 	}
549a86c6181SAlex Tomas 
550bba90743SEric Sandeen 	ext_debug("binsearch for %u:  ", block);
551a86c6181SAlex Tomas 
552a86c6181SAlex Tomas 	l = EXT_FIRST_EXTENT(eh) + 1;
553e9f410b1SDmitry Monakhov 	r = EXT_LAST_EXTENT(eh);
554a86c6181SAlex Tomas 
555a86c6181SAlex Tomas 	while (l <= r) {
556a86c6181SAlex Tomas 		m = l + (r - l) / 2;
557a86c6181SAlex Tomas 		if (block < le32_to_cpu(m->ee_block))
558a86c6181SAlex Tomas 			r = m - 1;
559a86c6181SAlex Tomas 		else
560a86c6181SAlex Tomas 			l = m + 1;
56126d535edSDmitry Monakhov 		ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
56226d535edSDmitry Monakhov 				m, le32_to_cpu(m->ee_block),
56326d535edSDmitry Monakhov 				r, le32_to_cpu(r->ee_block));
564a86c6181SAlex Tomas 	}
565a86c6181SAlex Tomas 
566a86c6181SAlex Tomas 	path->p_ext = l - 1;
567553f9008SMingming 	ext_debug("  -> %d:%llu:[%d]%d ",
568a86c6181SAlex Tomas 			le32_to_cpu(path->p_ext->ee_block),
569*bf89d16fSTheodore Ts'o 			ext4_ext_pblock(path->p_ext),
570553f9008SMingming 			ext4_ext_is_uninitialized(path->p_ext),
571a2df2a63SAmit Arora 			ext4_ext_get_actual_len(path->p_ext));
572a86c6181SAlex Tomas 
573a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH
574a86c6181SAlex Tomas 	{
575a86c6181SAlex Tomas 		struct ext4_extent *chex, *ex;
576a86c6181SAlex Tomas 		int k;
577a86c6181SAlex Tomas 
578a86c6181SAlex Tomas 		chex = ex = EXT_FIRST_EXTENT(eh);
579a86c6181SAlex Tomas 		for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
580a86c6181SAlex Tomas 			BUG_ON(k && le32_to_cpu(ex->ee_block)
581a86c6181SAlex Tomas 					  <= le32_to_cpu(ex[-1].ee_block));
582a86c6181SAlex Tomas 			if (block < le32_to_cpu(ex->ee_block))
583a86c6181SAlex Tomas 				break;
584a86c6181SAlex Tomas 			chex = ex;
585a86c6181SAlex Tomas 		}
586a86c6181SAlex Tomas 		BUG_ON(chex != path->p_ext);
587a86c6181SAlex Tomas 	}
588a86c6181SAlex Tomas #endif
589a86c6181SAlex Tomas 
590a86c6181SAlex Tomas }
591a86c6181SAlex Tomas 
592a86c6181SAlex Tomas int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
593a86c6181SAlex Tomas {
594a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
595a86c6181SAlex Tomas 
596a86c6181SAlex Tomas 	eh = ext_inode_hdr(inode);
597a86c6181SAlex Tomas 	eh->eh_depth = 0;
598a86c6181SAlex Tomas 	eh->eh_entries = 0;
599a86c6181SAlex Tomas 	eh->eh_magic = EXT4_EXT_MAGIC;
60055ad63bfSTheodore Ts'o 	eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
601a86c6181SAlex Tomas 	ext4_mark_inode_dirty(handle, inode);
602a86c6181SAlex Tomas 	ext4_ext_invalidate_cache(inode);
603a86c6181SAlex Tomas 	return 0;
604a86c6181SAlex Tomas }
605a86c6181SAlex Tomas 
606a86c6181SAlex Tomas struct ext4_ext_path *
607725d26d3SAneesh Kumar K.V ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
608725d26d3SAneesh Kumar K.V 					struct ext4_ext_path *path)
609a86c6181SAlex Tomas {
610a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
611a86c6181SAlex Tomas 	struct buffer_head *bh;
612a86c6181SAlex Tomas 	short int depth, i, ppos = 0, alloc = 0;
613a86c6181SAlex Tomas 
614a86c6181SAlex Tomas 	eh = ext_inode_hdr(inode);
615c29c0ae7SAlex Tomas 	depth = ext_depth(inode);
616a86c6181SAlex Tomas 
617a86c6181SAlex Tomas 	/* account possible depth increase */
618a86c6181SAlex Tomas 	if (!path) {
6195d4958f9SAvantika Mathur 		path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
620a86c6181SAlex Tomas 				GFP_NOFS);
621a86c6181SAlex Tomas 		if (!path)
622a86c6181SAlex Tomas 			return ERR_PTR(-ENOMEM);
623a86c6181SAlex Tomas 		alloc = 1;
624a86c6181SAlex Tomas 	}
625a86c6181SAlex Tomas 	path[0].p_hdr = eh;
6261973adcbSShen Feng 	path[0].p_bh = NULL;
627a86c6181SAlex Tomas 
628c29c0ae7SAlex Tomas 	i = depth;
629a86c6181SAlex Tomas 	/* walk through the tree */
630a86c6181SAlex Tomas 	while (i) {
6317a262f7cSAneesh Kumar K.V 		int need_to_validate = 0;
6327a262f7cSAneesh Kumar K.V 
633a86c6181SAlex Tomas 		ext_debug("depth %d: num %d, max %d\n",
634a86c6181SAlex Tomas 			  ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
635c29c0ae7SAlex Tomas 
636a86c6181SAlex Tomas 		ext4_ext_binsearch_idx(inode, path + ppos, block);
637*bf89d16fSTheodore Ts'o 		path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
638a86c6181SAlex Tomas 		path[ppos].p_depth = i;
639a86c6181SAlex Tomas 		path[ppos].p_ext = NULL;
640a86c6181SAlex Tomas 
6417a262f7cSAneesh Kumar K.V 		bh = sb_getblk(inode->i_sb, path[ppos].p_block);
6427a262f7cSAneesh Kumar K.V 		if (unlikely(!bh))
643a86c6181SAlex Tomas 			goto err;
6447a262f7cSAneesh Kumar K.V 		if (!bh_uptodate_or_lock(bh)) {
6457a262f7cSAneesh Kumar K.V 			if (bh_submit_read(bh) < 0) {
6467a262f7cSAneesh Kumar K.V 				put_bh(bh);
6477a262f7cSAneesh Kumar K.V 				goto err;
6487a262f7cSAneesh Kumar K.V 			}
6497a262f7cSAneesh Kumar K.V 			/* validate the extent entries */
6507a262f7cSAneesh Kumar K.V 			need_to_validate = 1;
6517a262f7cSAneesh Kumar K.V 		}
652a86c6181SAlex Tomas 		eh = ext_block_hdr(bh);
653a86c6181SAlex Tomas 		ppos++;
654273df556SFrank Mayhar 		if (unlikely(ppos > depth)) {
655273df556SFrank Mayhar 			put_bh(bh);
656273df556SFrank Mayhar 			EXT4_ERROR_INODE(inode,
657273df556SFrank Mayhar 					 "ppos %d > depth %d", ppos, depth);
658273df556SFrank Mayhar 			goto err;
659273df556SFrank Mayhar 		}
660a86c6181SAlex Tomas 		path[ppos].p_bh = bh;
661a86c6181SAlex Tomas 		path[ppos].p_hdr = eh;
662a86c6181SAlex Tomas 		i--;
663a86c6181SAlex Tomas 
6647a262f7cSAneesh Kumar K.V 		if (need_to_validate && ext4_ext_check(inode, eh, i))
665a86c6181SAlex Tomas 			goto err;
666a86c6181SAlex Tomas 	}
667a86c6181SAlex Tomas 
668a86c6181SAlex Tomas 	path[ppos].p_depth = i;
669a86c6181SAlex Tomas 	path[ppos].p_ext = NULL;
670a86c6181SAlex Tomas 	path[ppos].p_idx = NULL;
671a86c6181SAlex Tomas 
672a86c6181SAlex Tomas 	/* find extent */
673a86c6181SAlex Tomas 	ext4_ext_binsearch(inode, path + ppos, block);
6741973adcbSShen Feng 	/* if not an empty leaf */
6751973adcbSShen Feng 	if (path[ppos].p_ext)
676*bf89d16fSTheodore Ts'o 		path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
677a86c6181SAlex Tomas 
678a86c6181SAlex Tomas 	ext4_ext_show_path(inode, path);
679a86c6181SAlex Tomas 
680a86c6181SAlex Tomas 	return path;
681a86c6181SAlex Tomas 
682a86c6181SAlex Tomas err:
683a86c6181SAlex Tomas 	ext4_ext_drop_refs(path);
684a86c6181SAlex Tomas 	if (alloc)
685a86c6181SAlex Tomas 		kfree(path);
686a86c6181SAlex Tomas 	return ERR_PTR(-EIO);
687a86c6181SAlex Tomas }
688a86c6181SAlex Tomas 
689a86c6181SAlex Tomas /*
690d0d856e8SRandy Dunlap  * ext4_ext_insert_index:
691d0d856e8SRandy Dunlap  * insert new index [@logical;@ptr] into the block at @curp;
692d0d856e8SRandy Dunlap  * check where to insert: before @curp or after @curp
693a86c6181SAlex Tomas  */
6941f109d5aSTheodore Ts'o static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
695a86c6181SAlex Tomas 				 struct ext4_ext_path *curp,
696f65e6fbaSAlex Tomas 				 int logical, ext4_fsblk_t ptr)
697a86c6181SAlex Tomas {
698a86c6181SAlex Tomas 	struct ext4_extent_idx *ix;
699a86c6181SAlex Tomas 	int len, err;
700a86c6181SAlex Tomas 
7017e028976SAvantika Mathur 	err = ext4_ext_get_access(handle, inode, curp);
7027e028976SAvantika Mathur 	if (err)
703a86c6181SAlex Tomas 		return err;
704a86c6181SAlex Tomas 
705273df556SFrank Mayhar 	if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
706273df556SFrank Mayhar 		EXT4_ERROR_INODE(inode,
707273df556SFrank Mayhar 				 "logical %d == ei_block %d!",
708273df556SFrank Mayhar 				 logical, le32_to_cpu(curp->p_idx->ei_block));
709273df556SFrank Mayhar 		return -EIO;
710273df556SFrank Mayhar 	}
711a86c6181SAlex Tomas 	len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
712a86c6181SAlex Tomas 	if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
713a86c6181SAlex Tomas 		/* insert after */
714a86c6181SAlex Tomas 		if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
715a86c6181SAlex Tomas 			len = (len - 1) * sizeof(struct ext4_extent_idx);
716a86c6181SAlex Tomas 			len = len < 0 ? 0 : len;
71726d535edSDmitry Monakhov 			ext_debug("insert new index %d after: %llu. "
718a86c6181SAlex Tomas 					"move %d from 0x%p to 0x%p\n",
719a86c6181SAlex Tomas 					logical, ptr, len,
720a86c6181SAlex Tomas 					(curp->p_idx + 1), (curp->p_idx + 2));
721a86c6181SAlex Tomas 			memmove(curp->p_idx + 2, curp->p_idx + 1, len);
722a86c6181SAlex Tomas 		}
723a86c6181SAlex Tomas 		ix = curp->p_idx + 1;
724a86c6181SAlex Tomas 	} else {
725a86c6181SAlex Tomas 		/* insert before */
726a86c6181SAlex Tomas 		len = len * sizeof(struct ext4_extent_idx);
727a86c6181SAlex Tomas 		len = len < 0 ? 0 : len;
72826d535edSDmitry Monakhov 		ext_debug("insert new index %d before: %llu. "
729a86c6181SAlex Tomas 				"move %d from 0x%p to 0x%p\n",
730a86c6181SAlex Tomas 				logical, ptr, len,
731a86c6181SAlex Tomas 				curp->p_idx, (curp->p_idx + 1));
732a86c6181SAlex Tomas 		memmove(curp->p_idx + 1, curp->p_idx, len);
733a86c6181SAlex Tomas 		ix = curp->p_idx;
734a86c6181SAlex Tomas 	}
735a86c6181SAlex Tomas 
736a86c6181SAlex Tomas 	ix->ei_block = cpu_to_le32(logical);
737f65e6fbaSAlex Tomas 	ext4_idx_store_pblock(ix, ptr);
738e8546d06SMarcin Slusarz 	le16_add_cpu(&curp->p_hdr->eh_entries, 1);
739a86c6181SAlex Tomas 
740273df556SFrank Mayhar 	if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
741273df556SFrank Mayhar 			     > le16_to_cpu(curp->p_hdr->eh_max))) {
742273df556SFrank Mayhar 		EXT4_ERROR_INODE(inode,
743273df556SFrank Mayhar 				 "logical %d == ei_block %d!",
744273df556SFrank Mayhar 				 logical, le32_to_cpu(curp->p_idx->ei_block));
745273df556SFrank Mayhar 		return -EIO;
746273df556SFrank Mayhar 	}
747273df556SFrank Mayhar 	if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
748273df556SFrank Mayhar 		EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
749273df556SFrank Mayhar 		return -EIO;
750273df556SFrank Mayhar 	}
751a86c6181SAlex Tomas 
752a86c6181SAlex Tomas 	err = ext4_ext_dirty(handle, inode, curp);
753a86c6181SAlex Tomas 	ext4_std_error(inode->i_sb, err);
754a86c6181SAlex Tomas 
755a86c6181SAlex Tomas 	return err;
756a86c6181SAlex Tomas }
757a86c6181SAlex Tomas 
758a86c6181SAlex Tomas /*
759d0d856e8SRandy Dunlap  * ext4_ext_split:
760d0d856e8SRandy Dunlap  * inserts new subtree into the path, using free index entry
761d0d856e8SRandy Dunlap  * at depth @at:
762a86c6181SAlex Tomas  * - allocates all needed blocks (new leaf and all intermediate index blocks)
763a86c6181SAlex Tomas  * - makes decision where to split
764d0d856e8SRandy Dunlap  * - moves remaining extents and index entries (right to the split point)
765a86c6181SAlex Tomas  *   into the newly allocated blocks
766d0d856e8SRandy Dunlap  * - initializes subtree
767a86c6181SAlex Tomas  */
768a86c6181SAlex Tomas static int ext4_ext_split(handle_t *handle, struct inode *inode,
769a86c6181SAlex Tomas 				struct ext4_ext_path *path,
770a86c6181SAlex Tomas 				struct ext4_extent *newext, int at)
771a86c6181SAlex Tomas {
772a86c6181SAlex Tomas 	struct buffer_head *bh = NULL;
773a86c6181SAlex Tomas 	int depth = ext_depth(inode);
774a86c6181SAlex Tomas 	struct ext4_extent_header *neh;
775a86c6181SAlex Tomas 	struct ext4_extent_idx *fidx;
776a86c6181SAlex Tomas 	struct ext4_extent *ex;
777a86c6181SAlex Tomas 	int i = at, k, m, a;
778f65e6fbaSAlex Tomas 	ext4_fsblk_t newblock, oldblock;
779a86c6181SAlex Tomas 	__le32 border;
780f65e6fbaSAlex Tomas 	ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
781a86c6181SAlex Tomas 	int err = 0;
782a86c6181SAlex Tomas 
783a86c6181SAlex Tomas 	/* make decision: where to split? */
784d0d856e8SRandy Dunlap 	/* FIXME: now decision is simplest: at current extent */
785a86c6181SAlex Tomas 
786d0d856e8SRandy Dunlap 	/* if current leaf will be split, then we should use
787a86c6181SAlex Tomas 	 * border from split point */
788273df556SFrank Mayhar 	if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
789273df556SFrank Mayhar 		EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
790273df556SFrank Mayhar 		return -EIO;
791273df556SFrank Mayhar 	}
792a86c6181SAlex Tomas 	if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
793a86c6181SAlex Tomas 		border = path[depth].p_ext[1].ee_block;
794d0d856e8SRandy Dunlap 		ext_debug("leaf will be split."
795a86c6181SAlex Tomas 				" next leaf starts at %d\n",
796a86c6181SAlex Tomas 				  le32_to_cpu(border));
797a86c6181SAlex Tomas 	} else {
798a86c6181SAlex Tomas 		border = newext->ee_block;
799a86c6181SAlex Tomas 		ext_debug("leaf will be added."
800a86c6181SAlex Tomas 				" next leaf starts at %d\n",
801a86c6181SAlex Tomas 				le32_to_cpu(border));
802a86c6181SAlex Tomas 	}
803a86c6181SAlex Tomas 
804a86c6181SAlex Tomas 	/*
805d0d856e8SRandy Dunlap 	 * If error occurs, then we break processing
806d0d856e8SRandy Dunlap 	 * and mark filesystem read-only. index won't
807a86c6181SAlex Tomas 	 * be inserted and tree will be in consistent
808d0d856e8SRandy Dunlap 	 * state. Next mount will repair buffers too.
809a86c6181SAlex Tomas 	 */
810a86c6181SAlex Tomas 
811a86c6181SAlex Tomas 	/*
812d0d856e8SRandy Dunlap 	 * Get array to track all allocated blocks.
813d0d856e8SRandy Dunlap 	 * We need this to handle errors and free blocks
814d0d856e8SRandy Dunlap 	 * upon them.
815a86c6181SAlex Tomas 	 */
8165d4958f9SAvantika Mathur 	ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
817a86c6181SAlex Tomas 	if (!ablocks)
818a86c6181SAlex Tomas 		return -ENOMEM;
819a86c6181SAlex Tomas 
820a86c6181SAlex Tomas 	/* allocate all needed blocks */
821a86c6181SAlex Tomas 	ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
822a86c6181SAlex Tomas 	for (a = 0; a < depth - at; a++) {
823654b4908SAneesh Kumar K.V 		newblock = ext4_ext_new_meta_block(handle, inode, path,
824654b4908SAneesh Kumar K.V 						   newext, &err);
825a86c6181SAlex Tomas 		if (newblock == 0)
826a86c6181SAlex Tomas 			goto cleanup;
827a86c6181SAlex Tomas 		ablocks[a] = newblock;
828a86c6181SAlex Tomas 	}
829a86c6181SAlex Tomas 
830a86c6181SAlex Tomas 	/* initialize new leaf */
831a86c6181SAlex Tomas 	newblock = ablocks[--a];
832273df556SFrank Mayhar 	if (unlikely(newblock == 0)) {
833273df556SFrank Mayhar 		EXT4_ERROR_INODE(inode, "newblock == 0!");
834273df556SFrank Mayhar 		err = -EIO;
835273df556SFrank Mayhar 		goto cleanup;
836273df556SFrank Mayhar 	}
837a86c6181SAlex Tomas 	bh = sb_getblk(inode->i_sb, newblock);
838a86c6181SAlex Tomas 	if (!bh) {
839a86c6181SAlex Tomas 		err = -EIO;
840a86c6181SAlex Tomas 		goto cleanup;
841a86c6181SAlex Tomas 	}
842a86c6181SAlex Tomas 	lock_buffer(bh);
843a86c6181SAlex Tomas 
8447e028976SAvantika Mathur 	err = ext4_journal_get_create_access(handle, bh);
8457e028976SAvantika Mathur 	if (err)
846a86c6181SAlex Tomas 		goto cleanup;
847a86c6181SAlex Tomas 
848a86c6181SAlex Tomas 	neh = ext_block_hdr(bh);
849a86c6181SAlex Tomas 	neh->eh_entries = 0;
85055ad63bfSTheodore Ts'o 	neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
851a86c6181SAlex Tomas 	neh->eh_magic = EXT4_EXT_MAGIC;
852a86c6181SAlex Tomas 	neh->eh_depth = 0;
853a86c6181SAlex Tomas 	ex = EXT_FIRST_EXTENT(neh);
854a86c6181SAlex Tomas 
855d0d856e8SRandy Dunlap 	/* move remainder of path[depth] to the new leaf */
856273df556SFrank Mayhar 	if (unlikely(path[depth].p_hdr->eh_entries !=
857273df556SFrank Mayhar 		     path[depth].p_hdr->eh_max)) {
858273df556SFrank Mayhar 		EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
859273df556SFrank Mayhar 				 path[depth].p_hdr->eh_entries,
860273df556SFrank Mayhar 				 path[depth].p_hdr->eh_max);
861273df556SFrank Mayhar 		err = -EIO;
862273df556SFrank Mayhar 		goto cleanup;
863273df556SFrank Mayhar 	}
864a86c6181SAlex Tomas 	/* start copy from next extent */
865a86c6181SAlex Tomas 	/* TODO: we could do it by single memmove */
866a86c6181SAlex Tomas 	m = 0;
867a86c6181SAlex Tomas 	path[depth].p_ext++;
868a86c6181SAlex Tomas 	while (path[depth].p_ext <=
869a86c6181SAlex Tomas 			EXT_MAX_EXTENT(path[depth].p_hdr)) {
870553f9008SMingming 		ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
871a86c6181SAlex Tomas 				le32_to_cpu(path[depth].p_ext->ee_block),
872*bf89d16fSTheodore Ts'o 				ext4_ext_pblock(path[depth].p_ext),
873553f9008SMingming 				ext4_ext_is_uninitialized(path[depth].p_ext),
874a2df2a63SAmit Arora 				ext4_ext_get_actual_len(path[depth].p_ext),
875a86c6181SAlex Tomas 				newblock);
876a86c6181SAlex Tomas 		/*memmove(ex++, path[depth].p_ext++,
877a86c6181SAlex Tomas 				sizeof(struct ext4_extent));
878a86c6181SAlex Tomas 		neh->eh_entries++;*/
879a86c6181SAlex Tomas 		path[depth].p_ext++;
880a86c6181SAlex Tomas 		m++;
881a86c6181SAlex Tomas 	}
882a86c6181SAlex Tomas 	if (m) {
883a86c6181SAlex Tomas 		memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
884e8546d06SMarcin Slusarz 		le16_add_cpu(&neh->eh_entries, m);
885a86c6181SAlex Tomas 	}
886a86c6181SAlex Tomas 
887a86c6181SAlex Tomas 	set_buffer_uptodate(bh);
888a86c6181SAlex Tomas 	unlock_buffer(bh);
889a86c6181SAlex Tomas 
8900390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, inode, bh);
8917e028976SAvantika Mathur 	if (err)
892a86c6181SAlex Tomas 		goto cleanup;
893a86c6181SAlex Tomas 	brelse(bh);
894a86c6181SAlex Tomas 	bh = NULL;
895a86c6181SAlex Tomas 
896a86c6181SAlex Tomas 	/* correct old leaf */
897a86c6181SAlex Tomas 	if (m) {
8987e028976SAvantika Mathur 		err = ext4_ext_get_access(handle, inode, path + depth);
8997e028976SAvantika Mathur 		if (err)
900a86c6181SAlex Tomas 			goto cleanup;
901e8546d06SMarcin Slusarz 		le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
9027e028976SAvantika Mathur 		err = ext4_ext_dirty(handle, inode, path + depth);
9037e028976SAvantika Mathur 		if (err)
904a86c6181SAlex Tomas 			goto cleanup;
905a86c6181SAlex Tomas 
906a86c6181SAlex Tomas 	}
907a86c6181SAlex Tomas 
908a86c6181SAlex Tomas 	/* create intermediate indexes */
909a86c6181SAlex Tomas 	k = depth - at - 1;
910273df556SFrank Mayhar 	if (unlikely(k < 0)) {
911273df556SFrank Mayhar 		EXT4_ERROR_INODE(inode, "k %d < 0!", k);
912273df556SFrank Mayhar 		err = -EIO;
913273df556SFrank Mayhar 		goto cleanup;
914273df556SFrank Mayhar 	}
915a86c6181SAlex Tomas 	if (k)
916a86c6181SAlex Tomas 		ext_debug("create %d intermediate indices\n", k);
917a86c6181SAlex Tomas 	/* insert new index into current index block */
918a86c6181SAlex Tomas 	/* current depth stored in i var */
919a86c6181SAlex Tomas 	i = depth - 1;
920a86c6181SAlex Tomas 	while (k--) {
921a86c6181SAlex Tomas 		oldblock = newblock;
922a86c6181SAlex Tomas 		newblock = ablocks[--a];
923bba90743SEric Sandeen 		bh = sb_getblk(inode->i_sb, newblock);
924a86c6181SAlex Tomas 		if (!bh) {
925a86c6181SAlex Tomas 			err = -EIO;
926a86c6181SAlex Tomas 			goto cleanup;
927a86c6181SAlex Tomas 		}
928a86c6181SAlex Tomas 		lock_buffer(bh);
929a86c6181SAlex Tomas 
9307e028976SAvantika Mathur 		err = ext4_journal_get_create_access(handle, bh);
9317e028976SAvantika Mathur 		if (err)
932a86c6181SAlex Tomas 			goto cleanup;
933a86c6181SAlex Tomas 
934a86c6181SAlex Tomas 		neh = ext_block_hdr(bh);
935a86c6181SAlex Tomas 		neh->eh_entries = cpu_to_le16(1);
936a86c6181SAlex Tomas 		neh->eh_magic = EXT4_EXT_MAGIC;
93755ad63bfSTheodore Ts'o 		neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
938a86c6181SAlex Tomas 		neh->eh_depth = cpu_to_le16(depth - i);
939a86c6181SAlex Tomas 		fidx = EXT_FIRST_INDEX(neh);
940a86c6181SAlex Tomas 		fidx->ei_block = border;
941f65e6fbaSAlex Tomas 		ext4_idx_store_pblock(fidx, oldblock);
942a86c6181SAlex Tomas 
943bba90743SEric Sandeen 		ext_debug("int.index at %d (block %llu): %u -> %llu\n",
944bba90743SEric Sandeen 				i, newblock, le32_to_cpu(border), oldblock);
945a86c6181SAlex Tomas 		/* copy indexes */
946a86c6181SAlex Tomas 		m = 0;
947a86c6181SAlex Tomas 		path[i].p_idx++;
948a86c6181SAlex Tomas 
949a86c6181SAlex Tomas 		ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
950a86c6181SAlex Tomas 				EXT_MAX_INDEX(path[i].p_hdr));
951273df556SFrank Mayhar 		if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
952273df556SFrank Mayhar 					EXT_LAST_INDEX(path[i].p_hdr))) {
953273df556SFrank Mayhar 			EXT4_ERROR_INODE(inode,
954273df556SFrank Mayhar 					 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
955273df556SFrank Mayhar 					 le32_to_cpu(path[i].p_ext->ee_block));
956273df556SFrank Mayhar 			err = -EIO;
957273df556SFrank Mayhar 			goto cleanup;
958273df556SFrank Mayhar 		}
959a86c6181SAlex Tomas 		while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
96026d535edSDmitry Monakhov 			ext_debug("%d: move %d:%llu in new index %llu\n", i,
961a86c6181SAlex Tomas 					le32_to_cpu(path[i].p_idx->ei_block),
962*bf89d16fSTheodore Ts'o 					ext4_idx_pblock(path[i].p_idx),
963a86c6181SAlex Tomas 					newblock);
964a86c6181SAlex Tomas 			/*memmove(++fidx, path[i].p_idx++,
965a86c6181SAlex Tomas 					sizeof(struct ext4_extent_idx));
966a86c6181SAlex Tomas 			neh->eh_entries++;
967a86c6181SAlex Tomas 			BUG_ON(neh->eh_entries > neh->eh_max);*/
968a86c6181SAlex Tomas 			path[i].p_idx++;
969a86c6181SAlex Tomas 			m++;
970a86c6181SAlex Tomas 		}
971a86c6181SAlex Tomas 		if (m) {
972a86c6181SAlex Tomas 			memmove(++fidx, path[i].p_idx - m,
973a86c6181SAlex Tomas 				sizeof(struct ext4_extent_idx) * m);
974e8546d06SMarcin Slusarz 			le16_add_cpu(&neh->eh_entries, m);
975a86c6181SAlex Tomas 		}
976a86c6181SAlex Tomas 		set_buffer_uptodate(bh);
977a86c6181SAlex Tomas 		unlock_buffer(bh);
978a86c6181SAlex Tomas 
9790390131bSFrank Mayhar 		err = ext4_handle_dirty_metadata(handle, inode, bh);
9807e028976SAvantika Mathur 		if (err)
981a86c6181SAlex Tomas 			goto cleanup;
982a86c6181SAlex Tomas 		brelse(bh);
983a86c6181SAlex Tomas 		bh = NULL;
984a86c6181SAlex Tomas 
985a86c6181SAlex Tomas 		/* correct old index */
986a86c6181SAlex Tomas 		if (m) {
987a86c6181SAlex Tomas 			err = ext4_ext_get_access(handle, inode, path + i);
988a86c6181SAlex Tomas 			if (err)
989a86c6181SAlex Tomas 				goto cleanup;
990e8546d06SMarcin Slusarz 			le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
991a86c6181SAlex Tomas 			err = ext4_ext_dirty(handle, inode, path + i);
992a86c6181SAlex Tomas 			if (err)
993a86c6181SAlex Tomas 				goto cleanup;
994a86c6181SAlex Tomas 		}
995a86c6181SAlex Tomas 
996a86c6181SAlex Tomas 		i--;
997a86c6181SAlex Tomas 	}
998a86c6181SAlex Tomas 
999a86c6181SAlex Tomas 	/* insert new index */
1000a86c6181SAlex Tomas 	err = ext4_ext_insert_index(handle, inode, path + at,
1001a86c6181SAlex Tomas 				    le32_to_cpu(border), newblock);
1002a86c6181SAlex Tomas 
1003a86c6181SAlex Tomas cleanup:
1004a86c6181SAlex Tomas 	if (bh) {
1005a86c6181SAlex Tomas 		if (buffer_locked(bh))
1006a86c6181SAlex Tomas 			unlock_buffer(bh);
1007a86c6181SAlex Tomas 		brelse(bh);
1008a86c6181SAlex Tomas 	}
1009a86c6181SAlex Tomas 
1010a86c6181SAlex Tomas 	if (err) {
1011a86c6181SAlex Tomas 		/* free all allocated blocks in error case */
1012a86c6181SAlex Tomas 		for (i = 0; i < depth; i++) {
1013a86c6181SAlex Tomas 			if (!ablocks[i])
1014a86c6181SAlex Tomas 				continue;
1015e6362609STheodore Ts'o 			ext4_free_blocks(handle, inode, 0, ablocks[i], 1,
1016e6362609STheodore Ts'o 					 EXT4_FREE_BLOCKS_METADATA);
1017a86c6181SAlex Tomas 		}
1018a86c6181SAlex Tomas 	}
1019a86c6181SAlex Tomas 	kfree(ablocks);
1020a86c6181SAlex Tomas 
1021a86c6181SAlex Tomas 	return err;
1022a86c6181SAlex Tomas }
1023a86c6181SAlex Tomas 
1024a86c6181SAlex Tomas /*
1025d0d856e8SRandy Dunlap  * ext4_ext_grow_indepth:
1026d0d856e8SRandy Dunlap  * implements tree growing procedure:
1027a86c6181SAlex Tomas  * - allocates new block
1028a86c6181SAlex Tomas  * - moves top-level data (index block or leaf) into the new block
1029d0d856e8SRandy Dunlap  * - initializes new top-level, creating index that points to the
1030a86c6181SAlex Tomas  *   just created block
1031a86c6181SAlex Tomas  */
1032a86c6181SAlex Tomas static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
1033a86c6181SAlex Tomas 					struct ext4_ext_path *path,
1034a86c6181SAlex Tomas 					struct ext4_extent *newext)
1035a86c6181SAlex Tomas {
1036a86c6181SAlex Tomas 	struct ext4_ext_path *curp = path;
1037a86c6181SAlex Tomas 	struct ext4_extent_header *neh;
1038a86c6181SAlex Tomas 	struct buffer_head *bh;
1039f65e6fbaSAlex Tomas 	ext4_fsblk_t newblock;
1040a86c6181SAlex Tomas 	int err = 0;
1041a86c6181SAlex Tomas 
1042654b4908SAneesh Kumar K.V 	newblock = ext4_ext_new_meta_block(handle, inode, path, newext, &err);
1043a86c6181SAlex Tomas 	if (newblock == 0)
1044a86c6181SAlex Tomas 		return err;
1045a86c6181SAlex Tomas 
1046a86c6181SAlex Tomas 	bh = sb_getblk(inode->i_sb, newblock);
1047a86c6181SAlex Tomas 	if (!bh) {
1048a86c6181SAlex Tomas 		err = -EIO;
1049a86c6181SAlex Tomas 		ext4_std_error(inode->i_sb, err);
1050a86c6181SAlex Tomas 		return err;
1051a86c6181SAlex Tomas 	}
1052a86c6181SAlex Tomas 	lock_buffer(bh);
1053a86c6181SAlex Tomas 
10547e028976SAvantika Mathur 	err = ext4_journal_get_create_access(handle, bh);
10557e028976SAvantika Mathur 	if (err) {
1056a86c6181SAlex Tomas 		unlock_buffer(bh);
1057a86c6181SAlex Tomas 		goto out;
1058a86c6181SAlex Tomas 	}
1059a86c6181SAlex Tomas 
1060a86c6181SAlex Tomas 	/* move top-level index/leaf into new block */
1061a86c6181SAlex Tomas 	memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
1062a86c6181SAlex Tomas 
1063a86c6181SAlex Tomas 	/* set size of new block */
1064a86c6181SAlex Tomas 	neh = ext_block_hdr(bh);
1065a86c6181SAlex Tomas 	/* old root could have indexes or leaves
1066a86c6181SAlex Tomas 	 * so calculate e_max right way */
1067a86c6181SAlex Tomas 	if (ext_depth(inode))
106855ad63bfSTheodore Ts'o 		neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
1069a86c6181SAlex Tomas 	else
107055ad63bfSTheodore Ts'o 		neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
1071a86c6181SAlex Tomas 	neh->eh_magic = EXT4_EXT_MAGIC;
1072a86c6181SAlex Tomas 	set_buffer_uptodate(bh);
1073a86c6181SAlex Tomas 	unlock_buffer(bh);
1074a86c6181SAlex Tomas 
10750390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, inode, bh);
10767e028976SAvantika Mathur 	if (err)
1077a86c6181SAlex Tomas 		goto out;
1078a86c6181SAlex Tomas 
1079a86c6181SAlex Tomas 	/* create index in new top-level index: num,max,pointer */
10807e028976SAvantika Mathur 	err = ext4_ext_get_access(handle, inode, curp);
10817e028976SAvantika Mathur 	if (err)
1082a86c6181SAlex Tomas 		goto out;
1083a86c6181SAlex Tomas 
1084a86c6181SAlex Tomas 	curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
108555ad63bfSTheodore Ts'o 	curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
1086a86c6181SAlex Tomas 	curp->p_hdr->eh_entries = cpu_to_le16(1);
1087a86c6181SAlex Tomas 	curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
1088e9f410b1SDmitry Monakhov 
1089e9f410b1SDmitry Monakhov 	if (path[0].p_hdr->eh_depth)
1090e9f410b1SDmitry Monakhov 		curp->p_idx->ei_block =
1091e9f410b1SDmitry Monakhov 			EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
1092e9f410b1SDmitry Monakhov 	else
1093e9f410b1SDmitry Monakhov 		curp->p_idx->ei_block =
1094e9f410b1SDmitry Monakhov 			EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
1095f65e6fbaSAlex Tomas 	ext4_idx_store_pblock(curp->p_idx, newblock);
1096a86c6181SAlex Tomas 
1097a86c6181SAlex Tomas 	neh = ext_inode_hdr(inode);
10982ae02107SMingming Cao 	ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
1099a86c6181SAlex Tomas 		  le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
11005a0790c2SAndi Kleen 		  le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
1101*bf89d16fSTheodore Ts'o 		  ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
1102a86c6181SAlex Tomas 
1103a86c6181SAlex Tomas 	neh->eh_depth = cpu_to_le16(path->p_depth + 1);
1104a86c6181SAlex Tomas 	err = ext4_ext_dirty(handle, inode, curp);
1105a86c6181SAlex Tomas out:
1106a86c6181SAlex Tomas 	brelse(bh);
1107a86c6181SAlex Tomas 
1108a86c6181SAlex Tomas 	return err;
1109a86c6181SAlex Tomas }
1110a86c6181SAlex Tomas 
1111a86c6181SAlex Tomas /*
1112d0d856e8SRandy Dunlap  * ext4_ext_create_new_leaf:
1113d0d856e8SRandy Dunlap  * finds empty index and adds new leaf.
1114d0d856e8SRandy Dunlap  * if no free index is found, then it requests in-depth growing.
1115a86c6181SAlex Tomas  */
1116a86c6181SAlex Tomas static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
1117a86c6181SAlex Tomas 					struct ext4_ext_path *path,
1118a86c6181SAlex Tomas 					struct ext4_extent *newext)
1119a86c6181SAlex Tomas {
1120a86c6181SAlex Tomas 	struct ext4_ext_path *curp;
1121a86c6181SAlex Tomas 	int depth, i, err = 0;
1122a86c6181SAlex Tomas 
1123a86c6181SAlex Tomas repeat:
1124a86c6181SAlex Tomas 	i = depth = ext_depth(inode);
1125a86c6181SAlex Tomas 
1126a86c6181SAlex Tomas 	/* walk up to the tree and look for free index entry */
1127a86c6181SAlex Tomas 	curp = path + depth;
1128a86c6181SAlex Tomas 	while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1129a86c6181SAlex Tomas 		i--;
1130a86c6181SAlex Tomas 		curp--;
1131a86c6181SAlex Tomas 	}
1132a86c6181SAlex Tomas 
1133d0d856e8SRandy Dunlap 	/* we use already allocated block for index block,
1134d0d856e8SRandy Dunlap 	 * so subsequent data blocks should be contiguous */
1135a86c6181SAlex Tomas 	if (EXT_HAS_FREE_INDEX(curp)) {
1136a86c6181SAlex Tomas 		/* if we found index with free entry, then use that
1137a86c6181SAlex Tomas 		 * entry: create all needed subtree and add new leaf */
1138a86c6181SAlex Tomas 		err = ext4_ext_split(handle, inode, path, newext, i);
1139787e0981SShen Feng 		if (err)
1140787e0981SShen Feng 			goto out;
1141a86c6181SAlex Tomas 
1142a86c6181SAlex Tomas 		/* refill path */
1143a86c6181SAlex Tomas 		ext4_ext_drop_refs(path);
1144a86c6181SAlex Tomas 		path = ext4_ext_find_extent(inode,
1145725d26d3SAneesh Kumar K.V 				    (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1146a86c6181SAlex Tomas 				    path);
1147a86c6181SAlex Tomas 		if (IS_ERR(path))
1148a86c6181SAlex Tomas 			err = PTR_ERR(path);
1149a86c6181SAlex Tomas 	} else {
1150a86c6181SAlex Tomas 		/* tree is full, time to grow in depth */
1151a86c6181SAlex Tomas 		err = ext4_ext_grow_indepth(handle, inode, path, newext);
1152a86c6181SAlex Tomas 		if (err)
1153a86c6181SAlex Tomas 			goto out;
1154a86c6181SAlex Tomas 
1155a86c6181SAlex Tomas 		/* refill path */
1156a86c6181SAlex Tomas 		ext4_ext_drop_refs(path);
1157a86c6181SAlex Tomas 		path = ext4_ext_find_extent(inode,
1158725d26d3SAneesh Kumar K.V 				   (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1159a86c6181SAlex Tomas 				    path);
1160a86c6181SAlex Tomas 		if (IS_ERR(path)) {
1161a86c6181SAlex Tomas 			err = PTR_ERR(path);
1162a86c6181SAlex Tomas 			goto out;
1163a86c6181SAlex Tomas 		}
1164a86c6181SAlex Tomas 
1165a86c6181SAlex Tomas 		/*
1166d0d856e8SRandy Dunlap 		 * only first (depth 0 -> 1) produces free space;
1167d0d856e8SRandy Dunlap 		 * in all other cases we have to split the grown tree
1168a86c6181SAlex Tomas 		 */
1169a86c6181SAlex Tomas 		depth = ext_depth(inode);
1170a86c6181SAlex Tomas 		if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
1171d0d856e8SRandy Dunlap 			/* now we need to split */
1172a86c6181SAlex Tomas 			goto repeat;
1173a86c6181SAlex Tomas 		}
1174a86c6181SAlex Tomas 	}
1175a86c6181SAlex Tomas 
1176a86c6181SAlex Tomas out:
1177a86c6181SAlex Tomas 	return err;
1178a86c6181SAlex Tomas }
1179a86c6181SAlex Tomas 
1180a86c6181SAlex Tomas /*
11811988b51eSAlex Tomas  * search the closest allocated block to the left for *logical
11821988b51eSAlex Tomas  * and returns it at @logical + it's physical address at @phys
11831988b51eSAlex Tomas  * if *logical is the smallest allocated block, the function
11841988b51eSAlex Tomas  * returns 0 at @phys
11851988b51eSAlex Tomas  * return value contains 0 (success) or error code
11861988b51eSAlex Tomas  */
11871f109d5aSTheodore Ts'o static int ext4_ext_search_left(struct inode *inode,
11881f109d5aSTheodore Ts'o 				struct ext4_ext_path *path,
11891988b51eSAlex Tomas 				ext4_lblk_t *logical, ext4_fsblk_t *phys)
11901988b51eSAlex Tomas {
11911988b51eSAlex Tomas 	struct ext4_extent_idx *ix;
11921988b51eSAlex Tomas 	struct ext4_extent *ex;
1193b939e376SAneesh Kumar K.V 	int depth, ee_len;
11941988b51eSAlex Tomas 
1195273df556SFrank Mayhar 	if (unlikely(path == NULL)) {
1196273df556SFrank Mayhar 		EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1197273df556SFrank Mayhar 		return -EIO;
1198273df556SFrank Mayhar 	}
11991988b51eSAlex Tomas 	depth = path->p_depth;
12001988b51eSAlex Tomas 	*phys = 0;
12011988b51eSAlex Tomas 
12021988b51eSAlex Tomas 	if (depth == 0 && path->p_ext == NULL)
12031988b51eSAlex Tomas 		return 0;
12041988b51eSAlex Tomas 
12051988b51eSAlex Tomas 	/* usually extent in the path covers blocks smaller
12061988b51eSAlex Tomas 	 * then *logical, but it can be that extent is the
12071988b51eSAlex Tomas 	 * first one in the file */
12081988b51eSAlex Tomas 
12091988b51eSAlex Tomas 	ex = path[depth].p_ext;
1210b939e376SAneesh Kumar K.V 	ee_len = ext4_ext_get_actual_len(ex);
12111988b51eSAlex Tomas 	if (*logical < le32_to_cpu(ex->ee_block)) {
1212273df556SFrank Mayhar 		if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1213273df556SFrank Mayhar 			EXT4_ERROR_INODE(inode,
1214273df556SFrank Mayhar 					 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1215273df556SFrank Mayhar 					 *logical, le32_to_cpu(ex->ee_block));
1216273df556SFrank Mayhar 			return -EIO;
1217273df556SFrank Mayhar 		}
12181988b51eSAlex Tomas 		while (--depth >= 0) {
12191988b51eSAlex Tomas 			ix = path[depth].p_idx;
1220273df556SFrank Mayhar 			if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1221273df556SFrank Mayhar 				EXT4_ERROR_INODE(inode,
1222273df556SFrank Mayhar 				  "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
1223273df556SFrank Mayhar 				  ix != NULL ? ix->ei_block : 0,
1224273df556SFrank Mayhar 				  EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
1225273df556SFrank Mayhar 				    EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block : 0,
1226273df556SFrank Mayhar 				  depth);
1227273df556SFrank Mayhar 				return -EIO;
1228273df556SFrank Mayhar 			}
12291988b51eSAlex Tomas 		}
12301988b51eSAlex Tomas 		return 0;
12311988b51eSAlex Tomas 	}
12321988b51eSAlex Tomas 
1233273df556SFrank Mayhar 	if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1234273df556SFrank Mayhar 		EXT4_ERROR_INODE(inode,
1235273df556SFrank Mayhar 				 "logical %d < ee_block %d + ee_len %d!",
1236273df556SFrank Mayhar 				 *logical, le32_to_cpu(ex->ee_block), ee_len);
1237273df556SFrank Mayhar 		return -EIO;
1238273df556SFrank Mayhar 	}
12391988b51eSAlex Tomas 
1240b939e376SAneesh Kumar K.V 	*logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
1241*bf89d16fSTheodore Ts'o 	*phys = ext4_ext_pblock(ex) + ee_len - 1;
12421988b51eSAlex Tomas 	return 0;
12431988b51eSAlex Tomas }
12441988b51eSAlex Tomas 
12451988b51eSAlex Tomas /*
12461988b51eSAlex Tomas  * search the closest allocated block to the right for *logical
12471988b51eSAlex Tomas  * and returns it at @logical + it's physical address at @phys
12481988b51eSAlex Tomas  * if *logical is the smallest allocated block, the function
12491988b51eSAlex Tomas  * returns 0 at @phys
12501988b51eSAlex Tomas  * return value contains 0 (success) or error code
12511988b51eSAlex Tomas  */
12521f109d5aSTheodore Ts'o static int ext4_ext_search_right(struct inode *inode,
12531f109d5aSTheodore Ts'o 				 struct ext4_ext_path *path,
12541988b51eSAlex Tomas 				 ext4_lblk_t *logical, ext4_fsblk_t *phys)
12551988b51eSAlex Tomas {
12561988b51eSAlex Tomas 	struct buffer_head *bh = NULL;
12571988b51eSAlex Tomas 	struct ext4_extent_header *eh;
12581988b51eSAlex Tomas 	struct ext4_extent_idx *ix;
12591988b51eSAlex Tomas 	struct ext4_extent *ex;
12601988b51eSAlex Tomas 	ext4_fsblk_t block;
1261395a87bfSEric Sandeen 	int depth;	/* Note, NOT eh_depth; depth from top of tree */
1262395a87bfSEric Sandeen 	int ee_len;
12631988b51eSAlex Tomas 
1264273df556SFrank Mayhar 	if (unlikely(path == NULL)) {
1265273df556SFrank Mayhar 		EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1266273df556SFrank Mayhar 		return -EIO;
1267273df556SFrank Mayhar 	}
12681988b51eSAlex Tomas 	depth = path->p_depth;
12691988b51eSAlex Tomas 	*phys = 0;
12701988b51eSAlex Tomas 
12711988b51eSAlex Tomas 	if (depth == 0 && path->p_ext == NULL)
12721988b51eSAlex Tomas 		return 0;
12731988b51eSAlex Tomas 
12741988b51eSAlex Tomas 	/* usually extent in the path covers blocks smaller
12751988b51eSAlex Tomas 	 * then *logical, but it can be that extent is the
12761988b51eSAlex Tomas 	 * first one in the file */
12771988b51eSAlex Tomas 
12781988b51eSAlex Tomas 	ex = path[depth].p_ext;
1279b939e376SAneesh Kumar K.V 	ee_len = ext4_ext_get_actual_len(ex);
12801988b51eSAlex Tomas 	if (*logical < le32_to_cpu(ex->ee_block)) {
1281273df556SFrank Mayhar 		if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1282273df556SFrank Mayhar 			EXT4_ERROR_INODE(inode,
1283273df556SFrank Mayhar 					 "first_extent(path[%d].p_hdr) != ex",
1284273df556SFrank Mayhar 					 depth);
1285273df556SFrank Mayhar 			return -EIO;
1286273df556SFrank Mayhar 		}
12871988b51eSAlex Tomas 		while (--depth >= 0) {
12881988b51eSAlex Tomas 			ix = path[depth].p_idx;
1289273df556SFrank Mayhar 			if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1290273df556SFrank Mayhar 				EXT4_ERROR_INODE(inode,
1291273df556SFrank Mayhar 						 "ix != EXT_FIRST_INDEX *logical %d!",
1292273df556SFrank Mayhar 						 *logical);
1293273df556SFrank Mayhar 				return -EIO;
1294273df556SFrank Mayhar 			}
12951988b51eSAlex Tomas 		}
12961988b51eSAlex Tomas 		*logical = le32_to_cpu(ex->ee_block);
1297*bf89d16fSTheodore Ts'o 		*phys = ext4_ext_pblock(ex);
12981988b51eSAlex Tomas 		return 0;
12991988b51eSAlex Tomas 	}
13001988b51eSAlex Tomas 
1301273df556SFrank Mayhar 	if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1302273df556SFrank Mayhar 		EXT4_ERROR_INODE(inode,
1303273df556SFrank Mayhar 				 "logical %d < ee_block %d + ee_len %d!",
1304273df556SFrank Mayhar 				 *logical, le32_to_cpu(ex->ee_block), ee_len);
1305273df556SFrank Mayhar 		return -EIO;
1306273df556SFrank Mayhar 	}
13071988b51eSAlex Tomas 
13081988b51eSAlex Tomas 	if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
13091988b51eSAlex Tomas 		/* next allocated block in this leaf */
13101988b51eSAlex Tomas 		ex++;
13111988b51eSAlex Tomas 		*logical = le32_to_cpu(ex->ee_block);
1312*bf89d16fSTheodore Ts'o 		*phys = ext4_ext_pblock(ex);
13131988b51eSAlex Tomas 		return 0;
13141988b51eSAlex Tomas 	}
13151988b51eSAlex Tomas 
13161988b51eSAlex Tomas 	/* go up and search for index to the right */
13171988b51eSAlex Tomas 	while (--depth >= 0) {
13181988b51eSAlex Tomas 		ix = path[depth].p_idx;
13191988b51eSAlex Tomas 		if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
132025f1ee3aSWu Fengguang 			goto got_index;
13211988b51eSAlex Tomas 	}
13221988b51eSAlex Tomas 
132325f1ee3aSWu Fengguang 	/* we've gone up to the root and found no index to the right */
13241988b51eSAlex Tomas 	return 0;
13251988b51eSAlex Tomas 
132625f1ee3aSWu Fengguang got_index:
13271988b51eSAlex Tomas 	/* we've found index to the right, let's
13281988b51eSAlex Tomas 	 * follow it and find the closest allocated
13291988b51eSAlex Tomas 	 * block to the right */
13301988b51eSAlex Tomas 	ix++;
1331*bf89d16fSTheodore Ts'o 	block = ext4_idx_pblock(ix);
13321988b51eSAlex Tomas 	while (++depth < path->p_depth) {
13331988b51eSAlex Tomas 		bh = sb_bread(inode->i_sb, block);
13341988b51eSAlex Tomas 		if (bh == NULL)
13351988b51eSAlex Tomas 			return -EIO;
13361988b51eSAlex Tomas 		eh = ext_block_hdr(bh);
1337395a87bfSEric Sandeen 		/* subtract from p_depth to get proper eh_depth */
133856b19868SAneesh Kumar K.V 		if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
13391988b51eSAlex Tomas 			put_bh(bh);
13401988b51eSAlex Tomas 			return -EIO;
13411988b51eSAlex Tomas 		}
13421988b51eSAlex Tomas 		ix = EXT_FIRST_INDEX(eh);
1343*bf89d16fSTheodore Ts'o 		block = ext4_idx_pblock(ix);
13441988b51eSAlex Tomas 		put_bh(bh);
13451988b51eSAlex Tomas 	}
13461988b51eSAlex Tomas 
13471988b51eSAlex Tomas 	bh = sb_bread(inode->i_sb, block);
13481988b51eSAlex Tomas 	if (bh == NULL)
13491988b51eSAlex Tomas 		return -EIO;
13501988b51eSAlex Tomas 	eh = ext_block_hdr(bh);
135156b19868SAneesh Kumar K.V 	if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
13521988b51eSAlex Tomas 		put_bh(bh);
13531988b51eSAlex Tomas 		return -EIO;
13541988b51eSAlex Tomas 	}
13551988b51eSAlex Tomas 	ex = EXT_FIRST_EXTENT(eh);
13561988b51eSAlex Tomas 	*logical = le32_to_cpu(ex->ee_block);
1357*bf89d16fSTheodore Ts'o 	*phys = ext4_ext_pblock(ex);
13581988b51eSAlex Tomas 	put_bh(bh);
13591988b51eSAlex Tomas 	return 0;
13601988b51eSAlex Tomas }
13611988b51eSAlex Tomas 
13621988b51eSAlex Tomas /*
1363d0d856e8SRandy Dunlap  * ext4_ext_next_allocated_block:
1364d0d856e8SRandy Dunlap  * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1365d0d856e8SRandy Dunlap  * NOTE: it considers block number from index entry as
1366d0d856e8SRandy Dunlap  * allocated block. Thus, index entries have to be consistent
1367d0d856e8SRandy Dunlap  * with leaves.
1368a86c6181SAlex Tomas  */
1369725d26d3SAneesh Kumar K.V static ext4_lblk_t
1370a86c6181SAlex Tomas ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1371a86c6181SAlex Tomas {
1372a86c6181SAlex Tomas 	int depth;
1373a86c6181SAlex Tomas 
1374a86c6181SAlex Tomas 	BUG_ON(path == NULL);
1375a86c6181SAlex Tomas 	depth = path->p_depth;
1376a86c6181SAlex Tomas 
1377a86c6181SAlex Tomas 	if (depth == 0 && path->p_ext == NULL)
1378a86c6181SAlex Tomas 		return EXT_MAX_BLOCK;
1379a86c6181SAlex Tomas 
1380a86c6181SAlex Tomas 	while (depth >= 0) {
1381a86c6181SAlex Tomas 		if (depth == path->p_depth) {
1382a86c6181SAlex Tomas 			/* leaf */
1383a86c6181SAlex Tomas 			if (path[depth].p_ext !=
1384a86c6181SAlex Tomas 					EXT_LAST_EXTENT(path[depth].p_hdr))
1385a86c6181SAlex Tomas 			  return le32_to_cpu(path[depth].p_ext[1].ee_block);
1386a86c6181SAlex Tomas 		} else {
1387a86c6181SAlex Tomas 			/* index */
1388a86c6181SAlex Tomas 			if (path[depth].p_idx !=
1389a86c6181SAlex Tomas 					EXT_LAST_INDEX(path[depth].p_hdr))
1390a86c6181SAlex Tomas 			  return le32_to_cpu(path[depth].p_idx[1].ei_block);
1391a86c6181SAlex Tomas 		}
1392a86c6181SAlex Tomas 		depth--;
1393a86c6181SAlex Tomas 	}
1394a86c6181SAlex Tomas 
1395a86c6181SAlex Tomas 	return EXT_MAX_BLOCK;
1396a86c6181SAlex Tomas }
1397a86c6181SAlex Tomas 
1398a86c6181SAlex Tomas /*
1399d0d856e8SRandy Dunlap  * ext4_ext_next_leaf_block:
1400a86c6181SAlex Tomas  * returns first allocated block from next leaf or EXT_MAX_BLOCK
1401a86c6181SAlex Tomas  */
1402725d26d3SAneesh Kumar K.V static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode,
1403a86c6181SAlex Tomas 					struct ext4_ext_path *path)
1404a86c6181SAlex Tomas {
1405a86c6181SAlex Tomas 	int depth;
1406a86c6181SAlex Tomas 
1407a86c6181SAlex Tomas 	BUG_ON(path == NULL);
1408a86c6181SAlex Tomas 	depth = path->p_depth;
1409a86c6181SAlex Tomas 
1410a86c6181SAlex Tomas 	/* zero-tree has no leaf blocks at all */
1411a86c6181SAlex Tomas 	if (depth == 0)
1412a86c6181SAlex Tomas 		return EXT_MAX_BLOCK;
1413a86c6181SAlex Tomas 
1414a86c6181SAlex Tomas 	/* go to index block */
1415a86c6181SAlex Tomas 	depth--;
1416a86c6181SAlex Tomas 
1417a86c6181SAlex Tomas 	while (depth >= 0) {
1418a86c6181SAlex Tomas 		if (path[depth].p_idx !=
1419a86c6181SAlex Tomas 				EXT_LAST_INDEX(path[depth].p_hdr))
1420725d26d3SAneesh Kumar K.V 			return (ext4_lblk_t)
1421725d26d3SAneesh Kumar K.V 				le32_to_cpu(path[depth].p_idx[1].ei_block);
1422a86c6181SAlex Tomas 		depth--;
1423a86c6181SAlex Tomas 	}
1424a86c6181SAlex Tomas 
1425a86c6181SAlex Tomas 	return EXT_MAX_BLOCK;
1426a86c6181SAlex Tomas }
1427a86c6181SAlex Tomas 
1428a86c6181SAlex Tomas /*
1429d0d856e8SRandy Dunlap  * ext4_ext_correct_indexes:
1430d0d856e8SRandy Dunlap  * if leaf gets modified and modified extent is first in the leaf,
1431d0d856e8SRandy Dunlap  * then we have to correct all indexes above.
1432a86c6181SAlex Tomas  * TODO: do we need to correct tree in all cases?
1433a86c6181SAlex Tomas  */
14341d03ec98SAneesh Kumar K.V static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
1435a86c6181SAlex Tomas 				struct ext4_ext_path *path)
1436a86c6181SAlex Tomas {
1437a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
1438a86c6181SAlex Tomas 	int depth = ext_depth(inode);
1439a86c6181SAlex Tomas 	struct ext4_extent *ex;
1440a86c6181SAlex Tomas 	__le32 border;
1441a86c6181SAlex Tomas 	int k, err = 0;
1442a86c6181SAlex Tomas 
1443a86c6181SAlex Tomas 	eh = path[depth].p_hdr;
1444a86c6181SAlex Tomas 	ex = path[depth].p_ext;
1445273df556SFrank Mayhar 
1446273df556SFrank Mayhar 	if (unlikely(ex == NULL || eh == NULL)) {
1447273df556SFrank Mayhar 		EXT4_ERROR_INODE(inode,
1448273df556SFrank Mayhar 				 "ex %p == NULL or eh %p == NULL", ex, eh);
1449273df556SFrank Mayhar 		return -EIO;
1450273df556SFrank Mayhar 	}
1451a86c6181SAlex Tomas 
1452a86c6181SAlex Tomas 	if (depth == 0) {
1453a86c6181SAlex Tomas 		/* there is no tree at all */
1454a86c6181SAlex Tomas 		return 0;
1455a86c6181SAlex Tomas 	}
1456a86c6181SAlex Tomas 
1457a86c6181SAlex Tomas 	if (ex != EXT_FIRST_EXTENT(eh)) {
1458a86c6181SAlex Tomas 		/* we correct tree if first leaf got modified only */
1459a86c6181SAlex Tomas 		return 0;
1460a86c6181SAlex Tomas 	}
1461a86c6181SAlex Tomas 
1462a86c6181SAlex Tomas 	/*
1463d0d856e8SRandy Dunlap 	 * TODO: we need correction if border is smaller than current one
1464a86c6181SAlex Tomas 	 */
1465a86c6181SAlex Tomas 	k = depth - 1;
1466a86c6181SAlex Tomas 	border = path[depth].p_ext->ee_block;
14677e028976SAvantika Mathur 	err = ext4_ext_get_access(handle, inode, path + k);
14687e028976SAvantika Mathur 	if (err)
1469a86c6181SAlex Tomas 		return err;
1470a86c6181SAlex Tomas 	path[k].p_idx->ei_block = border;
14717e028976SAvantika Mathur 	err = ext4_ext_dirty(handle, inode, path + k);
14727e028976SAvantika Mathur 	if (err)
1473a86c6181SAlex Tomas 		return err;
1474a86c6181SAlex Tomas 
1475a86c6181SAlex Tomas 	while (k--) {
1476a86c6181SAlex Tomas 		/* change all left-side indexes */
1477a86c6181SAlex Tomas 		if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1478a86c6181SAlex Tomas 			break;
14797e028976SAvantika Mathur 		err = ext4_ext_get_access(handle, inode, path + k);
14807e028976SAvantika Mathur 		if (err)
1481a86c6181SAlex Tomas 			break;
1482a86c6181SAlex Tomas 		path[k].p_idx->ei_block = border;
14837e028976SAvantika Mathur 		err = ext4_ext_dirty(handle, inode, path + k);
14847e028976SAvantika Mathur 		if (err)
1485a86c6181SAlex Tomas 			break;
1486a86c6181SAlex Tomas 	}
1487a86c6181SAlex Tomas 
1488a86c6181SAlex Tomas 	return err;
1489a86c6181SAlex Tomas }
1490a86c6181SAlex Tomas 
1491748de673SAkira Fujita int
1492a86c6181SAlex Tomas ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1493a86c6181SAlex Tomas 				struct ext4_extent *ex2)
1494a86c6181SAlex Tomas {
1495749269faSAmit Arora 	unsigned short ext1_ee_len, ext2_ee_len, max_len;
1496a2df2a63SAmit Arora 
1497a2df2a63SAmit Arora 	/*
1498a2df2a63SAmit Arora 	 * Make sure that either both extents are uninitialized, or
1499a2df2a63SAmit Arora 	 * both are _not_.
1500a2df2a63SAmit Arora 	 */
1501a2df2a63SAmit Arora 	if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1502a2df2a63SAmit Arora 		return 0;
1503a2df2a63SAmit Arora 
1504749269faSAmit Arora 	if (ext4_ext_is_uninitialized(ex1))
1505749269faSAmit Arora 		max_len = EXT_UNINIT_MAX_LEN;
1506749269faSAmit Arora 	else
1507749269faSAmit Arora 		max_len = EXT_INIT_MAX_LEN;
1508749269faSAmit Arora 
1509a2df2a63SAmit Arora 	ext1_ee_len = ext4_ext_get_actual_len(ex1);
1510a2df2a63SAmit Arora 	ext2_ee_len = ext4_ext_get_actual_len(ex2);
1511a2df2a63SAmit Arora 
1512a2df2a63SAmit Arora 	if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
151363f57933SAndrew Morton 			le32_to_cpu(ex2->ee_block))
1514a86c6181SAlex Tomas 		return 0;
1515a86c6181SAlex Tomas 
1516471d4011SSuparna Bhattacharya 	/*
1517471d4011SSuparna Bhattacharya 	 * To allow future support for preallocated extents to be added
1518471d4011SSuparna Bhattacharya 	 * as an RO_COMPAT feature, refuse to merge to extents if
1519d0d856e8SRandy Dunlap 	 * this can result in the top bit of ee_len being set.
1520471d4011SSuparna Bhattacharya 	 */
1521749269faSAmit Arora 	if (ext1_ee_len + ext2_ee_len > max_len)
1522471d4011SSuparna Bhattacharya 		return 0;
1523bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST
1524b939e376SAneesh Kumar K.V 	if (ext1_ee_len >= 4)
1525a86c6181SAlex Tomas 		return 0;
1526a86c6181SAlex Tomas #endif
1527a86c6181SAlex Tomas 
1528*bf89d16fSTheodore Ts'o 	if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
1529a86c6181SAlex Tomas 		return 1;
1530a86c6181SAlex Tomas 	return 0;
1531a86c6181SAlex Tomas }
1532a86c6181SAlex Tomas 
1533a86c6181SAlex Tomas /*
153456055d3aSAmit Arora  * This function tries to merge the "ex" extent to the next extent in the tree.
153556055d3aSAmit Arora  * It always tries to merge towards right. If you want to merge towards
153656055d3aSAmit Arora  * left, pass "ex - 1" as argument instead of "ex".
153756055d3aSAmit Arora  * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
153856055d3aSAmit Arora  * 1 if they got merged.
153956055d3aSAmit Arora  */
15401f109d5aSTheodore Ts'o static int ext4_ext_try_to_merge(struct inode *inode,
154156055d3aSAmit Arora 				 struct ext4_ext_path *path,
154256055d3aSAmit Arora 				 struct ext4_extent *ex)
154356055d3aSAmit Arora {
154456055d3aSAmit Arora 	struct ext4_extent_header *eh;
154556055d3aSAmit Arora 	unsigned int depth, len;
154656055d3aSAmit Arora 	int merge_done = 0;
154756055d3aSAmit Arora 	int uninitialized = 0;
154856055d3aSAmit Arora 
154956055d3aSAmit Arora 	depth = ext_depth(inode);
155056055d3aSAmit Arora 	BUG_ON(path[depth].p_hdr == NULL);
155156055d3aSAmit Arora 	eh = path[depth].p_hdr;
155256055d3aSAmit Arora 
155356055d3aSAmit Arora 	while (ex < EXT_LAST_EXTENT(eh)) {
155456055d3aSAmit Arora 		if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
155556055d3aSAmit Arora 			break;
155656055d3aSAmit Arora 		/* merge with next extent! */
155756055d3aSAmit Arora 		if (ext4_ext_is_uninitialized(ex))
155856055d3aSAmit Arora 			uninitialized = 1;
155956055d3aSAmit Arora 		ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
156056055d3aSAmit Arora 				+ ext4_ext_get_actual_len(ex + 1));
156156055d3aSAmit Arora 		if (uninitialized)
156256055d3aSAmit Arora 			ext4_ext_mark_uninitialized(ex);
156356055d3aSAmit Arora 
156456055d3aSAmit Arora 		if (ex + 1 < EXT_LAST_EXTENT(eh)) {
156556055d3aSAmit Arora 			len = (EXT_LAST_EXTENT(eh) - ex - 1)
156656055d3aSAmit Arora 				* sizeof(struct ext4_extent);
156756055d3aSAmit Arora 			memmove(ex + 1, ex + 2, len);
156856055d3aSAmit Arora 		}
1569e8546d06SMarcin Slusarz 		le16_add_cpu(&eh->eh_entries, -1);
157056055d3aSAmit Arora 		merge_done = 1;
157156055d3aSAmit Arora 		WARN_ON(eh->eh_entries == 0);
157256055d3aSAmit Arora 		if (!eh->eh_entries)
157324676da4STheodore Ts'o 			EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
157456055d3aSAmit Arora 	}
157556055d3aSAmit Arora 
157656055d3aSAmit Arora 	return merge_done;
157756055d3aSAmit Arora }
157856055d3aSAmit Arora 
157956055d3aSAmit Arora /*
158025d14f98SAmit Arora  * check if a portion of the "newext" extent overlaps with an
158125d14f98SAmit Arora  * existing extent.
158225d14f98SAmit Arora  *
158325d14f98SAmit Arora  * If there is an overlap discovered, it updates the length of the newext
158425d14f98SAmit Arora  * such that there will be no overlap, and then returns 1.
158525d14f98SAmit Arora  * If there is no overlap found, it returns 0.
158625d14f98SAmit Arora  */
15871f109d5aSTheodore Ts'o static unsigned int ext4_ext_check_overlap(struct inode *inode,
158825d14f98SAmit Arora 					   struct ext4_extent *newext,
158925d14f98SAmit Arora 					   struct ext4_ext_path *path)
159025d14f98SAmit Arora {
1591725d26d3SAneesh Kumar K.V 	ext4_lblk_t b1, b2;
159225d14f98SAmit Arora 	unsigned int depth, len1;
159325d14f98SAmit Arora 	unsigned int ret = 0;
159425d14f98SAmit Arora 
159525d14f98SAmit Arora 	b1 = le32_to_cpu(newext->ee_block);
1596a2df2a63SAmit Arora 	len1 = ext4_ext_get_actual_len(newext);
159725d14f98SAmit Arora 	depth = ext_depth(inode);
159825d14f98SAmit Arora 	if (!path[depth].p_ext)
159925d14f98SAmit Arora 		goto out;
160025d14f98SAmit Arora 	b2 = le32_to_cpu(path[depth].p_ext->ee_block);
160125d14f98SAmit Arora 
160225d14f98SAmit Arora 	/*
160325d14f98SAmit Arora 	 * get the next allocated block if the extent in the path
160425d14f98SAmit Arora 	 * is before the requested block(s)
160525d14f98SAmit Arora 	 */
160625d14f98SAmit Arora 	if (b2 < b1) {
160725d14f98SAmit Arora 		b2 = ext4_ext_next_allocated_block(path);
160825d14f98SAmit Arora 		if (b2 == EXT_MAX_BLOCK)
160925d14f98SAmit Arora 			goto out;
161025d14f98SAmit Arora 	}
161125d14f98SAmit Arora 
1612725d26d3SAneesh Kumar K.V 	/* check for wrap through zero on extent logical start block*/
161325d14f98SAmit Arora 	if (b1 + len1 < b1) {
161425d14f98SAmit Arora 		len1 = EXT_MAX_BLOCK - b1;
161525d14f98SAmit Arora 		newext->ee_len = cpu_to_le16(len1);
161625d14f98SAmit Arora 		ret = 1;
161725d14f98SAmit Arora 	}
161825d14f98SAmit Arora 
161925d14f98SAmit Arora 	/* check for overlap */
162025d14f98SAmit Arora 	if (b1 + len1 > b2) {
162125d14f98SAmit Arora 		newext->ee_len = cpu_to_le16(b2 - b1);
162225d14f98SAmit Arora 		ret = 1;
162325d14f98SAmit Arora 	}
162425d14f98SAmit Arora out:
162525d14f98SAmit Arora 	return ret;
162625d14f98SAmit Arora }
162725d14f98SAmit Arora 
162825d14f98SAmit Arora /*
1629d0d856e8SRandy Dunlap  * ext4_ext_insert_extent:
1630d0d856e8SRandy Dunlap  * tries to merge requsted extent into the existing extent or
1631d0d856e8SRandy Dunlap  * inserts requested extent as new one into the tree,
1632d0d856e8SRandy Dunlap  * creating new leaf in the no-space case.
1633a86c6181SAlex Tomas  */
1634a86c6181SAlex Tomas int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1635a86c6181SAlex Tomas 				struct ext4_ext_path *path,
16360031462bSMingming Cao 				struct ext4_extent *newext, int flag)
1637a86c6181SAlex Tomas {
1638a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
1639a86c6181SAlex Tomas 	struct ext4_extent *ex, *fex;
1640a86c6181SAlex Tomas 	struct ext4_extent *nearex; /* nearest extent */
1641a86c6181SAlex Tomas 	struct ext4_ext_path *npath = NULL;
1642725d26d3SAneesh Kumar K.V 	int depth, len, err;
1643725d26d3SAneesh Kumar K.V 	ext4_lblk_t next;
1644a2df2a63SAmit Arora 	unsigned uninitialized = 0;
1645a86c6181SAlex Tomas 
1646273df556SFrank Mayhar 	if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1647273df556SFrank Mayhar 		EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
1648273df556SFrank Mayhar 		return -EIO;
1649273df556SFrank Mayhar 	}
1650a86c6181SAlex Tomas 	depth = ext_depth(inode);
1651a86c6181SAlex Tomas 	ex = path[depth].p_ext;
1652273df556SFrank Mayhar 	if (unlikely(path[depth].p_hdr == NULL)) {
1653273df556SFrank Mayhar 		EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1654273df556SFrank Mayhar 		return -EIO;
1655273df556SFrank Mayhar 	}
1656a86c6181SAlex Tomas 
1657a86c6181SAlex Tomas 	/* try to insert block into found extent and return */
1658744692dcSJiaying Zhang 	if (ex && !(flag & EXT4_GET_BLOCKS_PRE_IO)
16590031462bSMingming Cao 		&& ext4_can_extents_be_merged(inode, ex, newext)) {
1660553f9008SMingming 		ext_debug("append [%d]%d block to %d:[%d]%d (from %llu)\n",
1661553f9008SMingming 			  ext4_ext_is_uninitialized(newext),
1662a2df2a63SAmit Arora 			  ext4_ext_get_actual_len(newext),
1663a86c6181SAlex Tomas 			  le32_to_cpu(ex->ee_block),
1664553f9008SMingming 			  ext4_ext_is_uninitialized(ex),
1665*bf89d16fSTheodore Ts'o 			  ext4_ext_get_actual_len(ex),
1666*bf89d16fSTheodore Ts'o 			  ext4_ext_pblock(ex));
16677e028976SAvantika Mathur 		err = ext4_ext_get_access(handle, inode, path + depth);
16687e028976SAvantika Mathur 		if (err)
1669a86c6181SAlex Tomas 			return err;
1670a2df2a63SAmit Arora 
1671a2df2a63SAmit Arora 		/*
1672a2df2a63SAmit Arora 		 * ext4_can_extents_be_merged should have checked that either
1673a2df2a63SAmit Arora 		 * both extents are uninitialized, or both aren't. Thus we
1674a2df2a63SAmit Arora 		 * need to check only one of them here.
1675a2df2a63SAmit Arora 		 */
1676a2df2a63SAmit Arora 		if (ext4_ext_is_uninitialized(ex))
1677a2df2a63SAmit Arora 			uninitialized = 1;
1678a2df2a63SAmit Arora 		ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1679a2df2a63SAmit Arora 					+ ext4_ext_get_actual_len(newext));
1680a2df2a63SAmit Arora 		if (uninitialized)
1681a2df2a63SAmit Arora 			ext4_ext_mark_uninitialized(ex);
1682a86c6181SAlex Tomas 		eh = path[depth].p_hdr;
1683a86c6181SAlex Tomas 		nearex = ex;
1684a86c6181SAlex Tomas 		goto merge;
1685a86c6181SAlex Tomas 	}
1686a86c6181SAlex Tomas 
1687a86c6181SAlex Tomas repeat:
1688a86c6181SAlex Tomas 	depth = ext_depth(inode);
1689a86c6181SAlex Tomas 	eh = path[depth].p_hdr;
1690a86c6181SAlex Tomas 	if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1691a86c6181SAlex Tomas 		goto has_space;
1692a86c6181SAlex Tomas 
1693a86c6181SAlex Tomas 	/* probably next leaf has space for us? */
1694a86c6181SAlex Tomas 	fex = EXT_LAST_EXTENT(eh);
1695a86c6181SAlex Tomas 	next = ext4_ext_next_leaf_block(inode, path);
1696a86c6181SAlex Tomas 	if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
1697a86c6181SAlex Tomas 	    && next != EXT_MAX_BLOCK) {
1698a86c6181SAlex Tomas 		ext_debug("next leaf block - %d\n", next);
1699a86c6181SAlex Tomas 		BUG_ON(npath != NULL);
1700a86c6181SAlex Tomas 		npath = ext4_ext_find_extent(inode, next, NULL);
1701a86c6181SAlex Tomas 		if (IS_ERR(npath))
1702a86c6181SAlex Tomas 			return PTR_ERR(npath);
1703a86c6181SAlex Tomas 		BUG_ON(npath->p_depth != path->p_depth);
1704a86c6181SAlex Tomas 		eh = npath[depth].p_hdr;
1705a86c6181SAlex Tomas 		if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
1706a86c6181SAlex Tomas 			ext_debug("next leaf isnt full(%d)\n",
1707a86c6181SAlex Tomas 				  le16_to_cpu(eh->eh_entries));
1708a86c6181SAlex Tomas 			path = npath;
1709a86c6181SAlex Tomas 			goto repeat;
1710a86c6181SAlex Tomas 		}
1711a86c6181SAlex Tomas 		ext_debug("next leaf has no free space(%d,%d)\n",
1712a86c6181SAlex Tomas 			  le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1713a86c6181SAlex Tomas 	}
1714a86c6181SAlex Tomas 
1715a86c6181SAlex Tomas 	/*
1716d0d856e8SRandy Dunlap 	 * There is no free space in the found leaf.
1717d0d856e8SRandy Dunlap 	 * We're gonna add a new leaf in the tree.
1718a86c6181SAlex Tomas 	 */
1719a86c6181SAlex Tomas 	err = ext4_ext_create_new_leaf(handle, inode, path, newext);
1720a86c6181SAlex Tomas 	if (err)
1721a86c6181SAlex Tomas 		goto cleanup;
1722a86c6181SAlex Tomas 	depth = ext_depth(inode);
1723a86c6181SAlex Tomas 	eh = path[depth].p_hdr;
1724a86c6181SAlex Tomas 
1725a86c6181SAlex Tomas has_space:
1726a86c6181SAlex Tomas 	nearex = path[depth].p_ext;
1727a86c6181SAlex Tomas 
17287e028976SAvantika Mathur 	err = ext4_ext_get_access(handle, inode, path + depth);
17297e028976SAvantika Mathur 	if (err)
1730a86c6181SAlex Tomas 		goto cleanup;
1731a86c6181SAlex Tomas 
1732a86c6181SAlex Tomas 	if (!nearex) {
1733a86c6181SAlex Tomas 		/* there is no extent in this leaf, create first one */
1734553f9008SMingming 		ext_debug("first extent in the leaf: %d:%llu:[%d]%d\n",
1735a86c6181SAlex Tomas 				le32_to_cpu(newext->ee_block),
1736*bf89d16fSTheodore Ts'o 				ext4_ext_pblock(newext),
1737553f9008SMingming 				ext4_ext_is_uninitialized(newext),
1738a2df2a63SAmit Arora 				ext4_ext_get_actual_len(newext));
1739a86c6181SAlex Tomas 		path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1740a86c6181SAlex Tomas 	} else if (le32_to_cpu(newext->ee_block)
1741a86c6181SAlex Tomas 			   > le32_to_cpu(nearex->ee_block)) {
1742a86c6181SAlex Tomas /*		BUG_ON(newext->ee_block == nearex->ee_block); */
1743a86c6181SAlex Tomas 		if (nearex != EXT_LAST_EXTENT(eh)) {
1744a86c6181SAlex Tomas 			len = EXT_MAX_EXTENT(eh) - nearex;
1745a86c6181SAlex Tomas 			len = (len - 1) * sizeof(struct ext4_extent);
1746a86c6181SAlex Tomas 			len = len < 0 ? 0 : len;
1747553f9008SMingming 			ext_debug("insert %d:%llu:[%d]%d after: nearest 0x%p, "
1748a86c6181SAlex Tomas 					"move %d from 0x%p to 0x%p\n",
1749a86c6181SAlex Tomas 					le32_to_cpu(newext->ee_block),
1750*bf89d16fSTheodore Ts'o 					ext4_ext_pblock(newext),
1751553f9008SMingming 					ext4_ext_is_uninitialized(newext),
1752a2df2a63SAmit Arora 					ext4_ext_get_actual_len(newext),
1753a86c6181SAlex Tomas 					nearex, len, nearex + 1, nearex + 2);
1754a86c6181SAlex Tomas 			memmove(nearex + 2, nearex + 1, len);
1755a86c6181SAlex Tomas 		}
1756a86c6181SAlex Tomas 		path[depth].p_ext = nearex + 1;
1757a86c6181SAlex Tomas 	} else {
1758a86c6181SAlex Tomas 		BUG_ON(newext->ee_block == nearex->ee_block);
1759a86c6181SAlex Tomas 		len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1760a86c6181SAlex Tomas 		len = len < 0 ? 0 : len;
1761553f9008SMingming 		ext_debug("insert %d:%llu:[%d]%d before: nearest 0x%p, "
1762a86c6181SAlex Tomas 				"move %d from 0x%p to 0x%p\n",
1763a86c6181SAlex Tomas 				le32_to_cpu(newext->ee_block),
1764*bf89d16fSTheodore Ts'o 				ext4_ext_pblock(newext),
1765553f9008SMingming 				ext4_ext_is_uninitialized(newext),
1766a2df2a63SAmit Arora 				ext4_ext_get_actual_len(newext),
1767a86c6181SAlex Tomas 				nearex, len, nearex + 1, nearex + 2);
1768a86c6181SAlex Tomas 		memmove(nearex + 1, nearex, len);
1769a86c6181SAlex Tomas 		path[depth].p_ext = nearex;
1770a86c6181SAlex Tomas 	}
1771a86c6181SAlex Tomas 
1772e8546d06SMarcin Slusarz 	le16_add_cpu(&eh->eh_entries, 1);
1773a86c6181SAlex Tomas 	nearex = path[depth].p_ext;
1774a86c6181SAlex Tomas 	nearex->ee_block = newext->ee_block;
1775*bf89d16fSTheodore Ts'o 	ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
1776a86c6181SAlex Tomas 	nearex->ee_len = newext->ee_len;
1777a86c6181SAlex Tomas 
1778a86c6181SAlex Tomas merge:
1779a86c6181SAlex Tomas 	/* try to merge extents to the right */
1780744692dcSJiaying Zhang 	if (!(flag & EXT4_GET_BLOCKS_PRE_IO))
178156055d3aSAmit Arora 		ext4_ext_try_to_merge(inode, path, nearex);
1782a86c6181SAlex Tomas 
1783a86c6181SAlex Tomas 	/* try to merge extents to the left */
1784a86c6181SAlex Tomas 
1785a86c6181SAlex Tomas 	/* time to correct all indexes above */
1786a86c6181SAlex Tomas 	err = ext4_ext_correct_indexes(handle, inode, path);
1787a86c6181SAlex Tomas 	if (err)
1788a86c6181SAlex Tomas 		goto cleanup;
1789a86c6181SAlex Tomas 
1790a86c6181SAlex Tomas 	err = ext4_ext_dirty(handle, inode, path + depth);
1791a86c6181SAlex Tomas 
1792a86c6181SAlex Tomas cleanup:
1793a86c6181SAlex Tomas 	if (npath) {
1794a86c6181SAlex Tomas 		ext4_ext_drop_refs(npath);
1795a86c6181SAlex Tomas 		kfree(npath);
1796a86c6181SAlex Tomas 	}
1797a86c6181SAlex Tomas 	ext4_ext_invalidate_cache(inode);
1798a86c6181SAlex Tomas 	return err;
1799a86c6181SAlex Tomas }
1800a86c6181SAlex Tomas 
18011f109d5aSTheodore Ts'o static int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block,
18026873fa0dSEric Sandeen 			       ext4_lblk_t num, ext_prepare_callback func,
18036873fa0dSEric Sandeen 			       void *cbdata)
18046873fa0dSEric Sandeen {
18056873fa0dSEric Sandeen 	struct ext4_ext_path *path = NULL;
18066873fa0dSEric Sandeen 	struct ext4_ext_cache cbex;
18076873fa0dSEric Sandeen 	struct ext4_extent *ex;
18086873fa0dSEric Sandeen 	ext4_lblk_t next, start = 0, end = 0;
18096873fa0dSEric Sandeen 	ext4_lblk_t last = block + num;
18106873fa0dSEric Sandeen 	int depth, exists, err = 0;
18116873fa0dSEric Sandeen 
18126873fa0dSEric Sandeen 	BUG_ON(func == NULL);
18136873fa0dSEric Sandeen 	BUG_ON(inode == NULL);
18146873fa0dSEric Sandeen 
18156873fa0dSEric Sandeen 	while (block < last && block != EXT_MAX_BLOCK) {
18166873fa0dSEric Sandeen 		num = last - block;
18176873fa0dSEric Sandeen 		/* find extent for this block */
1818fab3a549STheodore Ts'o 		down_read(&EXT4_I(inode)->i_data_sem);
18196873fa0dSEric Sandeen 		path = ext4_ext_find_extent(inode, block, path);
1820fab3a549STheodore Ts'o 		up_read(&EXT4_I(inode)->i_data_sem);
18216873fa0dSEric Sandeen 		if (IS_ERR(path)) {
18226873fa0dSEric Sandeen 			err = PTR_ERR(path);
18236873fa0dSEric Sandeen 			path = NULL;
18246873fa0dSEric Sandeen 			break;
18256873fa0dSEric Sandeen 		}
18266873fa0dSEric Sandeen 
18276873fa0dSEric Sandeen 		depth = ext_depth(inode);
1828273df556SFrank Mayhar 		if (unlikely(path[depth].p_hdr == NULL)) {
1829273df556SFrank Mayhar 			EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1830273df556SFrank Mayhar 			err = -EIO;
1831273df556SFrank Mayhar 			break;
1832273df556SFrank Mayhar 		}
18336873fa0dSEric Sandeen 		ex = path[depth].p_ext;
18346873fa0dSEric Sandeen 		next = ext4_ext_next_allocated_block(path);
18356873fa0dSEric Sandeen 
18366873fa0dSEric Sandeen 		exists = 0;
18376873fa0dSEric Sandeen 		if (!ex) {
18386873fa0dSEric Sandeen 			/* there is no extent yet, so try to allocate
18396873fa0dSEric Sandeen 			 * all requested space */
18406873fa0dSEric Sandeen 			start = block;
18416873fa0dSEric Sandeen 			end = block + num;
18426873fa0dSEric Sandeen 		} else if (le32_to_cpu(ex->ee_block) > block) {
18436873fa0dSEric Sandeen 			/* need to allocate space before found extent */
18446873fa0dSEric Sandeen 			start = block;
18456873fa0dSEric Sandeen 			end = le32_to_cpu(ex->ee_block);
18466873fa0dSEric Sandeen 			if (block + num < end)
18476873fa0dSEric Sandeen 				end = block + num;
18486873fa0dSEric Sandeen 		} else if (block >= le32_to_cpu(ex->ee_block)
18496873fa0dSEric Sandeen 					+ ext4_ext_get_actual_len(ex)) {
18506873fa0dSEric Sandeen 			/* need to allocate space after found extent */
18516873fa0dSEric Sandeen 			start = block;
18526873fa0dSEric Sandeen 			end = block + num;
18536873fa0dSEric Sandeen 			if (end >= next)
18546873fa0dSEric Sandeen 				end = next;
18556873fa0dSEric Sandeen 		} else if (block >= le32_to_cpu(ex->ee_block)) {
18566873fa0dSEric Sandeen 			/*
18576873fa0dSEric Sandeen 			 * some part of requested space is covered
18586873fa0dSEric Sandeen 			 * by found extent
18596873fa0dSEric Sandeen 			 */
18606873fa0dSEric Sandeen 			start = block;
18616873fa0dSEric Sandeen 			end = le32_to_cpu(ex->ee_block)
18626873fa0dSEric Sandeen 				+ ext4_ext_get_actual_len(ex);
18636873fa0dSEric Sandeen 			if (block + num < end)
18646873fa0dSEric Sandeen 				end = block + num;
18656873fa0dSEric Sandeen 			exists = 1;
18666873fa0dSEric Sandeen 		} else {
18676873fa0dSEric Sandeen 			BUG();
18686873fa0dSEric Sandeen 		}
18696873fa0dSEric Sandeen 		BUG_ON(end <= start);
18706873fa0dSEric Sandeen 
18716873fa0dSEric Sandeen 		if (!exists) {
18726873fa0dSEric Sandeen 			cbex.ec_block = start;
18736873fa0dSEric Sandeen 			cbex.ec_len = end - start;
18746873fa0dSEric Sandeen 			cbex.ec_start = 0;
18756873fa0dSEric Sandeen 			cbex.ec_type = EXT4_EXT_CACHE_GAP;
18766873fa0dSEric Sandeen 		} else {
18776873fa0dSEric Sandeen 			cbex.ec_block = le32_to_cpu(ex->ee_block);
18786873fa0dSEric Sandeen 			cbex.ec_len = ext4_ext_get_actual_len(ex);
1879*bf89d16fSTheodore Ts'o 			cbex.ec_start = ext4_ext_pblock(ex);
18806873fa0dSEric Sandeen 			cbex.ec_type = EXT4_EXT_CACHE_EXTENT;
18816873fa0dSEric Sandeen 		}
18826873fa0dSEric Sandeen 
1883273df556SFrank Mayhar 		if (unlikely(cbex.ec_len == 0)) {
1884273df556SFrank Mayhar 			EXT4_ERROR_INODE(inode, "cbex.ec_len == 0");
1885273df556SFrank Mayhar 			err = -EIO;
1886273df556SFrank Mayhar 			break;
1887273df556SFrank Mayhar 		}
18886873fa0dSEric Sandeen 		err = func(inode, path, &cbex, ex, cbdata);
18896873fa0dSEric Sandeen 		ext4_ext_drop_refs(path);
18906873fa0dSEric Sandeen 
18916873fa0dSEric Sandeen 		if (err < 0)
18926873fa0dSEric Sandeen 			break;
18936873fa0dSEric Sandeen 
18946873fa0dSEric Sandeen 		if (err == EXT_REPEAT)
18956873fa0dSEric Sandeen 			continue;
18966873fa0dSEric Sandeen 		else if (err == EXT_BREAK) {
18976873fa0dSEric Sandeen 			err = 0;
18986873fa0dSEric Sandeen 			break;
18996873fa0dSEric Sandeen 		}
19006873fa0dSEric Sandeen 
19016873fa0dSEric Sandeen 		if (ext_depth(inode) != depth) {
19026873fa0dSEric Sandeen 			/* depth was changed. we have to realloc path */
19036873fa0dSEric Sandeen 			kfree(path);
19046873fa0dSEric Sandeen 			path = NULL;
19056873fa0dSEric Sandeen 		}
19066873fa0dSEric Sandeen 
19076873fa0dSEric Sandeen 		block = cbex.ec_block + cbex.ec_len;
19086873fa0dSEric Sandeen 	}
19096873fa0dSEric Sandeen 
19106873fa0dSEric Sandeen 	if (path) {
19116873fa0dSEric Sandeen 		ext4_ext_drop_refs(path);
19126873fa0dSEric Sandeen 		kfree(path);
19136873fa0dSEric Sandeen 	}
19146873fa0dSEric Sandeen 
19156873fa0dSEric Sandeen 	return err;
19166873fa0dSEric Sandeen }
19176873fa0dSEric Sandeen 
191809b88252SAvantika Mathur static void
1919725d26d3SAneesh Kumar K.V ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
1920dd54567aSMingming Cao 			__u32 len, ext4_fsblk_t start, int type)
1921a86c6181SAlex Tomas {
1922a86c6181SAlex Tomas 	struct ext4_ext_cache *cex;
1923a86c6181SAlex Tomas 	BUG_ON(len == 0);
19242ec0ae3aSTheodore Ts'o 	spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1925a86c6181SAlex Tomas 	cex = &EXT4_I(inode)->i_cached_extent;
1926a86c6181SAlex Tomas 	cex->ec_type = type;
1927a86c6181SAlex Tomas 	cex->ec_block = block;
1928a86c6181SAlex Tomas 	cex->ec_len = len;
1929a86c6181SAlex Tomas 	cex->ec_start = start;
19302ec0ae3aSTheodore Ts'o 	spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1931a86c6181SAlex Tomas }
1932a86c6181SAlex Tomas 
1933a86c6181SAlex Tomas /*
1934d0d856e8SRandy Dunlap  * ext4_ext_put_gap_in_cache:
1935d0d856e8SRandy Dunlap  * calculate boundaries of the gap that the requested block fits into
1936a86c6181SAlex Tomas  * and cache this gap
1937a86c6181SAlex Tomas  */
193809b88252SAvantika Mathur static void
1939a86c6181SAlex Tomas ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
1940725d26d3SAneesh Kumar K.V 				ext4_lblk_t block)
1941a86c6181SAlex Tomas {
1942a86c6181SAlex Tomas 	int depth = ext_depth(inode);
1943725d26d3SAneesh Kumar K.V 	unsigned long len;
1944725d26d3SAneesh Kumar K.V 	ext4_lblk_t lblock;
1945a86c6181SAlex Tomas 	struct ext4_extent *ex;
1946a86c6181SAlex Tomas 
1947a86c6181SAlex Tomas 	ex = path[depth].p_ext;
1948a86c6181SAlex Tomas 	if (ex == NULL) {
1949a86c6181SAlex Tomas 		/* there is no extent yet, so gap is [0;-] */
1950a86c6181SAlex Tomas 		lblock = 0;
1951a86c6181SAlex Tomas 		len = EXT_MAX_BLOCK;
1952a86c6181SAlex Tomas 		ext_debug("cache gap(whole file):");
1953a86c6181SAlex Tomas 	} else if (block < le32_to_cpu(ex->ee_block)) {
1954a86c6181SAlex Tomas 		lblock = block;
1955a86c6181SAlex Tomas 		len = le32_to_cpu(ex->ee_block) - block;
1956bba90743SEric Sandeen 		ext_debug("cache gap(before): %u [%u:%u]",
1957bba90743SEric Sandeen 				block,
1958bba90743SEric Sandeen 				le32_to_cpu(ex->ee_block),
1959bba90743SEric Sandeen 				 ext4_ext_get_actual_len(ex));
1960a86c6181SAlex Tomas 	} else if (block >= le32_to_cpu(ex->ee_block)
1961a2df2a63SAmit Arora 			+ ext4_ext_get_actual_len(ex)) {
1962725d26d3SAneesh Kumar K.V 		ext4_lblk_t next;
1963a86c6181SAlex Tomas 		lblock = le32_to_cpu(ex->ee_block)
1964a2df2a63SAmit Arora 			+ ext4_ext_get_actual_len(ex);
1965725d26d3SAneesh Kumar K.V 
1966725d26d3SAneesh Kumar K.V 		next = ext4_ext_next_allocated_block(path);
1967bba90743SEric Sandeen 		ext_debug("cache gap(after): [%u:%u] %u",
1968bba90743SEric Sandeen 				le32_to_cpu(ex->ee_block),
1969bba90743SEric Sandeen 				ext4_ext_get_actual_len(ex),
1970bba90743SEric Sandeen 				block);
1971725d26d3SAneesh Kumar K.V 		BUG_ON(next == lblock);
1972725d26d3SAneesh Kumar K.V 		len = next - lblock;
1973a86c6181SAlex Tomas 	} else {
1974a86c6181SAlex Tomas 		lblock = len = 0;
1975a86c6181SAlex Tomas 		BUG();
1976a86c6181SAlex Tomas 	}
1977a86c6181SAlex Tomas 
1978bba90743SEric Sandeen 	ext_debug(" -> %u:%lu\n", lblock, len);
1979a86c6181SAlex Tomas 	ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
1980a86c6181SAlex Tomas }
1981a86c6181SAlex Tomas 
198209b88252SAvantika Mathur static int
1983725d26d3SAneesh Kumar K.V ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
1984a86c6181SAlex Tomas 			struct ext4_extent *ex)
1985a86c6181SAlex Tomas {
1986a86c6181SAlex Tomas 	struct ext4_ext_cache *cex;
19872ec0ae3aSTheodore Ts'o 	int ret = EXT4_EXT_CACHE_NO;
1988a86c6181SAlex Tomas 
19892ec0ae3aSTheodore Ts'o 	/*
19902ec0ae3aSTheodore Ts'o 	 * We borrow i_block_reservation_lock to protect i_cached_extent
19912ec0ae3aSTheodore Ts'o 	 */
19922ec0ae3aSTheodore Ts'o 	spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1993a86c6181SAlex Tomas 	cex = &EXT4_I(inode)->i_cached_extent;
1994a86c6181SAlex Tomas 
1995a86c6181SAlex Tomas 	/* has cache valid data? */
1996a86c6181SAlex Tomas 	if (cex->ec_type == EXT4_EXT_CACHE_NO)
19972ec0ae3aSTheodore Ts'o 		goto errout;
1998a86c6181SAlex Tomas 
1999a86c6181SAlex Tomas 	BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
2000a86c6181SAlex Tomas 			cex->ec_type != EXT4_EXT_CACHE_EXTENT);
2001731eb1a0SAkinobu Mita 	if (in_range(block, cex->ec_block, cex->ec_len)) {
2002a86c6181SAlex Tomas 		ex->ee_block = cpu_to_le32(cex->ec_block);
2003f65e6fbaSAlex Tomas 		ext4_ext_store_pblock(ex, cex->ec_start);
2004a86c6181SAlex Tomas 		ex->ee_len = cpu_to_le16(cex->ec_len);
2005bba90743SEric Sandeen 		ext_debug("%u cached by %u:%u:%llu\n",
2006bba90743SEric Sandeen 				block,
2007bba90743SEric Sandeen 				cex->ec_block, cex->ec_len, cex->ec_start);
20082ec0ae3aSTheodore Ts'o 		ret = cex->ec_type;
2009a86c6181SAlex Tomas 	}
20102ec0ae3aSTheodore Ts'o errout:
20112ec0ae3aSTheodore Ts'o 	spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
20122ec0ae3aSTheodore Ts'o 	return ret;
2013a86c6181SAlex Tomas }
2014a86c6181SAlex Tomas 
2015a86c6181SAlex Tomas /*
2016d0d856e8SRandy Dunlap  * ext4_ext_rm_idx:
2017d0d856e8SRandy Dunlap  * removes index from the index block.
2018d0d856e8SRandy Dunlap  * It's used in truncate case only, thus all requests are for
2019d0d856e8SRandy Dunlap  * last index in the block only.
2020a86c6181SAlex Tomas  */
20211d03ec98SAneesh Kumar K.V static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
2022a86c6181SAlex Tomas 			struct ext4_ext_path *path)
2023a86c6181SAlex Tomas {
2024a86c6181SAlex Tomas 	int err;
2025f65e6fbaSAlex Tomas 	ext4_fsblk_t leaf;
2026a86c6181SAlex Tomas 
2027a86c6181SAlex Tomas 	/* free index block */
2028a86c6181SAlex Tomas 	path--;
2029*bf89d16fSTheodore Ts'o 	leaf = ext4_idx_pblock(path->p_idx);
2030273df556SFrank Mayhar 	if (unlikely(path->p_hdr->eh_entries == 0)) {
2031273df556SFrank Mayhar 		EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
2032273df556SFrank Mayhar 		return -EIO;
2033273df556SFrank Mayhar 	}
20347e028976SAvantika Mathur 	err = ext4_ext_get_access(handle, inode, path);
20357e028976SAvantika Mathur 	if (err)
2036a86c6181SAlex Tomas 		return err;
2037e8546d06SMarcin Slusarz 	le16_add_cpu(&path->p_hdr->eh_entries, -1);
20387e028976SAvantika Mathur 	err = ext4_ext_dirty(handle, inode, path);
20397e028976SAvantika Mathur 	if (err)
2040a86c6181SAlex Tomas 		return err;
20412ae02107SMingming Cao 	ext_debug("index is empty, remove it, free block %llu\n", leaf);
2042e6362609STheodore Ts'o 	ext4_free_blocks(handle, inode, 0, leaf, 1,
2043e6362609STheodore Ts'o 			 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
2044a86c6181SAlex Tomas 	return err;
2045a86c6181SAlex Tomas }
2046a86c6181SAlex Tomas 
2047a86c6181SAlex Tomas /*
2048ee12b630SMingming Cao  * ext4_ext_calc_credits_for_single_extent:
2049ee12b630SMingming Cao  * This routine returns max. credits that needed to insert an extent
2050ee12b630SMingming Cao  * to the extent tree.
2051ee12b630SMingming Cao  * When pass the actual path, the caller should calculate credits
2052ee12b630SMingming Cao  * under i_data_sem.
2053a86c6181SAlex Tomas  */
2054525f4ed8SMingming Cao int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
2055a86c6181SAlex Tomas 						struct ext4_ext_path *path)
2056a86c6181SAlex Tomas {
2057a86c6181SAlex Tomas 	if (path) {
2058ee12b630SMingming Cao 		int depth = ext_depth(inode);
2059f3bd1f3fSMingming Cao 		int ret = 0;
2060ee12b630SMingming Cao 
2061a86c6181SAlex Tomas 		/* probably there is space in leaf? */
2062a86c6181SAlex Tomas 		if (le16_to_cpu(path[depth].p_hdr->eh_entries)
2063ee12b630SMingming Cao 				< le16_to_cpu(path[depth].p_hdr->eh_max)) {
2064ee12b630SMingming Cao 
2065ee12b630SMingming Cao 			/*
2066ee12b630SMingming Cao 			 *  There are some space in the leaf tree, no
2067ee12b630SMingming Cao 			 *  need to account for leaf block credit
2068ee12b630SMingming Cao 			 *
2069ee12b630SMingming Cao 			 *  bitmaps and block group descriptor blocks
2070ee12b630SMingming Cao 			 *  and other metadat blocks still need to be
2071ee12b630SMingming Cao 			 *  accounted.
2072ee12b630SMingming Cao 			 */
2073525f4ed8SMingming Cao 			/* 1 bitmap, 1 block group descriptor */
2074ee12b630SMingming Cao 			ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
20755887e98bSAneesh Kumar K.V 			return ret;
2076ee12b630SMingming Cao 		}
2077ee12b630SMingming Cao 	}
2078ee12b630SMingming Cao 
2079525f4ed8SMingming Cao 	return ext4_chunk_trans_blocks(inode, nrblocks);
2080a86c6181SAlex Tomas }
2081a86c6181SAlex Tomas 
2082a86c6181SAlex Tomas /*
2083ee12b630SMingming Cao  * How many index/leaf blocks need to change/allocate to modify nrblocks?
2084ee12b630SMingming Cao  *
2085ee12b630SMingming Cao  * if nrblocks are fit in a single extent (chunk flag is 1), then
2086ee12b630SMingming Cao  * in the worse case, each tree level index/leaf need to be changed
2087ee12b630SMingming Cao  * if the tree split due to insert a new extent, then the old tree
2088ee12b630SMingming Cao  * index/leaf need to be updated too
2089ee12b630SMingming Cao  *
2090ee12b630SMingming Cao  * If the nrblocks are discontiguous, they could cause
2091ee12b630SMingming Cao  * the whole tree split more than once, but this is really rare.
2092a86c6181SAlex Tomas  */
2093525f4ed8SMingming Cao int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
2094ee12b630SMingming Cao {
2095ee12b630SMingming Cao 	int index;
2096ee12b630SMingming Cao 	int depth = ext_depth(inode);
2097a86c6181SAlex Tomas 
2098ee12b630SMingming Cao 	if (chunk)
2099ee12b630SMingming Cao 		index = depth * 2;
2100ee12b630SMingming Cao 	else
2101ee12b630SMingming Cao 		index = depth * 3;
2102a86c6181SAlex Tomas 
2103ee12b630SMingming Cao 	return index;
2104a86c6181SAlex Tomas }
2105a86c6181SAlex Tomas 
2106a86c6181SAlex Tomas static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
2107a86c6181SAlex Tomas 				struct ext4_extent *ex,
2108725d26d3SAneesh Kumar K.V 				ext4_lblk_t from, ext4_lblk_t to)
2109a86c6181SAlex Tomas {
2110a2df2a63SAmit Arora 	unsigned short ee_len =  ext4_ext_get_actual_len(ex);
2111e6362609STheodore Ts'o 	int flags = EXT4_FREE_BLOCKS_FORGET;
2112a86c6181SAlex Tomas 
2113c9de560dSAlex Tomas 	if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2114e6362609STheodore Ts'o 		flags |= EXT4_FREE_BLOCKS_METADATA;
2115a86c6181SAlex Tomas #ifdef EXTENTS_STATS
2116a86c6181SAlex Tomas 	{
2117a86c6181SAlex Tomas 		struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2118a86c6181SAlex Tomas 		spin_lock(&sbi->s_ext_stats_lock);
2119a86c6181SAlex Tomas 		sbi->s_ext_blocks += ee_len;
2120a86c6181SAlex Tomas 		sbi->s_ext_extents++;
2121a86c6181SAlex Tomas 		if (ee_len < sbi->s_ext_min)
2122a86c6181SAlex Tomas 			sbi->s_ext_min = ee_len;
2123a86c6181SAlex Tomas 		if (ee_len > sbi->s_ext_max)
2124a86c6181SAlex Tomas 			sbi->s_ext_max = ee_len;
2125a86c6181SAlex Tomas 		if (ext_depth(inode) > sbi->s_depth_max)
2126a86c6181SAlex Tomas 			sbi->s_depth_max = ext_depth(inode);
2127a86c6181SAlex Tomas 		spin_unlock(&sbi->s_ext_stats_lock);
2128a86c6181SAlex Tomas 	}
2129a86c6181SAlex Tomas #endif
2130a86c6181SAlex Tomas 	if (from >= le32_to_cpu(ex->ee_block)
2131a2df2a63SAmit Arora 	    && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
2132a86c6181SAlex Tomas 		/* tail removal */
2133725d26d3SAneesh Kumar K.V 		ext4_lblk_t num;
2134f65e6fbaSAlex Tomas 		ext4_fsblk_t start;
2135725d26d3SAneesh Kumar K.V 
2136a2df2a63SAmit Arora 		num = le32_to_cpu(ex->ee_block) + ee_len - from;
2137*bf89d16fSTheodore Ts'o 		start = ext4_ext_pblock(ex) + ee_len - num;
2138725d26d3SAneesh Kumar K.V 		ext_debug("free last %u blocks starting %llu\n", num, start);
2139e6362609STheodore Ts'o 		ext4_free_blocks(handle, inode, 0, start, num, flags);
2140a86c6181SAlex Tomas 	} else if (from == le32_to_cpu(ex->ee_block)
2141a2df2a63SAmit Arora 		   && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
2142725d26d3SAneesh Kumar K.V 		printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n",
2143a2df2a63SAmit Arora 			from, to, le32_to_cpu(ex->ee_block), ee_len);
2144a86c6181SAlex Tomas 	} else {
2145725d26d3SAneesh Kumar K.V 		printk(KERN_INFO "strange request: removal(2) "
2146725d26d3SAneesh Kumar K.V 				"%u-%u from %u:%u\n",
2147a2df2a63SAmit Arora 				from, to, le32_to_cpu(ex->ee_block), ee_len);
2148a86c6181SAlex Tomas 	}
2149a86c6181SAlex Tomas 	return 0;
2150a86c6181SAlex Tomas }
2151a86c6181SAlex Tomas 
2152a86c6181SAlex Tomas static int
2153a86c6181SAlex Tomas ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
2154725d26d3SAneesh Kumar K.V 		struct ext4_ext_path *path, ext4_lblk_t start)
2155a86c6181SAlex Tomas {
2156a86c6181SAlex Tomas 	int err = 0, correct_index = 0;
2157a86c6181SAlex Tomas 	int depth = ext_depth(inode), credits;
2158a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
2159725d26d3SAneesh Kumar K.V 	ext4_lblk_t a, b, block;
2160725d26d3SAneesh Kumar K.V 	unsigned num;
2161725d26d3SAneesh Kumar K.V 	ext4_lblk_t ex_ee_block;
2162a86c6181SAlex Tomas 	unsigned short ex_ee_len;
2163a2df2a63SAmit Arora 	unsigned uninitialized = 0;
2164a86c6181SAlex Tomas 	struct ext4_extent *ex;
2165a86c6181SAlex Tomas 
2166c29c0ae7SAlex Tomas 	/* the header must be checked already in ext4_ext_remove_space() */
2167725d26d3SAneesh Kumar K.V 	ext_debug("truncate since %u in leaf\n", start);
2168a86c6181SAlex Tomas 	if (!path[depth].p_hdr)
2169a86c6181SAlex Tomas 		path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2170a86c6181SAlex Tomas 	eh = path[depth].p_hdr;
2171273df556SFrank Mayhar 	if (unlikely(path[depth].p_hdr == NULL)) {
2172273df556SFrank Mayhar 		EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
2173273df556SFrank Mayhar 		return -EIO;
2174273df556SFrank Mayhar 	}
2175a86c6181SAlex Tomas 	/* find where to start removing */
2176a86c6181SAlex Tomas 	ex = EXT_LAST_EXTENT(eh);
2177a86c6181SAlex Tomas 
2178a86c6181SAlex Tomas 	ex_ee_block = le32_to_cpu(ex->ee_block);
2179a2df2a63SAmit Arora 	ex_ee_len = ext4_ext_get_actual_len(ex);
2180a86c6181SAlex Tomas 
2181a86c6181SAlex Tomas 	while (ex >= EXT_FIRST_EXTENT(eh) &&
2182a86c6181SAlex Tomas 			ex_ee_block + ex_ee_len > start) {
2183a41f2071SAneesh Kumar K.V 
2184a41f2071SAneesh Kumar K.V 		if (ext4_ext_is_uninitialized(ex))
2185a41f2071SAneesh Kumar K.V 			uninitialized = 1;
2186a41f2071SAneesh Kumar K.V 		else
2187a41f2071SAneesh Kumar K.V 			uninitialized = 0;
2188a41f2071SAneesh Kumar K.V 
2189553f9008SMingming 		ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
2190553f9008SMingming 			 uninitialized, ex_ee_len);
2191a86c6181SAlex Tomas 		path[depth].p_ext = ex;
2192a86c6181SAlex Tomas 
2193a86c6181SAlex Tomas 		a = ex_ee_block > start ? ex_ee_block : start;
2194a86c6181SAlex Tomas 		b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
2195a86c6181SAlex Tomas 			ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
2196a86c6181SAlex Tomas 
2197a86c6181SAlex Tomas 		ext_debug("  border %u:%u\n", a, b);
2198a86c6181SAlex Tomas 
2199a86c6181SAlex Tomas 		if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
2200a86c6181SAlex Tomas 			block = 0;
2201a86c6181SAlex Tomas 			num = 0;
2202a86c6181SAlex Tomas 			BUG();
2203a86c6181SAlex Tomas 		} else if (a != ex_ee_block) {
2204a86c6181SAlex Tomas 			/* remove tail of the extent */
2205a86c6181SAlex Tomas 			block = ex_ee_block;
2206a86c6181SAlex Tomas 			num = a - block;
2207a86c6181SAlex Tomas 		} else if (b != ex_ee_block + ex_ee_len - 1) {
2208a86c6181SAlex Tomas 			/* remove head of the extent */
2209a86c6181SAlex Tomas 			block = a;
2210a86c6181SAlex Tomas 			num = b - a;
2211a86c6181SAlex Tomas 			/* there is no "make a hole" API yet */
2212a86c6181SAlex Tomas 			BUG();
2213a86c6181SAlex Tomas 		} else {
2214a86c6181SAlex Tomas 			/* remove whole extent: excellent! */
2215a86c6181SAlex Tomas 			block = ex_ee_block;
2216a86c6181SAlex Tomas 			num = 0;
2217a86c6181SAlex Tomas 			BUG_ON(a != ex_ee_block);
2218a86c6181SAlex Tomas 			BUG_ON(b != ex_ee_block + ex_ee_len - 1);
2219a86c6181SAlex Tomas 		}
2220a86c6181SAlex Tomas 
222134071da7STheodore Ts'o 		/*
222234071da7STheodore Ts'o 		 * 3 for leaf, sb, and inode plus 2 (bmap and group
222334071da7STheodore Ts'o 		 * descriptor) for each block group; assume two block
222434071da7STheodore Ts'o 		 * groups plus ex_ee_len/blocks_per_block_group for
222534071da7STheodore Ts'o 		 * the worst case
222634071da7STheodore Ts'o 		 */
222734071da7STheodore Ts'o 		credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
2228a86c6181SAlex Tomas 		if (ex == EXT_FIRST_EXTENT(eh)) {
2229a86c6181SAlex Tomas 			correct_index = 1;
2230a86c6181SAlex Tomas 			credits += (ext_depth(inode)) + 1;
2231a86c6181SAlex Tomas 		}
22325aca07ebSDmitry Monakhov 		credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
2233a86c6181SAlex Tomas 
2234487caeefSJan Kara 		err = ext4_ext_truncate_extend_restart(handle, inode, credits);
22359102e4faSShen Feng 		if (err)
2236a86c6181SAlex Tomas 			goto out;
2237a86c6181SAlex Tomas 
2238a86c6181SAlex Tomas 		err = ext4_ext_get_access(handle, inode, path + depth);
2239a86c6181SAlex Tomas 		if (err)
2240a86c6181SAlex Tomas 			goto out;
2241a86c6181SAlex Tomas 
2242a86c6181SAlex Tomas 		err = ext4_remove_blocks(handle, inode, ex, a, b);
2243a86c6181SAlex Tomas 		if (err)
2244a86c6181SAlex Tomas 			goto out;
2245a86c6181SAlex Tomas 
2246a86c6181SAlex Tomas 		if (num == 0) {
2247d0d856e8SRandy Dunlap 			/* this extent is removed; mark slot entirely unused */
2248f65e6fbaSAlex Tomas 			ext4_ext_store_pblock(ex, 0);
2249e8546d06SMarcin Slusarz 			le16_add_cpu(&eh->eh_entries, -1);
2250a86c6181SAlex Tomas 		}
2251a86c6181SAlex Tomas 
2252a86c6181SAlex Tomas 		ex->ee_block = cpu_to_le32(block);
2253a86c6181SAlex Tomas 		ex->ee_len = cpu_to_le16(num);
2254749269faSAmit Arora 		/*
2255749269faSAmit Arora 		 * Do not mark uninitialized if all the blocks in the
2256749269faSAmit Arora 		 * extent have been removed.
2257749269faSAmit Arora 		 */
2258749269faSAmit Arora 		if (uninitialized && num)
2259a2df2a63SAmit Arora 			ext4_ext_mark_uninitialized(ex);
2260a86c6181SAlex Tomas 
2261a86c6181SAlex Tomas 		err = ext4_ext_dirty(handle, inode, path + depth);
2262a86c6181SAlex Tomas 		if (err)
2263a86c6181SAlex Tomas 			goto out;
2264a86c6181SAlex Tomas 
22652ae02107SMingming Cao 		ext_debug("new extent: %u:%u:%llu\n", block, num,
2266*bf89d16fSTheodore Ts'o 				ext4_ext_pblock(ex));
2267a86c6181SAlex Tomas 		ex--;
2268a86c6181SAlex Tomas 		ex_ee_block = le32_to_cpu(ex->ee_block);
2269a2df2a63SAmit Arora 		ex_ee_len = ext4_ext_get_actual_len(ex);
2270a86c6181SAlex Tomas 	}
2271a86c6181SAlex Tomas 
2272a86c6181SAlex Tomas 	if (correct_index && eh->eh_entries)
2273a86c6181SAlex Tomas 		err = ext4_ext_correct_indexes(handle, inode, path);
2274a86c6181SAlex Tomas 
2275a86c6181SAlex Tomas 	/* if this leaf is free, then we should
2276a86c6181SAlex Tomas 	 * remove it from index block above */
2277a86c6181SAlex Tomas 	if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
2278a86c6181SAlex Tomas 		err = ext4_ext_rm_idx(handle, inode, path + depth);
2279a86c6181SAlex Tomas 
2280a86c6181SAlex Tomas out:
2281a86c6181SAlex Tomas 	return err;
2282a86c6181SAlex Tomas }
2283a86c6181SAlex Tomas 
2284a86c6181SAlex Tomas /*
2285d0d856e8SRandy Dunlap  * ext4_ext_more_to_rm:
2286d0d856e8SRandy Dunlap  * returns 1 if current index has to be freed (even partial)
2287a86c6181SAlex Tomas  */
228809b88252SAvantika Mathur static int
2289a86c6181SAlex Tomas ext4_ext_more_to_rm(struct ext4_ext_path *path)
2290a86c6181SAlex Tomas {
2291a86c6181SAlex Tomas 	BUG_ON(path->p_idx == NULL);
2292a86c6181SAlex Tomas 
2293a86c6181SAlex Tomas 	if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2294a86c6181SAlex Tomas 		return 0;
2295a86c6181SAlex Tomas 
2296a86c6181SAlex Tomas 	/*
2297d0d856e8SRandy Dunlap 	 * if truncate on deeper level happened, it wasn't partial,
2298a86c6181SAlex Tomas 	 * so we have to consider current index for truncation
2299a86c6181SAlex Tomas 	 */
2300a86c6181SAlex Tomas 	if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2301a86c6181SAlex Tomas 		return 0;
2302a86c6181SAlex Tomas 	return 1;
2303a86c6181SAlex Tomas }
2304a86c6181SAlex Tomas 
23051d03ec98SAneesh Kumar K.V static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
2306a86c6181SAlex Tomas {
2307a86c6181SAlex Tomas 	struct super_block *sb = inode->i_sb;
2308a86c6181SAlex Tomas 	int depth = ext_depth(inode);
2309a86c6181SAlex Tomas 	struct ext4_ext_path *path;
2310a86c6181SAlex Tomas 	handle_t *handle;
23110617b83fSDmitry Monakhov 	int i, err;
2312a86c6181SAlex Tomas 
2313725d26d3SAneesh Kumar K.V 	ext_debug("truncate since %u\n", start);
2314a86c6181SAlex Tomas 
2315a86c6181SAlex Tomas 	/* probably first extent we're gonna free will be last in block */
2316a86c6181SAlex Tomas 	handle = ext4_journal_start(inode, depth + 1);
2317a86c6181SAlex Tomas 	if (IS_ERR(handle))
2318a86c6181SAlex Tomas 		return PTR_ERR(handle);
2319a86c6181SAlex Tomas 
23200617b83fSDmitry Monakhov again:
2321a86c6181SAlex Tomas 	ext4_ext_invalidate_cache(inode);
2322a86c6181SAlex Tomas 
2323a86c6181SAlex Tomas 	/*
2324d0d856e8SRandy Dunlap 	 * We start scanning from right side, freeing all the blocks
2325d0d856e8SRandy Dunlap 	 * after i_size and walking into the tree depth-wise.
2326a86c6181SAlex Tomas 	 */
23270617b83fSDmitry Monakhov 	depth = ext_depth(inode);
2328216553c4SJosef Bacik 	path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
2329a86c6181SAlex Tomas 	if (path == NULL) {
2330a86c6181SAlex Tomas 		ext4_journal_stop(handle);
2331a86c6181SAlex Tomas 		return -ENOMEM;
2332a86c6181SAlex Tomas 	}
23330617b83fSDmitry Monakhov 	path[0].p_depth = depth;
2334a86c6181SAlex Tomas 	path[0].p_hdr = ext_inode_hdr(inode);
233556b19868SAneesh Kumar K.V 	if (ext4_ext_check(inode, path[0].p_hdr, depth)) {
2336a86c6181SAlex Tomas 		err = -EIO;
2337a86c6181SAlex Tomas 		goto out;
2338a86c6181SAlex Tomas 	}
23390617b83fSDmitry Monakhov 	i = err = 0;
2340a86c6181SAlex Tomas 
2341a86c6181SAlex Tomas 	while (i >= 0 && err == 0) {
2342a86c6181SAlex Tomas 		if (i == depth) {
2343a86c6181SAlex Tomas 			/* this is leaf block */
2344a86c6181SAlex Tomas 			err = ext4_ext_rm_leaf(handle, inode, path, start);
2345d0d856e8SRandy Dunlap 			/* root level has p_bh == NULL, brelse() eats this */
2346a86c6181SAlex Tomas 			brelse(path[i].p_bh);
2347a86c6181SAlex Tomas 			path[i].p_bh = NULL;
2348a86c6181SAlex Tomas 			i--;
2349a86c6181SAlex Tomas 			continue;
2350a86c6181SAlex Tomas 		}
2351a86c6181SAlex Tomas 
2352a86c6181SAlex Tomas 		/* this is index block */
2353a86c6181SAlex Tomas 		if (!path[i].p_hdr) {
2354a86c6181SAlex Tomas 			ext_debug("initialize header\n");
2355a86c6181SAlex Tomas 			path[i].p_hdr = ext_block_hdr(path[i].p_bh);
2356a86c6181SAlex Tomas 		}
2357a86c6181SAlex Tomas 
2358a86c6181SAlex Tomas 		if (!path[i].p_idx) {
2359d0d856e8SRandy Dunlap 			/* this level hasn't been touched yet */
2360a86c6181SAlex Tomas 			path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2361a86c6181SAlex Tomas 			path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2362a86c6181SAlex Tomas 			ext_debug("init index ptr: hdr 0x%p, num %d\n",
2363a86c6181SAlex Tomas 				  path[i].p_hdr,
2364a86c6181SAlex Tomas 				  le16_to_cpu(path[i].p_hdr->eh_entries));
2365a86c6181SAlex Tomas 		} else {
2366d0d856e8SRandy Dunlap 			/* we were already here, see at next index */
2367a86c6181SAlex Tomas 			path[i].p_idx--;
2368a86c6181SAlex Tomas 		}
2369a86c6181SAlex Tomas 
2370a86c6181SAlex Tomas 		ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2371a86c6181SAlex Tomas 				i, EXT_FIRST_INDEX(path[i].p_hdr),
2372a86c6181SAlex Tomas 				path[i].p_idx);
2373a86c6181SAlex Tomas 		if (ext4_ext_more_to_rm(path + i)) {
2374c29c0ae7SAlex Tomas 			struct buffer_head *bh;
2375a86c6181SAlex Tomas 			/* go to the next level */
23762ae02107SMingming Cao 			ext_debug("move to level %d (block %llu)\n",
2377*bf89d16fSTheodore Ts'o 				  i + 1, ext4_idx_pblock(path[i].p_idx));
2378a86c6181SAlex Tomas 			memset(path + i + 1, 0, sizeof(*path));
2379*bf89d16fSTheodore Ts'o 			bh = sb_bread(sb, ext4_idx_pblock(path[i].p_idx));
2380c29c0ae7SAlex Tomas 			if (!bh) {
2381a86c6181SAlex Tomas 				/* should we reset i_size? */
2382a86c6181SAlex Tomas 				err = -EIO;
2383a86c6181SAlex Tomas 				break;
2384a86c6181SAlex Tomas 			}
2385c29c0ae7SAlex Tomas 			if (WARN_ON(i + 1 > depth)) {
2386c29c0ae7SAlex Tomas 				err = -EIO;
2387c29c0ae7SAlex Tomas 				break;
2388c29c0ae7SAlex Tomas 			}
238956b19868SAneesh Kumar K.V 			if (ext4_ext_check(inode, ext_block_hdr(bh),
2390c29c0ae7SAlex Tomas 							depth - i - 1)) {
2391c29c0ae7SAlex Tomas 				err = -EIO;
2392c29c0ae7SAlex Tomas 				break;
2393c29c0ae7SAlex Tomas 			}
2394c29c0ae7SAlex Tomas 			path[i + 1].p_bh = bh;
2395a86c6181SAlex Tomas 
2396d0d856e8SRandy Dunlap 			/* save actual number of indexes since this
2397d0d856e8SRandy Dunlap 			 * number is changed at the next iteration */
2398a86c6181SAlex Tomas 			path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2399a86c6181SAlex Tomas 			i++;
2400a86c6181SAlex Tomas 		} else {
2401d0d856e8SRandy Dunlap 			/* we finished processing this index, go up */
2402a86c6181SAlex Tomas 			if (path[i].p_hdr->eh_entries == 0 && i > 0) {
2403d0d856e8SRandy Dunlap 				/* index is empty, remove it;
2404a86c6181SAlex Tomas 				 * handle must be already prepared by the
2405a86c6181SAlex Tomas 				 * truncatei_leaf() */
2406a86c6181SAlex Tomas 				err = ext4_ext_rm_idx(handle, inode, path + i);
2407a86c6181SAlex Tomas 			}
2408d0d856e8SRandy Dunlap 			/* root level has p_bh == NULL, brelse() eats this */
2409a86c6181SAlex Tomas 			brelse(path[i].p_bh);
2410a86c6181SAlex Tomas 			path[i].p_bh = NULL;
2411a86c6181SAlex Tomas 			i--;
2412a86c6181SAlex Tomas 			ext_debug("return to level %d\n", i);
2413a86c6181SAlex Tomas 		}
2414a86c6181SAlex Tomas 	}
2415a86c6181SAlex Tomas 
2416a86c6181SAlex Tomas 	/* TODO: flexible tree reduction should be here */
2417a86c6181SAlex Tomas 	if (path->p_hdr->eh_entries == 0) {
2418a86c6181SAlex Tomas 		/*
2419d0d856e8SRandy Dunlap 		 * truncate to zero freed all the tree,
2420d0d856e8SRandy Dunlap 		 * so we need to correct eh_depth
2421a86c6181SAlex Tomas 		 */
2422a86c6181SAlex Tomas 		err = ext4_ext_get_access(handle, inode, path);
2423a86c6181SAlex Tomas 		if (err == 0) {
2424a86c6181SAlex Tomas 			ext_inode_hdr(inode)->eh_depth = 0;
2425a86c6181SAlex Tomas 			ext_inode_hdr(inode)->eh_max =
242655ad63bfSTheodore Ts'o 				cpu_to_le16(ext4_ext_space_root(inode, 0));
2427a86c6181SAlex Tomas 			err = ext4_ext_dirty(handle, inode, path);
2428a86c6181SAlex Tomas 		}
2429a86c6181SAlex Tomas 	}
2430a86c6181SAlex Tomas out:
2431a86c6181SAlex Tomas 	ext4_ext_drop_refs(path);
2432a86c6181SAlex Tomas 	kfree(path);
24330617b83fSDmitry Monakhov 	if (err == -EAGAIN)
24340617b83fSDmitry Monakhov 		goto again;
2435a86c6181SAlex Tomas 	ext4_journal_stop(handle);
2436a86c6181SAlex Tomas 
2437a86c6181SAlex Tomas 	return err;
2438a86c6181SAlex Tomas }
2439a86c6181SAlex Tomas 
2440a86c6181SAlex Tomas /*
2441a86c6181SAlex Tomas  * called at mount time
2442a86c6181SAlex Tomas  */
2443a86c6181SAlex Tomas void ext4_ext_init(struct super_block *sb)
2444a86c6181SAlex Tomas {
2445a86c6181SAlex Tomas 	/*
2446a86c6181SAlex Tomas 	 * possible initialization would be here
2447a86c6181SAlex Tomas 	 */
2448a86c6181SAlex Tomas 
244983982b6fSTheodore Ts'o 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
245090576c0bSTheodore Ts'o #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
24514776004fSTheodore Ts'o 		printk(KERN_INFO "EXT4-fs: file extents enabled");
2452bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST
2453bbf2f9fbSRobert P. J. Day 		printk(", aggressive tests");
2454a86c6181SAlex Tomas #endif
2455a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH
2456a86c6181SAlex Tomas 		printk(", check binsearch");
2457a86c6181SAlex Tomas #endif
2458a86c6181SAlex Tomas #ifdef EXTENTS_STATS
2459a86c6181SAlex Tomas 		printk(", stats");
2460a86c6181SAlex Tomas #endif
2461a86c6181SAlex Tomas 		printk("\n");
246290576c0bSTheodore Ts'o #endif
2463a86c6181SAlex Tomas #ifdef EXTENTS_STATS
2464a86c6181SAlex Tomas 		spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2465a86c6181SAlex Tomas 		EXT4_SB(sb)->s_ext_min = 1 << 30;
2466a86c6181SAlex Tomas 		EXT4_SB(sb)->s_ext_max = 0;
2467a86c6181SAlex Tomas #endif
2468a86c6181SAlex Tomas 	}
2469a86c6181SAlex Tomas }
2470a86c6181SAlex Tomas 
2471a86c6181SAlex Tomas /*
2472a86c6181SAlex Tomas  * called at umount time
2473a86c6181SAlex Tomas  */
2474a86c6181SAlex Tomas void ext4_ext_release(struct super_block *sb)
2475a86c6181SAlex Tomas {
247683982b6fSTheodore Ts'o 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
2477a86c6181SAlex Tomas 		return;
2478a86c6181SAlex Tomas 
2479a86c6181SAlex Tomas #ifdef EXTENTS_STATS
2480a86c6181SAlex Tomas 	if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2481a86c6181SAlex Tomas 		struct ext4_sb_info *sbi = EXT4_SB(sb);
2482a86c6181SAlex Tomas 		printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2483a86c6181SAlex Tomas 			sbi->s_ext_blocks, sbi->s_ext_extents,
2484a86c6181SAlex Tomas 			sbi->s_ext_blocks / sbi->s_ext_extents);
2485a86c6181SAlex Tomas 		printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2486a86c6181SAlex Tomas 			sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2487a86c6181SAlex Tomas 	}
2488a86c6181SAlex Tomas #endif
2489a86c6181SAlex Tomas }
2490a86c6181SAlex Tomas 
2491093a088bSAneesh Kumar K.V /* FIXME!! we need to try to merge to left or right after zero-out  */
2492093a088bSAneesh Kumar K.V static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2493093a088bSAneesh Kumar K.V {
24942407518dSLukas Czerner 	ext4_fsblk_t ee_pblock;
24952407518dSLukas Czerner 	unsigned int ee_len;
2496b720303dSJing Zhang 	int ret;
2497093a088bSAneesh Kumar K.V 
2498093a088bSAneesh Kumar K.V 	ee_len    = ext4_ext_get_actual_len(ex);
2499*bf89d16fSTheodore Ts'o 	ee_pblock = ext4_ext_pblock(ex);
2500093a088bSAneesh Kumar K.V 
25012407518dSLukas Czerner 	ret = sb_issue_zeroout(inode->i_sb, ee_pblock, ee_len,
25022407518dSLukas Czerner 			       GFP_NOFS, BLKDEV_IFL_WAIT);
25032407518dSLukas Czerner 	if (ret > 0)
25042407518dSLukas Czerner 		ret = 0;
2505093a088bSAneesh Kumar K.V 
25062407518dSLukas Czerner 	return ret;
2507093a088bSAneesh Kumar K.V }
2508093a088bSAneesh Kumar K.V 
25093977c965SAneesh Kumar K.V #define EXT4_EXT_ZERO_LEN 7
251056055d3aSAmit Arora /*
2511e35fd660STheodore Ts'o  * This function is called by ext4_ext_map_blocks() if someone tries to write
251256055d3aSAmit Arora  * to an uninitialized extent. It may result in splitting the uninitialized
251356055d3aSAmit Arora  * extent into multiple extents (upto three - one initialized and two
251456055d3aSAmit Arora  * uninitialized).
251556055d3aSAmit Arora  * There are three possibilities:
251656055d3aSAmit Arora  *   a> There is no split required: Entire extent should be initialized
251756055d3aSAmit Arora  *   b> Splits in two extents: Write is happening at either end of the extent
251856055d3aSAmit Arora  *   c> Splits in three extents: Somone is writing in middle of the extent
251956055d3aSAmit Arora  */
2520725d26d3SAneesh Kumar K.V static int ext4_ext_convert_to_initialized(handle_t *handle,
2521725d26d3SAneesh Kumar K.V 					   struct inode *inode,
2522e35fd660STheodore Ts'o 					   struct ext4_map_blocks *map,
2523e35fd660STheodore Ts'o 					   struct ext4_ext_path *path)
252456055d3aSAmit Arora {
252595c3889cSAneesh Kumar K.V 	struct ext4_extent *ex, newex, orig_ex;
252656055d3aSAmit Arora 	struct ext4_extent *ex1 = NULL;
252756055d3aSAmit Arora 	struct ext4_extent *ex2 = NULL;
252856055d3aSAmit Arora 	struct ext4_extent *ex3 = NULL;
252956055d3aSAmit Arora 	struct ext4_extent_header *eh;
253021ca087aSDmitry Monakhov 	ext4_lblk_t ee_block, eof_block;
2531725d26d3SAneesh Kumar K.V 	unsigned int allocated, ee_len, depth;
253256055d3aSAmit Arora 	ext4_fsblk_t newblock;
253356055d3aSAmit Arora 	int err = 0;
253456055d3aSAmit Arora 	int ret = 0;
253521ca087aSDmitry Monakhov 	int may_zeroout;
253621ca087aSDmitry Monakhov 
253721ca087aSDmitry Monakhov 	ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
253821ca087aSDmitry Monakhov 		"block %llu, max_blocks %u\n", inode->i_ino,
2539e35fd660STheodore Ts'o 		(unsigned long long)map->m_lblk, map->m_len);
254021ca087aSDmitry Monakhov 
254121ca087aSDmitry Monakhov 	eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
254221ca087aSDmitry Monakhov 		inode->i_sb->s_blocksize_bits;
2543e35fd660STheodore Ts'o 	if (eof_block < map->m_lblk + map->m_len)
2544e35fd660STheodore Ts'o 		eof_block = map->m_lblk + map->m_len;
254556055d3aSAmit Arora 
254656055d3aSAmit Arora 	depth = ext_depth(inode);
254756055d3aSAmit Arora 	eh = path[depth].p_hdr;
254856055d3aSAmit Arora 	ex = path[depth].p_ext;
254956055d3aSAmit Arora 	ee_block = le32_to_cpu(ex->ee_block);
255056055d3aSAmit Arora 	ee_len = ext4_ext_get_actual_len(ex);
2551e35fd660STheodore Ts'o 	allocated = ee_len - (map->m_lblk - ee_block);
2552*bf89d16fSTheodore Ts'o 	newblock = map->m_lblk - ee_block + ext4_ext_pblock(ex);
255321ca087aSDmitry Monakhov 
255456055d3aSAmit Arora 	ex2 = ex;
255595c3889cSAneesh Kumar K.V 	orig_ex.ee_block = ex->ee_block;
255695c3889cSAneesh Kumar K.V 	orig_ex.ee_len   = cpu_to_le16(ee_len);
2557*bf89d16fSTheodore Ts'o 	ext4_ext_store_pblock(&orig_ex, ext4_ext_pblock(ex));
255856055d3aSAmit Arora 
255921ca087aSDmitry Monakhov 	/*
256021ca087aSDmitry Monakhov 	 * It is safe to convert extent to initialized via explicit
256121ca087aSDmitry Monakhov 	 * zeroout only if extent is fully insde i_size or new_size.
256221ca087aSDmitry Monakhov 	 */
256321ca087aSDmitry Monakhov 	may_zeroout = ee_block + ee_len <= eof_block;
256421ca087aSDmitry Monakhov 
25659df5643aSAneesh Kumar K.V 	err = ext4_ext_get_access(handle, inode, path + depth);
25669df5643aSAneesh Kumar K.V 	if (err)
25679df5643aSAneesh Kumar K.V 		goto out;
25683977c965SAneesh Kumar K.V 	/* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
256921ca087aSDmitry Monakhov 	if (ee_len <= 2*EXT4_EXT_ZERO_LEN && may_zeroout) {
25703977c965SAneesh Kumar K.V 		err =  ext4_ext_zeroout(inode, &orig_ex);
25713977c965SAneesh Kumar K.V 		if (err)
25723977c965SAneesh Kumar K.V 			goto fix_extent_len;
25733977c965SAneesh Kumar K.V 		/* update the extent length and mark as initialized */
25743977c965SAneesh Kumar K.V 		ex->ee_block = orig_ex.ee_block;
25753977c965SAneesh Kumar K.V 		ex->ee_len   = orig_ex.ee_len;
2576*bf89d16fSTheodore Ts'o 		ext4_ext_store_pblock(ex, ext4_ext_pblock(&orig_ex));
25773977c965SAneesh Kumar K.V 		ext4_ext_dirty(handle, inode, path + depth);
2578161e7b7cSAneesh Kumar K.V 		/* zeroed the full extent */
2579161e7b7cSAneesh Kumar K.V 		return allocated;
25803977c965SAneesh Kumar K.V 	}
25819df5643aSAneesh Kumar K.V 
2582e35fd660STheodore Ts'o 	/* ex1: ee_block to map->m_lblk - 1 : uninitialized */
2583e35fd660STheodore Ts'o 	if (map->m_lblk > ee_block) {
258456055d3aSAmit Arora 		ex1 = ex;
2585e35fd660STheodore Ts'o 		ex1->ee_len = cpu_to_le16(map->m_lblk - ee_block);
258656055d3aSAmit Arora 		ext4_ext_mark_uninitialized(ex1);
258756055d3aSAmit Arora 		ex2 = &newex;
258856055d3aSAmit Arora 	}
258956055d3aSAmit Arora 	/*
259056055d3aSAmit Arora 	 * for sanity, update the length of the ex2 extent before
259156055d3aSAmit Arora 	 * we insert ex3, if ex1 is NULL. This is to avoid temporary
259256055d3aSAmit Arora 	 * overlap of blocks.
259356055d3aSAmit Arora 	 */
2594e35fd660STheodore Ts'o 	if (!ex1 && allocated > map->m_len)
2595e35fd660STheodore Ts'o 		ex2->ee_len = cpu_to_le16(map->m_len);
259656055d3aSAmit Arora 	/* ex3: to ee_block + ee_len : uninitialised */
2597e35fd660STheodore Ts'o 	if (allocated > map->m_len) {
259856055d3aSAmit Arora 		unsigned int newdepth;
25993977c965SAneesh Kumar K.V 		/* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */
260021ca087aSDmitry Monakhov 		if (allocated <= EXT4_EXT_ZERO_LEN && may_zeroout) {
2601d03856bdSAneesh Kumar K.V 			/*
2602e35fd660STheodore Ts'o 			 * map->m_lblk == ee_block is handled by the zerouout
2603d03856bdSAneesh Kumar K.V 			 * at the beginning.
2604d03856bdSAneesh Kumar K.V 			 * Mark first half uninitialized.
26053977c965SAneesh Kumar K.V 			 * Mark second half initialized and zero out the
26063977c965SAneesh Kumar K.V 			 * initialized extent
26073977c965SAneesh Kumar K.V 			 */
26083977c965SAneesh Kumar K.V 			ex->ee_block = orig_ex.ee_block;
26093977c965SAneesh Kumar K.V 			ex->ee_len   = cpu_to_le16(ee_len - allocated);
26103977c965SAneesh Kumar K.V 			ext4_ext_mark_uninitialized(ex);
2611*bf89d16fSTheodore Ts'o 			ext4_ext_store_pblock(ex, ext4_ext_pblock(&orig_ex));
26123977c965SAneesh Kumar K.V 			ext4_ext_dirty(handle, inode, path + depth);
26133977c965SAneesh Kumar K.V 
26143977c965SAneesh Kumar K.V 			ex3 = &newex;
2615e35fd660STheodore Ts'o 			ex3->ee_block = cpu_to_le32(map->m_lblk);
26163977c965SAneesh Kumar K.V 			ext4_ext_store_pblock(ex3, newblock);
26173977c965SAneesh Kumar K.V 			ex3->ee_len = cpu_to_le16(allocated);
26180031462bSMingming Cao 			err = ext4_ext_insert_extent(handle, inode, path,
26190031462bSMingming Cao 							ex3, 0);
26203977c965SAneesh Kumar K.V 			if (err == -ENOSPC) {
26213977c965SAneesh Kumar K.V 				err =  ext4_ext_zeroout(inode, &orig_ex);
26223977c965SAneesh Kumar K.V 				if (err)
26233977c965SAneesh Kumar K.V 					goto fix_extent_len;
26243977c965SAneesh Kumar K.V 				ex->ee_block = orig_ex.ee_block;
26253977c965SAneesh Kumar K.V 				ex->ee_len   = orig_ex.ee_len;
2626*bf89d16fSTheodore Ts'o 				ext4_ext_store_pblock(ex,
2627*bf89d16fSTheodore Ts'o 					ext4_ext_pblock(&orig_ex));
26283977c965SAneesh Kumar K.V 				ext4_ext_dirty(handle, inode, path + depth);
2629e35fd660STheodore Ts'o 				/* blocks available from map->m_lblk */
2630161e7b7cSAneesh Kumar K.V 				return allocated;
26313977c965SAneesh Kumar K.V 
26323977c965SAneesh Kumar K.V 			} else if (err)
26333977c965SAneesh Kumar K.V 				goto fix_extent_len;
26343977c965SAneesh Kumar K.V 
2635161e7b7cSAneesh Kumar K.V 			/*
2636161e7b7cSAneesh Kumar K.V 			 * We need to zero out the second half because
2637161e7b7cSAneesh Kumar K.V 			 * an fallocate request can update file size and
2638161e7b7cSAneesh Kumar K.V 			 * converting the second half to initialized extent
2639161e7b7cSAneesh Kumar K.V 			 * implies that we can leak some junk data to user
2640161e7b7cSAneesh Kumar K.V 			 * space.
2641161e7b7cSAneesh Kumar K.V 			 */
2642161e7b7cSAneesh Kumar K.V 			err =  ext4_ext_zeroout(inode, ex3);
2643161e7b7cSAneesh Kumar K.V 			if (err) {
2644161e7b7cSAneesh Kumar K.V 				/*
2645161e7b7cSAneesh Kumar K.V 				 * We should actually mark the
2646161e7b7cSAneesh Kumar K.V 				 * second half as uninit and return error
2647161e7b7cSAneesh Kumar K.V 				 * Insert would have changed the extent
2648161e7b7cSAneesh Kumar K.V 				 */
2649161e7b7cSAneesh Kumar K.V 				depth = ext_depth(inode);
2650161e7b7cSAneesh Kumar K.V 				ext4_ext_drop_refs(path);
2651e35fd660STheodore Ts'o 				path = ext4_ext_find_extent(inode, map->m_lblk,
2652e35fd660STheodore Ts'o 							    path);
2653161e7b7cSAneesh Kumar K.V 				if (IS_ERR(path)) {
2654161e7b7cSAneesh Kumar K.V 					err = PTR_ERR(path);
2655161e7b7cSAneesh Kumar K.V 					return err;
2656161e7b7cSAneesh Kumar K.V 				}
2657d03856bdSAneesh Kumar K.V 				/* get the second half extent details */
2658161e7b7cSAneesh Kumar K.V 				ex = path[depth].p_ext;
2659161e7b7cSAneesh Kumar K.V 				err = ext4_ext_get_access(handle, inode,
2660161e7b7cSAneesh Kumar K.V 								path + depth);
2661161e7b7cSAneesh Kumar K.V 				if (err)
2662161e7b7cSAneesh Kumar K.V 					return err;
2663161e7b7cSAneesh Kumar K.V 				ext4_ext_mark_uninitialized(ex);
2664161e7b7cSAneesh Kumar K.V 				ext4_ext_dirty(handle, inode, path + depth);
2665161e7b7cSAneesh Kumar K.V 				return err;
2666161e7b7cSAneesh Kumar K.V 			}
2667161e7b7cSAneesh Kumar K.V 
2668161e7b7cSAneesh Kumar K.V 			/* zeroed the second half */
26693977c965SAneesh Kumar K.V 			return allocated;
26703977c965SAneesh Kumar K.V 		}
267156055d3aSAmit Arora 		ex3 = &newex;
2672e35fd660STheodore Ts'o 		ex3->ee_block = cpu_to_le32(map->m_lblk + map->m_len);
2673e35fd660STheodore Ts'o 		ext4_ext_store_pblock(ex3, newblock + map->m_len);
2674e35fd660STheodore Ts'o 		ex3->ee_len = cpu_to_le16(allocated - map->m_len);
267556055d3aSAmit Arora 		ext4_ext_mark_uninitialized(ex3);
26760031462bSMingming Cao 		err = ext4_ext_insert_extent(handle, inode, path, ex3, 0);
267721ca087aSDmitry Monakhov 		if (err == -ENOSPC && may_zeroout) {
2678093a088bSAneesh Kumar K.V 			err =  ext4_ext_zeroout(inode, &orig_ex);
2679093a088bSAneesh Kumar K.V 			if (err)
2680093a088bSAneesh Kumar K.V 				goto fix_extent_len;
2681093a088bSAneesh Kumar K.V 			/* update the extent length and mark as initialized */
268295c3889cSAneesh Kumar K.V 			ex->ee_block = orig_ex.ee_block;
268395c3889cSAneesh Kumar K.V 			ex->ee_len   = orig_ex.ee_len;
2684*bf89d16fSTheodore Ts'o 			ext4_ext_store_pblock(ex, ext4_ext_pblock(&orig_ex));
268595c3889cSAneesh Kumar K.V 			ext4_ext_dirty(handle, inode, path + depth);
2686161e7b7cSAneesh Kumar K.V 			/* zeroed the full extent */
2687e35fd660STheodore Ts'o 			/* blocks available from map->m_lblk */
2688161e7b7cSAneesh Kumar K.V 			return allocated;
2689093a088bSAneesh Kumar K.V 
2690093a088bSAneesh Kumar K.V 		} else if (err)
2691093a088bSAneesh Kumar K.V 			goto fix_extent_len;
269256055d3aSAmit Arora 		/*
269356055d3aSAmit Arora 		 * The depth, and hence eh & ex might change
269456055d3aSAmit Arora 		 * as part of the insert above.
269556055d3aSAmit Arora 		 */
269656055d3aSAmit Arora 		newdepth = ext_depth(inode);
269795c3889cSAneesh Kumar K.V 		/*
269873ac36eaSColy Li 		 * update the extent length after successful insert of the
269995c3889cSAneesh Kumar K.V 		 * split extent
270095c3889cSAneesh Kumar K.V 		 */
270121ca087aSDmitry Monakhov 		ee_len -= ext4_ext_get_actual_len(ex3);
270221ca087aSDmitry Monakhov 		orig_ex.ee_len = cpu_to_le16(ee_len);
270321ca087aSDmitry Monakhov 		may_zeroout = ee_block + ee_len <= eof_block;
270421ca087aSDmitry Monakhov 
270556055d3aSAmit Arora 		depth = newdepth;
2706b35905c1SAneesh Kumar K.V 		ext4_ext_drop_refs(path);
2707e35fd660STheodore Ts'o 		path = ext4_ext_find_extent(inode, map->m_lblk, path);
270856055d3aSAmit Arora 		if (IS_ERR(path)) {
270956055d3aSAmit Arora 			err = PTR_ERR(path);
271056055d3aSAmit Arora 			goto out;
271156055d3aSAmit Arora 		}
271256055d3aSAmit Arora 		eh = path[depth].p_hdr;
271356055d3aSAmit Arora 		ex = path[depth].p_ext;
271456055d3aSAmit Arora 		if (ex2 != &newex)
271556055d3aSAmit Arora 			ex2 = ex;
27169df5643aSAneesh Kumar K.V 
27179df5643aSAneesh Kumar K.V 		err = ext4_ext_get_access(handle, inode, path + depth);
27189df5643aSAneesh Kumar K.V 		if (err)
27199df5643aSAneesh Kumar K.V 			goto out;
2720d03856bdSAneesh Kumar K.V 
2721e35fd660STheodore Ts'o 		allocated = map->m_len;
27223977c965SAneesh Kumar K.V 
27233977c965SAneesh Kumar K.V 		/* If extent has less than EXT4_EXT_ZERO_LEN and we are trying
27243977c965SAneesh Kumar K.V 		 * to insert a extent in the middle zerout directly
27253977c965SAneesh Kumar K.V 		 * otherwise give the extent a chance to merge to left
27263977c965SAneesh Kumar K.V 		 */
27273977c965SAneesh Kumar K.V 		if (le16_to_cpu(orig_ex.ee_len) <= EXT4_EXT_ZERO_LEN &&
2728e35fd660STheodore Ts'o 			map->m_lblk != ee_block && may_zeroout) {
27293977c965SAneesh Kumar K.V 			err =  ext4_ext_zeroout(inode, &orig_ex);
27303977c965SAneesh Kumar K.V 			if (err)
27313977c965SAneesh Kumar K.V 				goto fix_extent_len;
27323977c965SAneesh Kumar K.V 			/* update the extent length and mark as initialized */
27333977c965SAneesh Kumar K.V 			ex->ee_block = orig_ex.ee_block;
27343977c965SAneesh Kumar K.V 			ex->ee_len   = orig_ex.ee_len;
2735*bf89d16fSTheodore Ts'o 			ext4_ext_store_pblock(ex, ext4_ext_pblock(&orig_ex));
27363977c965SAneesh Kumar K.V 			ext4_ext_dirty(handle, inode, path + depth);
2737161e7b7cSAneesh Kumar K.V 			/* zero out the first half */
2738e35fd660STheodore Ts'o 			/* blocks available from map->m_lblk */
2739161e7b7cSAneesh Kumar K.V 			return allocated;
27403977c965SAneesh Kumar K.V 		}
274156055d3aSAmit Arora 	}
274256055d3aSAmit Arora 	/*
274356055d3aSAmit Arora 	 * If there was a change of depth as part of the
274456055d3aSAmit Arora 	 * insertion of ex3 above, we need to update the length
274556055d3aSAmit Arora 	 * of the ex1 extent again here
274656055d3aSAmit Arora 	 */
274756055d3aSAmit Arora 	if (ex1 && ex1 != ex) {
274856055d3aSAmit Arora 		ex1 = ex;
2749e35fd660STheodore Ts'o 		ex1->ee_len = cpu_to_le16(map->m_lblk - ee_block);
275056055d3aSAmit Arora 		ext4_ext_mark_uninitialized(ex1);
275156055d3aSAmit Arora 		ex2 = &newex;
275256055d3aSAmit Arora 	}
2753e35fd660STheodore Ts'o 	/* ex2: map->m_lblk to map->m_lblk + maxblocks-1 : initialised */
2754e35fd660STheodore Ts'o 	ex2->ee_block = cpu_to_le32(map->m_lblk);
275556055d3aSAmit Arora 	ext4_ext_store_pblock(ex2, newblock);
275656055d3aSAmit Arora 	ex2->ee_len = cpu_to_le16(allocated);
275756055d3aSAmit Arora 	if (ex2 != ex)
275856055d3aSAmit Arora 		goto insert;
275956055d3aSAmit Arora 	/*
276056055d3aSAmit Arora 	 * New (initialized) extent starts from the first block
276156055d3aSAmit Arora 	 * in the current extent. i.e., ex2 == ex
276256055d3aSAmit Arora 	 * We have to see if it can be merged with the extent
276356055d3aSAmit Arora 	 * on the left.
276456055d3aSAmit Arora 	 */
276556055d3aSAmit Arora 	if (ex2 > EXT_FIRST_EXTENT(eh)) {
276656055d3aSAmit Arora 		/*
276756055d3aSAmit Arora 		 * To merge left, pass "ex2 - 1" to try_to_merge(),
276856055d3aSAmit Arora 		 * since it merges towards right _only_.
276956055d3aSAmit Arora 		 */
277056055d3aSAmit Arora 		ret = ext4_ext_try_to_merge(inode, path, ex2 - 1);
277156055d3aSAmit Arora 		if (ret) {
277256055d3aSAmit Arora 			err = ext4_ext_correct_indexes(handle, inode, path);
277356055d3aSAmit Arora 			if (err)
277456055d3aSAmit Arora 				goto out;
277556055d3aSAmit Arora 			depth = ext_depth(inode);
277656055d3aSAmit Arora 			ex2--;
277756055d3aSAmit Arora 		}
277856055d3aSAmit Arora 	}
277956055d3aSAmit Arora 	/*
278056055d3aSAmit Arora 	 * Try to Merge towards right. This might be required
278156055d3aSAmit Arora 	 * only when the whole extent is being written to.
278256055d3aSAmit Arora 	 * i.e. ex2 == ex and ex3 == NULL.
278356055d3aSAmit Arora 	 */
278456055d3aSAmit Arora 	if (!ex3) {
278556055d3aSAmit Arora 		ret = ext4_ext_try_to_merge(inode, path, ex2);
278656055d3aSAmit Arora 		if (ret) {
278756055d3aSAmit Arora 			err = ext4_ext_correct_indexes(handle, inode, path);
278856055d3aSAmit Arora 			if (err)
278956055d3aSAmit Arora 				goto out;
279056055d3aSAmit Arora 		}
279156055d3aSAmit Arora 	}
279256055d3aSAmit Arora 	/* Mark modified extent as dirty */
279356055d3aSAmit Arora 	err = ext4_ext_dirty(handle, inode, path + depth);
279456055d3aSAmit Arora 	goto out;
279556055d3aSAmit Arora insert:
27960031462bSMingming Cao 	err = ext4_ext_insert_extent(handle, inode, path, &newex, 0);
279721ca087aSDmitry Monakhov 	if (err == -ENOSPC && may_zeroout) {
2798093a088bSAneesh Kumar K.V 		err =  ext4_ext_zeroout(inode, &orig_ex);
2799093a088bSAneesh Kumar K.V 		if (err)
2800093a088bSAneesh Kumar K.V 			goto fix_extent_len;
2801093a088bSAneesh Kumar K.V 		/* update the extent length and mark as initialized */
2802093a088bSAneesh Kumar K.V 		ex->ee_block = orig_ex.ee_block;
2803093a088bSAneesh Kumar K.V 		ex->ee_len   = orig_ex.ee_len;
2804*bf89d16fSTheodore Ts'o 		ext4_ext_store_pblock(ex, ext4_ext_pblock(&orig_ex));
2805093a088bSAneesh Kumar K.V 		ext4_ext_dirty(handle, inode, path + depth);
2806161e7b7cSAneesh Kumar K.V 		/* zero out the first half */
2807161e7b7cSAneesh Kumar K.V 		return allocated;
2808093a088bSAneesh Kumar K.V 	} else if (err)
2809093a088bSAneesh Kumar K.V 		goto fix_extent_len;
2810093a088bSAneesh Kumar K.V out:
2811553f9008SMingming 	ext4_ext_show_leaf(inode, path);
2812093a088bSAneesh Kumar K.V 	return err ? err : allocated;
2813093a088bSAneesh Kumar K.V 
2814093a088bSAneesh Kumar K.V fix_extent_len:
281595c3889cSAneesh Kumar K.V 	ex->ee_block = orig_ex.ee_block;
281695c3889cSAneesh Kumar K.V 	ex->ee_len   = orig_ex.ee_len;
2817*bf89d16fSTheodore Ts'o 	ext4_ext_store_pblock(ex, ext4_ext_pblock(&orig_ex));
281895c3889cSAneesh Kumar K.V 	ext4_ext_mark_uninitialized(ex);
281995c3889cSAneesh Kumar K.V 	ext4_ext_dirty(handle, inode, path + depth);
2820093a088bSAneesh Kumar K.V 	return err;
282156055d3aSAmit Arora }
282256055d3aSAmit Arora 
2823c278bfecSAneesh Kumar K.V /*
2824e35fd660STheodore Ts'o  * This function is called by ext4_ext_map_blocks() from
28250031462bSMingming Cao  * ext4_get_blocks_dio_write() when DIO to write
28260031462bSMingming Cao  * to an uninitialized extent.
28270031462bSMingming Cao  *
28280031462bSMingming Cao  * Writing to an uninitized extent may result in splitting the uninitialized
28290031462bSMingming Cao  * extent into multiple /intialized unintialized extents (up to three)
28300031462bSMingming Cao  * There are three possibilities:
28310031462bSMingming Cao  *   a> There is no split required: Entire extent should be uninitialized
28320031462bSMingming Cao  *   b> Splits in two extents: Write is happening at either end of the extent
28330031462bSMingming Cao  *   c> Splits in three extents: Somone is writing in middle of the extent
28340031462bSMingming Cao  *
28350031462bSMingming Cao  * One of more index blocks maybe needed if the extent tree grow after
28360031462bSMingming Cao  * the unintialized extent split. To prevent ENOSPC occur at the IO
28370031462bSMingming Cao  * complete, we need to split the uninitialized extent before DIO submit
2838421f91d2SUwe Kleine-König  * the IO. The uninitialized extent called at this time will be split
28390031462bSMingming Cao  * into three uninitialized extent(at most). After IO complete, the part
28400031462bSMingming Cao  * being filled will be convert to initialized by the end_io callback function
28410031462bSMingming Cao  * via ext4_convert_unwritten_extents().
2842ba230c3fSMingming  *
2843ba230c3fSMingming  * Returns the size of uninitialized extent to be written on success.
28440031462bSMingming Cao  */
28450031462bSMingming Cao static int ext4_split_unwritten_extents(handle_t *handle,
28460031462bSMingming Cao 					struct inode *inode,
2847e35fd660STheodore Ts'o 					struct ext4_map_blocks *map,
28480031462bSMingming Cao 					struct ext4_ext_path *path,
28490031462bSMingming Cao 					int flags)
28500031462bSMingming Cao {
28510031462bSMingming Cao 	struct ext4_extent *ex, newex, orig_ex;
28520031462bSMingming Cao 	struct ext4_extent *ex1 = NULL;
28530031462bSMingming Cao 	struct ext4_extent *ex2 = NULL;
28540031462bSMingming Cao 	struct ext4_extent *ex3 = NULL;
285521ca087aSDmitry Monakhov 	ext4_lblk_t ee_block, eof_block;
28560031462bSMingming Cao 	unsigned int allocated, ee_len, depth;
28570031462bSMingming Cao 	ext4_fsblk_t newblock;
28580031462bSMingming Cao 	int err = 0;
285921ca087aSDmitry Monakhov 	int may_zeroout;
28600031462bSMingming Cao 
286121ca087aSDmitry Monakhov 	ext_debug("ext4_split_unwritten_extents: inode %lu, logical"
286221ca087aSDmitry Monakhov 		"block %llu, max_blocks %u\n", inode->i_ino,
2863e35fd660STheodore Ts'o 		(unsigned long long)map->m_lblk, map->m_len);
286421ca087aSDmitry Monakhov 
286521ca087aSDmitry Monakhov 	eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
286621ca087aSDmitry Monakhov 		inode->i_sb->s_blocksize_bits;
2867e35fd660STheodore Ts'o 	if (eof_block < map->m_lblk + map->m_len)
2868e35fd660STheodore Ts'o 		eof_block = map->m_lblk + map->m_len;
286921ca087aSDmitry Monakhov 
28700031462bSMingming Cao 	depth = ext_depth(inode);
28710031462bSMingming Cao 	ex = path[depth].p_ext;
28720031462bSMingming Cao 	ee_block = le32_to_cpu(ex->ee_block);
28730031462bSMingming Cao 	ee_len = ext4_ext_get_actual_len(ex);
2874e35fd660STheodore Ts'o 	allocated = ee_len - (map->m_lblk - ee_block);
2875*bf89d16fSTheodore Ts'o 	newblock = map->m_lblk - ee_block + ext4_ext_pblock(ex);
287621ca087aSDmitry Monakhov 
28770031462bSMingming Cao 	ex2 = ex;
28780031462bSMingming Cao 	orig_ex.ee_block = ex->ee_block;
28790031462bSMingming Cao 	orig_ex.ee_len   = cpu_to_le16(ee_len);
2880*bf89d16fSTheodore Ts'o 	ext4_ext_store_pblock(&orig_ex, ext4_ext_pblock(ex));
28810031462bSMingming Cao 
28820031462bSMingming Cao 	/*
288321ca087aSDmitry Monakhov 	 * It is safe to convert extent to initialized via explicit
288421ca087aSDmitry Monakhov 	 * zeroout only if extent is fully insde i_size or new_size.
288521ca087aSDmitry Monakhov 	 */
288621ca087aSDmitry Monakhov 	may_zeroout = ee_block + ee_len <= eof_block;
288721ca087aSDmitry Monakhov 
288821ca087aSDmitry Monakhov 	/*
2889ba230c3fSMingming  	 * If the uninitialized extent begins at the same logical
2890ba230c3fSMingming  	 * block where the write begins, and the write completely
2891ba230c3fSMingming  	 * covers the extent, then we don't need to split it.
28920031462bSMingming Cao  	 */
2893e35fd660STheodore Ts'o 	if ((map->m_lblk == ee_block) && (allocated <= map->m_len))
2894ba230c3fSMingming 		return allocated;
28950031462bSMingming Cao 
28960031462bSMingming Cao 	err = ext4_ext_get_access(handle, inode, path + depth);
28970031462bSMingming Cao 	if (err)
28980031462bSMingming Cao 		goto out;
2899e35fd660STheodore Ts'o 	/* ex1: ee_block to map->m_lblk - 1 : uninitialized */
2900e35fd660STheodore Ts'o 	if (map->m_lblk > ee_block) {
29010031462bSMingming Cao 		ex1 = ex;
2902e35fd660STheodore Ts'o 		ex1->ee_len = cpu_to_le16(map->m_lblk - ee_block);
29030031462bSMingming Cao 		ext4_ext_mark_uninitialized(ex1);
29040031462bSMingming Cao 		ex2 = &newex;
29050031462bSMingming Cao 	}
29060031462bSMingming Cao 	/*
29070031462bSMingming Cao 	 * for sanity, update the length of the ex2 extent before
29080031462bSMingming Cao 	 * we insert ex3, if ex1 is NULL. This is to avoid temporary
29090031462bSMingming Cao 	 * overlap of blocks.
29100031462bSMingming Cao 	 */
2911e35fd660STheodore Ts'o 	if (!ex1 && allocated > map->m_len)
2912e35fd660STheodore Ts'o 		ex2->ee_len = cpu_to_le16(map->m_len);
29130031462bSMingming Cao 	/* ex3: to ee_block + ee_len : uninitialised */
2914e35fd660STheodore Ts'o 	if (allocated > map->m_len) {
29150031462bSMingming Cao 		unsigned int newdepth;
29160031462bSMingming Cao 		ex3 = &newex;
2917e35fd660STheodore Ts'o 		ex3->ee_block = cpu_to_le32(map->m_lblk + map->m_len);
2918e35fd660STheodore Ts'o 		ext4_ext_store_pblock(ex3, newblock + map->m_len);
2919e35fd660STheodore Ts'o 		ex3->ee_len = cpu_to_le16(allocated - map->m_len);
29200031462bSMingming Cao 		ext4_ext_mark_uninitialized(ex3);
29210031462bSMingming Cao 		err = ext4_ext_insert_extent(handle, inode, path, ex3, flags);
292221ca087aSDmitry Monakhov 		if (err == -ENOSPC && may_zeroout) {
29230031462bSMingming Cao 			err =  ext4_ext_zeroout(inode, &orig_ex);
29240031462bSMingming Cao 			if (err)
29250031462bSMingming Cao 				goto fix_extent_len;
29260031462bSMingming Cao 			/* update the extent length and mark as initialized */
29270031462bSMingming Cao 			ex->ee_block = orig_ex.ee_block;
29280031462bSMingming Cao 			ex->ee_len   = orig_ex.ee_len;
2929*bf89d16fSTheodore Ts'o 			ext4_ext_store_pblock(ex, ext4_ext_pblock(&orig_ex));
29300031462bSMingming Cao 			ext4_ext_dirty(handle, inode, path + depth);
29310031462bSMingming Cao 			/* zeroed the full extent */
2932e35fd660STheodore Ts'o 			/* blocks available from map->m_lblk */
29330031462bSMingming Cao 			return allocated;
29340031462bSMingming Cao 
29350031462bSMingming Cao 		} else if (err)
29360031462bSMingming Cao 			goto fix_extent_len;
29370031462bSMingming Cao 		/*
29380031462bSMingming Cao 		 * The depth, and hence eh & ex might change
29390031462bSMingming Cao 		 * as part of the insert above.
29400031462bSMingming Cao 		 */
29410031462bSMingming Cao 		newdepth = ext_depth(inode);
29420031462bSMingming Cao 		/*
29430031462bSMingming Cao 		 * update the extent length after successful insert of the
29440031462bSMingming Cao 		 * split extent
29450031462bSMingming Cao 		 */
294621ca087aSDmitry Monakhov 		ee_len -= ext4_ext_get_actual_len(ex3);
294721ca087aSDmitry Monakhov 		orig_ex.ee_len = cpu_to_le16(ee_len);
294821ca087aSDmitry Monakhov 		may_zeroout = ee_block + ee_len <= eof_block;
294921ca087aSDmitry Monakhov 
29500031462bSMingming Cao 		depth = newdepth;
29510031462bSMingming Cao 		ext4_ext_drop_refs(path);
2952e35fd660STheodore Ts'o 		path = ext4_ext_find_extent(inode, map->m_lblk, path);
29530031462bSMingming Cao 		if (IS_ERR(path)) {
29540031462bSMingming Cao 			err = PTR_ERR(path);
29550031462bSMingming Cao 			goto out;
29560031462bSMingming Cao 		}
29570031462bSMingming Cao 		ex = path[depth].p_ext;
29580031462bSMingming Cao 		if (ex2 != &newex)
29590031462bSMingming Cao 			ex2 = ex;
29600031462bSMingming Cao 
29610031462bSMingming Cao 		err = ext4_ext_get_access(handle, inode, path + depth);
29620031462bSMingming Cao 		if (err)
29630031462bSMingming Cao 			goto out;
29640031462bSMingming Cao 
2965e35fd660STheodore Ts'o 		allocated = map->m_len;
29660031462bSMingming Cao 	}
29670031462bSMingming Cao 	/*
29680031462bSMingming Cao 	 * If there was a change of depth as part of the
29690031462bSMingming Cao 	 * insertion of ex3 above, we need to update the length
29700031462bSMingming Cao 	 * of the ex1 extent again here
29710031462bSMingming Cao 	 */
29720031462bSMingming Cao 	if (ex1 && ex1 != ex) {
29730031462bSMingming Cao 		ex1 = ex;
2974e35fd660STheodore Ts'o 		ex1->ee_len = cpu_to_le16(map->m_lblk - ee_block);
29750031462bSMingming Cao 		ext4_ext_mark_uninitialized(ex1);
29760031462bSMingming Cao 		ex2 = &newex;
29770031462bSMingming Cao 	}
29780031462bSMingming Cao 	/*
2979e35fd660STheodore Ts'o 	 * ex2: map->m_lblk to map->m_lblk + map->m_len-1 : to be written
2980e35fd660STheodore Ts'o 	 * using direct I/O, uninitialised still.
29810031462bSMingming Cao 	 */
2982e35fd660STheodore Ts'o 	ex2->ee_block = cpu_to_le32(map->m_lblk);
29830031462bSMingming Cao 	ext4_ext_store_pblock(ex2, newblock);
29840031462bSMingming Cao 	ex2->ee_len = cpu_to_le16(allocated);
29850031462bSMingming Cao 	ext4_ext_mark_uninitialized(ex2);
29860031462bSMingming Cao 	if (ex2 != ex)
29870031462bSMingming Cao 		goto insert;
29880031462bSMingming Cao 	/* Mark modified extent as dirty */
29890031462bSMingming Cao 	err = ext4_ext_dirty(handle, inode, path + depth);
29900031462bSMingming Cao 	ext_debug("out here\n");
29910031462bSMingming Cao 	goto out;
29920031462bSMingming Cao insert:
29930031462bSMingming Cao 	err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
299421ca087aSDmitry Monakhov 	if (err == -ENOSPC && may_zeroout) {
29950031462bSMingming Cao 		err =  ext4_ext_zeroout(inode, &orig_ex);
29960031462bSMingming Cao 		if (err)
29970031462bSMingming Cao 			goto fix_extent_len;
29980031462bSMingming Cao 		/* update the extent length and mark as initialized */
29990031462bSMingming Cao 		ex->ee_block = orig_ex.ee_block;
30000031462bSMingming Cao 		ex->ee_len   = orig_ex.ee_len;
3001*bf89d16fSTheodore Ts'o 		ext4_ext_store_pblock(ex, ext4_ext_pblock(&orig_ex));
30020031462bSMingming Cao 		ext4_ext_dirty(handle, inode, path + depth);
30030031462bSMingming Cao 		/* zero out the first half */
30040031462bSMingming Cao 		return allocated;
30050031462bSMingming Cao 	} else if (err)
30060031462bSMingming Cao 		goto fix_extent_len;
30070031462bSMingming Cao out:
30080031462bSMingming Cao 	ext4_ext_show_leaf(inode, path);
30090031462bSMingming Cao 	return err ? err : allocated;
30100031462bSMingming Cao 
30110031462bSMingming Cao fix_extent_len:
30120031462bSMingming Cao 	ex->ee_block = orig_ex.ee_block;
30130031462bSMingming Cao 	ex->ee_len   = orig_ex.ee_len;
3014*bf89d16fSTheodore Ts'o 	ext4_ext_store_pblock(ex, ext4_ext_pblock(&orig_ex));
30150031462bSMingming Cao 	ext4_ext_mark_uninitialized(ex);
30160031462bSMingming Cao 	ext4_ext_dirty(handle, inode, path + depth);
30170031462bSMingming Cao 	return err;
30180031462bSMingming Cao }
3019c7064ef1SJiaying Zhang static int ext4_convert_unwritten_extents_endio(handle_t *handle,
30200031462bSMingming Cao 					      struct inode *inode,
30210031462bSMingming Cao 					      struct ext4_ext_path *path)
30220031462bSMingming Cao {
30230031462bSMingming Cao 	struct ext4_extent *ex;
30240031462bSMingming Cao 	struct ext4_extent_header *eh;
30250031462bSMingming Cao 	int depth;
30260031462bSMingming Cao 	int err = 0;
30270031462bSMingming Cao 	int ret = 0;
30280031462bSMingming Cao 
30290031462bSMingming Cao 	depth = ext_depth(inode);
30300031462bSMingming Cao 	eh = path[depth].p_hdr;
30310031462bSMingming Cao 	ex = path[depth].p_ext;
30320031462bSMingming Cao 
30330031462bSMingming Cao 	err = ext4_ext_get_access(handle, inode, path + depth);
30340031462bSMingming Cao 	if (err)
30350031462bSMingming Cao 		goto out;
30360031462bSMingming Cao 	/* first mark the extent as initialized */
30370031462bSMingming Cao 	ext4_ext_mark_initialized(ex);
30380031462bSMingming Cao 
30390031462bSMingming Cao 	/*
30400031462bSMingming Cao 	 * We have to see if it can be merged with the extent
30410031462bSMingming Cao 	 * on the left.
30420031462bSMingming Cao 	 */
30430031462bSMingming Cao 	if (ex > EXT_FIRST_EXTENT(eh)) {
30440031462bSMingming Cao 		/*
30450031462bSMingming Cao 		 * To merge left, pass "ex - 1" to try_to_merge(),
30460031462bSMingming Cao 		 * since it merges towards right _only_.
30470031462bSMingming Cao 		 */
30480031462bSMingming Cao 		ret = ext4_ext_try_to_merge(inode, path, ex - 1);
30490031462bSMingming Cao 		if (ret) {
30500031462bSMingming Cao 			err = ext4_ext_correct_indexes(handle, inode, path);
30510031462bSMingming Cao 			if (err)
30520031462bSMingming Cao 				goto out;
30530031462bSMingming Cao 			depth = ext_depth(inode);
30540031462bSMingming Cao 			ex--;
30550031462bSMingming Cao 		}
30560031462bSMingming Cao 	}
30570031462bSMingming Cao 	/*
30580031462bSMingming Cao 	 * Try to Merge towards right.
30590031462bSMingming Cao 	 */
30600031462bSMingming Cao 	ret = ext4_ext_try_to_merge(inode, path, ex);
30610031462bSMingming Cao 	if (ret) {
30620031462bSMingming Cao 		err = ext4_ext_correct_indexes(handle, inode, path);
30630031462bSMingming Cao 		if (err)
30640031462bSMingming Cao 			goto out;
30650031462bSMingming Cao 		depth = ext_depth(inode);
30660031462bSMingming Cao 	}
30670031462bSMingming Cao 	/* Mark modified extent as dirty */
30680031462bSMingming Cao 	err = ext4_ext_dirty(handle, inode, path + depth);
30690031462bSMingming Cao out:
30700031462bSMingming Cao 	ext4_ext_show_leaf(inode, path);
30710031462bSMingming Cao 	return err;
30720031462bSMingming Cao }
30730031462bSMingming Cao 
3074515f41c3SAneesh Kumar K.V static void unmap_underlying_metadata_blocks(struct block_device *bdev,
3075515f41c3SAneesh Kumar K.V 			sector_t block, int count)
3076515f41c3SAneesh Kumar K.V {
3077515f41c3SAneesh Kumar K.V 	int i;
3078515f41c3SAneesh Kumar K.V 	for (i = 0; i < count; i++)
3079515f41c3SAneesh Kumar K.V                 unmap_underlying_metadata(bdev, block + i);
3080515f41c3SAneesh Kumar K.V }
3081515f41c3SAneesh Kumar K.V 
308258590b06STheodore Ts'o /*
308358590b06STheodore Ts'o  * Handle EOFBLOCKS_FL flag, clearing it if necessary
308458590b06STheodore Ts'o  */
308558590b06STheodore Ts'o static int check_eofblocks_fl(handle_t *handle, struct inode *inode,
308658590b06STheodore Ts'o 			      struct ext4_map_blocks *map,
308758590b06STheodore Ts'o 			      struct ext4_ext_path *path,
308858590b06STheodore Ts'o 			      unsigned int len)
308958590b06STheodore Ts'o {
309058590b06STheodore Ts'o 	int i, depth;
309158590b06STheodore Ts'o 	struct ext4_extent_header *eh;
309258590b06STheodore Ts'o 	struct ext4_extent *ex, *last_ex;
309358590b06STheodore Ts'o 
309458590b06STheodore Ts'o 	if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
309558590b06STheodore Ts'o 		return 0;
309658590b06STheodore Ts'o 
309758590b06STheodore Ts'o 	depth = ext_depth(inode);
309858590b06STheodore Ts'o 	eh = path[depth].p_hdr;
309958590b06STheodore Ts'o 	ex = path[depth].p_ext;
310058590b06STheodore Ts'o 
310158590b06STheodore Ts'o 	if (unlikely(!eh->eh_entries)) {
310258590b06STheodore Ts'o 		EXT4_ERROR_INODE(inode, "eh->eh_entries == 0 and "
310358590b06STheodore Ts'o 				 "EOFBLOCKS_FL set");
310458590b06STheodore Ts'o 		return -EIO;
310558590b06STheodore Ts'o 	}
310658590b06STheodore Ts'o 	last_ex = EXT_LAST_EXTENT(eh);
310758590b06STheodore Ts'o 	/*
310858590b06STheodore Ts'o 	 * We should clear the EOFBLOCKS_FL flag if we are writing the
310958590b06STheodore Ts'o 	 * last block in the last extent in the file.  We test this by
311058590b06STheodore Ts'o 	 * first checking to see if the caller to
311158590b06STheodore Ts'o 	 * ext4_ext_get_blocks() was interested in the last block (or
311258590b06STheodore Ts'o 	 * a block beyond the last block) in the current extent.  If
311358590b06STheodore Ts'o 	 * this turns out to be false, we can bail out from this
311458590b06STheodore Ts'o 	 * function immediately.
311558590b06STheodore Ts'o 	 */
311658590b06STheodore Ts'o 	if (map->m_lblk + len < le32_to_cpu(last_ex->ee_block) +
311758590b06STheodore Ts'o 	    ext4_ext_get_actual_len(last_ex))
311858590b06STheodore Ts'o 		return 0;
311958590b06STheodore Ts'o 	/*
312058590b06STheodore Ts'o 	 * If the caller does appear to be planning to write at or
312158590b06STheodore Ts'o 	 * beyond the end of the current extent, we then test to see
312258590b06STheodore Ts'o 	 * if the current extent is the last extent in the file, by
312358590b06STheodore Ts'o 	 * checking to make sure it was reached via the rightmost node
312458590b06STheodore Ts'o 	 * at each level of the tree.
312558590b06STheodore Ts'o 	 */
312658590b06STheodore Ts'o 	for (i = depth-1; i >= 0; i--)
312758590b06STheodore Ts'o 		if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr))
312858590b06STheodore Ts'o 			return 0;
312958590b06STheodore Ts'o 	ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
313058590b06STheodore Ts'o 	return ext4_mark_inode_dirty(handle, inode);
313158590b06STheodore Ts'o }
313258590b06STheodore Ts'o 
31330031462bSMingming Cao static int
31340031462bSMingming Cao ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode,
3135e35fd660STheodore Ts'o 			struct ext4_map_blocks *map,
31360031462bSMingming Cao 			struct ext4_ext_path *path, int flags,
3137e35fd660STheodore Ts'o 			unsigned int allocated, ext4_fsblk_t newblock)
31380031462bSMingming Cao {
31390031462bSMingming Cao 	int ret = 0;
31400031462bSMingming Cao 	int err = 0;
31418d5d02e6SMingming Cao 	ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
31420031462bSMingming Cao 
31430031462bSMingming Cao 	ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical"
31440031462bSMingming Cao 		  "block %llu, max_blocks %u, flags %d, allocated %u",
3145e35fd660STheodore Ts'o 		  inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
31460031462bSMingming Cao 		  flags, allocated);
31470031462bSMingming Cao 	ext4_ext_show_leaf(inode, path);
31480031462bSMingming Cao 
3149c7064ef1SJiaying Zhang 	/* get_block() before submit the IO, split the extent */
3150744692dcSJiaying Zhang 	if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
3151e35fd660STheodore Ts'o 		ret = ext4_split_unwritten_extents(handle, inode, map,
3152e35fd660STheodore Ts'o 						   path, flags);
31535f524950SMingming 		/*
31545f524950SMingming 		 * Flag the inode(non aio case) or end_io struct (aio case)
31555f524950SMingming 		 * that this IO needs to convertion to written when IO is
31565f524950SMingming 		 * completed
31575f524950SMingming 		 */
31588d5d02e6SMingming Cao 		if (io)
3159bd2d0210STheodore Ts'o 			io->flag = EXT4_IO_END_UNWRITTEN;
31605f524950SMingming 		else
316119f5fb7aSTheodore Ts'o 			ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
3162744692dcSJiaying Zhang 		if (ext4_should_dioread_nolock(inode))
3163e35fd660STheodore Ts'o 			map->m_flags |= EXT4_MAP_UNINIT;
31640031462bSMingming Cao 		goto out;
31650031462bSMingming Cao 	}
3166c7064ef1SJiaying Zhang 	/* IO end_io complete, convert the filled extent to written */
3167744692dcSJiaying Zhang 	if ((flags & EXT4_GET_BLOCKS_CONVERT)) {
3168c7064ef1SJiaying Zhang 		ret = ext4_convert_unwritten_extents_endio(handle, inode,
31690031462bSMingming Cao 							path);
317058590b06STheodore Ts'o 		if (ret >= 0) {
3171b436b9beSJan Kara 			ext4_update_inode_fsync_trans(handle, inode, 1);
317258590b06STheodore Ts'o 			err = check_eofblocks_fl(handle, inode, map, path,
317358590b06STheodore Ts'o 						 map->m_len);
317458590b06STheodore Ts'o 		} else
317558590b06STheodore Ts'o 			err = ret;
31760031462bSMingming Cao 		goto out2;
31770031462bSMingming Cao 	}
31780031462bSMingming Cao 	/* buffered IO case */
31790031462bSMingming Cao 	/*
31800031462bSMingming Cao 	 * repeat fallocate creation request
31810031462bSMingming Cao 	 * we already have an unwritten extent
31820031462bSMingming Cao 	 */
31830031462bSMingming Cao 	if (flags & EXT4_GET_BLOCKS_UNINIT_EXT)
31840031462bSMingming Cao 		goto map_out;
31850031462bSMingming Cao 
31860031462bSMingming Cao 	/* buffered READ or buffered write_begin() lookup */
31870031462bSMingming Cao 	if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
31880031462bSMingming Cao 		/*
31890031462bSMingming Cao 		 * We have blocks reserved already.  We
31900031462bSMingming Cao 		 * return allocated blocks so that delalloc
31910031462bSMingming Cao 		 * won't do block reservation for us.  But
31920031462bSMingming Cao 		 * the buffer head will be unmapped so that
31930031462bSMingming Cao 		 * a read from the block returns 0s.
31940031462bSMingming Cao 		 */
3195e35fd660STheodore Ts'o 		map->m_flags |= EXT4_MAP_UNWRITTEN;
31960031462bSMingming Cao 		goto out1;
31970031462bSMingming Cao 	}
31980031462bSMingming Cao 
31990031462bSMingming Cao 	/* buffered write, writepage time, convert*/
3200e35fd660STheodore Ts'o 	ret = ext4_ext_convert_to_initialized(handle, inode, map, path);
320158590b06STheodore Ts'o 	if (ret >= 0) {
3202b436b9beSJan Kara 		ext4_update_inode_fsync_trans(handle, inode, 1);
320358590b06STheodore Ts'o 		err = check_eofblocks_fl(handle, inode, map, path, map->m_len);
320458590b06STheodore Ts'o 		if (err < 0)
320558590b06STheodore Ts'o 			goto out2;
320658590b06STheodore Ts'o 	}
320758590b06STheodore Ts'o 
32080031462bSMingming Cao out:
32090031462bSMingming Cao 	if (ret <= 0) {
32100031462bSMingming Cao 		err = ret;
32110031462bSMingming Cao 		goto out2;
32120031462bSMingming Cao 	} else
32130031462bSMingming Cao 		allocated = ret;
3214e35fd660STheodore Ts'o 	map->m_flags |= EXT4_MAP_NEW;
3215515f41c3SAneesh Kumar K.V 	/*
3216515f41c3SAneesh Kumar K.V 	 * if we allocated more blocks than requested
3217515f41c3SAneesh Kumar K.V 	 * we need to make sure we unmap the extra block
3218515f41c3SAneesh Kumar K.V 	 * allocated. The actual needed block will get
3219515f41c3SAneesh Kumar K.V 	 * unmapped later when we find the buffer_head marked
3220515f41c3SAneesh Kumar K.V 	 * new.
3221515f41c3SAneesh Kumar K.V 	 */
3222e35fd660STheodore Ts'o 	if (allocated > map->m_len) {
3223515f41c3SAneesh Kumar K.V 		unmap_underlying_metadata_blocks(inode->i_sb->s_bdev,
3224e35fd660STheodore Ts'o 					newblock + map->m_len,
3225e35fd660STheodore Ts'o 					allocated - map->m_len);
3226e35fd660STheodore Ts'o 		allocated = map->m_len;
3227515f41c3SAneesh Kumar K.V 	}
32285f634d06SAneesh Kumar K.V 
32295f634d06SAneesh Kumar K.V 	/*
32305f634d06SAneesh Kumar K.V 	 * If we have done fallocate with the offset that is already
32315f634d06SAneesh Kumar K.V 	 * delayed allocated, we would have block reservation
32325f634d06SAneesh Kumar K.V 	 * and quota reservation done in the delayed write path.
32335f634d06SAneesh Kumar K.V 	 * But fallocate would have already updated quota and block
32345f634d06SAneesh Kumar K.V 	 * count for this offset. So cancel these reservation
32355f634d06SAneesh Kumar K.V 	 */
32361296cc85SAneesh Kumar K.V 	if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
32375f634d06SAneesh Kumar K.V 		ext4_da_update_reserve_space(inode, allocated, 0);
32385f634d06SAneesh Kumar K.V 
32390031462bSMingming Cao map_out:
3240e35fd660STheodore Ts'o 	map->m_flags |= EXT4_MAP_MAPPED;
32410031462bSMingming Cao out1:
3242e35fd660STheodore Ts'o 	if (allocated > map->m_len)
3243e35fd660STheodore Ts'o 		allocated = map->m_len;
32440031462bSMingming Cao 	ext4_ext_show_leaf(inode, path);
3245e35fd660STheodore Ts'o 	map->m_pblk = newblock;
3246e35fd660STheodore Ts'o 	map->m_len = allocated;
32470031462bSMingming Cao out2:
32480031462bSMingming Cao 	if (path) {
32490031462bSMingming Cao 		ext4_ext_drop_refs(path);
32500031462bSMingming Cao 		kfree(path);
32510031462bSMingming Cao 	}
32520031462bSMingming Cao 	return err ? err : allocated;
32530031462bSMingming Cao }
325458590b06STheodore Ts'o 
32550031462bSMingming Cao /*
3256f5ab0d1fSMingming Cao  * Block allocation/map/preallocation routine for extents based files
3257f5ab0d1fSMingming Cao  *
3258f5ab0d1fSMingming Cao  *
3259c278bfecSAneesh Kumar K.V  * Need to be called with
32600e855ac8SAneesh Kumar K.V  * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
32610e855ac8SAneesh Kumar K.V  * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
3262f5ab0d1fSMingming Cao  *
3263f5ab0d1fSMingming Cao  * return > 0, number of of blocks already mapped/allocated
3264f5ab0d1fSMingming Cao  *          if create == 0 and these are pre-allocated blocks
3265f5ab0d1fSMingming Cao  *          	buffer head is unmapped
3266f5ab0d1fSMingming Cao  *          otherwise blocks are mapped
3267f5ab0d1fSMingming Cao  *
3268f5ab0d1fSMingming Cao  * return = 0, if plain look up failed (blocks have not been allocated)
3269f5ab0d1fSMingming Cao  *          buffer head is unmapped
3270f5ab0d1fSMingming Cao  *
3271f5ab0d1fSMingming Cao  * return < 0, error case.
3272c278bfecSAneesh Kumar K.V  */
3273e35fd660STheodore Ts'o int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
3274e35fd660STheodore Ts'o 			struct ext4_map_blocks *map, int flags)
3275a86c6181SAlex Tomas {
3276a86c6181SAlex Tomas 	struct ext4_ext_path *path = NULL;
327756055d3aSAmit Arora 	struct ext4_extent_header *eh;
327858590b06STheodore Ts'o 	struct ext4_extent newex, *ex;
3279498e5f24STheodore Ts'o 	ext4_fsblk_t newblock;
328058590b06STheodore Ts'o 	int err = 0, depth, ret, cache_type;
3281498e5f24STheodore Ts'o 	unsigned int allocated = 0;
3282c9de560dSAlex Tomas 	struct ext4_allocation_request ar;
32838d5d02e6SMingming Cao 	ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
3284a86c6181SAlex Tomas 
328584fe3befSMingming 	ext_debug("blocks %u/%u requested for inode %lu\n",
3286e35fd660STheodore Ts'o 		  map->m_lblk, map->m_len, inode->i_ino);
3287a86c6181SAlex Tomas 
3288a86c6181SAlex Tomas 	/* check in cache */
3289e35fd660STheodore Ts'o 	cache_type = ext4_ext_in_cache(inode, map->m_lblk, &newex);
3290498e5f24STheodore Ts'o 	if (cache_type) {
3291498e5f24STheodore Ts'o 		if (cache_type == EXT4_EXT_CACHE_GAP) {
3292c2177057STheodore Ts'o 			if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
329356055d3aSAmit Arora 				/*
329456055d3aSAmit Arora 				 * block isn't allocated yet and
329556055d3aSAmit Arora 				 * user doesn't want to allocate it
329656055d3aSAmit Arora 				 */
3297a86c6181SAlex Tomas 				goto out2;
3298a86c6181SAlex Tomas 			}
3299a86c6181SAlex Tomas 			/* we should allocate requested block */
3300498e5f24STheodore Ts'o 		} else if (cache_type == EXT4_EXT_CACHE_EXTENT) {
3301a86c6181SAlex Tomas 			/* block is already allocated */
3302e35fd660STheodore Ts'o 			newblock = map->m_lblk
3303a86c6181SAlex Tomas 				   - le32_to_cpu(newex.ee_block)
3304*bf89d16fSTheodore Ts'o 				   + ext4_ext_pblock(&newex);
3305d0d856e8SRandy Dunlap 			/* number of remaining blocks in the extent */
3306b939e376SAneesh Kumar K.V 			allocated = ext4_ext_get_actual_len(&newex) -
3307e35fd660STheodore Ts'o 				(map->m_lblk - le32_to_cpu(newex.ee_block));
3308a86c6181SAlex Tomas 			goto out;
3309a86c6181SAlex Tomas 		} else {
3310a86c6181SAlex Tomas 			BUG();
3311a86c6181SAlex Tomas 		}
3312a86c6181SAlex Tomas 	}
3313a86c6181SAlex Tomas 
3314a86c6181SAlex Tomas 	/* find extent for this block */
3315e35fd660STheodore Ts'o 	path = ext4_ext_find_extent(inode, map->m_lblk, NULL);
3316a86c6181SAlex Tomas 	if (IS_ERR(path)) {
3317a86c6181SAlex Tomas 		err = PTR_ERR(path);
3318a86c6181SAlex Tomas 		path = NULL;
3319a86c6181SAlex Tomas 		goto out2;
3320a86c6181SAlex Tomas 	}
3321a86c6181SAlex Tomas 
3322a86c6181SAlex Tomas 	depth = ext_depth(inode);
3323a86c6181SAlex Tomas 
3324a86c6181SAlex Tomas 	/*
3325d0d856e8SRandy Dunlap 	 * consistent leaf must not be empty;
3326d0d856e8SRandy Dunlap 	 * this situation is possible, though, _during_ tree modification;
3327a86c6181SAlex Tomas 	 * this is why assert can't be put in ext4_ext_find_extent()
3328a86c6181SAlex Tomas 	 */
3329273df556SFrank Mayhar 	if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
3330273df556SFrank Mayhar 		EXT4_ERROR_INODE(inode, "bad extent address "
3331f70f362bSTheodore Ts'o 				 "lblock: %lu, depth: %d pblock %lld",
3332f70f362bSTheodore Ts'o 				 (unsigned long) map->m_lblk, depth,
3333f70f362bSTheodore Ts'o 				 path[depth].p_block);
3334034fb4c9SSurbhi Palande 		err = -EIO;
3335034fb4c9SSurbhi Palande 		goto out2;
3336034fb4c9SSurbhi Palande 	}
333756055d3aSAmit Arora 	eh = path[depth].p_hdr;
3338a86c6181SAlex Tomas 
33397e028976SAvantika Mathur 	ex = path[depth].p_ext;
33407e028976SAvantika Mathur 	if (ex) {
3341725d26d3SAneesh Kumar K.V 		ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
3342*bf89d16fSTheodore Ts'o 		ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
3343a2df2a63SAmit Arora 		unsigned short ee_len;
3344471d4011SSuparna Bhattacharya 
3345471d4011SSuparna Bhattacharya 		/*
3346471d4011SSuparna Bhattacharya 		 * Uninitialized extents are treated as holes, except that
334756055d3aSAmit Arora 		 * we split out initialized portions during a write.
3348471d4011SSuparna Bhattacharya 		 */
3349a2df2a63SAmit Arora 		ee_len = ext4_ext_get_actual_len(ex);
3350d0d856e8SRandy Dunlap 		/* if found extent covers block, simply return it */
3351e35fd660STheodore Ts'o 		if (in_range(map->m_lblk, ee_block, ee_len)) {
3352e35fd660STheodore Ts'o 			newblock = map->m_lblk - ee_block + ee_start;
3353d0d856e8SRandy Dunlap 			/* number of remaining blocks in the extent */
3354e35fd660STheodore Ts'o 			allocated = ee_len - (map->m_lblk - ee_block);
3355e35fd660STheodore Ts'o 			ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
3356a86c6181SAlex Tomas 				  ee_block, ee_len, newblock);
335756055d3aSAmit Arora 
3358a2df2a63SAmit Arora 			/* Do not put uninitialized extent in the cache */
335956055d3aSAmit Arora 			if (!ext4_ext_is_uninitialized(ex)) {
3360a2df2a63SAmit Arora 				ext4_ext_put_in_cache(inode, ee_block,
3361a2df2a63SAmit Arora 							ee_len, ee_start,
3362a2df2a63SAmit Arora 							EXT4_EXT_CACHE_EXTENT);
3363a86c6181SAlex Tomas 				goto out;
3364a86c6181SAlex Tomas 			}
33650031462bSMingming Cao 			ret = ext4_ext_handle_uninitialized_extents(handle,
3366e35fd660STheodore Ts'o 					inode, map, path, flags, allocated,
3367e35fd660STheodore Ts'o 					newblock);
33680031462bSMingming Cao 			return ret;
336956055d3aSAmit Arora 		}
3370a86c6181SAlex Tomas 	}
3371a86c6181SAlex Tomas 
3372a86c6181SAlex Tomas 	/*
3373d0d856e8SRandy Dunlap 	 * requested block isn't allocated yet;
3374a86c6181SAlex Tomas 	 * we couldn't try to create block if create flag is zero
3375a86c6181SAlex Tomas 	 */
3376c2177057STheodore Ts'o 	if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
337756055d3aSAmit Arora 		/*
337856055d3aSAmit Arora 		 * put just found gap into cache to speed up
337956055d3aSAmit Arora 		 * subsequent requests
338056055d3aSAmit Arora 		 */
3381e35fd660STheodore Ts'o 		ext4_ext_put_gap_in_cache(inode, path, map->m_lblk);
3382a86c6181SAlex Tomas 		goto out2;
3383a86c6181SAlex Tomas 	}
3384a86c6181SAlex Tomas 	/*
3385c2ea3fdeSTheodore Ts'o 	 * Okay, we need to do block allocation.
3386a86c6181SAlex Tomas 	 */
3387a86c6181SAlex Tomas 
3388c9de560dSAlex Tomas 	/* find neighbour allocated blocks */
3389e35fd660STheodore Ts'o 	ar.lleft = map->m_lblk;
3390c9de560dSAlex Tomas 	err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
3391c9de560dSAlex Tomas 	if (err)
3392c9de560dSAlex Tomas 		goto out2;
3393e35fd660STheodore Ts'o 	ar.lright = map->m_lblk;
3394c9de560dSAlex Tomas 	err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright);
3395c9de560dSAlex Tomas 	if (err)
3396c9de560dSAlex Tomas 		goto out2;
339725d14f98SAmit Arora 
3398749269faSAmit Arora 	/*
3399749269faSAmit Arora 	 * See if request is beyond maximum number of blocks we can have in
3400749269faSAmit Arora 	 * a single extent. For an initialized extent this limit is
3401749269faSAmit Arora 	 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
3402749269faSAmit Arora 	 * EXT_UNINIT_MAX_LEN.
3403749269faSAmit Arora 	 */
3404e35fd660STheodore Ts'o 	if (map->m_len > EXT_INIT_MAX_LEN &&
3405c2177057STheodore Ts'o 	    !(flags & EXT4_GET_BLOCKS_UNINIT_EXT))
3406e35fd660STheodore Ts'o 		map->m_len = EXT_INIT_MAX_LEN;
3407e35fd660STheodore Ts'o 	else if (map->m_len > EXT_UNINIT_MAX_LEN &&
3408c2177057STheodore Ts'o 		 (flags & EXT4_GET_BLOCKS_UNINIT_EXT))
3409e35fd660STheodore Ts'o 		map->m_len = EXT_UNINIT_MAX_LEN;
3410749269faSAmit Arora 
3411e35fd660STheodore Ts'o 	/* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
3412e35fd660STheodore Ts'o 	newex.ee_block = cpu_to_le32(map->m_lblk);
3413e35fd660STheodore Ts'o 	newex.ee_len = cpu_to_le16(map->m_len);
341425d14f98SAmit Arora 	err = ext4_ext_check_overlap(inode, &newex, path);
341525d14f98SAmit Arora 	if (err)
3416b939e376SAneesh Kumar K.V 		allocated = ext4_ext_get_actual_len(&newex);
341725d14f98SAmit Arora 	else
3418e35fd660STheodore Ts'o 		allocated = map->m_len;
3419c9de560dSAlex Tomas 
3420c9de560dSAlex Tomas 	/* allocate new block */
3421c9de560dSAlex Tomas 	ar.inode = inode;
3422e35fd660STheodore Ts'o 	ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
3423e35fd660STheodore Ts'o 	ar.logical = map->m_lblk;
3424c9de560dSAlex Tomas 	ar.len = allocated;
3425c9de560dSAlex Tomas 	if (S_ISREG(inode->i_mode))
3426c9de560dSAlex Tomas 		ar.flags = EXT4_MB_HINT_DATA;
3427c9de560dSAlex Tomas 	else
3428c9de560dSAlex Tomas 		/* disable in-core preallocation for non-regular files */
3429c9de560dSAlex Tomas 		ar.flags = 0;
3430c9de560dSAlex Tomas 	newblock = ext4_mb_new_blocks(handle, &ar, &err);
3431a86c6181SAlex Tomas 	if (!newblock)
3432a86c6181SAlex Tomas 		goto out2;
343384fe3befSMingming 	ext_debug("allocate new block: goal %llu, found %llu/%u\n",
3434498e5f24STheodore Ts'o 		  ar.goal, newblock, allocated);
3435a86c6181SAlex Tomas 
3436a86c6181SAlex Tomas 	/* try to insert new extent into found leaf and return */
3437f65e6fbaSAlex Tomas 	ext4_ext_store_pblock(&newex, newblock);
3438c9de560dSAlex Tomas 	newex.ee_len = cpu_to_le16(ar.len);
34398d5d02e6SMingming Cao 	/* Mark uninitialized */
34408d5d02e6SMingming Cao 	if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){
3441a2df2a63SAmit Arora 		ext4_ext_mark_uninitialized(&newex);
34428d5d02e6SMingming Cao 		/*
3443744692dcSJiaying Zhang 		 * io_end structure was created for every IO write to an
3444744692dcSJiaying Zhang 		 * uninitialized extent. To avoid unecessary conversion,
3445744692dcSJiaying Zhang 		 * here we flag the IO that really needs the conversion.
34465f524950SMingming 		 * For non asycn direct IO case, flag the inode state
34475f524950SMingming 		 * that we need to perform convertion when IO is done.
34488d5d02e6SMingming Cao 		 */
3449744692dcSJiaying Zhang 		if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
34505f524950SMingming 			if (io)
3451bd2d0210STheodore Ts'o 				io->flag = EXT4_IO_END_UNWRITTEN;
34525f524950SMingming 			else
345319f5fb7aSTheodore Ts'o 				ext4_set_inode_state(inode,
345419f5fb7aSTheodore Ts'o 						     EXT4_STATE_DIO_UNWRITTEN);
34555f524950SMingming 		}
3456744692dcSJiaying Zhang 		if (ext4_should_dioread_nolock(inode))
3457e35fd660STheodore Ts'o 			map->m_flags |= EXT4_MAP_UNINIT;
34588d5d02e6SMingming Cao 	}
3459c8d46e41SJiaying Zhang 
346058590b06STheodore Ts'o 	err = check_eofblocks_fl(handle, inode, map, path, ar.len);
346158590b06STheodore Ts'o 	if (err)
3462273df556SFrank Mayhar 		goto out2;
346358590b06STheodore Ts'o 
34640031462bSMingming Cao 	err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
3465315054f0SAlex Tomas 	if (err) {
3466315054f0SAlex Tomas 		/* free data blocks we just allocated */
3467c9de560dSAlex Tomas 		/* not a good idea to call discard here directly,
3468c9de560dSAlex Tomas 		 * but otherwise we'd need to call it every free() */
3469c2ea3fdeSTheodore Ts'o 		ext4_discard_preallocations(inode);
3470*bf89d16fSTheodore Ts'o 		ext4_free_blocks(handle, inode, 0, ext4_ext_pblock(&newex),
3471b939e376SAneesh Kumar K.V 				 ext4_ext_get_actual_len(&newex), 0);
3472a86c6181SAlex Tomas 		goto out2;
3473315054f0SAlex Tomas 	}
3474a86c6181SAlex Tomas 
3475a86c6181SAlex Tomas 	/* previous routine could use block we allocated */
3476*bf89d16fSTheodore Ts'o 	newblock = ext4_ext_pblock(&newex);
3477b939e376SAneesh Kumar K.V 	allocated = ext4_ext_get_actual_len(&newex);
3478e35fd660STheodore Ts'o 	if (allocated > map->m_len)
3479e35fd660STheodore Ts'o 		allocated = map->m_len;
3480e35fd660STheodore Ts'o 	map->m_flags |= EXT4_MAP_NEW;
3481a86c6181SAlex Tomas 
3482b436b9beSJan Kara 	/*
34835f634d06SAneesh Kumar K.V 	 * Update reserved blocks/metadata blocks after successful
34845f634d06SAneesh Kumar K.V 	 * block allocation which had been deferred till now.
34855f634d06SAneesh Kumar K.V 	 */
34861296cc85SAneesh Kumar K.V 	if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
34875f634d06SAneesh Kumar K.V 		ext4_da_update_reserve_space(inode, allocated, 1);
34885f634d06SAneesh Kumar K.V 
34895f634d06SAneesh Kumar K.V 	/*
3490b436b9beSJan Kara 	 * Cache the extent and update transaction to commit on fdatasync only
3491b436b9beSJan Kara 	 * when it is _not_ an uninitialized extent.
3492b436b9beSJan Kara 	 */
3493b436b9beSJan Kara 	if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) {
3494e35fd660STheodore Ts'o 		ext4_ext_put_in_cache(inode, map->m_lblk, allocated, newblock,
3495a86c6181SAlex Tomas 						EXT4_EXT_CACHE_EXTENT);
3496b436b9beSJan Kara 		ext4_update_inode_fsync_trans(handle, inode, 1);
3497b436b9beSJan Kara 	} else
3498b436b9beSJan Kara 		ext4_update_inode_fsync_trans(handle, inode, 0);
3499a86c6181SAlex Tomas out:
3500e35fd660STheodore Ts'o 	if (allocated > map->m_len)
3501e35fd660STheodore Ts'o 		allocated = map->m_len;
3502a86c6181SAlex Tomas 	ext4_ext_show_leaf(inode, path);
3503e35fd660STheodore Ts'o 	map->m_flags |= EXT4_MAP_MAPPED;
3504e35fd660STheodore Ts'o 	map->m_pblk = newblock;
3505e35fd660STheodore Ts'o 	map->m_len = allocated;
3506a86c6181SAlex Tomas out2:
3507a86c6181SAlex Tomas 	if (path) {
3508a86c6181SAlex Tomas 		ext4_ext_drop_refs(path);
3509a86c6181SAlex Tomas 		kfree(path);
3510a86c6181SAlex Tomas 	}
3511a86c6181SAlex Tomas 	return err ? err : allocated;
3512a86c6181SAlex Tomas }
3513a86c6181SAlex Tomas 
3514cf108bcaSJan Kara void ext4_ext_truncate(struct inode *inode)
3515a86c6181SAlex Tomas {
3516a86c6181SAlex Tomas 	struct address_space *mapping = inode->i_mapping;
3517a86c6181SAlex Tomas 	struct super_block *sb = inode->i_sb;
3518725d26d3SAneesh Kumar K.V 	ext4_lblk_t last_block;
3519a86c6181SAlex Tomas 	handle_t *handle;
3520a86c6181SAlex Tomas 	int err = 0;
3521a86c6181SAlex Tomas 
3522a86c6181SAlex Tomas 	/*
3523a86c6181SAlex Tomas 	 * probably first extent we're gonna free will be last in block
3524a86c6181SAlex Tomas 	 */
3525f3bd1f3fSMingming Cao 	err = ext4_writepage_trans_blocks(inode);
3526a86c6181SAlex Tomas 	handle = ext4_journal_start(inode, err);
3527cf108bcaSJan Kara 	if (IS_ERR(handle))
3528a86c6181SAlex Tomas 		return;
3529a86c6181SAlex Tomas 
3530cf108bcaSJan Kara 	if (inode->i_size & (sb->s_blocksize - 1))
3531cf108bcaSJan Kara 		ext4_block_truncate_page(handle, mapping, inode->i_size);
3532a86c6181SAlex Tomas 
35339ddfc3dcSJan Kara 	if (ext4_orphan_add(handle, inode))
35349ddfc3dcSJan Kara 		goto out_stop;
35359ddfc3dcSJan Kara 
35360e855ac8SAneesh Kumar K.V 	down_write(&EXT4_I(inode)->i_data_sem);
3537a86c6181SAlex Tomas 	ext4_ext_invalidate_cache(inode);
3538a86c6181SAlex Tomas 
3539c2ea3fdeSTheodore Ts'o 	ext4_discard_preallocations(inode);
3540c9de560dSAlex Tomas 
3541a86c6181SAlex Tomas 	/*
3542d0d856e8SRandy Dunlap 	 * TODO: optimization is possible here.
3543d0d856e8SRandy Dunlap 	 * Probably we need not scan at all,
3544d0d856e8SRandy Dunlap 	 * because page truncation is enough.
3545a86c6181SAlex Tomas 	 */
3546a86c6181SAlex Tomas 
3547a86c6181SAlex Tomas 	/* we have to know where to truncate from in crash case */
3548a86c6181SAlex Tomas 	EXT4_I(inode)->i_disksize = inode->i_size;
3549a86c6181SAlex Tomas 	ext4_mark_inode_dirty(handle, inode);
3550a86c6181SAlex Tomas 
3551a86c6181SAlex Tomas 	last_block = (inode->i_size + sb->s_blocksize - 1)
3552a86c6181SAlex Tomas 			>> EXT4_BLOCK_SIZE_BITS(sb);
3553a86c6181SAlex Tomas 	err = ext4_ext_remove_space(inode, last_block);
3554a86c6181SAlex Tomas 
3555a86c6181SAlex Tomas 	/* In a multi-transaction truncate, we only make the final
355656055d3aSAmit Arora 	 * transaction synchronous.
355756055d3aSAmit Arora 	 */
3558a86c6181SAlex Tomas 	if (IS_SYNC(inode))
35590390131bSFrank Mayhar 		ext4_handle_sync(handle);
3560a86c6181SAlex Tomas 
3561a86c6181SAlex Tomas out_stop:
35629ddfc3dcSJan Kara 	up_write(&EXT4_I(inode)->i_data_sem);
3563a86c6181SAlex Tomas 	/*
3564d0d856e8SRandy Dunlap 	 * If this was a simple ftruncate() and the file will remain alive,
3565a86c6181SAlex Tomas 	 * then we need to clear up the orphan record which we created above.
3566a86c6181SAlex Tomas 	 * However, if this was a real unlink then we were called by
3567a86c6181SAlex Tomas 	 * ext4_delete_inode(), and we allow that function to clean up the
3568a86c6181SAlex Tomas 	 * orphan info for us.
3569a86c6181SAlex Tomas 	 */
3570a86c6181SAlex Tomas 	if (inode->i_nlink)
3571a86c6181SAlex Tomas 		ext4_orphan_del(handle, inode);
3572a86c6181SAlex Tomas 
3573ef737728SSolofo Ramangalahy 	inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
3574ef737728SSolofo Ramangalahy 	ext4_mark_inode_dirty(handle, inode);
3575a86c6181SAlex Tomas 	ext4_journal_stop(handle);
3576a86c6181SAlex Tomas }
3577a86c6181SAlex Tomas 
3578fd28784aSAneesh Kumar K.V static void ext4_falloc_update_inode(struct inode *inode,
3579fd28784aSAneesh Kumar K.V 				int mode, loff_t new_size, int update_ctime)
3580fd28784aSAneesh Kumar K.V {
3581fd28784aSAneesh Kumar K.V 	struct timespec now;
3582fd28784aSAneesh Kumar K.V 
3583fd28784aSAneesh Kumar K.V 	if (update_ctime) {
3584fd28784aSAneesh Kumar K.V 		now = current_fs_time(inode->i_sb);
3585fd28784aSAneesh Kumar K.V 		if (!timespec_equal(&inode->i_ctime, &now))
3586fd28784aSAneesh Kumar K.V 			inode->i_ctime = now;
3587fd28784aSAneesh Kumar K.V 	}
3588fd28784aSAneesh Kumar K.V 	/*
3589fd28784aSAneesh Kumar K.V 	 * Update only when preallocation was requested beyond
3590fd28784aSAneesh Kumar K.V 	 * the file size.
3591fd28784aSAneesh Kumar K.V 	 */
3592cf17fea6SAneesh Kumar K.V 	if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3593cf17fea6SAneesh Kumar K.V 		if (new_size > i_size_read(inode))
3594fd28784aSAneesh Kumar K.V 			i_size_write(inode, new_size);
3595cf17fea6SAneesh Kumar K.V 		if (new_size > EXT4_I(inode)->i_disksize)
3596cf17fea6SAneesh Kumar K.V 			ext4_update_i_disksize(inode, new_size);
3597c8d46e41SJiaying Zhang 	} else {
3598c8d46e41SJiaying Zhang 		/*
3599c8d46e41SJiaying Zhang 		 * Mark that we allocate beyond EOF so the subsequent truncate
3600c8d46e41SJiaying Zhang 		 * can proceed even if the new size is the same as i_size.
3601c8d46e41SJiaying Zhang 		 */
3602c8d46e41SJiaying Zhang 		if (new_size > i_size_read(inode))
360312e9b892SDmitry Monakhov 			ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
3604fd28784aSAneesh Kumar K.V 	}
3605fd28784aSAneesh Kumar K.V 
3606fd28784aSAneesh Kumar K.V }
3607fd28784aSAneesh Kumar K.V 
3608a2df2a63SAmit Arora /*
3609a2df2a63SAmit Arora  * preallocate space for a file. This implements ext4's fallocate inode
3610a2df2a63SAmit Arora  * operation, which gets called from sys_fallocate system call.
3611a2df2a63SAmit Arora  * For block-mapped files, posix_fallocate should fall back to the method
3612a2df2a63SAmit Arora  * of writing zeroes to the required new blocks (the same behavior which is
3613a2df2a63SAmit Arora  * expected for file systems which do not support fallocate() system call).
3614a2df2a63SAmit Arora  */
3615a2df2a63SAmit Arora long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
3616a2df2a63SAmit Arora {
3617a2df2a63SAmit Arora 	handle_t *handle;
3618fd28784aSAneesh Kumar K.V 	loff_t new_size;
3619498e5f24STheodore Ts'o 	unsigned int max_blocks;
3620a2df2a63SAmit Arora 	int ret = 0;
3621a2df2a63SAmit Arora 	int ret2 = 0;
3622a2df2a63SAmit Arora 	int retries = 0;
36232ed88685STheodore Ts'o 	struct ext4_map_blocks map;
3624a2df2a63SAmit Arora 	unsigned int credits, blkbits = inode->i_blkbits;
3625a2df2a63SAmit Arora 
3626a2df2a63SAmit Arora 	/*
3627a2df2a63SAmit Arora 	 * currently supporting (pre)allocate mode for extent-based
3628a2df2a63SAmit Arora 	 * files _only_
3629a2df2a63SAmit Arora 	 */
363012e9b892SDmitry Monakhov 	if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
3631a2df2a63SAmit Arora 		return -EOPNOTSUPP;
3632a2df2a63SAmit Arora 
3633a2df2a63SAmit Arora 	/* preallocation to directories is currently not supported */
3634a2df2a63SAmit Arora 	if (S_ISDIR(inode->i_mode))
3635a2df2a63SAmit Arora 		return -ENODEV;
3636a2df2a63SAmit Arora 
36372ed88685STheodore Ts'o 	map.m_lblk = offset >> blkbits;
3638fd28784aSAneesh Kumar K.V 	/*
3639fd28784aSAneesh Kumar K.V 	 * We can't just convert len to max_blocks because
3640fd28784aSAneesh Kumar K.V 	 * If blocksize = 4096 offset = 3072 and len = 2048
3641fd28784aSAneesh Kumar K.V 	 */
3642a2df2a63SAmit Arora 	max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
36432ed88685STheodore Ts'o 		- map.m_lblk;
3644a2df2a63SAmit Arora 	/*
3645f3bd1f3fSMingming Cao 	 * credits to insert 1 extent into extent tree
3646a2df2a63SAmit Arora 	 */
3647f3bd1f3fSMingming Cao 	credits = ext4_chunk_trans_blocks(inode, max_blocks);
364855bd725aSAneesh Kumar K.V 	mutex_lock(&inode->i_mutex);
36496d19c42bSNikanth Karthikesan 	ret = inode_newsize_ok(inode, (len + offset));
36506d19c42bSNikanth Karthikesan 	if (ret) {
36516d19c42bSNikanth Karthikesan 		mutex_unlock(&inode->i_mutex);
36526d19c42bSNikanth Karthikesan 		return ret;
36536d19c42bSNikanth Karthikesan 	}
3654a2df2a63SAmit Arora retry:
3655a2df2a63SAmit Arora 	while (ret >= 0 && ret < max_blocks) {
36562ed88685STheodore Ts'o 		map.m_lblk = map.m_lblk + ret;
36572ed88685STheodore Ts'o 		map.m_len = max_blocks = max_blocks - ret;
3658a2df2a63SAmit Arora 		handle = ext4_journal_start(inode, credits);
3659a2df2a63SAmit Arora 		if (IS_ERR(handle)) {
3660a2df2a63SAmit Arora 			ret = PTR_ERR(handle);
3661a2df2a63SAmit Arora 			break;
3662a2df2a63SAmit Arora 		}
36632ed88685STheodore Ts'o 		ret = ext4_map_blocks(handle, inode, &map,
3664c2177057STheodore Ts'o 				      EXT4_GET_BLOCKS_CREATE_UNINIT_EXT);
3665221879c9SAneesh Kumar K.V 		if (ret <= 0) {
36662c98615dSAneesh Kumar K.V #ifdef EXT4FS_DEBUG
36672c98615dSAneesh Kumar K.V 			WARN_ON(ret <= 0);
3668e35fd660STheodore Ts'o 			printk(KERN_ERR "%s: ext4_ext_map_blocks "
36692c98615dSAneesh Kumar K.V 				    "returned error inode#%lu, block=%u, "
36709fd9784cSThadeu Lima de Souza Cascardo 				    "max_blocks=%u", __func__,
3671bba90743SEric Sandeen 				    inode->i_ino, block, max_blocks);
36722c98615dSAneesh Kumar K.V #endif
3673a2df2a63SAmit Arora 			ext4_mark_inode_dirty(handle, inode);
3674a2df2a63SAmit Arora 			ret2 = ext4_journal_stop(handle);
3675a2df2a63SAmit Arora 			break;
3676a2df2a63SAmit Arora 		}
36772ed88685STheodore Ts'o 		if ((map.m_lblk + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
3678fd28784aSAneesh Kumar K.V 						blkbits) >> blkbits))
3679fd28784aSAneesh Kumar K.V 			new_size = offset + len;
3680fd28784aSAneesh Kumar K.V 		else
36812ed88685STheodore Ts'o 			new_size = (map.m_lblk + ret) << blkbits;
3682a2df2a63SAmit Arora 
3683fd28784aSAneesh Kumar K.V 		ext4_falloc_update_inode(inode, mode, new_size,
36842ed88685STheodore Ts'o 					 (map.m_flags & EXT4_MAP_NEW));
3685a2df2a63SAmit Arora 		ext4_mark_inode_dirty(handle, inode);
3686a2df2a63SAmit Arora 		ret2 = ext4_journal_stop(handle);
3687a2df2a63SAmit Arora 		if (ret2)
3688a2df2a63SAmit Arora 			break;
3689a2df2a63SAmit Arora 	}
3690fd28784aSAneesh Kumar K.V 	if (ret == -ENOSPC &&
3691fd28784aSAneesh Kumar K.V 			ext4_should_retry_alloc(inode->i_sb, &retries)) {
3692fd28784aSAneesh Kumar K.V 		ret = 0;
3693a2df2a63SAmit Arora 		goto retry;
3694a2df2a63SAmit Arora 	}
369555bd725aSAneesh Kumar K.V 	mutex_unlock(&inode->i_mutex);
3696a2df2a63SAmit Arora 	return ret > 0 ? ret2 : ret;
3697a2df2a63SAmit Arora }
36986873fa0dSEric Sandeen 
36996873fa0dSEric Sandeen /*
37000031462bSMingming Cao  * This function convert a range of blocks to written extents
37010031462bSMingming Cao  * The caller of this function will pass the start offset and the size.
37020031462bSMingming Cao  * all unwritten extents within this range will be converted to
37030031462bSMingming Cao  * written extents.
37040031462bSMingming Cao  *
37050031462bSMingming Cao  * This function is called from the direct IO end io call back
37060031462bSMingming Cao  * function, to convert the fallocated extents after IO is completed.
3707109f5565SMingming  * Returns 0 on success.
37080031462bSMingming Cao  */
37090031462bSMingming Cao int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset,
3710a1de02dcSEric Sandeen 				    ssize_t len)
37110031462bSMingming Cao {
37120031462bSMingming Cao 	handle_t *handle;
37130031462bSMingming Cao 	unsigned int max_blocks;
37140031462bSMingming Cao 	int ret = 0;
37150031462bSMingming Cao 	int ret2 = 0;
37162ed88685STheodore Ts'o 	struct ext4_map_blocks map;
37170031462bSMingming Cao 	unsigned int credits, blkbits = inode->i_blkbits;
37180031462bSMingming Cao 
37192ed88685STheodore Ts'o 	map.m_lblk = offset >> blkbits;
37200031462bSMingming Cao 	/*
37210031462bSMingming Cao 	 * We can't just convert len to max_blocks because
37220031462bSMingming Cao 	 * If blocksize = 4096 offset = 3072 and len = 2048
37230031462bSMingming Cao 	 */
37242ed88685STheodore Ts'o 	max_blocks = ((EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) -
37252ed88685STheodore Ts'o 		      map.m_lblk);
37260031462bSMingming Cao 	/*
37270031462bSMingming Cao 	 * credits to insert 1 extent into extent tree
37280031462bSMingming Cao 	 */
37290031462bSMingming Cao 	credits = ext4_chunk_trans_blocks(inode, max_blocks);
37300031462bSMingming Cao 	while (ret >= 0 && ret < max_blocks) {
37312ed88685STheodore Ts'o 		map.m_lblk += ret;
37322ed88685STheodore Ts'o 		map.m_len = (max_blocks -= ret);
37330031462bSMingming Cao 		handle = ext4_journal_start(inode, credits);
37340031462bSMingming Cao 		if (IS_ERR(handle)) {
37350031462bSMingming Cao 			ret = PTR_ERR(handle);
37360031462bSMingming Cao 			break;
37370031462bSMingming Cao 		}
37382ed88685STheodore Ts'o 		ret = ext4_map_blocks(handle, inode, &map,
3739c7064ef1SJiaying Zhang 				      EXT4_GET_BLOCKS_IO_CONVERT_EXT);
37400031462bSMingming Cao 		if (ret <= 0) {
37410031462bSMingming Cao 			WARN_ON(ret <= 0);
3742e35fd660STheodore Ts'o 			printk(KERN_ERR "%s: ext4_ext_map_blocks "
37430031462bSMingming Cao 				    "returned error inode#%lu, block=%u, "
37440031462bSMingming Cao 				    "max_blocks=%u", __func__,
37452ed88685STheodore Ts'o 				    inode->i_ino, map.m_lblk, map.m_len);
37460031462bSMingming Cao 		}
37470031462bSMingming Cao 		ext4_mark_inode_dirty(handle, inode);
37480031462bSMingming Cao 		ret2 = ext4_journal_stop(handle);
37490031462bSMingming Cao 		if (ret <= 0 || ret2 )
37500031462bSMingming Cao 			break;
37510031462bSMingming Cao 	}
37520031462bSMingming Cao 	return ret > 0 ? ret2 : ret;
37530031462bSMingming Cao }
37540031462bSMingming Cao /*
37556873fa0dSEric Sandeen  * Callback function called for each extent to gather FIEMAP information.
37566873fa0dSEric Sandeen  */
37573a06d778SAneesh Kumar K.V static int ext4_ext_fiemap_cb(struct inode *inode, struct ext4_ext_path *path,
37586873fa0dSEric Sandeen 		       struct ext4_ext_cache *newex, struct ext4_extent *ex,
37596873fa0dSEric Sandeen 		       void *data)
37606873fa0dSEric Sandeen {
37616873fa0dSEric Sandeen 	struct fiemap_extent_info *fieinfo = data;
3762c9877b20SEric Sandeen 	unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
37636873fa0dSEric Sandeen 	__u64	logical;
37646873fa0dSEric Sandeen 	__u64	physical;
37656873fa0dSEric Sandeen 	__u64	length;
37666873fa0dSEric Sandeen 	__u32	flags = 0;
37676873fa0dSEric Sandeen 	int	error;
37686873fa0dSEric Sandeen 
37696873fa0dSEric Sandeen 	logical =  (__u64)newex->ec_block << blksize_bits;
37706873fa0dSEric Sandeen 
37716873fa0dSEric Sandeen 	if (newex->ec_type == EXT4_EXT_CACHE_GAP) {
37726873fa0dSEric Sandeen 		pgoff_t offset;
37736873fa0dSEric Sandeen 		struct page *page;
37746873fa0dSEric Sandeen 		struct buffer_head *bh = NULL;
37756873fa0dSEric Sandeen 
37766873fa0dSEric Sandeen 		offset = logical >> PAGE_SHIFT;
37776873fa0dSEric Sandeen 		page = find_get_page(inode->i_mapping, offset);
37786873fa0dSEric Sandeen 		if (!page || !page_has_buffers(page))
37796873fa0dSEric Sandeen 			return EXT_CONTINUE;
37806873fa0dSEric Sandeen 
37816873fa0dSEric Sandeen 		bh = page_buffers(page);
37826873fa0dSEric Sandeen 
37836873fa0dSEric Sandeen 		if (!bh)
37846873fa0dSEric Sandeen 			return EXT_CONTINUE;
37856873fa0dSEric Sandeen 
37866873fa0dSEric Sandeen 		if (buffer_delay(bh)) {
37876873fa0dSEric Sandeen 			flags |= FIEMAP_EXTENT_DELALLOC;
37886873fa0dSEric Sandeen 			page_cache_release(page);
37896873fa0dSEric Sandeen 		} else {
37906873fa0dSEric Sandeen 			page_cache_release(page);
37916873fa0dSEric Sandeen 			return EXT_CONTINUE;
37926873fa0dSEric Sandeen 		}
37936873fa0dSEric Sandeen 	}
37946873fa0dSEric Sandeen 
37956873fa0dSEric Sandeen 	physical = (__u64)newex->ec_start << blksize_bits;
37966873fa0dSEric Sandeen 	length =   (__u64)newex->ec_len << blksize_bits;
37976873fa0dSEric Sandeen 
37986873fa0dSEric Sandeen 	if (ex && ext4_ext_is_uninitialized(ex))
37996873fa0dSEric Sandeen 		flags |= FIEMAP_EXTENT_UNWRITTEN;
38006873fa0dSEric Sandeen 
38016873fa0dSEric Sandeen 	/*
38026873fa0dSEric Sandeen 	 * If this extent reaches EXT_MAX_BLOCK, it must be last.
38036873fa0dSEric Sandeen 	 *
38046873fa0dSEric Sandeen 	 * Or if ext4_ext_next_allocated_block is EXT_MAX_BLOCK,
38056873fa0dSEric Sandeen 	 * this also indicates no more allocated blocks.
38066873fa0dSEric Sandeen 	 *
38076873fa0dSEric Sandeen 	 * XXX this might miss a single-block extent at EXT_MAX_BLOCK
38086873fa0dSEric Sandeen 	 */
3809c9877b20SEric Sandeen 	if (ext4_ext_next_allocated_block(path) == EXT_MAX_BLOCK ||
3810eefd7f03STheodore Ts'o 	    newex->ec_block + newex->ec_len - 1 == EXT_MAX_BLOCK) {
3811eefd7f03STheodore Ts'o 		loff_t size = i_size_read(inode);
3812eefd7f03STheodore Ts'o 		loff_t bs = EXT4_BLOCK_SIZE(inode->i_sb);
3813eefd7f03STheodore Ts'o 
38146873fa0dSEric Sandeen 		flags |= FIEMAP_EXTENT_LAST;
3815eefd7f03STheodore Ts'o 		if ((flags & FIEMAP_EXTENT_DELALLOC) &&
3816eefd7f03STheodore Ts'o 		    logical+length > size)
3817eefd7f03STheodore Ts'o 			length = (size - logical + bs - 1) & ~(bs-1);
3818eefd7f03STheodore Ts'o 	}
38196873fa0dSEric Sandeen 
38206873fa0dSEric Sandeen 	error = fiemap_fill_next_extent(fieinfo, logical, physical,
38216873fa0dSEric Sandeen 					length, flags);
38226873fa0dSEric Sandeen 	if (error < 0)
38236873fa0dSEric Sandeen 		return error;
38246873fa0dSEric Sandeen 	if (error == 1)
38256873fa0dSEric Sandeen 		return EXT_BREAK;
38266873fa0dSEric Sandeen 
38276873fa0dSEric Sandeen 	return EXT_CONTINUE;
38286873fa0dSEric Sandeen }
38296873fa0dSEric Sandeen 
38306873fa0dSEric Sandeen /* fiemap flags we can handle specified here */
38316873fa0dSEric Sandeen #define EXT4_FIEMAP_FLAGS	(FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
38326873fa0dSEric Sandeen 
38333a06d778SAneesh Kumar K.V static int ext4_xattr_fiemap(struct inode *inode,
38343a06d778SAneesh Kumar K.V 				struct fiemap_extent_info *fieinfo)
38356873fa0dSEric Sandeen {
38366873fa0dSEric Sandeen 	__u64 physical = 0;
38376873fa0dSEric Sandeen 	__u64 length;
38386873fa0dSEric Sandeen 	__u32 flags = FIEMAP_EXTENT_LAST;
38396873fa0dSEric Sandeen 	int blockbits = inode->i_sb->s_blocksize_bits;
38406873fa0dSEric Sandeen 	int error = 0;
38416873fa0dSEric Sandeen 
38426873fa0dSEric Sandeen 	/* in-inode? */
384319f5fb7aSTheodore Ts'o 	if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
38446873fa0dSEric Sandeen 		struct ext4_iloc iloc;
38456873fa0dSEric Sandeen 		int offset;	/* offset of xattr in inode */
38466873fa0dSEric Sandeen 
38476873fa0dSEric Sandeen 		error = ext4_get_inode_loc(inode, &iloc);
38486873fa0dSEric Sandeen 		if (error)
38496873fa0dSEric Sandeen 			return error;
38506873fa0dSEric Sandeen 		physical = iloc.bh->b_blocknr << blockbits;
38516873fa0dSEric Sandeen 		offset = EXT4_GOOD_OLD_INODE_SIZE +
38526873fa0dSEric Sandeen 				EXT4_I(inode)->i_extra_isize;
38536873fa0dSEric Sandeen 		physical += offset;
38546873fa0dSEric Sandeen 		length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
38556873fa0dSEric Sandeen 		flags |= FIEMAP_EXTENT_DATA_INLINE;
3856fd2dd9fbSCurt Wohlgemuth 		brelse(iloc.bh);
38576873fa0dSEric Sandeen 	} else { /* external block */
38586873fa0dSEric Sandeen 		physical = EXT4_I(inode)->i_file_acl << blockbits;
38596873fa0dSEric Sandeen 		length = inode->i_sb->s_blocksize;
38606873fa0dSEric Sandeen 	}
38616873fa0dSEric Sandeen 
38626873fa0dSEric Sandeen 	if (physical)
38636873fa0dSEric Sandeen 		error = fiemap_fill_next_extent(fieinfo, 0, physical,
38646873fa0dSEric Sandeen 						length, flags);
38656873fa0dSEric Sandeen 	return (error < 0 ? error : 0);
38666873fa0dSEric Sandeen }
38676873fa0dSEric Sandeen 
38686873fa0dSEric Sandeen int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
38696873fa0dSEric Sandeen 		__u64 start, __u64 len)
38706873fa0dSEric Sandeen {
38716873fa0dSEric Sandeen 	ext4_lblk_t start_blk;
38726873fa0dSEric Sandeen 	int error = 0;
38736873fa0dSEric Sandeen 
38746873fa0dSEric Sandeen 	/* fallback to generic here if not in extents fmt */
387512e9b892SDmitry Monakhov 	if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
38766873fa0dSEric Sandeen 		return generic_block_fiemap(inode, fieinfo, start, len,
38776873fa0dSEric Sandeen 			ext4_get_block);
38786873fa0dSEric Sandeen 
38796873fa0dSEric Sandeen 	if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
38806873fa0dSEric Sandeen 		return -EBADR;
38816873fa0dSEric Sandeen 
38826873fa0dSEric Sandeen 	if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
38836873fa0dSEric Sandeen 		error = ext4_xattr_fiemap(inode, fieinfo);
38846873fa0dSEric Sandeen 	} else {
3885aca92ff6SLeonard Michlmayr 		ext4_lblk_t len_blks;
3886aca92ff6SLeonard Michlmayr 		__u64 last_blk;
3887aca92ff6SLeonard Michlmayr 
38886873fa0dSEric Sandeen 		start_blk = start >> inode->i_sb->s_blocksize_bits;
3889aca92ff6SLeonard Michlmayr 		last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
3890aca92ff6SLeonard Michlmayr 		if (last_blk >= EXT_MAX_BLOCK)
3891aca92ff6SLeonard Michlmayr 			last_blk = EXT_MAX_BLOCK-1;
3892aca92ff6SLeonard Michlmayr 		len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
38936873fa0dSEric Sandeen 
38946873fa0dSEric Sandeen 		/*
38956873fa0dSEric Sandeen 		 * Walk the extent tree gathering extent information.
38966873fa0dSEric Sandeen 		 * ext4_ext_fiemap_cb will push extents back to user.
38976873fa0dSEric Sandeen 		 */
38986873fa0dSEric Sandeen 		error = ext4_ext_walk_space(inode, start_blk, len_blks,
38996873fa0dSEric Sandeen 					  ext4_ext_fiemap_cb, fieinfo);
39006873fa0dSEric Sandeen 	}
39016873fa0dSEric Sandeen 
39026873fa0dSEric Sandeen 	return error;
39036873fa0dSEric Sandeen }
39046873fa0dSEric Sandeen 
3905