blob: b722bce7d662b5931a777a3e1a09d5d3353309e5 [file] [log] [blame]
Alex Tomasa86c6182006-10-11 01:21:03 -07001/*
2 * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
3 * Written by Alex Tomas <alex@clusterfs.com>
4 *
5 * Architecture independence:
6 * Copyright (c) 2005, Bull S.A.
7 * Written by Pierre Peiffer <pierre.peiffer@bull.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public Licens
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
21 */
22
23/*
24 * Extents support for EXT4
25 *
26 * TODO:
27 * - ext4*_error() should be used in some situations
28 * - analyze all BUG()/BUG_ON(), use -EIO where appropriate
29 * - smart tree reduction
30 */
31
32#include <linux/module.h>
33#include <linux/fs.h>
34#include <linux/time.h>
Mingming Caocd02ff02007-10-16 18:38:25 -040035#include <linux/jbd2.h>
Alex Tomasa86c6182006-10-11 01:21:03 -070036#include <linux/highuid.h>
37#include <linux/pagemap.h>
38#include <linux/quotaops.h>
39#include <linux/string.h>
40#include <linux/slab.h>
Amit Aroraa2df2a62007-07-17 21:42:41 -040041#include <linux/falloc.h>
Alex Tomasa86c6182006-10-11 01:21:03 -070042#include <asm/uaccess.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040043#include "ext4_jbd2.h"
44#include "ext4_extents.h"
Alex Tomasa86c6182006-10-11 01:21:03 -070045
46
Randy Dunlapd0d856e2006-10-11 01:21:07 -070047/*
48 * ext_pblock:
49 * combine low and high parts of physical block number into ext4_fsblk_t
50 */
Avantika Mathur09b88252006-12-06 20:41:36 -080051static ext4_fsblk_t ext_pblock(struct ext4_extent *ex)
Alex Tomasf65e6fb2006-10-11 01:21:05 -070052{
53 ext4_fsblk_t block;
54
Aneesh Kumar K.Vb3776112007-10-16 18:38:25 -040055 block = le32_to_cpu(ex->ee_start_lo);
Mingming Cao9b8f1f02006-10-11 01:21:13 -070056 block |= ((ext4_fsblk_t) le16_to_cpu(ex->ee_start_hi) << 31) << 1;
Alex Tomasf65e6fb2006-10-11 01:21:05 -070057 return block;
58}
59
Randy Dunlapd0d856e2006-10-11 01:21:07 -070060/*
61 * idx_pblock:
62 * combine low and high parts of a leaf physical block number into ext4_fsblk_t
63 */
Aneesh Kumar K.Vc14c6fd2008-01-28 23:58:26 -050064ext4_fsblk_t idx_pblock(struct ext4_extent_idx *ix)
Alex Tomasf65e6fb2006-10-11 01:21:05 -070065{
66 ext4_fsblk_t block;
67
Aneesh Kumar K.Vd8dd0b42007-10-16 18:38:25 -040068 block = le32_to_cpu(ix->ei_leaf_lo);
Mingming Cao9b8f1f02006-10-11 01:21:13 -070069 block |= ((ext4_fsblk_t) le16_to_cpu(ix->ei_leaf_hi) << 31) << 1;
Alex Tomasf65e6fb2006-10-11 01:21:05 -070070 return block;
71}
72
Randy Dunlapd0d856e2006-10-11 01:21:07 -070073/*
74 * ext4_ext_store_pblock:
75 * stores a large physical block number into an extent struct,
76 * breaking it into parts
77 */
Aneesh Kumar K.Vc14c6fd2008-01-28 23:58:26 -050078void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb)
Alex Tomasf65e6fb2006-10-11 01:21:05 -070079{
Aneesh Kumar K.Vb3776112007-10-16 18:38:25 -040080 ex->ee_start_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff));
Mingming Cao9b8f1f02006-10-11 01:21:13 -070081 ex->ee_start_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
Alex Tomasf65e6fb2006-10-11 01:21:05 -070082}
83
Randy Dunlapd0d856e2006-10-11 01:21:07 -070084/*
85 * ext4_idx_store_pblock:
86 * stores a large physical block number into an index struct,
87 * breaking it into parts
88 */
Avantika Mathur09b88252006-12-06 20:41:36 -080089static void ext4_idx_store_pblock(struct ext4_extent_idx *ix, ext4_fsblk_t pb)
Alex Tomasf65e6fb2006-10-11 01:21:05 -070090{
Aneesh Kumar K.Vd8dd0b42007-10-16 18:38:25 -040091 ix->ei_leaf_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff));
Mingming Cao9b8f1f02006-10-11 01:21:13 -070092 ix->ei_leaf_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
Alex Tomasf65e6fb2006-10-11 01:21:05 -070093}
94
Shen Feng9102e4f2008-07-11 19:27:31 -040095static int ext4_ext_journal_restart(handle_t *handle, int needed)
Alex Tomasa86c6182006-10-11 01:21:03 -070096{
97 int err;
98
99 if (handle->h_buffer_credits > needed)
Shen Feng9102e4f2008-07-11 19:27:31 -0400100 return 0;
101 err = ext4_journal_extend(handle, needed);
102 if (err)
103 return err;
104 return ext4_journal_restart(handle, needed);
Alex Tomasa86c6182006-10-11 01:21:03 -0700105}
106
107/*
108 * could return:
109 * - EROFS
110 * - ENOMEM
111 */
112static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
113 struct ext4_ext_path *path)
114{
115 if (path->p_bh) {
116 /* path points to block */
117 return ext4_journal_get_write_access(handle, path->p_bh);
118 }
119 /* path points to leaf/index in inode body */
120 /* we use in-core data, no need to protect them */
121 return 0;
122}
123
124/*
125 * could return:
126 * - EROFS
127 * - ENOMEM
128 * - EIO
129 */
130static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
131 struct ext4_ext_path *path)
132{
133 int err;
134 if (path->p_bh) {
135 /* path points to block */
136 err = ext4_journal_dirty_metadata(handle, path->p_bh);
137 } else {
138 /* path points to leaf/index in inode body */
139 err = ext4_mark_inode_dirty(handle, inode);
140 }
141 return err;
142}
143
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700144static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700145 struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500146 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700147{
148 struct ext4_inode_info *ei = EXT4_I(inode);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700149 ext4_fsblk_t bg_start;
Valerie Clement74d34872008-02-15 13:43:07 -0500150 ext4_fsblk_t last_block;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700151 ext4_grpblk_t colour;
Alex Tomasa86c6182006-10-11 01:21:03 -0700152 int depth;
153
154 if (path) {
155 struct ext4_extent *ex;
156 depth = path->p_depth;
157
158 /* try to predict block placement */
Avantika Mathur7e028972006-12-06 20:41:33 -0800159 ex = path[depth].p_ext;
160 if (ex)
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700161 return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700162
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700163 /* it looks like index is empty;
164 * try to find starting block from index itself */
Alex Tomasa86c6182006-10-11 01:21:03 -0700165 if (path[depth].p_bh)
166 return path[depth].p_bh->b_blocknr;
167 }
168
169 /* OK. use inode's group */
170 bg_start = (ei->i_block_group * EXT4_BLOCKS_PER_GROUP(inode->i_sb)) +
171 le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_first_data_block);
Valerie Clement74d34872008-02-15 13:43:07 -0500172 last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
173
174 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
175 colour = (current->pid % 16) *
Alex Tomasa86c6182006-10-11 01:21:03 -0700176 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
Valerie Clement74d34872008-02-15 13:43:07 -0500177 else
178 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
Alex Tomasa86c6182006-10-11 01:21:03 -0700179 return bg_start + colour + block;
180}
181
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400182/*
183 * Allocation for a meta data block
184 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700185static ext4_fsblk_t
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400186ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700187 struct ext4_ext_path *path,
188 struct ext4_extent *ex, int *err)
189{
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700190 ext4_fsblk_t goal, newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700191
192 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400193 newblock = ext4_new_meta_block(handle, inode, goal, err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700194 return newblock;
195}
196
Avantika Mathur09b88252006-12-06 20:41:36 -0800197static int ext4_ext_space_block(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -0700198{
199 int size;
200
201 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
202 / sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100203#ifdef AGGRESSIVE_TEST
Alex Tomasa86c6182006-10-11 01:21:03 -0700204 if (size > 6)
205 size = 6;
206#endif
207 return size;
208}
209
Avantika Mathur09b88252006-12-06 20:41:36 -0800210static int ext4_ext_space_block_idx(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -0700211{
212 int size;
213
214 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
215 / sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100216#ifdef AGGRESSIVE_TEST
Alex Tomasa86c6182006-10-11 01:21:03 -0700217 if (size > 5)
218 size = 5;
219#endif
220 return size;
221}
222
Avantika Mathur09b88252006-12-06 20:41:36 -0800223static int ext4_ext_space_root(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -0700224{
225 int size;
226
227 size = sizeof(EXT4_I(inode)->i_data);
228 size -= sizeof(struct ext4_extent_header);
229 size /= sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100230#ifdef AGGRESSIVE_TEST
Alex Tomasa86c6182006-10-11 01:21:03 -0700231 if (size > 3)
232 size = 3;
233#endif
234 return size;
235}
236
Avantika Mathur09b88252006-12-06 20:41:36 -0800237static int ext4_ext_space_root_idx(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -0700238{
239 int size;
240
241 size = sizeof(EXT4_I(inode)->i_data);
242 size -= sizeof(struct ext4_extent_header);
243 size /= sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100244#ifdef AGGRESSIVE_TEST
Alex Tomasa86c6182006-10-11 01:21:03 -0700245 if (size > 4)
246 size = 4;
247#endif
248 return size;
249}
250
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400251static int
252ext4_ext_max_entries(struct inode *inode, int depth)
253{
254 int max;
255
256 if (depth == ext_depth(inode)) {
257 if (depth == 0)
258 max = ext4_ext_space_root(inode);
259 else
260 max = ext4_ext_space_root_idx(inode);
261 } else {
262 if (depth == 0)
263 max = ext4_ext_space_block(inode);
264 else
265 max = ext4_ext_space_block_idx(inode);
266 }
267
268 return max;
269}
270
271static int __ext4_ext_check_header(const char *function, struct inode *inode,
272 struct ext4_extent_header *eh,
273 int depth)
274{
275 const char *error_msg;
276 int max = 0;
277
278 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
279 error_msg = "invalid magic";
280 goto corrupted;
281 }
282 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
283 error_msg = "unexpected eh_depth";
284 goto corrupted;
285 }
286 if (unlikely(eh->eh_max == 0)) {
287 error_msg = "invalid eh_max";
288 goto corrupted;
289 }
290 max = ext4_ext_max_entries(inode, depth);
291 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
292 error_msg = "too large eh_max";
293 goto corrupted;
294 }
295 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
296 error_msg = "invalid eh_entries";
297 goto corrupted;
298 }
299 return 0;
300
301corrupted:
302 ext4_error(inode->i_sb, function,
303 "bad header in inode #%lu: %s - magic %x, "
304 "entries %u, max %u(%u), depth %u(%u)",
305 inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic),
306 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
307 max, le16_to_cpu(eh->eh_depth), depth);
308
309 return -EIO;
310}
311
312#define ext4_ext_check_header(inode, eh, depth) \
Harvey Harrison46e665e2008-04-17 10:38:59 -0400313 __ext4_ext_check_header(__func__, inode, eh, depth)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400314
Alex Tomasa86c6182006-10-11 01:21:03 -0700315#ifdef EXT_DEBUG
316static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
317{
318 int k, l = path->p_depth;
319
320 ext_debug("path:");
321 for (k = 0; k <= l; k++, path++) {
322 if (path->p_idx) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700323 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700324 idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700325 } else if (path->p_ext) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700326 ext_debug(" %d:%d:%llu ",
Alex Tomasa86c6182006-10-11 01:21:03 -0700327 le32_to_cpu(path->p_ext->ee_block),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400328 ext4_ext_get_actual_len(path->p_ext),
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700329 ext_pblock(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700330 } else
331 ext_debug(" []");
332 }
333 ext_debug("\n");
334}
335
336static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
337{
338 int depth = ext_depth(inode);
339 struct ext4_extent_header *eh;
340 struct ext4_extent *ex;
341 int i;
342
343 if (!path)
344 return;
345
346 eh = path[depth].p_hdr;
347 ex = EXT_FIRST_EXTENT(eh);
348
349 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700350 ext_debug("%d:%d:%llu ", le32_to_cpu(ex->ee_block),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400351 ext4_ext_get_actual_len(ex), ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -0700352 }
353 ext_debug("\n");
354}
355#else
356#define ext4_ext_show_path(inode,path)
357#define ext4_ext_show_leaf(inode,path)
358#endif
359
Aneesh Kumar K.Vb35905c2008-02-25 16:54:37 -0500360void ext4_ext_drop_refs(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700361{
362 int depth = path->p_depth;
363 int i;
364
365 for (i = 0; i <= depth; i++, path++)
366 if (path->p_bh) {
367 brelse(path->p_bh);
368 path->p_bh = NULL;
369 }
370}
371
372/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700373 * ext4_ext_binsearch_idx:
374 * binary search for the closest index of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400375 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700376 */
377static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500378ext4_ext_binsearch_idx(struct inode *inode,
379 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700380{
381 struct ext4_extent_header *eh = path->p_hdr;
382 struct ext4_extent_idx *r, *l, *m;
383
Alex Tomasa86c6182006-10-11 01:21:03 -0700384
Eric Sandeenbba90742008-01-28 23:58:27 -0500385 ext_debug("binsearch for %u(idx): ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700386
387 l = EXT_FIRST_INDEX(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400388 r = EXT_LAST_INDEX(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700389 while (l <= r) {
390 m = l + (r - l) / 2;
391 if (block < le32_to_cpu(m->ei_block))
392 r = m - 1;
393 else
394 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400395 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
396 m, le32_to_cpu(m->ei_block),
397 r, le32_to_cpu(r->ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700398 }
399
400 path->p_idx = l - 1;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700401 ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400402 idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700403
404#ifdef CHECK_BINSEARCH
405 {
406 struct ext4_extent_idx *chix, *ix;
407 int k;
408
409 chix = ix = EXT_FIRST_INDEX(eh);
410 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
411 if (k != 0 &&
412 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
413 printk("k=%d, ix=0x%p, first=0x%p\n", k,
414 ix, EXT_FIRST_INDEX(eh));
415 printk("%u <= %u\n",
416 le32_to_cpu(ix->ei_block),
417 le32_to_cpu(ix[-1].ei_block));
418 }
419 BUG_ON(k && le32_to_cpu(ix->ei_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400420 <= le32_to_cpu(ix[-1].ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700421 if (block < le32_to_cpu(ix->ei_block))
422 break;
423 chix = ix;
424 }
425 BUG_ON(chix != path->p_idx);
426 }
427#endif
428
429}
430
431/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700432 * ext4_ext_binsearch:
433 * binary search for closest extent of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400434 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700435 */
436static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500437ext4_ext_binsearch(struct inode *inode,
438 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700439{
440 struct ext4_extent_header *eh = path->p_hdr;
441 struct ext4_extent *r, *l, *m;
442
Alex Tomasa86c6182006-10-11 01:21:03 -0700443 if (eh->eh_entries == 0) {
444 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700445 * this leaf is empty:
446 * we get such a leaf in split/add case
Alex Tomasa86c6182006-10-11 01:21:03 -0700447 */
448 return;
449 }
450
Eric Sandeenbba90742008-01-28 23:58:27 -0500451 ext_debug("binsearch for %u: ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700452
453 l = EXT_FIRST_EXTENT(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400454 r = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700455
456 while (l <= r) {
457 m = l + (r - l) / 2;
458 if (block < le32_to_cpu(m->ee_block))
459 r = m - 1;
460 else
461 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400462 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
463 m, le32_to_cpu(m->ee_block),
464 r, le32_to_cpu(r->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700465 }
466
467 path->p_ext = l - 1;
Mingming Cao2ae02102006-10-11 01:21:11 -0700468 ext_debug(" -> %d:%llu:%d ",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400469 le32_to_cpu(path->p_ext->ee_block),
470 ext_pblock(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400471 ext4_ext_get_actual_len(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700472
473#ifdef CHECK_BINSEARCH
474 {
475 struct ext4_extent *chex, *ex;
476 int k;
477
478 chex = ex = EXT_FIRST_EXTENT(eh);
479 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
480 BUG_ON(k && le32_to_cpu(ex->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400481 <= le32_to_cpu(ex[-1].ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700482 if (block < le32_to_cpu(ex->ee_block))
483 break;
484 chex = ex;
485 }
486 BUG_ON(chex != path->p_ext);
487 }
488#endif
489
490}
491
492int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
493{
494 struct ext4_extent_header *eh;
495
496 eh = ext_inode_hdr(inode);
497 eh->eh_depth = 0;
498 eh->eh_entries = 0;
499 eh->eh_magic = EXT4_EXT_MAGIC;
500 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode));
501 ext4_mark_inode_dirty(handle, inode);
502 ext4_ext_invalidate_cache(inode);
503 return 0;
504}
505
506struct ext4_ext_path *
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500507ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
508 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700509{
510 struct ext4_extent_header *eh;
511 struct buffer_head *bh;
512 short int depth, i, ppos = 0, alloc = 0;
513
514 eh = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400515 depth = ext_depth(inode);
516 if (ext4_ext_check_header(inode, eh, depth))
Alex Tomasa86c6182006-10-11 01:21:03 -0700517 return ERR_PTR(-EIO);
518
Alex Tomasa86c6182006-10-11 01:21:03 -0700519
520 /* account possible depth increase */
521 if (!path) {
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800522 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
Alex Tomasa86c6182006-10-11 01:21:03 -0700523 GFP_NOFS);
524 if (!path)
525 return ERR_PTR(-ENOMEM);
526 alloc = 1;
527 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700528 path[0].p_hdr = eh;
Shen Feng1973adc2008-07-11 19:27:31 -0400529 path[0].p_bh = NULL;
Alex Tomasa86c6182006-10-11 01:21:03 -0700530
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400531 i = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700532 /* walk through the tree */
533 while (i) {
534 ext_debug("depth %d: num %d, max %d\n",
535 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400536
Alex Tomasa86c6182006-10-11 01:21:03 -0700537 ext4_ext_binsearch_idx(inode, path + ppos, block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700538 path[ppos].p_block = idx_pblock(path[ppos].p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -0700539 path[ppos].p_depth = i;
540 path[ppos].p_ext = NULL;
541
542 bh = sb_bread(inode->i_sb, path[ppos].p_block);
543 if (!bh)
544 goto err;
545
546 eh = ext_block_hdr(bh);
547 ppos++;
548 BUG_ON(ppos > depth);
549 path[ppos].p_bh = bh;
550 path[ppos].p_hdr = eh;
551 i--;
552
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400553 if (ext4_ext_check_header(inode, eh, i))
Alex Tomasa86c6182006-10-11 01:21:03 -0700554 goto err;
555 }
556
557 path[ppos].p_depth = i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700558 path[ppos].p_ext = NULL;
559 path[ppos].p_idx = NULL;
560
Alex Tomasa86c6182006-10-11 01:21:03 -0700561 /* find extent */
562 ext4_ext_binsearch(inode, path + ppos, block);
Shen Feng1973adc2008-07-11 19:27:31 -0400563 /* if not an empty leaf */
564 if (path[ppos].p_ext)
565 path[ppos].p_block = ext_pblock(path[ppos].p_ext);
Alex Tomasa86c6182006-10-11 01:21:03 -0700566
567 ext4_ext_show_path(inode, path);
568
569 return path;
570
571err:
572 ext4_ext_drop_refs(path);
573 if (alloc)
574 kfree(path);
575 return ERR_PTR(-EIO);
576}
577
578/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700579 * ext4_ext_insert_index:
580 * insert new index [@logical;@ptr] into the block at @curp;
581 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700582 */
583static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
584 struct ext4_ext_path *curp,
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700585 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700586{
587 struct ext4_extent_idx *ix;
588 int len, err;
589
Avantika Mathur7e028972006-12-06 20:41:33 -0800590 err = ext4_ext_get_access(handle, inode, curp);
591 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700592 return err;
593
594 BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block));
595 len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
596 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
597 /* insert after */
598 if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
599 len = (len - 1) * sizeof(struct ext4_extent_idx);
600 len = len < 0 ? 0 : len;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400601 ext_debug("insert new index %d after: %llu. "
Alex Tomasa86c6182006-10-11 01:21:03 -0700602 "move %d from 0x%p to 0x%p\n",
603 logical, ptr, len,
604 (curp->p_idx + 1), (curp->p_idx + 2));
605 memmove(curp->p_idx + 2, curp->p_idx + 1, len);
606 }
607 ix = curp->p_idx + 1;
608 } else {
609 /* insert before */
610 len = len * sizeof(struct ext4_extent_idx);
611 len = len < 0 ? 0 : len;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400612 ext_debug("insert new index %d before: %llu. "
Alex Tomasa86c6182006-10-11 01:21:03 -0700613 "move %d from 0x%p to 0x%p\n",
614 logical, ptr, len,
615 curp->p_idx, (curp->p_idx + 1));
616 memmove(curp->p_idx + 1, curp->p_idx, len);
617 ix = curp->p_idx;
618 }
619
620 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700621 ext4_idx_store_pblock(ix, ptr);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400622 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -0700623
624 BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400625 > le16_to_cpu(curp->p_hdr->eh_max));
Alex Tomasa86c6182006-10-11 01:21:03 -0700626 BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr));
627
628 err = ext4_ext_dirty(handle, inode, curp);
629 ext4_std_error(inode->i_sb, err);
630
631 return err;
632}
633
634/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700635 * ext4_ext_split:
636 * inserts new subtree into the path, using free index entry
637 * at depth @at:
638 * - allocates all needed blocks (new leaf and all intermediate index blocks)
639 * - makes decision where to split
640 * - moves remaining extents and index entries (right to the split point)
641 * into the newly allocated blocks
642 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -0700643 */
644static int ext4_ext_split(handle_t *handle, struct inode *inode,
645 struct ext4_ext_path *path,
646 struct ext4_extent *newext, int at)
647{
648 struct buffer_head *bh = NULL;
649 int depth = ext_depth(inode);
650 struct ext4_extent_header *neh;
651 struct ext4_extent_idx *fidx;
652 struct ext4_extent *ex;
653 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700654 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700655 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700656 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Alex Tomasa86c6182006-10-11 01:21:03 -0700657 int err = 0;
658
659 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700660 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -0700661
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700662 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -0700663 * border from split point */
664 BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr));
665 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
666 border = path[depth].p_ext[1].ee_block;
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700667 ext_debug("leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -0700668 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400669 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700670 } else {
671 border = newext->ee_block;
672 ext_debug("leaf will be added."
673 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400674 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700675 }
676
677 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700678 * If error occurs, then we break processing
679 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -0700680 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700681 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -0700682 */
683
684 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700685 * Get array to track all allocated blocks.
686 * We need this to handle errors and free blocks
687 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -0700688 */
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800689 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -0700690 if (!ablocks)
691 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -0700692
693 /* allocate all needed blocks */
694 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
695 for (a = 0; a < depth - at; a++) {
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400696 newblock = ext4_ext_new_meta_block(handle, inode, path,
697 newext, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700698 if (newblock == 0)
699 goto cleanup;
700 ablocks[a] = newblock;
701 }
702
703 /* initialize new leaf */
704 newblock = ablocks[--a];
705 BUG_ON(newblock == 0);
706 bh = sb_getblk(inode->i_sb, newblock);
707 if (!bh) {
708 err = -EIO;
709 goto cleanup;
710 }
711 lock_buffer(bh);
712
Avantika Mathur7e028972006-12-06 20:41:33 -0800713 err = ext4_journal_get_create_access(handle, bh);
714 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700715 goto cleanup;
716
717 neh = ext_block_hdr(bh);
718 neh->eh_entries = 0;
719 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
720 neh->eh_magic = EXT4_EXT_MAGIC;
721 neh->eh_depth = 0;
722 ex = EXT_FIRST_EXTENT(neh);
723
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700724 /* move remainder of path[depth] to the new leaf */
Alex Tomasa86c6182006-10-11 01:21:03 -0700725 BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max);
726 /* start copy from next extent */
727 /* TODO: we could do it by single memmove */
728 m = 0;
729 path[depth].p_ext++;
730 while (path[depth].p_ext <=
731 EXT_MAX_EXTENT(path[depth].p_hdr)) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700732 ext_debug("move %d:%llu:%d in new leaf %llu\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400733 le32_to_cpu(path[depth].p_ext->ee_block),
734 ext_pblock(path[depth].p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400735 ext4_ext_get_actual_len(path[depth].p_ext),
Alex Tomasa86c6182006-10-11 01:21:03 -0700736 newblock);
737 /*memmove(ex++, path[depth].p_ext++,
738 sizeof(struct ext4_extent));
739 neh->eh_entries++;*/
740 path[depth].p_ext++;
741 m++;
742 }
743 if (m) {
744 memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400745 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700746 }
747
748 set_buffer_uptodate(bh);
749 unlock_buffer(bh);
750
Avantika Mathur7e028972006-12-06 20:41:33 -0800751 err = ext4_journal_dirty_metadata(handle, bh);
752 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700753 goto cleanup;
754 brelse(bh);
755 bh = NULL;
756
757 /* correct old leaf */
758 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -0800759 err = ext4_ext_get_access(handle, inode, path + depth);
760 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700761 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -0400762 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
Avantika Mathur7e028972006-12-06 20:41:33 -0800763 err = ext4_ext_dirty(handle, inode, path + depth);
764 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700765 goto cleanup;
766
767 }
768
769 /* create intermediate indexes */
770 k = depth - at - 1;
771 BUG_ON(k < 0);
772 if (k)
773 ext_debug("create %d intermediate indices\n", k);
774 /* insert new index into current index block */
775 /* current depth stored in i var */
776 i = depth - 1;
777 while (k--) {
778 oldblock = newblock;
779 newblock = ablocks[--a];
Eric Sandeenbba90742008-01-28 23:58:27 -0500780 bh = sb_getblk(inode->i_sb, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700781 if (!bh) {
782 err = -EIO;
783 goto cleanup;
784 }
785 lock_buffer(bh);
786
Avantika Mathur7e028972006-12-06 20:41:33 -0800787 err = ext4_journal_get_create_access(handle, bh);
788 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700789 goto cleanup;
790
791 neh = ext_block_hdr(bh);
792 neh->eh_entries = cpu_to_le16(1);
793 neh->eh_magic = EXT4_EXT_MAGIC;
794 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
795 neh->eh_depth = cpu_to_le16(depth - i);
796 fidx = EXT_FIRST_INDEX(neh);
797 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700798 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700799
Eric Sandeenbba90742008-01-28 23:58:27 -0500800 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
801 i, newblock, le32_to_cpu(border), oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700802 /* copy indexes */
803 m = 0;
804 path[i].p_idx++;
805
806 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
807 EXT_MAX_INDEX(path[i].p_hdr));
808 BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) !=
809 EXT_LAST_INDEX(path[i].p_hdr));
810 while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400811 ext_debug("%d: move %d:%llu in new index %llu\n", i,
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400812 le32_to_cpu(path[i].p_idx->ei_block),
813 idx_pblock(path[i].p_idx),
814 newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700815 /*memmove(++fidx, path[i].p_idx++,
816 sizeof(struct ext4_extent_idx));
817 neh->eh_entries++;
818 BUG_ON(neh->eh_entries > neh->eh_max);*/
819 path[i].p_idx++;
820 m++;
821 }
822 if (m) {
823 memmove(++fidx, path[i].p_idx - m,
824 sizeof(struct ext4_extent_idx) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400825 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700826 }
827 set_buffer_uptodate(bh);
828 unlock_buffer(bh);
829
Avantika Mathur7e028972006-12-06 20:41:33 -0800830 err = ext4_journal_dirty_metadata(handle, bh);
831 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700832 goto cleanup;
833 brelse(bh);
834 bh = NULL;
835
836 /* correct old index */
837 if (m) {
838 err = ext4_ext_get_access(handle, inode, path + i);
839 if (err)
840 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -0400841 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700842 err = ext4_ext_dirty(handle, inode, path + i);
843 if (err)
844 goto cleanup;
845 }
846
847 i--;
848 }
849
850 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -0700851 err = ext4_ext_insert_index(handle, inode, path + at,
852 le32_to_cpu(border), newblock);
853
854cleanup:
855 if (bh) {
856 if (buffer_locked(bh))
857 unlock_buffer(bh);
858 brelse(bh);
859 }
860
861 if (err) {
862 /* free all allocated blocks in error case */
863 for (i = 0; i < depth; i++) {
864 if (!ablocks[i])
865 continue;
Alex Tomasc9de5602008-01-29 00:19:52 -0500866 ext4_free_blocks(handle, inode, ablocks[i], 1, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -0700867 }
868 }
869 kfree(ablocks);
870
871 return err;
872}
873
874/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700875 * ext4_ext_grow_indepth:
876 * implements tree growing procedure:
877 * - allocates new block
878 * - moves top-level data (index block or leaf) into the new block
879 * - initializes new top-level, creating index that points to the
880 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -0700881 */
882static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
883 struct ext4_ext_path *path,
884 struct ext4_extent *newext)
885{
886 struct ext4_ext_path *curp = path;
887 struct ext4_extent_header *neh;
888 struct ext4_extent_idx *fidx;
889 struct buffer_head *bh;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700890 ext4_fsblk_t newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700891 int err = 0;
892
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400893 newblock = ext4_ext_new_meta_block(handle, inode, path, newext, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700894 if (newblock == 0)
895 return err;
896
897 bh = sb_getblk(inode->i_sb, newblock);
898 if (!bh) {
899 err = -EIO;
900 ext4_std_error(inode->i_sb, err);
901 return err;
902 }
903 lock_buffer(bh);
904
Avantika Mathur7e028972006-12-06 20:41:33 -0800905 err = ext4_journal_get_create_access(handle, bh);
906 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -0700907 unlock_buffer(bh);
908 goto out;
909 }
910
911 /* move top-level index/leaf into new block */
912 memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
913
914 /* set size of new block */
915 neh = ext_block_hdr(bh);
916 /* old root could have indexes or leaves
917 * so calculate e_max right way */
918 if (ext_depth(inode))
919 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
920 else
921 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
922 neh->eh_magic = EXT4_EXT_MAGIC;
923 set_buffer_uptodate(bh);
924 unlock_buffer(bh);
925
Avantika Mathur7e028972006-12-06 20:41:33 -0800926 err = ext4_journal_dirty_metadata(handle, bh);
927 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700928 goto out;
929
930 /* create index in new top-level index: num,max,pointer */
Avantika Mathur7e028972006-12-06 20:41:33 -0800931 err = ext4_ext_get_access(handle, inode, curp);
932 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700933 goto out;
934
935 curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
936 curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode));
937 curp->p_hdr->eh_entries = cpu_to_le16(1);
938 curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400939
940 if (path[0].p_hdr->eh_depth)
941 curp->p_idx->ei_block =
942 EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
943 else
944 curp->p_idx->ei_block =
945 EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700946 ext4_idx_store_pblock(curp->p_idx, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700947
948 neh = ext_inode_hdr(inode);
949 fidx = EXT_FIRST_INDEX(neh);
Mingming Cao2ae02102006-10-11 01:21:11 -0700950 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700951 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700952 le32_to_cpu(fidx->ei_block), idx_pblock(fidx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700953
954 neh->eh_depth = cpu_to_le16(path->p_depth + 1);
955 err = ext4_ext_dirty(handle, inode, curp);
956out:
957 brelse(bh);
958
959 return err;
960}
961
962/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700963 * ext4_ext_create_new_leaf:
964 * finds empty index and adds new leaf.
965 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -0700966 */
967static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
968 struct ext4_ext_path *path,
969 struct ext4_extent *newext)
970{
971 struct ext4_ext_path *curp;
972 int depth, i, err = 0;
973
974repeat:
975 i = depth = ext_depth(inode);
976
977 /* walk up to the tree and look for free index entry */
978 curp = path + depth;
979 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
980 i--;
981 curp--;
982 }
983
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700984 /* we use already allocated block for index block,
985 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -0700986 if (EXT_HAS_FREE_INDEX(curp)) {
987 /* if we found index with free entry, then use that
988 * entry: create all needed subtree and add new leaf */
989 err = ext4_ext_split(handle, inode, path, newext, i);
Shen Feng787e0982008-07-11 19:27:31 -0400990 if (err)
991 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -0700992
993 /* refill path */
994 ext4_ext_drop_refs(path);
995 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500996 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
997 path);
Alex Tomasa86c6182006-10-11 01:21:03 -0700998 if (IS_ERR(path))
999 err = PTR_ERR(path);
1000 } else {
1001 /* tree is full, time to grow in depth */
1002 err = ext4_ext_grow_indepth(handle, inode, path, newext);
1003 if (err)
1004 goto out;
1005
1006 /* refill path */
1007 ext4_ext_drop_refs(path);
1008 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001009 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1010 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001011 if (IS_ERR(path)) {
1012 err = PTR_ERR(path);
1013 goto out;
1014 }
1015
1016 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001017 * only first (depth 0 -> 1) produces free space;
1018 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001019 */
1020 depth = ext_depth(inode);
1021 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001022 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001023 goto repeat;
1024 }
1025 }
1026
1027out:
1028 return err;
1029}
1030
1031/*
Alex Tomas1988b512008-01-28 23:58:27 -05001032 * search the closest allocated block to the left for *logical
1033 * and returns it at @logical + it's physical address at @phys
1034 * if *logical is the smallest allocated block, the function
1035 * returns 0 at @phys
1036 * return value contains 0 (success) or error code
1037 */
1038int
1039ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path,
1040 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1041{
1042 struct ext4_extent_idx *ix;
1043 struct ext4_extent *ex;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001044 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001045
1046 BUG_ON(path == NULL);
1047 depth = path->p_depth;
1048 *phys = 0;
1049
1050 if (depth == 0 && path->p_ext == NULL)
1051 return 0;
1052
1053 /* usually extent in the path covers blocks smaller
1054 * then *logical, but it can be that extent is the
1055 * first one in the file */
1056
1057 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001058 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001059 if (*logical < le32_to_cpu(ex->ee_block)) {
1060 BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
1061 while (--depth >= 0) {
1062 ix = path[depth].p_idx;
1063 BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
1064 }
1065 return 0;
1066 }
1067
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001068 BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
Alex Tomas1988b512008-01-28 23:58:27 -05001069
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001070 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
1071 *phys = ext_pblock(ex) + ee_len - 1;
Alex Tomas1988b512008-01-28 23:58:27 -05001072 return 0;
1073}
1074
1075/*
1076 * search the closest allocated block to the right for *logical
1077 * and returns it at @logical + it's physical address at @phys
1078 * if *logical is the smallest allocated block, the function
1079 * returns 0 at @phys
1080 * return value contains 0 (success) or error code
1081 */
1082int
1083ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path,
1084 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1085{
1086 struct buffer_head *bh = NULL;
1087 struct ext4_extent_header *eh;
1088 struct ext4_extent_idx *ix;
1089 struct ext4_extent *ex;
1090 ext4_fsblk_t block;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001091 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001092
1093 BUG_ON(path == NULL);
1094 depth = path->p_depth;
1095 *phys = 0;
1096
1097 if (depth == 0 && path->p_ext == NULL)
1098 return 0;
1099
1100 /* usually extent in the path covers blocks smaller
1101 * then *logical, but it can be that extent is the
1102 * first one in the file */
1103
1104 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001105 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001106 if (*logical < le32_to_cpu(ex->ee_block)) {
1107 BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
1108 while (--depth >= 0) {
1109 ix = path[depth].p_idx;
1110 BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
1111 }
1112 *logical = le32_to_cpu(ex->ee_block);
1113 *phys = ext_pblock(ex);
1114 return 0;
1115 }
1116
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001117 BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
Alex Tomas1988b512008-01-28 23:58:27 -05001118
1119 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1120 /* next allocated block in this leaf */
1121 ex++;
1122 *logical = le32_to_cpu(ex->ee_block);
1123 *phys = ext_pblock(ex);
1124 return 0;
1125 }
1126
1127 /* go up and search for index to the right */
1128 while (--depth >= 0) {
1129 ix = path[depth].p_idx;
1130 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
1131 break;
1132 }
1133
1134 if (depth < 0) {
1135 /* we've gone up to the root and
1136 * found no index to the right */
1137 return 0;
1138 }
1139
1140 /* we've found index to the right, let's
1141 * follow it and find the closest allocated
1142 * block to the right */
1143 ix++;
1144 block = idx_pblock(ix);
1145 while (++depth < path->p_depth) {
1146 bh = sb_bread(inode->i_sb, block);
1147 if (bh == NULL)
1148 return -EIO;
1149 eh = ext_block_hdr(bh);
1150 if (ext4_ext_check_header(inode, eh, depth)) {
1151 put_bh(bh);
1152 return -EIO;
1153 }
1154 ix = EXT_FIRST_INDEX(eh);
1155 block = idx_pblock(ix);
1156 put_bh(bh);
1157 }
1158
1159 bh = sb_bread(inode->i_sb, block);
1160 if (bh == NULL)
1161 return -EIO;
1162 eh = ext_block_hdr(bh);
1163 if (ext4_ext_check_header(inode, eh, path->p_depth - depth)) {
1164 put_bh(bh);
1165 return -EIO;
1166 }
1167 ex = EXT_FIRST_EXTENT(eh);
1168 *logical = le32_to_cpu(ex->ee_block);
1169 *phys = ext_pblock(ex);
1170 put_bh(bh);
1171 return 0;
1172
1173}
1174
1175/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001176 * ext4_ext_next_allocated_block:
1177 * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1178 * NOTE: it considers block number from index entry as
1179 * allocated block. Thus, index entries have to be consistent
1180 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001181 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001182static ext4_lblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -07001183ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1184{
1185 int depth;
1186
1187 BUG_ON(path == NULL);
1188 depth = path->p_depth;
1189
1190 if (depth == 0 && path->p_ext == NULL)
1191 return EXT_MAX_BLOCK;
1192
1193 while (depth >= 0) {
1194 if (depth == path->p_depth) {
1195 /* leaf */
1196 if (path[depth].p_ext !=
1197 EXT_LAST_EXTENT(path[depth].p_hdr))
1198 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1199 } else {
1200 /* index */
1201 if (path[depth].p_idx !=
1202 EXT_LAST_INDEX(path[depth].p_hdr))
1203 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1204 }
1205 depth--;
1206 }
1207
1208 return EXT_MAX_BLOCK;
1209}
1210
1211/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001212 * ext4_ext_next_leaf_block:
Alex Tomasa86c6182006-10-11 01:21:03 -07001213 * returns first allocated block from next leaf or EXT_MAX_BLOCK
1214 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001215static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode,
Andrew Morton63f57932006-10-11 01:21:24 -07001216 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001217{
1218 int depth;
1219
1220 BUG_ON(path == NULL);
1221 depth = path->p_depth;
1222
1223 /* zero-tree has no leaf blocks at all */
1224 if (depth == 0)
1225 return EXT_MAX_BLOCK;
1226
1227 /* go to index block */
1228 depth--;
1229
1230 while (depth >= 0) {
1231 if (path[depth].p_idx !=
1232 EXT_LAST_INDEX(path[depth].p_hdr))
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001233 return (ext4_lblk_t)
1234 le32_to_cpu(path[depth].p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001235 depth--;
1236 }
1237
1238 return EXT_MAX_BLOCK;
1239}
1240
1241/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001242 * ext4_ext_correct_indexes:
1243 * if leaf gets modified and modified extent is first in the leaf,
1244 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001245 * TODO: do we need to correct tree in all cases?
1246 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001247static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001248 struct ext4_ext_path *path)
1249{
1250 struct ext4_extent_header *eh;
1251 int depth = ext_depth(inode);
1252 struct ext4_extent *ex;
1253 __le32 border;
1254 int k, err = 0;
1255
1256 eh = path[depth].p_hdr;
1257 ex = path[depth].p_ext;
1258 BUG_ON(ex == NULL);
1259 BUG_ON(eh == NULL);
1260
1261 if (depth == 0) {
1262 /* there is no tree at all */
1263 return 0;
1264 }
1265
1266 if (ex != EXT_FIRST_EXTENT(eh)) {
1267 /* we correct tree if first leaf got modified only */
1268 return 0;
1269 }
1270
1271 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001272 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001273 */
1274 k = depth - 1;
1275 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001276 err = ext4_ext_get_access(handle, inode, path + k);
1277 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001278 return err;
1279 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001280 err = ext4_ext_dirty(handle, inode, path + k);
1281 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001282 return err;
1283
1284 while (k--) {
1285 /* change all left-side indexes */
1286 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1287 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001288 err = ext4_ext_get_access(handle, inode, path + k);
1289 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001290 break;
1291 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001292 err = ext4_ext_dirty(handle, inode, path + k);
1293 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001294 break;
1295 }
1296
1297 return err;
1298}
1299
Avantika Mathur09b88252006-12-06 20:41:36 -08001300static int
Alex Tomasa86c6182006-10-11 01:21:03 -07001301ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1302 struct ext4_extent *ex2)
1303{
Amit Arora749269f2007-07-18 09:02:56 -04001304 unsigned short ext1_ee_len, ext2_ee_len, max_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001305
1306 /*
1307 * Make sure that either both extents are uninitialized, or
1308 * both are _not_.
1309 */
1310 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1311 return 0;
1312
Amit Arora749269f2007-07-18 09:02:56 -04001313 if (ext4_ext_is_uninitialized(ex1))
1314 max_len = EXT_UNINIT_MAX_LEN;
1315 else
1316 max_len = EXT_INIT_MAX_LEN;
1317
Amit Aroraa2df2a62007-07-17 21:42:41 -04001318 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1319 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1320
1321 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001322 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001323 return 0;
1324
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001325 /*
1326 * To allow future support for preallocated extents to be added
1327 * as an RO_COMPAT feature, refuse to merge to extents if
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001328 * this can result in the top bit of ee_len being set.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001329 */
Amit Arora749269f2007-07-18 09:02:56 -04001330 if (ext1_ee_len + ext2_ee_len > max_len)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001331 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001332#ifdef AGGRESSIVE_TEST
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001333 if (ext1_ee_len >= 4)
Alex Tomasa86c6182006-10-11 01:21:03 -07001334 return 0;
1335#endif
1336
Amit Aroraa2df2a62007-07-17 21:42:41 -04001337 if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001338 return 1;
1339 return 0;
1340}
1341
1342/*
Amit Arora56055d32007-07-17 21:42:38 -04001343 * This function tries to merge the "ex" extent to the next extent in the tree.
1344 * It always tries to merge towards right. If you want to merge towards
1345 * left, pass "ex - 1" as argument instead of "ex".
1346 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1347 * 1 if they got merged.
1348 */
1349int ext4_ext_try_to_merge(struct inode *inode,
1350 struct ext4_ext_path *path,
1351 struct ext4_extent *ex)
1352{
1353 struct ext4_extent_header *eh;
1354 unsigned int depth, len;
1355 int merge_done = 0;
1356 int uninitialized = 0;
1357
1358 depth = ext_depth(inode);
1359 BUG_ON(path[depth].p_hdr == NULL);
1360 eh = path[depth].p_hdr;
1361
1362 while (ex < EXT_LAST_EXTENT(eh)) {
1363 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1364 break;
1365 /* merge with next extent! */
1366 if (ext4_ext_is_uninitialized(ex))
1367 uninitialized = 1;
1368 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1369 + ext4_ext_get_actual_len(ex + 1));
1370 if (uninitialized)
1371 ext4_ext_mark_uninitialized(ex);
1372
1373 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1374 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1375 * sizeof(struct ext4_extent);
1376 memmove(ex + 1, ex + 2, len);
1377 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04001378 le16_add_cpu(&eh->eh_entries, -1);
Amit Arora56055d32007-07-17 21:42:38 -04001379 merge_done = 1;
1380 WARN_ON(eh->eh_entries == 0);
1381 if (!eh->eh_entries)
1382 ext4_error(inode->i_sb, "ext4_ext_try_to_merge",
1383 "inode#%lu, eh->eh_entries = 0!", inode->i_ino);
1384 }
1385
1386 return merge_done;
1387}
1388
1389/*
Amit Arora25d14f92007-05-24 13:04:13 -04001390 * check if a portion of the "newext" extent overlaps with an
1391 * existing extent.
1392 *
1393 * If there is an overlap discovered, it updates the length of the newext
1394 * such that there will be no overlap, and then returns 1.
1395 * If there is no overlap found, it returns 0.
1396 */
1397unsigned int ext4_ext_check_overlap(struct inode *inode,
1398 struct ext4_extent *newext,
1399 struct ext4_ext_path *path)
1400{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001401 ext4_lblk_t b1, b2;
Amit Arora25d14f92007-05-24 13:04:13 -04001402 unsigned int depth, len1;
1403 unsigned int ret = 0;
1404
1405 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001406 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001407 depth = ext_depth(inode);
1408 if (!path[depth].p_ext)
1409 goto out;
1410 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
1411
1412 /*
1413 * get the next allocated block if the extent in the path
1414 * is before the requested block(s)
1415 */
1416 if (b2 < b1) {
1417 b2 = ext4_ext_next_allocated_block(path);
1418 if (b2 == EXT_MAX_BLOCK)
1419 goto out;
1420 }
1421
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001422 /* check for wrap through zero on extent logical start block*/
Amit Arora25d14f92007-05-24 13:04:13 -04001423 if (b1 + len1 < b1) {
1424 len1 = EXT_MAX_BLOCK - b1;
1425 newext->ee_len = cpu_to_le16(len1);
1426 ret = 1;
1427 }
1428
1429 /* check for overlap */
1430 if (b1 + len1 > b2) {
1431 newext->ee_len = cpu_to_le16(b2 - b1);
1432 ret = 1;
1433 }
1434out:
1435 return ret;
1436}
1437
1438/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001439 * ext4_ext_insert_extent:
1440 * tries to merge requsted extent into the existing extent or
1441 * inserts requested extent as new one into the tree,
1442 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001443 */
1444int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1445 struct ext4_ext_path *path,
1446 struct ext4_extent *newext)
1447{
1448 struct ext4_extent_header * eh;
1449 struct ext4_extent *ex, *fex;
1450 struct ext4_extent *nearex; /* nearest extent */
1451 struct ext4_ext_path *npath = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001452 int depth, len, err;
1453 ext4_lblk_t next;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001454 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001455
Amit Aroraa2df2a62007-07-17 21:42:41 -04001456 BUG_ON(ext4_ext_get_actual_len(newext) == 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07001457 depth = ext_depth(inode);
1458 ex = path[depth].p_ext;
1459 BUG_ON(path[depth].p_hdr == NULL);
1460
1461 /* try to insert block into found extent and return */
1462 if (ex && ext4_can_extents_be_merged(inode, ex, newext)) {
Mingming Cao2ae02102006-10-11 01:21:11 -07001463 ext_debug("append %d block to %d:%d (from %llu)\n",
Amit Aroraa2df2a62007-07-17 21:42:41 -04001464 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001465 le32_to_cpu(ex->ee_block),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001466 ext4_ext_get_actual_len(ex), ext_pblock(ex));
Avantika Mathur7e028972006-12-06 20:41:33 -08001467 err = ext4_ext_get_access(handle, inode, path + depth);
1468 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001469 return err;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001470
1471 /*
1472 * ext4_can_extents_be_merged should have checked that either
1473 * both extents are uninitialized, or both aren't. Thus we
1474 * need to check only one of them here.
1475 */
1476 if (ext4_ext_is_uninitialized(ex))
1477 uninitialized = 1;
1478 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1479 + ext4_ext_get_actual_len(newext));
1480 if (uninitialized)
1481 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001482 eh = path[depth].p_hdr;
1483 nearex = ex;
1484 goto merge;
1485 }
1486
1487repeat:
1488 depth = ext_depth(inode);
1489 eh = path[depth].p_hdr;
1490 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1491 goto has_space;
1492
1493 /* probably next leaf has space for us? */
1494 fex = EXT_LAST_EXTENT(eh);
1495 next = ext4_ext_next_leaf_block(inode, path);
1496 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
1497 && next != EXT_MAX_BLOCK) {
1498 ext_debug("next leaf block - %d\n", next);
1499 BUG_ON(npath != NULL);
1500 npath = ext4_ext_find_extent(inode, next, NULL);
1501 if (IS_ERR(npath))
1502 return PTR_ERR(npath);
1503 BUG_ON(npath->p_depth != path->p_depth);
1504 eh = npath[depth].p_hdr;
1505 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
1506 ext_debug("next leaf isnt full(%d)\n",
1507 le16_to_cpu(eh->eh_entries));
1508 path = npath;
1509 goto repeat;
1510 }
1511 ext_debug("next leaf has no free space(%d,%d)\n",
1512 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1513 }
1514
1515 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001516 * There is no free space in the found leaf.
1517 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07001518 */
1519 err = ext4_ext_create_new_leaf(handle, inode, path, newext);
1520 if (err)
1521 goto cleanup;
1522 depth = ext_depth(inode);
1523 eh = path[depth].p_hdr;
1524
1525has_space:
1526 nearex = path[depth].p_ext;
1527
Avantika Mathur7e028972006-12-06 20:41:33 -08001528 err = ext4_ext_get_access(handle, inode, path + depth);
1529 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001530 goto cleanup;
1531
1532 if (!nearex) {
1533 /* there is no extent in this leaf, create first one */
Mingming Cao2ae02102006-10-11 01:21:11 -07001534 ext_debug("first extent in the leaf: %d:%llu:%d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001535 le32_to_cpu(newext->ee_block),
1536 ext_pblock(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001537 ext4_ext_get_actual_len(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001538 path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1539 } else if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001540 > le32_to_cpu(nearex->ee_block)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001541/* BUG_ON(newext->ee_block == nearex->ee_block); */
1542 if (nearex != EXT_LAST_EXTENT(eh)) {
1543 len = EXT_MAX_EXTENT(eh) - nearex;
1544 len = (len - 1) * sizeof(struct ext4_extent);
1545 len = len < 0 ? 0 : len;
Mingming Cao2ae02102006-10-11 01:21:11 -07001546 ext_debug("insert %d:%llu:%d after: nearest 0x%p, "
Alex Tomasa86c6182006-10-11 01:21:03 -07001547 "move %d from 0x%p to 0x%p\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001548 le32_to_cpu(newext->ee_block),
1549 ext_pblock(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001550 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001551 nearex, len, nearex + 1, nearex + 2);
1552 memmove(nearex + 2, nearex + 1, len);
1553 }
1554 path[depth].p_ext = nearex + 1;
1555 } else {
1556 BUG_ON(newext->ee_block == nearex->ee_block);
1557 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1558 len = len < 0 ? 0 : len;
Mingming Cao2ae02102006-10-11 01:21:11 -07001559 ext_debug("insert %d:%llu:%d before: nearest 0x%p, "
Alex Tomasa86c6182006-10-11 01:21:03 -07001560 "move %d from 0x%p to 0x%p\n",
1561 le32_to_cpu(newext->ee_block),
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001562 ext_pblock(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001563 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001564 nearex, len, nearex + 1, nearex + 2);
1565 memmove(nearex + 1, nearex, len);
1566 path[depth].p_ext = nearex;
1567 }
1568
Marcin Slusarze8546d02008-04-17 10:38:59 -04001569 le16_add_cpu(&eh->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07001570 nearex = path[depth].p_ext;
1571 nearex->ee_block = newext->ee_block;
Aneesh Kumar K.Vb3776112007-10-16 18:38:25 -04001572 ext4_ext_store_pblock(nearex, ext_pblock(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001573 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07001574
1575merge:
1576 /* try to merge extents to the right */
Amit Arora56055d32007-07-17 21:42:38 -04001577 ext4_ext_try_to_merge(inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001578
1579 /* try to merge extents to the left */
1580
1581 /* time to correct all indexes above */
1582 err = ext4_ext_correct_indexes(handle, inode, path);
1583 if (err)
1584 goto cleanup;
1585
1586 err = ext4_ext_dirty(handle, inode, path + depth);
1587
1588cleanup:
1589 if (npath) {
1590 ext4_ext_drop_refs(npath);
1591 kfree(npath);
1592 }
1593 ext4_ext_tree_changed(inode);
1594 ext4_ext_invalidate_cache(inode);
1595 return err;
1596}
1597
Avantika Mathur09b88252006-12-06 20:41:36 -08001598static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001599ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
Mingming Caodd545672007-07-31 00:37:46 -07001600 __u32 len, ext4_fsblk_t start, int type)
Alex Tomasa86c6182006-10-11 01:21:03 -07001601{
1602 struct ext4_ext_cache *cex;
1603 BUG_ON(len == 0);
1604 cex = &EXT4_I(inode)->i_cached_extent;
1605 cex->ec_type = type;
1606 cex->ec_block = block;
1607 cex->ec_len = len;
1608 cex->ec_start = start;
1609}
1610
1611/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001612 * ext4_ext_put_gap_in_cache:
1613 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07001614 * and cache this gap
1615 */
Avantika Mathur09b88252006-12-06 20:41:36 -08001616static void
Alex Tomasa86c6182006-10-11 01:21:03 -07001617ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001618 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -07001619{
1620 int depth = ext_depth(inode);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001621 unsigned long len;
1622 ext4_lblk_t lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001623 struct ext4_extent *ex;
1624
1625 ex = path[depth].p_ext;
1626 if (ex == NULL) {
1627 /* there is no extent yet, so gap is [0;-] */
1628 lblock = 0;
1629 len = EXT_MAX_BLOCK;
1630 ext_debug("cache gap(whole file):");
1631 } else if (block < le32_to_cpu(ex->ee_block)) {
1632 lblock = block;
1633 len = le32_to_cpu(ex->ee_block) - block;
Eric Sandeenbba90742008-01-28 23:58:27 -05001634 ext_debug("cache gap(before): %u [%u:%u]",
1635 block,
1636 le32_to_cpu(ex->ee_block),
1637 ext4_ext_get_actual_len(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07001638 } else if (block >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001639 + ext4_ext_get_actual_len(ex)) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001640 ext4_lblk_t next;
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001641 lblock = le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001642 + ext4_ext_get_actual_len(ex);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001643
1644 next = ext4_ext_next_allocated_block(path);
Eric Sandeenbba90742008-01-28 23:58:27 -05001645 ext_debug("cache gap(after): [%u:%u] %u",
1646 le32_to_cpu(ex->ee_block),
1647 ext4_ext_get_actual_len(ex),
1648 block);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001649 BUG_ON(next == lblock);
1650 len = next - lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001651 } else {
1652 lblock = len = 0;
1653 BUG();
1654 }
1655
Eric Sandeenbba90742008-01-28 23:58:27 -05001656 ext_debug(" -> %u:%lu\n", lblock, len);
Alex Tomasa86c6182006-10-11 01:21:03 -07001657 ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
1658}
1659
Avantika Mathur09b88252006-12-06 20:41:36 -08001660static int
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001661ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
Alex Tomasa86c6182006-10-11 01:21:03 -07001662 struct ext4_extent *ex)
1663{
1664 struct ext4_ext_cache *cex;
1665
1666 cex = &EXT4_I(inode)->i_cached_extent;
1667
1668 /* has cache valid data? */
1669 if (cex->ec_type == EXT4_EXT_CACHE_NO)
1670 return EXT4_EXT_CACHE_NO;
1671
1672 BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
1673 cex->ec_type != EXT4_EXT_CACHE_EXTENT);
1674 if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001675 ex->ee_block = cpu_to_le32(cex->ec_block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001676 ext4_ext_store_pblock(ex, cex->ec_start);
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001677 ex->ee_len = cpu_to_le16(cex->ec_len);
Eric Sandeenbba90742008-01-28 23:58:27 -05001678 ext_debug("%u cached by %u:%u:%llu\n",
1679 block,
1680 cex->ec_block, cex->ec_len, cex->ec_start);
Alex Tomasa86c6182006-10-11 01:21:03 -07001681 return cex->ec_type;
1682 }
1683
1684 /* not in cache */
1685 return EXT4_EXT_CACHE_NO;
1686}
1687
1688/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001689 * ext4_ext_rm_idx:
1690 * removes index from the index block.
1691 * It's used in truncate case only, thus all requests are for
1692 * last index in the block only.
Alex Tomasa86c6182006-10-11 01:21:03 -07001693 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001694static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001695 struct ext4_ext_path *path)
1696{
1697 struct buffer_head *bh;
1698 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001699 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07001700
1701 /* free index block */
1702 path--;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001703 leaf = idx_pblock(path->p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -07001704 BUG_ON(path->p_hdr->eh_entries == 0);
Avantika Mathur7e028972006-12-06 20:41:33 -08001705 err = ext4_ext_get_access(handle, inode, path);
1706 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001707 return err;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001708 le16_add_cpu(&path->p_hdr->eh_entries, -1);
Avantika Mathur7e028972006-12-06 20:41:33 -08001709 err = ext4_ext_dirty(handle, inode, path);
1710 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001711 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07001712 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Alex Tomasa86c6182006-10-11 01:21:03 -07001713 bh = sb_find_get_block(inode->i_sb, leaf);
1714 ext4_forget(handle, 1, inode, bh, leaf);
Alex Tomasc9de5602008-01-29 00:19:52 -05001715 ext4_free_blocks(handle, inode, leaf, 1, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07001716 return err;
1717}
1718
1719/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001720 * ext4_ext_calc_credits_for_insert:
1721 * This routine returns max. credits that the extent tree can consume.
Alex Tomasa86c6182006-10-11 01:21:03 -07001722 * It should be OK for low-performance paths like ->writepage()
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001723 * To allow many writing processes to fit into a single transaction,
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001724 * the caller should calculate credits under i_data_sem and
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001725 * pass the actual path.
Alex Tomasa86c6182006-10-11 01:21:03 -07001726 */
Avantika Mathur09b88252006-12-06 20:41:36 -08001727int ext4_ext_calc_credits_for_insert(struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001728 struct ext4_ext_path *path)
1729{
1730 int depth, needed;
1731
1732 if (path) {
1733 /* probably there is space in leaf? */
1734 depth = ext_depth(inode);
1735 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
1736 < le16_to_cpu(path[depth].p_hdr->eh_max))
1737 return 1;
1738 }
1739
1740 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001741 * given 32-bit logical block (4294967296 blocks), max. tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001742 * can be 4 levels in depth -- 4 * 340^4 == 53453440000.
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001743 * Let's also add one more level for imbalance.
Alex Tomasa86c6182006-10-11 01:21:03 -07001744 */
1745 depth = 5;
1746
1747 /* allocation of new data block(s) */
1748 needed = 2;
1749
1750 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001751 * tree can be full, so it would need to grow in depth:
Johann Lombardifeb18922006-12-06 20:40:46 -08001752 * we need one credit to modify old root, credits for
1753 * new root will be added in split accounting
Alex Tomasa86c6182006-10-11 01:21:03 -07001754 */
Johann Lombardifeb18922006-12-06 20:40:46 -08001755 needed += 1;
Alex Tomasa86c6182006-10-11 01:21:03 -07001756
1757 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001758 * Index split can happen, we would need:
Alex Tomasa86c6182006-10-11 01:21:03 -07001759 * allocate intermediate indexes (bitmap + group)
1760 * + change two blocks at each level, but root (already included)
1761 */
Johann Lombardifeb18922006-12-06 20:40:46 -08001762 needed += (depth * 2) + (depth * 2);
Alex Tomasa86c6182006-10-11 01:21:03 -07001763
1764 /* any allocation modifies superblock */
1765 needed += 1;
1766
1767 return needed;
1768}
1769
1770static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
1771 struct ext4_extent *ex,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001772 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07001773{
1774 struct buffer_head *bh;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001775 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05001776 int i, metadata = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001777
Alex Tomasc9de5602008-01-29 00:19:52 -05001778 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
1779 metadata = 1;
Alex Tomasa86c6182006-10-11 01:21:03 -07001780#ifdef EXTENTS_STATS
1781 {
1782 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07001783 spin_lock(&sbi->s_ext_stats_lock);
1784 sbi->s_ext_blocks += ee_len;
1785 sbi->s_ext_extents++;
1786 if (ee_len < sbi->s_ext_min)
1787 sbi->s_ext_min = ee_len;
1788 if (ee_len > sbi->s_ext_max)
1789 sbi->s_ext_max = ee_len;
1790 if (ext_depth(inode) > sbi->s_depth_max)
1791 sbi->s_depth_max = ext_depth(inode);
1792 spin_unlock(&sbi->s_ext_stats_lock);
1793 }
1794#endif
1795 if (from >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001796 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001797 /* tail removal */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001798 ext4_lblk_t num;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001799 ext4_fsblk_t start;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001800
Amit Aroraa2df2a62007-07-17 21:42:41 -04001801 num = le32_to_cpu(ex->ee_block) + ee_len - from;
1802 start = ext_pblock(ex) + ee_len - num;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001803 ext_debug("free last %u blocks starting %llu\n", num, start);
Alex Tomasa86c6182006-10-11 01:21:03 -07001804 for (i = 0; i < num; i++) {
1805 bh = sb_find_get_block(inode->i_sb, start + i);
1806 ext4_forget(handle, 0, inode, bh, start + i);
1807 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001808 ext4_free_blocks(handle, inode, start, num, metadata);
Alex Tomasa86c6182006-10-11 01:21:03 -07001809 } else if (from == le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001810 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001811 printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n",
Amit Aroraa2df2a62007-07-17 21:42:41 -04001812 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07001813 } else {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001814 printk(KERN_INFO "strange request: removal(2) "
1815 "%u-%u from %u:%u\n",
1816 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07001817 }
1818 return 0;
1819}
1820
1821static int
1822ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001823 struct ext4_ext_path *path, ext4_lblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07001824{
1825 int err = 0, correct_index = 0;
1826 int depth = ext_depth(inode), credits;
1827 struct ext4_extent_header *eh;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001828 ext4_lblk_t a, b, block;
1829 unsigned num;
1830 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07001831 unsigned short ex_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001832 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001833 struct ext4_extent *ex;
1834
Alex Tomasc29c0ae2007-07-18 09:19:09 -04001835 /* the header must be checked already in ext4_ext_remove_space() */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001836 ext_debug("truncate since %u in leaf\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07001837 if (!path[depth].p_hdr)
1838 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
1839 eh = path[depth].p_hdr;
1840 BUG_ON(eh == NULL);
Alex Tomasa86c6182006-10-11 01:21:03 -07001841
1842 /* find where to start removing */
1843 ex = EXT_LAST_EXTENT(eh);
1844
1845 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001846 if (ext4_ext_is_uninitialized(ex))
1847 uninitialized = 1;
1848 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001849
1850 while (ex >= EXT_FIRST_EXTENT(eh) &&
1851 ex_ee_block + ex_ee_len > start) {
1852 ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len);
1853 path[depth].p_ext = ex;
1854
1855 a = ex_ee_block > start ? ex_ee_block : start;
1856 b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
1857 ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
1858
1859 ext_debug(" border %u:%u\n", a, b);
1860
1861 if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
1862 block = 0;
1863 num = 0;
1864 BUG();
1865 } else if (a != ex_ee_block) {
1866 /* remove tail of the extent */
1867 block = ex_ee_block;
1868 num = a - block;
1869 } else if (b != ex_ee_block + ex_ee_len - 1) {
1870 /* remove head of the extent */
1871 block = a;
1872 num = b - a;
1873 /* there is no "make a hole" API yet */
1874 BUG();
1875 } else {
1876 /* remove whole extent: excellent! */
1877 block = ex_ee_block;
1878 num = 0;
1879 BUG_ON(a != ex_ee_block);
1880 BUG_ON(b != ex_ee_block + ex_ee_len - 1);
1881 }
1882
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001883 /* at present, extent can't cross block group: */
Alex Tomasa86c6182006-10-11 01:21:03 -07001884 /* leaf + bitmap + group desc + sb + inode */
1885 credits = 5;
1886 if (ex == EXT_FIRST_EXTENT(eh)) {
1887 correct_index = 1;
1888 credits += (ext_depth(inode)) + 1;
1889 }
1890#ifdef CONFIG_QUOTA
1891 credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
1892#endif
1893
Shen Feng9102e4f2008-07-11 19:27:31 -04001894 err = ext4_ext_journal_restart(handle, credits);
1895 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001896 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001897
1898 err = ext4_ext_get_access(handle, inode, path + depth);
1899 if (err)
1900 goto out;
1901
1902 err = ext4_remove_blocks(handle, inode, ex, a, b);
1903 if (err)
1904 goto out;
1905
1906 if (num == 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001907 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001908 ext4_ext_store_pblock(ex, 0);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001909 le16_add_cpu(&eh->eh_entries, -1);
Alex Tomasa86c6182006-10-11 01:21:03 -07001910 }
1911
1912 ex->ee_block = cpu_to_le32(block);
1913 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04001914 /*
1915 * Do not mark uninitialized if all the blocks in the
1916 * extent have been removed.
1917 */
1918 if (uninitialized && num)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001919 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001920
1921 err = ext4_ext_dirty(handle, inode, path + depth);
1922 if (err)
1923 goto out;
1924
Mingming Cao2ae02102006-10-11 01:21:11 -07001925 ext_debug("new extent: %u:%u:%llu\n", block, num,
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001926 ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07001927 ex--;
1928 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001929 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001930 }
1931
1932 if (correct_index && eh->eh_entries)
1933 err = ext4_ext_correct_indexes(handle, inode, path);
1934
1935 /* if this leaf is free, then we should
1936 * remove it from index block above */
1937 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
1938 err = ext4_ext_rm_idx(handle, inode, path + depth);
1939
1940out:
1941 return err;
1942}
1943
1944/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001945 * ext4_ext_more_to_rm:
1946 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07001947 */
Avantika Mathur09b88252006-12-06 20:41:36 -08001948static int
Alex Tomasa86c6182006-10-11 01:21:03 -07001949ext4_ext_more_to_rm(struct ext4_ext_path *path)
1950{
1951 BUG_ON(path->p_idx == NULL);
1952
1953 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
1954 return 0;
1955
1956 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001957 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07001958 * so we have to consider current index for truncation
1959 */
1960 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
1961 return 0;
1962 return 1;
1963}
1964
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001965static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07001966{
1967 struct super_block *sb = inode->i_sb;
1968 int depth = ext_depth(inode);
1969 struct ext4_ext_path *path;
1970 handle_t *handle;
1971 int i = 0, err = 0;
1972
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001973 ext_debug("truncate since %u\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07001974
1975 /* probably first extent we're gonna free will be last in block */
1976 handle = ext4_journal_start(inode, depth + 1);
1977 if (IS_ERR(handle))
1978 return PTR_ERR(handle);
1979
1980 ext4_ext_invalidate_cache(inode);
1981
1982 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001983 * We start scanning from right side, freeing all the blocks
1984 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07001985 */
Josef Bacik216553c2008-04-29 22:02:02 -04001986 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -07001987 if (path == NULL) {
1988 ext4_journal_stop(handle);
1989 return -ENOMEM;
1990 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001991 path[0].p_hdr = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -04001992 if (ext4_ext_check_header(inode, path[0].p_hdr, depth)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001993 err = -EIO;
1994 goto out;
1995 }
1996 path[0].p_depth = depth;
1997
1998 while (i >= 0 && err == 0) {
1999 if (i == depth) {
2000 /* this is leaf block */
2001 err = ext4_ext_rm_leaf(handle, inode, path, start);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002002 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002003 brelse(path[i].p_bh);
2004 path[i].p_bh = NULL;
2005 i--;
2006 continue;
2007 }
2008
2009 /* this is index block */
2010 if (!path[i].p_hdr) {
2011 ext_debug("initialize header\n");
2012 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002013 }
2014
Alex Tomasa86c6182006-10-11 01:21:03 -07002015 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002016 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07002017 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2018 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2019 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2020 path[i].p_hdr,
2021 le16_to_cpu(path[i].p_hdr->eh_entries));
2022 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002023 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07002024 path[i].p_idx--;
2025 }
2026
2027 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2028 i, EXT_FIRST_INDEX(path[i].p_hdr),
2029 path[i].p_idx);
2030 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002031 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002032 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07002033 ext_debug("move to level %d (block %llu)\n",
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002034 i + 1, idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07002035 memset(path + i + 1, 0, sizeof(*path));
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002036 bh = sb_bread(sb, idx_pblock(path[i].p_idx));
2037 if (!bh) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002038 /* should we reset i_size? */
2039 err = -EIO;
2040 break;
2041 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002042 if (WARN_ON(i + 1 > depth)) {
2043 err = -EIO;
2044 break;
2045 }
2046 if (ext4_ext_check_header(inode, ext_block_hdr(bh),
2047 depth - i - 1)) {
2048 err = -EIO;
2049 break;
2050 }
2051 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002052
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002053 /* save actual number of indexes since this
2054 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07002055 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2056 i++;
2057 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002058 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07002059 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002060 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07002061 * handle must be already prepared by the
2062 * truncatei_leaf() */
2063 err = ext4_ext_rm_idx(handle, inode, path + i);
2064 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002065 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002066 brelse(path[i].p_bh);
2067 path[i].p_bh = NULL;
2068 i--;
2069 ext_debug("return to level %d\n", i);
2070 }
2071 }
2072
2073 /* TODO: flexible tree reduction should be here */
2074 if (path->p_hdr->eh_entries == 0) {
2075 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002076 * truncate to zero freed all the tree,
2077 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07002078 */
2079 err = ext4_ext_get_access(handle, inode, path);
2080 if (err == 0) {
2081 ext_inode_hdr(inode)->eh_depth = 0;
2082 ext_inode_hdr(inode)->eh_max =
2083 cpu_to_le16(ext4_ext_space_root(inode));
2084 err = ext4_ext_dirty(handle, inode, path);
2085 }
2086 }
2087out:
2088 ext4_ext_tree_changed(inode);
2089 ext4_ext_drop_refs(path);
2090 kfree(path);
2091 ext4_journal_stop(handle);
2092
2093 return err;
2094}
2095
2096/*
2097 * called at mount time
2098 */
2099void ext4_ext_init(struct super_block *sb)
2100{
2101 /*
2102 * possible initialization would be here
2103 */
2104
2105 if (test_opt(sb, EXTENTS)) {
2106 printk("EXT4-fs: file extents enabled");
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01002107#ifdef AGGRESSIVE_TEST
2108 printk(", aggressive tests");
Alex Tomasa86c6182006-10-11 01:21:03 -07002109#endif
2110#ifdef CHECK_BINSEARCH
2111 printk(", check binsearch");
2112#endif
2113#ifdef EXTENTS_STATS
2114 printk(", stats");
2115#endif
2116 printk("\n");
2117#ifdef EXTENTS_STATS
2118 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2119 EXT4_SB(sb)->s_ext_min = 1 << 30;
2120 EXT4_SB(sb)->s_ext_max = 0;
2121#endif
2122 }
2123}
2124
2125/*
2126 * called at umount time
2127 */
2128void ext4_ext_release(struct super_block *sb)
2129{
2130 if (!test_opt(sb, EXTENTS))
2131 return;
2132
2133#ifdef EXTENTS_STATS
2134 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2135 struct ext4_sb_info *sbi = EXT4_SB(sb);
2136 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2137 sbi->s_ext_blocks, sbi->s_ext_extents,
2138 sbi->s_ext_blocks / sbi->s_ext_extents);
2139 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2140 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2141 }
2142#endif
2143}
2144
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002145static void bi_complete(struct bio *bio, int error)
2146{
2147 complete((struct completion *)bio->bi_private);
2148}
2149
2150/* FIXME!! we need to try to merge to left or right after zero-out */
2151static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2152{
2153 int ret = -EIO;
2154 struct bio *bio;
2155 int blkbits, blocksize;
2156 sector_t ee_pblock;
2157 struct completion event;
2158 unsigned int ee_len, len, done, offset;
2159
2160
2161 blkbits = inode->i_blkbits;
2162 blocksize = inode->i_sb->s_blocksize;
2163 ee_len = ext4_ext_get_actual_len(ex);
2164 ee_pblock = ext_pblock(ex);
2165
2166 /* convert ee_pblock to 512 byte sectors */
2167 ee_pblock = ee_pblock << (blkbits - 9);
2168
2169 while (ee_len > 0) {
2170
2171 if (ee_len > BIO_MAX_PAGES)
2172 len = BIO_MAX_PAGES;
2173 else
2174 len = ee_len;
2175
2176 bio = bio_alloc(GFP_NOIO, len);
2177 if (!bio)
2178 return -ENOMEM;
2179 bio->bi_sector = ee_pblock;
2180 bio->bi_bdev = inode->i_sb->s_bdev;
2181
2182 done = 0;
2183 offset = 0;
2184 while (done < len) {
2185 ret = bio_add_page(bio, ZERO_PAGE(0),
2186 blocksize, offset);
2187 if (ret != blocksize) {
2188 /*
2189 * We can't add any more pages because of
2190 * hardware limitations. Start a new bio.
2191 */
2192 break;
2193 }
2194 done++;
2195 offset += blocksize;
2196 if (offset >= PAGE_CACHE_SIZE)
2197 offset = 0;
2198 }
2199
2200 init_completion(&event);
2201 bio->bi_private = &event;
2202 bio->bi_end_io = bi_complete;
2203 submit_bio(WRITE, bio);
2204 wait_for_completion(&event);
2205
2206 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
2207 ret = 0;
2208 else {
2209 ret = -EIO;
2210 break;
2211 }
2212 bio_put(bio);
2213 ee_len -= done;
2214 ee_pblock += done << (blkbits - 9);
2215 }
2216 return ret;
2217}
2218
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002219#define EXT4_EXT_ZERO_LEN 7
2220
Amit Arora56055d32007-07-17 21:42:38 -04002221/*
2222 * This function is called by ext4_ext_get_blocks() if someone tries to write
2223 * to an uninitialized extent. It may result in splitting the uninitialized
2224 * extent into multiple extents (upto three - one initialized and two
2225 * uninitialized).
2226 * There are three possibilities:
2227 * a> There is no split required: Entire extent should be initialized
2228 * b> Splits in two extents: Write is happening at either end of the extent
2229 * c> Splits in three extents: Somone is writing in middle of the extent
2230 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002231static int ext4_ext_convert_to_initialized(handle_t *handle,
2232 struct inode *inode,
2233 struct ext4_ext_path *path,
2234 ext4_lblk_t iblock,
2235 unsigned long max_blocks)
Amit Arora56055d32007-07-17 21:42:38 -04002236{
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002237 struct ext4_extent *ex, newex, orig_ex;
Amit Arora56055d32007-07-17 21:42:38 -04002238 struct ext4_extent *ex1 = NULL;
2239 struct ext4_extent *ex2 = NULL;
2240 struct ext4_extent *ex3 = NULL;
2241 struct ext4_extent_header *eh;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002242 ext4_lblk_t ee_block;
2243 unsigned int allocated, ee_len, depth;
Amit Arora56055d32007-07-17 21:42:38 -04002244 ext4_fsblk_t newblock;
2245 int err = 0;
2246 int ret = 0;
2247
2248 depth = ext_depth(inode);
2249 eh = path[depth].p_hdr;
2250 ex = path[depth].p_ext;
2251 ee_block = le32_to_cpu(ex->ee_block);
2252 ee_len = ext4_ext_get_actual_len(ex);
2253 allocated = ee_len - (iblock - ee_block);
2254 newblock = iblock - ee_block + ext_pblock(ex);
2255 ex2 = ex;
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002256 orig_ex.ee_block = ex->ee_block;
2257 orig_ex.ee_len = cpu_to_le16(ee_len);
2258 ext4_ext_store_pblock(&orig_ex, ext_pblock(ex));
Amit Arora56055d32007-07-17 21:42:38 -04002259
Aneesh Kumar K.V9df56432008-02-22 06:17:31 -05002260 err = ext4_ext_get_access(handle, inode, path + depth);
2261 if (err)
2262 goto out;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002263 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
2264 if (ee_len <= 2*EXT4_EXT_ZERO_LEN) {
2265 err = ext4_ext_zeroout(inode, &orig_ex);
2266 if (err)
2267 goto fix_extent_len;
2268 /* update the extent length and mark as initialized */
2269 ex->ee_block = orig_ex.ee_block;
2270 ex->ee_len = orig_ex.ee_len;
2271 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2272 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002273 /* zeroed the full extent */
2274 return allocated;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002275 }
Aneesh Kumar K.V9df56432008-02-22 06:17:31 -05002276
Amit Arora56055d32007-07-17 21:42:38 -04002277 /* ex1: ee_block to iblock - 1 : uninitialized */
2278 if (iblock > ee_block) {
2279 ex1 = ex;
2280 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2281 ext4_ext_mark_uninitialized(ex1);
2282 ex2 = &newex;
2283 }
2284 /*
2285 * for sanity, update the length of the ex2 extent before
2286 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2287 * overlap of blocks.
2288 */
2289 if (!ex1 && allocated > max_blocks)
2290 ex2->ee_len = cpu_to_le16(max_blocks);
2291 /* ex3: to ee_block + ee_len : uninitialised */
2292 if (allocated > max_blocks) {
2293 unsigned int newdepth;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002294 /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */
2295 if (allocated <= EXT4_EXT_ZERO_LEN) {
2296 /* Mark first half uninitialized.
2297 * Mark second half initialized and zero out the
2298 * initialized extent
2299 */
2300 ex->ee_block = orig_ex.ee_block;
2301 ex->ee_len = cpu_to_le16(ee_len - allocated);
2302 ext4_ext_mark_uninitialized(ex);
2303 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2304 ext4_ext_dirty(handle, inode, path + depth);
2305
2306 ex3 = &newex;
2307 ex3->ee_block = cpu_to_le32(iblock);
2308 ext4_ext_store_pblock(ex3, newblock);
2309 ex3->ee_len = cpu_to_le16(allocated);
2310 err = ext4_ext_insert_extent(handle, inode, path, ex3);
2311 if (err == -ENOSPC) {
2312 err = ext4_ext_zeroout(inode, &orig_ex);
2313 if (err)
2314 goto fix_extent_len;
2315 ex->ee_block = orig_ex.ee_block;
2316 ex->ee_len = orig_ex.ee_len;
2317 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2318 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002319 /* zeroed the full extent */
2320 return allocated;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002321
2322 } else if (err)
2323 goto fix_extent_len;
2324
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002325 /*
2326 * We need to zero out the second half because
2327 * an fallocate request can update file size and
2328 * converting the second half to initialized extent
2329 * implies that we can leak some junk data to user
2330 * space.
2331 */
2332 err = ext4_ext_zeroout(inode, ex3);
2333 if (err) {
2334 /*
2335 * We should actually mark the
2336 * second half as uninit and return error
2337 * Insert would have changed the extent
2338 */
2339 depth = ext_depth(inode);
2340 ext4_ext_drop_refs(path);
2341 path = ext4_ext_find_extent(inode,
2342 iblock, path);
2343 if (IS_ERR(path)) {
2344 err = PTR_ERR(path);
2345 return err;
2346 }
2347 ex = path[depth].p_ext;
2348 err = ext4_ext_get_access(handle, inode,
2349 path + depth);
2350 if (err)
2351 return err;
2352 ext4_ext_mark_uninitialized(ex);
2353 ext4_ext_dirty(handle, inode, path + depth);
2354 return err;
2355 }
2356
2357 /* zeroed the second half */
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002358 return allocated;
2359 }
Amit Arora56055d32007-07-17 21:42:38 -04002360 ex3 = &newex;
2361 ex3->ee_block = cpu_to_le32(iblock + max_blocks);
2362 ext4_ext_store_pblock(ex3, newblock + max_blocks);
2363 ex3->ee_len = cpu_to_le16(allocated - max_blocks);
2364 ext4_ext_mark_uninitialized(ex3);
2365 err = ext4_ext_insert_extent(handle, inode, path, ex3);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002366 if (err == -ENOSPC) {
2367 err = ext4_ext_zeroout(inode, &orig_ex);
2368 if (err)
2369 goto fix_extent_len;
2370 /* update the extent length and mark as initialized */
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002371 ex->ee_block = orig_ex.ee_block;
2372 ex->ee_len = orig_ex.ee_len;
2373 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002374 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002375 /* zeroed the full extent */
2376 return allocated;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002377
2378 } else if (err)
2379 goto fix_extent_len;
Amit Arora56055d32007-07-17 21:42:38 -04002380 /*
2381 * The depth, and hence eh & ex might change
2382 * as part of the insert above.
2383 */
2384 newdepth = ext_depth(inode);
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002385 /*
2386 * update the extent length after successfull insert of the
2387 * split extent
2388 */
2389 orig_ex.ee_len = cpu_to_le16(ee_len -
2390 ext4_ext_get_actual_len(ex3));
Amit Arora56055d32007-07-17 21:42:38 -04002391 if (newdepth != depth) {
2392 depth = newdepth;
Aneesh Kumar K.Vb35905c2008-02-25 16:54:37 -05002393 ext4_ext_drop_refs(path);
2394 path = ext4_ext_find_extent(inode, iblock, path);
Amit Arora56055d32007-07-17 21:42:38 -04002395 if (IS_ERR(path)) {
2396 err = PTR_ERR(path);
Amit Arora56055d32007-07-17 21:42:38 -04002397 goto out;
2398 }
2399 eh = path[depth].p_hdr;
2400 ex = path[depth].p_ext;
2401 if (ex2 != &newex)
2402 ex2 = ex;
Aneesh Kumar K.V9df56432008-02-22 06:17:31 -05002403
2404 err = ext4_ext_get_access(handle, inode, path + depth);
2405 if (err)
2406 goto out;
Amit Arora56055d32007-07-17 21:42:38 -04002407 }
2408 allocated = max_blocks;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002409
2410 /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying
2411 * to insert a extent in the middle zerout directly
2412 * otherwise give the extent a chance to merge to left
2413 */
2414 if (le16_to_cpu(orig_ex.ee_len) <= EXT4_EXT_ZERO_LEN &&
2415 iblock != ee_block) {
2416 err = ext4_ext_zeroout(inode, &orig_ex);
2417 if (err)
2418 goto fix_extent_len;
2419 /* update the extent length and mark as initialized */
2420 ex->ee_block = orig_ex.ee_block;
2421 ex->ee_len = orig_ex.ee_len;
2422 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2423 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002424 /* zero out the first half */
2425 return allocated;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002426 }
Amit Arora56055d32007-07-17 21:42:38 -04002427 }
2428 /*
2429 * If there was a change of depth as part of the
2430 * insertion of ex3 above, we need to update the length
2431 * of the ex1 extent again here
2432 */
2433 if (ex1 && ex1 != ex) {
2434 ex1 = ex;
2435 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2436 ext4_ext_mark_uninitialized(ex1);
2437 ex2 = &newex;
2438 }
2439 /* ex2: iblock to iblock + maxblocks-1 : initialised */
2440 ex2->ee_block = cpu_to_le32(iblock);
Amit Arora56055d32007-07-17 21:42:38 -04002441 ext4_ext_store_pblock(ex2, newblock);
2442 ex2->ee_len = cpu_to_le16(allocated);
2443 if (ex2 != ex)
2444 goto insert;
Amit Arora56055d32007-07-17 21:42:38 -04002445 /*
2446 * New (initialized) extent starts from the first block
2447 * in the current extent. i.e., ex2 == ex
2448 * We have to see if it can be merged with the extent
2449 * on the left.
2450 */
2451 if (ex2 > EXT_FIRST_EXTENT(eh)) {
2452 /*
2453 * To merge left, pass "ex2 - 1" to try_to_merge(),
2454 * since it merges towards right _only_.
2455 */
2456 ret = ext4_ext_try_to_merge(inode, path, ex2 - 1);
2457 if (ret) {
2458 err = ext4_ext_correct_indexes(handle, inode, path);
2459 if (err)
2460 goto out;
2461 depth = ext_depth(inode);
2462 ex2--;
2463 }
2464 }
2465 /*
2466 * Try to Merge towards right. This might be required
2467 * only when the whole extent is being written to.
2468 * i.e. ex2 == ex and ex3 == NULL.
2469 */
2470 if (!ex3) {
2471 ret = ext4_ext_try_to_merge(inode, path, ex2);
2472 if (ret) {
2473 err = ext4_ext_correct_indexes(handle, inode, path);
2474 if (err)
2475 goto out;
2476 }
2477 }
2478 /* Mark modified extent as dirty */
2479 err = ext4_ext_dirty(handle, inode, path + depth);
2480 goto out;
2481insert:
2482 err = ext4_ext_insert_extent(handle, inode, path, &newex);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002483 if (err == -ENOSPC) {
2484 err = ext4_ext_zeroout(inode, &orig_ex);
2485 if (err)
2486 goto fix_extent_len;
2487 /* update the extent length and mark as initialized */
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002488 ex->ee_block = orig_ex.ee_block;
2489 ex->ee_len = orig_ex.ee_len;
2490 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002491 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002492 /* zero out the first half */
2493 return allocated;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002494 } else if (err)
2495 goto fix_extent_len;
Amit Arora56055d32007-07-17 21:42:38 -04002496out:
2497 return err ? err : allocated;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002498
2499fix_extent_len:
2500 ex->ee_block = orig_ex.ee_block;
2501 ex->ee_len = orig_ex.ee_len;
2502 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2503 ext4_ext_mark_uninitialized(ex);
2504 ext4_ext_dirty(handle, inode, path + depth);
2505 return err;
Amit Arora56055d32007-07-17 21:42:38 -04002506}
2507
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05002508/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05002509 * Block allocation/map/preallocation routine for extents based files
2510 *
2511 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05002512 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05002513 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
2514 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05002515 *
2516 * return > 0, number of of blocks already mapped/allocated
2517 * if create == 0 and these are pre-allocated blocks
2518 * buffer head is unmapped
2519 * otherwise blocks are mapped
2520 *
2521 * return = 0, if plain look up failed (blocks have not been allocated)
2522 * buffer head is unmapped
2523 *
2524 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05002525 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002526int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002527 ext4_lblk_t iblock,
Alex Tomasa86c6182006-10-11 01:21:03 -07002528 unsigned long max_blocks, struct buffer_head *bh_result,
2529 int create, int extend_disksize)
2530{
2531 struct ext4_ext_path *path = NULL;
Amit Arora56055d32007-07-17 21:42:38 -04002532 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002533 struct ext4_extent newex, *ex;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002534 ext4_fsblk_t goal, newblock;
Amit Arora56055d32007-07-17 21:42:38 -04002535 int err = 0, depth, ret;
Alex Tomasa86c6182006-10-11 01:21:03 -07002536 unsigned long allocated = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002537 struct ext4_allocation_request ar;
Alex Tomasa86c6182006-10-11 01:21:03 -07002538
2539 __clear_bit(BH_New, &bh_result->b_state);
Eric Sandeenbba90742008-01-28 23:58:27 -05002540 ext_debug("blocks %u/%lu requested for inode %u\n",
2541 iblock, max_blocks, inode->i_ino);
Alex Tomasa86c6182006-10-11 01:21:03 -07002542
2543 /* check in cache */
Avantika Mathur7e028972006-12-06 20:41:33 -08002544 goal = ext4_ext_in_cache(inode, iblock, &newex);
2545 if (goal) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002546 if (goal == EXT4_EXT_CACHE_GAP) {
2547 if (!create) {
Amit Arora56055d32007-07-17 21:42:38 -04002548 /*
2549 * block isn't allocated yet and
2550 * user doesn't want to allocate it
2551 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002552 goto out2;
2553 }
2554 /* we should allocate requested block */
2555 } else if (goal == EXT4_EXT_CACHE_EXTENT) {
2556 /* block is already allocated */
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002557 newblock = iblock
2558 - le32_to_cpu(newex.ee_block)
2559 + ext_pblock(&newex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002560 /* number of remaining blocks in the extent */
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05002561 allocated = ext4_ext_get_actual_len(&newex) -
Alex Tomasa86c6182006-10-11 01:21:03 -07002562 (iblock - le32_to_cpu(newex.ee_block));
2563 goto out;
2564 } else {
2565 BUG();
2566 }
2567 }
2568
2569 /* find extent for this block */
2570 path = ext4_ext_find_extent(inode, iblock, NULL);
2571 if (IS_ERR(path)) {
2572 err = PTR_ERR(path);
2573 path = NULL;
2574 goto out2;
2575 }
2576
2577 depth = ext_depth(inode);
2578
2579 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002580 * consistent leaf must not be empty;
2581 * this situation is possible, though, _during_ tree modification;
Alex Tomasa86c6182006-10-11 01:21:03 -07002582 * this is why assert can't be put in ext4_ext_find_extent()
2583 */
2584 BUG_ON(path[depth].p_ext == NULL && depth != 0);
Amit Arora56055d32007-07-17 21:42:38 -04002585 eh = path[depth].p_hdr;
Alex Tomasa86c6182006-10-11 01:21:03 -07002586
Avantika Mathur7e028972006-12-06 20:41:33 -08002587 ex = path[depth].p_ext;
2588 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002589 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002590 ext4_fsblk_t ee_start = ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002591 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07002592
2593 /*
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07002594 * Uninitialized extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04002595 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07002596 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04002597 ee_len = ext4_ext_get_actual_len(ex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002598 /* if found extent covers block, simply return it */
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002599 if (iblock >= ee_block && iblock < ee_block + ee_len) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002600 newblock = iblock - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002601 /* number of remaining blocks in the extent */
Alex Tomasa86c6182006-10-11 01:21:03 -07002602 allocated = ee_len - (iblock - ee_block);
Eric Sandeenbba90742008-01-28 23:58:27 -05002603 ext_debug("%u fit into %lu:%d -> %llu\n", iblock,
Alex Tomasa86c6182006-10-11 01:21:03 -07002604 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04002605
Amit Aroraa2df2a62007-07-17 21:42:41 -04002606 /* Do not put uninitialized extent in the cache */
Amit Arora56055d32007-07-17 21:42:38 -04002607 if (!ext4_ext_is_uninitialized(ex)) {
Amit Aroraa2df2a62007-07-17 21:42:41 -04002608 ext4_ext_put_in_cache(inode, ee_block,
2609 ee_len, ee_start,
2610 EXT4_EXT_CACHE_EXTENT);
Amit Arora56055d32007-07-17 21:42:38 -04002611 goto out;
2612 }
2613 if (create == EXT4_CREATE_UNINITIALIZED_EXT)
2614 goto out;
Aneesh Kumar K.Ve067ba02008-04-29 08:11:12 -04002615 if (!create) {
2616 /*
2617 * We have blocks reserved already. We
2618 * return allocated blocks so that delalloc
2619 * won't do block reservation for us. But
2620 * the buffer head will be unmapped so that
2621 * a read from the block returns 0s.
2622 */
2623 if (allocated > max_blocks)
2624 allocated = max_blocks;
Eric Sandeen953e6222008-07-11 19:27:31 -04002625 set_buffer_unwritten(bh_result);
Amit Arora56055d32007-07-17 21:42:38 -04002626 goto out2;
Aneesh Kumar K.Ve067ba02008-04-29 08:11:12 -04002627 }
Amit Arora56055d32007-07-17 21:42:38 -04002628
2629 ret = ext4_ext_convert_to_initialized(handle, inode,
2630 path, iblock,
2631 max_blocks);
Dmitry Monakhovdbf9d7d2008-01-28 23:58:27 -05002632 if (ret <= 0) {
2633 err = ret;
Amit Arora56055d32007-07-17 21:42:38 -04002634 goto out2;
Dmitry Monakhovdbf9d7d2008-01-28 23:58:27 -05002635 } else
Amit Arora56055d32007-07-17 21:42:38 -04002636 allocated = ret;
2637 goto outnew;
Alex Tomasa86c6182006-10-11 01:21:03 -07002638 }
2639 }
2640
2641 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002642 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07002643 * we couldn't try to create block if create flag is zero
2644 */
2645 if (!create) {
Amit Arora56055d32007-07-17 21:42:38 -04002646 /*
2647 * put just found gap into cache to speed up
2648 * subsequent requests
2649 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002650 ext4_ext_put_gap_in_cache(inode, path, iblock);
2651 goto out2;
2652 }
2653 /*
Andrew Morton63f57932006-10-11 01:21:24 -07002654 * Okay, we need to do block allocation. Lazily initialize the block
2655 * allocation info here if necessary.
2656 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002657 if (S_ISREG(inode->i_mode) && (!EXT4_I(inode)->i_block_alloc_info))
2658 ext4_init_block_alloc_info(inode);
2659
Alex Tomasc9de5602008-01-29 00:19:52 -05002660 /* find neighbour allocated blocks */
2661 ar.lleft = iblock;
2662 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
2663 if (err)
2664 goto out2;
2665 ar.lright = iblock;
2666 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright);
2667 if (err)
2668 goto out2;
Amit Arora25d14f92007-05-24 13:04:13 -04002669
Amit Arora749269f2007-07-18 09:02:56 -04002670 /*
2671 * See if request is beyond maximum number of blocks we can have in
2672 * a single extent. For an initialized extent this limit is
2673 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
2674 * EXT_UNINIT_MAX_LEN.
2675 */
2676 if (max_blocks > EXT_INIT_MAX_LEN &&
2677 create != EXT4_CREATE_UNINITIALIZED_EXT)
2678 max_blocks = EXT_INIT_MAX_LEN;
2679 else if (max_blocks > EXT_UNINIT_MAX_LEN &&
2680 create == EXT4_CREATE_UNINITIALIZED_EXT)
2681 max_blocks = EXT_UNINIT_MAX_LEN;
2682
Amit Arora25d14f92007-05-24 13:04:13 -04002683 /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */
2684 newex.ee_block = cpu_to_le32(iblock);
2685 newex.ee_len = cpu_to_le16(max_blocks);
2686 err = ext4_ext_check_overlap(inode, &newex, path);
2687 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05002688 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04002689 else
2690 allocated = max_blocks;
Alex Tomasc9de5602008-01-29 00:19:52 -05002691
2692 /* allocate new block */
2693 ar.inode = inode;
2694 ar.goal = ext4_ext_find_goal(inode, path, iblock);
2695 ar.logical = iblock;
2696 ar.len = allocated;
2697 if (S_ISREG(inode->i_mode))
2698 ar.flags = EXT4_MB_HINT_DATA;
2699 else
2700 /* disable in-core preallocation for non-regular files */
2701 ar.flags = 0;
2702 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07002703 if (!newblock)
2704 goto out2;
Mingming Cao2ae02102006-10-11 01:21:11 -07002705 ext_debug("allocate new block: goal %llu, found %llu/%lu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07002706 goal, newblock, allocated);
2707
2708 /* try to insert new extent into found leaf and return */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002709 ext4_ext_store_pblock(&newex, newblock);
Alex Tomasc9de5602008-01-29 00:19:52 -05002710 newex.ee_len = cpu_to_le16(ar.len);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002711 if (create == EXT4_CREATE_UNINITIALIZED_EXT) /* Mark uninitialized */
2712 ext4_ext_mark_uninitialized(&newex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002713 err = ext4_ext_insert_extent(handle, inode, path, &newex);
Alex Tomas315054f2007-05-24 13:04:25 -04002714 if (err) {
2715 /* free data blocks we just allocated */
Alex Tomasc9de5602008-01-29 00:19:52 -05002716 /* not a good idea to call discard here directly,
2717 * but otherwise we'd need to call it every free() */
2718 ext4_mb_discard_inode_preallocations(inode);
Alex Tomas315054f2007-05-24 13:04:25 -04002719 ext4_free_blocks(handle, inode, ext_pblock(&newex),
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05002720 ext4_ext_get_actual_len(&newex), 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002721 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04002722 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002723
Alex Tomasa86c6182006-10-11 01:21:03 -07002724 /* previous routine could use block we allocated */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002725 newblock = ext_pblock(&newex);
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05002726 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora56055d32007-07-17 21:42:38 -04002727outnew:
Aneesh Kumar K.Va379cd12008-07-11 19:27:31 -04002728 if (extend_disksize && inode->i_size > EXT4_I(inode)->i_disksize)
2729 EXT4_I(inode)->i_disksize = inode->i_size;
2730
Eric Sandeen953e6222008-07-11 19:27:31 -04002731 set_buffer_new(bh_result);
Alex Tomasa86c6182006-10-11 01:21:03 -07002732
Amit Aroraa2df2a62007-07-17 21:42:41 -04002733 /* Cache only when it is _not_ an uninitialized extent */
2734 if (create != EXT4_CREATE_UNINITIALIZED_EXT)
2735 ext4_ext_put_in_cache(inode, iblock, allocated, newblock,
2736 EXT4_EXT_CACHE_EXTENT);
Alex Tomasa86c6182006-10-11 01:21:03 -07002737out:
2738 if (allocated > max_blocks)
2739 allocated = max_blocks;
2740 ext4_ext_show_leaf(inode, path);
Eric Sandeen953e6222008-07-11 19:27:31 -04002741 set_buffer_mapped(bh_result);
Alex Tomasa86c6182006-10-11 01:21:03 -07002742 bh_result->b_bdev = inode->i_sb->s_bdev;
2743 bh_result->b_blocknr = newblock;
2744out2:
2745 if (path) {
2746 ext4_ext_drop_refs(path);
2747 kfree(path);
2748 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002749 return err ? err : allocated;
2750}
2751
Jan Karacf108bc2008-07-11 19:27:31 -04002752void ext4_ext_truncate(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07002753{
2754 struct address_space *mapping = inode->i_mapping;
2755 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002756 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002757 handle_t *handle;
2758 int err = 0;
2759
2760 /*
2761 * probably first extent we're gonna free will be last in block
2762 */
2763 err = ext4_writepage_trans_blocks(inode) + 3;
2764 handle = ext4_journal_start(inode, err);
Jan Karacf108bc2008-07-11 19:27:31 -04002765 if (IS_ERR(handle))
Alex Tomasa86c6182006-10-11 01:21:03 -07002766 return;
Alex Tomasa86c6182006-10-11 01:21:03 -07002767
Jan Karacf108bc2008-07-11 19:27:31 -04002768 if (inode->i_size & (sb->s_blocksize - 1))
2769 ext4_block_truncate_page(handle, mapping, inode->i_size);
Alex Tomasa86c6182006-10-11 01:21:03 -07002770
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05002771 down_write(&EXT4_I(inode)->i_data_sem);
Alex Tomasa86c6182006-10-11 01:21:03 -07002772 ext4_ext_invalidate_cache(inode);
2773
Alex Tomasc9de5602008-01-29 00:19:52 -05002774 ext4_mb_discard_inode_preallocations(inode);
2775
Alex Tomasa86c6182006-10-11 01:21:03 -07002776 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002777 * TODO: optimization is possible here.
2778 * Probably we need not scan at all,
2779 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07002780 */
2781 if (ext4_orphan_add(handle, inode))
2782 goto out_stop;
2783
2784 /* we have to know where to truncate from in crash case */
2785 EXT4_I(inode)->i_disksize = inode->i_size;
2786 ext4_mark_inode_dirty(handle, inode);
2787
2788 last_block = (inode->i_size + sb->s_blocksize - 1)
2789 >> EXT4_BLOCK_SIZE_BITS(sb);
2790 err = ext4_ext_remove_space(inode, last_block);
2791
2792 /* In a multi-transaction truncate, we only make the final
Amit Arora56055d32007-07-17 21:42:38 -04002793 * transaction synchronous.
2794 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002795 if (IS_SYNC(inode))
2796 handle->h_sync = 1;
2797
2798out_stop:
2799 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002800 * If this was a simple ftruncate() and the file will remain alive,
Alex Tomasa86c6182006-10-11 01:21:03 -07002801 * then we need to clear up the orphan record which we created above.
2802 * However, if this was a real unlink then we were called by
2803 * ext4_delete_inode(), and we allow that function to clean up the
2804 * orphan info for us.
2805 */
2806 if (inode->i_nlink)
2807 ext4_orphan_del(handle, inode);
2808
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05002809 up_write(&EXT4_I(inode)->i_data_sem);
Solofo Ramangalahyef737722008-04-29 22:00:41 -04002810 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
2811 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002812 ext4_journal_stop(handle);
2813}
2814
2815/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002816 * ext4_ext_writepage_trans_blocks:
2817 * calculate max number of blocks we could modify
Alex Tomasa86c6182006-10-11 01:21:03 -07002818 * in order to allocate new block for an inode
2819 */
2820int ext4_ext_writepage_trans_blocks(struct inode *inode, int num)
2821{
2822 int needed;
2823
2824 needed = ext4_ext_calc_credits_for_insert(inode, NULL);
2825
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002826 /* caller wants to allocate num blocks, but note it includes sb */
Alex Tomasa86c6182006-10-11 01:21:03 -07002827 needed = needed * num - (num - 1);
2828
2829#ifdef CONFIG_QUOTA
2830 needed += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
2831#endif
2832
2833 return needed;
2834}
Amit Aroraa2df2a62007-07-17 21:42:41 -04002835
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04002836static void ext4_falloc_update_inode(struct inode *inode,
2837 int mode, loff_t new_size, int update_ctime)
2838{
2839 struct timespec now;
2840
2841 if (update_ctime) {
2842 now = current_fs_time(inode->i_sb);
2843 if (!timespec_equal(&inode->i_ctime, &now))
2844 inode->i_ctime = now;
2845 }
2846 /*
2847 * Update only when preallocation was requested beyond
2848 * the file size.
2849 */
2850 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
2851 new_size > i_size_read(inode)) {
2852 i_size_write(inode, new_size);
2853 EXT4_I(inode)->i_disksize = new_size;
2854 }
2855
2856}
2857
Amit Aroraa2df2a62007-07-17 21:42:41 -04002858/*
2859 * preallocate space for a file. This implements ext4's fallocate inode
2860 * operation, which gets called from sys_fallocate system call.
2861 * For block-mapped files, posix_fallocate should fall back to the method
2862 * of writing zeroes to the required new blocks (the same behavior which is
2863 * expected for file systems which do not support fallocate() system call).
2864 */
2865long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
2866{
2867 handle_t *handle;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002868 ext4_lblk_t block;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04002869 loff_t new_size;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002870 unsigned long max_blocks;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002871 int ret = 0;
2872 int ret2 = 0;
2873 int retries = 0;
2874 struct buffer_head map_bh;
2875 unsigned int credits, blkbits = inode->i_blkbits;
2876
2877 /*
2878 * currently supporting (pre)allocate mode for extent-based
2879 * files _only_
2880 */
2881 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
2882 return -EOPNOTSUPP;
2883
2884 /* preallocation to directories is currently not supported */
2885 if (S_ISDIR(inode->i_mode))
2886 return -ENODEV;
2887
2888 block = offset >> blkbits;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04002889 /*
2890 * We can't just convert len to max_blocks because
2891 * If blocksize = 4096 offset = 3072 and len = 2048
2892 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04002893 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04002894 - block;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002895 /*
2896 * credits to insert 1 extent into extent tree + buffers to be able to
2897 * modify 1 super block, 1 block bitmap and 1 group descriptor.
2898 */
2899 credits = EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + 3;
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05002900 mutex_lock(&inode->i_mutex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002901retry:
2902 while (ret >= 0 && ret < max_blocks) {
2903 block = block + ret;
2904 max_blocks = max_blocks - ret;
2905 handle = ext4_journal_start(inode, credits);
2906 if (IS_ERR(handle)) {
2907 ret = PTR_ERR(handle);
2908 break;
2909 }
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05002910 ret = ext4_get_blocks_wrap(handle, inode, block,
Amit Aroraa2df2a62007-07-17 21:42:41 -04002911 max_blocks, &map_bh,
2912 EXT4_CREATE_UNINITIALIZED_EXT, 0);
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05002913 if (ret <= 0) {
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05002914#ifdef EXT4FS_DEBUG
2915 WARN_ON(ret <= 0);
2916 printk(KERN_ERR "%s: ext4_ext_get_blocks "
2917 "returned error inode#%lu, block=%u, "
2918 "max_blocks=%lu", __func__,
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05002919 inode->i_ino, block, max_blocks);
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05002920#endif
Amit Aroraa2df2a62007-07-17 21:42:41 -04002921 ext4_mark_inode_dirty(handle, inode);
2922 ret2 = ext4_journal_stop(handle);
2923 break;
2924 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04002925 if ((block + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
2926 blkbits) >> blkbits))
2927 new_size = offset + len;
2928 else
2929 new_size = (block + ret) << blkbits;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002930
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04002931 ext4_falloc_update_inode(inode, mode, new_size,
2932 buffer_new(&map_bh));
Amit Aroraa2df2a62007-07-17 21:42:41 -04002933 ext4_mark_inode_dirty(handle, inode);
2934 ret2 = ext4_journal_stop(handle);
2935 if (ret2)
2936 break;
2937 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04002938 if (ret == -ENOSPC &&
2939 ext4_should_retry_alloc(inode->i_sb, &retries)) {
2940 ret = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002941 goto retry;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002942 }
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05002943 mutex_unlock(&inode->i_mutex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002944 return ret > 0 ? ret2 : ret;
2945}