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
33 #include <linux/time.h>
34 #include <linux/jbd2.h>
35 #include <linux/highuid.h>
36 #include <linux/pagemap.h>
37 #include <linux/quotaops.h>
38 #include <linux/string.h>
39 #include <linux/slab.h>
40 #include <asm/uaccess.h>
41 #include <linux/fiemap.h>
42 #include "ext4_jbd2.h"
43 #include "ext4_extents.h"
46 #include <trace/events/ext4.h>
49 * used by extent splitting.
51 #define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \
53 #define EXT4_EXT_MARK_UNWRIT1 0x2 /* mark first half unwritten */
54 #define EXT4_EXT_MARK_UNWRIT2 0x4 /* mark second half unwritten */
56 #define EXT4_EXT_DATA_VALID1 0x8 /* first half contains valid data */
57 #define EXT4_EXT_DATA_VALID2 0x10 /* second half contains valid data */
59 static __le32 ext4_extent_block_csum(struct inode *inode,
60 struct ext4_extent_header *eh)
62 struct ext4_inode_info *ei = EXT4_I(inode);
63 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
66 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)eh,
67 EXT4_EXTENT_TAIL_OFFSET(eh));
68 return cpu_to_le32(csum);
71 static int ext4_extent_block_csum_verify(struct inode *inode,
72 struct ext4_extent_header *eh)
74 struct ext4_extent_tail *et;
76 if (!ext4_has_metadata_csum(inode->i_sb))
79 et = find_ext4_extent_tail(eh);
80 if (et->et_checksum != ext4_extent_block_csum(inode, eh))
85 static void ext4_extent_block_csum_set(struct inode *inode,
86 struct ext4_extent_header *eh)
88 struct ext4_extent_tail *et;
90 if (!ext4_has_metadata_csum(inode->i_sb))
93 et = find_ext4_extent_tail(eh);
94 et->et_checksum = ext4_extent_block_csum(inode, eh);
97 static int ext4_split_extent(handle_t *handle,
99 struct ext4_ext_path *path,
100 struct ext4_map_blocks *map,
104 static int ext4_split_extent_at(handle_t *handle,
106 struct ext4_ext_path *path,
111 static int ext4_find_delayed_extent(struct inode *inode,
112 struct extent_status *newes);
114 static int ext4_ext_truncate_extend_restart(handle_t *handle,
120 if (!ext4_handle_valid(handle))
122 if (handle->h_buffer_credits > needed)
124 err = ext4_journal_extend(handle, needed);
127 err = ext4_truncate_restart_trans(handle, inode, needed);
139 static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
140 struct ext4_ext_path *path)
143 /* path points to block */
144 BUFFER_TRACE(path->p_bh, "get_write_access");
145 return ext4_journal_get_write_access(handle, path->p_bh);
147 /* path points to leaf/index in inode body */
148 /* we use in-core data, no need to protect them */
158 int __ext4_ext_dirty(const char *where, unsigned int line, handle_t *handle,
159 struct inode *inode, struct ext4_ext_path *path)
163 ext4_extent_block_csum_set(inode, ext_block_hdr(path->p_bh));
164 /* path points to block */
165 err = __ext4_handle_dirty_metadata(where, line, handle,
168 /* path points to leaf/index in inode body */
169 err = ext4_mark_inode_dirty(handle, inode);
174 static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
175 struct ext4_ext_path *path,
179 int depth = path->p_depth;
180 struct ext4_extent *ex;
183 * Try to predict block placement assuming that we are
184 * filling in a file which will eventually be
185 * non-sparse --- i.e., in the case of libbfd writing
186 * an ELF object sections out-of-order but in a way
187 * the eventually results in a contiguous object or
188 * executable file, or some database extending a table
189 * space file. However, this is actually somewhat
190 * non-ideal if we are writing a sparse file such as
191 * qemu or KVM writing a raw image file that is going
192 * to stay fairly sparse, since it will end up
193 * fragmenting the file system's free space. Maybe we
194 * should have some hueristics or some way to allow
195 * userspace to pass a hint to file system,
196 * especially if the latter case turns out to be
199 ex = path[depth].p_ext;
201 ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex);
202 ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block);
204 if (block > ext_block)
205 return ext_pblk + (block - ext_block);
207 return ext_pblk - (ext_block - block);
210 /* it looks like index is empty;
211 * try to find starting block from index itself */
212 if (path[depth].p_bh)
213 return path[depth].p_bh->b_blocknr;
216 /* OK. use inode's group */
217 return ext4_inode_to_goal_block(inode);
221 * Allocation for a meta data block
224 ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
225 struct ext4_ext_path *path,
226 struct ext4_extent *ex, int *err, unsigned int flags)
228 ext4_fsblk_t goal, newblock;
230 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
231 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
236 static inline int ext4_ext_space_block(struct inode *inode, int check)
240 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
241 / sizeof(struct ext4_extent);
242 #ifdef AGGRESSIVE_TEST
243 if (!check && size > 6)
249 static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
253 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
254 / sizeof(struct ext4_extent_idx);
255 #ifdef AGGRESSIVE_TEST
256 if (!check && size > 5)
262 static inline int ext4_ext_space_root(struct inode *inode, int check)
266 size = sizeof(EXT4_I(inode)->i_data);
267 size -= sizeof(struct ext4_extent_header);
268 size /= sizeof(struct ext4_extent);
269 #ifdef AGGRESSIVE_TEST
270 if (!check && size > 3)
276 static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
280 size = sizeof(EXT4_I(inode)->i_data);
281 size -= sizeof(struct ext4_extent_header);
282 size /= sizeof(struct ext4_extent_idx);
283 #ifdef AGGRESSIVE_TEST
284 if (!check && size > 4)
291 * Calculate the number of metadata blocks needed
292 * to allocate @blocks
293 * Worse case is one block per extent
295 int ext4_ext_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
297 struct ext4_inode_info *ei = EXT4_I(inode);
300 idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
301 / sizeof(struct ext4_extent_idx));
304 * If the new delayed allocation block is contiguous with the
305 * previous da block, it can share index blocks with the
306 * previous block, so we only need to allocate a new index
307 * block every idxs leaf blocks. At ldxs**2 blocks, we need
308 * an additional index block, and at ldxs**3 blocks, yet
309 * another index blocks.
311 if (ei->i_da_metadata_calc_len &&
312 ei->i_da_metadata_calc_last_lblock+1 == lblock) {
315 if ((ei->i_da_metadata_calc_len % idxs) == 0)
317 if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0)
319 if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) {
321 ei->i_da_metadata_calc_len = 0;
323 ei->i_da_metadata_calc_len++;
324 ei->i_da_metadata_calc_last_lblock++;
329 * In the worst case we need a new set of index blocks at
330 * every level of the inode's extent tree.
332 ei->i_da_metadata_calc_len = 1;
333 ei->i_da_metadata_calc_last_lblock = lblock;
334 return ext_depth(inode) + 1;
338 ext4_ext_max_entries(struct inode *inode, int depth)
342 if (depth == ext_depth(inode)) {
344 max = ext4_ext_space_root(inode, 1);
346 max = ext4_ext_space_root_idx(inode, 1);
349 max = ext4_ext_space_block(inode, 1);
351 max = ext4_ext_space_block_idx(inode, 1);
357 static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
359 ext4_fsblk_t block = ext4_ext_pblock(ext);
360 int len = ext4_ext_get_actual_len(ext);
361 ext4_lblk_t lblock = le32_to_cpu(ext->ee_block);
366 * - overflow/wrap-around
368 if (lblock + len <= lblock)
370 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
373 static int ext4_valid_extent_idx(struct inode *inode,
374 struct ext4_extent_idx *ext_idx)
376 ext4_fsblk_t block = ext4_idx_pblock(ext_idx);
378 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
381 static int ext4_valid_extent_entries(struct inode *inode,
382 struct ext4_extent_header *eh,
385 unsigned short entries;
386 if (eh->eh_entries == 0)
389 entries = le16_to_cpu(eh->eh_entries);
393 struct ext4_extent *ext = EXT_FIRST_EXTENT(eh);
394 struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
395 ext4_fsblk_t pblock = 0;
396 ext4_lblk_t lblock = 0;
397 ext4_lblk_t prev = 0;
400 if (!ext4_valid_extent(inode, ext))
403 /* Check for overlapping extents */
404 lblock = le32_to_cpu(ext->ee_block);
405 len = ext4_ext_get_actual_len(ext);
406 if ((lblock <= prev) && prev) {
407 pblock = ext4_ext_pblock(ext);
408 es->s_last_error_block = cpu_to_le64(pblock);
413 prev = lblock + len - 1;
416 struct ext4_extent_idx *ext_idx = EXT_FIRST_INDEX(eh);
418 if (!ext4_valid_extent_idx(inode, ext_idx))
427 static int __ext4_ext_check(const char *function, unsigned int line,
428 struct inode *inode, struct ext4_extent_header *eh,
429 int depth, ext4_fsblk_t pblk)
431 const char *error_msg;
434 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
435 error_msg = "invalid magic";
438 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
439 error_msg = "unexpected eh_depth";
442 if (unlikely(eh->eh_max == 0)) {
443 error_msg = "invalid eh_max";
446 max = ext4_ext_max_entries(inode, depth);
447 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
448 error_msg = "too large eh_max";
451 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
452 error_msg = "invalid eh_entries";
455 if (!ext4_valid_extent_entries(inode, eh, depth)) {
456 error_msg = "invalid extent entries";
459 /* Verify checksum on non-root extent tree nodes */
460 if (ext_depth(inode) != depth &&
461 !ext4_extent_block_csum_verify(inode, eh)) {
462 error_msg = "extent tree corrupted";
468 ext4_error_inode(inode, function, line, 0,
469 "pblk %llu bad header/extent: %s - magic %x, "
470 "entries %u, max %u(%u), depth %u(%u)",
471 (unsigned long long) pblk, error_msg,
472 le16_to_cpu(eh->eh_magic),
473 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
474 max, le16_to_cpu(eh->eh_depth), depth);
478 #define ext4_ext_check(inode, eh, depth, pblk) \
479 __ext4_ext_check(__func__, __LINE__, (inode), (eh), (depth), (pblk))
481 int ext4_ext_check_inode(struct inode *inode)
483 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode), 0);
486 static struct buffer_head *
487 __read_extent_tree_block(const char *function, unsigned int line,
488 struct inode *inode, ext4_fsblk_t pblk, int depth,
491 struct buffer_head *bh;
494 bh = sb_getblk_gfp(inode->i_sb, pblk, __GFP_MOVABLE | GFP_NOFS);
496 return ERR_PTR(-ENOMEM);
498 if (!bh_uptodate_or_lock(bh)) {
499 trace_ext4_ext_load_extent(inode, pblk, _RET_IP_);
500 err = bh_submit_read(bh);
504 if (buffer_verified(bh) && !(flags & EXT4_EX_FORCE_CACHE))
506 if (!EXT4_HAS_COMPAT_FEATURE(inode->i_sb,
507 EXT4_FEATURE_COMPAT_HAS_JOURNAL) ||
509 le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum))) {
510 err = __ext4_ext_check(function, line, inode,
511 ext_block_hdr(bh), depth, pblk);
515 set_buffer_verified(bh);
517 * If this is a leaf block, cache all of its entries
519 if (!(flags & EXT4_EX_NOCACHE) && depth == 0) {
520 struct ext4_extent_header *eh = ext_block_hdr(bh);
521 struct ext4_extent *ex = EXT_FIRST_EXTENT(eh);
522 ext4_lblk_t prev = 0;
525 for (i = le16_to_cpu(eh->eh_entries); i > 0; i--, ex++) {
526 unsigned int status = EXTENT_STATUS_WRITTEN;
527 ext4_lblk_t lblk = le32_to_cpu(ex->ee_block);
528 int len = ext4_ext_get_actual_len(ex);
530 if (prev && (prev != lblk))
531 ext4_es_cache_extent(inode, prev,
535 if (ext4_ext_is_unwritten(ex))
536 status = EXTENT_STATUS_UNWRITTEN;
537 ext4_es_cache_extent(inode, lblk, len,
538 ext4_ext_pblock(ex), status);
549 #define read_extent_tree_block(inode, pblk, depth, flags) \
550 __read_extent_tree_block(__func__, __LINE__, (inode), (pblk), \
554 * This function is called to cache a file's extent information in the
557 int ext4_ext_precache(struct inode *inode)
559 struct ext4_inode_info *ei = EXT4_I(inode);
560 struct ext4_ext_path *path = NULL;
561 struct buffer_head *bh;
562 int i = 0, depth, ret = 0;
564 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
565 return 0; /* not an extent-mapped inode */
567 down_read(&ei->i_data_sem);
568 depth = ext_depth(inode);
570 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1),
573 up_read(&ei->i_data_sem);
577 /* Don't cache anything if there are no external extent blocks */
580 path[0].p_hdr = ext_inode_hdr(inode);
581 ret = ext4_ext_check(inode, path[0].p_hdr, depth, 0);
584 path[0].p_idx = EXT_FIRST_INDEX(path[0].p_hdr);
587 * If this is a leaf block or we've reached the end of
588 * the index block, go up
591 path[i].p_idx > EXT_LAST_INDEX(path[i].p_hdr)) {
592 brelse(path[i].p_bh);
597 bh = read_extent_tree_block(inode,
598 ext4_idx_pblock(path[i].p_idx++),
600 EXT4_EX_FORCE_CACHE);
607 path[i].p_hdr = ext_block_hdr(bh);
608 path[i].p_idx = EXT_FIRST_INDEX(path[i].p_hdr);
610 ext4_set_inode_state(inode, EXT4_STATE_EXT_PRECACHED);
612 up_read(&ei->i_data_sem);
613 ext4_ext_drop_refs(path);
619 static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
621 int k, l = path->p_depth;
624 for (k = 0; k <= l; k++, path++) {
626 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
627 ext4_idx_pblock(path->p_idx));
628 } else if (path->p_ext) {
629 ext_debug(" %d:[%d]%d:%llu ",
630 le32_to_cpu(path->p_ext->ee_block),
631 ext4_ext_is_unwritten(path->p_ext),
632 ext4_ext_get_actual_len(path->p_ext),
633 ext4_ext_pblock(path->p_ext));
640 static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
642 int depth = ext_depth(inode);
643 struct ext4_extent_header *eh;
644 struct ext4_extent *ex;
650 eh = path[depth].p_hdr;
651 ex = EXT_FIRST_EXTENT(eh);
653 ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
655 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
656 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
657 ext4_ext_is_unwritten(ex),
658 ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
663 static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path,
664 ext4_fsblk_t newblock, int level)
666 int depth = ext_depth(inode);
667 struct ext4_extent *ex;
669 if (depth != level) {
670 struct ext4_extent_idx *idx;
671 idx = path[level].p_idx;
672 while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) {
673 ext_debug("%d: move %d:%llu in new index %llu\n", level,
674 le32_to_cpu(idx->ei_block),
675 ext4_idx_pblock(idx),
683 ex = path[depth].p_ext;
684 while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) {
685 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
686 le32_to_cpu(ex->ee_block),
688 ext4_ext_is_unwritten(ex),
689 ext4_ext_get_actual_len(ex),
696 #define ext4_ext_show_path(inode, path)
697 #define ext4_ext_show_leaf(inode, path)
698 #define ext4_ext_show_move(inode, path, newblock, level)
701 void ext4_ext_drop_refs(struct ext4_ext_path *path)
703 int depth = path->p_depth;
706 for (i = 0; i <= depth; i++, path++)
714 * ext4_ext_binsearch_idx:
715 * binary search for the closest index of the given block
716 * the header must be checked before calling this
719 ext4_ext_binsearch_idx(struct inode *inode,
720 struct ext4_ext_path *path, ext4_lblk_t block)
722 struct ext4_extent_header *eh = path->p_hdr;
723 struct ext4_extent_idx *r, *l, *m;
726 ext_debug("binsearch for %u(idx): ", block);
728 l = EXT_FIRST_INDEX(eh) + 1;
729 r = EXT_LAST_INDEX(eh);
732 if (block < le32_to_cpu(m->ei_block))
736 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
737 m, le32_to_cpu(m->ei_block),
738 r, le32_to_cpu(r->ei_block));
742 ext_debug(" -> %u->%lld ", le32_to_cpu(path->p_idx->ei_block),
743 ext4_idx_pblock(path->p_idx));
745 #ifdef CHECK_BINSEARCH
747 struct ext4_extent_idx *chix, *ix;
750 chix = ix = EXT_FIRST_INDEX(eh);
751 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
753 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
754 printk(KERN_DEBUG "k=%d, ix=0x%p, "
756 ix, EXT_FIRST_INDEX(eh));
757 printk(KERN_DEBUG "%u <= %u\n",
758 le32_to_cpu(ix->ei_block),
759 le32_to_cpu(ix[-1].ei_block));
761 BUG_ON(k && le32_to_cpu(ix->ei_block)
762 <= le32_to_cpu(ix[-1].ei_block));
763 if (block < le32_to_cpu(ix->ei_block))
767 BUG_ON(chix != path->p_idx);
774 * ext4_ext_binsearch:
775 * binary search for closest extent of the given block
776 * the header must be checked before calling this
779 ext4_ext_binsearch(struct inode *inode,
780 struct ext4_ext_path *path, ext4_lblk_t block)
782 struct ext4_extent_header *eh = path->p_hdr;
783 struct ext4_extent *r, *l, *m;
785 if (eh->eh_entries == 0) {
787 * this leaf is empty:
788 * we get such a leaf in split/add case
793 ext_debug("binsearch for %u: ", block);
795 l = EXT_FIRST_EXTENT(eh) + 1;
796 r = EXT_LAST_EXTENT(eh);
800 if (block < le32_to_cpu(m->ee_block))
804 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
805 m, le32_to_cpu(m->ee_block),
806 r, le32_to_cpu(r->ee_block));
810 ext_debug(" -> %d:%llu:[%d]%d ",
811 le32_to_cpu(path->p_ext->ee_block),
812 ext4_ext_pblock(path->p_ext),
813 ext4_ext_is_unwritten(path->p_ext),
814 ext4_ext_get_actual_len(path->p_ext));
816 #ifdef CHECK_BINSEARCH
818 struct ext4_extent *chex, *ex;
821 chex = ex = EXT_FIRST_EXTENT(eh);
822 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
823 BUG_ON(k && le32_to_cpu(ex->ee_block)
824 <= le32_to_cpu(ex[-1].ee_block));
825 if (block < le32_to_cpu(ex->ee_block))
829 BUG_ON(chex != path->p_ext);
835 int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
837 struct ext4_extent_header *eh;
839 eh = ext_inode_hdr(inode);
842 eh->eh_magic = EXT4_EXT_MAGIC;
843 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
844 ext4_mark_inode_dirty(handle, inode);
848 struct ext4_ext_path *
849 ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
850 struct ext4_ext_path *path, int flags)
852 struct ext4_extent_header *eh;
853 struct buffer_head *bh;
854 short int depth, i, ppos = 0, alloc = 0;
857 eh = ext_inode_hdr(inode);
858 depth = ext_depth(inode);
859 if (depth < 0 || depth > EXT4_MAX_EXTENT_DEPTH) {
860 EXT4_ERROR_INODE(inode, "inode has invalid extent depth: %d",
866 /* account possible depth increase */
868 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
871 return ERR_PTR(-ENOMEM);
878 /* walk through the tree */
880 ext_debug("depth %d: num %d, max %d\n",
881 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
883 ext4_ext_binsearch_idx(inode, path + ppos, block);
884 path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
885 path[ppos].p_depth = i;
886 path[ppos].p_ext = NULL;
888 bh = read_extent_tree_block(inode, path[ppos].p_block, --i,
895 eh = ext_block_hdr(bh);
897 if (unlikely(ppos > depth)) {
899 EXT4_ERROR_INODE(inode,
900 "ppos %d > depth %d", ppos, depth);
904 path[ppos].p_bh = bh;
905 path[ppos].p_hdr = eh;
908 path[ppos].p_depth = i;
909 path[ppos].p_ext = NULL;
910 path[ppos].p_idx = NULL;
913 ext4_ext_binsearch(inode, path + ppos, block);
914 /* if not an empty leaf */
915 if (path[ppos].p_ext)
916 path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
918 ext4_ext_show_path(inode, path);
923 ext4_ext_drop_refs(path);
930 * ext4_ext_insert_index:
931 * insert new index [@logical;@ptr] into the block at @curp;
932 * check where to insert: before @curp or after @curp
934 static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
935 struct ext4_ext_path *curp,
936 int logical, ext4_fsblk_t ptr)
938 struct ext4_extent_idx *ix;
941 err = ext4_ext_get_access(handle, inode, curp);
945 if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
946 EXT4_ERROR_INODE(inode,
947 "logical %d == ei_block %d!",
948 logical, le32_to_cpu(curp->p_idx->ei_block));
952 if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
953 >= le16_to_cpu(curp->p_hdr->eh_max))) {
954 EXT4_ERROR_INODE(inode,
955 "eh_entries %d >= eh_max %d!",
956 le16_to_cpu(curp->p_hdr->eh_entries),
957 le16_to_cpu(curp->p_hdr->eh_max));
961 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
963 ext_debug("insert new index %d after: %llu\n", logical, ptr);
964 ix = curp->p_idx + 1;
967 ext_debug("insert new index %d before: %llu\n", logical, ptr);
971 len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1;
974 ext_debug("insert new index %d: "
975 "move %d indices from 0x%p to 0x%p\n",
976 logical, len, ix, ix + 1);
977 memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx));
980 if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) {
981 EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!");
985 ix->ei_block = cpu_to_le32(logical);
986 ext4_idx_store_pblock(ix, ptr);
987 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
989 if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
990 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
994 err = ext4_ext_dirty(handle, inode, curp);
995 ext4_std_error(inode->i_sb, err);
1002 * inserts new subtree into the path, using free index entry
1004 * - allocates all needed blocks (new leaf and all intermediate index blocks)
1005 * - makes decision where to split
1006 * - moves remaining extents and index entries (right to the split point)
1007 * into the newly allocated blocks
1008 * - initializes subtree
1010 static int ext4_ext_split(handle_t *handle, struct inode *inode,
1012 struct ext4_ext_path *path,
1013 struct ext4_extent *newext, int at)
1015 struct buffer_head *bh = NULL;
1016 int depth = ext_depth(inode);
1017 struct ext4_extent_header *neh;
1018 struct ext4_extent_idx *fidx;
1019 int i = at, k, m, a;
1020 ext4_fsblk_t newblock, oldblock;
1022 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
1024 size_t ext_size = 0;
1026 /* make decision: where to split? */
1027 /* FIXME: now decision is simplest: at current extent */
1029 /* if current leaf will be split, then we should use
1030 * border from split point */
1031 if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
1032 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
1035 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
1036 border = path[depth].p_ext[1].ee_block;
1037 ext_debug("leaf will be split."
1038 " next leaf starts at %d\n",
1039 le32_to_cpu(border));
1041 border = newext->ee_block;
1042 ext_debug("leaf will be added."
1043 " next leaf starts at %d\n",
1044 le32_to_cpu(border));
1048 * If error occurs, then we break processing
1049 * and mark filesystem read-only. index won't
1050 * be inserted and tree will be in consistent
1051 * state. Next mount will repair buffers too.
1055 * Get array to track all allocated blocks.
1056 * We need this to handle errors and free blocks
1059 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
1063 /* allocate all needed blocks */
1064 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
1065 for (a = 0; a < depth - at; a++) {
1066 newblock = ext4_ext_new_meta_block(handle, inode, path,
1067 newext, &err, flags);
1070 ablocks[a] = newblock;
1073 /* initialize new leaf */
1074 newblock = ablocks[--a];
1075 if (unlikely(newblock == 0)) {
1076 EXT4_ERROR_INODE(inode, "newblock == 0!");
1080 bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS);
1081 if (unlikely(!bh)) {
1087 err = ext4_journal_get_create_access(handle, bh);
1091 neh = ext_block_hdr(bh);
1092 neh->eh_entries = 0;
1093 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
1094 neh->eh_magic = EXT4_EXT_MAGIC;
1097 /* move remainder of path[depth] to the new leaf */
1098 if (unlikely(path[depth].p_hdr->eh_entries !=
1099 path[depth].p_hdr->eh_max)) {
1100 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
1101 path[depth].p_hdr->eh_entries,
1102 path[depth].p_hdr->eh_max);
1106 /* start copy from next extent */
1107 m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++;
1108 ext4_ext_show_move(inode, path, newblock, depth);
1110 struct ext4_extent *ex;
1111 ex = EXT_FIRST_EXTENT(neh);
1112 memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m);
1113 le16_add_cpu(&neh->eh_entries, m);
1116 /* zero out unused area in the extent block */
1117 ext_size = sizeof(struct ext4_extent_header) +
1118 sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries);
1119 memset(bh->b_data + ext_size, 0, inode->i_sb->s_blocksize - ext_size);
1120 ext4_extent_block_csum_set(inode, neh);
1121 set_buffer_uptodate(bh);
1124 err = ext4_handle_dirty_metadata(handle, inode, bh);
1130 /* correct old leaf */
1132 err = ext4_ext_get_access(handle, inode, path + depth);
1135 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
1136 err = ext4_ext_dirty(handle, inode, path + depth);
1142 /* create intermediate indexes */
1144 if (unlikely(k < 0)) {
1145 EXT4_ERROR_INODE(inode, "k %d < 0!", k);
1150 ext_debug("create %d intermediate indices\n", k);
1151 /* insert new index into current index block */
1152 /* current depth stored in i var */
1155 oldblock = newblock;
1156 newblock = ablocks[--a];
1157 bh = sb_getblk(inode->i_sb, newblock);
1158 if (unlikely(!bh)) {
1164 err = ext4_journal_get_create_access(handle, bh);
1168 neh = ext_block_hdr(bh);
1169 neh->eh_entries = cpu_to_le16(1);
1170 neh->eh_magic = EXT4_EXT_MAGIC;
1171 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
1172 neh->eh_depth = cpu_to_le16(depth - i);
1173 fidx = EXT_FIRST_INDEX(neh);
1174 fidx->ei_block = border;
1175 ext4_idx_store_pblock(fidx, oldblock);
1177 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
1178 i, newblock, le32_to_cpu(border), oldblock);
1180 /* move remainder of path[i] to the new index block */
1181 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
1182 EXT_LAST_INDEX(path[i].p_hdr))) {
1183 EXT4_ERROR_INODE(inode,
1184 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
1185 le32_to_cpu(path[i].p_ext->ee_block));
1189 /* start copy indexes */
1190 m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++;
1191 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
1192 EXT_MAX_INDEX(path[i].p_hdr));
1193 ext4_ext_show_move(inode, path, newblock, i);
1195 memmove(++fidx, path[i].p_idx,
1196 sizeof(struct ext4_extent_idx) * m);
1197 le16_add_cpu(&neh->eh_entries, m);
1199 /* zero out unused area in the extent block */
1200 ext_size = sizeof(struct ext4_extent_header) +
1201 (sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries));
1202 memset(bh->b_data + ext_size, 0,
1203 inode->i_sb->s_blocksize - ext_size);
1204 ext4_extent_block_csum_set(inode, neh);
1205 set_buffer_uptodate(bh);
1208 err = ext4_handle_dirty_metadata(handle, inode, bh);
1214 /* correct old index */
1216 err = ext4_ext_get_access(handle, inode, path + i);
1219 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
1220 err = ext4_ext_dirty(handle, inode, path + i);
1228 /* insert new index */
1229 err = ext4_ext_insert_index(handle, inode, path + at,
1230 le32_to_cpu(border), newblock);
1234 if (buffer_locked(bh))
1240 /* free all allocated blocks in error case */
1241 for (i = 0; i < depth; i++) {
1244 ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
1245 EXT4_FREE_BLOCKS_METADATA);
1254 * ext4_ext_grow_indepth:
1255 * implements tree growing procedure:
1256 * - allocates new block
1257 * - moves top-level data (index block or leaf) into the new block
1258 * - initializes new top-level, creating index that points to the
1259 * just created block
1261 static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
1263 struct ext4_extent *newext)
1265 struct ext4_extent_header *neh;
1266 struct buffer_head *bh;
1267 ext4_fsblk_t newblock;
1269 size_t ext_size = 0;
1271 newblock = ext4_ext_new_meta_block(handle, inode, NULL,
1272 newext, &err, flags);
1276 bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS);
1281 err = ext4_journal_get_create_access(handle, bh);
1287 ext_size = sizeof(EXT4_I(inode)->i_data);
1288 /* move top-level index/leaf into new block */
1289 memmove(bh->b_data, EXT4_I(inode)->i_data, ext_size);
1290 /* zero out unused area in the extent block */
1291 memset(bh->b_data + ext_size, 0, inode->i_sb->s_blocksize - ext_size);
1293 /* set size of new block */
1294 neh = ext_block_hdr(bh);
1295 /* old root could have indexes or leaves
1296 * so calculate e_max right way */
1297 if (ext_depth(inode))
1298 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
1300 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
1301 neh->eh_magic = EXT4_EXT_MAGIC;
1302 ext4_extent_block_csum_set(inode, neh);
1303 set_buffer_uptodate(bh);
1306 err = ext4_handle_dirty_metadata(handle, inode, bh);
1310 /* Update top-level index: num,max,pointer */
1311 neh = ext_inode_hdr(inode);
1312 neh->eh_entries = cpu_to_le16(1);
1313 ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock);
1314 if (neh->eh_depth == 0) {
1315 /* Root extent block becomes index block */
1316 neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
1317 EXT_FIRST_INDEX(neh)->ei_block =
1318 EXT_FIRST_EXTENT(neh)->ee_block;
1320 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
1321 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
1322 le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
1323 ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
1325 le16_add_cpu(&neh->eh_depth, 1);
1326 ext4_mark_inode_dirty(handle, inode);
1334 * ext4_ext_create_new_leaf:
1335 * finds empty index and adds new leaf.
1336 * if no free index is found, then it requests in-depth growing.
1338 static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
1339 unsigned int mb_flags,
1340 unsigned int gb_flags,
1341 struct ext4_ext_path *path,
1342 struct ext4_extent *newext)
1344 struct ext4_ext_path *curp;
1345 int depth, i, err = 0;
1348 i = depth = ext_depth(inode);
1350 /* walk up to the tree and look for free index entry */
1351 curp = path + depth;
1352 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1357 /* we use already allocated block for index block,
1358 * so subsequent data blocks should be contiguous */
1359 if (EXT_HAS_FREE_INDEX(curp)) {
1360 /* if we found index with free entry, then use that
1361 * entry: create all needed subtree and add new leaf */
1362 err = ext4_ext_split(handle, inode, mb_flags, path, newext, i);
1367 ext4_ext_drop_refs(path);
1368 path = ext4_ext_find_extent(inode,
1369 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1372 err = PTR_ERR(path);
1374 /* tree is full, time to grow in depth */
1375 err = ext4_ext_grow_indepth(handle, inode, mb_flags, newext);
1380 ext4_ext_drop_refs(path);
1381 path = ext4_ext_find_extent(inode,
1382 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1385 err = PTR_ERR(path);
1390 * only first (depth 0 -> 1) produces free space;
1391 * in all other cases we have to split the grown tree
1393 depth = ext_depth(inode);
1394 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
1395 /* now we need to split */
1405 * search the closest allocated block to the left for *logical
1406 * and returns it at @logical + it's physical address at @phys
1407 * if *logical is the smallest allocated block, the function
1408 * returns 0 at @phys
1409 * return value contains 0 (success) or error code
1411 static int ext4_ext_search_left(struct inode *inode,
1412 struct ext4_ext_path *path,
1413 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1415 struct ext4_extent_idx *ix;
1416 struct ext4_extent *ex;
1419 if (unlikely(path == NULL)) {
1420 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1423 depth = path->p_depth;
1426 if (depth == 0 && path->p_ext == NULL)
1429 /* usually extent in the path covers blocks smaller
1430 * then *logical, but it can be that extent is the
1431 * first one in the file */
1433 ex = path[depth].p_ext;
1434 ee_len = ext4_ext_get_actual_len(ex);
1435 if (*logical < le32_to_cpu(ex->ee_block)) {
1436 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1437 EXT4_ERROR_INODE(inode,
1438 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1439 *logical, le32_to_cpu(ex->ee_block));
1442 while (--depth >= 0) {
1443 ix = path[depth].p_idx;
1444 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1445 EXT4_ERROR_INODE(inode,
1446 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
1447 ix != NULL ? le32_to_cpu(ix->ei_block) : 0,
1448 EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
1449 le32_to_cpu(EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block) : 0,
1457 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1458 EXT4_ERROR_INODE(inode,
1459 "logical %d < ee_block %d + ee_len %d!",
1460 *logical, le32_to_cpu(ex->ee_block), ee_len);
1464 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
1465 *phys = ext4_ext_pblock(ex) + ee_len - 1;
1470 * search the closest allocated block to the right for *logical
1471 * and returns it at @logical + it's physical address at @phys
1472 * if *logical is the largest allocated block, the function
1473 * returns 0 at @phys
1474 * return value contains 0 (success) or error code
1476 static int ext4_ext_search_right(struct inode *inode,
1477 struct ext4_ext_path *path,
1478 ext4_lblk_t *logical, ext4_fsblk_t *phys,
1479 struct ext4_extent **ret_ex)
1481 struct buffer_head *bh = NULL;
1482 struct ext4_extent_header *eh;
1483 struct ext4_extent_idx *ix;
1484 struct ext4_extent *ex;
1486 int depth; /* Note, NOT eh_depth; depth from top of tree */
1489 if (unlikely(path == NULL)) {
1490 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1493 depth = path->p_depth;
1496 if (depth == 0 && path->p_ext == NULL)
1499 /* usually extent in the path covers blocks smaller
1500 * then *logical, but it can be that extent is the
1501 * first one in the file */
1503 ex = path[depth].p_ext;
1504 ee_len = ext4_ext_get_actual_len(ex);
1505 if (*logical < le32_to_cpu(ex->ee_block)) {
1506 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1507 EXT4_ERROR_INODE(inode,
1508 "first_extent(path[%d].p_hdr) != ex",
1512 while (--depth >= 0) {
1513 ix = path[depth].p_idx;
1514 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1515 EXT4_ERROR_INODE(inode,
1516 "ix != EXT_FIRST_INDEX *logical %d!",
1524 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1525 EXT4_ERROR_INODE(inode,
1526 "logical %d < ee_block %d + ee_len %d!",
1527 *logical, le32_to_cpu(ex->ee_block), ee_len);
1531 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1532 /* next allocated block in this leaf */
1537 /* go up and search for index to the right */
1538 while (--depth >= 0) {
1539 ix = path[depth].p_idx;
1540 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
1544 /* we've gone up to the root and found no index to the right */
1548 /* we've found index to the right, let's
1549 * follow it and find the closest allocated
1550 * block to the right */
1552 block = ext4_idx_pblock(ix);
1553 while (++depth < path->p_depth) {
1554 /* subtract from p_depth to get proper eh_depth */
1555 bh = read_extent_tree_block(inode, block,
1556 path->p_depth - depth, 0);
1559 eh = ext_block_hdr(bh);
1560 ix = EXT_FIRST_INDEX(eh);
1561 block = ext4_idx_pblock(ix);
1565 bh = read_extent_tree_block(inode, block, path->p_depth - depth, 0);
1568 eh = ext_block_hdr(bh);
1569 ex = EXT_FIRST_EXTENT(eh);
1571 *logical = le32_to_cpu(ex->ee_block);
1572 *phys = ext4_ext_pblock(ex);
1580 * ext4_ext_next_allocated_block:
1581 * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
1582 * NOTE: it considers block number from index entry as
1583 * allocated block. Thus, index entries have to be consistent
1587 ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1591 BUG_ON(path == NULL);
1592 depth = path->p_depth;
1594 if (depth == 0 && path->p_ext == NULL)
1595 return EXT_MAX_BLOCKS;
1597 while (depth >= 0) {
1598 if (depth == path->p_depth) {
1600 if (path[depth].p_ext &&
1601 path[depth].p_ext !=
1602 EXT_LAST_EXTENT(path[depth].p_hdr))
1603 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1606 if (path[depth].p_idx !=
1607 EXT_LAST_INDEX(path[depth].p_hdr))
1608 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1613 return EXT_MAX_BLOCKS;
1617 * ext4_ext_next_leaf_block:
1618 * returns first allocated block from next leaf or EXT_MAX_BLOCKS
1620 static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path)
1624 BUG_ON(path == NULL);
1625 depth = path->p_depth;
1627 /* zero-tree has no leaf blocks at all */
1629 return EXT_MAX_BLOCKS;
1631 /* go to index block */
1634 while (depth >= 0) {
1635 if (path[depth].p_idx !=
1636 EXT_LAST_INDEX(path[depth].p_hdr))
1637 return (ext4_lblk_t)
1638 le32_to_cpu(path[depth].p_idx[1].ei_block);
1642 return EXT_MAX_BLOCKS;
1646 * ext4_ext_correct_indexes:
1647 * if leaf gets modified and modified extent is first in the leaf,
1648 * then we have to correct all indexes above.
1649 * TODO: do we need to correct tree in all cases?
1651 static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
1652 struct ext4_ext_path *path)
1654 struct ext4_extent_header *eh;
1655 int depth = ext_depth(inode);
1656 struct ext4_extent *ex;
1660 eh = path[depth].p_hdr;
1661 ex = path[depth].p_ext;
1663 if (unlikely(ex == NULL || eh == NULL)) {
1664 EXT4_ERROR_INODE(inode,
1665 "ex %p == NULL or eh %p == NULL", ex, eh);
1670 /* there is no tree at all */
1674 if (ex != EXT_FIRST_EXTENT(eh)) {
1675 /* we correct tree if first leaf got modified only */
1680 * TODO: we need correction if border is smaller than current one
1683 border = path[depth].p_ext->ee_block;
1684 err = ext4_ext_get_access(handle, inode, path + k);
1687 path[k].p_idx->ei_block = border;
1688 err = ext4_ext_dirty(handle, inode, path + k);
1693 /* change all left-side indexes */
1694 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1696 err = ext4_ext_get_access(handle, inode, path + k);
1699 path[k].p_idx->ei_block = border;
1700 err = ext4_ext_dirty(handle, inode, path + k);
1709 ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1710 struct ext4_extent *ex2)
1712 unsigned short ext1_ee_len, ext2_ee_len;
1715 * Make sure that both extents are initialized. We don't merge
1716 * unwritten extents so that we can be sure that end_io code has
1717 * the extent that was written properly split out and conversion to
1718 * initialized is trivial.
1720 if (ext4_ext_is_unwritten(ex1) != ext4_ext_is_unwritten(ex2))
1723 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1724 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1726 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
1727 le32_to_cpu(ex2->ee_block))
1731 * To allow future support for preallocated extents to be added
1732 * as an RO_COMPAT feature, refuse to merge to extents if
1733 * this can result in the top bit of ee_len being set.
1735 if (ext1_ee_len + ext2_ee_len > EXT_INIT_MAX_LEN)
1737 if (ext4_ext_is_unwritten(ex1) &&
1738 (ext4_test_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN) ||
1739 atomic_read(&EXT4_I(inode)->i_unwritten) ||
1740 (ext1_ee_len + ext2_ee_len > EXT_UNWRITTEN_MAX_LEN)))
1742 #ifdef AGGRESSIVE_TEST
1743 if (ext1_ee_len >= 4)
1747 if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
1753 * This function tries to merge the "ex" extent to the next extent in the tree.
1754 * It always tries to merge towards right. If you want to merge towards
1755 * left, pass "ex - 1" as argument instead of "ex".
1756 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1757 * 1 if they got merged.
1759 static int ext4_ext_try_to_merge_right(struct inode *inode,
1760 struct ext4_ext_path *path,
1761 struct ext4_extent *ex)
1763 struct ext4_extent_header *eh;
1764 unsigned int depth, len;
1765 int merge_done = 0, unwritten;
1767 depth = ext_depth(inode);
1768 BUG_ON(path[depth].p_hdr == NULL);
1769 eh = path[depth].p_hdr;
1771 while (ex < EXT_LAST_EXTENT(eh)) {
1772 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1774 /* merge with next extent! */
1775 unwritten = ext4_ext_is_unwritten(ex);
1776 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1777 + ext4_ext_get_actual_len(ex + 1));
1779 ext4_ext_mark_unwritten(ex);
1781 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1782 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1783 * sizeof(struct ext4_extent);
1784 memmove(ex + 1, ex + 2, len);
1786 le16_add_cpu(&eh->eh_entries, -1);
1788 WARN_ON(eh->eh_entries == 0);
1789 if (!eh->eh_entries)
1790 EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
1797 * This function does a very simple check to see if we can collapse
1798 * an extent tree with a single extent tree leaf block into the inode.
1800 static void ext4_ext_try_to_merge_up(handle_t *handle,
1801 struct inode *inode,
1802 struct ext4_ext_path *path)
1805 unsigned max_root = ext4_ext_space_root(inode, 0);
1808 if ((path[0].p_depth != 1) ||
1809 (le16_to_cpu(path[0].p_hdr->eh_entries) != 1) ||
1810 (le16_to_cpu(path[1].p_hdr->eh_entries) > max_root))
1814 * We need to modify the block allocation bitmap and the block
1815 * group descriptor to release the extent tree block. If we
1816 * can't get the journal credits, give up.
1818 if (ext4_journal_extend(handle, 2))
1822 * Copy the extent data up to the inode
1824 blk = ext4_idx_pblock(path[0].p_idx);
1825 s = le16_to_cpu(path[1].p_hdr->eh_entries) *
1826 sizeof(struct ext4_extent_idx);
1827 s += sizeof(struct ext4_extent_header);
1829 memcpy(path[0].p_hdr, path[1].p_hdr, s);
1830 path[0].p_depth = 0;
1831 path[0].p_ext = EXT_FIRST_EXTENT(path[0].p_hdr) +
1832 (path[1].p_ext - EXT_FIRST_EXTENT(path[1].p_hdr));
1833 path[0].p_hdr->eh_max = cpu_to_le16(max_root);
1835 brelse(path[1].p_bh);
1836 ext4_free_blocks(handle, inode, NULL, blk, 1,
1837 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET |
1838 EXT4_FREE_BLOCKS_RESERVE);
1842 * This function tries to merge the @ex extent to neighbours in the tree.
1843 * return 1 if merge left else 0.
1845 static void ext4_ext_try_to_merge(handle_t *handle,
1846 struct inode *inode,
1847 struct ext4_ext_path *path,
1848 struct ext4_extent *ex) {
1849 struct ext4_extent_header *eh;
1853 depth = ext_depth(inode);
1854 BUG_ON(path[depth].p_hdr == NULL);
1855 eh = path[depth].p_hdr;
1857 if (ex > EXT_FIRST_EXTENT(eh))
1858 merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
1861 (void) ext4_ext_try_to_merge_right(inode, path, ex);
1863 ext4_ext_try_to_merge_up(handle, inode, path);
1867 * check if a portion of the "newext" extent overlaps with an
1870 * If there is an overlap discovered, it updates the length of the newext
1871 * such that there will be no overlap, and then returns 1.
1872 * If there is no overlap found, it returns 0.
1874 static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi,
1875 struct inode *inode,
1876 struct ext4_extent *newext,
1877 struct ext4_ext_path *path)
1880 unsigned int depth, len1;
1881 unsigned int ret = 0;
1883 b1 = le32_to_cpu(newext->ee_block);
1884 len1 = ext4_ext_get_actual_len(newext);
1885 depth = ext_depth(inode);
1886 if (!path[depth].p_ext)
1888 b2 = EXT4_LBLK_CMASK(sbi, le32_to_cpu(path[depth].p_ext->ee_block));
1891 * get the next allocated block if the extent in the path
1892 * is before the requested block(s)
1895 b2 = ext4_ext_next_allocated_block(path);
1896 if (b2 == EXT_MAX_BLOCKS)
1898 b2 = EXT4_LBLK_CMASK(sbi, b2);
1901 /* check for wrap through zero on extent logical start block*/
1902 if (b1 + len1 < b1) {
1903 len1 = EXT_MAX_BLOCKS - b1;
1904 newext->ee_len = cpu_to_le16(len1);
1908 /* check for overlap */
1909 if (b1 + len1 > b2) {
1910 newext->ee_len = cpu_to_le16(b2 - b1);
1918 * ext4_ext_insert_extent:
1919 * tries to merge requsted extent into the existing extent or
1920 * inserts requested extent as new one into the tree,
1921 * creating new leaf in the no-space case.
1923 int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1924 struct ext4_ext_path *path,
1925 struct ext4_extent *newext, int gb_flags)
1927 struct ext4_extent_header *eh;
1928 struct ext4_extent *ex, *fex;
1929 struct ext4_extent *nearex; /* nearest extent */
1930 struct ext4_ext_path *npath = NULL;
1931 int depth, len, err;
1933 int mb_flags = 0, unwritten;
1935 if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1936 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
1939 depth = ext_depth(inode);
1940 ex = path[depth].p_ext;
1941 eh = path[depth].p_hdr;
1942 if (unlikely(path[depth].p_hdr == NULL)) {
1943 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1947 /* try to insert block into found extent and return */
1948 if (ex && !(gb_flags & EXT4_GET_BLOCKS_PRE_IO)) {
1951 * Try to see whether we should rather test the extent on
1952 * right from ex, or from the left of ex. This is because
1953 * ext4_ext_find_extent() can return either extent on the
1954 * left, or on the right from the searched position. This
1955 * will make merging more effective.
1957 if (ex < EXT_LAST_EXTENT(eh) &&
1958 (le32_to_cpu(ex->ee_block) +
1959 ext4_ext_get_actual_len(ex) <
1960 le32_to_cpu(newext->ee_block))) {
1963 } else if ((ex > EXT_FIRST_EXTENT(eh)) &&
1964 (le32_to_cpu(newext->ee_block) +
1965 ext4_ext_get_actual_len(newext) <
1966 le32_to_cpu(ex->ee_block)))
1969 /* Try to append newex to the ex */
1970 if (ext4_can_extents_be_merged(inode, ex, newext)) {
1971 ext_debug("append [%d]%d block to %u:[%d]%d"
1973 ext4_ext_is_unwritten(newext),
1974 ext4_ext_get_actual_len(newext),
1975 le32_to_cpu(ex->ee_block),
1976 ext4_ext_is_unwritten(ex),
1977 ext4_ext_get_actual_len(ex),
1978 ext4_ext_pblock(ex));
1979 err = ext4_ext_get_access(handle, inode,
1983 unwritten = ext4_ext_is_unwritten(ex);
1984 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1985 + ext4_ext_get_actual_len(newext));
1987 ext4_ext_mark_unwritten(ex);
1988 eh = path[depth].p_hdr;
1994 /* Try to prepend newex to the ex */
1995 if (ext4_can_extents_be_merged(inode, newext, ex)) {
1996 ext_debug("prepend %u[%d]%d block to %u:[%d]%d"
1998 le32_to_cpu(newext->ee_block),
1999 ext4_ext_is_unwritten(newext),
2000 ext4_ext_get_actual_len(newext),
2001 le32_to_cpu(ex->ee_block),
2002 ext4_ext_is_unwritten(ex),
2003 ext4_ext_get_actual_len(ex),
2004 ext4_ext_pblock(ex));
2005 err = ext4_ext_get_access(handle, inode,
2010 unwritten = ext4_ext_is_unwritten(ex);
2011 ex->ee_block = newext->ee_block;
2012 ext4_ext_store_pblock(ex, ext4_ext_pblock(newext));
2013 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
2014 + ext4_ext_get_actual_len(newext));
2016 ext4_ext_mark_unwritten(ex);
2017 eh = path[depth].p_hdr;
2023 depth = ext_depth(inode);
2024 eh = path[depth].p_hdr;
2025 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
2028 /* probably next leaf has space for us? */
2029 fex = EXT_LAST_EXTENT(eh);
2030 next = EXT_MAX_BLOCKS;
2031 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block))
2032 next = ext4_ext_next_leaf_block(path);
2033 if (next != EXT_MAX_BLOCKS) {
2034 ext_debug("next leaf block - %u\n", next);
2035 BUG_ON(npath != NULL);
2036 npath = ext4_ext_find_extent(inode, next, NULL, 0);
2038 return PTR_ERR(npath);
2039 BUG_ON(npath->p_depth != path->p_depth);
2040 eh = npath[depth].p_hdr;
2041 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
2042 ext_debug("next leaf isn't full(%d)\n",
2043 le16_to_cpu(eh->eh_entries));
2047 ext_debug("next leaf has no free space(%d,%d)\n",
2048 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
2052 * There is no free space in the found leaf.
2053 * We're gonna add a new leaf in the tree.
2055 if (gb_flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
2056 mb_flags = EXT4_MB_USE_RESERVED;
2057 err = ext4_ext_create_new_leaf(handle, inode, mb_flags, gb_flags,
2061 depth = ext_depth(inode);
2062 eh = path[depth].p_hdr;
2065 nearex = path[depth].p_ext;
2067 err = ext4_ext_get_access(handle, inode, path + depth);
2072 /* there is no extent in this leaf, create first one */
2073 ext_debug("first extent in the leaf: %u:%llu:[%d]%d\n",
2074 le32_to_cpu(newext->ee_block),
2075 ext4_ext_pblock(newext),
2076 ext4_ext_is_unwritten(newext),
2077 ext4_ext_get_actual_len(newext));
2078 nearex = EXT_FIRST_EXTENT(eh);
2080 if (le32_to_cpu(newext->ee_block)
2081 > le32_to_cpu(nearex->ee_block)) {
2083 ext_debug("insert %u:%llu:[%d]%d before: "
2085 le32_to_cpu(newext->ee_block),
2086 ext4_ext_pblock(newext),
2087 ext4_ext_is_unwritten(newext),
2088 ext4_ext_get_actual_len(newext),
2093 BUG_ON(newext->ee_block == nearex->ee_block);
2094 ext_debug("insert %u:%llu:[%d]%d after: "
2096 le32_to_cpu(newext->ee_block),
2097 ext4_ext_pblock(newext),
2098 ext4_ext_is_unwritten(newext),
2099 ext4_ext_get_actual_len(newext),
2102 len = EXT_LAST_EXTENT(eh) - nearex + 1;
2104 ext_debug("insert %u:%llu:[%d]%d: "
2105 "move %d extents from 0x%p to 0x%p\n",
2106 le32_to_cpu(newext->ee_block),
2107 ext4_ext_pblock(newext),
2108 ext4_ext_is_unwritten(newext),
2109 ext4_ext_get_actual_len(newext),
2110 len, nearex, nearex + 1);
2111 memmove(nearex + 1, nearex,
2112 len * sizeof(struct ext4_extent));
2116 le16_add_cpu(&eh->eh_entries, 1);
2117 path[depth].p_ext = nearex;
2118 nearex->ee_block = newext->ee_block;
2119 ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
2120 nearex->ee_len = newext->ee_len;
2123 /* try to merge extents */
2124 if (!(gb_flags & EXT4_GET_BLOCKS_PRE_IO))
2125 ext4_ext_try_to_merge(handle, inode, path, nearex);
2128 /* time to correct all indexes above */
2129 err = ext4_ext_correct_indexes(handle, inode, path);
2133 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
2137 ext4_ext_drop_refs(npath);
2143 static int ext4_fill_fiemap_extents(struct inode *inode,
2144 ext4_lblk_t block, ext4_lblk_t num,
2145 struct fiemap_extent_info *fieinfo)
2147 struct ext4_ext_path *path = NULL;
2148 struct ext4_extent *ex;
2149 struct extent_status es;
2150 ext4_lblk_t next, next_del, start = 0, end = 0;
2151 ext4_lblk_t last = block + num;
2152 int exists, depth = 0, err = 0;
2153 unsigned int flags = 0;
2154 unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
2156 while (block < last && block != EXT_MAX_BLOCKS) {
2158 /* find extent for this block */
2159 down_read(&EXT4_I(inode)->i_data_sem);
2161 if (path && ext_depth(inode) != depth) {
2162 /* depth was changed. we have to realloc path */
2167 path = ext4_ext_find_extent(inode, block, path, 0);
2169 up_read(&EXT4_I(inode)->i_data_sem);
2170 err = PTR_ERR(path);
2175 depth = ext_depth(inode);
2176 if (unlikely(path[depth].p_hdr == NULL)) {
2177 up_read(&EXT4_I(inode)->i_data_sem);
2178 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
2182 ex = path[depth].p_ext;
2183 next = ext4_ext_next_allocated_block(path);
2184 ext4_ext_drop_refs(path);
2189 /* there is no extent yet, so try to allocate
2190 * all requested space */
2193 } else if (le32_to_cpu(ex->ee_block) > block) {
2194 /* need to allocate space before found extent */
2196 end = le32_to_cpu(ex->ee_block);
2197 if (block + num < end)
2199 } else if (block >= le32_to_cpu(ex->ee_block)
2200 + ext4_ext_get_actual_len(ex)) {
2201 /* need to allocate space after found extent */
2206 } else if (block >= le32_to_cpu(ex->ee_block)) {
2208 * some part of requested space is covered
2212 end = le32_to_cpu(ex->ee_block)
2213 + ext4_ext_get_actual_len(ex);
2214 if (block + num < end)
2220 BUG_ON(end <= start);
2224 es.es_len = end - start;
2227 es.es_lblk = le32_to_cpu(ex->ee_block);
2228 es.es_len = ext4_ext_get_actual_len(ex);
2229 es.es_pblk = ext4_ext_pblock(ex);
2230 if (ext4_ext_is_unwritten(ex))
2231 flags |= FIEMAP_EXTENT_UNWRITTEN;
2235 * Find delayed extent and update es accordingly. We call
2236 * it even in !exists case to find out whether es is the
2237 * last existing extent or not.
2239 next_del = ext4_find_delayed_extent(inode, &es);
2240 if (!exists && next_del) {
2242 flags |= (FIEMAP_EXTENT_DELALLOC |
2243 FIEMAP_EXTENT_UNKNOWN);
2245 up_read(&EXT4_I(inode)->i_data_sem);
2247 if (unlikely(es.es_len == 0)) {
2248 EXT4_ERROR_INODE(inode, "es.es_len == 0");
2254 * This is possible iff next == next_del == EXT_MAX_BLOCKS.
2255 * we need to check next == EXT_MAX_BLOCKS because it is
2256 * possible that an extent is with unwritten and delayed
2257 * status due to when an extent is delayed allocated and
2258 * is allocated by fallocate status tree will track both of
2261 * So we could return a unwritten and delayed extent, and
2262 * its block is equal to 'next'.
2264 if (next == next_del && next == EXT_MAX_BLOCKS) {
2265 flags |= FIEMAP_EXTENT_LAST;
2266 if (unlikely(next_del != EXT_MAX_BLOCKS ||
2267 next != EXT_MAX_BLOCKS)) {
2268 EXT4_ERROR_INODE(inode,
2269 "next extent == %u, next "
2270 "delalloc extent = %u",
2278 err = fiemap_fill_next_extent(fieinfo,
2279 (__u64)es.es_lblk << blksize_bits,
2280 (__u64)es.es_pblk << blksize_bits,
2281 (__u64)es.es_len << blksize_bits,
2291 block = es.es_lblk + es.es_len;
2295 ext4_ext_drop_refs(path);
2303 * ext4_ext_put_gap_in_cache:
2304 * calculate boundaries of the gap that the requested block fits into
2305 * and cache this gap
2308 ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
2311 int depth = ext_depth(inode);
2312 unsigned long len = 0;
2313 ext4_lblk_t lblock = 0;
2314 struct ext4_extent *ex;
2316 ex = path[depth].p_ext;
2319 * there is no extent yet, so gap is [0;-] and we
2322 ext_debug("cache gap(whole file):");
2323 } else if (block < le32_to_cpu(ex->ee_block)) {
2325 len = le32_to_cpu(ex->ee_block) - block;
2326 ext_debug("cache gap(before): %u [%u:%u]",
2328 le32_to_cpu(ex->ee_block),
2329 ext4_ext_get_actual_len(ex));
2330 if (!ext4_find_delalloc_range(inode, lblock, lblock + len - 1))
2331 ext4_es_insert_extent(inode, lblock, len, ~0,
2332 EXTENT_STATUS_HOLE);
2333 } else if (block >= le32_to_cpu(ex->ee_block)
2334 + ext4_ext_get_actual_len(ex)) {
2336 lblock = le32_to_cpu(ex->ee_block)
2337 + ext4_ext_get_actual_len(ex);
2339 next = ext4_ext_next_allocated_block(path);
2340 ext_debug("cache gap(after): [%u:%u] %u",
2341 le32_to_cpu(ex->ee_block),
2342 ext4_ext_get_actual_len(ex),
2344 BUG_ON(next == lblock);
2345 len = next - lblock;
2346 if (!ext4_find_delalloc_range(inode, lblock, lblock + len - 1))
2347 ext4_es_insert_extent(inode, lblock, len, ~0,
2348 EXTENT_STATUS_HOLE);
2353 ext_debug(" -> %u:%lu\n", lblock, len);
2358 * removes index from the index block.
2360 static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
2361 struct ext4_ext_path *path, int depth)
2366 /* free index block */
2368 path = path + depth;
2369 leaf = ext4_idx_pblock(path->p_idx);
2370 if (unlikely(path->p_hdr->eh_entries == 0)) {
2371 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
2374 err = ext4_ext_get_access(handle, inode, path);
2378 if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) {
2379 int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx;
2380 len *= sizeof(struct ext4_extent_idx);
2381 memmove(path->p_idx, path->p_idx + 1, len);
2384 le16_add_cpu(&path->p_hdr->eh_entries, -1);
2385 err = ext4_ext_dirty(handle, inode, path);
2388 ext_debug("index is empty, remove it, free block %llu\n", leaf);
2389 trace_ext4_ext_rm_idx(inode, leaf);
2391 ext4_free_blocks(handle, inode, NULL, leaf, 1,
2392 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
2394 while (--depth >= 0) {
2395 if (path->p_idx != EXT_FIRST_INDEX(path->p_hdr))
2398 err = ext4_ext_get_access(handle, inode, path);
2401 path->p_idx->ei_block = (path+1)->p_idx->ei_block;
2402 err = ext4_ext_dirty(handle, inode, path);
2410 * ext4_ext_calc_credits_for_single_extent:
2411 * This routine returns max. credits that needed to insert an extent
2412 * to the extent tree.
2413 * When pass the actual path, the caller should calculate credits
2416 int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
2417 struct ext4_ext_path *path)
2420 int depth = ext_depth(inode);
2423 /* probably there is space in leaf? */
2424 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
2425 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2428 * There are some space in the leaf tree, no
2429 * need to account for leaf block credit
2431 * bitmaps and block group descriptor blocks
2432 * and other metadata blocks still need to be
2435 /* 1 bitmap, 1 block group descriptor */
2436 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
2441 return ext4_chunk_trans_blocks(inode, nrblocks);
2445 * How many index/leaf blocks need to change/allocate to add @extents extents?
2447 * If we add a single extent, then in the worse case, each tree level
2448 * index/leaf need to be changed in case of the tree split.
2450 * If more extents are inserted, they could cause the whole tree split more
2451 * than once, but this is really rare.
2453 int ext4_ext_index_trans_blocks(struct inode *inode, int extents)
2458 /* If we are converting the inline data, only one is needed here. */
2459 if (ext4_has_inline_data(inode))
2462 depth = ext_depth(inode);
2472 static inline int get_default_free_blocks_flags(struct inode *inode)
2474 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2475 return EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET;
2476 else if (ext4_should_journal_data(inode))
2477 return EXT4_FREE_BLOCKS_FORGET;
2481 static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
2482 struct ext4_extent *ex,
2483 long long *partial_cluster,
2484 ext4_lblk_t from, ext4_lblk_t to)
2486 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2487 unsigned short ee_len = ext4_ext_get_actual_len(ex);
2489 int flags = get_default_free_blocks_flags(inode);
2492 * For bigalloc file systems, we never free a partial cluster
2493 * at the beginning of the extent. Instead, we make a note
2494 * that we tried freeing the cluster, and check to see if we
2495 * need to free it on a subsequent call to ext4_remove_blocks,
2496 * or at the end of the ext4_truncate() operation.
2498 flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER;
2500 trace_ext4_remove_blocks(inode, ex, from, to, *partial_cluster);
2502 * If we have a partial cluster, and it's different from the
2503 * cluster of the last block, we need to explicitly free the
2504 * partial cluster here.
2506 pblk = ext4_ext_pblock(ex) + ee_len - 1;
2507 if ((*partial_cluster > 0) &&
2508 (EXT4_B2C(sbi, pblk) != *partial_cluster)) {
2509 ext4_free_blocks(handle, inode, NULL,
2510 EXT4_C2B(sbi, *partial_cluster),
2511 sbi->s_cluster_ratio, flags);
2512 *partial_cluster = 0;
2515 #ifdef EXTENTS_STATS
2517 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2518 spin_lock(&sbi->s_ext_stats_lock);
2519 sbi->s_ext_blocks += ee_len;
2520 sbi->s_ext_extents++;
2521 if (ee_len < sbi->s_ext_min)
2522 sbi->s_ext_min = ee_len;
2523 if (ee_len > sbi->s_ext_max)
2524 sbi->s_ext_max = ee_len;
2525 if (ext_depth(inode) > sbi->s_depth_max)
2526 sbi->s_depth_max = ext_depth(inode);
2527 spin_unlock(&sbi->s_ext_stats_lock);
2530 if (from >= le32_to_cpu(ex->ee_block)
2531 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
2534 unsigned int unaligned;
2536 num = le32_to_cpu(ex->ee_block) + ee_len - from;
2537 pblk = ext4_ext_pblock(ex) + ee_len - num;
2539 * Usually we want to free partial cluster at the end of the
2540 * extent, except for the situation when the cluster is still
2541 * used by any other extent (partial_cluster is negative).
2543 if (*partial_cluster < 0 &&
2544 -(*partial_cluster) == EXT4_B2C(sbi, pblk + num - 1))
2545 flags |= EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER;
2547 ext_debug("free last %u blocks starting %llu partial %lld\n",
2548 num, pblk, *partial_cluster);
2549 ext4_free_blocks(handle, inode, NULL, pblk, num, flags);
2551 * If the block range to be freed didn't start at the
2552 * beginning of a cluster, and we removed the entire
2553 * extent and the cluster is not used by any other extent,
2554 * save the partial cluster here, since we might need to
2555 * delete if we determine that the truncate operation has
2556 * removed all of the blocks in the cluster.
2558 * On the other hand, if we did not manage to free the whole
2559 * extent, we have to mark the cluster as used (store negative
2560 * cluster number in partial_cluster).
2562 unaligned = EXT4_PBLK_COFF(sbi, pblk);
2563 if (unaligned && (ee_len == num) &&
2564 (*partial_cluster != -((long long)EXT4_B2C(sbi, pblk))))
2565 *partial_cluster = EXT4_B2C(sbi, pblk);
2567 *partial_cluster = -((long long)EXT4_B2C(sbi, pblk));
2568 else if (*partial_cluster > 0)
2569 *partial_cluster = 0;
2571 ext4_error(sbi->s_sb, "strange request: removal(2) "
2572 "%u-%u from %u:%u\n",
2573 from, to, le32_to_cpu(ex->ee_block), ee_len);
2579 * ext4_ext_rm_leaf() Removes the extents associated with the
2580 * blocks appearing between "start" and "end", and splits the extents
2581 * if "start" and "end" appear in the same extent
2583 * @handle: The journal handle
2584 * @inode: The files inode
2585 * @path: The path to the leaf
2586 * @partial_cluster: The cluster which we'll have to free if all extents
2587 * has been released from it. It gets negative in case
2588 * that the cluster is still used.
2589 * @start: The first block to remove
2590 * @end: The last block to remove
2593 ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
2594 struct ext4_ext_path *path,
2595 long long *partial_cluster,
2596 ext4_lblk_t start, ext4_lblk_t end)
2598 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2599 int err = 0, correct_index = 0;
2600 int depth = ext_depth(inode), credits;
2601 struct ext4_extent_header *eh;
2604 ext4_lblk_t ex_ee_block;
2605 unsigned short ex_ee_len;
2606 unsigned unwritten = 0;
2607 struct ext4_extent *ex;
2610 /* the header must be checked already in ext4_ext_remove_space() */
2611 ext_debug("truncate since %u in leaf to %u\n", start, end);
2612 if (!path[depth].p_hdr)
2613 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2614 eh = path[depth].p_hdr;
2615 if (unlikely(path[depth].p_hdr == NULL)) {
2616 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
2619 /* find where to start removing */
2620 ex = path[depth].p_ext;
2622 ex = EXT_LAST_EXTENT(eh);
2624 ex_ee_block = le32_to_cpu(ex->ee_block);
2625 ex_ee_len = ext4_ext_get_actual_len(ex);
2628 * If we're starting with an extent other than the last one in the
2629 * node, we need to see if it shares a cluster with the extent to
2630 * the right (towards the end of the file). If its leftmost cluster
2631 * is this extent's rightmost cluster and it is not cluster aligned,
2632 * we'll mark it as a partial that is not to be deallocated.
2635 if (ex != EXT_LAST_EXTENT(eh)) {
2636 ext4_fsblk_t current_pblk, right_pblk;
2637 long long current_cluster, right_cluster;
2639 current_pblk = ext4_ext_pblock(ex) + ex_ee_len - 1;
2640 current_cluster = (long long)EXT4_B2C(sbi, current_pblk);
2641 right_pblk = ext4_ext_pblock(ex + 1);
2642 right_cluster = (long long)EXT4_B2C(sbi, right_pblk);
2643 if (current_cluster == right_cluster &&
2644 EXT4_PBLK_COFF(sbi, right_pblk))
2645 *partial_cluster = -right_cluster;
2648 trace_ext4_ext_rm_leaf(inode, start, ex, *partial_cluster);
2650 while (ex >= EXT_FIRST_EXTENT(eh) &&
2651 ex_ee_block + ex_ee_len > start) {
2653 if (ext4_ext_is_unwritten(ex))
2658 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
2659 unwritten, ex_ee_len);
2660 path[depth].p_ext = ex;
2662 a = ex_ee_block > start ? ex_ee_block : start;
2663 b = ex_ee_block+ex_ee_len - 1 < end ?
2664 ex_ee_block+ex_ee_len - 1 : end;
2666 ext_debug(" border %u:%u\n", a, b);
2668 /* If this extent is beyond the end of the hole, skip it */
2669 if (end < ex_ee_block) {
2671 * We're going to skip this extent and move to another,
2672 * so if this extent is not cluster aligned we have
2673 * to mark the current cluster as used to avoid
2674 * accidentally freeing it later on
2676 pblk = ext4_ext_pblock(ex);
2677 if (EXT4_PBLK_COFF(sbi, pblk))
2679 -((long long)EXT4_B2C(sbi, pblk));
2681 ex_ee_block = le32_to_cpu(ex->ee_block);
2682 ex_ee_len = ext4_ext_get_actual_len(ex);
2684 } else if (b != ex_ee_block + ex_ee_len - 1) {
2685 EXT4_ERROR_INODE(inode,
2686 "can not handle truncate %u:%u "
2688 start, end, ex_ee_block,
2689 ex_ee_block + ex_ee_len - 1);
2692 } else if (a != ex_ee_block) {
2693 /* remove tail of the extent */
2694 num = a - ex_ee_block;
2696 /* remove whole extent: excellent! */
2700 * 3 for leaf, sb, and inode plus 2 (bmap and group
2701 * descriptor) for each block group; assume two block
2702 * groups plus ex_ee_len/blocks_per_block_group for
2705 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
2706 if (ex == EXT_FIRST_EXTENT(eh)) {
2708 credits += (ext_depth(inode)) + 1;
2710 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
2712 err = ext4_ext_truncate_extend_restart(handle, inode, credits);
2716 err = ext4_ext_get_access(handle, inode, path + depth);
2720 err = ext4_remove_blocks(handle, inode, ex, partial_cluster,
2726 /* this extent is removed; mark slot entirely unused */
2727 ext4_ext_store_pblock(ex, 0);
2729 ex->ee_len = cpu_to_le16(num);
2731 * Do not mark unwritten if all the blocks in the
2732 * extent have been removed.
2734 if (unwritten && num)
2735 ext4_ext_mark_unwritten(ex);
2737 * If the extent was completely released,
2738 * we need to remove it from the leaf
2741 if (end != EXT_MAX_BLOCKS - 1) {
2743 * For hole punching, we need to scoot all the
2744 * extents up when an extent is removed so that
2745 * we dont have blank extents in the middle
2747 memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) *
2748 sizeof(struct ext4_extent));
2750 /* Now get rid of the one at the end */
2751 memset(EXT_LAST_EXTENT(eh), 0,
2752 sizeof(struct ext4_extent));
2754 le16_add_cpu(&eh->eh_entries, -1);
2755 } else if (*partial_cluster > 0)
2756 *partial_cluster = 0;
2758 err = ext4_ext_dirty(handle, inode, path + depth);
2762 ext_debug("new extent: %u:%u:%llu\n", ex_ee_block, num,
2763 ext4_ext_pblock(ex));
2765 ex_ee_block = le32_to_cpu(ex->ee_block);
2766 ex_ee_len = ext4_ext_get_actual_len(ex);
2769 if (correct_index && eh->eh_entries)
2770 err = ext4_ext_correct_indexes(handle, inode, path);
2773 * If there's a partial cluster and at least one extent remains in
2774 * the leaf, free the partial cluster if it isn't shared with the
2775 * current extent. If there's a partial cluster and no extents
2776 * remain in the leaf, it can't be freed here. It can only be
2777 * freed when it's possible to determine if it's not shared with
2778 * any other extent - when the next leaf is processed or when space
2779 * removal is complete.
2781 if (*partial_cluster > 0 && eh->eh_entries &&
2782 (EXT4_B2C(sbi, ext4_ext_pblock(ex) + ex_ee_len - 1) !=
2783 *partial_cluster)) {
2784 int flags = get_default_free_blocks_flags(inode);
2786 ext4_free_blocks(handle, inode, NULL,
2787 EXT4_C2B(sbi, *partial_cluster),
2788 sbi->s_cluster_ratio, flags);
2789 *partial_cluster = 0;
2792 /* if this leaf is free, then we should
2793 * remove it from index block above */
2794 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
2795 err = ext4_ext_rm_idx(handle, inode, path, depth);
2802 * ext4_ext_more_to_rm:
2803 * returns 1 if current index has to be freed (even partial)
2806 ext4_ext_more_to_rm(struct ext4_ext_path *path)
2808 BUG_ON(path->p_idx == NULL);
2810 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2814 * if truncate on deeper level happened, it wasn't partial,
2815 * so we have to consider current index for truncation
2817 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2822 int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
2825 struct super_block *sb = inode->i_sb;
2826 int depth = ext_depth(inode);
2827 struct ext4_ext_path *path = NULL;
2828 long long partial_cluster = 0;
2832 ext_debug("truncate since %u to %u\n", start, end);
2834 /* probably first extent we're gonna free will be last in block */
2835 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, depth + 1);
2837 return PTR_ERR(handle);
2840 trace_ext4_ext_remove_space(inode, start, end, depth);
2843 * Check if we are removing extents inside the extent tree. If that
2844 * is the case, we are going to punch a hole inside the extent tree
2845 * so we have to check whether we need to split the extent covering
2846 * the last block to remove so we can easily remove the part of it
2847 * in ext4_ext_rm_leaf().
2849 if (end < EXT_MAX_BLOCKS - 1) {
2850 struct ext4_extent *ex;
2851 ext4_lblk_t ee_block;
2853 /* find extent for this block */
2854 path = ext4_ext_find_extent(inode, end, NULL, EXT4_EX_NOCACHE);
2856 ext4_journal_stop(handle);
2857 return PTR_ERR(path);
2859 depth = ext_depth(inode);
2860 /* Leaf not may not exist only if inode has no blocks at all */
2861 ex = path[depth].p_ext;
2864 EXT4_ERROR_INODE(inode,
2865 "path[%d].p_hdr == NULL",
2872 ee_block = le32_to_cpu(ex->ee_block);
2875 * See if the last block is inside the extent, if so split
2876 * the extent at 'end' block so we can easily remove the
2877 * tail of the first part of the split extent in
2878 * ext4_ext_rm_leaf().
2880 if (end >= ee_block &&
2881 end < ee_block + ext4_ext_get_actual_len(ex) - 1) {
2884 if (ext4_ext_is_unwritten(ex))
2885 split_flag = EXT4_EXT_MARK_UNWRIT1 |
2886 EXT4_EXT_MARK_UNWRIT2;
2889 * Split the extent in two so that 'end' is the last
2890 * block in the first new extent. Also we should not
2891 * fail removing space due to ENOSPC so try to use
2892 * reserved block if that happens.
2894 err = ext4_split_extent_at(handle, inode, path,
2895 end + 1, split_flag,
2897 EXT4_GET_BLOCKS_PRE_IO |
2898 EXT4_GET_BLOCKS_METADATA_NOFAIL);
2905 * We start scanning from right side, freeing all the blocks
2906 * after i_size and walking into the tree depth-wise.
2908 depth = ext_depth(inode);
2913 le16_to_cpu(path[k].p_hdr->eh_entries)+1;
2915 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1),
2918 ext4_journal_stop(handle);
2921 path[0].p_depth = depth;
2922 path[0].p_hdr = ext_inode_hdr(inode);
2925 if (ext4_ext_check(inode, path[0].p_hdr, depth, 0)) {
2932 while (i >= 0 && err == 0) {
2934 /* this is leaf block */
2935 err = ext4_ext_rm_leaf(handle, inode, path,
2936 &partial_cluster, start,
2938 /* root level has p_bh == NULL, brelse() eats this */
2939 brelse(path[i].p_bh);
2940 path[i].p_bh = NULL;
2945 /* this is index block */
2946 if (!path[i].p_hdr) {
2947 ext_debug("initialize header\n");
2948 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
2951 if (!path[i].p_idx) {
2952 /* this level hasn't been touched yet */
2953 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2954 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2955 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2957 le16_to_cpu(path[i].p_hdr->eh_entries));
2959 /* we were already here, see at next index */
2963 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2964 i, EXT_FIRST_INDEX(path[i].p_hdr),
2966 if (ext4_ext_more_to_rm(path + i)) {
2967 struct buffer_head *bh;
2968 /* go to the next level */
2969 ext_debug("move to level %d (block %llu)\n",
2970 i + 1, ext4_idx_pblock(path[i].p_idx));
2971 memset(path + i + 1, 0, sizeof(*path));
2972 bh = read_extent_tree_block(inode,
2973 ext4_idx_pblock(path[i].p_idx), depth - i - 1,
2976 /* should we reset i_size? */
2980 /* Yield here to deal with large extent trees.
2981 * Should be a no-op if we did IO above. */
2983 if (WARN_ON(i + 1 > depth)) {
2987 path[i + 1].p_bh = bh;
2989 /* save actual number of indexes since this
2990 * number is changed at the next iteration */
2991 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2994 /* we finished processing this index, go up */
2995 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
2996 /* index is empty, remove it;
2997 * handle must be already prepared by the
2998 * truncatei_leaf() */
2999 err = ext4_ext_rm_idx(handle, inode, path, i);
3001 /* root level has p_bh == NULL, brelse() eats this */
3002 brelse(path[i].p_bh);
3003 path[i].p_bh = NULL;
3005 ext_debug("return to level %d\n", i);
3009 trace_ext4_ext_remove_space_done(inode, start, end, depth,
3010 partial_cluster, path->p_hdr->eh_entries);
3012 /* If we still have something in the partial cluster and we have removed
3013 * even the first extent, then we should free the blocks in the partial
3014 * cluster as well. */
3015 if (partial_cluster > 0 && path->p_hdr->eh_entries == 0) {
3016 int flags = get_default_free_blocks_flags(inode);
3018 ext4_free_blocks(handle, inode, NULL,
3019 EXT4_C2B(EXT4_SB(sb), partial_cluster),
3020 EXT4_SB(sb)->s_cluster_ratio, flags);
3021 partial_cluster = 0;
3024 /* TODO: flexible tree reduction should be here */
3025 if (path->p_hdr->eh_entries == 0) {
3027 * truncate to zero freed all the tree,
3028 * so we need to correct eh_depth
3030 err = ext4_ext_get_access(handle, inode, path);
3032 ext_inode_hdr(inode)->eh_depth = 0;
3033 ext_inode_hdr(inode)->eh_max =
3034 cpu_to_le16(ext4_ext_space_root(inode, 0));
3035 err = ext4_ext_dirty(handle, inode, path);
3039 ext4_ext_drop_refs(path);
3041 if (err == -EAGAIN) {
3045 ext4_journal_stop(handle);
3051 * called at mount time
3053 void ext4_ext_init(struct super_block *sb)
3056 * possible initialization would be here
3059 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
3060 #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
3061 printk(KERN_INFO "EXT4-fs: file extents enabled"
3062 #ifdef AGGRESSIVE_TEST
3063 ", aggressive tests"
3065 #ifdef CHECK_BINSEARCH
3068 #ifdef EXTENTS_STATS
3073 #ifdef EXTENTS_STATS
3074 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
3075 EXT4_SB(sb)->s_ext_min = 1 << 30;
3076 EXT4_SB(sb)->s_ext_max = 0;
3082 * called at umount time
3084 void ext4_ext_release(struct super_block *sb)
3086 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
3089 #ifdef EXTENTS_STATS
3090 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
3091 struct ext4_sb_info *sbi = EXT4_SB(sb);
3092 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
3093 sbi->s_ext_blocks, sbi->s_ext_extents,
3094 sbi->s_ext_blocks / sbi->s_ext_extents);
3095 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
3096 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
3101 static int ext4_zeroout_es(struct inode *inode, struct ext4_extent *ex)
3103 ext4_lblk_t ee_block;
3104 ext4_fsblk_t ee_pblock;
3105 unsigned int ee_len;
3107 ee_block = le32_to_cpu(ex->ee_block);
3108 ee_len = ext4_ext_get_actual_len(ex);
3109 ee_pblock = ext4_ext_pblock(ex);
3114 return ext4_es_insert_extent(inode, ee_block, ee_len, ee_pblock,
3115 EXTENT_STATUS_WRITTEN);
3118 /* FIXME!! we need to try to merge to left or right after zero-out */
3119 static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
3121 ext4_fsblk_t ee_pblock;
3122 unsigned int ee_len;
3125 ee_len = ext4_ext_get_actual_len(ex);
3126 ee_pblock = ext4_ext_pblock(ex);
3128 ret = sb_issue_zeroout(inode->i_sb, ee_pblock, ee_len, GFP_NOFS);
3136 * ext4_split_extent_at() splits an extent at given block.
3138 * @handle: the journal handle
3139 * @inode: the file inode
3140 * @path: the path to the extent
3141 * @split: the logical block where the extent is splitted.
3142 * @split_flags: indicates if the extent could be zeroout if split fails, and
3143 * the states(init or unwritten) of new extents.
3144 * @flags: flags used to insert new extent to extent tree.
3147 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
3148 * of which are deterimined by split_flag.
3150 * There are two cases:
3151 * a> the extent are splitted into two extent.
3152 * b> split is not needed, and just mark the extent.
3154 * return 0 on success.
3156 static int ext4_split_extent_at(handle_t *handle,
3157 struct inode *inode,
3158 struct ext4_ext_path *path,
3163 ext4_fsblk_t newblock;
3164 ext4_lblk_t ee_block;
3165 struct ext4_extent *ex, newex, orig_ex, zero_ex;
3166 struct ext4_extent *ex2 = NULL;
3167 unsigned int ee_len, depth;
3170 BUG_ON((split_flag & (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)) ==
3171 (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2));
3173 ext_debug("ext4_split_extents_at: inode %lu, logical"
3174 "block %llu\n", inode->i_ino, (unsigned long long)split);
3176 ext4_ext_show_leaf(inode, path);
3178 depth = ext_depth(inode);
3179 ex = path[depth].p_ext;
3180 ee_block = le32_to_cpu(ex->ee_block);
3181 ee_len = ext4_ext_get_actual_len(ex);
3182 newblock = split - ee_block + ext4_ext_pblock(ex);
3184 BUG_ON(split < ee_block || split >= (ee_block + ee_len));
3185 BUG_ON(!ext4_ext_is_unwritten(ex) &&
3186 split_flag & (EXT4_EXT_MAY_ZEROOUT |
3187 EXT4_EXT_MARK_UNWRIT1 |
3188 EXT4_EXT_MARK_UNWRIT2));
3190 err = ext4_ext_get_access(handle, inode, path + depth);
3194 if (split == ee_block) {
3196 * case b: block @split is the block that the extent begins with
3197 * then we just change the state of the extent, and splitting
3200 if (split_flag & EXT4_EXT_MARK_UNWRIT2)
3201 ext4_ext_mark_unwritten(ex);
3203 ext4_ext_mark_initialized(ex);
3205 if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
3206 ext4_ext_try_to_merge(handle, inode, path, ex);
3208 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
3213 memcpy(&orig_ex, ex, sizeof(orig_ex));
3214 ex->ee_len = cpu_to_le16(split - ee_block);
3215 if (split_flag & EXT4_EXT_MARK_UNWRIT1)
3216 ext4_ext_mark_unwritten(ex);
3219 * path may lead to new leaf, not to original leaf any more
3220 * after ext4_ext_insert_extent() returns,
3222 err = ext4_ext_dirty(handle, inode, path + depth);
3224 goto fix_extent_len;
3227 ex2->ee_block = cpu_to_le32(split);
3228 ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
3229 ext4_ext_store_pblock(ex2, newblock);
3230 if (split_flag & EXT4_EXT_MARK_UNWRIT2)
3231 ext4_ext_mark_unwritten(ex2);
3233 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
3234 if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
3235 if (split_flag & (EXT4_EXT_DATA_VALID1|EXT4_EXT_DATA_VALID2)) {
3236 if (split_flag & EXT4_EXT_DATA_VALID1) {
3237 err = ext4_ext_zeroout(inode, ex2);
3238 zero_ex.ee_block = ex2->ee_block;
3239 zero_ex.ee_len = cpu_to_le16(
3240 ext4_ext_get_actual_len(ex2));
3241 ext4_ext_store_pblock(&zero_ex,
3242 ext4_ext_pblock(ex2));
3244 err = ext4_ext_zeroout(inode, ex);
3245 zero_ex.ee_block = ex->ee_block;
3246 zero_ex.ee_len = cpu_to_le16(
3247 ext4_ext_get_actual_len(ex));
3248 ext4_ext_store_pblock(&zero_ex,
3249 ext4_ext_pblock(ex));
3252 err = ext4_ext_zeroout(inode, &orig_ex);
3253 zero_ex.ee_block = orig_ex.ee_block;
3254 zero_ex.ee_len = cpu_to_le16(
3255 ext4_ext_get_actual_len(&orig_ex));
3256 ext4_ext_store_pblock(&zero_ex,
3257 ext4_ext_pblock(&orig_ex));
3261 goto fix_extent_len;
3262 /* update the extent length and mark as initialized */
3263 ex->ee_len = cpu_to_le16(ee_len);
3264 ext4_ext_try_to_merge(handle, inode, path, ex);
3265 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
3267 goto fix_extent_len;
3269 /* update extent status tree */
3270 err = ext4_zeroout_es(inode, &zero_ex);
3274 goto fix_extent_len;
3277 ext4_ext_show_leaf(inode, path);
3281 ex->ee_len = orig_ex.ee_len;
3282 ext4_ext_dirty(handle, inode, path + depth);
3287 * ext4_split_extents() splits an extent and mark extent which is covered
3288 * by @map as split_flags indicates
3290 * It may result in splitting the extent into multiple extents (up to three)
3291 * There are three possibilities:
3292 * a> There is no split required
3293 * b> Splits in two extents: Split is happening at either end of the extent
3294 * c> Splits in three extents: Somone is splitting in middle of the extent
3297 static int ext4_split_extent(handle_t *handle,
3298 struct inode *inode,
3299 struct ext4_ext_path *path,
3300 struct ext4_map_blocks *map,
3304 ext4_lblk_t ee_block;
3305 struct ext4_extent *ex;
3306 unsigned int ee_len, depth;
3309 int split_flag1, flags1;
3310 int allocated = map->m_len;
3312 depth = ext_depth(inode);
3313 ex = path[depth].p_ext;
3314 ee_block = le32_to_cpu(ex->ee_block);
3315 ee_len = ext4_ext_get_actual_len(ex);
3316 unwritten = ext4_ext_is_unwritten(ex);
3318 if (map->m_lblk + map->m_len < ee_block + ee_len) {
3319 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT;
3320 flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
3322 split_flag1 |= EXT4_EXT_MARK_UNWRIT1 |
3323 EXT4_EXT_MARK_UNWRIT2;
3324 if (split_flag & EXT4_EXT_DATA_VALID2)
3325 split_flag1 |= EXT4_EXT_DATA_VALID1;
3326 err = ext4_split_extent_at(handle, inode, path,
3327 map->m_lblk + map->m_len, split_flag1, flags1);
3331 allocated = ee_len - (map->m_lblk - ee_block);
3334 * Update path is required because previous ext4_split_extent_at() may
3335 * result in split of original leaf or extent zeroout.
3337 ext4_ext_drop_refs(path);
3338 path = ext4_ext_find_extent(inode, map->m_lblk, path, 0);
3340 return PTR_ERR(path);
3341 depth = ext_depth(inode);
3342 ex = path[depth].p_ext;
3344 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
3345 (unsigned long) map->m_lblk);
3348 unwritten = ext4_ext_is_unwritten(ex);
3351 if (map->m_lblk >= ee_block) {
3352 split_flag1 = split_flag & EXT4_EXT_DATA_VALID2;
3354 split_flag1 |= EXT4_EXT_MARK_UNWRIT1;
3355 split_flag1 |= split_flag & (EXT4_EXT_MAY_ZEROOUT |
3356 EXT4_EXT_MARK_UNWRIT2);
3358 err = ext4_split_extent_at(handle, inode, path,
3359 map->m_lblk, split_flag1, flags);
3364 ext4_ext_show_leaf(inode, path);
3366 return err ? err : allocated;
3370 * This function is called by ext4_ext_map_blocks() if someone tries to write
3371 * to an unwritten extent. It may result in splitting the unwritten
3372 * extent into multiple extents (up to three - one initialized and two
3374 * There are three possibilities:
3375 * a> There is no split required: Entire extent should be initialized
3376 * b> Splits in two extents: Write is happening at either end of the extent
3377 * c> Splits in three extents: Somone is writing in middle of the extent
3380 * - The extent pointed to by 'path' is unwritten.
3381 * - The extent pointed to by 'path' contains a superset
3382 * of the logical span [map->m_lblk, map->m_lblk + map->m_len).
3384 * Post-conditions on success:
3385 * - the returned value is the number of blocks beyond map->l_lblk
3386 * that are allocated and initialized.
3387 * It is guaranteed to be >= map->m_len.
3389 static int ext4_ext_convert_to_initialized(handle_t *handle,
3390 struct inode *inode,
3391 struct ext4_map_blocks *map,
3392 struct ext4_ext_path *path,
3395 struct ext4_sb_info *sbi;
3396 struct ext4_extent_header *eh;
3397 struct ext4_map_blocks split_map;
3398 struct ext4_extent zero_ex;
3399 struct ext4_extent *ex, *abut_ex;
3400 ext4_lblk_t ee_block, eof_block;
3401 unsigned int ee_len, depth, map_len = map->m_len;
3402 int allocated = 0, max_zeroout = 0;
3406 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
3407 "block %llu, max_blocks %u\n", inode->i_ino,
3408 (unsigned long long)map->m_lblk, map_len);
3410 sbi = EXT4_SB(inode->i_sb);
3411 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3412 inode->i_sb->s_blocksize_bits;
3413 if (eof_block < map->m_lblk + map_len)
3414 eof_block = map->m_lblk + map_len;
3416 depth = ext_depth(inode);
3417 eh = path[depth].p_hdr;
3418 ex = path[depth].p_ext;
3419 ee_block = le32_to_cpu(ex->ee_block);
3420 ee_len = ext4_ext_get_actual_len(ex);
3423 trace_ext4_ext_convert_to_initialized_enter(inode, map, ex);
3425 /* Pre-conditions */
3426 BUG_ON(!ext4_ext_is_unwritten(ex));
3427 BUG_ON(!in_range(map->m_lblk, ee_block, ee_len));
3430 * Attempt to transfer newly initialized blocks from the currently
3431 * unwritten extent to its neighbor. This is much cheaper
3432 * than an insertion followed by a merge as those involve costly
3433 * memmove() calls. Transferring to the left is the common case in
3434 * steady state for workloads doing fallocate(FALLOC_FL_KEEP_SIZE)
3435 * followed by append writes.
3437 * Limitations of the current logic:
3438 * - L1: we do not deal with writes covering the whole extent.
3439 * This would require removing the extent if the transfer
3441 * - L2: we only attempt to merge with an extent stored in the
3442 * same extent tree node.
3444 if ((map->m_lblk == ee_block) &&
3445 /* See if we can merge left */
3446 (map_len < ee_len) && /*L1*/
3447 (ex > EXT_FIRST_EXTENT(eh))) { /*L2*/
3448 ext4_lblk_t prev_lblk;
3449 ext4_fsblk_t prev_pblk, ee_pblk;
3450 unsigned int prev_len;
3453 prev_lblk = le32_to_cpu(abut_ex->ee_block);
3454 prev_len = ext4_ext_get_actual_len(abut_ex);
3455 prev_pblk = ext4_ext_pblock(abut_ex);
3456 ee_pblk = ext4_ext_pblock(ex);
3459 * A transfer of blocks from 'ex' to 'abut_ex' is allowed
3460 * upon those conditions:
3461 * - C1: abut_ex is initialized,
3462 * - C2: abut_ex is logically abutting ex,
3463 * - C3: abut_ex is physically abutting ex,
3464 * - C4: abut_ex can receive the additional blocks without
3465 * overflowing the (initialized) length limit.
3467 if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/
3468 ((prev_lblk + prev_len) == ee_block) && /*C2*/
3469 ((prev_pblk + prev_len) == ee_pblk) && /*C3*/
3470 (prev_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/
3471 err = ext4_ext_get_access(handle, inode, path + depth);
3475 trace_ext4_ext_convert_to_initialized_fastpath(inode,
3478 /* Shift the start of ex by 'map_len' blocks */
3479 ex->ee_block = cpu_to_le32(ee_block + map_len);
3480 ext4_ext_store_pblock(ex, ee_pblk + map_len);
3481 ex->ee_len = cpu_to_le16(ee_len - map_len);
3482 ext4_ext_mark_unwritten(ex); /* Restore the flag */
3484 /* Extend abut_ex by 'map_len' blocks */
3485 abut_ex->ee_len = cpu_to_le16(prev_len + map_len);
3487 /* Result: number of initialized blocks past m_lblk */
3488 allocated = map_len;
3490 } else if (((map->m_lblk + map_len) == (ee_block + ee_len)) &&
3491 (map_len < ee_len) && /*L1*/
3492 ex < EXT_LAST_EXTENT(eh)) { /*L2*/
3493 /* See if we can merge right */
3494 ext4_lblk_t next_lblk;
3495 ext4_fsblk_t next_pblk, ee_pblk;
3496 unsigned int next_len;
3499 next_lblk = le32_to_cpu(abut_ex->ee_block);
3500 next_len = ext4_ext_get_actual_len(abut_ex);
3501 next_pblk = ext4_ext_pblock(abut_ex);
3502 ee_pblk = ext4_ext_pblock(ex);
3505 * A transfer of blocks from 'ex' to 'abut_ex' is allowed
3506 * upon those conditions:
3507 * - C1: abut_ex is initialized,
3508 * - C2: abut_ex is logically abutting ex,
3509 * - C3: abut_ex is physically abutting ex,
3510 * - C4: abut_ex can receive the additional blocks without
3511 * overflowing the (initialized) length limit.
3513 if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/
3514 ((map->m_lblk + map_len) == next_lblk) && /*C2*/
3515 ((ee_pblk + ee_len) == next_pblk) && /*C3*/
3516 (next_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/
3517 err = ext4_ext_get_access(handle, inode, path + depth);
3521 trace_ext4_ext_convert_to_initialized_fastpath(inode,
3524 /* Shift the start of abut_ex by 'map_len' blocks */
3525 abut_ex->ee_block = cpu_to_le32(next_lblk - map_len);
3526 ext4_ext_store_pblock(abut_ex, next_pblk - map_len);
3527 ex->ee_len = cpu_to_le16(ee_len - map_len);
3528 ext4_ext_mark_unwritten(ex); /* Restore the flag */
3530 /* Extend abut_ex by 'map_len' blocks */
3531 abut_ex->ee_len = cpu_to_le16(next_len + map_len);
3533 /* Result: number of initialized blocks past m_lblk */
3534 allocated = map_len;
3538 /* Mark the block containing both extents as dirty */
3539 ext4_ext_dirty(handle, inode, path + depth);
3541 /* Update path to point to the right extent */
3542 path[depth].p_ext = abut_ex;
3545 allocated = ee_len - (map->m_lblk - ee_block);
3547 WARN_ON(map->m_lblk < ee_block);
3549 * It is safe to convert extent to initialized via explicit
3550 * zeroout only if extent is fully inside i_size or new_size.
3552 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
3554 if (EXT4_EXT_MAY_ZEROOUT & split_flag)
3555 max_zeroout = sbi->s_extent_max_zeroout_kb >>
3556 (inode->i_sb->s_blocksize_bits - 10);
3558 /* If extent is less than s_max_zeroout_kb, zeroout directly */
3559 if (max_zeroout && (ee_len <= max_zeroout)) {
3560 err = ext4_ext_zeroout(inode, ex);
3563 zero_ex.ee_block = ex->ee_block;
3564 zero_ex.ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex));
3565 ext4_ext_store_pblock(&zero_ex, ext4_ext_pblock(ex));
3567 err = ext4_ext_get_access(handle, inode, path + depth);
3570 ext4_ext_mark_initialized(ex);
3571 ext4_ext_try_to_merge(handle, inode, path, ex);
3572 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
3578 * 1. split the extent into three extents.
3579 * 2. split the extent into two extents, zeroout the first half.
3580 * 3. split the extent into two extents, zeroout the second half.
3581 * 4. split the extent into two extents with out zeroout.
3583 split_map.m_lblk = map->m_lblk;
3584 split_map.m_len = map->m_len;
3586 if (max_zeroout && (allocated > map->m_len)) {
3587 if (allocated <= max_zeroout) {
3590 cpu_to_le32(map->m_lblk);
3591 zero_ex.ee_len = cpu_to_le16(allocated);
3592 ext4_ext_store_pblock(&zero_ex,
3593 ext4_ext_pblock(ex) + map->m_lblk - ee_block);
3594 err = ext4_ext_zeroout(inode, &zero_ex);
3597 split_map.m_lblk = map->m_lblk;
3598 split_map.m_len = allocated;
3599 } else if (map->m_lblk - ee_block + map->m_len < max_zeroout) {
3601 if (map->m_lblk != ee_block) {
3602 zero_ex.ee_block = ex->ee_block;
3603 zero_ex.ee_len = cpu_to_le16(map->m_lblk -
3605 ext4_ext_store_pblock(&zero_ex,
3606 ext4_ext_pblock(ex));
3607 err = ext4_ext_zeroout(inode, &zero_ex);
3612 split_map.m_lblk = ee_block;
3613 split_map.m_len = map->m_lblk - ee_block + map->m_len;
3614 allocated = map->m_len;
3618 allocated = ext4_split_extent(handle, inode, path,
3619 &split_map, split_flag, flags);
3624 /* If we have gotten a failure, don't zero out status tree */
3626 err = ext4_zeroout_es(inode, &zero_ex);
3627 return err ? err : allocated;
3631 * This function is called by ext4_ext_map_blocks() from
3632 * ext4_get_blocks_dio_write() when DIO to write
3633 * to an unwritten extent.
3635 * Writing to an unwritten extent may result in splitting the unwritten
3636 * extent into multiple initialized/unwritten extents (up to three)
3637 * There are three possibilities:
3638 * a> There is no split required: Entire extent should be unwritten
3639 * b> Splits in two extents: Write is happening at either end of the extent
3640 * c> Splits in three extents: Somone is writing in middle of the extent
3642 * This works the same way in the case of initialized -> unwritten conversion.
3644 * One of more index blocks maybe needed if the extent tree grow after
3645 * the unwritten extent split. To prevent ENOSPC occur at the IO
3646 * complete, we need to split the unwritten extent before DIO submit
3647 * the IO. The unwritten extent called at this time will be split
3648 * into three unwritten extent(at most). After IO complete, the part
3649 * being filled will be convert to initialized by the end_io callback function
3650 * via ext4_convert_unwritten_extents().
3652 * Returns the size of unwritten extent to be written on success.
3654 static int ext4_split_convert_extents(handle_t *handle,
3655 struct inode *inode,
3656 struct ext4_map_blocks *map,
3657 struct ext4_ext_path *path,
3660 ext4_lblk_t eof_block;
3661 ext4_lblk_t ee_block;
3662 struct ext4_extent *ex;
3663 unsigned int ee_len;
3664 int split_flag = 0, depth;
3666 ext_debug("%s: inode %lu, logical block %llu, max_blocks %u\n",
3667 __func__, inode->i_ino,
3668 (unsigned long long)map->m_lblk, map->m_len);
3670 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3671 inode->i_sb->s_blocksize_bits;
3672 if (eof_block < map->m_lblk + map->m_len)
3673 eof_block = map->m_lblk + map->m_len;
3675 * It is safe to convert extent to initialized via explicit
3676 * zeroout only if extent is fully insde i_size or new_size.
3678 depth = ext_depth(inode);
3679 ex = path[depth].p_ext;
3680 ee_block = le32_to_cpu(ex->ee_block);
3681 ee_len = ext4_ext_get_actual_len(ex);
3683 /* Convert to unwritten */
3684 if (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) {
3685 split_flag |= EXT4_EXT_DATA_VALID1;
3686 /* Convert to initialized */
3687 } else if (flags & EXT4_GET_BLOCKS_CONVERT) {
3688 split_flag |= ee_block + ee_len <= eof_block ?
3689 EXT4_EXT_MAY_ZEROOUT : 0;
3690 split_flag |= (EXT4_EXT_MARK_UNWRIT2 | EXT4_EXT_DATA_VALID2);
3692 flags |= EXT4_GET_BLOCKS_PRE_IO;
3693 return ext4_split_extent(handle, inode, path, map, split_flag, flags);
3696 static int ext4_convert_initialized_extents(handle_t *handle,
3697 struct inode *inode,
3698 struct ext4_map_blocks *map,
3699 struct ext4_ext_path *path)
3701 struct ext4_extent *ex;
3702 ext4_lblk_t ee_block;
3703 unsigned int ee_len;
3707 depth = ext_depth(inode);
3708 ex = path[depth].p_ext;
3709 ee_block = le32_to_cpu(ex->ee_block);
3710 ee_len = ext4_ext_get_actual_len(ex);
3712 ext_debug("%s: inode %lu, logical"
3713 "block %llu, max_blocks %u\n", __func__, inode->i_ino,
3714 (unsigned long long)ee_block, ee_len);
3716 if (ee_block != map->m_lblk || ee_len > map->m_len) {
3717 err = ext4_split_convert_extents(handle, inode, map, path,
3718 EXT4_GET_BLOCKS_CONVERT_UNWRITTEN);
3721 ext4_ext_drop_refs(path);
3722 path = ext4_ext_find_extent(inode, map->m_lblk, path, 0);
3724 err = PTR_ERR(path);
3727 depth = ext_depth(inode);
3728 ex = path[depth].p_ext;
3730 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
3731 (unsigned long) map->m_lblk);
3737 err = ext4_ext_get_access(handle, inode, path + depth);
3740 /* first mark the extent as unwritten */
3741 ext4_ext_mark_unwritten(ex);
3743 /* note: ext4_ext_correct_indexes() isn't needed here because
3744 * borders are not changed
3746 ext4_ext_try_to_merge(handle, inode, path, ex);
3748 /* Mark modified extent as dirty */
3749 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
3751 ext4_ext_show_leaf(inode, path);
3756 static int ext4_convert_unwritten_extents_endio(handle_t *handle,
3757 struct inode *inode,
3758 struct ext4_map_blocks *map,
3759 struct ext4_ext_path *path)
3761 struct ext4_extent *ex;
3762 ext4_lblk_t ee_block;
3763 unsigned int ee_len;
3767 depth = ext_depth(inode);
3768 ex = path[depth].p_ext;
3769 ee_block = le32_to_cpu(ex->ee_block);
3770 ee_len = ext4_ext_get_actual_len(ex);
3772 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
3773 "block %llu, max_blocks %u\n", inode->i_ino,
3774 (unsigned long long)ee_block, ee_len);
3776 /* If extent is larger than requested it is a clear sign that we still
3777 * have some extent state machine issues left. So extent_split is still
3779 * TODO: Once all related issues will be fixed this situation should be
3782 if (ee_block != map->m_lblk || ee_len > map->m_len) {
3783 #ifdef CONFIG_EXT4_DEBUG
3784 ext4_warning(inode->i_sb, "Inode (%ld) finished: extent logical block %llu,"
3785 " len %u; IO logical block %llu, len %u\n",
3786 inode->i_ino, (unsigned long long)ee_block, ee_len,
3787 (unsigned long long)map->m_lblk, map->m_len);
3789 err = ext4_split_convert_extents(handle, inode, map, path,
3790 EXT4_GET_BLOCKS_CONVERT);
3793 ext4_ext_drop_refs(path);
3794 path = ext4_ext_find_extent(inode, map->m_lblk, path, 0);
3796 err = PTR_ERR(path);
3799 depth = ext_depth(inode);
3800 ex = path[depth].p_ext;
3803 err = ext4_ext_get_access(handle, inode, path + depth);
3806 /* first mark the extent as initialized */
3807 ext4_ext_mark_initialized(ex);
3809 /* note: ext4_ext_correct_indexes() isn't needed here because
3810 * borders are not changed
3812 ext4_ext_try_to_merge(handle, inode, path, ex);
3814 /* Mark modified extent as dirty */
3815 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
3817 ext4_ext_show_leaf(inode, path);
3821 static void unmap_underlying_metadata_blocks(struct block_device *bdev,
3822 sector_t block, int count)
3825 for (i = 0; i < count; i++)
3826 unmap_underlying_metadata(bdev, block + i);
3830 * Handle EOFBLOCKS_FL flag, clearing it if necessary
3832 static int check_eofblocks_fl(handle_t *handle, struct inode *inode,
3834 struct ext4_ext_path *path,
3838 struct ext4_extent_header *eh;
3839 struct ext4_extent *last_ex;
3841 if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
3844 depth = ext_depth(inode);
3845 eh = path[depth].p_hdr;
3848 * We're going to remove EOFBLOCKS_FL entirely in future so we
3849 * do not care for this case anymore. Simply remove the flag
3850 * if there are no extents.
3852 if (unlikely(!eh->eh_entries))
3854 last_ex = EXT_LAST_EXTENT(eh);
3856 * We should clear the EOFBLOCKS_FL flag if we are writing the
3857 * last block in the last extent in the file. We test this by
3858 * first checking to see if the caller to
3859 * ext4_ext_get_blocks() was interested in the last block (or
3860 * a block beyond the last block) in the current extent. If
3861 * this turns out to be false, we can bail out from this
3862 * function immediately.
3864 if (lblk + len < le32_to_cpu(last_ex->ee_block) +
3865 ext4_ext_get_actual_len(last_ex))
3868 * If the caller does appear to be planning to write at or
3869 * beyond the end of the current extent, we then test to see
3870 * if the current extent is the last extent in the file, by
3871 * checking to make sure it was reached via the rightmost node
3872 * at each level of the tree.
3874 for (i = depth-1; i >= 0; i--)
3875 if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr))
3878 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
3879 return ext4_mark_inode_dirty(handle, inode);
3883 * ext4_find_delalloc_range: find delayed allocated block in the given range.
3885 * Return 1 if there is a delalloc block in the range, otherwise 0.
3887 int ext4_find_delalloc_range(struct inode *inode,
3888 ext4_lblk_t lblk_start,
3889 ext4_lblk_t lblk_end)
3891 struct extent_status es;
3893 ext4_es_find_delayed_extent_range(inode, lblk_start, lblk_end, &es);
3895 return 0; /* there is no delay extent in this tree */
3896 else if (es.es_lblk <= lblk_start &&
3897 lblk_start < es.es_lblk + es.es_len)
3899 else if (lblk_start <= es.es_lblk && es.es_lblk <= lblk_end)
3905 int ext4_find_delalloc_cluster(struct inode *inode, ext4_lblk_t lblk)
3907 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3908 ext4_lblk_t lblk_start, lblk_end;
3909 lblk_start = EXT4_LBLK_CMASK(sbi, lblk);
3910 lblk_end = lblk_start + sbi->s_cluster_ratio - 1;
3912 return ext4_find_delalloc_range(inode, lblk_start, lblk_end);
3916 * Determines how many complete clusters (out of those specified by the 'map')
3917 * are under delalloc and were reserved quota for.
3918 * This function is called when we are writing out the blocks that were
3919 * originally written with their allocation delayed, but then the space was
3920 * allocated using fallocate() before the delayed allocation could be resolved.
3921 * The cases to look for are:
3922 * ('=' indicated delayed allocated blocks
3923 * '-' indicates non-delayed allocated blocks)
3924 * (a) partial clusters towards beginning and/or end outside of allocated range
3925 * are not delalloc'ed.
3927 * |----c---=|====c====|====c====|===-c----|
3928 * |++++++ allocated ++++++|
3929 * ==> 4 complete clusters in above example
3931 * (b) partial cluster (outside of allocated range) towards either end is
3932 * marked for delayed allocation. In this case, we will exclude that
3935 * |----====c========|========c========|
3936 * |++++++ allocated ++++++|
3937 * ==> 1 complete clusters in above example
3940 * |================c================|
3941 * |++++++ allocated ++++++|
3942 * ==> 0 complete clusters in above example
3944 * The ext4_da_update_reserve_space will be called only if we
3945 * determine here that there were some "entire" clusters that span
3946 * this 'allocated' range.
3947 * In the non-bigalloc case, this function will just end up returning num_blks
3948 * without ever calling ext4_find_delalloc_range.
3951 get_reserved_cluster_alloc(struct inode *inode, ext4_lblk_t lblk_start,
3952 unsigned int num_blks)
3954 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3955 ext4_lblk_t alloc_cluster_start, alloc_cluster_end;
3956 ext4_lblk_t lblk_from, lblk_to, c_offset;
3957 unsigned int allocated_clusters = 0;
3959 alloc_cluster_start = EXT4_B2C(sbi, lblk_start);
3960 alloc_cluster_end = EXT4_B2C(sbi, lblk_start + num_blks - 1);
3962 /* max possible clusters for this allocation */
3963 allocated_clusters = alloc_cluster_end - alloc_cluster_start + 1;
3965 trace_ext4_get_reserved_cluster_alloc(inode, lblk_start, num_blks);
3967 /* Check towards left side */
3968 c_offset = EXT4_LBLK_COFF(sbi, lblk_start);
3970 lblk_from = EXT4_LBLK_CMASK(sbi, lblk_start);
3971 lblk_to = lblk_from + c_offset - 1;
3973 if (ext4_find_delalloc_range(inode, lblk_from, lblk_to))
3974 allocated_clusters--;
3977 /* Now check towards right. */
3978 c_offset = EXT4_LBLK_COFF(sbi, lblk_start + num_blks);
3979 if (allocated_clusters && c_offset) {
3980 lblk_from = lblk_start + num_blks;
3981 lblk_to = lblk_from + (sbi->s_cluster_ratio - c_offset) - 1;
3983 if (ext4_find_delalloc_range(inode, lblk_from, lblk_to))
3984 allocated_clusters--;
3987 return allocated_clusters;
3991 ext4_ext_convert_initialized_extent(handle_t *handle, struct inode *inode,
3992 struct ext4_map_blocks *map,
3993 struct ext4_ext_path *path, int flags,
3994 unsigned int allocated, ext4_fsblk_t newblock)
4000 * Make sure that the extent is no bigger than we support with
4003 if (map->m_len > EXT_UNWRITTEN_MAX_LEN)
4004 map->m_len = EXT_UNWRITTEN_MAX_LEN / 2;
4006 ret = ext4_convert_initialized_extents(handle, inode, map,
4009 ext4_update_inode_fsync_trans(handle, inode, 1);
4010 err = check_eofblocks_fl(handle, inode, map->m_lblk,
4014 map->m_flags |= EXT4_MAP_UNWRITTEN;
4015 if (allocated > map->m_len)
4016 allocated = map->m_len;
4017 map->m_len = allocated;
4019 return err ? err : allocated;
4023 ext4_ext_handle_unwritten_extents(handle_t *handle, struct inode *inode,
4024 struct ext4_map_blocks *map,
4025 struct ext4_ext_path *path, int flags,
4026 unsigned int allocated, ext4_fsblk_t newblock)
4030 ext4_io_end_t *io = ext4_inode_aio(inode);
4032 ext_debug("ext4_ext_handle_unwritten_extents: inode %lu, logical "
4033 "block %llu, max_blocks %u, flags %x, allocated %u\n",
4034 inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
4036 ext4_ext_show_leaf(inode, path);
4039 * When writing into unwritten space, we should not fail to
4040 * allocate metadata blocks for the new extent block if needed.
4042 flags |= EXT4_GET_BLOCKS_METADATA_NOFAIL;
4044 trace_ext4_ext_handle_unwritten_extents(inode, map, flags,
4045 allocated, newblock);
4047 /* get_block() before submit the IO, split the extent */
4048 if (flags & EXT4_GET_BLOCKS_PRE_IO) {
4049 ret = ext4_split_convert_extents(handle, inode, map,
4050 path, flags | EXT4_GET_BLOCKS_CONVERT);
4054 * Flag the inode(non aio case) or end_io struct (aio case)
4055 * that this IO needs to conversion to written when IO is
4059 ext4_set_io_unwritten_flag(inode, io);
4061 ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
4062 map->m_flags |= EXT4_MAP_UNWRITTEN;
4065 /* IO end_io complete, convert the filled extent to written */
4066 if (flags & EXT4_GET_BLOCKS_CONVERT) {
4067 ret = ext4_convert_unwritten_extents_endio(handle, inode, map,
4070 ext4_update_inode_fsync_trans(handle, inode, 1);
4071 err = check_eofblocks_fl(handle, inode, map->m_lblk,
4075 map->m_flags |= EXT4_MAP_MAPPED;
4076 map->m_pblk = newblock;
4077 if (allocated > map->m_len)
4078 allocated = map->m_len;
4079 map->m_len = allocated;
4082 /* buffered IO case */
4084 * repeat fallocate creation request
4085 * we already have an unwritten extent
4087 if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) {
4088 map->m_flags |= EXT4_MAP_UNWRITTEN;
4092 /* buffered READ or buffered write_begin() lookup */
4093 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
4095 * We have blocks reserved already. We
4096 * return allocated blocks so that delalloc
4097 * won't do block reservation for us. But
4098 * the buffer head will be unmapped so that
4099 * a read from the block returns 0s.
4101 map->m_flags |= EXT4_MAP_UNWRITTEN;
4105 /* buffered write, writepage time, convert*/
4106 ret = ext4_ext_convert_to_initialized(handle, inode, map, path, flags);
4108 ext4_update_inode_fsync_trans(handle, inode, 1);
4115 map->m_flags |= EXT4_MAP_NEW;
4117 * if we allocated more blocks than requested
4118 * we need to make sure we unmap the extra block
4119 * allocated. The actual needed block will get
4120 * unmapped later when we find the buffer_head marked
4123 if (allocated > map->m_len) {
4124 unmap_underlying_metadata_blocks(inode->i_sb->s_bdev,
4125 newblock + map->m_len,
4126 allocated - map->m_len);
4127 allocated = map->m_len;
4129 map->m_len = allocated;
4132 * If we have done fallocate with the offset that is already
4133 * delayed allocated, we would have block reservation
4134 * and quota reservation done in the delayed write path.
4135 * But fallocate would have already updated quota and block
4136 * count for this offset. So cancel these reservation
4138 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
4139 unsigned int reserved_clusters;
4140 reserved_clusters = get_reserved_cluster_alloc(inode,
4141 map->m_lblk, map->m_len);
4142 if (reserved_clusters)
4143 ext4_da_update_reserve_space(inode,
4149 map->m_flags |= EXT4_MAP_MAPPED;
4150 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0) {
4151 err = check_eofblocks_fl(handle, inode, map->m_lblk, path,
4157 if (allocated > map->m_len)
4158 allocated = map->m_len;
4159 ext4_ext_show_leaf(inode, path);
4160 map->m_pblk = newblock;
4161 map->m_len = allocated;
4163 return err ? err : allocated;
4167 * get_implied_cluster_alloc - check to see if the requested
4168 * allocation (in the map structure) overlaps with a cluster already
4169 * allocated in an extent.
4170 * @sb The filesystem superblock structure
4171 * @map The requested lblk->pblk mapping
4172 * @ex The extent structure which might contain an implied
4173 * cluster allocation
4175 * This function is called by ext4_ext_map_blocks() after we failed to
4176 * find blocks that were already in the inode's extent tree. Hence,
4177 * we know that the beginning of the requested region cannot overlap
4178 * the extent from the inode's extent tree. There are three cases we
4179 * want to catch. The first is this case:
4181 * |--- cluster # N--|
4182 * |--- extent ---| |---- requested region ---|
4185 * The second case that we need to test for is this one:
4187 * |--------- cluster # N ----------------|
4188 * |--- requested region --| |------- extent ----|
4189 * |=======================|
4191 * The third case is when the requested region lies between two extents
4192 * within the same cluster:
4193 * |------------- cluster # N-------------|
4194 * |----- ex -----| |---- ex_right ----|
4195 * |------ requested region ------|
4196 * |================|
4198 * In each of the above cases, we need to set the map->m_pblk and
4199 * map->m_len so it corresponds to the return the extent labelled as
4200 * "|====|" from cluster #N, since it is already in use for data in
4201 * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to
4202 * signal to ext4_ext_map_blocks() that map->m_pblk should be treated
4203 * as a new "allocated" block region. Otherwise, we will return 0 and
4204 * ext4_ext_map_blocks() will then allocate one or more new clusters
4205 * by calling ext4_mb_new_blocks().
4207 static int get_implied_cluster_alloc(struct super_block *sb,
4208 struct ext4_map_blocks *map,
4209 struct ext4_extent *ex,
4210 struct ext4_ext_path *path)
4212 struct ext4_sb_info *sbi = EXT4_SB(sb);
4213 ext4_lblk_t c_offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
4214 ext4_lblk_t ex_cluster_start, ex_cluster_end;
4215 ext4_lblk_t rr_cluster_start;
4216 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
4217 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
4218 unsigned short ee_len = ext4_ext_get_actual_len(ex);
4220 /* The extent passed in that we are trying to match */
4221 ex_cluster_start = EXT4_B2C(sbi, ee_block);
4222 ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1);
4224 /* The requested region passed into ext4_map_blocks() */
4225 rr_cluster_start = EXT4_B2C(sbi, map->m_lblk);
4227 if ((rr_cluster_start == ex_cluster_end) ||
4228 (rr_cluster_start == ex_cluster_start)) {
4229 if (rr_cluster_start == ex_cluster_end)
4230 ee_start += ee_len - 1;
4231 map->m_pblk = EXT4_PBLK_CMASK(sbi, ee_start) + c_offset;
4232 map->m_len = min(map->m_len,
4233 (unsigned) sbi->s_cluster_ratio - c_offset);
4235 * Check for and handle this case:
4237 * |--------- cluster # N-------------|
4238 * |------- extent ----|
4239 * |--- requested region ---|
4243 if (map->m_lblk < ee_block)
4244 map->m_len = min(map->m_len, ee_block - map->m_lblk);
4247 * Check for the case where there is already another allocated
4248 * block to the right of 'ex' but before the end of the cluster.
4250 * |------------- cluster # N-------------|
4251 * |----- ex -----| |---- ex_right ----|
4252 * |------ requested region ------|
4253 * |================|
4255 if (map->m_lblk > ee_block) {
4256 ext4_lblk_t next = ext4_ext_next_allocated_block(path);
4257 map->m_len = min(map->m_len, next - map->m_lblk);
4260 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1);
4264 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0);
4270 * Block allocation/map/preallocation routine for extents based files
4273 * Need to be called with
4274 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
4275 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
4277 * return > 0, number of of blocks already mapped/allocated
4278 * if create == 0 and these are pre-allocated blocks
4279 * buffer head is unmapped
4280 * otherwise blocks are mapped
4282 * return = 0, if plain look up failed (blocks have not been allocated)
4283 * buffer head is unmapped
4285 * return < 0, error case.
4287 int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
4288 struct ext4_map_blocks *map, int flags)
4290 struct ext4_ext_path *path = NULL;
4291 struct ext4_extent newex, *ex, *ex2;
4292 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4293 ext4_fsblk_t newblock = 0;
4294 int free_on_err = 0, err = 0, depth, ret;
4295 unsigned int allocated = 0, offset = 0;
4296 unsigned int allocated_clusters = 0;
4297 struct ext4_allocation_request ar;
4298 ext4_io_end_t *io = ext4_inode_aio(inode);
4299 ext4_lblk_t cluster_offset;
4300 int set_unwritten = 0;
4302 ext_debug("blocks %u/%u requested for inode %lu\n",
4303 map->m_lblk, map->m_len, inode->i_ino);
4304 trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
4306 /* find extent for this block */
4307 path = ext4_ext_find_extent(inode, map->m_lblk, NULL, 0);
4309 err = PTR_ERR(path);
4314 depth = ext_depth(inode);
4317 * consistent leaf must not be empty;
4318 * this situation is possible, though, _during_ tree modification;
4319 * this is why assert can't be put in ext4_ext_find_extent()
4321 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
4322 EXT4_ERROR_INODE(inode, "bad extent address "
4323 "lblock: %lu, depth: %d pblock %lld",
4324 (unsigned long) map->m_lblk, depth,
4325 path[depth].p_block);
4330 ex = path[depth].p_ext;
4332 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
4333 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
4334 unsigned short ee_len;
4338 * unwritten extents are treated as holes, except that
4339 * we split out initialized portions during a write.
4341 ee_len = ext4_ext_get_actual_len(ex);
4343 trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len);
4345 /* if found extent covers block, simply return it */
4346 if (in_range(map->m_lblk, ee_block, ee_len)) {
4347 newblock = map->m_lblk - ee_block + ee_start;
4348 /* number of remaining blocks in the extent */
4349 allocated = ee_len - (map->m_lblk - ee_block);
4350 ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
4351 ee_block, ee_len, newblock);
4354 * If the extent is initialized check whether the
4355 * caller wants to convert it to unwritten.
4357 if ((!ext4_ext_is_unwritten(ex)) &&
4358 (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) {
4359 allocated = ext4_ext_convert_initialized_extent(
4360 handle, inode, map, path, flags,
4361 allocated, newblock);
4363 } else if (!ext4_ext_is_unwritten(ex))
4366 ret = ext4_ext_handle_unwritten_extents(
4367 handle, inode, map, path, flags,
4368 allocated, newblock);
4377 if ((sbi->s_cluster_ratio > 1) &&
4378 ext4_find_delalloc_cluster(inode, map->m_lblk))
4379 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
4382 * requested block isn't allocated yet;
4383 * we couldn't try to create block if create flag is zero
4385 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
4387 * put just found gap into cache to speed up
4388 * subsequent requests
4390 if ((flags & EXT4_GET_BLOCKS_NO_PUT_HOLE) == 0)
4391 ext4_ext_put_gap_in_cache(inode, path, map->m_lblk);
4396 * Okay, we need to do block allocation.
4398 map->m_flags &= ~EXT4_MAP_FROM_CLUSTER;
4399 newex.ee_block = cpu_to_le32(map->m_lblk);
4400 cluster_offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
4403 * If we are doing bigalloc, check to see if the extent returned
4404 * by ext4_ext_find_extent() implies a cluster we can use.
4406 if (cluster_offset && ex &&
4407 get_implied_cluster_alloc(inode->i_sb, map, ex, path)) {
4408 ar.len = allocated = map->m_len;
4409 newblock = map->m_pblk;
4410 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
4411 goto got_allocated_blocks;
4414 /* find neighbour allocated blocks */
4415 ar.lleft = map->m_lblk;
4416 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
4419 ar.lright = map->m_lblk;
4421 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2);
4425 /* Check if the extent after searching to the right implies a
4426 * cluster we can use. */
4427 if ((sbi->s_cluster_ratio > 1) && ex2 &&
4428 get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) {
4429 ar.len = allocated = map->m_len;
4430 newblock = map->m_pblk;
4431 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
4432 goto got_allocated_blocks;
4436 * See if request is beyond maximum number of blocks we can have in
4437 * a single extent. For an initialized extent this limit is
4438 * EXT_INIT_MAX_LEN and for an unwritten extent this limit is
4439 * EXT_UNWRITTEN_MAX_LEN.
4441 if (map->m_len > EXT_INIT_MAX_LEN &&
4442 !(flags & EXT4_GET_BLOCKS_UNWRIT_EXT))
4443 map->m_len = EXT_INIT_MAX_LEN;
4444 else if (map->m_len > EXT_UNWRITTEN_MAX_LEN &&
4445 (flags & EXT4_GET_BLOCKS_UNWRIT_EXT))
4446 map->m_len = EXT_UNWRITTEN_MAX_LEN;
4448 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
4449 newex.ee_len = cpu_to_le16(map->m_len);
4450 err = ext4_ext_check_overlap(sbi, inode, &newex, path);
4452 allocated = ext4_ext_get_actual_len(&newex);
4454 allocated = map->m_len;
4456 /* allocate new block */
4458 ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
4459 ar.logical = map->m_lblk;
4461 * We calculate the offset from the beginning of the cluster
4462 * for the logical block number, since when we allocate a
4463 * physical cluster, the physical block should start at the
4464 * same offset from the beginning of the cluster. This is
4465 * needed so that future calls to get_implied_cluster_alloc()
4468 offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
4469 ar.len = EXT4_NUM_B2C(sbi, offset+allocated);
4471 ar.logical -= offset;
4472 if (S_ISREG(inode->i_mode))
4473 ar.flags = EXT4_MB_HINT_DATA;
4475 /* disable in-core preallocation for non-regular files */
4477 if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE)
4478 ar.flags |= EXT4_MB_HINT_NOPREALLOC;
4479 newblock = ext4_mb_new_blocks(handle, &ar, &err);
4482 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
4483 ar.goal, newblock, allocated);
4485 allocated_clusters = ar.len;
4486 ar.len = EXT4_C2B(sbi, ar.len) - offset;
4487 if (ar.len > allocated)
4490 got_allocated_blocks:
4491 /* try to insert new extent into found leaf and return */
4492 ext4_ext_store_pblock(&newex, newblock + offset);
4493 newex.ee_len = cpu_to_le16(ar.len);
4494 /* Mark unwritten */
4495 if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT){
4496 ext4_ext_mark_unwritten(&newex);
4497 map->m_flags |= EXT4_MAP_UNWRITTEN;
4499 * io_end structure was created for every IO write to an
4500 * unwritten extent. To avoid unnecessary conversion,
4501 * here we flag the IO that really needs the conversion.
4502 * For non asycn direct IO case, flag the inode state
4503 * that we need to perform conversion when IO is done.
4505 if (flags & EXT4_GET_BLOCKS_PRE_IO)
4510 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0)
4511 err = check_eofblocks_fl(handle, inode, map->m_lblk,
4514 err = ext4_ext_insert_extent(handle, inode, path,
4517 if (!err && set_unwritten) {
4519 ext4_set_io_unwritten_flag(inode, io);
4521 ext4_set_inode_state(inode,
4522 EXT4_STATE_DIO_UNWRITTEN);
4525 if (err && free_on_err) {
4526 int fb_flags = flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE ?
4527 EXT4_FREE_BLOCKS_NO_QUOT_UPDATE : 0;
4528 /* free data blocks we just allocated */
4529 /* not a good idea to call discard here directly,
4530 * but otherwise we'd need to call it every free() */
4531 ext4_discard_preallocations(inode);
4532 ext4_free_blocks(handle, inode, NULL, newblock,
4533 EXT4_C2B(sbi, allocated_clusters), fb_flags);
4537 /* previous routine could use block we allocated */
4538 newblock = ext4_ext_pblock(&newex);
4539 allocated = ext4_ext_get_actual_len(&newex);
4540 if (allocated > map->m_len)
4541 allocated = map->m_len;
4542 map->m_flags |= EXT4_MAP_NEW;
4545 * Update reserved blocks/metadata blocks after successful
4546 * block allocation which had been deferred till now.
4548 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
4549 unsigned int reserved_clusters;
4551 * Check how many clusters we had reserved this allocated range
4553 reserved_clusters = get_reserved_cluster_alloc(inode,
4554 map->m_lblk, allocated);
4555 if (map->m_flags & EXT4_MAP_FROM_CLUSTER) {
4556 if (reserved_clusters) {
4558 * We have clusters reserved for this range.
4559 * But since we are not doing actual allocation
4560 * and are simply using blocks from previously
4561 * allocated cluster, we should release the
4562 * reservation and not claim quota.
4564 ext4_da_update_reserve_space(inode,
4565 reserved_clusters, 0);
4568 BUG_ON(allocated_clusters < reserved_clusters);
4569 if (reserved_clusters < allocated_clusters) {
4570 struct ext4_inode_info *ei = EXT4_I(inode);
4571 int reservation = allocated_clusters -
4574 * It seems we claimed few clusters outside of
4575 * the range of this allocation. We should give
4576 * it back to the reservation pool. This can
4577 * happen in the following case:
4579 * * Suppose s_cluster_ratio is 4 (i.e., each
4580 * cluster has 4 blocks. Thus, the clusters
4581 * are [0-3],[4-7],[8-11]...
4582 * * First comes delayed allocation write for
4583 * logical blocks 10 & 11. Since there were no
4584 * previous delayed allocated blocks in the
4585 * range [8-11], we would reserve 1 cluster
4587 * * Next comes write for logical blocks 3 to 8.
4588 * In this case, we will reserve 2 clusters
4589 * (for [0-3] and [4-7]; and not for [8-11] as
4590 * that range has a delayed allocated blocks.
4591 * Thus total reserved clusters now becomes 3.
4592 * * Now, during the delayed allocation writeout
4593 * time, we will first write blocks [3-8] and
4594 * allocate 3 clusters for writing these
4595 * blocks. Also, we would claim all these
4596 * three clusters above.
4597 * * Now when we come here to writeout the
4598 * blocks [10-11], we would expect to claim
4599 * the reservation of 1 cluster we had made
4600 * (and we would claim it since there are no
4601 * more delayed allocated blocks in the range
4602 * [8-11]. But our reserved cluster count had
4603 * already gone to 0.
4605 * Thus, at the step 4 above when we determine
4606 * that there are still some unwritten delayed
4607 * allocated blocks outside of our current
4608 * block range, we should increment the
4609 * reserved clusters count so that when the
4610 * remaining blocks finally gets written, we
4613 dquot_reserve_block(inode,
4614 EXT4_C2B(sbi, reservation));
4615 spin_lock(&ei->i_block_reservation_lock);
4616 ei->i_reserved_data_blocks += reservation;
4617 spin_unlock(&ei->i_block_reservation_lock);
4620 * We will claim quota for all newly allocated blocks.
4621 * We're updating the reserved space *after* the
4622 * correction above so we do not accidentally free
4623 * all the metadata reservation because we might
4624 * actually need it later on.
4626 ext4_da_update_reserve_space(inode, allocated_clusters,
4632 * Cache the extent and update transaction to commit on fdatasync only
4633 * when it is _not_ an unwritten extent.
4635 if ((flags & EXT4_GET_BLOCKS_UNWRIT_EXT) == 0)
4636 ext4_update_inode_fsync_trans(handle, inode, 1);
4638 ext4_update_inode_fsync_trans(handle, inode, 0);
4640 if (allocated > map->m_len)
4641 allocated = map->m_len;
4642 ext4_ext_show_leaf(inode, path);
4643 map->m_flags |= EXT4_MAP_MAPPED;
4644 map->m_pblk = newblock;
4645 map->m_len = allocated;
4648 ext4_ext_drop_refs(path);
4652 trace_ext4_ext_map_blocks_exit(inode, flags, map,
4653 err ? err : allocated);
4654 ext4_es_lru_add(inode);
4655 return err ? err : allocated;
4658 void ext4_ext_truncate(handle_t *handle, struct inode *inode)
4660 struct super_block *sb = inode->i_sb;
4661 ext4_lblk_t last_block;
4665 * TODO: optimization is possible here.
4666 * Probably we need not scan at all,
4667 * because page truncation is enough.
4670 /* we have to know where to truncate from in crash case */
4671 EXT4_I(inode)->i_disksize = inode->i_size;
4672 ext4_mark_inode_dirty(handle, inode);
4674 last_block = (inode->i_size + sb->s_blocksize - 1)
4675 >> EXT4_BLOCK_SIZE_BITS(sb);
4677 err = ext4_es_remove_extent(inode, last_block,
4678 EXT_MAX_BLOCKS - last_block);
4679 if (err == -ENOMEM) {
4681 congestion_wait(BLK_RW_ASYNC, HZ/50);
4685 ext4_std_error(inode->i_sb, err);
4688 err = ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1);
4689 ext4_std_error(inode->i_sb, err);
4692 static int ext4_alloc_file_blocks(struct file *file, ext4_lblk_t offset,
4693 ext4_lblk_t len, loff_t new_size,
4694 int flags, int mode)
4696 struct inode *inode = file_inode(file);
4701 struct ext4_map_blocks map;
4702 unsigned int credits;
4705 map.m_lblk = offset;
4708 * Don't normalize the request if it can fit in one extent so
4709 * that it doesn't get unnecessarily split into multiple
4712 if (len <= EXT_UNWRITTEN_MAX_LEN)
4713 flags |= EXT4_GET_BLOCKS_NO_NORMALIZE;
4716 * credits to insert 1 extent into extent tree
4718 credits = ext4_chunk_trans_blocks(inode, len);
4721 while (ret >= 0 && len) {
4722 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
4724 if (IS_ERR(handle)) {
4725 ret = PTR_ERR(handle);
4728 ret = ext4_map_blocks(handle, inode, &map, flags);
4730 ext4_debug("inode #%lu: block %u: len %u: "
4731 "ext4_ext_map_blocks returned %d",
4732 inode->i_ino, map.m_lblk,
4734 ext4_mark_inode_dirty(handle, inode);
4735 ret2 = ext4_journal_stop(handle);
4739 map.m_len = len = len - ret;
4740 epos = (loff_t)map.m_lblk << inode->i_blkbits;
4741 inode->i_ctime = ext4_current_time(inode);
4743 if (epos > new_size)
4745 if (ext4_update_inode_size(inode, epos) & 0x1)
4746 inode->i_mtime = inode->i_ctime;
4748 if (epos > inode->i_size)
4749 ext4_set_inode_flag(inode,
4750 EXT4_INODE_EOFBLOCKS);
4752 ext4_mark_inode_dirty(handle, inode);
4753 ext4_update_inode_fsync_trans(handle, inode, 1);
4754 ret2 = ext4_journal_stop(handle);
4758 if (ret == -ENOSPC &&
4759 ext4_should_retry_alloc(inode->i_sb, &retries)) {
4764 return ret > 0 ? ret2 : ret;
4767 static long ext4_zero_range(struct file *file, loff_t offset,
4768 loff_t len, int mode)
4770 struct inode *inode = file_inode(file);
4771 handle_t *handle = NULL;
4772 unsigned int max_blocks;
4773 loff_t new_size = 0;
4777 int partial_begin, partial_end;
4780 unsigned int blkbits = inode->i_blkbits;
4782 trace_ext4_zero_range(inode, offset, len, mode);
4784 if (!S_ISREG(inode->i_mode))
4787 /* Call ext4_force_commit to flush all data in case of data=journal. */
4788 if (ext4_should_journal_data(inode)) {
4789 ret = ext4_force_commit(inode->i_sb);
4795 * Round up offset. This is not fallocate, we neet to zero out
4796 * blocks, so convert interior block aligned part of the range to
4797 * unwritten and possibly manually zero out unaligned parts of the
4800 start = round_up(offset, 1 << blkbits);
4801 end = round_down((offset + len), 1 << blkbits);
4803 if (start < offset || end > offset + len)
4805 partial_begin = offset & ((1 << blkbits) - 1);
4806 partial_end = (offset + len) & ((1 << blkbits) - 1);
4808 lblk = start >> blkbits;
4809 max_blocks = (end >> blkbits);
4810 if (max_blocks < lblk)
4815 mutex_lock(&inode->i_mutex);
4818 * Indirect files do not support unwritten extnets
4820 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
4825 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
4826 (offset + len > i_size_read(inode) ||
4827 offset + len > EXT4_I(inode)->i_disksize)) {
4828 new_size = offset + len;
4829 ret = inode_newsize_ok(inode, new_size);
4834 flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
4835 if (mode & FALLOC_FL_KEEP_SIZE)
4836 flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
4838 /* Wait all existing dio workers, newcomers will block on i_mutex */
4839 ext4_inode_block_unlocked_dio(inode);
4840 inode_dio_wait(inode);
4842 /* Preallocate the range including the unaligned edges */
4843 if (partial_begin || partial_end) {
4844 ret = ext4_alloc_file_blocks(file,
4845 round_down(offset, 1 << blkbits) >> blkbits,
4846 (round_up((offset + len), 1 << blkbits) -
4847 round_down(offset, 1 << blkbits)) >> blkbits,
4848 new_size, flags, mode);
4854 /* Zero range excluding the unaligned edges */
4855 if (max_blocks > 0) {
4856 flags |= (EXT4_GET_BLOCKS_CONVERT_UNWRITTEN |
4860 * Prevent page faults from reinstantiating pages we have
4861 * released from page cache.
4863 down_write(&EXT4_I(inode)->i_mmap_sem);
4864 ret = ext4_update_disksize_before_punch(inode, offset, len);
4866 up_write(&EXT4_I(inode)->i_mmap_sem);
4869 /* Now release the pages and zero block aligned part of pages */
4870 truncate_pagecache_range(inode, start, end - 1);
4871 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
4873 ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size,
4875 up_write(&EXT4_I(inode)->i_mmap_sem);
4879 if (!partial_begin && !partial_end)
4883 * In worst case we have to writeout two nonadjacent unwritten
4884 * blocks and update the inode
4886 credits = (2 * ext4_ext_index_trans_blocks(inode, 2)) + 1;
4887 if (ext4_should_journal_data(inode))
4889 handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
4890 if (IS_ERR(handle)) {
4891 ret = PTR_ERR(handle);
4892 ext4_std_error(inode->i_sb, ret);
4896 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
4898 ext4_update_inode_size(inode, new_size);
4901 * Mark that we allocate beyond EOF so the subsequent truncate
4902 * can proceed even if the new size is the same as i_size.
4904 if ((offset + len) > i_size_read(inode))
4905 ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
4907 ext4_mark_inode_dirty(handle, inode);
4909 /* Zero out partial block at the edges of the range */
4910 ret = ext4_zero_partial_blocks(handle, inode, offset, len);
4912 ext4_update_inode_fsync_trans(handle, inode, 1);
4914 if (file->f_flags & O_SYNC)
4915 ext4_handle_sync(handle);
4917 ext4_journal_stop(handle);
4919 ext4_inode_resume_unlocked_dio(inode);
4921 mutex_unlock(&inode->i_mutex);
4926 * preallocate space for a file. This implements ext4's fallocate file
4927 * operation, which gets called from sys_fallocate system call.
4928 * For block-mapped files, posix_fallocate should fall back to the method
4929 * of writing zeroes to the required new blocks (the same behavior which is
4930 * expected for file systems which do not support fallocate() system call).
4932 long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
4934 struct inode *inode = file_inode(file);
4935 loff_t new_size = 0;
4936 unsigned int max_blocks;
4940 unsigned int blkbits = inode->i_blkbits;
4942 /* Return error if mode is not supported */
4943 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
4944 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE))
4947 if (mode & FALLOC_FL_PUNCH_HOLE)
4948 return ext4_punch_hole(inode, offset, len);
4950 ret = ext4_convert_inline_data(inode);
4954 if (mode & FALLOC_FL_COLLAPSE_RANGE)
4955 return ext4_collapse_range(inode, offset, len);
4957 if (mode & FALLOC_FL_ZERO_RANGE)
4958 return ext4_zero_range(file, offset, len, mode);
4960 trace_ext4_fallocate_enter(inode, offset, len, mode);
4961 lblk = offset >> blkbits;
4963 * We can't just convert len to max_blocks because
4964 * If blocksize = 4096 offset = 3072 and len = 2048
4966 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
4969 flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
4970 if (mode & FALLOC_FL_KEEP_SIZE)
4971 flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
4973 mutex_lock(&inode->i_mutex);
4976 * We only support preallocation for extent-based files only
4978 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
4983 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
4984 (offset + len > i_size_read(inode) ||
4985 offset + len > EXT4_I(inode)->i_disksize)) {
4986 new_size = offset + len;
4987 ret = inode_newsize_ok(inode, new_size);
4992 /* Wait all existing dio workers, newcomers will block on i_mutex */
4993 ext4_inode_block_unlocked_dio(inode);
4994 inode_dio_wait(inode);
4996 ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size,
4998 ext4_inode_resume_unlocked_dio(inode);
5002 if (file->f_flags & O_SYNC && EXT4_SB(inode->i_sb)->s_journal) {
5003 ret = jbd2_complete_transaction(EXT4_SB(inode->i_sb)->s_journal,
5004 EXT4_I(inode)->i_sync_tid);
5007 mutex_unlock(&inode->i_mutex);
5008 trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
5013 * This function convert a range of blocks to written extents
5014 * The caller of this function will pass the start offset and the size.
5015 * all unwritten extents within this range will be converted to
5018 * This function is called from the direct IO end io call back
5019 * function, to convert the fallocated extents after IO is completed.
5020 * Returns 0 on success.
5022 int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode,
5023 loff_t offset, ssize_t len)
5025 unsigned int max_blocks;
5028 struct ext4_map_blocks map;
5029 unsigned int credits, blkbits = inode->i_blkbits;
5031 map.m_lblk = offset >> blkbits;
5033 * We can't just convert len to max_blocks because
5034 * If blocksize = 4096 offset = 3072 and len = 2048
5036 max_blocks = ((EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) -
5039 * This is somewhat ugly but the idea is clear: When transaction is
5040 * reserved, everything goes into it. Otherwise we rather start several
5041 * smaller transactions for conversion of each extent separately.
5044 handle = ext4_journal_start_reserved(handle,
5045 EXT4_HT_EXT_CONVERT);
5047 return PTR_ERR(handle);
5051 * credits to insert 1 extent into extent tree
5053 credits = ext4_chunk_trans_blocks(inode, max_blocks);
5055 while (ret >= 0 && ret < max_blocks) {
5057 map.m_len = (max_blocks -= ret);
5059 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
5061 if (IS_ERR(handle)) {
5062 ret = PTR_ERR(handle);
5066 ret = ext4_map_blocks(handle, inode, &map,
5067 EXT4_GET_BLOCKS_IO_CONVERT_EXT);
5069 ext4_warning(inode->i_sb,
5070 "inode #%lu: block %u: len %u: "
5071 "ext4_ext_map_blocks returned %d",
5072 inode->i_ino, map.m_lblk,
5074 ext4_mark_inode_dirty(handle, inode);
5076 ret2 = ext4_journal_stop(handle);
5077 if (ret <= 0 || ret2)
5081 ret2 = ext4_journal_stop(handle);
5082 return ret > 0 ? ret2 : ret;
5086 * If newes is not existing extent (newes->ec_pblk equals zero) find
5087 * delayed extent at start of newes and update newes accordingly and
5088 * return start of the next delayed extent.
5090 * If newes is existing extent (newes->ec_pblk is not equal zero)
5091 * return start of next delayed extent or EXT_MAX_BLOCKS if no delayed
5092 * extent found. Leave newes unmodified.
5094 static int ext4_find_delayed_extent(struct inode *inode,
5095 struct extent_status *newes)
5097 struct extent_status es;
5098 ext4_lblk_t block, next_del;
5100 if (newes->es_pblk == 0) {
5101 ext4_es_find_delayed_extent_range(inode, newes->es_lblk,
5102 newes->es_lblk + newes->es_len - 1, &es);
5105 * No extent in extent-tree contains block @newes->es_pblk,
5106 * then the block may stay in 1)a hole or 2)delayed-extent.
5112 if (es.es_lblk > newes->es_lblk) {
5114 newes->es_len = min(es.es_lblk - newes->es_lblk,
5119 newes->es_len = es.es_lblk + es.es_len - newes->es_lblk;
5122 block = newes->es_lblk + newes->es_len;
5123 ext4_es_find_delayed_extent_range(inode, block, EXT_MAX_BLOCKS, &es);
5125 next_del = EXT_MAX_BLOCKS;
5127 next_del = es.es_lblk;
5131 /* fiemap flags we can handle specified here */
5132 #define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
5134 static int ext4_xattr_fiemap(struct inode *inode,
5135 struct fiemap_extent_info *fieinfo)
5139 __u32 flags = FIEMAP_EXTENT_LAST;
5140 int blockbits = inode->i_sb->s_blocksize_bits;
5144 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
5145 struct ext4_iloc iloc;
5146 int offset; /* offset of xattr in inode */
5148 error = ext4_get_inode_loc(inode, &iloc);
5151 physical = (__u64)iloc.bh->b_blocknr << blockbits;
5152 offset = EXT4_GOOD_OLD_INODE_SIZE +
5153 EXT4_I(inode)->i_extra_isize;
5155 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
5156 flags |= FIEMAP_EXTENT_DATA_INLINE;
5158 } else { /* external block */
5159 physical = (__u64)EXT4_I(inode)->i_file_acl << blockbits;
5160 length = inode->i_sb->s_blocksize;
5164 error = fiemap_fill_next_extent(fieinfo, 0, physical,
5166 return (error < 0 ? error : 0);
5169 int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
5170 __u64 start, __u64 len)
5172 ext4_lblk_t start_blk;
5175 if (ext4_has_inline_data(inode)) {
5178 error = ext4_inline_data_fiemap(inode, fieinfo, &has_inline,
5185 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
5186 error = ext4_ext_precache(inode);
5191 /* fallback to generic here if not in extents fmt */
5192 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
5193 return generic_block_fiemap(inode, fieinfo, start, len,
5196 if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
5199 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
5200 error = ext4_xattr_fiemap(inode, fieinfo);
5202 ext4_lblk_t len_blks;
5205 start_blk = start >> inode->i_sb->s_blocksize_bits;
5206 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
5207 if (last_blk >= EXT_MAX_BLOCKS)
5208 last_blk = EXT_MAX_BLOCKS-1;
5209 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
5212 * Walk the extent tree gathering extent information
5213 * and pushing extents back to the user.
5215 error = ext4_fill_fiemap_extents(inode, start_blk,
5218 ext4_es_lru_add(inode);
5224 * Function to access the path buffer for marking it dirty.
5225 * It also checks if there are sufficient credits left in the journal handle
5229 ext4_access_path(handle_t *handle, struct inode *inode,
5230 struct ext4_ext_path *path)
5234 if (!ext4_handle_valid(handle))
5238 * Check if need to extend journal credits
5239 * 3 for leaf, sb, and inode plus 2 (bmap and group
5240 * descriptor) for each block group; assume two block
5243 if (handle->h_buffer_credits < 7) {
5244 credits = ext4_writepage_trans_blocks(inode);
5245 err = ext4_ext_truncate_extend_restart(handle, inode, credits);
5246 /* EAGAIN is success */
5247 if (err && err != -EAGAIN)
5251 err = ext4_ext_get_access(handle, inode, path);
5256 * ext4_ext_shift_path_extents:
5257 * Shift the extents of a path structure lying between path[depth].p_ext
5258 * and EXT_LAST_EXTENT(path[depth].p_hdr) downwards, by subtracting shift
5259 * from starting block for each extent.
5262 ext4_ext_shift_path_extents(struct ext4_ext_path *path, ext4_lblk_t shift,
5263 struct inode *inode, handle_t *handle,
5267 struct ext4_extent *ex_start, *ex_last;
5269 depth = path->p_depth;
5271 while (depth >= 0) {
5272 if (depth == path->p_depth) {
5273 ex_start = path[depth].p_ext;
5277 ex_last = EXT_LAST_EXTENT(path[depth].p_hdr);
5281 err = ext4_access_path(handle, inode, path + depth);
5285 if (ex_start == EXT_FIRST_EXTENT(path[depth].p_hdr))
5288 *start = le32_to_cpu(ex_last->ee_block) +
5289 ext4_ext_get_actual_len(ex_last);
5291 while (ex_start <= ex_last) {
5292 le32_add_cpu(&ex_start->ee_block, -shift);
5293 /* Try to merge to the left. */
5295 EXT_FIRST_EXTENT(path[depth].p_hdr)) &&
5296 ext4_ext_try_to_merge_right(inode,
5297 path, ex_start - 1))
5302 err = ext4_ext_dirty(handle, inode, path + depth);
5306 if (--depth < 0 || !update)
5310 /* Update index too */
5311 err = ext4_access_path(handle, inode, path + depth);
5315 le32_add_cpu(&path[depth].p_idx->ei_block, -shift);
5316 err = ext4_ext_dirty(handle, inode, path + depth);
5320 /* we are done if current index is not a starting index */
5321 if (path[depth].p_idx != EXT_FIRST_INDEX(path[depth].p_hdr))
5332 * ext4_ext_shift_extents:
5333 * All the extents which lies in the range from start to the last allocated
5334 * block for the file are shifted downwards by shift blocks.
5335 * On success, 0 is returned, error otherwise.
5338 ext4_ext_shift_extents(struct inode *inode, handle_t *handle,
5339 ext4_lblk_t start, ext4_lblk_t shift)
5341 struct ext4_ext_path *path;
5343 struct ext4_extent *extent;
5344 ext4_lblk_t stop_block, current_block;
5345 ext4_lblk_t ex_start, ex_end;
5347 /* Let path point to the last extent */
5348 path = ext4_ext_find_extent(inode, EXT_MAX_BLOCKS - 1, NULL, 0);
5350 return PTR_ERR(path);
5352 depth = path->p_depth;
5353 extent = path[depth].p_ext;
5355 ext4_ext_drop_refs(path);
5360 stop_block = le32_to_cpu(extent->ee_block) +
5361 ext4_ext_get_actual_len(extent);
5362 ext4_ext_drop_refs(path);
5365 /* Nothing to shift, if hole is at the end of file */
5366 if (start >= stop_block)
5370 * Don't start shifting extents until we make sure the hole is big
5371 * enough to accomodate the shift.
5373 path = ext4_ext_find_extent(inode, start - 1, NULL, 0);
5375 return PTR_ERR(path);
5376 depth = path->p_depth;
5377 extent = path[depth].p_ext;
5379 ex_start = le32_to_cpu(extent->ee_block);
5380 ex_end = le32_to_cpu(extent->ee_block) +
5381 ext4_ext_get_actual_len(extent);
5386 ext4_ext_drop_refs(path);
5389 if ((start == ex_start && shift > ex_start) ||
5390 (shift > start - ex_end))
5393 /* Its safe to start updating extents */
5394 while (start < stop_block) {
5395 path = ext4_ext_find_extent(inode, start, NULL, 0);
5397 return PTR_ERR(path);
5398 depth = path->p_depth;
5399 extent = path[depth].p_ext;
5401 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
5402 (unsigned long) start);
5406 current_block = le32_to_cpu(extent->ee_block);
5407 if (start > current_block) {
5408 /* Hole, move to the next extent */
5409 ret = mext_next_extent(inode, path, &extent);
5411 ext4_ext_drop_refs(path);
5418 ret = ext4_ext_shift_path_extents(path, shift, inode,
5420 ext4_ext_drop_refs(path);
5430 * ext4_collapse_range:
5431 * This implements the fallocate's collapse range functionality for ext4
5432 * Returns: 0 and non-zero on error.
5434 int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len)
5436 struct super_block *sb = inode->i_sb;
5437 ext4_lblk_t punch_start, punch_stop;
5439 unsigned int credits;
5440 loff_t new_size, ioffset;
5443 /* Collapse range works only on fs block size aligned offsets. */
5444 if (offset & (EXT4_BLOCK_SIZE(sb) - 1) ||
5445 len & (EXT4_BLOCK_SIZE(sb) - 1))
5448 if (!S_ISREG(inode->i_mode))
5451 if (EXT4_SB(inode->i_sb)->s_cluster_ratio > 1)
5454 trace_ext4_collapse_range(inode, offset, len);
5456 punch_start = offset >> EXT4_BLOCK_SIZE_BITS(sb);
5457 punch_stop = (offset + len) >> EXT4_BLOCK_SIZE_BITS(sb);
5459 /* Call ext4_force_commit to flush all data in case of data=journal. */
5460 if (ext4_should_journal_data(inode)) {
5461 ret = ext4_force_commit(inode->i_sb);
5466 mutex_lock(&inode->i_mutex);
5468 * There is no need to overlap collapse range with EOF, in which case
5469 * it is effectively a truncate operation
5471 if (offset + len >= i_size_read(inode)) {
5476 /* Currently just for extent based files */
5477 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
5482 /* Wait for existing dio to complete */
5483 ext4_inode_block_unlocked_dio(inode);
5484 inode_dio_wait(inode);
5487 * Prevent page faults from reinstantiating pages we have released from
5490 down_write(&EXT4_I(inode)->i_mmap_sem);
5492 * Need to round down offset to be aligned with page size boundary
5493 * for page size > block size.
5495 ioffset = round_down(offset, PAGE_SIZE);
5497 * Write tail of the last page before removed range since it will get
5498 * removed from the page cache below.
5500 ret = filemap_write_and_wait_range(inode->i_mapping, ioffset, offset);
5504 * Write data that will be shifted to preserve them when discarding
5505 * page cache below. We are also protected from pages becoming dirty
5508 ret = filemap_write_and_wait_range(inode->i_mapping, offset + len,
5512 truncate_pagecache(inode, ioffset);
5514 credits = ext4_writepage_trans_blocks(inode);
5515 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
5516 if (IS_ERR(handle)) {
5517 ret = PTR_ERR(handle);
5521 down_write(&EXT4_I(inode)->i_data_sem);
5522 ext4_discard_preallocations(inode);
5524 ret = ext4_es_remove_extent(inode, punch_start,
5525 EXT_MAX_BLOCKS - punch_start);
5527 up_write(&EXT4_I(inode)->i_data_sem);
5531 ret = ext4_ext_remove_space(inode, punch_start, punch_stop - 1);
5533 up_write(&EXT4_I(inode)->i_data_sem);
5536 ext4_discard_preallocations(inode);
5538 ret = ext4_ext_shift_extents(inode, handle, punch_stop,
5539 punch_stop - punch_start);
5541 up_write(&EXT4_I(inode)->i_data_sem);
5545 new_size = i_size_read(inode) - len;
5546 i_size_write(inode, new_size);
5547 EXT4_I(inode)->i_disksize = new_size;
5549 up_write(&EXT4_I(inode)->i_data_sem);
5551 ext4_handle_sync(handle);
5552 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
5553 ext4_mark_inode_dirty(handle, inode);
5554 ext4_update_inode_fsync_trans(handle, inode, 1);
5557 ext4_journal_stop(handle);
5559 up_write(&EXT4_I(inode)->i_mmap_sem);
5560 ext4_inode_resume_unlocked_dio(inode);
5562 mutex_unlock(&inode->i_mutex);