revert-mm-fix-blkdev-size-calculation-in-generic_write_checks
[linux-2.6/linux-trees-mm.git] / fs / ext4 / extents.c
blobce58d455dcd29d3cb79f08adffda5fe0927d732d
1 /*
2 * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
3 * Written by Alex Tomas <alex@clusterfs.com>
5 * Architecture independence:
6 * Copyright (c) 2005, Bull S.A.
7 * Written by Pierre Peiffer <pierre.peiffer@bull.net>
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.
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.
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-
24 * Extents support for EXT4
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
32 #include <linux/module.h>
33 #include <linux/fs.h>
34 #include <linux/time.h>
35 #include <linux/ext4_jbd2.h>
36 #include <linux/jbd2.h>
37 #include <linux/highuid.h>
38 #include <linux/pagemap.h>
39 #include <linux/quotaops.h>
40 #include <linux/string.h>
41 #include <linux/slab.h>
42 #include <linux/falloc.h>
43 #include <linux/ext4_fs_extents.h>
44 #include <asm/uaccess.h>
48 * ext_pblock:
49 * combine low and high parts of physical block number into ext4_fsblk_t
51 static ext4_fsblk_t ext_pblock(struct ext4_extent *ex)
53 ext4_fsblk_t block;
55 block = le32_to_cpu(ex->ee_start_lo);
56 block |= ((ext4_fsblk_t) le16_to_cpu(ex->ee_start_hi) << 31) << 1;
57 return block;
61 * idx_pblock:
62 * combine low and high parts of a leaf physical block number into ext4_fsblk_t
64 static ext4_fsblk_t idx_pblock(struct ext4_extent_idx *ix)
66 ext4_fsblk_t block;
68 block = le32_to_cpu(ix->ei_leaf_lo);
69 block |= ((ext4_fsblk_t) le16_to_cpu(ix->ei_leaf_hi) << 31) << 1;
70 return block;
74 * ext4_ext_store_pblock:
75 * stores a large physical block number into an extent struct,
76 * breaking it into parts
78 static void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb)
80 ex->ee_start_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff));
81 ex->ee_start_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
85 * ext4_idx_store_pblock:
86 * stores a large physical block number into an index struct,
87 * breaking it into parts
89 static void ext4_idx_store_pblock(struct ext4_extent_idx *ix, ext4_fsblk_t pb)
91 ix->ei_leaf_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff));
92 ix->ei_leaf_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
95 static handle_t *ext4_ext_journal_restart(handle_t *handle, int needed)
97 int err;
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);
105 return handle;
109 * could return:
110 * - EROFS
111 * - ENOMEM
113 static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
114 struct ext4_ext_path *path)
116 if (path->p_bh) {
117 /* path points to block */
118 return ext4_journal_get_write_access(handle, path->p_bh);
120 /* path points to leaf/index in inode body */
121 /* we use in-core data, no need to protect them */
122 return 0;
126 * could return:
127 * - EROFS
128 * - ENOMEM
129 * - EIO
131 static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
132 struct ext4_ext_path *path)
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);
142 return err;
145 static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
146 struct ext4_ext_path *path,
147 ext4_lblk_t block)
149 struct ext4_inode_info *ei = EXT4_I(inode);
150 ext4_fsblk_t bg_start;
151 ext4_grpblk_t colour;
152 int depth;
154 if (path) {
155 struct ext4_extent *ex;
156 depth = path->p_depth;
158 /* try to predict block placement */
159 ex = path[depth].p_ext;
160 if (ex)
161 return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block));
163 /* it looks like index is empty;
164 * try to find starting block from index itself */
165 if (path[depth].p_bh)
166 return path[depth].p_bh->b_blocknr;
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;
177 static ext4_fsblk_t
178 ext4_ext_new_block(handle_t *handle, struct inode *inode,
179 struct ext4_ext_path *path,
180 struct ext4_extent *ex, int *err)
182 ext4_fsblk_t goal, newblock;
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;
189 static int ext4_ext_space_block(struct inode *inode)
191 int size;
193 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
194 / sizeof(struct ext4_extent);
195 #ifdef AGGRESSIVE_TEST
196 if (size > 6)
197 size = 6;
198 #endif
199 return size;
202 static int ext4_ext_space_block_idx(struct inode *inode)
204 int size;
206 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
207 / sizeof(struct ext4_extent_idx);
208 #ifdef AGGRESSIVE_TEST
209 if (size > 5)
210 size = 5;
211 #endif
212 return size;
215 static int ext4_ext_space_root(struct inode *inode)
217 int size;
219 size = sizeof(EXT4_I(inode)->i_data);
220 size -= sizeof(struct ext4_extent_header);
221 size /= sizeof(struct ext4_extent);
222 #ifdef AGGRESSIVE_TEST
223 if (size > 3)
224 size = 3;
225 #endif
226 return size;
229 static int ext4_ext_space_root_idx(struct inode *inode)
231 int size;
233 size = sizeof(EXT4_I(inode)->i_data);
234 size -= sizeof(struct ext4_extent_header);
235 size /= sizeof(struct ext4_extent_idx);
236 #ifdef AGGRESSIVE_TEST
237 if (size > 4)
238 size = 4;
239 #endif
240 return size;
243 static int
244 ext4_ext_max_entries(struct inode *inode, int depth)
246 int max;
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);
260 return max;
263 static int __ext4_ext_check_header(const char *function, struct inode *inode,
264 struct ext4_extent_header *eh,
265 int depth)
267 const char *error_msg;
268 int max = 0;
270 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
271 error_msg = "invalid magic";
272 goto corrupted;
274 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
275 error_msg = "unexpected eh_depth";
276 goto corrupted;
278 if (unlikely(eh->eh_max == 0)) {
279 error_msg = "invalid eh_max";
280 goto corrupted;
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;
287 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
288 error_msg = "invalid eh_entries";
289 goto corrupted;
291 return 0;
293 corrupted:
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);
301 return -EIO;
304 #define ext4_ext_check_header(inode, eh, depth) \
305 __ext4_ext_check_header(__FUNCTION__, inode, eh, depth)
307 #ifdef EXT_DEBUG
308 static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
310 int k, l = path->p_depth;
312 ext_debug("path:");
313 for (k = 0; k <= l; k++, path++) {
314 if (path->p_idx) {
315 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
316 idx_pblock(path->p_idx));
317 } else if (path->p_ext) {
318 ext_debug(" %d:%d:%llu ",
319 le32_to_cpu(path->p_ext->ee_block),
320 ext4_ext_get_actual_len(path->p_ext),
321 ext_pblock(path->p_ext));
322 } else
323 ext_debug(" []");
325 ext_debug("\n");
328 static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
330 int depth = ext_depth(inode);
331 struct ext4_extent_header *eh;
332 struct ext4_extent *ex;
333 int i;
335 if (!path)
336 return;
338 eh = path[depth].p_hdr;
339 ex = EXT_FIRST_EXTENT(eh);
341 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
342 ext_debug("%d:%d:%llu ", le32_to_cpu(ex->ee_block),
343 ext4_ext_get_actual_len(ex), ext_pblock(ex));
345 ext_debug("\n");
347 #else
348 #define ext4_ext_show_path(inode,path)
349 #define ext4_ext_show_leaf(inode,path)
350 #endif
352 static void ext4_ext_drop_refs(struct ext4_ext_path *path)
354 int depth = path->p_depth;
355 int i;
357 for (i = 0; i <= depth; i++, path++)
358 if (path->p_bh) {
359 brelse(path->p_bh);
360 path->p_bh = NULL;
365 * ext4_ext_binsearch_idx:
366 * binary search for the closest index of the given block
367 * the header must be checked before calling this
369 static void
370 ext4_ext_binsearch_idx(struct inode *inode,
371 struct ext4_ext_path *path, ext4_lblk_t block)
373 struct ext4_extent_header *eh = path->p_hdr;
374 struct ext4_extent_idx *r, *l, *m;
377 ext_debug("binsearch for %lu(idx): ", (unsigned long)block);
379 l = EXT_FIRST_INDEX(eh) + 1;
380 r = EXT_LAST_INDEX(eh);
381 while (l <= r) {
382 m = l + (r - l) / 2;
383 if (block < le32_to_cpu(m->ei_block))
384 r = m - 1;
385 else
386 l = m + 1;
387 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
388 m, le32_to_cpu(m->ei_block),
389 r, le32_to_cpu(r->ei_block));
392 path->p_idx = l - 1;
393 ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
394 idx_pblock(path->p_idx));
396 #ifdef CHECK_BINSEARCH
398 struct ext4_extent_idx *chix, *ix;
399 int k;
401 chix = ix = EXT_FIRST_INDEX(eh);
402 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
403 if (k != 0 &&
404 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
405 printk("k=%d, ix=0x%p, first=0x%p\n", k,
406 ix, EXT_FIRST_INDEX(eh));
407 printk("%u <= %u\n",
408 le32_to_cpu(ix->ei_block),
409 le32_to_cpu(ix[-1].ei_block));
411 BUG_ON(k && le32_to_cpu(ix->ei_block)
412 <= le32_to_cpu(ix[-1].ei_block));
413 if (block < le32_to_cpu(ix->ei_block))
414 break;
415 chix = ix;
417 BUG_ON(chix != path->p_idx);
419 #endif
424 * ext4_ext_binsearch:
425 * binary search for closest extent of the given block
426 * the header must be checked before calling this
428 static void
429 ext4_ext_binsearch(struct inode *inode,
430 struct ext4_ext_path *path, ext4_lblk_t block)
432 struct ext4_extent_header *eh = path->p_hdr;
433 struct ext4_extent *r, *l, *m;
435 if (eh->eh_entries == 0) {
437 * this leaf is empty:
438 * we get such a leaf in split/add case
440 return;
443 ext_debug("binsearch for %lu: ", (unsigned long)block);
445 l = EXT_FIRST_EXTENT(eh) + 1;
446 r = EXT_LAST_EXTENT(eh);
448 while (l <= r) {
449 m = l + (r - l) / 2;
450 if (block < le32_to_cpu(m->ee_block))
451 r = m - 1;
452 else
453 l = m + 1;
454 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
455 m, le32_to_cpu(m->ee_block),
456 r, le32_to_cpu(r->ee_block));
459 path->p_ext = l - 1;
460 ext_debug(" -> %d:%llu:%d ",
461 le32_to_cpu(path->p_ext->ee_block),
462 ext_pblock(path->p_ext),
463 ext4_ext_get_actual_len(path->p_ext));
465 #ifdef CHECK_BINSEARCH
467 struct ext4_extent *chex, *ex;
468 int k;
470 chex = ex = EXT_FIRST_EXTENT(eh);
471 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
472 BUG_ON(k && le32_to_cpu(ex->ee_block)
473 <= le32_to_cpu(ex[-1].ee_block));
474 if (block < le32_to_cpu(ex->ee_block))
475 break;
476 chex = ex;
478 BUG_ON(chex != path->p_ext);
480 #endif
484 int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
486 struct ext4_extent_header *eh;
488 eh = ext_inode_hdr(inode);
489 eh->eh_depth = 0;
490 eh->eh_entries = 0;
491 eh->eh_magic = EXT4_EXT_MAGIC;
492 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode));
493 ext4_mark_inode_dirty(handle, inode);
494 ext4_ext_invalidate_cache(inode);
495 return 0;
498 struct ext4_ext_path *
499 ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
500 struct ext4_ext_path *path)
502 struct ext4_extent_header *eh;
503 struct buffer_head *bh;
504 short int depth, i, ppos = 0, alloc = 0;
506 eh = ext_inode_hdr(inode);
507 depth = ext_depth(inode);
508 if (ext4_ext_check_header(inode, eh, depth))
509 return ERR_PTR(-EIO);
512 /* account possible depth increase */
513 if (!path) {
514 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
515 GFP_NOFS);
516 if (!path)
517 return ERR_PTR(-ENOMEM);
518 alloc = 1;
520 path[0].p_hdr = eh;
522 i = depth;
523 /* walk through the tree */
524 while (i) {
525 ext_debug("depth %d: num %d, max %d\n",
526 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
528 ext4_ext_binsearch_idx(inode, path + ppos, block);
529 path[ppos].p_block = idx_pblock(path[ppos].p_idx);
530 path[ppos].p_depth = i;
531 path[ppos].p_ext = NULL;
533 bh = sb_bread(inode->i_sb, path[ppos].p_block);
534 if (!bh)
535 goto err;
537 eh = ext_block_hdr(bh);
538 ppos++;
539 BUG_ON(ppos > depth);
540 path[ppos].p_bh = bh;
541 path[ppos].p_hdr = eh;
542 i--;
544 if (ext4_ext_check_header(inode, eh, i))
545 goto err;
548 path[ppos].p_depth = i;
549 path[ppos].p_hdr = eh;
550 path[ppos].p_ext = NULL;
551 path[ppos].p_idx = NULL;
553 /* find extent */
554 ext4_ext_binsearch(inode, path + ppos, block);
556 ext4_ext_show_path(inode, path);
558 return path;
560 err:
561 ext4_ext_drop_refs(path);
562 if (alloc)
563 kfree(path);
564 return ERR_PTR(-EIO);
568 * ext4_ext_insert_index:
569 * insert new index [@logical;@ptr] into the block at @curp;
570 * check where to insert: before @curp or after @curp
572 static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
573 struct ext4_ext_path *curp,
574 int logical, ext4_fsblk_t ptr)
576 struct ext4_extent_idx *ix;
577 int len, err;
579 err = ext4_ext_get_access(handle, inode, curp);
580 if (err)
581 return err;
583 BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block));
584 len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
585 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
586 /* insert after */
587 if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
588 len = (len - 1) * sizeof(struct ext4_extent_idx);
589 len = len < 0 ? 0 : len;
590 ext_debug("insert new index %d after: %llu. "
591 "move %d from 0x%p to 0x%p\n",
592 logical, ptr, len,
593 (curp->p_idx + 1), (curp->p_idx + 2));
594 memmove(curp->p_idx + 2, curp->p_idx + 1, len);
596 ix = curp->p_idx + 1;
597 } else {
598 /* insert before */
599 len = len * sizeof(struct ext4_extent_idx);
600 len = len < 0 ? 0 : len;
601 ext_debug("insert new index %d before: %llu. "
602 "move %d from 0x%p to 0x%p\n",
603 logical, ptr, len,
604 curp->p_idx, (curp->p_idx + 1));
605 memmove(curp->p_idx + 1, curp->p_idx, len);
606 ix = curp->p_idx;
609 ix->ei_block = cpu_to_le32(logical);
610 ext4_idx_store_pblock(ix, ptr);
611 curp->p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(curp->p_hdr->eh_entries)+1);
613 BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries)
614 > le16_to_cpu(curp->p_hdr->eh_max));
615 BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr));
617 err = ext4_ext_dirty(handle, inode, curp);
618 ext4_std_error(inode->i_sb, err);
620 return err;
624 * ext4_ext_split:
625 * inserts new subtree into the path, using free index entry
626 * at depth @at:
627 * - allocates all needed blocks (new leaf and all intermediate index blocks)
628 * - makes decision where to split
629 * - moves remaining extents and index entries (right to the split point)
630 * into the newly allocated blocks
631 * - initializes subtree
633 static int ext4_ext_split(handle_t *handle, struct inode *inode,
634 struct ext4_ext_path *path,
635 struct ext4_extent *newext, int at)
637 struct buffer_head *bh = NULL;
638 int depth = ext_depth(inode);
639 struct ext4_extent_header *neh;
640 struct ext4_extent_idx *fidx;
641 struct ext4_extent *ex;
642 int i = at, k, m, a;
643 ext4_fsblk_t newblock, oldblock;
644 __le32 border;
645 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
646 int err = 0;
648 /* make decision: where to split? */
649 /* FIXME: now decision is simplest: at current extent */
651 /* if current leaf will be split, then we should use
652 * border from split point */
653 BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr));
654 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
655 border = path[depth].p_ext[1].ee_block;
656 ext_debug("leaf will be split."
657 " next leaf starts at %d\n",
658 le32_to_cpu(border));
659 } else {
660 border = newext->ee_block;
661 ext_debug("leaf will be added."
662 " next leaf starts at %d\n",
663 le32_to_cpu(border));
667 * If error occurs, then we break processing
668 * and mark filesystem read-only. index won't
669 * be inserted and tree will be in consistent
670 * state. Next mount will repair buffers too.
674 * Get array to track all allocated blocks.
675 * We need this to handle errors and free blocks
676 * upon them.
678 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
679 if (!ablocks)
680 return -ENOMEM;
682 /* allocate all needed blocks */
683 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
684 for (a = 0; a < depth - at; a++) {
685 newblock = ext4_ext_new_block(handle, inode, path, newext, &err);
686 if (newblock == 0)
687 goto cleanup;
688 ablocks[a] = newblock;
691 /* initialize new leaf */
692 newblock = ablocks[--a];
693 BUG_ON(newblock == 0);
694 bh = sb_getblk(inode->i_sb, newblock);
695 if (!bh) {
696 err = -EIO;
697 goto cleanup;
699 lock_buffer(bh);
701 err = ext4_journal_get_create_access(handle, bh);
702 if (err)
703 goto cleanup;
705 neh = ext_block_hdr(bh);
706 neh->eh_entries = 0;
707 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
708 neh->eh_magic = EXT4_EXT_MAGIC;
709 neh->eh_depth = 0;
710 ex = EXT_FIRST_EXTENT(neh);
712 /* move remainder of path[depth] to the new leaf */
713 BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max);
714 /* start copy from next extent */
715 /* TODO: we could do it by single memmove */
716 m = 0;
717 path[depth].p_ext++;
718 while (path[depth].p_ext <=
719 EXT_MAX_EXTENT(path[depth].p_hdr)) {
720 ext_debug("move %d:%llu:%d in new leaf %llu\n",
721 le32_to_cpu(path[depth].p_ext->ee_block),
722 ext_pblock(path[depth].p_ext),
723 ext4_ext_get_actual_len(path[depth].p_ext),
724 newblock);
725 /*memmove(ex++, path[depth].p_ext++,
726 sizeof(struct ext4_extent));
727 neh->eh_entries++;*/
728 path[depth].p_ext++;
729 m++;
731 if (m) {
732 memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
733 neh->eh_entries = cpu_to_le16(le16_to_cpu(neh->eh_entries)+m);
736 set_buffer_uptodate(bh);
737 unlock_buffer(bh);
739 err = ext4_journal_dirty_metadata(handle, bh);
740 if (err)
741 goto cleanup;
742 brelse(bh);
743 bh = NULL;
745 /* correct old leaf */
746 if (m) {
747 err = ext4_ext_get_access(handle, inode, path + depth);
748 if (err)
749 goto cleanup;
750 path[depth].p_hdr->eh_entries =
751 cpu_to_le16(le16_to_cpu(path[depth].p_hdr->eh_entries)-m);
752 err = ext4_ext_dirty(handle, inode, path + depth);
753 if (err)
754 goto cleanup;
758 /* create intermediate indexes */
759 k = depth - at - 1;
760 BUG_ON(k < 0);
761 if (k)
762 ext_debug("create %d intermediate indices\n", k);
763 /* insert new index into current index block */
764 /* current depth stored in i var */
765 i = depth - 1;
766 while (k--) {
767 oldblock = newblock;
768 newblock = ablocks[--a];
769 bh = sb_getblk(inode->i_sb, (ext4_fsblk_t)newblock);
770 if (!bh) {
771 err = -EIO;
772 goto cleanup;
774 lock_buffer(bh);
776 err = ext4_journal_get_create_access(handle, bh);
777 if (err)
778 goto cleanup;
780 neh = ext_block_hdr(bh);
781 neh->eh_entries = cpu_to_le16(1);
782 neh->eh_magic = EXT4_EXT_MAGIC;
783 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
784 neh->eh_depth = cpu_to_le16(depth - i);
785 fidx = EXT_FIRST_INDEX(neh);
786 fidx->ei_block = border;
787 ext4_idx_store_pblock(fidx, oldblock);
789 ext_debug("int.index at %d (block %llu): %lu -> %llu\n", i,
790 newblock, (unsigned long) le32_to_cpu(border),
791 oldblock);
792 /* copy indexes */
793 m = 0;
794 path[i].p_idx++;
796 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
797 EXT_MAX_INDEX(path[i].p_hdr));
798 BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) !=
799 EXT_LAST_INDEX(path[i].p_hdr));
800 while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
801 ext_debug("%d: move %d:%llu in new index %llu\n", i,
802 le32_to_cpu(path[i].p_idx->ei_block),
803 idx_pblock(path[i].p_idx),
804 newblock);
805 /*memmove(++fidx, path[i].p_idx++,
806 sizeof(struct ext4_extent_idx));
807 neh->eh_entries++;
808 BUG_ON(neh->eh_entries > neh->eh_max);*/
809 path[i].p_idx++;
810 m++;
812 if (m) {
813 memmove(++fidx, path[i].p_idx - m,
814 sizeof(struct ext4_extent_idx) * m);
815 neh->eh_entries =
816 cpu_to_le16(le16_to_cpu(neh->eh_entries) + m);
818 set_buffer_uptodate(bh);
819 unlock_buffer(bh);
821 err = ext4_journal_dirty_metadata(handle, bh);
822 if (err)
823 goto cleanup;
824 brelse(bh);
825 bh = NULL;
827 /* correct old index */
828 if (m) {
829 err = ext4_ext_get_access(handle, inode, path + i);
830 if (err)
831 goto cleanup;
832 path[i].p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(path[i].p_hdr->eh_entries)-m);
833 err = ext4_ext_dirty(handle, inode, path + i);
834 if (err)
835 goto cleanup;
838 i--;
841 /* insert new index */
842 err = ext4_ext_insert_index(handle, inode, path + at,
843 le32_to_cpu(border), newblock);
845 cleanup:
846 if (bh) {
847 if (buffer_locked(bh))
848 unlock_buffer(bh);
849 brelse(bh);
852 if (err) {
853 /* free all allocated blocks in error case */
854 for (i = 0; i < depth; i++) {
855 if (!ablocks[i])
856 continue;
857 ext4_free_blocks(handle, inode, ablocks[i], 1, 1);
860 kfree(ablocks);
862 return err;
866 * ext4_ext_grow_indepth:
867 * implements tree growing procedure:
868 * - allocates new block
869 * - moves top-level data (index block or leaf) into the new block
870 * - initializes new top-level, creating index that points to the
871 * just created block
873 static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
874 struct ext4_ext_path *path,
875 struct ext4_extent *newext)
877 struct ext4_ext_path *curp = path;
878 struct ext4_extent_header *neh;
879 struct ext4_extent_idx *fidx;
880 struct buffer_head *bh;
881 ext4_fsblk_t newblock;
882 int err = 0;
884 newblock = ext4_ext_new_block(handle, inode, path, newext, &err);
885 if (newblock == 0)
886 return err;
888 bh = sb_getblk(inode->i_sb, newblock);
889 if (!bh) {
890 err = -EIO;
891 ext4_std_error(inode->i_sb, err);
892 return err;
894 lock_buffer(bh);
896 err = ext4_journal_get_create_access(handle, bh);
897 if (err) {
898 unlock_buffer(bh);
899 goto out;
902 /* move top-level index/leaf into new block */
903 memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
905 /* set size of new block */
906 neh = ext_block_hdr(bh);
907 /* old root could have indexes or leaves
908 * so calculate e_max right way */
909 if (ext_depth(inode))
910 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
911 else
912 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
913 neh->eh_magic = EXT4_EXT_MAGIC;
914 set_buffer_uptodate(bh);
915 unlock_buffer(bh);
917 err = ext4_journal_dirty_metadata(handle, bh);
918 if (err)
919 goto out;
921 /* create index in new top-level index: num,max,pointer */
922 err = ext4_ext_get_access(handle, inode, curp);
923 if (err)
924 goto out;
926 curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
927 curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode));
928 curp->p_hdr->eh_entries = cpu_to_le16(1);
929 curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
931 if (path[0].p_hdr->eh_depth)
932 curp->p_idx->ei_block =
933 EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
934 else
935 curp->p_idx->ei_block =
936 EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
937 ext4_idx_store_pblock(curp->p_idx, newblock);
939 neh = ext_inode_hdr(inode);
940 fidx = EXT_FIRST_INDEX(neh);
941 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
942 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
943 le32_to_cpu(fidx->ei_block), idx_pblock(fidx));
945 neh->eh_depth = cpu_to_le16(path->p_depth + 1);
946 err = ext4_ext_dirty(handle, inode, curp);
947 out:
948 brelse(bh);
950 return err;
954 * ext4_ext_create_new_leaf:
955 * finds empty index and adds new leaf.
956 * if no free index is found, then it requests in-depth growing.
958 static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
959 struct ext4_ext_path *path,
960 struct ext4_extent *newext)
962 struct ext4_ext_path *curp;
963 int depth, i, err = 0;
965 repeat:
966 i = depth = ext_depth(inode);
968 /* walk up to the tree and look for free index entry */
969 curp = path + depth;
970 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
971 i--;
972 curp--;
975 /* we use already allocated block for index block,
976 * so subsequent data blocks should be contiguous */
977 if (EXT_HAS_FREE_INDEX(curp)) {
978 /* if we found index with free entry, then use that
979 * entry: create all needed subtree and add new leaf */
980 err = ext4_ext_split(handle, inode, path, newext, i);
982 /* refill path */
983 ext4_ext_drop_refs(path);
984 path = ext4_ext_find_extent(inode,
985 (ext4_lblk_t )le32_to_cpu(newext->ee_block),
986 path);
987 if (IS_ERR(path))
988 err = PTR_ERR(path);
989 } else {
990 /* tree is full, time to grow in depth */
991 err = ext4_ext_grow_indepth(handle, inode, path, newext);
992 if (err)
993 goto out;
995 /* refill path */
996 ext4_ext_drop_refs(path);
997 path = ext4_ext_find_extent(inode,
998 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
999 path);
1000 if (IS_ERR(path)) {
1001 err = PTR_ERR(path);
1002 goto out;
1006 * only first (depth 0 -> 1) produces free space;
1007 * in all other cases we have to split the grown tree
1009 depth = ext_depth(inode);
1010 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
1011 /* now we need to split */
1012 goto repeat;
1016 out:
1017 return err;
1021 * search the closest allocated block to the left for *logical
1022 * and returns it at @logical + it's physical address at @phys
1023 * if *logical is the smallest allocated block, the function
1024 * returns 0 at @phys
1025 * return value contains 0 (success) or error code
1028 ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path,
1029 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1031 struct ext4_extent_idx *ix;
1032 struct ext4_extent *ex;
1033 int depth;
1035 BUG_ON(path == NULL);
1036 depth = path->p_depth;
1037 *phys = 0;
1039 if (depth == 0 && path->p_ext == NULL)
1040 return 0;
1042 /* usually extent in the path covers blocks smaller
1043 * then *logical, but it can be that extent is the
1044 * first one in the file */
1046 ex = path[depth].p_ext;
1047 if (*logical < le32_to_cpu(ex->ee_block)) {
1048 BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
1049 while (--depth >= 0) {
1050 ix = path[depth].p_idx;
1051 BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
1053 return 0;
1056 BUG_ON(*logical < le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len));
1058 *logical = le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len) - 1;
1059 *phys = ext_pblock(ex) + le16_to_cpu(ex->ee_len) - 1;
1060 return 0;
1064 * search the closest allocated block to the right for *logical
1065 * and returns it at @logical + it's physical address at @phys
1066 * if *logical is the smallest allocated block, the function
1067 * returns 0 at @phys
1068 * return value contains 0 (success) or error code
1071 ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path,
1072 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1074 struct buffer_head *bh = NULL;
1075 struct ext4_extent_header *eh;
1076 struct ext4_extent_idx *ix;
1077 struct ext4_extent *ex;
1078 ext4_fsblk_t block;
1079 int depth;
1081 BUG_ON(path == NULL);
1082 depth = path->p_depth;
1083 *phys = 0;
1085 if (depth == 0 && path->p_ext == NULL)
1086 return 0;
1088 /* usually extent in the path covers blocks smaller
1089 * then *logical, but it can be that extent is the
1090 * first one in the file */
1092 ex = path[depth].p_ext;
1093 if (*logical < le32_to_cpu(ex->ee_block)) {
1094 BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
1095 while (--depth >= 0) {
1096 ix = path[depth].p_idx;
1097 BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
1099 *logical = le32_to_cpu(ex->ee_block);
1100 *phys = ext_pblock(ex);
1101 return 0;
1104 BUG_ON(*logical < le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len));
1106 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1107 /* next allocated block in this leaf */
1108 ex++;
1109 *logical = le32_to_cpu(ex->ee_block);
1110 *phys = ext_pblock(ex);
1111 return 0;
1114 /* go up and search for index to the right */
1115 while (--depth >= 0) {
1116 ix = path[depth].p_idx;
1117 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
1118 break;
1121 if (depth < 0) {
1122 /* we've gone up to the root and
1123 * found no index to the right */
1124 return 0;
1127 /* we've found index to the right, let's
1128 * follow it and find the closest allocated
1129 * block to the right */
1130 ix++;
1131 block = idx_pblock(ix);
1132 while (++depth < path->p_depth) {
1133 bh = sb_bread(inode->i_sb, block);
1134 if (bh == NULL)
1135 return -EIO;
1136 eh = ext_block_hdr(bh);
1137 if (ext4_ext_check_header(inode, eh, depth)) {
1138 brelse(bh);
1139 return -EIO;
1141 ix = EXT_FIRST_INDEX(eh);
1142 block = idx_pblock(ix);
1143 brelse(bh);
1146 bh = sb_bread(inode->i_sb, block);
1147 if (bh == NULL)
1148 return -EIO;
1149 eh = ext_block_hdr(bh);
1150 if (ext4_ext_check_header(inode, eh, path->p_depth - depth)) {
1151 brelse(bh);
1152 return -EIO;
1154 ex = EXT_FIRST_EXTENT(eh);
1155 *logical = le32_to_cpu(ex->ee_block);
1156 *phys = ext_pblock(ex);
1157 brelse(bh);
1158 return 0;
1163 * ext4_ext_next_allocated_block:
1164 * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1165 * NOTE: it considers block number from index entry as
1166 * allocated block. Thus, index entries have to be consistent
1167 * with leaves.
1169 static unsigned long
1170 ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1172 int depth;
1174 BUG_ON(path == NULL);
1175 depth = path->p_depth;
1177 if (depth == 0 && path->p_ext == NULL)
1178 return EXT_MAX_BLOCK;
1180 while (depth >= 0) {
1181 if (depth == path->p_depth) {
1182 /* leaf */
1183 if (path[depth].p_ext !=
1184 EXT_LAST_EXTENT(path[depth].p_hdr))
1185 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1186 } else {
1187 /* index */
1188 if (path[depth].p_idx !=
1189 EXT_LAST_INDEX(path[depth].p_hdr))
1190 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1192 depth--;
1195 return EXT_MAX_BLOCK;
1199 * ext4_ext_next_leaf_block:
1200 * returns first allocated block from next leaf or EXT_MAX_BLOCK
1202 static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode,
1203 struct ext4_ext_path *path)
1205 int depth;
1207 BUG_ON(path == NULL);
1208 depth = path->p_depth;
1210 /* zero-tree has no leaf blocks at all */
1211 if (depth == 0)
1212 return EXT_MAX_BLOCK;
1214 /* go to index block */
1215 depth--;
1217 while (depth >= 0) {
1218 if (path[depth].p_idx !=
1219 EXT_LAST_INDEX(path[depth].p_hdr))
1220 return (ext4_lblk_t)le32_to_cpu(path[depth].p_idx[1].ei_block);
1221 depth--;
1224 return EXT_MAX_BLOCK;
1228 * ext4_ext_correct_indexes:
1229 * if leaf gets modified and modified extent is first in the leaf,
1230 * then we have to correct all indexes above.
1231 * TODO: do we need to correct tree in all cases?
1233 static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
1234 struct ext4_ext_path *path)
1236 struct ext4_extent_header *eh;
1237 int depth = ext_depth(inode);
1238 struct ext4_extent *ex;
1239 __le32 border;
1240 int k, err = 0;
1242 eh = path[depth].p_hdr;
1243 ex = path[depth].p_ext;
1244 BUG_ON(ex == NULL);
1245 BUG_ON(eh == NULL);
1247 if (depth == 0) {
1248 /* there is no tree at all */
1249 return 0;
1252 if (ex != EXT_FIRST_EXTENT(eh)) {
1253 /* we correct tree if first leaf got modified only */
1254 return 0;
1258 * TODO: we need correction if border is smaller than current one
1260 k = depth - 1;
1261 border = path[depth].p_ext->ee_block;
1262 err = ext4_ext_get_access(handle, inode, path + k);
1263 if (err)
1264 return err;
1265 path[k].p_idx->ei_block = border;
1266 err = ext4_ext_dirty(handle, inode, path + k);
1267 if (err)
1268 return err;
1270 while (k--) {
1271 /* change all left-side indexes */
1272 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1273 break;
1274 err = ext4_ext_get_access(handle, inode, path + k);
1275 if (err)
1276 break;
1277 path[k].p_idx->ei_block = border;
1278 err = ext4_ext_dirty(handle, inode, path + k);
1279 if (err)
1280 break;
1283 return err;
1286 static int
1287 ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1288 struct ext4_extent *ex2)
1290 unsigned short ext1_ee_len, ext2_ee_len, max_len;
1293 * Make sure that either both extents are uninitialized, or
1294 * both are _not_.
1296 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1297 return 0;
1299 if (ext4_ext_is_uninitialized(ex1))
1300 max_len = EXT_UNINIT_MAX_LEN;
1301 else
1302 max_len = EXT_INIT_MAX_LEN;
1304 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1305 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1307 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
1308 le32_to_cpu(ex2->ee_block))
1309 return 0;
1312 * To allow future support for preallocated extents to be added
1313 * as an RO_COMPAT feature, refuse to merge to extents if
1314 * this can result in the top bit of ee_len being set.
1316 if (ext1_ee_len + ext2_ee_len > max_len)
1317 return 0;
1318 #ifdef AGGRESSIVE_TEST
1319 if (le16_to_cpu(ex1->ee_len) >= 4)
1320 return 0;
1321 #endif
1323 if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2))
1324 return 1;
1325 return 0;
1329 * This function tries to merge the "ex" extent to the next extent in the tree.
1330 * It always tries to merge towards right. If you want to merge towards
1331 * left, pass "ex - 1" as argument instead of "ex".
1332 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1333 * 1 if they got merged.
1335 int ext4_ext_try_to_merge(struct inode *inode,
1336 struct ext4_ext_path *path,
1337 struct ext4_extent *ex)
1339 struct ext4_extent_header *eh;
1340 unsigned int depth, len;
1341 int merge_done = 0;
1342 int uninitialized = 0;
1344 depth = ext_depth(inode);
1345 BUG_ON(path[depth].p_hdr == NULL);
1346 eh = path[depth].p_hdr;
1348 while (ex < EXT_LAST_EXTENT(eh)) {
1349 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1350 break;
1351 /* merge with next extent! */
1352 if (ext4_ext_is_uninitialized(ex))
1353 uninitialized = 1;
1354 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1355 + ext4_ext_get_actual_len(ex + 1));
1356 if (uninitialized)
1357 ext4_ext_mark_uninitialized(ex);
1359 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1360 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1361 * sizeof(struct ext4_extent);
1362 memmove(ex + 1, ex + 2, len);
1364 eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries) - 1);
1365 merge_done = 1;
1366 WARN_ON(eh->eh_entries == 0);
1367 if (!eh->eh_entries)
1368 ext4_error(inode->i_sb, "ext4_ext_try_to_merge",
1369 "inode#%lu, eh->eh_entries = 0!", inode->i_ino);
1372 return merge_done;
1376 * check if a portion of the "newext" extent overlaps with an
1377 * existing extent.
1379 * If there is an overlap discovered, it updates the length of the newext
1380 * such that there will be no overlap, and then returns 1.
1381 * If there is no overlap found, it returns 0.
1383 unsigned int ext4_ext_check_overlap(struct inode *inode,
1384 struct ext4_extent *newext,
1385 struct ext4_ext_path *path)
1387 unsigned long b1, b2;
1388 unsigned int depth, len1;
1389 unsigned int ret = 0;
1391 b1 = le32_to_cpu(newext->ee_block);
1392 len1 = ext4_ext_get_actual_len(newext);
1393 depth = ext_depth(inode);
1394 if (!path[depth].p_ext)
1395 goto out;
1396 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
1399 * get the next allocated block if the extent in the path
1400 * is before the requested block(s)
1402 if (b2 < b1) {
1403 b2 = ext4_ext_next_allocated_block(path);
1404 if (b2 == EXT_MAX_BLOCK)
1405 goto out;
1408 /* check for wrap through zero */
1409 if (b1 + len1 < b1) {
1410 len1 = EXT_MAX_BLOCK - b1;
1411 newext->ee_len = cpu_to_le16(len1);
1412 ret = 1;
1415 /* check for overlap */
1416 if (b1 + len1 > b2) {
1417 newext->ee_len = cpu_to_le16(b2 - b1);
1418 ret = 1;
1420 out:
1421 return ret;
1425 * ext4_ext_insert_extent:
1426 * tries to merge requsted extent into the existing extent or
1427 * inserts requested extent as new one into the tree,
1428 * creating new leaf in the no-space case.
1430 int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1431 struct ext4_ext_path *path,
1432 struct ext4_extent *newext)
1434 struct ext4_extent_header * eh;
1435 struct ext4_extent *ex, *fex;
1436 struct ext4_extent *nearex; /* nearest extent */
1437 struct ext4_ext_path *npath = NULL;
1438 int depth, len, err;
1439 ext4_lblk_t next;
1440 unsigned uninitialized = 0;
1442 BUG_ON(ext4_ext_get_actual_len(newext) == 0);
1443 depth = ext_depth(inode);
1444 ex = path[depth].p_ext;
1445 BUG_ON(path[depth].p_hdr == NULL);
1447 /* try to insert block into found extent and return */
1448 if (ex && ext4_can_extents_be_merged(inode, ex, newext)) {
1449 ext_debug("append %d block to %d:%d (from %llu)\n",
1450 ext4_ext_get_actual_len(newext),
1451 le32_to_cpu(ex->ee_block),
1452 ext4_ext_get_actual_len(ex), ext_pblock(ex));
1453 err = ext4_ext_get_access(handle, inode, path + depth);
1454 if (err)
1455 return err;
1458 * ext4_can_extents_be_merged should have checked that either
1459 * both extents are uninitialized, or both aren't. Thus we
1460 * need to check only one of them here.
1462 if (ext4_ext_is_uninitialized(ex))
1463 uninitialized = 1;
1464 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1465 + ext4_ext_get_actual_len(newext));
1466 if (uninitialized)
1467 ext4_ext_mark_uninitialized(ex);
1468 eh = path[depth].p_hdr;
1469 nearex = ex;
1470 goto merge;
1473 repeat:
1474 depth = ext_depth(inode);
1475 eh = path[depth].p_hdr;
1476 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1477 goto has_space;
1479 /* probably next leaf has space for us? */
1480 fex = EXT_LAST_EXTENT(eh);
1481 next = ext4_ext_next_leaf_block(inode, path);
1482 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
1483 && next != EXT_MAX_BLOCK) {
1484 ext_debug("next leaf block - %d\n", next);
1485 BUG_ON(npath != NULL);
1486 npath = ext4_ext_find_extent(inode, next, NULL);
1487 if (IS_ERR(npath))
1488 return PTR_ERR(npath);
1489 BUG_ON(npath->p_depth != path->p_depth);
1490 eh = npath[depth].p_hdr;
1491 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
1492 ext_debug("next leaf isnt full(%d)\n",
1493 le16_to_cpu(eh->eh_entries));
1494 path = npath;
1495 goto repeat;
1497 ext_debug("next leaf has no free space(%d,%d)\n",
1498 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1502 * There is no free space in the found leaf.
1503 * We're gonna add a new leaf in the tree.
1505 err = ext4_ext_create_new_leaf(handle, inode, path, newext);
1506 if (err)
1507 goto cleanup;
1508 depth = ext_depth(inode);
1509 eh = path[depth].p_hdr;
1511 has_space:
1512 nearex = path[depth].p_ext;
1514 err = ext4_ext_get_access(handle, inode, path + depth);
1515 if (err)
1516 goto cleanup;
1518 if (!nearex) {
1519 /* there is no extent in this leaf, create first one */
1520 ext_debug("first extent in the leaf: %d:%llu:%d\n",
1521 le32_to_cpu(newext->ee_block),
1522 ext_pblock(newext),
1523 ext4_ext_get_actual_len(newext));
1524 path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1525 } else if (le32_to_cpu(newext->ee_block)
1526 > le32_to_cpu(nearex->ee_block)) {
1527 /* BUG_ON(newext->ee_block == nearex->ee_block); */
1528 if (nearex != EXT_LAST_EXTENT(eh)) {
1529 len = EXT_MAX_EXTENT(eh) - nearex;
1530 len = (len - 1) * sizeof(struct ext4_extent);
1531 len = len < 0 ? 0 : len;
1532 ext_debug("insert %d:%llu:%d after: nearest 0x%p, "
1533 "move %d from 0x%p to 0x%p\n",
1534 le32_to_cpu(newext->ee_block),
1535 ext_pblock(newext),
1536 ext4_ext_get_actual_len(newext),
1537 nearex, len, nearex + 1, nearex + 2);
1538 memmove(nearex + 2, nearex + 1, len);
1540 path[depth].p_ext = nearex + 1;
1541 } else {
1542 BUG_ON(newext->ee_block == nearex->ee_block);
1543 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1544 len = len < 0 ? 0 : len;
1545 ext_debug("insert %d:%llu:%d before: nearest 0x%p, "
1546 "move %d from 0x%p to 0x%p\n",
1547 le32_to_cpu(newext->ee_block),
1548 ext_pblock(newext),
1549 ext4_ext_get_actual_len(newext),
1550 nearex, len, nearex + 1, nearex + 2);
1551 memmove(nearex + 1, nearex, len);
1552 path[depth].p_ext = nearex;
1555 eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)+1);
1556 nearex = path[depth].p_ext;
1557 nearex->ee_block = newext->ee_block;
1558 ext4_ext_store_pblock(nearex, ext_pblock(newext));
1559 nearex->ee_len = newext->ee_len;
1561 merge:
1562 /* try to merge extents to the right */
1563 ext4_ext_try_to_merge(inode, path, nearex);
1565 /* try to merge extents to the left */
1567 /* time to correct all indexes above */
1568 err = ext4_ext_correct_indexes(handle, inode, path);
1569 if (err)
1570 goto cleanup;
1572 err = ext4_ext_dirty(handle, inode, path + depth);
1574 cleanup:
1575 if (npath) {
1576 ext4_ext_drop_refs(npath);
1577 kfree(npath);
1579 ext4_ext_tree_changed(inode);
1580 ext4_ext_invalidate_cache(inode);
1581 return err;
1584 static void
1585 ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
1586 __u32 len, ext4_fsblk_t start, int type)
1588 struct ext4_ext_cache *cex;
1589 BUG_ON(len == 0);
1590 cex = &EXT4_I(inode)->i_cached_extent;
1591 cex->ec_type = type;
1592 cex->ec_block = block;
1593 cex->ec_len = len;
1594 cex->ec_start = start;
1598 * ext4_ext_put_gap_in_cache:
1599 * calculate boundaries of the gap that the requested block fits into
1600 * and cache this gap
1602 static void
1603 ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
1604 ext4_lblk_t block)
1606 int depth = ext_depth(inode);
1607 unsigned long len;
1608 ext4_lblk_t lblock;
1609 struct ext4_extent *ex;
1611 ex = path[depth].p_ext;
1612 if (ex == NULL) {
1613 /* there is no extent yet, so gap is [0;-] */
1614 lblock = 0;
1615 len = EXT_MAX_BLOCK;
1616 ext_debug("cache gap(whole file):");
1617 } else if (block < le32_to_cpu(ex->ee_block)) {
1618 lblock = block;
1619 len = le32_to_cpu(ex->ee_block) - block;
1620 ext_debug("cache gap(before): %lu [%lu:%lu]",
1621 (unsigned long) block,
1622 (unsigned long) le32_to_cpu(ex->ee_block),
1623 (unsigned long) ext4_ext_get_actual_len(ex));
1624 } else if (block >= le32_to_cpu(ex->ee_block)
1625 + ext4_ext_get_actual_len(ex)) {
1626 lblock = le32_to_cpu(ex->ee_block)
1627 + ext4_ext_get_actual_len(ex);
1628 len = ext4_ext_next_allocated_block(path);
1629 ext_debug("cache gap(after): [%lu:%lu] %lu",
1630 (unsigned long) le32_to_cpu(ex->ee_block),
1631 (unsigned long) ext4_ext_get_actual_len(ex),
1632 (unsigned long) block);
1633 BUG_ON(len == lblock);
1634 len = len - lblock;
1635 } else {
1636 lblock = len = 0;
1637 BUG();
1640 ext_debug(" -> %lu:%lu\n", (unsigned long) lblock, len);
1641 ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
1644 static int
1645 ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
1646 struct ext4_extent *ex)
1648 struct ext4_ext_cache *cex;
1650 cex = &EXT4_I(inode)->i_cached_extent;
1652 /* has cache valid data? */
1653 if (cex->ec_type == EXT4_EXT_CACHE_NO)
1654 return EXT4_EXT_CACHE_NO;
1656 BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
1657 cex->ec_type != EXT4_EXT_CACHE_EXTENT);
1658 if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
1659 ex->ee_block = cpu_to_le32(cex->ec_block);
1660 ext4_ext_store_pblock(ex, cex->ec_start);
1661 ex->ee_len = cpu_to_le16(cex->ec_len);
1662 ext_debug("%lu cached by %lu:%lu:%llu\n",
1663 (unsigned long) block,
1664 (unsigned long) cex->ec_block,
1665 (unsigned long) cex->ec_len,
1666 cex->ec_start);
1667 return cex->ec_type;
1670 /* not in cache */
1671 return EXT4_EXT_CACHE_NO;
1675 * ext4_ext_rm_idx:
1676 * removes index from the index block.
1677 * It's used in truncate case only, thus all requests are for
1678 * last index in the block only.
1680 static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
1681 struct ext4_ext_path *path)
1683 struct buffer_head *bh;
1684 int err;
1685 ext4_fsblk_t leaf;
1687 /* free index block */
1688 path--;
1689 leaf = idx_pblock(path->p_idx);
1690 BUG_ON(path->p_hdr->eh_entries == 0);
1691 err = ext4_ext_get_access(handle, inode, path);
1692 if (err)
1693 return err;
1694 path->p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(path->p_hdr->eh_entries)-1);
1695 err = ext4_ext_dirty(handle, inode, path);
1696 if (err)
1697 return err;
1698 ext_debug("index is empty, remove it, free block %llu\n", leaf);
1699 bh = sb_find_get_block(inode->i_sb, leaf);
1700 ext4_forget(handle, 1, inode, bh, leaf);
1701 ext4_free_blocks(handle, inode, leaf, 1, 1);
1702 return err;
1706 * ext4_ext_calc_credits_for_insert:
1707 * This routine returns max. credits that the extent tree can consume.
1708 * It should be OK for low-performance paths like ->writepage()
1709 * To allow many writing processes to fit into a single transaction,
1710 * the caller should calculate credits under truncate_mutex and
1711 * pass the actual path.
1713 int ext4_ext_calc_credits_for_insert(struct inode *inode,
1714 struct ext4_ext_path *path)
1716 int depth, needed;
1718 if (path) {
1719 /* probably there is space in leaf? */
1720 depth = ext_depth(inode);
1721 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
1722 < le16_to_cpu(path[depth].p_hdr->eh_max))
1723 return 1;
1727 * given 32-bit logical block (4294967296 blocks), max. tree
1728 * can be 4 levels in depth -- 4 * 340^4 == 53453440000.
1729 * Let's also add one more level for imbalance.
1731 depth = 5;
1733 /* allocation of new data block(s) */
1734 needed = 2;
1737 * tree can be full, so it would need to grow in depth:
1738 * we need one credit to modify old root, credits for
1739 * new root will be added in split accounting
1741 needed += 1;
1744 * Index split can happen, we would need:
1745 * allocate intermediate indexes (bitmap + group)
1746 * + change two blocks at each level, but root (already included)
1748 needed += (depth * 2) + (depth * 2);
1750 /* any allocation modifies superblock */
1751 needed += 1;
1753 return needed;
1756 static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
1757 struct ext4_extent *ex,
1758 unsigned long from, unsigned long to)
1760 struct buffer_head *bh;
1761 unsigned short ee_len = ext4_ext_get_actual_len(ex);
1762 int i, metadata = 0;
1764 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
1765 metadata = 1;
1766 #ifdef EXTENTS_STATS
1768 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1769 spin_lock(&sbi->s_ext_stats_lock);
1770 sbi->s_ext_blocks += ee_len;
1771 sbi->s_ext_extents++;
1772 if (ee_len < sbi->s_ext_min)
1773 sbi->s_ext_min = ee_len;
1774 if (ee_len > sbi->s_ext_max)
1775 sbi->s_ext_max = ee_len;
1776 if (ext_depth(inode) > sbi->s_depth_max)
1777 sbi->s_depth_max = ext_depth(inode);
1778 spin_unlock(&sbi->s_ext_stats_lock);
1780 #endif
1781 if (from >= le32_to_cpu(ex->ee_block)
1782 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
1783 /* tail removal */
1784 unsigned long num;
1785 ext4_fsblk_t start;
1786 num = le32_to_cpu(ex->ee_block) + ee_len - from;
1787 start = ext_pblock(ex) + ee_len - num;
1788 ext_debug("free last %lu blocks starting %llu\n", num, start);
1789 for (i = 0; i < num; i++) {
1790 bh = sb_find_get_block(inode->i_sb, start + i);
1791 ext4_forget(handle, 0, inode, bh, start + i);
1793 ext4_free_blocks(handle, inode, start, num, metadata);
1794 } else if (from == le32_to_cpu(ex->ee_block)
1795 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
1796 printk("strange request: removal %lu-%lu from %u:%u\n",
1797 from, to, le32_to_cpu(ex->ee_block), ee_len);
1798 } else {
1799 printk("strange request: removal(2) %lu-%lu from %u:%u\n",
1800 from, to, le32_to_cpu(ex->ee_block), ee_len);
1802 return 0;
1805 static int
1806 ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
1807 struct ext4_ext_path *path, unsigned long start)
1809 int err = 0, correct_index = 0;
1810 int depth = ext_depth(inode), credits;
1811 struct ext4_extent_header *eh;
1812 unsigned a, b, block, num;
1813 unsigned long ex_ee_block;
1814 unsigned short ex_ee_len;
1815 unsigned uninitialized = 0;
1816 struct ext4_extent *ex;
1818 /* the header must be checked already in ext4_ext_remove_space() */
1819 ext_debug("truncate since %lu in leaf\n", start);
1820 if (!path[depth].p_hdr)
1821 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
1822 eh = path[depth].p_hdr;
1823 BUG_ON(eh == NULL);
1825 /* find where to start removing */
1826 ex = EXT_LAST_EXTENT(eh);
1828 ex_ee_block = le32_to_cpu(ex->ee_block);
1829 if (ext4_ext_is_uninitialized(ex))
1830 uninitialized = 1;
1831 ex_ee_len = ext4_ext_get_actual_len(ex);
1833 while (ex >= EXT_FIRST_EXTENT(eh) &&
1834 ex_ee_block + ex_ee_len > start) {
1835 ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len);
1836 path[depth].p_ext = ex;
1838 a = ex_ee_block > start ? ex_ee_block : start;
1839 b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
1840 ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
1842 ext_debug(" border %u:%u\n", a, b);
1844 if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
1845 block = 0;
1846 num = 0;
1847 BUG();
1848 } else if (a != ex_ee_block) {
1849 /* remove tail of the extent */
1850 block = ex_ee_block;
1851 num = a - block;
1852 } else if (b != ex_ee_block + ex_ee_len - 1) {
1853 /* remove head of the extent */
1854 block = a;
1855 num = b - a;
1856 /* there is no "make a hole" API yet */
1857 BUG();
1858 } else {
1859 /* remove whole extent: excellent! */
1860 block = ex_ee_block;
1861 num = 0;
1862 BUG_ON(a != ex_ee_block);
1863 BUG_ON(b != ex_ee_block + ex_ee_len - 1);
1866 /* at present, extent can't cross block group: */
1867 /* leaf + bitmap + group desc + sb + inode */
1868 credits = 5;
1869 if (ex == EXT_FIRST_EXTENT(eh)) {
1870 correct_index = 1;
1871 credits += (ext_depth(inode)) + 1;
1873 #ifdef CONFIG_QUOTA
1874 credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
1875 #endif
1877 handle = ext4_ext_journal_restart(handle, credits);
1878 if (IS_ERR(handle)) {
1879 err = PTR_ERR(handle);
1880 goto out;
1883 err = ext4_ext_get_access(handle, inode, path + depth);
1884 if (err)
1885 goto out;
1887 err = ext4_remove_blocks(handle, inode, ex, a, b);
1888 if (err)
1889 goto out;
1891 if (num == 0) {
1892 /* this extent is removed; mark slot entirely unused */
1893 ext4_ext_store_pblock(ex, 0);
1894 eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)-1);
1897 ex->ee_block = cpu_to_le32(block);
1898 ex->ee_len = cpu_to_le16(num);
1900 * Do not mark uninitialized if all the blocks in the
1901 * extent have been removed.
1903 if (uninitialized && num)
1904 ext4_ext_mark_uninitialized(ex);
1906 err = ext4_ext_dirty(handle, inode, path + depth);
1907 if (err)
1908 goto out;
1910 ext_debug("new extent: %u:%u:%llu\n", block, num,
1911 ext_pblock(ex));
1912 ex--;
1913 ex_ee_block = le32_to_cpu(ex->ee_block);
1914 ex_ee_len = ext4_ext_get_actual_len(ex);
1917 if (correct_index && eh->eh_entries)
1918 err = ext4_ext_correct_indexes(handle, inode, path);
1920 /* if this leaf is free, then we should
1921 * remove it from index block above */
1922 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
1923 err = ext4_ext_rm_idx(handle, inode, path + depth);
1925 out:
1926 return err;
1930 * ext4_ext_more_to_rm:
1931 * returns 1 if current index has to be freed (even partial)
1933 static int
1934 ext4_ext_more_to_rm(struct ext4_ext_path *path)
1936 BUG_ON(path->p_idx == NULL);
1938 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
1939 return 0;
1942 * if truncate on deeper level happened, it wasn't partial,
1943 * so we have to consider current index for truncation
1945 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
1946 return 0;
1947 return 1;
1950 static int ext4_ext_remove_space(struct inode *inode, unsigned long start)
1952 struct super_block *sb = inode->i_sb;
1953 int depth = ext_depth(inode);
1954 struct ext4_ext_path *path;
1955 handle_t *handle;
1956 int i = 0, err = 0;
1958 ext_debug("truncate since %lu\n", start);
1960 /* probably first extent we're gonna free will be last in block */
1961 handle = ext4_journal_start(inode, depth + 1);
1962 if (IS_ERR(handle))
1963 return PTR_ERR(handle);
1965 ext4_ext_invalidate_cache(inode);
1968 * We start scanning from right side, freeing all the blocks
1969 * after i_size and walking into the tree depth-wise.
1971 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_KERNEL);
1972 if (path == NULL) {
1973 ext4_journal_stop(handle);
1974 return -ENOMEM;
1976 path[0].p_hdr = ext_inode_hdr(inode);
1977 if (ext4_ext_check_header(inode, path[0].p_hdr, depth)) {
1978 err = -EIO;
1979 goto out;
1981 path[0].p_depth = depth;
1983 while (i >= 0 && err == 0) {
1984 if (i == depth) {
1985 /* this is leaf block */
1986 err = ext4_ext_rm_leaf(handle, inode, path, start);
1987 /* root level has p_bh == NULL, brelse() eats this */
1988 brelse(path[i].p_bh);
1989 path[i].p_bh = NULL;
1990 i--;
1991 continue;
1994 /* this is index block */
1995 if (!path[i].p_hdr) {
1996 ext_debug("initialize header\n");
1997 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
2000 if (!path[i].p_idx) {
2001 /* this level hasn't been touched yet */
2002 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2003 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2004 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2005 path[i].p_hdr,
2006 le16_to_cpu(path[i].p_hdr->eh_entries));
2007 } else {
2008 /* we were already here, see at next index */
2009 path[i].p_idx--;
2012 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2013 i, EXT_FIRST_INDEX(path[i].p_hdr),
2014 path[i].p_idx);
2015 if (ext4_ext_more_to_rm(path + i)) {
2016 struct buffer_head *bh;
2017 /* go to the next level */
2018 ext_debug("move to level %d (block %llu)\n",
2019 i + 1, idx_pblock(path[i].p_idx));
2020 memset(path + i + 1, 0, sizeof(*path));
2021 bh = sb_bread(sb, idx_pblock(path[i].p_idx));
2022 if (!bh) {
2023 /* should we reset i_size? */
2024 err = -EIO;
2025 break;
2027 if (WARN_ON(i + 1 > depth)) {
2028 err = -EIO;
2029 break;
2031 if (ext4_ext_check_header(inode, ext_block_hdr(bh),
2032 depth - i - 1)) {
2033 err = -EIO;
2034 break;
2036 path[i + 1].p_bh = bh;
2038 /* save actual number of indexes since this
2039 * number is changed at the next iteration */
2040 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2041 i++;
2042 } else {
2043 /* we finished processing this index, go up */
2044 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
2045 /* index is empty, remove it;
2046 * handle must be already prepared by the
2047 * truncatei_leaf() */
2048 err = ext4_ext_rm_idx(handle, inode, path + i);
2050 /* root level has p_bh == NULL, brelse() eats this */
2051 brelse(path[i].p_bh);
2052 path[i].p_bh = NULL;
2053 i--;
2054 ext_debug("return to level %d\n", i);
2058 /* TODO: flexible tree reduction should be here */
2059 if (path->p_hdr->eh_entries == 0) {
2061 * truncate to zero freed all the tree,
2062 * so we need to correct eh_depth
2064 err = ext4_ext_get_access(handle, inode, path);
2065 if (err == 0) {
2066 ext_inode_hdr(inode)->eh_depth = 0;
2067 ext_inode_hdr(inode)->eh_max =
2068 cpu_to_le16(ext4_ext_space_root(inode));
2069 err = ext4_ext_dirty(handle, inode, path);
2072 out:
2073 ext4_ext_tree_changed(inode);
2074 ext4_ext_drop_refs(path);
2075 kfree(path);
2076 ext4_journal_stop(handle);
2078 return err;
2082 * called at mount time
2084 void ext4_ext_init(struct super_block *sb)
2087 * possible initialization would be here
2090 if (test_opt(sb, EXTENTS)) {
2091 printk("EXT4-fs: file extents enabled");
2092 #ifdef AGGRESSIVE_TEST
2093 printk(", aggressive tests");
2094 #endif
2095 #ifdef CHECK_BINSEARCH
2096 printk(", check binsearch");
2097 #endif
2098 #ifdef EXTENTS_STATS
2099 printk(", stats");
2100 #endif
2101 printk("\n");
2102 #ifdef EXTENTS_STATS
2103 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2104 EXT4_SB(sb)->s_ext_min = 1 << 30;
2105 EXT4_SB(sb)->s_ext_max = 0;
2106 #endif
2111 * called at umount time
2113 void ext4_ext_release(struct super_block *sb)
2115 if (!test_opt(sb, EXTENTS))
2116 return;
2118 #ifdef EXTENTS_STATS
2119 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2120 struct ext4_sb_info *sbi = EXT4_SB(sb);
2121 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2122 sbi->s_ext_blocks, sbi->s_ext_extents,
2123 sbi->s_ext_blocks / sbi->s_ext_extents);
2124 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2125 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2127 #endif
2131 * This function is called by ext4_ext_get_blocks() if someone tries to write
2132 * to an uninitialized extent. It may result in splitting the uninitialized
2133 * extent into multiple extents (upto three - one initialized and two
2134 * uninitialized).
2135 * There are three possibilities:
2136 * a> There is no split required: Entire extent should be initialized
2137 * b> Splits in two extents: Write is happening at either end of the extent
2138 * c> Splits in three extents: Somone is writing in middle of the extent
2140 static int ext4_ext_convert_to_initialized(handle_t *handle, struct inode *inode,
2141 struct ext4_ext_path *path,
2142 ext4_lblk_t iblock,
2143 unsigned long max_blocks)
2145 struct ext4_extent *ex, newex;
2146 struct ext4_extent *ex1 = NULL;
2147 struct ext4_extent *ex2 = NULL;
2148 struct ext4_extent *ex3 = NULL;
2149 struct ext4_extent_header *eh;
2150 unsigned int allocated, ee_block, ee_len, depth;
2151 ext4_fsblk_t newblock;
2152 int err = 0;
2153 int ret = 0;
2155 depth = ext_depth(inode);
2156 eh = path[depth].p_hdr;
2157 ex = path[depth].p_ext;
2158 ee_block = le32_to_cpu(ex->ee_block);
2159 ee_len = ext4_ext_get_actual_len(ex);
2160 allocated = ee_len - (iblock - ee_block);
2161 newblock = iblock - ee_block + ext_pblock(ex);
2162 ex2 = ex;
2164 /* ex1: ee_block to iblock - 1 : uninitialized */
2165 if (iblock > ee_block) {
2166 ex1 = ex;
2167 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2168 ext4_ext_mark_uninitialized(ex1);
2169 ex2 = &newex;
2172 * for sanity, update the length of the ex2 extent before
2173 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2174 * overlap of blocks.
2176 if (!ex1 && allocated > max_blocks)
2177 ex2->ee_len = cpu_to_le16(max_blocks);
2178 /* ex3: to ee_block + ee_len : uninitialised */
2179 if (allocated > max_blocks) {
2180 unsigned int newdepth;
2181 ex3 = &newex;
2182 ex3->ee_block = cpu_to_le32(iblock + max_blocks);
2183 ext4_ext_store_pblock(ex3, newblock + max_blocks);
2184 ex3->ee_len = cpu_to_le16(allocated - max_blocks);
2185 ext4_ext_mark_uninitialized(ex3);
2186 err = ext4_ext_insert_extent(handle, inode, path, ex3);
2187 if (err)
2188 goto out;
2190 * The depth, and hence eh & ex might change
2191 * as part of the insert above.
2193 newdepth = ext_depth(inode);
2194 if (newdepth != depth) {
2195 depth = newdepth;
2196 path = ext4_ext_find_extent(inode, iblock, NULL);
2197 if (IS_ERR(path)) {
2198 err = PTR_ERR(path);
2199 path = NULL;
2200 goto out;
2202 eh = path[depth].p_hdr;
2203 ex = path[depth].p_ext;
2204 if (ex2 != &newex)
2205 ex2 = ex;
2207 allocated = max_blocks;
2210 * If there was a change of depth as part of the
2211 * insertion of ex3 above, we need to update the length
2212 * of the ex1 extent again here
2214 if (ex1 && ex1 != ex) {
2215 ex1 = ex;
2216 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2217 ext4_ext_mark_uninitialized(ex1);
2218 ex2 = &newex;
2220 /* ex2: iblock to iblock + maxblocks-1 : initialised */
2221 ex2->ee_block = cpu_to_le32(iblock);
2222 ext4_ext_store_pblock(ex2, newblock);
2223 ex2->ee_len = cpu_to_le16(allocated);
2224 if (ex2 != ex)
2225 goto insert;
2226 err = ext4_ext_get_access(handle, inode, path + depth);
2227 if (err)
2228 goto out;
2230 * New (initialized) extent starts from the first block
2231 * in the current extent. i.e., ex2 == ex
2232 * We have to see if it can be merged with the extent
2233 * on the left.
2235 if (ex2 > EXT_FIRST_EXTENT(eh)) {
2237 * To merge left, pass "ex2 - 1" to try_to_merge(),
2238 * since it merges towards right _only_.
2240 ret = ext4_ext_try_to_merge(inode, path, ex2 - 1);
2241 if (ret) {
2242 err = ext4_ext_correct_indexes(handle, inode, path);
2243 if (err)
2244 goto out;
2245 depth = ext_depth(inode);
2246 ex2--;
2250 * Try to Merge towards right. This might be required
2251 * only when the whole extent is being written to.
2252 * i.e. ex2 == ex and ex3 == NULL.
2254 if (!ex3) {
2255 ret = ext4_ext_try_to_merge(inode, path, ex2);
2256 if (ret) {
2257 err = ext4_ext_correct_indexes(handle, inode, path);
2258 if (err)
2259 goto out;
2262 /* Mark modified extent as dirty */
2263 err = ext4_ext_dirty(handle, inode, path + depth);
2264 goto out;
2265 insert:
2266 err = ext4_ext_insert_extent(handle, inode, path, &newex);
2267 out:
2268 return err ? err : allocated;
2272 * Need to be called with
2273 * mutex_lock(&EXT4_I(inode)->truncate_mutex);
2275 int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
2276 ext4_lblk_t iblock,
2277 unsigned long max_blocks, struct buffer_head *bh_result,
2278 int create, int extend_disksize)
2280 struct ext4_ext_path *path = NULL;
2281 struct ext4_extent_header *eh;
2282 struct ext4_extent newex, *ex;
2283 ext4_fsblk_t goal, newblock;
2284 int err = 0, depth, ret;
2285 unsigned long allocated = 0;
2286 struct ext4_allocation_request ar;
2288 __clear_bit(BH_New, &bh_result->b_state);
2289 ext_debug("blocks %lu/%lu requested for inode %u\n",
2290 (unsigned long) iblock, max_blocks,
2291 (unsigned) inode->i_ino);
2293 /* check in cache */
2294 goal = ext4_ext_in_cache(inode, iblock, &newex);
2295 if (goal) {
2296 if (goal == EXT4_EXT_CACHE_GAP) {
2297 if (!create) {
2299 * block isn't allocated yet and
2300 * user doesn't want to allocate it
2302 goto out2;
2304 /* we should allocate requested block */
2305 } else if (goal == EXT4_EXT_CACHE_EXTENT) {
2306 /* block is already allocated */
2307 newblock = iblock
2308 - le32_to_cpu(newex.ee_block)
2309 + ext_pblock(&newex);
2310 /* number of remaining blocks in the extent */
2311 allocated = le16_to_cpu(newex.ee_len) -
2312 (iblock - le32_to_cpu(newex.ee_block));
2313 goto out;
2314 } else {
2315 BUG();
2319 /* find extent for this block */
2320 path = ext4_ext_find_extent(inode, iblock, NULL);
2321 if (IS_ERR(path)) {
2322 err = PTR_ERR(path);
2323 path = NULL;
2324 goto out2;
2327 depth = ext_depth(inode);
2330 * consistent leaf must not be empty;
2331 * this situation is possible, though, _during_ tree modification;
2332 * this is why assert can't be put in ext4_ext_find_extent()
2334 BUG_ON(path[depth].p_ext == NULL && depth != 0);
2335 eh = path[depth].p_hdr;
2337 ex = path[depth].p_ext;
2338 if (ex) {
2339 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
2340 ext4_fsblk_t ee_start = ext_pblock(ex);
2341 unsigned short ee_len;
2344 * Uninitialized extents are treated as holes, except that
2345 * we split out initialized portions during a write.
2347 ee_len = ext4_ext_get_actual_len(ex);
2348 /* if found extent covers block, simply return it */
2349 if (iblock >= ee_block && iblock < ee_block + ee_len) {
2350 newblock = iblock - ee_block + ee_start;
2351 /* number of remaining blocks in the extent */
2352 allocated = ee_len - (iblock - ee_block);
2353 ext_debug("%d fit into %lu:%d -> %llu\n", (int) iblock,
2354 ee_block, ee_len, newblock);
2356 /* Do not put uninitialized extent in the cache */
2357 if (!ext4_ext_is_uninitialized(ex)) {
2358 ext4_ext_put_in_cache(inode, ee_block,
2359 ee_len, ee_start,
2360 EXT4_EXT_CACHE_EXTENT);
2361 goto out;
2363 if (create == EXT4_CREATE_UNINITIALIZED_EXT)
2364 goto out;
2365 if (!create)
2366 goto out2;
2368 ret = ext4_ext_convert_to_initialized(handle, inode,
2369 path, iblock,
2370 max_blocks);
2371 if (ret <= 0)
2372 goto out2;
2373 else
2374 allocated = ret;
2375 goto outnew;
2380 * requested block isn't allocated yet;
2381 * we couldn't try to create block if create flag is zero
2383 if (!create) {
2385 * put just found gap into cache to speed up
2386 * subsequent requests
2388 ext4_ext_put_gap_in_cache(inode, path, iblock);
2389 goto out2;
2392 * Okay, we need to do block allocation. Lazily initialize the block
2393 * allocation info here if necessary.
2395 if (S_ISREG(inode->i_mode) && (!EXT4_I(inode)->i_block_alloc_info))
2396 ext4_init_block_alloc_info(inode);
2398 /* find neighbour allocated blocks */
2399 ar.lleft = iblock;
2400 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
2401 if (err)
2402 goto out2;
2403 ar.lright = iblock;
2404 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright);
2405 if (err)
2406 goto out2;
2409 * See if request is beyond maximum number of blocks we can have in
2410 * a single extent. For an initialized extent this limit is
2411 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
2412 * EXT_UNINIT_MAX_LEN.
2414 if (max_blocks > EXT_INIT_MAX_LEN &&
2415 create != EXT4_CREATE_UNINITIALIZED_EXT)
2416 max_blocks = EXT_INIT_MAX_LEN;
2417 else if (max_blocks > EXT_UNINIT_MAX_LEN &&
2418 create == EXT4_CREATE_UNINITIALIZED_EXT)
2419 max_blocks = EXT_UNINIT_MAX_LEN;
2421 /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */
2422 newex.ee_block = cpu_to_le32(iblock);
2423 newex.ee_len = cpu_to_le16(max_blocks);
2424 err = ext4_ext_check_overlap(inode, &newex, path);
2425 if (err)
2426 allocated = le16_to_cpu(newex.ee_len);
2427 else
2428 allocated = max_blocks;
2430 /* allocate new block */
2431 ar.inode = inode;
2432 ar.goal = ext4_ext_find_goal(inode, path, iblock);
2433 ar.logical = iblock;
2434 ar.len = allocated;
2435 if (S_ISREG(inode->i_mode))
2436 ar.flags = EXT4_MB_HINT_DATA;
2437 else
2438 /* disable in-core preallocation for non-regular files */
2439 ar.flags = 0;
2440 newblock = ext4_mb_new_blocks(handle, &ar, &err);
2441 if (!newblock)
2442 goto out2;
2443 ext_debug("allocate new block: goal %llu, found %llu/%lu\n",
2444 goal, newblock, allocated);
2446 /* try to insert new extent into found leaf and return */
2447 ext4_ext_store_pblock(&newex, newblock);
2448 newex.ee_len = cpu_to_le16(ar.len);
2449 if (create == EXT4_CREATE_UNINITIALIZED_EXT) /* Mark uninitialized */
2450 ext4_ext_mark_uninitialized(&newex);
2451 err = ext4_ext_insert_extent(handle, inode, path, &newex);
2452 if (err) {
2453 /* free data blocks we just allocated */
2454 /* not a good idea to call discard here directly,
2455 * but otherwise we'd need to call it every free() */
2456 ext4_mb_discard_inode_preallocations(inode);
2457 ext4_free_blocks(handle, inode, ext_pblock(&newex),
2458 le16_to_cpu(newex.ee_len), 0);
2459 goto out2;
2462 if (extend_disksize && inode->i_size > EXT4_I(inode)->i_disksize)
2463 EXT4_I(inode)->i_disksize = inode->i_size;
2465 /* previous routine could use block we allocated */
2466 newblock = ext_pblock(&newex);
2467 allocated = le16_to_cpu(newex.ee_len);
2468 outnew:
2469 __set_bit(BH_New, &bh_result->b_state);
2471 /* Cache only when it is _not_ an uninitialized extent */
2472 if (create != EXT4_CREATE_UNINITIALIZED_EXT)
2473 ext4_ext_put_in_cache(inode, iblock, allocated, newblock,
2474 EXT4_EXT_CACHE_EXTENT);
2475 out:
2476 if (allocated > max_blocks)
2477 allocated = max_blocks;
2478 ext4_ext_show_leaf(inode, path);
2479 __set_bit(BH_Mapped, &bh_result->b_state);
2480 bh_result->b_bdev = inode->i_sb->s_bdev;
2481 bh_result->b_blocknr = newblock;
2482 out2:
2483 if (path) {
2484 ext4_ext_drop_refs(path);
2485 kfree(path);
2487 return err ? err : allocated;
2490 void ext4_ext_truncate(struct inode * inode, struct page *page)
2492 struct address_space *mapping = inode->i_mapping;
2493 struct super_block *sb = inode->i_sb;
2494 unsigned long last_block;
2495 handle_t *handle;
2496 int err = 0;
2499 * probably first extent we're gonna free will be last in block
2501 err = ext4_writepage_trans_blocks(inode) + 3;
2502 handle = ext4_journal_start(inode, err);
2503 if (IS_ERR(handle)) {
2504 if (page) {
2505 clear_highpage(page);
2506 flush_dcache_page(page);
2507 unlock_page(page);
2508 page_cache_release(page);
2510 return;
2513 if (page)
2514 ext4_block_truncate_page(handle, page, mapping, inode->i_size);
2516 mutex_lock(&EXT4_I(inode)->truncate_mutex);
2517 ext4_ext_invalidate_cache(inode);
2519 ext4_mb_discard_inode_preallocations(inode);
2522 * TODO: optimization is possible here.
2523 * Probably we need not scan at all,
2524 * because page truncation is enough.
2526 if (ext4_orphan_add(handle, inode))
2527 goto out_stop;
2529 /* we have to know where to truncate from in crash case */
2530 EXT4_I(inode)->i_disksize = inode->i_size;
2531 ext4_mark_inode_dirty(handle, inode);
2533 last_block = (inode->i_size + sb->s_blocksize - 1)
2534 >> EXT4_BLOCK_SIZE_BITS(sb);
2535 err = ext4_ext_remove_space(inode, last_block);
2537 /* In a multi-transaction truncate, we only make the final
2538 * transaction synchronous.
2540 if (IS_SYNC(inode))
2541 handle->h_sync = 1;
2543 out_stop:
2545 * If this was a simple ftruncate() and the file will remain alive,
2546 * then we need to clear up the orphan record which we created above.
2547 * However, if this was a real unlink then we were called by
2548 * ext4_delete_inode(), and we allow that function to clean up the
2549 * orphan info for us.
2551 if (inode->i_nlink)
2552 ext4_orphan_del(handle, inode);
2554 mutex_unlock(&EXT4_I(inode)->truncate_mutex);
2555 ext4_journal_stop(handle);
2559 * ext4_ext_writepage_trans_blocks:
2560 * calculate max number of blocks we could modify
2561 * in order to allocate new block for an inode
2563 int ext4_ext_writepage_trans_blocks(struct inode *inode, int num)
2565 int needed;
2567 needed = ext4_ext_calc_credits_for_insert(inode, NULL);
2569 /* caller wants to allocate num blocks, but note it includes sb */
2570 needed = needed * num - (num - 1);
2572 #ifdef CONFIG_QUOTA
2573 needed += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
2574 #endif
2576 return needed;
2580 * preallocate space for a file. This implements ext4's fallocate inode
2581 * operation, which gets called from sys_fallocate system call.
2582 * For block-mapped files, posix_fallocate should fall back to the method
2583 * of writing zeroes to the required new blocks (the same behavior which is
2584 * expected for file systems which do not support fallocate() system call).
2586 long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
2588 handle_t *handle;
2589 ext4_lblk_t block;
2590 unsigned long max_blocks;
2591 ext4_fsblk_t nblocks = 0;
2592 int ret = 0;
2593 int ret2 = 0;
2594 int retries = 0;
2595 struct buffer_head map_bh;
2596 unsigned int credits, blkbits = inode->i_blkbits;
2599 * currently supporting (pre)allocate mode for extent-based
2600 * files _only_
2602 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
2603 return -EOPNOTSUPP;
2605 /* preallocation to directories is currently not supported */
2606 if (S_ISDIR(inode->i_mode))
2607 return -ENODEV;
2609 block = offset >> blkbits;
2610 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
2611 - block;
2614 * credits to insert 1 extent into extent tree + buffers to be able to
2615 * modify 1 super block, 1 block bitmap and 1 group descriptor.
2617 credits = EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + 3;
2618 retry:
2619 while (ret >= 0 && ret < max_blocks) {
2620 block = block + ret;
2621 max_blocks = max_blocks - ret;
2622 handle = ext4_journal_start(inode, credits);
2623 if (IS_ERR(handle)) {
2624 ret = PTR_ERR(handle);
2625 break;
2628 ret = ext4_ext_get_blocks(handle, inode, block,
2629 max_blocks, &map_bh,
2630 EXT4_CREATE_UNINITIALIZED_EXT, 0);
2631 WARN_ON(!ret);
2632 if (!ret) {
2633 ext4_error(inode->i_sb, "ext4_fallocate",
2634 "ext4_ext_get_blocks returned 0! inode#%lu"
2635 ", block=%lu, max_blocks=%lu",
2636 inode->i_ino, (unsigned long)block,
2637 (unsigned long)max_blocks);
2638 ret = -EIO;
2639 ext4_mark_inode_dirty(handle, inode);
2640 ret2 = ext4_journal_stop(handle);
2641 break;
2643 if (ret > 0) {
2644 /* check wrap through sign-bit/zero here */
2645 if ((block + ret) < 0 || (block + ret) < block) {
2646 ret = -EIO;
2647 ext4_mark_inode_dirty(handle, inode);
2648 ret2 = ext4_journal_stop(handle);
2649 break;
2651 if (buffer_new(&map_bh) && ((block + ret) >
2652 (EXT4_BLOCK_ALIGN(i_size_read(inode), blkbits)
2653 >> blkbits)))
2654 nblocks = nblocks + ret;
2657 /* Update ctime if new blocks get allocated */
2658 if (nblocks) {
2659 struct timespec now;
2661 now = current_fs_time(inode->i_sb);
2662 if (!timespec_equal(&inode->i_ctime, &now))
2663 inode->i_ctime = now;
2666 ext4_mark_inode_dirty(handle, inode);
2667 ret2 = ext4_journal_stop(handle);
2668 if (ret2)
2669 break;
2672 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
2673 goto retry;
2676 * Time to update the file size.
2677 * Update only when preallocation was requested beyond the file size.
2679 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
2680 (offset + len) > i_size_read(inode)) {
2681 if (ret > 0) {
2683 * if no error, we assume preallocation succeeded
2684 * completely
2686 mutex_lock(&inode->i_mutex);
2687 i_size_write(inode, offset + len);
2688 EXT4_I(inode)->i_disksize = i_size_read(inode);
2689 mutex_unlock(&inode->i_mutex);
2690 } else if (ret < 0 && nblocks) {
2691 /* Handle partial allocation scenario */
2692 loff_t newsize;
2694 mutex_lock(&inode->i_mutex);
2695 newsize = (nblocks << blkbits) + i_size_read(inode);
2696 i_size_write(inode, EXT4_BLOCK_ALIGN(newsize, blkbits));
2697 EXT4_I(inode)->i_disksize = i_size_read(inode);
2698 mutex_unlock(&inode->i_mutex);
2702 return ret > 0 ? ret2 : ret;