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 int ext4_ext_journal_restart(handle_t *handle, int needed)
99 if (handle->h_buffer_credits > needed)
101 err = ext4_journal_extend(handle, needed);
104 return ext4_journal_restart(handle, needed);
112 static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
113 struct ext4_ext_path *path)
116 /* path points to block */
117 return ext4_journal_get_write_access(handle, path->p_bh);
119 /* path points to leaf/index in inode body */
120 /* we use in-core data, no need to protect them */
130 static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
131 struct ext4_ext_path *path)
135 /* path points to block */
136 err = ext4_journal_dirty_metadata(handle, path->p_bh);
138 /* path points to leaf/index in inode body */
139 err = ext4_mark_inode_dirty(handle, inode);
144 static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
145 struct ext4_ext_path *path,
148 struct ext4_inode_info *ei = EXT4_I(inode);
149 ext4_fsblk_t bg_start;
150 ext4_fsblk_t last_block;
151 ext4_grpblk_t colour;
155 struct ext4_extent *ex;
156 depth = path->p_depth;
158 /* try to predict block placement */
159 ex = path[depth].p_ext;
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 last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
174 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
175 colour = (current->pid % 16) *
176 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
178 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
179 return bg_start + colour + block;
183 * Allocation for a meta data block
186 ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
187 struct ext4_ext_path *path,
188 struct ext4_extent *ex, int *err)
190 ext4_fsblk_t goal, newblock;
192 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
193 newblock = ext4_new_meta_block(handle, inode, goal, err);
197 static int ext4_ext_space_block(struct inode *inode)
201 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
202 / sizeof(struct ext4_extent);
203 #ifdef AGGRESSIVE_TEST
210 static int ext4_ext_space_block_idx(struct inode *inode)
214 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
215 / sizeof(struct ext4_extent_idx);
216 #ifdef AGGRESSIVE_TEST
223 static int ext4_ext_space_root(struct inode *inode)
227 size = sizeof(EXT4_I(inode)->i_data);
228 size -= sizeof(struct ext4_extent_header);
229 size /= sizeof(struct ext4_extent);
230 #ifdef AGGRESSIVE_TEST
237 static int ext4_ext_space_root_idx(struct inode *inode)
241 size = sizeof(EXT4_I(inode)->i_data);
242 size -= sizeof(struct ext4_extent_header);
243 size /= sizeof(struct ext4_extent_idx);
244 #ifdef AGGRESSIVE_TEST
252 ext4_ext_max_entries(struct inode *inode, int depth)
256 if (depth == ext_depth(inode)) {
258 max = ext4_ext_space_root(inode);
260 max = ext4_ext_space_root_idx(inode);
263 max = ext4_ext_space_block(inode);
265 max = ext4_ext_space_block_idx(inode);
271 static int __ext4_ext_check_header(const char *function, struct inode *inode,
272 struct ext4_extent_header *eh,
275 const char *error_msg;
278 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
279 error_msg = "invalid magic";
282 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
283 error_msg = "unexpected eh_depth";
286 if (unlikely(eh->eh_max == 0)) {
287 error_msg = "invalid eh_max";
290 max = ext4_ext_max_entries(inode, depth);
291 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
292 error_msg = "too large eh_max";
295 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
296 error_msg = "invalid eh_entries";
302 ext4_error(inode->i_sb, function,
303 "bad header in inode #%lu: %s - magic %x, "
304 "entries %u, max %u(%u), depth %u(%u)",
305 inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic),
306 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
307 max, le16_to_cpu(eh->eh_depth), depth);
312 #define ext4_ext_check_header(inode, eh, depth) \
313 __ext4_ext_check_header(__func__, inode, eh, depth)
316 static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
318 int k, l = path->p_depth;
321 for (k = 0; k <= l; k++, path++) {
323 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
324 idx_pblock(path->p_idx));
325 } else if (path->p_ext) {
326 ext_debug(" %d:%d:%llu ",
327 le32_to_cpu(path->p_ext->ee_block),
328 ext4_ext_get_actual_len(path->p_ext),
329 ext_pblock(path->p_ext));
336 static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
338 int depth = ext_depth(inode);
339 struct ext4_extent_header *eh;
340 struct ext4_extent *ex;
346 eh = path[depth].p_hdr;
347 ex = EXT_FIRST_EXTENT(eh);
349 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
350 ext_debug("%d:%d:%llu ", le32_to_cpu(ex->ee_block),
351 ext4_ext_get_actual_len(ex), ext_pblock(ex));
356 #define ext4_ext_show_path(inode,path)
357 #define ext4_ext_show_leaf(inode,path)
360 void ext4_ext_drop_refs(struct ext4_ext_path *path)
362 int depth = path->p_depth;
365 for (i = 0; i <= depth; i++, path++)
373 * ext4_ext_binsearch_idx:
374 * binary search for the closest index of the given block
375 * the header must be checked before calling this
378 ext4_ext_binsearch_idx(struct inode *inode,
379 struct ext4_ext_path *path, ext4_lblk_t block)
381 struct ext4_extent_header *eh = path->p_hdr;
382 struct ext4_extent_idx *r, *l, *m;
385 ext_debug("binsearch for %u(idx): ", block);
387 l = EXT_FIRST_INDEX(eh) + 1;
388 r = EXT_LAST_INDEX(eh);
391 if (block < le32_to_cpu(m->ei_block))
395 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
396 m, le32_to_cpu(m->ei_block),
397 r, le32_to_cpu(r->ei_block));
401 ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
402 idx_pblock(path->p_idx));
404 #ifdef CHECK_BINSEARCH
406 struct ext4_extent_idx *chix, *ix;
409 chix = ix = EXT_FIRST_INDEX(eh);
410 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
412 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
413 printk("k=%d, ix=0x%p, first=0x%p\n", k,
414 ix, EXT_FIRST_INDEX(eh));
416 le32_to_cpu(ix->ei_block),
417 le32_to_cpu(ix[-1].ei_block));
419 BUG_ON(k && le32_to_cpu(ix->ei_block)
420 <= le32_to_cpu(ix[-1].ei_block));
421 if (block < le32_to_cpu(ix->ei_block))
425 BUG_ON(chix != path->p_idx);
432 * ext4_ext_binsearch:
433 * binary search for closest extent of the given block
434 * the header must be checked before calling this
437 ext4_ext_binsearch(struct inode *inode,
438 struct ext4_ext_path *path, ext4_lblk_t block)
440 struct ext4_extent_header *eh = path->p_hdr;
441 struct ext4_extent *r, *l, *m;
443 if (eh->eh_entries == 0) {
445 * this leaf is empty:
446 * we get such a leaf in split/add case
451 ext_debug("binsearch for %u: ", block);
453 l = EXT_FIRST_EXTENT(eh) + 1;
454 r = EXT_LAST_EXTENT(eh);
458 if (block < le32_to_cpu(m->ee_block))
462 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
463 m, le32_to_cpu(m->ee_block),
464 r, le32_to_cpu(r->ee_block));
468 ext_debug(" -> %d:%llu:%d ",
469 le32_to_cpu(path->p_ext->ee_block),
470 ext_pblock(path->p_ext),
471 ext4_ext_get_actual_len(path->p_ext));
473 #ifdef CHECK_BINSEARCH
475 struct ext4_extent *chex, *ex;
478 chex = ex = EXT_FIRST_EXTENT(eh);
479 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
480 BUG_ON(k && le32_to_cpu(ex->ee_block)
481 <= le32_to_cpu(ex[-1].ee_block));
482 if (block < le32_to_cpu(ex->ee_block))
486 BUG_ON(chex != path->p_ext);
492 int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
494 struct ext4_extent_header *eh;
496 eh = ext_inode_hdr(inode);
499 eh->eh_magic = EXT4_EXT_MAGIC;
500 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode));
501 ext4_mark_inode_dirty(handle, inode);
502 ext4_ext_invalidate_cache(inode);
506 struct ext4_ext_path *
507 ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
508 struct ext4_ext_path *path)
510 struct ext4_extent_header *eh;
511 struct buffer_head *bh;
512 short int depth, i, ppos = 0, alloc = 0;
514 eh = ext_inode_hdr(inode);
515 depth = ext_depth(inode);
516 if (ext4_ext_check_header(inode, eh, depth))
517 return ERR_PTR(-EIO);
520 /* account possible depth increase */
522 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
525 return ERR_PTR(-ENOMEM);
532 /* walk through the tree */
534 ext_debug("depth %d: num %d, max %d\n",
535 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
537 ext4_ext_binsearch_idx(inode, path + ppos, block);
538 path[ppos].p_block = idx_pblock(path[ppos].p_idx);
539 path[ppos].p_depth = i;
540 path[ppos].p_ext = NULL;
542 bh = sb_bread(inode->i_sb, path[ppos].p_block);
546 eh = ext_block_hdr(bh);
548 BUG_ON(ppos > depth);
549 path[ppos].p_bh = bh;
550 path[ppos].p_hdr = eh;
553 if (ext4_ext_check_header(inode, eh, i))
557 path[ppos].p_depth = i;
558 path[ppos].p_ext = NULL;
559 path[ppos].p_idx = NULL;
562 ext4_ext_binsearch(inode, path + ppos, block);
563 /* if not an empty leaf */
564 if (path[ppos].p_ext)
565 path[ppos].p_block = ext_pblock(path[ppos].p_ext);
567 ext4_ext_show_path(inode, path);
572 ext4_ext_drop_refs(path);
575 return ERR_PTR(-EIO);
579 * ext4_ext_insert_index:
580 * insert new index [@logical;@ptr] into the block at @curp;
581 * check where to insert: before @curp or after @curp
583 static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
584 struct ext4_ext_path *curp,
585 int logical, ext4_fsblk_t ptr)
587 struct ext4_extent_idx *ix;
590 err = ext4_ext_get_access(handle, inode, curp);
594 BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block));
595 len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
596 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
598 if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
599 len = (len - 1) * sizeof(struct ext4_extent_idx);
600 len = len < 0 ? 0 : len;
601 ext_debug("insert new index %d after: %llu. "
602 "move %d from 0x%p to 0x%p\n",
604 (curp->p_idx + 1), (curp->p_idx + 2));
605 memmove(curp->p_idx + 2, curp->p_idx + 1, len);
607 ix = curp->p_idx + 1;
610 len = len * sizeof(struct ext4_extent_idx);
611 len = len < 0 ? 0 : len;
612 ext_debug("insert new index %d before: %llu. "
613 "move %d from 0x%p to 0x%p\n",
615 curp->p_idx, (curp->p_idx + 1));
616 memmove(curp->p_idx + 1, curp->p_idx, len);
620 ix->ei_block = cpu_to_le32(logical);
621 ext4_idx_store_pblock(ix, ptr);
622 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
624 BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries)
625 > le16_to_cpu(curp->p_hdr->eh_max));
626 BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr));
628 err = ext4_ext_dirty(handle, inode, curp);
629 ext4_std_error(inode->i_sb, err);
636 * inserts new subtree into the path, using free index entry
638 * - allocates all needed blocks (new leaf and all intermediate index blocks)
639 * - makes decision where to split
640 * - moves remaining extents and index entries (right to the split point)
641 * into the newly allocated blocks
642 * - initializes subtree
644 static int ext4_ext_split(handle_t *handle, struct inode *inode,
645 struct ext4_ext_path *path,
646 struct ext4_extent *newext, int at)
648 struct buffer_head *bh = NULL;
649 int depth = ext_depth(inode);
650 struct ext4_extent_header *neh;
651 struct ext4_extent_idx *fidx;
652 struct ext4_extent *ex;
654 ext4_fsblk_t newblock, oldblock;
656 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
659 /* make decision: where to split? */
660 /* FIXME: now decision is simplest: at current extent */
662 /* if current leaf will be split, then we should use
663 * border from split point */
664 BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr));
665 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
666 border = path[depth].p_ext[1].ee_block;
667 ext_debug("leaf will be split."
668 " next leaf starts at %d\n",
669 le32_to_cpu(border));
671 border = newext->ee_block;
672 ext_debug("leaf will be added."
673 " next leaf starts at %d\n",
674 le32_to_cpu(border));
678 * If error occurs, then we break processing
679 * and mark filesystem read-only. index won't
680 * be inserted and tree will be in consistent
681 * state. Next mount will repair buffers too.
685 * Get array to track all allocated blocks.
686 * We need this to handle errors and free blocks
689 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
693 /* allocate all needed blocks */
694 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
695 for (a = 0; a < depth - at; a++) {
696 newblock = ext4_ext_new_meta_block(handle, inode, path,
700 ablocks[a] = newblock;
703 /* initialize new leaf */
704 newblock = ablocks[--a];
705 BUG_ON(newblock == 0);
706 bh = sb_getblk(inode->i_sb, newblock);
713 err = ext4_journal_get_create_access(handle, bh);
717 neh = ext_block_hdr(bh);
719 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
720 neh->eh_magic = EXT4_EXT_MAGIC;
722 ex = EXT_FIRST_EXTENT(neh);
724 /* move remainder of path[depth] to the new leaf */
725 BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max);
726 /* start copy from next extent */
727 /* TODO: we could do it by single memmove */
730 while (path[depth].p_ext <=
731 EXT_MAX_EXTENT(path[depth].p_hdr)) {
732 ext_debug("move %d:%llu:%d in new leaf %llu\n",
733 le32_to_cpu(path[depth].p_ext->ee_block),
734 ext_pblock(path[depth].p_ext),
735 ext4_ext_get_actual_len(path[depth].p_ext),
737 /*memmove(ex++, path[depth].p_ext++,
738 sizeof(struct ext4_extent));
744 memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
745 le16_add_cpu(&neh->eh_entries, m);
748 set_buffer_uptodate(bh);
751 err = ext4_journal_dirty_metadata(handle, bh);
757 /* correct old leaf */
759 err = ext4_ext_get_access(handle, inode, path + depth);
762 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
763 err = ext4_ext_dirty(handle, inode, path + depth);
769 /* create intermediate indexes */
773 ext_debug("create %d intermediate indices\n", k);
774 /* insert new index into current index block */
775 /* current depth stored in i var */
779 newblock = ablocks[--a];
780 bh = sb_getblk(inode->i_sb, newblock);
787 err = ext4_journal_get_create_access(handle, bh);
791 neh = ext_block_hdr(bh);
792 neh->eh_entries = cpu_to_le16(1);
793 neh->eh_magic = EXT4_EXT_MAGIC;
794 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
795 neh->eh_depth = cpu_to_le16(depth - i);
796 fidx = EXT_FIRST_INDEX(neh);
797 fidx->ei_block = border;
798 ext4_idx_store_pblock(fidx, oldblock);
800 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
801 i, newblock, le32_to_cpu(border), oldblock);
806 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
807 EXT_MAX_INDEX(path[i].p_hdr));
808 BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) !=
809 EXT_LAST_INDEX(path[i].p_hdr));
810 while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
811 ext_debug("%d: move %d:%llu in new index %llu\n", i,
812 le32_to_cpu(path[i].p_idx->ei_block),
813 idx_pblock(path[i].p_idx),
815 /*memmove(++fidx, path[i].p_idx++,
816 sizeof(struct ext4_extent_idx));
818 BUG_ON(neh->eh_entries > neh->eh_max);*/
823 memmove(++fidx, path[i].p_idx - m,
824 sizeof(struct ext4_extent_idx) * m);
825 le16_add_cpu(&neh->eh_entries, m);
827 set_buffer_uptodate(bh);
830 err = ext4_journal_dirty_metadata(handle, bh);
836 /* correct old index */
838 err = ext4_ext_get_access(handle, inode, path + i);
841 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
842 err = ext4_ext_dirty(handle, inode, path + i);
850 /* insert new index */
851 err = ext4_ext_insert_index(handle, inode, path + at,
852 le32_to_cpu(border), newblock);
856 if (buffer_locked(bh))
862 /* free all allocated blocks in error case */
863 for (i = 0; i < depth; i++) {
866 ext4_free_blocks(handle, inode, ablocks[i], 1, 1);
875 * ext4_ext_grow_indepth:
876 * implements tree growing procedure:
877 * - allocates new block
878 * - moves top-level data (index block or leaf) into the new block
879 * - initializes new top-level, creating index that points to the
882 static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
883 struct ext4_ext_path *path,
884 struct ext4_extent *newext)
886 struct ext4_ext_path *curp = path;
887 struct ext4_extent_header *neh;
888 struct ext4_extent_idx *fidx;
889 struct buffer_head *bh;
890 ext4_fsblk_t newblock;
893 newblock = ext4_ext_new_meta_block(handle, inode, path, newext, &err);
897 bh = sb_getblk(inode->i_sb, newblock);
900 ext4_std_error(inode->i_sb, err);
905 err = ext4_journal_get_create_access(handle, bh);
911 /* move top-level index/leaf into new block */
912 memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
914 /* set size of new block */
915 neh = ext_block_hdr(bh);
916 /* old root could have indexes or leaves
917 * so calculate e_max right way */
918 if (ext_depth(inode))
919 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
921 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
922 neh->eh_magic = EXT4_EXT_MAGIC;
923 set_buffer_uptodate(bh);
926 err = ext4_journal_dirty_metadata(handle, bh);
930 /* create index in new top-level index: num,max,pointer */
931 err = ext4_ext_get_access(handle, inode, curp);
935 curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
936 curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode));
937 curp->p_hdr->eh_entries = cpu_to_le16(1);
938 curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
940 if (path[0].p_hdr->eh_depth)
941 curp->p_idx->ei_block =
942 EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
944 curp->p_idx->ei_block =
945 EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
946 ext4_idx_store_pblock(curp->p_idx, newblock);
948 neh = ext_inode_hdr(inode);
949 fidx = EXT_FIRST_INDEX(neh);
950 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
951 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
952 le32_to_cpu(fidx->ei_block), idx_pblock(fidx));
954 neh->eh_depth = cpu_to_le16(path->p_depth + 1);
955 err = ext4_ext_dirty(handle, inode, curp);
963 * ext4_ext_create_new_leaf:
964 * finds empty index and adds new leaf.
965 * if no free index is found, then it requests in-depth growing.
967 static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
968 struct ext4_ext_path *path,
969 struct ext4_extent *newext)
971 struct ext4_ext_path *curp;
972 int depth, i, err = 0;
975 i = depth = ext_depth(inode);
977 /* walk up to the tree and look for free index entry */
979 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
984 /* we use already allocated block for index block,
985 * so subsequent data blocks should be contiguous */
986 if (EXT_HAS_FREE_INDEX(curp)) {
987 /* if we found index with free entry, then use that
988 * entry: create all needed subtree and add new leaf */
989 err = ext4_ext_split(handle, inode, path, newext, i);
994 ext4_ext_drop_refs(path);
995 path = ext4_ext_find_extent(inode,
996 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1001 /* tree is full, time to grow in depth */
1002 err = ext4_ext_grow_indepth(handle, inode, path, newext);
1007 ext4_ext_drop_refs(path);
1008 path = ext4_ext_find_extent(inode,
1009 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1012 err = PTR_ERR(path);
1017 * only first (depth 0 -> 1) produces free space;
1018 * in all other cases we have to split the grown tree
1020 depth = ext_depth(inode);
1021 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
1022 /* now we need to split */
1032 * search the closest allocated block to the left for *logical
1033 * and returns it at @logical + it's physical address at @phys
1034 * if *logical is the smallest allocated block, the function
1035 * returns 0 at @phys
1036 * return value contains 0 (success) or error code
1039 ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path,
1040 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1042 struct ext4_extent_idx *ix;
1043 struct ext4_extent *ex;
1046 BUG_ON(path == NULL);
1047 depth = path->p_depth;
1050 if (depth == 0 && path->p_ext == NULL)
1053 /* usually extent in the path covers blocks smaller
1054 * then *logical, but it can be that extent is the
1055 * first one in the file */
1057 ex = path[depth].p_ext;
1058 ee_len = ext4_ext_get_actual_len(ex);
1059 if (*logical < le32_to_cpu(ex->ee_block)) {
1060 BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
1061 while (--depth >= 0) {
1062 ix = path[depth].p_idx;
1063 BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
1068 BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
1070 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
1071 *phys = ext_pblock(ex) + ee_len - 1;
1076 * search the closest allocated block to the right for *logical
1077 * and returns it at @logical + it's physical address at @phys
1078 * if *logical is the smallest allocated block, the function
1079 * returns 0 at @phys
1080 * return value contains 0 (success) or error code
1083 ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path,
1084 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1086 struct buffer_head *bh = NULL;
1087 struct ext4_extent_header *eh;
1088 struct ext4_extent_idx *ix;
1089 struct ext4_extent *ex;
1093 BUG_ON(path == NULL);
1094 depth = path->p_depth;
1097 if (depth == 0 && path->p_ext == NULL)
1100 /* usually extent in the path covers blocks smaller
1101 * then *logical, but it can be that extent is the
1102 * first one in the file */
1104 ex = path[depth].p_ext;
1105 ee_len = ext4_ext_get_actual_len(ex);
1106 if (*logical < le32_to_cpu(ex->ee_block)) {
1107 BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
1108 while (--depth >= 0) {
1109 ix = path[depth].p_idx;
1110 BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
1112 *logical = le32_to_cpu(ex->ee_block);
1113 *phys = ext_pblock(ex);
1117 BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
1119 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1120 /* next allocated block in this leaf */
1122 *logical = le32_to_cpu(ex->ee_block);
1123 *phys = ext_pblock(ex);
1127 /* go up and search for index to the right */
1128 while (--depth >= 0) {
1129 ix = path[depth].p_idx;
1130 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
1135 /* we've gone up to the root and
1136 * found no index to the right */
1140 /* we've found index to the right, let's
1141 * follow it and find the closest allocated
1142 * block to the right */
1144 block = idx_pblock(ix);
1145 while (++depth < path->p_depth) {
1146 bh = sb_bread(inode->i_sb, block);
1149 eh = ext_block_hdr(bh);
1150 if (ext4_ext_check_header(inode, eh, depth)) {
1154 ix = EXT_FIRST_INDEX(eh);
1155 block = idx_pblock(ix);
1159 bh = sb_bread(inode->i_sb, block);
1162 eh = ext_block_hdr(bh);
1163 if (ext4_ext_check_header(inode, eh, path->p_depth - depth)) {
1167 ex = EXT_FIRST_EXTENT(eh);
1168 *logical = le32_to_cpu(ex->ee_block);
1169 *phys = ext_pblock(ex);
1176 * ext4_ext_next_allocated_block:
1177 * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1178 * NOTE: it considers block number from index entry as
1179 * allocated block. Thus, index entries have to be consistent
1183 ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1187 BUG_ON(path == NULL);
1188 depth = path->p_depth;
1190 if (depth == 0 && path->p_ext == NULL)
1191 return EXT_MAX_BLOCK;
1193 while (depth >= 0) {
1194 if (depth == path->p_depth) {
1196 if (path[depth].p_ext !=
1197 EXT_LAST_EXTENT(path[depth].p_hdr))
1198 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1201 if (path[depth].p_idx !=
1202 EXT_LAST_INDEX(path[depth].p_hdr))
1203 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1208 return EXT_MAX_BLOCK;
1212 * ext4_ext_next_leaf_block:
1213 * returns first allocated block from next leaf or EXT_MAX_BLOCK
1215 static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode,
1216 struct ext4_ext_path *path)
1220 BUG_ON(path == NULL);
1221 depth = path->p_depth;
1223 /* zero-tree has no leaf blocks at all */
1225 return EXT_MAX_BLOCK;
1227 /* go to index block */
1230 while (depth >= 0) {
1231 if (path[depth].p_idx !=
1232 EXT_LAST_INDEX(path[depth].p_hdr))
1233 return (ext4_lblk_t)
1234 le32_to_cpu(path[depth].p_idx[1].ei_block);
1238 return EXT_MAX_BLOCK;
1242 * ext4_ext_correct_indexes:
1243 * if leaf gets modified and modified extent is first in the leaf,
1244 * then we have to correct all indexes above.
1245 * TODO: do we need to correct tree in all cases?
1247 static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
1248 struct ext4_ext_path *path)
1250 struct ext4_extent_header *eh;
1251 int depth = ext_depth(inode);
1252 struct ext4_extent *ex;
1256 eh = path[depth].p_hdr;
1257 ex = path[depth].p_ext;
1262 /* there is no tree at all */
1266 if (ex != EXT_FIRST_EXTENT(eh)) {
1267 /* we correct tree if first leaf got modified only */
1272 * TODO: we need correction if border is smaller than current one
1275 border = path[depth].p_ext->ee_block;
1276 err = ext4_ext_get_access(handle, inode, path + k);
1279 path[k].p_idx->ei_block = border;
1280 err = ext4_ext_dirty(handle, inode, path + k);
1285 /* change all left-side indexes */
1286 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1288 err = ext4_ext_get_access(handle, inode, path + k);
1291 path[k].p_idx->ei_block = border;
1292 err = ext4_ext_dirty(handle, inode, path + k);
1301 ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1302 struct ext4_extent *ex2)
1304 unsigned short ext1_ee_len, ext2_ee_len, max_len;
1307 * Make sure that either both extents are uninitialized, or
1310 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1313 if (ext4_ext_is_uninitialized(ex1))
1314 max_len = EXT_UNINIT_MAX_LEN;
1316 max_len = EXT_INIT_MAX_LEN;
1318 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1319 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1321 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
1322 le32_to_cpu(ex2->ee_block))
1326 * To allow future support for preallocated extents to be added
1327 * as an RO_COMPAT feature, refuse to merge to extents if
1328 * this can result in the top bit of ee_len being set.
1330 if (ext1_ee_len + ext2_ee_len > max_len)
1332 #ifdef AGGRESSIVE_TEST
1333 if (ext1_ee_len >= 4)
1337 if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2))
1343 * This function tries to merge the "ex" extent to the next extent in the tree.
1344 * It always tries to merge towards right. If you want to merge towards
1345 * left, pass "ex - 1" as argument instead of "ex".
1346 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1347 * 1 if they got merged.
1349 int ext4_ext_try_to_merge(struct inode *inode,
1350 struct ext4_ext_path *path,
1351 struct ext4_extent *ex)
1353 struct ext4_extent_header *eh;
1354 unsigned int depth, len;
1356 int uninitialized = 0;
1358 depth = ext_depth(inode);
1359 BUG_ON(path[depth].p_hdr == NULL);
1360 eh = path[depth].p_hdr;
1362 while (ex < EXT_LAST_EXTENT(eh)) {
1363 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1365 /* merge with next extent! */
1366 if (ext4_ext_is_uninitialized(ex))
1368 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1369 + ext4_ext_get_actual_len(ex + 1));
1371 ext4_ext_mark_uninitialized(ex);
1373 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1374 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1375 * sizeof(struct ext4_extent);
1376 memmove(ex + 1, ex + 2, len);
1378 le16_add_cpu(&eh->eh_entries, -1);
1380 WARN_ON(eh->eh_entries == 0);
1381 if (!eh->eh_entries)
1382 ext4_error(inode->i_sb, "ext4_ext_try_to_merge",
1383 "inode#%lu, eh->eh_entries = 0!", inode->i_ino);
1390 * check if a portion of the "newext" extent overlaps with an
1393 * If there is an overlap discovered, it updates the length of the newext
1394 * such that there will be no overlap, and then returns 1.
1395 * If there is no overlap found, it returns 0.
1397 unsigned int ext4_ext_check_overlap(struct inode *inode,
1398 struct ext4_extent *newext,
1399 struct ext4_ext_path *path)
1402 unsigned int depth, len1;
1403 unsigned int ret = 0;
1405 b1 = le32_to_cpu(newext->ee_block);
1406 len1 = ext4_ext_get_actual_len(newext);
1407 depth = ext_depth(inode);
1408 if (!path[depth].p_ext)
1410 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
1413 * get the next allocated block if the extent in the path
1414 * is before the requested block(s)
1417 b2 = ext4_ext_next_allocated_block(path);
1418 if (b2 == EXT_MAX_BLOCK)
1422 /* check for wrap through zero on extent logical start block*/
1423 if (b1 + len1 < b1) {
1424 len1 = EXT_MAX_BLOCK - b1;
1425 newext->ee_len = cpu_to_le16(len1);
1429 /* check for overlap */
1430 if (b1 + len1 > b2) {
1431 newext->ee_len = cpu_to_le16(b2 - b1);
1439 * ext4_ext_insert_extent:
1440 * tries to merge requsted extent into the existing extent or
1441 * inserts requested extent as new one into the tree,
1442 * creating new leaf in the no-space case.
1444 int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1445 struct ext4_ext_path *path,
1446 struct ext4_extent *newext)
1448 struct ext4_extent_header * eh;
1449 struct ext4_extent *ex, *fex;
1450 struct ext4_extent *nearex; /* nearest extent */
1451 struct ext4_ext_path *npath = NULL;
1452 int depth, len, err;
1454 unsigned uninitialized = 0;
1456 BUG_ON(ext4_ext_get_actual_len(newext) == 0);
1457 depth = ext_depth(inode);
1458 ex = path[depth].p_ext;
1459 BUG_ON(path[depth].p_hdr == NULL);
1461 /* try to insert block into found extent and return */
1462 if (ex && ext4_can_extents_be_merged(inode, ex, newext)) {
1463 ext_debug("append %d block to %d:%d (from %llu)\n",
1464 ext4_ext_get_actual_len(newext),
1465 le32_to_cpu(ex->ee_block),
1466 ext4_ext_get_actual_len(ex), ext_pblock(ex));
1467 err = ext4_ext_get_access(handle, inode, path + depth);
1472 * ext4_can_extents_be_merged should have checked that either
1473 * both extents are uninitialized, or both aren't. Thus we
1474 * need to check only one of them here.
1476 if (ext4_ext_is_uninitialized(ex))
1478 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1479 + ext4_ext_get_actual_len(newext));
1481 ext4_ext_mark_uninitialized(ex);
1482 eh = path[depth].p_hdr;
1488 depth = ext_depth(inode);
1489 eh = path[depth].p_hdr;
1490 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1493 /* probably next leaf has space for us? */
1494 fex = EXT_LAST_EXTENT(eh);
1495 next = ext4_ext_next_leaf_block(inode, path);
1496 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
1497 && next != EXT_MAX_BLOCK) {
1498 ext_debug("next leaf block - %d\n", next);
1499 BUG_ON(npath != NULL);
1500 npath = ext4_ext_find_extent(inode, next, NULL);
1502 return PTR_ERR(npath);
1503 BUG_ON(npath->p_depth != path->p_depth);
1504 eh = npath[depth].p_hdr;
1505 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
1506 ext_debug("next leaf isnt full(%d)\n",
1507 le16_to_cpu(eh->eh_entries));
1511 ext_debug("next leaf has no free space(%d,%d)\n",
1512 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1516 * There is no free space in the found leaf.
1517 * We're gonna add a new leaf in the tree.
1519 err = ext4_ext_create_new_leaf(handle, inode, path, newext);
1522 depth = ext_depth(inode);
1523 eh = path[depth].p_hdr;
1526 nearex = path[depth].p_ext;
1528 err = ext4_ext_get_access(handle, inode, path + depth);
1533 /* there is no extent in this leaf, create first one */
1534 ext_debug("first extent in the leaf: %d:%llu:%d\n",
1535 le32_to_cpu(newext->ee_block),
1537 ext4_ext_get_actual_len(newext));
1538 path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1539 } else if (le32_to_cpu(newext->ee_block)
1540 > le32_to_cpu(nearex->ee_block)) {
1541 /* BUG_ON(newext->ee_block == nearex->ee_block); */
1542 if (nearex != EXT_LAST_EXTENT(eh)) {
1543 len = EXT_MAX_EXTENT(eh) - nearex;
1544 len = (len - 1) * sizeof(struct ext4_extent);
1545 len = len < 0 ? 0 : len;
1546 ext_debug("insert %d:%llu:%d after: nearest 0x%p, "
1547 "move %d from 0x%p to 0x%p\n",
1548 le32_to_cpu(newext->ee_block),
1550 ext4_ext_get_actual_len(newext),
1551 nearex, len, nearex + 1, nearex + 2);
1552 memmove(nearex + 2, nearex + 1, len);
1554 path[depth].p_ext = nearex + 1;
1556 BUG_ON(newext->ee_block == nearex->ee_block);
1557 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1558 len = len < 0 ? 0 : len;
1559 ext_debug("insert %d:%llu:%d before: nearest 0x%p, "
1560 "move %d from 0x%p to 0x%p\n",
1561 le32_to_cpu(newext->ee_block),
1563 ext4_ext_get_actual_len(newext),
1564 nearex, len, nearex + 1, nearex + 2);
1565 memmove(nearex + 1, nearex, len);
1566 path[depth].p_ext = nearex;
1569 le16_add_cpu(&eh->eh_entries, 1);
1570 nearex = path[depth].p_ext;
1571 nearex->ee_block = newext->ee_block;
1572 ext4_ext_store_pblock(nearex, ext_pblock(newext));
1573 nearex->ee_len = newext->ee_len;
1576 /* try to merge extents to the right */
1577 ext4_ext_try_to_merge(inode, path, nearex);
1579 /* try to merge extents to the left */
1581 /* time to correct all indexes above */
1582 err = ext4_ext_correct_indexes(handle, inode, path);
1586 err = ext4_ext_dirty(handle, inode, path + depth);
1590 ext4_ext_drop_refs(npath);
1593 ext4_ext_tree_changed(inode);
1594 ext4_ext_invalidate_cache(inode);
1599 ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
1600 __u32 len, ext4_fsblk_t start, int type)
1602 struct ext4_ext_cache *cex;
1604 cex = &EXT4_I(inode)->i_cached_extent;
1605 cex->ec_type = type;
1606 cex->ec_block = block;
1608 cex->ec_start = start;
1612 * ext4_ext_put_gap_in_cache:
1613 * calculate boundaries of the gap that the requested block fits into
1614 * and cache this gap
1617 ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
1620 int depth = ext_depth(inode);
1623 struct ext4_extent *ex;
1625 ex = path[depth].p_ext;
1627 /* there is no extent yet, so gap is [0;-] */
1629 len = EXT_MAX_BLOCK;
1630 ext_debug("cache gap(whole file):");
1631 } else if (block < le32_to_cpu(ex->ee_block)) {
1633 len = le32_to_cpu(ex->ee_block) - block;
1634 ext_debug("cache gap(before): %u [%u:%u]",
1636 le32_to_cpu(ex->ee_block),
1637 ext4_ext_get_actual_len(ex));
1638 } else if (block >= le32_to_cpu(ex->ee_block)
1639 + ext4_ext_get_actual_len(ex)) {
1641 lblock = le32_to_cpu(ex->ee_block)
1642 + ext4_ext_get_actual_len(ex);
1644 next = ext4_ext_next_allocated_block(path);
1645 ext_debug("cache gap(after): [%u:%u] %u",
1646 le32_to_cpu(ex->ee_block),
1647 ext4_ext_get_actual_len(ex),
1649 BUG_ON(next == lblock);
1650 len = next - lblock;
1656 ext_debug(" -> %u:%lu\n", lblock, len);
1657 ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
1661 ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
1662 struct ext4_extent *ex)
1664 struct ext4_ext_cache *cex;
1666 cex = &EXT4_I(inode)->i_cached_extent;
1668 /* has cache valid data? */
1669 if (cex->ec_type == EXT4_EXT_CACHE_NO)
1670 return EXT4_EXT_CACHE_NO;
1672 BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
1673 cex->ec_type != EXT4_EXT_CACHE_EXTENT);
1674 if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
1675 ex->ee_block = cpu_to_le32(cex->ec_block);
1676 ext4_ext_store_pblock(ex, cex->ec_start);
1677 ex->ee_len = cpu_to_le16(cex->ec_len);
1678 ext_debug("%u cached by %u:%u:%llu\n",
1680 cex->ec_block, cex->ec_len, cex->ec_start);
1681 return cex->ec_type;
1685 return EXT4_EXT_CACHE_NO;
1690 * removes index from the index block.
1691 * It's used in truncate case only, thus all requests are for
1692 * last index in the block only.
1694 static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
1695 struct ext4_ext_path *path)
1697 struct buffer_head *bh;
1701 /* free index block */
1703 leaf = idx_pblock(path->p_idx);
1704 BUG_ON(path->p_hdr->eh_entries == 0);
1705 err = ext4_ext_get_access(handle, inode, path);
1708 le16_add_cpu(&path->p_hdr->eh_entries, -1);
1709 err = ext4_ext_dirty(handle, inode, path);
1712 ext_debug("index is empty, remove it, free block %llu\n", leaf);
1713 bh = sb_find_get_block(inode->i_sb, leaf);
1714 ext4_forget(handle, 1, inode, bh, leaf);
1715 ext4_free_blocks(handle, inode, leaf, 1, 1);
1720 * ext4_ext_calc_credits_for_insert:
1721 * This routine returns max. credits that the extent tree can consume.
1722 * It should be OK for low-performance paths like ->writepage()
1723 * To allow many writing processes to fit into a single transaction,
1724 * the caller should calculate credits under i_data_sem and
1725 * pass the actual path.
1727 int ext4_ext_calc_credits_for_insert(struct inode *inode,
1728 struct ext4_ext_path *path)
1733 /* probably there is space in leaf? */
1734 depth = ext_depth(inode);
1735 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
1736 < le16_to_cpu(path[depth].p_hdr->eh_max))
1741 * given 32-bit logical block (4294967296 blocks), max. tree
1742 * can be 4 levels in depth -- 4 * 340^4 == 53453440000.
1743 * Let's also add one more level for imbalance.
1747 /* allocation of new data block(s) */
1751 * tree can be full, so it would need to grow in depth:
1752 * we need one credit to modify old root, credits for
1753 * new root will be added in split accounting
1758 * Index split can happen, we would need:
1759 * allocate intermediate indexes (bitmap + group)
1760 * + change two blocks at each level, but root (already included)
1762 needed += (depth * 2) + (depth * 2);
1764 /* any allocation modifies superblock */
1770 static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
1771 struct ext4_extent *ex,
1772 ext4_lblk_t from, ext4_lblk_t to)
1774 struct buffer_head *bh;
1775 unsigned short ee_len = ext4_ext_get_actual_len(ex);
1776 int i, metadata = 0;
1778 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
1780 #ifdef EXTENTS_STATS
1782 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1783 spin_lock(&sbi->s_ext_stats_lock);
1784 sbi->s_ext_blocks += ee_len;
1785 sbi->s_ext_extents++;
1786 if (ee_len < sbi->s_ext_min)
1787 sbi->s_ext_min = ee_len;
1788 if (ee_len > sbi->s_ext_max)
1789 sbi->s_ext_max = ee_len;
1790 if (ext_depth(inode) > sbi->s_depth_max)
1791 sbi->s_depth_max = ext_depth(inode);
1792 spin_unlock(&sbi->s_ext_stats_lock);
1795 if (from >= le32_to_cpu(ex->ee_block)
1796 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
1801 num = le32_to_cpu(ex->ee_block) + ee_len - from;
1802 start = ext_pblock(ex) + ee_len - num;
1803 ext_debug("free last %u blocks starting %llu\n", num, start);
1804 for (i = 0; i < num; i++) {
1805 bh = sb_find_get_block(inode->i_sb, start + i);
1806 ext4_forget(handle, 0, inode, bh, start + i);
1808 ext4_free_blocks(handle, inode, start, num, metadata);
1809 } else if (from == le32_to_cpu(ex->ee_block)
1810 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
1811 printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n",
1812 from, to, le32_to_cpu(ex->ee_block), ee_len);
1814 printk(KERN_INFO "strange request: removal(2) "
1815 "%u-%u from %u:%u\n",
1816 from, to, le32_to_cpu(ex->ee_block), ee_len);
1822 ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
1823 struct ext4_ext_path *path, ext4_lblk_t start)
1825 int err = 0, correct_index = 0;
1826 int depth = ext_depth(inode), credits;
1827 struct ext4_extent_header *eh;
1828 ext4_lblk_t a, b, block;
1830 ext4_lblk_t ex_ee_block;
1831 unsigned short ex_ee_len;
1832 unsigned uninitialized = 0;
1833 struct ext4_extent *ex;
1835 /* the header must be checked already in ext4_ext_remove_space() */
1836 ext_debug("truncate since %u in leaf\n", start);
1837 if (!path[depth].p_hdr)
1838 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
1839 eh = path[depth].p_hdr;
1842 /* find where to start removing */
1843 ex = EXT_LAST_EXTENT(eh);
1845 ex_ee_block = le32_to_cpu(ex->ee_block);
1846 if (ext4_ext_is_uninitialized(ex))
1848 ex_ee_len = ext4_ext_get_actual_len(ex);
1850 while (ex >= EXT_FIRST_EXTENT(eh) &&
1851 ex_ee_block + ex_ee_len > start) {
1852 ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len);
1853 path[depth].p_ext = ex;
1855 a = ex_ee_block > start ? ex_ee_block : start;
1856 b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
1857 ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
1859 ext_debug(" border %u:%u\n", a, b);
1861 if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
1865 } else if (a != ex_ee_block) {
1866 /* remove tail of the extent */
1867 block = ex_ee_block;
1869 } else if (b != ex_ee_block + ex_ee_len - 1) {
1870 /* remove head of the extent */
1873 /* there is no "make a hole" API yet */
1876 /* remove whole extent: excellent! */
1877 block = ex_ee_block;
1879 BUG_ON(a != ex_ee_block);
1880 BUG_ON(b != ex_ee_block + ex_ee_len - 1);
1883 /* at present, extent can't cross block group: */
1884 /* leaf + bitmap + group desc + sb + inode */
1886 if (ex == EXT_FIRST_EXTENT(eh)) {
1888 credits += (ext_depth(inode)) + 1;
1891 credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
1894 err = ext4_ext_journal_restart(handle, credits);
1898 err = ext4_ext_get_access(handle, inode, path + depth);
1902 err = ext4_remove_blocks(handle, inode, ex, a, b);
1907 /* this extent is removed; mark slot entirely unused */
1908 ext4_ext_store_pblock(ex, 0);
1909 le16_add_cpu(&eh->eh_entries, -1);
1912 ex->ee_block = cpu_to_le32(block);
1913 ex->ee_len = cpu_to_le16(num);
1915 * Do not mark uninitialized if all the blocks in the
1916 * extent have been removed.
1918 if (uninitialized && num)
1919 ext4_ext_mark_uninitialized(ex);
1921 err = ext4_ext_dirty(handle, inode, path + depth);
1925 ext_debug("new extent: %u:%u:%llu\n", block, num,
1928 ex_ee_block = le32_to_cpu(ex->ee_block);
1929 ex_ee_len = ext4_ext_get_actual_len(ex);
1932 if (correct_index && eh->eh_entries)
1933 err = ext4_ext_correct_indexes(handle, inode, path);
1935 /* if this leaf is free, then we should
1936 * remove it from index block above */
1937 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
1938 err = ext4_ext_rm_idx(handle, inode, path + depth);
1945 * ext4_ext_more_to_rm:
1946 * returns 1 if current index has to be freed (even partial)
1949 ext4_ext_more_to_rm(struct ext4_ext_path *path)
1951 BUG_ON(path->p_idx == NULL);
1953 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
1957 * if truncate on deeper level happened, it wasn't partial,
1958 * so we have to consider current index for truncation
1960 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
1965 static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
1967 struct super_block *sb = inode->i_sb;
1968 int depth = ext_depth(inode);
1969 struct ext4_ext_path *path;
1973 ext_debug("truncate since %u\n", start);
1975 /* probably first extent we're gonna free will be last in block */
1976 handle = ext4_journal_start(inode, depth + 1);
1978 return PTR_ERR(handle);
1980 ext4_ext_invalidate_cache(inode);
1983 * We start scanning from right side, freeing all the blocks
1984 * after i_size and walking into the tree depth-wise.
1986 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
1988 ext4_journal_stop(handle);
1991 path[0].p_hdr = ext_inode_hdr(inode);
1992 if (ext4_ext_check_header(inode, path[0].p_hdr, depth)) {
1996 path[0].p_depth = depth;
1998 while (i >= 0 && err == 0) {
2000 /* this is leaf block */
2001 err = ext4_ext_rm_leaf(handle, inode, path, start);
2002 /* root level has p_bh == NULL, brelse() eats this */
2003 brelse(path[i].p_bh);
2004 path[i].p_bh = NULL;
2009 /* this is index block */
2010 if (!path[i].p_hdr) {
2011 ext_debug("initialize header\n");
2012 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
2015 if (!path[i].p_idx) {
2016 /* this level hasn't been touched yet */
2017 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2018 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2019 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2021 le16_to_cpu(path[i].p_hdr->eh_entries));
2023 /* we were already here, see at next index */
2027 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2028 i, EXT_FIRST_INDEX(path[i].p_hdr),
2030 if (ext4_ext_more_to_rm(path + i)) {
2031 struct buffer_head *bh;
2032 /* go to the next level */
2033 ext_debug("move to level %d (block %llu)\n",
2034 i + 1, idx_pblock(path[i].p_idx));
2035 memset(path + i + 1, 0, sizeof(*path));
2036 bh = sb_bread(sb, idx_pblock(path[i].p_idx));
2038 /* should we reset i_size? */
2042 if (WARN_ON(i + 1 > depth)) {
2046 if (ext4_ext_check_header(inode, ext_block_hdr(bh),
2051 path[i + 1].p_bh = bh;
2053 /* save actual number of indexes since this
2054 * number is changed at the next iteration */
2055 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2058 /* we finished processing this index, go up */
2059 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
2060 /* index is empty, remove it;
2061 * handle must be already prepared by the
2062 * truncatei_leaf() */
2063 err = ext4_ext_rm_idx(handle, inode, path + i);
2065 /* root level has p_bh == NULL, brelse() eats this */
2066 brelse(path[i].p_bh);
2067 path[i].p_bh = NULL;
2069 ext_debug("return to level %d\n", i);
2073 /* TODO: flexible tree reduction should be here */
2074 if (path->p_hdr->eh_entries == 0) {
2076 * truncate to zero freed all the tree,
2077 * so we need to correct eh_depth
2079 err = ext4_ext_get_access(handle, inode, path);
2081 ext_inode_hdr(inode)->eh_depth = 0;
2082 ext_inode_hdr(inode)->eh_max =
2083 cpu_to_le16(ext4_ext_space_root(inode));
2084 err = ext4_ext_dirty(handle, inode, path);
2088 ext4_ext_tree_changed(inode);
2089 ext4_ext_drop_refs(path);
2091 ext4_journal_stop(handle);
2097 * called at mount time
2099 void ext4_ext_init(struct super_block *sb)
2102 * possible initialization would be here
2105 if (test_opt(sb, EXTENTS)) {
2106 printk("EXT4-fs: file extents enabled");
2107 #ifdef AGGRESSIVE_TEST
2108 printk(", aggressive tests");
2110 #ifdef CHECK_BINSEARCH
2111 printk(", check binsearch");
2113 #ifdef EXTENTS_STATS
2117 #ifdef EXTENTS_STATS
2118 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2119 EXT4_SB(sb)->s_ext_min = 1 << 30;
2120 EXT4_SB(sb)->s_ext_max = 0;
2126 * called at umount time
2128 void ext4_ext_release(struct super_block *sb)
2130 if (!test_opt(sb, EXTENTS))
2133 #ifdef EXTENTS_STATS
2134 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2135 struct ext4_sb_info *sbi = EXT4_SB(sb);
2136 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2137 sbi->s_ext_blocks, sbi->s_ext_extents,
2138 sbi->s_ext_blocks / sbi->s_ext_extents);
2139 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2140 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2145 static void bi_complete(struct bio *bio, int error)
2147 complete((struct completion *)bio->bi_private);
2150 /* FIXME!! we need to try to merge to left or right after zero-out */
2151 static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2155 int blkbits, blocksize;
2157 struct completion event;
2158 unsigned int ee_len, len, done, offset;
2161 blkbits = inode->i_blkbits;
2162 blocksize = inode->i_sb->s_blocksize;
2163 ee_len = ext4_ext_get_actual_len(ex);
2164 ee_pblock = ext_pblock(ex);
2166 /* convert ee_pblock to 512 byte sectors */
2167 ee_pblock = ee_pblock << (blkbits - 9);
2169 while (ee_len > 0) {
2171 if (ee_len > BIO_MAX_PAGES)
2172 len = BIO_MAX_PAGES;
2176 bio = bio_alloc(GFP_NOIO, len);
2179 bio->bi_sector = ee_pblock;
2180 bio->bi_bdev = inode->i_sb->s_bdev;
2184 while (done < len) {
2185 ret = bio_add_page(bio, ZERO_PAGE(0),
2187 if (ret != blocksize) {
2189 * We can't add any more pages because of
2190 * hardware limitations. Start a new bio.
2195 offset += blocksize;
2196 if (offset >= PAGE_CACHE_SIZE)
2200 init_completion(&event);
2201 bio->bi_private = &event;
2202 bio->bi_end_io = bi_complete;
2203 submit_bio(WRITE, bio);
2204 wait_for_completion(&event);
2206 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
2214 ee_pblock += done << (blkbits - 9);
2219 #define EXT4_EXT_ZERO_LEN 7
2222 * This function is called by ext4_ext_get_blocks() if someone tries to write
2223 * to an uninitialized extent. It may result in splitting the uninitialized
2224 * extent into multiple extents (upto three - one initialized and two
2226 * There are three possibilities:
2227 * a> There is no split required: Entire extent should be initialized
2228 * b> Splits in two extents: Write is happening at either end of the extent
2229 * c> Splits in three extents: Somone is writing in middle of the extent
2231 static int ext4_ext_convert_to_initialized(handle_t *handle,
2232 struct inode *inode,
2233 struct ext4_ext_path *path,
2235 unsigned long max_blocks)
2237 struct ext4_extent *ex, newex, orig_ex;
2238 struct ext4_extent *ex1 = NULL;
2239 struct ext4_extent *ex2 = NULL;
2240 struct ext4_extent *ex3 = NULL;
2241 struct ext4_extent_header *eh;
2242 ext4_lblk_t ee_block;
2243 unsigned int allocated, ee_len, depth;
2244 ext4_fsblk_t newblock;
2248 depth = ext_depth(inode);
2249 eh = path[depth].p_hdr;
2250 ex = path[depth].p_ext;
2251 ee_block = le32_to_cpu(ex->ee_block);
2252 ee_len = ext4_ext_get_actual_len(ex);
2253 allocated = ee_len - (iblock - ee_block);
2254 newblock = iblock - ee_block + ext_pblock(ex);
2256 orig_ex.ee_block = ex->ee_block;
2257 orig_ex.ee_len = cpu_to_le16(ee_len);
2258 ext4_ext_store_pblock(&orig_ex, ext_pblock(ex));
2260 err = ext4_ext_get_access(handle, inode, path + depth);
2263 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
2264 if (ee_len <= 2*EXT4_EXT_ZERO_LEN) {
2265 err = ext4_ext_zeroout(inode, &orig_ex);
2267 goto fix_extent_len;
2268 /* update the extent length and mark as initialized */
2269 ex->ee_block = orig_ex.ee_block;
2270 ex->ee_len = orig_ex.ee_len;
2271 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2272 ext4_ext_dirty(handle, inode, path + depth);
2273 /* zeroed the full extent */
2277 /* ex1: ee_block to iblock - 1 : uninitialized */
2278 if (iblock > ee_block) {
2280 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2281 ext4_ext_mark_uninitialized(ex1);
2285 * for sanity, update the length of the ex2 extent before
2286 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2287 * overlap of blocks.
2289 if (!ex1 && allocated > max_blocks)
2290 ex2->ee_len = cpu_to_le16(max_blocks);
2291 /* ex3: to ee_block + ee_len : uninitialised */
2292 if (allocated > max_blocks) {
2293 unsigned int newdepth;
2294 /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */
2295 if (allocated <= EXT4_EXT_ZERO_LEN) {
2296 /* Mark first half uninitialized.
2297 * Mark second half initialized and zero out the
2298 * initialized extent
2300 ex->ee_block = orig_ex.ee_block;
2301 ex->ee_len = cpu_to_le16(ee_len - allocated);
2302 ext4_ext_mark_uninitialized(ex);
2303 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2304 ext4_ext_dirty(handle, inode, path + depth);
2307 ex3->ee_block = cpu_to_le32(iblock);
2308 ext4_ext_store_pblock(ex3, newblock);
2309 ex3->ee_len = cpu_to_le16(allocated);
2310 err = ext4_ext_insert_extent(handle, inode, path, ex3);
2311 if (err == -ENOSPC) {
2312 err = ext4_ext_zeroout(inode, &orig_ex);
2314 goto fix_extent_len;
2315 ex->ee_block = orig_ex.ee_block;
2316 ex->ee_len = orig_ex.ee_len;
2317 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2318 ext4_ext_dirty(handle, inode, path + depth);
2319 /* zeroed the full extent */
2323 goto fix_extent_len;
2326 * We need to zero out the second half because
2327 * an fallocate request can update file size and
2328 * converting the second half to initialized extent
2329 * implies that we can leak some junk data to user
2332 err = ext4_ext_zeroout(inode, ex3);
2335 * We should actually mark the
2336 * second half as uninit and return error
2337 * Insert would have changed the extent
2339 depth = ext_depth(inode);
2340 ext4_ext_drop_refs(path);
2341 path = ext4_ext_find_extent(inode,
2344 err = PTR_ERR(path);
2347 ex = path[depth].p_ext;
2348 err = ext4_ext_get_access(handle, inode,
2352 ext4_ext_mark_uninitialized(ex);
2353 ext4_ext_dirty(handle, inode, path + depth);
2357 /* zeroed the second half */
2361 ex3->ee_block = cpu_to_le32(iblock + max_blocks);
2362 ext4_ext_store_pblock(ex3, newblock + max_blocks);
2363 ex3->ee_len = cpu_to_le16(allocated - max_blocks);
2364 ext4_ext_mark_uninitialized(ex3);
2365 err = ext4_ext_insert_extent(handle, inode, path, ex3);
2366 if (err == -ENOSPC) {
2367 err = ext4_ext_zeroout(inode, &orig_ex);
2369 goto fix_extent_len;
2370 /* update the extent length and mark as initialized */
2371 ex->ee_block = orig_ex.ee_block;
2372 ex->ee_len = orig_ex.ee_len;
2373 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2374 ext4_ext_dirty(handle, inode, path + depth);
2375 /* zeroed the full extent */
2379 goto fix_extent_len;
2381 * The depth, and hence eh & ex might change
2382 * as part of the insert above.
2384 newdepth = ext_depth(inode);
2386 * update the extent length after successfull insert of the
2389 orig_ex.ee_len = cpu_to_le16(ee_len -
2390 ext4_ext_get_actual_len(ex3));
2391 if (newdepth != depth) {
2393 ext4_ext_drop_refs(path);
2394 path = ext4_ext_find_extent(inode, iblock, path);
2396 err = PTR_ERR(path);
2399 eh = path[depth].p_hdr;
2400 ex = path[depth].p_ext;
2404 err = ext4_ext_get_access(handle, inode, path + depth);
2408 allocated = max_blocks;
2410 /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying
2411 * to insert a extent in the middle zerout directly
2412 * otherwise give the extent a chance to merge to left
2414 if (le16_to_cpu(orig_ex.ee_len) <= EXT4_EXT_ZERO_LEN &&
2415 iblock != ee_block) {
2416 err = ext4_ext_zeroout(inode, &orig_ex);
2418 goto fix_extent_len;
2419 /* update the extent length and mark as initialized */
2420 ex->ee_block = orig_ex.ee_block;
2421 ex->ee_len = orig_ex.ee_len;
2422 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2423 ext4_ext_dirty(handle, inode, path + depth);
2424 /* zero out the first half */
2429 * If there was a change of depth as part of the
2430 * insertion of ex3 above, we need to update the length
2431 * of the ex1 extent again here
2433 if (ex1 && ex1 != ex) {
2435 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2436 ext4_ext_mark_uninitialized(ex1);
2439 /* ex2: iblock to iblock + maxblocks-1 : initialised */
2440 ex2->ee_block = cpu_to_le32(iblock);
2441 ext4_ext_store_pblock(ex2, newblock);
2442 ex2->ee_len = cpu_to_le16(allocated);
2446 * New (initialized) extent starts from the first block
2447 * in the current extent. i.e., ex2 == ex
2448 * We have to see if it can be merged with the extent
2451 if (ex2 > EXT_FIRST_EXTENT(eh)) {
2453 * To merge left, pass "ex2 - 1" to try_to_merge(),
2454 * since it merges towards right _only_.
2456 ret = ext4_ext_try_to_merge(inode, path, ex2 - 1);
2458 err = ext4_ext_correct_indexes(handle, inode, path);
2461 depth = ext_depth(inode);
2466 * Try to Merge towards right. This might be required
2467 * only when the whole extent is being written to.
2468 * i.e. ex2 == ex and ex3 == NULL.
2471 ret = ext4_ext_try_to_merge(inode, path, ex2);
2473 err = ext4_ext_correct_indexes(handle, inode, path);
2478 /* Mark modified extent as dirty */
2479 err = ext4_ext_dirty(handle, inode, path + depth);
2482 err = ext4_ext_insert_extent(handle, inode, path, &newex);
2483 if (err == -ENOSPC) {
2484 err = ext4_ext_zeroout(inode, &orig_ex);
2486 goto fix_extent_len;
2487 /* update the extent length and mark as initialized */
2488 ex->ee_block = orig_ex.ee_block;
2489 ex->ee_len = orig_ex.ee_len;
2490 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2491 ext4_ext_dirty(handle, inode, path + depth);
2492 /* zero out the first half */
2495 goto fix_extent_len;
2497 return err ? err : allocated;
2500 ex->ee_block = orig_ex.ee_block;
2501 ex->ee_len = orig_ex.ee_len;
2502 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2503 ext4_ext_mark_uninitialized(ex);
2504 ext4_ext_dirty(handle, inode, path + depth);
2509 * Block allocation/map/preallocation routine for extents based files
2512 * Need to be called with
2513 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
2514 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
2516 * return > 0, number of of blocks already mapped/allocated
2517 * if create == 0 and these are pre-allocated blocks
2518 * buffer head is unmapped
2519 * otherwise blocks are mapped
2521 * return = 0, if plain look up failed (blocks have not been allocated)
2522 * buffer head is unmapped
2524 * return < 0, error case.
2526 int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
2528 unsigned long max_blocks, struct buffer_head *bh_result,
2529 int create, int extend_disksize)
2531 struct ext4_ext_path *path = NULL;
2532 struct ext4_extent_header *eh;
2533 struct ext4_extent newex, *ex;
2534 ext4_fsblk_t goal, newblock;
2535 int err = 0, depth, ret;
2536 unsigned long allocated = 0;
2537 struct ext4_allocation_request ar;
2539 __clear_bit(BH_New, &bh_result->b_state);
2540 ext_debug("blocks %u/%lu requested for inode %u\n",
2541 iblock, max_blocks, inode->i_ino);
2543 /* check in cache */
2544 goal = ext4_ext_in_cache(inode, iblock, &newex);
2546 if (goal == EXT4_EXT_CACHE_GAP) {
2549 * block isn't allocated yet and
2550 * user doesn't want to allocate it
2554 /* we should allocate requested block */
2555 } else if (goal == EXT4_EXT_CACHE_EXTENT) {
2556 /* block is already allocated */
2558 - le32_to_cpu(newex.ee_block)
2559 + ext_pblock(&newex);
2560 /* number of remaining blocks in the extent */
2561 allocated = ext4_ext_get_actual_len(&newex) -
2562 (iblock - le32_to_cpu(newex.ee_block));
2569 /* find extent for this block */
2570 path = ext4_ext_find_extent(inode, iblock, NULL);
2572 err = PTR_ERR(path);
2577 depth = ext_depth(inode);
2580 * consistent leaf must not be empty;
2581 * this situation is possible, though, _during_ tree modification;
2582 * this is why assert can't be put in ext4_ext_find_extent()
2584 BUG_ON(path[depth].p_ext == NULL && depth != 0);
2585 eh = path[depth].p_hdr;
2587 ex = path[depth].p_ext;
2589 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
2590 ext4_fsblk_t ee_start = ext_pblock(ex);
2591 unsigned short ee_len;
2594 * Uninitialized extents are treated as holes, except that
2595 * we split out initialized portions during a write.
2597 ee_len = ext4_ext_get_actual_len(ex);
2598 /* if found extent covers block, simply return it */
2599 if (iblock >= ee_block && iblock < ee_block + ee_len) {
2600 newblock = iblock - ee_block + ee_start;
2601 /* number of remaining blocks in the extent */
2602 allocated = ee_len - (iblock - ee_block);
2603 ext_debug("%u fit into %lu:%d -> %llu\n", iblock,
2604 ee_block, ee_len, newblock);
2606 /* Do not put uninitialized extent in the cache */
2607 if (!ext4_ext_is_uninitialized(ex)) {
2608 ext4_ext_put_in_cache(inode, ee_block,
2610 EXT4_EXT_CACHE_EXTENT);
2613 if (create == EXT4_CREATE_UNINITIALIZED_EXT)
2617 * We have blocks reserved already. We
2618 * return allocated blocks so that delalloc
2619 * won't do block reservation for us. But
2620 * the buffer head will be unmapped so that
2621 * a read from the block returns 0s.
2623 if (allocated > max_blocks)
2624 allocated = max_blocks;
2625 set_buffer_unwritten(bh_result);
2629 ret = ext4_ext_convert_to_initialized(handle, inode,
2642 * requested block isn't allocated yet;
2643 * we couldn't try to create block if create flag is zero
2647 * put just found gap into cache to speed up
2648 * subsequent requests
2650 ext4_ext_put_gap_in_cache(inode, path, iblock);
2654 * Okay, we need to do block allocation. Lazily initialize the block
2655 * allocation info here if necessary.
2657 if (S_ISREG(inode->i_mode) && (!EXT4_I(inode)->i_block_alloc_info))
2658 ext4_init_block_alloc_info(inode);
2660 /* find neighbour allocated blocks */
2662 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
2666 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright);
2671 * See if request is beyond maximum number of blocks we can have in
2672 * a single extent. For an initialized extent this limit is
2673 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
2674 * EXT_UNINIT_MAX_LEN.
2676 if (max_blocks > EXT_INIT_MAX_LEN &&
2677 create != EXT4_CREATE_UNINITIALIZED_EXT)
2678 max_blocks = EXT_INIT_MAX_LEN;
2679 else if (max_blocks > EXT_UNINIT_MAX_LEN &&
2680 create == EXT4_CREATE_UNINITIALIZED_EXT)
2681 max_blocks = EXT_UNINIT_MAX_LEN;
2683 /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */
2684 newex.ee_block = cpu_to_le32(iblock);
2685 newex.ee_len = cpu_to_le16(max_blocks);
2686 err = ext4_ext_check_overlap(inode, &newex, path);
2688 allocated = ext4_ext_get_actual_len(&newex);
2690 allocated = max_blocks;
2692 /* allocate new block */
2694 ar.goal = ext4_ext_find_goal(inode, path, iblock);
2695 ar.logical = iblock;
2697 if (S_ISREG(inode->i_mode))
2698 ar.flags = EXT4_MB_HINT_DATA;
2700 /* disable in-core preallocation for non-regular files */
2702 newblock = ext4_mb_new_blocks(handle, &ar, &err);
2705 ext_debug("allocate new block: goal %llu, found %llu/%lu\n",
2706 goal, newblock, allocated);
2708 /* try to insert new extent into found leaf and return */
2709 ext4_ext_store_pblock(&newex, newblock);
2710 newex.ee_len = cpu_to_le16(ar.len);
2711 if (create == EXT4_CREATE_UNINITIALIZED_EXT) /* Mark uninitialized */
2712 ext4_ext_mark_uninitialized(&newex);
2713 err = ext4_ext_insert_extent(handle, inode, path, &newex);
2715 /* free data blocks we just allocated */
2716 /* not a good idea to call discard here directly,
2717 * but otherwise we'd need to call it every free() */
2718 ext4_mb_discard_inode_preallocations(inode);
2719 ext4_free_blocks(handle, inode, ext_pblock(&newex),
2720 ext4_ext_get_actual_len(&newex), 0);
2724 /* previous routine could use block we allocated */
2725 newblock = ext_pblock(&newex);
2726 allocated = ext4_ext_get_actual_len(&newex);
2728 if (extend_disksize && inode->i_size > EXT4_I(inode)->i_disksize)
2729 EXT4_I(inode)->i_disksize = inode->i_size;
2731 set_buffer_new(bh_result);
2733 /* Cache only when it is _not_ an uninitialized extent */
2734 if (create != EXT4_CREATE_UNINITIALIZED_EXT)
2735 ext4_ext_put_in_cache(inode, iblock, allocated, newblock,
2736 EXT4_EXT_CACHE_EXTENT);
2738 if (allocated > max_blocks)
2739 allocated = max_blocks;
2740 ext4_ext_show_leaf(inode, path);
2741 set_buffer_mapped(bh_result);
2742 bh_result->b_bdev = inode->i_sb->s_bdev;
2743 bh_result->b_blocknr = newblock;
2746 ext4_ext_drop_refs(path);
2749 return err ? err : allocated;
2752 void ext4_ext_truncate(struct inode *inode)
2754 struct address_space *mapping = inode->i_mapping;
2755 struct super_block *sb = inode->i_sb;
2756 ext4_lblk_t last_block;
2761 * probably first extent we're gonna free will be last in block
2763 err = ext4_writepage_trans_blocks(inode) + 3;
2764 handle = ext4_journal_start(inode, err);
2768 if (inode->i_size & (sb->s_blocksize - 1))
2769 ext4_block_truncate_page(handle, mapping, inode->i_size);
2771 if (ext4_orphan_add(handle, inode))
2774 down_write(&EXT4_I(inode)->i_data_sem);
2775 ext4_ext_invalidate_cache(inode);
2777 ext4_mb_discard_inode_preallocations(inode);
2780 * TODO: optimization is possible here.
2781 * Probably we need not scan at all,
2782 * because page truncation is enough.
2785 /* we have to know where to truncate from in crash case */
2786 EXT4_I(inode)->i_disksize = inode->i_size;
2787 ext4_mark_inode_dirty(handle, inode);
2789 last_block = (inode->i_size + sb->s_blocksize - 1)
2790 >> EXT4_BLOCK_SIZE_BITS(sb);
2791 err = ext4_ext_remove_space(inode, last_block);
2793 /* In a multi-transaction truncate, we only make the final
2794 * transaction synchronous.
2800 up_write(&EXT4_I(inode)->i_data_sem);
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 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
2812 ext4_mark_inode_dirty(handle, inode);
2813 ext4_journal_stop(handle);
2817 * ext4_ext_writepage_trans_blocks:
2818 * calculate max number of blocks we could modify
2819 * in order to allocate new block for an inode
2821 int ext4_ext_writepage_trans_blocks(struct inode *inode, int num)
2825 needed = ext4_ext_calc_credits_for_insert(inode, NULL);
2827 /* caller wants to allocate num blocks, but note it includes sb */
2828 needed = needed * num - (num - 1);
2831 needed += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
2837 static void ext4_falloc_update_inode(struct inode *inode,
2838 int mode, loff_t new_size, int update_ctime)
2840 struct timespec now;
2843 now = current_fs_time(inode->i_sb);
2844 if (!timespec_equal(&inode->i_ctime, &now))
2845 inode->i_ctime = now;
2848 * Update only when preallocation was requested beyond
2851 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
2852 new_size > i_size_read(inode)) {
2853 i_size_write(inode, new_size);
2854 EXT4_I(inode)->i_disksize = new_size;
2860 * preallocate space for a file. This implements ext4's fallocate inode
2861 * operation, which gets called from sys_fallocate system call.
2862 * For block-mapped files, posix_fallocate should fall back to the method
2863 * of writing zeroes to the required new blocks (the same behavior which is
2864 * expected for file systems which do not support fallocate() system call).
2866 long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
2871 unsigned long max_blocks;
2875 struct buffer_head map_bh;
2876 unsigned int credits, blkbits = inode->i_blkbits;
2879 * currently supporting (pre)allocate mode for extent-based
2882 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
2885 /* preallocation to directories is currently not supported */
2886 if (S_ISDIR(inode->i_mode))
2889 block = offset >> blkbits;
2891 * We can't just convert len to max_blocks because
2892 * If blocksize = 4096 offset = 3072 and len = 2048
2894 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
2897 * credits to insert 1 extent into extent tree + buffers to be able to
2898 * modify 1 super block, 1 block bitmap and 1 group descriptor.
2900 credits = EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + 3;
2901 mutex_lock(&inode->i_mutex);
2903 while (ret >= 0 && ret < max_blocks) {
2904 block = block + ret;
2905 max_blocks = max_blocks - ret;
2906 handle = ext4_journal_start(inode, credits);
2907 if (IS_ERR(handle)) {
2908 ret = PTR_ERR(handle);
2911 ret = ext4_get_blocks_wrap(handle, inode, block,
2912 max_blocks, &map_bh,
2913 EXT4_CREATE_UNINITIALIZED_EXT, 0);
2917 printk(KERN_ERR "%s: ext4_ext_get_blocks "
2918 "returned error inode#%lu, block=%u, "
2919 "max_blocks=%lu", __func__,
2920 inode->i_ino, block, max_blocks);
2922 ext4_mark_inode_dirty(handle, inode);
2923 ret2 = ext4_journal_stop(handle);
2926 if ((block + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
2927 blkbits) >> blkbits))
2928 new_size = offset + len;
2930 new_size = (block + ret) << blkbits;
2932 ext4_falloc_update_inode(inode, mode, new_size,
2933 buffer_new(&map_bh));
2934 ext4_mark_inode_dirty(handle, inode);
2935 ret2 = ext4_journal_stop(handle);
2939 if (ret == -ENOSPC &&
2940 ext4_should_retry_alloc(inode->i_sb, &retries)) {
2944 mutex_unlock(&inode->i_mutex);
2945 return ret > 0 ? ret2 : ret;