blob: 19d8059b58aad7db2dc2f80f2d56acd4b0efef62 [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>
35#include <linux/ext4_jbd2.h>
Mingming Caocd02ff02007-10-16 18:38:25 -040036#include <linux/jbd2.h>
Alex Tomasa86c6182006-10-11 01:21:03 -070037#include <linux/highuid.h>
38#include <linux/pagemap.h>
39#include <linux/quotaops.h>
40#include <linux/string.h>
41#include <linux/slab.h>
Amit Aroraa2df2a62007-07-17 21:42:41 -040042#include <linux/falloc.h>
Alex Tomasa86c6182006-10-11 01:21:03 -070043#include <linux/ext4_fs_extents.h>
44#include <asm/uaccess.h>
45
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 */
Avantika Mathur09b88252006-12-06 20:41:36 -080064static ext4_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 */
Avantika Mathur09b88252006-12-06 20:41:36 -080078static void 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
Alex Tomasa86c6182006-10-11 01:21:03 -070095static handle_t *ext4_ext_journal_restart(handle_t *handle, int needed)
96{
97 int err;
98
99 if (handle->h_buffer_credits > needed)
100 return handle;
101 if (!ext4_journal_extend(handle, needed))
102 return handle;
103 err = ext4_journal_restart(handle, needed);
104
105 return handle;
106}
107
108/*
109 * could return:
110 * - EROFS
111 * - ENOMEM
112 */
113static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
114 struct ext4_ext_path *path)
115{
116 if (path->p_bh) {
117 /* path points to block */
118 return ext4_journal_get_write_access(handle, path->p_bh);
119 }
120 /* path points to leaf/index in inode body */
121 /* we use in-core data, no need to protect them */
122 return 0;
123}
124
125/*
126 * could return:
127 * - EROFS
128 * - ENOMEM
129 * - EIO
130 */
131static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
132 struct ext4_ext_path *path)
133{
134 int err;
135 if (path->p_bh) {
136 /* path points to block */
137 err = ext4_journal_dirty_metadata(handle, path->p_bh);
138 } else {
139 /* path points to leaf/index in inode body */
140 err = ext4_mark_inode_dirty(handle, inode);
141 }
142 return err;
143}
144
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700145static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700146 struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500147 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700148{
149 struct ext4_inode_info *ei = EXT4_I(inode);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700150 ext4_fsblk_t bg_start;
151 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);
172 colour = (current->pid % 16) *
173 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
174 return bg_start + colour + block;
175}
176
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700177static ext4_fsblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -0700178ext4_ext_new_block(handle_t *handle, struct inode *inode,
179 struct ext4_ext_path *path,
180 struct ext4_extent *ex, int *err)
181{
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700182 ext4_fsblk_t goal, newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700183
184 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
185 newblock = ext4_new_block(handle, inode, goal, err);
186 return newblock;
187}
188
Avantika Mathur09b88252006-12-06 20:41:36 -0800189static int ext4_ext_space_block(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -0700190{
191 int size;
192
193 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
194 / sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100195#ifdef AGGRESSIVE_TEST
Alex Tomasa86c6182006-10-11 01:21:03 -0700196 if (size > 6)
197 size = 6;
198#endif
199 return size;
200}
201
Avantika Mathur09b88252006-12-06 20:41:36 -0800202static int ext4_ext_space_block_idx(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -0700203{
204 int size;
205
206 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
207 / sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100208#ifdef AGGRESSIVE_TEST
Alex Tomasa86c6182006-10-11 01:21:03 -0700209 if (size > 5)
210 size = 5;
211#endif
212 return size;
213}
214
Avantika Mathur09b88252006-12-06 20:41:36 -0800215static int ext4_ext_space_root(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -0700216{
217 int size;
218
219 size = sizeof(EXT4_I(inode)->i_data);
220 size -= sizeof(struct ext4_extent_header);
221 size /= sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100222#ifdef AGGRESSIVE_TEST
Alex Tomasa86c6182006-10-11 01:21:03 -0700223 if (size > 3)
224 size = 3;
225#endif
226 return size;
227}
228
Avantika Mathur09b88252006-12-06 20:41:36 -0800229static int ext4_ext_space_root_idx(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -0700230{
231 int size;
232
233 size = sizeof(EXT4_I(inode)->i_data);
234 size -= sizeof(struct ext4_extent_header);
235 size /= sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100236#ifdef AGGRESSIVE_TEST
Alex Tomasa86c6182006-10-11 01:21:03 -0700237 if (size > 4)
238 size = 4;
239#endif
240 return size;
241}
242
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400243static int
244ext4_ext_max_entries(struct inode *inode, int depth)
245{
246 int max;
247
248 if (depth == ext_depth(inode)) {
249 if (depth == 0)
250 max = ext4_ext_space_root(inode);
251 else
252 max = ext4_ext_space_root_idx(inode);
253 } else {
254 if (depth == 0)
255 max = ext4_ext_space_block(inode);
256 else
257 max = ext4_ext_space_block_idx(inode);
258 }
259
260 return max;
261}
262
263static int __ext4_ext_check_header(const char *function, struct inode *inode,
264 struct ext4_extent_header *eh,
265 int depth)
266{
267 const char *error_msg;
268 int max = 0;
269
270 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
271 error_msg = "invalid magic";
272 goto corrupted;
273 }
274 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
275 error_msg = "unexpected eh_depth";
276 goto corrupted;
277 }
278 if (unlikely(eh->eh_max == 0)) {
279 error_msg = "invalid eh_max";
280 goto corrupted;
281 }
282 max = ext4_ext_max_entries(inode, depth);
283 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
284 error_msg = "too large eh_max";
285 goto corrupted;
286 }
287 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
288 error_msg = "invalid eh_entries";
289 goto corrupted;
290 }
291 return 0;
292
293corrupted:
294 ext4_error(inode->i_sb, function,
295 "bad header in inode #%lu: %s - magic %x, "
296 "entries %u, max %u(%u), depth %u(%u)",
297 inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic),
298 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
299 max, le16_to_cpu(eh->eh_depth), depth);
300
301 return -EIO;
302}
303
304#define ext4_ext_check_header(inode, eh, depth) \
305 __ext4_ext_check_header(__FUNCTION__, inode, eh, depth)
306
Alex Tomasa86c6182006-10-11 01:21:03 -0700307#ifdef EXT_DEBUG
308static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
309{
310 int k, l = path->p_depth;
311
312 ext_debug("path:");
313 for (k = 0; k <= l; k++, path++) {
314 if (path->p_idx) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700315 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700316 idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700317 } else if (path->p_ext) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700318 ext_debug(" %d:%d:%llu ",
Alex Tomasa86c6182006-10-11 01:21:03 -0700319 le32_to_cpu(path->p_ext->ee_block),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400320 ext4_ext_get_actual_len(path->p_ext),
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700321 ext_pblock(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700322 } else
323 ext_debug(" []");
324 }
325 ext_debug("\n");
326}
327
328static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
329{
330 int depth = ext_depth(inode);
331 struct ext4_extent_header *eh;
332 struct ext4_extent *ex;
333 int i;
334
335 if (!path)
336 return;
337
338 eh = path[depth].p_hdr;
339 ex = EXT_FIRST_EXTENT(eh);
340
341 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700342 ext_debug("%d:%d:%llu ", le32_to_cpu(ex->ee_block),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400343 ext4_ext_get_actual_len(ex), ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -0700344 }
345 ext_debug("\n");
346}
347#else
348#define ext4_ext_show_path(inode,path)
349#define ext4_ext_show_leaf(inode,path)
350#endif
351
352static void ext4_ext_drop_refs(struct ext4_ext_path *path)
353{
354 int depth = path->p_depth;
355 int i;
356
357 for (i = 0; i <= depth; i++, path++)
358 if (path->p_bh) {
359 brelse(path->p_bh);
360 path->p_bh = NULL;
361 }
362}
363
364/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700365 * ext4_ext_binsearch_idx:
366 * binary search for the closest index of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400367 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700368 */
369static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500370ext4_ext_binsearch_idx(struct inode *inode,
371 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700372{
373 struct ext4_extent_header *eh = path->p_hdr;
374 struct ext4_extent_idx *r, *l, *m;
375
Alex Tomasa86c6182006-10-11 01:21:03 -0700376
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500377 ext_debug("binsearch for %lu(idx): ", (unsigned long)block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700378
379 l = EXT_FIRST_INDEX(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400380 r = EXT_LAST_INDEX(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700381 while (l <= r) {
382 m = l + (r - l) / 2;
383 if (block < le32_to_cpu(m->ei_block))
384 r = m - 1;
385 else
386 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400387 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
388 m, le32_to_cpu(m->ei_block),
389 r, le32_to_cpu(r->ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700390 }
391
392 path->p_idx = l - 1;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700393 ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400394 idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700395
396#ifdef CHECK_BINSEARCH
397 {
398 struct ext4_extent_idx *chix, *ix;
399 int k;
400
401 chix = ix = EXT_FIRST_INDEX(eh);
402 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
403 if (k != 0 &&
404 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
405 printk("k=%d, ix=0x%p, first=0x%p\n", k,
406 ix, EXT_FIRST_INDEX(eh));
407 printk("%u <= %u\n",
408 le32_to_cpu(ix->ei_block),
409 le32_to_cpu(ix[-1].ei_block));
410 }
411 BUG_ON(k && le32_to_cpu(ix->ei_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400412 <= le32_to_cpu(ix[-1].ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700413 if (block < le32_to_cpu(ix->ei_block))
414 break;
415 chix = ix;
416 }
417 BUG_ON(chix != path->p_idx);
418 }
419#endif
420
421}
422
423/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700424 * ext4_ext_binsearch:
425 * binary search for closest extent of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400426 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700427 */
428static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500429ext4_ext_binsearch(struct inode *inode,
430 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700431{
432 struct ext4_extent_header *eh = path->p_hdr;
433 struct ext4_extent *r, *l, *m;
434
Alex Tomasa86c6182006-10-11 01:21:03 -0700435 if (eh->eh_entries == 0) {
436 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700437 * this leaf is empty:
438 * we get such a leaf in split/add case
Alex Tomasa86c6182006-10-11 01:21:03 -0700439 */
440 return;
441 }
442
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500443 ext_debug("binsearch for %lu: ", (unsigned long)block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700444
445 l = EXT_FIRST_EXTENT(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400446 r = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700447
448 while (l <= r) {
449 m = l + (r - l) / 2;
450 if (block < le32_to_cpu(m->ee_block))
451 r = m - 1;
452 else
453 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400454 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
455 m, le32_to_cpu(m->ee_block),
456 r, le32_to_cpu(r->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700457 }
458
459 path->p_ext = l - 1;
Mingming Cao2ae02102006-10-11 01:21:11 -0700460 ext_debug(" -> %d:%llu:%d ",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400461 le32_to_cpu(path->p_ext->ee_block),
462 ext_pblock(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400463 ext4_ext_get_actual_len(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700464
465#ifdef CHECK_BINSEARCH
466 {
467 struct ext4_extent *chex, *ex;
468 int k;
469
470 chex = ex = EXT_FIRST_EXTENT(eh);
471 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
472 BUG_ON(k && le32_to_cpu(ex->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400473 <= le32_to_cpu(ex[-1].ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700474 if (block < le32_to_cpu(ex->ee_block))
475 break;
476 chex = ex;
477 }
478 BUG_ON(chex != path->p_ext);
479 }
480#endif
481
482}
483
484int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
485{
486 struct ext4_extent_header *eh;
487
488 eh = ext_inode_hdr(inode);
489 eh->eh_depth = 0;
490 eh->eh_entries = 0;
491 eh->eh_magic = EXT4_EXT_MAGIC;
492 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode));
493 ext4_mark_inode_dirty(handle, inode);
494 ext4_ext_invalidate_cache(inode);
495 return 0;
496}
497
498struct ext4_ext_path *
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500499ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
500 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700501{
502 struct ext4_extent_header *eh;
503 struct buffer_head *bh;
504 short int depth, i, ppos = 0, alloc = 0;
505
506 eh = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400507 depth = ext_depth(inode);
508 if (ext4_ext_check_header(inode, eh, depth))
Alex Tomasa86c6182006-10-11 01:21:03 -0700509 return ERR_PTR(-EIO);
510
Alex Tomasa86c6182006-10-11 01:21:03 -0700511
512 /* account possible depth increase */
513 if (!path) {
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800514 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
Alex Tomasa86c6182006-10-11 01:21:03 -0700515 GFP_NOFS);
516 if (!path)
517 return ERR_PTR(-ENOMEM);
518 alloc = 1;
519 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700520 path[0].p_hdr = eh;
521
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400522 i = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700523 /* walk through the tree */
524 while (i) {
525 ext_debug("depth %d: num %d, max %d\n",
526 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400527
Alex Tomasa86c6182006-10-11 01:21:03 -0700528 ext4_ext_binsearch_idx(inode, path + ppos, block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700529 path[ppos].p_block = idx_pblock(path[ppos].p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -0700530 path[ppos].p_depth = i;
531 path[ppos].p_ext = NULL;
532
533 bh = sb_bread(inode->i_sb, path[ppos].p_block);
534 if (!bh)
535 goto err;
536
537 eh = ext_block_hdr(bh);
538 ppos++;
539 BUG_ON(ppos > depth);
540 path[ppos].p_bh = bh;
541 path[ppos].p_hdr = eh;
542 i--;
543
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400544 if (ext4_ext_check_header(inode, eh, i))
Alex Tomasa86c6182006-10-11 01:21:03 -0700545 goto err;
546 }
547
548 path[ppos].p_depth = i;
549 path[ppos].p_hdr = eh;
550 path[ppos].p_ext = NULL;
551 path[ppos].p_idx = NULL;
552
Alex Tomasa86c6182006-10-11 01:21:03 -0700553 /* find extent */
554 ext4_ext_binsearch(inode, path + ppos, block);
555
556 ext4_ext_show_path(inode, path);
557
558 return path;
559
560err:
561 ext4_ext_drop_refs(path);
562 if (alloc)
563 kfree(path);
564 return ERR_PTR(-EIO);
565}
566
567/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700568 * ext4_ext_insert_index:
569 * insert new index [@logical;@ptr] into the block at @curp;
570 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700571 */
572static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
573 struct ext4_ext_path *curp,
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700574 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700575{
576 struct ext4_extent_idx *ix;
577 int len, err;
578
Avantika Mathur7e028972006-12-06 20:41:33 -0800579 err = ext4_ext_get_access(handle, inode, curp);
580 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700581 return err;
582
583 BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block));
584 len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
585 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
586 /* insert after */
587 if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
588 len = (len - 1) * sizeof(struct ext4_extent_idx);
589 len = len < 0 ? 0 : len;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400590 ext_debug("insert new index %d after: %llu. "
Alex Tomasa86c6182006-10-11 01:21:03 -0700591 "move %d from 0x%p to 0x%p\n",
592 logical, ptr, len,
593 (curp->p_idx + 1), (curp->p_idx + 2));
594 memmove(curp->p_idx + 2, curp->p_idx + 1, len);
595 }
596 ix = curp->p_idx + 1;
597 } else {
598 /* insert before */
599 len = len * 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 before: %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, (curp->p_idx + 1));
605 memmove(curp->p_idx + 1, curp->p_idx, len);
606 ix = curp->p_idx;
607 }
608
609 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700610 ext4_idx_store_pblock(ix, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700611 curp->p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(curp->p_hdr->eh_entries)+1);
612
613 BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400614 > le16_to_cpu(curp->p_hdr->eh_max));
Alex Tomasa86c6182006-10-11 01:21:03 -0700615 BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr));
616
617 err = ext4_ext_dirty(handle, inode, curp);
618 ext4_std_error(inode->i_sb, err);
619
620 return err;
621}
622
623/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700624 * ext4_ext_split:
625 * inserts new subtree into the path, using free index entry
626 * at depth @at:
627 * - allocates all needed blocks (new leaf and all intermediate index blocks)
628 * - makes decision where to split
629 * - moves remaining extents and index entries (right to the split point)
630 * into the newly allocated blocks
631 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -0700632 */
633static int ext4_ext_split(handle_t *handle, struct inode *inode,
634 struct ext4_ext_path *path,
635 struct ext4_extent *newext, int at)
636{
637 struct buffer_head *bh = NULL;
638 int depth = ext_depth(inode);
639 struct ext4_extent_header *neh;
640 struct ext4_extent_idx *fidx;
641 struct ext4_extent *ex;
642 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700643 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700644 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700645 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Alex Tomasa86c6182006-10-11 01:21:03 -0700646 int err = 0;
647
648 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700649 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -0700650
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700651 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -0700652 * border from split point */
653 BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr));
654 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
655 border = path[depth].p_ext[1].ee_block;
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700656 ext_debug("leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -0700657 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400658 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700659 } else {
660 border = newext->ee_block;
661 ext_debug("leaf will be added."
662 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400663 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700664 }
665
666 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700667 * If error occurs, then we break processing
668 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -0700669 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700670 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -0700671 */
672
673 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700674 * Get array to track all allocated blocks.
675 * We need this to handle errors and free blocks
676 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -0700677 */
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800678 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -0700679 if (!ablocks)
680 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -0700681
682 /* allocate all needed blocks */
683 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
684 for (a = 0; a < depth - at; a++) {
685 newblock = ext4_ext_new_block(handle, inode, path, newext, &err);
686 if (newblock == 0)
687 goto cleanup;
688 ablocks[a] = newblock;
689 }
690
691 /* initialize new leaf */
692 newblock = ablocks[--a];
693 BUG_ON(newblock == 0);
694 bh = sb_getblk(inode->i_sb, newblock);
695 if (!bh) {
696 err = -EIO;
697 goto cleanup;
698 }
699 lock_buffer(bh);
700
Avantika Mathur7e028972006-12-06 20:41:33 -0800701 err = ext4_journal_get_create_access(handle, bh);
702 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700703 goto cleanup;
704
705 neh = ext_block_hdr(bh);
706 neh->eh_entries = 0;
707 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
708 neh->eh_magic = EXT4_EXT_MAGIC;
709 neh->eh_depth = 0;
710 ex = EXT_FIRST_EXTENT(neh);
711
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700712 /* move remainder of path[depth] to the new leaf */
Alex Tomasa86c6182006-10-11 01:21:03 -0700713 BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max);
714 /* start copy from next extent */
715 /* TODO: we could do it by single memmove */
716 m = 0;
717 path[depth].p_ext++;
718 while (path[depth].p_ext <=
719 EXT_MAX_EXTENT(path[depth].p_hdr)) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700720 ext_debug("move %d:%llu:%d in new leaf %llu\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400721 le32_to_cpu(path[depth].p_ext->ee_block),
722 ext_pblock(path[depth].p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400723 ext4_ext_get_actual_len(path[depth].p_ext),
Alex Tomasa86c6182006-10-11 01:21:03 -0700724 newblock);
725 /*memmove(ex++, path[depth].p_ext++,
726 sizeof(struct ext4_extent));
727 neh->eh_entries++;*/
728 path[depth].p_ext++;
729 m++;
730 }
731 if (m) {
732 memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
733 neh->eh_entries = cpu_to_le16(le16_to_cpu(neh->eh_entries)+m);
734 }
735
736 set_buffer_uptodate(bh);
737 unlock_buffer(bh);
738
Avantika Mathur7e028972006-12-06 20:41:33 -0800739 err = ext4_journal_dirty_metadata(handle, bh);
740 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700741 goto cleanup;
742 brelse(bh);
743 bh = NULL;
744
745 /* correct old leaf */
746 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -0800747 err = ext4_ext_get_access(handle, inode, path + depth);
748 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700749 goto cleanup;
750 path[depth].p_hdr->eh_entries =
751 cpu_to_le16(le16_to_cpu(path[depth].p_hdr->eh_entries)-m);
Avantika Mathur7e028972006-12-06 20:41:33 -0800752 err = ext4_ext_dirty(handle, inode, path + depth);
753 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700754 goto cleanup;
755
756 }
757
758 /* create intermediate indexes */
759 k = depth - at - 1;
760 BUG_ON(k < 0);
761 if (k)
762 ext_debug("create %d intermediate indices\n", k);
763 /* insert new index into current index block */
764 /* current depth stored in i var */
765 i = depth - 1;
766 while (k--) {
767 oldblock = newblock;
768 newblock = ablocks[--a];
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700769 bh = sb_getblk(inode->i_sb, (ext4_fsblk_t)newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700770 if (!bh) {
771 err = -EIO;
772 goto cleanup;
773 }
774 lock_buffer(bh);
775
Avantika Mathur7e028972006-12-06 20:41:33 -0800776 err = ext4_journal_get_create_access(handle, bh);
777 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700778 goto cleanup;
779
780 neh = ext_block_hdr(bh);
781 neh->eh_entries = cpu_to_le16(1);
782 neh->eh_magic = EXT4_EXT_MAGIC;
783 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
784 neh->eh_depth = cpu_to_le16(depth - i);
785 fidx = EXT_FIRST_INDEX(neh);
786 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700787 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700788
Mingming Cao2ae02102006-10-11 01:21:11 -0700789 ext_debug("int.index at %d (block %llu): %lu -> %llu\n", i,
Alex Tomasa86c6182006-10-11 01:21:03 -0700790 newblock, (unsigned long) le32_to_cpu(border),
791 oldblock);
792 /* copy indexes */
793 m = 0;
794 path[i].p_idx++;
795
796 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
797 EXT_MAX_INDEX(path[i].p_hdr));
798 BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) !=
799 EXT_LAST_INDEX(path[i].p_hdr));
800 while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400801 ext_debug("%d: move %d:%llu in new index %llu\n", i,
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400802 le32_to_cpu(path[i].p_idx->ei_block),
803 idx_pblock(path[i].p_idx),
804 newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700805 /*memmove(++fidx, path[i].p_idx++,
806 sizeof(struct ext4_extent_idx));
807 neh->eh_entries++;
808 BUG_ON(neh->eh_entries > neh->eh_max);*/
809 path[i].p_idx++;
810 m++;
811 }
812 if (m) {
813 memmove(++fidx, path[i].p_idx - m,
814 sizeof(struct ext4_extent_idx) * m);
815 neh->eh_entries =
816 cpu_to_le16(le16_to_cpu(neh->eh_entries) + m);
817 }
818 set_buffer_uptodate(bh);
819 unlock_buffer(bh);
820
Avantika Mathur7e028972006-12-06 20:41:33 -0800821 err = ext4_journal_dirty_metadata(handle, bh);
822 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700823 goto cleanup;
824 brelse(bh);
825 bh = NULL;
826
827 /* correct old index */
828 if (m) {
829 err = ext4_ext_get_access(handle, inode, path + i);
830 if (err)
831 goto cleanup;
832 path[i].p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(path[i].p_hdr->eh_entries)-m);
833 err = ext4_ext_dirty(handle, inode, path + i);
834 if (err)
835 goto cleanup;
836 }
837
838 i--;
839 }
840
841 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -0700842 err = ext4_ext_insert_index(handle, inode, path + at,
843 le32_to_cpu(border), newblock);
844
845cleanup:
846 if (bh) {
847 if (buffer_locked(bh))
848 unlock_buffer(bh);
849 brelse(bh);
850 }
851
852 if (err) {
853 /* free all allocated blocks in error case */
854 for (i = 0; i < depth; i++) {
855 if (!ablocks[i])
856 continue;
857 ext4_free_blocks(handle, inode, ablocks[i], 1);
858 }
859 }
860 kfree(ablocks);
861
862 return err;
863}
864
865/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700866 * ext4_ext_grow_indepth:
867 * implements tree growing procedure:
868 * - allocates new block
869 * - moves top-level data (index block or leaf) into the new block
870 * - initializes new top-level, creating index that points to the
871 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -0700872 */
873static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
874 struct ext4_ext_path *path,
875 struct ext4_extent *newext)
876{
877 struct ext4_ext_path *curp = path;
878 struct ext4_extent_header *neh;
879 struct ext4_extent_idx *fidx;
880 struct buffer_head *bh;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700881 ext4_fsblk_t newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700882 int err = 0;
883
884 newblock = ext4_ext_new_block(handle, inode, path, newext, &err);
885 if (newblock == 0)
886 return err;
887
888 bh = sb_getblk(inode->i_sb, newblock);
889 if (!bh) {
890 err = -EIO;
891 ext4_std_error(inode->i_sb, err);
892 return err;
893 }
894 lock_buffer(bh);
895
Avantika Mathur7e028972006-12-06 20:41:33 -0800896 err = ext4_journal_get_create_access(handle, bh);
897 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -0700898 unlock_buffer(bh);
899 goto out;
900 }
901
902 /* move top-level index/leaf into new block */
903 memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
904
905 /* set size of new block */
906 neh = ext_block_hdr(bh);
907 /* old root could have indexes or leaves
908 * so calculate e_max right way */
909 if (ext_depth(inode))
910 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
911 else
912 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
913 neh->eh_magic = EXT4_EXT_MAGIC;
914 set_buffer_uptodate(bh);
915 unlock_buffer(bh);
916
Avantika Mathur7e028972006-12-06 20:41:33 -0800917 err = ext4_journal_dirty_metadata(handle, bh);
918 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700919 goto out;
920
921 /* create index in new top-level index: num,max,pointer */
Avantika Mathur7e028972006-12-06 20:41:33 -0800922 err = ext4_ext_get_access(handle, inode, curp);
923 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700924 goto out;
925
926 curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
927 curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode));
928 curp->p_hdr->eh_entries = cpu_to_le16(1);
929 curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400930
931 if (path[0].p_hdr->eh_depth)
932 curp->p_idx->ei_block =
933 EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
934 else
935 curp->p_idx->ei_block =
936 EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700937 ext4_idx_store_pblock(curp->p_idx, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700938
939 neh = ext_inode_hdr(inode);
940 fidx = EXT_FIRST_INDEX(neh);
Mingming Cao2ae02102006-10-11 01:21:11 -0700941 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700942 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700943 le32_to_cpu(fidx->ei_block), idx_pblock(fidx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700944
945 neh->eh_depth = cpu_to_le16(path->p_depth + 1);
946 err = ext4_ext_dirty(handle, inode, curp);
947out:
948 brelse(bh);
949
950 return err;
951}
952
953/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700954 * ext4_ext_create_new_leaf:
955 * finds empty index and adds new leaf.
956 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -0700957 */
958static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
959 struct ext4_ext_path *path,
960 struct ext4_extent *newext)
961{
962 struct ext4_ext_path *curp;
963 int depth, i, err = 0;
964
965repeat:
966 i = depth = ext_depth(inode);
967
968 /* walk up to the tree and look for free index entry */
969 curp = path + depth;
970 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
971 i--;
972 curp--;
973 }
974
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700975 /* we use already allocated block for index block,
976 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -0700977 if (EXT_HAS_FREE_INDEX(curp)) {
978 /* if we found index with free entry, then use that
979 * entry: create all needed subtree and add new leaf */
980 err = ext4_ext_split(handle, inode, path, newext, i);
981
982 /* refill path */
983 ext4_ext_drop_refs(path);
984 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500985 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
986 path);
Alex Tomasa86c6182006-10-11 01:21:03 -0700987 if (IS_ERR(path))
988 err = PTR_ERR(path);
989 } else {
990 /* tree is full, time to grow in depth */
991 err = ext4_ext_grow_indepth(handle, inode, path, newext);
992 if (err)
993 goto out;
994
995 /* refill path */
996 ext4_ext_drop_refs(path);
997 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500998 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
999 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001000 if (IS_ERR(path)) {
1001 err = PTR_ERR(path);
1002 goto out;
1003 }
1004
1005 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001006 * only first (depth 0 -> 1) produces free space;
1007 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001008 */
1009 depth = ext_depth(inode);
1010 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001011 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001012 goto repeat;
1013 }
1014 }
1015
1016out:
1017 return err;
1018}
1019
1020/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001021 * ext4_ext_next_allocated_block:
1022 * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1023 * NOTE: it considers block number from index entry as
1024 * allocated block. Thus, index entries have to be consistent
1025 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001026 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001027static ext4_lblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -07001028ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1029{
1030 int depth;
1031
1032 BUG_ON(path == NULL);
1033 depth = path->p_depth;
1034
1035 if (depth == 0 && path->p_ext == NULL)
1036 return EXT_MAX_BLOCK;
1037
1038 while (depth >= 0) {
1039 if (depth == path->p_depth) {
1040 /* leaf */
1041 if (path[depth].p_ext !=
1042 EXT_LAST_EXTENT(path[depth].p_hdr))
1043 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1044 } else {
1045 /* index */
1046 if (path[depth].p_idx !=
1047 EXT_LAST_INDEX(path[depth].p_hdr))
1048 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1049 }
1050 depth--;
1051 }
1052
1053 return EXT_MAX_BLOCK;
1054}
1055
1056/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001057 * ext4_ext_next_leaf_block:
Alex Tomasa86c6182006-10-11 01:21:03 -07001058 * returns first allocated block from next leaf or EXT_MAX_BLOCK
1059 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001060static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode,
Andrew Morton63f57932006-10-11 01:21:24 -07001061 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001062{
1063 int depth;
1064
1065 BUG_ON(path == NULL);
1066 depth = path->p_depth;
1067
1068 /* zero-tree has no leaf blocks at all */
1069 if (depth == 0)
1070 return EXT_MAX_BLOCK;
1071
1072 /* go to index block */
1073 depth--;
1074
1075 while (depth >= 0) {
1076 if (path[depth].p_idx !=
1077 EXT_LAST_INDEX(path[depth].p_hdr))
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001078 return (ext4_lblk_t)
1079 le32_to_cpu(path[depth].p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001080 depth--;
1081 }
1082
1083 return EXT_MAX_BLOCK;
1084}
1085
1086/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001087 * ext4_ext_correct_indexes:
1088 * if leaf gets modified and modified extent is first in the leaf,
1089 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001090 * TODO: do we need to correct tree in all cases?
1091 */
1092int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
1093 struct ext4_ext_path *path)
1094{
1095 struct ext4_extent_header *eh;
1096 int depth = ext_depth(inode);
1097 struct ext4_extent *ex;
1098 __le32 border;
1099 int k, err = 0;
1100
1101 eh = path[depth].p_hdr;
1102 ex = path[depth].p_ext;
1103 BUG_ON(ex == NULL);
1104 BUG_ON(eh == NULL);
1105
1106 if (depth == 0) {
1107 /* there is no tree at all */
1108 return 0;
1109 }
1110
1111 if (ex != EXT_FIRST_EXTENT(eh)) {
1112 /* we correct tree if first leaf got modified only */
1113 return 0;
1114 }
1115
1116 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001117 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001118 */
1119 k = depth - 1;
1120 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001121 err = ext4_ext_get_access(handle, inode, path + k);
1122 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001123 return err;
1124 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001125 err = ext4_ext_dirty(handle, inode, path + k);
1126 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001127 return err;
1128
1129 while (k--) {
1130 /* change all left-side indexes */
1131 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1132 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001133 err = ext4_ext_get_access(handle, inode, path + k);
1134 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001135 break;
1136 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001137 err = ext4_ext_dirty(handle, inode, path + k);
1138 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001139 break;
1140 }
1141
1142 return err;
1143}
1144
Avantika Mathur09b88252006-12-06 20:41:36 -08001145static int
Alex Tomasa86c6182006-10-11 01:21:03 -07001146ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1147 struct ext4_extent *ex2)
1148{
Amit Arora749269f2007-07-18 09:02:56 -04001149 unsigned short ext1_ee_len, ext2_ee_len, max_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001150
1151 /*
1152 * Make sure that either both extents are uninitialized, or
1153 * both are _not_.
1154 */
1155 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1156 return 0;
1157
Amit Arora749269f2007-07-18 09:02:56 -04001158 if (ext4_ext_is_uninitialized(ex1))
1159 max_len = EXT_UNINIT_MAX_LEN;
1160 else
1161 max_len = EXT_INIT_MAX_LEN;
1162
Amit Aroraa2df2a62007-07-17 21:42:41 -04001163 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1164 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1165
1166 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001167 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001168 return 0;
1169
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001170 /*
1171 * To allow future support for preallocated extents to be added
1172 * as an RO_COMPAT feature, refuse to merge to extents if
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001173 * this can result in the top bit of ee_len being set.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001174 */
Amit Arora749269f2007-07-18 09:02:56 -04001175 if (ext1_ee_len + ext2_ee_len > max_len)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001176 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001177#ifdef AGGRESSIVE_TEST
Alex Tomasa86c6182006-10-11 01:21:03 -07001178 if (le16_to_cpu(ex1->ee_len) >= 4)
1179 return 0;
1180#endif
1181
Amit Aroraa2df2a62007-07-17 21:42:41 -04001182 if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001183 return 1;
1184 return 0;
1185}
1186
1187/*
Amit Arora56055d32007-07-17 21:42:38 -04001188 * This function tries to merge the "ex" extent to the next extent in the tree.
1189 * It always tries to merge towards right. If you want to merge towards
1190 * left, pass "ex - 1" as argument instead of "ex".
1191 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1192 * 1 if they got merged.
1193 */
1194int ext4_ext_try_to_merge(struct inode *inode,
1195 struct ext4_ext_path *path,
1196 struct ext4_extent *ex)
1197{
1198 struct ext4_extent_header *eh;
1199 unsigned int depth, len;
1200 int merge_done = 0;
1201 int uninitialized = 0;
1202
1203 depth = ext_depth(inode);
1204 BUG_ON(path[depth].p_hdr == NULL);
1205 eh = path[depth].p_hdr;
1206
1207 while (ex < EXT_LAST_EXTENT(eh)) {
1208 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1209 break;
1210 /* merge with next extent! */
1211 if (ext4_ext_is_uninitialized(ex))
1212 uninitialized = 1;
1213 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1214 + ext4_ext_get_actual_len(ex + 1));
1215 if (uninitialized)
1216 ext4_ext_mark_uninitialized(ex);
1217
1218 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1219 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1220 * sizeof(struct ext4_extent);
1221 memmove(ex + 1, ex + 2, len);
1222 }
1223 eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries) - 1);
1224 merge_done = 1;
1225 WARN_ON(eh->eh_entries == 0);
1226 if (!eh->eh_entries)
1227 ext4_error(inode->i_sb, "ext4_ext_try_to_merge",
1228 "inode#%lu, eh->eh_entries = 0!", inode->i_ino);
1229 }
1230
1231 return merge_done;
1232}
1233
1234/*
Amit Arora25d14f92007-05-24 13:04:13 -04001235 * check if a portion of the "newext" extent overlaps with an
1236 * existing extent.
1237 *
1238 * If there is an overlap discovered, it updates the length of the newext
1239 * such that there will be no overlap, and then returns 1.
1240 * If there is no overlap found, it returns 0.
1241 */
1242unsigned int ext4_ext_check_overlap(struct inode *inode,
1243 struct ext4_extent *newext,
1244 struct ext4_ext_path *path)
1245{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001246 ext4_lblk_t b1, b2;
Amit Arora25d14f92007-05-24 13:04:13 -04001247 unsigned int depth, len1;
1248 unsigned int ret = 0;
1249
1250 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001251 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001252 depth = ext_depth(inode);
1253 if (!path[depth].p_ext)
1254 goto out;
1255 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
1256
1257 /*
1258 * get the next allocated block if the extent in the path
1259 * is before the requested block(s)
1260 */
1261 if (b2 < b1) {
1262 b2 = ext4_ext_next_allocated_block(path);
1263 if (b2 == EXT_MAX_BLOCK)
1264 goto out;
1265 }
1266
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001267 /* check for wrap through zero on extent logical start block*/
Amit Arora25d14f92007-05-24 13:04:13 -04001268 if (b1 + len1 < b1) {
1269 len1 = EXT_MAX_BLOCK - b1;
1270 newext->ee_len = cpu_to_le16(len1);
1271 ret = 1;
1272 }
1273
1274 /* check for overlap */
1275 if (b1 + len1 > b2) {
1276 newext->ee_len = cpu_to_le16(b2 - b1);
1277 ret = 1;
1278 }
1279out:
1280 return ret;
1281}
1282
1283/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001284 * ext4_ext_insert_extent:
1285 * tries to merge requsted extent into the existing extent or
1286 * inserts requested extent as new one into the tree,
1287 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001288 */
1289int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1290 struct ext4_ext_path *path,
1291 struct ext4_extent *newext)
1292{
1293 struct ext4_extent_header * eh;
1294 struct ext4_extent *ex, *fex;
1295 struct ext4_extent *nearex; /* nearest extent */
1296 struct ext4_ext_path *npath = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001297 int depth, len, err;
1298 ext4_lblk_t next;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001299 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001300
Amit Aroraa2df2a62007-07-17 21:42:41 -04001301 BUG_ON(ext4_ext_get_actual_len(newext) == 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07001302 depth = ext_depth(inode);
1303 ex = path[depth].p_ext;
1304 BUG_ON(path[depth].p_hdr == NULL);
1305
1306 /* try to insert block into found extent and return */
1307 if (ex && ext4_can_extents_be_merged(inode, ex, newext)) {
Mingming Cao2ae02102006-10-11 01:21:11 -07001308 ext_debug("append %d block to %d:%d (from %llu)\n",
Amit Aroraa2df2a62007-07-17 21:42:41 -04001309 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001310 le32_to_cpu(ex->ee_block),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001311 ext4_ext_get_actual_len(ex), ext_pblock(ex));
Avantika Mathur7e028972006-12-06 20:41:33 -08001312 err = ext4_ext_get_access(handle, inode, path + depth);
1313 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001314 return err;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001315
1316 /*
1317 * ext4_can_extents_be_merged should have checked that either
1318 * both extents are uninitialized, or both aren't. Thus we
1319 * need to check only one of them here.
1320 */
1321 if (ext4_ext_is_uninitialized(ex))
1322 uninitialized = 1;
1323 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1324 + ext4_ext_get_actual_len(newext));
1325 if (uninitialized)
1326 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001327 eh = path[depth].p_hdr;
1328 nearex = ex;
1329 goto merge;
1330 }
1331
1332repeat:
1333 depth = ext_depth(inode);
1334 eh = path[depth].p_hdr;
1335 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1336 goto has_space;
1337
1338 /* probably next leaf has space for us? */
1339 fex = EXT_LAST_EXTENT(eh);
1340 next = ext4_ext_next_leaf_block(inode, path);
1341 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
1342 && next != EXT_MAX_BLOCK) {
1343 ext_debug("next leaf block - %d\n", next);
1344 BUG_ON(npath != NULL);
1345 npath = ext4_ext_find_extent(inode, next, NULL);
1346 if (IS_ERR(npath))
1347 return PTR_ERR(npath);
1348 BUG_ON(npath->p_depth != path->p_depth);
1349 eh = npath[depth].p_hdr;
1350 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
1351 ext_debug("next leaf isnt full(%d)\n",
1352 le16_to_cpu(eh->eh_entries));
1353 path = npath;
1354 goto repeat;
1355 }
1356 ext_debug("next leaf has no free space(%d,%d)\n",
1357 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1358 }
1359
1360 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001361 * There is no free space in the found leaf.
1362 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07001363 */
1364 err = ext4_ext_create_new_leaf(handle, inode, path, newext);
1365 if (err)
1366 goto cleanup;
1367 depth = ext_depth(inode);
1368 eh = path[depth].p_hdr;
1369
1370has_space:
1371 nearex = path[depth].p_ext;
1372
Avantika Mathur7e028972006-12-06 20:41:33 -08001373 err = ext4_ext_get_access(handle, inode, path + depth);
1374 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001375 goto cleanup;
1376
1377 if (!nearex) {
1378 /* there is no extent in this leaf, create first one */
Mingming Cao2ae02102006-10-11 01:21:11 -07001379 ext_debug("first extent in the leaf: %d:%llu:%d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001380 le32_to_cpu(newext->ee_block),
1381 ext_pblock(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001382 ext4_ext_get_actual_len(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001383 path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1384 } else if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001385 > le32_to_cpu(nearex->ee_block)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001386/* BUG_ON(newext->ee_block == nearex->ee_block); */
1387 if (nearex != EXT_LAST_EXTENT(eh)) {
1388 len = EXT_MAX_EXTENT(eh) - nearex;
1389 len = (len - 1) * sizeof(struct ext4_extent);
1390 len = len < 0 ? 0 : len;
Mingming Cao2ae02102006-10-11 01:21:11 -07001391 ext_debug("insert %d:%llu:%d after: nearest 0x%p, "
Alex Tomasa86c6182006-10-11 01:21:03 -07001392 "move %d from 0x%p to 0x%p\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001393 le32_to_cpu(newext->ee_block),
1394 ext_pblock(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001395 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001396 nearex, len, nearex + 1, nearex + 2);
1397 memmove(nearex + 2, nearex + 1, len);
1398 }
1399 path[depth].p_ext = nearex + 1;
1400 } else {
1401 BUG_ON(newext->ee_block == nearex->ee_block);
1402 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1403 len = len < 0 ? 0 : len;
Mingming Cao2ae02102006-10-11 01:21:11 -07001404 ext_debug("insert %d:%llu:%d before: nearest 0x%p, "
Alex Tomasa86c6182006-10-11 01:21:03 -07001405 "move %d from 0x%p to 0x%p\n",
1406 le32_to_cpu(newext->ee_block),
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001407 ext_pblock(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001408 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001409 nearex, len, nearex + 1, nearex + 2);
1410 memmove(nearex + 1, nearex, len);
1411 path[depth].p_ext = nearex;
1412 }
1413
1414 eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)+1);
1415 nearex = path[depth].p_ext;
1416 nearex->ee_block = newext->ee_block;
Aneesh Kumar K.Vb3776112007-10-16 18:38:25 -04001417 ext4_ext_store_pblock(nearex, ext_pblock(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001418 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07001419
1420merge:
1421 /* try to merge extents to the right */
Amit Arora56055d32007-07-17 21:42:38 -04001422 ext4_ext_try_to_merge(inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001423
1424 /* try to merge extents to the left */
1425
1426 /* time to correct all indexes above */
1427 err = ext4_ext_correct_indexes(handle, inode, path);
1428 if (err)
1429 goto cleanup;
1430
1431 err = ext4_ext_dirty(handle, inode, path + depth);
1432
1433cleanup:
1434 if (npath) {
1435 ext4_ext_drop_refs(npath);
1436 kfree(npath);
1437 }
1438 ext4_ext_tree_changed(inode);
1439 ext4_ext_invalidate_cache(inode);
1440 return err;
1441}
1442
Avantika Mathur09b88252006-12-06 20:41:36 -08001443static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001444ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
Mingming Caodd545672007-07-31 00:37:46 -07001445 __u32 len, ext4_fsblk_t start, int type)
Alex Tomasa86c6182006-10-11 01:21:03 -07001446{
1447 struct ext4_ext_cache *cex;
1448 BUG_ON(len == 0);
1449 cex = &EXT4_I(inode)->i_cached_extent;
1450 cex->ec_type = type;
1451 cex->ec_block = block;
1452 cex->ec_len = len;
1453 cex->ec_start = start;
1454}
1455
1456/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001457 * ext4_ext_put_gap_in_cache:
1458 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07001459 * and cache this gap
1460 */
Avantika Mathur09b88252006-12-06 20:41:36 -08001461static void
Alex Tomasa86c6182006-10-11 01:21:03 -07001462ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001463 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -07001464{
1465 int depth = ext_depth(inode);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001466 unsigned long len;
1467 ext4_lblk_t lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001468 struct ext4_extent *ex;
1469
1470 ex = path[depth].p_ext;
1471 if (ex == NULL) {
1472 /* there is no extent yet, so gap is [0;-] */
1473 lblock = 0;
1474 len = EXT_MAX_BLOCK;
1475 ext_debug("cache gap(whole file):");
1476 } else if (block < le32_to_cpu(ex->ee_block)) {
1477 lblock = block;
1478 len = le32_to_cpu(ex->ee_block) - block;
1479 ext_debug("cache gap(before): %lu [%lu:%lu]",
1480 (unsigned long) block,
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001481 (unsigned long) le32_to_cpu(ex->ee_block),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001482 (unsigned long) ext4_ext_get_actual_len(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07001483 } else if (block >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001484 + ext4_ext_get_actual_len(ex)) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001485 ext4_lblk_t next;
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001486 lblock = le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001487 + ext4_ext_get_actual_len(ex);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001488
1489 next = ext4_ext_next_allocated_block(path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001490 ext_debug("cache gap(after): [%lu:%lu] %lu",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001491 (unsigned long) le32_to_cpu(ex->ee_block),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001492 (unsigned long) ext4_ext_get_actual_len(ex),
Alex Tomasa86c6182006-10-11 01:21:03 -07001493 (unsigned long) block);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001494 BUG_ON(next == lblock);
1495 len = next - lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001496 } else {
1497 lblock = len = 0;
1498 BUG();
1499 }
1500
1501 ext_debug(" -> %lu:%lu\n", (unsigned long) lblock, len);
1502 ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
1503}
1504
Avantika Mathur09b88252006-12-06 20:41:36 -08001505static int
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001506ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
Alex Tomasa86c6182006-10-11 01:21:03 -07001507 struct ext4_extent *ex)
1508{
1509 struct ext4_ext_cache *cex;
1510
1511 cex = &EXT4_I(inode)->i_cached_extent;
1512
1513 /* has cache valid data? */
1514 if (cex->ec_type == EXT4_EXT_CACHE_NO)
1515 return EXT4_EXT_CACHE_NO;
1516
1517 BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
1518 cex->ec_type != EXT4_EXT_CACHE_EXTENT);
1519 if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001520 ex->ee_block = cpu_to_le32(cex->ec_block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001521 ext4_ext_store_pblock(ex, cex->ec_start);
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001522 ex->ee_len = cpu_to_le16(cex->ec_len);
Mingming Cao2ae02102006-10-11 01:21:11 -07001523 ext_debug("%lu cached by %lu:%lu:%llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001524 (unsigned long) block,
1525 (unsigned long) cex->ec_block,
1526 (unsigned long) cex->ec_len,
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001527 cex->ec_start);
Alex Tomasa86c6182006-10-11 01:21:03 -07001528 return cex->ec_type;
1529 }
1530
1531 /* not in cache */
1532 return EXT4_EXT_CACHE_NO;
1533}
1534
1535/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001536 * ext4_ext_rm_idx:
1537 * removes index from the index block.
1538 * It's used in truncate case only, thus all requests are for
1539 * last index in the block only.
Alex Tomasa86c6182006-10-11 01:21:03 -07001540 */
1541int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
1542 struct ext4_ext_path *path)
1543{
1544 struct buffer_head *bh;
1545 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001546 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07001547
1548 /* free index block */
1549 path--;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001550 leaf = idx_pblock(path->p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -07001551 BUG_ON(path->p_hdr->eh_entries == 0);
Avantika Mathur7e028972006-12-06 20:41:33 -08001552 err = ext4_ext_get_access(handle, inode, path);
1553 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001554 return err;
1555 path->p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(path->p_hdr->eh_entries)-1);
Avantika Mathur7e028972006-12-06 20:41:33 -08001556 err = ext4_ext_dirty(handle, inode, path);
1557 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001558 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07001559 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Alex Tomasa86c6182006-10-11 01:21:03 -07001560 bh = sb_find_get_block(inode->i_sb, leaf);
1561 ext4_forget(handle, 1, inode, bh, leaf);
1562 ext4_free_blocks(handle, inode, leaf, 1);
1563 return err;
1564}
1565
1566/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001567 * ext4_ext_calc_credits_for_insert:
1568 * This routine returns max. credits that the extent tree can consume.
Alex Tomasa86c6182006-10-11 01:21:03 -07001569 * It should be OK for low-performance paths like ->writepage()
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001570 * To allow many writing processes to fit into a single transaction,
1571 * the caller should calculate credits under truncate_mutex and
1572 * pass the actual path.
Alex Tomasa86c6182006-10-11 01:21:03 -07001573 */
Avantika Mathur09b88252006-12-06 20:41:36 -08001574int ext4_ext_calc_credits_for_insert(struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001575 struct ext4_ext_path *path)
1576{
1577 int depth, needed;
1578
1579 if (path) {
1580 /* probably there is space in leaf? */
1581 depth = ext_depth(inode);
1582 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
1583 < le16_to_cpu(path[depth].p_hdr->eh_max))
1584 return 1;
1585 }
1586
1587 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001588 * given 32-bit logical block (4294967296 blocks), max. tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001589 * can be 4 levels in depth -- 4 * 340^4 == 53453440000.
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001590 * Let's also add one more level for imbalance.
Alex Tomasa86c6182006-10-11 01:21:03 -07001591 */
1592 depth = 5;
1593
1594 /* allocation of new data block(s) */
1595 needed = 2;
1596
1597 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001598 * tree can be full, so it would need to grow in depth:
Johann Lombardifeb18922006-12-06 20:40:46 -08001599 * we need one credit to modify old root, credits for
1600 * new root will be added in split accounting
Alex Tomasa86c6182006-10-11 01:21:03 -07001601 */
Johann Lombardifeb18922006-12-06 20:40:46 -08001602 needed += 1;
Alex Tomasa86c6182006-10-11 01:21:03 -07001603
1604 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001605 * Index split can happen, we would need:
Alex Tomasa86c6182006-10-11 01:21:03 -07001606 * allocate intermediate indexes (bitmap + group)
1607 * + change two blocks at each level, but root (already included)
1608 */
Johann Lombardifeb18922006-12-06 20:40:46 -08001609 needed += (depth * 2) + (depth * 2);
Alex Tomasa86c6182006-10-11 01:21:03 -07001610
1611 /* any allocation modifies superblock */
1612 needed += 1;
1613
1614 return needed;
1615}
1616
1617static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
1618 struct ext4_extent *ex,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001619 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07001620{
1621 struct buffer_head *bh;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001622 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001623 int i;
1624
1625#ifdef EXTENTS_STATS
1626 {
1627 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07001628 spin_lock(&sbi->s_ext_stats_lock);
1629 sbi->s_ext_blocks += ee_len;
1630 sbi->s_ext_extents++;
1631 if (ee_len < sbi->s_ext_min)
1632 sbi->s_ext_min = ee_len;
1633 if (ee_len > sbi->s_ext_max)
1634 sbi->s_ext_max = ee_len;
1635 if (ext_depth(inode) > sbi->s_depth_max)
1636 sbi->s_depth_max = ext_depth(inode);
1637 spin_unlock(&sbi->s_ext_stats_lock);
1638 }
1639#endif
1640 if (from >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001641 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001642 /* tail removal */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001643 ext4_lblk_t num;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001644 ext4_fsblk_t start;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001645
Amit Aroraa2df2a62007-07-17 21:42:41 -04001646 num = le32_to_cpu(ex->ee_block) + ee_len - from;
1647 start = ext_pblock(ex) + ee_len - num;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001648 ext_debug("free last %u blocks starting %llu\n", num, start);
Alex Tomasa86c6182006-10-11 01:21:03 -07001649 for (i = 0; i < num; i++) {
1650 bh = sb_find_get_block(inode->i_sb, start + i);
1651 ext4_forget(handle, 0, inode, bh, start + i);
1652 }
1653 ext4_free_blocks(handle, inode, start, num);
1654 } else if (from == le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001655 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001656 printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n",
Amit Aroraa2df2a62007-07-17 21:42:41 -04001657 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07001658 } else {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001659 printk(KERN_INFO "strange request: removal(2) "
1660 "%u-%u from %u:%u\n",
1661 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07001662 }
1663 return 0;
1664}
1665
1666static int
1667ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001668 struct ext4_ext_path *path, ext4_lblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07001669{
1670 int err = 0, correct_index = 0;
1671 int depth = ext_depth(inode), credits;
1672 struct ext4_extent_header *eh;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001673 ext4_lblk_t a, b, block;
1674 unsigned num;
1675 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07001676 unsigned short ex_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001677 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001678 struct ext4_extent *ex;
1679
Alex Tomasc29c0ae2007-07-18 09:19:09 -04001680 /* the header must be checked already in ext4_ext_remove_space() */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001681 ext_debug("truncate since %u in leaf\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07001682 if (!path[depth].p_hdr)
1683 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
1684 eh = path[depth].p_hdr;
1685 BUG_ON(eh == NULL);
Alex Tomasa86c6182006-10-11 01:21:03 -07001686
1687 /* find where to start removing */
1688 ex = EXT_LAST_EXTENT(eh);
1689
1690 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001691 if (ext4_ext_is_uninitialized(ex))
1692 uninitialized = 1;
1693 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001694
1695 while (ex >= EXT_FIRST_EXTENT(eh) &&
1696 ex_ee_block + ex_ee_len > start) {
1697 ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len);
1698 path[depth].p_ext = ex;
1699
1700 a = ex_ee_block > start ? ex_ee_block : start;
1701 b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
1702 ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
1703
1704 ext_debug(" border %u:%u\n", a, b);
1705
1706 if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
1707 block = 0;
1708 num = 0;
1709 BUG();
1710 } else if (a != ex_ee_block) {
1711 /* remove tail of the extent */
1712 block = ex_ee_block;
1713 num = a - block;
1714 } else if (b != ex_ee_block + ex_ee_len - 1) {
1715 /* remove head of the extent */
1716 block = a;
1717 num = b - a;
1718 /* there is no "make a hole" API yet */
1719 BUG();
1720 } else {
1721 /* remove whole extent: excellent! */
1722 block = ex_ee_block;
1723 num = 0;
1724 BUG_ON(a != ex_ee_block);
1725 BUG_ON(b != ex_ee_block + ex_ee_len - 1);
1726 }
1727
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001728 /* at present, extent can't cross block group: */
Alex Tomasa86c6182006-10-11 01:21:03 -07001729 /* leaf + bitmap + group desc + sb + inode */
1730 credits = 5;
1731 if (ex == EXT_FIRST_EXTENT(eh)) {
1732 correct_index = 1;
1733 credits += (ext_depth(inode)) + 1;
1734 }
1735#ifdef CONFIG_QUOTA
1736 credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
1737#endif
1738
1739 handle = ext4_ext_journal_restart(handle, credits);
1740 if (IS_ERR(handle)) {
1741 err = PTR_ERR(handle);
1742 goto out;
1743 }
1744
1745 err = ext4_ext_get_access(handle, inode, path + depth);
1746 if (err)
1747 goto out;
1748
1749 err = ext4_remove_blocks(handle, inode, ex, a, b);
1750 if (err)
1751 goto out;
1752
1753 if (num == 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001754 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001755 ext4_ext_store_pblock(ex, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07001756 eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)-1);
1757 }
1758
1759 ex->ee_block = cpu_to_le32(block);
1760 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04001761 /*
1762 * Do not mark uninitialized if all the blocks in the
1763 * extent have been removed.
1764 */
1765 if (uninitialized && num)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001766 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001767
1768 err = ext4_ext_dirty(handle, inode, path + depth);
1769 if (err)
1770 goto out;
1771
Mingming Cao2ae02102006-10-11 01:21:11 -07001772 ext_debug("new extent: %u:%u:%llu\n", block, num,
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001773 ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07001774 ex--;
1775 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001776 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001777 }
1778
1779 if (correct_index && eh->eh_entries)
1780 err = ext4_ext_correct_indexes(handle, inode, path);
1781
1782 /* if this leaf is free, then we should
1783 * remove it from index block above */
1784 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
1785 err = ext4_ext_rm_idx(handle, inode, path + depth);
1786
1787out:
1788 return err;
1789}
1790
1791/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001792 * ext4_ext_more_to_rm:
1793 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07001794 */
Avantika Mathur09b88252006-12-06 20:41:36 -08001795static int
Alex Tomasa86c6182006-10-11 01:21:03 -07001796ext4_ext_more_to_rm(struct ext4_ext_path *path)
1797{
1798 BUG_ON(path->p_idx == NULL);
1799
1800 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
1801 return 0;
1802
1803 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001804 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07001805 * so we have to consider current index for truncation
1806 */
1807 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
1808 return 0;
1809 return 1;
1810}
1811
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001812int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07001813{
1814 struct super_block *sb = inode->i_sb;
1815 int depth = ext_depth(inode);
1816 struct ext4_ext_path *path;
1817 handle_t *handle;
1818 int i = 0, err = 0;
1819
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001820 ext_debug("truncate since %u\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07001821
1822 /* probably first extent we're gonna free will be last in block */
1823 handle = ext4_journal_start(inode, depth + 1);
1824 if (IS_ERR(handle))
1825 return PTR_ERR(handle);
1826
1827 ext4_ext_invalidate_cache(inode);
1828
1829 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001830 * We start scanning from right side, freeing all the blocks
1831 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07001832 */
Avantika Mathur5d4958f2006-12-06 20:41:35 -08001833 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_KERNEL);
Alex Tomasa86c6182006-10-11 01:21:03 -07001834 if (path == NULL) {
1835 ext4_journal_stop(handle);
1836 return -ENOMEM;
1837 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001838 path[0].p_hdr = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -04001839 if (ext4_ext_check_header(inode, path[0].p_hdr, depth)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001840 err = -EIO;
1841 goto out;
1842 }
1843 path[0].p_depth = depth;
1844
1845 while (i >= 0 && err == 0) {
1846 if (i == depth) {
1847 /* this is leaf block */
1848 err = ext4_ext_rm_leaf(handle, inode, path, start);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001849 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07001850 brelse(path[i].p_bh);
1851 path[i].p_bh = NULL;
1852 i--;
1853 continue;
1854 }
1855
1856 /* this is index block */
1857 if (!path[i].p_hdr) {
1858 ext_debug("initialize header\n");
1859 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001860 }
1861
Alex Tomasa86c6182006-10-11 01:21:03 -07001862 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001863 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07001864 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
1865 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
1866 ext_debug("init index ptr: hdr 0x%p, num %d\n",
1867 path[i].p_hdr,
1868 le16_to_cpu(path[i].p_hdr->eh_entries));
1869 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001870 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07001871 path[i].p_idx--;
1872 }
1873
1874 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
1875 i, EXT_FIRST_INDEX(path[i].p_hdr),
1876 path[i].p_idx);
1877 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04001878 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001879 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07001880 ext_debug("move to level %d (block %llu)\n",
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001881 i + 1, idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07001882 memset(path + i + 1, 0, sizeof(*path));
Alex Tomasc29c0ae2007-07-18 09:19:09 -04001883 bh = sb_bread(sb, idx_pblock(path[i].p_idx));
1884 if (!bh) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001885 /* should we reset i_size? */
1886 err = -EIO;
1887 break;
1888 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04001889 if (WARN_ON(i + 1 > depth)) {
1890 err = -EIO;
1891 break;
1892 }
1893 if (ext4_ext_check_header(inode, ext_block_hdr(bh),
1894 depth - i - 1)) {
1895 err = -EIO;
1896 break;
1897 }
1898 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001899
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001900 /* save actual number of indexes since this
1901 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07001902 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
1903 i++;
1904 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001905 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07001906 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001907 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07001908 * handle must be already prepared by the
1909 * truncatei_leaf() */
1910 err = ext4_ext_rm_idx(handle, inode, path + i);
1911 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001912 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07001913 brelse(path[i].p_bh);
1914 path[i].p_bh = NULL;
1915 i--;
1916 ext_debug("return to level %d\n", i);
1917 }
1918 }
1919
1920 /* TODO: flexible tree reduction should be here */
1921 if (path->p_hdr->eh_entries == 0) {
1922 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001923 * truncate to zero freed all the tree,
1924 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07001925 */
1926 err = ext4_ext_get_access(handle, inode, path);
1927 if (err == 0) {
1928 ext_inode_hdr(inode)->eh_depth = 0;
1929 ext_inode_hdr(inode)->eh_max =
1930 cpu_to_le16(ext4_ext_space_root(inode));
1931 err = ext4_ext_dirty(handle, inode, path);
1932 }
1933 }
1934out:
1935 ext4_ext_tree_changed(inode);
1936 ext4_ext_drop_refs(path);
1937 kfree(path);
1938 ext4_journal_stop(handle);
1939
1940 return err;
1941}
1942
1943/*
1944 * called at mount time
1945 */
1946void ext4_ext_init(struct super_block *sb)
1947{
1948 /*
1949 * possible initialization would be here
1950 */
1951
1952 if (test_opt(sb, EXTENTS)) {
1953 printk("EXT4-fs: file extents enabled");
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001954#ifdef AGGRESSIVE_TEST
1955 printk(", aggressive tests");
Alex Tomasa86c6182006-10-11 01:21:03 -07001956#endif
1957#ifdef CHECK_BINSEARCH
1958 printk(", check binsearch");
1959#endif
1960#ifdef EXTENTS_STATS
1961 printk(", stats");
1962#endif
1963 printk("\n");
1964#ifdef EXTENTS_STATS
1965 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
1966 EXT4_SB(sb)->s_ext_min = 1 << 30;
1967 EXT4_SB(sb)->s_ext_max = 0;
1968#endif
1969 }
1970}
1971
1972/*
1973 * called at umount time
1974 */
1975void ext4_ext_release(struct super_block *sb)
1976{
1977 if (!test_opt(sb, EXTENTS))
1978 return;
1979
1980#ifdef EXTENTS_STATS
1981 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
1982 struct ext4_sb_info *sbi = EXT4_SB(sb);
1983 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
1984 sbi->s_ext_blocks, sbi->s_ext_extents,
1985 sbi->s_ext_blocks / sbi->s_ext_extents);
1986 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
1987 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
1988 }
1989#endif
1990}
1991
Amit Arora56055d32007-07-17 21:42:38 -04001992/*
1993 * This function is called by ext4_ext_get_blocks() if someone tries to write
1994 * to an uninitialized extent. It may result in splitting the uninitialized
1995 * extent into multiple extents (upto three - one initialized and two
1996 * uninitialized).
1997 * There are three possibilities:
1998 * a> There is no split required: Entire extent should be initialized
1999 * b> Splits in two extents: Write is happening at either end of the extent
2000 * c> Splits in three extents: Somone is writing in middle of the extent
2001 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002002static int ext4_ext_convert_to_initialized(handle_t *handle,
2003 struct inode *inode,
2004 struct ext4_ext_path *path,
2005 ext4_lblk_t iblock,
2006 unsigned long max_blocks)
Amit Arora56055d32007-07-17 21:42:38 -04002007{
2008 struct ext4_extent *ex, newex;
2009 struct ext4_extent *ex1 = NULL;
2010 struct ext4_extent *ex2 = NULL;
2011 struct ext4_extent *ex3 = NULL;
2012 struct ext4_extent_header *eh;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002013 ext4_lblk_t ee_block;
2014 unsigned int allocated, ee_len, depth;
Amit Arora56055d32007-07-17 21:42:38 -04002015 ext4_fsblk_t newblock;
2016 int err = 0;
2017 int ret = 0;
2018
2019 depth = ext_depth(inode);
2020 eh = path[depth].p_hdr;
2021 ex = path[depth].p_ext;
2022 ee_block = le32_to_cpu(ex->ee_block);
2023 ee_len = ext4_ext_get_actual_len(ex);
2024 allocated = ee_len - (iblock - ee_block);
2025 newblock = iblock - ee_block + ext_pblock(ex);
2026 ex2 = ex;
2027
2028 /* ex1: ee_block to iblock - 1 : uninitialized */
2029 if (iblock > ee_block) {
2030 ex1 = ex;
2031 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2032 ext4_ext_mark_uninitialized(ex1);
2033 ex2 = &newex;
2034 }
2035 /*
2036 * for sanity, update the length of the ex2 extent before
2037 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2038 * overlap of blocks.
2039 */
2040 if (!ex1 && allocated > max_blocks)
2041 ex2->ee_len = cpu_to_le16(max_blocks);
2042 /* ex3: to ee_block + ee_len : uninitialised */
2043 if (allocated > max_blocks) {
2044 unsigned int newdepth;
2045 ex3 = &newex;
2046 ex3->ee_block = cpu_to_le32(iblock + max_blocks);
2047 ext4_ext_store_pblock(ex3, newblock + max_blocks);
2048 ex3->ee_len = cpu_to_le16(allocated - max_blocks);
2049 ext4_ext_mark_uninitialized(ex3);
2050 err = ext4_ext_insert_extent(handle, inode, path, ex3);
2051 if (err)
2052 goto out;
2053 /*
2054 * The depth, and hence eh & ex might change
2055 * as part of the insert above.
2056 */
2057 newdepth = ext_depth(inode);
2058 if (newdepth != depth) {
2059 depth = newdepth;
2060 path = ext4_ext_find_extent(inode, iblock, NULL);
2061 if (IS_ERR(path)) {
2062 err = PTR_ERR(path);
2063 path = NULL;
2064 goto out;
2065 }
2066 eh = path[depth].p_hdr;
2067 ex = path[depth].p_ext;
2068 if (ex2 != &newex)
2069 ex2 = ex;
2070 }
2071 allocated = max_blocks;
2072 }
2073 /*
2074 * If there was a change of depth as part of the
2075 * insertion of ex3 above, we need to update the length
2076 * of the ex1 extent again here
2077 */
2078 if (ex1 && ex1 != ex) {
2079 ex1 = ex;
2080 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2081 ext4_ext_mark_uninitialized(ex1);
2082 ex2 = &newex;
2083 }
2084 /* ex2: iblock to iblock + maxblocks-1 : initialised */
2085 ex2->ee_block = cpu_to_le32(iblock);
Amit Arora56055d32007-07-17 21:42:38 -04002086 ext4_ext_store_pblock(ex2, newblock);
2087 ex2->ee_len = cpu_to_le16(allocated);
2088 if (ex2 != ex)
2089 goto insert;
2090 err = ext4_ext_get_access(handle, inode, path + depth);
2091 if (err)
2092 goto out;
2093 /*
2094 * New (initialized) extent starts from the first block
2095 * in the current extent. i.e., ex2 == ex
2096 * We have to see if it can be merged with the extent
2097 * on the left.
2098 */
2099 if (ex2 > EXT_FIRST_EXTENT(eh)) {
2100 /*
2101 * To merge left, pass "ex2 - 1" to try_to_merge(),
2102 * since it merges towards right _only_.
2103 */
2104 ret = ext4_ext_try_to_merge(inode, path, ex2 - 1);
2105 if (ret) {
2106 err = ext4_ext_correct_indexes(handle, inode, path);
2107 if (err)
2108 goto out;
2109 depth = ext_depth(inode);
2110 ex2--;
2111 }
2112 }
2113 /*
2114 * Try to Merge towards right. This might be required
2115 * only when the whole extent is being written to.
2116 * i.e. ex2 == ex and ex3 == NULL.
2117 */
2118 if (!ex3) {
2119 ret = ext4_ext_try_to_merge(inode, path, ex2);
2120 if (ret) {
2121 err = ext4_ext_correct_indexes(handle, inode, path);
2122 if (err)
2123 goto out;
2124 }
2125 }
2126 /* Mark modified extent as dirty */
2127 err = ext4_ext_dirty(handle, inode, path + depth);
2128 goto out;
2129insert:
2130 err = ext4_ext_insert_extent(handle, inode, path, &newex);
2131out:
2132 return err ? err : allocated;
2133}
2134
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002135int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002136 ext4_lblk_t iblock,
Alex Tomasa86c6182006-10-11 01:21:03 -07002137 unsigned long max_blocks, struct buffer_head *bh_result,
2138 int create, int extend_disksize)
2139{
2140 struct ext4_ext_path *path = NULL;
Amit Arora56055d32007-07-17 21:42:38 -04002141 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002142 struct ext4_extent newex, *ex;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002143 ext4_fsblk_t goal, newblock;
Amit Arora56055d32007-07-17 21:42:38 -04002144 int err = 0, depth, ret;
Alex Tomasa86c6182006-10-11 01:21:03 -07002145 unsigned long allocated = 0;
2146
2147 __clear_bit(BH_New, &bh_result->b_state);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002148 ext_debug("blocks %lu/%lu requested for inode %u\n",
2149 (unsigned long) iblock, max_blocks,
2150 (unsigned) inode->i_ino);
Alex Tomasa86c6182006-10-11 01:21:03 -07002151 mutex_lock(&EXT4_I(inode)->truncate_mutex);
2152
2153 /* check in cache */
Avantika Mathur7e028972006-12-06 20:41:33 -08002154 goal = ext4_ext_in_cache(inode, iblock, &newex);
2155 if (goal) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002156 if (goal == EXT4_EXT_CACHE_GAP) {
2157 if (!create) {
Amit Arora56055d32007-07-17 21:42:38 -04002158 /*
2159 * block isn't allocated yet and
2160 * user doesn't want to allocate it
2161 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002162 goto out2;
2163 }
2164 /* we should allocate requested block */
2165 } else if (goal == EXT4_EXT_CACHE_EXTENT) {
2166 /* block is already allocated */
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002167 newblock = iblock
2168 - le32_to_cpu(newex.ee_block)
2169 + ext_pblock(&newex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002170 /* number of remaining blocks in the extent */
Alex Tomasa86c6182006-10-11 01:21:03 -07002171 allocated = le16_to_cpu(newex.ee_len) -
2172 (iblock - le32_to_cpu(newex.ee_block));
2173 goto out;
2174 } else {
2175 BUG();
2176 }
2177 }
2178
2179 /* find extent for this block */
2180 path = ext4_ext_find_extent(inode, iblock, NULL);
2181 if (IS_ERR(path)) {
2182 err = PTR_ERR(path);
2183 path = NULL;
2184 goto out2;
2185 }
2186
2187 depth = ext_depth(inode);
2188
2189 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002190 * consistent leaf must not be empty;
2191 * this situation is possible, though, _during_ tree modification;
Alex Tomasa86c6182006-10-11 01:21:03 -07002192 * this is why assert can't be put in ext4_ext_find_extent()
2193 */
2194 BUG_ON(path[depth].p_ext == NULL && depth != 0);
Amit Arora56055d32007-07-17 21:42:38 -04002195 eh = path[depth].p_hdr;
Alex Tomasa86c6182006-10-11 01:21:03 -07002196
Avantika Mathur7e028972006-12-06 20:41:33 -08002197 ex = path[depth].p_ext;
2198 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002199 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002200 ext4_fsblk_t ee_start = ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002201 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07002202
2203 /*
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07002204 * Uninitialized extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04002205 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07002206 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04002207 ee_len = ext4_ext_get_actual_len(ex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002208 /* if found extent covers block, simply return it */
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002209 if (iblock >= ee_block && iblock < ee_block + ee_len) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002210 newblock = iblock - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002211 /* number of remaining blocks in the extent */
Alex Tomasa86c6182006-10-11 01:21:03 -07002212 allocated = ee_len - (iblock - ee_block);
Mingming Cao2ae02102006-10-11 01:21:11 -07002213 ext_debug("%d fit into %lu:%d -> %llu\n", (int) iblock,
Alex Tomasa86c6182006-10-11 01:21:03 -07002214 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04002215
Amit Aroraa2df2a62007-07-17 21:42:41 -04002216 /* Do not put uninitialized extent in the cache */
Amit Arora56055d32007-07-17 21:42:38 -04002217 if (!ext4_ext_is_uninitialized(ex)) {
Amit Aroraa2df2a62007-07-17 21:42:41 -04002218 ext4_ext_put_in_cache(inode, ee_block,
2219 ee_len, ee_start,
2220 EXT4_EXT_CACHE_EXTENT);
Amit Arora56055d32007-07-17 21:42:38 -04002221 goto out;
2222 }
2223 if (create == EXT4_CREATE_UNINITIALIZED_EXT)
2224 goto out;
2225 if (!create)
2226 goto out2;
2227
2228 ret = ext4_ext_convert_to_initialized(handle, inode,
2229 path, iblock,
2230 max_blocks);
2231 if (ret <= 0)
2232 goto out2;
2233 else
2234 allocated = ret;
2235 goto outnew;
Alex Tomasa86c6182006-10-11 01:21:03 -07002236 }
2237 }
2238
2239 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002240 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07002241 * we couldn't try to create block if create flag is zero
2242 */
2243 if (!create) {
Amit Arora56055d32007-07-17 21:42:38 -04002244 /*
2245 * put just found gap into cache to speed up
2246 * subsequent requests
2247 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002248 ext4_ext_put_gap_in_cache(inode, path, iblock);
2249 goto out2;
2250 }
2251 /*
Andrew Morton63f57932006-10-11 01:21:24 -07002252 * Okay, we need to do block allocation. Lazily initialize the block
2253 * allocation info here if necessary.
2254 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002255 if (S_ISREG(inode->i_mode) && (!EXT4_I(inode)->i_block_alloc_info))
2256 ext4_init_block_alloc_info(inode);
2257
2258 /* allocate new block */
2259 goal = ext4_ext_find_goal(inode, path, iblock);
Amit Arora25d14f92007-05-24 13:04:13 -04002260
Amit Arora749269f2007-07-18 09:02:56 -04002261 /*
2262 * See if request is beyond maximum number of blocks we can have in
2263 * a single extent. For an initialized extent this limit is
2264 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
2265 * EXT_UNINIT_MAX_LEN.
2266 */
2267 if (max_blocks > EXT_INIT_MAX_LEN &&
2268 create != EXT4_CREATE_UNINITIALIZED_EXT)
2269 max_blocks = EXT_INIT_MAX_LEN;
2270 else if (max_blocks > EXT_UNINIT_MAX_LEN &&
2271 create == EXT4_CREATE_UNINITIALIZED_EXT)
2272 max_blocks = EXT_UNINIT_MAX_LEN;
2273
Amit Arora25d14f92007-05-24 13:04:13 -04002274 /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */
2275 newex.ee_block = cpu_to_le32(iblock);
2276 newex.ee_len = cpu_to_le16(max_blocks);
2277 err = ext4_ext_check_overlap(inode, &newex, path);
2278 if (err)
2279 allocated = le16_to_cpu(newex.ee_len);
2280 else
2281 allocated = max_blocks;
Alex Tomasa86c6182006-10-11 01:21:03 -07002282 newblock = ext4_new_blocks(handle, inode, goal, &allocated, &err);
2283 if (!newblock)
2284 goto out2;
Mingming Cao2ae02102006-10-11 01:21:11 -07002285 ext_debug("allocate new block: goal %llu, found %llu/%lu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07002286 goal, newblock, allocated);
2287
2288 /* try to insert new extent into found leaf and return */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002289 ext4_ext_store_pblock(&newex, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07002290 newex.ee_len = cpu_to_le16(allocated);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002291 if (create == EXT4_CREATE_UNINITIALIZED_EXT) /* Mark uninitialized */
2292 ext4_ext_mark_uninitialized(&newex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002293 err = ext4_ext_insert_extent(handle, inode, path, &newex);
Alex Tomas315054f2007-05-24 13:04:25 -04002294 if (err) {
2295 /* free data blocks we just allocated */
2296 ext4_free_blocks(handle, inode, ext_pblock(&newex),
2297 le16_to_cpu(newex.ee_len));
Alex Tomasa86c6182006-10-11 01:21:03 -07002298 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04002299 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002300
2301 if (extend_disksize && inode->i_size > EXT4_I(inode)->i_disksize)
2302 EXT4_I(inode)->i_disksize = inode->i_size;
2303
2304 /* previous routine could use block we allocated */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002305 newblock = ext_pblock(&newex);
Amit Arora56055d32007-07-17 21:42:38 -04002306outnew:
Alex Tomasa86c6182006-10-11 01:21:03 -07002307 __set_bit(BH_New, &bh_result->b_state);
2308
Amit Aroraa2df2a62007-07-17 21:42:41 -04002309 /* Cache only when it is _not_ an uninitialized extent */
2310 if (create != EXT4_CREATE_UNINITIALIZED_EXT)
2311 ext4_ext_put_in_cache(inode, iblock, allocated, newblock,
2312 EXT4_EXT_CACHE_EXTENT);
Alex Tomasa86c6182006-10-11 01:21:03 -07002313out:
2314 if (allocated > max_blocks)
2315 allocated = max_blocks;
2316 ext4_ext_show_leaf(inode, path);
2317 __set_bit(BH_Mapped, &bh_result->b_state);
2318 bh_result->b_bdev = inode->i_sb->s_bdev;
2319 bh_result->b_blocknr = newblock;
2320out2:
2321 if (path) {
2322 ext4_ext_drop_refs(path);
2323 kfree(path);
2324 }
2325 mutex_unlock(&EXT4_I(inode)->truncate_mutex);
2326
2327 return err ? err : allocated;
2328}
2329
2330void ext4_ext_truncate(struct inode * inode, struct page *page)
2331{
2332 struct address_space *mapping = inode->i_mapping;
2333 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002334 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002335 handle_t *handle;
2336 int err = 0;
2337
2338 /*
2339 * probably first extent we're gonna free will be last in block
2340 */
2341 err = ext4_writepage_trans_blocks(inode) + 3;
2342 handle = ext4_journal_start(inode, err);
2343 if (IS_ERR(handle)) {
2344 if (page) {
2345 clear_highpage(page);
2346 flush_dcache_page(page);
2347 unlock_page(page);
2348 page_cache_release(page);
2349 }
2350 return;
2351 }
2352
2353 if (page)
2354 ext4_block_truncate_page(handle, page, mapping, inode->i_size);
2355
2356 mutex_lock(&EXT4_I(inode)->truncate_mutex);
2357 ext4_ext_invalidate_cache(inode);
2358
2359 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002360 * TODO: optimization is possible here.
2361 * Probably we need not scan at all,
2362 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07002363 */
2364 if (ext4_orphan_add(handle, inode))
2365 goto out_stop;
2366
2367 /* we have to know where to truncate from in crash case */
2368 EXT4_I(inode)->i_disksize = inode->i_size;
2369 ext4_mark_inode_dirty(handle, inode);
2370
2371 last_block = (inode->i_size + sb->s_blocksize - 1)
2372 >> EXT4_BLOCK_SIZE_BITS(sb);
2373 err = ext4_ext_remove_space(inode, last_block);
2374
2375 /* In a multi-transaction truncate, we only make the final
Amit Arora56055d32007-07-17 21:42:38 -04002376 * transaction synchronous.
2377 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002378 if (IS_SYNC(inode))
2379 handle->h_sync = 1;
2380
2381out_stop:
2382 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002383 * If this was a simple ftruncate() and the file will remain alive,
Alex Tomasa86c6182006-10-11 01:21:03 -07002384 * then we need to clear up the orphan record which we created above.
2385 * However, if this was a real unlink then we were called by
2386 * ext4_delete_inode(), and we allow that function to clean up the
2387 * orphan info for us.
2388 */
2389 if (inode->i_nlink)
2390 ext4_orphan_del(handle, inode);
2391
2392 mutex_unlock(&EXT4_I(inode)->truncate_mutex);
2393 ext4_journal_stop(handle);
2394}
2395
2396/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002397 * ext4_ext_writepage_trans_blocks:
2398 * calculate max number of blocks we could modify
Alex Tomasa86c6182006-10-11 01:21:03 -07002399 * in order to allocate new block for an inode
2400 */
2401int ext4_ext_writepage_trans_blocks(struct inode *inode, int num)
2402{
2403 int needed;
2404
2405 needed = ext4_ext_calc_credits_for_insert(inode, NULL);
2406
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002407 /* caller wants to allocate num blocks, but note it includes sb */
Alex Tomasa86c6182006-10-11 01:21:03 -07002408 needed = needed * num - (num - 1);
2409
2410#ifdef CONFIG_QUOTA
2411 needed += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
2412#endif
2413
2414 return needed;
2415}
Amit Aroraa2df2a62007-07-17 21:42:41 -04002416
2417/*
2418 * preallocate space for a file. This implements ext4's fallocate inode
2419 * operation, which gets called from sys_fallocate system call.
2420 * For block-mapped files, posix_fallocate should fall back to the method
2421 * of writing zeroes to the required new blocks (the same behavior which is
2422 * expected for file systems which do not support fallocate() system call).
2423 */
2424long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
2425{
2426 handle_t *handle;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002427 ext4_lblk_t block;
2428 unsigned long max_blocks;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002429 ext4_fsblk_t nblocks = 0;
2430 int ret = 0;
2431 int ret2 = 0;
2432 int retries = 0;
2433 struct buffer_head map_bh;
2434 unsigned int credits, blkbits = inode->i_blkbits;
2435
2436 /*
2437 * currently supporting (pre)allocate mode for extent-based
2438 * files _only_
2439 */
2440 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
2441 return -EOPNOTSUPP;
2442
2443 /* preallocation to directories is currently not supported */
2444 if (S_ISDIR(inode->i_mode))
2445 return -ENODEV;
2446
2447 block = offset >> blkbits;
2448 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
2449 - block;
2450
2451 /*
2452 * credits to insert 1 extent into extent tree + buffers to be able to
2453 * modify 1 super block, 1 block bitmap and 1 group descriptor.
2454 */
2455 credits = EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + 3;
2456retry:
2457 while (ret >= 0 && ret < max_blocks) {
2458 block = block + ret;
2459 max_blocks = max_blocks - ret;
2460 handle = ext4_journal_start(inode, credits);
2461 if (IS_ERR(handle)) {
2462 ret = PTR_ERR(handle);
2463 break;
2464 }
2465
2466 ret = ext4_ext_get_blocks(handle, inode, block,
2467 max_blocks, &map_bh,
2468 EXT4_CREATE_UNINITIALIZED_EXT, 0);
2469 WARN_ON(!ret);
2470 if (!ret) {
2471 ext4_error(inode->i_sb, "ext4_fallocate",
2472 "ext4_ext_get_blocks returned 0! inode#%lu"
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002473 ", block=%lu, max_blocks=%lu",
2474 inode->i_ino, (unsigned long)block,
2475 (unsigned long)max_blocks);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002476 ret = -EIO;
2477 ext4_mark_inode_dirty(handle, inode);
2478 ret2 = ext4_journal_stop(handle);
2479 break;
2480 }
2481 if (ret > 0) {
2482 /* check wrap through sign-bit/zero here */
2483 if ((block + ret) < 0 || (block + ret) < block) {
2484 ret = -EIO;
2485 ext4_mark_inode_dirty(handle, inode);
2486 ret2 = ext4_journal_stop(handle);
2487 break;
2488 }
2489 if (buffer_new(&map_bh) && ((block + ret) >
2490 (EXT4_BLOCK_ALIGN(i_size_read(inode), blkbits)
2491 >> blkbits)))
2492 nblocks = nblocks + ret;
2493 }
2494
2495 /* Update ctime if new blocks get allocated */
2496 if (nblocks) {
2497 struct timespec now;
2498
2499 now = current_fs_time(inode->i_sb);
2500 if (!timespec_equal(&inode->i_ctime, &now))
2501 inode->i_ctime = now;
2502 }
2503
2504 ext4_mark_inode_dirty(handle, inode);
2505 ret2 = ext4_journal_stop(handle);
2506 if (ret2)
2507 break;
2508 }
2509
2510 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
2511 goto retry;
2512
2513 /*
2514 * Time to update the file size.
2515 * Update only when preallocation was requested beyond the file size.
2516 */
2517 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
2518 (offset + len) > i_size_read(inode)) {
2519 if (ret > 0) {
2520 /*
2521 * if no error, we assume preallocation succeeded
2522 * completely
2523 */
2524 mutex_lock(&inode->i_mutex);
2525 i_size_write(inode, offset + len);
2526 EXT4_I(inode)->i_disksize = i_size_read(inode);
2527 mutex_unlock(&inode->i_mutex);
2528 } else if (ret < 0 && nblocks) {
2529 /* Handle partial allocation scenario */
2530 loff_t newsize;
2531
2532 mutex_lock(&inode->i_mutex);
2533 newsize = (nblocks << blkbits) + i_size_read(inode);
2534 i_size_write(inode, EXT4_BLOCK_ALIGN(newsize, blkbits));
2535 EXT4_I(inode)->i_disksize = i_size_read(inode);
2536 mutex_unlock(&inode->i_mutex);
2537 }
2538 }
2539
2540 return ret > 0 ? ret2 : ret;
2541}