xref: /openbmc/linux/fs/ext4/extents.c (revision d0d856e8)
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>
35a86c6181SAlex Tomas #include <linux/ext4_jbd2.h>
36a86c6181SAlex Tomas #include <linux/jbd.h>
37a86c6181SAlex Tomas #include <linux/smp_lock.h>
38a86c6181SAlex Tomas #include <linux/highuid.h>
39a86c6181SAlex Tomas #include <linux/pagemap.h>
40a86c6181SAlex Tomas #include <linux/quotaops.h>
41a86c6181SAlex Tomas #include <linux/string.h>
42a86c6181SAlex Tomas #include <linux/slab.h>
43a86c6181SAlex Tomas #include <linux/ext4_fs_extents.h>
44a86c6181SAlex Tomas #include <asm/uaccess.h>
45a86c6181SAlex Tomas 
46a86c6181SAlex Tomas 
47*d0d856e8SRandy Dunlap /*
48*d0d856e8SRandy Dunlap  * ext_pblock:
49*d0d856e8SRandy Dunlap  * combine low and high parts of physical block number into ext4_fsblk_t
50*d0d856e8SRandy Dunlap  */
51f65e6fbaSAlex Tomas static inline ext4_fsblk_t ext_pblock(struct ext4_extent *ex)
52f65e6fbaSAlex Tomas {
53f65e6fbaSAlex Tomas 	ext4_fsblk_t block;
54f65e6fbaSAlex Tomas 
55f65e6fbaSAlex Tomas 	block = le32_to_cpu(ex->ee_start);
56f65e6fbaSAlex Tomas 	if (sizeof(ext4_fsblk_t) > 4)
57f65e6fbaSAlex Tomas 		block |= ((ext4_fsblk_t) le16_to_cpu(ex->ee_start_hi) << 31) << 1;
58f65e6fbaSAlex Tomas 	return block;
59f65e6fbaSAlex Tomas }
60f65e6fbaSAlex Tomas 
61*d0d856e8SRandy Dunlap /*
62*d0d856e8SRandy Dunlap  * idx_pblock:
63*d0d856e8SRandy Dunlap  * combine low and high parts of a leaf physical block number into ext4_fsblk_t
64*d0d856e8SRandy Dunlap  */
65f65e6fbaSAlex Tomas static inline ext4_fsblk_t idx_pblock(struct ext4_extent_idx *ix)
66f65e6fbaSAlex Tomas {
67f65e6fbaSAlex Tomas 	ext4_fsblk_t block;
68f65e6fbaSAlex Tomas 
69f65e6fbaSAlex Tomas 	block = le32_to_cpu(ix->ei_leaf);
70f65e6fbaSAlex Tomas 	if (sizeof(ext4_fsblk_t) > 4)
71f65e6fbaSAlex Tomas 		block |= ((ext4_fsblk_t) le16_to_cpu(ix->ei_leaf_hi) << 31) << 1;
72f65e6fbaSAlex Tomas 	return block;
73f65e6fbaSAlex Tomas }
74f65e6fbaSAlex Tomas 
75*d0d856e8SRandy Dunlap /*
76*d0d856e8SRandy Dunlap  * ext4_ext_store_pblock:
77*d0d856e8SRandy Dunlap  * stores a large physical block number into an extent struct,
78*d0d856e8SRandy Dunlap  * breaking it into parts
79*d0d856e8SRandy Dunlap  */
80f65e6fbaSAlex Tomas static inline void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb)
81f65e6fbaSAlex Tomas {
82f65e6fbaSAlex Tomas 	ex->ee_start = cpu_to_le32((unsigned long) (pb & 0xffffffff));
83f65e6fbaSAlex Tomas 	if (sizeof(ext4_fsblk_t) > 4)
84f65e6fbaSAlex Tomas 		ex->ee_start_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
85f65e6fbaSAlex Tomas }
86f65e6fbaSAlex Tomas 
87*d0d856e8SRandy Dunlap /*
88*d0d856e8SRandy Dunlap  * ext4_idx_store_pblock:
89*d0d856e8SRandy Dunlap  * stores a large physical block number into an index struct,
90*d0d856e8SRandy Dunlap  * breaking it into parts
91*d0d856e8SRandy Dunlap  */
92f65e6fbaSAlex Tomas static inline void ext4_idx_store_pblock(struct ext4_extent_idx *ix, ext4_fsblk_t pb)
93f65e6fbaSAlex Tomas {
94f65e6fbaSAlex Tomas 	ix->ei_leaf = cpu_to_le32((unsigned long) (pb & 0xffffffff));
95f65e6fbaSAlex Tomas 	if (sizeof(ext4_fsblk_t) > 4)
96f65e6fbaSAlex Tomas 		ix->ei_leaf_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
97f65e6fbaSAlex Tomas }
98f65e6fbaSAlex Tomas 
99a86c6181SAlex Tomas static int ext4_ext_check_header(const char *function, struct inode *inode,
100a86c6181SAlex Tomas 				struct ext4_extent_header *eh)
101a86c6181SAlex Tomas {
102a86c6181SAlex Tomas 	const char *error_msg = NULL;
103a86c6181SAlex Tomas 
104a86c6181SAlex Tomas 	if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
105a86c6181SAlex Tomas 		error_msg = "invalid magic";
106a86c6181SAlex Tomas 		goto corrupted;
107a86c6181SAlex Tomas 	}
108a86c6181SAlex Tomas 	if (unlikely(eh->eh_max == 0)) {
109a86c6181SAlex Tomas 		error_msg = "invalid eh_max";
110a86c6181SAlex Tomas 		goto corrupted;
111a86c6181SAlex Tomas 	}
112a86c6181SAlex Tomas 	if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
113a86c6181SAlex Tomas 		error_msg = "invalid eh_entries";
114a86c6181SAlex Tomas 		goto corrupted;
115a86c6181SAlex Tomas 	}
116a86c6181SAlex Tomas 	return 0;
117a86c6181SAlex Tomas 
118a86c6181SAlex Tomas corrupted:
119a86c6181SAlex Tomas 	ext4_error(inode->i_sb, function,
120a86c6181SAlex Tomas 			"bad header in inode #%lu: %s - magic %x, "
121a86c6181SAlex Tomas 			"entries %u, max %u, depth %u",
122a86c6181SAlex Tomas 			inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic),
123a86c6181SAlex Tomas 			le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
124a86c6181SAlex Tomas 			le16_to_cpu(eh->eh_depth));
125a86c6181SAlex Tomas 
126a86c6181SAlex Tomas 	return -EIO;
127a86c6181SAlex Tomas }
128a86c6181SAlex Tomas 
129a86c6181SAlex Tomas static handle_t *ext4_ext_journal_restart(handle_t *handle, int needed)
130a86c6181SAlex Tomas {
131a86c6181SAlex Tomas 	int err;
132a86c6181SAlex Tomas 
133a86c6181SAlex Tomas 	if (handle->h_buffer_credits > needed)
134a86c6181SAlex Tomas 		return handle;
135a86c6181SAlex Tomas 	if (!ext4_journal_extend(handle, needed))
136a86c6181SAlex Tomas 		return handle;
137a86c6181SAlex Tomas 	err = ext4_journal_restart(handle, needed);
138a86c6181SAlex Tomas 
139a86c6181SAlex Tomas 	return handle;
140a86c6181SAlex Tomas }
141a86c6181SAlex Tomas 
142a86c6181SAlex Tomas /*
143a86c6181SAlex Tomas  * could return:
144a86c6181SAlex Tomas  *  - EROFS
145a86c6181SAlex Tomas  *  - ENOMEM
146a86c6181SAlex Tomas  */
147a86c6181SAlex Tomas static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
148a86c6181SAlex Tomas 				struct ext4_ext_path *path)
149a86c6181SAlex Tomas {
150a86c6181SAlex Tomas 	if (path->p_bh) {
151a86c6181SAlex Tomas 		/* path points to block */
152a86c6181SAlex Tomas 		return ext4_journal_get_write_access(handle, path->p_bh);
153a86c6181SAlex Tomas 	}
154a86c6181SAlex Tomas 	/* path points to leaf/index in inode body */
155a86c6181SAlex Tomas 	/* we use in-core data, no need to protect them */
156a86c6181SAlex Tomas 	return 0;
157a86c6181SAlex Tomas }
158a86c6181SAlex Tomas 
159a86c6181SAlex Tomas /*
160a86c6181SAlex Tomas  * could return:
161a86c6181SAlex Tomas  *  - EROFS
162a86c6181SAlex Tomas  *  - ENOMEM
163a86c6181SAlex Tomas  *  - EIO
164a86c6181SAlex Tomas  */
165a86c6181SAlex Tomas static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
166a86c6181SAlex Tomas 				struct ext4_ext_path *path)
167a86c6181SAlex Tomas {
168a86c6181SAlex Tomas 	int err;
169a86c6181SAlex Tomas 	if (path->p_bh) {
170a86c6181SAlex Tomas 		/* path points to block */
171a86c6181SAlex Tomas 		err = ext4_journal_dirty_metadata(handle, path->p_bh);
172a86c6181SAlex Tomas 	} else {
173a86c6181SAlex Tomas 		/* path points to leaf/index in inode body */
174a86c6181SAlex Tomas 		err = ext4_mark_inode_dirty(handle, inode);
175a86c6181SAlex Tomas 	}
176a86c6181SAlex Tomas 	return err;
177a86c6181SAlex Tomas }
178a86c6181SAlex Tomas 
179f65e6fbaSAlex Tomas static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
180a86c6181SAlex Tomas 			      struct ext4_ext_path *path,
181f65e6fbaSAlex Tomas 			      ext4_fsblk_t block)
182a86c6181SAlex Tomas {
183a86c6181SAlex Tomas 	struct ext4_inode_info *ei = EXT4_I(inode);
184f65e6fbaSAlex Tomas 	ext4_fsblk_t bg_start;
185f65e6fbaSAlex Tomas 	ext4_grpblk_t colour;
186a86c6181SAlex Tomas 	int depth;
187a86c6181SAlex Tomas 
188a86c6181SAlex Tomas 	if (path) {
189a86c6181SAlex Tomas 		struct ext4_extent *ex;
190a86c6181SAlex Tomas 		depth = path->p_depth;
191a86c6181SAlex Tomas 
192a86c6181SAlex Tomas 		/* try to predict block placement */
193a86c6181SAlex Tomas 		if ((ex = path[depth].p_ext))
194f65e6fbaSAlex Tomas 			return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block));
195a86c6181SAlex Tomas 
196*d0d856e8SRandy Dunlap 		/* it looks like index is empty;
197*d0d856e8SRandy Dunlap 		 * try to find starting block from index itself */
198a86c6181SAlex Tomas 		if (path[depth].p_bh)
199a86c6181SAlex Tomas 			return path[depth].p_bh->b_blocknr;
200a86c6181SAlex Tomas 	}
201a86c6181SAlex Tomas 
202a86c6181SAlex Tomas 	/* OK. use inode's group */
203a86c6181SAlex Tomas 	bg_start = (ei->i_block_group * EXT4_BLOCKS_PER_GROUP(inode->i_sb)) +
204a86c6181SAlex Tomas 		le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_first_data_block);
205a86c6181SAlex Tomas 	colour = (current->pid % 16) *
206a86c6181SAlex Tomas 			(EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
207a86c6181SAlex Tomas 	return bg_start + colour + block;
208a86c6181SAlex Tomas }
209a86c6181SAlex Tomas 
210f65e6fbaSAlex Tomas static ext4_fsblk_t
211a86c6181SAlex Tomas ext4_ext_new_block(handle_t *handle, struct inode *inode,
212a86c6181SAlex Tomas 			struct ext4_ext_path *path,
213a86c6181SAlex Tomas 			struct ext4_extent *ex, int *err)
214a86c6181SAlex Tomas {
215f65e6fbaSAlex Tomas 	ext4_fsblk_t goal, newblock;
216a86c6181SAlex Tomas 
217a86c6181SAlex Tomas 	goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
218a86c6181SAlex Tomas 	newblock = ext4_new_block(handle, inode, goal, err);
219a86c6181SAlex Tomas 	return newblock;
220a86c6181SAlex Tomas }
221a86c6181SAlex Tomas 
222a86c6181SAlex Tomas static inline int ext4_ext_space_block(struct inode *inode)
223a86c6181SAlex Tomas {
224a86c6181SAlex Tomas 	int size;
225a86c6181SAlex Tomas 
226a86c6181SAlex Tomas 	size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
227a86c6181SAlex Tomas 			/ sizeof(struct ext4_extent);
228a86c6181SAlex Tomas #ifdef AGRESSIVE_TEST
229a86c6181SAlex Tomas 	if (size > 6)
230a86c6181SAlex Tomas 		size = 6;
231a86c6181SAlex Tomas #endif
232a86c6181SAlex Tomas 	return size;
233a86c6181SAlex Tomas }
234a86c6181SAlex Tomas 
235a86c6181SAlex Tomas static inline int ext4_ext_space_block_idx(struct inode *inode)
236a86c6181SAlex Tomas {
237a86c6181SAlex Tomas 	int size;
238a86c6181SAlex Tomas 
239a86c6181SAlex Tomas 	size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
240a86c6181SAlex Tomas 			/ sizeof(struct ext4_extent_idx);
241a86c6181SAlex Tomas #ifdef AGRESSIVE_TEST
242a86c6181SAlex Tomas 	if (size > 5)
243a86c6181SAlex Tomas 		size = 5;
244a86c6181SAlex Tomas #endif
245a86c6181SAlex Tomas 	return size;
246a86c6181SAlex Tomas }
247a86c6181SAlex Tomas 
248a86c6181SAlex Tomas static inline int ext4_ext_space_root(struct inode *inode)
249a86c6181SAlex Tomas {
250a86c6181SAlex Tomas 	int size;
251a86c6181SAlex Tomas 
252a86c6181SAlex Tomas 	size = sizeof(EXT4_I(inode)->i_data);
253a86c6181SAlex Tomas 	size -= sizeof(struct ext4_extent_header);
254a86c6181SAlex Tomas 	size /= sizeof(struct ext4_extent);
255a86c6181SAlex Tomas #ifdef AGRESSIVE_TEST
256a86c6181SAlex Tomas 	if (size > 3)
257a86c6181SAlex Tomas 		size = 3;
258a86c6181SAlex Tomas #endif
259a86c6181SAlex Tomas 	return size;
260a86c6181SAlex Tomas }
261a86c6181SAlex Tomas 
262a86c6181SAlex Tomas static inline int ext4_ext_space_root_idx(struct inode *inode)
263a86c6181SAlex Tomas {
264a86c6181SAlex Tomas 	int size;
265a86c6181SAlex Tomas 
266a86c6181SAlex Tomas 	size = sizeof(EXT4_I(inode)->i_data);
267a86c6181SAlex Tomas 	size -= sizeof(struct ext4_extent_header);
268a86c6181SAlex Tomas 	size /= sizeof(struct ext4_extent_idx);
269a86c6181SAlex Tomas #ifdef AGRESSIVE_TEST
270a86c6181SAlex Tomas 	if (size > 4)
271a86c6181SAlex Tomas 		size = 4;
272a86c6181SAlex Tomas #endif
273a86c6181SAlex Tomas 	return size;
274a86c6181SAlex Tomas }
275a86c6181SAlex Tomas 
276a86c6181SAlex Tomas #ifdef EXT_DEBUG
277a86c6181SAlex Tomas static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
278a86c6181SAlex Tomas {
279a86c6181SAlex Tomas 	int k, l = path->p_depth;
280a86c6181SAlex Tomas 
281a86c6181SAlex Tomas 	ext_debug("path:");
282a86c6181SAlex Tomas 	for (k = 0; k <= l; k++, path++) {
283a86c6181SAlex Tomas 		if (path->p_idx) {
284f65e6fbaSAlex Tomas 		  ext_debug("  %d->"E3FSBLK, le32_to_cpu(path->p_idx->ei_block),
285f65e6fbaSAlex Tomas 			    idx_pblock(path->p_idx));
286a86c6181SAlex Tomas 		} else if (path->p_ext) {
287f65e6fbaSAlex Tomas 			ext_debug("  %d:%d:"E3FSBLK" ",
288a86c6181SAlex Tomas 				  le32_to_cpu(path->p_ext->ee_block),
289a86c6181SAlex Tomas 				  le16_to_cpu(path->p_ext->ee_len),
290f65e6fbaSAlex Tomas 				  ext_pblock(path->p_ext));
291a86c6181SAlex Tomas 		} else
292a86c6181SAlex Tomas 			ext_debug("  []");
293a86c6181SAlex Tomas 	}
294a86c6181SAlex Tomas 	ext_debug("\n");
295a86c6181SAlex Tomas }
296a86c6181SAlex Tomas 
297a86c6181SAlex Tomas static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
298a86c6181SAlex Tomas {
299a86c6181SAlex Tomas 	int depth = ext_depth(inode);
300a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
301a86c6181SAlex Tomas 	struct ext4_extent *ex;
302a86c6181SAlex Tomas 	int i;
303a86c6181SAlex Tomas 
304a86c6181SAlex Tomas 	if (!path)
305a86c6181SAlex Tomas 		return;
306a86c6181SAlex Tomas 
307a86c6181SAlex Tomas 	eh = path[depth].p_hdr;
308a86c6181SAlex Tomas 	ex = EXT_FIRST_EXTENT(eh);
309a86c6181SAlex Tomas 
310a86c6181SAlex Tomas 	for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
311f65e6fbaSAlex Tomas 		ext_debug("%d:%d:"E3FSBLK" ", le32_to_cpu(ex->ee_block),
312f65e6fbaSAlex Tomas 			  le16_to_cpu(ex->ee_len), ext_pblock(ex));
313a86c6181SAlex Tomas 	}
314a86c6181SAlex Tomas 	ext_debug("\n");
315a86c6181SAlex Tomas }
316a86c6181SAlex Tomas #else
317a86c6181SAlex Tomas #define ext4_ext_show_path(inode,path)
318a86c6181SAlex Tomas #define ext4_ext_show_leaf(inode,path)
319a86c6181SAlex Tomas #endif
320a86c6181SAlex Tomas 
321a86c6181SAlex Tomas static void ext4_ext_drop_refs(struct ext4_ext_path *path)
322a86c6181SAlex Tomas {
323a86c6181SAlex Tomas 	int depth = path->p_depth;
324a86c6181SAlex Tomas 	int i;
325a86c6181SAlex Tomas 
326a86c6181SAlex Tomas 	for (i = 0; i <= depth; i++, path++)
327a86c6181SAlex Tomas 		if (path->p_bh) {
328a86c6181SAlex Tomas 			brelse(path->p_bh);
329a86c6181SAlex Tomas 			path->p_bh = NULL;
330a86c6181SAlex Tomas 		}
331a86c6181SAlex Tomas }
332a86c6181SAlex Tomas 
333a86c6181SAlex Tomas /*
334*d0d856e8SRandy Dunlap  * ext4_ext_binsearch_idx:
335*d0d856e8SRandy Dunlap  * binary search for the closest index of the given block
336a86c6181SAlex Tomas  */
337a86c6181SAlex Tomas static void
338a86c6181SAlex Tomas ext4_ext_binsearch_idx(struct inode *inode, struct ext4_ext_path *path, int block)
339a86c6181SAlex Tomas {
340a86c6181SAlex Tomas 	struct ext4_extent_header *eh = path->p_hdr;
341a86c6181SAlex Tomas 	struct ext4_extent_idx *r, *l, *m;
342a86c6181SAlex Tomas 
343a86c6181SAlex Tomas 	BUG_ON(eh->eh_magic != EXT4_EXT_MAGIC);
344a86c6181SAlex Tomas 	BUG_ON(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max));
345a86c6181SAlex Tomas 	BUG_ON(le16_to_cpu(eh->eh_entries) <= 0);
346a86c6181SAlex Tomas 
347a86c6181SAlex Tomas 	ext_debug("binsearch for %d(idx):  ", block);
348a86c6181SAlex Tomas 
349a86c6181SAlex Tomas 	l = EXT_FIRST_INDEX(eh) + 1;
350a86c6181SAlex Tomas 	r = EXT_FIRST_INDEX(eh) + le16_to_cpu(eh->eh_entries) - 1;
351a86c6181SAlex Tomas 	while (l <= r) {
352a86c6181SAlex Tomas 		m = l + (r - l) / 2;
353a86c6181SAlex Tomas 		if (block < le32_to_cpu(m->ei_block))
354a86c6181SAlex Tomas 			r = m - 1;
355a86c6181SAlex Tomas 		else
356a86c6181SAlex Tomas 			l = m + 1;
357a86c6181SAlex Tomas 		ext_debug("%p(%u):%p(%u):%p(%u) ", l, l->ei_block,
358a86c6181SAlex Tomas 				m, m->ei_block, r, r->ei_block);
359a86c6181SAlex Tomas 	}
360a86c6181SAlex Tomas 
361a86c6181SAlex Tomas 	path->p_idx = l - 1;
362f65e6fbaSAlex Tomas 	ext_debug("  -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
363f65e6fbaSAlex Tomas 		  idx_block(path->p_idx));
364a86c6181SAlex Tomas 
365a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH
366a86c6181SAlex Tomas 	{
367a86c6181SAlex Tomas 		struct ext4_extent_idx *chix, *ix;
368a86c6181SAlex Tomas 		int k;
369a86c6181SAlex Tomas 
370a86c6181SAlex Tomas 		chix = ix = EXT_FIRST_INDEX(eh);
371a86c6181SAlex Tomas 		for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
372a86c6181SAlex Tomas 		  if (k != 0 &&
373a86c6181SAlex Tomas 		      le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
374a86c6181SAlex Tomas 				printk("k=%d, ix=0x%p, first=0x%p\n", k,
375a86c6181SAlex Tomas 					ix, EXT_FIRST_INDEX(eh));
376a86c6181SAlex Tomas 				printk("%u <= %u\n",
377a86c6181SAlex Tomas 				       le32_to_cpu(ix->ei_block),
378a86c6181SAlex Tomas 				       le32_to_cpu(ix[-1].ei_block));
379a86c6181SAlex Tomas 			}
380a86c6181SAlex Tomas 			BUG_ON(k && le32_to_cpu(ix->ei_block)
381a86c6181SAlex Tomas 				           <= le32_to_cpu(ix[-1].ei_block));
382a86c6181SAlex Tomas 			if (block < le32_to_cpu(ix->ei_block))
383a86c6181SAlex Tomas 				break;
384a86c6181SAlex Tomas 			chix = ix;
385a86c6181SAlex Tomas 		}
386a86c6181SAlex Tomas 		BUG_ON(chix != path->p_idx);
387a86c6181SAlex Tomas 	}
388a86c6181SAlex Tomas #endif
389a86c6181SAlex Tomas 
390a86c6181SAlex Tomas }
391a86c6181SAlex Tomas 
392a86c6181SAlex Tomas /*
393*d0d856e8SRandy Dunlap  * ext4_ext_binsearch:
394*d0d856e8SRandy Dunlap  * binary search for closest extent of the given block
395a86c6181SAlex Tomas  */
396a86c6181SAlex Tomas static void
397a86c6181SAlex Tomas ext4_ext_binsearch(struct inode *inode, struct ext4_ext_path *path, int block)
398a86c6181SAlex Tomas {
399a86c6181SAlex Tomas 	struct ext4_extent_header *eh = path->p_hdr;
400a86c6181SAlex Tomas 	struct ext4_extent *r, *l, *m;
401a86c6181SAlex Tomas 
402a86c6181SAlex Tomas 	BUG_ON(eh->eh_magic != EXT4_EXT_MAGIC);
403a86c6181SAlex Tomas 	BUG_ON(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max));
404a86c6181SAlex Tomas 
405a86c6181SAlex Tomas 	if (eh->eh_entries == 0) {
406a86c6181SAlex Tomas 		/*
407*d0d856e8SRandy Dunlap 		 * this leaf is empty:
408a86c6181SAlex Tomas 		 * we get such a leaf in split/add case
409a86c6181SAlex Tomas 		 */
410a86c6181SAlex Tomas 		return;
411a86c6181SAlex Tomas 	}
412a86c6181SAlex Tomas 
413a86c6181SAlex Tomas 	ext_debug("binsearch for %d:  ", block);
414a86c6181SAlex Tomas 
415a86c6181SAlex Tomas 	l = EXT_FIRST_EXTENT(eh) + 1;
416a86c6181SAlex Tomas 	r = EXT_FIRST_EXTENT(eh) + le16_to_cpu(eh->eh_entries) - 1;
417a86c6181SAlex Tomas 
418a86c6181SAlex Tomas 	while (l <= r) {
419a86c6181SAlex Tomas 		m = l + (r - l) / 2;
420a86c6181SAlex Tomas 		if (block < le32_to_cpu(m->ee_block))
421a86c6181SAlex Tomas 			r = m - 1;
422a86c6181SAlex Tomas 		else
423a86c6181SAlex Tomas 			l = m + 1;
424a86c6181SAlex Tomas 		ext_debug("%p(%u):%p(%u):%p(%u) ", l, l->ee_block,
425a86c6181SAlex Tomas 				m, m->ee_block, r, r->ee_block);
426a86c6181SAlex Tomas 	}
427a86c6181SAlex Tomas 
428a86c6181SAlex Tomas 	path->p_ext = l - 1;
429f65e6fbaSAlex Tomas 	ext_debug("  -> %d:"E3FSBLK":%d ",
430a86c6181SAlex Tomas 		        le32_to_cpu(path->p_ext->ee_block),
431f65e6fbaSAlex Tomas 		        ext_pblock(path->p_ext),
432a86c6181SAlex Tomas 			le16_to_cpu(path->p_ext->ee_len));
433a86c6181SAlex Tomas 
434a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH
435a86c6181SAlex Tomas 	{
436a86c6181SAlex Tomas 		struct ext4_extent *chex, *ex;
437a86c6181SAlex Tomas 		int k;
438a86c6181SAlex Tomas 
439a86c6181SAlex Tomas 		chex = ex = EXT_FIRST_EXTENT(eh);
440a86c6181SAlex Tomas 		for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
441a86c6181SAlex Tomas 			BUG_ON(k && le32_to_cpu(ex->ee_block)
442a86c6181SAlex Tomas 				          <= le32_to_cpu(ex[-1].ee_block));
443a86c6181SAlex Tomas 			if (block < le32_to_cpu(ex->ee_block))
444a86c6181SAlex Tomas 				break;
445a86c6181SAlex Tomas 			chex = ex;
446a86c6181SAlex Tomas 		}
447a86c6181SAlex Tomas 		BUG_ON(chex != path->p_ext);
448a86c6181SAlex Tomas 	}
449a86c6181SAlex Tomas #endif
450a86c6181SAlex Tomas 
451a86c6181SAlex Tomas }
452a86c6181SAlex Tomas 
453a86c6181SAlex Tomas int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
454a86c6181SAlex Tomas {
455a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
456a86c6181SAlex Tomas 
457a86c6181SAlex Tomas 	eh = ext_inode_hdr(inode);
458a86c6181SAlex Tomas 	eh->eh_depth = 0;
459a86c6181SAlex Tomas 	eh->eh_entries = 0;
460a86c6181SAlex Tomas 	eh->eh_magic = EXT4_EXT_MAGIC;
461a86c6181SAlex Tomas 	eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode));
462a86c6181SAlex Tomas 	ext4_mark_inode_dirty(handle, inode);
463a86c6181SAlex Tomas 	ext4_ext_invalidate_cache(inode);
464a86c6181SAlex Tomas 	return 0;
465a86c6181SAlex Tomas }
466a86c6181SAlex Tomas 
467a86c6181SAlex Tomas struct ext4_ext_path *
468a86c6181SAlex Tomas ext4_ext_find_extent(struct inode *inode, int block, struct ext4_ext_path *path)
469a86c6181SAlex Tomas {
470a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
471a86c6181SAlex Tomas 	struct buffer_head *bh;
472a86c6181SAlex Tomas 	short int depth, i, ppos = 0, alloc = 0;
473a86c6181SAlex Tomas 
474a86c6181SAlex Tomas 	eh = ext_inode_hdr(inode);
475a86c6181SAlex Tomas 	BUG_ON(eh == NULL);
476a86c6181SAlex Tomas 	if (ext4_ext_check_header(__FUNCTION__, inode, eh))
477a86c6181SAlex Tomas 		return ERR_PTR(-EIO);
478a86c6181SAlex Tomas 
479a86c6181SAlex Tomas 	i = depth = ext_depth(inode);
480a86c6181SAlex Tomas 
481a86c6181SAlex Tomas 	/* account possible depth increase */
482a86c6181SAlex Tomas 	if (!path) {
483a86c6181SAlex Tomas 		path = kmalloc(sizeof(struct ext4_ext_path) * (depth + 2),
484a86c6181SAlex Tomas 				GFP_NOFS);
485a86c6181SAlex Tomas 		if (!path)
486a86c6181SAlex Tomas 			return ERR_PTR(-ENOMEM);
487a86c6181SAlex Tomas 		alloc = 1;
488a86c6181SAlex Tomas 	}
489a86c6181SAlex Tomas 	memset(path, 0, sizeof(struct ext4_ext_path) * (depth + 1));
490a86c6181SAlex Tomas 	path[0].p_hdr = eh;
491a86c6181SAlex Tomas 
492a86c6181SAlex Tomas 	/* walk through the tree */
493a86c6181SAlex Tomas 	while (i) {
494a86c6181SAlex Tomas 		ext_debug("depth %d: num %d, max %d\n",
495a86c6181SAlex Tomas 			  ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
496a86c6181SAlex Tomas 		ext4_ext_binsearch_idx(inode, path + ppos, block);
497f65e6fbaSAlex Tomas 		path[ppos].p_block = idx_pblock(path[ppos].p_idx);
498a86c6181SAlex Tomas 		path[ppos].p_depth = i;
499a86c6181SAlex Tomas 		path[ppos].p_ext = NULL;
500a86c6181SAlex Tomas 
501a86c6181SAlex Tomas 		bh = sb_bread(inode->i_sb, path[ppos].p_block);
502a86c6181SAlex Tomas 		if (!bh)
503a86c6181SAlex Tomas 			goto err;
504a86c6181SAlex Tomas 
505a86c6181SAlex Tomas 		eh = ext_block_hdr(bh);
506a86c6181SAlex Tomas 		ppos++;
507a86c6181SAlex Tomas 		BUG_ON(ppos > depth);
508a86c6181SAlex Tomas 		path[ppos].p_bh = bh;
509a86c6181SAlex Tomas 		path[ppos].p_hdr = eh;
510a86c6181SAlex Tomas 		i--;
511a86c6181SAlex Tomas 
512a86c6181SAlex Tomas 		if (ext4_ext_check_header(__FUNCTION__, inode, eh))
513a86c6181SAlex Tomas 			goto err;
514a86c6181SAlex Tomas 	}
515a86c6181SAlex Tomas 
516a86c6181SAlex Tomas 	path[ppos].p_depth = i;
517a86c6181SAlex Tomas 	path[ppos].p_hdr = eh;
518a86c6181SAlex Tomas 	path[ppos].p_ext = NULL;
519a86c6181SAlex Tomas 	path[ppos].p_idx = NULL;
520a86c6181SAlex Tomas 
521a86c6181SAlex Tomas 	if (ext4_ext_check_header(__FUNCTION__, inode, eh))
522a86c6181SAlex Tomas 		goto err;
523a86c6181SAlex Tomas 
524a86c6181SAlex Tomas 	/* find extent */
525a86c6181SAlex Tomas 	ext4_ext_binsearch(inode, path + ppos, block);
526a86c6181SAlex Tomas 
527a86c6181SAlex Tomas 	ext4_ext_show_path(inode, path);
528a86c6181SAlex Tomas 
529a86c6181SAlex Tomas 	return path;
530a86c6181SAlex Tomas 
531a86c6181SAlex Tomas err:
532a86c6181SAlex Tomas 	ext4_ext_drop_refs(path);
533a86c6181SAlex Tomas 	if (alloc)
534a86c6181SAlex Tomas 		kfree(path);
535a86c6181SAlex Tomas 	return ERR_PTR(-EIO);
536a86c6181SAlex Tomas }
537a86c6181SAlex Tomas 
538a86c6181SAlex Tomas /*
539*d0d856e8SRandy Dunlap  * ext4_ext_insert_index:
540*d0d856e8SRandy Dunlap  * insert new index [@logical;@ptr] into the block at @curp;
541*d0d856e8SRandy Dunlap  * check where to insert: before @curp or after @curp
542a86c6181SAlex Tomas  */
543a86c6181SAlex Tomas static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
544a86c6181SAlex Tomas 				struct ext4_ext_path *curp,
545f65e6fbaSAlex Tomas 				int logical, ext4_fsblk_t ptr)
546a86c6181SAlex Tomas {
547a86c6181SAlex Tomas 	struct ext4_extent_idx *ix;
548a86c6181SAlex Tomas 	int len, err;
549a86c6181SAlex Tomas 
550a86c6181SAlex Tomas 	if ((err = ext4_ext_get_access(handle, inode, curp)))
551a86c6181SAlex Tomas 		return err;
552a86c6181SAlex Tomas 
553a86c6181SAlex Tomas 	BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block));
554a86c6181SAlex Tomas 	len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
555a86c6181SAlex Tomas 	if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
556a86c6181SAlex Tomas 		/* insert after */
557a86c6181SAlex Tomas 		if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
558a86c6181SAlex Tomas 			len = (len - 1) * sizeof(struct ext4_extent_idx);
559a86c6181SAlex Tomas 			len = len < 0 ? 0 : len;
560a86c6181SAlex Tomas 			ext_debug("insert new index %d after: %d. "
561a86c6181SAlex Tomas 					"move %d from 0x%p to 0x%p\n",
562a86c6181SAlex Tomas 					logical, ptr, len,
563a86c6181SAlex Tomas 					(curp->p_idx + 1), (curp->p_idx + 2));
564a86c6181SAlex Tomas 			memmove(curp->p_idx + 2, curp->p_idx + 1, len);
565a86c6181SAlex Tomas 		}
566a86c6181SAlex Tomas 		ix = curp->p_idx + 1;
567a86c6181SAlex Tomas 	} else {
568a86c6181SAlex Tomas 		/* insert before */
569a86c6181SAlex Tomas 		len = len * sizeof(struct ext4_extent_idx);
570a86c6181SAlex Tomas 		len = len < 0 ? 0 : len;
571a86c6181SAlex Tomas 		ext_debug("insert new index %d before: %d. "
572a86c6181SAlex Tomas 				"move %d from 0x%p to 0x%p\n",
573a86c6181SAlex Tomas 				logical, ptr, len,
574a86c6181SAlex Tomas 				curp->p_idx, (curp->p_idx + 1));
575a86c6181SAlex Tomas 		memmove(curp->p_idx + 1, curp->p_idx, len);
576a86c6181SAlex Tomas 		ix = curp->p_idx;
577a86c6181SAlex Tomas 	}
578a86c6181SAlex Tomas 
579a86c6181SAlex Tomas 	ix->ei_block = cpu_to_le32(logical);
580f65e6fbaSAlex Tomas 	ext4_idx_store_pblock(ix, ptr);
581a86c6181SAlex Tomas 	curp->p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(curp->p_hdr->eh_entries)+1);
582a86c6181SAlex Tomas 
583a86c6181SAlex Tomas 	BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries)
584a86c6181SAlex Tomas 	                     > le16_to_cpu(curp->p_hdr->eh_max));
585a86c6181SAlex Tomas 	BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr));
586a86c6181SAlex Tomas 
587a86c6181SAlex Tomas 	err = ext4_ext_dirty(handle, inode, curp);
588a86c6181SAlex Tomas 	ext4_std_error(inode->i_sb, err);
589a86c6181SAlex Tomas 
590a86c6181SAlex Tomas 	return err;
591a86c6181SAlex Tomas }
592a86c6181SAlex Tomas 
593a86c6181SAlex Tomas /*
594*d0d856e8SRandy Dunlap  * ext4_ext_split:
595*d0d856e8SRandy Dunlap  * inserts new subtree into the path, using free index entry
596*d0d856e8SRandy Dunlap  * at depth @at:
597a86c6181SAlex Tomas  * - allocates all needed blocks (new leaf and all intermediate index blocks)
598a86c6181SAlex Tomas  * - makes decision where to split
599*d0d856e8SRandy Dunlap  * - moves remaining extents and index entries (right to the split point)
600a86c6181SAlex Tomas  *   into the newly allocated blocks
601*d0d856e8SRandy Dunlap  * - initializes subtree
602a86c6181SAlex Tomas  */
603a86c6181SAlex Tomas static int ext4_ext_split(handle_t *handle, struct inode *inode,
604a86c6181SAlex Tomas 				struct ext4_ext_path *path,
605a86c6181SAlex Tomas 				struct ext4_extent *newext, int at)
606a86c6181SAlex Tomas {
607a86c6181SAlex Tomas 	struct buffer_head *bh = NULL;
608a86c6181SAlex Tomas 	int depth = ext_depth(inode);
609a86c6181SAlex Tomas 	struct ext4_extent_header *neh;
610a86c6181SAlex Tomas 	struct ext4_extent_idx *fidx;
611a86c6181SAlex Tomas 	struct ext4_extent *ex;
612a86c6181SAlex Tomas 	int i = at, k, m, a;
613f65e6fbaSAlex Tomas 	ext4_fsblk_t newblock, oldblock;
614a86c6181SAlex Tomas 	__le32 border;
615f65e6fbaSAlex Tomas 	ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
616a86c6181SAlex Tomas 	int err = 0;
617a86c6181SAlex Tomas 
618a86c6181SAlex Tomas 	/* make decision: where to split? */
619*d0d856e8SRandy Dunlap 	/* FIXME: now decision is simplest: at current extent */
620a86c6181SAlex Tomas 
621*d0d856e8SRandy Dunlap 	/* if current leaf will be split, then we should use
622a86c6181SAlex Tomas 	 * border from split point */
623a86c6181SAlex Tomas 	BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr));
624a86c6181SAlex Tomas 	if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
625a86c6181SAlex Tomas 		border = path[depth].p_ext[1].ee_block;
626*d0d856e8SRandy Dunlap 		ext_debug("leaf will be split."
627a86c6181SAlex Tomas 				" next leaf starts at %d\n",
628a86c6181SAlex Tomas 			          le32_to_cpu(border));
629a86c6181SAlex Tomas 	} else {
630a86c6181SAlex Tomas 		border = newext->ee_block;
631a86c6181SAlex Tomas 		ext_debug("leaf will be added."
632a86c6181SAlex Tomas 				" next leaf starts at %d\n",
633a86c6181SAlex Tomas 			        le32_to_cpu(border));
634a86c6181SAlex Tomas 	}
635a86c6181SAlex Tomas 
636a86c6181SAlex Tomas 	/*
637*d0d856e8SRandy Dunlap 	 * If error occurs, then we break processing
638*d0d856e8SRandy Dunlap 	 * and mark filesystem read-only. index won't
639a86c6181SAlex Tomas 	 * be inserted and tree will be in consistent
640*d0d856e8SRandy Dunlap 	 * state. Next mount will repair buffers too.
641a86c6181SAlex Tomas 	 */
642a86c6181SAlex Tomas 
643a86c6181SAlex Tomas 	/*
644*d0d856e8SRandy Dunlap 	 * Get array to track all allocated blocks.
645*d0d856e8SRandy Dunlap 	 * We need this to handle errors and free blocks
646*d0d856e8SRandy Dunlap 	 * upon them.
647a86c6181SAlex Tomas 	 */
648f65e6fbaSAlex Tomas 	ablocks = kmalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
649a86c6181SAlex Tomas 	if (!ablocks)
650a86c6181SAlex Tomas 		return -ENOMEM;
651f65e6fbaSAlex Tomas 	memset(ablocks, 0, sizeof(ext4_fsblk_t) * depth);
652a86c6181SAlex Tomas 
653a86c6181SAlex Tomas 	/* allocate all needed blocks */
654a86c6181SAlex Tomas 	ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
655a86c6181SAlex Tomas 	for (a = 0; a < depth - at; a++) {
656a86c6181SAlex Tomas 		newblock = ext4_ext_new_block(handle, inode, path, newext, &err);
657a86c6181SAlex Tomas 		if (newblock == 0)
658a86c6181SAlex Tomas 			goto cleanup;
659a86c6181SAlex Tomas 		ablocks[a] = newblock;
660a86c6181SAlex Tomas 	}
661a86c6181SAlex Tomas 
662a86c6181SAlex Tomas 	/* initialize new leaf */
663a86c6181SAlex Tomas 	newblock = ablocks[--a];
664a86c6181SAlex Tomas 	BUG_ON(newblock == 0);
665a86c6181SAlex Tomas 	bh = sb_getblk(inode->i_sb, newblock);
666a86c6181SAlex Tomas 	if (!bh) {
667a86c6181SAlex Tomas 		err = -EIO;
668a86c6181SAlex Tomas 		goto cleanup;
669a86c6181SAlex Tomas 	}
670a86c6181SAlex Tomas 	lock_buffer(bh);
671a86c6181SAlex Tomas 
672a86c6181SAlex Tomas 	if ((err = ext4_journal_get_create_access(handle, bh)))
673a86c6181SAlex Tomas 		goto cleanup;
674a86c6181SAlex Tomas 
675a86c6181SAlex Tomas 	neh = ext_block_hdr(bh);
676a86c6181SAlex Tomas 	neh->eh_entries = 0;
677a86c6181SAlex Tomas 	neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
678a86c6181SAlex Tomas 	neh->eh_magic = EXT4_EXT_MAGIC;
679a86c6181SAlex Tomas 	neh->eh_depth = 0;
680a86c6181SAlex Tomas 	ex = EXT_FIRST_EXTENT(neh);
681a86c6181SAlex Tomas 
682*d0d856e8SRandy Dunlap 	/* move remainder of path[depth] to the new leaf */
683a86c6181SAlex Tomas 	BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max);
684a86c6181SAlex Tomas 	/* start copy from next extent */
685a86c6181SAlex Tomas 	/* TODO: we could do it by single memmove */
686a86c6181SAlex Tomas 	m = 0;
687a86c6181SAlex Tomas 	path[depth].p_ext++;
688a86c6181SAlex Tomas 	while (path[depth].p_ext <=
689a86c6181SAlex Tomas 			EXT_MAX_EXTENT(path[depth].p_hdr)) {
690f65e6fbaSAlex Tomas 		ext_debug("move %d:"E3FSBLK":%d in new leaf "E3FSBLK"\n",
691a86c6181SAlex Tomas 			        le32_to_cpu(path[depth].p_ext->ee_block),
692f65e6fbaSAlex Tomas 			        ext_pblock(path[depth].p_ext),
693a86c6181SAlex Tomas 			        le16_to_cpu(path[depth].p_ext->ee_len),
694a86c6181SAlex Tomas 				newblock);
695a86c6181SAlex Tomas 		/*memmove(ex++, path[depth].p_ext++,
696a86c6181SAlex Tomas 				sizeof(struct ext4_extent));
697a86c6181SAlex Tomas 		neh->eh_entries++;*/
698a86c6181SAlex Tomas 		path[depth].p_ext++;
699a86c6181SAlex Tomas 		m++;
700a86c6181SAlex Tomas 	}
701a86c6181SAlex Tomas 	if (m) {
702a86c6181SAlex Tomas 		memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
703a86c6181SAlex Tomas 		neh->eh_entries = cpu_to_le16(le16_to_cpu(neh->eh_entries)+m);
704a86c6181SAlex Tomas 	}
705a86c6181SAlex Tomas 
706a86c6181SAlex Tomas 	set_buffer_uptodate(bh);
707a86c6181SAlex Tomas 	unlock_buffer(bh);
708a86c6181SAlex Tomas 
709a86c6181SAlex Tomas 	if ((err = ext4_journal_dirty_metadata(handle, bh)))
710a86c6181SAlex Tomas 		goto cleanup;
711a86c6181SAlex Tomas 	brelse(bh);
712a86c6181SAlex Tomas 	bh = NULL;
713a86c6181SAlex Tomas 
714a86c6181SAlex Tomas 	/* correct old leaf */
715a86c6181SAlex Tomas 	if (m) {
716a86c6181SAlex Tomas 		if ((err = ext4_ext_get_access(handle, inode, path + depth)))
717a86c6181SAlex Tomas 			goto cleanup;
718a86c6181SAlex Tomas 		path[depth].p_hdr->eh_entries =
719a86c6181SAlex Tomas 		     cpu_to_le16(le16_to_cpu(path[depth].p_hdr->eh_entries)-m);
720a86c6181SAlex Tomas 		if ((err = ext4_ext_dirty(handle, inode, path + depth)))
721a86c6181SAlex Tomas 			goto cleanup;
722a86c6181SAlex Tomas 
723a86c6181SAlex Tomas 	}
724a86c6181SAlex Tomas 
725a86c6181SAlex Tomas 	/* create intermediate indexes */
726a86c6181SAlex Tomas 	k = depth - at - 1;
727a86c6181SAlex Tomas 	BUG_ON(k < 0);
728a86c6181SAlex Tomas 	if (k)
729a86c6181SAlex Tomas 		ext_debug("create %d intermediate indices\n", k);
730a86c6181SAlex Tomas 	/* insert new index into current index block */
731a86c6181SAlex Tomas 	/* current depth stored in i var */
732a86c6181SAlex Tomas 	i = depth - 1;
733a86c6181SAlex Tomas 	while (k--) {
734a86c6181SAlex Tomas 		oldblock = newblock;
735a86c6181SAlex Tomas 		newblock = ablocks[--a];
736f65e6fbaSAlex Tomas 		bh = sb_getblk(inode->i_sb, (ext4_fsblk_t)newblock);
737a86c6181SAlex Tomas 		if (!bh) {
738a86c6181SAlex Tomas 			err = -EIO;
739a86c6181SAlex Tomas 			goto cleanup;
740a86c6181SAlex Tomas 		}
741a86c6181SAlex Tomas 		lock_buffer(bh);
742a86c6181SAlex Tomas 
743a86c6181SAlex Tomas 		if ((err = ext4_journal_get_create_access(handle, bh)))
744a86c6181SAlex Tomas 			goto cleanup;
745a86c6181SAlex Tomas 
746a86c6181SAlex Tomas 		neh = ext_block_hdr(bh);
747a86c6181SAlex Tomas 		neh->eh_entries = cpu_to_le16(1);
748a86c6181SAlex Tomas 		neh->eh_magic = EXT4_EXT_MAGIC;
749a86c6181SAlex Tomas 		neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
750a86c6181SAlex Tomas 		neh->eh_depth = cpu_to_le16(depth - i);
751a86c6181SAlex Tomas 		fidx = EXT_FIRST_INDEX(neh);
752a86c6181SAlex Tomas 		fidx->ei_block = border;
753f65e6fbaSAlex Tomas 		ext4_idx_store_pblock(fidx, oldblock);
754a86c6181SAlex Tomas 
755f65e6fbaSAlex Tomas 		ext_debug("int.index at %d (block "E3FSBLK"): %lu -> "E3FSBLK"\n", i,
756a86c6181SAlex Tomas 				newblock, (unsigned long) le32_to_cpu(border),
757a86c6181SAlex Tomas 				oldblock);
758a86c6181SAlex Tomas 		/* copy indexes */
759a86c6181SAlex Tomas 		m = 0;
760a86c6181SAlex Tomas 		path[i].p_idx++;
761a86c6181SAlex Tomas 
762a86c6181SAlex Tomas 		ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
763a86c6181SAlex Tomas 				EXT_MAX_INDEX(path[i].p_hdr));
764a86c6181SAlex Tomas 		BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) !=
765a86c6181SAlex Tomas 				EXT_LAST_INDEX(path[i].p_hdr));
766a86c6181SAlex Tomas 		while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
767f65e6fbaSAlex Tomas 			ext_debug("%d: move %d:%d in new index "E3FSBLK"\n", i,
768a86c6181SAlex Tomas 				        le32_to_cpu(path[i].p_idx->ei_block),
769f65e6fbaSAlex Tomas 				        idx_pblock(path[i].p_idx),
770a86c6181SAlex Tomas 				        newblock);
771a86c6181SAlex Tomas 			/*memmove(++fidx, path[i].p_idx++,
772a86c6181SAlex Tomas 					sizeof(struct ext4_extent_idx));
773a86c6181SAlex Tomas 			neh->eh_entries++;
774a86c6181SAlex Tomas 			BUG_ON(neh->eh_entries > neh->eh_max);*/
775a86c6181SAlex Tomas 			path[i].p_idx++;
776a86c6181SAlex Tomas 			m++;
777a86c6181SAlex Tomas 		}
778a86c6181SAlex Tomas 		if (m) {
779a86c6181SAlex Tomas 			memmove(++fidx, path[i].p_idx - m,
780a86c6181SAlex Tomas 				sizeof(struct ext4_extent_idx) * m);
781a86c6181SAlex Tomas 			neh->eh_entries =
782a86c6181SAlex Tomas 				cpu_to_le16(le16_to_cpu(neh->eh_entries) + m);
783a86c6181SAlex Tomas 		}
784a86c6181SAlex Tomas 		set_buffer_uptodate(bh);
785a86c6181SAlex Tomas 		unlock_buffer(bh);
786a86c6181SAlex Tomas 
787a86c6181SAlex Tomas 		if ((err = ext4_journal_dirty_metadata(handle, bh)))
788a86c6181SAlex Tomas 			goto cleanup;
789a86c6181SAlex Tomas 		brelse(bh);
790a86c6181SAlex Tomas 		bh = NULL;
791a86c6181SAlex Tomas 
792a86c6181SAlex Tomas 		/* correct old index */
793a86c6181SAlex Tomas 		if (m) {
794a86c6181SAlex Tomas 			err = ext4_ext_get_access(handle, inode, path + i);
795a86c6181SAlex Tomas 			if (err)
796a86c6181SAlex Tomas 				goto cleanup;
797a86c6181SAlex Tomas 			path[i].p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(path[i].p_hdr->eh_entries)-m);
798a86c6181SAlex Tomas 			err = ext4_ext_dirty(handle, inode, path + i);
799a86c6181SAlex Tomas 			if (err)
800a86c6181SAlex Tomas 				goto cleanup;
801a86c6181SAlex Tomas 		}
802a86c6181SAlex Tomas 
803a86c6181SAlex Tomas 		i--;
804a86c6181SAlex Tomas 	}
805a86c6181SAlex Tomas 
806a86c6181SAlex Tomas 	/* insert new index */
807a86c6181SAlex Tomas 	if (err)
808a86c6181SAlex Tomas 		goto cleanup;
809a86c6181SAlex Tomas 
810a86c6181SAlex Tomas 	err = ext4_ext_insert_index(handle, inode, path + at,
811a86c6181SAlex Tomas 				    le32_to_cpu(border), newblock);
812a86c6181SAlex Tomas 
813a86c6181SAlex Tomas cleanup:
814a86c6181SAlex Tomas 	if (bh) {
815a86c6181SAlex Tomas 		if (buffer_locked(bh))
816a86c6181SAlex Tomas 			unlock_buffer(bh);
817a86c6181SAlex Tomas 		brelse(bh);
818a86c6181SAlex Tomas 	}
819a86c6181SAlex Tomas 
820a86c6181SAlex Tomas 	if (err) {
821a86c6181SAlex Tomas 		/* free all allocated blocks in error case */
822a86c6181SAlex Tomas 		for (i = 0; i < depth; i++) {
823a86c6181SAlex Tomas 			if (!ablocks[i])
824a86c6181SAlex Tomas 				continue;
825a86c6181SAlex Tomas 			ext4_free_blocks(handle, inode, ablocks[i], 1);
826a86c6181SAlex Tomas 		}
827a86c6181SAlex Tomas 	}
828a86c6181SAlex Tomas 	kfree(ablocks);
829a86c6181SAlex Tomas 
830a86c6181SAlex Tomas 	return err;
831a86c6181SAlex Tomas }
832a86c6181SAlex Tomas 
833a86c6181SAlex Tomas /*
834*d0d856e8SRandy Dunlap  * ext4_ext_grow_indepth:
835*d0d856e8SRandy Dunlap  * implements tree growing procedure:
836a86c6181SAlex Tomas  * - allocates new block
837a86c6181SAlex Tomas  * - moves top-level data (index block or leaf) into the new block
838*d0d856e8SRandy Dunlap  * - initializes new top-level, creating index that points to the
839a86c6181SAlex Tomas  *   just created block
840a86c6181SAlex Tomas  */
841a86c6181SAlex Tomas static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
842a86c6181SAlex Tomas 					struct ext4_ext_path *path,
843a86c6181SAlex Tomas 					struct ext4_extent *newext)
844a86c6181SAlex Tomas {
845a86c6181SAlex Tomas 	struct ext4_ext_path *curp = path;
846a86c6181SAlex Tomas 	struct ext4_extent_header *neh;
847a86c6181SAlex Tomas 	struct ext4_extent_idx *fidx;
848a86c6181SAlex Tomas 	struct buffer_head *bh;
849f65e6fbaSAlex Tomas 	ext4_fsblk_t newblock;
850a86c6181SAlex Tomas 	int err = 0;
851a86c6181SAlex Tomas 
852a86c6181SAlex Tomas 	newblock = ext4_ext_new_block(handle, inode, path, newext, &err);
853a86c6181SAlex Tomas 	if (newblock == 0)
854a86c6181SAlex Tomas 		return err;
855a86c6181SAlex Tomas 
856a86c6181SAlex Tomas 	bh = sb_getblk(inode->i_sb, newblock);
857a86c6181SAlex Tomas 	if (!bh) {
858a86c6181SAlex Tomas 		err = -EIO;
859a86c6181SAlex Tomas 		ext4_std_error(inode->i_sb, err);
860a86c6181SAlex Tomas 		return err;
861a86c6181SAlex Tomas 	}
862a86c6181SAlex Tomas 	lock_buffer(bh);
863a86c6181SAlex Tomas 
864a86c6181SAlex Tomas 	if ((err = ext4_journal_get_create_access(handle, bh))) {
865a86c6181SAlex Tomas 		unlock_buffer(bh);
866a86c6181SAlex Tomas 		goto out;
867a86c6181SAlex Tomas 	}
868a86c6181SAlex Tomas 
869a86c6181SAlex Tomas 	/* move top-level index/leaf into new block */
870a86c6181SAlex Tomas 	memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
871a86c6181SAlex Tomas 
872a86c6181SAlex Tomas 	/* set size of new block */
873a86c6181SAlex Tomas 	neh = ext_block_hdr(bh);
874a86c6181SAlex Tomas 	/* old root could have indexes or leaves
875a86c6181SAlex Tomas 	 * so calculate e_max right way */
876a86c6181SAlex Tomas 	if (ext_depth(inode))
877a86c6181SAlex Tomas 	  neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
878a86c6181SAlex Tomas 	else
879a86c6181SAlex Tomas 	  neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
880a86c6181SAlex Tomas 	neh->eh_magic = EXT4_EXT_MAGIC;
881a86c6181SAlex Tomas 	set_buffer_uptodate(bh);
882a86c6181SAlex Tomas 	unlock_buffer(bh);
883a86c6181SAlex Tomas 
884a86c6181SAlex Tomas 	if ((err = ext4_journal_dirty_metadata(handle, bh)))
885a86c6181SAlex Tomas 		goto out;
886a86c6181SAlex Tomas 
887a86c6181SAlex Tomas 	/* create index in new top-level index: num,max,pointer */
888a86c6181SAlex Tomas 	if ((err = ext4_ext_get_access(handle, inode, curp)))
889a86c6181SAlex Tomas 		goto out;
890a86c6181SAlex Tomas 
891a86c6181SAlex Tomas 	curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
892a86c6181SAlex Tomas 	curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode));
893a86c6181SAlex Tomas 	curp->p_hdr->eh_entries = cpu_to_le16(1);
894a86c6181SAlex Tomas 	curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
895a86c6181SAlex Tomas 	/* FIXME: it works, but actually path[0] can be index */
896a86c6181SAlex Tomas 	curp->p_idx->ei_block = EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
897f65e6fbaSAlex Tomas 	ext4_idx_store_pblock(curp->p_idx, newblock);
898a86c6181SAlex Tomas 
899a86c6181SAlex Tomas 	neh = ext_inode_hdr(inode);
900a86c6181SAlex Tomas 	fidx = EXT_FIRST_INDEX(neh);
901f65e6fbaSAlex Tomas 	ext_debug("new root: num %d(%d), lblock %d, ptr "E3FSBLK"\n",
902a86c6181SAlex Tomas 		  le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
903f65e6fbaSAlex Tomas 		  le32_to_cpu(fidx->ei_block), idx_pblock(fidx));
904a86c6181SAlex Tomas 
905a86c6181SAlex Tomas 	neh->eh_depth = cpu_to_le16(path->p_depth + 1);
906a86c6181SAlex Tomas 	err = ext4_ext_dirty(handle, inode, curp);
907a86c6181SAlex Tomas out:
908a86c6181SAlex Tomas 	brelse(bh);
909a86c6181SAlex Tomas 
910a86c6181SAlex Tomas 	return err;
911a86c6181SAlex Tomas }
912a86c6181SAlex Tomas 
913a86c6181SAlex Tomas /*
914*d0d856e8SRandy Dunlap  * ext4_ext_create_new_leaf:
915*d0d856e8SRandy Dunlap  * finds empty index and adds new leaf.
916*d0d856e8SRandy Dunlap  * if no free index is found, then it requests in-depth growing.
917a86c6181SAlex Tomas  */
918a86c6181SAlex Tomas static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
919a86c6181SAlex Tomas 					struct ext4_ext_path *path,
920a86c6181SAlex Tomas 					struct ext4_extent *newext)
921a86c6181SAlex Tomas {
922a86c6181SAlex Tomas 	struct ext4_ext_path *curp;
923a86c6181SAlex Tomas 	int depth, i, err = 0;
924a86c6181SAlex Tomas 
925a86c6181SAlex Tomas repeat:
926a86c6181SAlex Tomas 	i = depth = ext_depth(inode);
927a86c6181SAlex Tomas 
928a86c6181SAlex Tomas 	/* walk up to the tree and look for free index entry */
929a86c6181SAlex Tomas 	curp = path + depth;
930a86c6181SAlex Tomas 	while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
931a86c6181SAlex Tomas 		i--;
932a86c6181SAlex Tomas 		curp--;
933a86c6181SAlex Tomas 	}
934a86c6181SAlex Tomas 
935*d0d856e8SRandy Dunlap 	/* we use already allocated block for index block,
936*d0d856e8SRandy Dunlap 	 * so subsequent data blocks should be contiguous */
937a86c6181SAlex Tomas 	if (EXT_HAS_FREE_INDEX(curp)) {
938a86c6181SAlex Tomas 		/* if we found index with free entry, then use that
939a86c6181SAlex Tomas 		 * entry: create all needed subtree and add new leaf */
940a86c6181SAlex Tomas 		err = ext4_ext_split(handle, inode, path, newext, i);
941a86c6181SAlex Tomas 
942a86c6181SAlex Tomas 		/* refill path */
943a86c6181SAlex Tomas 		ext4_ext_drop_refs(path);
944a86c6181SAlex Tomas 		path = ext4_ext_find_extent(inode,
945a86c6181SAlex Tomas 					    le32_to_cpu(newext->ee_block),
946a86c6181SAlex Tomas 					    path);
947a86c6181SAlex Tomas 		if (IS_ERR(path))
948a86c6181SAlex Tomas 			err = PTR_ERR(path);
949a86c6181SAlex Tomas 	} else {
950a86c6181SAlex Tomas 		/* tree is full, time to grow in depth */
951a86c6181SAlex Tomas 		err = ext4_ext_grow_indepth(handle, inode, path, newext);
952a86c6181SAlex Tomas 		if (err)
953a86c6181SAlex Tomas 			goto out;
954a86c6181SAlex Tomas 
955a86c6181SAlex Tomas 		/* refill path */
956a86c6181SAlex Tomas 		ext4_ext_drop_refs(path);
957a86c6181SAlex Tomas 		path = ext4_ext_find_extent(inode,
958a86c6181SAlex Tomas 					    le32_to_cpu(newext->ee_block),
959a86c6181SAlex Tomas 					    path);
960a86c6181SAlex Tomas 		if (IS_ERR(path)) {
961a86c6181SAlex Tomas 			err = PTR_ERR(path);
962a86c6181SAlex Tomas 			goto out;
963a86c6181SAlex Tomas 		}
964a86c6181SAlex Tomas 
965a86c6181SAlex Tomas 		/*
966*d0d856e8SRandy Dunlap 		 * only first (depth 0 -> 1) produces free space;
967*d0d856e8SRandy Dunlap 		 * in all other cases we have to split the grown tree
968a86c6181SAlex Tomas 		 */
969a86c6181SAlex Tomas 		depth = ext_depth(inode);
970a86c6181SAlex Tomas 		if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
971*d0d856e8SRandy Dunlap 			/* now we need to split */
972a86c6181SAlex Tomas 			goto repeat;
973a86c6181SAlex Tomas 		}
974a86c6181SAlex Tomas 	}
975a86c6181SAlex Tomas 
976a86c6181SAlex Tomas out:
977a86c6181SAlex Tomas 	return err;
978a86c6181SAlex Tomas }
979a86c6181SAlex Tomas 
980a86c6181SAlex Tomas /*
981*d0d856e8SRandy Dunlap  * ext4_ext_next_allocated_block:
982*d0d856e8SRandy Dunlap  * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
983*d0d856e8SRandy Dunlap  * NOTE: it considers block number from index entry as
984*d0d856e8SRandy Dunlap  * allocated block. Thus, index entries have to be consistent
985*d0d856e8SRandy Dunlap  * with leaves.
986a86c6181SAlex Tomas  */
987a86c6181SAlex Tomas static unsigned long
988a86c6181SAlex Tomas ext4_ext_next_allocated_block(struct ext4_ext_path *path)
989a86c6181SAlex Tomas {
990a86c6181SAlex Tomas 	int depth;
991a86c6181SAlex Tomas 
992a86c6181SAlex Tomas 	BUG_ON(path == NULL);
993a86c6181SAlex Tomas 	depth = path->p_depth;
994a86c6181SAlex Tomas 
995a86c6181SAlex Tomas 	if (depth == 0 && path->p_ext == NULL)
996a86c6181SAlex Tomas 		return EXT_MAX_BLOCK;
997a86c6181SAlex Tomas 
998a86c6181SAlex Tomas 	while (depth >= 0) {
999a86c6181SAlex Tomas 		if (depth == path->p_depth) {
1000a86c6181SAlex Tomas 			/* leaf */
1001a86c6181SAlex Tomas 			if (path[depth].p_ext !=
1002a86c6181SAlex Tomas 					EXT_LAST_EXTENT(path[depth].p_hdr))
1003a86c6181SAlex Tomas 			  return le32_to_cpu(path[depth].p_ext[1].ee_block);
1004a86c6181SAlex Tomas 		} else {
1005a86c6181SAlex Tomas 			/* index */
1006a86c6181SAlex Tomas 			if (path[depth].p_idx !=
1007a86c6181SAlex Tomas 					EXT_LAST_INDEX(path[depth].p_hdr))
1008a86c6181SAlex Tomas 			  return le32_to_cpu(path[depth].p_idx[1].ei_block);
1009a86c6181SAlex Tomas 		}
1010a86c6181SAlex Tomas 		depth--;
1011a86c6181SAlex Tomas 	}
1012a86c6181SAlex Tomas 
1013a86c6181SAlex Tomas 	return EXT_MAX_BLOCK;
1014a86c6181SAlex Tomas }
1015a86c6181SAlex Tomas 
1016a86c6181SAlex Tomas /*
1017*d0d856e8SRandy Dunlap  * ext4_ext_next_leaf_block:
1018a86c6181SAlex Tomas  * returns first allocated block from next leaf or EXT_MAX_BLOCK
1019a86c6181SAlex Tomas  */
1020a86c6181SAlex Tomas static unsigned ext4_ext_next_leaf_block(struct inode *inode,
1021a86c6181SAlex Tomas                                                struct ext4_ext_path *path)
1022a86c6181SAlex Tomas {
1023a86c6181SAlex Tomas 	int depth;
1024a86c6181SAlex Tomas 
1025a86c6181SAlex Tomas 	BUG_ON(path == NULL);
1026a86c6181SAlex Tomas 	depth = path->p_depth;
1027a86c6181SAlex Tomas 
1028a86c6181SAlex Tomas 	/* zero-tree has no leaf blocks at all */
1029a86c6181SAlex Tomas 	if (depth == 0)
1030a86c6181SAlex Tomas 		return EXT_MAX_BLOCK;
1031a86c6181SAlex Tomas 
1032a86c6181SAlex Tomas 	/* go to index block */
1033a86c6181SAlex Tomas 	depth--;
1034a86c6181SAlex Tomas 
1035a86c6181SAlex Tomas 	while (depth >= 0) {
1036a86c6181SAlex Tomas 		if (path[depth].p_idx !=
1037a86c6181SAlex Tomas 				EXT_LAST_INDEX(path[depth].p_hdr))
1038a86c6181SAlex Tomas 		  return le32_to_cpu(path[depth].p_idx[1].ei_block);
1039a86c6181SAlex Tomas 		depth--;
1040a86c6181SAlex Tomas 	}
1041a86c6181SAlex Tomas 
1042a86c6181SAlex Tomas 	return EXT_MAX_BLOCK;
1043a86c6181SAlex Tomas }
1044a86c6181SAlex Tomas 
1045a86c6181SAlex Tomas /*
1046*d0d856e8SRandy Dunlap  * ext4_ext_correct_indexes:
1047*d0d856e8SRandy Dunlap  * if leaf gets modified and modified extent is first in the leaf,
1048*d0d856e8SRandy Dunlap  * then we have to correct all indexes above.
1049a86c6181SAlex Tomas  * TODO: do we need to correct tree in all cases?
1050a86c6181SAlex Tomas  */
1051a86c6181SAlex Tomas int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
1052a86c6181SAlex Tomas 				struct ext4_ext_path *path)
1053a86c6181SAlex Tomas {
1054a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
1055a86c6181SAlex Tomas 	int depth = ext_depth(inode);
1056a86c6181SAlex Tomas 	struct ext4_extent *ex;
1057a86c6181SAlex Tomas 	__le32 border;
1058a86c6181SAlex Tomas 	int k, err = 0;
1059a86c6181SAlex Tomas 
1060a86c6181SAlex Tomas 	eh = path[depth].p_hdr;
1061a86c6181SAlex Tomas 	ex = path[depth].p_ext;
1062a86c6181SAlex Tomas 	BUG_ON(ex == NULL);
1063a86c6181SAlex Tomas 	BUG_ON(eh == NULL);
1064a86c6181SAlex Tomas 
1065a86c6181SAlex Tomas 	if (depth == 0) {
1066a86c6181SAlex Tomas 		/* there is no tree at all */
1067a86c6181SAlex Tomas 		return 0;
1068a86c6181SAlex Tomas 	}
1069a86c6181SAlex Tomas 
1070a86c6181SAlex Tomas 	if (ex != EXT_FIRST_EXTENT(eh)) {
1071a86c6181SAlex Tomas 		/* we correct tree if first leaf got modified only */
1072a86c6181SAlex Tomas 		return 0;
1073a86c6181SAlex Tomas 	}
1074a86c6181SAlex Tomas 
1075a86c6181SAlex Tomas 	/*
1076*d0d856e8SRandy Dunlap 	 * TODO: we need correction if border is smaller than current one
1077a86c6181SAlex Tomas 	 */
1078a86c6181SAlex Tomas 	k = depth - 1;
1079a86c6181SAlex Tomas 	border = path[depth].p_ext->ee_block;
1080a86c6181SAlex Tomas 	if ((err = ext4_ext_get_access(handle, inode, path + k)))
1081a86c6181SAlex Tomas 		return err;
1082a86c6181SAlex Tomas 	path[k].p_idx->ei_block = border;
1083a86c6181SAlex Tomas 	if ((err = ext4_ext_dirty(handle, inode, path + k)))
1084a86c6181SAlex Tomas 		return err;
1085a86c6181SAlex Tomas 
1086a86c6181SAlex Tomas 	while (k--) {
1087a86c6181SAlex Tomas 		/* change all left-side indexes */
1088a86c6181SAlex Tomas 		if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1089a86c6181SAlex Tomas 			break;
1090a86c6181SAlex Tomas 		if ((err = ext4_ext_get_access(handle, inode, path + k)))
1091a86c6181SAlex Tomas 			break;
1092a86c6181SAlex Tomas 		path[k].p_idx->ei_block = border;
1093a86c6181SAlex Tomas 		if ((err = ext4_ext_dirty(handle, inode, path + k)))
1094a86c6181SAlex Tomas 			break;
1095a86c6181SAlex Tomas 	}
1096a86c6181SAlex Tomas 
1097a86c6181SAlex Tomas 	return err;
1098a86c6181SAlex Tomas }
1099a86c6181SAlex Tomas 
1100a86c6181SAlex Tomas static int inline
1101a86c6181SAlex Tomas ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1102a86c6181SAlex Tomas 				struct ext4_extent *ex2)
1103a86c6181SAlex Tomas {
1104a86c6181SAlex Tomas         if (le32_to_cpu(ex1->ee_block) + le16_to_cpu(ex1->ee_len)
1105a86c6181SAlex Tomas 	    != le32_to_cpu(ex2->ee_block))
1106a86c6181SAlex Tomas 		return 0;
1107a86c6181SAlex Tomas 
1108471d4011SSuparna Bhattacharya 	/*
1109471d4011SSuparna Bhattacharya 	 * To allow future support for preallocated extents to be added
1110471d4011SSuparna Bhattacharya 	 * as an RO_COMPAT feature, refuse to merge to extents if
1111*d0d856e8SRandy Dunlap 	 * this can result in the top bit of ee_len being set.
1112471d4011SSuparna Bhattacharya 	 */
1113471d4011SSuparna Bhattacharya 	if (le16_to_cpu(ex1->ee_len) + le16_to_cpu(ex2->ee_len) > EXT_MAX_LEN)
1114471d4011SSuparna Bhattacharya 		return 0;
1115a86c6181SAlex Tomas #ifdef AGRESSIVE_TEST
1116a86c6181SAlex Tomas 	if (le16_to_cpu(ex1->ee_len) >= 4)
1117a86c6181SAlex Tomas 		return 0;
1118a86c6181SAlex Tomas #endif
1119a86c6181SAlex Tomas 
1120f65e6fbaSAlex Tomas         if (ext_pblock(ex1) + le16_to_cpu(ex1->ee_len) == ext_pblock(ex2))
1121a86c6181SAlex Tomas 		return 1;
1122a86c6181SAlex Tomas 	return 0;
1123a86c6181SAlex Tomas }
1124a86c6181SAlex Tomas 
1125a86c6181SAlex Tomas /*
1126*d0d856e8SRandy Dunlap  * ext4_ext_insert_extent:
1127*d0d856e8SRandy Dunlap  * tries to merge requsted extent into the existing extent or
1128*d0d856e8SRandy Dunlap  * inserts requested extent as new one into the tree,
1129*d0d856e8SRandy Dunlap  * creating new leaf in the no-space case.
1130a86c6181SAlex Tomas  */
1131a86c6181SAlex Tomas int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1132a86c6181SAlex Tomas 				struct ext4_ext_path *path,
1133a86c6181SAlex Tomas 				struct ext4_extent *newext)
1134a86c6181SAlex Tomas {
1135a86c6181SAlex Tomas 	struct ext4_extent_header * eh;
1136a86c6181SAlex Tomas 	struct ext4_extent *ex, *fex;
1137a86c6181SAlex Tomas 	struct ext4_extent *nearex; /* nearest extent */
1138a86c6181SAlex Tomas 	struct ext4_ext_path *npath = NULL;
1139a86c6181SAlex Tomas 	int depth, len, err, next;
1140a86c6181SAlex Tomas 
1141a86c6181SAlex Tomas 	BUG_ON(newext->ee_len == 0);
1142a86c6181SAlex Tomas 	depth = ext_depth(inode);
1143a86c6181SAlex Tomas 	ex = path[depth].p_ext;
1144a86c6181SAlex Tomas 	BUG_ON(path[depth].p_hdr == NULL);
1145a86c6181SAlex Tomas 
1146a86c6181SAlex Tomas 	/* try to insert block into found extent and return */
1147a86c6181SAlex Tomas 	if (ex && ext4_can_extents_be_merged(inode, ex, newext)) {
1148f65e6fbaSAlex Tomas 		ext_debug("append %d block to %d:%d (from "E3FSBLK")\n",
1149a86c6181SAlex Tomas 				le16_to_cpu(newext->ee_len),
1150a86c6181SAlex Tomas 				le32_to_cpu(ex->ee_block),
1151f65e6fbaSAlex Tomas 				le16_to_cpu(ex->ee_len), ext_pblock(ex));
1152a86c6181SAlex Tomas 		if ((err = ext4_ext_get_access(handle, inode, path + depth)))
1153a86c6181SAlex Tomas 			return err;
1154a86c6181SAlex Tomas 		ex->ee_len = cpu_to_le16(le16_to_cpu(ex->ee_len)
1155a86c6181SAlex Tomas 					 + le16_to_cpu(newext->ee_len));
1156a86c6181SAlex Tomas 		eh = path[depth].p_hdr;
1157a86c6181SAlex Tomas 		nearex = ex;
1158a86c6181SAlex Tomas 		goto merge;
1159a86c6181SAlex Tomas 	}
1160a86c6181SAlex Tomas 
1161a86c6181SAlex Tomas repeat:
1162a86c6181SAlex Tomas 	depth = ext_depth(inode);
1163a86c6181SAlex Tomas 	eh = path[depth].p_hdr;
1164a86c6181SAlex Tomas 	if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1165a86c6181SAlex Tomas 		goto has_space;
1166a86c6181SAlex Tomas 
1167a86c6181SAlex Tomas 	/* probably next leaf has space for us? */
1168a86c6181SAlex Tomas 	fex = EXT_LAST_EXTENT(eh);
1169a86c6181SAlex Tomas 	next = ext4_ext_next_leaf_block(inode, path);
1170a86c6181SAlex Tomas 	if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
1171a86c6181SAlex Tomas 	    && next != EXT_MAX_BLOCK) {
1172a86c6181SAlex Tomas 		ext_debug("next leaf block - %d\n", next);
1173a86c6181SAlex Tomas 		BUG_ON(npath != NULL);
1174a86c6181SAlex Tomas 		npath = ext4_ext_find_extent(inode, next, NULL);
1175a86c6181SAlex Tomas 		if (IS_ERR(npath))
1176a86c6181SAlex Tomas 			return PTR_ERR(npath);
1177a86c6181SAlex Tomas 		BUG_ON(npath->p_depth != path->p_depth);
1178a86c6181SAlex Tomas 		eh = npath[depth].p_hdr;
1179a86c6181SAlex Tomas 		if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
1180a86c6181SAlex Tomas 			ext_debug("next leaf isnt full(%d)\n",
1181a86c6181SAlex Tomas 				  le16_to_cpu(eh->eh_entries));
1182a86c6181SAlex Tomas 			path = npath;
1183a86c6181SAlex Tomas 			goto repeat;
1184a86c6181SAlex Tomas 		}
1185a86c6181SAlex Tomas 		ext_debug("next leaf has no free space(%d,%d)\n",
1186a86c6181SAlex Tomas 			  le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1187a86c6181SAlex Tomas 	}
1188a86c6181SAlex Tomas 
1189a86c6181SAlex Tomas 	/*
1190*d0d856e8SRandy Dunlap 	 * There is no free space in the found leaf.
1191*d0d856e8SRandy Dunlap 	 * We're gonna add a new leaf in the tree.
1192a86c6181SAlex Tomas 	 */
1193a86c6181SAlex Tomas 	err = ext4_ext_create_new_leaf(handle, inode, path, newext);
1194a86c6181SAlex Tomas 	if (err)
1195a86c6181SAlex Tomas 		goto cleanup;
1196a86c6181SAlex Tomas 	depth = ext_depth(inode);
1197a86c6181SAlex Tomas 	eh = path[depth].p_hdr;
1198a86c6181SAlex Tomas 
1199a86c6181SAlex Tomas has_space:
1200a86c6181SAlex Tomas 	nearex = path[depth].p_ext;
1201a86c6181SAlex Tomas 
1202a86c6181SAlex Tomas 	if ((err = ext4_ext_get_access(handle, inode, path + depth)))
1203a86c6181SAlex Tomas 		goto cleanup;
1204a86c6181SAlex Tomas 
1205a86c6181SAlex Tomas 	if (!nearex) {
1206a86c6181SAlex Tomas 		/* there is no extent in this leaf, create first one */
1207f65e6fbaSAlex Tomas 		ext_debug("first extent in the leaf: %d:"E3FSBLK":%d\n",
1208a86c6181SAlex Tomas 			        le32_to_cpu(newext->ee_block),
1209f65e6fbaSAlex Tomas 			        ext_pblock(newext),
1210a86c6181SAlex Tomas 			        le16_to_cpu(newext->ee_len));
1211a86c6181SAlex Tomas 		path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1212a86c6181SAlex Tomas 	} else if (le32_to_cpu(newext->ee_block)
1213a86c6181SAlex Tomas 		           > le32_to_cpu(nearex->ee_block)) {
1214a86c6181SAlex Tomas /*		BUG_ON(newext->ee_block == nearex->ee_block); */
1215a86c6181SAlex Tomas 		if (nearex != EXT_LAST_EXTENT(eh)) {
1216a86c6181SAlex Tomas 			len = EXT_MAX_EXTENT(eh) - nearex;
1217a86c6181SAlex Tomas 			len = (len - 1) * sizeof(struct ext4_extent);
1218a86c6181SAlex Tomas 			len = len < 0 ? 0 : len;
1219f65e6fbaSAlex Tomas 			ext_debug("insert %d:"E3FSBLK":%d after: nearest 0x%p, "
1220a86c6181SAlex Tomas 					"move %d from 0x%p to 0x%p\n",
1221a86c6181SAlex Tomas 				        le32_to_cpu(newext->ee_block),
1222f65e6fbaSAlex Tomas 				        ext_pblock(newext),
1223a86c6181SAlex Tomas 				        le16_to_cpu(newext->ee_len),
1224a86c6181SAlex Tomas 					nearex, len, nearex + 1, nearex + 2);
1225a86c6181SAlex Tomas 			memmove(nearex + 2, nearex + 1, len);
1226a86c6181SAlex Tomas 		}
1227a86c6181SAlex Tomas 		path[depth].p_ext = nearex + 1;
1228a86c6181SAlex Tomas 	} else {
1229a86c6181SAlex Tomas 		BUG_ON(newext->ee_block == nearex->ee_block);
1230a86c6181SAlex Tomas 		len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1231a86c6181SAlex Tomas 		len = len < 0 ? 0 : len;
1232f65e6fbaSAlex Tomas 		ext_debug("insert %d:"E3FSBLK":%d before: nearest 0x%p, "
1233a86c6181SAlex Tomas 				"move %d from 0x%p to 0x%p\n",
1234a86c6181SAlex Tomas 				le32_to_cpu(newext->ee_block),
1235f65e6fbaSAlex Tomas 				ext_pblock(newext),
1236a86c6181SAlex Tomas 				le16_to_cpu(newext->ee_len),
1237a86c6181SAlex Tomas 				nearex, len, nearex + 1, nearex + 2);
1238a86c6181SAlex Tomas 		memmove(nearex + 1, nearex, len);
1239a86c6181SAlex Tomas 		path[depth].p_ext = nearex;
1240a86c6181SAlex Tomas 	}
1241a86c6181SAlex Tomas 
1242a86c6181SAlex Tomas 	eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)+1);
1243a86c6181SAlex Tomas 	nearex = path[depth].p_ext;
1244a86c6181SAlex Tomas 	nearex->ee_block = newext->ee_block;
1245a86c6181SAlex Tomas 	nearex->ee_start = newext->ee_start;
1246f65e6fbaSAlex Tomas 	nearex->ee_start_hi = newext->ee_start_hi;
1247a86c6181SAlex Tomas 	nearex->ee_len = newext->ee_len;
1248a86c6181SAlex Tomas 
1249a86c6181SAlex Tomas merge:
1250a86c6181SAlex Tomas 	/* try to merge extents to the right */
1251a86c6181SAlex Tomas 	while (nearex < EXT_LAST_EXTENT(eh)) {
1252a86c6181SAlex Tomas 		if (!ext4_can_extents_be_merged(inode, nearex, nearex + 1))
1253a86c6181SAlex Tomas 			break;
1254a86c6181SAlex Tomas 		/* merge with next extent! */
1255a86c6181SAlex Tomas 		nearex->ee_len = cpu_to_le16(le16_to_cpu(nearex->ee_len)
1256a86c6181SAlex Tomas 					     + le16_to_cpu(nearex[1].ee_len));
1257a86c6181SAlex Tomas 		if (nearex + 1 < EXT_LAST_EXTENT(eh)) {
1258a86c6181SAlex Tomas 			len = (EXT_LAST_EXTENT(eh) - nearex - 1)
1259a86c6181SAlex Tomas 					* sizeof(struct ext4_extent);
1260a86c6181SAlex Tomas 			memmove(nearex + 1, nearex + 2, len);
1261a86c6181SAlex Tomas 		}
1262a86c6181SAlex Tomas 		eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)-1);
1263a86c6181SAlex Tomas 		BUG_ON(eh->eh_entries == 0);
1264a86c6181SAlex Tomas 	}
1265a86c6181SAlex Tomas 
1266a86c6181SAlex Tomas 	/* try to merge extents to the left */
1267a86c6181SAlex Tomas 
1268a86c6181SAlex Tomas 	/* time to correct all indexes above */
1269a86c6181SAlex Tomas 	err = ext4_ext_correct_indexes(handle, inode, path);
1270a86c6181SAlex Tomas 	if (err)
1271a86c6181SAlex Tomas 		goto cleanup;
1272a86c6181SAlex Tomas 
1273a86c6181SAlex Tomas 	err = ext4_ext_dirty(handle, inode, path + depth);
1274a86c6181SAlex Tomas 
1275a86c6181SAlex Tomas cleanup:
1276a86c6181SAlex Tomas 	if (npath) {
1277a86c6181SAlex Tomas 		ext4_ext_drop_refs(npath);
1278a86c6181SAlex Tomas 		kfree(npath);
1279a86c6181SAlex Tomas 	}
1280a86c6181SAlex Tomas 	ext4_ext_tree_changed(inode);
1281a86c6181SAlex Tomas 	ext4_ext_invalidate_cache(inode);
1282a86c6181SAlex Tomas 	return err;
1283a86c6181SAlex Tomas }
1284a86c6181SAlex Tomas 
1285a86c6181SAlex Tomas int ext4_ext_walk_space(struct inode *inode, unsigned long block,
1286a86c6181SAlex Tomas 			unsigned long num, ext_prepare_callback func,
1287a86c6181SAlex Tomas 			void *cbdata)
1288a86c6181SAlex Tomas {
1289a86c6181SAlex Tomas 	struct ext4_ext_path *path = NULL;
1290a86c6181SAlex Tomas 	struct ext4_ext_cache cbex;
1291a86c6181SAlex Tomas 	struct ext4_extent *ex;
1292a86c6181SAlex Tomas 	unsigned long next, start = 0, end = 0;
1293a86c6181SAlex Tomas 	unsigned long last = block + num;
1294a86c6181SAlex Tomas 	int depth, exists, err = 0;
1295a86c6181SAlex Tomas 
1296a86c6181SAlex Tomas 	BUG_ON(func == NULL);
1297a86c6181SAlex Tomas 	BUG_ON(inode == NULL);
1298a86c6181SAlex Tomas 
1299a86c6181SAlex Tomas 	while (block < last && block != EXT_MAX_BLOCK) {
1300a86c6181SAlex Tomas 		num = last - block;
1301a86c6181SAlex Tomas 		/* find extent for this block */
1302a86c6181SAlex Tomas 		path = ext4_ext_find_extent(inode, block, path);
1303a86c6181SAlex Tomas 		if (IS_ERR(path)) {
1304a86c6181SAlex Tomas 			err = PTR_ERR(path);
1305a86c6181SAlex Tomas 			path = NULL;
1306a86c6181SAlex Tomas 			break;
1307a86c6181SAlex Tomas 		}
1308a86c6181SAlex Tomas 
1309a86c6181SAlex Tomas 		depth = ext_depth(inode);
1310a86c6181SAlex Tomas 		BUG_ON(path[depth].p_hdr == NULL);
1311a86c6181SAlex Tomas 		ex = path[depth].p_ext;
1312a86c6181SAlex Tomas 		next = ext4_ext_next_allocated_block(path);
1313a86c6181SAlex Tomas 
1314a86c6181SAlex Tomas 		exists = 0;
1315a86c6181SAlex Tomas 		if (!ex) {
1316a86c6181SAlex Tomas 			/* there is no extent yet, so try to allocate
1317a86c6181SAlex Tomas 			 * all requested space */
1318a86c6181SAlex Tomas 			start = block;
1319a86c6181SAlex Tomas 			end = block + num;
1320a86c6181SAlex Tomas 		} else if (le32_to_cpu(ex->ee_block) > block) {
1321a86c6181SAlex Tomas 			/* need to allocate space before found extent */
1322a86c6181SAlex Tomas 			start = block;
1323a86c6181SAlex Tomas 			end = le32_to_cpu(ex->ee_block);
1324a86c6181SAlex Tomas 			if (block + num < end)
1325a86c6181SAlex Tomas 				end = block + num;
1326a86c6181SAlex Tomas 		} else if (block >=
1327a86c6181SAlex Tomas 			     le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len)) {
1328a86c6181SAlex Tomas 			/* need to allocate space after found extent */
1329a86c6181SAlex Tomas 			start = block;
1330a86c6181SAlex Tomas 			end = block + num;
1331a86c6181SAlex Tomas 			if (end >= next)
1332a86c6181SAlex Tomas 				end = next;
1333a86c6181SAlex Tomas 		} else if (block >= le32_to_cpu(ex->ee_block)) {
1334a86c6181SAlex Tomas 			/*
1335a86c6181SAlex Tomas 			 * some part of requested space is covered
1336a86c6181SAlex Tomas 			 * by found extent
1337a86c6181SAlex Tomas 			 */
1338a86c6181SAlex Tomas 			start = block;
1339a86c6181SAlex Tomas 			end = le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len);
1340a86c6181SAlex Tomas 			if (block + num < end)
1341a86c6181SAlex Tomas 				end = block + num;
1342a86c6181SAlex Tomas 			exists = 1;
1343a86c6181SAlex Tomas 		} else {
1344a86c6181SAlex Tomas 			BUG();
1345a86c6181SAlex Tomas 		}
1346a86c6181SAlex Tomas 		BUG_ON(end <= start);
1347a86c6181SAlex Tomas 
1348a86c6181SAlex Tomas 		if (!exists) {
1349a86c6181SAlex Tomas 			cbex.ec_block = start;
1350a86c6181SAlex Tomas 			cbex.ec_len = end - start;
1351a86c6181SAlex Tomas 			cbex.ec_start = 0;
1352a86c6181SAlex Tomas 			cbex.ec_type = EXT4_EXT_CACHE_GAP;
1353a86c6181SAlex Tomas 		} else {
1354a86c6181SAlex Tomas 		        cbex.ec_block = le32_to_cpu(ex->ee_block);
1355a86c6181SAlex Tomas 		        cbex.ec_len = le16_to_cpu(ex->ee_len);
1356f65e6fbaSAlex Tomas 		        cbex.ec_start = ext_pblock(ex);
1357a86c6181SAlex Tomas 			cbex.ec_type = EXT4_EXT_CACHE_EXTENT;
1358a86c6181SAlex Tomas 		}
1359a86c6181SAlex Tomas 
1360a86c6181SAlex Tomas 		BUG_ON(cbex.ec_len == 0);
1361a86c6181SAlex Tomas 		err = func(inode, path, &cbex, cbdata);
1362a86c6181SAlex Tomas 		ext4_ext_drop_refs(path);
1363a86c6181SAlex Tomas 
1364a86c6181SAlex Tomas 		if (err < 0)
1365a86c6181SAlex Tomas 			break;
1366a86c6181SAlex Tomas 		if (err == EXT_REPEAT)
1367a86c6181SAlex Tomas 			continue;
1368a86c6181SAlex Tomas 		else if (err == EXT_BREAK) {
1369a86c6181SAlex Tomas 			err = 0;
1370a86c6181SAlex Tomas 			break;
1371a86c6181SAlex Tomas 		}
1372a86c6181SAlex Tomas 
1373a86c6181SAlex Tomas 		if (ext_depth(inode) != depth) {
1374a86c6181SAlex Tomas 			/* depth was changed. we have to realloc path */
1375a86c6181SAlex Tomas 			kfree(path);
1376a86c6181SAlex Tomas 			path = NULL;
1377a86c6181SAlex Tomas 		}
1378a86c6181SAlex Tomas 
1379a86c6181SAlex Tomas 		block = cbex.ec_block + cbex.ec_len;
1380a86c6181SAlex Tomas 	}
1381a86c6181SAlex Tomas 
1382a86c6181SAlex Tomas 	if (path) {
1383a86c6181SAlex Tomas 		ext4_ext_drop_refs(path);
1384a86c6181SAlex Tomas 		kfree(path);
1385a86c6181SAlex Tomas 	}
1386a86c6181SAlex Tomas 
1387a86c6181SAlex Tomas 	return err;
1388a86c6181SAlex Tomas }
1389a86c6181SAlex Tomas 
1390a86c6181SAlex Tomas static inline void
1391a86c6181SAlex Tomas ext4_ext_put_in_cache(struct inode *inode, __u32 block,
1392a86c6181SAlex Tomas 			__u32 len, __u32 start, int type)
1393a86c6181SAlex Tomas {
1394a86c6181SAlex Tomas 	struct ext4_ext_cache *cex;
1395a86c6181SAlex Tomas 	BUG_ON(len == 0);
1396a86c6181SAlex Tomas 	cex = &EXT4_I(inode)->i_cached_extent;
1397a86c6181SAlex Tomas 	cex->ec_type = type;
1398a86c6181SAlex Tomas 	cex->ec_block = block;
1399a86c6181SAlex Tomas 	cex->ec_len = len;
1400a86c6181SAlex Tomas 	cex->ec_start = start;
1401a86c6181SAlex Tomas }
1402a86c6181SAlex Tomas 
1403a86c6181SAlex Tomas /*
1404*d0d856e8SRandy Dunlap  * ext4_ext_put_gap_in_cache:
1405*d0d856e8SRandy Dunlap  * calculate boundaries of the gap that the requested block fits into
1406a86c6181SAlex Tomas  * and cache this gap
1407a86c6181SAlex Tomas  */
1408a86c6181SAlex Tomas static inline void
1409a86c6181SAlex Tomas ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
1410a86c6181SAlex Tomas 				unsigned long block)
1411a86c6181SAlex Tomas {
1412a86c6181SAlex Tomas 	int depth = ext_depth(inode);
1413a86c6181SAlex Tomas 	unsigned long lblock, len;
1414a86c6181SAlex Tomas 	struct ext4_extent *ex;
1415a86c6181SAlex Tomas 
1416a86c6181SAlex Tomas 	ex = path[depth].p_ext;
1417a86c6181SAlex Tomas 	if (ex == NULL) {
1418a86c6181SAlex Tomas 		/* there is no extent yet, so gap is [0;-] */
1419a86c6181SAlex Tomas 		lblock = 0;
1420a86c6181SAlex Tomas 		len = EXT_MAX_BLOCK;
1421a86c6181SAlex Tomas 		ext_debug("cache gap(whole file):");
1422a86c6181SAlex Tomas 	} else if (block < le32_to_cpu(ex->ee_block)) {
1423a86c6181SAlex Tomas 		lblock = block;
1424a86c6181SAlex Tomas 		len = le32_to_cpu(ex->ee_block) - block;
1425a86c6181SAlex Tomas 		ext_debug("cache gap(before): %lu [%lu:%lu]",
1426a86c6181SAlex Tomas 				(unsigned long) block,
1427a86c6181SAlex Tomas 			        (unsigned long) le32_to_cpu(ex->ee_block),
1428a86c6181SAlex Tomas 			        (unsigned long) le16_to_cpu(ex->ee_len));
1429a86c6181SAlex Tomas 	} else if (block >= le32_to_cpu(ex->ee_block)
1430a86c6181SAlex Tomas 		            + le16_to_cpu(ex->ee_len)) {
1431a86c6181SAlex Tomas 	        lblock = le32_to_cpu(ex->ee_block)
1432a86c6181SAlex Tomas 		         + le16_to_cpu(ex->ee_len);
1433a86c6181SAlex Tomas 		len = ext4_ext_next_allocated_block(path);
1434a86c6181SAlex Tomas 		ext_debug("cache gap(after): [%lu:%lu] %lu",
1435a86c6181SAlex Tomas 			        (unsigned long) le32_to_cpu(ex->ee_block),
1436a86c6181SAlex Tomas 			        (unsigned long) le16_to_cpu(ex->ee_len),
1437a86c6181SAlex Tomas 				(unsigned long) block);
1438a86c6181SAlex Tomas 		BUG_ON(len == lblock);
1439a86c6181SAlex Tomas 		len = len - lblock;
1440a86c6181SAlex Tomas 	} else {
1441a86c6181SAlex Tomas 		lblock = len = 0;
1442a86c6181SAlex Tomas 		BUG();
1443a86c6181SAlex Tomas 	}
1444a86c6181SAlex Tomas 
1445a86c6181SAlex Tomas 	ext_debug(" -> %lu:%lu\n", (unsigned long) lblock, len);
1446a86c6181SAlex Tomas 	ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
1447a86c6181SAlex Tomas }
1448a86c6181SAlex Tomas 
1449a86c6181SAlex Tomas static inline int
1450a86c6181SAlex Tomas ext4_ext_in_cache(struct inode *inode, unsigned long block,
1451a86c6181SAlex Tomas 			struct ext4_extent *ex)
1452a86c6181SAlex Tomas {
1453a86c6181SAlex Tomas 	struct ext4_ext_cache *cex;
1454a86c6181SAlex Tomas 
1455a86c6181SAlex Tomas 	cex = &EXT4_I(inode)->i_cached_extent;
1456a86c6181SAlex Tomas 
1457a86c6181SAlex Tomas 	/* has cache valid data? */
1458a86c6181SAlex Tomas 	if (cex->ec_type == EXT4_EXT_CACHE_NO)
1459a86c6181SAlex Tomas 		return EXT4_EXT_CACHE_NO;
1460a86c6181SAlex Tomas 
1461a86c6181SAlex Tomas 	BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
1462a86c6181SAlex Tomas 			cex->ec_type != EXT4_EXT_CACHE_EXTENT);
1463a86c6181SAlex Tomas 	if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
1464a86c6181SAlex Tomas 	        ex->ee_block = cpu_to_le32(cex->ec_block);
1465f65e6fbaSAlex Tomas 		ext4_ext_store_pblock(ex, cex->ec_start);
1466a86c6181SAlex Tomas 	        ex->ee_len = cpu_to_le16(cex->ec_len);
1467f65e6fbaSAlex Tomas 		ext_debug("%lu cached by %lu:%lu:"E3FSBLK"\n",
1468a86c6181SAlex Tomas 				(unsigned long) block,
1469a86c6181SAlex Tomas 				(unsigned long) cex->ec_block,
1470a86c6181SAlex Tomas 				(unsigned long) cex->ec_len,
1471f65e6fbaSAlex Tomas 				cex->ec_start);
1472a86c6181SAlex Tomas 		return cex->ec_type;
1473a86c6181SAlex Tomas 	}
1474a86c6181SAlex Tomas 
1475a86c6181SAlex Tomas 	/* not in cache */
1476a86c6181SAlex Tomas 	return EXT4_EXT_CACHE_NO;
1477a86c6181SAlex Tomas }
1478a86c6181SAlex Tomas 
1479a86c6181SAlex Tomas /*
1480*d0d856e8SRandy Dunlap  * ext4_ext_rm_idx:
1481*d0d856e8SRandy Dunlap  * removes index from the index block.
1482*d0d856e8SRandy Dunlap  * It's used in truncate case only, thus all requests are for
1483*d0d856e8SRandy Dunlap  * last index in the block only.
1484a86c6181SAlex Tomas  */
1485a86c6181SAlex Tomas int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
1486a86c6181SAlex Tomas 			struct ext4_ext_path *path)
1487a86c6181SAlex Tomas {
1488a86c6181SAlex Tomas 	struct buffer_head *bh;
1489a86c6181SAlex Tomas 	int err;
1490f65e6fbaSAlex Tomas 	ext4_fsblk_t leaf;
1491a86c6181SAlex Tomas 
1492a86c6181SAlex Tomas 	/* free index block */
1493a86c6181SAlex Tomas 	path--;
1494f65e6fbaSAlex Tomas 	leaf = idx_pblock(path->p_idx);
1495a86c6181SAlex Tomas 	BUG_ON(path->p_hdr->eh_entries == 0);
1496a86c6181SAlex Tomas 	if ((err = ext4_ext_get_access(handle, inode, path)))
1497a86c6181SAlex Tomas 		return err;
1498a86c6181SAlex Tomas 	path->p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(path->p_hdr->eh_entries)-1);
1499a86c6181SAlex Tomas 	if ((err = ext4_ext_dirty(handle, inode, path)))
1500a86c6181SAlex Tomas 		return err;
1501f65e6fbaSAlex Tomas 	ext_debug("index is empty, remove it, free block "E3FSBLK"\n", leaf);
1502a86c6181SAlex Tomas 	bh = sb_find_get_block(inode->i_sb, leaf);
1503a86c6181SAlex Tomas 	ext4_forget(handle, 1, inode, bh, leaf);
1504a86c6181SAlex Tomas 	ext4_free_blocks(handle, inode, leaf, 1);
1505a86c6181SAlex Tomas 	return err;
1506a86c6181SAlex Tomas }
1507a86c6181SAlex Tomas 
1508a86c6181SAlex Tomas /*
1509*d0d856e8SRandy Dunlap  * ext4_ext_calc_credits_for_insert:
1510*d0d856e8SRandy Dunlap  * This routine returns max. credits that the extent tree can consume.
1511a86c6181SAlex Tomas  * It should be OK for low-performance paths like ->writepage()
1512*d0d856e8SRandy Dunlap  * To allow many writing processes to fit into a single transaction,
1513*d0d856e8SRandy Dunlap  * the caller should calculate credits under truncate_mutex and
1514*d0d856e8SRandy Dunlap  * pass the actual path.
1515a86c6181SAlex Tomas  */
1516a86c6181SAlex Tomas int inline ext4_ext_calc_credits_for_insert(struct inode *inode,
1517a86c6181SAlex Tomas 						struct ext4_ext_path *path)
1518a86c6181SAlex Tomas {
1519a86c6181SAlex Tomas 	int depth, needed;
1520a86c6181SAlex Tomas 
1521a86c6181SAlex Tomas 	if (path) {
1522a86c6181SAlex Tomas 		/* probably there is space in leaf? */
1523a86c6181SAlex Tomas 		depth = ext_depth(inode);
1524a86c6181SAlex Tomas 		if (le16_to_cpu(path[depth].p_hdr->eh_entries)
1525a86c6181SAlex Tomas 				< le16_to_cpu(path[depth].p_hdr->eh_max))
1526a86c6181SAlex Tomas 			return 1;
1527a86c6181SAlex Tomas 	}
1528a86c6181SAlex Tomas 
1529a86c6181SAlex Tomas 	/*
1530*d0d856e8SRandy Dunlap 	 * given 32-bit logical block (4294967296 blocks), max. tree
1531a86c6181SAlex Tomas 	 * can be 4 levels in depth -- 4 * 340^4 == 53453440000.
1532*d0d856e8SRandy Dunlap 	 * Let's also add one more level for imbalance.
1533a86c6181SAlex Tomas 	 */
1534a86c6181SAlex Tomas 	depth = 5;
1535a86c6181SAlex Tomas 
1536a86c6181SAlex Tomas 	/* allocation of new data block(s) */
1537a86c6181SAlex Tomas 	needed = 2;
1538a86c6181SAlex Tomas 
1539a86c6181SAlex Tomas 	/*
1540*d0d856e8SRandy Dunlap 	 * tree can be full, so it would need to grow in depth:
1541a86c6181SAlex Tomas 	 * allocation + old root + new root
1542a86c6181SAlex Tomas 	 */
1543a86c6181SAlex Tomas 	needed += 2 + 1 + 1;
1544a86c6181SAlex Tomas 
1545a86c6181SAlex Tomas 	/*
1546*d0d856e8SRandy Dunlap 	 * Index split can happen, we would need:
1547a86c6181SAlex Tomas 	 *    allocate intermediate indexes (bitmap + group)
1548a86c6181SAlex Tomas 	 *  + change two blocks at each level, but root (already included)
1549a86c6181SAlex Tomas 	 */
1550a86c6181SAlex Tomas 	needed = (depth * 2) + (depth * 2);
1551a86c6181SAlex Tomas 
1552a86c6181SAlex Tomas 	/* any allocation modifies superblock */
1553a86c6181SAlex Tomas 	needed += 1;
1554a86c6181SAlex Tomas 
1555a86c6181SAlex Tomas 	return needed;
1556a86c6181SAlex Tomas }
1557a86c6181SAlex Tomas 
1558a86c6181SAlex Tomas static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
1559a86c6181SAlex Tomas 				struct ext4_extent *ex,
1560a86c6181SAlex Tomas 				unsigned long from, unsigned long to)
1561a86c6181SAlex Tomas {
1562a86c6181SAlex Tomas 	struct buffer_head *bh;
1563a86c6181SAlex Tomas 	int i;
1564a86c6181SAlex Tomas 
1565a86c6181SAlex Tomas #ifdef EXTENTS_STATS
1566a86c6181SAlex Tomas 	{
1567a86c6181SAlex Tomas 		struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1568a86c6181SAlex Tomas 		unsigned short ee_len =  le16_to_cpu(ex->ee_len);
1569a86c6181SAlex Tomas 		spin_lock(&sbi->s_ext_stats_lock);
1570a86c6181SAlex Tomas 		sbi->s_ext_blocks += ee_len;
1571a86c6181SAlex Tomas 		sbi->s_ext_extents++;
1572a86c6181SAlex Tomas 		if (ee_len < sbi->s_ext_min)
1573a86c6181SAlex Tomas 			sbi->s_ext_min = ee_len;
1574a86c6181SAlex Tomas 		if (ee_len > sbi->s_ext_max)
1575a86c6181SAlex Tomas 			sbi->s_ext_max = ee_len;
1576a86c6181SAlex Tomas 		if (ext_depth(inode) > sbi->s_depth_max)
1577a86c6181SAlex Tomas 			sbi->s_depth_max = ext_depth(inode);
1578a86c6181SAlex Tomas 		spin_unlock(&sbi->s_ext_stats_lock);
1579a86c6181SAlex Tomas 	}
1580a86c6181SAlex Tomas #endif
1581a86c6181SAlex Tomas 	if (from >= le32_to_cpu(ex->ee_block)
1582a86c6181SAlex Tomas 	    && to == le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len) - 1) {
1583a86c6181SAlex Tomas 		/* tail removal */
1584f65e6fbaSAlex Tomas 		unsigned long num;
1585f65e6fbaSAlex Tomas 		ext4_fsblk_t start;
1586a86c6181SAlex Tomas 		num = le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len) - from;
1587f65e6fbaSAlex Tomas 		start = ext_pblock(ex) + le16_to_cpu(ex->ee_len) - num;
1588f65e6fbaSAlex Tomas 		ext_debug("free last %lu blocks starting "E3FSBLK"\n", num, start);
1589a86c6181SAlex Tomas 		for (i = 0; i < num; i++) {
1590a86c6181SAlex Tomas 			bh = sb_find_get_block(inode->i_sb, start + i);
1591a86c6181SAlex Tomas 			ext4_forget(handle, 0, inode, bh, start + i);
1592a86c6181SAlex Tomas 		}
1593a86c6181SAlex Tomas 		ext4_free_blocks(handle, inode, start, num);
1594a86c6181SAlex Tomas 	} else if (from == le32_to_cpu(ex->ee_block)
1595a86c6181SAlex Tomas 		   && to <= le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len) - 1) {
1596a86c6181SAlex Tomas 		printk("strange request: removal %lu-%lu from %u:%u\n",
1597a86c6181SAlex Tomas 		       from, to, le32_to_cpu(ex->ee_block), le16_to_cpu(ex->ee_len));
1598a86c6181SAlex Tomas 	} else {
1599a86c6181SAlex Tomas 		printk("strange request: removal(2) %lu-%lu from %u:%u\n",
1600a86c6181SAlex Tomas 		       from, to, le32_to_cpu(ex->ee_block), le16_to_cpu(ex->ee_len));
1601a86c6181SAlex Tomas 	}
1602a86c6181SAlex Tomas 	return 0;
1603a86c6181SAlex Tomas }
1604a86c6181SAlex Tomas 
1605a86c6181SAlex Tomas static int
1606a86c6181SAlex Tomas ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
1607a86c6181SAlex Tomas 		struct ext4_ext_path *path, unsigned long start)
1608a86c6181SAlex Tomas {
1609a86c6181SAlex Tomas 	int err = 0, correct_index = 0;
1610a86c6181SAlex Tomas 	int depth = ext_depth(inode), credits;
1611a86c6181SAlex Tomas 	struct ext4_extent_header *eh;
1612a86c6181SAlex Tomas 	unsigned a, b, block, num;
1613a86c6181SAlex Tomas 	unsigned long ex_ee_block;
1614a86c6181SAlex Tomas 	unsigned short ex_ee_len;
1615a86c6181SAlex Tomas 	struct ext4_extent *ex;
1616a86c6181SAlex Tomas 
1617a86c6181SAlex Tomas 	ext_debug("truncate since %lu in leaf\n", start);
1618a86c6181SAlex Tomas 	if (!path[depth].p_hdr)
1619a86c6181SAlex Tomas 		path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
1620a86c6181SAlex Tomas 	eh = path[depth].p_hdr;
1621a86c6181SAlex Tomas 	BUG_ON(eh == NULL);
1622a86c6181SAlex Tomas 	BUG_ON(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max));
1623a86c6181SAlex Tomas 	BUG_ON(eh->eh_magic != EXT4_EXT_MAGIC);
1624a86c6181SAlex Tomas 
1625a86c6181SAlex Tomas 	/* find where to start removing */
1626a86c6181SAlex Tomas 	ex = EXT_LAST_EXTENT(eh);
1627a86c6181SAlex Tomas 
1628a86c6181SAlex Tomas 	ex_ee_block = le32_to_cpu(ex->ee_block);
1629a86c6181SAlex Tomas 	ex_ee_len = le16_to_cpu(ex->ee_len);
1630a86c6181SAlex Tomas 
1631a86c6181SAlex Tomas 	while (ex >= EXT_FIRST_EXTENT(eh) &&
1632a86c6181SAlex Tomas 			ex_ee_block + ex_ee_len > start) {
1633a86c6181SAlex Tomas 		ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len);
1634a86c6181SAlex Tomas 		path[depth].p_ext = ex;
1635a86c6181SAlex Tomas 
1636a86c6181SAlex Tomas 		a = ex_ee_block > start ? ex_ee_block : start;
1637a86c6181SAlex Tomas 		b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
1638a86c6181SAlex Tomas 			ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
1639a86c6181SAlex Tomas 
1640a86c6181SAlex Tomas 		ext_debug("  border %u:%u\n", a, b);
1641a86c6181SAlex Tomas 
1642a86c6181SAlex Tomas 		if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
1643a86c6181SAlex Tomas 			block = 0;
1644a86c6181SAlex Tomas 			num = 0;
1645a86c6181SAlex Tomas 			BUG();
1646a86c6181SAlex Tomas 		} else if (a != ex_ee_block) {
1647a86c6181SAlex Tomas 			/* remove tail of the extent */
1648a86c6181SAlex Tomas 			block = ex_ee_block;
1649a86c6181SAlex Tomas 			num = a - block;
1650a86c6181SAlex Tomas 		} else if (b != ex_ee_block + ex_ee_len - 1) {
1651a86c6181SAlex Tomas 			/* remove head of the extent */
1652a86c6181SAlex Tomas 			block = a;
1653a86c6181SAlex Tomas 			num = b - a;
1654a86c6181SAlex Tomas 			/* there is no "make a hole" API yet */
1655a86c6181SAlex Tomas 			BUG();
1656a86c6181SAlex Tomas 		} else {
1657a86c6181SAlex Tomas 			/* remove whole extent: excellent! */
1658a86c6181SAlex Tomas 			block = ex_ee_block;
1659a86c6181SAlex Tomas 			num = 0;
1660a86c6181SAlex Tomas 			BUG_ON(a != ex_ee_block);
1661a86c6181SAlex Tomas 			BUG_ON(b != ex_ee_block + ex_ee_len - 1);
1662a86c6181SAlex Tomas 		}
1663a86c6181SAlex Tomas 
1664*d0d856e8SRandy Dunlap 		/* at present, extent can't cross block group: */
1665a86c6181SAlex Tomas 		/* leaf + bitmap + group desc + sb + inode */
1666a86c6181SAlex Tomas 		credits = 5;
1667a86c6181SAlex Tomas 		if (ex == EXT_FIRST_EXTENT(eh)) {
1668a86c6181SAlex Tomas 			correct_index = 1;
1669a86c6181SAlex Tomas 			credits += (ext_depth(inode)) + 1;
1670a86c6181SAlex Tomas 		}
1671a86c6181SAlex Tomas #ifdef CONFIG_QUOTA
1672a86c6181SAlex Tomas 		credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
1673a86c6181SAlex Tomas #endif
1674a86c6181SAlex Tomas 
1675a86c6181SAlex Tomas 		handle = ext4_ext_journal_restart(handle, credits);
1676a86c6181SAlex Tomas 		if (IS_ERR(handle)) {
1677a86c6181SAlex Tomas 			err = PTR_ERR(handle);
1678a86c6181SAlex Tomas 			goto out;
1679a86c6181SAlex Tomas 		}
1680a86c6181SAlex Tomas 
1681a86c6181SAlex Tomas 		err = ext4_ext_get_access(handle, inode, path + depth);
1682a86c6181SAlex Tomas 		if (err)
1683a86c6181SAlex Tomas 			goto out;
1684a86c6181SAlex Tomas 
1685a86c6181SAlex Tomas 		err = ext4_remove_blocks(handle, inode, ex, a, b);
1686a86c6181SAlex Tomas 		if (err)
1687a86c6181SAlex Tomas 			goto out;
1688a86c6181SAlex Tomas 
1689a86c6181SAlex Tomas 		if (num == 0) {
1690*d0d856e8SRandy Dunlap 			/* this extent is removed; mark slot entirely unused */
1691f65e6fbaSAlex Tomas 			ext4_ext_store_pblock(ex, 0);
1692a86c6181SAlex Tomas 			eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)-1);
1693a86c6181SAlex Tomas 		}
1694a86c6181SAlex Tomas 
1695a86c6181SAlex Tomas 		ex->ee_block = cpu_to_le32(block);
1696a86c6181SAlex Tomas 		ex->ee_len = cpu_to_le16(num);
1697a86c6181SAlex Tomas 
1698a86c6181SAlex Tomas 		err = ext4_ext_dirty(handle, inode, path + depth);
1699a86c6181SAlex Tomas 		if (err)
1700a86c6181SAlex Tomas 			goto out;
1701a86c6181SAlex Tomas 
1702f65e6fbaSAlex Tomas 		ext_debug("new extent: %u:%u:"E3FSBLK"\n", block, num,
1703f65e6fbaSAlex Tomas 				ext_pblock(ex));
1704a86c6181SAlex Tomas 		ex--;
1705a86c6181SAlex Tomas 		ex_ee_block = le32_to_cpu(ex->ee_block);
1706a86c6181SAlex Tomas 		ex_ee_len = le16_to_cpu(ex->ee_len);
1707a86c6181SAlex Tomas 	}
1708a86c6181SAlex Tomas 
1709a86c6181SAlex Tomas 	if (correct_index && eh->eh_entries)
1710a86c6181SAlex Tomas 		err = ext4_ext_correct_indexes(handle, inode, path);
1711a86c6181SAlex Tomas 
1712a86c6181SAlex Tomas 	/* if this leaf is free, then we should
1713a86c6181SAlex Tomas 	 * remove it from index block above */
1714a86c6181SAlex Tomas 	if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
1715a86c6181SAlex Tomas 		err = ext4_ext_rm_idx(handle, inode, path + depth);
1716a86c6181SAlex Tomas 
1717a86c6181SAlex Tomas out:
1718a86c6181SAlex Tomas 	return err;
1719a86c6181SAlex Tomas }
1720a86c6181SAlex Tomas 
1721a86c6181SAlex Tomas /*
1722*d0d856e8SRandy Dunlap  * ext4_ext_more_to_rm:
1723*d0d856e8SRandy Dunlap  * returns 1 if current index has to be freed (even partial)
1724a86c6181SAlex Tomas  */
1725a86c6181SAlex Tomas static int inline
1726a86c6181SAlex Tomas ext4_ext_more_to_rm(struct ext4_ext_path *path)
1727a86c6181SAlex Tomas {
1728a86c6181SAlex Tomas 	BUG_ON(path->p_idx == NULL);
1729a86c6181SAlex Tomas 
1730a86c6181SAlex Tomas 	if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
1731a86c6181SAlex Tomas 		return 0;
1732a86c6181SAlex Tomas 
1733a86c6181SAlex Tomas 	/*
1734*d0d856e8SRandy Dunlap 	 * if truncate on deeper level happened, it wasn't partial,
1735a86c6181SAlex Tomas 	 * so we have to consider current index for truncation
1736a86c6181SAlex Tomas 	 */
1737a86c6181SAlex Tomas 	if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
1738a86c6181SAlex Tomas 		return 0;
1739a86c6181SAlex Tomas 	return 1;
1740a86c6181SAlex Tomas }
1741a86c6181SAlex Tomas 
1742a86c6181SAlex Tomas int ext4_ext_remove_space(struct inode *inode, unsigned long start)
1743a86c6181SAlex Tomas {
1744a86c6181SAlex Tomas 	struct super_block *sb = inode->i_sb;
1745a86c6181SAlex Tomas 	int depth = ext_depth(inode);
1746a86c6181SAlex Tomas 	struct ext4_ext_path *path;
1747a86c6181SAlex Tomas 	handle_t *handle;
1748a86c6181SAlex Tomas 	int i = 0, err = 0;
1749a86c6181SAlex Tomas 
1750a86c6181SAlex Tomas 	ext_debug("truncate since %lu\n", start);
1751a86c6181SAlex Tomas 
1752a86c6181SAlex Tomas 	/* probably first extent we're gonna free will be last in block */
1753a86c6181SAlex Tomas 	handle = ext4_journal_start(inode, depth + 1);
1754a86c6181SAlex Tomas 	if (IS_ERR(handle))
1755a86c6181SAlex Tomas 		return PTR_ERR(handle);
1756a86c6181SAlex Tomas 
1757a86c6181SAlex Tomas 	ext4_ext_invalidate_cache(inode);
1758a86c6181SAlex Tomas 
1759a86c6181SAlex Tomas 	/*
1760*d0d856e8SRandy Dunlap 	 * We start scanning from right side, freeing all the blocks
1761*d0d856e8SRandy Dunlap 	 * after i_size and walking into the tree depth-wise.
1762a86c6181SAlex Tomas 	 */
1763a86c6181SAlex Tomas 	path = kmalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_KERNEL);
1764a86c6181SAlex Tomas 	if (path == NULL) {
1765a86c6181SAlex Tomas 		ext4_journal_stop(handle);
1766a86c6181SAlex Tomas 		return -ENOMEM;
1767a86c6181SAlex Tomas 	}
1768a86c6181SAlex Tomas 	memset(path, 0, sizeof(struct ext4_ext_path) * (depth + 1));
1769a86c6181SAlex Tomas 	path[0].p_hdr = ext_inode_hdr(inode);
1770a86c6181SAlex Tomas 	if (ext4_ext_check_header(__FUNCTION__, inode, path[0].p_hdr)) {
1771a86c6181SAlex Tomas 		err = -EIO;
1772a86c6181SAlex Tomas 		goto out;
1773a86c6181SAlex Tomas 	}
1774a86c6181SAlex Tomas 	path[0].p_depth = depth;
1775a86c6181SAlex Tomas 
1776a86c6181SAlex Tomas 	while (i >= 0 && err == 0) {
1777a86c6181SAlex Tomas 		if (i == depth) {
1778a86c6181SAlex Tomas 			/* this is leaf block */
1779a86c6181SAlex Tomas 			err = ext4_ext_rm_leaf(handle, inode, path, start);
1780*d0d856e8SRandy Dunlap 			/* root level has p_bh == NULL, brelse() eats this */
1781a86c6181SAlex Tomas 			brelse(path[i].p_bh);
1782a86c6181SAlex Tomas 			path[i].p_bh = NULL;
1783a86c6181SAlex Tomas 			i--;
1784a86c6181SAlex Tomas 			continue;
1785a86c6181SAlex Tomas 		}
1786a86c6181SAlex Tomas 
1787a86c6181SAlex Tomas 		/* this is index block */
1788a86c6181SAlex Tomas 		if (!path[i].p_hdr) {
1789a86c6181SAlex Tomas 			ext_debug("initialize header\n");
1790a86c6181SAlex Tomas 			path[i].p_hdr = ext_block_hdr(path[i].p_bh);
1791a86c6181SAlex Tomas 			if (ext4_ext_check_header(__FUNCTION__, inode,
1792a86c6181SAlex Tomas 							path[i].p_hdr)) {
1793a86c6181SAlex Tomas 				err = -EIO;
1794a86c6181SAlex Tomas 				goto out;
1795a86c6181SAlex Tomas 			}
1796a86c6181SAlex Tomas 		}
1797a86c6181SAlex Tomas 
1798a86c6181SAlex Tomas 		BUG_ON(le16_to_cpu(path[i].p_hdr->eh_entries)
1799a86c6181SAlex Tomas 			   > le16_to_cpu(path[i].p_hdr->eh_max));
1800a86c6181SAlex Tomas 		BUG_ON(path[i].p_hdr->eh_magic != EXT4_EXT_MAGIC);
1801a86c6181SAlex Tomas 
1802a86c6181SAlex Tomas 		if (!path[i].p_idx) {
1803*d0d856e8SRandy Dunlap 			/* this level hasn't been touched yet */
1804a86c6181SAlex Tomas 			path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
1805a86c6181SAlex Tomas 			path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
1806a86c6181SAlex Tomas 			ext_debug("init index ptr: hdr 0x%p, num %d\n",
1807a86c6181SAlex Tomas 				  path[i].p_hdr,
1808a86c6181SAlex Tomas 				  le16_to_cpu(path[i].p_hdr->eh_entries));
1809a86c6181SAlex Tomas 		} else {
1810*d0d856e8SRandy Dunlap 			/* we were already here, see at next index */
1811a86c6181SAlex Tomas 			path[i].p_idx--;
1812a86c6181SAlex Tomas 		}
1813a86c6181SAlex Tomas 
1814a86c6181SAlex Tomas 		ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
1815a86c6181SAlex Tomas 				i, EXT_FIRST_INDEX(path[i].p_hdr),
1816a86c6181SAlex Tomas 				path[i].p_idx);
1817a86c6181SAlex Tomas 		if (ext4_ext_more_to_rm(path + i)) {
1818a86c6181SAlex Tomas 			/* go to the next level */
1819f65e6fbaSAlex Tomas 			ext_debug("move to level %d (block "E3FSBLK")\n",
1820f65e6fbaSAlex Tomas 				  i + 1, idx_pblock(path[i].p_idx));
1821a86c6181SAlex Tomas 			memset(path + i + 1, 0, sizeof(*path));
1822a86c6181SAlex Tomas 			path[i+1].p_bh =
1823f65e6fbaSAlex Tomas 				sb_bread(sb, idx_pblock(path[i].p_idx));
1824a86c6181SAlex Tomas 			if (!path[i+1].p_bh) {
1825a86c6181SAlex Tomas 				/* should we reset i_size? */
1826a86c6181SAlex Tomas 				err = -EIO;
1827a86c6181SAlex Tomas 				break;
1828a86c6181SAlex Tomas 			}
1829a86c6181SAlex Tomas 
1830*d0d856e8SRandy Dunlap 			/* save actual number of indexes since this
1831*d0d856e8SRandy Dunlap 			 * number is changed at the next iteration */
1832a86c6181SAlex Tomas 			path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
1833a86c6181SAlex Tomas 			i++;
1834a86c6181SAlex Tomas 		} else {
1835*d0d856e8SRandy Dunlap 			/* we finished processing this index, go up */
1836a86c6181SAlex Tomas 			if (path[i].p_hdr->eh_entries == 0 && i > 0) {
1837*d0d856e8SRandy Dunlap 				/* index is empty, remove it;
1838a86c6181SAlex Tomas 				 * handle must be already prepared by the
1839a86c6181SAlex Tomas 				 * truncatei_leaf() */
1840a86c6181SAlex Tomas 				err = ext4_ext_rm_idx(handle, inode, path + i);
1841a86c6181SAlex Tomas 			}
1842*d0d856e8SRandy Dunlap 			/* root level has p_bh == NULL, brelse() eats this */
1843a86c6181SAlex Tomas 			brelse(path[i].p_bh);
1844a86c6181SAlex Tomas 			path[i].p_bh = NULL;
1845a86c6181SAlex Tomas 			i--;
1846a86c6181SAlex Tomas 			ext_debug("return to level %d\n", i);
1847a86c6181SAlex Tomas 		}
1848a86c6181SAlex Tomas 	}
1849a86c6181SAlex Tomas 
1850a86c6181SAlex Tomas 	/* TODO: flexible tree reduction should be here */
1851a86c6181SAlex Tomas 	if (path->p_hdr->eh_entries == 0) {
1852a86c6181SAlex Tomas 		/*
1853*d0d856e8SRandy Dunlap 		 * truncate to zero freed all the tree,
1854*d0d856e8SRandy Dunlap 		 * so we need to correct eh_depth
1855a86c6181SAlex Tomas 		 */
1856a86c6181SAlex Tomas 		err = ext4_ext_get_access(handle, inode, path);
1857a86c6181SAlex Tomas 		if (err == 0) {
1858a86c6181SAlex Tomas 			ext_inode_hdr(inode)->eh_depth = 0;
1859a86c6181SAlex Tomas 			ext_inode_hdr(inode)->eh_max =
1860a86c6181SAlex Tomas 				cpu_to_le16(ext4_ext_space_root(inode));
1861a86c6181SAlex Tomas 			err = ext4_ext_dirty(handle, inode, path);
1862a86c6181SAlex Tomas 		}
1863a86c6181SAlex Tomas 	}
1864a86c6181SAlex Tomas out:
1865a86c6181SAlex Tomas 	ext4_ext_tree_changed(inode);
1866a86c6181SAlex Tomas 	ext4_ext_drop_refs(path);
1867a86c6181SAlex Tomas 	kfree(path);
1868a86c6181SAlex Tomas 	ext4_journal_stop(handle);
1869a86c6181SAlex Tomas 
1870a86c6181SAlex Tomas 	return err;
1871a86c6181SAlex Tomas }
1872a86c6181SAlex Tomas 
1873a86c6181SAlex Tomas /*
1874a86c6181SAlex Tomas  * called at mount time
1875a86c6181SAlex Tomas  */
1876a86c6181SAlex Tomas void ext4_ext_init(struct super_block *sb)
1877a86c6181SAlex Tomas {
1878a86c6181SAlex Tomas 	/*
1879a86c6181SAlex Tomas 	 * possible initialization would be here
1880a86c6181SAlex Tomas 	 */
1881a86c6181SAlex Tomas 
1882a86c6181SAlex Tomas 	if (test_opt(sb, EXTENTS)) {
1883a86c6181SAlex Tomas 		printk("EXT4-fs: file extents enabled");
1884a86c6181SAlex Tomas #ifdef AGRESSIVE_TEST
1885a86c6181SAlex Tomas 		printk(", agressive tests");
1886a86c6181SAlex Tomas #endif
1887a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH
1888a86c6181SAlex Tomas 		printk(", check binsearch");
1889a86c6181SAlex Tomas #endif
1890a86c6181SAlex Tomas #ifdef EXTENTS_STATS
1891a86c6181SAlex Tomas 		printk(", stats");
1892a86c6181SAlex Tomas #endif
1893a86c6181SAlex Tomas 		printk("\n");
1894a86c6181SAlex Tomas #ifdef EXTENTS_STATS
1895a86c6181SAlex Tomas 		spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
1896a86c6181SAlex Tomas 		EXT4_SB(sb)->s_ext_min = 1 << 30;
1897a86c6181SAlex Tomas 		EXT4_SB(sb)->s_ext_max = 0;
1898a86c6181SAlex Tomas #endif
1899a86c6181SAlex Tomas 	}
1900a86c6181SAlex Tomas }
1901a86c6181SAlex Tomas 
1902a86c6181SAlex Tomas /*
1903a86c6181SAlex Tomas  * called at umount time
1904a86c6181SAlex Tomas  */
1905a86c6181SAlex Tomas void ext4_ext_release(struct super_block *sb)
1906a86c6181SAlex Tomas {
1907a86c6181SAlex Tomas 	if (!test_opt(sb, EXTENTS))
1908a86c6181SAlex Tomas 		return;
1909a86c6181SAlex Tomas 
1910a86c6181SAlex Tomas #ifdef EXTENTS_STATS
1911a86c6181SAlex Tomas 	if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
1912a86c6181SAlex Tomas 		struct ext4_sb_info *sbi = EXT4_SB(sb);
1913a86c6181SAlex Tomas 		printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
1914a86c6181SAlex Tomas 			sbi->s_ext_blocks, sbi->s_ext_extents,
1915a86c6181SAlex Tomas 			sbi->s_ext_blocks / sbi->s_ext_extents);
1916a86c6181SAlex Tomas 		printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
1917a86c6181SAlex Tomas 			sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
1918a86c6181SAlex Tomas 	}
1919a86c6181SAlex Tomas #endif
1920a86c6181SAlex Tomas }
1921a86c6181SAlex Tomas 
1922f65e6fbaSAlex Tomas int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
1923f65e6fbaSAlex Tomas 			ext4_fsblk_t iblock,
1924a86c6181SAlex Tomas 			unsigned long max_blocks, struct buffer_head *bh_result,
1925a86c6181SAlex Tomas 			int create, int extend_disksize)
1926a86c6181SAlex Tomas {
1927a86c6181SAlex Tomas 	struct ext4_ext_path *path = NULL;
1928a86c6181SAlex Tomas 	struct ext4_extent newex, *ex;
1929f65e6fbaSAlex Tomas 	ext4_fsblk_t goal, newblock;
1930f65e6fbaSAlex Tomas 	int err = 0, depth;
1931a86c6181SAlex Tomas 	unsigned long allocated = 0;
1932a86c6181SAlex Tomas 
1933a86c6181SAlex Tomas 	__clear_bit(BH_New, &bh_result->b_state);
1934a86c6181SAlex Tomas 	ext_debug("blocks %d/%lu requested for inode %u\n", (int) iblock,
1935a86c6181SAlex Tomas 			max_blocks, (unsigned) inode->i_ino);
1936a86c6181SAlex Tomas 	mutex_lock(&EXT4_I(inode)->truncate_mutex);
1937a86c6181SAlex Tomas 
1938a86c6181SAlex Tomas 	/* check in cache */
1939a86c6181SAlex Tomas 	if ((goal = ext4_ext_in_cache(inode, iblock, &newex))) {
1940a86c6181SAlex Tomas 		if (goal == EXT4_EXT_CACHE_GAP) {
1941a86c6181SAlex Tomas 			if (!create) {
1942a86c6181SAlex Tomas 				/* block isn't allocated yet and
1943*d0d856e8SRandy Dunlap 				 * user doesn't want to allocate it */
1944a86c6181SAlex Tomas 				goto out2;
1945a86c6181SAlex Tomas 			}
1946a86c6181SAlex Tomas 			/* we should allocate requested block */
1947a86c6181SAlex Tomas 		} else if (goal == EXT4_EXT_CACHE_EXTENT) {
1948a86c6181SAlex Tomas 			/* block is already allocated */
1949a86c6181SAlex Tomas 		        newblock = iblock
1950a86c6181SAlex Tomas 		                   - le32_to_cpu(newex.ee_block)
1951f65e6fbaSAlex Tomas 			           + ext_pblock(&newex);
1952*d0d856e8SRandy Dunlap 			/* number of remaining blocks in the extent */
1953a86c6181SAlex Tomas 			allocated = le16_to_cpu(newex.ee_len) -
1954a86c6181SAlex Tomas 					(iblock - le32_to_cpu(newex.ee_block));
1955a86c6181SAlex Tomas 			goto out;
1956a86c6181SAlex Tomas 		} else {
1957a86c6181SAlex Tomas 			BUG();
1958a86c6181SAlex Tomas 		}
1959a86c6181SAlex Tomas 	}
1960a86c6181SAlex Tomas 
1961a86c6181SAlex Tomas 	/* find extent for this block */
1962a86c6181SAlex Tomas 	path = ext4_ext_find_extent(inode, iblock, NULL);
1963a86c6181SAlex Tomas 	if (IS_ERR(path)) {
1964a86c6181SAlex Tomas 		err = PTR_ERR(path);
1965a86c6181SAlex Tomas 		path = NULL;
1966a86c6181SAlex Tomas 		goto out2;
1967a86c6181SAlex Tomas 	}
1968a86c6181SAlex Tomas 
1969a86c6181SAlex Tomas 	depth = ext_depth(inode);
1970a86c6181SAlex Tomas 
1971a86c6181SAlex Tomas 	/*
1972*d0d856e8SRandy Dunlap 	 * consistent leaf must not be empty;
1973*d0d856e8SRandy Dunlap 	 * this situation is possible, though, _during_ tree modification;
1974a86c6181SAlex Tomas 	 * this is why assert can't be put in ext4_ext_find_extent()
1975a86c6181SAlex Tomas 	 */
1976a86c6181SAlex Tomas 	BUG_ON(path[depth].p_ext == NULL && depth != 0);
1977a86c6181SAlex Tomas 
1978a86c6181SAlex Tomas 	if ((ex = path[depth].p_ext)) {
1979a86c6181SAlex Tomas 	        unsigned long ee_block = le32_to_cpu(ex->ee_block);
1980f65e6fbaSAlex Tomas 		ext4_fsblk_t ee_start = ext_pblock(ex);
1981a86c6181SAlex Tomas 		unsigned short ee_len  = le16_to_cpu(ex->ee_len);
1982471d4011SSuparna Bhattacharya 
1983471d4011SSuparna Bhattacharya 		/*
1984471d4011SSuparna Bhattacharya 		 * Allow future support for preallocated extents to be added
1985471d4011SSuparna Bhattacharya 		 * as an RO_COMPAT feature:
1986471d4011SSuparna Bhattacharya 		 * Uninitialized extents are treated as holes, except that
1987471d4011SSuparna Bhattacharya 		 * we avoid (fail) allocating new blocks during a write.
1988471d4011SSuparna Bhattacharya 		 */
1989471d4011SSuparna Bhattacharya 		if (ee_len > EXT_MAX_LEN)
1990471d4011SSuparna Bhattacharya 			goto out2;
1991*d0d856e8SRandy Dunlap 		/* if found extent covers block, simply return it */
1992a86c6181SAlex Tomas 	        if (iblock >= ee_block && iblock < ee_block + ee_len) {
1993a86c6181SAlex Tomas 			newblock = iblock - ee_block + ee_start;
1994*d0d856e8SRandy Dunlap 			/* number of remaining blocks in the extent */
1995a86c6181SAlex Tomas 			allocated = ee_len - (iblock - ee_block);
1996f65e6fbaSAlex Tomas 			ext_debug("%d fit into %lu:%d -> "E3FSBLK"\n", (int) iblock,
1997a86c6181SAlex Tomas 					ee_block, ee_len, newblock);
1998a86c6181SAlex Tomas 			ext4_ext_put_in_cache(inode, ee_block, ee_len,
1999a86c6181SAlex Tomas 						ee_start, EXT4_EXT_CACHE_EXTENT);
2000a86c6181SAlex Tomas 			goto out;
2001a86c6181SAlex Tomas 		}
2002a86c6181SAlex Tomas 	}
2003a86c6181SAlex Tomas 
2004a86c6181SAlex Tomas 	/*
2005*d0d856e8SRandy Dunlap 	 * requested block isn't allocated yet;
2006a86c6181SAlex Tomas 	 * we couldn't try to create block if create flag is zero
2007a86c6181SAlex Tomas 	 */
2008a86c6181SAlex Tomas 	if (!create) {
2009*d0d856e8SRandy Dunlap 		/* put just found gap into cache to speed up
2010*d0d856e8SRandy Dunlap 		 * subsequent requests */
2011a86c6181SAlex Tomas 		ext4_ext_put_gap_in_cache(inode, path, iblock);
2012a86c6181SAlex Tomas 		goto out2;
2013a86c6181SAlex Tomas 	}
2014a86c6181SAlex Tomas 	/*
2015a86c6181SAlex Tomas          * Okay, we need to do block allocation.  Lazily initialize the block
2016*d0d856e8SRandy Dunlap          * allocation info here if necessary.
2017a86c6181SAlex Tomas         */
2018a86c6181SAlex Tomas 	if (S_ISREG(inode->i_mode) && (!EXT4_I(inode)->i_block_alloc_info))
2019a86c6181SAlex Tomas 		ext4_init_block_alloc_info(inode);
2020a86c6181SAlex Tomas 
2021a86c6181SAlex Tomas 	/* allocate new block */
2022a86c6181SAlex Tomas 	goal = ext4_ext_find_goal(inode, path, iblock);
2023a86c6181SAlex Tomas 	allocated = max_blocks;
2024a86c6181SAlex Tomas 	newblock = ext4_new_blocks(handle, inode, goal, &allocated, &err);
2025a86c6181SAlex Tomas 	if (!newblock)
2026a86c6181SAlex Tomas 		goto out2;
2027f65e6fbaSAlex Tomas 	ext_debug("allocate new block: goal "E3FSBLK", found "E3FSBLK"/%lu\n",
2028a86c6181SAlex Tomas 			goal, newblock, allocated);
2029a86c6181SAlex Tomas 
2030a86c6181SAlex Tomas 	/* try to insert new extent into found leaf and return */
2031a86c6181SAlex Tomas 	newex.ee_block = cpu_to_le32(iblock);
2032f65e6fbaSAlex Tomas 	ext4_ext_store_pblock(&newex, newblock);
2033a86c6181SAlex Tomas 	newex.ee_len = cpu_to_le16(allocated);
2034a86c6181SAlex Tomas 	err = ext4_ext_insert_extent(handle, inode, path, &newex);
2035a86c6181SAlex Tomas 	if (err)
2036a86c6181SAlex Tomas 		goto out2;
2037a86c6181SAlex Tomas 
2038a86c6181SAlex Tomas 	if (extend_disksize && inode->i_size > EXT4_I(inode)->i_disksize)
2039a86c6181SAlex Tomas 		EXT4_I(inode)->i_disksize = inode->i_size;
2040a86c6181SAlex Tomas 
2041a86c6181SAlex Tomas 	/* previous routine could use block we allocated */
2042f65e6fbaSAlex Tomas 	newblock = ext_pblock(&newex);
2043a86c6181SAlex Tomas 	__set_bit(BH_New, &bh_result->b_state);
2044a86c6181SAlex Tomas 
2045a86c6181SAlex Tomas 	ext4_ext_put_in_cache(inode, iblock, allocated, newblock,
2046a86c6181SAlex Tomas 				EXT4_EXT_CACHE_EXTENT);
2047a86c6181SAlex Tomas out:
2048a86c6181SAlex Tomas 	if (allocated > max_blocks)
2049a86c6181SAlex Tomas 		allocated = max_blocks;
2050a86c6181SAlex Tomas 	ext4_ext_show_leaf(inode, path);
2051a86c6181SAlex Tomas 	__set_bit(BH_Mapped, &bh_result->b_state);
2052a86c6181SAlex Tomas 	bh_result->b_bdev = inode->i_sb->s_bdev;
2053a86c6181SAlex Tomas 	bh_result->b_blocknr = newblock;
2054a86c6181SAlex Tomas out2:
2055a86c6181SAlex Tomas 	if (path) {
2056a86c6181SAlex Tomas 		ext4_ext_drop_refs(path);
2057a86c6181SAlex Tomas 		kfree(path);
2058a86c6181SAlex Tomas 	}
2059a86c6181SAlex Tomas 	mutex_unlock(&EXT4_I(inode)->truncate_mutex);
2060a86c6181SAlex Tomas 
2061a86c6181SAlex Tomas 	return err ? err : allocated;
2062a86c6181SAlex Tomas }
2063a86c6181SAlex Tomas 
2064a86c6181SAlex Tomas void ext4_ext_truncate(struct inode * inode, struct page *page)
2065a86c6181SAlex Tomas {
2066a86c6181SAlex Tomas 	struct address_space *mapping = inode->i_mapping;
2067a86c6181SAlex Tomas 	struct super_block *sb = inode->i_sb;
2068a86c6181SAlex Tomas 	unsigned long last_block;
2069a86c6181SAlex Tomas 	handle_t *handle;
2070a86c6181SAlex Tomas 	int err = 0;
2071a86c6181SAlex Tomas 
2072a86c6181SAlex Tomas 	/*
2073a86c6181SAlex Tomas 	 * probably first extent we're gonna free will be last in block
2074a86c6181SAlex Tomas 	 */
2075a86c6181SAlex Tomas 	err = ext4_writepage_trans_blocks(inode) + 3;
2076a86c6181SAlex Tomas 	handle = ext4_journal_start(inode, err);
2077a86c6181SAlex Tomas 	if (IS_ERR(handle)) {
2078a86c6181SAlex Tomas 		if (page) {
2079a86c6181SAlex Tomas 			clear_highpage(page);
2080a86c6181SAlex Tomas 			flush_dcache_page(page);
2081a86c6181SAlex Tomas 			unlock_page(page);
2082a86c6181SAlex Tomas 			page_cache_release(page);
2083a86c6181SAlex Tomas 		}
2084a86c6181SAlex Tomas 		return;
2085a86c6181SAlex Tomas 	}
2086a86c6181SAlex Tomas 
2087a86c6181SAlex Tomas 	if (page)
2088a86c6181SAlex Tomas 		ext4_block_truncate_page(handle, page, mapping, inode->i_size);
2089a86c6181SAlex Tomas 
2090a86c6181SAlex Tomas 	mutex_lock(&EXT4_I(inode)->truncate_mutex);
2091a86c6181SAlex Tomas 	ext4_ext_invalidate_cache(inode);
2092a86c6181SAlex Tomas 
2093a86c6181SAlex Tomas 	/*
2094*d0d856e8SRandy Dunlap 	 * TODO: optimization is possible here.
2095*d0d856e8SRandy Dunlap 	 * Probably we need not scan at all,
2096*d0d856e8SRandy Dunlap 	 * because page truncation is enough.
2097a86c6181SAlex Tomas 	 */
2098a86c6181SAlex Tomas 	if (ext4_orphan_add(handle, inode))
2099a86c6181SAlex Tomas 		goto out_stop;
2100a86c6181SAlex Tomas 
2101a86c6181SAlex Tomas 	/* we have to know where to truncate from in crash case */
2102a86c6181SAlex Tomas 	EXT4_I(inode)->i_disksize = inode->i_size;
2103a86c6181SAlex Tomas 	ext4_mark_inode_dirty(handle, inode);
2104a86c6181SAlex Tomas 
2105a86c6181SAlex Tomas 	last_block = (inode->i_size + sb->s_blocksize - 1)
2106a86c6181SAlex Tomas 			>> EXT4_BLOCK_SIZE_BITS(sb);
2107a86c6181SAlex Tomas 	err = ext4_ext_remove_space(inode, last_block);
2108a86c6181SAlex Tomas 
2109a86c6181SAlex Tomas 	/* In a multi-transaction truncate, we only make the final
2110*d0d856e8SRandy Dunlap 	 * transaction synchronous. */
2111a86c6181SAlex Tomas 	if (IS_SYNC(inode))
2112a86c6181SAlex Tomas 		handle->h_sync = 1;
2113a86c6181SAlex Tomas 
2114a86c6181SAlex Tomas out_stop:
2115a86c6181SAlex Tomas 	/*
2116*d0d856e8SRandy Dunlap 	 * If this was a simple ftruncate() and the file will remain alive,
2117a86c6181SAlex Tomas 	 * then we need to clear up the orphan record which we created above.
2118a86c6181SAlex Tomas 	 * However, if this was a real unlink then we were called by
2119a86c6181SAlex Tomas 	 * ext4_delete_inode(), and we allow that function to clean up the
2120a86c6181SAlex Tomas 	 * orphan info for us.
2121a86c6181SAlex Tomas 	 */
2122a86c6181SAlex Tomas 	if (inode->i_nlink)
2123a86c6181SAlex Tomas 		ext4_orphan_del(handle, inode);
2124a86c6181SAlex Tomas 
2125a86c6181SAlex Tomas 	mutex_unlock(&EXT4_I(inode)->truncate_mutex);
2126a86c6181SAlex Tomas 	ext4_journal_stop(handle);
2127a86c6181SAlex Tomas }
2128a86c6181SAlex Tomas 
2129a86c6181SAlex Tomas /*
2130*d0d856e8SRandy Dunlap  * ext4_ext_writepage_trans_blocks:
2131*d0d856e8SRandy Dunlap  * calculate max number of blocks we could modify
2132a86c6181SAlex Tomas  * in order to allocate new block for an inode
2133a86c6181SAlex Tomas  */
2134a86c6181SAlex Tomas int ext4_ext_writepage_trans_blocks(struct inode *inode, int num)
2135a86c6181SAlex Tomas {
2136a86c6181SAlex Tomas 	int needed;
2137a86c6181SAlex Tomas 
2138a86c6181SAlex Tomas 	needed = ext4_ext_calc_credits_for_insert(inode, NULL);
2139a86c6181SAlex Tomas 
2140*d0d856e8SRandy Dunlap 	/* caller wants to allocate num blocks, but note it includes sb */
2141a86c6181SAlex Tomas 	needed = needed * num - (num - 1);
2142a86c6181SAlex Tomas 
2143a86c6181SAlex Tomas #ifdef CONFIG_QUOTA
2144a86c6181SAlex Tomas 	needed += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
2145a86c6181SAlex Tomas #endif
2146a86c6181SAlex Tomas 
2147a86c6181SAlex Tomas 	return needed;
2148a86c6181SAlex Tomas }
2149a86c6181SAlex Tomas 
2150a86c6181SAlex Tomas EXPORT_SYMBOL(ext4_mark_inode_dirty);
2151a86c6181SAlex Tomas EXPORT_SYMBOL(ext4_ext_invalidate_cache);
2152a86c6181SAlex Tomas EXPORT_SYMBOL(ext4_ext_insert_extent);
2153a86c6181SAlex Tomas EXPORT_SYMBOL(ext4_ext_walk_space);
2154a86c6181SAlex Tomas EXPORT_SYMBOL(ext4_ext_find_goal);
2155a86c6181SAlex Tomas EXPORT_SYMBOL(ext4_ext_calc_credits_for_insert);
2156a86c6181SAlex Tomas 
2157