blob: 96264b2ef0a386208b69935013f2b22eac9d7c68 [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>
36#include <linux/jbd.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
55 block = le32_to_cpu(ex->ee_start);
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
68 block = le32_to_cpu(ix->ei_leaf);
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{
80 ex->ee_start = 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{
91 ix->ei_leaf = 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,
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700147 ext4_fsblk_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
370ext4_ext_binsearch_idx(struct inode *inode, struct ext4_ext_path *path, int block)
371{
372 struct ext4_extent_header *eh = path->p_hdr;
373 struct ext4_extent_idx *r, *l, *m;
374
Alex Tomasa86c6182006-10-11 01:21:03 -0700375
376 ext_debug("binsearch for %d(idx): ", block);
377
378 l = EXT_FIRST_INDEX(eh) + 1;
379 r = EXT_FIRST_INDEX(eh) + le16_to_cpu(eh->eh_entries) - 1;
380 while (l <= r) {
381 m = l + (r - l) / 2;
382 if (block < le32_to_cpu(m->ei_block))
383 r = m - 1;
384 else
385 l = m + 1;
386 ext_debug("%p(%u):%p(%u):%p(%u) ", l, l->ei_block,
387 m, m->ei_block, r, r->ei_block);
388 }
389
390 path->p_idx = l - 1;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700391 ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
392 idx_block(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700393
394#ifdef CHECK_BINSEARCH
395 {
396 struct ext4_extent_idx *chix, *ix;
397 int k;
398
399 chix = ix = EXT_FIRST_INDEX(eh);
400 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
401 if (k != 0 &&
402 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
403 printk("k=%d, ix=0x%p, first=0x%p\n", k,
404 ix, EXT_FIRST_INDEX(eh));
405 printk("%u <= %u\n",
406 le32_to_cpu(ix->ei_block),
407 le32_to_cpu(ix[-1].ei_block));
408 }
409 BUG_ON(k && le32_to_cpu(ix->ei_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400410 <= le32_to_cpu(ix[-1].ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700411 if (block < le32_to_cpu(ix->ei_block))
412 break;
413 chix = ix;
414 }
415 BUG_ON(chix != path->p_idx);
416 }
417#endif
418
419}
420
421/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700422 * ext4_ext_binsearch:
423 * binary search for closest extent of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400424 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700425 */
426static void
427ext4_ext_binsearch(struct inode *inode, struct ext4_ext_path *path, int block)
428{
429 struct ext4_extent_header *eh = path->p_hdr;
430 struct ext4_extent *r, *l, *m;
431
Alex Tomasa86c6182006-10-11 01:21:03 -0700432 if (eh->eh_entries == 0) {
433 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700434 * this leaf is empty:
435 * we get such a leaf in split/add case
Alex Tomasa86c6182006-10-11 01:21:03 -0700436 */
437 return;
438 }
439
440 ext_debug("binsearch for %d: ", block);
441
442 l = EXT_FIRST_EXTENT(eh) + 1;
443 r = EXT_FIRST_EXTENT(eh) + le16_to_cpu(eh->eh_entries) - 1;
444
445 while (l <= r) {
446 m = l + (r - l) / 2;
447 if (block < le32_to_cpu(m->ee_block))
448 r = m - 1;
449 else
450 l = m + 1;
451 ext_debug("%p(%u):%p(%u):%p(%u) ", l, l->ee_block,
452 m, m->ee_block, r, r->ee_block);
453 }
454
455 path->p_ext = l - 1;
Mingming Cao2ae02102006-10-11 01:21:11 -0700456 ext_debug(" -> %d:%llu:%d ",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400457 le32_to_cpu(path->p_ext->ee_block),
458 ext_pblock(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400459 ext4_ext_get_actual_len(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700460
461#ifdef CHECK_BINSEARCH
462 {
463 struct ext4_extent *chex, *ex;
464 int k;
465
466 chex = ex = EXT_FIRST_EXTENT(eh);
467 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
468 BUG_ON(k && le32_to_cpu(ex->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400469 <= le32_to_cpu(ex[-1].ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700470 if (block < le32_to_cpu(ex->ee_block))
471 break;
472 chex = ex;
473 }
474 BUG_ON(chex != path->p_ext);
475 }
476#endif
477
478}
479
480int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
481{
482 struct ext4_extent_header *eh;
483
484 eh = ext_inode_hdr(inode);
485 eh->eh_depth = 0;
486 eh->eh_entries = 0;
487 eh->eh_magic = EXT4_EXT_MAGIC;
488 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode));
489 ext4_mark_inode_dirty(handle, inode);
490 ext4_ext_invalidate_cache(inode);
491 return 0;
492}
493
494struct ext4_ext_path *
495ext4_ext_find_extent(struct inode *inode, int block, struct ext4_ext_path *path)
496{
497 struct ext4_extent_header *eh;
498 struct buffer_head *bh;
499 short int depth, i, ppos = 0, alloc = 0;
500
501 eh = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400502 depth = ext_depth(inode);
503 if (ext4_ext_check_header(inode, eh, depth))
Alex Tomasa86c6182006-10-11 01:21:03 -0700504 return ERR_PTR(-EIO);
505
Alex Tomasa86c6182006-10-11 01:21:03 -0700506
507 /* account possible depth increase */
508 if (!path) {
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800509 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
Alex Tomasa86c6182006-10-11 01:21:03 -0700510 GFP_NOFS);
511 if (!path)
512 return ERR_PTR(-ENOMEM);
513 alloc = 1;
514 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700515 path[0].p_hdr = eh;
516
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400517 i = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700518 /* walk through the tree */
519 while (i) {
520 ext_debug("depth %d: num %d, max %d\n",
521 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400522
Alex Tomasa86c6182006-10-11 01:21:03 -0700523 ext4_ext_binsearch_idx(inode, path + ppos, block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700524 path[ppos].p_block = idx_pblock(path[ppos].p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -0700525 path[ppos].p_depth = i;
526 path[ppos].p_ext = NULL;
527
528 bh = sb_bread(inode->i_sb, path[ppos].p_block);
529 if (!bh)
530 goto err;
531
532 eh = ext_block_hdr(bh);
533 ppos++;
534 BUG_ON(ppos > depth);
535 path[ppos].p_bh = bh;
536 path[ppos].p_hdr = eh;
537 i--;
538
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400539 if (ext4_ext_check_header(inode, eh, i))
Alex Tomasa86c6182006-10-11 01:21:03 -0700540 goto err;
541 }
542
543 path[ppos].p_depth = i;
544 path[ppos].p_hdr = eh;
545 path[ppos].p_ext = NULL;
546 path[ppos].p_idx = NULL;
547
Alex Tomasa86c6182006-10-11 01:21:03 -0700548 /* find extent */
549 ext4_ext_binsearch(inode, path + ppos, block);
550
551 ext4_ext_show_path(inode, path);
552
553 return path;
554
555err:
556 ext4_ext_drop_refs(path);
557 if (alloc)
558 kfree(path);
559 return ERR_PTR(-EIO);
560}
561
562/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700563 * ext4_ext_insert_index:
564 * insert new index [@logical;@ptr] into the block at @curp;
565 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700566 */
567static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
568 struct ext4_ext_path *curp,
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700569 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700570{
571 struct ext4_extent_idx *ix;
572 int len, err;
573
Avantika Mathur7e028972006-12-06 20:41:33 -0800574 err = ext4_ext_get_access(handle, inode, curp);
575 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700576 return err;
577
578 BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block));
579 len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
580 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
581 /* insert after */
582 if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
583 len = (len - 1) * sizeof(struct ext4_extent_idx);
584 len = len < 0 ? 0 : len;
585 ext_debug("insert new index %d after: %d. "
586 "move %d from 0x%p to 0x%p\n",
587 logical, ptr, len,
588 (curp->p_idx + 1), (curp->p_idx + 2));
589 memmove(curp->p_idx + 2, curp->p_idx + 1, len);
590 }
591 ix = curp->p_idx + 1;
592 } else {
593 /* insert before */
594 len = len * sizeof(struct ext4_extent_idx);
595 len = len < 0 ? 0 : len;
596 ext_debug("insert new index %d before: %d. "
597 "move %d from 0x%p to 0x%p\n",
598 logical, ptr, len,
599 curp->p_idx, (curp->p_idx + 1));
600 memmove(curp->p_idx + 1, curp->p_idx, len);
601 ix = curp->p_idx;
602 }
603
604 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700605 ext4_idx_store_pblock(ix, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700606 curp->p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(curp->p_hdr->eh_entries)+1);
607
608 BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400609 > le16_to_cpu(curp->p_hdr->eh_max));
Alex Tomasa86c6182006-10-11 01:21:03 -0700610 BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr));
611
612 err = ext4_ext_dirty(handle, inode, curp);
613 ext4_std_error(inode->i_sb, err);
614
615 return err;
616}
617
618/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700619 * ext4_ext_split:
620 * inserts new subtree into the path, using free index entry
621 * at depth @at:
622 * - allocates all needed blocks (new leaf and all intermediate index blocks)
623 * - makes decision where to split
624 * - moves remaining extents and index entries (right to the split point)
625 * into the newly allocated blocks
626 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -0700627 */
628static int ext4_ext_split(handle_t *handle, struct inode *inode,
629 struct ext4_ext_path *path,
630 struct ext4_extent *newext, int at)
631{
632 struct buffer_head *bh = NULL;
633 int depth = ext_depth(inode);
634 struct ext4_extent_header *neh;
635 struct ext4_extent_idx *fidx;
636 struct ext4_extent *ex;
637 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700638 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700639 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700640 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Alex Tomasa86c6182006-10-11 01:21:03 -0700641 int err = 0;
642
643 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700644 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -0700645
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700646 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -0700647 * border from split point */
648 BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr));
649 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
650 border = path[depth].p_ext[1].ee_block;
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700651 ext_debug("leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -0700652 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400653 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700654 } else {
655 border = newext->ee_block;
656 ext_debug("leaf will be added."
657 " 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 }
660
661 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700662 * If error occurs, then we break processing
663 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -0700664 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700665 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -0700666 */
667
668 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700669 * Get array to track all allocated blocks.
670 * We need this to handle errors and free blocks
671 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -0700672 */
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800673 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -0700674 if (!ablocks)
675 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -0700676
677 /* allocate all needed blocks */
678 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
679 for (a = 0; a < depth - at; a++) {
680 newblock = ext4_ext_new_block(handle, inode, path, newext, &err);
681 if (newblock == 0)
682 goto cleanup;
683 ablocks[a] = newblock;
684 }
685
686 /* initialize new leaf */
687 newblock = ablocks[--a];
688 BUG_ON(newblock == 0);
689 bh = sb_getblk(inode->i_sb, newblock);
690 if (!bh) {
691 err = -EIO;
692 goto cleanup;
693 }
694 lock_buffer(bh);
695
Avantika Mathur7e028972006-12-06 20:41:33 -0800696 err = ext4_journal_get_create_access(handle, bh);
697 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700698 goto cleanup;
699
700 neh = ext_block_hdr(bh);
701 neh->eh_entries = 0;
702 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
703 neh->eh_magic = EXT4_EXT_MAGIC;
704 neh->eh_depth = 0;
705 ex = EXT_FIRST_EXTENT(neh);
706
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700707 /* move remainder of path[depth] to the new leaf */
Alex Tomasa86c6182006-10-11 01:21:03 -0700708 BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max);
709 /* start copy from next extent */
710 /* TODO: we could do it by single memmove */
711 m = 0;
712 path[depth].p_ext++;
713 while (path[depth].p_ext <=
714 EXT_MAX_EXTENT(path[depth].p_hdr)) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700715 ext_debug("move %d:%llu:%d in new leaf %llu\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400716 le32_to_cpu(path[depth].p_ext->ee_block),
717 ext_pblock(path[depth].p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400718 ext4_ext_get_actual_len(path[depth].p_ext),
Alex Tomasa86c6182006-10-11 01:21:03 -0700719 newblock);
720 /*memmove(ex++, path[depth].p_ext++,
721 sizeof(struct ext4_extent));
722 neh->eh_entries++;*/
723 path[depth].p_ext++;
724 m++;
725 }
726 if (m) {
727 memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
728 neh->eh_entries = cpu_to_le16(le16_to_cpu(neh->eh_entries)+m);
729 }
730
731 set_buffer_uptodate(bh);
732 unlock_buffer(bh);
733
Avantika Mathur7e028972006-12-06 20:41:33 -0800734 err = ext4_journal_dirty_metadata(handle, bh);
735 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700736 goto cleanup;
737 brelse(bh);
738 bh = NULL;
739
740 /* correct old leaf */
741 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -0800742 err = ext4_ext_get_access(handle, inode, path + depth);
743 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700744 goto cleanup;
745 path[depth].p_hdr->eh_entries =
746 cpu_to_le16(le16_to_cpu(path[depth].p_hdr->eh_entries)-m);
Avantika Mathur7e028972006-12-06 20:41:33 -0800747 err = ext4_ext_dirty(handle, inode, path + depth);
748 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700749 goto cleanup;
750
751 }
752
753 /* create intermediate indexes */
754 k = depth - at - 1;
755 BUG_ON(k < 0);
756 if (k)
757 ext_debug("create %d intermediate indices\n", k);
758 /* insert new index into current index block */
759 /* current depth stored in i var */
760 i = depth - 1;
761 while (k--) {
762 oldblock = newblock;
763 newblock = ablocks[--a];
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700764 bh = sb_getblk(inode->i_sb, (ext4_fsblk_t)newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700765 if (!bh) {
766 err = -EIO;
767 goto cleanup;
768 }
769 lock_buffer(bh);
770
Avantika Mathur7e028972006-12-06 20:41:33 -0800771 err = ext4_journal_get_create_access(handle, bh);
772 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700773 goto cleanup;
774
775 neh = ext_block_hdr(bh);
776 neh->eh_entries = cpu_to_le16(1);
777 neh->eh_magic = EXT4_EXT_MAGIC;
778 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
779 neh->eh_depth = cpu_to_le16(depth - i);
780 fidx = EXT_FIRST_INDEX(neh);
781 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700782 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700783
Mingming Cao2ae02102006-10-11 01:21:11 -0700784 ext_debug("int.index at %d (block %llu): %lu -> %llu\n", i,
Alex Tomasa86c6182006-10-11 01:21:03 -0700785 newblock, (unsigned long) le32_to_cpu(border),
786 oldblock);
787 /* copy indexes */
788 m = 0;
789 path[i].p_idx++;
790
791 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
792 EXT_MAX_INDEX(path[i].p_hdr));
793 BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) !=
794 EXT_LAST_INDEX(path[i].p_hdr));
795 while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700796 ext_debug("%d: move %d:%d in new index %llu\n", i,
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400797 le32_to_cpu(path[i].p_idx->ei_block),
798 idx_pblock(path[i].p_idx),
799 newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700800 /*memmove(++fidx, path[i].p_idx++,
801 sizeof(struct ext4_extent_idx));
802 neh->eh_entries++;
803 BUG_ON(neh->eh_entries > neh->eh_max);*/
804 path[i].p_idx++;
805 m++;
806 }
807 if (m) {
808 memmove(++fidx, path[i].p_idx - m,
809 sizeof(struct ext4_extent_idx) * m);
810 neh->eh_entries =
811 cpu_to_le16(le16_to_cpu(neh->eh_entries) + m);
812 }
813 set_buffer_uptodate(bh);
814 unlock_buffer(bh);
815
Avantika Mathur7e028972006-12-06 20:41:33 -0800816 err = ext4_journal_dirty_metadata(handle, bh);
817 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700818 goto cleanup;
819 brelse(bh);
820 bh = NULL;
821
822 /* correct old index */
823 if (m) {
824 err = ext4_ext_get_access(handle, inode, path + i);
825 if (err)
826 goto cleanup;
827 path[i].p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(path[i].p_hdr->eh_entries)-m);
828 err = ext4_ext_dirty(handle, inode, path + i);
829 if (err)
830 goto cleanup;
831 }
832
833 i--;
834 }
835
836 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -0700837 err = ext4_ext_insert_index(handle, inode, path + at,
838 le32_to_cpu(border), newblock);
839
840cleanup:
841 if (bh) {
842 if (buffer_locked(bh))
843 unlock_buffer(bh);
844 brelse(bh);
845 }
846
847 if (err) {
848 /* free all allocated blocks in error case */
849 for (i = 0; i < depth; i++) {
850 if (!ablocks[i])
851 continue;
852 ext4_free_blocks(handle, inode, ablocks[i], 1);
853 }
854 }
855 kfree(ablocks);
856
857 return err;
858}
859
860/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700861 * ext4_ext_grow_indepth:
862 * implements tree growing procedure:
863 * - allocates new block
864 * - moves top-level data (index block or leaf) into the new block
865 * - initializes new top-level, creating index that points to the
866 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -0700867 */
868static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
869 struct ext4_ext_path *path,
870 struct ext4_extent *newext)
871{
872 struct ext4_ext_path *curp = path;
873 struct ext4_extent_header *neh;
874 struct ext4_extent_idx *fidx;
875 struct buffer_head *bh;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700876 ext4_fsblk_t newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700877 int err = 0;
878
879 newblock = ext4_ext_new_block(handle, inode, path, newext, &err);
880 if (newblock == 0)
881 return err;
882
883 bh = sb_getblk(inode->i_sb, newblock);
884 if (!bh) {
885 err = -EIO;
886 ext4_std_error(inode->i_sb, err);
887 return err;
888 }
889 lock_buffer(bh);
890
Avantika Mathur7e028972006-12-06 20:41:33 -0800891 err = ext4_journal_get_create_access(handle, bh);
892 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -0700893 unlock_buffer(bh);
894 goto out;
895 }
896
897 /* move top-level index/leaf into new block */
898 memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
899
900 /* set size of new block */
901 neh = ext_block_hdr(bh);
902 /* old root could have indexes or leaves
903 * so calculate e_max right way */
904 if (ext_depth(inode))
905 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
906 else
907 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
908 neh->eh_magic = EXT4_EXT_MAGIC;
909 set_buffer_uptodate(bh);
910 unlock_buffer(bh);
911
Avantika Mathur7e028972006-12-06 20:41:33 -0800912 err = ext4_journal_dirty_metadata(handle, bh);
913 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700914 goto out;
915
916 /* create index in new top-level index: num,max,pointer */
Avantika Mathur7e028972006-12-06 20:41:33 -0800917 err = ext4_ext_get_access(handle, inode, curp);
918 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700919 goto out;
920
921 curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
922 curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode));
923 curp->p_hdr->eh_entries = cpu_to_le16(1);
924 curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
925 /* FIXME: it works, but actually path[0] can be index */
926 curp->p_idx->ei_block = EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700927 ext4_idx_store_pblock(curp->p_idx, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700928
929 neh = ext_inode_hdr(inode);
930 fidx = EXT_FIRST_INDEX(neh);
Mingming Cao2ae02102006-10-11 01:21:11 -0700931 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700932 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700933 le32_to_cpu(fidx->ei_block), idx_pblock(fidx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700934
935 neh->eh_depth = cpu_to_le16(path->p_depth + 1);
936 err = ext4_ext_dirty(handle, inode, curp);
937out:
938 brelse(bh);
939
940 return err;
941}
942
943/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700944 * ext4_ext_create_new_leaf:
945 * finds empty index and adds new leaf.
946 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -0700947 */
948static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
949 struct ext4_ext_path *path,
950 struct ext4_extent *newext)
951{
952 struct ext4_ext_path *curp;
953 int depth, i, err = 0;
954
955repeat:
956 i = depth = ext_depth(inode);
957
958 /* walk up to the tree and look for free index entry */
959 curp = path + depth;
960 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
961 i--;
962 curp--;
963 }
964
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700965 /* we use already allocated block for index block,
966 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -0700967 if (EXT_HAS_FREE_INDEX(curp)) {
968 /* if we found index with free entry, then use that
969 * entry: create all needed subtree and add new leaf */
970 err = ext4_ext_split(handle, inode, path, newext, i);
971
972 /* refill path */
973 ext4_ext_drop_refs(path);
974 path = ext4_ext_find_extent(inode,
975 le32_to_cpu(newext->ee_block),
976 path);
977 if (IS_ERR(path))
978 err = PTR_ERR(path);
979 } else {
980 /* tree is full, time to grow in depth */
981 err = ext4_ext_grow_indepth(handle, inode, path, newext);
982 if (err)
983 goto out;
984
985 /* refill path */
986 ext4_ext_drop_refs(path);
987 path = ext4_ext_find_extent(inode,
988 le32_to_cpu(newext->ee_block),
989 path);
990 if (IS_ERR(path)) {
991 err = PTR_ERR(path);
992 goto out;
993 }
994
995 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700996 * only first (depth 0 -> 1) produces free space;
997 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -0700998 */
999 depth = ext_depth(inode);
1000 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001001 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001002 goto repeat;
1003 }
1004 }
1005
1006out:
1007 return err;
1008}
1009
1010/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001011 * ext4_ext_next_allocated_block:
1012 * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1013 * NOTE: it considers block number from index entry as
1014 * allocated block. Thus, index entries have to be consistent
1015 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001016 */
1017static unsigned long
1018ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1019{
1020 int depth;
1021
1022 BUG_ON(path == NULL);
1023 depth = path->p_depth;
1024
1025 if (depth == 0 && path->p_ext == NULL)
1026 return EXT_MAX_BLOCK;
1027
1028 while (depth >= 0) {
1029 if (depth == path->p_depth) {
1030 /* leaf */
1031 if (path[depth].p_ext !=
1032 EXT_LAST_EXTENT(path[depth].p_hdr))
1033 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1034 } else {
1035 /* index */
1036 if (path[depth].p_idx !=
1037 EXT_LAST_INDEX(path[depth].p_hdr))
1038 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1039 }
1040 depth--;
1041 }
1042
1043 return EXT_MAX_BLOCK;
1044}
1045
1046/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001047 * ext4_ext_next_leaf_block:
Alex Tomasa86c6182006-10-11 01:21:03 -07001048 * returns first allocated block from next leaf or EXT_MAX_BLOCK
1049 */
1050static unsigned ext4_ext_next_leaf_block(struct inode *inode,
Andrew Morton63f57932006-10-11 01:21:24 -07001051 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001052{
1053 int depth;
1054
1055 BUG_ON(path == NULL);
1056 depth = path->p_depth;
1057
1058 /* zero-tree has no leaf blocks at all */
1059 if (depth == 0)
1060 return EXT_MAX_BLOCK;
1061
1062 /* go to index block */
1063 depth--;
1064
1065 while (depth >= 0) {
1066 if (path[depth].p_idx !=
1067 EXT_LAST_INDEX(path[depth].p_hdr))
1068 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1069 depth--;
1070 }
1071
1072 return EXT_MAX_BLOCK;
1073}
1074
1075/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001076 * ext4_ext_correct_indexes:
1077 * if leaf gets modified and modified extent is first in the leaf,
1078 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001079 * TODO: do we need to correct tree in all cases?
1080 */
1081int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
1082 struct ext4_ext_path *path)
1083{
1084 struct ext4_extent_header *eh;
1085 int depth = ext_depth(inode);
1086 struct ext4_extent *ex;
1087 __le32 border;
1088 int k, err = 0;
1089
1090 eh = path[depth].p_hdr;
1091 ex = path[depth].p_ext;
1092 BUG_ON(ex == NULL);
1093 BUG_ON(eh == NULL);
1094
1095 if (depth == 0) {
1096 /* there is no tree at all */
1097 return 0;
1098 }
1099
1100 if (ex != EXT_FIRST_EXTENT(eh)) {
1101 /* we correct tree if first leaf got modified only */
1102 return 0;
1103 }
1104
1105 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001106 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001107 */
1108 k = depth - 1;
1109 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001110 err = ext4_ext_get_access(handle, inode, path + k);
1111 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001112 return err;
1113 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001114 err = ext4_ext_dirty(handle, inode, path + k);
1115 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001116 return err;
1117
1118 while (k--) {
1119 /* change all left-side indexes */
1120 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1121 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001122 err = ext4_ext_get_access(handle, inode, path + k);
1123 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001124 break;
1125 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001126 err = ext4_ext_dirty(handle, inode, path + k);
1127 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001128 break;
1129 }
1130
1131 return err;
1132}
1133
Avantika Mathur09b88252006-12-06 20:41:36 -08001134static int
Alex Tomasa86c6182006-10-11 01:21:03 -07001135ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1136 struct ext4_extent *ex2)
1137{
Amit Arora749269f2007-07-18 09:02:56 -04001138 unsigned short ext1_ee_len, ext2_ee_len, max_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001139
1140 /*
1141 * Make sure that either both extents are uninitialized, or
1142 * both are _not_.
1143 */
1144 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1145 return 0;
1146
Amit Arora749269f2007-07-18 09:02:56 -04001147 if (ext4_ext_is_uninitialized(ex1))
1148 max_len = EXT_UNINIT_MAX_LEN;
1149 else
1150 max_len = EXT_INIT_MAX_LEN;
1151
Amit Aroraa2df2a62007-07-17 21:42:41 -04001152 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1153 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1154
1155 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001156 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001157 return 0;
1158
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001159 /*
1160 * To allow future support for preallocated extents to be added
1161 * as an RO_COMPAT feature, refuse to merge to extents if
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001162 * this can result in the top bit of ee_len being set.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001163 */
Amit Arora749269f2007-07-18 09:02:56 -04001164 if (ext1_ee_len + ext2_ee_len > max_len)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001165 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001166#ifdef AGGRESSIVE_TEST
Alex Tomasa86c6182006-10-11 01:21:03 -07001167 if (le16_to_cpu(ex1->ee_len) >= 4)
1168 return 0;
1169#endif
1170
Amit Aroraa2df2a62007-07-17 21:42:41 -04001171 if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001172 return 1;
1173 return 0;
1174}
1175
1176/*
Amit Arora56055d32007-07-17 21:42:38 -04001177 * This function tries to merge the "ex" extent to the next extent in the tree.
1178 * It always tries to merge towards right. If you want to merge towards
1179 * left, pass "ex - 1" as argument instead of "ex".
1180 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1181 * 1 if they got merged.
1182 */
1183int ext4_ext_try_to_merge(struct inode *inode,
1184 struct ext4_ext_path *path,
1185 struct ext4_extent *ex)
1186{
1187 struct ext4_extent_header *eh;
1188 unsigned int depth, len;
1189 int merge_done = 0;
1190 int uninitialized = 0;
1191
1192 depth = ext_depth(inode);
1193 BUG_ON(path[depth].p_hdr == NULL);
1194 eh = path[depth].p_hdr;
1195
1196 while (ex < EXT_LAST_EXTENT(eh)) {
1197 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1198 break;
1199 /* merge with next extent! */
1200 if (ext4_ext_is_uninitialized(ex))
1201 uninitialized = 1;
1202 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1203 + ext4_ext_get_actual_len(ex + 1));
1204 if (uninitialized)
1205 ext4_ext_mark_uninitialized(ex);
1206
1207 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1208 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1209 * sizeof(struct ext4_extent);
1210 memmove(ex + 1, ex + 2, len);
1211 }
1212 eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries) - 1);
1213 merge_done = 1;
1214 WARN_ON(eh->eh_entries == 0);
1215 if (!eh->eh_entries)
1216 ext4_error(inode->i_sb, "ext4_ext_try_to_merge",
1217 "inode#%lu, eh->eh_entries = 0!", inode->i_ino);
1218 }
1219
1220 return merge_done;
1221}
1222
1223/*
Amit Arora25d14f92007-05-24 13:04:13 -04001224 * check if a portion of the "newext" extent overlaps with an
1225 * existing extent.
1226 *
1227 * If there is an overlap discovered, it updates the length of the newext
1228 * such that there will be no overlap, and then returns 1.
1229 * If there is no overlap found, it returns 0.
1230 */
1231unsigned int ext4_ext_check_overlap(struct inode *inode,
1232 struct ext4_extent *newext,
1233 struct ext4_ext_path *path)
1234{
1235 unsigned long b1, b2;
1236 unsigned int depth, len1;
1237 unsigned int ret = 0;
1238
1239 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001240 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001241 depth = ext_depth(inode);
1242 if (!path[depth].p_ext)
1243 goto out;
1244 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
1245
1246 /*
1247 * get the next allocated block if the extent in the path
1248 * is before the requested block(s)
1249 */
1250 if (b2 < b1) {
1251 b2 = ext4_ext_next_allocated_block(path);
1252 if (b2 == EXT_MAX_BLOCK)
1253 goto out;
1254 }
1255
1256 /* check for wrap through zero */
1257 if (b1 + len1 < b1) {
1258 len1 = EXT_MAX_BLOCK - b1;
1259 newext->ee_len = cpu_to_le16(len1);
1260 ret = 1;
1261 }
1262
1263 /* check for overlap */
1264 if (b1 + len1 > b2) {
1265 newext->ee_len = cpu_to_le16(b2 - b1);
1266 ret = 1;
1267 }
1268out:
1269 return ret;
1270}
1271
1272/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001273 * ext4_ext_insert_extent:
1274 * tries to merge requsted extent into the existing extent or
1275 * inserts requested extent as new one into the tree,
1276 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001277 */
1278int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1279 struct ext4_ext_path *path,
1280 struct ext4_extent *newext)
1281{
1282 struct ext4_extent_header * eh;
1283 struct ext4_extent *ex, *fex;
1284 struct ext4_extent *nearex; /* nearest extent */
1285 struct ext4_ext_path *npath = NULL;
1286 int depth, len, err, next;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001287 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001288
Amit Aroraa2df2a62007-07-17 21:42:41 -04001289 BUG_ON(ext4_ext_get_actual_len(newext) == 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07001290 depth = ext_depth(inode);
1291 ex = path[depth].p_ext;
1292 BUG_ON(path[depth].p_hdr == NULL);
1293
1294 /* try to insert block into found extent and return */
1295 if (ex && ext4_can_extents_be_merged(inode, ex, newext)) {
Mingming Cao2ae02102006-10-11 01:21:11 -07001296 ext_debug("append %d block to %d:%d (from %llu)\n",
Amit Aroraa2df2a62007-07-17 21:42:41 -04001297 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001298 le32_to_cpu(ex->ee_block),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001299 ext4_ext_get_actual_len(ex), ext_pblock(ex));
Avantika Mathur7e028972006-12-06 20:41:33 -08001300 err = ext4_ext_get_access(handle, inode, path + depth);
1301 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001302 return err;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001303
1304 /*
1305 * ext4_can_extents_be_merged should have checked that either
1306 * both extents are uninitialized, or both aren't. Thus we
1307 * need to check only one of them here.
1308 */
1309 if (ext4_ext_is_uninitialized(ex))
1310 uninitialized = 1;
1311 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1312 + ext4_ext_get_actual_len(newext));
1313 if (uninitialized)
1314 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001315 eh = path[depth].p_hdr;
1316 nearex = ex;
1317 goto merge;
1318 }
1319
1320repeat:
1321 depth = ext_depth(inode);
1322 eh = path[depth].p_hdr;
1323 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1324 goto has_space;
1325
1326 /* probably next leaf has space for us? */
1327 fex = EXT_LAST_EXTENT(eh);
1328 next = ext4_ext_next_leaf_block(inode, path);
1329 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
1330 && next != EXT_MAX_BLOCK) {
1331 ext_debug("next leaf block - %d\n", next);
1332 BUG_ON(npath != NULL);
1333 npath = ext4_ext_find_extent(inode, next, NULL);
1334 if (IS_ERR(npath))
1335 return PTR_ERR(npath);
1336 BUG_ON(npath->p_depth != path->p_depth);
1337 eh = npath[depth].p_hdr;
1338 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
1339 ext_debug("next leaf isnt full(%d)\n",
1340 le16_to_cpu(eh->eh_entries));
1341 path = npath;
1342 goto repeat;
1343 }
1344 ext_debug("next leaf has no free space(%d,%d)\n",
1345 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1346 }
1347
1348 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001349 * There is no free space in the found leaf.
1350 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07001351 */
1352 err = ext4_ext_create_new_leaf(handle, inode, path, newext);
1353 if (err)
1354 goto cleanup;
1355 depth = ext_depth(inode);
1356 eh = path[depth].p_hdr;
1357
1358has_space:
1359 nearex = path[depth].p_ext;
1360
Avantika Mathur7e028972006-12-06 20:41:33 -08001361 err = ext4_ext_get_access(handle, inode, path + depth);
1362 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001363 goto cleanup;
1364
1365 if (!nearex) {
1366 /* there is no extent in this leaf, create first one */
Mingming Cao2ae02102006-10-11 01:21:11 -07001367 ext_debug("first extent in the leaf: %d:%llu:%d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001368 le32_to_cpu(newext->ee_block),
1369 ext_pblock(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001370 ext4_ext_get_actual_len(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001371 path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1372 } else if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001373 > le32_to_cpu(nearex->ee_block)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001374/* BUG_ON(newext->ee_block == nearex->ee_block); */
1375 if (nearex != EXT_LAST_EXTENT(eh)) {
1376 len = EXT_MAX_EXTENT(eh) - nearex;
1377 len = (len - 1) * sizeof(struct ext4_extent);
1378 len = len < 0 ? 0 : len;
Mingming Cao2ae02102006-10-11 01:21:11 -07001379 ext_debug("insert %d:%llu:%d after: nearest 0x%p, "
Alex Tomasa86c6182006-10-11 01:21:03 -07001380 "move %d from 0x%p to 0x%p\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001381 le32_to_cpu(newext->ee_block),
1382 ext_pblock(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001383 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001384 nearex, len, nearex + 1, nearex + 2);
1385 memmove(nearex + 2, nearex + 1, len);
1386 }
1387 path[depth].p_ext = nearex + 1;
1388 } else {
1389 BUG_ON(newext->ee_block == nearex->ee_block);
1390 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1391 len = len < 0 ? 0 : len;
Mingming Cao2ae02102006-10-11 01:21:11 -07001392 ext_debug("insert %d:%llu:%d before: nearest 0x%p, "
Alex Tomasa86c6182006-10-11 01:21:03 -07001393 "move %d from 0x%p to 0x%p\n",
1394 le32_to_cpu(newext->ee_block),
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001395 ext_pblock(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001396 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001397 nearex, len, nearex + 1, nearex + 2);
1398 memmove(nearex + 1, nearex, len);
1399 path[depth].p_ext = nearex;
1400 }
1401
1402 eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)+1);
1403 nearex = path[depth].p_ext;
1404 nearex->ee_block = newext->ee_block;
1405 nearex->ee_start = newext->ee_start;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001406 nearex->ee_start_hi = newext->ee_start_hi;
Alex Tomasa86c6182006-10-11 01:21:03 -07001407 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07001408
1409merge:
1410 /* try to merge extents to the right */
Amit Arora56055d32007-07-17 21:42:38 -04001411 ext4_ext_try_to_merge(inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001412
1413 /* try to merge extents to the left */
1414
1415 /* time to correct all indexes above */
1416 err = ext4_ext_correct_indexes(handle, inode, path);
1417 if (err)
1418 goto cleanup;
1419
1420 err = ext4_ext_dirty(handle, inode, path + depth);
1421
1422cleanup:
1423 if (npath) {
1424 ext4_ext_drop_refs(npath);
1425 kfree(npath);
1426 }
1427 ext4_ext_tree_changed(inode);
1428 ext4_ext_invalidate_cache(inode);
1429 return err;
1430}
1431
1432int ext4_ext_walk_space(struct inode *inode, unsigned long block,
1433 unsigned long num, ext_prepare_callback func,
1434 void *cbdata)
1435{
1436 struct ext4_ext_path *path = NULL;
1437 struct ext4_ext_cache cbex;
1438 struct ext4_extent *ex;
1439 unsigned long next, start = 0, end = 0;
1440 unsigned long last = block + num;
1441 int depth, exists, err = 0;
1442
1443 BUG_ON(func == NULL);
1444 BUG_ON(inode == NULL);
1445
1446 while (block < last && block != EXT_MAX_BLOCK) {
1447 num = last - block;
1448 /* find extent for this block */
1449 path = ext4_ext_find_extent(inode, block, path);
1450 if (IS_ERR(path)) {
1451 err = PTR_ERR(path);
1452 path = NULL;
1453 break;
1454 }
1455
1456 depth = ext_depth(inode);
1457 BUG_ON(path[depth].p_hdr == NULL);
1458 ex = path[depth].p_ext;
1459 next = ext4_ext_next_allocated_block(path);
1460
1461 exists = 0;
1462 if (!ex) {
1463 /* there is no extent yet, so try to allocate
1464 * all requested space */
1465 start = block;
1466 end = block + num;
1467 } else if (le32_to_cpu(ex->ee_block) > block) {
1468 /* need to allocate space before found extent */
1469 start = block;
1470 end = le32_to_cpu(ex->ee_block);
1471 if (block + num < end)
1472 end = block + num;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001473 } else if (block >= le32_to_cpu(ex->ee_block)
1474 + ext4_ext_get_actual_len(ex)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001475 /* need to allocate space after found extent */
1476 start = block;
1477 end = block + num;
1478 if (end >= next)
1479 end = next;
1480 } else if (block >= le32_to_cpu(ex->ee_block)) {
1481 /*
1482 * some part of requested space is covered
1483 * by found extent
1484 */
1485 start = block;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001486 end = le32_to_cpu(ex->ee_block)
1487 + ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001488 if (block + num < end)
1489 end = block + num;
1490 exists = 1;
1491 } else {
1492 BUG();
1493 }
1494 BUG_ON(end <= start);
1495
1496 if (!exists) {
1497 cbex.ec_block = start;
1498 cbex.ec_len = end - start;
1499 cbex.ec_start = 0;
1500 cbex.ec_type = EXT4_EXT_CACHE_GAP;
1501 } else {
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001502 cbex.ec_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001503 cbex.ec_len = ext4_ext_get_actual_len(ex);
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001504 cbex.ec_start = ext_pblock(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001505 cbex.ec_type = EXT4_EXT_CACHE_EXTENT;
1506 }
1507
1508 BUG_ON(cbex.ec_len == 0);
1509 err = func(inode, path, &cbex, cbdata);
1510 ext4_ext_drop_refs(path);
1511
1512 if (err < 0)
1513 break;
1514 if (err == EXT_REPEAT)
1515 continue;
1516 else if (err == EXT_BREAK) {
1517 err = 0;
1518 break;
1519 }
1520
1521 if (ext_depth(inode) != depth) {
1522 /* depth was changed. we have to realloc path */
1523 kfree(path);
1524 path = NULL;
1525 }
1526
1527 block = cbex.ec_block + cbex.ec_len;
1528 }
1529
1530 if (path) {
1531 ext4_ext_drop_refs(path);
1532 kfree(path);
1533 }
1534
1535 return err;
1536}
1537
Avantika Mathur09b88252006-12-06 20:41:36 -08001538static void
Alex Tomasa86c6182006-10-11 01:21:03 -07001539ext4_ext_put_in_cache(struct inode *inode, __u32 block,
1540 __u32 len, __u32 start, int type)
1541{
1542 struct ext4_ext_cache *cex;
1543 BUG_ON(len == 0);
1544 cex = &EXT4_I(inode)->i_cached_extent;
1545 cex->ec_type = type;
1546 cex->ec_block = block;
1547 cex->ec_len = len;
1548 cex->ec_start = start;
1549}
1550
1551/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001552 * ext4_ext_put_gap_in_cache:
1553 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07001554 * and cache this gap
1555 */
Avantika Mathur09b88252006-12-06 20:41:36 -08001556static void
Alex Tomasa86c6182006-10-11 01:21:03 -07001557ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
1558 unsigned long block)
1559{
1560 int depth = ext_depth(inode);
1561 unsigned long lblock, len;
1562 struct ext4_extent *ex;
1563
1564 ex = path[depth].p_ext;
1565 if (ex == NULL) {
1566 /* there is no extent yet, so gap is [0;-] */
1567 lblock = 0;
1568 len = EXT_MAX_BLOCK;
1569 ext_debug("cache gap(whole file):");
1570 } else if (block < le32_to_cpu(ex->ee_block)) {
1571 lblock = block;
1572 len = le32_to_cpu(ex->ee_block) - block;
1573 ext_debug("cache gap(before): %lu [%lu:%lu]",
1574 (unsigned long) block,
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001575 (unsigned long) le32_to_cpu(ex->ee_block),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001576 (unsigned long) ext4_ext_get_actual_len(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07001577 } else if (block >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001578 + ext4_ext_get_actual_len(ex)) {
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001579 lblock = le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001580 + ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001581 len = ext4_ext_next_allocated_block(path);
1582 ext_debug("cache gap(after): [%lu:%lu] %lu",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001583 (unsigned long) le32_to_cpu(ex->ee_block),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001584 (unsigned long) ext4_ext_get_actual_len(ex),
Alex Tomasa86c6182006-10-11 01:21:03 -07001585 (unsigned long) block);
1586 BUG_ON(len == lblock);
1587 len = len - lblock;
1588 } else {
1589 lblock = len = 0;
1590 BUG();
1591 }
1592
1593 ext_debug(" -> %lu:%lu\n", (unsigned long) lblock, len);
1594 ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
1595}
1596
Avantika Mathur09b88252006-12-06 20:41:36 -08001597static int
Alex Tomasa86c6182006-10-11 01:21:03 -07001598ext4_ext_in_cache(struct inode *inode, unsigned long block,
1599 struct ext4_extent *ex)
1600{
1601 struct ext4_ext_cache *cex;
1602
1603 cex = &EXT4_I(inode)->i_cached_extent;
1604
1605 /* has cache valid data? */
1606 if (cex->ec_type == EXT4_EXT_CACHE_NO)
1607 return EXT4_EXT_CACHE_NO;
1608
1609 BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
1610 cex->ec_type != EXT4_EXT_CACHE_EXTENT);
1611 if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001612 ex->ee_block = cpu_to_le32(cex->ec_block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001613 ext4_ext_store_pblock(ex, cex->ec_start);
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001614 ex->ee_len = cpu_to_le16(cex->ec_len);
Mingming Cao2ae02102006-10-11 01:21:11 -07001615 ext_debug("%lu cached by %lu:%lu:%llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001616 (unsigned long) block,
1617 (unsigned long) cex->ec_block,
1618 (unsigned long) cex->ec_len,
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001619 cex->ec_start);
Alex Tomasa86c6182006-10-11 01:21:03 -07001620 return cex->ec_type;
1621 }
1622
1623 /* not in cache */
1624 return EXT4_EXT_CACHE_NO;
1625}
1626
1627/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001628 * ext4_ext_rm_idx:
1629 * removes index from the index block.
1630 * It's used in truncate case only, thus all requests are for
1631 * last index in the block only.
Alex Tomasa86c6182006-10-11 01:21:03 -07001632 */
1633int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
1634 struct ext4_ext_path *path)
1635{
1636 struct buffer_head *bh;
1637 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001638 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07001639
1640 /* free index block */
1641 path--;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001642 leaf = idx_pblock(path->p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -07001643 BUG_ON(path->p_hdr->eh_entries == 0);
Avantika Mathur7e028972006-12-06 20:41:33 -08001644 err = ext4_ext_get_access(handle, inode, path);
1645 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001646 return err;
1647 path->p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(path->p_hdr->eh_entries)-1);
Avantika Mathur7e028972006-12-06 20:41:33 -08001648 err = ext4_ext_dirty(handle, inode, path);
1649 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001650 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07001651 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Alex Tomasa86c6182006-10-11 01:21:03 -07001652 bh = sb_find_get_block(inode->i_sb, leaf);
1653 ext4_forget(handle, 1, inode, bh, leaf);
1654 ext4_free_blocks(handle, inode, leaf, 1);
1655 return err;
1656}
1657
1658/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001659 * ext4_ext_calc_credits_for_insert:
1660 * This routine returns max. credits that the extent tree can consume.
Alex Tomasa86c6182006-10-11 01:21:03 -07001661 * It should be OK for low-performance paths like ->writepage()
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001662 * To allow many writing processes to fit into a single transaction,
1663 * the caller should calculate credits under truncate_mutex and
1664 * pass the actual path.
Alex Tomasa86c6182006-10-11 01:21:03 -07001665 */
Avantika Mathur09b88252006-12-06 20:41:36 -08001666int ext4_ext_calc_credits_for_insert(struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001667 struct ext4_ext_path *path)
1668{
1669 int depth, needed;
1670
1671 if (path) {
1672 /* probably there is space in leaf? */
1673 depth = ext_depth(inode);
1674 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
1675 < le16_to_cpu(path[depth].p_hdr->eh_max))
1676 return 1;
1677 }
1678
1679 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001680 * given 32-bit logical block (4294967296 blocks), max. tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001681 * can be 4 levels in depth -- 4 * 340^4 == 53453440000.
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001682 * Let's also add one more level for imbalance.
Alex Tomasa86c6182006-10-11 01:21:03 -07001683 */
1684 depth = 5;
1685
1686 /* allocation of new data block(s) */
1687 needed = 2;
1688
1689 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001690 * tree can be full, so it would need to grow in depth:
Johann Lombardifeb18922006-12-06 20:40:46 -08001691 * we need one credit to modify old root, credits for
1692 * new root will be added in split accounting
Alex Tomasa86c6182006-10-11 01:21:03 -07001693 */
Johann Lombardifeb18922006-12-06 20:40:46 -08001694 needed += 1;
Alex Tomasa86c6182006-10-11 01:21:03 -07001695
1696 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001697 * Index split can happen, we would need:
Alex Tomasa86c6182006-10-11 01:21:03 -07001698 * allocate intermediate indexes (bitmap + group)
1699 * + change two blocks at each level, but root (already included)
1700 */
Johann Lombardifeb18922006-12-06 20:40:46 -08001701 needed += (depth * 2) + (depth * 2);
Alex Tomasa86c6182006-10-11 01:21:03 -07001702
1703 /* any allocation modifies superblock */
1704 needed += 1;
1705
1706 return needed;
1707}
1708
1709static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
1710 struct ext4_extent *ex,
1711 unsigned long from, unsigned long to)
1712{
1713 struct buffer_head *bh;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001714 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001715 int i;
1716
1717#ifdef EXTENTS_STATS
1718 {
1719 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07001720 spin_lock(&sbi->s_ext_stats_lock);
1721 sbi->s_ext_blocks += ee_len;
1722 sbi->s_ext_extents++;
1723 if (ee_len < sbi->s_ext_min)
1724 sbi->s_ext_min = ee_len;
1725 if (ee_len > sbi->s_ext_max)
1726 sbi->s_ext_max = ee_len;
1727 if (ext_depth(inode) > sbi->s_depth_max)
1728 sbi->s_depth_max = ext_depth(inode);
1729 spin_unlock(&sbi->s_ext_stats_lock);
1730 }
1731#endif
1732 if (from >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001733 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001734 /* tail removal */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001735 unsigned long num;
1736 ext4_fsblk_t start;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001737 num = le32_to_cpu(ex->ee_block) + ee_len - from;
1738 start = ext_pblock(ex) + ee_len - num;
Mingming Cao2ae02102006-10-11 01:21:11 -07001739 ext_debug("free last %lu blocks starting %llu\n", num, start);
Alex Tomasa86c6182006-10-11 01:21:03 -07001740 for (i = 0; i < num; i++) {
1741 bh = sb_find_get_block(inode->i_sb, start + i);
1742 ext4_forget(handle, 0, inode, bh, start + i);
1743 }
1744 ext4_free_blocks(handle, inode, start, num);
1745 } else if (from == le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001746 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001747 printk("strange request: removal %lu-%lu from %u:%u\n",
Amit Aroraa2df2a62007-07-17 21:42:41 -04001748 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07001749 } else {
1750 printk("strange request: removal(2) %lu-%lu from %u:%u\n",
Amit Aroraa2df2a62007-07-17 21:42:41 -04001751 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07001752 }
1753 return 0;
1754}
1755
1756static int
1757ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
1758 struct ext4_ext_path *path, unsigned long start)
1759{
1760 int err = 0, correct_index = 0;
1761 int depth = ext_depth(inode), credits;
1762 struct ext4_extent_header *eh;
1763 unsigned a, b, block, num;
1764 unsigned long ex_ee_block;
1765 unsigned short ex_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001766 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001767 struct ext4_extent *ex;
1768
Alex Tomasc29c0ae2007-07-18 09:19:09 -04001769 /* the header must be checked already in ext4_ext_remove_space() */
Alex Tomasa86c6182006-10-11 01:21:03 -07001770 ext_debug("truncate since %lu in leaf\n", start);
1771 if (!path[depth].p_hdr)
1772 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
1773 eh = path[depth].p_hdr;
1774 BUG_ON(eh == NULL);
Alex Tomasa86c6182006-10-11 01:21:03 -07001775
1776 /* find where to start removing */
1777 ex = EXT_LAST_EXTENT(eh);
1778
1779 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001780 if (ext4_ext_is_uninitialized(ex))
1781 uninitialized = 1;
1782 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001783
1784 while (ex >= EXT_FIRST_EXTENT(eh) &&
1785 ex_ee_block + ex_ee_len > start) {
1786 ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len);
1787 path[depth].p_ext = ex;
1788
1789 a = ex_ee_block > start ? ex_ee_block : start;
1790 b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
1791 ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
1792
1793 ext_debug(" border %u:%u\n", a, b);
1794
1795 if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
1796 block = 0;
1797 num = 0;
1798 BUG();
1799 } else if (a != ex_ee_block) {
1800 /* remove tail of the extent */
1801 block = ex_ee_block;
1802 num = a - block;
1803 } else if (b != ex_ee_block + ex_ee_len - 1) {
1804 /* remove head of the extent */
1805 block = a;
1806 num = b - a;
1807 /* there is no "make a hole" API yet */
1808 BUG();
1809 } else {
1810 /* remove whole extent: excellent! */
1811 block = ex_ee_block;
1812 num = 0;
1813 BUG_ON(a != ex_ee_block);
1814 BUG_ON(b != ex_ee_block + ex_ee_len - 1);
1815 }
1816
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001817 /* at present, extent can't cross block group: */
Alex Tomasa86c6182006-10-11 01:21:03 -07001818 /* leaf + bitmap + group desc + sb + inode */
1819 credits = 5;
1820 if (ex == EXT_FIRST_EXTENT(eh)) {
1821 correct_index = 1;
1822 credits += (ext_depth(inode)) + 1;
1823 }
1824#ifdef CONFIG_QUOTA
1825 credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
1826#endif
1827
1828 handle = ext4_ext_journal_restart(handle, credits);
1829 if (IS_ERR(handle)) {
1830 err = PTR_ERR(handle);
1831 goto out;
1832 }
1833
1834 err = ext4_ext_get_access(handle, inode, path + depth);
1835 if (err)
1836 goto out;
1837
1838 err = ext4_remove_blocks(handle, inode, ex, a, b);
1839 if (err)
1840 goto out;
1841
1842 if (num == 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001843 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001844 ext4_ext_store_pblock(ex, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07001845 eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)-1);
1846 }
1847
1848 ex->ee_block = cpu_to_le32(block);
1849 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04001850 /*
1851 * Do not mark uninitialized if all the blocks in the
1852 * extent have been removed.
1853 */
1854 if (uninitialized && num)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001855 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001856
1857 err = ext4_ext_dirty(handle, inode, path + depth);
1858 if (err)
1859 goto out;
1860
Mingming Cao2ae02102006-10-11 01:21:11 -07001861 ext_debug("new extent: %u:%u:%llu\n", block, num,
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001862 ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07001863 ex--;
1864 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001865 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001866 }
1867
1868 if (correct_index && eh->eh_entries)
1869 err = ext4_ext_correct_indexes(handle, inode, path);
1870
1871 /* if this leaf is free, then we should
1872 * remove it from index block above */
1873 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
1874 err = ext4_ext_rm_idx(handle, inode, path + depth);
1875
1876out:
1877 return err;
1878}
1879
1880/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001881 * ext4_ext_more_to_rm:
1882 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07001883 */
Avantika Mathur09b88252006-12-06 20:41:36 -08001884static int
Alex Tomasa86c6182006-10-11 01:21:03 -07001885ext4_ext_more_to_rm(struct ext4_ext_path *path)
1886{
1887 BUG_ON(path->p_idx == NULL);
1888
1889 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
1890 return 0;
1891
1892 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001893 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07001894 * so we have to consider current index for truncation
1895 */
1896 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
1897 return 0;
1898 return 1;
1899}
1900
1901int ext4_ext_remove_space(struct inode *inode, unsigned long start)
1902{
1903 struct super_block *sb = inode->i_sb;
1904 int depth = ext_depth(inode);
1905 struct ext4_ext_path *path;
1906 handle_t *handle;
1907 int i = 0, err = 0;
1908
1909 ext_debug("truncate since %lu\n", start);
1910
1911 /* probably first extent we're gonna free will be last in block */
1912 handle = ext4_journal_start(inode, depth + 1);
1913 if (IS_ERR(handle))
1914 return PTR_ERR(handle);
1915
1916 ext4_ext_invalidate_cache(inode);
1917
1918 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001919 * We start scanning from right side, freeing all the blocks
1920 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07001921 */
Avantika Mathur5d4958f2006-12-06 20:41:35 -08001922 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_KERNEL);
Alex Tomasa86c6182006-10-11 01:21:03 -07001923 if (path == NULL) {
1924 ext4_journal_stop(handle);
1925 return -ENOMEM;
1926 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001927 path[0].p_hdr = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -04001928 if (ext4_ext_check_header(inode, path[0].p_hdr, depth)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001929 err = -EIO;
1930 goto out;
1931 }
1932 path[0].p_depth = depth;
1933
1934 while (i >= 0 && err == 0) {
1935 if (i == depth) {
1936 /* this is leaf block */
1937 err = ext4_ext_rm_leaf(handle, inode, path, start);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001938 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07001939 brelse(path[i].p_bh);
1940 path[i].p_bh = NULL;
1941 i--;
1942 continue;
1943 }
1944
1945 /* this is index block */
1946 if (!path[i].p_hdr) {
1947 ext_debug("initialize header\n");
1948 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001949 }
1950
Alex Tomasa86c6182006-10-11 01:21:03 -07001951 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001952 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07001953 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
1954 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
1955 ext_debug("init index ptr: hdr 0x%p, num %d\n",
1956 path[i].p_hdr,
1957 le16_to_cpu(path[i].p_hdr->eh_entries));
1958 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001959 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07001960 path[i].p_idx--;
1961 }
1962
1963 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
1964 i, EXT_FIRST_INDEX(path[i].p_hdr),
1965 path[i].p_idx);
1966 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04001967 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001968 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07001969 ext_debug("move to level %d (block %llu)\n",
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001970 i + 1, idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07001971 memset(path + i + 1, 0, sizeof(*path));
Alex Tomasc29c0ae2007-07-18 09:19:09 -04001972 bh = sb_bread(sb, idx_pblock(path[i].p_idx));
1973 if (!bh) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001974 /* should we reset i_size? */
1975 err = -EIO;
1976 break;
1977 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04001978 if (WARN_ON(i + 1 > depth)) {
1979 err = -EIO;
1980 break;
1981 }
1982 if (ext4_ext_check_header(inode, ext_block_hdr(bh),
1983 depth - i - 1)) {
1984 err = -EIO;
1985 break;
1986 }
1987 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001988
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001989 /* save actual number of indexes since this
1990 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07001991 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
1992 i++;
1993 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001994 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07001995 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001996 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07001997 * handle must be already prepared by the
1998 * truncatei_leaf() */
1999 err = ext4_ext_rm_idx(handle, inode, path + i);
2000 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002001 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002002 brelse(path[i].p_bh);
2003 path[i].p_bh = NULL;
2004 i--;
2005 ext_debug("return to level %d\n", i);
2006 }
2007 }
2008
2009 /* TODO: flexible tree reduction should be here */
2010 if (path->p_hdr->eh_entries == 0) {
2011 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002012 * truncate to zero freed all the tree,
2013 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07002014 */
2015 err = ext4_ext_get_access(handle, inode, path);
2016 if (err == 0) {
2017 ext_inode_hdr(inode)->eh_depth = 0;
2018 ext_inode_hdr(inode)->eh_max =
2019 cpu_to_le16(ext4_ext_space_root(inode));
2020 err = ext4_ext_dirty(handle, inode, path);
2021 }
2022 }
2023out:
2024 ext4_ext_tree_changed(inode);
2025 ext4_ext_drop_refs(path);
2026 kfree(path);
2027 ext4_journal_stop(handle);
2028
2029 return err;
2030}
2031
2032/*
2033 * called at mount time
2034 */
2035void ext4_ext_init(struct super_block *sb)
2036{
2037 /*
2038 * possible initialization would be here
2039 */
2040
2041 if (test_opt(sb, EXTENTS)) {
2042 printk("EXT4-fs: file extents enabled");
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01002043#ifdef AGGRESSIVE_TEST
2044 printk(", aggressive tests");
Alex Tomasa86c6182006-10-11 01:21:03 -07002045#endif
2046#ifdef CHECK_BINSEARCH
2047 printk(", check binsearch");
2048#endif
2049#ifdef EXTENTS_STATS
2050 printk(", stats");
2051#endif
2052 printk("\n");
2053#ifdef EXTENTS_STATS
2054 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2055 EXT4_SB(sb)->s_ext_min = 1 << 30;
2056 EXT4_SB(sb)->s_ext_max = 0;
2057#endif
2058 }
2059}
2060
2061/*
2062 * called at umount time
2063 */
2064void ext4_ext_release(struct super_block *sb)
2065{
2066 if (!test_opt(sb, EXTENTS))
2067 return;
2068
2069#ifdef EXTENTS_STATS
2070 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2071 struct ext4_sb_info *sbi = EXT4_SB(sb);
2072 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2073 sbi->s_ext_blocks, sbi->s_ext_extents,
2074 sbi->s_ext_blocks / sbi->s_ext_extents);
2075 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2076 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2077 }
2078#endif
2079}
2080
Amit Arora56055d32007-07-17 21:42:38 -04002081/*
2082 * This function is called by ext4_ext_get_blocks() if someone tries to write
2083 * to an uninitialized extent. It may result in splitting the uninitialized
2084 * extent into multiple extents (upto three - one initialized and two
2085 * uninitialized).
2086 * There are three possibilities:
2087 * a> There is no split required: Entire extent should be initialized
2088 * b> Splits in two extents: Write is happening at either end of the extent
2089 * c> Splits in three extents: Somone is writing in middle of the extent
2090 */
2091int ext4_ext_convert_to_initialized(handle_t *handle, struct inode *inode,
2092 struct ext4_ext_path *path,
2093 ext4_fsblk_t iblock,
2094 unsigned long max_blocks)
2095{
2096 struct ext4_extent *ex, newex;
2097 struct ext4_extent *ex1 = NULL;
2098 struct ext4_extent *ex2 = NULL;
2099 struct ext4_extent *ex3 = NULL;
2100 struct ext4_extent_header *eh;
2101 unsigned int allocated, ee_block, ee_len, depth;
2102 ext4_fsblk_t newblock;
2103 int err = 0;
2104 int ret = 0;
2105
2106 depth = ext_depth(inode);
2107 eh = path[depth].p_hdr;
2108 ex = path[depth].p_ext;
2109 ee_block = le32_to_cpu(ex->ee_block);
2110 ee_len = ext4_ext_get_actual_len(ex);
2111 allocated = ee_len - (iblock - ee_block);
2112 newblock = iblock - ee_block + ext_pblock(ex);
2113 ex2 = ex;
2114
2115 /* ex1: ee_block to iblock - 1 : uninitialized */
2116 if (iblock > ee_block) {
2117 ex1 = ex;
2118 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2119 ext4_ext_mark_uninitialized(ex1);
2120 ex2 = &newex;
2121 }
2122 /*
2123 * for sanity, update the length of the ex2 extent before
2124 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2125 * overlap of blocks.
2126 */
2127 if (!ex1 && allocated > max_blocks)
2128 ex2->ee_len = cpu_to_le16(max_blocks);
2129 /* ex3: to ee_block + ee_len : uninitialised */
2130 if (allocated > max_blocks) {
2131 unsigned int newdepth;
2132 ex3 = &newex;
2133 ex3->ee_block = cpu_to_le32(iblock + max_blocks);
2134 ext4_ext_store_pblock(ex3, newblock + max_blocks);
2135 ex3->ee_len = cpu_to_le16(allocated - max_blocks);
2136 ext4_ext_mark_uninitialized(ex3);
2137 err = ext4_ext_insert_extent(handle, inode, path, ex3);
2138 if (err)
2139 goto out;
2140 /*
2141 * The depth, and hence eh & ex might change
2142 * as part of the insert above.
2143 */
2144 newdepth = ext_depth(inode);
2145 if (newdepth != depth) {
2146 depth = newdepth;
2147 path = ext4_ext_find_extent(inode, iblock, NULL);
2148 if (IS_ERR(path)) {
2149 err = PTR_ERR(path);
2150 path = NULL;
2151 goto out;
2152 }
2153 eh = path[depth].p_hdr;
2154 ex = path[depth].p_ext;
2155 if (ex2 != &newex)
2156 ex2 = ex;
2157 }
2158 allocated = max_blocks;
2159 }
2160 /*
2161 * If there was a change of depth as part of the
2162 * insertion of ex3 above, we need to update the length
2163 * of the ex1 extent again here
2164 */
2165 if (ex1 && ex1 != ex) {
2166 ex1 = ex;
2167 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2168 ext4_ext_mark_uninitialized(ex1);
2169 ex2 = &newex;
2170 }
2171 /* ex2: iblock to iblock + maxblocks-1 : initialised */
2172 ex2->ee_block = cpu_to_le32(iblock);
2173 ex2->ee_start = cpu_to_le32(newblock);
2174 ext4_ext_store_pblock(ex2, newblock);
2175 ex2->ee_len = cpu_to_le16(allocated);
2176 if (ex2 != ex)
2177 goto insert;
2178 err = ext4_ext_get_access(handle, inode, path + depth);
2179 if (err)
2180 goto out;
2181 /*
2182 * New (initialized) extent starts from the first block
2183 * in the current extent. i.e., ex2 == ex
2184 * We have to see if it can be merged with the extent
2185 * on the left.
2186 */
2187 if (ex2 > EXT_FIRST_EXTENT(eh)) {
2188 /*
2189 * To merge left, pass "ex2 - 1" to try_to_merge(),
2190 * since it merges towards right _only_.
2191 */
2192 ret = ext4_ext_try_to_merge(inode, path, ex2 - 1);
2193 if (ret) {
2194 err = ext4_ext_correct_indexes(handle, inode, path);
2195 if (err)
2196 goto out;
2197 depth = ext_depth(inode);
2198 ex2--;
2199 }
2200 }
2201 /*
2202 * Try to Merge towards right. This might be required
2203 * only when the whole extent is being written to.
2204 * i.e. ex2 == ex and ex3 == NULL.
2205 */
2206 if (!ex3) {
2207 ret = ext4_ext_try_to_merge(inode, path, ex2);
2208 if (ret) {
2209 err = ext4_ext_correct_indexes(handle, inode, path);
2210 if (err)
2211 goto out;
2212 }
2213 }
2214 /* Mark modified extent as dirty */
2215 err = ext4_ext_dirty(handle, inode, path + depth);
2216 goto out;
2217insert:
2218 err = ext4_ext_insert_extent(handle, inode, path, &newex);
2219out:
2220 return err ? err : allocated;
2221}
2222
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002223int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
2224 ext4_fsblk_t iblock,
Alex Tomasa86c6182006-10-11 01:21:03 -07002225 unsigned long max_blocks, struct buffer_head *bh_result,
2226 int create, int extend_disksize)
2227{
2228 struct ext4_ext_path *path = NULL;
Amit Arora56055d32007-07-17 21:42:38 -04002229 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002230 struct ext4_extent newex, *ex;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002231 ext4_fsblk_t goal, newblock;
Amit Arora56055d32007-07-17 21:42:38 -04002232 int err = 0, depth, ret;
Alex Tomasa86c6182006-10-11 01:21:03 -07002233 unsigned long allocated = 0;
2234
2235 __clear_bit(BH_New, &bh_result->b_state);
2236 ext_debug("blocks %d/%lu requested for inode %u\n", (int) iblock,
2237 max_blocks, (unsigned) inode->i_ino);
2238 mutex_lock(&EXT4_I(inode)->truncate_mutex);
2239
2240 /* check in cache */
Avantika Mathur7e028972006-12-06 20:41:33 -08002241 goal = ext4_ext_in_cache(inode, iblock, &newex);
2242 if (goal) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002243 if (goal == EXT4_EXT_CACHE_GAP) {
2244 if (!create) {
Amit Arora56055d32007-07-17 21:42:38 -04002245 /*
2246 * block isn't allocated yet and
2247 * user doesn't want to allocate it
2248 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002249 goto out2;
2250 }
2251 /* we should allocate requested block */
2252 } else if (goal == EXT4_EXT_CACHE_EXTENT) {
2253 /* block is already allocated */
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002254 newblock = iblock
2255 - le32_to_cpu(newex.ee_block)
2256 + ext_pblock(&newex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002257 /* number of remaining blocks in the extent */
Alex Tomasa86c6182006-10-11 01:21:03 -07002258 allocated = le16_to_cpu(newex.ee_len) -
2259 (iblock - le32_to_cpu(newex.ee_block));
2260 goto out;
2261 } else {
2262 BUG();
2263 }
2264 }
2265
2266 /* find extent for this block */
2267 path = ext4_ext_find_extent(inode, iblock, NULL);
2268 if (IS_ERR(path)) {
2269 err = PTR_ERR(path);
2270 path = NULL;
2271 goto out2;
2272 }
2273
2274 depth = ext_depth(inode);
2275
2276 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002277 * consistent leaf must not be empty;
2278 * this situation is possible, though, _during_ tree modification;
Alex Tomasa86c6182006-10-11 01:21:03 -07002279 * this is why assert can't be put in ext4_ext_find_extent()
2280 */
2281 BUG_ON(path[depth].p_ext == NULL && depth != 0);
Amit Arora56055d32007-07-17 21:42:38 -04002282 eh = path[depth].p_hdr;
Alex Tomasa86c6182006-10-11 01:21:03 -07002283
Avantika Mathur7e028972006-12-06 20:41:33 -08002284 ex = path[depth].p_ext;
2285 if (ex) {
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002286 unsigned long ee_block = le32_to_cpu(ex->ee_block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002287 ext4_fsblk_t ee_start = ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002288 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07002289
2290 /*
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07002291 * Uninitialized extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04002292 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07002293 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04002294 ee_len = ext4_ext_get_actual_len(ex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002295 /* if found extent covers block, simply return it */
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002296 if (iblock >= ee_block && iblock < ee_block + ee_len) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002297 newblock = iblock - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002298 /* number of remaining blocks in the extent */
Alex Tomasa86c6182006-10-11 01:21:03 -07002299 allocated = ee_len - (iblock - ee_block);
Mingming Cao2ae02102006-10-11 01:21:11 -07002300 ext_debug("%d fit into %lu:%d -> %llu\n", (int) iblock,
Alex Tomasa86c6182006-10-11 01:21:03 -07002301 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04002302
Amit Aroraa2df2a62007-07-17 21:42:41 -04002303 /* Do not put uninitialized extent in the cache */
Amit Arora56055d32007-07-17 21:42:38 -04002304 if (!ext4_ext_is_uninitialized(ex)) {
Amit Aroraa2df2a62007-07-17 21:42:41 -04002305 ext4_ext_put_in_cache(inode, ee_block,
2306 ee_len, ee_start,
2307 EXT4_EXT_CACHE_EXTENT);
Amit Arora56055d32007-07-17 21:42:38 -04002308 goto out;
2309 }
2310 if (create == EXT4_CREATE_UNINITIALIZED_EXT)
2311 goto out;
2312 if (!create)
2313 goto out2;
2314
2315 ret = ext4_ext_convert_to_initialized(handle, inode,
2316 path, iblock,
2317 max_blocks);
2318 if (ret <= 0)
2319 goto out2;
2320 else
2321 allocated = ret;
2322 goto outnew;
Alex Tomasa86c6182006-10-11 01:21:03 -07002323 }
2324 }
2325
2326 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002327 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07002328 * we couldn't try to create block if create flag is zero
2329 */
2330 if (!create) {
Amit Arora56055d32007-07-17 21:42:38 -04002331 /*
2332 * put just found gap into cache to speed up
2333 * subsequent requests
2334 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002335 ext4_ext_put_gap_in_cache(inode, path, iblock);
2336 goto out2;
2337 }
2338 /*
Andrew Morton63f57932006-10-11 01:21:24 -07002339 * Okay, we need to do block allocation. Lazily initialize the block
2340 * allocation info here if necessary.
2341 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002342 if (S_ISREG(inode->i_mode) && (!EXT4_I(inode)->i_block_alloc_info))
2343 ext4_init_block_alloc_info(inode);
2344
2345 /* allocate new block */
2346 goal = ext4_ext_find_goal(inode, path, iblock);
Amit Arora25d14f92007-05-24 13:04:13 -04002347
Amit Arora749269f2007-07-18 09:02:56 -04002348 /*
2349 * See if request is beyond maximum number of blocks we can have in
2350 * a single extent. For an initialized extent this limit is
2351 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
2352 * EXT_UNINIT_MAX_LEN.
2353 */
2354 if (max_blocks > EXT_INIT_MAX_LEN &&
2355 create != EXT4_CREATE_UNINITIALIZED_EXT)
2356 max_blocks = EXT_INIT_MAX_LEN;
2357 else if (max_blocks > EXT_UNINIT_MAX_LEN &&
2358 create == EXT4_CREATE_UNINITIALIZED_EXT)
2359 max_blocks = EXT_UNINIT_MAX_LEN;
2360
Amit Arora25d14f92007-05-24 13:04:13 -04002361 /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */
2362 newex.ee_block = cpu_to_le32(iblock);
2363 newex.ee_len = cpu_to_le16(max_blocks);
2364 err = ext4_ext_check_overlap(inode, &newex, path);
2365 if (err)
2366 allocated = le16_to_cpu(newex.ee_len);
2367 else
2368 allocated = max_blocks;
Alex Tomasa86c6182006-10-11 01:21:03 -07002369 newblock = ext4_new_blocks(handle, inode, goal, &allocated, &err);
2370 if (!newblock)
2371 goto out2;
Mingming Cao2ae02102006-10-11 01:21:11 -07002372 ext_debug("allocate new block: goal %llu, found %llu/%lu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07002373 goal, newblock, allocated);
2374
2375 /* try to insert new extent into found leaf and return */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002376 ext4_ext_store_pblock(&newex, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07002377 newex.ee_len = cpu_to_le16(allocated);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002378 if (create == EXT4_CREATE_UNINITIALIZED_EXT) /* Mark uninitialized */
2379 ext4_ext_mark_uninitialized(&newex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002380 err = ext4_ext_insert_extent(handle, inode, path, &newex);
Alex Tomas315054f2007-05-24 13:04:25 -04002381 if (err) {
2382 /* free data blocks we just allocated */
2383 ext4_free_blocks(handle, inode, ext_pblock(&newex),
2384 le16_to_cpu(newex.ee_len));
Alex Tomasa86c6182006-10-11 01:21:03 -07002385 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04002386 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002387
2388 if (extend_disksize && inode->i_size > EXT4_I(inode)->i_disksize)
2389 EXT4_I(inode)->i_disksize = inode->i_size;
2390
2391 /* previous routine could use block we allocated */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002392 newblock = ext_pblock(&newex);
Amit Arora56055d32007-07-17 21:42:38 -04002393outnew:
Alex Tomasa86c6182006-10-11 01:21:03 -07002394 __set_bit(BH_New, &bh_result->b_state);
2395
Amit Aroraa2df2a62007-07-17 21:42:41 -04002396 /* Cache only when it is _not_ an uninitialized extent */
2397 if (create != EXT4_CREATE_UNINITIALIZED_EXT)
2398 ext4_ext_put_in_cache(inode, iblock, allocated, newblock,
2399 EXT4_EXT_CACHE_EXTENT);
Alex Tomasa86c6182006-10-11 01:21:03 -07002400out:
2401 if (allocated > max_blocks)
2402 allocated = max_blocks;
2403 ext4_ext_show_leaf(inode, path);
2404 __set_bit(BH_Mapped, &bh_result->b_state);
2405 bh_result->b_bdev = inode->i_sb->s_bdev;
2406 bh_result->b_blocknr = newblock;
2407out2:
2408 if (path) {
2409 ext4_ext_drop_refs(path);
2410 kfree(path);
2411 }
2412 mutex_unlock(&EXT4_I(inode)->truncate_mutex);
2413
2414 return err ? err : allocated;
2415}
2416
2417void ext4_ext_truncate(struct inode * inode, struct page *page)
2418{
2419 struct address_space *mapping = inode->i_mapping;
2420 struct super_block *sb = inode->i_sb;
2421 unsigned long last_block;
2422 handle_t *handle;
2423 int err = 0;
2424
2425 /*
2426 * probably first extent we're gonna free will be last in block
2427 */
2428 err = ext4_writepage_trans_blocks(inode) + 3;
2429 handle = ext4_journal_start(inode, err);
2430 if (IS_ERR(handle)) {
2431 if (page) {
2432 clear_highpage(page);
2433 flush_dcache_page(page);
2434 unlock_page(page);
2435 page_cache_release(page);
2436 }
2437 return;
2438 }
2439
2440 if (page)
2441 ext4_block_truncate_page(handle, page, mapping, inode->i_size);
2442
2443 mutex_lock(&EXT4_I(inode)->truncate_mutex);
2444 ext4_ext_invalidate_cache(inode);
2445
2446 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002447 * TODO: optimization is possible here.
2448 * Probably we need not scan at all,
2449 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07002450 */
2451 if (ext4_orphan_add(handle, inode))
2452 goto out_stop;
2453
2454 /* we have to know where to truncate from in crash case */
2455 EXT4_I(inode)->i_disksize = inode->i_size;
2456 ext4_mark_inode_dirty(handle, inode);
2457
2458 last_block = (inode->i_size + sb->s_blocksize - 1)
2459 >> EXT4_BLOCK_SIZE_BITS(sb);
2460 err = ext4_ext_remove_space(inode, last_block);
2461
2462 /* In a multi-transaction truncate, we only make the final
Amit Arora56055d32007-07-17 21:42:38 -04002463 * transaction synchronous.
2464 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002465 if (IS_SYNC(inode))
2466 handle->h_sync = 1;
2467
2468out_stop:
2469 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002470 * If this was a simple ftruncate() and the file will remain alive,
Alex Tomasa86c6182006-10-11 01:21:03 -07002471 * then we need to clear up the orphan record which we created above.
2472 * However, if this was a real unlink then we were called by
2473 * ext4_delete_inode(), and we allow that function to clean up the
2474 * orphan info for us.
2475 */
2476 if (inode->i_nlink)
2477 ext4_orphan_del(handle, inode);
2478
2479 mutex_unlock(&EXT4_I(inode)->truncate_mutex);
2480 ext4_journal_stop(handle);
2481}
2482
2483/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002484 * ext4_ext_writepage_trans_blocks:
2485 * calculate max number of blocks we could modify
Alex Tomasa86c6182006-10-11 01:21:03 -07002486 * in order to allocate new block for an inode
2487 */
2488int ext4_ext_writepage_trans_blocks(struct inode *inode, int num)
2489{
2490 int needed;
2491
2492 needed = ext4_ext_calc_credits_for_insert(inode, NULL);
2493
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002494 /* caller wants to allocate num blocks, but note it includes sb */
Alex Tomasa86c6182006-10-11 01:21:03 -07002495 needed = needed * num - (num - 1);
2496
2497#ifdef CONFIG_QUOTA
2498 needed += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
2499#endif
2500
2501 return needed;
2502}
Amit Aroraa2df2a62007-07-17 21:42:41 -04002503
2504/*
2505 * preallocate space for a file. This implements ext4's fallocate inode
2506 * operation, which gets called from sys_fallocate system call.
2507 * For block-mapped files, posix_fallocate should fall back to the method
2508 * of writing zeroes to the required new blocks (the same behavior which is
2509 * expected for file systems which do not support fallocate() system call).
2510 */
2511long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
2512{
2513 handle_t *handle;
2514 ext4_fsblk_t block, max_blocks;
2515 ext4_fsblk_t nblocks = 0;
2516 int ret = 0;
2517 int ret2 = 0;
2518 int retries = 0;
2519 struct buffer_head map_bh;
2520 unsigned int credits, blkbits = inode->i_blkbits;
2521
2522 /*
2523 * currently supporting (pre)allocate mode for extent-based
2524 * files _only_
2525 */
2526 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
2527 return -EOPNOTSUPP;
2528
2529 /* preallocation to directories is currently not supported */
2530 if (S_ISDIR(inode->i_mode))
2531 return -ENODEV;
2532
2533 block = offset >> blkbits;
2534 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
2535 - block;
2536
2537 /*
2538 * credits to insert 1 extent into extent tree + buffers to be able to
2539 * modify 1 super block, 1 block bitmap and 1 group descriptor.
2540 */
2541 credits = EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + 3;
2542retry:
2543 while (ret >= 0 && ret < max_blocks) {
2544 block = block + ret;
2545 max_blocks = max_blocks - ret;
2546 handle = ext4_journal_start(inode, credits);
2547 if (IS_ERR(handle)) {
2548 ret = PTR_ERR(handle);
2549 break;
2550 }
2551
2552 ret = ext4_ext_get_blocks(handle, inode, block,
2553 max_blocks, &map_bh,
2554 EXT4_CREATE_UNINITIALIZED_EXT, 0);
2555 WARN_ON(!ret);
2556 if (!ret) {
2557 ext4_error(inode->i_sb, "ext4_fallocate",
2558 "ext4_ext_get_blocks returned 0! inode#%lu"
2559 ", block=%llu, max_blocks=%llu",
2560 inode->i_ino, block, max_blocks);
2561 ret = -EIO;
2562 ext4_mark_inode_dirty(handle, inode);
2563 ret2 = ext4_journal_stop(handle);
2564 break;
2565 }
2566 if (ret > 0) {
2567 /* check wrap through sign-bit/zero here */
2568 if ((block + ret) < 0 || (block + ret) < block) {
2569 ret = -EIO;
2570 ext4_mark_inode_dirty(handle, inode);
2571 ret2 = ext4_journal_stop(handle);
2572 break;
2573 }
2574 if (buffer_new(&map_bh) && ((block + ret) >
2575 (EXT4_BLOCK_ALIGN(i_size_read(inode), blkbits)
2576 >> blkbits)))
2577 nblocks = nblocks + ret;
2578 }
2579
2580 /* Update ctime if new blocks get allocated */
2581 if (nblocks) {
2582 struct timespec now;
2583
2584 now = current_fs_time(inode->i_sb);
2585 if (!timespec_equal(&inode->i_ctime, &now))
2586 inode->i_ctime = now;
2587 }
2588
2589 ext4_mark_inode_dirty(handle, inode);
2590 ret2 = ext4_journal_stop(handle);
2591 if (ret2)
2592 break;
2593 }
2594
2595 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
2596 goto retry;
2597
2598 /*
2599 * Time to update the file size.
2600 * Update only when preallocation was requested beyond the file size.
2601 */
2602 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
2603 (offset + len) > i_size_read(inode)) {
2604 if (ret > 0) {
2605 /*
2606 * if no error, we assume preallocation succeeded
2607 * completely
2608 */
2609 mutex_lock(&inode->i_mutex);
2610 i_size_write(inode, offset + len);
2611 EXT4_I(inode)->i_disksize = i_size_read(inode);
2612 mutex_unlock(&inode->i_mutex);
2613 } else if (ret < 0 && nblocks) {
2614 /* Handle partial allocation scenario */
2615 loff_t newsize;
2616
2617 mutex_lock(&inode->i_mutex);
2618 newsize = (nblocks << blkbits) + i_size_read(inode);
2619 i_size_write(inode, EXT4_BLOCK_ALIGN(newsize, blkbits));
2620 EXT4_I(inode)->i_disksize = i_size_read(inode);
2621 mutex_unlock(&inode->i_mutex);
2622 }
2623 }
2624
2625 return ret > 0 ? ret2 : ret;
2626}