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
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>
34 #include <linux/time.h>
35 #include <linux/jbd2.h>
36 #include <linux/highuid.h>
37 #include <linux/pagemap.h>
38 #include <linux/quotaops.h>
39 #include <linux/string.h>
40 #include <linux/slab.h>
41 #include <linux/falloc.h>
42 #include <asm/uaccess.h>
43 #include "ext4_jbd2.h"
44 #include "ext4_extents.h"
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)
55 block = le32_to_cpu(ex->ee_start_lo);
56 block |= ((ext4_fsblk_t) le16_to_cpu(ex->ee_start_hi) << 31) << 1;
62 * combine low and high parts of a leaf physical block number into ext4_fsblk_t
64 ext4_fsblk_t idx_pblock(struct ext4_extent_idx *ix)
68 block = le32_to_cpu(ix->ei_leaf_lo);
69 block |= ((ext4_fsblk_t) le16_to_cpu(ix->ei_leaf_hi) << 31) << 1;
74 * ext4_ext_store_pblock:
75 * stores a large physical block number into an extent struct,
76 * breaking it into parts
78 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)
99 if (handle->h_buffer_credits > needed)
101 if (!ext4_journal_extend(handle, needed))
103 err = ext4_journal_restart(handle, needed);
113 static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
114 struct ext4_ext_path *path)
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 */
131 static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
132 struct ext4_ext_path *path)
136 /* path points to block */
137 err = ext4_journal_dirty_metadata(handle, path->p_bh);
139 /* path points to leaf/index in inode body */
140 err = ext4_mark_inode_dirty(handle, inode);
145 static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
146 struct ext4_ext_path *path,
149 struct ext4_inode_info *ei = EXT4_I(inode);
150 ext4_fsblk_t bg_start;
151 ext4_fsblk_t last_block;
152 ext4_grpblk_t colour;
156 struct ext4_extent *ex;
157 depth = path->p_depth;
159 /* try to predict block placement */
160 ex = path[depth].p_ext;
162 return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block));
164 /* it looks like index is empty;
165 * try to find starting block from index itself */
166 if (path[depth].p_bh)
167 return path[depth].p_bh->b_blocknr;
170 /* OK. use inode's group */
171 bg_start = (ei->i_block_group * EXT4_BLOCKS_PER_GROUP(inode->i_sb)) +
172 le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_first_data_block);
173 last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
175 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
176 colour = (current->pid % 16) *
177 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
179 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
180 return bg_start + colour + block;
184 ext4_ext_new_block(handle_t *handle, struct inode *inode,
185 struct ext4_ext_path *path,
186 struct ext4_extent *ex, int *err)
188 ext4_fsblk_t goal, newblock;
190 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
191 newblock = ext4_new_block(handle, inode, goal, err);
195 static int ext4_ext_space_block(struct inode *inode)
199 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
200 / sizeof(struct ext4_extent);
201 #ifdef AGGRESSIVE_TEST
208 static int ext4_ext_space_block_idx(struct inode *inode)
212 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
213 / sizeof(struct ext4_extent_idx);
214 #ifdef AGGRESSIVE_TEST
221 static int ext4_ext_space_root(struct inode *inode)
225 size = sizeof(EXT4_I(inode)->i_data);
226 size -= sizeof(struct ext4_extent_header);
227 size /= sizeof(struct ext4_extent);
228 #ifdef AGGRESSIVE_TEST
235 static int ext4_ext_space_root_idx(struct inode *inode)
239 size = sizeof(EXT4_I(inode)->i_data);
240 size -= sizeof(struct ext4_extent_header);
241 size /= sizeof(struct ext4_extent_idx);
242 #ifdef AGGRESSIVE_TEST
250 ext4_ext_max_entries(struct inode *inode, int depth)
254 if (depth == ext_depth(inode)) {
256 max = ext4_ext_space_root(inode);
258 max = ext4_ext_space_root_idx(inode);
261 max = ext4_ext_space_block(inode);
263 max = ext4_ext_space_block_idx(inode);
269 static int __ext4_ext_check_header(const char *function, struct inode *inode,
270 struct ext4_extent_header *eh,
273 const char *error_msg;
276 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
277 error_msg = "invalid magic";
280 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
281 error_msg = "unexpected eh_depth";
284 if (unlikely(eh->eh_max == 0)) {
285 error_msg = "invalid eh_max";
288 max = ext4_ext_max_entries(inode, depth);
289 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
290 error_msg = "too large eh_max";
293 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
294 error_msg = "invalid eh_entries";
300 ext4_error(inode->i_sb, function,
301 "bad header in inode #%lu: %s - magic %x, "
302 "entries %u, max %u(%u), depth %u(%u)",
303 inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic),
304 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
305 max, le16_to_cpu(eh->eh_depth), depth);
310 #define ext4_ext_check_header(inode, eh, depth) \
311 __ext4_ext_check_header(__func__, inode, eh, depth)
314 static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
316 int k, l = path->p_depth;
319 for (k = 0; k <= l; k++, path++) {
321 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
322 idx_pblock(path->p_idx));
323 } else if (path->p_ext) {
324 ext_debug(" %d:%d:%llu ",
325 le32_to_cpu(path->p_ext->ee_block),
326 ext4_ext_get_actual_len(path->p_ext),
327 ext_pblock(path->p_ext));
334 static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
336 int depth = ext_depth(inode);
337 struct ext4_extent_header *eh;
338 struct ext4_extent *ex;
344 eh = path[depth].p_hdr;
345 ex = EXT_FIRST_EXTENT(eh);
347 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
348 ext_debug("%d:%d:%llu ", le32_to_cpu(ex->ee_block),
349 ext4_ext_get_actual_len(ex), ext_pblock(ex));
354 #define ext4_ext_show_path(inode,path)
355 #define ext4_ext_show_leaf(inode,path)
358 void ext4_ext_drop_refs(struct ext4_ext_path *path)
360 int depth = path->p_depth;
363 for (i = 0; i <= depth; i++, path++)
371 * ext4_ext_binsearch_idx:
372 * binary search for the closest index of the given block
373 * the header must be checked before calling this
376 ext4_ext_binsearch_idx(struct inode *inode,
377 struct ext4_ext_path *path, ext4_lblk_t block)
379 struct ext4_extent_header *eh = path->p_hdr;
380 struct ext4_extent_idx *r, *l, *m;
383 ext_debug("binsearch for %u(idx): ", block);
385 l = EXT_FIRST_INDEX(eh) + 1;
386 r = EXT_LAST_INDEX(eh);
389 if (block < le32_to_cpu(m->ei_block))
393 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
394 m, le32_to_cpu(m->ei_block),
395 r, le32_to_cpu(r->ei_block));
399 ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
400 idx_pblock(path->p_idx));
402 #ifdef CHECK_BINSEARCH
404 struct ext4_extent_idx *chix, *ix;
407 chix = ix = EXT_FIRST_INDEX(eh);
408 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
410 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
411 printk("k=%d, ix=0x%p, first=0x%p\n", k,
412 ix, EXT_FIRST_INDEX(eh));
414 le32_to_cpu(ix->ei_block),
415 le32_to_cpu(ix[-1].ei_block));
417 BUG_ON(k && le32_to_cpu(ix->ei_block)
418 <= le32_to_cpu(ix[-1].ei_block));
419 if (block < le32_to_cpu(ix->ei_block))
423 BUG_ON(chix != path->p_idx);
430 * ext4_ext_binsearch:
431 * binary search for closest extent of the given block
432 * the header must be checked before calling this
435 ext4_ext_binsearch(struct inode *inode,
436 struct ext4_ext_path *path, ext4_lblk_t block)
438 struct ext4_extent_header *eh = path->p_hdr;
439 struct ext4_extent *r, *l, *m;
441 if (eh->eh_entries == 0) {
443 * this leaf is empty:
444 * we get such a leaf in split/add case
449 ext_debug("binsearch for %u: ", block);
451 l = EXT_FIRST_EXTENT(eh) + 1;
452 r = EXT_LAST_EXTENT(eh);
456 if (block < le32_to_cpu(m->ee_block))
460 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
461 m, le32_to_cpu(m->ee_block),
462 r, le32_to_cpu(r->ee_block));
466 ext_debug(" -> %d:%llu:%d ",
467 le32_to_cpu(path->p_ext->ee_block),
468 ext_pblock(path->p_ext),
469 ext4_ext_get_actual_len(path->p_ext));
471 #ifdef CHECK_BINSEARCH
473 struct ext4_extent *chex, *ex;
476 chex = ex = EXT_FIRST_EXTENT(eh);
477 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
478 BUG_ON(k && le32_to_cpu(ex->ee_block)
479 <= le32_to_cpu(ex[-1].ee_block));
480 if (block < le32_to_cpu(ex->ee_block))
484 BUG_ON(chex != path->p_ext);
490 int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
492 struct ext4_extent_header *eh;
494 eh = ext_inode_hdr(inode);
497 eh->eh_magic = EXT4_EXT_MAGIC;
498 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode));
499 ext4_mark_inode_dirty(handle, inode);
500 ext4_ext_invalidate_cache(inode);
504 struct ext4_ext_path *
505 ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
506 struct ext4_ext_path *path)
508 struct ext4_extent_header *eh;
509 struct buffer_head *bh;
510 short int depth, i, ppos = 0, alloc = 0;
512 eh = ext_inode_hdr(inode);
513 depth = ext_depth(inode);
514 if (ext4_ext_check_header(inode, eh, depth))
515 return ERR_PTR(-EIO);
518 /* account possible depth increase */
520 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
523 return ERR_PTR(-ENOMEM);
529 /* walk through the tree */
531 ext_debug("depth %d: num %d, max %d\n",
532 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
534 ext4_ext_binsearch_idx(inode, path + ppos, block);
535 path[ppos].p_block = idx_pblock(path[ppos].p_idx);
536 path[ppos].p_depth = i;
537 path[ppos].p_ext = NULL;
539 bh = sb_bread(inode->i_sb, path[ppos].p_block);
543 eh = ext_block_hdr(bh);
545 BUG_ON(ppos > depth);
546 path[ppos].p_bh = bh;
547 path[ppos].p_hdr = eh;
550 if (ext4_ext_check_header(inode, eh, i))
554 path[ppos].p_depth = i;
555 path[ppos].p_hdr = eh;
556 path[ppos].p_ext = NULL;
557 path[ppos].p_idx = NULL;
560 ext4_ext_binsearch(inode, path + ppos, block);
562 ext4_ext_show_path(inode, path);
567 ext4_ext_drop_refs(path);
570 return ERR_PTR(-EIO);
574 * ext4_ext_insert_index:
575 * insert new index [@logical;@ptr] into the block at @curp;
576 * check where to insert: before @curp or after @curp
578 static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
579 struct ext4_ext_path *curp,
580 int logical, ext4_fsblk_t ptr)
582 struct ext4_extent_idx *ix;
585 err = ext4_ext_get_access(handle, inode, curp);
589 BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block));
590 len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
591 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
593 if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
594 len = (len - 1) * sizeof(struct ext4_extent_idx);
595 len = len < 0 ? 0 : len;
596 ext_debug("insert new index %d after: %llu. "
597 "move %d from 0x%p to 0x%p\n",
599 (curp->p_idx + 1), (curp->p_idx + 2));
600 memmove(curp->p_idx + 2, curp->p_idx + 1, len);
602 ix = curp->p_idx + 1;
605 len = len * sizeof(struct ext4_extent_idx);
606 len = len < 0 ? 0 : len;
607 ext_debug("insert new index %d before: %llu. "
608 "move %d from 0x%p to 0x%p\n",
610 curp->p_idx, (curp->p_idx + 1));
611 memmove(curp->p_idx + 1, curp->p_idx, len);
615 ix->ei_block = cpu_to_le32(logical);
616 ext4_idx_store_pblock(ix, ptr);
617 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
619 BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries)
620 > le16_to_cpu(curp->p_hdr->eh_max));
621 BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr));
623 err = ext4_ext_dirty(handle, inode, curp);
624 ext4_std_error(inode->i_sb, err);
631 * inserts new subtree into the path, using free index entry
633 * - allocates all needed blocks (new leaf and all intermediate index blocks)
634 * - makes decision where to split
635 * - moves remaining extents and index entries (right to the split point)
636 * into the newly allocated blocks
637 * - initializes subtree
639 static int ext4_ext_split(handle_t *handle, struct inode *inode,
640 struct ext4_ext_path *path,
641 struct ext4_extent *newext, int at)
643 struct buffer_head *bh = NULL;
644 int depth = ext_depth(inode);
645 struct ext4_extent_header *neh;
646 struct ext4_extent_idx *fidx;
647 struct ext4_extent *ex;
649 ext4_fsblk_t newblock, oldblock;
651 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
654 /* make decision: where to split? */
655 /* FIXME: now decision is simplest: at current extent */
657 /* if current leaf will be split, then we should use
658 * border from split point */
659 BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr));
660 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
661 border = path[depth].p_ext[1].ee_block;
662 ext_debug("leaf will be split."
663 " next leaf starts at %d\n",
664 le32_to_cpu(border));
666 border = newext->ee_block;
667 ext_debug("leaf will be added."
668 " next leaf starts at %d\n",
669 le32_to_cpu(border));
673 * If error occurs, then we break processing
674 * and mark filesystem read-only. index won't
675 * be inserted and tree will be in consistent
676 * state. Next mount will repair buffers too.
680 * Get array to track all allocated blocks.
681 * We need this to handle errors and free blocks
684 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
688 /* allocate all needed blocks */
689 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
690 for (a = 0; a < depth - at; a++) {
691 newblock = ext4_ext_new_block(handle, inode, path, newext, &err);
694 ablocks[a] = newblock;
697 /* initialize new leaf */
698 newblock = ablocks[--a];
699 BUG_ON(newblock == 0);
700 bh = sb_getblk(inode->i_sb, newblock);
707 err = ext4_journal_get_create_access(handle, bh);
711 neh = ext_block_hdr(bh);
713 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
714 neh->eh_magic = EXT4_EXT_MAGIC;
716 ex = EXT_FIRST_EXTENT(neh);
718 /* move remainder of path[depth] to the new leaf */
719 BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max);
720 /* start copy from next extent */
721 /* TODO: we could do it by single memmove */
724 while (path[depth].p_ext <=
725 EXT_MAX_EXTENT(path[depth].p_hdr)) {
726 ext_debug("move %d:%llu:%d in new leaf %llu\n",
727 le32_to_cpu(path[depth].p_ext->ee_block),
728 ext_pblock(path[depth].p_ext),
729 ext4_ext_get_actual_len(path[depth].p_ext),
731 /*memmove(ex++, path[depth].p_ext++,
732 sizeof(struct ext4_extent));
738 memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
739 le16_add_cpu(&neh->eh_entries, m);
742 set_buffer_uptodate(bh);
745 err = ext4_journal_dirty_metadata(handle, bh);
751 /* correct old leaf */
753 err = ext4_ext_get_access(handle, inode, path + depth);
756 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
757 err = ext4_ext_dirty(handle, inode, path + depth);
763 /* create intermediate indexes */
767 ext_debug("create %d intermediate indices\n", k);
768 /* insert new index into current index block */
769 /* current depth stored in i var */
773 newblock = ablocks[--a];
774 bh = sb_getblk(inode->i_sb, newblock);
781 err = ext4_journal_get_create_access(handle, bh);
785 neh = ext_block_hdr(bh);
786 neh->eh_entries = cpu_to_le16(1);
787 neh->eh_magic = EXT4_EXT_MAGIC;
788 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
789 neh->eh_depth = cpu_to_le16(depth - i);
790 fidx = EXT_FIRST_INDEX(neh);
791 fidx->ei_block = border;
792 ext4_idx_store_pblock(fidx, oldblock);
794 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
795 i, newblock, le32_to_cpu(border), oldblock);
800 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
801 EXT_MAX_INDEX(path[i].p_hdr));
802 BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) !=
803 EXT_LAST_INDEX(path[i].p_hdr));
804 while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
805 ext_debug("%d: move %d:%llu in new index %llu\n", i,
806 le32_to_cpu(path[i].p_idx->ei_block),
807 idx_pblock(path[i].p_idx),
809 /*memmove(++fidx, path[i].p_idx++,
810 sizeof(struct ext4_extent_idx));
812 BUG_ON(neh->eh_entries > neh->eh_max);*/
817 memmove(++fidx, path[i].p_idx - m,
818 sizeof(struct ext4_extent_idx) * m);
819 le16_add_cpu(&neh->eh_entries, m);
821 set_buffer_uptodate(bh);
824 err = ext4_journal_dirty_metadata(handle, bh);
830 /* correct old index */
832 err = ext4_ext_get_access(handle, inode, path + i);
835 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
836 err = ext4_ext_dirty(handle, inode, path + i);
844 /* insert new index */
845 err = ext4_ext_insert_index(handle, inode, path + at,
846 le32_to_cpu(border), newblock);
850 if (buffer_locked(bh))
856 /* free all allocated blocks in error case */
857 for (i = 0; i < depth; i++) {
860 ext4_free_blocks(handle, inode, ablocks[i], 1, 1);
869 * ext4_ext_grow_indepth:
870 * implements tree growing procedure:
871 * - allocates new block
872 * - moves top-level data (index block or leaf) into the new block
873 * - initializes new top-level, creating index that points to the
876 static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
877 struct ext4_ext_path *path,
878 struct ext4_extent *newext)
880 struct ext4_ext_path *curp = path;
881 struct ext4_extent_header *neh;
882 struct ext4_extent_idx *fidx;
883 struct buffer_head *bh;
884 ext4_fsblk_t newblock;
887 newblock = ext4_ext_new_block(handle, inode, path, newext, &err);
891 bh = sb_getblk(inode->i_sb, newblock);
894 ext4_std_error(inode->i_sb, err);
899 err = ext4_journal_get_create_access(handle, bh);
905 /* move top-level index/leaf into new block */
906 memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
908 /* set size of new block */
909 neh = ext_block_hdr(bh);
910 /* old root could have indexes or leaves
911 * so calculate e_max right way */
912 if (ext_depth(inode))
913 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
915 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
916 neh->eh_magic = EXT4_EXT_MAGIC;
917 set_buffer_uptodate(bh);
920 err = ext4_journal_dirty_metadata(handle, bh);
924 /* create index in new top-level index: num,max,pointer */
925 err = ext4_ext_get_access(handle, inode, curp);
929 curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
930 curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode));
931 curp->p_hdr->eh_entries = cpu_to_le16(1);
932 curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
934 if (path[0].p_hdr->eh_depth)
935 curp->p_idx->ei_block =
936 EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
938 curp->p_idx->ei_block =
939 EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
940 ext4_idx_store_pblock(curp->p_idx, newblock);
942 neh = ext_inode_hdr(inode);
943 fidx = EXT_FIRST_INDEX(neh);
944 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
945 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
946 le32_to_cpu(fidx->ei_block), idx_pblock(fidx));
948 neh->eh_depth = cpu_to_le16(path->p_depth + 1);
949 err = ext4_ext_dirty(handle, inode, curp);
957 * ext4_ext_create_new_leaf:
958 * finds empty index and adds new leaf.
959 * if no free index is found, then it requests in-depth growing.
961 static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
962 struct ext4_ext_path *path,
963 struct ext4_extent *newext)
965 struct ext4_ext_path *curp;
966 int depth, i, err = 0;
969 i = depth = ext_depth(inode);
971 /* walk up to the tree and look for free index entry */
973 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
978 /* we use already allocated block for index block,
979 * so subsequent data blocks should be contiguous */
980 if (EXT_HAS_FREE_INDEX(curp)) {
981 /* if we found index with free entry, then use that
982 * entry: create all needed subtree and add new leaf */
983 err = ext4_ext_split(handle, inode, path, newext, i);
986 ext4_ext_drop_refs(path);
987 path = ext4_ext_find_extent(inode,
988 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
993 /* tree is full, time to grow in depth */
994 err = ext4_ext_grow_indepth(handle, inode, path, newext);
999 ext4_ext_drop_refs(path);
1000 path = ext4_ext_find_extent(inode,
1001 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1004 err = PTR_ERR(path);
1009 * only first (depth 0 -> 1) produces free space;
1010 * in all other cases we have to split the grown tree
1012 depth = ext_depth(inode);
1013 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
1014 /* now we need to split */
1024 * search the closest allocated block to the left for *logical
1025 * and returns it at @logical + it's physical address at @phys
1026 * if *logical is the smallest allocated block, the function
1027 * returns 0 at @phys
1028 * return value contains 0 (success) or error code
1031 ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path,
1032 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1034 struct ext4_extent_idx *ix;
1035 struct ext4_extent *ex;
1038 BUG_ON(path == NULL);
1039 depth = path->p_depth;
1042 if (depth == 0 && path->p_ext == NULL)
1045 /* usually extent in the path covers blocks smaller
1046 * then *logical, but it can be that extent is the
1047 * first one in the file */
1049 ex = path[depth].p_ext;
1050 ee_len = ext4_ext_get_actual_len(ex);
1051 if (*logical < le32_to_cpu(ex->ee_block)) {
1052 BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
1053 while (--depth >= 0) {
1054 ix = path[depth].p_idx;
1055 BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
1060 BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
1062 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
1063 *phys = ext_pblock(ex) + ee_len - 1;
1068 * search the closest allocated block to the right for *logical
1069 * and returns it at @logical + it's physical address at @phys
1070 * if *logical is the smallest allocated block, the function
1071 * returns 0 at @phys
1072 * return value contains 0 (success) or error code
1075 ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path,
1076 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1078 struct buffer_head *bh = NULL;
1079 struct ext4_extent_header *eh;
1080 struct ext4_extent_idx *ix;
1081 struct ext4_extent *ex;
1085 BUG_ON(path == NULL);
1086 depth = path->p_depth;
1089 if (depth == 0 && path->p_ext == NULL)
1092 /* usually extent in the path covers blocks smaller
1093 * then *logical, but it can be that extent is the
1094 * first one in the file */
1096 ex = path[depth].p_ext;
1097 ee_len = ext4_ext_get_actual_len(ex);
1098 if (*logical < le32_to_cpu(ex->ee_block)) {
1099 BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
1100 while (--depth >= 0) {
1101 ix = path[depth].p_idx;
1102 BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
1104 *logical = le32_to_cpu(ex->ee_block);
1105 *phys = ext_pblock(ex);
1109 BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
1111 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1112 /* next allocated block in this leaf */
1114 *logical = le32_to_cpu(ex->ee_block);
1115 *phys = ext_pblock(ex);
1119 /* go up and search for index to the right */
1120 while (--depth >= 0) {
1121 ix = path[depth].p_idx;
1122 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
1127 /* we've gone up to the root and
1128 * found no index to the right */
1132 /* we've found index to the right, let's
1133 * follow it and find the closest allocated
1134 * block to the right */
1136 block = idx_pblock(ix);
1137 while (++depth < path->p_depth) {
1138 bh = sb_bread(inode->i_sb, block);
1141 eh = ext_block_hdr(bh);
1142 if (ext4_ext_check_header(inode, eh, depth)) {
1146 ix = EXT_FIRST_INDEX(eh);
1147 block = idx_pblock(ix);
1151 bh = sb_bread(inode->i_sb, block);
1154 eh = ext_block_hdr(bh);
1155 if (ext4_ext_check_header(inode, eh, path->p_depth - depth)) {
1159 ex = EXT_FIRST_EXTENT(eh);
1160 *logical = le32_to_cpu(ex->ee_block);
1161 *phys = ext_pblock(ex);
1168 * ext4_ext_next_allocated_block:
1169 * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1170 * NOTE: it considers block number from index entry as
1171 * allocated block. Thus, index entries have to be consistent
1175 ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1179 BUG_ON(path == NULL);
1180 depth = path->p_depth;
1182 if (depth == 0 && path->p_ext == NULL)
1183 return EXT_MAX_BLOCK;
1185 while (depth >= 0) {
1186 if (depth == path->p_depth) {
1188 if (path[depth].p_ext !=
1189 EXT_LAST_EXTENT(path[depth].p_hdr))
1190 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1193 if (path[depth].p_idx !=
1194 EXT_LAST_INDEX(path[depth].p_hdr))
1195 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1200 return EXT_MAX_BLOCK;
1204 * ext4_ext_next_leaf_block:
1205 * returns first allocated block from next leaf or EXT_MAX_BLOCK
1207 static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode,
1208 struct ext4_ext_path *path)
1212 BUG_ON(path == NULL);
1213 depth = path->p_depth;
1215 /* zero-tree has no leaf blocks at all */
1217 return EXT_MAX_BLOCK;
1219 /* go to index block */
1222 while (depth >= 0) {
1223 if (path[depth].p_idx !=
1224 EXT_LAST_INDEX(path[depth].p_hdr))
1225 return (ext4_lblk_t)
1226 le32_to_cpu(path[depth].p_idx[1].ei_block);
1230 return EXT_MAX_BLOCK;
1234 * ext4_ext_correct_indexes:
1235 * if leaf gets modified and modified extent is first in the leaf,
1236 * then we have to correct all indexes above.
1237 * TODO: do we need to correct tree in all cases?
1239 static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
1240 struct ext4_ext_path *path)
1242 struct ext4_extent_header *eh;
1243 int depth = ext_depth(inode);
1244 struct ext4_extent *ex;
1248 eh = path[depth].p_hdr;
1249 ex = path[depth].p_ext;
1254 /* there is no tree at all */
1258 if (ex != EXT_FIRST_EXTENT(eh)) {
1259 /* we correct tree if first leaf got modified only */
1264 * TODO: we need correction if border is smaller than current one
1267 border = path[depth].p_ext->ee_block;
1268 err = ext4_ext_get_access(handle, inode, path + k);
1271 path[k].p_idx->ei_block = border;
1272 err = ext4_ext_dirty(handle, inode, path + k);
1277 /* change all left-side indexes */
1278 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1280 err = ext4_ext_get_access(handle, inode, path + k);
1283 path[k].p_idx->ei_block = border;
1284 err = ext4_ext_dirty(handle, inode, path + k);
1293 ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1294 struct ext4_extent *ex2)
1296 unsigned short ext1_ee_len, ext2_ee_len, max_len;
1299 * Make sure that either both extents are uninitialized, or
1302 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1305 if (ext4_ext_is_uninitialized(ex1))
1306 max_len = EXT_UNINIT_MAX_LEN;
1308 max_len = EXT_INIT_MAX_LEN;
1310 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1311 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1313 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
1314 le32_to_cpu(ex2->ee_block))
1318 * To allow future support for preallocated extents to be added
1319 * as an RO_COMPAT feature, refuse to merge to extents if
1320 * this can result in the top bit of ee_len being set.
1322 if (ext1_ee_len + ext2_ee_len > max_len)
1324 #ifdef AGGRESSIVE_TEST
1325 if (ext1_ee_len >= 4)
1329 if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2))
1335 * This function tries to merge the "ex" extent to the next extent in the tree.
1336 * It always tries to merge towards right. If you want to merge towards
1337 * left, pass "ex - 1" as argument instead of "ex".
1338 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1339 * 1 if they got merged.
1341 int ext4_ext_try_to_merge(struct inode *inode,
1342 struct ext4_ext_path *path,
1343 struct ext4_extent *ex)
1345 struct ext4_extent_header *eh;
1346 unsigned int depth, len;
1348 int uninitialized = 0;
1350 depth = ext_depth(inode);
1351 BUG_ON(path[depth].p_hdr == NULL);
1352 eh = path[depth].p_hdr;
1354 while (ex < EXT_LAST_EXTENT(eh)) {
1355 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1357 /* merge with next extent! */
1358 if (ext4_ext_is_uninitialized(ex))
1360 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1361 + ext4_ext_get_actual_len(ex + 1));
1363 ext4_ext_mark_uninitialized(ex);
1365 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1366 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1367 * sizeof(struct ext4_extent);
1368 memmove(ex + 1, ex + 2, len);
1370 le16_add_cpu(&eh->eh_entries, -1);
1372 WARN_ON(eh->eh_entries == 0);
1373 if (!eh->eh_entries)
1374 ext4_error(inode->i_sb, "ext4_ext_try_to_merge",
1375 "inode#%lu, eh->eh_entries = 0!", inode->i_ino);
1382 * check if a portion of the "newext" extent overlaps with an
1385 * If there is an overlap discovered, it updates the length of the newext
1386 * such that there will be no overlap, and then returns 1.
1387 * If there is no overlap found, it returns 0.
1389 unsigned int ext4_ext_check_overlap(struct inode *inode,
1390 struct ext4_extent *newext,
1391 struct ext4_ext_path *path)
1394 unsigned int depth, len1;
1395 unsigned int ret = 0;
1397 b1 = le32_to_cpu(newext->ee_block);
1398 len1 = ext4_ext_get_actual_len(newext);
1399 depth = ext_depth(inode);
1400 if (!path[depth].p_ext)
1402 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
1405 * get the next allocated block if the extent in the path
1406 * is before the requested block(s)
1409 b2 = ext4_ext_next_allocated_block(path);
1410 if (b2 == EXT_MAX_BLOCK)
1414 /* check for wrap through zero on extent logical start block*/
1415 if (b1 + len1 < b1) {
1416 len1 = EXT_MAX_BLOCK - b1;
1417 newext->ee_len = cpu_to_le16(len1);
1421 /* check for overlap */
1422 if (b1 + len1 > b2) {
1423 newext->ee_len = cpu_to_le16(b2 - b1);
1431 * ext4_ext_insert_extent:
1432 * tries to merge requsted extent into the existing extent or
1433 * inserts requested extent as new one into the tree,
1434 * creating new leaf in the no-space case.
1436 int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1437 struct ext4_ext_path *path,
1438 struct ext4_extent *newext)
1440 struct ext4_extent_header * eh;
1441 struct ext4_extent *ex, *fex;
1442 struct ext4_extent *nearex; /* nearest extent */
1443 struct ext4_ext_path *npath = NULL;
1444 int depth, len, err;
1446 unsigned uninitialized = 0;
1448 BUG_ON(ext4_ext_get_actual_len(newext) == 0);
1449 depth = ext_depth(inode);
1450 ex = path[depth].p_ext;
1451 BUG_ON(path[depth].p_hdr == NULL);
1453 /* try to insert block into found extent and return */
1454 if (ex && ext4_can_extents_be_merged(inode, ex, newext)) {
1455 ext_debug("append %d block to %d:%d (from %llu)\n",
1456 ext4_ext_get_actual_len(newext),
1457 le32_to_cpu(ex->ee_block),
1458 ext4_ext_get_actual_len(ex), ext_pblock(ex));
1459 err = ext4_ext_get_access(handle, inode, path + depth);
1464 * ext4_can_extents_be_merged should have checked that either
1465 * both extents are uninitialized, or both aren't. Thus we
1466 * need to check only one of them here.
1468 if (ext4_ext_is_uninitialized(ex))
1470 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1471 + ext4_ext_get_actual_len(newext));
1473 ext4_ext_mark_uninitialized(ex);
1474 eh = path[depth].p_hdr;
1480 depth = ext_depth(inode);
1481 eh = path[depth].p_hdr;
1482 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1485 /* probably next leaf has space for us? */
1486 fex = EXT_LAST_EXTENT(eh);
1487 next = ext4_ext_next_leaf_block(inode, path);
1488 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
1489 && next != EXT_MAX_BLOCK) {
1490 ext_debug("next leaf block - %d\n", next);
1491 BUG_ON(npath != NULL);
1492 npath = ext4_ext_find_extent(inode, next, NULL);
1494 return PTR_ERR(npath);
1495 BUG_ON(npath->p_depth != path->p_depth);
1496 eh = npath[depth].p_hdr;
1497 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
1498 ext_debug("next leaf isnt full(%d)\n",
1499 le16_to_cpu(eh->eh_entries));
1503 ext_debug("next leaf has no free space(%d,%d)\n",
1504 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1508 * There is no free space in the found leaf.
1509 * We're gonna add a new leaf in the tree.
1511 err = ext4_ext_create_new_leaf(handle, inode, path, newext);
1514 depth = ext_depth(inode);
1515 eh = path[depth].p_hdr;
1518 nearex = path[depth].p_ext;
1520 err = ext4_ext_get_access(handle, inode, path + depth);
1525 /* there is no extent in this leaf, create first one */
1526 ext_debug("first extent in the leaf: %d:%llu:%d\n",
1527 le32_to_cpu(newext->ee_block),
1529 ext4_ext_get_actual_len(newext));
1530 path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1531 } else if (le32_to_cpu(newext->ee_block)
1532 > le32_to_cpu(nearex->ee_block)) {
1533 /* BUG_ON(newext->ee_block == nearex->ee_block); */
1534 if (nearex != EXT_LAST_EXTENT(eh)) {
1535 len = EXT_MAX_EXTENT(eh) - nearex;
1536 len = (len - 1) * sizeof(struct ext4_extent);
1537 len = len < 0 ? 0 : len;
1538 ext_debug("insert %d:%llu:%d after: nearest 0x%p, "
1539 "move %d from 0x%p to 0x%p\n",
1540 le32_to_cpu(newext->ee_block),
1542 ext4_ext_get_actual_len(newext),
1543 nearex, len, nearex + 1, nearex + 2);
1544 memmove(nearex + 2, nearex + 1, len);
1546 path[depth].p_ext = nearex + 1;
1548 BUG_ON(newext->ee_block == nearex->ee_block);
1549 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1550 len = len < 0 ? 0 : len;
1551 ext_debug("insert %d:%llu:%d before: nearest 0x%p, "
1552 "move %d from 0x%p to 0x%p\n",
1553 le32_to_cpu(newext->ee_block),
1555 ext4_ext_get_actual_len(newext),
1556 nearex, len, nearex + 1, nearex + 2);
1557 memmove(nearex + 1, nearex, len);
1558 path[depth].p_ext = nearex;
1561 le16_add_cpu(&eh->eh_entries, 1);
1562 nearex = path[depth].p_ext;
1563 nearex->ee_block = newext->ee_block;
1564 ext4_ext_store_pblock(nearex, ext_pblock(newext));
1565 nearex->ee_len = newext->ee_len;
1568 /* try to merge extents to the right */
1569 ext4_ext_try_to_merge(inode, path, nearex);
1571 /* try to merge extents to the left */
1573 /* time to correct all indexes above */
1574 err = ext4_ext_correct_indexes(handle, inode, path);
1578 err = ext4_ext_dirty(handle, inode, path + depth);
1582 ext4_ext_drop_refs(npath);
1585 ext4_ext_tree_changed(inode);
1586 ext4_ext_invalidate_cache(inode);
1591 ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
1592 __u32 len, ext4_fsblk_t start, int type)
1594 struct ext4_ext_cache *cex;
1596 cex = &EXT4_I(inode)->i_cached_extent;
1597 cex->ec_type = type;
1598 cex->ec_block = block;
1600 cex->ec_start = start;
1604 * ext4_ext_put_gap_in_cache:
1605 * calculate boundaries of the gap that the requested block fits into
1606 * and cache this gap
1609 ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
1612 int depth = ext_depth(inode);
1615 struct ext4_extent *ex;
1617 ex = path[depth].p_ext;
1619 /* there is no extent yet, so gap is [0;-] */
1621 len = EXT_MAX_BLOCK;
1622 ext_debug("cache gap(whole file):");
1623 } else if (block < le32_to_cpu(ex->ee_block)) {
1625 len = le32_to_cpu(ex->ee_block) - block;
1626 ext_debug("cache gap(before): %u [%u:%u]",
1628 le32_to_cpu(ex->ee_block),
1629 ext4_ext_get_actual_len(ex));
1630 } else if (block >= le32_to_cpu(ex->ee_block)
1631 + ext4_ext_get_actual_len(ex)) {
1633 lblock = le32_to_cpu(ex->ee_block)
1634 + ext4_ext_get_actual_len(ex);
1636 next = ext4_ext_next_allocated_block(path);
1637 ext_debug("cache gap(after): [%u:%u] %u",
1638 le32_to_cpu(ex->ee_block),
1639 ext4_ext_get_actual_len(ex),
1641 BUG_ON(next == lblock);
1642 len = next - lblock;
1648 ext_debug(" -> %u:%lu\n", lblock, len);
1649 ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
1653 ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
1654 struct ext4_extent *ex)
1656 struct ext4_ext_cache *cex;
1658 cex = &EXT4_I(inode)->i_cached_extent;
1660 /* has cache valid data? */
1661 if (cex->ec_type == EXT4_EXT_CACHE_NO)
1662 return EXT4_EXT_CACHE_NO;
1664 BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
1665 cex->ec_type != EXT4_EXT_CACHE_EXTENT);
1666 if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
1667 ex->ee_block = cpu_to_le32(cex->ec_block);
1668 ext4_ext_store_pblock(ex, cex->ec_start);
1669 ex->ee_len = cpu_to_le16(cex->ec_len);
1670 ext_debug("%u cached by %u:%u:%llu\n",
1672 cex->ec_block, cex->ec_len, cex->ec_start);
1673 return cex->ec_type;
1677 return EXT4_EXT_CACHE_NO;
1682 * removes index from the index block.
1683 * It's used in truncate case only, thus all requests are for
1684 * last index in the block only.
1686 static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
1687 struct ext4_ext_path *path)
1689 struct buffer_head *bh;
1693 /* free index block */
1695 leaf = idx_pblock(path->p_idx);
1696 BUG_ON(path->p_hdr->eh_entries == 0);
1697 err = ext4_ext_get_access(handle, inode, path);
1700 le16_add_cpu(&path->p_hdr->eh_entries, -1);
1701 err = ext4_ext_dirty(handle, inode, path);
1704 ext_debug("index is empty, remove it, free block %llu\n", leaf);
1705 bh = sb_find_get_block(inode->i_sb, leaf);
1706 ext4_forget(handle, 1, inode, bh, leaf);
1707 ext4_free_blocks(handle, inode, leaf, 1, 1);
1712 * ext4_ext_calc_credits_for_insert:
1713 * This routine returns max. credits that the extent tree can consume.
1714 * It should be OK for low-performance paths like ->writepage()
1715 * To allow many writing processes to fit into a single transaction,
1716 * the caller should calculate credits under i_data_sem and
1717 * pass the actual path.
1719 int ext4_ext_calc_credits_for_insert(struct inode *inode,
1720 struct ext4_ext_path *path)
1725 /* probably there is space in leaf? */
1726 depth = ext_depth(inode);
1727 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
1728 < le16_to_cpu(path[depth].p_hdr->eh_max))
1733 * given 32-bit logical block (4294967296 blocks), max. tree
1734 * can be 4 levels in depth -- 4 * 340^4 == 53453440000.
1735 * Let's also add one more level for imbalance.
1739 /* allocation of new data block(s) */
1743 * tree can be full, so it would need to grow in depth:
1744 * we need one credit to modify old root, credits for
1745 * new root will be added in split accounting
1750 * Index split can happen, we would need:
1751 * allocate intermediate indexes (bitmap + group)
1752 * + change two blocks at each level, but root (already included)
1754 needed += (depth * 2) + (depth * 2);
1756 /* any allocation modifies superblock */
1762 static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
1763 struct ext4_extent *ex,
1764 ext4_lblk_t from, ext4_lblk_t to)
1766 struct buffer_head *bh;
1767 unsigned short ee_len = ext4_ext_get_actual_len(ex);
1768 int i, metadata = 0;
1770 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
1772 #ifdef EXTENTS_STATS
1774 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1775 spin_lock(&sbi->s_ext_stats_lock);
1776 sbi->s_ext_blocks += ee_len;
1777 sbi->s_ext_extents++;
1778 if (ee_len < sbi->s_ext_min)
1779 sbi->s_ext_min = ee_len;
1780 if (ee_len > sbi->s_ext_max)
1781 sbi->s_ext_max = ee_len;
1782 if (ext_depth(inode) > sbi->s_depth_max)
1783 sbi->s_depth_max = ext_depth(inode);
1784 spin_unlock(&sbi->s_ext_stats_lock);
1787 if (from >= le32_to_cpu(ex->ee_block)
1788 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
1793 num = le32_to_cpu(ex->ee_block) + ee_len - from;
1794 start = ext_pblock(ex) + ee_len - num;
1795 ext_debug("free last %u blocks starting %llu\n", num, start);
1796 for (i = 0; i < num; i++) {
1797 bh = sb_find_get_block(inode->i_sb, start + i);
1798 ext4_forget(handle, 0, inode, bh, start + i);
1800 ext4_free_blocks(handle, inode, start, num, metadata);
1801 } else if (from == le32_to_cpu(ex->ee_block)
1802 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
1803 printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n",
1804 from, to, le32_to_cpu(ex->ee_block), ee_len);
1806 printk(KERN_INFO "strange request: removal(2) "
1807 "%u-%u from %u:%u\n",
1808 from, to, le32_to_cpu(ex->ee_block), ee_len);
1814 ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
1815 struct ext4_ext_path *path, ext4_lblk_t start)
1817 int err = 0, correct_index = 0;
1818 int depth = ext_depth(inode), credits;
1819 struct ext4_extent_header *eh;
1820 ext4_lblk_t a, b, block;
1822 ext4_lblk_t ex_ee_block;
1823 unsigned short ex_ee_len;
1824 unsigned uninitialized = 0;
1825 struct ext4_extent *ex;
1827 /* the header must be checked already in ext4_ext_remove_space() */
1828 ext_debug("truncate since %u in leaf\n", start);
1829 if (!path[depth].p_hdr)
1830 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
1831 eh = path[depth].p_hdr;
1834 /* find where to start removing */
1835 ex = EXT_LAST_EXTENT(eh);
1837 ex_ee_block = le32_to_cpu(ex->ee_block);
1838 if (ext4_ext_is_uninitialized(ex))
1840 ex_ee_len = ext4_ext_get_actual_len(ex);
1842 while (ex >= EXT_FIRST_EXTENT(eh) &&
1843 ex_ee_block + ex_ee_len > start) {
1844 ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len);
1845 path[depth].p_ext = ex;
1847 a = ex_ee_block > start ? ex_ee_block : start;
1848 b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
1849 ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
1851 ext_debug(" border %u:%u\n", a, b);
1853 if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
1857 } else if (a != ex_ee_block) {
1858 /* remove tail of the extent */
1859 block = ex_ee_block;
1861 } else if (b != ex_ee_block + ex_ee_len - 1) {
1862 /* remove head of the extent */
1865 /* there is no "make a hole" API yet */
1868 /* remove whole extent: excellent! */
1869 block = ex_ee_block;
1871 BUG_ON(a != ex_ee_block);
1872 BUG_ON(b != ex_ee_block + ex_ee_len - 1);
1875 /* at present, extent can't cross block group: */
1876 /* leaf + bitmap + group desc + sb + inode */
1878 if (ex == EXT_FIRST_EXTENT(eh)) {
1880 credits += (ext_depth(inode)) + 1;
1883 credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
1886 handle = ext4_ext_journal_restart(handle, credits);
1887 if (IS_ERR(handle)) {
1888 err = PTR_ERR(handle);
1892 err = ext4_ext_get_access(handle, inode, path + depth);
1896 err = ext4_remove_blocks(handle, inode, ex, a, b);
1901 /* this extent is removed; mark slot entirely unused */
1902 ext4_ext_store_pblock(ex, 0);
1903 le16_add_cpu(&eh->eh_entries, -1);
1906 ex->ee_block = cpu_to_le32(block);
1907 ex->ee_len = cpu_to_le16(num);
1909 * Do not mark uninitialized if all the blocks in the
1910 * extent have been removed.
1912 if (uninitialized && num)
1913 ext4_ext_mark_uninitialized(ex);
1915 err = ext4_ext_dirty(handle, inode, path + depth);
1919 ext_debug("new extent: %u:%u:%llu\n", block, num,
1922 ex_ee_block = le32_to_cpu(ex->ee_block);
1923 ex_ee_len = ext4_ext_get_actual_len(ex);
1926 if (correct_index && eh->eh_entries)
1927 err = ext4_ext_correct_indexes(handle, inode, path);
1929 /* if this leaf is free, then we should
1930 * remove it from index block above */
1931 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
1932 err = ext4_ext_rm_idx(handle, inode, path + depth);
1939 * ext4_ext_more_to_rm:
1940 * returns 1 if current index has to be freed (even partial)
1943 ext4_ext_more_to_rm(struct ext4_ext_path *path)
1945 BUG_ON(path->p_idx == NULL);
1947 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
1951 * if truncate on deeper level happened, it wasn't partial,
1952 * so we have to consider current index for truncation
1954 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
1959 static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
1961 struct super_block *sb = inode->i_sb;
1962 int depth = ext_depth(inode);
1963 struct ext4_ext_path *path;
1967 ext_debug("truncate since %u\n", start);
1969 /* probably first extent we're gonna free will be last in block */
1970 handle = ext4_journal_start(inode, depth + 1);
1972 return PTR_ERR(handle);
1974 ext4_ext_invalidate_cache(inode);
1977 * We start scanning from right side, freeing all the blocks
1978 * after i_size and walking into the tree depth-wise.
1980 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
1982 ext4_journal_stop(handle);
1985 path[0].p_hdr = ext_inode_hdr(inode);
1986 if (ext4_ext_check_header(inode, path[0].p_hdr, depth)) {
1990 path[0].p_depth = depth;
1992 while (i >= 0 && err == 0) {
1994 /* this is leaf block */
1995 err = ext4_ext_rm_leaf(handle, inode, path, start);
1996 /* root level has p_bh == NULL, brelse() eats this */
1997 brelse(path[i].p_bh);
1998 path[i].p_bh = NULL;
2003 /* this is index block */
2004 if (!path[i].p_hdr) {
2005 ext_debug("initialize header\n");
2006 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
2009 if (!path[i].p_idx) {
2010 /* this level hasn't been touched yet */
2011 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2012 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2013 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2015 le16_to_cpu(path[i].p_hdr->eh_entries));
2017 /* we were already here, see at next index */
2021 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2022 i, EXT_FIRST_INDEX(path[i].p_hdr),
2024 if (ext4_ext_more_to_rm(path + i)) {
2025 struct buffer_head *bh;
2026 /* go to the next level */
2027 ext_debug("move to level %d (block %llu)\n",
2028 i + 1, idx_pblock(path[i].p_idx));
2029 memset(path + i + 1, 0, sizeof(*path));
2030 bh = sb_bread(sb, idx_pblock(path[i].p_idx));
2032 /* should we reset i_size? */
2036 if (WARN_ON(i + 1 > depth)) {
2040 if (ext4_ext_check_header(inode, ext_block_hdr(bh),
2045 path[i + 1].p_bh = bh;
2047 /* save actual number of indexes since this
2048 * number is changed at the next iteration */
2049 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2052 /* we finished processing this index, go up */
2053 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
2054 /* index is empty, remove it;
2055 * handle must be already prepared by the
2056 * truncatei_leaf() */
2057 err = ext4_ext_rm_idx(handle, inode, path + i);
2059 /* root level has p_bh == NULL, brelse() eats this */
2060 brelse(path[i].p_bh);
2061 path[i].p_bh = NULL;
2063 ext_debug("return to level %d\n", i);
2067 /* TODO: flexible tree reduction should be here */
2068 if (path->p_hdr->eh_entries == 0) {
2070 * truncate to zero freed all the tree,
2071 * so we need to correct eh_depth
2073 err = ext4_ext_get_access(handle, inode, path);
2075 ext_inode_hdr(inode)->eh_depth = 0;
2076 ext_inode_hdr(inode)->eh_max =
2077 cpu_to_le16(ext4_ext_space_root(inode));
2078 err = ext4_ext_dirty(handle, inode, path);
2082 ext4_ext_tree_changed(inode);
2083 ext4_ext_drop_refs(path);
2085 ext4_journal_stop(handle);
2091 * called at mount time
2093 void ext4_ext_init(struct super_block *sb)
2096 * possible initialization would be here
2099 if (test_opt(sb, EXTENTS)) {
2100 printk("EXT4-fs: file extents enabled");
2101 #ifdef AGGRESSIVE_TEST
2102 printk(", aggressive tests");
2104 #ifdef CHECK_BINSEARCH
2105 printk(", check binsearch");
2107 #ifdef EXTENTS_STATS
2111 #ifdef EXTENTS_STATS
2112 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2113 EXT4_SB(sb)->s_ext_min = 1 << 30;
2114 EXT4_SB(sb)->s_ext_max = 0;
2120 * called at umount time
2122 void ext4_ext_release(struct super_block *sb)
2124 if (!test_opt(sb, EXTENTS))
2127 #ifdef EXTENTS_STATS
2128 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2129 struct ext4_sb_info *sbi = EXT4_SB(sb);
2130 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2131 sbi->s_ext_blocks, sbi->s_ext_extents,
2132 sbi->s_ext_blocks / sbi->s_ext_extents);
2133 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2134 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2139 static void bi_complete(struct bio *bio, int error)
2141 complete((struct completion *)bio->bi_private);
2144 /* FIXME!! we need to try to merge to left or right after zero-out */
2145 static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2149 int blkbits, blocksize;
2151 struct completion event;
2152 unsigned int ee_len, len, done, offset;
2155 blkbits = inode->i_blkbits;
2156 blocksize = inode->i_sb->s_blocksize;
2157 ee_len = ext4_ext_get_actual_len(ex);
2158 ee_pblock = ext_pblock(ex);
2160 /* convert ee_pblock to 512 byte sectors */
2161 ee_pblock = ee_pblock << (blkbits - 9);
2163 while (ee_len > 0) {
2165 if (ee_len > BIO_MAX_PAGES)
2166 len = BIO_MAX_PAGES;
2170 bio = bio_alloc(GFP_NOIO, len);
2173 bio->bi_sector = ee_pblock;
2174 bio->bi_bdev = inode->i_sb->s_bdev;
2178 while (done < len) {
2179 ret = bio_add_page(bio, ZERO_PAGE(0),
2181 if (ret != blocksize) {
2183 * We can't add any more pages because of
2184 * hardware limitations. Start a new bio.
2189 offset += blocksize;
2190 if (offset >= PAGE_CACHE_SIZE)
2194 init_completion(&event);
2195 bio->bi_private = &event;
2196 bio->bi_end_io = bi_complete;
2197 submit_bio(WRITE, bio);
2198 wait_for_completion(&event);
2200 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
2208 ee_pblock += done << (blkbits - 9);
2213 #define EXT4_EXT_ZERO_LEN 7
2216 * This function is called by ext4_ext_get_blocks() if someone tries to write
2217 * to an uninitialized extent. It may result in splitting the uninitialized
2218 * extent into multiple extents (upto three - one initialized and two
2220 * There are three possibilities:
2221 * a> There is no split required: Entire extent should be initialized
2222 * b> Splits in two extents: Write is happening at either end of the extent
2223 * c> Splits in three extents: Somone is writing in middle of the extent
2225 static int ext4_ext_convert_to_initialized(handle_t *handle,
2226 struct inode *inode,
2227 struct ext4_ext_path *path,
2229 unsigned long max_blocks)
2231 struct ext4_extent *ex, newex, orig_ex;
2232 struct ext4_extent *ex1 = NULL;
2233 struct ext4_extent *ex2 = NULL;
2234 struct ext4_extent *ex3 = NULL;
2235 struct ext4_extent_header *eh;
2236 ext4_lblk_t ee_block;
2237 unsigned int allocated, ee_len, depth;
2238 ext4_fsblk_t newblock;
2242 depth = ext_depth(inode);
2243 eh = path[depth].p_hdr;
2244 ex = path[depth].p_ext;
2245 ee_block = le32_to_cpu(ex->ee_block);
2246 ee_len = ext4_ext_get_actual_len(ex);
2247 allocated = ee_len - (iblock - ee_block);
2248 newblock = iblock - ee_block + ext_pblock(ex);
2250 orig_ex.ee_block = ex->ee_block;
2251 orig_ex.ee_len = cpu_to_le16(ee_len);
2252 ext4_ext_store_pblock(&orig_ex, ext_pblock(ex));
2254 err = ext4_ext_get_access(handle, inode, path + depth);
2257 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
2258 if (ee_len <= 2*EXT4_EXT_ZERO_LEN) {
2259 err = ext4_ext_zeroout(inode, &orig_ex);
2261 goto fix_extent_len;
2262 /* update the extent length and mark as initialized */
2263 ex->ee_block = orig_ex.ee_block;
2264 ex->ee_len = orig_ex.ee_len;
2265 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2266 ext4_ext_dirty(handle, inode, path + depth);
2267 /* zeroed the full extent */
2271 /* ex1: ee_block to iblock - 1 : uninitialized */
2272 if (iblock > ee_block) {
2274 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2275 ext4_ext_mark_uninitialized(ex1);
2279 * for sanity, update the length of the ex2 extent before
2280 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2281 * overlap of blocks.
2283 if (!ex1 && allocated > max_blocks)
2284 ex2->ee_len = cpu_to_le16(max_blocks);
2285 /* ex3: to ee_block + ee_len : uninitialised */
2286 if (allocated > max_blocks) {
2287 unsigned int newdepth;
2288 /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */
2289 if (allocated <= EXT4_EXT_ZERO_LEN) {
2290 /* Mark first half uninitialized.
2291 * Mark second half initialized and zero out the
2292 * initialized extent
2294 ex->ee_block = orig_ex.ee_block;
2295 ex->ee_len = cpu_to_le16(ee_len - allocated);
2296 ext4_ext_mark_uninitialized(ex);
2297 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2298 ext4_ext_dirty(handle, inode, path + depth);
2301 ex3->ee_block = cpu_to_le32(iblock);
2302 ext4_ext_store_pblock(ex3, newblock);
2303 ex3->ee_len = cpu_to_le16(allocated);
2304 err = ext4_ext_insert_extent(handle, inode, path, ex3);
2305 if (err == -ENOSPC) {
2306 err = ext4_ext_zeroout(inode, &orig_ex);
2308 goto fix_extent_len;
2309 ex->ee_block = orig_ex.ee_block;
2310 ex->ee_len = orig_ex.ee_len;
2311 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2312 ext4_ext_dirty(handle, inode, path + depth);
2313 /* zeroed the full extent */
2317 goto fix_extent_len;
2320 * We need to zero out the second half because
2321 * an fallocate request can update file size and
2322 * converting the second half to initialized extent
2323 * implies that we can leak some junk data to user
2326 err = ext4_ext_zeroout(inode, ex3);
2329 * We should actually mark the
2330 * second half as uninit and return error
2331 * Insert would have changed the extent
2333 depth = ext_depth(inode);
2334 ext4_ext_drop_refs(path);
2335 path = ext4_ext_find_extent(inode,
2338 err = PTR_ERR(path);
2341 ex = path[depth].p_ext;
2342 err = ext4_ext_get_access(handle, inode,
2346 ext4_ext_mark_uninitialized(ex);
2347 ext4_ext_dirty(handle, inode, path + depth);
2351 /* zeroed the second half */
2355 ex3->ee_block = cpu_to_le32(iblock + max_blocks);
2356 ext4_ext_store_pblock(ex3, newblock + max_blocks);
2357 ex3->ee_len = cpu_to_le16(allocated - max_blocks);
2358 ext4_ext_mark_uninitialized(ex3);
2359 err = ext4_ext_insert_extent(handle, inode, path, ex3);
2360 if (err == -ENOSPC) {
2361 err = ext4_ext_zeroout(inode, &orig_ex);
2363 goto fix_extent_len;
2364 /* update the extent length and mark as initialized */
2365 ex->ee_block = orig_ex.ee_block;
2366 ex->ee_len = orig_ex.ee_len;
2367 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2368 ext4_ext_dirty(handle, inode, path + depth);
2369 /* zeroed the full extent */
2373 goto fix_extent_len;
2375 * The depth, and hence eh & ex might change
2376 * as part of the insert above.
2378 newdepth = ext_depth(inode);
2380 * update the extent length after successfull insert of the
2383 orig_ex.ee_len = cpu_to_le16(ee_len -
2384 ext4_ext_get_actual_len(ex3));
2385 if (newdepth != depth) {
2387 ext4_ext_drop_refs(path);
2388 path = ext4_ext_find_extent(inode, iblock, path);
2390 err = PTR_ERR(path);
2393 eh = path[depth].p_hdr;
2394 ex = path[depth].p_ext;
2398 err = ext4_ext_get_access(handle, inode, path + depth);
2402 allocated = max_blocks;
2404 /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying
2405 * to insert a extent in the middle zerout directly
2406 * otherwise give the extent a chance to merge to left
2408 if (le16_to_cpu(orig_ex.ee_len) <= EXT4_EXT_ZERO_LEN &&
2409 iblock != ee_block) {
2410 err = ext4_ext_zeroout(inode, &orig_ex);
2412 goto fix_extent_len;
2413 /* update the extent length and mark as initialized */
2414 ex->ee_block = orig_ex.ee_block;
2415 ex->ee_len = orig_ex.ee_len;
2416 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2417 ext4_ext_dirty(handle, inode, path + depth);
2418 /* zero out the first half */
2423 * If there was a change of depth as part of the
2424 * insertion of ex3 above, we need to update the length
2425 * of the ex1 extent again here
2427 if (ex1 && ex1 != ex) {
2429 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2430 ext4_ext_mark_uninitialized(ex1);
2433 /* ex2: iblock to iblock + maxblocks-1 : initialised */
2434 ex2->ee_block = cpu_to_le32(iblock);
2435 ext4_ext_store_pblock(ex2, newblock);
2436 ex2->ee_len = cpu_to_le16(allocated);
2440 * New (initialized) extent starts from the first block
2441 * in the current extent. i.e., ex2 == ex
2442 * We have to see if it can be merged with the extent
2445 if (ex2 > EXT_FIRST_EXTENT(eh)) {
2447 * To merge left, pass "ex2 - 1" to try_to_merge(),
2448 * since it merges towards right _only_.
2450 ret = ext4_ext_try_to_merge(inode, path, ex2 - 1);
2452 err = ext4_ext_correct_indexes(handle, inode, path);
2455 depth = ext_depth(inode);
2460 * Try to Merge towards right. This might be required
2461 * only when the whole extent is being written to.
2462 * i.e. ex2 == ex and ex3 == NULL.
2465 ret = ext4_ext_try_to_merge(inode, path, ex2);
2467 err = ext4_ext_correct_indexes(handle, inode, path);
2472 /* Mark modified extent as dirty */
2473 err = ext4_ext_dirty(handle, inode, path + depth);
2476 err = ext4_ext_insert_extent(handle, inode, path, &newex);
2477 if (err == -ENOSPC) {
2478 err = ext4_ext_zeroout(inode, &orig_ex);
2480 goto fix_extent_len;
2481 /* update the extent length and mark as initialized */
2482 ex->ee_block = orig_ex.ee_block;
2483 ex->ee_len = orig_ex.ee_len;
2484 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2485 ext4_ext_dirty(handle, inode, path + depth);
2486 /* zero out the first half */
2489 goto fix_extent_len;
2491 return err ? err : allocated;
2494 ex->ee_block = orig_ex.ee_block;
2495 ex->ee_len = orig_ex.ee_len;
2496 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2497 ext4_ext_mark_uninitialized(ex);
2498 ext4_ext_dirty(handle, inode, path + depth);
2503 * Block allocation/map/preallocation routine for extents based files
2506 * Need to be called with
2507 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
2508 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
2510 * return > 0, number of of blocks already mapped/allocated
2511 * if create == 0 and these are pre-allocated blocks
2512 * buffer head is unmapped
2513 * otherwise blocks are mapped
2515 * return = 0, if plain look up failed (blocks have not been allocated)
2516 * buffer head is unmapped
2518 * return < 0, error case.
2520 int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
2522 unsigned long max_blocks, struct buffer_head *bh_result,
2523 int create, int extend_disksize)
2525 struct ext4_ext_path *path = NULL;
2526 struct ext4_extent_header *eh;
2527 struct ext4_extent newex, *ex;
2528 ext4_fsblk_t goal, newblock;
2529 int err = 0, depth, ret;
2530 unsigned long allocated = 0;
2531 struct ext4_allocation_request ar;
2533 __clear_bit(BH_New, &bh_result->b_state);
2534 ext_debug("blocks %u/%lu requested for inode %u\n",
2535 iblock, max_blocks, inode->i_ino);
2537 /* check in cache */
2538 goal = ext4_ext_in_cache(inode, iblock, &newex);
2540 if (goal == EXT4_EXT_CACHE_GAP) {
2543 * block isn't allocated yet and
2544 * user doesn't want to allocate it
2548 /* we should allocate requested block */
2549 } else if (goal == EXT4_EXT_CACHE_EXTENT) {
2550 /* block is already allocated */
2552 - le32_to_cpu(newex.ee_block)
2553 + ext_pblock(&newex);
2554 /* number of remaining blocks in the extent */
2555 allocated = ext4_ext_get_actual_len(&newex) -
2556 (iblock - le32_to_cpu(newex.ee_block));
2563 /* find extent for this block */
2564 path = ext4_ext_find_extent(inode, iblock, NULL);
2566 err = PTR_ERR(path);
2571 depth = ext_depth(inode);
2574 * consistent leaf must not be empty;
2575 * this situation is possible, though, _during_ tree modification;
2576 * this is why assert can't be put in ext4_ext_find_extent()
2578 BUG_ON(path[depth].p_ext == NULL && depth != 0);
2579 eh = path[depth].p_hdr;
2581 ex = path[depth].p_ext;
2583 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
2584 ext4_fsblk_t ee_start = ext_pblock(ex);
2585 unsigned short ee_len;
2588 * Uninitialized extents are treated as holes, except that
2589 * we split out initialized portions during a write.
2591 ee_len = ext4_ext_get_actual_len(ex);
2592 /* if found extent covers block, simply return it */
2593 if (iblock >= ee_block && iblock < ee_block + ee_len) {
2594 newblock = iblock - ee_block + ee_start;
2595 /* number of remaining blocks in the extent */
2596 allocated = ee_len - (iblock - ee_block);
2597 ext_debug("%u fit into %lu:%d -> %llu\n", iblock,
2598 ee_block, ee_len, newblock);
2600 /* Do not put uninitialized extent in the cache */
2601 if (!ext4_ext_is_uninitialized(ex)) {
2602 ext4_ext_put_in_cache(inode, ee_block,
2604 EXT4_EXT_CACHE_EXTENT);
2607 if (create == EXT4_CREATE_UNINITIALIZED_EXT)
2611 * We have blocks reserved already. We
2612 * return allocated blocks so that delalloc
2613 * won't do block reservation for us. But
2614 * the buffer head will be unmapped so that
2615 * a read from the block returns 0s.
2617 if (allocated > max_blocks)
2618 allocated = max_blocks;
2619 /* mark the buffer unwritten */
2620 __set_bit(BH_Unwritten, &bh_result->b_state);
2624 ret = ext4_ext_convert_to_initialized(handle, inode,
2637 * requested block isn't allocated yet;
2638 * we couldn't try to create block if create flag is zero
2642 * put just found gap into cache to speed up
2643 * subsequent requests
2645 ext4_ext_put_gap_in_cache(inode, path, iblock);
2649 * Okay, we need to do block allocation. Lazily initialize the block
2650 * allocation info here if necessary.
2652 if (S_ISREG(inode->i_mode) && (!EXT4_I(inode)->i_block_alloc_info))
2653 ext4_init_block_alloc_info(inode);
2655 /* find neighbour allocated blocks */
2657 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
2661 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright);
2666 * See if request is beyond maximum number of blocks we can have in
2667 * a single extent. For an initialized extent this limit is
2668 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
2669 * EXT_UNINIT_MAX_LEN.
2671 if (max_blocks > EXT_INIT_MAX_LEN &&
2672 create != EXT4_CREATE_UNINITIALIZED_EXT)
2673 max_blocks = EXT_INIT_MAX_LEN;
2674 else if (max_blocks > EXT_UNINIT_MAX_LEN &&
2675 create == EXT4_CREATE_UNINITIALIZED_EXT)
2676 max_blocks = EXT_UNINIT_MAX_LEN;
2678 /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */
2679 newex.ee_block = cpu_to_le32(iblock);
2680 newex.ee_len = cpu_to_le16(max_blocks);
2681 err = ext4_ext_check_overlap(inode, &newex, path);
2683 allocated = ext4_ext_get_actual_len(&newex);
2685 allocated = max_blocks;
2687 /* allocate new block */
2689 ar.goal = ext4_ext_find_goal(inode, path, iblock);
2690 ar.logical = iblock;
2692 if (S_ISREG(inode->i_mode))
2693 ar.flags = EXT4_MB_HINT_DATA;
2695 /* disable in-core preallocation for non-regular files */
2697 newblock = ext4_mb_new_blocks(handle, &ar, &err);
2700 ext_debug("allocate new block: goal %llu, found %llu/%lu\n",
2701 goal, newblock, allocated);
2703 /* try to insert new extent into found leaf and return */
2704 ext4_ext_store_pblock(&newex, newblock);
2705 newex.ee_len = cpu_to_le16(ar.len);
2706 if (create == EXT4_CREATE_UNINITIALIZED_EXT) /* Mark uninitialized */
2707 ext4_ext_mark_uninitialized(&newex);
2708 err = ext4_ext_insert_extent(handle, inode, path, &newex);
2710 /* free data blocks we just allocated */
2711 /* not a good idea to call discard here directly,
2712 * but otherwise we'd need to call it every free() */
2713 ext4_mb_discard_inode_preallocations(inode);
2714 ext4_free_blocks(handle, inode, ext_pblock(&newex),
2715 ext4_ext_get_actual_len(&newex), 0);
2719 /* previous routine could use block we allocated */
2720 newblock = ext_pblock(&newex);
2721 allocated = ext4_ext_get_actual_len(&newex);
2723 if (extend_disksize && inode->i_size > EXT4_I(inode)->i_disksize)
2724 EXT4_I(inode)->i_disksize = inode->i_size;
2726 __set_bit(BH_New, &bh_result->b_state);
2728 /* Cache only when it is _not_ an uninitialized extent */
2729 if (create != EXT4_CREATE_UNINITIALIZED_EXT)
2730 ext4_ext_put_in_cache(inode, iblock, allocated, newblock,
2731 EXT4_EXT_CACHE_EXTENT);
2733 if (allocated > max_blocks)
2734 allocated = max_blocks;
2735 ext4_ext_show_leaf(inode, path);
2736 __set_bit(BH_Mapped, &bh_result->b_state);
2737 bh_result->b_bdev = inode->i_sb->s_bdev;
2738 bh_result->b_blocknr = newblock;
2741 ext4_ext_drop_refs(path);
2744 return err ? err : allocated;
2747 void ext4_ext_truncate(struct inode * inode, struct page *page)
2749 struct address_space *mapping = inode->i_mapping;
2750 struct super_block *sb = inode->i_sb;
2751 ext4_lblk_t last_block;
2756 * probably first extent we're gonna free will be last in block
2758 err = ext4_writepage_trans_blocks(inode) + 3;
2759 handle = ext4_journal_start(inode, err);
2760 if (IS_ERR(handle)) {
2762 clear_highpage(page);
2763 flush_dcache_page(page);
2765 page_cache_release(page);
2771 ext4_block_truncate_page(handle, page, mapping, inode->i_size);
2773 down_write(&EXT4_I(inode)->i_data_sem);
2774 ext4_ext_invalidate_cache(inode);
2776 ext4_mb_discard_inode_preallocations(inode);
2779 * TODO: optimization is possible here.
2780 * Probably we need not scan at all,
2781 * because page truncation is enough.
2783 if (ext4_orphan_add(handle, inode))
2786 /* we have to know where to truncate from in crash case */
2787 EXT4_I(inode)->i_disksize = inode->i_size;
2788 ext4_mark_inode_dirty(handle, inode);
2790 last_block = (inode->i_size + sb->s_blocksize - 1)
2791 >> EXT4_BLOCK_SIZE_BITS(sb);
2792 err = ext4_ext_remove_space(inode, last_block);
2794 /* In a multi-transaction truncate, we only make the final
2795 * transaction synchronous.
2802 * If this was a simple ftruncate() and the file will remain alive,
2803 * then we need to clear up the orphan record which we created above.
2804 * However, if this was a real unlink then we were called by
2805 * ext4_delete_inode(), and we allow that function to clean up the
2806 * orphan info for us.
2809 ext4_orphan_del(handle, inode);
2811 up_write(&EXT4_I(inode)->i_data_sem);
2812 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
2813 ext4_mark_inode_dirty(handle, inode);
2814 ext4_journal_stop(handle);
2818 * ext4_ext_writepage_trans_blocks:
2819 * calculate max number of blocks we could modify
2820 * in order to allocate new block for an inode
2822 int ext4_ext_writepage_trans_blocks(struct inode *inode, int num)
2826 needed = ext4_ext_calc_credits_for_insert(inode, NULL);
2828 /* caller wants to allocate num blocks, but note it includes sb */
2829 needed = needed * num - (num - 1);
2832 needed += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
2838 static void ext4_falloc_update_inode(struct inode *inode,
2839 int mode, loff_t new_size, int update_ctime)
2841 struct timespec now;
2844 now = current_fs_time(inode->i_sb);
2845 if (!timespec_equal(&inode->i_ctime, &now))
2846 inode->i_ctime = now;
2849 * Update only when preallocation was requested beyond
2852 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
2853 new_size > i_size_read(inode)) {
2854 i_size_write(inode, new_size);
2855 EXT4_I(inode)->i_disksize = new_size;
2861 * preallocate space for a file. This implements ext4's fallocate inode
2862 * operation, which gets called from sys_fallocate system call.
2863 * For block-mapped files, posix_fallocate should fall back to the method
2864 * of writing zeroes to the required new blocks (the same behavior which is
2865 * expected for file systems which do not support fallocate() system call).
2867 long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
2872 unsigned long max_blocks;
2876 struct buffer_head map_bh;
2877 unsigned int credits, blkbits = inode->i_blkbits;
2880 * currently supporting (pre)allocate mode for extent-based
2883 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
2886 /* preallocation to directories is currently not supported */
2887 if (S_ISDIR(inode->i_mode))
2890 block = offset >> blkbits;
2892 * We can't just convert len to max_blocks because
2893 * If blocksize = 4096 offset = 3072 and len = 2048
2895 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
2898 * credits to insert 1 extent into extent tree + buffers to be able to
2899 * modify 1 super block, 1 block bitmap and 1 group descriptor.
2901 credits = EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + 3;
2902 mutex_lock(&inode->i_mutex);
2904 while (ret >= 0 && ret < max_blocks) {
2905 block = block + ret;
2906 max_blocks = max_blocks - ret;
2907 handle = ext4_journal_start(inode, credits);
2908 if (IS_ERR(handle)) {
2909 ret = PTR_ERR(handle);
2912 ret = ext4_get_blocks_wrap(handle, inode, block,
2913 max_blocks, &map_bh,
2914 EXT4_CREATE_UNINITIALIZED_EXT, 0);
2918 printk(KERN_ERR "%s: ext4_ext_get_blocks "
2919 "returned error inode#%lu, block=%u, "
2920 "max_blocks=%lu", __func__,
2921 inode->i_ino, block, max_blocks);
2923 ext4_mark_inode_dirty(handle, inode);
2924 ret2 = ext4_journal_stop(handle);
2927 if ((block + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
2928 blkbits) >> blkbits))
2929 new_size = offset + len;
2931 new_size = (block + ret) << blkbits;
2933 ext4_falloc_update_inode(inode, mode, new_size,
2934 buffer_new(&map_bh));
2935 ext4_mark_inode_dirty(handle, inode);
2936 ret2 = ext4_journal_stop(handle);
2940 if (ret == -ENOSPC &&
2941 ext4_should_retry_alloc(inode->i_sb, &retries)) {
2945 mutex_unlock(&inode->i_mutex);
2946 return ret > 0 ? ret2 : ret;