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 <linux/fiemap.h>
44 #include "ext4_jbd2.h"
45 #include "ext4_extents.h"
50 * combine low and high parts of physical block number into ext4_fsblk_t
52 ext4_fsblk_t ext_pblock(struct ext4_extent *ex)
56 block = le32_to_cpu(ex->ee_start_lo);
57 block |= ((ext4_fsblk_t) le16_to_cpu(ex->ee_start_hi) << 31) << 1;
63 * combine low and high parts of a leaf physical block number into ext4_fsblk_t
65 ext4_fsblk_t idx_pblock(struct ext4_extent_idx *ix)
69 block = le32_to_cpu(ix->ei_leaf_lo);
70 block |= ((ext4_fsblk_t) le16_to_cpu(ix->ei_leaf_hi) << 31) << 1;
75 * ext4_ext_store_pblock:
76 * stores a large physical block number into an extent struct,
77 * breaking it into parts
79 void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb)
81 ex->ee_start_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff));
82 ex->ee_start_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
86 * ext4_idx_store_pblock:
87 * stores a large physical block number into an index struct,
88 * breaking it into parts
90 static void ext4_idx_store_pblock(struct ext4_extent_idx *ix, ext4_fsblk_t pb)
92 ix->ei_leaf_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff));
93 ix->ei_leaf_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
96 static int ext4_ext_truncate_extend_restart(handle_t *handle,
102 if (!ext4_handle_valid(handle))
104 if (handle->h_buffer_credits > needed)
106 err = ext4_journal_extend(handle, needed);
109 err = ext4_truncate_restart_trans(handle, inode, needed);
121 static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
122 struct ext4_ext_path *path)
125 /* path points to block */
126 return ext4_journal_get_write_access(handle, path->p_bh);
128 /* path points to leaf/index in inode body */
129 /* we use in-core data, no need to protect them */
139 static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
140 struct ext4_ext_path *path)
144 /* path points to block */
145 err = ext4_handle_dirty_metadata(handle, inode, path->p_bh);
147 /* path points to leaf/index in inode body */
148 err = ext4_mark_inode_dirty(handle, inode);
153 static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
154 struct ext4_ext_path *path,
157 struct ext4_inode_info *ei = EXT4_I(inode);
158 ext4_fsblk_t bg_start;
159 ext4_fsblk_t last_block;
160 ext4_grpblk_t colour;
161 ext4_group_t block_group;
162 int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
166 struct ext4_extent *ex;
167 depth = path->p_depth;
169 /* try to predict block placement */
170 ex = path[depth].p_ext;
172 return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block));
174 /* it looks like index is empty;
175 * try to find starting block from index itself */
176 if (path[depth].p_bh)
177 return path[depth].p_bh->b_blocknr;
180 /* OK. use inode's group */
181 block_group = ei->i_block_group;
182 if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
184 * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
185 * block groups per flexgroup, reserve the first block
186 * group for directories and special files. Regular
187 * files will start at the second block group. This
188 * tends to speed up directory access and improves
191 block_group &= ~(flex_size-1);
192 if (S_ISREG(inode->i_mode))
195 bg_start = (block_group * EXT4_BLOCKS_PER_GROUP(inode->i_sb)) +
196 le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_first_data_block);
197 last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
200 * If we are doing delayed allocation, we don't need take
201 * colour into account.
203 if (test_opt(inode->i_sb, DELALLOC))
206 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
207 colour = (current->pid % 16) *
208 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
210 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
211 return bg_start + colour + block;
215 * Allocation for a meta data block
218 ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
219 struct ext4_ext_path *path,
220 struct ext4_extent *ex, int *err)
222 ext4_fsblk_t goal, newblock;
224 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
225 newblock = ext4_new_meta_blocks(handle, inode, goal, NULL, err);
229 static inline int ext4_ext_space_block(struct inode *inode, int check)
233 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
234 / sizeof(struct ext4_extent);
236 #ifdef AGGRESSIVE_TEST
244 static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
248 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
249 / sizeof(struct ext4_extent_idx);
251 #ifdef AGGRESSIVE_TEST
259 static inline int ext4_ext_space_root(struct inode *inode, int check)
263 size = sizeof(EXT4_I(inode)->i_data);
264 size -= sizeof(struct ext4_extent_header);
265 size /= sizeof(struct ext4_extent);
267 #ifdef AGGRESSIVE_TEST
275 static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
279 size = sizeof(EXT4_I(inode)->i_data);
280 size -= sizeof(struct ext4_extent_header);
281 size /= sizeof(struct ext4_extent_idx);
283 #ifdef AGGRESSIVE_TEST
292 * Calculate the number of metadata blocks needed
293 * to allocate @blocks
294 * Worse case is one block per extent
296 int ext4_ext_calc_metadata_amount(struct inode *inode, sector_t lblock)
298 struct ext4_inode_info *ei = EXT4_I(inode);
301 idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
302 / sizeof(struct ext4_extent_idx));
305 * If the new delayed allocation block is contiguous with the
306 * previous da block, it can share index blocks with the
307 * previous block, so we only need to allocate a new index
308 * block every idxs leaf blocks. At ldxs**2 blocks, we need
309 * an additional index block, and at ldxs**3 blocks, yet
310 * another index blocks.
312 if (ei->i_da_metadata_calc_len &&
313 ei->i_da_metadata_calc_last_lblock+1 == lblock) {
314 if ((ei->i_da_metadata_calc_len % idxs) == 0)
316 if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0)
318 if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) {
320 ei->i_da_metadata_calc_len = 0;
322 ei->i_da_metadata_calc_len++;
323 ei->i_da_metadata_calc_last_lblock++;
328 * In the worst case we need a new set of index blocks at
329 * every level of the inode's extent tree.
331 ei->i_da_metadata_calc_len = 1;
332 ei->i_da_metadata_calc_last_lblock = lblock;
333 return ext_depth(inode) + 1;
337 ext4_ext_max_entries(struct inode *inode, int depth)
341 if (depth == ext_depth(inode)) {
343 max = ext4_ext_space_root(inode, 1);
345 max = ext4_ext_space_root_idx(inode, 1);
348 max = ext4_ext_space_block(inode, 1);
350 max = ext4_ext_space_block_idx(inode, 1);
356 static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
358 ext4_fsblk_t block = ext_pblock(ext);
359 int len = ext4_ext_get_actual_len(ext);
361 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
364 static int ext4_valid_extent_idx(struct inode *inode,
365 struct ext4_extent_idx *ext_idx)
367 ext4_fsblk_t block = idx_pblock(ext_idx);
369 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
372 static int ext4_valid_extent_entries(struct inode *inode,
373 struct ext4_extent_header *eh,
376 struct ext4_extent *ext;
377 struct ext4_extent_idx *ext_idx;
378 unsigned short entries;
379 if (eh->eh_entries == 0)
382 entries = le16_to_cpu(eh->eh_entries);
386 ext = EXT_FIRST_EXTENT(eh);
388 if (!ext4_valid_extent(inode, ext))
394 ext_idx = EXT_FIRST_INDEX(eh);
396 if (!ext4_valid_extent_idx(inode, ext_idx))
405 static int __ext4_ext_check(const char *function, struct inode *inode,
406 struct ext4_extent_header *eh,
409 const char *error_msg;
412 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
413 error_msg = "invalid magic";
416 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
417 error_msg = "unexpected eh_depth";
420 if (unlikely(eh->eh_max == 0)) {
421 error_msg = "invalid eh_max";
424 max = ext4_ext_max_entries(inode, depth);
425 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
426 error_msg = "too large eh_max";
429 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
430 error_msg = "invalid eh_entries";
433 if (!ext4_valid_extent_entries(inode, eh, depth)) {
434 error_msg = "invalid extent entries";
440 ext4_error(inode->i_sb, function,
441 "bad header/extent in inode #%lu: %s - magic %x, "
442 "entries %u, max %u(%u), depth %u(%u)",
443 inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic),
444 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
445 max, le16_to_cpu(eh->eh_depth), depth);
450 #define ext4_ext_check(inode, eh, depth) \
451 __ext4_ext_check(__func__, inode, eh, depth)
453 int ext4_ext_check_inode(struct inode *inode)
455 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode));
459 static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
461 int k, l = path->p_depth;
464 for (k = 0; k <= l; k++, path++) {
466 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
467 idx_pblock(path->p_idx));
468 } else if (path->p_ext) {
469 ext_debug(" %d:[%d]%d:%llu ",
470 le32_to_cpu(path->p_ext->ee_block),
471 ext4_ext_is_uninitialized(path->p_ext),
472 ext4_ext_get_actual_len(path->p_ext),
473 ext_pblock(path->p_ext));
480 static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
482 int depth = ext_depth(inode);
483 struct ext4_extent_header *eh;
484 struct ext4_extent *ex;
490 eh = path[depth].p_hdr;
491 ex = EXT_FIRST_EXTENT(eh);
493 ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
495 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
496 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
497 ext4_ext_is_uninitialized(ex),
498 ext4_ext_get_actual_len(ex), ext_pblock(ex));
503 #define ext4_ext_show_path(inode, path)
504 #define ext4_ext_show_leaf(inode, path)
507 void ext4_ext_drop_refs(struct ext4_ext_path *path)
509 int depth = path->p_depth;
512 for (i = 0; i <= depth; i++, path++)
520 * ext4_ext_binsearch_idx:
521 * binary search for the closest index of the given block
522 * the header must be checked before calling this
525 ext4_ext_binsearch_idx(struct inode *inode,
526 struct ext4_ext_path *path, ext4_lblk_t block)
528 struct ext4_extent_header *eh = path->p_hdr;
529 struct ext4_extent_idx *r, *l, *m;
532 ext_debug("binsearch for %u(idx): ", block);
534 l = EXT_FIRST_INDEX(eh) + 1;
535 r = EXT_LAST_INDEX(eh);
538 if (block < le32_to_cpu(m->ei_block))
542 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
543 m, le32_to_cpu(m->ei_block),
544 r, le32_to_cpu(r->ei_block));
548 ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
549 idx_pblock(path->p_idx));
551 #ifdef CHECK_BINSEARCH
553 struct ext4_extent_idx *chix, *ix;
556 chix = ix = EXT_FIRST_INDEX(eh);
557 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
559 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
560 printk(KERN_DEBUG "k=%d, ix=0x%p, "
562 ix, EXT_FIRST_INDEX(eh));
563 printk(KERN_DEBUG "%u <= %u\n",
564 le32_to_cpu(ix->ei_block),
565 le32_to_cpu(ix[-1].ei_block));
567 BUG_ON(k && le32_to_cpu(ix->ei_block)
568 <= le32_to_cpu(ix[-1].ei_block));
569 if (block < le32_to_cpu(ix->ei_block))
573 BUG_ON(chix != path->p_idx);
580 * ext4_ext_binsearch:
581 * binary search for closest extent of the given block
582 * the header must be checked before calling this
585 ext4_ext_binsearch(struct inode *inode,
586 struct ext4_ext_path *path, ext4_lblk_t block)
588 struct ext4_extent_header *eh = path->p_hdr;
589 struct ext4_extent *r, *l, *m;
591 if (eh->eh_entries == 0) {
593 * this leaf is empty:
594 * we get such a leaf in split/add case
599 ext_debug("binsearch for %u: ", block);
601 l = EXT_FIRST_EXTENT(eh) + 1;
602 r = EXT_LAST_EXTENT(eh);
606 if (block < le32_to_cpu(m->ee_block))
610 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
611 m, le32_to_cpu(m->ee_block),
612 r, le32_to_cpu(r->ee_block));
616 ext_debug(" -> %d:%llu:[%d]%d ",
617 le32_to_cpu(path->p_ext->ee_block),
618 ext_pblock(path->p_ext),
619 ext4_ext_is_uninitialized(path->p_ext),
620 ext4_ext_get_actual_len(path->p_ext));
622 #ifdef CHECK_BINSEARCH
624 struct ext4_extent *chex, *ex;
627 chex = ex = EXT_FIRST_EXTENT(eh);
628 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
629 BUG_ON(k && le32_to_cpu(ex->ee_block)
630 <= le32_to_cpu(ex[-1].ee_block));
631 if (block < le32_to_cpu(ex->ee_block))
635 BUG_ON(chex != path->p_ext);
641 int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
643 struct ext4_extent_header *eh;
645 eh = ext_inode_hdr(inode);
648 eh->eh_magic = EXT4_EXT_MAGIC;
649 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
650 ext4_mark_inode_dirty(handle, inode);
651 ext4_ext_invalidate_cache(inode);
655 struct ext4_ext_path *
656 ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
657 struct ext4_ext_path *path)
659 struct ext4_extent_header *eh;
660 struct buffer_head *bh;
661 short int depth, i, ppos = 0, alloc = 0;
663 eh = ext_inode_hdr(inode);
664 depth = ext_depth(inode);
666 /* account possible depth increase */
668 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
671 return ERR_PTR(-ENOMEM);
678 /* walk through the tree */
680 int need_to_validate = 0;
682 ext_debug("depth %d: num %d, max %d\n",
683 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
685 ext4_ext_binsearch_idx(inode, path + ppos, block);
686 path[ppos].p_block = idx_pblock(path[ppos].p_idx);
687 path[ppos].p_depth = i;
688 path[ppos].p_ext = NULL;
690 bh = sb_getblk(inode->i_sb, path[ppos].p_block);
693 if (!bh_uptodate_or_lock(bh)) {
694 if (bh_submit_read(bh) < 0) {
698 /* validate the extent entries */
699 need_to_validate = 1;
701 eh = ext_block_hdr(bh);
703 BUG_ON(ppos > depth);
704 path[ppos].p_bh = bh;
705 path[ppos].p_hdr = eh;
708 if (need_to_validate && ext4_ext_check(inode, eh, i))
712 path[ppos].p_depth = i;
713 path[ppos].p_ext = NULL;
714 path[ppos].p_idx = NULL;
717 ext4_ext_binsearch(inode, path + ppos, block);
718 /* if not an empty leaf */
719 if (path[ppos].p_ext)
720 path[ppos].p_block = ext_pblock(path[ppos].p_ext);
722 ext4_ext_show_path(inode, path);
727 ext4_ext_drop_refs(path);
730 return ERR_PTR(-EIO);
734 * ext4_ext_insert_index:
735 * insert new index [@logical;@ptr] into the block at @curp;
736 * check where to insert: before @curp or after @curp
738 int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
739 struct ext4_ext_path *curp,
740 int logical, ext4_fsblk_t ptr)
742 struct ext4_extent_idx *ix;
745 err = ext4_ext_get_access(handle, inode, curp);
749 BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block));
750 len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
751 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
753 if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
754 len = (len - 1) * sizeof(struct ext4_extent_idx);
755 len = len < 0 ? 0 : len;
756 ext_debug("insert new index %d after: %llu. "
757 "move %d from 0x%p to 0x%p\n",
759 (curp->p_idx + 1), (curp->p_idx + 2));
760 memmove(curp->p_idx + 2, curp->p_idx + 1, len);
762 ix = curp->p_idx + 1;
765 len = len * sizeof(struct ext4_extent_idx);
766 len = len < 0 ? 0 : len;
767 ext_debug("insert new index %d before: %llu. "
768 "move %d from 0x%p to 0x%p\n",
770 curp->p_idx, (curp->p_idx + 1));
771 memmove(curp->p_idx + 1, curp->p_idx, len);
775 ix->ei_block = cpu_to_le32(logical);
776 ext4_idx_store_pblock(ix, ptr);
777 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
779 BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries)
780 > le16_to_cpu(curp->p_hdr->eh_max));
781 BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr));
783 err = ext4_ext_dirty(handle, inode, curp);
784 ext4_std_error(inode->i_sb, err);
791 * inserts new subtree into the path, using free index entry
793 * - allocates all needed blocks (new leaf and all intermediate index blocks)
794 * - makes decision where to split
795 * - moves remaining extents and index entries (right to the split point)
796 * into the newly allocated blocks
797 * - initializes subtree
799 static int ext4_ext_split(handle_t *handle, struct inode *inode,
800 struct ext4_ext_path *path,
801 struct ext4_extent *newext, int at)
803 struct buffer_head *bh = NULL;
804 int depth = ext_depth(inode);
805 struct ext4_extent_header *neh;
806 struct ext4_extent_idx *fidx;
807 struct ext4_extent *ex;
809 ext4_fsblk_t newblock, oldblock;
811 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
814 /* make decision: where to split? */
815 /* FIXME: now decision is simplest: at current extent */
817 /* if current leaf will be split, then we should use
818 * border from split point */
819 BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr));
820 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
821 border = path[depth].p_ext[1].ee_block;
822 ext_debug("leaf will be split."
823 " next leaf starts at %d\n",
824 le32_to_cpu(border));
826 border = newext->ee_block;
827 ext_debug("leaf will be added."
828 " next leaf starts at %d\n",
829 le32_to_cpu(border));
833 * If error occurs, then we break processing
834 * and mark filesystem read-only. index won't
835 * be inserted and tree will be in consistent
836 * state. Next mount will repair buffers too.
840 * Get array to track all allocated blocks.
841 * We need this to handle errors and free blocks
844 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
848 /* allocate all needed blocks */
849 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
850 for (a = 0; a < depth - at; a++) {
851 newblock = ext4_ext_new_meta_block(handle, inode, path,
855 ablocks[a] = newblock;
858 /* initialize new leaf */
859 newblock = ablocks[--a];
860 BUG_ON(newblock == 0);
861 bh = sb_getblk(inode->i_sb, newblock);
868 err = ext4_journal_get_create_access(handle, bh);
872 neh = ext_block_hdr(bh);
874 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
875 neh->eh_magic = EXT4_EXT_MAGIC;
877 ex = EXT_FIRST_EXTENT(neh);
879 /* move remainder of path[depth] to the new leaf */
880 BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max);
881 /* start copy from next extent */
882 /* TODO: we could do it by single memmove */
885 while (path[depth].p_ext <=
886 EXT_MAX_EXTENT(path[depth].p_hdr)) {
887 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
888 le32_to_cpu(path[depth].p_ext->ee_block),
889 ext_pblock(path[depth].p_ext),
890 ext4_ext_is_uninitialized(path[depth].p_ext),
891 ext4_ext_get_actual_len(path[depth].p_ext),
893 /*memmove(ex++, path[depth].p_ext++,
894 sizeof(struct ext4_extent));
900 memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
901 le16_add_cpu(&neh->eh_entries, m);
904 set_buffer_uptodate(bh);
907 err = ext4_handle_dirty_metadata(handle, inode, bh);
913 /* correct old leaf */
915 err = ext4_ext_get_access(handle, inode, path + depth);
918 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
919 err = ext4_ext_dirty(handle, inode, path + depth);
925 /* create intermediate indexes */
929 ext_debug("create %d intermediate indices\n", k);
930 /* insert new index into current index block */
931 /* current depth stored in i var */
935 newblock = ablocks[--a];
936 bh = sb_getblk(inode->i_sb, newblock);
943 err = ext4_journal_get_create_access(handle, bh);
947 neh = ext_block_hdr(bh);
948 neh->eh_entries = cpu_to_le16(1);
949 neh->eh_magic = EXT4_EXT_MAGIC;
950 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
951 neh->eh_depth = cpu_to_le16(depth - i);
952 fidx = EXT_FIRST_INDEX(neh);
953 fidx->ei_block = border;
954 ext4_idx_store_pblock(fidx, oldblock);
956 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
957 i, newblock, le32_to_cpu(border), oldblock);
962 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
963 EXT_MAX_INDEX(path[i].p_hdr));
964 BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) !=
965 EXT_LAST_INDEX(path[i].p_hdr));
966 while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
967 ext_debug("%d: move %d:%llu in new index %llu\n", i,
968 le32_to_cpu(path[i].p_idx->ei_block),
969 idx_pblock(path[i].p_idx),
971 /*memmove(++fidx, path[i].p_idx++,
972 sizeof(struct ext4_extent_idx));
974 BUG_ON(neh->eh_entries > neh->eh_max);*/
979 memmove(++fidx, path[i].p_idx - m,
980 sizeof(struct ext4_extent_idx) * m);
981 le16_add_cpu(&neh->eh_entries, m);
983 set_buffer_uptodate(bh);
986 err = ext4_handle_dirty_metadata(handle, inode, bh);
992 /* correct old index */
994 err = ext4_ext_get_access(handle, inode, path + i);
997 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
998 err = ext4_ext_dirty(handle, inode, path + i);
1006 /* insert new index */
1007 err = ext4_ext_insert_index(handle, inode, path + at,
1008 le32_to_cpu(border), newblock);
1012 if (buffer_locked(bh))
1018 /* free all allocated blocks in error case */
1019 for (i = 0; i < depth; i++) {
1022 ext4_free_blocks(handle, inode, ablocks[i], 1, 1);
1031 * ext4_ext_grow_indepth:
1032 * implements tree growing procedure:
1033 * - allocates new block
1034 * - moves top-level data (index block or leaf) into the new block
1035 * - initializes new top-level, creating index that points to the
1036 * just created block
1038 static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
1039 struct ext4_ext_path *path,
1040 struct ext4_extent *newext)
1042 struct ext4_ext_path *curp = path;
1043 struct ext4_extent_header *neh;
1044 struct ext4_extent_idx *fidx;
1045 struct buffer_head *bh;
1046 ext4_fsblk_t newblock;
1049 newblock = ext4_ext_new_meta_block(handle, inode, path, newext, &err);
1053 bh = sb_getblk(inode->i_sb, newblock);
1056 ext4_std_error(inode->i_sb, err);
1061 err = ext4_journal_get_create_access(handle, bh);
1067 /* move top-level index/leaf into new block */
1068 memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
1070 /* set size of new block */
1071 neh = ext_block_hdr(bh);
1072 /* old root could have indexes or leaves
1073 * so calculate e_max right way */
1074 if (ext_depth(inode))
1075 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
1077 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
1078 neh->eh_magic = EXT4_EXT_MAGIC;
1079 set_buffer_uptodate(bh);
1082 err = ext4_handle_dirty_metadata(handle, inode, bh);
1086 /* create index in new top-level index: num,max,pointer */
1087 err = ext4_ext_get_access(handle, inode, curp);
1091 curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
1092 curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
1093 curp->p_hdr->eh_entries = cpu_to_le16(1);
1094 curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
1096 if (path[0].p_hdr->eh_depth)
1097 curp->p_idx->ei_block =
1098 EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
1100 curp->p_idx->ei_block =
1101 EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
1102 ext4_idx_store_pblock(curp->p_idx, newblock);
1104 neh = ext_inode_hdr(inode);
1105 fidx = EXT_FIRST_INDEX(neh);
1106 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
1107 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
1108 le32_to_cpu(fidx->ei_block), idx_pblock(fidx));
1110 neh->eh_depth = cpu_to_le16(path->p_depth + 1);
1111 err = ext4_ext_dirty(handle, inode, curp);
1119 * ext4_ext_create_new_leaf:
1120 * finds empty index and adds new leaf.
1121 * if no free index is found, then it requests in-depth growing.
1123 static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
1124 struct ext4_ext_path *path,
1125 struct ext4_extent *newext)
1127 struct ext4_ext_path *curp;
1128 int depth, i, err = 0;
1131 i = depth = ext_depth(inode);
1133 /* walk up to the tree and look for free index entry */
1134 curp = path + depth;
1135 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1140 /* we use already allocated block for index block,
1141 * so subsequent data blocks should be contiguous */
1142 if (EXT_HAS_FREE_INDEX(curp)) {
1143 /* if we found index with free entry, then use that
1144 * entry: create all needed subtree and add new leaf */
1145 err = ext4_ext_split(handle, inode, path, newext, i);
1150 ext4_ext_drop_refs(path);
1151 path = ext4_ext_find_extent(inode,
1152 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1155 err = PTR_ERR(path);
1157 /* tree is full, time to grow in depth */
1158 err = ext4_ext_grow_indepth(handle, inode, path, newext);
1163 ext4_ext_drop_refs(path);
1164 path = ext4_ext_find_extent(inode,
1165 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1168 err = PTR_ERR(path);
1173 * only first (depth 0 -> 1) produces free space;
1174 * in all other cases we have to split the grown tree
1176 depth = ext_depth(inode);
1177 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
1178 /* now we need to split */
1188 * search the closest allocated block to the left for *logical
1189 * and returns it at @logical + it's physical address at @phys
1190 * if *logical is the smallest allocated block, the function
1191 * returns 0 at @phys
1192 * return value contains 0 (success) or error code
1195 ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path,
1196 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1198 struct ext4_extent_idx *ix;
1199 struct ext4_extent *ex;
1202 BUG_ON(path == NULL);
1203 depth = path->p_depth;
1206 if (depth == 0 && path->p_ext == NULL)
1209 /* usually extent in the path covers blocks smaller
1210 * then *logical, but it can be that extent is the
1211 * first one in the file */
1213 ex = path[depth].p_ext;
1214 ee_len = ext4_ext_get_actual_len(ex);
1215 if (*logical < le32_to_cpu(ex->ee_block)) {
1216 BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
1217 while (--depth >= 0) {
1218 ix = path[depth].p_idx;
1219 BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
1224 BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
1226 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
1227 *phys = ext_pblock(ex) + ee_len - 1;
1232 * search the closest allocated block to the right for *logical
1233 * and returns it at @logical + it's physical address at @phys
1234 * if *logical is the smallest allocated block, the function
1235 * returns 0 at @phys
1236 * return value contains 0 (success) or error code
1239 ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path,
1240 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1242 struct buffer_head *bh = NULL;
1243 struct ext4_extent_header *eh;
1244 struct ext4_extent_idx *ix;
1245 struct ext4_extent *ex;
1247 int depth; /* Note, NOT eh_depth; depth from top of tree */
1250 BUG_ON(path == NULL);
1251 depth = path->p_depth;
1254 if (depth == 0 && path->p_ext == NULL)
1257 /* usually extent in the path covers blocks smaller
1258 * then *logical, but it can be that extent is the
1259 * first one in the file */
1261 ex = path[depth].p_ext;
1262 ee_len = ext4_ext_get_actual_len(ex);
1263 if (*logical < le32_to_cpu(ex->ee_block)) {
1264 BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
1265 while (--depth >= 0) {
1266 ix = path[depth].p_idx;
1267 BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
1269 *logical = le32_to_cpu(ex->ee_block);
1270 *phys = ext_pblock(ex);
1274 BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
1276 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1277 /* next allocated block in this leaf */
1279 *logical = le32_to_cpu(ex->ee_block);
1280 *phys = ext_pblock(ex);
1284 /* go up and search for index to the right */
1285 while (--depth >= 0) {
1286 ix = path[depth].p_idx;
1287 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
1291 /* we've gone up to the root and found no index to the right */
1295 /* we've found index to the right, let's
1296 * follow it and find the closest allocated
1297 * block to the right */
1299 block = idx_pblock(ix);
1300 while (++depth < path->p_depth) {
1301 bh = sb_bread(inode->i_sb, block);
1304 eh = ext_block_hdr(bh);
1305 /* subtract from p_depth to get proper eh_depth */
1306 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
1310 ix = EXT_FIRST_INDEX(eh);
1311 block = idx_pblock(ix);
1315 bh = sb_bread(inode->i_sb, block);
1318 eh = ext_block_hdr(bh);
1319 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
1323 ex = EXT_FIRST_EXTENT(eh);
1324 *logical = le32_to_cpu(ex->ee_block);
1325 *phys = ext_pblock(ex);
1331 * ext4_ext_next_allocated_block:
1332 * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1333 * NOTE: it considers block number from index entry as
1334 * allocated block. Thus, index entries have to be consistent
1338 ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1342 BUG_ON(path == NULL);
1343 depth = path->p_depth;
1345 if (depth == 0 && path->p_ext == NULL)
1346 return EXT_MAX_BLOCK;
1348 while (depth >= 0) {
1349 if (depth == path->p_depth) {
1351 if (path[depth].p_ext !=
1352 EXT_LAST_EXTENT(path[depth].p_hdr))
1353 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1356 if (path[depth].p_idx !=
1357 EXT_LAST_INDEX(path[depth].p_hdr))
1358 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1363 return EXT_MAX_BLOCK;
1367 * ext4_ext_next_leaf_block:
1368 * returns first allocated block from next leaf or EXT_MAX_BLOCK
1370 static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode,
1371 struct ext4_ext_path *path)
1375 BUG_ON(path == NULL);
1376 depth = path->p_depth;
1378 /* zero-tree has no leaf blocks at all */
1380 return EXT_MAX_BLOCK;
1382 /* go to index block */
1385 while (depth >= 0) {
1386 if (path[depth].p_idx !=
1387 EXT_LAST_INDEX(path[depth].p_hdr))
1388 return (ext4_lblk_t)
1389 le32_to_cpu(path[depth].p_idx[1].ei_block);
1393 return EXT_MAX_BLOCK;
1397 * ext4_ext_correct_indexes:
1398 * if leaf gets modified and modified extent is first in the leaf,
1399 * then we have to correct all indexes above.
1400 * TODO: do we need to correct tree in all cases?
1402 static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
1403 struct ext4_ext_path *path)
1405 struct ext4_extent_header *eh;
1406 int depth = ext_depth(inode);
1407 struct ext4_extent *ex;
1411 eh = path[depth].p_hdr;
1412 ex = path[depth].p_ext;
1417 /* there is no tree at all */
1421 if (ex != EXT_FIRST_EXTENT(eh)) {
1422 /* we correct tree if first leaf got modified only */
1427 * TODO: we need correction if border is smaller than current one
1430 border = path[depth].p_ext->ee_block;
1431 err = ext4_ext_get_access(handle, inode, path + k);
1434 path[k].p_idx->ei_block = border;
1435 err = ext4_ext_dirty(handle, inode, path + k);
1440 /* change all left-side indexes */
1441 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1443 err = ext4_ext_get_access(handle, inode, path + k);
1446 path[k].p_idx->ei_block = border;
1447 err = ext4_ext_dirty(handle, inode, path + k);
1456 ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1457 struct ext4_extent *ex2)
1459 unsigned short ext1_ee_len, ext2_ee_len, max_len;
1462 * Make sure that either both extents are uninitialized, or
1465 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1468 if (ext4_ext_is_uninitialized(ex1))
1469 max_len = EXT_UNINIT_MAX_LEN;
1471 max_len = EXT_INIT_MAX_LEN;
1473 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1474 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1476 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
1477 le32_to_cpu(ex2->ee_block))
1481 * To allow future support for preallocated extents to be added
1482 * as an RO_COMPAT feature, refuse to merge to extents if
1483 * this can result in the top bit of ee_len being set.
1485 if (ext1_ee_len + ext2_ee_len > max_len)
1487 #ifdef AGGRESSIVE_TEST
1488 if (ext1_ee_len >= 4)
1492 if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2))
1498 * This function tries to merge the "ex" extent to the next extent in the tree.
1499 * It always tries to merge towards right. If you want to merge towards
1500 * left, pass "ex - 1" as argument instead of "ex".
1501 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1502 * 1 if they got merged.
1504 int ext4_ext_try_to_merge(struct inode *inode,
1505 struct ext4_ext_path *path,
1506 struct ext4_extent *ex)
1508 struct ext4_extent_header *eh;
1509 unsigned int depth, len;
1511 int uninitialized = 0;
1513 depth = ext_depth(inode);
1514 BUG_ON(path[depth].p_hdr == NULL);
1515 eh = path[depth].p_hdr;
1517 while (ex < EXT_LAST_EXTENT(eh)) {
1518 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1520 /* merge with next extent! */
1521 if (ext4_ext_is_uninitialized(ex))
1523 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1524 + ext4_ext_get_actual_len(ex + 1));
1526 ext4_ext_mark_uninitialized(ex);
1528 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1529 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1530 * sizeof(struct ext4_extent);
1531 memmove(ex + 1, ex + 2, len);
1533 le16_add_cpu(&eh->eh_entries, -1);
1535 WARN_ON(eh->eh_entries == 0);
1536 if (!eh->eh_entries)
1537 ext4_error(inode->i_sb, "ext4_ext_try_to_merge",
1538 "inode#%lu, eh->eh_entries = 0!", inode->i_ino);
1545 * check if a portion of the "newext" extent overlaps with an
1548 * If there is an overlap discovered, it updates the length of the newext
1549 * such that there will be no overlap, and then returns 1.
1550 * If there is no overlap found, it returns 0.
1552 unsigned int ext4_ext_check_overlap(struct inode *inode,
1553 struct ext4_extent *newext,
1554 struct ext4_ext_path *path)
1557 unsigned int depth, len1;
1558 unsigned int ret = 0;
1560 b1 = le32_to_cpu(newext->ee_block);
1561 len1 = ext4_ext_get_actual_len(newext);
1562 depth = ext_depth(inode);
1563 if (!path[depth].p_ext)
1565 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
1568 * get the next allocated block if the extent in the path
1569 * is before the requested block(s)
1572 b2 = ext4_ext_next_allocated_block(path);
1573 if (b2 == EXT_MAX_BLOCK)
1577 /* check for wrap through zero on extent logical start block*/
1578 if (b1 + len1 < b1) {
1579 len1 = EXT_MAX_BLOCK - b1;
1580 newext->ee_len = cpu_to_le16(len1);
1584 /* check for overlap */
1585 if (b1 + len1 > b2) {
1586 newext->ee_len = cpu_to_le16(b2 - b1);
1594 * ext4_ext_insert_extent:
1595 * tries to merge requsted extent into the existing extent or
1596 * inserts requested extent as new one into the tree,
1597 * creating new leaf in the no-space case.
1599 int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1600 struct ext4_ext_path *path,
1601 struct ext4_extent *newext, int flag)
1603 struct ext4_extent_header *eh;
1604 struct ext4_extent *ex, *fex;
1605 struct ext4_extent *nearex; /* nearest extent */
1606 struct ext4_ext_path *npath = NULL;
1607 int depth, len, err;
1609 unsigned uninitialized = 0;
1611 BUG_ON(ext4_ext_get_actual_len(newext) == 0);
1612 depth = ext_depth(inode);
1613 ex = path[depth].p_ext;
1614 BUG_ON(path[depth].p_hdr == NULL);
1616 /* try to insert block into found extent and return */
1617 if (ex && (flag != EXT4_GET_BLOCKS_DIO_CREATE_EXT)
1618 && ext4_can_extents_be_merged(inode, ex, newext)) {
1619 ext_debug("append [%d]%d block to %d:[%d]%d (from %llu)\n",
1620 ext4_ext_is_uninitialized(newext),
1621 ext4_ext_get_actual_len(newext),
1622 le32_to_cpu(ex->ee_block),
1623 ext4_ext_is_uninitialized(ex),
1624 ext4_ext_get_actual_len(ex), ext_pblock(ex));
1625 err = ext4_ext_get_access(handle, inode, path + depth);
1630 * ext4_can_extents_be_merged should have checked that either
1631 * both extents are uninitialized, or both aren't. Thus we
1632 * need to check only one of them here.
1634 if (ext4_ext_is_uninitialized(ex))
1636 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1637 + ext4_ext_get_actual_len(newext));
1639 ext4_ext_mark_uninitialized(ex);
1640 eh = path[depth].p_hdr;
1646 depth = ext_depth(inode);
1647 eh = path[depth].p_hdr;
1648 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1651 /* probably next leaf has space for us? */
1652 fex = EXT_LAST_EXTENT(eh);
1653 next = ext4_ext_next_leaf_block(inode, path);
1654 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
1655 && next != EXT_MAX_BLOCK) {
1656 ext_debug("next leaf block - %d\n", next);
1657 BUG_ON(npath != NULL);
1658 npath = ext4_ext_find_extent(inode, next, NULL);
1660 return PTR_ERR(npath);
1661 BUG_ON(npath->p_depth != path->p_depth);
1662 eh = npath[depth].p_hdr;
1663 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
1664 ext_debug("next leaf isnt full(%d)\n",
1665 le16_to_cpu(eh->eh_entries));
1669 ext_debug("next leaf has no free space(%d,%d)\n",
1670 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1674 * There is no free space in the found leaf.
1675 * We're gonna add a new leaf in the tree.
1677 err = ext4_ext_create_new_leaf(handle, inode, path, newext);
1680 depth = ext_depth(inode);
1681 eh = path[depth].p_hdr;
1684 nearex = path[depth].p_ext;
1686 err = ext4_ext_get_access(handle, inode, path + depth);
1691 /* there is no extent in this leaf, create first one */
1692 ext_debug("first extent in the leaf: %d:%llu:[%d]%d\n",
1693 le32_to_cpu(newext->ee_block),
1695 ext4_ext_is_uninitialized(newext),
1696 ext4_ext_get_actual_len(newext));
1697 path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1698 } else if (le32_to_cpu(newext->ee_block)
1699 > le32_to_cpu(nearex->ee_block)) {
1700 /* BUG_ON(newext->ee_block == nearex->ee_block); */
1701 if (nearex != EXT_LAST_EXTENT(eh)) {
1702 len = EXT_MAX_EXTENT(eh) - nearex;
1703 len = (len - 1) * sizeof(struct ext4_extent);
1704 len = len < 0 ? 0 : len;
1705 ext_debug("insert %d:%llu:[%d]%d after: nearest 0x%p, "
1706 "move %d from 0x%p to 0x%p\n",
1707 le32_to_cpu(newext->ee_block),
1709 ext4_ext_is_uninitialized(newext),
1710 ext4_ext_get_actual_len(newext),
1711 nearex, len, nearex + 1, nearex + 2);
1712 memmove(nearex + 2, nearex + 1, len);
1714 path[depth].p_ext = nearex + 1;
1716 BUG_ON(newext->ee_block == nearex->ee_block);
1717 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1718 len = len < 0 ? 0 : len;
1719 ext_debug("insert %d:%llu:[%d]%d before: nearest 0x%p, "
1720 "move %d from 0x%p to 0x%p\n",
1721 le32_to_cpu(newext->ee_block),
1723 ext4_ext_is_uninitialized(newext),
1724 ext4_ext_get_actual_len(newext),
1725 nearex, len, nearex + 1, nearex + 2);
1726 memmove(nearex + 1, nearex, len);
1727 path[depth].p_ext = nearex;
1730 le16_add_cpu(&eh->eh_entries, 1);
1731 nearex = path[depth].p_ext;
1732 nearex->ee_block = newext->ee_block;
1733 ext4_ext_store_pblock(nearex, ext_pblock(newext));
1734 nearex->ee_len = newext->ee_len;
1737 /* try to merge extents to the right */
1738 if (flag != EXT4_GET_BLOCKS_DIO_CREATE_EXT)
1739 ext4_ext_try_to_merge(inode, path, nearex);
1741 /* try to merge extents to the left */
1743 /* time to correct all indexes above */
1744 err = ext4_ext_correct_indexes(handle, inode, path);
1748 err = ext4_ext_dirty(handle, inode, path + depth);
1752 ext4_ext_drop_refs(npath);
1755 ext4_ext_invalidate_cache(inode);
1759 int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block,
1760 ext4_lblk_t num, ext_prepare_callback func,
1763 struct ext4_ext_path *path = NULL;
1764 struct ext4_ext_cache cbex;
1765 struct ext4_extent *ex;
1766 ext4_lblk_t next, start = 0, end = 0;
1767 ext4_lblk_t last = block + num;
1768 int depth, exists, err = 0;
1770 BUG_ON(func == NULL);
1771 BUG_ON(inode == NULL);
1773 while (block < last && block != EXT_MAX_BLOCK) {
1775 /* find extent for this block */
1776 down_read(&EXT4_I(inode)->i_data_sem);
1777 path = ext4_ext_find_extent(inode, block, path);
1778 up_read(&EXT4_I(inode)->i_data_sem);
1780 err = PTR_ERR(path);
1785 depth = ext_depth(inode);
1786 BUG_ON(path[depth].p_hdr == NULL);
1787 ex = path[depth].p_ext;
1788 next = ext4_ext_next_allocated_block(path);
1792 /* there is no extent yet, so try to allocate
1793 * all requested space */
1796 } else if (le32_to_cpu(ex->ee_block) > block) {
1797 /* need to allocate space before found extent */
1799 end = le32_to_cpu(ex->ee_block);
1800 if (block + num < end)
1802 } else if (block >= le32_to_cpu(ex->ee_block)
1803 + ext4_ext_get_actual_len(ex)) {
1804 /* need to allocate space after found extent */
1809 } else if (block >= le32_to_cpu(ex->ee_block)) {
1811 * some part of requested space is covered
1815 end = le32_to_cpu(ex->ee_block)
1816 + ext4_ext_get_actual_len(ex);
1817 if (block + num < end)
1823 BUG_ON(end <= start);
1826 cbex.ec_block = start;
1827 cbex.ec_len = end - start;
1829 cbex.ec_type = EXT4_EXT_CACHE_GAP;
1831 cbex.ec_block = le32_to_cpu(ex->ee_block);
1832 cbex.ec_len = ext4_ext_get_actual_len(ex);
1833 cbex.ec_start = ext_pblock(ex);
1834 cbex.ec_type = EXT4_EXT_CACHE_EXTENT;
1837 BUG_ON(cbex.ec_len == 0);
1838 err = func(inode, path, &cbex, ex, cbdata);
1839 ext4_ext_drop_refs(path);
1844 if (err == EXT_REPEAT)
1846 else if (err == EXT_BREAK) {
1851 if (ext_depth(inode) != depth) {
1852 /* depth was changed. we have to realloc path */
1857 block = cbex.ec_block + cbex.ec_len;
1861 ext4_ext_drop_refs(path);
1869 ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
1870 __u32 len, ext4_fsblk_t start, int type)
1872 struct ext4_ext_cache *cex;
1874 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1875 cex = &EXT4_I(inode)->i_cached_extent;
1876 cex->ec_type = type;
1877 cex->ec_block = block;
1879 cex->ec_start = start;
1880 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1884 * ext4_ext_put_gap_in_cache:
1885 * calculate boundaries of the gap that the requested block fits into
1886 * and cache this gap
1889 ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
1892 int depth = ext_depth(inode);
1895 struct ext4_extent *ex;
1897 ex = path[depth].p_ext;
1899 /* there is no extent yet, so gap is [0;-] */
1901 len = EXT_MAX_BLOCK;
1902 ext_debug("cache gap(whole file):");
1903 } else if (block < le32_to_cpu(ex->ee_block)) {
1905 len = le32_to_cpu(ex->ee_block) - block;
1906 ext_debug("cache gap(before): %u [%u:%u]",
1908 le32_to_cpu(ex->ee_block),
1909 ext4_ext_get_actual_len(ex));
1910 } else if (block >= le32_to_cpu(ex->ee_block)
1911 + ext4_ext_get_actual_len(ex)) {
1913 lblock = le32_to_cpu(ex->ee_block)
1914 + ext4_ext_get_actual_len(ex);
1916 next = ext4_ext_next_allocated_block(path);
1917 ext_debug("cache gap(after): [%u:%u] %u",
1918 le32_to_cpu(ex->ee_block),
1919 ext4_ext_get_actual_len(ex),
1921 BUG_ON(next == lblock);
1922 len = next - lblock;
1928 ext_debug(" -> %u:%lu\n", lblock, len);
1929 ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
1933 ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
1934 struct ext4_extent *ex)
1936 struct ext4_ext_cache *cex;
1937 int ret = EXT4_EXT_CACHE_NO;
1940 * We borrow i_block_reservation_lock to protect i_cached_extent
1942 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1943 cex = &EXT4_I(inode)->i_cached_extent;
1945 /* has cache valid data? */
1946 if (cex->ec_type == EXT4_EXT_CACHE_NO)
1949 BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
1950 cex->ec_type != EXT4_EXT_CACHE_EXTENT);
1951 if (in_range(block, cex->ec_block, cex->ec_len)) {
1952 ex->ee_block = cpu_to_le32(cex->ec_block);
1953 ext4_ext_store_pblock(ex, cex->ec_start);
1954 ex->ee_len = cpu_to_le16(cex->ec_len);
1955 ext_debug("%u cached by %u:%u:%llu\n",
1957 cex->ec_block, cex->ec_len, cex->ec_start);
1961 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1967 * removes index from the index block.
1968 * It's used in truncate case only, thus all requests are for
1969 * last index in the block only.
1971 static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
1972 struct ext4_ext_path *path)
1974 struct buffer_head *bh;
1978 /* free index block */
1980 leaf = idx_pblock(path->p_idx);
1981 BUG_ON(path->p_hdr->eh_entries == 0);
1982 err = ext4_ext_get_access(handle, inode, path);
1985 le16_add_cpu(&path->p_hdr->eh_entries, -1);
1986 err = ext4_ext_dirty(handle, inode, path);
1989 ext_debug("index is empty, remove it, free block %llu\n", leaf);
1990 bh = sb_find_get_block(inode->i_sb, leaf);
1991 ext4_forget(handle, 1, inode, bh, leaf);
1992 ext4_free_blocks(handle, inode, leaf, 1, 1);
1997 * ext4_ext_calc_credits_for_single_extent:
1998 * This routine returns max. credits that needed to insert an extent
1999 * to the extent tree.
2000 * When pass the actual path, the caller should calculate credits
2003 int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
2004 struct ext4_ext_path *path)
2007 int depth = ext_depth(inode);
2010 /* probably there is space in leaf? */
2011 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
2012 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2015 * There are some space in the leaf tree, no
2016 * need to account for leaf block credit
2018 * bitmaps and block group descriptor blocks
2019 * and other metadat blocks still need to be
2022 /* 1 bitmap, 1 block group descriptor */
2023 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
2028 return ext4_chunk_trans_blocks(inode, nrblocks);
2032 * How many index/leaf blocks need to change/allocate to modify nrblocks?
2034 * if nrblocks are fit in a single extent (chunk flag is 1), then
2035 * in the worse case, each tree level index/leaf need to be changed
2036 * if the tree split due to insert a new extent, then the old tree
2037 * index/leaf need to be updated too
2039 * If the nrblocks are discontiguous, they could cause
2040 * the whole tree split more than once, but this is really rare.
2042 int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
2045 int depth = ext_depth(inode);
2055 static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
2056 struct ext4_extent *ex,
2057 ext4_lblk_t from, ext4_lblk_t to)
2059 struct buffer_head *bh;
2060 unsigned short ee_len = ext4_ext_get_actual_len(ex);
2061 int i, metadata = 0;
2063 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2065 #ifdef EXTENTS_STATS
2067 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2068 spin_lock(&sbi->s_ext_stats_lock);
2069 sbi->s_ext_blocks += ee_len;
2070 sbi->s_ext_extents++;
2071 if (ee_len < sbi->s_ext_min)
2072 sbi->s_ext_min = ee_len;
2073 if (ee_len > sbi->s_ext_max)
2074 sbi->s_ext_max = ee_len;
2075 if (ext_depth(inode) > sbi->s_depth_max)
2076 sbi->s_depth_max = ext_depth(inode);
2077 spin_unlock(&sbi->s_ext_stats_lock);
2080 if (from >= le32_to_cpu(ex->ee_block)
2081 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
2086 num = le32_to_cpu(ex->ee_block) + ee_len - from;
2087 start = ext_pblock(ex) + ee_len - num;
2088 ext_debug("free last %u blocks starting %llu\n", num, start);
2089 for (i = 0; i < num; i++) {
2090 bh = sb_find_get_block(inode->i_sb, start + i);
2091 ext4_forget(handle, metadata, inode, bh, start + i);
2093 ext4_free_blocks(handle, inode, start, num, metadata);
2094 } else if (from == le32_to_cpu(ex->ee_block)
2095 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
2096 printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n",
2097 from, to, le32_to_cpu(ex->ee_block), ee_len);
2099 printk(KERN_INFO "strange request: removal(2) "
2100 "%u-%u from %u:%u\n",
2101 from, to, le32_to_cpu(ex->ee_block), ee_len);
2107 ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
2108 struct ext4_ext_path *path, ext4_lblk_t start)
2110 int err = 0, correct_index = 0;
2111 int depth = ext_depth(inode), credits;
2112 struct ext4_extent_header *eh;
2113 ext4_lblk_t a, b, block;
2115 ext4_lblk_t ex_ee_block;
2116 unsigned short ex_ee_len;
2117 unsigned uninitialized = 0;
2118 struct ext4_extent *ex;
2120 /* the header must be checked already in ext4_ext_remove_space() */
2121 ext_debug("truncate since %u in leaf\n", start);
2122 if (!path[depth].p_hdr)
2123 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2124 eh = path[depth].p_hdr;
2127 /* find where to start removing */
2128 ex = EXT_LAST_EXTENT(eh);
2130 ex_ee_block = le32_to_cpu(ex->ee_block);
2131 ex_ee_len = ext4_ext_get_actual_len(ex);
2133 while (ex >= EXT_FIRST_EXTENT(eh) &&
2134 ex_ee_block + ex_ee_len > start) {
2136 if (ext4_ext_is_uninitialized(ex))
2141 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
2142 uninitialized, ex_ee_len);
2143 path[depth].p_ext = ex;
2145 a = ex_ee_block > start ? ex_ee_block : start;
2146 b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
2147 ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
2149 ext_debug(" border %u:%u\n", a, b);
2151 if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
2155 } else if (a != ex_ee_block) {
2156 /* remove tail of the extent */
2157 block = ex_ee_block;
2159 } else if (b != ex_ee_block + ex_ee_len - 1) {
2160 /* remove head of the extent */
2163 /* there is no "make a hole" API yet */
2166 /* remove whole extent: excellent! */
2167 block = ex_ee_block;
2169 BUG_ON(a != ex_ee_block);
2170 BUG_ON(b != ex_ee_block + ex_ee_len - 1);
2174 * 3 for leaf, sb, and inode plus 2 (bmap and group
2175 * descriptor) for each block group; assume two block
2176 * groups plus ex_ee_len/blocks_per_block_group for
2179 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
2180 if (ex == EXT_FIRST_EXTENT(eh)) {
2182 credits += (ext_depth(inode)) + 1;
2184 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
2186 err = ext4_ext_truncate_extend_restart(handle, inode, credits);
2190 err = ext4_ext_get_access(handle, inode, path + depth);
2194 err = ext4_remove_blocks(handle, inode, ex, a, b);
2199 /* this extent is removed; mark slot entirely unused */
2200 ext4_ext_store_pblock(ex, 0);
2201 le16_add_cpu(&eh->eh_entries, -1);
2204 ex->ee_block = cpu_to_le32(block);
2205 ex->ee_len = cpu_to_le16(num);
2207 * Do not mark uninitialized if all the blocks in the
2208 * extent have been removed.
2210 if (uninitialized && num)
2211 ext4_ext_mark_uninitialized(ex);
2213 err = ext4_ext_dirty(handle, inode, path + depth);
2217 ext_debug("new extent: %u:%u:%llu\n", block, num,
2220 ex_ee_block = le32_to_cpu(ex->ee_block);
2221 ex_ee_len = ext4_ext_get_actual_len(ex);
2224 if (correct_index && eh->eh_entries)
2225 err = ext4_ext_correct_indexes(handle, inode, path);
2227 /* if this leaf is free, then we should
2228 * remove it from index block above */
2229 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
2230 err = ext4_ext_rm_idx(handle, inode, path + depth);
2237 * ext4_ext_more_to_rm:
2238 * returns 1 if current index has to be freed (even partial)
2241 ext4_ext_more_to_rm(struct ext4_ext_path *path)
2243 BUG_ON(path->p_idx == NULL);
2245 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2249 * if truncate on deeper level happened, it wasn't partial,
2250 * so we have to consider current index for truncation
2252 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2257 static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
2259 struct super_block *sb = inode->i_sb;
2260 int depth = ext_depth(inode);
2261 struct ext4_ext_path *path;
2265 ext_debug("truncate since %u\n", start);
2267 /* probably first extent we're gonna free will be last in block */
2268 handle = ext4_journal_start(inode, depth + 1);
2270 return PTR_ERR(handle);
2273 ext4_ext_invalidate_cache(inode);
2276 * We start scanning from right side, freeing all the blocks
2277 * after i_size and walking into the tree depth-wise.
2279 depth = ext_depth(inode);
2280 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
2282 ext4_journal_stop(handle);
2285 path[0].p_depth = depth;
2286 path[0].p_hdr = ext_inode_hdr(inode);
2287 if (ext4_ext_check(inode, path[0].p_hdr, depth)) {
2293 while (i >= 0 && err == 0) {
2295 /* this is leaf block */
2296 err = ext4_ext_rm_leaf(handle, inode, path, start);
2297 /* root level has p_bh == NULL, brelse() eats this */
2298 brelse(path[i].p_bh);
2299 path[i].p_bh = NULL;
2304 /* this is index block */
2305 if (!path[i].p_hdr) {
2306 ext_debug("initialize header\n");
2307 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
2310 if (!path[i].p_idx) {
2311 /* this level hasn't been touched yet */
2312 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2313 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2314 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2316 le16_to_cpu(path[i].p_hdr->eh_entries));
2318 /* we were already here, see at next index */
2322 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2323 i, EXT_FIRST_INDEX(path[i].p_hdr),
2325 if (ext4_ext_more_to_rm(path + i)) {
2326 struct buffer_head *bh;
2327 /* go to the next level */
2328 ext_debug("move to level %d (block %llu)\n",
2329 i + 1, idx_pblock(path[i].p_idx));
2330 memset(path + i + 1, 0, sizeof(*path));
2331 bh = sb_bread(sb, idx_pblock(path[i].p_idx));
2333 /* should we reset i_size? */
2337 if (WARN_ON(i + 1 > depth)) {
2341 if (ext4_ext_check(inode, ext_block_hdr(bh),
2346 path[i + 1].p_bh = bh;
2348 /* save actual number of indexes since this
2349 * number is changed at the next iteration */
2350 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2353 /* we finished processing this index, go up */
2354 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
2355 /* index is empty, remove it;
2356 * handle must be already prepared by the
2357 * truncatei_leaf() */
2358 err = ext4_ext_rm_idx(handle, inode, path + i);
2360 /* root level has p_bh == NULL, brelse() eats this */
2361 brelse(path[i].p_bh);
2362 path[i].p_bh = NULL;
2364 ext_debug("return to level %d\n", i);
2368 /* TODO: flexible tree reduction should be here */
2369 if (path->p_hdr->eh_entries == 0) {
2371 * truncate to zero freed all the tree,
2372 * so we need to correct eh_depth
2374 err = ext4_ext_get_access(handle, inode, path);
2376 ext_inode_hdr(inode)->eh_depth = 0;
2377 ext_inode_hdr(inode)->eh_max =
2378 cpu_to_le16(ext4_ext_space_root(inode, 0));
2379 err = ext4_ext_dirty(handle, inode, path);
2383 ext4_ext_drop_refs(path);
2387 ext4_journal_stop(handle);
2393 * called at mount time
2395 void ext4_ext_init(struct super_block *sb)
2398 * possible initialization would be here
2401 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
2402 #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
2403 printk(KERN_INFO "EXT4-fs: file extents enabled");
2404 #ifdef AGGRESSIVE_TEST
2405 printk(", aggressive tests");
2407 #ifdef CHECK_BINSEARCH
2408 printk(", check binsearch");
2410 #ifdef EXTENTS_STATS
2415 #ifdef EXTENTS_STATS
2416 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2417 EXT4_SB(sb)->s_ext_min = 1 << 30;
2418 EXT4_SB(sb)->s_ext_max = 0;
2424 * called at umount time
2426 void ext4_ext_release(struct super_block *sb)
2428 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
2431 #ifdef EXTENTS_STATS
2432 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2433 struct ext4_sb_info *sbi = EXT4_SB(sb);
2434 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2435 sbi->s_ext_blocks, sbi->s_ext_extents,
2436 sbi->s_ext_blocks / sbi->s_ext_extents);
2437 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2438 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2443 static void bi_complete(struct bio *bio, int error)
2445 complete((struct completion *)bio->bi_private);
2448 /* FIXME!! we need to try to merge to left or right after zero-out */
2449 static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2453 int blkbits, blocksize;
2455 struct completion event;
2456 unsigned int ee_len, len, done, offset;
2459 blkbits = inode->i_blkbits;
2460 blocksize = inode->i_sb->s_blocksize;
2461 ee_len = ext4_ext_get_actual_len(ex);
2462 ee_pblock = ext_pblock(ex);
2464 /* convert ee_pblock to 512 byte sectors */
2465 ee_pblock = ee_pblock << (blkbits - 9);
2467 while (ee_len > 0) {
2469 if (ee_len > BIO_MAX_PAGES)
2470 len = BIO_MAX_PAGES;
2474 bio = bio_alloc(GFP_NOIO, len);
2478 bio->bi_sector = ee_pblock;
2479 bio->bi_bdev = inode->i_sb->s_bdev;
2483 while (done < len) {
2484 ret = bio_add_page(bio, ZERO_PAGE(0),
2486 if (ret != blocksize) {
2488 * We can't add any more pages because of
2489 * hardware limitations. Start a new bio.
2494 offset += blocksize;
2495 if (offset >= PAGE_CACHE_SIZE)
2499 init_completion(&event);
2500 bio->bi_private = &event;
2501 bio->bi_end_io = bi_complete;
2502 submit_bio(WRITE, bio);
2503 wait_for_completion(&event);
2505 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
2511 ee_pblock += done << (blkbits - 9);
2516 #define EXT4_EXT_ZERO_LEN 7
2518 * This function is called by ext4_ext_get_blocks() if someone tries to write
2519 * to an uninitialized extent. It may result in splitting the uninitialized
2520 * extent into multiple extents (upto three - one initialized and two
2522 * There are three possibilities:
2523 * a> There is no split required: Entire extent should be initialized
2524 * b> Splits in two extents: Write is happening at either end of the extent
2525 * c> Splits in three extents: Somone is writing in middle of the extent
2527 static int ext4_ext_convert_to_initialized(handle_t *handle,
2528 struct inode *inode,
2529 struct ext4_ext_path *path,
2531 unsigned int max_blocks)
2533 struct ext4_extent *ex, newex, orig_ex;
2534 struct ext4_extent *ex1 = NULL;
2535 struct ext4_extent *ex2 = NULL;
2536 struct ext4_extent *ex3 = NULL;
2537 struct ext4_extent_header *eh;
2538 ext4_lblk_t ee_block, eof_block;
2539 unsigned int allocated, ee_len, depth;
2540 ext4_fsblk_t newblock;
2545 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
2546 "block %llu, max_blocks %u\n", inode->i_ino,
2547 (unsigned long long)iblock, max_blocks);
2549 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
2550 inode->i_sb->s_blocksize_bits;
2551 if (eof_block < iblock + max_blocks)
2552 eof_block = iblock + max_blocks;
2554 depth = ext_depth(inode);
2555 eh = path[depth].p_hdr;
2556 ex = path[depth].p_ext;
2557 ee_block = le32_to_cpu(ex->ee_block);
2558 ee_len = ext4_ext_get_actual_len(ex);
2559 allocated = ee_len - (iblock - ee_block);
2560 newblock = iblock - ee_block + ext_pblock(ex);
2563 orig_ex.ee_block = ex->ee_block;
2564 orig_ex.ee_len = cpu_to_le16(ee_len);
2565 ext4_ext_store_pblock(&orig_ex, ext_pblock(ex));
2568 * It is safe to convert extent to initialized via explicit
2569 * zeroout only if extent is fully insde i_size or new_size.
2571 may_zeroout = ee_block + ee_len <= eof_block;
2573 err = ext4_ext_get_access(handle, inode, path + depth);
2576 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
2577 if (ee_len <= 2*EXT4_EXT_ZERO_LEN && may_zeroout) {
2578 err = ext4_ext_zeroout(inode, &orig_ex);
2580 goto fix_extent_len;
2581 /* update the extent length and mark as initialized */
2582 ex->ee_block = orig_ex.ee_block;
2583 ex->ee_len = orig_ex.ee_len;
2584 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2585 ext4_ext_dirty(handle, inode, path + depth);
2586 /* zeroed the full extent */
2590 /* ex1: ee_block to iblock - 1 : uninitialized */
2591 if (iblock > ee_block) {
2593 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2594 ext4_ext_mark_uninitialized(ex1);
2598 * for sanity, update the length of the ex2 extent before
2599 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2600 * overlap of blocks.
2602 if (!ex1 && allocated > max_blocks)
2603 ex2->ee_len = cpu_to_le16(max_blocks);
2604 /* ex3: to ee_block + ee_len : uninitialised */
2605 if (allocated > max_blocks) {
2606 unsigned int newdepth;
2607 /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */
2608 if (allocated <= EXT4_EXT_ZERO_LEN && may_zeroout) {
2610 * iblock == ee_block is handled by the zerouout
2612 * Mark first half uninitialized.
2613 * Mark second half initialized and zero out the
2614 * initialized extent
2616 ex->ee_block = orig_ex.ee_block;
2617 ex->ee_len = cpu_to_le16(ee_len - allocated);
2618 ext4_ext_mark_uninitialized(ex);
2619 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2620 ext4_ext_dirty(handle, inode, path + depth);
2623 ex3->ee_block = cpu_to_le32(iblock);
2624 ext4_ext_store_pblock(ex3, newblock);
2625 ex3->ee_len = cpu_to_le16(allocated);
2626 err = ext4_ext_insert_extent(handle, inode, path,
2628 if (err == -ENOSPC) {
2629 err = ext4_ext_zeroout(inode, &orig_ex);
2631 goto fix_extent_len;
2632 ex->ee_block = orig_ex.ee_block;
2633 ex->ee_len = orig_ex.ee_len;
2634 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2635 ext4_ext_dirty(handle, inode, path + depth);
2636 /* blocks available from iblock */
2640 goto fix_extent_len;
2643 * We need to zero out the second half because
2644 * an fallocate request can update file size and
2645 * converting the second half to initialized extent
2646 * implies that we can leak some junk data to user
2649 err = ext4_ext_zeroout(inode, ex3);
2652 * We should actually mark the
2653 * second half as uninit and return error
2654 * Insert would have changed the extent
2656 depth = ext_depth(inode);
2657 ext4_ext_drop_refs(path);
2658 path = ext4_ext_find_extent(inode,
2661 err = PTR_ERR(path);
2664 /* get the second half extent details */
2665 ex = path[depth].p_ext;
2666 err = ext4_ext_get_access(handle, inode,
2670 ext4_ext_mark_uninitialized(ex);
2671 ext4_ext_dirty(handle, inode, path + depth);
2675 /* zeroed the second half */
2679 ex3->ee_block = cpu_to_le32(iblock + max_blocks);
2680 ext4_ext_store_pblock(ex3, newblock + max_blocks);
2681 ex3->ee_len = cpu_to_le16(allocated - max_blocks);
2682 ext4_ext_mark_uninitialized(ex3);
2683 err = ext4_ext_insert_extent(handle, inode, path, ex3, 0);
2684 if (err == -ENOSPC && may_zeroout) {
2685 err = ext4_ext_zeroout(inode, &orig_ex);
2687 goto fix_extent_len;
2688 /* update the extent length and mark as initialized */
2689 ex->ee_block = orig_ex.ee_block;
2690 ex->ee_len = orig_ex.ee_len;
2691 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2692 ext4_ext_dirty(handle, inode, path + depth);
2693 /* zeroed the full extent */
2694 /* blocks available from iblock */
2698 goto fix_extent_len;
2700 * The depth, and hence eh & ex might change
2701 * as part of the insert above.
2703 newdepth = ext_depth(inode);
2705 * update the extent length after successful insert of the
2708 ee_len -= ext4_ext_get_actual_len(ex3);
2709 orig_ex.ee_len = cpu_to_le16(ee_len);
2710 may_zeroout = ee_block + ee_len <= eof_block;
2713 ext4_ext_drop_refs(path);
2714 path = ext4_ext_find_extent(inode, iblock, path);
2716 err = PTR_ERR(path);
2719 eh = path[depth].p_hdr;
2720 ex = path[depth].p_ext;
2724 err = ext4_ext_get_access(handle, inode, path + depth);
2728 allocated = max_blocks;
2730 /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying
2731 * to insert a extent in the middle zerout directly
2732 * otherwise give the extent a chance to merge to left
2734 if (le16_to_cpu(orig_ex.ee_len) <= EXT4_EXT_ZERO_LEN &&
2735 iblock != ee_block && may_zeroout) {
2736 err = ext4_ext_zeroout(inode, &orig_ex);
2738 goto fix_extent_len;
2739 /* update the extent length and mark as initialized */
2740 ex->ee_block = orig_ex.ee_block;
2741 ex->ee_len = orig_ex.ee_len;
2742 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2743 ext4_ext_dirty(handle, inode, path + depth);
2744 /* zero out the first half */
2745 /* blocks available from iblock */
2750 * If there was a change of depth as part of the
2751 * insertion of ex3 above, we need to update the length
2752 * of the ex1 extent again here
2754 if (ex1 && ex1 != ex) {
2756 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2757 ext4_ext_mark_uninitialized(ex1);
2760 /* ex2: iblock to iblock + maxblocks-1 : initialised */
2761 ex2->ee_block = cpu_to_le32(iblock);
2762 ext4_ext_store_pblock(ex2, newblock);
2763 ex2->ee_len = cpu_to_le16(allocated);
2767 * New (initialized) extent starts from the first block
2768 * in the current extent. i.e., ex2 == ex
2769 * We have to see if it can be merged with the extent
2772 if (ex2 > EXT_FIRST_EXTENT(eh)) {
2774 * To merge left, pass "ex2 - 1" to try_to_merge(),
2775 * since it merges towards right _only_.
2777 ret = ext4_ext_try_to_merge(inode, path, ex2 - 1);
2779 err = ext4_ext_correct_indexes(handle, inode, path);
2782 depth = ext_depth(inode);
2787 * Try to Merge towards right. This might be required
2788 * only when the whole extent is being written to.
2789 * i.e. ex2 == ex and ex3 == NULL.
2792 ret = ext4_ext_try_to_merge(inode, path, ex2);
2794 err = ext4_ext_correct_indexes(handle, inode, path);
2799 /* Mark modified extent as dirty */
2800 err = ext4_ext_dirty(handle, inode, path + depth);
2803 err = ext4_ext_insert_extent(handle, inode, path, &newex, 0);
2804 if (err == -ENOSPC && may_zeroout) {
2805 err = ext4_ext_zeroout(inode, &orig_ex);
2807 goto fix_extent_len;
2808 /* update the extent length and mark as initialized */
2809 ex->ee_block = orig_ex.ee_block;
2810 ex->ee_len = orig_ex.ee_len;
2811 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2812 ext4_ext_dirty(handle, inode, path + depth);
2813 /* zero out the first half */
2816 goto fix_extent_len;
2818 ext4_ext_show_leaf(inode, path);
2819 return err ? err : allocated;
2822 ex->ee_block = orig_ex.ee_block;
2823 ex->ee_len = orig_ex.ee_len;
2824 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2825 ext4_ext_mark_uninitialized(ex);
2826 ext4_ext_dirty(handle, inode, path + depth);
2831 * This function is called by ext4_ext_get_blocks() from
2832 * ext4_get_blocks_dio_write() when DIO to write
2833 * to an uninitialized extent.
2835 * Writing to an uninitized extent may result in splitting the uninitialized
2836 * extent into multiple /intialized unintialized extents (up to three)
2837 * There are three possibilities:
2838 * a> There is no split required: Entire extent should be uninitialized
2839 * b> Splits in two extents: Write is happening at either end of the extent
2840 * c> Splits in three extents: Somone is writing in middle of the extent
2842 * One of more index blocks maybe needed if the extent tree grow after
2843 * the unintialized extent split. To prevent ENOSPC occur at the IO
2844 * complete, we need to split the uninitialized extent before DIO submit
2845 * the IO. The uninitilized extent called at this time will be split
2846 * into three uninitialized extent(at most). After IO complete, the part
2847 * being filled will be convert to initialized by the end_io callback function
2848 * via ext4_convert_unwritten_extents().
2850 * Returns the size of uninitialized extent to be written on success.
2852 static int ext4_split_unwritten_extents(handle_t *handle,
2853 struct inode *inode,
2854 struct ext4_ext_path *path,
2856 unsigned int max_blocks,
2859 struct ext4_extent *ex, newex, orig_ex;
2860 struct ext4_extent *ex1 = NULL;
2861 struct ext4_extent *ex2 = NULL;
2862 struct ext4_extent *ex3 = NULL;
2863 struct ext4_extent_header *eh;
2864 ext4_lblk_t ee_block, eof_block;
2865 unsigned int allocated, ee_len, depth;
2866 ext4_fsblk_t newblock;
2870 ext_debug("ext4_split_unwritten_extents: inode %lu, logical"
2871 "block %llu, max_blocks %u\n", inode->i_ino,
2872 (unsigned long long)iblock, max_blocks);
2874 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
2875 inode->i_sb->s_blocksize_bits;
2876 if (eof_block < iblock + max_blocks)
2877 eof_block = iblock + max_blocks;
2879 depth = ext_depth(inode);
2880 eh = path[depth].p_hdr;
2881 ex = path[depth].p_ext;
2882 ee_block = le32_to_cpu(ex->ee_block);
2883 ee_len = ext4_ext_get_actual_len(ex);
2884 allocated = ee_len - (iblock - ee_block);
2885 newblock = iblock - ee_block + ext_pblock(ex);
2888 orig_ex.ee_block = ex->ee_block;
2889 orig_ex.ee_len = cpu_to_le16(ee_len);
2890 ext4_ext_store_pblock(&orig_ex, ext_pblock(ex));
2893 * It is safe to convert extent to initialized via explicit
2894 * zeroout only if extent is fully insde i_size or new_size.
2896 may_zeroout = ee_block + ee_len <= eof_block;
2899 * If the uninitialized extent begins at the same logical
2900 * block where the write begins, and the write completely
2901 * covers the extent, then we don't need to split it.
2903 if ((iblock == ee_block) && (allocated <= max_blocks))
2906 err = ext4_ext_get_access(handle, inode, path + depth);
2909 /* ex1: ee_block to iblock - 1 : uninitialized */
2910 if (iblock > ee_block) {
2912 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2913 ext4_ext_mark_uninitialized(ex1);
2917 * for sanity, update the length of the ex2 extent before
2918 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2919 * overlap of blocks.
2921 if (!ex1 && allocated > max_blocks)
2922 ex2->ee_len = cpu_to_le16(max_blocks);
2923 /* ex3: to ee_block + ee_len : uninitialised */
2924 if (allocated > max_blocks) {
2925 unsigned int newdepth;
2927 ex3->ee_block = cpu_to_le32(iblock + max_blocks);
2928 ext4_ext_store_pblock(ex3, newblock + max_blocks);
2929 ex3->ee_len = cpu_to_le16(allocated - max_blocks);
2930 ext4_ext_mark_uninitialized(ex3);
2931 err = ext4_ext_insert_extent(handle, inode, path, ex3, flags);
2932 if (err == -ENOSPC && may_zeroout) {
2933 err = ext4_ext_zeroout(inode, &orig_ex);
2935 goto fix_extent_len;
2936 /* update the extent length and mark as initialized */
2937 ex->ee_block = orig_ex.ee_block;
2938 ex->ee_len = orig_ex.ee_len;
2939 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2940 ext4_ext_dirty(handle, inode, path + depth);
2941 /* zeroed the full extent */
2942 /* blocks available from iblock */
2946 goto fix_extent_len;
2948 * The depth, and hence eh & ex might change
2949 * as part of the insert above.
2951 newdepth = ext_depth(inode);
2953 * update the extent length after successful insert of the
2956 ee_len -= ext4_ext_get_actual_len(ex3);
2957 orig_ex.ee_len = cpu_to_le16(ee_len);
2958 may_zeroout = ee_block + ee_len <= eof_block;
2961 ext4_ext_drop_refs(path);
2962 path = ext4_ext_find_extent(inode, iblock, path);
2964 err = PTR_ERR(path);
2967 eh = path[depth].p_hdr;
2968 ex = path[depth].p_ext;
2972 err = ext4_ext_get_access(handle, inode, path + depth);
2976 allocated = max_blocks;
2979 * If there was a change of depth as part of the
2980 * insertion of ex3 above, we need to update the length
2981 * of the ex1 extent again here
2983 if (ex1 && ex1 != ex) {
2985 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2986 ext4_ext_mark_uninitialized(ex1);
2990 * ex2: iblock to iblock + maxblocks-1 : to be direct IO written,
2991 * uninitialised still.
2993 ex2->ee_block = cpu_to_le32(iblock);
2994 ext4_ext_store_pblock(ex2, newblock);
2995 ex2->ee_len = cpu_to_le16(allocated);
2996 ext4_ext_mark_uninitialized(ex2);
2999 /* Mark modified extent as dirty */
3000 err = ext4_ext_dirty(handle, inode, path + depth);
3001 ext_debug("out here\n");
3004 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
3005 if (err == -ENOSPC && may_zeroout) {
3006 err = ext4_ext_zeroout(inode, &orig_ex);
3008 goto fix_extent_len;
3009 /* update the extent length and mark as initialized */
3010 ex->ee_block = orig_ex.ee_block;
3011 ex->ee_len = orig_ex.ee_len;
3012 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
3013 ext4_ext_dirty(handle, inode, path + depth);
3014 /* zero out the first half */
3017 goto fix_extent_len;
3019 ext4_ext_show_leaf(inode, path);
3020 return err ? err : allocated;
3023 ex->ee_block = orig_ex.ee_block;
3024 ex->ee_len = orig_ex.ee_len;
3025 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
3026 ext4_ext_mark_uninitialized(ex);
3027 ext4_ext_dirty(handle, inode, path + depth);
3030 static int ext4_convert_unwritten_extents_dio(handle_t *handle,
3031 struct inode *inode,
3032 struct ext4_ext_path *path)
3034 struct ext4_extent *ex;
3035 struct ext4_extent_header *eh;
3040 depth = ext_depth(inode);
3041 eh = path[depth].p_hdr;
3042 ex = path[depth].p_ext;
3044 err = ext4_ext_get_access(handle, inode, path + depth);
3047 /* first mark the extent as initialized */
3048 ext4_ext_mark_initialized(ex);
3051 * We have to see if it can be merged with the extent
3054 if (ex > EXT_FIRST_EXTENT(eh)) {
3056 * To merge left, pass "ex - 1" to try_to_merge(),
3057 * since it merges towards right _only_.
3059 ret = ext4_ext_try_to_merge(inode, path, ex - 1);
3061 err = ext4_ext_correct_indexes(handle, inode, path);
3064 depth = ext_depth(inode);
3069 * Try to Merge towards right.
3071 ret = ext4_ext_try_to_merge(inode, path, ex);
3073 err = ext4_ext_correct_indexes(handle, inode, path);
3076 depth = ext_depth(inode);
3078 /* Mark modified extent as dirty */
3079 err = ext4_ext_dirty(handle, inode, path + depth);
3081 ext4_ext_show_leaf(inode, path);
3085 static void unmap_underlying_metadata_blocks(struct block_device *bdev,
3086 sector_t block, int count)
3089 for (i = 0; i < count; i++)
3090 unmap_underlying_metadata(bdev, block + i);
3094 ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode,
3095 ext4_lblk_t iblock, unsigned int max_blocks,
3096 struct ext4_ext_path *path, int flags,
3097 unsigned int allocated, struct buffer_head *bh_result,
3098 ext4_fsblk_t newblock)
3102 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
3104 ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical"
3105 "block %llu, max_blocks %u, flags %d, allocated %u",
3106 inode->i_ino, (unsigned long long)iblock, max_blocks,
3108 ext4_ext_show_leaf(inode, path);
3110 /* DIO get_block() before submit the IO, split the extent */
3111 if (flags == EXT4_GET_BLOCKS_DIO_CREATE_EXT) {
3112 ret = ext4_split_unwritten_extents(handle,
3113 inode, path, iblock,
3116 * Flag the inode(non aio case) or end_io struct (aio case)
3117 * that this IO needs to convertion to written when IO is
3121 io->flag = DIO_AIO_UNWRITTEN;
3123 ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
3126 /* async DIO end_io complete, convert the filled extent to written */
3127 if (flags == EXT4_GET_BLOCKS_DIO_CONVERT_EXT) {
3128 ret = ext4_convert_unwritten_extents_dio(handle, inode,
3131 ext4_update_inode_fsync_trans(handle, inode, 1);
3134 /* buffered IO case */
3136 * repeat fallocate creation request
3137 * we already have an unwritten extent
3139 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT)
3142 /* buffered READ or buffered write_begin() lookup */
3143 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3145 * We have blocks reserved already. We
3146 * return allocated blocks so that delalloc
3147 * won't do block reservation for us. But
3148 * the buffer head will be unmapped so that
3149 * a read from the block returns 0s.
3151 set_buffer_unwritten(bh_result);
3155 /* buffered write, writepage time, convert*/
3156 ret = ext4_ext_convert_to_initialized(handle, inode,
3160 ext4_update_inode_fsync_trans(handle, inode, 1);
3167 set_buffer_new(bh_result);
3169 * if we allocated more blocks than requested
3170 * we need to make sure we unmap the extra block
3171 * allocated. The actual needed block will get
3172 * unmapped later when we find the buffer_head marked
3175 if (allocated > max_blocks) {
3176 unmap_underlying_metadata_blocks(inode->i_sb->s_bdev,
3177 newblock + max_blocks,
3178 allocated - max_blocks);
3179 allocated = max_blocks;
3183 * If we have done fallocate with the offset that is already
3184 * delayed allocated, we would have block reservation
3185 * and quota reservation done in the delayed write path.
3186 * But fallocate would have already updated quota and block
3187 * count for this offset. So cancel these reservation
3189 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
3190 ext4_da_update_reserve_space(inode, allocated, 0);
3193 set_buffer_mapped(bh_result);
3195 if (allocated > max_blocks)
3196 allocated = max_blocks;
3197 ext4_ext_show_leaf(inode, path);
3198 bh_result->b_bdev = inode->i_sb->s_bdev;
3199 bh_result->b_blocknr = newblock;
3202 ext4_ext_drop_refs(path);
3205 return err ? err : allocated;
3208 * Block allocation/map/preallocation routine for extents based files
3211 * Need to be called with
3212 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
3213 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
3215 * return > 0, number of of blocks already mapped/allocated
3216 * if create == 0 and these are pre-allocated blocks
3217 * buffer head is unmapped
3218 * otherwise blocks are mapped
3220 * return = 0, if plain look up failed (blocks have not been allocated)
3221 * buffer head is unmapped
3223 * return < 0, error case.
3225 int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
3227 unsigned int max_blocks, struct buffer_head *bh_result,
3230 struct ext4_ext_path *path = NULL;
3231 struct ext4_extent_header *eh;
3232 struct ext4_extent newex, *ex, *last_ex;
3233 ext4_fsblk_t newblock;
3234 int i, err = 0, depth, ret, cache_type;
3235 unsigned int allocated = 0;
3236 struct ext4_allocation_request ar;
3237 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
3239 __clear_bit(BH_New, &bh_result->b_state);
3240 ext_debug("blocks %u/%u requested for inode %lu\n",
3241 iblock, max_blocks, inode->i_ino);
3243 /* check in cache */
3244 cache_type = ext4_ext_in_cache(inode, iblock, &newex);
3246 if (cache_type == EXT4_EXT_CACHE_GAP) {
3247 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3249 * block isn't allocated yet and
3250 * user doesn't want to allocate it
3254 /* we should allocate requested block */
3255 } else if (cache_type == EXT4_EXT_CACHE_EXTENT) {
3256 /* block is already allocated */
3258 - le32_to_cpu(newex.ee_block)
3259 + ext_pblock(&newex);
3260 /* number of remaining blocks in the extent */
3261 allocated = ext4_ext_get_actual_len(&newex) -
3262 (iblock - le32_to_cpu(newex.ee_block));
3269 /* find extent for this block */
3270 path = ext4_ext_find_extent(inode, iblock, NULL);
3272 err = PTR_ERR(path);
3277 depth = ext_depth(inode);
3280 * consistent leaf must not be empty;
3281 * this situation is possible, though, _during_ tree modification;
3282 * this is why assert can't be put in ext4_ext_find_extent()
3284 if (path[depth].p_ext == NULL && depth != 0) {
3285 ext4_error(inode->i_sb, __func__, "bad extent address "
3286 "inode: %lu, iblock: %lu, depth: %d",
3287 inode->i_ino, (unsigned long) iblock, depth);
3291 eh = path[depth].p_hdr;
3293 ex = path[depth].p_ext;
3295 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
3296 ext4_fsblk_t ee_start = ext_pblock(ex);
3297 unsigned short ee_len;
3300 * Uninitialized extents are treated as holes, except that
3301 * we split out initialized portions during a write.
3303 ee_len = ext4_ext_get_actual_len(ex);
3304 /* if found extent covers block, simply return it */
3305 if (in_range(iblock, ee_block, ee_len)) {
3306 newblock = iblock - ee_block + ee_start;
3307 /* number of remaining blocks in the extent */
3308 allocated = ee_len - (iblock - ee_block);
3309 ext_debug("%u fit into %u:%d -> %llu\n", iblock,
3310 ee_block, ee_len, newblock);
3312 /* Do not put uninitialized extent in the cache */
3313 if (!ext4_ext_is_uninitialized(ex)) {
3314 ext4_ext_put_in_cache(inode, ee_block,
3316 EXT4_EXT_CACHE_EXTENT);
3319 ret = ext4_ext_handle_uninitialized_extents(handle,
3320 inode, iblock, max_blocks, path,
3321 flags, allocated, bh_result, newblock);
3327 * requested block isn't allocated yet;
3328 * we couldn't try to create block if create flag is zero
3330 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3332 * put just found gap into cache to speed up
3333 * subsequent requests
3335 ext4_ext_put_gap_in_cache(inode, path, iblock);
3339 * Okay, we need to do block allocation.
3342 /* find neighbour allocated blocks */
3344 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
3348 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright);
3353 * See if request is beyond maximum number of blocks we can have in
3354 * a single extent. For an initialized extent this limit is
3355 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
3356 * EXT_UNINIT_MAX_LEN.
3358 if (max_blocks > EXT_INIT_MAX_LEN &&
3359 !(flags & EXT4_GET_BLOCKS_UNINIT_EXT))
3360 max_blocks = EXT_INIT_MAX_LEN;
3361 else if (max_blocks > EXT_UNINIT_MAX_LEN &&
3362 (flags & EXT4_GET_BLOCKS_UNINIT_EXT))
3363 max_blocks = EXT_UNINIT_MAX_LEN;
3365 /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */
3366 newex.ee_block = cpu_to_le32(iblock);
3367 newex.ee_len = cpu_to_le16(max_blocks);
3368 err = ext4_ext_check_overlap(inode, &newex, path);
3370 allocated = ext4_ext_get_actual_len(&newex);
3372 allocated = max_blocks;
3374 /* allocate new block */
3376 ar.goal = ext4_ext_find_goal(inode, path, iblock);
3377 ar.logical = iblock;
3379 if (S_ISREG(inode->i_mode))
3380 ar.flags = EXT4_MB_HINT_DATA;
3382 /* disable in-core preallocation for non-regular files */
3384 newblock = ext4_mb_new_blocks(handle, &ar, &err);
3387 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
3388 ar.goal, newblock, allocated);
3390 /* try to insert new extent into found leaf and return */
3391 ext4_ext_store_pblock(&newex, newblock);
3392 newex.ee_len = cpu_to_le16(ar.len);
3393 /* Mark uninitialized */
3394 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){
3395 ext4_ext_mark_uninitialized(&newex);
3397 * io_end structure was created for every async
3398 * direct IO write to the middle of the file.
3399 * To avoid unecessary convertion for every aio dio rewrite
3400 * to the mid of file, here we flag the IO that is really
3401 * need the convertion.
3402 * For non asycn direct IO case, flag the inode state
3403 * that we need to perform convertion when IO is done.
3405 if (flags == EXT4_GET_BLOCKS_DIO_CREATE_EXT) {
3407 io->flag = DIO_AIO_UNWRITTEN;
3409 ext4_set_inode_state(inode,
3410 EXT4_STATE_DIO_UNWRITTEN);
3414 if (unlikely(ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))) {
3415 if (unlikely(!eh->eh_entries)) {
3416 ext4_error(inode->i_sb, __func__,
3417 "inode#%lu, eh->eh_entries = 0 and "
3418 "EOFBLOCKS_FL set", inode->i_ino);
3422 last_ex = EXT_LAST_EXTENT(eh);
3424 * If the current leaf block was reached by looking at
3425 * the last index block all the way down the tree, and
3426 * we are extending the inode beyond the last extent
3427 * in the current leaf block, then clear the
3428 * EOFBLOCKS_FL flag.
3430 for (i = depth-1; i >= 0; i--) {
3431 if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr))
3435 (iblock + ar.len > le32_to_cpu(last_ex->ee_block) +
3436 ext4_ext_get_actual_len(last_ex)))
3437 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
3439 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
3441 /* free data blocks we just allocated */
3442 /* not a good idea to call discard here directly,
3443 * but otherwise we'd need to call it every free() */
3444 ext4_discard_preallocations(inode);
3445 ext4_free_blocks(handle, inode, ext_pblock(&newex),
3446 ext4_ext_get_actual_len(&newex), 0);
3450 /* previous routine could use block we allocated */
3451 newblock = ext_pblock(&newex);
3452 allocated = ext4_ext_get_actual_len(&newex);
3453 if (allocated > max_blocks)
3454 allocated = max_blocks;
3455 set_buffer_new(bh_result);
3458 * Update reserved blocks/metadata blocks after successful
3459 * block allocation which had been deferred till now.
3461 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
3462 ext4_da_update_reserve_space(inode, allocated, 1);
3465 * Cache the extent and update transaction to commit on fdatasync only
3466 * when it is _not_ an uninitialized extent.
3468 if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) {
3469 ext4_ext_put_in_cache(inode, iblock, allocated, newblock,
3470 EXT4_EXT_CACHE_EXTENT);
3471 ext4_update_inode_fsync_trans(handle, inode, 1);
3473 ext4_update_inode_fsync_trans(handle, inode, 0);
3475 if (allocated > max_blocks)
3476 allocated = max_blocks;
3477 ext4_ext_show_leaf(inode, path);
3478 set_buffer_mapped(bh_result);
3479 bh_result->b_bdev = inode->i_sb->s_bdev;
3480 bh_result->b_blocknr = newblock;
3483 ext4_ext_drop_refs(path);
3486 return err ? err : allocated;
3489 void ext4_ext_truncate(struct inode *inode)
3491 struct address_space *mapping = inode->i_mapping;
3492 struct super_block *sb = inode->i_sb;
3493 ext4_lblk_t last_block;
3498 * probably first extent we're gonna free will be last in block
3500 err = ext4_writepage_trans_blocks(inode);
3501 handle = ext4_journal_start(inode, err);
3505 if (inode->i_size & (sb->s_blocksize - 1))
3506 ext4_block_truncate_page(handle, mapping, inode->i_size);
3508 if (ext4_orphan_add(handle, inode))
3511 down_write(&EXT4_I(inode)->i_data_sem);
3512 ext4_ext_invalidate_cache(inode);
3514 ext4_discard_preallocations(inode);
3517 * TODO: optimization is possible here.
3518 * Probably we need not scan at all,
3519 * because page truncation is enough.
3522 /* we have to know where to truncate from in crash case */
3523 EXT4_I(inode)->i_disksize = inode->i_size;
3524 ext4_mark_inode_dirty(handle, inode);
3526 last_block = (inode->i_size + sb->s_blocksize - 1)
3527 >> EXT4_BLOCK_SIZE_BITS(sb);
3528 err = ext4_ext_remove_space(inode, last_block);
3530 /* In a multi-transaction truncate, we only make the final
3531 * transaction synchronous.
3534 ext4_handle_sync(handle);
3537 up_write(&EXT4_I(inode)->i_data_sem);
3539 * If this was a simple ftruncate() and the file will remain alive,
3540 * then we need to clear up the orphan record which we created above.
3541 * However, if this was a real unlink then we were called by
3542 * ext4_delete_inode(), and we allow that function to clean up the
3543 * orphan info for us.
3546 ext4_orphan_del(handle, inode);
3548 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
3549 ext4_mark_inode_dirty(handle, inode);
3550 ext4_journal_stop(handle);
3553 static void ext4_falloc_update_inode(struct inode *inode,
3554 int mode, loff_t new_size, int update_ctime)
3556 struct timespec now;
3559 now = current_fs_time(inode->i_sb);
3560 if (!timespec_equal(&inode->i_ctime, &now))
3561 inode->i_ctime = now;
3564 * Update only when preallocation was requested beyond
3567 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3568 if (new_size > i_size_read(inode))
3569 i_size_write(inode, new_size);
3570 if (new_size > EXT4_I(inode)->i_disksize)
3571 ext4_update_i_disksize(inode, new_size);
3574 * Mark that we allocate beyond EOF so the subsequent truncate
3575 * can proceed even if the new size is the same as i_size.
3577 if (new_size > i_size_read(inode))
3578 ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
3584 * preallocate space for a file. This implements ext4's fallocate inode
3585 * operation, which gets called from sys_fallocate system call.
3586 * For block-mapped files, posix_fallocate should fall back to the method
3587 * of writing zeroes to the required new blocks (the same behavior which is
3588 * expected for file systems which do not support fallocate() system call).
3590 long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
3595 unsigned int max_blocks;
3599 struct buffer_head map_bh;
3600 unsigned int credits, blkbits = inode->i_blkbits;
3603 * currently supporting (pre)allocate mode for extent-based
3606 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
3609 /* preallocation to directories is currently not supported */
3610 if (S_ISDIR(inode->i_mode))
3613 block = offset >> blkbits;
3615 * We can't just convert len to max_blocks because
3616 * If blocksize = 4096 offset = 3072 and len = 2048
3618 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
3621 * credits to insert 1 extent into extent tree
3623 credits = ext4_chunk_trans_blocks(inode, max_blocks);
3624 mutex_lock(&inode->i_mutex);
3625 ret = inode_newsize_ok(inode, (len + offset));
3627 mutex_unlock(&inode->i_mutex);
3631 while (ret >= 0 && ret < max_blocks) {
3632 block = block + ret;
3633 max_blocks = max_blocks - ret;
3634 handle = ext4_journal_start(inode, credits);
3635 if (IS_ERR(handle)) {
3636 ret = PTR_ERR(handle);
3640 ret = ext4_get_blocks(handle, inode, block,
3641 max_blocks, &map_bh,
3642 EXT4_GET_BLOCKS_CREATE_UNINIT_EXT);
3646 printk(KERN_ERR "%s: ext4_ext_get_blocks "
3647 "returned error inode#%lu, block=%u, "
3648 "max_blocks=%u", __func__,
3649 inode->i_ino, block, max_blocks);
3651 ext4_mark_inode_dirty(handle, inode);
3652 ret2 = ext4_journal_stop(handle);
3655 if ((block + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
3656 blkbits) >> blkbits))
3657 new_size = offset + len;
3659 new_size = (block + ret) << blkbits;
3661 ext4_falloc_update_inode(inode, mode, new_size,
3662 buffer_new(&map_bh));
3663 ext4_mark_inode_dirty(handle, inode);
3664 ret2 = ext4_journal_stop(handle);
3668 if (ret == -ENOSPC &&
3669 ext4_should_retry_alloc(inode->i_sb, &retries)) {
3673 mutex_unlock(&inode->i_mutex);
3674 return ret > 0 ? ret2 : ret;
3678 * This function convert a range of blocks to written extents
3679 * The caller of this function will pass the start offset and the size.
3680 * all unwritten extents within this range will be converted to
3683 * This function is called from the direct IO end io call back
3684 * function, to convert the fallocated extents after IO is completed.
3685 * Returns 0 on success.
3687 int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset,
3692 unsigned int max_blocks;
3695 struct buffer_head map_bh;
3696 unsigned int credits, blkbits = inode->i_blkbits;
3698 block = offset >> blkbits;
3700 * We can't just convert len to max_blocks because
3701 * If blocksize = 4096 offset = 3072 and len = 2048
3703 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
3706 * credits to insert 1 extent into extent tree
3708 credits = ext4_chunk_trans_blocks(inode, max_blocks);
3709 while (ret >= 0 && ret < max_blocks) {
3710 block = block + ret;
3711 max_blocks = max_blocks - ret;
3712 handle = ext4_journal_start(inode, credits);
3713 if (IS_ERR(handle)) {
3714 ret = PTR_ERR(handle);
3718 ret = ext4_get_blocks(handle, inode, block,
3719 max_blocks, &map_bh,
3720 EXT4_GET_BLOCKS_DIO_CONVERT_EXT);
3723 printk(KERN_ERR "%s: ext4_ext_get_blocks "
3724 "returned error inode#%lu, block=%u, "
3725 "max_blocks=%u", __func__,
3726 inode->i_ino, block, max_blocks);
3728 ext4_mark_inode_dirty(handle, inode);
3729 ret2 = ext4_journal_stop(handle);
3730 if (ret <= 0 || ret2 )
3733 return ret > 0 ? ret2 : ret;
3736 * Callback function called for each extent to gather FIEMAP information.
3738 static int ext4_ext_fiemap_cb(struct inode *inode, struct ext4_ext_path *path,
3739 struct ext4_ext_cache *newex, struct ext4_extent *ex,
3742 struct fiemap_extent_info *fieinfo = data;
3743 unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
3750 logical = (__u64)newex->ec_block << blksize_bits;
3752 if (newex->ec_type == EXT4_EXT_CACHE_GAP) {
3755 struct buffer_head *bh = NULL;
3757 offset = logical >> PAGE_SHIFT;
3758 page = find_get_page(inode->i_mapping, offset);
3759 if (!page || !page_has_buffers(page))
3760 return EXT_CONTINUE;
3762 bh = page_buffers(page);
3765 return EXT_CONTINUE;
3767 if (buffer_delay(bh)) {
3768 flags |= FIEMAP_EXTENT_DELALLOC;
3769 page_cache_release(page);
3771 page_cache_release(page);
3772 return EXT_CONTINUE;
3776 physical = (__u64)newex->ec_start << blksize_bits;
3777 length = (__u64)newex->ec_len << blksize_bits;
3779 if (ex && ext4_ext_is_uninitialized(ex))
3780 flags |= FIEMAP_EXTENT_UNWRITTEN;
3783 * If this extent reaches EXT_MAX_BLOCK, it must be last.
3785 * Or if ext4_ext_next_allocated_block is EXT_MAX_BLOCK,
3786 * this also indicates no more allocated blocks.
3788 * XXX this might miss a single-block extent at EXT_MAX_BLOCK
3790 if (ext4_ext_next_allocated_block(path) == EXT_MAX_BLOCK ||
3791 newex->ec_block + newex->ec_len - 1 == EXT_MAX_BLOCK) {
3792 loff_t size = i_size_read(inode);
3793 loff_t bs = EXT4_BLOCK_SIZE(inode->i_sb);
3795 flags |= FIEMAP_EXTENT_LAST;
3796 if ((flags & FIEMAP_EXTENT_DELALLOC) &&
3797 logical+length > size)
3798 length = (size - logical + bs - 1) & ~(bs-1);
3801 error = fiemap_fill_next_extent(fieinfo, logical, physical,
3808 return EXT_CONTINUE;
3811 /* fiemap flags we can handle specified here */
3812 #define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
3814 static int ext4_xattr_fiemap(struct inode *inode,
3815 struct fiemap_extent_info *fieinfo)
3819 __u32 flags = FIEMAP_EXTENT_LAST;
3820 int blockbits = inode->i_sb->s_blocksize_bits;
3824 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
3825 struct ext4_iloc iloc;
3826 int offset; /* offset of xattr in inode */
3828 error = ext4_get_inode_loc(inode, &iloc);
3831 physical = iloc.bh->b_blocknr << blockbits;
3832 offset = EXT4_GOOD_OLD_INODE_SIZE +
3833 EXT4_I(inode)->i_extra_isize;
3835 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
3836 flags |= FIEMAP_EXTENT_DATA_INLINE;
3838 } else { /* external block */
3839 physical = EXT4_I(inode)->i_file_acl << blockbits;
3840 length = inode->i_sb->s_blocksize;
3844 error = fiemap_fill_next_extent(fieinfo, 0, physical,
3846 return (error < 0 ? error : 0);
3849 int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3850 __u64 start, __u64 len)
3852 ext4_lblk_t start_blk;
3855 /* fallback to generic here if not in extents fmt */
3856 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
3857 return generic_block_fiemap(inode, fieinfo, start, len,
3860 if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
3863 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
3864 error = ext4_xattr_fiemap(inode, fieinfo);
3866 ext4_lblk_t len_blks;
3869 start_blk = start >> inode->i_sb->s_blocksize_bits;
3870 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
3871 if (last_blk >= EXT_MAX_BLOCK)
3872 last_blk = EXT_MAX_BLOCK-1;
3873 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
3876 * Walk the extent tree gathering extent information.
3877 * ext4_ext_fiemap_cb will push extents back to user.
3879 error = ext4_ext_walk_space(inode, start_blk, len_blks,
3880 ext4_ext_fiemap_cb, fieinfo);