xref: /openbmc/linux/fs/ext4/extents.c (revision cf108bca465dde0c015f32dd453b99457d31c7c7)
1a86c6181SAlex Tomas /*
2a86c6181SAlex Tomas  * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
3a86c6181SAlex Tomas  * Written by Alex Tomas <alex@clusterfs.com>
4a86c6181SAlex Tomas  *
5a86c6181SAlex Tomas  * Architecture independence:
6a86c6181SAlex Tomas  *   Copyright (c) 2005, Bull S.A.
7a86c6181SAlex Tomas  *   Written by Pierre Peiffer <pierre.peiffer@bull.net>
8a86c6181SAlex Tomas  *
9a86c6181SAlex Tomas  * This program is free software; you can redistribute it and/or modify
10a86c6181SAlex Tomas  * it under the terms of the GNU General Public License version 2 as
11a86c6181SAlex Tomas  * published by the Free Software Foundation.
12a86c6181SAlex Tomas  *
13a86c6181SAlex Tomas  * This program is distributed in the hope that it will be useful,
14a86c6181SAlex Tomas  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15a86c6181SAlex Tomas  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16a86c6181SAlex Tomas  * GNU General Public License for more details.
17a86c6181SAlex Tomas  *
18a86c6181SAlex Tomas  * You should have received a copy of the GNU General Public Licens
19a86c6181SAlex Tomas  * along with this program; if not, write to the Free Software
20a86c6181SAlex Tomas  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-
21a86c6181SAlex Tomas  */
22a86c6181SAlex Tomas 
23a86c6181SAlex Tomas /*
24a86c6181SAlex Tomas  * Extents support for EXT4
25a86c6181SAlex Tomas  *
26a86c6181SAlex Tomas  * TODO:
27a86c6181SAlex Tomas  *   - ext4*_error() should be used in some situations
28a86c6181SAlex Tomas  *   - analyze all BUG()/BUG_ON(), use -EIO where appropriate
29a86c6181SAlex Tomas  *   - smart tree reduction
30a86c6181SAlex Tomas  */
31a86c6181SAlex Tomas 
32a86c6181SAlex Tomas #include <linux/module.h>
33a86c6181SAlex Tomas #include <linux/fs.h>
34a86c6181SAlex Tomas #include <linux/time.h>
35cd02ff0bSMingming Cao #include <linux/jbd2.h>
36a86c6181SAlex Tomas #include <linux/highuid.h>
37a86c6181SAlex Tomas #include <linux/pagemap.h>
38a86c6181SAlex Tomas #include <linux/quotaops.h>
39a86c6181SAlex Tomas #include <linux/string.h>
40a86c6181SAlex Tomas #include <linux/slab.h>
41a2df2a63SAmit Arora #include <linux/falloc.h>
42a86c6181SAlex Tomas #include <asm/uaccess.h>
433dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
443dcf5451SChristoph Hellwig #include "ext4_extents.h"
45a86c6181SAlex Tomas 
46a86c6181SAlex Tomas 
47d0d856e8SRandy Dunlap /*
48d0d856e8SRandy Dunlap  * ext_pblock:
49d0d856e8SRandy Dunlap  * combine low and high parts of physical block number into ext4_fsblk_t
50d0d856e8SRandy Dunlap  */
5109b88252SAvantika Mathur static ext4_fsblk_t ext_pblock(struct ext4_extent *ex)
52f65e6fbaSAlex Tomas {
53f65e6fbaSAlex Tomas 	ext4_fsblk_t block;
54f65e6fbaSAlex Tomas 
55b377611dSAneesh Kumar K.V 	block = le32_to_cpu(ex->ee_start_lo);
56f65e6fbaSAlex Tomas 	block |= ((ext4_fsblk_t) le16_to_cpu(ex->ee_start_hi) << 31) << 1;
57f65e6fbaSAlex Tomas 	return block;
58f65e6fbaSAlex Tomas }
59f65e6fbaSAlex Tomas 
60d0d856e8SRandy Dunlap /*
61d0d856e8SRandy Dunlap  * idx_pblock:
62d0d856e8SRandy Dunlap  * combine low and high parts of a leaf physical block number into ext4_fsblk_t
63d0d856e8SRandy Dunlap  */
64c14c6fd5SAneesh Kumar K.V ext4_fsblk_t idx_pblock(struct ext4_extent_idx *ix)
65f65e6fbaSAlex Tomas {
66f65e6fbaSAlex Tomas 	ext4_fsblk_t block;
67f65e6fbaSAlex Tomas 
68d8dd0b45SAneesh Kumar K.V 	block = le32_to_cpu(ix->ei_leaf_lo);
69f65e6fbaSAlex Tomas 	block |= ((ext4_fsblk_t) le16_to_cpu(ix->ei_leaf_hi) << 31) << 1;
70f65e6fbaSAlex Tomas 	return block;
71f65e6fbaSAlex Tomas }
72f65e6fbaSAlex Tomas 
73d0d856e8SRandy Dunlap /*
74d0d856e8SRandy Dunlap  * ext4_ext_store_pblock:
75d0d856e8SRandy Dunlap  * stores a large physical block number into an extent struct,
76d0d856e8SRandy Dunlap  * breaking it into parts
77d0d856e8SRandy Dunlap  */
78c14c6fd5SAneesh Kumar K.V void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb)
79f65e6fbaSAlex Tomas {
80b377611dSAneesh Kumar K.V 	ex->ee_start_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff));
81f65e6fbaSAlex Tomas 	ex->ee_start_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
82f65e6fbaSAlex Tomas }
83f65e6fbaSAlex Tomas 
84d0d856e8SRandy Dunlap /*
85d0d856e8SRandy Dunlap  * ext4_idx_store_pblock:
86d0d856e8SRandy Dunlap  * stores a large physical block number into an index struct,
87d0d856e8SRandy Dunlap  * breaking it into parts
88d0d856e8SRandy Dunlap  */
8909b88252SAvantika Mathur static void ext4_idx_store_pblock(struct ext4_extent_idx *ix, ext4_fsblk_t pb)
90f65e6fbaSAlex Tomas {
91d8dd0b45SAneesh Kumar K.V 	ix->ei_leaf_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff));
92f65e6fbaSAlex Tomas 	ix->ei_leaf_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
93f65e6fbaSAlex Tomas }
94f65e6fbaSAlex Tomas 
959102e4faSShen Feng static int ext4_ext_journal_restart(handle_t *handle, int needed)
96a86c6181SAlex Tomas {
97a86c6181SAlex Tomas 	int err;
98a86c6181SAlex Tomas 
99a86c6181SAlex Tomas 	if (handle->h_buffer_credits > needed)
1009102e4faSShen Feng 		return 0;
1019102e4faSShen Feng 	err = ext4_journal_extend(handle, needed);
1029102e4faSShen Feng 	if (err)
1039102e4faSShen Feng 		return err;
1049102e4faSShen Feng 	return ext4_journal_restart(handle, needed);
105a86c6181SAlex Tomas }
106a86c6181SAlex Tomas 
107a86c6181SAlex Tomas /*
108a86c6181SAlex Tomas  * could return:
109a86c6181SAlex Tomas  *  - EROFS
110a86c6181SAlex Tomas  *  - ENOMEM
111a86c6181SAlex Tomas  */
112a86c6181SAlex Tomas static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
113a86c6181SAlex Tomas 				struct ext4_ext_path *path)
114a86c6181SAlex Tomas {
115a86c6181SAlex Tomas 	if (path->p_bh) {
116a86c6181SAlex Tomas 		/* path points to block */
117a86c6181SAlex Tomas 		return ext4_journal_get_write_access(handle, path->p_bh);
118a86c6181SAlex Tomas 	}
119a86c6181SAlex Tomas 	/* path points to leaf/index in inode body */
120a86c6181SAlex Tomas 	/* we use in-core data, no need to protect them */
121a86c6181SAlex Tomas 	return 0;
122a86c6181SAlex Tomas }
123a86c6181SAlex Tomas 
124a86c6181SAlex Tomas /*
125a86c6181SAlex Tomas  * could return:
126a86c6181SAlex Tomas  *  - EROFS
127a86c6181SAlex Tomas  *  - ENOMEM
128a86c6181SAlex Tomas  *  - EIO
129a86c6181SAlex Tomas  */
130a86c6181SAlex Tomas static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
131a86c6181SAlex Tomas 				struct ext4_ext_path *path)
132a86c6181SAlex Tomas {
133a86c6181SAlex Tomas 	int err;
134a86c6181SAlex Tomas 	if (path->p_bh) {
135a86c6181SAlex Tomas 		/* path points to block */
136a86c6181SAlex Tomas 		err = ext4_journal_dirty_metadata(handle, path->p_bh);
137a86c6181SAlex Tomas 	} else {
138a86c6181SAlex Tomas 		/* path points to leaf/index in inode body */
139a86c6181SAlex Tomas 		err = ext4_mark_inode_dirty(handle, inode);
140a86c6181SAlex Tomas 	}
141a86c6181SAlex Tomas 	return err;
142a86c6181SAlex Tomas }
143a86c6181SAlex Tomas 
144f65e6fbaSAlex Tomas static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
145a86c6181SAlex Tomas 			      struct ext4_ext_path *path,
146725d26d3SAneesh Kumar K.V 			      ext4_lblk_t block)
147a86c6181SAlex Tomas {
148a86c6181SAlex Tomas 	struct ext4_inode_info *ei = EXT4_I(inode);
149f65e6fbaSAlex Tomas 	ext4_fsblk_t bg_start;
15074d3487fSValerie Clement 	ext4_fsblk_t last_block;
151f65e6fbaSAlex Tomas 	ext4_grpblk_t colour;
152a86c6181SAlex Tomas 	int depth;
153a86c6181SAlex Tomas 
154a86c6181SAlex Tomas 	if (path) {
155a86c6181SAlex Tomas 		struct ext4_extent *ex;
156a86c6181SAlex Tomas 		depth = path->p_depth;
157a86c6181SAlex Tomas 
158a86c6181SAlex Tomas 		/* try to predict block placement */
1597e028976SAvantika Mathur 		ex = path[depth].p_ext;
1607e028976SAvantika Mathur 		if (ex)
161f65e6fbaSAlex Tomas 			return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block));
162a86c6181SAlex Tomas 
163d0d856e8SRandy Dunlap 		/* it looks like index is empty;
164d0d856e8SRandy Dunlap 		 * try to find starting block from index itself */
165a86c6181SAlex Tomas 		if (path[depth].p_bh)
166a86c6181SAlex Tomas 			return path[depth].p_bh->b_blocknr;
167a86c6181SAlex Tomas 	}
168a86c6181SAlex Tomas 
169a86c6181SAlex Tomas 	/* OK. use inode's group */
170a86c6181SAlex Tomas 	bg_start = (ei->i_block_group * EXT4_BLOCKS_PER_GROUP(inode->i_sb)) +
171a86c6181SAlex Tomas 		le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_first_data_block);
17274d3487fSValerie Clement 	last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
17374d3487fSValerie Clement 
17474d3487fSValerie Clement 	if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
175a86c6181SAlex Tomas 		colour = (current->pid % 16) *
176a86c6181SAlex Tomas 			(EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
17774d3487fSValerie Clement 	else
17874d3487fSValerie Clement 		colour = (current->pid % 16) * ((last_block - bg_start) / 16);
179a86c6181SAlex Tomas 	return bg_start + colour + block;
180a86c6181SAlex Tomas }
181a86c6181SAlex Tomas 
182654b4908SAneesh Kumar K.V /*
183654b4908SAneesh Kumar K.V  * Allocation for a meta data block
184654b4908SAneesh Kumar K.V  */
185f65e6fbaSAlex Tomas static ext4_fsblk_t
186654b4908SAneesh Kumar K.V ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
187a86c6181SAlex Tomas 			struct ext4_ext_path *path,
188a86c6181SAlex Tomas 			struct ext4_extent *ex, int *err)
189a86c6181SAlex Tomas {
190f65e6fbaSAlex Tomas 	ext4_fsblk_t goal, newblock;
191a86c6181SAlex Tomas 
192a86c6181SAlex Tomas 	goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
1937061eba7SAneesh Kumar K.V 	newblock = ext4_new_meta_block(handle, inode, goal, err);
194a86c6181SAlex Tomas 	return newblock;
195a86c6181SAlex Tomas }
196a86c6181SAlex Tomas 
19709b88252SAvantika Mathur static int ext4_ext_space_block(struct inode *inode)
198a86c6181SAlex Tomas {
199a86c6181SAlex Tomas 	int size;
200a86c6181SAlex Tomas 
201a86c6181SAlex Tomas 	size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
202a86c6181SAlex Tomas 			/ sizeof(struct ext4_extent);
203bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST
204a86c6181SAlex Tomas 	if (size > 6)
205a86c6181SAlex Tomas 		size = 6;
206a86c6181SAlex Tomas #endif
207a86c6181SAlex Tomas 	return size;
208a86c6181SAlex Tomas }
209a86c6181SAlex Tomas 
21009b88252SAvantika Mathur static int ext4_ext_space_block_idx(struct inode *inode)
211a86c6181SAlex Tomas {
212a86c6181SAlex Tomas 	int size;
213a86c6181SAlex Tomas 
214a86c6181SAlex Tomas 	size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
215a86c6181SAlex Tomas 			/ sizeof(struct ext4_extent_idx);
216bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST
217a86c6181SAlex Tomas 	if (size > 5)
218a86c6181SAlex Tomas 		size = 5;
219a86c6181SAlex Tomas #endif
220a86c6181SAlex Tomas 	return size;
221a86c6181SAlex Tomas }
222a86c6181SAlex Tomas 
22309b88252SAvantika Mathur static int ext4_ext_space_root(struct inode *inode)
224a86c6181SAlex Tomas {
225a86c6181SAlex Tomas 	int size;
226a86c6181SAlex Tomas 
227a86c6181SAlex Tomas 	size = sizeof(EXT4_I(inode)->i_data);
228a86c6181SAlex Tomas 	size -= sizeof(struct ext4_extent_header);
229a86c6181SAlex Tomas 	size /= sizeof(struct ext4_extent);
230bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST
231a86c6181SAlex Tomas 	if (size > 3)
232a86c6181SAlex Tomas 		size = 3;
233a86c6181SAlex Tomas #endif
234a86c6181SAlex Tomas 	return size;
235a86c6181SAlex Tomas }
236a86c6181SAlex Tomas 
23709b88252SAvantika Mathur static int ext4_ext_space_root_idx(struct inode *inode)
238a86c6181SAlex Tomas {
239a86c6181SAlex Tomas 	int size;
240a86c6181SAlex Tomas 
241a86c6181SAlex Tomas 	size = sizeof(EXT4_I(inode)->i_data);
242a86c6181SAlex Tomas 	size -= sizeof(struct ext4_extent_header);
243a86c6181SAlex Tomas 	size /= sizeof(struct ext4_extent_idx);
244bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST
245a86c6181SAlex Tomas 	if (size > 4)
246a86c6181SAlex Tomas 		size = 4;
247a86c6181SAlex Tomas #endif
248a86c6181SAlex Tomas 	return size;
249a86c6181SAlex Tomas }
250a86c6181SAlex Tomas 
251c29c0ae7SAlex Tomas static int
252c29c0ae7SAlex Tomas ext4_ext_max_entries(struct inode *inode, int depth)
253c29c0ae7SAlex Tomas {
254c29c0ae7SAlex Tomas 	int max;
255c29c0ae7SAlex Tomas 
256c29c0ae7SAlex Tomas 	if (depth == ext_depth(inode)) {
257c29c0ae7SAlex Tomas 		if (depth == 0)
258c29c0ae7SAlex Tomas 			max = ext4_ext_space_root(inode);
259c29c0ae7SAlex Tomas 		else
260c29c0ae7SAlex Tomas 			max = ext4_ext_space_root_idx(inode);
261c29c0ae7SAlex Tomas 	} else {
262c29c0ae7SAlex Tomas 		if (depth == 0)
263c29c0ae7SAlex Tomas 			max = ext4_ext_space_block(inode);
264c29c0ae7SAlex Tomas 		else
265c29c0ae7SAlex Tomas 			max = ext4_ext_space_block_idx(inode);
266c29c0ae7SAlex Tomas 	}
267c29c0ae7SAlex Tomas 
268c29c0ae7SAlex Tomas 	return max;
269c29c0ae7SAlex Tomas }
270c29c0ae7SAlex Tomas 
271c29c0ae7SAlex Tomas static int __ext4_ext_check_header(const char *function, struct inode *inode,
272c29c0ae7SAlex Tomas 					struct ext4_extent_header *eh,
273c29c0ae7SAlex Tomas 					int depth)
274c29c0ae7SAlex Tomas {
275c29c0ae7SAlex Tomas 	const char *error_msg;
276c29c0ae7SAlex Tomas 	int max = 0;
277c29c0ae7SAlex Tomas 
278c29c0ae7SAlex Tomas 	if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
279c29c0ae7SAlex Tomas 		error_msg = "invalid magic";
280c29c0ae7SAlex Tomas 		goto corrupted;
281c29c0ae7SAlex Tomas 	}
282c29c0ae7SAlex Tomas 	if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
283c29c0ae7SAlex Tomas 		error_msg = "unexpected eh_depth";
284c29c0ae7SAlex Tomas 		goto corrupted;
285c29c0ae7SAlex Tomas 	}
286c29c0ae7SAlex Tomas 	if (unlikely(eh->eh_max == 0)) {
287c29c0ae7SAlex Tomas 		error_msg = "invalid eh_max";
288c29c0ae7SAlex Tomas 		goto corrupted;
289c29c0ae7SAlex Tomas 	}
290c29c0ae7SAlex Tomas 	max = ext4_ext_max_entries(inode, depth);
291c29c0ae7SAlex Tomas 	if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
292c29c0ae7SAlex Tomas 		error_msg = "too large eh_max";
293c29c0ae7SAlex Tomas 		goto corrupted;
294c29c0ae7SAlex Tomas 	}
295c29c0ae7SAlex Tomas 	if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
296c29c0ae7SAlex Tomas 		error_msg = "invalid eh_entries";
297c29c0ae7SAlex Tomas 		goto corrupted;
298c29c0ae7SAlex Tomas 	}
299c29c0ae7SAlex Tomas 	return 0;
300c29c0ae7SAlex Tomas 
301c29c0ae7SAlex Tomas corrupted:
302c29c0ae7SAlex Tomas 	ext4_error(inode->i_sb, function,
303c29c0ae7SAlex Tomas 			"bad header in inode #%lu: %s - magic %x, "
304c29c0ae7SAlex Tomas 			"entries %u, max %u(%u), depth %u(%u)",
305c29c0ae7SAlex Tomas 			inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic),
306c29c0ae7SAlex Tomas 			le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
307c29c0ae7SAlex Tomas 			max, le16_to_cpu(eh->eh_depth), depth);
308c29c0ae7SAlex Tomas 
309c29c0ae7SAlex Tomas 	return -EIO;
310c29c0ae7SAlex Tomas }
311c29c0ae7SAlex Tomas 
312c29c0ae7SAlex Tomas #define ext4_ext_check_header(inode, eh, depth)	\
31346e665e9SHarvey Harrison 	__ext4_ext_check_header(__func__, inode, eh, depth)
314c29c0ae7SAlex Tomas 
315a86c6181SAlex Tomas #ifdef EXT_DEBUG
316a86c6181SAlex Tomas static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
317a86c6181SAlex Tomas {
318a86c6181SAlex Tomas 	int k, l = path->p_depth;
319a86c6181SAlex Tomas 
320a86c6181SAlex Tomas 	ext_debug("path:");
321a86c6181SAlex Tomas 	for (k = 0; k <= l; k++, path++) {
322a86c6181SAlex Tomas 		if (path->p_idx) {
3232ae02107SMingming Cao 		  ext_debug("  %d->%llu", le32_to_cpu(path->p_idx->ei_block),
324f65e6fbaSAlex Tomas 			    idx_pblock(path->p_idx));
325a86c6181SAlex Tomas 		} else if (path->p_ext) {
3262ae02107SMingming Cao 			ext_debug("  %d:%d:%llu ",
327a86c6181SAlex Tomas 				  le32_to_cpu(path->p_ext->ee_block),
328a2df2a63SAmit Arora 				  ext4_ext_get_actual_len(path->p_ext),
329f65e6fbaSAlex Tomas 				  ext_pblock(path->p_ext));
330a86c6181SAlex Tomas 		} else
331a86c6181SAlex Tomas 			ext_debug("  []");
332a86c6181SAlex Tomas 	}
333a86c6181SAlex Tomas 	ext_debug("\n");
334a86c6181SAlex Tomas }
335a86c6181SAlex Tomas 
336a86c6181SAlex Tomas static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
337a86c6181SAlex Tomas {
338a86c6181SAlex Tomas 	int depth = ext_depth(inode);
339a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
340a86c6181SAlex Tomas 	struct ext4_extent *ex;
341a86c6181SAlex Tomas 	int i;
342a86c6181SAlex Tomas 
343a86c6181SAlex Tomas 	if (!path)
344a86c6181SAlex Tomas 		return;
345a86c6181SAlex Tomas 
346a86c6181SAlex Tomas 	eh = path[depth].p_hdr;
347a86c6181SAlex Tomas 	ex = EXT_FIRST_EXTENT(eh);
348a86c6181SAlex Tomas 
349a86c6181SAlex Tomas 	for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
3502ae02107SMingming Cao 		ext_debug("%d:%d:%llu ", le32_to_cpu(ex->ee_block),
351a2df2a63SAmit Arora 			  ext4_ext_get_actual_len(ex), ext_pblock(ex));
352a86c6181SAlex Tomas 	}
353a86c6181SAlex Tomas 	ext_debug("\n");
354a86c6181SAlex Tomas }
355a86c6181SAlex Tomas #else
356a86c6181SAlex Tomas #define ext4_ext_show_path(inode,path)
357a86c6181SAlex Tomas #define ext4_ext_show_leaf(inode,path)
358a86c6181SAlex Tomas #endif
359a86c6181SAlex Tomas 
360b35905c1SAneesh Kumar K.V void ext4_ext_drop_refs(struct ext4_ext_path *path)
361a86c6181SAlex Tomas {
362a86c6181SAlex Tomas 	int depth = path->p_depth;
363a86c6181SAlex Tomas 	int i;
364a86c6181SAlex Tomas 
365a86c6181SAlex Tomas 	for (i = 0; i <= depth; i++, path++)
366a86c6181SAlex Tomas 		if (path->p_bh) {
367a86c6181SAlex Tomas 			brelse(path->p_bh);
368a86c6181SAlex Tomas 			path->p_bh = NULL;
369a86c6181SAlex Tomas 		}
370a86c6181SAlex Tomas }
371a86c6181SAlex Tomas 
372a86c6181SAlex Tomas /*
373d0d856e8SRandy Dunlap  * ext4_ext_binsearch_idx:
374d0d856e8SRandy Dunlap  * binary search for the closest index of the given block
375c29c0ae7SAlex Tomas  * the header must be checked before calling this
376a86c6181SAlex Tomas  */
377a86c6181SAlex Tomas static void
378725d26d3SAneesh Kumar K.V ext4_ext_binsearch_idx(struct inode *inode,
379725d26d3SAneesh Kumar K.V 			struct ext4_ext_path *path, ext4_lblk_t block)
380a86c6181SAlex Tomas {
381a86c6181SAlex Tomas 	struct ext4_extent_header *eh = path->p_hdr;
382a86c6181SAlex Tomas 	struct ext4_extent_idx *r, *l, *m;
383a86c6181SAlex Tomas 
384a86c6181SAlex Tomas 
385bba90743SEric Sandeen 	ext_debug("binsearch for %u(idx):  ", block);
386a86c6181SAlex Tomas 
387a86c6181SAlex Tomas 	l = EXT_FIRST_INDEX(eh) + 1;
388e9f410b1SDmitry Monakhov 	r = EXT_LAST_INDEX(eh);
389a86c6181SAlex Tomas 	while (l <= r) {
390a86c6181SAlex Tomas 		m = l + (r - l) / 2;
391a86c6181SAlex Tomas 		if (block < le32_to_cpu(m->ei_block))
392a86c6181SAlex Tomas 			r = m - 1;
393a86c6181SAlex Tomas 		else
394a86c6181SAlex Tomas 			l = m + 1;
39526d535edSDmitry Monakhov 		ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
39626d535edSDmitry Monakhov 				m, le32_to_cpu(m->ei_block),
39726d535edSDmitry Monakhov 				r, le32_to_cpu(r->ei_block));
398a86c6181SAlex Tomas 	}
399a86c6181SAlex Tomas 
400a86c6181SAlex Tomas 	path->p_idx = l - 1;
401f65e6fbaSAlex Tomas 	ext_debug("  -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
40226d535edSDmitry Monakhov 		  idx_pblock(path->p_idx));
403a86c6181SAlex Tomas 
404a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH
405a86c6181SAlex Tomas 	{
406a86c6181SAlex Tomas 		struct ext4_extent_idx *chix, *ix;
407a86c6181SAlex Tomas 		int k;
408a86c6181SAlex Tomas 
409a86c6181SAlex Tomas 		chix = ix = EXT_FIRST_INDEX(eh);
410a86c6181SAlex Tomas 		for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
411a86c6181SAlex Tomas 		  if (k != 0 &&
412a86c6181SAlex Tomas 		      le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
413a86c6181SAlex Tomas 				printk("k=%d, ix=0x%p, first=0x%p\n", k,
414a86c6181SAlex Tomas 					ix, EXT_FIRST_INDEX(eh));
415a86c6181SAlex Tomas 				printk("%u <= %u\n",
416a86c6181SAlex Tomas 				       le32_to_cpu(ix->ei_block),
417a86c6181SAlex Tomas 				       le32_to_cpu(ix[-1].ei_block));
418a86c6181SAlex Tomas 			}
419a86c6181SAlex Tomas 			BUG_ON(k && le32_to_cpu(ix->ei_block)
420a86c6181SAlex Tomas 					   <= le32_to_cpu(ix[-1].ei_block));
421a86c6181SAlex Tomas 			if (block < le32_to_cpu(ix->ei_block))
422a86c6181SAlex Tomas 				break;
423a86c6181SAlex Tomas 			chix = ix;
424a86c6181SAlex Tomas 		}
425a86c6181SAlex Tomas 		BUG_ON(chix != path->p_idx);
426a86c6181SAlex Tomas 	}
427a86c6181SAlex Tomas #endif
428a86c6181SAlex Tomas 
429a86c6181SAlex Tomas }
430a86c6181SAlex Tomas 
431a86c6181SAlex Tomas /*
432d0d856e8SRandy Dunlap  * ext4_ext_binsearch:
433d0d856e8SRandy Dunlap  * binary search for closest extent of the given block
434c29c0ae7SAlex Tomas  * the header must be checked before calling this
435a86c6181SAlex Tomas  */
436a86c6181SAlex Tomas static void
437725d26d3SAneesh Kumar K.V ext4_ext_binsearch(struct inode *inode,
438725d26d3SAneesh Kumar K.V 		struct ext4_ext_path *path, ext4_lblk_t block)
439a86c6181SAlex Tomas {
440a86c6181SAlex Tomas 	struct ext4_extent_header *eh = path->p_hdr;
441a86c6181SAlex Tomas 	struct ext4_extent *r, *l, *m;
442a86c6181SAlex Tomas 
443a86c6181SAlex Tomas 	if (eh->eh_entries == 0) {
444a86c6181SAlex Tomas 		/*
445d0d856e8SRandy Dunlap 		 * this leaf is empty:
446a86c6181SAlex Tomas 		 * we get such a leaf in split/add case
447a86c6181SAlex Tomas 		 */
448a86c6181SAlex Tomas 		return;
449a86c6181SAlex Tomas 	}
450a86c6181SAlex Tomas 
451bba90743SEric Sandeen 	ext_debug("binsearch for %u:  ", block);
452a86c6181SAlex Tomas 
453a86c6181SAlex Tomas 	l = EXT_FIRST_EXTENT(eh) + 1;
454e9f410b1SDmitry Monakhov 	r = EXT_LAST_EXTENT(eh);
455a86c6181SAlex Tomas 
456a86c6181SAlex Tomas 	while (l <= r) {
457a86c6181SAlex Tomas 		m = l + (r - l) / 2;
458a86c6181SAlex Tomas 		if (block < le32_to_cpu(m->ee_block))
459a86c6181SAlex Tomas 			r = m - 1;
460a86c6181SAlex Tomas 		else
461a86c6181SAlex Tomas 			l = m + 1;
46226d535edSDmitry Monakhov 		ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
46326d535edSDmitry Monakhov 				m, le32_to_cpu(m->ee_block),
46426d535edSDmitry Monakhov 				r, le32_to_cpu(r->ee_block));
465a86c6181SAlex Tomas 	}
466a86c6181SAlex Tomas 
467a86c6181SAlex Tomas 	path->p_ext = l - 1;
4682ae02107SMingming Cao 	ext_debug("  -> %d:%llu:%d ",
469a86c6181SAlex Tomas 			le32_to_cpu(path->p_ext->ee_block),
470f65e6fbaSAlex Tomas 			ext_pblock(path->p_ext),
471a2df2a63SAmit Arora 			ext4_ext_get_actual_len(path->p_ext));
472a86c6181SAlex Tomas 
473a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH
474a86c6181SAlex Tomas 	{
475a86c6181SAlex Tomas 		struct ext4_extent *chex, *ex;
476a86c6181SAlex Tomas 		int k;
477a86c6181SAlex Tomas 
478a86c6181SAlex Tomas 		chex = ex = EXT_FIRST_EXTENT(eh);
479a86c6181SAlex Tomas 		for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
480a86c6181SAlex Tomas 			BUG_ON(k && le32_to_cpu(ex->ee_block)
481a86c6181SAlex Tomas 					  <= le32_to_cpu(ex[-1].ee_block));
482a86c6181SAlex Tomas 			if (block < le32_to_cpu(ex->ee_block))
483a86c6181SAlex Tomas 				break;
484a86c6181SAlex Tomas 			chex = ex;
485a86c6181SAlex Tomas 		}
486a86c6181SAlex Tomas 		BUG_ON(chex != path->p_ext);
487a86c6181SAlex Tomas 	}
488a86c6181SAlex Tomas #endif
489a86c6181SAlex Tomas 
490a86c6181SAlex Tomas }
491a86c6181SAlex Tomas 
492a86c6181SAlex Tomas int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
493a86c6181SAlex Tomas {
494a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
495a86c6181SAlex Tomas 
496a86c6181SAlex Tomas 	eh = ext_inode_hdr(inode);
497a86c6181SAlex Tomas 	eh->eh_depth = 0;
498a86c6181SAlex Tomas 	eh->eh_entries = 0;
499a86c6181SAlex Tomas 	eh->eh_magic = EXT4_EXT_MAGIC;
500a86c6181SAlex Tomas 	eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode));
501a86c6181SAlex Tomas 	ext4_mark_inode_dirty(handle, inode);
502a86c6181SAlex Tomas 	ext4_ext_invalidate_cache(inode);
503a86c6181SAlex Tomas 	return 0;
504a86c6181SAlex Tomas }
505a86c6181SAlex Tomas 
506a86c6181SAlex Tomas struct ext4_ext_path *
507725d26d3SAneesh Kumar K.V ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
508725d26d3SAneesh Kumar K.V 					struct ext4_ext_path *path)
509a86c6181SAlex Tomas {
510a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
511a86c6181SAlex Tomas 	struct buffer_head *bh;
512a86c6181SAlex Tomas 	short int depth, i, ppos = 0, alloc = 0;
513a86c6181SAlex Tomas 
514a86c6181SAlex Tomas 	eh = ext_inode_hdr(inode);
515c29c0ae7SAlex Tomas 	depth = ext_depth(inode);
516c29c0ae7SAlex Tomas 	if (ext4_ext_check_header(inode, eh, depth))
517a86c6181SAlex Tomas 		return ERR_PTR(-EIO);
518a86c6181SAlex Tomas 
519a86c6181SAlex Tomas 
520a86c6181SAlex Tomas 	/* account possible depth increase */
521a86c6181SAlex Tomas 	if (!path) {
5225d4958f9SAvantika Mathur 		path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
523a86c6181SAlex Tomas 				GFP_NOFS);
524a86c6181SAlex Tomas 		if (!path)
525a86c6181SAlex Tomas 			return ERR_PTR(-ENOMEM);
526a86c6181SAlex Tomas 		alloc = 1;
527a86c6181SAlex Tomas 	}
528a86c6181SAlex Tomas 	path[0].p_hdr = eh;
5291973adcbSShen Feng 	path[0].p_bh = NULL;
530a86c6181SAlex Tomas 
531c29c0ae7SAlex Tomas 	i = depth;
532a86c6181SAlex Tomas 	/* walk through the tree */
533a86c6181SAlex Tomas 	while (i) {
534a86c6181SAlex Tomas 		ext_debug("depth %d: num %d, max %d\n",
535a86c6181SAlex Tomas 			  ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
536c29c0ae7SAlex Tomas 
537a86c6181SAlex Tomas 		ext4_ext_binsearch_idx(inode, path + ppos, block);
538f65e6fbaSAlex Tomas 		path[ppos].p_block = idx_pblock(path[ppos].p_idx);
539a86c6181SAlex Tomas 		path[ppos].p_depth = i;
540a86c6181SAlex Tomas 		path[ppos].p_ext = NULL;
541a86c6181SAlex Tomas 
542a86c6181SAlex Tomas 		bh = sb_bread(inode->i_sb, path[ppos].p_block);
543a86c6181SAlex Tomas 		if (!bh)
544a86c6181SAlex Tomas 			goto err;
545a86c6181SAlex Tomas 
546a86c6181SAlex Tomas 		eh = ext_block_hdr(bh);
547a86c6181SAlex Tomas 		ppos++;
548a86c6181SAlex Tomas 		BUG_ON(ppos > depth);
549a86c6181SAlex Tomas 		path[ppos].p_bh = bh;
550a86c6181SAlex Tomas 		path[ppos].p_hdr = eh;
551a86c6181SAlex Tomas 		i--;
552a86c6181SAlex Tomas 
553c29c0ae7SAlex Tomas 		if (ext4_ext_check_header(inode, eh, i))
554a86c6181SAlex Tomas 			goto err;
555a86c6181SAlex Tomas 	}
556a86c6181SAlex Tomas 
557a86c6181SAlex Tomas 	path[ppos].p_depth = i;
558a86c6181SAlex Tomas 	path[ppos].p_ext = NULL;
559a86c6181SAlex Tomas 	path[ppos].p_idx = NULL;
560a86c6181SAlex Tomas 
561a86c6181SAlex Tomas 	/* find extent */
562a86c6181SAlex Tomas 	ext4_ext_binsearch(inode, path + ppos, block);
5631973adcbSShen Feng 	/* if not an empty leaf */
5641973adcbSShen Feng 	if (path[ppos].p_ext)
5651973adcbSShen Feng 		path[ppos].p_block = ext_pblock(path[ppos].p_ext);
566a86c6181SAlex Tomas 
567a86c6181SAlex Tomas 	ext4_ext_show_path(inode, path);
568a86c6181SAlex Tomas 
569a86c6181SAlex Tomas 	return path;
570a86c6181SAlex Tomas 
571a86c6181SAlex Tomas err:
572a86c6181SAlex Tomas 	ext4_ext_drop_refs(path);
573a86c6181SAlex Tomas 	if (alloc)
574a86c6181SAlex Tomas 		kfree(path);
575a86c6181SAlex Tomas 	return ERR_PTR(-EIO);
576a86c6181SAlex Tomas }
577a86c6181SAlex Tomas 
578a86c6181SAlex Tomas /*
579d0d856e8SRandy Dunlap  * ext4_ext_insert_index:
580d0d856e8SRandy Dunlap  * insert new index [@logical;@ptr] into the block at @curp;
581d0d856e8SRandy Dunlap  * check where to insert: before @curp or after @curp
582a86c6181SAlex Tomas  */
583a86c6181SAlex Tomas static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
584a86c6181SAlex Tomas 				struct ext4_ext_path *curp,
585f65e6fbaSAlex Tomas 				int logical, ext4_fsblk_t ptr)
586a86c6181SAlex Tomas {
587a86c6181SAlex Tomas 	struct ext4_extent_idx *ix;
588a86c6181SAlex Tomas 	int len, err;
589a86c6181SAlex Tomas 
5907e028976SAvantika Mathur 	err = ext4_ext_get_access(handle, inode, curp);
5917e028976SAvantika Mathur 	if (err)
592a86c6181SAlex Tomas 		return err;
593a86c6181SAlex Tomas 
594a86c6181SAlex Tomas 	BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block));
595a86c6181SAlex Tomas 	len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
596a86c6181SAlex Tomas 	if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
597a86c6181SAlex Tomas 		/* insert after */
598a86c6181SAlex Tomas 		if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
599a86c6181SAlex Tomas 			len = (len - 1) * sizeof(struct ext4_extent_idx);
600a86c6181SAlex Tomas 			len = len < 0 ? 0 : len;
60126d535edSDmitry Monakhov 			ext_debug("insert new index %d after: %llu. "
602a86c6181SAlex Tomas 					"move %d from 0x%p to 0x%p\n",
603a86c6181SAlex Tomas 					logical, ptr, len,
604a86c6181SAlex Tomas 					(curp->p_idx + 1), (curp->p_idx + 2));
605a86c6181SAlex Tomas 			memmove(curp->p_idx + 2, curp->p_idx + 1, len);
606a86c6181SAlex Tomas 		}
607a86c6181SAlex Tomas 		ix = curp->p_idx + 1;
608a86c6181SAlex Tomas 	} else {
609a86c6181SAlex Tomas 		/* insert before */
610a86c6181SAlex Tomas 		len = len * sizeof(struct ext4_extent_idx);
611a86c6181SAlex Tomas 		len = len < 0 ? 0 : len;
61226d535edSDmitry Monakhov 		ext_debug("insert new index %d before: %llu. "
613a86c6181SAlex Tomas 				"move %d from 0x%p to 0x%p\n",
614a86c6181SAlex Tomas 				logical, ptr, len,
615a86c6181SAlex Tomas 				curp->p_idx, (curp->p_idx + 1));
616a86c6181SAlex Tomas 		memmove(curp->p_idx + 1, curp->p_idx, len);
617a86c6181SAlex Tomas 		ix = curp->p_idx;
618a86c6181SAlex Tomas 	}
619a86c6181SAlex Tomas 
620a86c6181SAlex Tomas 	ix->ei_block = cpu_to_le32(logical);
621f65e6fbaSAlex Tomas 	ext4_idx_store_pblock(ix, ptr);
622e8546d06SMarcin Slusarz 	le16_add_cpu(&curp->p_hdr->eh_entries, 1);
623a86c6181SAlex Tomas 
624a86c6181SAlex Tomas 	BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries)
625a86c6181SAlex Tomas 			     > le16_to_cpu(curp->p_hdr->eh_max));
626a86c6181SAlex Tomas 	BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr));
627a86c6181SAlex Tomas 
628a86c6181SAlex Tomas 	err = ext4_ext_dirty(handle, inode, curp);
629a86c6181SAlex Tomas 	ext4_std_error(inode->i_sb, err);
630a86c6181SAlex Tomas 
631a86c6181SAlex Tomas 	return err;
632a86c6181SAlex Tomas }
633a86c6181SAlex Tomas 
634a86c6181SAlex Tomas /*
635d0d856e8SRandy Dunlap  * ext4_ext_split:
636d0d856e8SRandy Dunlap  * inserts new subtree into the path, using free index entry
637d0d856e8SRandy Dunlap  * at depth @at:
638a86c6181SAlex Tomas  * - allocates all needed blocks (new leaf and all intermediate index blocks)
639a86c6181SAlex Tomas  * - makes decision where to split
640d0d856e8SRandy Dunlap  * - moves remaining extents and index entries (right to the split point)
641a86c6181SAlex Tomas  *   into the newly allocated blocks
642d0d856e8SRandy Dunlap  * - initializes subtree
643a86c6181SAlex Tomas  */
644a86c6181SAlex Tomas static int ext4_ext_split(handle_t *handle, struct inode *inode,
645a86c6181SAlex Tomas 				struct ext4_ext_path *path,
646a86c6181SAlex Tomas 				struct ext4_extent *newext, int at)
647a86c6181SAlex Tomas {
648a86c6181SAlex Tomas 	struct buffer_head *bh = NULL;
649a86c6181SAlex Tomas 	int depth = ext_depth(inode);
650a86c6181SAlex Tomas 	struct ext4_extent_header *neh;
651a86c6181SAlex Tomas 	struct ext4_extent_idx *fidx;
652a86c6181SAlex Tomas 	struct ext4_extent *ex;
653a86c6181SAlex Tomas 	int i = at, k, m, a;
654f65e6fbaSAlex Tomas 	ext4_fsblk_t newblock, oldblock;
655a86c6181SAlex Tomas 	__le32 border;
656f65e6fbaSAlex Tomas 	ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
657a86c6181SAlex Tomas 	int err = 0;
658a86c6181SAlex Tomas 
659a86c6181SAlex Tomas 	/* make decision: where to split? */
660d0d856e8SRandy Dunlap 	/* FIXME: now decision is simplest: at current extent */
661a86c6181SAlex Tomas 
662d0d856e8SRandy Dunlap 	/* if current leaf will be split, then we should use
663a86c6181SAlex Tomas 	 * border from split point */
664a86c6181SAlex Tomas 	BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr));
665a86c6181SAlex Tomas 	if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
666a86c6181SAlex Tomas 		border = path[depth].p_ext[1].ee_block;
667d0d856e8SRandy Dunlap 		ext_debug("leaf will be split."
668a86c6181SAlex Tomas 				" next leaf starts at %d\n",
669a86c6181SAlex Tomas 				  le32_to_cpu(border));
670a86c6181SAlex Tomas 	} else {
671a86c6181SAlex Tomas 		border = newext->ee_block;
672a86c6181SAlex Tomas 		ext_debug("leaf will be added."
673a86c6181SAlex Tomas 				" next leaf starts at %d\n",
674a86c6181SAlex Tomas 				le32_to_cpu(border));
675a86c6181SAlex Tomas 	}
676a86c6181SAlex Tomas 
677a86c6181SAlex Tomas 	/*
678d0d856e8SRandy Dunlap 	 * If error occurs, then we break processing
679d0d856e8SRandy Dunlap 	 * and mark filesystem read-only. index won't
680a86c6181SAlex Tomas 	 * be inserted and tree will be in consistent
681d0d856e8SRandy Dunlap 	 * state. Next mount will repair buffers too.
682a86c6181SAlex Tomas 	 */
683a86c6181SAlex Tomas 
684a86c6181SAlex Tomas 	/*
685d0d856e8SRandy Dunlap 	 * Get array to track all allocated blocks.
686d0d856e8SRandy Dunlap 	 * We need this to handle errors and free blocks
687d0d856e8SRandy Dunlap 	 * upon them.
688a86c6181SAlex Tomas 	 */
6895d4958f9SAvantika Mathur 	ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
690a86c6181SAlex Tomas 	if (!ablocks)
691a86c6181SAlex Tomas 		return -ENOMEM;
692a86c6181SAlex Tomas 
693a86c6181SAlex Tomas 	/* allocate all needed blocks */
694a86c6181SAlex Tomas 	ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
695a86c6181SAlex Tomas 	for (a = 0; a < depth - at; a++) {
696654b4908SAneesh Kumar K.V 		newblock = ext4_ext_new_meta_block(handle, inode, path,
697654b4908SAneesh Kumar K.V 						   newext, &err);
698a86c6181SAlex Tomas 		if (newblock == 0)
699a86c6181SAlex Tomas 			goto cleanup;
700a86c6181SAlex Tomas 		ablocks[a] = newblock;
701a86c6181SAlex Tomas 	}
702a86c6181SAlex Tomas 
703a86c6181SAlex Tomas 	/* initialize new leaf */
704a86c6181SAlex Tomas 	newblock = ablocks[--a];
705a86c6181SAlex Tomas 	BUG_ON(newblock == 0);
706a86c6181SAlex Tomas 	bh = sb_getblk(inode->i_sb, newblock);
707a86c6181SAlex Tomas 	if (!bh) {
708a86c6181SAlex Tomas 		err = -EIO;
709a86c6181SAlex Tomas 		goto cleanup;
710a86c6181SAlex Tomas 	}
711a86c6181SAlex Tomas 	lock_buffer(bh);
712a86c6181SAlex Tomas 
7137e028976SAvantika Mathur 	err = ext4_journal_get_create_access(handle, bh);
7147e028976SAvantika Mathur 	if (err)
715a86c6181SAlex Tomas 		goto cleanup;
716a86c6181SAlex Tomas 
717a86c6181SAlex Tomas 	neh = ext_block_hdr(bh);
718a86c6181SAlex Tomas 	neh->eh_entries = 0;
719a86c6181SAlex Tomas 	neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
720a86c6181SAlex Tomas 	neh->eh_magic = EXT4_EXT_MAGIC;
721a86c6181SAlex Tomas 	neh->eh_depth = 0;
722a86c6181SAlex Tomas 	ex = EXT_FIRST_EXTENT(neh);
723a86c6181SAlex Tomas 
724d0d856e8SRandy Dunlap 	/* move remainder of path[depth] to the new leaf */
725a86c6181SAlex Tomas 	BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max);
726a86c6181SAlex Tomas 	/* start copy from next extent */
727a86c6181SAlex Tomas 	/* TODO: we could do it by single memmove */
728a86c6181SAlex Tomas 	m = 0;
729a86c6181SAlex Tomas 	path[depth].p_ext++;
730a86c6181SAlex Tomas 	while (path[depth].p_ext <=
731a86c6181SAlex Tomas 			EXT_MAX_EXTENT(path[depth].p_hdr)) {
7322ae02107SMingming Cao 		ext_debug("move %d:%llu:%d in new leaf %llu\n",
733a86c6181SAlex Tomas 				le32_to_cpu(path[depth].p_ext->ee_block),
734f65e6fbaSAlex Tomas 				ext_pblock(path[depth].p_ext),
735a2df2a63SAmit Arora 				ext4_ext_get_actual_len(path[depth].p_ext),
736a86c6181SAlex Tomas 				newblock);
737a86c6181SAlex Tomas 		/*memmove(ex++, path[depth].p_ext++,
738a86c6181SAlex Tomas 				sizeof(struct ext4_extent));
739a86c6181SAlex Tomas 		neh->eh_entries++;*/
740a86c6181SAlex Tomas 		path[depth].p_ext++;
741a86c6181SAlex Tomas 		m++;
742a86c6181SAlex Tomas 	}
743a86c6181SAlex Tomas 	if (m) {
744a86c6181SAlex Tomas 		memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
745e8546d06SMarcin Slusarz 		le16_add_cpu(&neh->eh_entries, m);
746a86c6181SAlex Tomas 	}
747a86c6181SAlex Tomas 
748a86c6181SAlex Tomas 	set_buffer_uptodate(bh);
749a86c6181SAlex Tomas 	unlock_buffer(bh);
750a86c6181SAlex Tomas 
7517e028976SAvantika Mathur 	err = ext4_journal_dirty_metadata(handle, bh);
7527e028976SAvantika Mathur 	if (err)
753a86c6181SAlex Tomas 		goto cleanup;
754a86c6181SAlex Tomas 	brelse(bh);
755a86c6181SAlex Tomas 	bh = NULL;
756a86c6181SAlex Tomas 
757a86c6181SAlex Tomas 	/* correct old leaf */
758a86c6181SAlex Tomas 	if (m) {
7597e028976SAvantika Mathur 		err = ext4_ext_get_access(handle, inode, path + depth);
7607e028976SAvantika Mathur 		if (err)
761a86c6181SAlex Tomas 			goto cleanup;
762e8546d06SMarcin Slusarz 		le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
7637e028976SAvantika Mathur 		err = ext4_ext_dirty(handle, inode, path + depth);
7647e028976SAvantika Mathur 		if (err)
765a86c6181SAlex Tomas 			goto cleanup;
766a86c6181SAlex Tomas 
767a86c6181SAlex Tomas 	}
768a86c6181SAlex Tomas 
769a86c6181SAlex Tomas 	/* create intermediate indexes */
770a86c6181SAlex Tomas 	k = depth - at - 1;
771a86c6181SAlex Tomas 	BUG_ON(k < 0);
772a86c6181SAlex Tomas 	if (k)
773a86c6181SAlex Tomas 		ext_debug("create %d intermediate indices\n", k);
774a86c6181SAlex Tomas 	/* insert new index into current index block */
775a86c6181SAlex Tomas 	/* current depth stored in i var */
776a86c6181SAlex Tomas 	i = depth - 1;
777a86c6181SAlex Tomas 	while (k--) {
778a86c6181SAlex Tomas 		oldblock = newblock;
779a86c6181SAlex Tomas 		newblock = ablocks[--a];
780bba90743SEric Sandeen 		bh = sb_getblk(inode->i_sb, newblock);
781a86c6181SAlex Tomas 		if (!bh) {
782a86c6181SAlex Tomas 			err = -EIO;
783a86c6181SAlex Tomas 			goto cleanup;
784a86c6181SAlex Tomas 		}
785a86c6181SAlex Tomas 		lock_buffer(bh);
786a86c6181SAlex Tomas 
7877e028976SAvantika Mathur 		err = ext4_journal_get_create_access(handle, bh);
7887e028976SAvantika Mathur 		if (err)
789a86c6181SAlex Tomas 			goto cleanup;
790a86c6181SAlex Tomas 
791a86c6181SAlex Tomas 		neh = ext_block_hdr(bh);
792a86c6181SAlex Tomas 		neh->eh_entries = cpu_to_le16(1);
793a86c6181SAlex Tomas 		neh->eh_magic = EXT4_EXT_MAGIC;
794a86c6181SAlex Tomas 		neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
795a86c6181SAlex Tomas 		neh->eh_depth = cpu_to_le16(depth - i);
796a86c6181SAlex Tomas 		fidx = EXT_FIRST_INDEX(neh);
797a86c6181SAlex Tomas 		fidx->ei_block = border;
798f65e6fbaSAlex Tomas 		ext4_idx_store_pblock(fidx, oldblock);
799a86c6181SAlex Tomas 
800bba90743SEric Sandeen 		ext_debug("int.index at %d (block %llu): %u -> %llu\n",
801bba90743SEric Sandeen 				i, newblock, le32_to_cpu(border), oldblock);
802a86c6181SAlex Tomas 		/* copy indexes */
803a86c6181SAlex Tomas 		m = 0;
804a86c6181SAlex Tomas 		path[i].p_idx++;
805a86c6181SAlex Tomas 
806a86c6181SAlex Tomas 		ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
807a86c6181SAlex Tomas 				EXT_MAX_INDEX(path[i].p_hdr));
808a86c6181SAlex Tomas 		BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) !=
809a86c6181SAlex Tomas 				EXT_LAST_INDEX(path[i].p_hdr));
810a86c6181SAlex Tomas 		while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
81126d535edSDmitry Monakhov 			ext_debug("%d: move %d:%llu in new index %llu\n", i,
812a86c6181SAlex Tomas 					le32_to_cpu(path[i].p_idx->ei_block),
813f65e6fbaSAlex Tomas 					idx_pblock(path[i].p_idx),
814a86c6181SAlex Tomas 					newblock);
815a86c6181SAlex Tomas 			/*memmove(++fidx, path[i].p_idx++,
816a86c6181SAlex Tomas 					sizeof(struct ext4_extent_idx));
817a86c6181SAlex Tomas 			neh->eh_entries++;
818a86c6181SAlex Tomas 			BUG_ON(neh->eh_entries > neh->eh_max);*/
819a86c6181SAlex Tomas 			path[i].p_idx++;
820a86c6181SAlex Tomas 			m++;
821a86c6181SAlex Tomas 		}
822a86c6181SAlex Tomas 		if (m) {
823a86c6181SAlex Tomas 			memmove(++fidx, path[i].p_idx - m,
824a86c6181SAlex Tomas 				sizeof(struct ext4_extent_idx) * m);
825e8546d06SMarcin Slusarz 			le16_add_cpu(&neh->eh_entries, m);
826a86c6181SAlex Tomas 		}
827a86c6181SAlex Tomas 		set_buffer_uptodate(bh);
828a86c6181SAlex Tomas 		unlock_buffer(bh);
829a86c6181SAlex Tomas 
8307e028976SAvantika Mathur 		err = ext4_journal_dirty_metadata(handle, bh);
8317e028976SAvantika Mathur 		if (err)
832a86c6181SAlex Tomas 			goto cleanup;
833a86c6181SAlex Tomas 		brelse(bh);
834a86c6181SAlex Tomas 		bh = NULL;
835a86c6181SAlex Tomas 
836a86c6181SAlex Tomas 		/* correct old index */
837a86c6181SAlex Tomas 		if (m) {
838a86c6181SAlex Tomas 			err = ext4_ext_get_access(handle, inode, path + i);
839a86c6181SAlex Tomas 			if (err)
840a86c6181SAlex Tomas 				goto cleanup;
841e8546d06SMarcin Slusarz 			le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
842a86c6181SAlex Tomas 			err = ext4_ext_dirty(handle, inode, path + i);
843a86c6181SAlex Tomas 			if (err)
844a86c6181SAlex Tomas 				goto cleanup;
845a86c6181SAlex Tomas 		}
846a86c6181SAlex Tomas 
847a86c6181SAlex Tomas 		i--;
848a86c6181SAlex Tomas 	}
849a86c6181SAlex Tomas 
850a86c6181SAlex Tomas 	/* insert new index */
851a86c6181SAlex Tomas 	err = ext4_ext_insert_index(handle, inode, path + at,
852a86c6181SAlex Tomas 				    le32_to_cpu(border), newblock);
853a86c6181SAlex Tomas 
854a86c6181SAlex Tomas cleanup:
855a86c6181SAlex Tomas 	if (bh) {
856a86c6181SAlex Tomas 		if (buffer_locked(bh))
857a86c6181SAlex Tomas 			unlock_buffer(bh);
858a86c6181SAlex Tomas 		brelse(bh);
859a86c6181SAlex Tomas 	}
860a86c6181SAlex Tomas 
861a86c6181SAlex Tomas 	if (err) {
862a86c6181SAlex Tomas 		/* free all allocated blocks in error case */
863a86c6181SAlex Tomas 		for (i = 0; i < depth; i++) {
864a86c6181SAlex Tomas 			if (!ablocks[i])
865a86c6181SAlex Tomas 				continue;
866c9de560dSAlex Tomas 			ext4_free_blocks(handle, inode, ablocks[i], 1, 1);
867a86c6181SAlex Tomas 		}
868a86c6181SAlex Tomas 	}
869a86c6181SAlex Tomas 	kfree(ablocks);
870a86c6181SAlex Tomas 
871a86c6181SAlex Tomas 	return err;
872a86c6181SAlex Tomas }
873a86c6181SAlex Tomas 
874a86c6181SAlex Tomas /*
875d0d856e8SRandy Dunlap  * ext4_ext_grow_indepth:
876d0d856e8SRandy Dunlap  * implements tree growing procedure:
877a86c6181SAlex Tomas  * - allocates new block
878a86c6181SAlex Tomas  * - moves top-level data (index block or leaf) into the new block
879d0d856e8SRandy Dunlap  * - initializes new top-level, creating index that points to the
880a86c6181SAlex Tomas  *   just created block
881a86c6181SAlex Tomas  */
882a86c6181SAlex Tomas static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
883a86c6181SAlex Tomas 					struct ext4_ext_path *path,
884a86c6181SAlex Tomas 					struct ext4_extent *newext)
885a86c6181SAlex Tomas {
886a86c6181SAlex Tomas 	struct ext4_ext_path *curp = path;
887a86c6181SAlex Tomas 	struct ext4_extent_header *neh;
888a86c6181SAlex Tomas 	struct ext4_extent_idx *fidx;
889a86c6181SAlex Tomas 	struct buffer_head *bh;
890f65e6fbaSAlex Tomas 	ext4_fsblk_t newblock;
891a86c6181SAlex Tomas 	int err = 0;
892a86c6181SAlex Tomas 
893654b4908SAneesh Kumar K.V 	newblock = ext4_ext_new_meta_block(handle, inode, path, newext, &err);
894a86c6181SAlex Tomas 	if (newblock == 0)
895a86c6181SAlex Tomas 		return err;
896a86c6181SAlex Tomas 
897a86c6181SAlex Tomas 	bh = sb_getblk(inode->i_sb, newblock);
898a86c6181SAlex Tomas 	if (!bh) {
899a86c6181SAlex Tomas 		err = -EIO;
900a86c6181SAlex Tomas 		ext4_std_error(inode->i_sb, err);
901a86c6181SAlex Tomas 		return err;
902a86c6181SAlex Tomas 	}
903a86c6181SAlex Tomas 	lock_buffer(bh);
904a86c6181SAlex Tomas 
9057e028976SAvantika Mathur 	err = ext4_journal_get_create_access(handle, bh);
9067e028976SAvantika Mathur 	if (err) {
907a86c6181SAlex Tomas 		unlock_buffer(bh);
908a86c6181SAlex Tomas 		goto out;
909a86c6181SAlex Tomas 	}
910a86c6181SAlex Tomas 
911a86c6181SAlex Tomas 	/* move top-level index/leaf into new block */
912a86c6181SAlex Tomas 	memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
913a86c6181SAlex Tomas 
914a86c6181SAlex Tomas 	/* set size of new block */
915a86c6181SAlex Tomas 	neh = ext_block_hdr(bh);
916a86c6181SAlex Tomas 	/* old root could have indexes or leaves
917a86c6181SAlex Tomas 	 * so calculate e_max right way */
918a86c6181SAlex Tomas 	if (ext_depth(inode))
919a86c6181SAlex Tomas 	  neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
920a86c6181SAlex Tomas 	else
921a86c6181SAlex Tomas 	  neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
922a86c6181SAlex Tomas 	neh->eh_magic = EXT4_EXT_MAGIC;
923a86c6181SAlex Tomas 	set_buffer_uptodate(bh);
924a86c6181SAlex Tomas 	unlock_buffer(bh);
925a86c6181SAlex Tomas 
9267e028976SAvantika Mathur 	err = ext4_journal_dirty_metadata(handle, bh);
9277e028976SAvantika Mathur 	if (err)
928a86c6181SAlex Tomas 		goto out;
929a86c6181SAlex Tomas 
930a86c6181SAlex Tomas 	/* create index in new top-level index: num,max,pointer */
9317e028976SAvantika Mathur 	err = ext4_ext_get_access(handle, inode, curp);
9327e028976SAvantika Mathur 	if (err)
933a86c6181SAlex Tomas 		goto out;
934a86c6181SAlex Tomas 
935a86c6181SAlex Tomas 	curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
936a86c6181SAlex Tomas 	curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode));
937a86c6181SAlex Tomas 	curp->p_hdr->eh_entries = cpu_to_le16(1);
938a86c6181SAlex Tomas 	curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
939e9f410b1SDmitry Monakhov 
940e9f410b1SDmitry Monakhov 	if (path[0].p_hdr->eh_depth)
941e9f410b1SDmitry Monakhov 		curp->p_idx->ei_block =
942e9f410b1SDmitry Monakhov 			EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
943e9f410b1SDmitry Monakhov 	else
944e9f410b1SDmitry Monakhov 		curp->p_idx->ei_block =
945e9f410b1SDmitry Monakhov 			EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
946f65e6fbaSAlex Tomas 	ext4_idx_store_pblock(curp->p_idx, newblock);
947a86c6181SAlex Tomas 
948a86c6181SAlex Tomas 	neh = ext_inode_hdr(inode);
949a86c6181SAlex Tomas 	fidx = EXT_FIRST_INDEX(neh);
9502ae02107SMingming Cao 	ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
951a86c6181SAlex Tomas 		  le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
952f65e6fbaSAlex Tomas 		  le32_to_cpu(fidx->ei_block), idx_pblock(fidx));
953a86c6181SAlex Tomas 
954a86c6181SAlex Tomas 	neh->eh_depth = cpu_to_le16(path->p_depth + 1);
955a86c6181SAlex Tomas 	err = ext4_ext_dirty(handle, inode, curp);
956a86c6181SAlex Tomas out:
957a86c6181SAlex Tomas 	brelse(bh);
958a86c6181SAlex Tomas 
959a86c6181SAlex Tomas 	return err;
960a86c6181SAlex Tomas }
961a86c6181SAlex Tomas 
962a86c6181SAlex Tomas /*
963d0d856e8SRandy Dunlap  * ext4_ext_create_new_leaf:
964d0d856e8SRandy Dunlap  * finds empty index and adds new leaf.
965d0d856e8SRandy Dunlap  * if no free index is found, then it requests in-depth growing.
966a86c6181SAlex Tomas  */
967a86c6181SAlex Tomas static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
968a86c6181SAlex Tomas 					struct ext4_ext_path *path,
969a86c6181SAlex Tomas 					struct ext4_extent *newext)
970a86c6181SAlex Tomas {
971a86c6181SAlex Tomas 	struct ext4_ext_path *curp;
972a86c6181SAlex Tomas 	int depth, i, err = 0;
973a86c6181SAlex Tomas 
974a86c6181SAlex Tomas repeat:
975a86c6181SAlex Tomas 	i = depth = ext_depth(inode);
976a86c6181SAlex Tomas 
977a86c6181SAlex Tomas 	/* walk up to the tree and look for free index entry */
978a86c6181SAlex Tomas 	curp = path + depth;
979a86c6181SAlex Tomas 	while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
980a86c6181SAlex Tomas 		i--;
981a86c6181SAlex Tomas 		curp--;
982a86c6181SAlex Tomas 	}
983a86c6181SAlex Tomas 
984d0d856e8SRandy Dunlap 	/* we use already allocated block for index block,
985d0d856e8SRandy Dunlap 	 * so subsequent data blocks should be contiguous */
986a86c6181SAlex Tomas 	if (EXT_HAS_FREE_INDEX(curp)) {
987a86c6181SAlex Tomas 		/* if we found index with free entry, then use that
988a86c6181SAlex Tomas 		 * entry: create all needed subtree and add new leaf */
989a86c6181SAlex Tomas 		err = ext4_ext_split(handle, inode, path, newext, i);
990787e0981SShen Feng 		if (err)
991787e0981SShen Feng 			goto out;
992a86c6181SAlex Tomas 
993a86c6181SAlex Tomas 		/* refill path */
994a86c6181SAlex Tomas 		ext4_ext_drop_refs(path);
995a86c6181SAlex Tomas 		path = ext4_ext_find_extent(inode,
996725d26d3SAneesh Kumar K.V 				    (ext4_lblk_t)le32_to_cpu(newext->ee_block),
997a86c6181SAlex Tomas 				    path);
998a86c6181SAlex Tomas 		if (IS_ERR(path))
999a86c6181SAlex Tomas 			err = PTR_ERR(path);
1000a86c6181SAlex Tomas 	} else {
1001a86c6181SAlex Tomas 		/* tree is full, time to grow in depth */
1002a86c6181SAlex Tomas 		err = ext4_ext_grow_indepth(handle, inode, path, newext);
1003a86c6181SAlex Tomas 		if (err)
1004a86c6181SAlex Tomas 			goto out;
1005a86c6181SAlex Tomas 
1006a86c6181SAlex Tomas 		/* refill path */
1007a86c6181SAlex Tomas 		ext4_ext_drop_refs(path);
1008a86c6181SAlex Tomas 		path = ext4_ext_find_extent(inode,
1009725d26d3SAneesh Kumar K.V 				   (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1010a86c6181SAlex Tomas 				    path);
1011a86c6181SAlex Tomas 		if (IS_ERR(path)) {
1012a86c6181SAlex Tomas 			err = PTR_ERR(path);
1013a86c6181SAlex Tomas 			goto out;
1014a86c6181SAlex Tomas 		}
1015a86c6181SAlex Tomas 
1016a86c6181SAlex Tomas 		/*
1017d0d856e8SRandy Dunlap 		 * only first (depth 0 -> 1) produces free space;
1018d0d856e8SRandy Dunlap 		 * in all other cases we have to split the grown tree
1019a86c6181SAlex Tomas 		 */
1020a86c6181SAlex Tomas 		depth = ext_depth(inode);
1021a86c6181SAlex Tomas 		if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
1022d0d856e8SRandy Dunlap 			/* now we need to split */
1023a86c6181SAlex Tomas 			goto repeat;
1024a86c6181SAlex Tomas 		}
1025a86c6181SAlex Tomas 	}
1026a86c6181SAlex Tomas 
1027a86c6181SAlex Tomas out:
1028a86c6181SAlex Tomas 	return err;
1029a86c6181SAlex Tomas }
1030a86c6181SAlex Tomas 
1031a86c6181SAlex Tomas /*
10321988b51eSAlex Tomas  * search the closest allocated block to the left for *logical
10331988b51eSAlex Tomas  * and returns it at @logical + it's physical address at @phys
10341988b51eSAlex Tomas  * if *logical is the smallest allocated block, the function
10351988b51eSAlex Tomas  * returns 0 at @phys
10361988b51eSAlex Tomas  * return value contains 0 (success) or error code
10371988b51eSAlex Tomas  */
10381988b51eSAlex Tomas int
10391988b51eSAlex Tomas ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path,
10401988b51eSAlex Tomas 			ext4_lblk_t *logical, ext4_fsblk_t *phys)
10411988b51eSAlex Tomas {
10421988b51eSAlex Tomas 	struct ext4_extent_idx *ix;
10431988b51eSAlex Tomas 	struct ext4_extent *ex;
1044b939e376SAneesh Kumar K.V 	int depth, ee_len;
10451988b51eSAlex Tomas 
10461988b51eSAlex Tomas 	BUG_ON(path == NULL);
10471988b51eSAlex Tomas 	depth = path->p_depth;
10481988b51eSAlex Tomas 	*phys = 0;
10491988b51eSAlex Tomas 
10501988b51eSAlex Tomas 	if (depth == 0 && path->p_ext == NULL)
10511988b51eSAlex Tomas 		return 0;
10521988b51eSAlex Tomas 
10531988b51eSAlex Tomas 	/* usually extent in the path covers blocks smaller
10541988b51eSAlex Tomas 	 * then *logical, but it can be that extent is the
10551988b51eSAlex Tomas 	 * first one in the file */
10561988b51eSAlex Tomas 
10571988b51eSAlex Tomas 	ex = path[depth].p_ext;
1058b939e376SAneesh Kumar K.V 	ee_len = ext4_ext_get_actual_len(ex);
10591988b51eSAlex Tomas 	if (*logical < le32_to_cpu(ex->ee_block)) {
10601988b51eSAlex Tomas 		BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
10611988b51eSAlex Tomas 		while (--depth >= 0) {
10621988b51eSAlex Tomas 			ix = path[depth].p_idx;
10631988b51eSAlex Tomas 			BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
10641988b51eSAlex Tomas 		}
10651988b51eSAlex Tomas 		return 0;
10661988b51eSAlex Tomas 	}
10671988b51eSAlex Tomas 
1068b939e376SAneesh Kumar K.V 	BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
10691988b51eSAlex Tomas 
1070b939e376SAneesh Kumar K.V 	*logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
1071b939e376SAneesh Kumar K.V 	*phys = ext_pblock(ex) + ee_len - 1;
10721988b51eSAlex Tomas 	return 0;
10731988b51eSAlex Tomas }
10741988b51eSAlex Tomas 
10751988b51eSAlex Tomas /*
10761988b51eSAlex Tomas  * search the closest allocated block to the right for *logical
10771988b51eSAlex Tomas  * and returns it at @logical + it's physical address at @phys
10781988b51eSAlex Tomas  * if *logical is the smallest allocated block, the function
10791988b51eSAlex Tomas  * returns 0 at @phys
10801988b51eSAlex Tomas  * return value contains 0 (success) or error code
10811988b51eSAlex Tomas  */
10821988b51eSAlex Tomas int
10831988b51eSAlex Tomas ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path,
10841988b51eSAlex Tomas 			ext4_lblk_t *logical, ext4_fsblk_t *phys)
10851988b51eSAlex Tomas {
10861988b51eSAlex Tomas 	struct buffer_head *bh = NULL;
10871988b51eSAlex Tomas 	struct ext4_extent_header *eh;
10881988b51eSAlex Tomas 	struct ext4_extent_idx *ix;
10891988b51eSAlex Tomas 	struct ext4_extent *ex;
10901988b51eSAlex Tomas 	ext4_fsblk_t block;
1091b939e376SAneesh Kumar K.V 	int depth, ee_len;
10921988b51eSAlex Tomas 
10931988b51eSAlex Tomas 	BUG_ON(path == NULL);
10941988b51eSAlex Tomas 	depth = path->p_depth;
10951988b51eSAlex Tomas 	*phys = 0;
10961988b51eSAlex Tomas 
10971988b51eSAlex Tomas 	if (depth == 0 && path->p_ext == NULL)
10981988b51eSAlex Tomas 		return 0;
10991988b51eSAlex Tomas 
11001988b51eSAlex Tomas 	/* usually extent in the path covers blocks smaller
11011988b51eSAlex Tomas 	 * then *logical, but it can be that extent is the
11021988b51eSAlex Tomas 	 * first one in the file */
11031988b51eSAlex Tomas 
11041988b51eSAlex Tomas 	ex = path[depth].p_ext;
1105b939e376SAneesh Kumar K.V 	ee_len = ext4_ext_get_actual_len(ex);
11061988b51eSAlex Tomas 	if (*logical < le32_to_cpu(ex->ee_block)) {
11071988b51eSAlex Tomas 		BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
11081988b51eSAlex Tomas 		while (--depth >= 0) {
11091988b51eSAlex Tomas 			ix = path[depth].p_idx;
11101988b51eSAlex Tomas 			BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
11111988b51eSAlex Tomas 		}
11121988b51eSAlex Tomas 		*logical = le32_to_cpu(ex->ee_block);
11131988b51eSAlex Tomas 		*phys = ext_pblock(ex);
11141988b51eSAlex Tomas 		return 0;
11151988b51eSAlex Tomas 	}
11161988b51eSAlex Tomas 
1117b939e376SAneesh Kumar K.V 	BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
11181988b51eSAlex Tomas 
11191988b51eSAlex Tomas 	if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
11201988b51eSAlex Tomas 		/* next allocated block in this leaf */
11211988b51eSAlex Tomas 		ex++;
11221988b51eSAlex Tomas 		*logical = le32_to_cpu(ex->ee_block);
11231988b51eSAlex Tomas 		*phys = ext_pblock(ex);
11241988b51eSAlex Tomas 		return 0;
11251988b51eSAlex Tomas 	}
11261988b51eSAlex Tomas 
11271988b51eSAlex Tomas 	/* go up and search for index to the right */
11281988b51eSAlex Tomas 	while (--depth >= 0) {
11291988b51eSAlex Tomas 		ix = path[depth].p_idx;
11301988b51eSAlex Tomas 		if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
11311988b51eSAlex Tomas 			break;
11321988b51eSAlex Tomas 	}
11331988b51eSAlex Tomas 
11341988b51eSAlex Tomas 	if (depth < 0) {
11351988b51eSAlex Tomas 		/* we've gone up to the root and
11361988b51eSAlex Tomas 		 * found no index to the right */
11371988b51eSAlex Tomas 		return 0;
11381988b51eSAlex Tomas 	}
11391988b51eSAlex Tomas 
11401988b51eSAlex Tomas 	/* we've found index to the right, let's
11411988b51eSAlex Tomas 	 * follow it and find the closest allocated
11421988b51eSAlex Tomas 	 * block to the right */
11431988b51eSAlex Tomas 	ix++;
11441988b51eSAlex Tomas 	block = idx_pblock(ix);
11451988b51eSAlex Tomas 	while (++depth < path->p_depth) {
11461988b51eSAlex Tomas 		bh = sb_bread(inode->i_sb, block);
11471988b51eSAlex Tomas 		if (bh == NULL)
11481988b51eSAlex Tomas 			return -EIO;
11491988b51eSAlex Tomas 		eh = ext_block_hdr(bh);
11501988b51eSAlex Tomas 		if (ext4_ext_check_header(inode, eh, depth)) {
11511988b51eSAlex Tomas 			put_bh(bh);
11521988b51eSAlex Tomas 			return -EIO;
11531988b51eSAlex Tomas 		}
11541988b51eSAlex Tomas 		ix = EXT_FIRST_INDEX(eh);
11551988b51eSAlex Tomas 		block = idx_pblock(ix);
11561988b51eSAlex Tomas 		put_bh(bh);
11571988b51eSAlex Tomas 	}
11581988b51eSAlex Tomas 
11591988b51eSAlex Tomas 	bh = sb_bread(inode->i_sb, block);
11601988b51eSAlex Tomas 	if (bh == NULL)
11611988b51eSAlex Tomas 		return -EIO;
11621988b51eSAlex Tomas 	eh = ext_block_hdr(bh);
11631988b51eSAlex Tomas 	if (ext4_ext_check_header(inode, eh, path->p_depth - depth)) {
11641988b51eSAlex Tomas 		put_bh(bh);
11651988b51eSAlex Tomas 		return -EIO;
11661988b51eSAlex Tomas 	}
11671988b51eSAlex Tomas 	ex = EXT_FIRST_EXTENT(eh);
11681988b51eSAlex Tomas 	*logical = le32_to_cpu(ex->ee_block);
11691988b51eSAlex Tomas 	*phys = ext_pblock(ex);
11701988b51eSAlex Tomas 	put_bh(bh);
11711988b51eSAlex Tomas 	return 0;
11721988b51eSAlex Tomas 
11731988b51eSAlex Tomas }
11741988b51eSAlex Tomas 
11751988b51eSAlex Tomas /*
1176d0d856e8SRandy Dunlap  * ext4_ext_next_allocated_block:
1177d0d856e8SRandy Dunlap  * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1178d0d856e8SRandy Dunlap  * NOTE: it considers block number from index entry as
1179d0d856e8SRandy Dunlap  * allocated block. Thus, index entries have to be consistent
1180d0d856e8SRandy Dunlap  * with leaves.
1181a86c6181SAlex Tomas  */
1182725d26d3SAneesh Kumar K.V static ext4_lblk_t
1183a86c6181SAlex Tomas ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1184a86c6181SAlex Tomas {
1185a86c6181SAlex Tomas 	int depth;
1186a86c6181SAlex Tomas 
1187a86c6181SAlex Tomas 	BUG_ON(path == NULL);
1188a86c6181SAlex Tomas 	depth = path->p_depth;
1189a86c6181SAlex Tomas 
1190a86c6181SAlex Tomas 	if (depth == 0 && path->p_ext == NULL)
1191a86c6181SAlex Tomas 		return EXT_MAX_BLOCK;
1192a86c6181SAlex Tomas 
1193a86c6181SAlex Tomas 	while (depth >= 0) {
1194a86c6181SAlex Tomas 		if (depth == path->p_depth) {
1195a86c6181SAlex Tomas 			/* leaf */
1196a86c6181SAlex Tomas 			if (path[depth].p_ext !=
1197a86c6181SAlex Tomas 					EXT_LAST_EXTENT(path[depth].p_hdr))
1198a86c6181SAlex Tomas 			  return le32_to_cpu(path[depth].p_ext[1].ee_block);
1199a86c6181SAlex Tomas 		} else {
1200a86c6181SAlex Tomas 			/* index */
1201a86c6181SAlex Tomas 			if (path[depth].p_idx !=
1202a86c6181SAlex Tomas 					EXT_LAST_INDEX(path[depth].p_hdr))
1203a86c6181SAlex Tomas 			  return le32_to_cpu(path[depth].p_idx[1].ei_block);
1204a86c6181SAlex Tomas 		}
1205a86c6181SAlex Tomas 		depth--;
1206a86c6181SAlex Tomas 	}
1207a86c6181SAlex Tomas 
1208a86c6181SAlex Tomas 	return EXT_MAX_BLOCK;
1209a86c6181SAlex Tomas }
1210a86c6181SAlex Tomas 
1211a86c6181SAlex Tomas /*
1212d0d856e8SRandy Dunlap  * ext4_ext_next_leaf_block:
1213a86c6181SAlex Tomas  * returns first allocated block from next leaf or EXT_MAX_BLOCK
1214a86c6181SAlex Tomas  */
1215725d26d3SAneesh Kumar K.V static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode,
1216a86c6181SAlex Tomas 					struct ext4_ext_path *path)
1217a86c6181SAlex Tomas {
1218a86c6181SAlex Tomas 	int depth;
1219a86c6181SAlex Tomas 
1220a86c6181SAlex Tomas 	BUG_ON(path == NULL);
1221a86c6181SAlex Tomas 	depth = path->p_depth;
1222a86c6181SAlex Tomas 
1223a86c6181SAlex Tomas 	/* zero-tree has no leaf blocks at all */
1224a86c6181SAlex Tomas 	if (depth == 0)
1225a86c6181SAlex Tomas 		return EXT_MAX_BLOCK;
1226a86c6181SAlex Tomas 
1227a86c6181SAlex Tomas 	/* go to index block */
1228a86c6181SAlex Tomas 	depth--;
1229a86c6181SAlex Tomas 
1230a86c6181SAlex Tomas 	while (depth >= 0) {
1231a86c6181SAlex Tomas 		if (path[depth].p_idx !=
1232a86c6181SAlex Tomas 				EXT_LAST_INDEX(path[depth].p_hdr))
1233725d26d3SAneesh Kumar K.V 			return (ext4_lblk_t)
1234725d26d3SAneesh Kumar K.V 				le32_to_cpu(path[depth].p_idx[1].ei_block);
1235a86c6181SAlex Tomas 		depth--;
1236a86c6181SAlex Tomas 	}
1237a86c6181SAlex Tomas 
1238a86c6181SAlex Tomas 	return EXT_MAX_BLOCK;
1239a86c6181SAlex Tomas }
1240a86c6181SAlex Tomas 
1241a86c6181SAlex Tomas /*
1242d0d856e8SRandy Dunlap  * ext4_ext_correct_indexes:
1243d0d856e8SRandy Dunlap  * if leaf gets modified and modified extent is first in the leaf,
1244d0d856e8SRandy Dunlap  * then we have to correct all indexes above.
1245a86c6181SAlex Tomas  * TODO: do we need to correct tree in all cases?
1246a86c6181SAlex Tomas  */
12471d03ec98SAneesh Kumar K.V static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
1248a86c6181SAlex Tomas 				struct ext4_ext_path *path)
1249a86c6181SAlex Tomas {
1250a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
1251a86c6181SAlex Tomas 	int depth = ext_depth(inode);
1252a86c6181SAlex Tomas 	struct ext4_extent *ex;
1253a86c6181SAlex Tomas 	__le32 border;
1254a86c6181SAlex Tomas 	int k, err = 0;
1255a86c6181SAlex Tomas 
1256a86c6181SAlex Tomas 	eh = path[depth].p_hdr;
1257a86c6181SAlex Tomas 	ex = path[depth].p_ext;
1258a86c6181SAlex Tomas 	BUG_ON(ex == NULL);
1259a86c6181SAlex Tomas 	BUG_ON(eh == NULL);
1260a86c6181SAlex Tomas 
1261a86c6181SAlex Tomas 	if (depth == 0) {
1262a86c6181SAlex Tomas 		/* there is no tree at all */
1263a86c6181SAlex Tomas 		return 0;
1264a86c6181SAlex Tomas 	}
1265a86c6181SAlex Tomas 
1266a86c6181SAlex Tomas 	if (ex != EXT_FIRST_EXTENT(eh)) {
1267a86c6181SAlex Tomas 		/* we correct tree if first leaf got modified only */
1268a86c6181SAlex Tomas 		return 0;
1269a86c6181SAlex Tomas 	}
1270a86c6181SAlex Tomas 
1271a86c6181SAlex Tomas 	/*
1272d0d856e8SRandy Dunlap 	 * TODO: we need correction if border is smaller than current one
1273a86c6181SAlex Tomas 	 */
1274a86c6181SAlex Tomas 	k = depth - 1;
1275a86c6181SAlex Tomas 	border = path[depth].p_ext->ee_block;
12767e028976SAvantika Mathur 	err = ext4_ext_get_access(handle, inode, path + k);
12777e028976SAvantika Mathur 	if (err)
1278a86c6181SAlex Tomas 		return err;
1279a86c6181SAlex Tomas 	path[k].p_idx->ei_block = border;
12807e028976SAvantika Mathur 	err = ext4_ext_dirty(handle, inode, path + k);
12817e028976SAvantika Mathur 	if (err)
1282a86c6181SAlex Tomas 		return err;
1283a86c6181SAlex Tomas 
1284a86c6181SAlex Tomas 	while (k--) {
1285a86c6181SAlex Tomas 		/* change all left-side indexes */
1286a86c6181SAlex Tomas 		if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1287a86c6181SAlex Tomas 			break;
12887e028976SAvantika Mathur 		err = ext4_ext_get_access(handle, inode, path + k);
12897e028976SAvantika Mathur 		if (err)
1290a86c6181SAlex Tomas 			break;
1291a86c6181SAlex Tomas 		path[k].p_idx->ei_block = border;
12927e028976SAvantika Mathur 		err = ext4_ext_dirty(handle, inode, path + k);
12937e028976SAvantika Mathur 		if (err)
1294a86c6181SAlex Tomas 			break;
1295a86c6181SAlex Tomas 	}
1296a86c6181SAlex Tomas 
1297a86c6181SAlex Tomas 	return err;
1298a86c6181SAlex Tomas }
1299a86c6181SAlex Tomas 
130009b88252SAvantika Mathur static int
1301a86c6181SAlex Tomas ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1302a86c6181SAlex Tomas 				struct ext4_extent *ex2)
1303a86c6181SAlex Tomas {
1304749269faSAmit Arora 	unsigned short ext1_ee_len, ext2_ee_len, max_len;
1305a2df2a63SAmit Arora 
1306a2df2a63SAmit Arora 	/*
1307a2df2a63SAmit Arora 	 * Make sure that either both extents are uninitialized, or
1308a2df2a63SAmit Arora 	 * both are _not_.
1309a2df2a63SAmit Arora 	 */
1310a2df2a63SAmit Arora 	if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1311a2df2a63SAmit Arora 		return 0;
1312a2df2a63SAmit Arora 
1313749269faSAmit Arora 	if (ext4_ext_is_uninitialized(ex1))
1314749269faSAmit Arora 		max_len = EXT_UNINIT_MAX_LEN;
1315749269faSAmit Arora 	else
1316749269faSAmit Arora 		max_len = EXT_INIT_MAX_LEN;
1317749269faSAmit Arora 
1318a2df2a63SAmit Arora 	ext1_ee_len = ext4_ext_get_actual_len(ex1);
1319a2df2a63SAmit Arora 	ext2_ee_len = ext4_ext_get_actual_len(ex2);
1320a2df2a63SAmit Arora 
1321a2df2a63SAmit Arora 	if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
132263f57933SAndrew Morton 			le32_to_cpu(ex2->ee_block))
1323a86c6181SAlex Tomas 		return 0;
1324a86c6181SAlex Tomas 
1325471d4011SSuparna Bhattacharya 	/*
1326471d4011SSuparna Bhattacharya 	 * To allow future support for preallocated extents to be added
1327471d4011SSuparna Bhattacharya 	 * as an RO_COMPAT feature, refuse to merge to extents if
1328d0d856e8SRandy Dunlap 	 * this can result in the top bit of ee_len being set.
1329471d4011SSuparna Bhattacharya 	 */
1330749269faSAmit Arora 	if (ext1_ee_len + ext2_ee_len > max_len)
1331471d4011SSuparna Bhattacharya 		return 0;
1332bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST
1333b939e376SAneesh Kumar K.V 	if (ext1_ee_len >= 4)
1334a86c6181SAlex Tomas 		return 0;
1335a86c6181SAlex Tomas #endif
1336a86c6181SAlex Tomas 
1337a2df2a63SAmit Arora 	if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2))
1338a86c6181SAlex Tomas 		return 1;
1339a86c6181SAlex Tomas 	return 0;
1340a86c6181SAlex Tomas }
1341a86c6181SAlex Tomas 
1342a86c6181SAlex Tomas /*
134356055d3aSAmit Arora  * This function tries to merge the "ex" extent to the next extent in the tree.
134456055d3aSAmit Arora  * It always tries to merge towards right. If you want to merge towards
134556055d3aSAmit Arora  * left, pass "ex - 1" as argument instead of "ex".
134656055d3aSAmit Arora  * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
134756055d3aSAmit Arora  * 1 if they got merged.
134856055d3aSAmit Arora  */
134956055d3aSAmit Arora int ext4_ext_try_to_merge(struct inode *inode,
135056055d3aSAmit Arora 			  struct ext4_ext_path *path,
135156055d3aSAmit Arora 			  struct ext4_extent *ex)
135256055d3aSAmit Arora {
135356055d3aSAmit Arora 	struct ext4_extent_header *eh;
135456055d3aSAmit Arora 	unsigned int depth, len;
135556055d3aSAmit Arora 	int merge_done = 0;
135656055d3aSAmit Arora 	int uninitialized = 0;
135756055d3aSAmit Arora 
135856055d3aSAmit Arora 	depth = ext_depth(inode);
135956055d3aSAmit Arora 	BUG_ON(path[depth].p_hdr == NULL);
136056055d3aSAmit Arora 	eh = path[depth].p_hdr;
136156055d3aSAmit Arora 
136256055d3aSAmit Arora 	while (ex < EXT_LAST_EXTENT(eh)) {
136356055d3aSAmit Arora 		if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
136456055d3aSAmit Arora 			break;
136556055d3aSAmit Arora 		/* merge with next extent! */
136656055d3aSAmit Arora 		if (ext4_ext_is_uninitialized(ex))
136756055d3aSAmit Arora 			uninitialized = 1;
136856055d3aSAmit Arora 		ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
136956055d3aSAmit Arora 				+ ext4_ext_get_actual_len(ex + 1));
137056055d3aSAmit Arora 		if (uninitialized)
137156055d3aSAmit Arora 			ext4_ext_mark_uninitialized(ex);
137256055d3aSAmit Arora 
137356055d3aSAmit Arora 		if (ex + 1 < EXT_LAST_EXTENT(eh)) {
137456055d3aSAmit Arora 			len = (EXT_LAST_EXTENT(eh) - ex - 1)
137556055d3aSAmit Arora 				* sizeof(struct ext4_extent);
137656055d3aSAmit Arora 			memmove(ex + 1, ex + 2, len);
137756055d3aSAmit Arora 		}
1378e8546d06SMarcin Slusarz 		le16_add_cpu(&eh->eh_entries, -1);
137956055d3aSAmit Arora 		merge_done = 1;
138056055d3aSAmit Arora 		WARN_ON(eh->eh_entries == 0);
138156055d3aSAmit Arora 		if (!eh->eh_entries)
138256055d3aSAmit Arora 			ext4_error(inode->i_sb, "ext4_ext_try_to_merge",
138356055d3aSAmit Arora 			   "inode#%lu, eh->eh_entries = 0!", inode->i_ino);
138456055d3aSAmit Arora 	}
138556055d3aSAmit Arora 
138656055d3aSAmit Arora 	return merge_done;
138756055d3aSAmit Arora }
138856055d3aSAmit Arora 
138956055d3aSAmit Arora /*
139025d14f98SAmit Arora  * check if a portion of the "newext" extent overlaps with an
139125d14f98SAmit Arora  * existing extent.
139225d14f98SAmit Arora  *
139325d14f98SAmit Arora  * If there is an overlap discovered, it updates the length of the newext
139425d14f98SAmit Arora  * such that there will be no overlap, and then returns 1.
139525d14f98SAmit Arora  * If there is no overlap found, it returns 0.
139625d14f98SAmit Arora  */
139725d14f98SAmit Arora unsigned int ext4_ext_check_overlap(struct inode *inode,
139825d14f98SAmit Arora 				    struct ext4_extent *newext,
139925d14f98SAmit Arora 				    struct ext4_ext_path *path)
140025d14f98SAmit Arora {
1401725d26d3SAneesh Kumar K.V 	ext4_lblk_t b1, b2;
140225d14f98SAmit Arora 	unsigned int depth, len1;
140325d14f98SAmit Arora 	unsigned int ret = 0;
140425d14f98SAmit Arora 
140525d14f98SAmit Arora 	b1 = le32_to_cpu(newext->ee_block);
1406a2df2a63SAmit Arora 	len1 = ext4_ext_get_actual_len(newext);
140725d14f98SAmit Arora 	depth = ext_depth(inode);
140825d14f98SAmit Arora 	if (!path[depth].p_ext)
140925d14f98SAmit Arora 		goto out;
141025d14f98SAmit Arora 	b2 = le32_to_cpu(path[depth].p_ext->ee_block);
141125d14f98SAmit Arora 
141225d14f98SAmit Arora 	/*
141325d14f98SAmit Arora 	 * get the next allocated block if the extent in the path
141425d14f98SAmit Arora 	 * is before the requested block(s)
141525d14f98SAmit Arora 	 */
141625d14f98SAmit Arora 	if (b2 < b1) {
141725d14f98SAmit Arora 		b2 = ext4_ext_next_allocated_block(path);
141825d14f98SAmit Arora 		if (b2 == EXT_MAX_BLOCK)
141925d14f98SAmit Arora 			goto out;
142025d14f98SAmit Arora 	}
142125d14f98SAmit Arora 
1422725d26d3SAneesh Kumar K.V 	/* check for wrap through zero on extent logical start block*/
142325d14f98SAmit Arora 	if (b1 + len1 < b1) {
142425d14f98SAmit Arora 		len1 = EXT_MAX_BLOCK - b1;
142525d14f98SAmit Arora 		newext->ee_len = cpu_to_le16(len1);
142625d14f98SAmit Arora 		ret = 1;
142725d14f98SAmit Arora 	}
142825d14f98SAmit Arora 
142925d14f98SAmit Arora 	/* check for overlap */
143025d14f98SAmit Arora 	if (b1 + len1 > b2) {
143125d14f98SAmit Arora 		newext->ee_len = cpu_to_le16(b2 - b1);
143225d14f98SAmit Arora 		ret = 1;
143325d14f98SAmit Arora 	}
143425d14f98SAmit Arora out:
143525d14f98SAmit Arora 	return ret;
143625d14f98SAmit Arora }
143725d14f98SAmit Arora 
143825d14f98SAmit Arora /*
1439d0d856e8SRandy Dunlap  * ext4_ext_insert_extent:
1440d0d856e8SRandy Dunlap  * tries to merge requsted extent into the existing extent or
1441d0d856e8SRandy Dunlap  * inserts requested extent as new one into the tree,
1442d0d856e8SRandy Dunlap  * creating new leaf in the no-space case.
1443a86c6181SAlex Tomas  */
1444a86c6181SAlex Tomas int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1445a86c6181SAlex Tomas 				struct ext4_ext_path *path,
1446a86c6181SAlex Tomas 				struct ext4_extent *newext)
1447a86c6181SAlex Tomas {
1448a86c6181SAlex Tomas 	struct ext4_extent_header * eh;
1449a86c6181SAlex Tomas 	struct ext4_extent *ex, *fex;
1450a86c6181SAlex Tomas 	struct ext4_extent *nearex; /* nearest extent */
1451a86c6181SAlex Tomas 	struct ext4_ext_path *npath = NULL;
1452725d26d3SAneesh Kumar K.V 	int depth, len, err;
1453725d26d3SAneesh Kumar K.V 	ext4_lblk_t next;
1454a2df2a63SAmit Arora 	unsigned uninitialized = 0;
1455a86c6181SAlex Tomas 
1456a2df2a63SAmit Arora 	BUG_ON(ext4_ext_get_actual_len(newext) == 0);
1457a86c6181SAlex Tomas 	depth = ext_depth(inode);
1458a86c6181SAlex Tomas 	ex = path[depth].p_ext;
1459a86c6181SAlex Tomas 	BUG_ON(path[depth].p_hdr == NULL);
1460a86c6181SAlex Tomas 
1461a86c6181SAlex Tomas 	/* try to insert block into found extent and return */
1462a86c6181SAlex Tomas 	if (ex && ext4_can_extents_be_merged(inode, ex, newext)) {
14632ae02107SMingming Cao 		ext_debug("append %d block to %d:%d (from %llu)\n",
1464a2df2a63SAmit Arora 				ext4_ext_get_actual_len(newext),
1465a86c6181SAlex Tomas 				le32_to_cpu(ex->ee_block),
1466a2df2a63SAmit Arora 				ext4_ext_get_actual_len(ex), ext_pblock(ex));
14677e028976SAvantika Mathur 		err = ext4_ext_get_access(handle, inode, path + depth);
14687e028976SAvantika Mathur 		if (err)
1469a86c6181SAlex Tomas 			return err;
1470a2df2a63SAmit Arora 
1471a2df2a63SAmit Arora 		/*
1472a2df2a63SAmit Arora 		 * ext4_can_extents_be_merged should have checked that either
1473a2df2a63SAmit Arora 		 * both extents are uninitialized, or both aren't. Thus we
1474a2df2a63SAmit Arora 		 * need to check only one of them here.
1475a2df2a63SAmit Arora 		 */
1476a2df2a63SAmit Arora 		if (ext4_ext_is_uninitialized(ex))
1477a2df2a63SAmit Arora 			uninitialized = 1;
1478a2df2a63SAmit Arora 		ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1479a2df2a63SAmit Arora 					+ ext4_ext_get_actual_len(newext));
1480a2df2a63SAmit Arora 		if (uninitialized)
1481a2df2a63SAmit Arora 			ext4_ext_mark_uninitialized(ex);
1482a86c6181SAlex Tomas 		eh = path[depth].p_hdr;
1483a86c6181SAlex Tomas 		nearex = ex;
1484a86c6181SAlex Tomas 		goto merge;
1485a86c6181SAlex Tomas 	}
1486a86c6181SAlex Tomas 
1487a86c6181SAlex Tomas repeat:
1488a86c6181SAlex Tomas 	depth = ext_depth(inode);
1489a86c6181SAlex Tomas 	eh = path[depth].p_hdr;
1490a86c6181SAlex Tomas 	if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1491a86c6181SAlex Tomas 		goto has_space;
1492a86c6181SAlex Tomas 
1493a86c6181SAlex Tomas 	/* probably next leaf has space for us? */
1494a86c6181SAlex Tomas 	fex = EXT_LAST_EXTENT(eh);
1495a86c6181SAlex Tomas 	next = ext4_ext_next_leaf_block(inode, path);
1496a86c6181SAlex Tomas 	if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
1497a86c6181SAlex Tomas 	    && next != EXT_MAX_BLOCK) {
1498a86c6181SAlex Tomas 		ext_debug("next leaf block - %d\n", next);
1499a86c6181SAlex Tomas 		BUG_ON(npath != NULL);
1500a86c6181SAlex Tomas 		npath = ext4_ext_find_extent(inode, next, NULL);
1501a86c6181SAlex Tomas 		if (IS_ERR(npath))
1502a86c6181SAlex Tomas 			return PTR_ERR(npath);
1503a86c6181SAlex Tomas 		BUG_ON(npath->p_depth != path->p_depth);
1504a86c6181SAlex Tomas 		eh = npath[depth].p_hdr;
1505a86c6181SAlex Tomas 		if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
1506a86c6181SAlex Tomas 			ext_debug("next leaf isnt full(%d)\n",
1507a86c6181SAlex Tomas 				  le16_to_cpu(eh->eh_entries));
1508a86c6181SAlex Tomas 			path = npath;
1509a86c6181SAlex Tomas 			goto repeat;
1510a86c6181SAlex Tomas 		}
1511a86c6181SAlex Tomas 		ext_debug("next leaf has no free space(%d,%d)\n",
1512a86c6181SAlex Tomas 			  le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1513a86c6181SAlex Tomas 	}
1514a86c6181SAlex Tomas 
1515a86c6181SAlex Tomas 	/*
1516d0d856e8SRandy Dunlap 	 * There is no free space in the found leaf.
1517d0d856e8SRandy Dunlap 	 * We're gonna add a new leaf in the tree.
1518a86c6181SAlex Tomas 	 */
1519a86c6181SAlex Tomas 	err = ext4_ext_create_new_leaf(handle, inode, path, newext);
1520a86c6181SAlex Tomas 	if (err)
1521a86c6181SAlex Tomas 		goto cleanup;
1522a86c6181SAlex Tomas 	depth = ext_depth(inode);
1523a86c6181SAlex Tomas 	eh = path[depth].p_hdr;
1524a86c6181SAlex Tomas 
1525a86c6181SAlex Tomas has_space:
1526a86c6181SAlex Tomas 	nearex = path[depth].p_ext;
1527a86c6181SAlex Tomas 
15287e028976SAvantika Mathur 	err = ext4_ext_get_access(handle, inode, path + depth);
15297e028976SAvantika Mathur 	if (err)
1530a86c6181SAlex Tomas 		goto cleanup;
1531a86c6181SAlex Tomas 
1532a86c6181SAlex Tomas 	if (!nearex) {
1533a86c6181SAlex Tomas 		/* there is no extent in this leaf, create first one */
15342ae02107SMingming Cao 		ext_debug("first extent in the leaf: %d:%llu:%d\n",
1535a86c6181SAlex Tomas 				le32_to_cpu(newext->ee_block),
1536f65e6fbaSAlex Tomas 				ext_pblock(newext),
1537a2df2a63SAmit Arora 				ext4_ext_get_actual_len(newext));
1538a86c6181SAlex Tomas 		path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1539a86c6181SAlex Tomas 	} else if (le32_to_cpu(newext->ee_block)
1540a86c6181SAlex Tomas 			   > le32_to_cpu(nearex->ee_block)) {
1541a86c6181SAlex Tomas /*		BUG_ON(newext->ee_block == nearex->ee_block); */
1542a86c6181SAlex Tomas 		if (nearex != EXT_LAST_EXTENT(eh)) {
1543a86c6181SAlex Tomas 			len = EXT_MAX_EXTENT(eh) - nearex;
1544a86c6181SAlex Tomas 			len = (len - 1) * sizeof(struct ext4_extent);
1545a86c6181SAlex Tomas 			len = len < 0 ? 0 : len;
15462ae02107SMingming Cao 			ext_debug("insert %d:%llu:%d after: nearest 0x%p, "
1547a86c6181SAlex Tomas 					"move %d from 0x%p to 0x%p\n",
1548a86c6181SAlex Tomas 					le32_to_cpu(newext->ee_block),
1549f65e6fbaSAlex Tomas 					ext_pblock(newext),
1550a2df2a63SAmit Arora 					ext4_ext_get_actual_len(newext),
1551a86c6181SAlex Tomas 					nearex, len, nearex + 1, nearex + 2);
1552a86c6181SAlex Tomas 			memmove(nearex + 2, nearex + 1, len);
1553a86c6181SAlex Tomas 		}
1554a86c6181SAlex Tomas 		path[depth].p_ext = nearex + 1;
1555a86c6181SAlex Tomas 	} else {
1556a86c6181SAlex Tomas 		BUG_ON(newext->ee_block == nearex->ee_block);
1557a86c6181SAlex Tomas 		len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1558a86c6181SAlex Tomas 		len = len < 0 ? 0 : len;
15592ae02107SMingming Cao 		ext_debug("insert %d:%llu:%d before: nearest 0x%p, "
1560a86c6181SAlex Tomas 				"move %d from 0x%p to 0x%p\n",
1561a86c6181SAlex Tomas 				le32_to_cpu(newext->ee_block),
1562f65e6fbaSAlex Tomas 				ext_pblock(newext),
1563a2df2a63SAmit Arora 				ext4_ext_get_actual_len(newext),
1564a86c6181SAlex Tomas 				nearex, len, nearex + 1, nearex + 2);
1565a86c6181SAlex Tomas 		memmove(nearex + 1, nearex, len);
1566a86c6181SAlex Tomas 		path[depth].p_ext = nearex;
1567a86c6181SAlex Tomas 	}
1568a86c6181SAlex Tomas 
1569e8546d06SMarcin Slusarz 	le16_add_cpu(&eh->eh_entries, 1);
1570a86c6181SAlex Tomas 	nearex = path[depth].p_ext;
1571a86c6181SAlex Tomas 	nearex->ee_block = newext->ee_block;
1572b377611dSAneesh Kumar K.V 	ext4_ext_store_pblock(nearex, ext_pblock(newext));
1573a86c6181SAlex Tomas 	nearex->ee_len = newext->ee_len;
1574a86c6181SAlex Tomas 
1575a86c6181SAlex Tomas merge:
1576a86c6181SAlex Tomas 	/* try to merge extents to the right */
157756055d3aSAmit Arora 	ext4_ext_try_to_merge(inode, path, nearex);
1578a86c6181SAlex Tomas 
1579a86c6181SAlex Tomas 	/* try to merge extents to the left */
1580a86c6181SAlex Tomas 
1581a86c6181SAlex Tomas 	/* time to correct all indexes above */
1582a86c6181SAlex Tomas 	err = ext4_ext_correct_indexes(handle, inode, path);
1583a86c6181SAlex Tomas 	if (err)
1584a86c6181SAlex Tomas 		goto cleanup;
1585a86c6181SAlex Tomas 
1586a86c6181SAlex Tomas 	err = ext4_ext_dirty(handle, inode, path + depth);
1587a86c6181SAlex Tomas 
1588a86c6181SAlex Tomas cleanup:
1589a86c6181SAlex Tomas 	if (npath) {
1590a86c6181SAlex Tomas 		ext4_ext_drop_refs(npath);
1591a86c6181SAlex Tomas 		kfree(npath);
1592a86c6181SAlex Tomas 	}
1593a86c6181SAlex Tomas 	ext4_ext_tree_changed(inode);
1594a86c6181SAlex Tomas 	ext4_ext_invalidate_cache(inode);
1595a86c6181SAlex Tomas 	return err;
1596a86c6181SAlex Tomas }
1597a86c6181SAlex Tomas 
159809b88252SAvantika Mathur static void
1599725d26d3SAneesh Kumar K.V ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
1600dd54567aSMingming Cao 			__u32 len, ext4_fsblk_t start, int type)
1601a86c6181SAlex Tomas {
1602a86c6181SAlex Tomas 	struct ext4_ext_cache *cex;
1603a86c6181SAlex Tomas 	BUG_ON(len == 0);
1604a86c6181SAlex Tomas 	cex = &EXT4_I(inode)->i_cached_extent;
1605a86c6181SAlex Tomas 	cex->ec_type = type;
1606a86c6181SAlex Tomas 	cex->ec_block = block;
1607a86c6181SAlex Tomas 	cex->ec_len = len;
1608a86c6181SAlex Tomas 	cex->ec_start = start;
1609a86c6181SAlex Tomas }
1610a86c6181SAlex Tomas 
1611a86c6181SAlex Tomas /*
1612d0d856e8SRandy Dunlap  * ext4_ext_put_gap_in_cache:
1613d0d856e8SRandy Dunlap  * calculate boundaries of the gap that the requested block fits into
1614a86c6181SAlex Tomas  * and cache this gap
1615a86c6181SAlex Tomas  */
161609b88252SAvantika Mathur static void
1617a86c6181SAlex Tomas ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
1618725d26d3SAneesh Kumar K.V 				ext4_lblk_t block)
1619a86c6181SAlex Tomas {
1620a86c6181SAlex Tomas 	int depth = ext_depth(inode);
1621725d26d3SAneesh Kumar K.V 	unsigned long len;
1622725d26d3SAneesh Kumar K.V 	ext4_lblk_t lblock;
1623a86c6181SAlex Tomas 	struct ext4_extent *ex;
1624a86c6181SAlex Tomas 
1625a86c6181SAlex Tomas 	ex = path[depth].p_ext;
1626a86c6181SAlex Tomas 	if (ex == NULL) {
1627a86c6181SAlex Tomas 		/* there is no extent yet, so gap is [0;-] */
1628a86c6181SAlex Tomas 		lblock = 0;
1629a86c6181SAlex Tomas 		len = EXT_MAX_BLOCK;
1630a86c6181SAlex Tomas 		ext_debug("cache gap(whole file):");
1631a86c6181SAlex Tomas 	} else if (block < le32_to_cpu(ex->ee_block)) {
1632a86c6181SAlex Tomas 		lblock = block;
1633a86c6181SAlex Tomas 		len = le32_to_cpu(ex->ee_block) - block;
1634bba90743SEric Sandeen 		ext_debug("cache gap(before): %u [%u:%u]",
1635bba90743SEric Sandeen 				block,
1636bba90743SEric Sandeen 				le32_to_cpu(ex->ee_block),
1637bba90743SEric Sandeen 				 ext4_ext_get_actual_len(ex));
1638a86c6181SAlex Tomas 	} else if (block >= le32_to_cpu(ex->ee_block)
1639a2df2a63SAmit Arora 			+ ext4_ext_get_actual_len(ex)) {
1640725d26d3SAneesh Kumar K.V 		ext4_lblk_t next;
1641a86c6181SAlex Tomas 		lblock = le32_to_cpu(ex->ee_block)
1642a2df2a63SAmit Arora 			+ ext4_ext_get_actual_len(ex);
1643725d26d3SAneesh Kumar K.V 
1644725d26d3SAneesh Kumar K.V 		next = ext4_ext_next_allocated_block(path);
1645bba90743SEric Sandeen 		ext_debug("cache gap(after): [%u:%u] %u",
1646bba90743SEric Sandeen 				le32_to_cpu(ex->ee_block),
1647bba90743SEric Sandeen 				ext4_ext_get_actual_len(ex),
1648bba90743SEric Sandeen 				block);
1649725d26d3SAneesh Kumar K.V 		BUG_ON(next == lblock);
1650725d26d3SAneesh Kumar K.V 		len = next - lblock;
1651a86c6181SAlex Tomas 	} else {
1652a86c6181SAlex Tomas 		lblock = len = 0;
1653a86c6181SAlex Tomas 		BUG();
1654a86c6181SAlex Tomas 	}
1655a86c6181SAlex Tomas 
1656bba90743SEric Sandeen 	ext_debug(" -> %u:%lu\n", lblock, len);
1657a86c6181SAlex Tomas 	ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
1658a86c6181SAlex Tomas }
1659a86c6181SAlex Tomas 
166009b88252SAvantika Mathur static int
1661725d26d3SAneesh Kumar K.V ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
1662a86c6181SAlex Tomas 			struct ext4_extent *ex)
1663a86c6181SAlex Tomas {
1664a86c6181SAlex Tomas 	struct ext4_ext_cache *cex;
1665a86c6181SAlex Tomas 
1666a86c6181SAlex Tomas 	cex = &EXT4_I(inode)->i_cached_extent;
1667a86c6181SAlex Tomas 
1668a86c6181SAlex Tomas 	/* has cache valid data? */
1669a86c6181SAlex Tomas 	if (cex->ec_type == EXT4_EXT_CACHE_NO)
1670a86c6181SAlex Tomas 		return EXT4_EXT_CACHE_NO;
1671a86c6181SAlex Tomas 
1672a86c6181SAlex Tomas 	BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
1673a86c6181SAlex Tomas 			cex->ec_type != EXT4_EXT_CACHE_EXTENT);
1674a86c6181SAlex Tomas 	if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
1675a86c6181SAlex Tomas 		ex->ee_block = cpu_to_le32(cex->ec_block);
1676f65e6fbaSAlex Tomas 		ext4_ext_store_pblock(ex, cex->ec_start);
1677a86c6181SAlex Tomas 		ex->ee_len = cpu_to_le16(cex->ec_len);
1678bba90743SEric Sandeen 		ext_debug("%u cached by %u:%u:%llu\n",
1679bba90743SEric Sandeen 				block,
1680bba90743SEric Sandeen 				cex->ec_block, cex->ec_len, cex->ec_start);
1681a86c6181SAlex Tomas 		return cex->ec_type;
1682a86c6181SAlex Tomas 	}
1683a86c6181SAlex Tomas 
1684a86c6181SAlex Tomas 	/* not in cache */
1685a86c6181SAlex Tomas 	return EXT4_EXT_CACHE_NO;
1686a86c6181SAlex Tomas }
1687a86c6181SAlex Tomas 
1688a86c6181SAlex Tomas /*
1689d0d856e8SRandy Dunlap  * ext4_ext_rm_idx:
1690d0d856e8SRandy Dunlap  * removes index from the index block.
1691d0d856e8SRandy Dunlap  * It's used in truncate case only, thus all requests are for
1692d0d856e8SRandy Dunlap  * last index in the block only.
1693a86c6181SAlex Tomas  */
16941d03ec98SAneesh Kumar K.V static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
1695a86c6181SAlex Tomas 			struct ext4_ext_path *path)
1696a86c6181SAlex Tomas {
1697a86c6181SAlex Tomas 	struct buffer_head *bh;
1698a86c6181SAlex Tomas 	int err;
1699f65e6fbaSAlex Tomas 	ext4_fsblk_t leaf;
1700a86c6181SAlex Tomas 
1701a86c6181SAlex Tomas 	/* free index block */
1702a86c6181SAlex Tomas 	path--;
1703f65e6fbaSAlex Tomas 	leaf = idx_pblock(path->p_idx);
1704a86c6181SAlex Tomas 	BUG_ON(path->p_hdr->eh_entries == 0);
17057e028976SAvantika Mathur 	err = ext4_ext_get_access(handle, inode, path);
17067e028976SAvantika Mathur 	if (err)
1707a86c6181SAlex Tomas 		return err;
1708e8546d06SMarcin Slusarz 	le16_add_cpu(&path->p_hdr->eh_entries, -1);
17097e028976SAvantika Mathur 	err = ext4_ext_dirty(handle, inode, path);
17107e028976SAvantika Mathur 	if (err)
1711a86c6181SAlex Tomas 		return err;
17122ae02107SMingming Cao 	ext_debug("index is empty, remove it, free block %llu\n", leaf);
1713a86c6181SAlex Tomas 	bh = sb_find_get_block(inode->i_sb, leaf);
1714a86c6181SAlex Tomas 	ext4_forget(handle, 1, inode, bh, leaf);
1715c9de560dSAlex Tomas 	ext4_free_blocks(handle, inode, leaf, 1, 1);
1716a86c6181SAlex Tomas 	return err;
1717a86c6181SAlex Tomas }
1718a86c6181SAlex Tomas 
1719a86c6181SAlex Tomas /*
1720d0d856e8SRandy Dunlap  * ext4_ext_calc_credits_for_insert:
1721d0d856e8SRandy Dunlap  * This routine returns max. credits that the extent tree can consume.
1722a86c6181SAlex Tomas  * It should be OK for low-performance paths like ->writepage()
1723d0d856e8SRandy Dunlap  * To allow many writing processes to fit into a single transaction,
17240e855ac8SAneesh Kumar K.V  * the caller should calculate credits under i_data_sem and
1725d0d856e8SRandy Dunlap  * pass the actual path.
1726a86c6181SAlex Tomas  */
172709b88252SAvantika Mathur int ext4_ext_calc_credits_for_insert(struct inode *inode,
1728a86c6181SAlex Tomas 						struct ext4_ext_path *path)
1729a86c6181SAlex Tomas {
1730a86c6181SAlex Tomas 	int depth, needed;
1731a86c6181SAlex Tomas 
1732a86c6181SAlex Tomas 	if (path) {
1733a86c6181SAlex Tomas 		/* probably there is space in leaf? */
1734a86c6181SAlex Tomas 		depth = ext_depth(inode);
1735a86c6181SAlex Tomas 		if (le16_to_cpu(path[depth].p_hdr->eh_entries)
1736a86c6181SAlex Tomas 				< le16_to_cpu(path[depth].p_hdr->eh_max))
1737a86c6181SAlex Tomas 			return 1;
1738a86c6181SAlex Tomas 	}
1739a86c6181SAlex Tomas 
1740a86c6181SAlex Tomas 	/*
1741d0d856e8SRandy Dunlap 	 * given 32-bit logical block (4294967296 blocks), max. tree
1742a86c6181SAlex Tomas 	 * can be 4 levels in depth -- 4 * 340^4 == 53453440000.
1743d0d856e8SRandy Dunlap 	 * Let's also add one more level for imbalance.
1744a86c6181SAlex Tomas 	 */
1745a86c6181SAlex Tomas 	depth = 5;
1746a86c6181SAlex Tomas 
1747a86c6181SAlex Tomas 	/* allocation of new data block(s) */
1748a86c6181SAlex Tomas 	needed = 2;
1749a86c6181SAlex Tomas 
1750a86c6181SAlex Tomas 	/*
1751d0d856e8SRandy Dunlap 	 * tree can be full, so it would need to grow in depth:
1752feb18927SJohann Lombardi 	 * we need one credit to modify old root, credits for
1753feb18927SJohann Lombardi 	 * new root will be added in split accounting
1754a86c6181SAlex Tomas 	 */
1755feb18927SJohann Lombardi 	needed += 1;
1756a86c6181SAlex Tomas 
1757a86c6181SAlex Tomas 	/*
1758d0d856e8SRandy Dunlap 	 * Index split can happen, we would need:
1759a86c6181SAlex Tomas 	 *    allocate intermediate indexes (bitmap + group)
1760a86c6181SAlex Tomas 	 *  + change two blocks at each level, but root (already included)
1761a86c6181SAlex Tomas 	 */
1762feb18927SJohann Lombardi 	needed += (depth * 2) + (depth * 2);
1763a86c6181SAlex Tomas 
1764a86c6181SAlex Tomas 	/* any allocation modifies superblock */
1765a86c6181SAlex Tomas 	needed += 1;
1766a86c6181SAlex Tomas 
1767a86c6181SAlex Tomas 	return needed;
1768a86c6181SAlex Tomas }
1769a86c6181SAlex Tomas 
1770a86c6181SAlex Tomas static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
1771a86c6181SAlex Tomas 				struct ext4_extent *ex,
1772725d26d3SAneesh Kumar K.V 				ext4_lblk_t from, ext4_lblk_t to)
1773a86c6181SAlex Tomas {
1774a86c6181SAlex Tomas 	struct buffer_head *bh;
1775a2df2a63SAmit Arora 	unsigned short ee_len =  ext4_ext_get_actual_len(ex);
1776c9de560dSAlex Tomas 	int i, metadata = 0;
1777a86c6181SAlex Tomas 
1778c9de560dSAlex Tomas 	if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
1779c9de560dSAlex Tomas 		metadata = 1;
1780a86c6181SAlex Tomas #ifdef EXTENTS_STATS
1781a86c6181SAlex Tomas 	{
1782a86c6181SAlex Tomas 		struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1783a86c6181SAlex Tomas 		spin_lock(&sbi->s_ext_stats_lock);
1784a86c6181SAlex Tomas 		sbi->s_ext_blocks += ee_len;
1785a86c6181SAlex Tomas 		sbi->s_ext_extents++;
1786a86c6181SAlex Tomas 		if (ee_len < sbi->s_ext_min)
1787a86c6181SAlex Tomas 			sbi->s_ext_min = ee_len;
1788a86c6181SAlex Tomas 		if (ee_len > sbi->s_ext_max)
1789a86c6181SAlex Tomas 			sbi->s_ext_max = ee_len;
1790a86c6181SAlex Tomas 		if (ext_depth(inode) > sbi->s_depth_max)
1791a86c6181SAlex Tomas 			sbi->s_depth_max = ext_depth(inode);
1792a86c6181SAlex Tomas 		spin_unlock(&sbi->s_ext_stats_lock);
1793a86c6181SAlex Tomas 	}
1794a86c6181SAlex Tomas #endif
1795a86c6181SAlex Tomas 	if (from >= le32_to_cpu(ex->ee_block)
1796a2df2a63SAmit Arora 	    && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
1797a86c6181SAlex Tomas 		/* tail removal */
1798725d26d3SAneesh Kumar K.V 		ext4_lblk_t num;
1799f65e6fbaSAlex Tomas 		ext4_fsblk_t start;
1800725d26d3SAneesh Kumar K.V 
1801a2df2a63SAmit Arora 		num = le32_to_cpu(ex->ee_block) + ee_len - from;
1802a2df2a63SAmit Arora 		start = ext_pblock(ex) + ee_len - num;
1803725d26d3SAneesh Kumar K.V 		ext_debug("free last %u blocks starting %llu\n", num, start);
1804a86c6181SAlex Tomas 		for (i = 0; i < num; i++) {
1805a86c6181SAlex Tomas 			bh = sb_find_get_block(inode->i_sb, start + i);
1806a86c6181SAlex Tomas 			ext4_forget(handle, 0, inode, bh, start + i);
1807a86c6181SAlex Tomas 		}
1808c9de560dSAlex Tomas 		ext4_free_blocks(handle, inode, start, num, metadata);
1809a86c6181SAlex Tomas 	} else if (from == le32_to_cpu(ex->ee_block)
1810a2df2a63SAmit Arora 		   && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
1811725d26d3SAneesh Kumar K.V 		printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n",
1812a2df2a63SAmit Arora 			from, to, le32_to_cpu(ex->ee_block), ee_len);
1813a86c6181SAlex Tomas 	} else {
1814725d26d3SAneesh Kumar K.V 		printk(KERN_INFO "strange request: removal(2) "
1815725d26d3SAneesh Kumar K.V 				"%u-%u from %u:%u\n",
1816a2df2a63SAmit Arora 				from, to, le32_to_cpu(ex->ee_block), ee_len);
1817a86c6181SAlex Tomas 	}
1818a86c6181SAlex Tomas 	return 0;
1819a86c6181SAlex Tomas }
1820a86c6181SAlex Tomas 
1821a86c6181SAlex Tomas static int
1822a86c6181SAlex Tomas ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
1823725d26d3SAneesh Kumar K.V 		struct ext4_ext_path *path, ext4_lblk_t start)
1824a86c6181SAlex Tomas {
1825a86c6181SAlex Tomas 	int err = 0, correct_index = 0;
1826a86c6181SAlex Tomas 	int depth = ext_depth(inode), credits;
1827a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
1828725d26d3SAneesh Kumar K.V 	ext4_lblk_t a, b, block;
1829725d26d3SAneesh Kumar K.V 	unsigned num;
1830725d26d3SAneesh Kumar K.V 	ext4_lblk_t ex_ee_block;
1831a86c6181SAlex Tomas 	unsigned short ex_ee_len;
1832a2df2a63SAmit Arora 	unsigned uninitialized = 0;
1833a86c6181SAlex Tomas 	struct ext4_extent *ex;
1834a86c6181SAlex Tomas 
1835c29c0ae7SAlex Tomas 	/* the header must be checked already in ext4_ext_remove_space() */
1836725d26d3SAneesh Kumar K.V 	ext_debug("truncate since %u in leaf\n", start);
1837a86c6181SAlex Tomas 	if (!path[depth].p_hdr)
1838a86c6181SAlex Tomas 		path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
1839a86c6181SAlex Tomas 	eh = path[depth].p_hdr;
1840a86c6181SAlex Tomas 	BUG_ON(eh == NULL);
1841a86c6181SAlex Tomas 
1842a86c6181SAlex Tomas 	/* find where to start removing */
1843a86c6181SAlex Tomas 	ex = EXT_LAST_EXTENT(eh);
1844a86c6181SAlex Tomas 
1845a86c6181SAlex Tomas 	ex_ee_block = le32_to_cpu(ex->ee_block);
1846a2df2a63SAmit Arora 	if (ext4_ext_is_uninitialized(ex))
1847a2df2a63SAmit Arora 		uninitialized = 1;
1848a2df2a63SAmit Arora 	ex_ee_len = ext4_ext_get_actual_len(ex);
1849a86c6181SAlex Tomas 
1850a86c6181SAlex Tomas 	while (ex >= EXT_FIRST_EXTENT(eh) &&
1851a86c6181SAlex Tomas 			ex_ee_block + ex_ee_len > start) {
1852a86c6181SAlex Tomas 		ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len);
1853a86c6181SAlex Tomas 		path[depth].p_ext = ex;
1854a86c6181SAlex Tomas 
1855a86c6181SAlex Tomas 		a = ex_ee_block > start ? ex_ee_block : start;
1856a86c6181SAlex Tomas 		b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
1857a86c6181SAlex Tomas 			ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
1858a86c6181SAlex Tomas 
1859a86c6181SAlex Tomas 		ext_debug("  border %u:%u\n", a, b);
1860a86c6181SAlex Tomas 
1861a86c6181SAlex Tomas 		if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
1862a86c6181SAlex Tomas 			block = 0;
1863a86c6181SAlex Tomas 			num = 0;
1864a86c6181SAlex Tomas 			BUG();
1865a86c6181SAlex Tomas 		} else if (a != ex_ee_block) {
1866a86c6181SAlex Tomas 			/* remove tail of the extent */
1867a86c6181SAlex Tomas 			block = ex_ee_block;
1868a86c6181SAlex Tomas 			num = a - block;
1869a86c6181SAlex Tomas 		} else if (b != ex_ee_block + ex_ee_len - 1) {
1870a86c6181SAlex Tomas 			/* remove head of the extent */
1871a86c6181SAlex Tomas 			block = a;
1872a86c6181SAlex Tomas 			num = b - a;
1873a86c6181SAlex Tomas 			/* there is no "make a hole" API yet */
1874a86c6181SAlex Tomas 			BUG();
1875a86c6181SAlex Tomas 		} else {
1876a86c6181SAlex Tomas 			/* remove whole extent: excellent! */
1877a86c6181SAlex Tomas 			block = ex_ee_block;
1878a86c6181SAlex Tomas 			num = 0;
1879a86c6181SAlex Tomas 			BUG_ON(a != ex_ee_block);
1880a86c6181SAlex Tomas 			BUG_ON(b != ex_ee_block + ex_ee_len - 1);
1881a86c6181SAlex Tomas 		}
1882a86c6181SAlex Tomas 
1883d0d856e8SRandy Dunlap 		/* at present, extent can't cross block group: */
1884a86c6181SAlex Tomas 		/* leaf + bitmap + group desc + sb + inode */
1885a86c6181SAlex Tomas 		credits = 5;
1886a86c6181SAlex Tomas 		if (ex == EXT_FIRST_EXTENT(eh)) {
1887a86c6181SAlex Tomas 			correct_index = 1;
1888a86c6181SAlex Tomas 			credits += (ext_depth(inode)) + 1;
1889a86c6181SAlex Tomas 		}
1890a86c6181SAlex Tomas #ifdef CONFIG_QUOTA
1891a86c6181SAlex Tomas 		credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
1892a86c6181SAlex Tomas #endif
1893a86c6181SAlex Tomas 
18949102e4faSShen Feng 		err = ext4_ext_journal_restart(handle, credits);
18959102e4faSShen Feng 		if (err)
1896a86c6181SAlex Tomas 			goto out;
1897a86c6181SAlex Tomas 
1898a86c6181SAlex Tomas 		err = ext4_ext_get_access(handle, inode, path + depth);
1899a86c6181SAlex Tomas 		if (err)
1900a86c6181SAlex Tomas 			goto out;
1901a86c6181SAlex Tomas 
1902a86c6181SAlex Tomas 		err = ext4_remove_blocks(handle, inode, ex, a, b);
1903a86c6181SAlex Tomas 		if (err)
1904a86c6181SAlex Tomas 			goto out;
1905a86c6181SAlex Tomas 
1906a86c6181SAlex Tomas 		if (num == 0) {
1907d0d856e8SRandy Dunlap 			/* this extent is removed; mark slot entirely unused */
1908f65e6fbaSAlex Tomas 			ext4_ext_store_pblock(ex, 0);
1909e8546d06SMarcin Slusarz 			le16_add_cpu(&eh->eh_entries, -1);
1910a86c6181SAlex Tomas 		}
1911a86c6181SAlex Tomas 
1912a86c6181SAlex Tomas 		ex->ee_block = cpu_to_le32(block);
1913a86c6181SAlex Tomas 		ex->ee_len = cpu_to_le16(num);
1914749269faSAmit Arora 		/*
1915749269faSAmit Arora 		 * Do not mark uninitialized if all the blocks in the
1916749269faSAmit Arora 		 * extent have been removed.
1917749269faSAmit Arora 		 */
1918749269faSAmit Arora 		if (uninitialized && num)
1919a2df2a63SAmit Arora 			ext4_ext_mark_uninitialized(ex);
1920a86c6181SAlex Tomas 
1921a86c6181SAlex Tomas 		err = ext4_ext_dirty(handle, inode, path + depth);
1922a86c6181SAlex Tomas 		if (err)
1923a86c6181SAlex Tomas 			goto out;
1924a86c6181SAlex Tomas 
19252ae02107SMingming Cao 		ext_debug("new extent: %u:%u:%llu\n", block, num,
1926f65e6fbaSAlex Tomas 				ext_pblock(ex));
1927a86c6181SAlex Tomas 		ex--;
1928a86c6181SAlex Tomas 		ex_ee_block = le32_to_cpu(ex->ee_block);
1929a2df2a63SAmit Arora 		ex_ee_len = ext4_ext_get_actual_len(ex);
1930a86c6181SAlex Tomas 	}
1931a86c6181SAlex Tomas 
1932a86c6181SAlex Tomas 	if (correct_index && eh->eh_entries)
1933a86c6181SAlex Tomas 		err = ext4_ext_correct_indexes(handle, inode, path);
1934a86c6181SAlex Tomas 
1935a86c6181SAlex Tomas 	/* if this leaf is free, then we should
1936a86c6181SAlex Tomas 	 * remove it from index block above */
1937a86c6181SAlex Tomas 	if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
1938a86c6181SAlex Tomas 		err = ext4_ext_rm_idx(handle, inode, path + depth);
1939a86c6181SAlex Tomas 
1940a86c6181SAlex Tomas out:
1941a86c6181SAlex Tomas 	return err;
1942a86c6181SAlex Tomas }
1943a86c6181SAlex Tomas 
1944a86c6181SAlex Tomas /*
1945d0d856e8SRandy Dunlap  * ext4_ext_more_to_rm:
1946d0d856e8SRandy Dunlap  * returns 1 if current index has to be freed (even partial)
1947a86c6181SAlex Tomas  */
194809b88252SAvantika Mathur static int
1949a86c6181SAlex Tomas ext4_ext_more_to_rm(struct ext4_ext_path *path)
1950a86c6181SAlex Tomas {
1951a86c6181SAlex Tomas 	BUG_ON(path->p_idx == NULL);
1952a86c6181SAlex Tomas 
1953a86c6181SAlex Tomas 	if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
1954a86c6181SAlex Tomas 		return 0;
1955a86c6181SAlex Tomas 
1956a86c6181SAlex Tomas 	/*
1957d0d856e8SRandy Dunlap 	 * if truncate on deeper level happened, it wasn't partial,
1958a86c6181SAlex Tomas 	 * so we have to consider current index for truncation
1959a86c6181SAlex Tomas 	 */
1960a86c6181SAlex Tomas 	if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
1961a86c6181SAlex Tomas 		return 0;
1962a86c6181SAlex Tomas 	return 1;
1963a86c6181SAlex Tomas }
1964a86c6181SAlex Tomas 
19651d03ec98SAneesh Kumar K.V static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
1966a86c6181SAlex Tomas {
1967a86c6181SAlex Tomas 	struct super_block *sb = inode->i_sb;
1968a86c6181SAlex Tomas 	int depth = ext_depth(inode);
1969a86c6181SAlex Tomas 	struct ext4_ext_path *path;
1970a86c6181SAlex Tomas 	handle_t *handle;
1971a86c6181SAlex Tomas 	int i = 0, err = 0;
1972a86c6181SAlex Tomas 
1973725d26d3SAneesh Kumar K.V 	ext_debug("truncate since %u\n", start);
1974a86c6181SAlex Tomas 
1975a86c6181SAlex Tomas 	/* probably first extent we're gonna free will be last in block */
1976a86c6181SAlex Tomas 	handle = ext4_journal_start(inode, depth + 1);
1977a86c6181SAlex Tomas 	if (IS_ERR(handle))
1978a86c6181SAlex Tomas 		return PTR_ERR(handle);
1979a86c6181SAlex Tomas 
1980a86c6181SAlex Tomas 	ext4_ext_invalidate_cache(inode);
1981a86c6181SAlex Tomas 
1982a86c6181SAlex Tomas 	/*
1983d0d856e8SRandy Dunlap 	 * We start scanning from right side, freeing all the blocks
1984d0d856e8SRandy Dunlap 	 * after i_size and walking into the tree depth-wise.
1985a86c6181SAlex Tomas 	 */
1986216553c4SJosef Bacik 	path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
1987a86c6181SAlex Tomas 	if (path == NULL) {
1988a86c6181SAlex Tomas 		ext4_journal_stop(handle);
1989a86c6181SAlex Tomas 		return -ENOMEM;
1990a86c6181SAlex Tomas 	}
1991a86c6181SAlex Tomas 	path[0].p_hdr = ext_inode_hdr(inode);
1992c29c0ae7SAlex Tomas 	if (ext4_ext_check_header(inode, path[0].p_hdr, depth)) {
1993a86c6181SAlex Tomas 		err = -EIO;
1994a86c6181SAlex Tomas 		goto out;
1995a86c6181SAlex Tomas 	}
1996a86c6181SAlex Tomas 	path[0].p_depth = depth;
1997a86c6181SAlex Tomas 
1998a86c6181SAlex Tomas 	while (i >= 0 && err == 0) {
1999a86c6181SAlex Tomas 		if (i == depth) {
2000a86c6181SAlex Tomas 			/* this is leaf block */
2001a86c6181SAlex Tomas 			err = ext4_ext_rm_leaf(handle, inode, path, start);
2002d0d856e8SRandy Dunlap 			/* root level has p_bh == NULL, brelse() eats this */
2003a86c6181SAlex Tomas 			brelse(path[i].p_bh);
2004a86c6181SAlex Tomas 			path[i].p_bh = NULL;
2005a86c6181SAlex Tomas 			i--;
2006a86c6181SAlex Tomas 			continue;
2007a86c6181SAlex Tomas 		}
2008a86c6181SAlex Tomas 
2009a86c6181SAlex Tomas 		/* this is index block */
2010a86c6181SAlex Tomas 		if (!path[i].p_hdr) {
2011a86c6181SAlex Tomas 			ext_debug("initialize header\n");
2012a86c6181SAlex Tomas 			path[i].p_hdr = ext_block_hdr(path[i].p_bh);
2013a86c6181SAlex Tomas 		}
2014a86c6181SAlex Tomas 
2015a86c6181SAlex Tomas 		if (!path[i].p_idx) {
2016d0d856e8SRandy Dunlap 			/* this level hasn't been touched yet */
2017a86c6181SAlex Tomas 			path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2018a86c6181SAlex Tomas 			path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2019a86c6181SAlex Tomas 			ext_debug("init index ptr: hdr 0x%p, num %d\n",
2020a86c6181SAlex Tomas 				  path[i].p_hdr,
2021a86c6181SAlex Tomas 				  le16_to_cpu(path[i].p_hdr->eh_entries));
2022a86c6181SAlex Tomas 		} else {
2023d0d856e8SRandy Dunlap 			/* we were already here, see at next index */
2024a86c6181SAlex Tomas 			path[i].p_idx--;
2025a86c6181SAlex Tomas 		}
2026a86c6181SAlex Tomas 
2027a86c6181SAlex Tomas 		ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2028a86c6181SAlex Tomas 				i, EXT_FIRST_INDEX(path[i].p_hdr),
2029a86c6181SAlex Tomas 				path[i].p_idx);
2030a86c6181SAlex Tomas 		if (ext4_ext_more_to_rm(path + i)) {
2031c29c0ae7SAlex Tomas 			struct buffer_head *bh;
2032a86c6181SAlex Tomas 			/* go to the next level */
20332ae02107SMingming Cao 			ext_debug("move to level %d (block %llu)\n",
2034f65e6fbaSAlex Tomas 				  i + 1, idx_pblock(path[i].p_idx));
2035a86c6181SAlex Tomas 			memset(path + i + 1, 0, sizeof(*path));
2036c29c0ae7SAlex Tomas 			bh = sb_bread(sb, idx_pblock(path[i].p_idx));
2037c29c0ae7SAlex Tomas 			if (!bh) {
2038a86c6181SAlex Tomas 				/* should we reset i_size? */
2039a86c6181SAlex Tomas 				err = -EIO;
2040a86c6181SAlex Tomas 				break;
2041a86c6181SAlex Tomas 			}
2042c29c0ae7SAlex Tomas 			if (WARN_ON(i + 1 > depth)) {
2043c29c0ae7SAlex Tomas 				err = -EIO;
2044c29c0ae7SAlex Tomas 				break;
2045c29c0ae7SAlex Tomas 			}
2046c29c0ae7SAlex Tomas 			if (ext4_ext_check_header(inode, ext_block_hdr(bh),
2047c29c0ae7SAlex Tomas 							depth - i - 1)) {
2048c29c0ae7SAlex Tomas 				err = -EIO;
2049c29c0ae7SAlex Tomas 				break;
2050c29c0ae7SAlex Tomas 			}
2051c29c0ae7SAlex Tomas 			path[i + 1].p_bh = bh;
2052a86c6181SAlex Tomas 
2053d0d856e8SRandy Dunlap 			/* save actual number of indexes since this
2054d0d856e8SRandy Dunlap 			 * number is changed at the next iteration */
2055a86c6181SAlex Tomas 			path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2056a86c6181SAlex Tomas 			i++;
2057a86c6181SAlex Tomas 		} else {
2058d0d856e8SRandy Dunlap 			/* we finished processing this index, go up */
2059a86c6181SAlex Tomas 			if (path[i].p_hdr->eh_entries == 0 && i > 0) {
2060d0d856e8SRandy Dunlap 				/* index is empty, remove it;
2061a86c6181SAlex Tomas 				 * handle must be already prepared by the
2062a86c6181SAlex Tomas 				 * truncatei_leaf() */
2063a86c6181SAlex Tomas 				err = ext4_ext_rm_idx(handle, inode, path + i);
2064a86c6181SAlex Tomas 			}
2065d0d856e8SRandy Dunlap 			/* root level has p_bh == NULL, brelse() eats this */
2066a86c6181SAlex Tomas 			brelse(path[i].p_bh);
2067a86c6181SAlex Tomas 			path[i].p_bh = NULL;
2068a86c6181SAlex Tomas 			i--;
2069a86c6181SAlex Tomas 			ext_debug("return to level %d\n", i);
2070a86c6181SAlex Tomas 		}
2071a86c6181SAlex Tomas 	}
2072a86c6181SAlex Tomas 
2073a86c6181SAlex Tomas 	/* TODO: flexible tree reduction should be here */
2074a86c6181SAlex Tomas 	if (path->p_hdr->eh_entries == 0) {
2075a86c6181SAlex Tomas 		/*
2076d0d856e8SRandy Dunlap 		 * truncate to zero freed all the tree,
2077d0d856e8SRandy Dunlap 		 * so we need to correct eh_depth
2078a86c6181SAlex Tomas 		 */
2079a86c6181SAlex Tomas 		err = ext4_ext_get_access(handle, inode, path);
2080a86c6181SAlex Tomas 		if (err == 0) {
2081a86c6181SAlex Tomas 			ext_inode_hdr(inode)->eh_depth = 0;
2082a86c6181SAlex Tomas 			ext_inode_hdr(inode)->eh_max =
2083a86c6181SAlex Tomas 				cpu_to_le16(ext4_ext_space_root(inode));
2084a86c6181SAlex Tomas 			err = ext4_ext_dirty(handle, inode, path);
2085a86c6181SAlex Tomas 		}
2086a86c6181SAlex Tomas 	}
2087a86c6181SAlex Tomas out:
2088a86c6181SAlex Tomas 	ext4_ext_tree_changed(inode);
2089a86c6181SAlex Tomas 	ext4_ext_drop_refs(path);
2090a86c6181SAlex Tomas 	kfree(path);
2091a86c6181SAlex Tomas 	ext4_journal_stop(handle);
2092a86c6181SAlex Tomas 
2093a86c6181SAlex Tomas 	return err;
2094a86c6181SAlex Tomas }
2095a86c6181SAlex Tomas 
2096a86c6181SAlex Tomas /*
2097a86c6181SAlex Tomas  * called at mount time
2098a86c6181SAlex Tomas  */
2099a86c6181SAlex Tomas void ext4_ext_init(struct super_block *sb)
2100a86c6181SAlex Tomas {
2101a86c6181SAlex Tomas 	/*
2102a86c6181SAlex Tomas 	 * possible initialization would be here
2103a86c6181SAlex Tomas 	 */
2104a86c6181SAlex Tomas 
2105a86c6181SAlex Tomas 	if (test_opt(sb, EXTENTS)) {
2106a86c6181SAlex Tomas 		printk("EXT4-fs: file extents enabled");
2107bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST
2108bbf2f9fbSRobert P. J. Day 		printk(", aggressive tests");
2109a86c6181SAlex Tomas #endif
2110a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH
2111a86c6181SAlex Tomas 		printk(", check binsearch");
2112a86c6181SAlex Tomas #endif
2113a86c6181SAlex Tomas #ifdef EXTENTS_STATS
2114a86c6181SAlex Tomas 		printk(", stats");
2115a86c6181SAlex Tomas #endif
2116a86c6181SAlex Tomas 		printk("\n");
2117a86c6181SAlex Tomas #ifdef EXTENTS_STATS
2118a86c6181SAlex Tomas 		spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2119a86c6181SAlex Tomas 		EXT4_SB(sb)->s_ext_min = 1 << 30;
2120a86c6181SAlex Tomas 		EXT4_SB(sb)->s_ext_max = 0;
2121a86c6181SAlex Tomas #endif
2122a86c6181SAlex Tomas 	}
2123a86c6181SAlex Tomas }
2124a86c6181SAlex Tomas 
2125a86c6181SAlex Tomas /*
2126a86c6181SAlex Tomas  * called at umount time
2127a86c6181SAlex Tomas  */
2128a86c6181SAlex Tomas void ext4_ext_release(struct super_block *sb)
2129a86c6181SAlex Tomas {
2130a86c6181SAlex Tomas 	if (!test_opt(sb, EXTENTS))
2131a86c6181SAlex Tomas 		return;
2132a86c6181SAlex Tomas 
2133a86c6181SAlex Tomas #ifdef EXTENTS_STATS
2134a86c6181SAlex Tomas 	if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2135a86c6181SAlex Tomas 		struct ext4_sb_info *sbi = EXT4_SB(sb);
2136a86c6181SAlex Tomas 		printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2137a86c6181SAlex Tomas 			sbi->s_ext_blocks, sbi->s_ext_extents,
2138a86c6181SAlex Tomas 			sbi->s_ext_blocks / sbi->s_ext_extents);
2139a86c6181SAlex Tomas 		printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2140a86c6181SAlex Tomas 			sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2141a86c6181SAlex Tomas 	}
2142a86c6181SAlex Tomas #endif
2143a86c6181SAlex Tomas }
2144a86c6181SAlex Tomas 
2145093a088bSAneesh Kumar K.V static void bi_complete(struct bio *bio, int error)
2146093a088bSAneesh Kumar K.V {
2147093a088bSAneesh Kumar K.V 	complete((struct completion *)bio->bi_private);
2148093a088bSAneesh Kumar K.V }
2149093a088bSAneesh Kumar K.V 
2150093a088bSAneesh Kumar K.V /* FIXME!! we need to try to merge to left or right after zero-out  */
2151093a088bSAneesh Kumar K.V static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2152093a088bSAneesh Kumar K.V {
2153093a088bSAneesh Kumar K.V 	int ret = -EIO;
2154093a088bSAneesh Kumar K.V 	struct bio *bio;
2155093a088bSAneesh Kumar K.V 	int blkbits, blocksize;
2156093a088bSAneesh Kumar K.V 	sector_t ee_pblock;
2157093a088bSAneesh Kumar K.V 	struct completion event;
2158093a088bSAneesh Kumar K.V 	unsigned int ee_len, len, done, offset;
2159093a088bSAneesh Kumar K.V 
2160093a088bSAneesh Kumar K.V 
2161093a088bSAneesh Kumar K.V 	blkbits   = inode->i_blkbits;
2162093a088bSAneesh Kumar K.V 	blocksize = inode->i_sb->s_blocksize;
2163093a088bSAneesh Kumar K.V 	ee_len    = ext4_ext_get_actual_len(ex);
2164093a088bSAneesh Kumar K.V 	ee_pblock = ext_pblock(ex);
2165093a088bSAneesh Kumar K.V 
2166093a088bSAneesh Kumar K.V 	/* convert ee_pblock to 512 byte sectors */
2167093a088bSAneesh Kumar K.V 	ee_pblock = ee_pblock << (blkbits - 9);
2168093a088bSAneesh Kumar K.V 
2169093a088bSAneesh Kumar K.V 	while (ee_len > 0) {
2170093a088bSAneesh Kumar K.V 
2171093a088bSAneesh Kumar K.V 		if (ee_len > BIO_MAX_PAGES)
2172093a088bSAneesh Kumar K.V 			len = BIO_MAX_PAGES;
2173093a088bSAneesh Kumar K.V 		else
2174093a088bSAneesh Kumar K.V 			len = ee_len;
2175093a088bSAneesh Kumar K.V 
2176093a088bSAneesh Kumar K.V 		bio = bio_alloc(GFP_NOIO, len);
2177093a088bSAneesh Kumar K.V 		if (!bio)
2178093a088bSAneesh Kumar K.V 			return -ENOMEM;
2179093a088bSAneesh Kumar K.V 		bio->bi_sector = ee_pblock;
2180093a088bSAneesh Kumar K.V 		bio->bi_bdev   = inode->i_sb->s_bdev;
2181093a088bSAneesh Kumar K.V 
2182093a088bSAneesh Kumar K.V 		done = 0;
2183093a088bSAneesh Kumar K.V 		offset = 0;
2184093a088bSAneesh Kumar K.V 		while (done < len) {
2185093a088bSAneesh Kumar K.V 			ret = bio_add_page(bio, ZERO_PAGE(0),
2186093a088bSAneesh Kumar K.V 							blocksize, offset);
2187093a088bSAneesh Kumar K.V 			if (ret != blocksize) {
2188093a088bSAneesh Kumar K.V 				/*
2189093a088bSAneesh Kumar K.V 				 * We can't add any more pages because of
2190093a088bSAneesh Kumar K.V 				 * hardware limitations.  Start a new bio.
2191093a088bSAneesh Kumar K.V 				 */
2192093a088bSAneesh Kumar K.V 				break;
2193093a088bSAneesh Kumar K.V 			}
2194093a088bSAneesh Kumar K.V 			done++;
2195093a088bSAneesh Kumar K.V 			offset += blocksize;
2196093a088bSAneesh Kumar K.V 			if (offset >= PAGE_CACHE_SIZE)
2197093a088bSAneesh Kumar K.V 				offset = 0;
2198093a088bSAneesh Kumar K.V 		}
2199093a088bSAneesh Kumar K.V 
2200093a088bSAneesh Kumar K.V 		init_completion(&event);
2201093a088bSAneesh Kumar K.V 		bio->bi_private = &event;
2202093a088bSAneesh Kumar K.V 		bio->bi_end_io = bi_complete;
2203093a088bSAneesh Kumar K.V 		submit_bio(WRITE, bio);
2204093a088bSAneesh Kumar K.V 		wait_for_completion(&event);
2205093a088bSAneesh Kumar K.V 
2206093a088bSAneesh Kumar K.V 		if (test_bit(BIO_UPTODATE, &bio->bi_flags))
2207093a088bSAneesh Kumar K.V 			ret = 0;
2208093a088bSAneesh Kumar K.V 		else {
2209093a088bSAneesh Kumar K.V 			ret = -EIO;
2210093a088bSAneesh Kumar K.V 			break;
2211093a088bSAneesh Kumar K.V 		}
2212093a088bSAneesh Kumar K.V 		bio_put(bio);
2213093a088bSAneesh Kumar K.V 		ee_len    -= done;
2214093a088bSAneesh Kumar K.V 		ee_pblock += done  << (blkbits - 9);
2215093a088bSAneesh Kumar K.V 	}
2216093a088bSAneesh Kumar K.V 	return ret;
2217093a088bSAneesh Kumar K.V }
2218093a088bSAneesh Kumar K.V 
22193977c965SAneesh Kumar K.V #define EXT4_EXT_ZERO_LEN 7
22203977c965SAneesh Kumar K.V 
222156055d3aSAmit Arora /*
222256055d3aSAmit Arora  * This function is called by ext4_ext_get_blocks() if someone tries to write
222356055d3aSAmit Arora  * to an uninitialized extent. It may result in splitting the uninitialized
222456055d3aSAmit Arora  * extent into multiple extents (upto three - one initialized and two
222556055d3aSAmit Arora  * uninitialized).
222656055d3aSAmit Arora  * There are three possibilities:
222756055d3aSAmit Arora  *   a> There is no split required: Entire extent should be initialized
222856055d3aSAmit Arora  *   b> Splits in two extents: Write is happening at either end of the extent
222956055d3aSAmit Arora  *   c> Splits in three extents: Somone is writing in middle of the extent
223056055d3aSAmit Arora  */
2231725d26d3SAneesh Kumar K.V static int ext4_ext_convert_to_initialized(handle_t *handle,
2232725d26d3SAneesh Kumar K.V 						struct inode *inode,
223356055d3aSAmit Arora 						struct ext4_ext_path *path,
2234725d26d3SAneesh Kumar K.V 						ext4_lblk_t iblock,
223556055d3aSAmit Arora 						unsigned long max_blocks)
223656055d3aSAmit Arora {
223795c3889cSAneesh Kumar K.V 	struct ext4_extent *ex, newex, orig_ex;
223856055d3aSAmit Arora 	struct ext4_extent *ex1 = NULL;
223956055d3aSAmit Arora 	struct ext4_extent *ex2 = NULL;
224056055d3aSAmit Arora 	struct ext4_extent *ex3 = NULL;
224156055d3aSAmit Arora 	struct ext4_extent_header *eh;
2242725d26d3SAneesh Kumar K.V 	ext4_lblk_t ee_block;
2243725d26d3SAneesh Kumar K.V 	unsigned int allocated, ee_len, depth;
224456055d3aSAmit Arora 	ext4_fsblk_t newblock;
224556055d3aSAmit Arora 	int err = 0;
224656055d3aSAmit Arora 	int ret = 0;
224756055d3aSAmit Arora 
224856055d3aSAmit Arora 	depth = ext_depth(inode);
224956055d3aSAmit Arora 	eh = path[depth].p_hdr;
225056055d3aSAmit Arora 	ex = path[depth].p_ext;
225156055d3aSAmit Arora 	ee_block = le32_to_cpu(ex->ee_block);
225256055d3aSAmit Arora 	ee_len = ext4_ext_get_actual_len(ex);
225356055d3aSAmit Arora 	allocated = ee_len - (iblock - ee_block);
225456055d3aSAmit Arora 	newblock = iblock - ee_block + ext_pblock(ex);
225556055d3aSAmit Arora 	ex2 = ex;
225695c3889cSAneesh Kumar K.V 	orig_ex.ee_block = ex->ee_block;
225795c3889cSAneesh Kumar K.V 	orig_ex.ee_len   = cpu_to_le16(ee_len);
225895c3889cSAneesh Kumar K.V 	ext4_ext_store_pblock(&orig_ex, ext_pblock(ex));
225956055d3aSAmit Arora 
22609df5643aSAneesh Kumar K.V 	err = ext4_ext_get_access(handle, inode, path + depth);
22619df5643aSAneesh Kumar K.V 	if (err)
22629df5643aSAneesh Kumar K.V 		goto out;
22633977c965SAneesh Kumar K.V 	/* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
22643977c965SAneesh Kumar K.V 	if (ee_len <= 2*EXT4_EXT_ZERO_LEN) {
22653977c965SAneesh Kumar K.V 		err =  ext4_ext_zeroout(inode, &orig_ex);
22663977c965SAneesh Kumar K.V 		if (err)
22673977c965SAneesh Kumar K.V 			goto fix_extent_len;
22683977c965SAneesh Kumar K.V 		/* update the extent length and mark as initialized */
22693977c965SAneesh Kumar K.V 		ex->ee_block = orig_ex.ee_block;
22703977c965SAneesh Kumar K.V 		ex->ee_len   = orig_ex.ee_len;
22713977c965SAneesh Kumar K.V 		ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
22723977c965SAneesh Kumar K.V 		ext4_ext_dirty(handle, inode, path + depth);
2273161e7b7cSAneesh Kumar K.V 		/* zeroed the full extent */
2274161e7b7cSAneesh Kumar K.V 		return allocated;
22753977c965SAneesh Kumar K.V 	}
22769df5643aSAneesh Kumar K.V 
227756055d3aSAmit Arora 	/* ex1: ee_block to iblock - 1 : uninitialized */
227856055d3aSAmit Arora 	if (iblock > ee_block) {
227956055d3aSAmit Arora 		ex1 = ex;
228056055d3aSAmit Arora 		ex1->ee_len = cpu_to_le16(iblock - ee_block);
228156055d3aSAmit Arora 		ext4_ext_mark_uninitialized(ex1);
228256055d3aSAmit Arora 		ex2 = &newex;
228356055d3aSAmit Arora 	}
228456055d3aSAmit Arora 	/*
228556055d3aSAmit Arora 	 * for sanity, update the length of the ex2 extent before
228656055d3aSAmit Arora 	 * we insert ex3, if ex1 is NULL. This is to avoid temporary
228756055d3aSAmit Arora 	 * overlap of blocks.
228856055d3aSAmit Arora 	 */
228956055d3aSAmit Arora 	if (!ex1 && allocated > max_blocks)
229056055d3aSAmit Arora 		ex2->ee_len = cpu_to_le16(max_blocks);
229156055d3aSAmit Arora 	/* ex3: to ee_block + ee_len : uninitialised */
229256055d3aSAmit Arora 	if (allocated > max_blocks) {
229356055d3aSAmit Arora 		unsigned int newdepth;
22943977c965SAneesh Kumar K.V 		/* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */
22953977c965SAneesh Kumar K.V 		if (allocated <= EXT4_EXT_ZERO_LEN) {
22963977c965SAneesh Kumar K.V 			/* Mark first half uninitialized.
22973977c965SAneesh Kumar K.V 			 * Mark second half initialized and zero out the
22983977c965SAneesh Kumar K.V 			 * initialized extent
22993977c965SAneesh Kumar K.V 			 */
23003977c965SAneesh Kumar K.V 			ex->ee_block = orig_ex.ee_block;
23013977c965SAneesh Kumar K.V 			ex->ee_len   = cpu_to_le16(ee_len - allocated);
23023977c965SAneesh Kumar K.V 			ext4_ext_mark_uninitialized(ex);
23033977c965SAneesh Kumar K.V 			ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
23043977c965SAneesh Kumar K.V 			ext4_ext_dirty(handle, inode, path + depth);
23053977c965SAneesh Kumar K.V 
23063977c965SAneesh Kumar K.V 			ex3 = &newex;
23073977c965SAneesh Kumar K.V 			ex3->ee_block = cpu_to_le32(iblock);
23083977c965SAneesh Kumar K.V 			ext4_ext_store_pblock(ex3, newblock);
23093977c965SAneesh Kumar K.V 			ex3->ee_len = cpu_to_le16(allocated);
23103977c965SAneesh Kumar K.V 			err = ext4_ext_insert_extent(handle, inode, path, ex3);
23113977c965SAneesh Kumar K.V 			if (err == -ENOSPC) {
23123977c965SAneesh Kumar K.V 				err =  ext4_ext_zeroout(inode, &orig_ex);
23133977c965SAneesh Kumar K.V 				if (err)
23143977c965SAneesh Kumar K.V 					goto fix_extent_len;
23153977c965SAneesh Kumar K.V 				ex->ee_block = orig_ex.ee_block;
23163977c965SAneesh Kumar K.V 				ex->ee_len   = orig_ex.ee_len;
23173977c965SAneesh Kumar K.V 				ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
23183977c965SAneesh Kumar K.V 				ext4_ext_dirty(handle, inode, path + depth);
2319161e7b7cSAneesh Kumar K.V 				/* zeroed the full extent */
2320161e7b7cSAneesh Kumar K.V 				return allocated;
23213977c965SAneesh Kumar K.V 
23223977c965SAneesh Kumar K.V 			} else if (err)
23233977c965SAneesh Kumar K.V 				goto fix_extent_len;
23243977c965SAneesh Kumar K.V 
2325161e7b7cSAneesh Kumar K.V 			/*
2326161e7b7cSAneesh Kumar K.V 			 * We need to zero out the second half because
2327161e7b7cSAneesh Kumar K.V 			 * an fallocate request can update file size and
2328161e7b7cSAneesh Kumar K.V 			 * converting the second half to initialized extent
2329161e7b7cSAneesh Kumar K.V 			 * implies that we can leak some junk data to user
2330161e7b7cSAneesh Kumar K.V 			 * space.
2331161e7b7cSAneesh Kumar K.V 			 */
2332161e7b7cSAneesh Kumar K.V 			err =  ext4_ext_zeroout(inode, ex3);
2333161e7b7cSAneesh Kumar K.V 			if (err) {
2334161e7b7cSAneesh Kumar K.V 				/*
2335161e7b7cSAneesh Kumar K.V 				 * We should actually mark the
2336161e7b7cSAneesh Kumar K.V 				 * second half as uninit and return error
2337161e7b7cSAneesh Kumar K.V 				 * Insert would have changed the extent
2338161e7b7cSAneesh Kumar K.V 				 */
2339161e7b7cSAneesh Kumar K.V 				depth = ext_depth(inode);
2340161e7b7cSAneesh Kumar K.V 				ext4_ext_drop_refs(path);
2341161e7b7cSAneesh Kumar K.V 				path = ext4_ext_find_extent(inode,
2342161e7b7cSAneesh Kumar K.V 								iblock, path);
2343161e7b7cSAneesh Kumar K.V 				if (IS_ERR(path)) {
2344161e7b7cSAneesh Kumar K.V 					err = PTR_ERR(path);
2345161e7b7cSAneesh Kumar K.V 					return err;
2346161e7b7cSAneesh Kumar K.V 				}
2347161e7b7cSAneesh Kumar K.V 				ex = path[depth].p_ext;
2348161e7b7cSAneesh Kumar K.V 				err = ext4_ext_get_access(handle, inode,
2349161e7b7cSAneesh Kumar K.V 								path + depth);
2350161e7b7cSAneesh Kumar K.V 				if (err)
2351161e7b7cSAneesh Kumar K.V 					return err;
2352161e7b7cSAneesh Kumar K.V 				ext4_ext_mark_uninitialized(ex);
2353161e7b7cSAneesh Kumar K.V 				ext4_ext_dirty(handle, inode, path + depth);
2354161e7b7cSAneesh Kumar K.V 				return err;
2355161e7b7cSAneesh Kumar K.V 			}
2356161e7b7cSAneesh Kumar K.V 
2357161e7b7cSAneesh Kumar K.V 			/* zeroed the second half */
23583977c965SAneesh Kumar K.V 			return allocated;
23593977c965SAneesh Kumar K.V 		}
236056055d3aSAmit Arora 		ex3 = &newex;
236156055d3aSAmit Arora 		ex3->ee_block = cpu_to_le32(iblock + max_blocks);
236256055d3aSAmit Arora 		ext4_ext_store_pblock(ex3, newblock + max_blocks);
236356055d3aSAmit Arora 		ex3->ee_len = cpu_to_le16(allocated - max_blocks);
236456055d3aSAmit Arora 		ext4_ext_mark_uninitialized(ex3);
236556055d3aSAmit Arora 		err = ext4_ext_insert_extent(handle, inode, path, ex3);
2366093a088bSAneesh Kumar K.V 		if (err == -ENOSPC) {
2367093a088bSAneesh Kumar K.V 			err =  ext4_ext_zeroout(inode, &orig_ex);
2368093a088bSAneesh Kumar K.V 			if (err)
2369093a088bSAneesh Kumar K.V 				goto fix_extent_len;
2370093a088bSAneesh Kumar K.V 			/* update the extent length and mark as initialized */
237195c3889cSAneesh Kumar K.V 			ex->ee_block = orig_ex.ee_block;
237295c3889cSAneesh Kumar K.V 			ex->ee_len   = orig_ex.ee_len;
237395c3889cSAneesh Kumar K.V 			ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
237495c3889cSAneesh Kumar K.V 			ext4_ext_dirty(handle, inode, path + depth);
2375161e7b7cSAneesh Kumar K.V 			/* zeroed the full extent */
2376161e7b7cSAneesh Kumar K.V 			return allocated;
2377093a088bSAneesh Kumar K.V 
2378093a088bSAneesh Kumar K.V 		} else if (err)
2379093a088bSAneesh Kumar K.V 			goto fix_extent_len;
238056055d3aSAmit Arora 		/*
238156055d3aSAmit Arora 		 * The depth, and hence eh & ex might change
238256055d3aSAmit Arora 		 * as part of the insert above.
238356055d3aSAmit Arora 		 */
238456055d3aSAmit Arora 		newdepth = ext_depth(inode);
238595c3889cSAneesh Kumar K.V 		/*
238695c3889cSAneesh Kumar K.V 		 * update the extent length after successfull insert of the
238795c3889cSAneesh Kumar K.V 		 * split extent
238895c3889cSAneesh Kumar K.V 		 */
238995c3889cSAneesh Kumar K.V 		orig_ex.ee_len = cpu_to_le16(ee_len -
239095c3889cSAneesh Kumar K.V 						ext4_ext_get_actual_len(ex3));
239156055d3aSAmit Arora 		if (newdepth != depth) {
239256055d3aSAmit Arora 			depth = newdepth;
2393b35905c1SAneesh Kumar K.V 			ext4_ext_drop_refs(path);
2394b35905c1SAneesh Kumar K.V 			path = ext4_ext_find_extent(inode, iblock, path);
239556055d3aSAmit Arora 			if (IS_ERR(path)) {
239656055d3aSAmit Arora 				err = PTR_ERR(path);
239756055d3aSAmit Arora 				goto out;
239856055d3aSAmit Arora 			}
239956055d3aSAmit Arora 			eh = path[depth].p_hdr;
240056055d3aSAmit Arora 			ex = path[depth].p_ext;
240156055d3aSAmit Arora 			if (ex2 != &newex)
240256055d3aSAmit Arora 				ex2 = ex;
24039df5643aSAneesh Kumar K.V 
24049df5643aSAneesh Kumar K.V 			err = ext4_ext_get_access(handle, inode, path + depth);
24059df5643aSAneesh Kumar K.V 			if (err)
24069df5643aSAneesh Kumar K.V 				goto out;
240756055d3aSAmit Arora 		}
240856055d3aSAmit Arora 		allocated = max_blocks;
24093977c965SAneesh Kumar K.V 
24103977c965SAneesh Kumar K.V 		/* If extent has less than EXT4_EXT_ZERO_LEN and we are trying
24113977c965SAneesh Kumar K.V 		 * to insert a extent in the middle zerout directly
24123977c965SAneesh Kumar K.V 		 * otherwise give the extent a chance to merge to left
24133977c965SAneesh Kumar K.V 		 */
24143977c965SAneesh Kumar K.V 		if (le16_to_cpu(orig_ex.ee_len) <= EXT4_EXT_ZERO_LEN &&
24153977c965SAneesh Kumar K.V 							iblock != ee_block) {
24163977c965SAneesh Kumar K.V 			err =  ext4_ext_zeroout(inode, &orig_ex);
24173977c965SAneesh Kumar K.V 			if (err)
24183977c965SAneesh Kumar K.V 				goto fix_extent_len;
24193977c965SAneesh Kumar K.V 			/* update the extent length and mark as initialized */
24203977c965SAneesh Kumar K.V 			ex->ee_block = orig_ex.ee_block;
24213977c965SAneesh Kumar K.V 			ex->ee_len   = orig_ex.ee_len;
24223977c965SAneesh Kumar K.V 			ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
24233977c965SAneesh Kumar K.V 			ext4_ext_dirty(handle, inode, path + depth);
2424161e7b7cSAneesh Kumar K.V 			/* zero out the first half */
2425161e7b7cSAneesh Kumar K.V 			return allocated;
24263977c965SAneesh Kumar K.V 		}
242756055d3aSAmit Arora 	}
242856055d3aSAmit Arora 	/*
242956055d3aSAmit Arora 	 * If there was a change of depth as part of the
243056055d3aSAmit Arora 	 * insertion of ex3 above, we need to update the length
243156055d3aSAmit Arora 	 * of the ex1 extent again here
243256055d3aSAmit Arora 	 */
243356055d3aSAmit Arora 	if (ex1 && ex1 != ex) {
243456055d3aSAmit Arora 		ex1 = ex;
243556055d3aSAmit Arora 		ex1->ee_len = cpu_to_le16(iblock - ee_block);
243656055d3aSAmit Arora 		ext4_ext_mark_uninitialized(ex1);
243756055d3aSAmit Arora 		ex2 = &newex;
243856055d3aSAmit Arora 	}
243956055d3aSAmit Arora 	/* ex2: iblock to iblock + maxblocks-1 : initialised */
244056055d3aSAmit Arora 	ex2->ee_block = cpu_to_le32(iblock);
244156055d3aSAmit Arora 	ext4_ext_store_pblock(ex2, newblock);
244256055d3aSAmit Arora 	ex2->ee_len = cpu_to_le16(allocated);
244356055d3aSAmit Arora 	if (ex2 != ex)
244456055d3aSAmit Arora 		goto insert;
244556055d3aSAmit Arora 	/*
244656055d3aSAmit Arora 	 * New (initialized) extent starts from the first block
244756055d3aSAmit Arora 	 * in the current extent. i.e., ex2 == ex
244856055d3aSAmit Arora 	 * We have to see if it can be merged with the extent
244956055d3aSAmit Arora 	 * on the left.
245056055d3aSAmit Arora 	 */
245156055d3aSAmit Arora 	if (ex2 > EXT_FIRST_EXTENT(eh)) {
245256055d3aSAmit Arora 		/*
245356055d3aSAmit Arora 		 * To merge left, pass "ex2 - 1" to try_to_merge(),
245456055d3aSAmit Arora 		 * since it merges towards right _only_.
245556055d3aSAmit Arora 		 */
245656055d3aSAmit Arora 		ret = ext4_ext_try_to_merge(inode, path, ex2 - 1);
245756055d3aSAmit Arora 		if (ret) {
245856055d3aSAmit Arora 			err = ext4_ext_correct_indexes(handle, inode, path);
245956055d3aSAmit Arora 			if (err)
246056055d3aSAmit Arora 				goto out;
246156055d3aSAmit Arora 			depth = ext_depth(inode);
246256055d3aSAmit Arora 			ex2--;
246356055d3aSAmit Arora 		}
246456055d3aSAmit Arora 	}
246556055d3aSAmit Arora 	/*
246656055d3aSAmit Arora 	 * Try to Merge towards right. This might be required
246756055d3aSAmit Arora 	 * only when the whole extent is being written to.
246856055d3aSAmit Arora 	 * i.e. ex2 == ex and ex3 == NULL.
246956055d3aSAmit Arora 	 */
247056055d3aSAmit Arora 	if (!ex3) {
247156055d3aSAmit Arora 		ret = ext4_ext_try_to_merge(inode, path, ex2);
247256055d3aSAmit Arora 		if (ret) {
247356055d3aSAmit Arora 			err = ext4_ext_correct_indexes(handle, inode, path);
247456055d3aSAmit Arora 			if (err)
247556055d3aSAmit Arora 				goto out;
247656055d3aSAmit Arora 		}
247756055d3aSAmit Arora 	}
247856055d3aSAmit Arora 	/* Mark modified extent as dirty */
247956055d3aSAmit Arora 	err = ext4_ext_dirty(handle, inode, path + depth);
248056055d3aSAmit Arora 	goto out;
248156055d3aSAmit Arora insert:
248256055d3aSAmit Arora 	err = ext4_ext_insert_extent(handle, inode, path, &newex);
2483093a088bSAneesh Kumar K.V 	if (err == -ENOSPC) {
2484093a088bSAneesh Kumar K.V 		err =  ext4_ext_zeroout(inode, &orig_ex);
2485093a088bSAneesh Kumar K.V 		if (err)
2486093a088bSAneesh Kumar K.V 			goto fix_extent_len;
2487093a088bSAneesh Kumar K.V 		/* update the extent length and mark as initialized */
2488093a088bSAneesh Kumar K.V 		ex->ee_block = orig_ex.ee_block;
2489093a088bSAneesh Kumar K.V 		ex->ee_len   = orig_ex.ee_len;
2490093a088bSAneesh Kumar K.V 		ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2491093a088bSAneesh Kumar K.V 		ext4_ext_dirty(handle, inode, path + depth);
2492161e7b7cSAneesh Kumar K.V 		/* zero out the first half */
2493161e7b7cSAneesh Kumar K.V 		return allocated;
2494093a088bSAneesh Kumar K.V 	} else if (err)
2495093a088bSAneesh Kumar K.V 		goto fix_extent_len;
2496093a088bSAneesh Kumar K.V out:
2497093a088bSAneesh Kumar K.V 	return err ? err : allocated;
2498093a088bSAneesh Kumar K.V 
2499093a088bSAneesh Kumar K.V fix_extent_len:
250095c3889cSAneesh Kumar K.V 	ex->ee_block = orig_ex.ee_block;
250195c3889cSAneesh Kumar K.V 	ex->ee_len   = orig_ex.ee_len;
250295c3889cSAneesh Kumar K.V 	ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
250395c3889cSAneesh Kumar K.V 	ext4_ext_mark_uninitialized(ex);
250495c3889cSAneesh Kumar K.V 	ext4_ext_dirty(handle, inode, path + depth);
2505093a088bSAneesh Kumar K.V 	return err;
250656055d3aSAmit Arora }
250756055d3aSAmit Arora 
2508c278bfecSAneesh Kumar K.V /*
2509f5ab0d1fSMingming Cao  * Block allocation/map/preallocation routine for extents based files
2510f5ab0d1fSMingming Cao  *
2511f5ab0d1fSMingming Cao  *
2512c278bfecSAneesh Kumar K.V  * Need to be called with
25130e855ac8SAneesh Kumar K.V  * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
25140e855ac8SAneesh Kumar K.V  * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
2515f5ab0d1fSMingming Cao  *
2516f5ab0d1fSMingming Cao  * return > 0, number of of blocks already mapped/allocated
2517f5ab0d1fSMingming Cao  *          if create == 0 and these are pre-allocated blocks
2518f5ab0d1fSMingming Cao  *          	buffer head is unmapped
2519f5ab0d1fSMingming Cao  *          otherwise blocks are mapped
2520f5ab0d1fSMingming Cao  *
2521f5ab0d1fSMingming Cao  * return = 0, if plain look up failed (blocks have not been allocated)
2522f5ab0d1fSMingming Cao  *          buffer head is unmapped
2523f5ab0d1fSMingming Cao  *
2524f5ab0d1fSMingming Cao  * return < 0, error case.
2525c278bfecSAneesh Kumar K.V  */
2526f65e6fbaSAlex Tomas int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
2527725d26d3SAneesh Kumar K.V 			ext4_lblk_t iblock,
2528a86c6181SAlex Tomas 			unsigned long max_blocks, struct buffer_head *bh_result,
2529a86c6181SAlex Tomas 			int create, int extend_disksize)
2530a86c6181SAlex Tomas {
2531a86c6181SAlex Tomas 	struct ext4_ext_path *path = NULL;
253256055d3aSAmit Arora 	struct ext4_extent_header *eh;
2533a86c6181SAlex Tomas 	struct ext4_extent newex, *ex;
2534f65e6fbaSAlex Tomas 	ext4_fsblk_t goal, newblock;
253556055d3aSAmit Arora 	int err = 0, depth, ret;
2536a86c6181SAlex Tomas 	unsigned long allocated = 0;
2537c9de560dSAlex Tomas 	struct ext4_allocation_request ar;
2538a86c6181SAlex Tomas 
2539a86c6181SAlex Tomas 	__clear_bit(BH_New, &bh_result->b_state);
2540bba90743SEric Sandeen 	ext_debug("blocks %u/%lu requested for inode %u\n",
2541bba90743SEric Sandeen 			iblock, max_blocks, inode->i_ino);
2542a86c6181SAlex Tomas 
2543a86c6181SAlex Tomas 	/* check in cache */
25447e028976SAvantika Mathur 	goal = ext4_ext_in_cache(inode, iblock, &newex);
25457e028976SAvantika Mathur 	if (goal) {
2546a86c6181SAlex Tomas 		if (goal == EXT4_EXT_CACHE_GAP) {
2547a86c6181SAlex Tomas 			if (!create) {
254856055d3aSAmit Arora 				/*
254956055d3aSAmit Arora 				 * block isn't allocated yet and
255056055d3aSAmit Arora 				 * user doesn't want to allocate it
255156055d3aSAmit Arora 				 */
2552a86c6181SAlex Tomas 				goto out2;
2553a86c6181SAlex Tomas 			}
2554a86c6181SAlex Tomas 			/* we should allocate requested block */
2555a86c6181SAlex Tomas 		} else if (goal == EXT4_EXT_CACHE_EXTENT) {
2556a86c6181SAlex Tomas 			/* block is already allocated */
2557a86c6181SAlex Tomas 			newblock = iblock
2558a86c6181SAlex Tomas 				   - le32_to_cpu(newex.ee_block)
2559f65e6fbaSAlex Tomas 				   + ext_pblock(&newex);
2560d0d856e8SRandy Dunlap 			/* number of remaining blocks in the extent */
2561b939e376SAneesh Kumar K.V 			allocated = ext4_ext_get_actual_len(&newex) -
2562a86c6181SAlex Tomas 					(iblock - le32_to_cpu(newex.ee_block));
2563a86c6181SAlex Tomas 			goto out;
2564a86c6181SAlex Tomas 		} else {
2565a86c6181SAlex Tomas 			BUG();
2566a86c6181SAlex Tomas 		}
2567a86c6181SAlex Tomas 	}
2568a86c6181SAlex Tomas 
2569a86c6181SAlex Tomas 	/* find extent for this block */
2570a86c6181SAlex Tomas 	path = ext4_ext_find_extent(inode, iblock, NULL);
2571a86c6181SAlex Tomas 	if (IS_ERR(path)) {
2572a86c6181SAlex Tomas 		err = PTR_ERR(path);
2573a86c6181SAlex Tomas 		path = NULL;
2574a86c6181SAlex Tomas 		goto out2;
2575a86c6181SAlex Tomas 	}
2576a86c6181SAlex Tomas 
2577a86c6181SAlex Tomas 	depth = ext_depth(inode);
2578a86c6181SAlex Tomas 
2579a86c6181SAlex Tomas 	/*
2580d0d856e8SRandy Dunlap 	 * consistent leaf must not be empty;
2581d0d856e8SRandy Dunlap 	 * this situation is possible, though, _during_ tree modification;
2582a86c6181SAlex Tomas 	 * this is why assert can't be put in ext4_ext_find_extent()
2583a86c6181SAlex Tomas 	 */
2584a86c6181SAlex Tomas 	BUG_ON(path[depth].p_ext == NULL && depth != 0);
258556055d3aSAmit Arora 	eh = path[depth].p_hdr;
2586a86c6181SAlex Tomas 
25877e028976SAvantika Mathur 	ex = path[depth].p_ext;
25887e028976SAvantika Mathur 	if (ex) {
2589725d26d3SAneesh Kumar K.V 		ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
2590f65e6fbaSAlex Tomas 		ext4_fsblk_t ee_start = ext_pblock(ex);
2591a2df2a63SAmit Arora 		unsigned short ee_len;
2592471d4011SSuparna Bhattacharya 
2593471d4011SSuparna Bhattacharya 		/*
2594471d4011SSuparna Bhattacharya 		 * Uninitialized extents are treated as holes, except that
259556055d3aSAmit Arora 		 * we split out initialized portions during a write.
2596471d4011SSuparna Bhattacharya 		 */
2597a2df2a63SAmit Arora 		ee_len = ext4_ext_get_actual_len(ex);
2598d0d856e8SRandy Dunlap 		/* if found extent covers block, simply return it */
2599a86c6181SAlex Tomas 		if (iblock >= ee_block && iblock < ee_block + ee_len) {
2600a86c6181SAlex Tomas 			newblock = iblock - ee_block + ee_start;
2601d0d856e8SRandy Dunlap 			/* number of remaining blocks in the extent */
2602a86c6181SAlex Tomas 			allocated = ee_len - (iblock - ee_block);
2603bba90743SEric Sandeen 			ext_debug("%u fit into %lu:%d -> %llu\n", iblock,
2604a86c6181SAlex Tomas 					ee_block, ee_len, newblock);
260556055d3aSAmit Arora 
2606a2df2a63SAmit Arora 			/* Do not put uninitialized extent in the cache */
260756055d3aSAmit Arora 			if (!ext4_ext_is_uninitialized(ex)) {
2608a2df2a63SAmit Arora 				ext4_ext_put_in_cache(inode, ee_block,
2609a2df2a63SAmit Arora 							ee_len, ee_start,
2610a2df2a63SAmit Arora 							EXT4_EXT_CACHE_EXTENT);
2611a86c6181SAlex Tomas 				goto out;
2612a86c6181SAlex Tomas 			}
261356055d3aSAmit Arora 			if (create == EXT4_CREATE_UNINITIALIZED_EXT)
261456055d3aSAmit Arora 				goto out;
2615e067ba00SAneesh Kumar K.V 			if (!create) {
2616e067ba00SAneesh Kumar K.V 				/*
2617e067ba00SAneesh Kumar K.V 				 * We have blocks reserved already.  We
2618e067ba00SAneesh Kumar K.V 				 * return allocated blocks so that delalloc
2619e067ba00SAneesh Kumar K.V 				 * won't do block reservation for us.  But
2620e067ba00SAneesh Kumar K.V 				 * the buffer head will be unmapped so that
2621e067ba00SAneesh Kumar K.V 				 * a read from the block returns 0s.
2622e067ba00SAneesh Kumar K.V 				 */
2623e067ba00SAneesh Kumar K.V 				if (allocated > max_blocks)
2624e067ba00SAneesh Kumar K.V 					allocated = max_blocks;
2625953e622bSEric Sandeen 				set_buffer_unwritten(bh_result);
262656055d3aSAmit Arora 				goto out2;
2627e067ba00SAneesh Kumar K.V 			}
262856055d3aSAmit Arora 
262956055d3aSAmit Arora 			ret = ext4_ext_convert_to_initialized(handle, inode,
263056055d3aSAmit Arora 								path, iblock,
263156055d3aSAmit Arora 								max_blocks);
2632dbf9d7daSDmitry Monakhov 			if (ret <= 0) {
2633dbf9d7daSDmitry Monakhov 				err = ret;
263456055d3aSAmit Arora 				goto out2;
2635dbf9d7daSDmitry Monakhov 			} else
263656055d3aSAmit Arora 				allocated = ret;
263756055d3aSAmit Arora 			goto outnew;
263856055d3aSAmit Arora 		}
2639a86c6181SAlex Tomas 	}
2640a86c6181SAlex Tomas 
2641a86c6181SAlex Tomas 	/*
2642d0d856e8SRandy Dunlap 	 * requested block isn't allocated yet;
2643a86c6181SAlex Tomas 	 * we couldn't try to create block if create flag is zero
2644a86c6181SAlex Tomas 	 */
2645a86c6181SAlex Tomas 	if (!create) {
264656055d3aSAmit Arora 		/*
264756055d3aSAmit Arora 		 * put just found gap into cache to speed up
264856055d3aSAmit Arora 		 * subsequent requests
264956055d3aSAmit Arora 		 */
2650a86c6181SAlex Tomas 		ext4_ext_put_gap_in_cache(inode, path, iblock);
2651a86c6181SAlex Tomas 		goto out2;
2652a86c6181SAlex Tomas 	}
2653a86c6181SAlex Tomas 	/*
2654a86c6181SAlex Tomas 	 * Okay, we need to do block allocation.  Lazily initialize the block
2655d0d856e8SRandy Dunlap 	 * allocation info here if necessary.
2656a86c6181SAlex Tomas 	 */
2657a86c6181SAlex Tomas 	if (S_ISREG(inode->i_mode) && (!EXT4_I(inode)->i_block_alloc_info))
2658a86c6181SAlex Tomas 		ext4_init_block_alloc_info(inode);
2659a86c6181SAlex Tomas 
2660c9de560dSAlex Tomas 	/* find neighbour allocated blocks */
2661c9de560dSAlex Tomas 	ar.lleft = iblock;
2662c9de560dSAlex Tomas 	err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
2663c9de560dSAlex Tomas 	if (err)
2664c9de560dSAlex Tomas 		goto out2;
2665c9de560dSAlex Tomas 	ar.lright = iblock;
2666c9de560dSAlex Tomas 	err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright);
2667c9de560dSAlex Tomas 	if (err)
2668c9de560dSAlex Tomas 		goto out2;
266925d14f98SAmit Arora 
2670749269faSAmit Arora 	/*
2671749269faSAmit Arora 	 * See if request is beyond maximum number of blocks we can have in
2672749269faSAmit Arora 	 * a single extent. For an initialized extent this limit is
2673749269faSAmit Arora 	 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
2674749269faSAmit Arora 	 * EXT_UNINIT_MAX_LEN.
2675749269faSAmit Arora 	 */
2676749269faSAmit Arora 	if (max_blocks > EXT_INIT_MAX_LEN &&
2677749269faSAmit Arora 	    create != EXT4_CREATE_UNINITIALIZED_EXT)
2678749269faSAmit Arora 		max_blocks = EXT_INIT_MAX_LEN;
2679749269faSAmit Arora 	else if (max_blocks > EXT_UNINIT_MAX_LEN &&
2680749269faSAmit Arora 		 create == EXT4_CREATE_UNINITIALIZED_EXT)
2681749269faSAmit Arora 		max_blocks = EXT_UNINIT_MAX_LEN;
2682749269faSAmit Arora 
268325d14f98SAmit Arora 	/* Check if we can really insert (iblock)::(iblock+max_blocks) extent */
268425d14f98SAmit Arora 	newex.ee_block = cpu_to_le32(iblock);
268525d14f98SAmit Arora 	newex.ee_len = cpu_to_le16(max_blocks);
268625d14f98SAmit Arora 	err = ext4_ext_check_overlap(inode, &newex, path);
268725d14f98SAmit Arora 	if (err)
2688b939e376SAneesh Kumar K.V 		allocated = ext4_ext_get_actual_len(&newex);
268925d14f98SAmit Arora 	else
2690a86c6181SAlex Tomas 		allocated = max_blocks;
2691c9de560dSAlex Tomas 
2692c9de560dSAlex Tomas 	/* allocate new block */
2693c9de560dSAlex Tomas 	ar.inode = inode;
2694c9de560dSAlex Tomas 	ar.goal = ext4_ext_find_goal(inode, path, iblock);
2695c9de560dSAlex Tomas 	ar.logical = iblock;
2696c9de560dSAlex Tomas 	ar.len = allocated;
2697c9de560dSAlex Tomas 	if (S_ISREG(inode->i_mode))
2698c9de560dSAlex Tomas 		ar.flags = EXT4_MB_HINT_DATA;
2699c9de560dSAlex Tomas 	else
2700c9de560dSAlex Tomas 		/* disable in-core preallocation for non-regular files */
2701c9de560dSAlex Tomas 		ar.flags = 0;
2702c9de560dSAlex Tomas 	newblock = ext4_mb_new_blocks(handle, &ar, &err);
2703a86c6181SAlex Tomas 	if (!newblock)
2704a86c6181SAlex Tomas 		goto out2;
27052ae02107SMingming Cao 	ext_debug("allocate new block: goal %llu, found %llu/%lu\n",
2706a86c6181SAlex Tomas 			goal, newblock, allocated);
2707a86c6181SAlex Tomas 
2708a86c6181SAlex Tomas 	/* try to insert new extent into found leaf and return */
2709f65e6fbaSAlex Tomas 	ext4_ext_store_pblock(&newex, newblock);
2710c9de560dSAlex Tomas 	newex.ee_len = cpu_to_le16(ar.len);
2711a2df2a63SAmit Arora 	if (create == EXT4_CREATE_UNINITIALIZED_EXT)  /* Mark uninitialized */
2712a2df2a63SAmit Arora 		ext4_ext_mark_uninitialized(&newex);
2713a86c6181SAlex Tomas 	err = ext4_ext_insert_extent(handle, inode, path, &newex);
2714315054f0SAlex Tomas 	if (err) {
2715315054f0SAlex Tomas 		/* free data blocks we just allocated */
2716c9de560dSAlex Tomas 		/* not a good idea to call discard here directly,
2717c9de560dSAlex Tomas 		 * but otherwise we'd need to call it every free() */
2718c9de560dSAlex Tomas 		ext4_mb_discard_inode_preallocations(inode);
2719315054f0SAlex Tomas 		ext4_free_blocks(handle, inode, ext_pblock(&newex),
2720b939e376SAneesh Kumar K.V 					ext4_ext_get_actual_len(&newex), 0);
2721a86c6181SAlex Tomas 		goto out2;
2722315054f0SAlex Tomas 	}
2723a86c6181SAlex Tomas 
2724a86c6181SAlex Tomas 	/* previous routine could use block we allocated */
2725f65e6fbaSAlex Tomas 	newblock = ext_pblock(&newex);
2726b939e376SAneesh Kumar K.V 	allocated = ext4_ext_get_actual_len(&newex);
272756055d3aSAmit Arora outnew:
2728a379cd1dSAneesh Kumar K.V 	if (extend_disksize && inode->i_size > EXT4_I(inode)->i_disksize)
2729a379cd1dSAneesh Kumar K.V 		EXT4_I(inode)->i_disksize = inode->i_size;
2730a379cd1dSAneesh Kumar K.V 
2731953e622bSEric Sandeen 	set_buffer_new(bh_result);
2732a86c6181SAlex Tomas 
2733a2df2a63SAmit Arora 	/* Cache only when it is _not_ an uninitialized extent */
2734a2df2a63SAmit Arora 	if (create != EXT4_CREATE_UNINITIALIZED_EXT)
2735a86c6181SAlex Tomas 		ext4_ext_put_in_cache(inode, iblock, allocated, newblock,
2736a86c6181SAlex Tomas 						EXT4_EXT_CACHE_EXTENT);
2737a86c6181SAlex Tomas out:
2738a86c6181SAlex Tomas 	if (allocated > max_blocks)
2739a86c6181SAlex Tomas 		allocated = max_blocks;
2740a86c6181SAlex Tomas 	ext4_ext_show_leaf(inode, path);
2741953e622bSEric Sandeen 	set_buffer_mapped(bh_result);
2742a86c6181SAlex Tomas 	bh_result->b_bdev = inode->i_sb->s_bdev;
2743a86c6181SAlex Tomas 	bh_result->b_blocknr = newblock;
2744a86c6181SAlex Tomas out2:
2745a86c6181SAlex Tomas 	if (path) {
2746a86c6181SAlex Tomas 		ext4_ext_drop_refs(path);
2747a86c6181SAlex Tomas 		kfree(path);
2748a86c6181SAlex Tomas 	}
2749a86c6181SAlex Tomas 	return err ? err : allocated;
2750a86c6181SAlex Tomas }
2751a86c6181SAlex Tomas 
2752*cf108bcaSJan Kara void ext4_ext_truncate(struct inode *inode)
2753a86c6181SAlex Tomas {
2754a86c6181SAlex Tomas 	struct address_space *mapping = inode->i_mapping;
2755a86c6181SAlex Tomas 	struct super_block *sb = inode->i_sb;
2756725d26d3SAneesh Kumar K.V 	ext4_lblk_t last_block;
2757a86c6181SAlex Tomas 	handle_t *handle;
2758a86c6181SAlex Tomas 	int err = 0;
2759a86c6181SAlex Tomas 
2760a86c6181SAlex Tomas 	/*
2761a86c6181SAlex Tomas 	 * probably first extent we're gonna free will be last in block
2762a86c6181SAlex Tomas 	 */
2763a86c6181SAlex Tomas 	err = ext4_writepage_trans_blocks(inode) + 3;
2764a86c6181SAlex Tomas 	handle = ext4_journal_start(inode, err);
2765*cf108bcaSJan Kara 	if (IS_ERR(handle))
2766a86c6181SAlex Tomas 		return;
2767a86c6181SAlex Tomas 
2768*cf108bcaSJan Kara 	if (inode->i_size & (sb->s_blocksize - 1))
2769*cf108bcaSJan Kara 		ext4_block_truncate_page(handle, mapping, inode->i_size);
2770a86c6181SAlex Tomas 
27710e855ac8SAneesh Kumar K.V 	down_write(&EXT4_I(inode)->i_data_sem);
2772a86c6181SAlex Tomas 	ext4_ext_invalidate_cache(inode);
2773a86c6181SAlex Tomas 
2774c9de560dSAlex Tomas 	ext4_mb_discard_inode_preallocations(inode);
2775c9de560dSAlex Tomas 
2776a86c6181SAlex Tomas 	/*
2777d0d856e8SRandy Dunlap 	 * TODO: optimization is possible here.
2778d0d856e8SRandy Dunlap 	 * Probably we need not scan at all,
2779d0d856e8SRandy Dunlap 	 * because page truncation is enough.
2780a86c6181SAlex Tomas 	 */
2781a86c6181SAlex Tomas 	if (ext4_orphan_add(handle, inode))
2782a86c6181SAlex Tomas 		goto out_stop;
2783a86c6181SAlex Tomas 
2784a86c6181SAlex Tomas 	/* we have to know where to truncate from in crash case */
2785a86c6181SAlex Tomas 	EXT4_I(inode)->i_disksize = inode->i_size;
2786a86c6181SAlex Tomas 	ext4_mark_inode_dirty(handle, inode);
2787a86c6181SAlex Tomas 
2788a86c6181SAlex Tomas 	last_block = (inode->i_size + sb->s_blocksize - 1)
2789a86c6181SAlex Tomas 			>> EXT4_BLOCK_SIZE_BITS(sb);
2790a86c6181SAlex Tomas 	err = ext4_ext_remove_space(inode, last_block);
2791a86c6181SAlex Tomas 
2792a86c6181SAlex Tomas 	/* In a multi-transaction truncate, we only make the final
279356055d3aSAmit Arora 	 * transaction synchronous.
279456055d3aSAmit Arora 	 */
2795a86c6181SAlex Tomas 	if (IS_SYNC(inode))
2796a86c6181SAlex Tomas 		handle->h_sync = 1;
2797a86c6181SAlex Tomas 
2798a86c6181SAlex Tomas out_stop:
2799a86c6181SAlex Tomas 	/*
2800d0d856e8SRandy Dunlap 	 * If this was a simple ftruncate() and the file will remain alive,
2801a86c6181SAlex Tomas 	 * then we need to clear up the orphan record which we created above.
2802a86c6181SAlex Tomas 	 * However, if this was a real unlink then we were called by
2803a86c6181SAlex Tomas 	 * ext4_delete_inode(), and we allow that function to clean up the
2804a86c6181SAlex Tomas 	 * orphan info for us.
2805a86c6181SAlex Tomas 	 */
2806a86c6181SAlex Tomas 	if (inode->i_nlink)
2807a86c6181SAlex Tomas 		ext4_orphan_del(handle, inode);
2808a86c6181SAlex Tomas 
28090e855ac8SAneesh Kumar K.V 	up_write(&EXT4_I(inode)->i_data_sem);
2810ef737728SSolofo Ramangalahy 	inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
2811ef737728SSolofo Ramangalahy 	ext4_mark_inode_dirty(handle, inode);
2812a86c6181SAlex Tomas 	ext4_journal_stop(handle);
2813a86c6181SAlex Tomas }
2814a86c6181SAlex Tomas 
2815a86c6181SAlex Tomas /*
2816d0d856e8SRandy Dunlap  * ext4_ext_writepage_trans_blocks:
2817d0d856e8SRandy Dunlap  * calculate max number of blocks we could modify
2818a86c6181SAlex Tomas  * in order to allocate new block for an inode
2819a86c6181SAlex Tomas  */
2820a86c6181SAlex Tomas int ext4_ext_writepage_trans_blocks(struct inode *inode, int num)
2821a86c6181SAlex Tomas {
2822a86c6181SAlex Tomas 	int needed;
2823a86c6181SAlex Tomas 
2824a86c6181SAlex Tomas 	needed = ext4_ext_calc_credits_for_insert(inode, NULL);
2825a86c6181SAlex Tomas 
2826d0d856e8SRandy Dunlap 	/* caller wants to allocate num blocks, but note it includes sb */
2827a86c6181SAlex Tomas 	needed = needed * num - (num - 1);
2828a86c6181SAlex Tomas 
2829a86c6181SAlex Tomas #ifdef CONFIG_QUOTA
2830a86c6181SAlex Tomas 	needed += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
2831a86c6181SAlex Tomas #endif
2832a86c6181SAlex Tomas 
2833a86c6181SAlex Tomas 	return needed;
2834a86c6181SAlex Tomas }
2835a2df2a63SAmit Arora 
2836fd28784aSAneesh Kumar K.V static void ext4_falloc_update_inode(struct inode *inode,
2837fd28784aSAneesh Kumar K.V 				int mode, loff_t new_size, int update_ctime)
2838fd28784aSAneesh Kumar K.V {
2839fd28784aSAneesh Kumar K.V 	struct timespec now;
2840fd28784aSAneesh Kumar K.V 
2841fd28784aSAneesh Kumar K.V 	if (update_ctime) {
2842fd28784aSAneesh Kumar K.V 		now = current_fs_time(inode->i_sb);
2843fd28784aSAneesh Kumar K.V 		if (!timespec_equal(&inode->i_ctime, &now))
2844fd28784aSAneesh Kumar K.V 			inode->i_ctime = now;
2845fd28784aSAneesh Kumar K.V 	}
2846fd28784aSAneesh Kumar K.V 	/*
2847fd28784aSAneesh Kumar K.V 	 * Update only when preallocation was requested beyond
2848fd28784aSAneesh Kumar K.V 	 * the file size.
2849fd28784aSAneesh Kumar K.V 	 */
2850fd28784aSAneesh Kumar K.V 	if (!(mode & FALLOC_FL_KEEP_SIZE) &&
2851fd28784aSAneesh Kumar K.V 				new_size > i_size_read(inode)) {
2852fd28784aSAneesh Kumar K.V 		i_size_write(inode, new_size);
2853fd28784aSAneesh Kumar K.V 		EXT4_I(inode)->i_disksize = new_size;
2854fd28784aSAneesh Kumar K.V 	}
2855fd28784aSAneesh Kumar K.V 
2856fd28784aSAneesh Kumar K.V }
2857fd28784aSAneesh Kumar K.V 
2858a2df2a63SAmit Arora /*
2859a2df2a63SAmit Arora  * preallocate space for a file. This implements ext4's fallocate inode
2860a2df2a63SAmit Arora  * operation, which gets called from sys_fallocate system call.
2861a2df2a63SAmit Arora  * For block-mapped files, posix_fallocate should fall back to the method
2862a2df2a63SAmit Arora  * of writing zeroes to the required new blocks (the same behavior which is
2863a2df2a63SAmit Arora  * expected for file systems which do not support fallocate() system call).
2864a2df2a63SAmit Arora  */
2865a2df2a63SAmit Arora long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
2866a2df2a63SAmit Arora {
2867a2df2a63SAmit Arora 	handle_t *handle;
2868725d26d3SAneesh Kumar K.V 	ext4_lblk_t block;
2869fd28784aSAneesh Kumar K.V 	loff_t new_size;
2870725d26d3SAneesh Kumar K.V 	unsigned long max_blocks;
2871a2df2a63SAmit Arora 	int ret = 0;
2872a2df2a63SAmit Arora 	int ret2 = 0;
2873a2df2a63SAmit Arora 	int retries = 0;
2874a2df2a63SAmit Arora 	struct buffer_head map_bh;
2875a2df2a63SAmit Arora 	unsigned int credits, blkbits = inode->i_blkbits;
2876a2df2a63SAmit Arora 
2877a2df2a63SAmit Arora 	/*
2878a2df2a63SAmit Arora 	 * currently supporting (pre)allocate mode for extent-based
2879a2df2a63SAmit Arora 	 * files _only_
2880a2df2a63SAmit Arora 	 */
2881a2df2a63SAmit Arora 	if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
2882a2df2a63SAmit Arora 		return -EOPNOTSUPP;
2883a2df2a63SAmit Arora 
2884a2df2a63SAmit Arora 	/* preallocation to directories is currently not supported */
2885a2df2a63SAmit Arora 	if (S_ISDIR(inode->i_mode))
2886a2df2a63SAmit Arora 		return -ENODEV;
2887a2df2a63SAmit Arora 
2888a2df2a63SAmit Arora 	block = offset >> blkbits;
2889fd28784aSAneesh Kumar K.V 	/*
2890fd28784aSAneesh Kumar K.V 	 * We can't just convert len to max_blocks because
2891fd28784aSAneesh Kumar K.V 	 * If blocksize = 4096 offset = 3072 and len = 2048
2892fd28784aSAneesh Kumar K.V 	 */
2893a2df2a63SAmit Arora 	max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
2894a2df2a63SAmit Arora 							- block;
2895a2df2a63SAmit Arora 	/*
2896a2df2a63SAmit Arora 	 * credits to insert 1 extent into extent tree + buffers to be able to
2897a2df2a63SAmit Arora 	 * modify 1 super block, 1 block bitmap and 1 group descriptor.
2898a2df2a63SAmit Arora 	 */
2899a2df2a63SAmit Arora 	credits = EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + 3;
290055bd725aSAneesh Kumar K.V 	mutex_lock(&inode->i_mutex);
2901a2df2a63SAmit Arora retry:
2902a2df2a63SAmit Arora 	while (ret >= 0 && ret < max_blocks) {
2903a2df2a63SAmit Arora 		block = block + ret;
2904a2df2a63SAmit Arora 		max_blocks = max_blocks - ret;
2905a2df2a63SAmit Arora 		handle = ext4_journal_start(inode, credits);
2906a2df2a63SAmit Arora 		if (IS_ERR(handle)) {
2907a2df2a63SAmit Arora 			ret = PTR_ERR(handle);
2908a2df2a63SAmit Arora 			break;
2909a2df2a63SAmit Arora 		}
291055bd725aSAneesh Kumar K.V 		ret = ext4_get_blocks_wrap(handle, inode, block,
2911a2df2a63SAmit Arora 					  max_blocks, &map_bh,
2912a2df2a63SAmit Arora 					  EXT4_CREATE_UNINITIALIZED_EXT, 0);
2913221879c9SAneesh Kumar K.V 		if (ret <= 0) {
29142c98615dSAneesh Kumar K.V #ifdef EXT4FS_DEBUG
29152c98615dSAneesh Kumar K.V 			WARN_ON(ret <= 0);
29162c98615dSAneesh Kumar K.V 			printk(KERN_ERR "%s: ext4_ext_get_blocks "
29172c98615dSAneesh Kumar K.V 				    "returned error inode#%lu, block=%u, "
29182c98615dSAneesh Kumar K.V 				    "max_blocks=%lu", __func__,
2919bba90743SEric Sandeen 				    inode->i_ino, block, max_blocks);
29202c98615dSAneesh Kumar K.V #endif
2921a2df2a63SAmit Arora 			ext4_mark_inode_dirty(handle, inode);
2922a2df2a63SAmit Arora 			ret2 = ext4_journal_stop(handle);
2923a2df2a63SAmit Arora 			break;
2924a2df2a63SAmit Arora 		}
2925fd28784aSAneesh Kumar K.V 		if ((block + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
2926fd28784aSAneesh Kumar K.V 						blkbits) >> blkbits))
2927fd28784aSAneesh Kumar K.V 			new_size = offset + len;
2928fd28784aSAneesh Kumar K.V 		else
2929fd28784aSAneesh Kumar K.V 			new_size = (block + ret) << blkbits;
2930a2df2a63SAmit Arora 
2931fd28784aSAneesh Kumar K.V 		ext4_falloc_update_inode(inode, mode, new_size,
2932fd28784aSAneesh Kumar K.V 						buffer_new(&map_bh));
2933a2df2a63SAmit Arora 		ext4_mark_inode_dirty(handle, inode);
2934a2df2a63SAmit Arora 		ret2 = ext4_journal_stop(handle);
2935a2df2a63SAmit Arora 		if (ret2)
2936a2df2a63SAmit Arora 			break;
2937a2df2a63SAmit Arora 	}
2938fd28784aSAneesh Kumar K.V 	if (ret == -ENOSPC &&
2939fd28784aSAneesh Kumar K.V 			ext4_should_retry_alloc(inode->i_sb, &retries)) {
2940fd28784aSAneesh Kumar K.V 		ret = 0;
2941a2df2a63SAmit Arora 		goto retry;
2942a2df2a63SAmit Arora 	}
294355bd725aSAneesh Kumar K.V 	mutex_unlock(&inode->i_mutex);
2944a2df2a63SAmit Arora 	return ret > 0 ? ret2 : ret;
2945a2df2a63SAmit Arora }
2946