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);
111 * We have dropped i_data_sem so someone might have cached again
112 * an extent we are going to truncate.
114 ext4_ext_invalidate_cache(inode);
124 static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
125 struct ext4_ext_path *path)
128 /* path points to block */
129 return ext4_journal_get_write_access(handle, path->p_bh);
131 /* path points to leaf/index in inode body */
132 /* we use in-core data, no need to protect them */
142 static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
143 struct ext4_ext_path *path)
147 /* path points to block */
148 err = ext4_handle_dirty_metadata(handle, inode, path->p_bh);
150 /* path points to leaf/index in inode body */
151 err = ext4_mark_inode_dirty(handle, inode);
156 static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
157 struct ext4_ext_path *path,
160 struct ext4_inode_info *ei = EXT4_I(inode);
161 ext4_fsblk_t bg_start;
162 ext4_fsblk_t last_block;
163 ext4_grpblk_t colour;
164 ext4_group_t block_group;
165 int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
169 struct ext4_extent *ex;
170 depth = path->p_depth;
172 /* try to predict block placement */
173 ex = path[depth].p_ext;
175 return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block));
177 /* it looks like index is empty;
178 * try to find starting block from index itself */
179 if (path[depth].p_bh)
180 return path[depth].p_bh->b_blocknr;
183 /* OK. use inode's group */
184 block_group = ei->i_block_group;
185 if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
187 * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
188 * block groups per flexgroup, reserve the first block
189 * group for directories and special files. Regular
190 * files will start at the second block group. This
191 * tends to speed up directory access and improves
194 block_group &= ~(flex_size-1);
195 if (S_ISREG(inode->i_mode))
198 bg_start = (block_group * EXT4_BLOCKS_PER_GROUP(inode->i_sb)) +
199 le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_first_data_block);
200 last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
203 * If we are doing delayed allocation, we don't need take
204 * colour into account.
206 if (test_opt(inode->i_sb, DELALLOC))
209 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
210 colour = (current->pid % 16) *
211 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
213 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
214 return bg_start + colour + block;
218 * Allocation for a meta data block
221 ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
222 struct ext4_ext_path *path,
223 struct ext4_extent *ex, int *err)
225 ext4_fsblk_t goal, newblock;
227 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
228 newblock = ext4_new_meta_blocks(handle, inode, goal, NULL, err);
232 static int ext4_ext_space_block(struct inode *inode)
236 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
237 / sizeof(struct ext4_extent);
238 #ifdef AGGRESSIVE_TEST
245 static int ext4_ext_space_block_idx(struct inode *inode)
249 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
250 / sizeof(struct ext4_extent_idx);
251 #ifdef AGGRESSIVE_TEST
258 static int ext4_ext_space_root(struct inode *inode)
262 size = sizeof(EXT4_I(inode)->i_data);
263 size -= sizeof(struct ext4_extent_header);
264 size /= sizeof(struct ext4_extent);
265 #ifdef AGGRESSIVE_TEST
272 static int ext4_ext_space_root_idx(struct inode *inode)
276 size = sizeof(EXT4_I(inode)->i_data);
277 size -= sizeof(struct ext4_extent_header);
278 size /= sizeof(struct ext4_extent_idx);
279 #ifdef AGGRESSIVE_TEST
287 * Calculate the number of metadata blocks needed
288 * to allocate @blocks
289 * Worse case is one block per extent
291 int ext4_ext_calc_metadata_amount(struct inode *inode, int blocks)
293 int lcap, icap, rcap, leafs, idxs, num;
294 int newextents = blocks;
296 rcap = ext4_ext_space_root_idx(inode);
297 lcap = ext4_ext_space_block(inode);
298 icap = ext4_ext_space_block_idx(inode);
300 /* number of new leaf blocks needed */
301 num = leafs = (newextents + lcap - 1) / lcap;
304 * Worse case, we need separate index block(s)
305 * to link all new leaf blocks
307 idxs = (leafs + icap - 1) / icap;
310 idxs = (idxs + icap - 1) / icap;
311 } while (idxs > rcap);
317 ext4_ext_max_entries(struct inode *inode, int depth)
321 if (depth == ext_depth(inode)) {
323 max = ext4_ext_space_root(inode);
325 max = ext4_ext_space_root_idx(inode);
328 max = ext4_ext_space_block(inode);
330 max = ext4_ext_space_block_idx(inode);
336 static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
338 ext4_fsblk_t block = ext_pblock(ext);
339 int len = ext4_ext_get_actual_len(ext);
341 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
344 static int ext4_valid_extent_idx(struct inode *inode,
345 struct ext4_extent_idx *ext_idx)
347 ext4_fsblk_t block = idx_pblock(ext_idx);
349 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
352 static int ext4_valid_extent_entries(struct inode *inode,
353 struct ext4_extent_header *eh,
356 struct ext4_extent *ext;
357 struct ext4_extent_idx *ext_idx;
358 unsigned short entries;
359 if (eh->eh_entries == 0)
362 entries = le16_to_cpu(eh->eh_entries);
366 ext = EXT_FIRST_EXTENT(eh);
368 if (!ext4_valid_extent(inode, ext))
374 ext_idx = EXT_FIRST_INDEX(eh);
376 if (!ext4_valid_extent_idx(inode, ext_idx))
385 static int __ext4_ext_check(const char *function, struct inode *inode,
386 struct ext4_extent_header *eh,
389 const char *error_msg;
392 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
393 error_msg = "invalid magic";
396 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
397 error_msg = "unexpected eh_depth";
400 if (unlikely(eh->eh_max == 0)) {
401 error_msg = "invalid eh_max";
404 max = ext4_ext_max_entries(inode, depth);
405 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
406 error_msg = "too large eh_max";
409 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
410 error_msg = "invalid eh_entries";
413 if (!ext4_valid_extent_entries(inode, eh, depth)) {
414 error_msg = "invalid extent entries";
420 ext4_error(inode->i_sb, function,
421 "bad header/extent in inode #%lu: %s - magic %x, "
422 "entries %u, max %u(%u), depth %u(%u)",
423 inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic),
424 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
425 max, le16_to_cpu(eh->eh_depth), depth);
430 #define ext4_ext_check(inode, eh, depth) \
431 __ext4_ext_check(__func__, inode, eh, depth)
433 int ext4_ext_check_inode(struct inode *inode)
435 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode));
439 static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
441 int k, l = path->p_depth;
444 for (k = 0; k <= l; k++, path++) {
446 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
447 idx_pblock(path->p_idx));
448 } else if (path->p_ext) {
449 ext_debug(" %d:%d:%llu ",
450 le32_to_cpu(path->p_ext->ee_block),
451 ext4_ext_get_actual_len(path->p_ext),
452 ext_pblock(path->p_ext));
459 static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
461 int depth = ext_depth(inode);
462 struct ext4_extent_header *eh;
463 struct ext4_extent *ex;
469 eh = path[depth].p_hdr;
470 ex = EXT_FIRST_EXTENT(eh);
472 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
473 ext_debug("%d:%d:%llu ", le32_to_cpu(ex->ee_block),
474 ext4_ext_get_actual_len(ex), ext_pblock(ex));
479 #define ext4_ext_show_path(inode, path)
480 #define ext4_ext_show_leaf(inode, path)
483 void ext4_ext_drop_refs(struct ext4_ext_path *path)
485 int depth = path->p_depth;
488 for (i = 0; i <= depth; i++, path++)
496 * ext4_ext_binsearch_idx:
497 * binary search for the closest index of the given block
498 * the header must be checked before calling this
501 ext4_ext_binsearch_idx(struct inode *inode,
502 struct ext4_ext_path *path, ext4_lblk_t block)
504 struct ext4_extent_header *eh = path->p_hdr;
505 struct ext4_extent_idx *r, *l, *m;
508 ext_debug("binsearch for %u(idx): ", block);
510 l = EXT_FIRST_INDEX(eh) + 1;
511 r = EXT_LAST_INDEX(eh);
514 if (block < le32_to_cpu(m->ei_block))
518 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
519 m, le32_to_cpu(m->ei_block),
520 r, le32_to_cpu(r->ei_block));
524 ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
525 idx_pblock(path->p_idx));
527 #ifdef CHECK_BINSEARCH
529 struct ext4_extent_idx *chix, *ix;
532 chix = ix = EXT_FIRST_INDEX(eh);
533 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
535 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
536 printk(KERN_DEBUG "k=%d, ix=0x%p, "
538 ix, EXT_FIRST_INDEX(eh));
539 printk(KERN_DEBUG "%u <= %u\n",
540 le32_to_cpu(ix->ei_block),
541 le32_to_cpu(ix[-1].ei_block));
543 BUG_ON(k && le32_to_cpu(ix->ei_block)
544 <= le32_to_cpu(ix[-1].ei_block));
545 if (block < le32_to_cpu(ix->ei_block))
549 BUG_ON(chix != path->p_idx);
556 * ext4_ext_binsearch:
557 * binary search for closest extent of the given block
558 * the header must be checked before calling this
561 ext4_ext_binsearch(struct inode *inode,
562 struct ext4_ext_path *path, ext4_lblk_t block)
564 struct ext4_extent_header *eh = path->p_hdr;
565 struct ext4_extent *r, *l, *m;
567 if (eh->eh_entries == 0) {
569 * this leaf is empty:
570 * we get such a leaf in split/add case
575 ext_debug("binsearch for %u: ", block);
577 l = EXT_FIRST_EXTENT(eh) + 1;
578 r = EXT_LAST_EXTENT(eh);
582 if (block < le32_to_cpu(m->ee_block))
586 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
587 m, le32_to_cpu(m->ee_block),
588 r, le32_to_cpu(r->ee_block));
592 ext_debug(" -> %d:%llu:%d ",
593 le32_to_cpu(path->p_ext->ee_block),
594 ext_pblock(path->p_ext),
595 ext4_ext_get_actual_len(path->p_ext));
597 #ifdef CHECK_BINSEARCH
599 struct ext4_extent *chex, *ex;
602 chex = ex = EXT_FIRST_EXTENT(eh);
603 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
604 BUG_ON(k && le32_to_cpu(ex->ee_block)
605 <= le32_to_cpu(ex[-1].ee_block));
606 if (block < le32_to_cpu(ex->ee_block))
610 BUG_ON(chex != path->p_ext);
616 int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
618 struct ext4_extent_header *eh;
620 eh = ext_inode_hdr(inode);
623 eh->eh_magic = EXT4_EXT_MAGIC;
624 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode));
625 ext4_mark_inode_dirty(handle, inode);
626 ext4_ext_invalidate_cache(inode);
630 struct ext4_ext_path *
631 ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
632 struct ext4_ext_path *path)
634 struct ext4_extent_header *eh;
635 struct buffer_head *bh;
636 short int depth, i, ppos = 0, alloc = 0;
638 eh = ext_inode_hdr(inode);
639 depth = ext_depth(inode);
641 /* account possible depth increase */
643 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
646 return ERR_PTR(-ENOMEM);
653 /* walk through the tree */
655 int need_to_validate = 0;
657 ext_debug("depth %d: num %d, max %d\n",
658 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
660 ext4_ext_binsearch_idx(inode, path + ppos, block);
661 path[ppos].p_block = idx_pblock(path[ppos].p_idx);
662 path[ppos].p_depth = i;
663 path[ppos].p_ext = NULL;
665 bh = sb_getblk(inode->i_sb, path[ppos].p_block);
668 if (!bh_uptodate_or_lock(bh)) {
669 if (bh_submit_read(bh) < 0) {
673 /* validate the extent entries */
674 need_to_validate = 1;
676 eh = ext_block_hdr(bh);
678 BUG_ON(ppos > depth);
679 path[ppos].p_bh = bh;
680 path[ppos].p_hdr = eh;
683 if (need_to_validate && ext4_ext_check(inode, eh, i))
687 path[ppos].p_depth = i;
688 path[ppos].p_ext = NULL;
689 path[ppos].p_idx = NULL;
692 ext4_ext_binsearch(inode, path + ppos, block);
693 /* if not an empty leaf */
694 if (path[ppos].p_ext)
695 path[ppos].p_block = ext_pblock(path[ppos].p_ext);
697 ext4_ext_show_path(inode, path);
702 ext4_ext_drop_refs(path);
705 return ERR_PTR(-EIO);
709 * ext4_ext_insert_index:
710 * insert new index [@logical;@ptr] into the block at @curp;
711 * check where to insert: before @curp or after @curp
713 int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
714 struct ext4_ext_path *curp,
715 int logical, ext4_fsblk_t ptr)
717 struct ext4_extent_idx *ix;
720 err = ext4_ext_get_access(handle, inode, curp);
724 BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block));
725 len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
726 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
728 if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
729 len = (len - 1) * sizeof(struct ext4_extent_idx);
730 len = len < 0 ? 0 : len;
731 ext_debug("insert new index %d after: %llu. "
732 "move %d from 0x%p to 0x%p\n",
734 (curp->p_idx + 1), (curp->p_idx + 2));
735 memmove(curp->p_idx + 2, curp->p_idx + 1, len);
737 ix = curp->p_idx + 1;
740 len = len * sizeof(struct ext4_extent_idx);
741 len = len < 0 ? 0 : len;
742 ext_debug("insert new index %d before: %llu. "
743 "move %d from 0x%p to 0x%p\n",
745 curp->p_idx, (curp->p_idx + 1));
746 memmove(curp->p_idx + 1, curp->p_idx, len);
750 ix->ei_block = cpu_to_le32(logical);
751 ext4_idx_store_pblock(ix, ptr);
752 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
754 BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries)
755 > le16_to_cpu(curp->p_hdr->eh_max));
756 BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr));
758 err = ext4_ext_dirty(handle, inode, curp);
759 ext4_std_error(inode->i_sb, err);
766 * inserts new subtree into the path, using free index entry
768 * - allocates all needed blocks (new leaf and all intermediate index blocks)
769 * - makes decision where to split
770 * - moves remaining extents and index entries (right to the split point)
771 * into the newly allocated blocks
772 * - initializes subtree
774 static int ext4_ext_split(handle_t *handle, struct inode *inode,
775 struct ext4_ext_path *path,
776 struct ext4_extent *newext, int at)
778 struct buffer_head *bh = NULL;
779 int depth = ext_depth(inode);
780 struct ext4_extent_header *neh;
781 struct ext4_extent_idx *fidx;
782 struct ext4_extent *ex;
784 ext4_fsblk_t newblock, oldblock;
786 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
789 /* make decision: where to split? */
790 /* FIXME: now decision is simplest: at current extent */
792 /* if current leaf will be split, then we should use
793 * border from split point */
794 BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr));
795 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
796 border = path[depth].p_ext[1].ee_block;
797 ext_debug("leaf will be split."
798 " next leaf starts at %d\n",
799 le32_to_cpu(border));
801 border = newext->ee_block;
802 ext_debug("leaf will be added."
803 " next leaf starts at %d\n",
804 le32_to_cpu(border));
808 * If error occurs, then we break processing
809 * and mark filesystem read-only. index won't
810 * be inserted and tree will be in consistent
811 * state. Next mount will repair buffers too.
815 * Get array to track all allocated blocks.
816 * We need this to handle errors and free blocks
819 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
823 /* allocate all needed blocks */
824 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
825 for (a = 0; a < depth - at; a++) {
826 newblock = ext4_ext_new_meta_block(handle, inode, path,
830 ablocks[a] = newblock;
833 /* initialize new leaf */
834 newblock = ablocks[--a];
835 BUG_ON(newblock == 0);
836 bh = sb_getblk(inode->i_sb, newblock);
843 err = ext4_journal_get_create_access(handle, bh);
847 neh = ext_block_hdr(bh);
849 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
850 neh->eh_magic = EXT4_EXT_MAGIC;
852 ex = EXT_FIRST_EXTENT(neh);
854 /* move remainder of path[depth] to the new leaf */
855 BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max);
856 /* start copy from next extent */
857 /* TODO: we could do it by single memmove */
860 while (path[depth].p_ext <=
861 EXT_MAX_EXTENT(path[depth].p_hdr)) {
862 ext_debug("move %d:%llu:%d in new leaf %llu\n",
863 le32_to_cpu(path[depth].p_ext->ee_block),
864 ext_pblock(path[depth].p_ext),
865 ext4_ext_get_actual_len(path[depth].p_ext),
867 /*memmove(ex++, path[depth].p_ext++,
868 sizeof(struct ext4_extent));
874 memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
875 le16_add_cpu(&neh->eh_entries, m);
878 set_buffer_uptodate(bh);
881 err = ext4_handle_dirty_metadata(handle, inode, bh);
887 /* correct old leaf */
889 err = ext4_ext_get_access(handle, inode, path + depth);
892 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
893 err = ext4_ext_dirty(handle, inode, path + depth);
899 /* create intermediate indexes */
903 ext_debug("create %d intermediate indices\n", k);
904 /* insert new index into current index block */
905 /* current depth stored in i var */
909 newblock = ablocks[--a];
910 bh = sb_getblk(inode->i_sb, newblock);
917 err = ext4_journal_get_create_access(handle, bh);
921 neh = ext_block_hdr(bh);
922 neh->eh_entries = cpu_to_le16(1);
923 neh->eh_magic = EXT4_EXT_MAGIC;
924 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
925 neh->eh_depth = cpu_to_le16(depth - i);
926 fidx = EXT_FIRST_INDEX(neh);
927 fidx->ei_block = border;
928 ext4_idx_store_pblock(fidx, oldblock);
930 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
931 i, newblock, le32_to_cpu(border), oldblock);
936 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
937 EXT_MAX_INDEX(path[i].p_hdr));
938 BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) !=
939 EXT_LAST_INDEX(path[i].p_hdr));
940 while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
941 ext_debug("%d: move %d:%llu in new index %llu\n", i,
942 le32_to_cpu(path[i].p_idx->ei_block),
943 idx_pblock(path[i].p_idx),
945 /*memmove(++fidx, path[i].p_idx++,
946 sizeof(struct ext4_extent_idx));
948 BUG_ON(neh->eh_entries > neh->eh_max);*/
953 memmove(++fidx, path[i].p_idx - m,
954 sizeof(struct ext4_extent_idx) * m);
955 le16_add_cpu(&neh->eh_entries, m);
957 set_buffer_uptodate(bh);
960 err = ext4_handle_dirty_metadata(handle, inode, bh);
966 /* correct old index */
968 err = ext4_ext_get_access(handle, inode, path + i);
971 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
972 err = ext4_ext_dirty(handle, inode, path + i);
980 /* insert new index */
981 err = ext4_ext_insert_index(handle, inode, path + at,
982 le32_to_cpu(border), newblock);
986 if (buffer_locked(bh))
992 /* free all allocated blocks in error case */
993 for (i = 0; i < depth; i++) {
996 ext4_free_blocks(handle, inode, ablocks[i], 1, 1);
1005 * ext4_ext_grow_indepth:
1006 * implements tree growing procedure:
1007 * - allocates new block
1008 * - moves top-level data (index block or leaf) into the new block
1009 * - initializes new top-level, creating index that points to the
1010 * just created block
1012 static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
1013 struct ext4_ext_path *path,
1014 struct ext4_extent *newext)
1016 struct ext4_ext_path *curp = path;
1017 struct ext4_extent_header *neh;
1018 struct ext4_extent_idx *fidx;
1019 struct buffer_head *bh;
1020 ext4_fsblk_t newblock;
1023 newblock = ext4_ext_new_meta_block(handle, inode, path, newext, &err);
1027 bh = sb_getblk(inode->i_sb, newblock);
1030 ext4_std_error(inode->i_sb, err);
1035 err = ext4_journal_get_create_access(handle, bh);
1041 /* move top-level index/leaf into new block */
1042 memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
1044 /* set size of new block */
1045 neh = ext_block_hdr(bh);
1046 /* old root could have indexes or leaves
1047 * so calculate e_max right way */
1048 if (ext_depth(inode))
1049 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
1051 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
1052 neh->eh_magic = EXT4_EXT_MAGIC;
1053 set_buffer_uptodate(bh);
1056 err = ext4_handle_dirty_metadata(handle, inode, bh);
1060 /* create index in new top-level index: num,max,pointer */
1061 err = ext4_ext_get_access(handle, inode, curp);
1065 curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
1066 curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode));
1067 curp->p_hdr->eh_entries = cpu_to_le16(1);
1068 curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
1070 if (path[0].p_hdr->eh_depth)
1071 curp->p_idx->ei_block =
1072 EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
1074 curp->p_idx->ei_block =
1075 EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
1076 ext4_idx_store_pblock(curp->p_idx, newblock);
1078 neh = ext_inode_hdr(inode);
1079 fidx = EXT_FIRST_INDEX(neh);
1080 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
1081 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
1082 le32_to_cpu(fidx->ei_block), idx_pblock(fidx));
1084 neh->eh_depth = cpu_to_le16(path->p_depth + 1);
1085 err = ext4_ext_dirty(handle, inode, curp);
1093 * ext4_ext_create_new_leaf:
1094 * finds empty index and adds new leaf.
1095 * if no free index is found, then it requests in-depth growing.
1097 static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
1098 struct ext4_ext_path *path,
1099 struct ext4_extent *newext)
1101 struct ext4_ext_path *curp;
1102 int depth, i, err = 0;
1105 i = depth = ext_depth(inode);
1107 /* walk up to the tree and look for free index entry */
1108 curp = path + depth;
1109 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1114 /* we use already allocated block for index block,
1115 * so subsequent data blocks should be contiguous */
1116 if (EXT_HAS_FREE_INDEX(curp)) {
1117 /* if we found index with free entry, then use that
1118 * entry: create all needed subtree and add new leaf */
1119 err = ext4_ext_split(handle, inode, path, newext, i);
1124 ext4_ext_drop_refs(path);
1125 path = ext4_ext_find_extent(inode,
1126 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1129 err = PTR_ERR(path);
1131 /* tree is full, time to grow in depth */
1132 err = ext4_ext_grow_indepth(handle, inode, path, newext);
1137 ext4_ext_drop_refs(path);
1138 path = ext4_ext_find_extent(inode,
1139 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1142 err = PTR_ERR(path);
1147 * only first (depth 0 -> 1) produces free space;
1148 * in all other cases we have to split the grown tree
1150 depth = ext_depth(inode);
1151 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
1152 /* now we need to split */
1162 * search the closest allocated block to the left for *logical
1163 * and returns it at @logical + it's physical address at @phys
1164 * if *logical is the smallest allocated block, the function
1165 * returns 0 at @phys
1166 * return value contains 0 (success) or error code
1169 ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path,
1170 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1172 struct ext4_extent_idx *ix;
1173 struct ext4_extent *ex;
1176 BUG_ON(path == NULL);
1177 depth = path->p_depth;
1180 if (depth == 0 && path->p_ext == NULL)
1183 /* usually extent in the path covers blocks smaller
1184 * then *logical, but it can be that extent is the
1185 * first one in the file */
1187 ex = path[depth].p_ext;
1188 ee_len = ext4_ext_get_actual_len(ex);
1189 if (*logical < le32_to_cpu(ex->ee_block)) {
1190 BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
1191 while (--depth >= 0) {
1192 ix = path[depth].p_idx;
1193 BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
1198 BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
1200 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
1201 *phys = ext_pblock(ex) + ee_len - 1;
1206 * search the closest allocated block to the right for *logical
1207 * and returns it at @logical + it's physical address at @phys
1208 * if *logical is the smallest allocated block, the function
1209 * returns 0 at @phys
1210 * return value contains 0 (success) or error code
1213 ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path,
1214 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1216 struct buffer_head *bh = NULL;
1217 struct ext4_extent_header *eh;
1218 struct ext4_extent_idx *ix;
1219 struct ext4_extent *ex;
1221 int depth; /* Note, NOT eh_depth; depth from top of tree */
1224 BUG_ON(path == NULL);
1225 depth = path->p_depth;
1228 if (depth == 0 && path->p_ext == NULL)
1231 /* usually extent in the path covers blocks smaller
1232 * then *logical, but it can be that extent is the
1233 * first one in the file */
1235 ex = path[depth].p_ext;
1236 ee_len = ext4_ext_get_actual_len(ex);
1237 if (*logical < le32_to_cpu(ex->ee_block)) {
1238 BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
1239 while (--depth >= 0) {
1240 ix = path[depth].p_idx;
1241 BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
1243 *logical = le32_to_cpu(ex->ee_block);
1244 *phys = ext_pblock(ex);
1248 BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
1250 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1251 /* next allocated block in this leaf */
1253 *logical = le32_to_cpu(ex->ee_block);
1254 *phys = ext_pblock(ex);
1258 /* go up and search for index to the right */
1259 while (--depth >= 0) {
1260 ix = path[depth].p_idx;
1261 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
1265 /* we've gone up to the root and found no index to the right */
1269 /* we've found index to the right, let's
1270 * follow it and find the closest allocated
1271 * block to the right */
1273 block = idx_pblock(ix);
1274 while (++depth < path->p_depth) {
1275 bh = sb_bread(inode->i_sb, block);
1278 eh = ext_block_hdr(bh);
1279 /* subtract from p_depth to get proper eh_depth */
1280 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
1284 ix = EXT_FIRST_INDEX(eh);
1285 block = idx_pblock(ix);
1289 bh = sb_bread(inode->i_sb, block);
1292 eh = ext_block_hdr(bh);
1293 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
1297 ex = EXT_FIRST_EXTENT(eh);
1298 *logical = le32_to_cpu(ex->ee_block);
1299 *phys = ext_pblock(ex);
1305 * ext4_ext_next_allocated_block:
1306 * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1307 * NOTE: it considers block number from index entry as
1308 * allocated block. Thus, index entries have to be consistent
1312 ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1316 BUG_ON(path == NULL);
1317 depth = path->p_depth;
1319 if (depth == 0 && path->p_ext == NULL)
1320 return EXT_MAX_BLOCK;
1322 while (depth >= 0) {
1323 if (depth == path->p_depth) {
1325 if (path[depth].p_ext !=
1326 EXT_LAST_EXTENT(path[depth].p_hdr))
1327 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1330 if (path[depth].p_idx !=
1331 EXT_LAST_INDEX(path[depth].p_hdr))
1332 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1337 return EXT_MAX_BLOCK;
1341 * ext4_ext_next_leaf_block:
1342 * returns first allocated block from next leaf or EXT_MAX_BLOCK
1344 static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode,
1345 struct ext4_ext_path *path)
1349 BUG_ON(path == NULL);
1350 depth = path->p_depth;
1352 /* zero-tree has no leaf blocks at all */
1354 return EXT_MAX_BLOCK;
1356 /* go to index block */
1359 while (depth >= 0) {
1360 if (path[depth].p_idx !=
1361 EXT_LAST_INDEX(path[depth].p_hdr))
1362 return (ext4_lblk_t)
1363 le32_to_cpu(path[depth].p_idx[1].ei_block);
1367 return EXT_MAX_BLOCK;
1371 * ext4_ext_correct_indexes:
1372 * if leaf gets modified and modified extent is first in the leaf,
1373 * then we have to correct all indexes above.
1374 * TODO: do we need to correct tree in all cases?
1376 static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
1377 struct ext4_ext_path *path)
1379 struct ext4_extent_header *eh;
1380 int depth = ext_depth(inode);
1381 struct ext4_extent *ex;
1385 eh = path[depth].p_hdr;
1386 ex = path[depth].p_ext;
1391 /* there is no tree at all */
1395 if (ex != EXT_FIRST_EXTENT(eh)) {
1396 /* we correct tree if first leaf got modified only */
1401 * TODO: we need correction if border is smaller than current one
1404 border = path[depth].p_ext->ee_block;
1405 err = ext4_ext_get_access(handle, inode, path + k);
1408 path[k].p_idx->ei_block = border;
1409 err = ext4_ext_dirty(handle, inode, path + k);
1414 /* change all left-side indexes */
1415 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1417 err = ext4_ext_get_access(handle, inode, path + k);
1420 path[k].p_idx->ei_block = border;
1421 err = ext4_ext_dirty(handle, inode, path + k);
1430 ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1431 struct ext4_extent *ex2)
1433 unsigned short ext1_ee_len, ext2_ee_len, max_len;
1436 * Make sure that either both extents are uninitialized, or
1439 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1442 if (ext4_ext_is_uninitialized(ex1))
1443 max_len = EXT_UNINIT_MAX_LEN;
1445 max_len = EXT_INIT_MAX_LEN;
1447 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1448 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1450 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
1451 le32_to_cpu(ex2->ee_block))
1455 * To allow future support for preallocated extents to be added
1456 * as an RO_COMPAT feature, refuse to merge to extents if
1457 * this can result in the top bit of ee_len being set.
1459 if (ext1_ee_len + ext2_ee_len > max_len)
1461 #ifdef AGGRESSIVE_TEST
1462 if (ext1_ee_len >= 4)
1466 if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2))
1472 * This function tries to merge the "ex" extent to the next extent in the tree.
1473 * It always tries to merge towards right. If you want to merge towards
1474 * left, pass "ex - 1" as argument instead of "ex".
1475 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1476 * 1 if they got merged.
1478 int ext4_ext_try_to_merge(struct inode *inode,
1479 struct ext4_ext_path *path,
1480 struct ext4_extent *ex)
1482 struct ext4_extent_header *eh;
1483 unsigned int depth, len;
1485 int uninitialized = 0;
1487 depth = ext_depth(inode);
1488 BUG_ON(path[depth].p_hdr == NULL);
1489 eh = path[depth].p_hdr;
1491 while (ex < EXT_LAST_EXTENT(eh)) {
1492 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1494 /* merge with next extent! */
1495 if (ext4_ext_is_uninitialized(ex))
1497 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1498 + ext4_ext_get_actual_len(ex + 1));
1500 ext4_ext_mark_uninitialized(ex);
1502 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1503 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1504 * sizeof(struct ext4_extent);
1505 memmove(ex + 1, ex + 2, len);
1507 le16_add_cpu(&eh->eh_entries, -1);
1509 WARN_ON(eh->eh_entries == 0);
1510 if (!eh->eh_entries)
1511 ext4_error(inode->i_sb, "ext4_ext_try_to_merge",
1512 "inode#%lu, eh->eh_entries = 0!", inode->i_ino);
1519 * check if a portion of the "newext" extent overlaps with an
1522 * If there is an overlap discovered, it updates the length of the newext
1523 * such that there will be no overlap, and then returns 1.
1524 * If there is no overlap found, it returns 0.
1526 unsigned int ext4_ext_check_overlap(struct inode *inode,
1527 struct ext4_extent *newext,
1528 struct ext4_ext_path *path)
1531 unsigned int depth, len1;
1532 unsigned int ret = 0;
1534 b1 = le32_to_cpu(newext->ee_block);
1535 len1 = ext4_ext_get_actual_len(newext);
1536 depth = ext_depth(inode);
1537 if (!path[depth].p_ext)
1539 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
1542 * get the next allocated block if the extent in the path
1543 * is before the requested block(s)
1546 b2 = ext4_ext_next_allocated_block(path);
1547 if (b2 == EXT_MAX_BLOCK)
1551 /* check for wrap through zero on extent logical start block*/
1552 if (b1 + len1 < b1) {
1553 len1 = EXT_MAX_BLOCK - b1;
1554 newext->ee_len = cpu_to_le16(len1);
1558 /* check for overlap */
1559 if (b1 + len1 > b2) {
1560 newext->ee_len = cpu_to_le16(b2 - b1);
1568 * ext4_ext_insert_extent:
1569 * tries to merge requsted extent into the existing extent or
1570 * inserts requested extent as new one into the tree,
1571 * creating new leaf in the no-space case.
1573 int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1574 struct ext4_ext_path *path,
1575 struct ext4_extent *newext, int flag)
1577 struct ext4_extent_header *eh;
1578 struct ext4_extent *ex, *fex;
1579 struct ext4_extent *nearex; /* nearest extent */
1580 struct ext4_ext_path *npath = NULL;
1581 int depth, len, err;
1583 unsigned uninitialized = 0;
1585 BUG_ON(ext4_ext_get_actual_len(newext) == 0);
1586 depth = ext_depth(inode);
1587 ex = path[depth].p_ext;
1588 BUG_ON(path[depth].p_hdr == NULL);
1590 /* try to insert block into found extent and return */
1591 if (ex && (flag != EXT4_GET_BLOCKS_DIO_CREATE_EXT)
1592 && ext4_can_extents_be_merged(inode, ex, newext)) {
1593 ext_debug("append %d block to %d:%d (from %llu)\n",
1594 ext4_ext_get_actual_len(newext),
1595 le32_to_cpu(ex->ee_block),
1596 ext4_ext_get_actual_len(ex), ext_pblock(ex));
1597 err = ext4_ext_get_access(handle, inode, path + depth);
1602 * ext4_can_extents_be_merged should have checked that either
1603 * both extents are uninitialized, or both aren't. Thus we
1604 * need to check only one of them here.
1606 if (ext4_ext_is_uninitialized(ex))
1608 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1609 + ext4_ext_get_actual_len(newext));
1611 ext4_ext_mark_uninitialized(ex);
1612 eh = path[depth].p_hdr;
1618 depth = ext_depth(inode);
1619 eh = path[depth].p_hdr;
1620 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1623 /* probably next leaf has space for us? */
1624 fex = EXT_LAST_EXTENT(eh);
1625 next = ext4_ext_next_leaf_block(inode, path);
1626 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
1627 && next != EXT_MAX_BLOCK) {
1628 ext_debug("next leaf block - %d\n", next);
1629 BUG_ON(npath != NULL);
1630 npath = ext4_ext_find_extent(inode, next, NULL);
1632 return PTR_ERR(npath);
1633 BUG_ON(npath->p_depth != path->p_depth);
1634 eh = npath[depth].p_hdr;
1635 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
1636 ext_debug("next leaf isnt full(%d)\n",
1637 le16_to_cpu(eh->eh_entries));
1641 ext_debug("next leaf has no free space(%d,%d)\n",
1642 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1646 * There is no free space in the found leaf.
1647 * We're gonna add a new leaf in the tree.
1649 err = ext4_ext_create_new_leaf(handle, inode, path, newext);
1652 depth = ext_depth(inode);
1653 eh = path[depth].p_hdr;
1656 nearex = path[depth].p_ext;
1658 err = ext4_ext_get_access(handle, inode, path + depth);
1663 /* there is no extent in this leaf, create first one */
1664 ext_debug("first extent in the leaf: %d:%llu:%d\n",
1665 le32_to_cpu(newext->ee_block),
1667 ext4_ext_get_actual_len(newext));
1668 path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1669 } else if (le32_to_cpu(newext->ee_block)
1670 > le32_to_cpu(nearex->ee_block)) {
1671 /* BUG_ON(newext->ee_block == nearex->ee_block); */
1672 if (nearex != EXT_LAST_EXTENT(eh)) {
1673 len = EXT_MAX_EXTENT(eh) - nearex;
1674 len = (len - 1) * sizeof(struct ext4_extent);
1675 len = len < 0 ? 0 : len;
1676 ext_debug("insert %d:%llu:%d after: nearest 0x%p, "
1677 "move %d from 0x%p to 0x%p\n",
1678 le32_to_cpu(newext->ee_block),
1680 ext4_ext_get_actual_len(newext),
1681 nearex, len, nearex + 1, nearex + 2);
1682 memmove(nearex + 2, nearex + 1, len);
1684 path[depth].p_ext = nearex + 1;
1686 BUG_ON(newext->ee_block == nearex->ee_block);
1687 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1688 len = len < 0 ? 0 : len;
1689 ext_debug("insert %d:%llu:%d before: nearest 0x%p, "
1690 "move %d from 0x%p to 0x%p\n",
1691 le32_to_cpu(newext->ee_block),
1693 ext4_ext_get_actual_len(newext),
1694 nearex, len, nearex + 1, nearex + 2);
1695 memmove(nearex + 1, nearex, len);
1696 path[depth].p_ext = nearex;
1699 le16_add_cpu(&eh->eh_entries, 1);
1700 nearex = path[depth].p_ext;
1701 nearex->ee_block = newext->ee_block;
1702 ext4_ext_store_pblock(nearex, ext_pblock(newext));
1703 nearex->ee_len = newext->ee_len;
1706 /* try to merge extents to the right */
1707 if (flag != EXT4_GET_BLOCKS_DIO_CREATE_EXT)
1708 ext4_ext_try_to_merge(inode, path, nearex);
1710 /* try to merge extents to the left */
1712 /* time to correct all indexes above */
1713 err = ext4_ext_correct_indexes(handle, inode, path);
1717 err = ext4_ext_dirty(handle, inode, path + depth);
1721 ext4_ext_drop_refs(npath);
1724 ext4_ext_invalidate_cache(inode);
1728 int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block,
1729 ext4_lblk_t num, ext_prepare_callback func,
1732 struct ext4_ext_path *path = NULL;
1733 struct ext4_ext_cache cbex;
1734 struct ext4_extent *ex;
1735 ext4_lblk_t next, start = 0, end = 0;
1736 ext4_lblk_t last = block + num;
1737 int depth, exists, err = 0;
1739 BUG_ON(func == NULL);
1740 BUG_ON(inode == NULL);
1742 while (block < last && block != EXT_MAX_BLOCK) {
1744 /* find extent for this block */
1745 path = ext4_ext_find_extent(inode, block, path);
1747 err = PTR_ERR(path);
1752 depth = ext_depth(inode);
1753 BUG_ON(path[depth].p_hdr == NULL);
1754 ex = path[depth].p_ext;
1755 next = ext4_ext_next_allocated_block(path);
1759 /* there is no extent yet, so try to allocate
1760 * all requested space */
1763 } else if (le32_to_cpu(ex->ee_block) > block) {
1764 /* need to allocate space before found extent */
1766 end = le32_to_cpu(ex->ee_block);
1767 if (block + num < end)
1769 } else if (block >= le32_to_cpu(ex->ee_block)
1770 + ext4_ext_get_actual_len(ex)) {
1771 /* need to allocate space after found extent */
1776 } else if (block >= le32_to_cpu(ex->ee_block)) {
1778 * some part of requested space is covered
1782 end = le32_to_cpu(ex->ee_block)
1783 + ext4_ext_get_actual_len(ex);
1784 if (block + num < end)
1790 BUG_ON(end <= start);
1793 cbex.ec_block = start;
1794 cbex.ec_len = end - start;
1796 cbex.ec_type = EXT4_EXT_CACHE_GAP;
1798 cbex.ec_block = le32_to_cpu(ex->ee_block);
1799 cbex.ec_len = ext4_ext_get_actual_len(ex);
1800 cbex.ec_start = ext_pblock(ex);
1801 cbex.ec_type = EXT4_EXT_CACHE_EXTENT;
1804 BUG_ON(cbex.ec_len == 0);
1805 err = func(inode, path, &cbex, ex, cbdata);
1806 ext4_ext_drop_refs(path);
1811 if (err == EXT_REPEAT)
1813 else if (err == EXT_BREAK) {
1818 if (ext_depth(inode) != depth) {
1819 /* depth was changed. we have to realloc path */
1824 block = cbex.ec_block + cbex.ec_len;
1828 ext4_ext_drop_refs(path);
1836 ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
1837 __u32 len, ext4_fsblk_t start, int type)
1839 struct ext4_ext_cache *cex;
1841 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1842 cex = &EXT4_I(inode)->i_cached_extent;
1843 cex->ec_type = type;
1844 cex->ec_block = block;
1846 cex->ec_start = start;
1847 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1851 * ext4_ext_put_gap_in_cache:
1852 * calculate boundaries of the gap that the requested block fits into
1853 * and cache this gap
1856 ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
1859 int depth = ext_depth(inode);
1862 struct ext4_extent *ex;
1864 ex = path[depth].p_ext;
1866 /* there is no extent yet, so gap is [0;-] */
1868 len = EXT_MAX_BLOCK;
1869 ext_debug("cache gap(whole file):");
1870 } else if (block < le32_to_cpu(ex->ee_block)) {
1872 len = le32_to_cpu(ex->ee_block) - block;
1873 ext_debug("cache gap(before): %u [%u:%u]",
1875 le32_to_cpu(ex->ee_block),
1876 ext4_ext_get_actual_len(ex));
1877 } else if (block >= le32_to_cpu(ex->ee_block)
1878 + ext4_ext_get_actual_len(ex)) {
1880 lblock = le32_to_cpu(ex->ee_block)
1881 + ext4_ext_get_actual_len(ex);
1883 next = ext4_ext_next_allocated_block(path);
1884 ext_debug("cache gap(after): [%u:%u] %u",
1885 le32_to_cpu(ex->ee_block),
1886 ext4_ext_get_actual_len(ex),
1888 BUG_ON(next == lblock);
1889 len = next - lblock;
1895 ext_debug(" -> %u:%lu\n", lblock, len);
1896 ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
1900 ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
1901 struct ext4_extent *ex)
1903 struct ext4_ext_cache *cex;
1904 int ret = EXT4_EXT_CACHE_NO;
1907 * We borrow i_block_reservation_lock to protect i_cached_extent
1909 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1910 cex = &EXT4_I(inode)->i_cached_extent;
1912 /* has cache valid data? */
1913 if (cex->ec_type == EXT4_EXT_CACHE_NO)
1916 BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
1917 cex->ec_type != EXT4_EXT_CACHE_EXTENT);
1918 if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
1919 ex->ee_block = cpu_to_le32(cex->ec_block);
1920 ext4_ext_store_pblock(ex, cex->ec_start);
1921 ex->ee_len = cpu_to_le16(cex->ec_len);
1922 ext_debug("%u cached by %u:%u:%llu\n",
1924 cex->ec_block, cex->ec_len, cex->ec_start);
1928 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1934 * removes index from the index block.
1935 * It's used in truncate case only, thus all requests are for
1936 * last index in the block only.
1938 static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
1939 struct ext4_ext_path *path)
1941 struct buffer_head *bh;
1945 /* free index block */
1947 leaf = idx_pblock(path->p_idx);
1948 BUG_ON(path->p_hdr->eh_entries == 0);
1949 err = ext4_ext_get_access(handle, inode, path);
1952 le16_add_cpu(&path->p_hdr->eh_entries, -1);
1953 err = ext4_ext_dirty(handle, inode, path);
1956 ext_debug("index is empty, remove it, free block %llu\n", leaf);
1957 bh = sb_find_get_block(inode->i_sb, leaf);
1958 ext4_forget(handle, 1, inode, bh, leaf);
1959 ext4_free_blocks(handle, inode, leaf, 1, 1);
1964 * ext4_ext_calc_credits_for_single_extent:
1965 * This routine returns max. credits that needed to insert an extent
1966 * to the extent tree.
1967 * When pass the actual path, the caller should calculate credits
1970 int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
1971 struct ext4_ext_path *path)
1974 int depth = ext_depth(inode);
1977 /* probably there is space in leaf? */
1978 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
1979 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
1982 * There are some space in the leaf tree, no
1983 * need to account for leaf block credit
1985 * bitmaps and block group descriptor blocks
1986 * and other metadat blocks still need to be
1989 /* 1 bitmap, 1 block group descriptor */
1990 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
1995 return ext4_chunk_trans_blocks(inode, nrblocks);
1999 * How many index/leaf blocks need to change/allocate to modify nrblocks?
2001 * if nrblocks are fit in a single extent (chunk flag is 1), then
2002 * in the worse case, each tree level index/leaf need to be changed
2003 * if the tree split due to insert a new extent, then the old tree
2004 * index/leaf need to be updated too
2006 * If the nrblocks are discontiguous, they could cause
2007 * the whole tree split more than once, but this is really rare.
2009 int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
2012 int depth = ext_depth(inode);
2022 static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
2023 struct ext4_extent *ex,
2024 ext4_lblk_t from, ext4_lblk_t to)
2026 struct buffer_head *bh;
2027 unsigned short ee_len = ext4_ext_get_actual_len(ex);
2028 int i, metadata = 0;
2030 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2032 #ifdef EXTENTS_STATS
2034 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2035 spin_lock(&sbi->s_ext_stats_lock);
2036 sbi->s_ext_blocks += ee_len;
2037 sbi->s_ext_extents++;
2038 if (ee_len < sbi->s_ext_min)
2039 sbi->s_ext_min = ee_len;
2040 if (ee_len > sbi->s_ext_max)
2041 sbi->s_ext_max = ee_len;
2042 if (ext_depth(inode) > sbi->s_depth_max)
2043 sbi->s_depth_max = ext_depth(inode);
2044 spin_unlock(&sbi->s_ext_stats_lock);
2047 if (from >= le32_to_cpu(ex->ee_block)
2048 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
2053 num = le32_to_cpu(ex->ee_block) + ee_len - from;
2054 start = ext_pblock(ex) + ee_len - num;
2055 ext_debug("free last %u blocks starting %llu\n", num, start);
2056 for (i = 0; i < num; i++) {
2057 bh = sb_find_get_block(inode->i_sb, start + i);
2058 ext4_forget(handle, metadata, inode, bh, start + i);
2060 ext4_free_blocks(handle, inode, start, num, metadata);
2061 } else if (from == le32_to_cpu(ex->ee_block)
2062 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
2063 printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n",
2064 from, to, le32_to_cpu(ex->ee_block), ee_len);
2066 printk(KERN_INFO "strange request: removal(2) "
2067 "%u-%u from %u:%u\n",
2068 from, to, le32_to_cpu(ex->ee_block), ee_len);
2074 ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
2075 struct ext4_ext_path *path, ext4_lblk_t start)
2077 int err = 0, correct_index = 0;
2078 int depth = ext_depth(inode), credits;
2079 struct ext4_extent_header *eh;
2080 ext4_lblk_t a, b, block;
2082 ext4_lblk_t ex_ee_block;
2083 unsigned short ex_ee_len;
2084 unsigned uninitialized = 0;
2085 struct ext4_extent *ex;
2087 /* the header must be checked already in ext4_ext_remove_space() */
2088 ext_debug("truncate since %u in leaf\n", start);
2089 if (!path[depth].p_hdr)
2090 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2091 eh = path[depth].p_hdr;
2094 /* find where to start removing */
2095 ex = EXT_LAST_EXTENT(eh);
2097 ex_ee_block = le32_to_cpu(ex->ee_block);
2098 ex_ee_len = ext4_ext_get_actual_len(ex);
2100 while (ex >= EXT_FIRST_EXTENT(eh) &&
2101 ex_ee_block + ex_ee_len > start) {
2103 if (ext4_ext_is_uninitialized(ex))
2108 ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len);
2109 path[depth].p_ext = ex;
2111 a = ex_ee_block > start ? ex_ee_block : start;
2112 b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
2113 ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
2115 ext_debug(" border %u:%u\n", a, b);
2117 if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
2121 } else if (a != ex_ee_block) {
2122 /* remove tail of the extent */
2123 block = ex_ee_block;
2125 } else if (b != ex_ee_block + ex_ee_len - 1) {
2126 /* remove head of the extent */
2129 /* there is no "make a hole" API yet */
2132 /* remove whole extent: excellent! */
2133 block = ex_ee_block;
2135 BUG_ON(a != ex_ee_block);
2136 BUG_ON(b != ex_ee_block + ex_ee_len - 1);
2140 * 3 for leaf, sb, and inode plus 2 (bmap and group
2141 * descriptor) for each block group; assume two block
2142 * groups plus ex_ee_len/blocks_per_block_group for
2145 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
2146 if (ex == EXT_FIRST_EXTENT(eh)) {
2148 credits += (ext_depth(inode)) + 1;
2150 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
2152 err = ext4_ext_truncate_extend_restart(handle, inode, credits);
2156 err = ext4_ext_get_access(handle, inode, path + depth);
2160 err = ext4_remove_blocks(handle, inode, ex, a, b);
2165 /* this extent is removed; mark slot entirely unused */
2166 ext4_ext_store_pblock(ex, 0);
2167 le16_add_cpu(&eh->eh_entries, -1);
2170 ex->ee_block = cpu_to_le32(block);
2171 ex->ee_len = cpu_to_le16(num);
2173 * Do not mark uninitialized if all the blocks in the
2174 * extent have been removed.
2176 if (uninitialized && num)
2177 ext4_ext_mark_uninitialized(ex);
2179 err = ext4_ext_dirty(handle, inode, path + depth);
2183 ext_debug("new extent: %u:%u:%llu\n", block, num,
2186 ex_ee_block = le32_to_cpu(ex->ee_block);
2187 ex_ee_len = ext4_ext_get_actual_len(ex);
2190 if (correct_index && eh->eh_entries)
2191 err = ext4_ext_correct_indexes(handle, inode, path);
2193 /* if this leaf is free, then we should
2194 * remove it from index block above */
2195 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
2196 err = ext4_ext_rm_idx(handle, inode, path + depth);
2203 * ext4_ext_more_to_rm:
2204 * returns 1 if current index has to be freed (even partial)
2207 ext4_ext_more_to_rm(struct ext4_ext_path *path)
2209 BUG_ON(path->p_idx == NULL);
2211 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2215 * if truncate on deeper level happened, it wasn't partial,
2216 * so we have to consider current index for truncation
2218 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2223 static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
2225 struct super_block *sb = inode->i_sb;
2226 int depth = ext_depth(inode);
2227 struct ext4_ext_path *path;
2231 ext_debug("truncate since %u\n", start);
2233 /* probably first extent we're gonna free will be last in block */
2234 handle = ext4_journal_start(inode, depth + 1);
2236 return PTR_ERR(handle);
2238 ext4_ext_invalidate_cache(inode);
2241 * We start scanning from right side, freeing all the blocks
2242 * after i_size and walking into the tree depth-wise.
2244 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
2246 ext4_journal_stop(handle);
2249 path[0].p_hdr = ext_inode_hdr(inode);
2250 if (ext4_ext_check(inode, path[0].p_hdr, depth)) {
2254 path[0].p_depth = depth;
2256 while (i >= 0 && err == 0) {
2258 /* this is leaf block */
2259 err = ext4_ext_rm_leaf(handle, inode, path, start);
2260 /* root level has p_bh == NULL, brelse() eats this */
2261 brelse(path[i].p_bh);
2262 path[i].p_bh = NULL;
2267 /* this is index block */
2268 if (!path[i].p_hdr) {
2269 ext_debug("initialize header\n");
2270 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
2273 if (!path[i].p_idx) {
2274 /* this level hasn't been touched yet */
2275 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2276 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2277 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2279 le16_to_cpu(path[i].p_hdr->eh_entries));
2281 /* we were already here, see at next index */
2285 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2286 i, EXT_FIRST_INDEX(path[i].p_hdr),
2288 if (ext4_ext_more_to_rm(path + i)) {
2289 struct buffer_head *bh;
2290 /* go to the next level */
2291 ext_debug("move to level %d (block %llu)\n",
2292 i + 1, idx_pblock(path[i].p_idx));
2293 memset(path + i + 1, 0, sizeof(*path));
2294 bh = sb_bread(sb, idx_pblock(path[i].p_idx));
2296 /* should we reset i_size? */
2300 if (WARN_ON(i + 1 > depth)) {
2304 if (ext4_ext_check(inode, ext_block_hdr(bh),
2309 path[i + 1].p_bh = bh;
2311 /* save actual number of indexes since this
2312 * number is changed at the next iteration */
2313 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2316 /* we finished processing this index, go up */
2317 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
2318 /* index is empty, remove it;
2319 * handle must be already prepared by the
2320 * truncatei_leaf() */
2321 err = ext4_ext_rm_idx(handle, inode, path + i);
2323 /* root level has p_bh == NULL, brelse() eats this */
2324 brelse(path[i].p_bh);
2325 path[i].p_bh = NULL;
2327 ext_debug("return to level %d\n", i);
2331 /* TODO: flexible tree reduction should be here */
2332 if (path->p_hdr->eh_entries == 0) {
2334 * truncate to zero freed all the tree,
2335 * so we need to correct eh_depth
2337 err = ext4_ext_get_access(handle, inode, path);
2339 ext_inode_hdr(inode)->eh_depth = 0;
2340 ext_inode_hdr(inode)->eh_max =
2341 cpu_to_le16(ext4_ext_space_root(inode));
2342 err = ext4_ext_dirty(handle, inode, path);
2346 ext4_ext_drop_refs(path);
2348 ext4_journal_stop(handle);
2354 * called at mount time
2356 void ext4_ext_init(struct super_block *sb)
2359 * possible initialization would be here
2362 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
2363 printk(KERN_INFO "EXT4-fs: file extents enabled");
2364 #ifdef AGGRESSIVE_TEST
2365 printk(", aggressive tests");
2367 #ifdef CHECK_BINSEARCH
2368 printk(", check binsearch");
2370 #ifdef EXTENTS_STATS
2374 #ifdef EXTENTS_STATS
2375 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2376 EXT4_SB(sb)->s_ext_min = 1 << 30;
2377 EXT4_SB(sb)->s_ext_max = 0;
2383 * called at umount time
2385 void ext4_ext_release(struct super_block *sb)
2387 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
2390 #ifdef EXTENTS_STATS
2391 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2392 struct ext4_sb_info *sbi = EXT4_SB(sb);
2393 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2394 sbi->s_ext_blocks, sbi->s_ext_extents,
2395 sbi->s_ext_blocks / sbi->s_ext_extents);
2396 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2397 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2402 static void bi_complete(struct bio *bio, int error)
2404 complete((struct completion *)bio->bi_private);
2407 /* FIXME!! we need to try to merge to left or right after zero-out */
2408 static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2412 int blkbits, blocksize;
2414 struct completion event;
2415 unsigned int ee_len, len, done, offset;
2418 blkbits = inode->i_blkbits;
2419 blocksize = inode->i_sb->s_blocksize;
2420 ee_len = ext4_ext_get_actual_len(ex);
2421 ee_pblock = ext_pblock(ex);
2423 /* convert ee_pblock to 512 byte sectors */
2424 ee_pblock = ee_pblock << (blkbits - 9);
2426 while (ee_len > 0) {
2428 if (ee_len > BIO_MAX_PAGES)
2429 len = BIO_MAX_PAGES;
2433 bio = bio_alloc(GFP_NOIO, len);
2434 bio->bi_sector = ee_pblock;
2435 bio->bi_bdev = inode->i_sb->s_bdev;
2439 while (done < len) {
2440 ret = bio_add_page(bio, ZERO_PAGE(0),
2442 if (ret != blocksize) {
2444 * We can't add any more pages because of
2445 * hardware limitations. Start a new bio.
2450 offset += blocksize;
2451 if (offset >= PAGE_CACHE_SIZE)
2455 init_completion(&event);
2456 bio->bi_private = &event;
2457 bio->bi_end_io = bi_complete;
2458 submit_bio(WRITE, bio);
2459 wait_for_completion(&event);
2461 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
2469 ee_pblock += done << (blkbits - 9);
2474 #define EXT4_EXT_ZERO_LEN 7
2476 * This function is called by ext4_ext_get_blocks() if someone tries to write
2477 * to an uninitialized extent. It may result in splitting the uninitialized
2478 * extent into multiple extents (upto three - one initialized and two
2480 * There are three possibilities:
2481 * a> There is no split required: Entire extent should be initialized
2482 * b> Splits in two extents: Write is happening at either end of the extent
2483 * c> Splits in three extents: Somone is writing in middle of the extent
2485 static int ext4_ext_convert_to_initialized(handle_t *handle,
2486 struct inode *inode,
2487 struct ext4_ext_path *path,
2489 unsigned int max_blocks)
2491 struct ext4_extent *ex, newex, orig_ex;
2492 struct ext4_extent *ex1 = NULL;
2493 struct ext4_extent *ex2 = NULL;
2494 struct ext4_extent *ex3 = NULL;
2495 struct ext4_extent_header *eh;
2496 ext4_lblk_t ee_block;
2497 unsigned int allocated, ee_len, depth;
2498 ext4_fsblk_t newblock;
2502 depth = ext_depth(inode);
2503 eh = path[depth].p_hdr;
2504 ex = path[depth].p_ext;
2505 ee_block = le32_to_cpu(ex->ee_block);
2506 ee_len = ext4_ext_get_actual_len(ex);
2507 allocated = ee_len - (iblock - ee_block);
2508 newblock = iblock - ee_block + ext_pblock(ex);
2510 orig_ex.ee_block = ex->ee_block;
2511 orig_ex.ee_len = cpu_to_le16(ee_len);
2512 ext4_ext_store_pblock(&orig_ex, ext_pblock(ex));
2514 err = ext4_ext_get_access(handle, inode, path + depth);
2517 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
2518 if (ee_len <= 2*EXT4_EXT_ZERO_LEN) {
2519 err = ext4_ext_zeroout(inode, &orig_ex);
2521 goto fix_extent_len;
2522 /* update the extent length and mark as initialized */
2523 ex->ee_block = orig_ex.ee_block;
2524 ex->ee_len = orig_ex.ee_len;
2525 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2526 ext4_ext_dirty(handle, inode, path + depth);
2527 /* zeroed the full extent */
2531 /* ex1: ee_block to iblock - 1 : uninitialized */
2532 if (iblock > ee_block) {
2534 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2535 ext4_ext_mark_uninitialized(ex1);
2539 * for sanity, update the length of the ex2 extent before
2540 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2541 * overlap of blocks.
2543 if (!ex1 && allocated > max_blocks)
2544 ex2->ee_len = cpu_to_le16(max_blocks);
2545 /* ex3: to ee_block + ee_len : uninitialised */
2546 if (allocated > max_blocks) {
2547 unsigned int newdepth;
2548 /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */
2549 if (allocated <= EXT4_EXT_ZERO_LEN) {
2551 * iblock == ee_block is handled by the zerouout
2553 * Mark first half uninitialized.
2554 * Mark second half initialized and zero out the
2555 * initialized extent
2557 ex->ee_block = orig_ex.ee_block;
2558 ex->ee_len = cpu_to_le16(ee_len - allocated);
2559 ext4_ext_mark_uninitialized(ex);
2560 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2561 ext4_ext_dirty(handle, inode, path + depth);
2564 ex3->ee_block = cpu_to_le32(iblock);
2565 ext4_ext_store_pblock(ex3, newblock);
2566 ex3->ee_len = cpu_to_le16(allocated);
2567 err = ext4_ext_insert_extent(handle, inode, path,
2569 if (err == -ENOSPC) {
2570 err = ext4_ext_zeroout(inode, &orig_ex);
2572 goto fix_extent_len;
2573 ex->ee_block = orig_ex.ee_block;
2574 ex->ee_len = orig_ex.ee_len;
2575 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2576 ext4_ext_dirty(handle, inode, path + depth);
2577 /* blocks available from iblock */
2581 goto fix_extent_len;
2584 * We need to zero out the second half because
2585 * an fallocate request can update file size and
2586 * converting the second half to initialized extent
2587 * implies that we can leak some junk data to user
2590 err = ext4_ext_zeroout(inode, ex3);
2593 * We should actually mark the
2594 * second half as uninit and return error
2595 * Insert would have changed the extent
2597 depth = ext_depth(inode);
2598 ext4_ext_drop_refs(path);
2599 path = ext4_ext_find_extent(inode,
2602 err = PTR_ERR(path);
2605 /* get the second half extent details */
2606 ex = path[depth].p_ext;
2607 err = ext4_ext_get_access(handle, inode,
2611 ext4_ext_mark_uninitialized(ex);
2612 ext4_ext_dirty(handle, inode, path + depth);
2616 /* zeroed the second half */
2620 ex3->ee_block = cpu_to_le32(iblock + max_blocks);
2621 ext4_ext_store_pblock(ex3, newblock + max_blocks);
2622 ex3->ee_len = cpu_to_le16(allocated - max_blocks);
2623 ext4_ext_mark_uninitialized(ex3);
2624 err = ext4_ext_insert_extent(handle, inode, path, ex3, 0);
2625 if (err == -ENOSPC) {
2626 err = ext4_ext_zeroout(inode, &orig_ex);
2628 goto fix_extent_len;
2629 /* update the extent length and mark as initialized */
2630 ex->ee_block = orig_ex.ee_block;
2631 ex->ee_len = orig_ex.ee_len;
2632 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2633 ext4_ext_dirty(handle, inode, path + depth);
2634 /* zeroed the full extent */
2635 /* blocks available from iblock */
2639 goto fix_extent_len;
2641 * The depth, and hence eh & ex might change
2642 * as part of the insert above.
2644 newdepth = ext_depth(inode);
2646 * update the extent length after successful insert of the
2649 orig_ex.ee_len = cpu_to_le16(ee_len -
2650 ext4_ext_get_actual_len(ex3));
2652 ext4_ext_drop_refs(path);
2653 path = ext4_ext_find_extent(inode, iblock, path);
2655 err = PTR_ERR(path);
2658 eh = path[depth].p_hdr;
2659 ex = path[depth].p_ext;
2663 err = ext4_ext_get_access(handle, inode, path + depth);
2667 allocated = max_blocks;
2669 /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying
2670 * to insert a extent in the middle zerout directly
2671 * otherwise give the extent a chance to merge to left
2673 if (le16_to_cpu(orig_ex.ee_len) <= EXT4_EXT_ZERO_LEN &&
2674 iblock != ee_block) {
2675 err = ext4_ext_zeroout(inode, &orig_ex);
2677 goto fix_extent_len;
2678 /* update the extent length and mark as initialized */
2679 ex->ee_block = orig_ex.ee_block;
2680 ex->ee_len = orig_ex.ee_len;
2681 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2682 ext4_ext_dirty(handle, inode, path + depth);
2683 /* zero out the first half */
2684 /* blocks available from iblock */
2689 * If there was a change of depth as part of the
2690 * insertion of ex3 above, we need to update the length
2691 * of the ex1 extent again here
2693 if (ex1 && ex1 != ex) {
2695 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2696 ext4_ext_mark_uninitialized(ex1);
2699 /* ex2: iblock to iblock + maxblocks-1 : initialised */
2700 ex2->ee_block = cpu_to_le32(iblock);
2701 ext4_ext_store_pblock(ex2, newblock);
2702 ex2->ee_len = cpu_to_le16(allocated);
2706 * New (initialized) extent starts from the first block
2707 * in the current extent. i.e., ex2 == ex
2708 * We have to see if it can be merged with the extent
2711 if (ex2 > EXT_FIRST_EXTENT(eh)) {
2713 * To merge left, pass "ex2 - 1" to try_to_merge(),
2714 * since it merges towards right _only_.
2716 ret = ext4_ext_try_to_merge(inode, path, ex2 - 1);
2718 err = ext4_ext_correct_indexes(handle, inode, path);
2721 depth = ext_depth(inode);
2726 * Try to Merge towards right. This might be required
2727 * only when the whole extent is being written to.
2728 * i.e. ex2 == ex and ex3 == NULL.
2731 ret = ext4_ext_try_to_merge(inode, path, ex2);
2733 err = ext4_ext_correct_indexes(handle, inode, path);
2738 /* Mark modified extent as dirty */
2739 err = ext4_ext_dirty(handle, inode, path + depth);
2742 err = ext4_ext_insert_extent(handle, inode, path, &newex, 0);
2743 if (err == -ENOSPC) {
2744 err = ext4_ext_zeroout(inode, &orig_ex);
2746 goto fix_extent_len;
2747 /* update the extent length and mark as initialized */
2748 ex->ee_block = orig_ex.ee_block;
2749 ex->ee_len = orig_ex.ee_len;
2750 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2751 ext4_ext_dirty(handle, inode, path + depth);
2752 /* zero out the first half */
2755 goto fix_extent_len;
2757 return err ? err : allocated;
2760 ex->ee_block = orig_ex.ee_block;
2761 ex->ee_len = orig_ex.ee_len;
2762 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2763 ext4_ext_mark_uninitialized(ex);
2764 ext4_ext_dirty(handle, inode, path + depth);
2769 * This function is called by ext4_ext_get_blocks() from
2770 * ext4_get_blocks_dio_write() when DIO to write
2771 * to an uninitialized extent.
2773 * Writing to an uninitized extent may result in splitting the uninitialized
2774 * extent into multiple /intialized unintialized extents (up to three)
2775 * There are three possibilities:
2776 * a> There is no split required: Entire extent should be uninitialized
2777 * b> Splits in two extents: Write is happening at either end of the extent
2778 * c> Splits in three extents: Somone is writing in middle of the extent
2780 * One of more index blocks maybe needed if the extent tree grow after
2781 * the unintialized extent split. To prevent ENOSPC occur at the IO
2782 * complete, we need to split the uninitialized extent before DIO submit
2783 * the IO. The uninitilized extent called at this time will be split
2784 * into three uninitialized extent(at most). After IO complete, the part
2785 * being filled will be convert to initialized by the end_io callback function
2786 * via ext4_convert_unwritten_extents().
2788 * Returns the size of uninitialized extent to be written on success.
2790 static int ext4_split_unwritten_extents(handle_t *handle,
2791 struct inode *inode,
2792 struct ext4_ext_path *path,
2794 unsigned int max_blocks,
2797 struct ext4_extent *ex, newex, orig_ex;
2798 struct ext4_extent *ex1 = NULL;
2799 struct ext4_extent *ex2 = NULL;
2800 struct ext4_extent *ex3 = NULL;
2801 struct ext4_extent_header *eh;
2802 ext4_lblk_t ee_block;
2803 unsigned int allocated, ee_len, depth;
2804 ext4_fsblk_t newblock;
2807 ext_debug("ext4_split_unwritten_extents: inode %lu,"
2808 "iblock %llu, max_blocks %u\n", inode->i_ino,
2809 (unsigned long long)iblock, max_blocks);
2810 depth = ext_depth(inode);
2811 eh = path[depth].p_hdr;
2812 ex = path[depth].p_ext;
2813 ee_block = le32_to_cpu(ex->ee_block);
2814 ee_len = ext4_ext_get_actual_len(ex);
2815 allocated = ee_len - (iblock - ee_block);
2816 newblock = iblock - ee_block + ext_pblock(ex);
2818 orig_ex.ee_block = ex->ee_block;
2819 orig_ex.ee_len = cpu_to_le16(ee_len);
2820 ext4_ext_store_pblock(&orig_ex, ext_pblock(ex));
2823 * If the uninitialized extent begins at the same logical
2824 * block where the write begins, and the write completely
2825 * covers the extent, then we don't need to split it.
2827 if ((iblock == ee_block) && (allocated <= max_blocks))
2830 err = ext4_ext_get_access(handle, inode, path + depth);
2833 /* ex1: ee_block to iblock - 1 : uninitialized */
2834 if (iblock > ee_block) {
2836 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2837 ext4_ext_mark_uninitialized(ex1);
2841 * for sanity, update the length of the ex2 extent before
2842 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2843 * overlap of blocks.
2845 if (!ex1 && allocated > max_blocks)
2846 ex2->ee_len = cpu_to_le16(max_blocks);
2847 /* ex3: to ee_block + ee_len : uninitialised */
2848 if (allocated > max_blocks) {
2849 unsigned int newdepth;
2851 ex3->ee_block = cpu_to_le32(iblock + max_blocks);
2852 ext4_ext_store_pblock(ex3, newblock + max_blocks);
2853 ex3->ee_len = cpu_to_le16(allocated - max_blocks);
2854 ext4_ext_mark_uninitialized(ex3);
2855 err = ext4_ext_insert_extent(handle, inode, path, ex3, flags);
2856 if (err == -ENOSPC) {
2857 err = ext4_ext_zeroout(inode, &orig_ex);
2859 goto fix_extent_len;
2860 /* update the extent length and mark as initialized */
2861 ex->ee_block = orig_ex.ee_block;
2862 ex->ee_len = orig_ex.ee_len;
2863 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2864 ext4_ext_dirty(handle, inode, path + depth);
2865 /* zeroed the full extent */
2866 /* blocks available from iblock */
2870 goto fix_extent_len;
2872 * The depth, and hence eh & ex might change
2873 * as part of the insert above.
2875 newdepth = ext_depth(inode);
2877 * update the extent length after successful insert of the
2880 orig_ex.ee_len = cpu_to_le16(ee_len -
2881 ext4_ext_get_actual_len(ex3));
2883 ext4_ext_drop_refs(path);
2884 path = ext4_ext_find_extent(inode, iblock, path);
2886 err = PTR_ERR(path);
2889 eh = path[depth].p_hdr;
2890 ex = path[depth].p_ext;
2894 err = ext4_ext_get_access(handle, inode, path + depth);
2898 allocated = max_blocks;
2901 * If there was a change of depth as part of the
2902 * insertion of ex3 above, we need to update the length
2903 * of the ex1 extent again here
2905 if (ex1 && ex1 != ex) {
2907 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2908 ext4_ext_mark_uninitialized(ex1);
2912 * ex2: iblock to iblock + maxblocks-1 : to be direct IO written,
2913 * uninitialised still.
2915 ex2->ee_block = cpu_to_le32(iblock);
2916 ext4_ext_store_pblock(ex2, newblock);
2917 ex2->ee_len = cpu_to_le16(allocated);
2918 ext4_ext_mark_uninitialized(ex2);
2921 /* Mark modified extent as dirty */
2922 err = ext4_ext_dirty(handle, inode, path + depth);
2923 ext_debug("out here\n");
2926 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
2927 if (err == -ENOSPC) {
2928 err = ext4_ext_zeroout(inode, &orig_ex);
2930 goto fix_extent_len;
2931 /* update the extent length and mark as initialized */
2932 ex->ee_block = orig_ex.ee_block;
2933 ex->ee_len = orig_ex.ee_len;
2934 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2935 ext4_ext_dirty(handle, inode, path + depth);
2936 /* zero out the first half */
2939 goto fix_extent_len;
2941 ext4_ext_show_leaf(inode, path);
2942 return err ? err : allocated;
2945 ex->ee_block = orig_ex.ee_block;
2946 ex->ee_len = orig_ex.ee_len;
2947 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2948 ext4_ext_mark_uninitialized(ex);
2949 ext4_ext_dirty(handle, inode, path + depth);
2952 static int ext4_convert_unwritten_extents_dio(handle_t *handle,
2953 struct inode *inode,
2954 struct ext4_ext_path *path)
2956 struct ext4_extent *ex;
2957 struct ext4_extent_header *eh;
2962 depth = ext_depth(inode);
2963 eh = path[depth].p_hdr;
2964 ex = path[depth].p_ext;
2966 err = ext4_ext_get_access(handle, inode, path + depth);
2969 /* first mark the extent as initialized */
2970 ext4_ext_mark_initialized(ex);
2973 * We have to see if it can be merged with the extent
2976 if (ex > EXT_FIRST_EXTENT(eh)) {
2978 * To merge left, pass "ex - 1" to try_to_merge(),
2979 * since it merges towards right _only_.
2981 ret = ext4_ext_try_to_merge(inode, path, ex - 1);
2983 err = ext4_ext_correct_indexes(handle, inode, path);
2986 depth = ext_depth(inode);
2991 * Try to Merge towards right.
2993 ret = ext4_ext_try_to_merge(inode, path, ex);
2995 err = ext4_ext_correct_indexes(handle, inode, path);
2998 depth = ext_depth(inode);
3000 /* Mark modified extent as dirty */
3001 err = ext4_ext_dirty(handle, inode, path + depth);
3003 ext4_ext_show_leaf(inode, path);
3008 ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode,
3009 ext4_lblk_t iblock, unsigned int max_blocks,
3010 struct ext4_ext_path *path, int flags,
3011 unsigned int allocated, struct buffer_head *bh_result,
3012 ext4_fsblk_t newblock)
3016 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
3018 ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical"
3019 "block %llu, max_blocks %u, flags %d, allocated %u",
3020 inode->i_ino, (unsigned long long)iblock, max_blocks,
3022 ext4_ext_show_leaf(inode, path);
3024 /* DIO get_block() before submit the IO, split the extent */
3025 if (flags == EXT4_GET_BLOCKS_DIO_CREATE_EXT) {
3026 ret = ext4_split_unwritten_extents(handle,
3027 inode, path, iblock,
3030 * Flag the inode(non aio case) or end_io struct (aio case)
3031 * that this IO needs to convertion to written when IO is
3035 io->flag = DIO_AIO_UNWRITTEN;
3037 EXT4_I(inode)->i_state |= EXT4_STATE_DIO_UNWRITTEN;
3040 /* async DIO end_io complete, convert the filled extent to written */
3041 if (flags == EXT4_GET_BLOCKS_DIO_CONVERT_EXT) {
3042 ret = ext4_convert_unwritten_extents_dio(handle, inode,
3045 ext4_update_inode_fsync_trans(handle, inode, 1);
3048 /* buffered IO case */
3050 * repeat fallocate creation request
3051 * we already have an unwritten extent
3053 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT)
3056 /* buffered READ or buffered write_begin() lookup */
3057 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3059 * We have blocks reserved already. We
3060 * return allocated blocks so that delalloc
3061 * won't do block reservation for us. But
3062 * the buffer head will be unmapped so that
3063 * a read from the block returns 0s.
3065 set_buffer_unwritten(bh_result);
3069 /* buffered write, writepage time, convert*/
3070 ret = ext4_ext_convert_to_initialized(handle, inode,
3074 ext4_update_inode_fsync_trans(handle, inode, 1);
3081 set_buffer_new(bh_result);
3083 set_buffer_mapped(bh_result);
3085 if (allocated > max_blocks)
3086 allocated = max_blocks;
3087 ext4_ext_show_leaf(inode, path);
3088 bh_result->b_bdev = inode->i_sb->s_bdev;
3089 bh_result->b_blocknr = newblock;
3092 ext4_ext_drop_refs(path);
3095 return err ? err : allocated;
3098 * Block allocation/map/preallocation routine for extents based files
3101 * Need to be called with
3102 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
3103 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
3105 * return > 0, number of of blocks already mapped/allocated
3106 * if create == 0 and these are pre-allocated blocks
3107 * buffer head is unmapped
3108 * otherwise blocks are mapped
3110 * return = 0, if plain look up failed (blocks have not been allocated)
3111 * buffer head is unmapped
3113 * return < 0, error case.
3115 int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
3117 unsigned int max_blocks, struct buffer_head *bh_result,
3120 struct ext4_ext_path *path = NULL;
3121 struct ext4_extent_header *eh;
3122 struct ext4_extent newex, *ex;
3123 ext4_fsblk_t newblock;
3124 int err = 0, depth, ret, cache_type;
3125 unsigned int allocated = 0;
3126 struct ext4_allocation_request ar;
3127 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
3129 __clear_bit(BH_New, &bh_result->b_state);
3130 ext_debug("blocks %u/%u requested for inode %u\n",
3131 iblock, max_blocks, inode->i_ino);
3133 /* check in cache */
3134 cache_type = ext4_ext_in_cache(inode, iblock, &newex);
3136 if (cache_type == EXT4_EXT_CACHE_GAP) {
3137 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3139 * block isn't allocated yet and
3140 * user doesn't want to allocate it
3144 /* we should allocate requested block */
3145 } else if (cache_type == EXT4_EXT_CACHE_EXTENT) {
3146 /* block is already allocated */
3148 - le32_to_cpu(newex.ee_block)
3149 + ext_pblock(&newex);
3150 /* number of remaining blocks in the extent */
3151 allocated = ext4_ext_get_actual_len(&newex) -
3152 (iblock - le32_to_cpu(newex.ee_block));
3159 /* find extent for this block */
3160 path = ext4_ext_find_extent(inode, iblock, NULL);
3162 err = PTR_ERR(path);
3167 depth = ext_depth(inode);
3170 * consistent leaf must not be empty;
3171 * this situation is possible, though, _during_ tree modification;
3172 * this is why assert can't be put in ext4_ext_find_extent()
3174 BUG_ON(path[depth].p_ext == NULL && depth != 0);
3175 eh = path[depth].p_hdr;
3177 ex = path[depth].p_ext;
3179 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
3180 ext4_fsblk_t ee_start = ext_pblock(ex);
3181 unsigned short ee_len;
3184 * Uninitialized extents are treated as holes, except that
3185 * we split out initialized portions during a write.
3187 ee_len = ext4_ext_get_actual_len(ex);
3188 /* if found extent covers block, simply return it */
3189 if (iblock >= ee_block && iblock < ee_block + ee_len) {
3190 newblock = iblock - ee_block + ee_start;
3191 /* number of remaining blocks in the extent */
3192 allocated = ee_len - (iblock - ee_block);
3193 ext_debug("%u fit into %lu:%d -> %llu\n", iblock,
3194 ee_block, ee_len, newblock);
3196 /* Do not put uninitialized extent in the cache */
3197 if (!ext4_ext_is_uninitialized(ex)) {
3198 ext4_ext_put_in_cache(inode, ee_block,
3200 EXT4_EXT_CACHE_EXTENT);
3203 ret = ext4_ext_handle_uninitialized_extents(handle,
3204 inode, iblock, max_blocks, path,
3205 flags, allocated, bh_result, newblock);
3211 * requested block isn't allocated yet;
3212 * we couldn't try to create block if create flag is zero
3214 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3216 * put just found gap into cache to speed up
3217 * subsequent requests
3219 ext4_ext_put_gap_in_cache(inode, path, iblock);
3223 * Okay, we need to do block allocation.
3226 /* find neighbour allocated blocks */
3228 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
3232 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright);
3237 * See if request is beyond maximum number of blocks we can have in
3238 * a single extent. For an initialized extent this limit is
3239 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
3240 * EXT_UNINIT_MAX_LEN.
3242 if (max_blocks > EXT_INIT_MAX_LEN &&
3243 !(flags & EXT4_GET_BLOCKS_UNINIT_EXT))
3244 max_blocks = EXT_INIT_MAX_LEN;
3245 else if (max_blocks > EXT_UNINIT_MAX_LEN &&
3246 (flags & EXT4_GET_BLOCKS_UNINIT_EXT))
3247 max_blocks = EXT_UNINIT_MAX_LEN;
3249 /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */
3250 newex.ee_block = cpu_to_le32(iblock);
3251 newex.ee_len = cpu_to_le16(max_blocks);
3252 err = ext4_ext_check_overlap(inode, &newex, path);
3254 allocated = ext4_ext_get_actual_len(&newex);
3256 allocated = max_blocks;
3258 /* allocate new block */
3260 ar.goal = ext4_ext_find_goal(inode, path, iblock);
3261 ar.logical = iblock;
3263 if (S_ISREG(inode->i_mode))
3264 ar.flags = EXT4_MB_HINT_DATA;
3266 /* disable in-core preallocation for non-regular files */
3268 newblock = ext4_mb_new_blocks(handle, &ar, &err);
3271 ext_debug("allocate new block: goal %llu, found %llu/%lu\n",
3272 ar.goal, newblock, allocated);
3274 /* try to insert new extent into found leaf and return */
3275 ext4_ext_store_pblock(&newex, newblock);
3276 newex.ee_len = cpu_to_le16(ar.len);
3277 /* Mark uninitialized */
3278 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){
3279 ext4_ext_mark_uninitialized(&newex);
3281 * io_end structure was created for every async
3282 * direct IO write to the middle of the file.
3283 * To avoid unecessary convertion for every aio dio rewrite
3284 * to the mid of file, here we flag the IO that is really
3285 * need the convertion.
3286 * For non asycn direct IO case, flag the inode state
3287 * that we need to perform convertion when IO is done.
3289 if (flags == EXT4_GET_BLOCKS_DIO_CREATE_EXT) {
3291 io->flag = DIO_AIO_UNWRITTEN;
3293 EXT4_I(inode)->i_state |=
3294 EXT4_STATE_DIO_UNWRITTEN;;
3297 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
3299 /* free data blocks we just allocated */
3300 /* not a good idea to call discard here directly,
3301 * but otherwise we'd need to call it every free() */
3302 ext4_discard_preallocations(inode);
3303 ext4_free_blocks(handle, inode, ext_pblock(&newex),
3304 ext4_ext_get_actual_len(&newex), 0);
3308 /* previous routine could use block we allocated */
3309 newblock = ext_pblock(&newex);
3310 allocated = ext4_ext_get_actual_len(&newex);
3311 set_buffer_new(bh_result);
3314 * Cache the extent and update transaction to commit on fdatasync only
3315 * when it is _not_ an uninitialized extent.
3317 if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) {
3318 ext4_ext_put_in_cache(inode, iblock, allocated, newblock,
3319 EXT4_EXT_CACHE_EXTENT);
3320 ext4_update_inode_fsync_trans(handle, inode, 1);
3322 ext4_update_inode_fsync_trans(handle, inode, 0);
3324 if (allocated > max_blocks)
3325 allocated = max_blocks;
3326 ext4_ext_show_leaf(inode, path);
3327 set_buffer_mapped(bh_result);
3328 bh_result->b_bdev = inode->i_sb->s_bdev;
3329 bh_result->b_blocknr = newblock;
3332 ext4_ext_drop_refs(path);
3335 return err ? err : allocated;
3338 void ext4_ext_truncate(struct inode *inode)
3340 struct address_space *mapping = inode->i_mapping;
3341 struct super_block *sb = inode->i_sb;
3342 ext4_lblk_t last_block;
3347 * probably first extent we're gonna free will be last in block
3349 err = ext4_writepage_trans_blocks(inode);
3350 handle = ext4_journal_start(inode, err);
3354 if (inode->i_size & (sb->s_blocksize - 1))
3355 ext4_block_truncate_page(handle, mapping, inode->i_size);
3357 if (ext4_orphan_add(handle, inode))
3360 down_write(&EXT4_I(inode)->i_data_sem);
3361 ext4_ext_invalidate_cache(inode);
3363 ext4_discard_preallocations(inode);
3366 * TODO: optimization is possible here.
3367 * Probably we need not scan at all,
3368 * because page truncation is enough.
3371 /* we have to know where to truncate from in crash case */
3372 EXT4_I(inode)->i_disksize = inode->i_size;
3373 ext4_mark_inode_dirty(handle, inode);
3375 last_block = (inode->i_size + sb->s_blocksize - 1)
3376 >> EXT4_BLOCK_SIZE_BITS(sb);
3377 err = ext4_ext_remove_space(inode, last_block);
3379 /* In a multi-transaction truncate, we only make the final
3380 * transaction synchronous.
3383 ext4_handle_sync(handle);
3386 up_write(&EXT4_I(inode)->i_data_sem);
3388 * If this was a simple ftruncate() and the file will remain alive,
3389 * then we need to clear up the orphan record which we created above.
3390 * However, if this was a real unlink then we were called by
3391 * ext4_delete_inode(), and we allow that function to clean up the
3392 * orphan info for us.
3395 ext4_orphan_del(handle, inode);
3397 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
3398 ext4_mark_inode_dirty(handle, inode);
3399 ext4_journal_stop(handle);
3402 static void ext4_falloc_update_inode(struct inode *inode,
3403 int mode, loff_t new_size, int update_ctime)
3405 struct timespec now;
3408 now = current_fs_time(inode->i_sb);
3409 if (!timespec_equal(&inode->i_ctime, &now))
3410 inode->i_ctime = now;
3413 * Update only when preallocation was requested beyond
3416 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3417 if (new_size > i_size_read(inode))
3418 i_size_write(inode, new_size);
3419 if (new_size > EXT4_I(inode)->i_disksize)
3420 ext4_update_i_disksize(inode, new_size);
3426 * preallocate space for a file. This implements ext4's fallocate inode
3427 * operation, which gets called from sys_fallocate system call.
3428 * For block-mapped files, posix_fallocate should fall back to the method
3429 * of writing zeroes to the required new blocks (the same behavior which is
3430 * expected for file systems which do not support fallocate() system call).
3432 long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
3437 unsigned int max_blocks;
3441 struct buffer_head map_bh;
3442 unsigned int credits, blkbits = inode->i_blkbits;
3445 * currently supporting (pre)allocate mode for extent-based
3448 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
3451 /* preallocation to directories is currently not supported */
3452 if (S_ISDIR(inode->i_mode))
3455 block = offset >> blkbits;
3457 * We can't just convert len to max_blocks because
3458 * If blocksize = 4096 offset = 3072 and len = 2048
3460 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
3463 * credits to insert 1 extent into extent tree
3465 credits = ext4_chunk_trans_blocks(inode, max_blocks);
3466 mutex_lock(&inode->i_mutex);
3468 while (ret >= 0 && ret < max_blocks) {
3469 block = block + ret;
3470 max_blocks = max_blocks - ret;
3471 handle = ext4_journal_start(inode, credits);
3472 if (IS_ERR(handle)) {
3473 ret = PTR_ERR(handle);
3477 ret = ext4_get_blocks(handle, inode, block,
3478 max_blocks, &map_bh,
3479 EXT4_GET_BLOCKS_CREATE_UNINIT_EXT);
3483 printk(KERN_ERR "%s: ext4_ext_get_blocks "
3484 "returned error inode#%lu, block=%u, "
3485 "max_blocks=%u", __func__,
3486 inode->i_ino, block, max_blocks);
3488 ext4_mark_inode_dirty(handle, inode);
3489 ret2 = ext4_journal_stop(handle);
3492 if ((block + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
3493 blkbits) >> blkbits))
3494 new_size = offset + len;
3496 new_size = (block + ret) << blkbits;
3498 ext4_falloc_update_inode(inode, mode, new_size,
3499 buffer_new(&map_bh));
3500 ext4_mark_inode_dirty(handle, inode);
3501 ret2 = ext4_journal_stop(handle);
3505 if (ret == -ENOSPC &&
3506 ext4_should_retry_alloc(inode->i_sb, &retries)) {
3510 mutex_unlock(&inode->i_mutex);
3511 return ret > 0 ? ret2 : ret;
3515 * This function convert a range of blocks to written extents
3516 * The caller of this function will pass the start offset and the size.
3517 * all unwritten extents within this range will be converted to
3520 * This function is called from the direct IO end io call back
3521 * function, to convert the fallocated extents after IO is completed.
3522 * Returns 0 on success.
3524 int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset,
3529 unsigned int max_blocks;
3532 struct buffer_head map_bh;
3533 unsigned int credits, blkbits = inode->i_blkbits;
3535 block = offset >> blkbits;
3537 * We can't just convert len to max_blocks because
3538 * If blocksize = 4096 offset = 3072 and len = 2048
3540 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
3543 * credits to insert 1 extent into extent tree
3545 credits = ext4_chunk_trans_blocks(inode, max_blocks);
3546 while (ret >= 0 && ret < max_blocks) {
3547 block = block + ret;
3548 max_blocks = max_blocks - ret;
3549 handle = ext4_journal_start(inode, credits);
3550 if (IS_ERR(handle)) {
3551 ret = PTR_ERR(handle);
3555 ret = ext4_get_blocks(handle, inode, block,
3556 max_blocks, &map_bh,
3557 EXT4_GET_BLOCKS_DIO_CONVERT_EXT);
3560 printk(KERN_ERR "%s: ext4_ext_get_blocks "
3561 "returned error inode#%lu, block=%u, "
3562 "max_blocks=%u", __func__,
3563 inode->i_ino, block, max_blocks);
3565 ext4_mark_inode_dirty(handle, inode);
3566 ret2 = ext4_journal_stop(handle);
3567 if (ret <= 0 || ret2 )
3570 return ret > 0 ? ret2 : ret;
3573 * Callback function called for each extent to gather FIEMAP information.
3575 static int ext4_ext_fiemap_cb(struct inode *inode, struct ext4_ext_path *path,
3576 struct ext4_ext_cache *newex, struct ext4_extent *ex,
3579 struct fiemap_extent_info *fieinfo = data;
3580 unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
3587 logical = (__u64)newex->ec_block << blksize_bits;
3589 if (newex->ec_type == EXT4_EXT_CACHE_GAP) {
3592 struct buffer_head *bh = NULL;
3594 offset = logical >> PAGE_SHIFT;
3595 page = find_get_page(inode->i_mapping, offset);
3596 if (!page || !page_has_buffers(page))
3597 return EXT_CONTINUE;
3599 bh = page_buffers(page);
3602 return EXT_CONTINUE;
3604 if (buffer_delay(bh)) {
3605 flags |= FIEMAP_EXTENT_DELALLOC;
3606 page_cache_release(page);
3608 page_cache_release(page);
3609 return EXT_CONTINUE;
3613 physical = (__u64)newex->ec_start << blksize_bits;
3614 length = (__u64)newex->ec_len << blksize_bits;
3616 if (ex && ext4_ext_is_uninitialized(ex))
3617 flags |= FIEMAP_EXTENT_UNWRITTEN;
3620 * If this extent reaches EXT_MAX_BLOCK, it must be last.
3622 * Or if ext4_ext_next_allocated_block is EXT_MAX_BLOCK,
3623 * this also indicates no more allocated blocks.
3625 * XXX this might miss a single-block extent at EXT_MAX_BLOCK
3627 if (ext4_ext_next_allocated_block(path) == EXT_MAX_BLOCK ||
3628 newex->ec_block + newex->ec_len - 1 == EXT_MAX_BLOCK) {
3629 loff_t size = i_size_read(inode);
3630 loff_t bs = EXT4_BLOCK_SIZE(inode->i_sb);
3632 flags |= FIEMAP_EXTENT_LAST;
3633 if ((flags & FIEMAP_EXTENT_DELALLOC) &&
3634 logical+length > size)
3635 length = (size - logical + bs - 1) & ~(bs-1);
3638 error = fiemap_fill_next_extent(fieinfo, logical, physical,
3645 return EXT_CONTINUE;
3648 /* fiemap flags we can handle specified here */
3649 #define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
3651 static int ext4_xattr_fiemap(struct inode *inode,
3652 struct fiemap_extent_info *fieinfo)
3656 __u32 flags = FIEMAP_EXTENT_LAST;
3657 int blockbits = inode->i_sb->s_blocksize_bits;
3661 if (EXT4_I(inode)->i_state & EXT4_STATE_XATTR) {
3662 struct ext4_iloc iloc;
3663 int offset; /* offset of xattr in inode */
3665 error = ext4_get_inode_loc(inode, &iloc);
3668 physical = iloc.bh->b_blocknr << blockbits;
3669 offset = EXT4_GOOD_OLD_INODE_SIZE +
3670 EXT4_I(inode)->i_extra_isize;
3672 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
3673 flags |= FIEMAP_EXTENT_DATA_INLINE;
3674 } else { /* external block */
3675 physical = EXT4_I(inode)->i_file_acl << blockbits;
3676 length = inode->i_sb->s_blocksize;
3680 error = fiemap_fill_next_extent(fieinfo, 0, physical,
3682 return (error < 0 ? error : 0);
3685 int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3686 __u64 start, __u64 len)
3688 ext4_lblk_t start_blk;
3689 ext4_lblk_t len_blks;
3692 /* fallback to generic here if not in extents fmt */
3693 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
3694 return generic_block_fiemap(inode, fieinfo, start, len,
3697 if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
3700 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
3701 error = ext4_xattr_fiemap(inode, fieinfo);
3703 start_blk = start >> inode->i_sb->s_blocksize_bits;
3704 len_blks = len >> inode->i_sb->s_blocksize_bits;
3707 * Walk the extent tree gathering extent information.
3708 * ext4_ext_fiemap_cb will push extents back to user.
3710 down_read(&EXT4_I(inode)->i_data_sem);
3711 error = ext4_ext_walk_space(inode, start_blk, len_blks,
3712 ext4_ext_fiemap_cb, fieinfo);
3713 up_read(&EXT4_I(inode)->i_data_sem);