2 * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
3 * Written by Alex Tomas <alex@clusterfs.com>
5 * Architecture independence:
6 * Copyright (c) 2005, Bull S.A.
7 * Written by Pierre Peiffer <pierre.peiffer@bull.net>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public Licens
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
24 * Extents support for EXT4
27 * - ext4*_error() should be used in some situations
28 * - analyze all BUG()/BUG_ON(), use -EIO where appropriate
29 * - smart tree reduction
32 #include <linux/module.h>
34 #include <linux/time.h>
35 #include <linux/jbd2.h>
36 #include <linux/highuid.h>
37 #include <linux/pagemap.h>
38 #include <linux/quotaops.h>
39 #include <linux/string.h>
40 #include <linux/slab.h>
41 #include <linux/falloc.h>
42 #include <asm/uaccess.h>
43 #include <linux/fiemap.h>
44 #include "ext4_jbd2.h"
46 #include <trace/events/ext4.h>
48 static int ext4_split_extent(handle_t *handle,
50 struct ext4_ext_path *path,
51 struct ext4_map_blocks *map,
55 static int ext4_ext_truncate_extend_restart(handle_t *handle,
61 if (!ext4_handle_valid(handle))
63 if (handle->h_buffer_credits > needed)
65 err = ext4_journal_extend(handle, needed);
68 err = ext4_truncate_restart_trans(handle, inode, needed);
80 static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
81 struct ext4_ext_path *path)
84 /* path points to block */
85 return ext4_journal_get_write_access(handle, path->p_bh);
87 /* path points to leaf/index in inode body */
88 /* we use in-core data, no need to protect them */
98 #define ext4_ext_dirty(handle, inode, path) \
99 __ext4_ext_dirty(__func__, __LINE__, (handle), (inode), (path))
100 static int __ext4_ext_dirty(const char *where, unsigned int line,
101 handle_t *handle, struct inode *inode,
102 struct ext4_ext_path *path)
106 /* path points to block */
107 err = __ext4_handle_dirty_metadata(where, line, handle,
110 /* path points to leaf/index in inode body */
111 err = ext4_mark_inode_dirty(handle, inode);
116 static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
117 struct ext4_ext_path *path,
123 struct ext4_extent *ex;
124 depth = path->p_depth;
127 * Try to predict block placement assuming that we are
128 * filling in a file which will eventually be
129 * non-sparse --- i.e., in the case of libbfd writing
130 * an ELF object sections out-of-order but in a way
131 * the eventually results in a contiguous object or
132 * executable file, or some database extending a table
133 * space file. However, this is actually somewhat
134 * non-ideal if we are writing a sparse file such as
135 * qemu or KVM writing a raw image file that is going
136 * to stay fairly sparse, since it will end up
137 * fragmenting the file system's free space. Maybe we
138 * should have some hueristics or some way to allow
139 * userspace to pass a hint to file system,
140 * especially if the latter case turns out to be
143 ex = path[depth].p_ext;
145 ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex);
146 ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block);
148 if (block > ext_block)
149 return ext_pblk + (block - ext_block);
151 return ext_pblk - (ext_block - block);
154 /* it looks like index is empty;
155 * try to find starting block from index itself */
156 if (path[depth].p_bh)
157 return path[depth].p_bh->b_blocknr;
160 /* OK. use inode's group */
161 return ext4_inode_to_goal_block(inode);
165 * Allocation for a meta data block
168 ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
169 struct ext4_ext_path *path,
170 struct ext4_extent *ex, int *err, unsigned int flags)
172 ext4_fsblk_t goal, newblock;
174 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
175 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
180 static inline int ext4_ext_space_block(struct inode *inode, int check)
184 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
185 / sizeof(struct ext4_extent);
187 #ifdef AGGRESSIVE_TEST
195 static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
199 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
200 / sizeof(struct ext4_extent_idx);
202 #ifdef AGGRESSIVE_TEST
210 static inline int ext4_ext_space_root(struct inode *inode, int check)
214 size = sizeof(EXT4_I(inode)->i_data);
215 size -= sizeof(struct ext4_extent_header);
216 size /= sizeof(struct ext4_extent);
218 #ifdef AGGRESSIVE_TEST
226 static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
230 size = sizeof(EXT4_I(inode)->i_data);
231 size -= sizeof(struct ext4_extent_header);
232 size /= sizeof(struct ext4_extent_idx);
234 #ifdef AGGRESSIVE_TEST
243 * Calculate the number of metadata blocks needed
244 * to allocate @blocks
245 * Worse case is one block per extent
247 int ext4_ext_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
249 struct ext4_inode_info *ei = EXT4_I(inode);
252 idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
253 / sizeof(struct ext4_extent_idx));
256 * If the new delayed allocation block is contiguous with the
257 * previous da block, it can share index blocks with the
258 * previous block, so we only need to allocate a new index
259 * block every idxs leaf blocks. At ldxs**2 blocks, we need
260 * an additional index block, and at ldxs**3 blocks, yet
261 * another index blocks.
263 if (ei->i_da_metadata_calc_len &&
264 ei->i_da_metadata_calc_last_lblock+1 == lblock) {
265 if ((ei->i_da_metadata_calc_len % idxs) == 0)
267 if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0)
269 if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) {
271 ei->i_da_metadata_calc_len = 0;
273 ei->i_da_metadata_calc_len++;
274 ei->i_da_metadata_calc_last_lblock++;
279 * In the worst case we need a new set of index blocks at
280 * every level of the inode's extent tree.
282 ei->i_da_metadata_calc_len = 1;
283 ei->i_da_metadata_calc_last_lblock = lblock;
284 return ext_depth(inode) + 1;
288 ext4_ext_max_entries(struct inode *inode, int depth)
292 if (depth == ext_depth(inode)) {
294 max = ext4_ext_space_root(inode, 1);
296 max = ext4_ext_space_root_idx(inode, 1);
299 max = ext4_ext_space_block(inode, 1);
301 max = ext4_ext_space_block_idx(inode, 1);
307 static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
309 ext4_fsblk_t block = ext4_ext_pblock(ext);
310 int len = ext4_ext_get_actual_len(ext);
312 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
315 static int ext4_valid_extent_idx(struct inode *inode,
316 struct ext4_extent_idx *ext_idx)
318 ext4_fsblk_t block = ext4_idx_pblock(ext_idx);
320 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
323 static int ext4_valid_extent_entries(struct inode *inode,
324 struct ext4_extent_header *eh,
327 struct ext4_extent *ext;
328 struct ext4_extent_idx *ext_idx;
329 unsigned short entries;
330 if (eh->eh_entries == 0)
333 entries = le16_to_cpu(eh->eh_entries);
337 ext = EXT_FIRST_EXTENT(eh);
339 if (!ext4_valid_extent(inode, ext))
345 ext_idx = EXT_FIRST_INDEX(eh);
347 if (!ext4_valid_extent_idx(inode, ext_idx))
356 static int __ext4_ext_check(const char *function, unsigned int line,
357 struct inode *inode, struct ext4_extent_header *eh,
360 const char *error_msg;
363 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
364 error_msg = "invalid magic";
367 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
368 error_msg = "unexpected eh_depth";
371 if (unlikely(eh->eh_max == 0)) {
372 error_msg = "invalid eh_max";
375 max = ext4_ext_max_entries(inode, depth);
376 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
377 error_msg = "too large eh_max";
380 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
381 error_msg = "invalid eh_entries";
384 if (!ext4_valid_extent_entries(inode, eh, depth)) {
385 error_msg = "invalid extent entries";
391 ext4_error_inode(inode, function, line, 0,
392 "bad header/extent: %s - magic %x, "
393 "entries %u, max %u(%u), depth %u(%u)",
394 error_msg, le16_to_cpu(eh->eh_magic),
395 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
396 max, le16_to_cpu(eh->eh_depth), depth);
401 #define ext4_ext_check(inode, eh, depth) \
402 __ext4_ext_check(__func__, __LINE__, inode, eh, depth)
404 int ext4_ext_check_inode(struct inode *inode)
406 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode));
410 static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
412 int k, l = path->p_depth;
415 for (k = 0; k <= l; k++, path++) {
417 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
418 ext4_idx_pblock(path->p_idx));
419 } else if (path->p_ext) {
420 ext_debug(" %d:[%d]%d:%llu ",
421 le32_to_cpu(path->p_ext->ee_block),
422 ext4_ext_is_uninitialized(path->p_ext),
423 ext4_ext_get_actual_len(path->p_ext),
424 ext4_ext_pblock(path->p_ext));
431 static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
433 int depth = ext_depth(inode);
434 struct ext4_extent_header *eh;
435 struct ext4_extent *ex;
441 eh = path[depth].p_hdr;
442 ex = EXT_FIRST_EXTENT(eh);
444 ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
446 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
447 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
448 ext4_ext_is_uninitialized(ex),
449 ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
454 static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path,
455 ext4_fsblk_t newblock, int level)
457 int depth = ext_depth(inode);
458 struct ext4_extent *ex;
460 if (depth != level) {
461 struct ext4_extent_idx *idx;
462 idx = path[level].p_idx;
463 while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) {
464 ext_debug("%d: move %d:%llu in new index %llu\n", level,
465 le32_to_cpu(idx->ei_block),
466 ext4_idx_pblock(idx),
474 ex = path[depth].p_ext;
475 while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) {
476 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
477 le32_to_cpu(ex->ee_block),
479 ext4_ext_is_uninitialized(ex),
480 ext4_ext_get_actual_len(ex),
487 #define ext4_ext_show_path(inode, path)
488 #define ext4_ext_show_leaf(inode, path)
489 #define ext4_ext_show_move(inode, path, newblock, level)
492 void ext4_ext_drop_refs(struct ext4_ext_path *path)
494 int depth = path->p_depth;
497 for (i = 0; i <= depth; i++, path++)
505 * ext4_ext_binsearch_idx:
506 * binary search for the closest index of the given block
507 * the header must be checked before calling this
510 ext4_ext_binsearch_idx(struct inode *inode,
511 struct ext4_ext_path *path, ext4_lblk_t block)
513 struct ext4_extent_header *eh = path->p_hdr;
514 struct ext4_extent_idx *r, *l, *m;
517 ext_debug("binsearch for %u(idx): ", block);
519 l = EXT_FIRST_INDEX(eh) + 1;
520 r = EXT_LAST_INDEX(eh);
523 if (block < le32_to_cpu(m->ei_block))
527 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
528 m, le32_to_cpu(m->ei_block),
529 r, le32_to_cpu(r->ei_block));
533 ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
534 ext4_idx_pblock(path->p_idx));
536 #ifdef CHECK_BINSEARCH
538 struct ext4_extent_idx *chix, *ix;
541 chix = ix = EXT_FIRST_INDEX(eh);
542 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
544 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
545 printk(KERN_DEBUG "k=%d, ix=0x%p, "
547 ix, EXT_FIRST_INDEX(eh));
548 printk(KERN_DEBUG "%u <= %u\n",
549 le32_to_cpu(ix->ei_block),
550 le32_to_cpu(ix[-1].ei_block));
552 BUG_ON(k && le32_to_cpu(ix->ei_block)
553 <= le32_to_cpu(ix[-1].ei_block));
554 if (block < le32_to_cpu(ix->ei_block))
558 BUG_ON(chix != path->p_idx);
565 * ext4_ext_binsearch:
566 * binary search for closest extent of the given block
567 * the header must be checked before calling this
570 ext4_ext_binsearch(struct inode *inode,
571 struct ext4_ext_path *path, ext4_lblk_t block)
573 struct ext4_extent_header *eh = path->p_hdr;
574 struct ext4_extent *r, *l, *m;
576 if (eh->eh_entries == 0) {
578 * this leaf is empty:
579 * we get such a leaf in split/add case
584 ext_debug("binsearch for %u: ", block);
586 l = EXT_FIRST_EXTENT(eh) + 1;
587 r = EXT_LAST_EXTENT(eh);
591 if (block < le32_to_cpu(m->ee_block))
595 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
596 m, le32_to_cpu(m->ee_block),
597 r, le32_to_cpu(r->ee_block));
601 ext_debug(" -> %d:%llu:[%d]%d ",
602 le32_to_cpu(path->p_ext->ee_block),
603 ext4_ext_pblock(path->p_ext),
604 ext4_ext_is_uninitialized(path->p_ext),
605 ext4_ext_get_actual_len(path->p_ext));
607 #ifdef CHECK_BINSEARCH
609 struct ext4_extent *chex, *ex;
612 chex = ex = EXT_FIRST_EXTENT(eh);
613 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
614 BUG_ON(k && le32_to_cpu(ex->ee_block)
615 <= le32_to_cpu(ex[-1].ee_block));
616 if (block < le32_to_cpu(ex->ee_block))
620 BUG_ON(chex != path->p_ext);
626 int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
628 struct ext4_extent_header *eh;
630 eh = ext_inode_hdr(inode);
633 eh->eh_magic = EXT4_EXT_MAGIC;
634 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
635 ext4_mark_inode_dirty(handle, inode);
636 ext4_ext_invalidate_cache(inode);
640 struct ext4_ext_path *
641 ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
642 struct ext4_ext_path *path)
644 struct ext4_extent_header *eh;
645 struct buffer_head *bh;
646 short int depth, i, ppos = 0, alloc = 0;
648 eh = ext_inode_hdr(inode);
649 depth = ext_depth(inode);
651 /* account possible depth increase */
653 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
656 return ERR_PTR(-ENOMEM);
663 /* walk through the tree */
665 int need_to_validate = 0;
667 ext_debug("depth %d: num %d, max %d\n",
668 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
670 ext4_ext_binsearch_idx(inode, path + ppos, block);
671 path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
672 path[ppos].p_depth = i;
673 path[ppos].p_ext = NULL;
675 bh = sb_getblk(inode->i_sb, path[ppos].p_block);
678 if (!bh_uptodate_or_lock(bh)) {
679 trace_ext4_ext_load_extent(inode, block,
681 if (bh_submit_read(bh) < 0) {
685 /* validate the extent entries */
686 need_to_validate = 1;
688 eh = ext_block_hdr(bh);
690 if (unlikely(ppos > depth)) {
692 EXT4_ERROR_INODE(inode,
693 "ppos %d > depth %d", ppos, depth);
696 path[ppos].p_bh = bh;
697 path[ppos].p_hdr = eh;
700 if (need_to_validate && ext4_ext_check(inode, eh, i))
704 path[ppos].p_depth = i;
705 path[ppos].p_ext = NULL;
706 path[ppos].p_idx = NULL;
709 ext4_ext_binsearch(inode, path + ppos, block);
710 /* if not an empty leaf */
711 if (path[ppos].p_ext)
712 path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
714 ext4_ext_show_path(inode, path);
719 ext4_ext_drop_refs(path);
722 return ERR_PTR(-EIO);
726 * ext4_ext_insert_index:
727 * insert new index [@logical;@ptr] into the block at @curp;
728 * check where to insert: before @curp or after @curp
730 static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
731 struct ext4_ext_path *curp,
732 int logical, ext4_fsblk_t ptr)
734 struct ext4_extent_idx *ix;
737 err = ext4_ext_get_access(handle, inode, curp);
741 if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
742 EXT4_ERROR_INODE(inode,
743 "logical %d == ei_block %d!",
744 logical, le32_to_cpu(curp->p_idx->ei_block));
748 if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
749 >= le16_to_cpu(curp->p_hdr->eh_max))) {
750 EXT4_ERROR_INODE(inode,
751 "eh_entries %d >= eh_max %d!",
752 le16_to_cpu(curp->p_hdr->eh_entries),
753 le16_to_cpu(curp->p_hdr->eh_max));
757 len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
758 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
760 if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
761 len = (len - 1) * sizeof(struct ext4_extent_idx);
762 len = len < 0 ? 0 : len;
763 ext_debug("insert new index %d after: %llu. "
764 "move %d from 0x%p to 0x%p\n",
766 (curp->p_idx + 1), (curp->p_idx + 2));
767 memmove(curp->p_idx + 2, curp->p_idx + 1, len);
769 ix = curp->p_idx + 1;
772 len = len * sizeof(struct ext4_extent_idx);
773 len = len < 0 ? 0 : len;
774 ext_debug("insert new index %d before: %llu. "
775 "move %d from 0x%p to 0x%p\n",
777 curp->p_idx, (curp->p_idx + 1));
778 memmove(curp->p_idx + 1, curp->p_idx, len);
782 if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) {
783 EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!");
787 ix->ei_block = cpu_to_le32(logical);
788 ext4_idx_store_pblock(ix, ptr);
789 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
791 if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
792 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
796 err = ext4_ext_dirty(handle, inode, curp);
797 ext4_std_error(inode->i_sb, err);
804 * inserts new subtree into the path, using free index entry
806 * - allocates all needed blocks (new leaf and all intermediate index blocks)
807 * - makes decision where to split
808 * - moves remaining extents and index entries (right to the split point)
809 * into the newly allocated blocks
810 * - initializes subtree
812 static int ext4_ext_split(handle_t *handle, struct inode *inode,
814 struct ext4_ext_path *path,
815 struct ext4_extent *newext, int at)
817 struct buffer_head *bh = NULL;
818 int depth = ext_depth(inode);
819 struct ext4_extent_header *neh;
820 struct ext4_extent_idx *fidx;
822 ext4_fsblk_t newblock, oldblock;
824 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
827 /* make decision: where to split? */
828 /* FIXME: now decision is simplest: at current extent */
830 /* if current leaf will be split, then we should use
831 * border from split point */
832 if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
833 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
836 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
837 border = path[depth].p_ext[1].ee_block;
838 ext_debug("leaf will be split."
839 " next leaf starts at %d\n",
840 le32_to_cpu(border));
842 border = newext->ee_block;
843 ext_debug("leaf will be added."
844 " next leaf starts at %d\n",
845 le32_to_cpu(border));
849 * If error occurs, then we break processing
850 * and mark filesystem read-only. index won't
851 * be inserted and tree will be in consistent
852 * state. Next mount will repair buffers too.
856 * Get array to track all allocated blocks.
857 * We need this to handle errors and free blocks
860 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
864 /* allocate all needed blocks */
865 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
866 for (a = 0; a < depth - at; a++) {
867 newblock = ext4_ext_new_meta_block(handle, inode, path,
868 newext, &err, flags);
871 ablocks[a] = newblock;
874 /* initialize new leaf */
875 newblock = ablocks[--a];
876 if (unlikely(newblock == 0)) {
877 EXT4_ERROR_INODE(inode, "newblock == 0!");
881 bh = sb_getblk(inode->i_sb, newblock);
888 err = ext4_journal_get_create_access(handle, bh);
892 neh = ext_block_hdr(bh);
894 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
895 neh->eh_magic = EXT4_EXT_MAGIC;
898 /* move remainder of path[depth] to the new leaf */
899 if (unlikely(path[depth].p_hdr->eh_entries !=
900 path[depth].p_hdr->eh_max)) {
901 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
902 path[depth].p_hdr->eh_entries,
903 path[depth].p_hdr->eh_max);
907 /* start copy from next extent */
908 m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++;
909 ext4_ext_show_move(inode, path, newblock, depth);
911 struct ext4_extent *ex;
912 ex = EXT_FIRST_EXTENT(neh);
913 memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m);
914 le16_add_cpu(&neh->eh_entries, m);
917 set_buffer_uptodate(bh);
920 err = ext4_handle_dirty_metadata(handle, inode, bh);
926 /* correct old leaf */
928 err = ext4_ext_get_access(handle, inode, path + depth);
931 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
932 err = ext4_ext_dirty(handle, inode, path + depth);
938 /* create intermediate indexes */
940 if (unlikely(k < 0)) {
941 EXT4_ERROR_INODE(inode, "k %d < 0!", k);
946 ext_debug("create %d intermediate indices\n", k);
947 /* insert new index into current index block */
948 /* current depth stored in i var */
952 newblock = ablocks[--a];
953 bh = sb_getblk(inode->i_sb, newblock);
960 err = ext4_journal_get_create_access(handle, bh);
964 neh = ext_block_hdr(bh);
965 neh->eh_entries = cpu_to_le16(1);
966 neh->eh_magic = EXT4_EXT_MAGIC;
967 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
968 neh->eh_depth = cpu_to_le16(depth - i);
969 fidx = EXT_FIRST_INDEX(neh);
970 fidx->ei_block = border;
971 ext4_idx_store_pblock(fidx, oldblock);
973 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
974 i, newblock, le32_to_cpu(border), oldblock);
976 /* move remainder of path[i] to the new index block */
977 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
978 EXT_LAST_INDEX(path[i].p_hdr))) {
979 EXT4_ERROR_INODE(inode,
980 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
981 le32_to_cpu(path[i].p_ext->ee_block));
985 /* start copy indexes */
986 m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++;
987 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
988 EXT_MAX_INDEX(path[i].p_hdr));
989 ext4_ext_show_move(inode, path, newblock, i);
991 memmove(++fidx, path[i].p_idx,
992 sizeof(struct ext4_extent_idx) * m);
993 le16_add_cpu(&neh->eh_entries, m);
995 set_buffer_uptodate(bh);
998 err = ext4_handle_dirty_metadata(handle, inode, bh);
1004 /* correct old index */
1006 err = ext4_ext_get_access(handle, inode, path + i);
1009 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
1010 err = ext4_ext_dirty(handle, inode, path + i);
1018 /* insert new index */
1019 err = ext4_ext_insert_index(handle, inode, path + at,
1020 le32_to_cpu(border), newblock);
1024 if (buffer_locked(bh))
1030 /* free all allocated blocks in error case */
1031 for (i = 0; i < depth; i++) {
1034 ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
1035 EXT4_FREE_BLOCKS_METADATA);
1044 * ext4_ext_grow_indepth:
1045 * implements tree growing procedure:
1046 * - allocates new block
1047 * - moves top-level data (index block or leaf) into the new block
1048 * - initializes new top-level, creating index that points to the
1049 * just created block
1051 static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
1053 struct ext4_extent *newext)
1055 struct ext4_extent_header *neh;
1056 struct buffer_head *bh;
1057 ext4_fsblk_t newblock;
1060 newblock = ext4_ext_new_meta_block(handle, inode, NULL,
1061 newext, &err, flags);
1065 bh = sb_getblk(inode->i_sb, newblock);
1068 ext4_std_error(inode->i_sb, err);
1073 err = ext4_journal_get_create_access(handle, bh);
1079 /* move top-level index/leaf into new block */
1080 memmove(bh->b_data, EXT4_I(inode)->i_data,
1081 sizeof(EXT4_I(inode)->i_data));
1083 /* set size of new block */
1084 neh = ext_block_hdr(bh);
1085 /* old root could have indexes or leaves
1086 * so calculate e_max right way */
1087 if (ext_depth(inode))
1088 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
1090 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
1091 neh->eh_magic = EXT4_EXT_MAGIC;
1092 set_buffer_uptodate(bh);
1095 err = ext4_handle_dirty_metadata(handle, inode, bh);
1099 /* Update top-level index: num,max,pointer */
1100 neh = ext_inode_hdr(inode);
1101 neh->eh_entries = cpu_to_le16(1);
1102 ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock);
1103 if (neh->eh_depth == 0) {
1104 /* Root extent block becomes index block */
1105 neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
1106 EXT_FIRST_INDEX(neh)->ei_block =
1107 EXT_FIRST_EXTENT(neh)->ee_block;
1109 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
1110 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
1111 le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
1112 ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
1114 neh->eh_depth = cpu_to_le16(neh->eh_depth + 1);
1115 ext4_mark_inode_dirty(handle, inode);
1123 * ext4_ext_create_new_leaf:
1124 * finds empty index and adds new leaf.
1125 * if no free index is found, then it requests in-depth growing.
1127 static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
1129 struct ext4_ext_path *path,
1130 struct ext4_extent *newext)
1132 struct ext4_ext_path *curp;
1133 int depth, i, err = 0;
1136 i = depth = ext_depth(inode);
1138 /* walk up to the tree and look for free index entry */
1139 curp = path + depth;
1140 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1145 /* we use already allocated block for index block,
1146 * so subsequent data blocks should be contiguous */
1147 if (EXT_HAS_FREE_INDEX(curp)) {
1148 /* if we found index with free entry, then use that
1149 * entry: create all needed subtree and add new leaf */
1150 err = ext4_ext_split(handle, inode, flags, path, newext, i);
1155 ext4_ext_drop_refs(path);
1156 path = ext4_ext_find_extent(inode,
1157 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1160 err = PTR_ERR(path);
1162 /* tree is full, time to grow in depth */
1163 err = ext4_ext_grow_indepth(handle, inode, flags, newext);
1168 ext4_ext_drop_refs(path);
1169 path = ext4_ext_find_extent(inode,
1170 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1173 err = PTR_ERR(path);
1178 * only first (depth 0 -> 1) produces free space;
1179 * in all other cases we have to split the grown tree
1181 depth = ext_depth(inode);
1182 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
1183 /* now we need to split */
1193 * search the closest allocated block to the left for *logical
1194 * and returns it at @logical + it's physical address at @phys
1195 * if *logical is the smallest allocated block, the function
1196 * returns 0 at @phys
1197 * return value contains 0 (success) or error code
1199 static int ext4_ext_search_left(struct inode *inode,
1200 struct ext4_ext_path *path,
1201 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1203 struct ext4_extent_idx *ix;
1204 struct ext4_extent *ex;
1207 if (unlikely(path == NULL)) {
1208 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1211 depth = path->p_depth;
1214 if (depth == 0 && path->p_ext == NULL)
1217 /* usually extent in the path covers blocks smaller
1218 * then *logical, but it can be that extent is the
1219 * first one in the file */
1221 ex = path[depth].p_ext;
1222 ee_len = ext4_ext_get_actual_len(ex);
1223 if (*logical < le32_to_cpu(ex->ee_block)) {
1224 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1225 EXT4_ERROR_INODE(inode,
1226 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1227 *logical, le32_to_cpu(ex->ee_block));
1230 while (--depth >= 0) {
1231 ix = path[depth].p_idx;
1232 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1233 EXT4_ERROR_INODE(inode,
1234 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
1235 ix != NULL ? le32_to_cpu(ix->ei_block) : 0,
1236 EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
1237 le32_to_cpu(EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block) : 0,
1245 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1246 EXT4_ERROR_INODE(inode,
1247 "logical %d < ee_block %d + ee_len %d!",
1248 *logical, le32_to_cpu(ex->ee_block), ee_len);
1252 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
1253 *phys = ext4_ext_pblock(ex) + ee_len - 1;
1258 * search the closest allocated block to the right for *logical
1259 * and returns it at @logical + it's physical address at @phys
1260 * if *logical is the largest allocated block, the function
1261 * returns 0 at @phys
1262 * return value contains 0 (success) or error code
1264 static int ext4_ext_search_right(struct inode *inode,
1265 struct ext4_ext_path *path,
1266 ext4_lblk_t *logical, ext4_fsblk_t *phys,
1267 struct ext4_extent **ret_ex)
1269 struct buffer_head *bh = NULL;
1270 struct ext4_extent_header *eh;
1271 struct ext4_extent_idx *ix;
1272 struct ext4_extent *ex;
1274 int depth; /* Note, NOT eh_depth; depth from top of tree */
1277 if (unlikely(path == NULL)) {
1278 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1281 depth = path->p_depth;
1284 if (depth == 0 && path->p_ext == NULL)
1287 /* usually extent in the path covers blocks smaller
1288 * then *logical, but it can be that extent is the
1289 * first one in the file */
1291 ex = path[depth].p_ext;
1292 ee_len = ext4_ext_get_actual_len(ex);
1293 if (*logical < le32_to_cpu(ex->ee_block)) {
1294 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1295 EXT4_ERROR_INODE(inode,
1296 "first_extent(path[%d].p_hdr) != ex",
1300 while (--depth >= 0) {
1301 ix = path[depth].p_idx;
1302 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1303 EXT4_ERROR_INODE(inode,
1304 "ix != EXT_FIRST_INDEX *logical %d!",
1312 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1313 EXT4_ERROR_INODE(inode,
1314 "logical %d < ee_block %d + ee_len %d!",
1315 *logical, le32_to_cpu(ex->ee_block), ee_len);
1319 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1320 /* next allocated block in this leaf */
1325 /* go up and search for index to the right */
1326 while (--depth >= 0) {
1327 ix = path[depth].p_idx;
1328 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
1332 /* we've gone up to the root and found no index to the right */
1336 /* we've found index to the right, let's
1337 * follow it and find the closest allocated
1338 * block to the right */
1340 block = ext4_idx_pblock(ix);
1341 while (++depth < path->p_depth) {
1342 bh = sb_bread(inode->i_sb, block);
1345 eh = ext_block_hdr(bh);
1346 /* subtract from p_depth to get proper eh_depth */
1347 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
1351 ix = EXT_FIRST_INDEX(eh);
1352 block = ext4_idx_pblock(ix);
1356 bh = sb_bread(inode->i_sb, block);
1359 eh = ext_block_hdr(bh);
1360 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
1364 ex = EXT_FIRST_EXTENT(eh);
1366 *logical = le32_to_cpu(ex->ee_block);
1367 *phys = ext4_ext_pblock(ex);
1375 * ext4_ext_next_allocated_block:
1376 * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
1377 * NOTE: it considers block number from index entry as
1378 * allocated block. Thus, index entries have to be consistent
1382 ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1386 BUG_ON(path == NULL);
1387 depth = path->p_depth;
1389 if (depth == 0 && path->p_ext == NULL)
1390 return EXT_MAX_BLOCKS;
1392 while (depth >= 0) {
1393 if (depth == path->p_depth) {
1395 if (path[depth].p_ext &&
1396 path[depth].p_ext !=
1397 EXT_LAST_EXTENT(path[depth].p_hdr))
1398 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1401 if (path[depth].p_idx !=
1402 EXT_LAST_INDEX(path[depth].p_hdr))
1403 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1408 return EXT_MAX_BLOCKS;
1412 * ext4_ext_next_leaf_block:
1413 * returns first allocated block from next leaf or EXT_MAX_BLOCKS
1415 static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path)
1419 BUG_ON(path == NULL);
1420 depth = path->p_depth;
1422 /* zero-tree has no leaf blocks at all */
1424 return EXT_MAX_BLOCKS;
1426 /* go to index block */
1429 while (depth >= 0) {
1430 if (path[depth].p_idx !=
1431 EXT_LAST_INDEX(path[depth].p_hdr))
1432 return (ext4_lblk_t)
1433 le32_to_cpu(path[depth].p_idx[1].ei_block);
1437 return EXT_MAX_BLOCKS;
1441 * ext4_ext_correct_indexes:
1442 * if leaf gets modified and modified extent is first in the leaf,
1443 * then we have to correct all indexes above.
1444 * TODO: do we need to correct tree in all cases?
1446 static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
1447 struct ext4_ext_path *path)
1449 struct ext4_extent_header *eh;
1450 int depth = ext_depth(inode);
1451 struct ext4_extent *ex;
1455 eh = path[depth].p_hdr;
1456 ex = path[depth].p_ext;
1458 if (unlikely(ex == NULL || eh == NULL)) {
1459 EXT4_ERROR_INODE(inode,
1460 "ex %p == NULL or eh %p == NULL", ex, eh);
1465 /* there is no tree at all */
1469 if (ex != EXT_FIRST_EXTENT(eh)) {
1470 /* we correct tree if first leaf got modified only */
1475 * TODO: we need correction if border is smaller than current one
1478 border = path[depth].p_ext->ee_block;
1479 err = ext4_ext_get_access(handle, inode, path + k);
1482 path[k].p_idx->ei_block = border;
1483 err = ext4_ext_dirty(handle, inode, path + k);
1488 /* change all left-side indexes */
1489 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1491 err = ext4_ext_get_access(handle, inode, path + k);
1494 path[k].p_idx->ei_block = border;
1495 err = ext4_ext_dirty(handle, inode, path + k);
1504 ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1505 struct ext4_extent *ex2)
1507 unsigned short ext1_ee_len, ext2_ee_len, max_len;
1510 * Make sure that either both extents are uninitialized, or
1513 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1516 if (ext4_ext_is_uninitialized(ex1))
1517 max_len = EXT_UNINIT_MAX_LEN;
1519 max_len = EXT_INIT_MAX_LEN;
1521 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1522 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1524 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
1525 le32_to_cpu(ex2->ee_block))
1529 * To allow future support for preallocated extents to be added
1530 * as an RO_COMPAT feature, refuse to merge to extents if
1531 * this can result in the top bit of ee_len being set.
1533 if (ext1_ee_len + ext2_ee_len > max_len)
1535 #ifdef AGGRESSIVE_TEST
1536 if (ext1_ee_len >= 4)
1540 if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
1546 * This function tries to merge the "ex" extent to the next extent in the tree.
1547 * It always tries to merge towards right. If you want to merge towards
1548 * left, pass "ex - 1" as argument instead of "ex".
1549 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1550 * 1 if they got merged.
1552 static int ext4_ext_try_to_merge_right(struct inode *inode,
1553 struct ext4_ext_path *path,
1554 struct ext4_extent *ex)
1556 struct ext4_extent_header *eh;
1557 unsigned int depth, len;
1559 int uninitialized = 0;
1561 depth = ext_depth(inode);
1562 BUG_ON(path[depth].p_hdr == NULL);
1563 eh = path[depth].p_hdr;
1565 while (ex < EXT_LAST_EXTENT(eh)) {
1566 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1568 /* merge with next extent! */
1569 if (ext4_ext_is_uninitialized(ex))
1571 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1572 + ext4_ext_get_actual_len(ex + 1));
1574 ext4_ext_mark_uninitialized(ex);
1576 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1577 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1578 * sizeof(struct ext4_extent);
1579 memmove(ex + 1, ex + 2, len);
1581 le16_add_cpu(&eh->eh_entries, -1);
1583 WARN_ON(eh->eh_entries == 0);
1584 if (!eh->eh_entries)
1585 EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
1592 * This function tries to merge the @ex extent to neighbours in the tree.
1593 * return 1 if merge left else 0.
1595 static int ext4_ext_try_to_merge(struct inode *inode,
1596 struct ext4_ext_path *path,
1597 struct ext4_extent *ex) {
1598 struct ext4_extent_header *eh;
1603 depth = ext_depth(inode);
1604 BUG_ON(path[depth].p_hdr == NULL);
1605 eh = path[depth].p_hdr;
1607 if (ex > EXT_FIRST_EXTENT(eh))
1608 merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
1611 ret = ext4_ext_try_to_merge_right(inode, path, ex);
1617 * check if a portion of the "newext" extent overlaps with an
1620 * If there is an overlap discovered, it updates the length of the newext
1621 * such that there will be no overlap, and then returns 1.
1622 * If there is no overlap found, it returns 0.
1624 static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi,
1625 struct inode *inode,
1626 struct ext4_extent *newext,
1627 struct ext4_ext_path *path)
1630 unsigned int depth, len1;
1631 unsigned int ret = 0;
1633 b1 = le32_to_cpu(newext->ee_block);
1634 len1 = ext4_ext_get_actual_len(newext);
1635 depth = ext_depth(inode);
1636 if (!path[depth].p_ext)
1638 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
1639 b2 &= ~(sbi->s_cluster_ratio - 1);
1642 * get the next allocated block if the extent in the path
1643 * is before the requested block(s)
1646 b2 = ext4_ext_next_allocated_block(path);
1647 if (b2 == EXT_MAX_BLOCKS)
1649 b2 &= ~(sbi->s_cluster_ratio - 1);
1652 /* check for wrap through zero on extent logical start block*/
1653 if (b1 + len1 < b1) {
1654 len1 = EXT_MAX_BLOCKS - b1;
1655 newext->ee_len = cpu_to_le16(len1);
1659 /* check for overlap */
1660 if (b1 + len1 > b2) {
1661 newext->ee_len = cpu_to_le16(b2 - b1);
1669 * ext4_ext_insert_extent:
1670 * tries to merge requsted extent into the existing extent or
1671 * inserts requested extent as new one into the tree,
1672 * creating new leaf in the no-space case.
1674 int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1675 struct ext4_ext_path *path,
1676 struct ext4_extent *newext, int flag)
1678 struct ext4_extent_header *eh;
1679 struct ext4_extent *ex, *fex;
1680 struct ext4_extent *nearex; /* nearest extent */
1681 struct ext4_ext_path *npath = NULL;
1682 int depth, len, err;
1684 unsigned uninitialized = 0;
1687 if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1688 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
1691 depth = ext_depth(inode);
1692 ex = path[depth].p_ext;
1693 if (unlikely(path[depth].p_hdr == NULL)) {
1694 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1698 /* try to insert block into found extent and return */
1699 if (ex && !(flag & EXT4_GET_BLOCKS_PRE_IO)
1700 && ext4_can_extents_be_merged(inode, ex, newext)) {
1701 ext_debug("append [%d]%d block to %d:[%d]%d (from %llu)\n",
1702 ext4_ext_is_uninitialized(newext),
1703 ext4_ext_get_actual_len(newext),
1704 le32_to_cpu(ex->ee_block),
1705 ext4_ext_is_uninitialized(ex),
1706 ext4_ext_get_actual_len(ex),
1707 ext4_ext_pblock(ex));
1708 err = ext4_ext_get_access(handle, inode, path + depth);
1713 * ext4_can_extents_be_merged should have checked that either
1714 * both extents are uninitialized, or both aren't. Thus we
1715 * need to check only one of them here.
1717 if (ext4_ext_is_uninitialized(ex))
1719 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1720 + ext4_ext_get_actual_len(newext));
1722 ext4_ext_mark_uninitialized(ex);
1723 eh = path[depth].p_hdr;
1728 depth = ext_depth(inode);
1729 eh = path[depth].p_hdr;
1730 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1733 /* probably next leaf has space for us? */
1734 fex = EXT_LAST_EXTENT(eh);
1735 next = EXT_MAX_BLOCKS;
1736 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block))
1737 next = ext4_ext_next_leaf_block(path);
1738 if (next != EXT_MAX_BLOCKS) {
1739 ext_debug("next leaf block - %d\n", next);
1740 BUG_ON(npath != NULL);
1741 npath = ext4_ext_find_extent(inode, next, NULL);
1743 return PTR_ERR(npath);
1744 BUG_ON(npath->p_depth != path->p_depth);
1745 eh = npath[depth].p_hdr;
1746 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
1747 ext_debug("next leaf isn't full(%d)\n",
1748 le16_to_cpu(eh->eh_entries));
1752 ext_debug("next leaf has no free space(%d,%d)\n",
1753 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1757 * There is no free space in the found leaf.
1758 * We're gonna add a new leaf in the tree.
1760 if (flag & EXT4_GET_BLOCKS_PUNCH_OUT_EXT)
1761 flags = EXT4_MB_USE_ROOT_BLOCKS;
1762 err = ext4_ext_create_new_leaf(handle, inode, flags, path, newext);
1765 depth = ext_depth(inode);
1766 eh = path[depth].p_hdr;
1769 nearex = path[depth].p_ext;
1771 err = ext4_ext_get_access(handle, inode, path + depth);
1776 /* there is no extent in this leaf, create first one */
1777 ext_debug("first extent in the leaf: %d:%llu:[%d]%d\n",
1778 le32_to_cpu(newext->ee_block),
1779 ext4_ext_pblock(newext),
1780 ext4_ext_is_uninitialized(newext),
1781 ext4_ext_get_actual_len(newext));
1782 path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1783 } else if (le32_to_cpu(newext->ee_block)
1784 > le32_to_cpu(nearex->ee_block)) {
1785 /* BUG_ON(newext->ee_block == nearex->ee_block); */
1786 if (nearex != EXT_LAST_EXTENT(eh)) {
1787 len = EXT_MAX_EXTENT(eh) - nearex;
1788 len = (len - 1) * sizeof(struct ext4_extent);
1789 len = len < 0 ? 0 : len;
1790 ext_debug("insert %d:%llu:[%d]%d after: nearest 0x%p, "
1791 "move %d from 0x%p to 0x%p\n",
1792 le32_to_cpu(newext->ee_block),
1793 ext4_ext_pblock(newext),
1794 ext4_ext_is_uninitialized(newext),
1795 ext4_ext_get_actual_len(newext),
1796 nearex, len, nearex + 1, nearex + 2);
1797 memmove(nearex + 2, nearex + 1, len);
1799 path[depth].p_ext = nearex + 1;
1801 BUG_ON(newext->ee_block == nearex->ee_block);
1802 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1803 len = len < 0 ? 0 : len;
1804 ext_debug("insert %d:%llu:[%d]%d before: nearest 0x%p, "
1805 "move %d from 0x%p to 0x%p\n",
1806 le32_to_cpu(newext->ee_block),
1807 ext4_ext_pblock(newext),
1808 ext4_ext_is_uninitialized(newext),
1809 ext4_ext_get_actual_len(newext),
1810 nearex, len, nearex, nearex + 1);
1811 memmove(nearex + 1, nearex, len);
1812 path[depth].p_ext = nearex;
1815 le16_add_cpu(&eh->eh_entries, 1);
1816 nearex = path[depth].p_ext;
1817 nearex->ee_block = newext->ee_block;
1818 ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
1819 nearex->ee_len = newext->ee_len;
1822 /* try to merge extents to the right */
1823 if (!(flag & EXT4_GET_BLOCKS_PRE_IO))
1824 ext4_ext_try_to_merge(inode, path, nearex);
1826 /* try to merge extents to the left */
1828 /* time to correct all indexes above */
1829 err = ext4_ext_correct_indexes(handle, inode, path);
1833 err = ext4_ext_dirty(handle, inode, path + depth);
1837 ext4_ext_drop_refs(npath);
1840 ext4_ext_invalidate_cache(inode);
1844 static int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block,
1845 ext4_lblk_t num, ext_prepare_callback func,
1848 struct ext4_ext_path *path = NULL;
1849 struct ext4_ext_cache cbex;
1850 struct ext4_extent *ex;
1851 ext4_lblk_t next, start = 0, end = 0;
1852 ext4_lblk_t last = block + num;
1853 int depth, exists, err = 0;
1855 BUG_ON(func == NULL);
1856 BUG_ON(inode == NULL);
1858 while (block < last && block != EXT_MAX_BLOCKS) {
1860 /* find extent for this block */
1861 down_read(&EXT4_I(inode)->i_data_sem);
1862 path = ext4_ext_find_extent(inode, block, path);
1863 up_read(&EXT4_I(inode)->i_data_sem);
1865 err = PTR_ERR(path);
1870 depth = ext_depth(inode);
1871 if (unlikely(path[depth].p_hdr == NULL)) {
1872 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1876 ex = path[depth].p_ext;
1877 next = ext4_ext_next_allocated_block(path);
1881 /* there is no extent yet, so try to allocate
1882 * all requested space */
1885 } else if (le32_to_cpu(ex->ee_block) > block) {
1886 /* need to allocate space before found extent */
1888 end = le32_to_cpu(ex->ee_block);
1889 if (block + num < end)
1891 } else if (block >= le32_to_cpu(ex->ee_block)
1892 + ext4_ext_get_actual_len(ex)) {
1893 /* need to allocate space after found extent */
1898 } else if (block >= le32_to_cpu(ex->ee_block)) {
1900 * some part of requested space is covered
1904 end = le32_to_cpu(ex->ee_block)
1905 + ext4_ext_get_actual_len(ex);
1906 if (block + num < end)
1912 BUG_ON(end <= start);
1915 cbex.ec_block = start;
1916 cbex.ec_len = end - start;
1919 cbex.ec_block = le32_to_cpu(ex->ee_block);
1920 cbex.ec_len = ext4_ext_get_actual_len(ex);
1921 cbex.ec_start = ext4_ext_pblock(ex);
1924 if (unlikely(cbex.ec_len == 0)) {
1925 EXT4_ERROR_INODE(inode, "cbex.ec_len == 0");
1929 err = func(inode, next, &cbex, ex, cbdata);
1930 ext4_ext_drop_refs(path);
1935 if (err == EXT_REPEAT)
1937 else if (err == EXT_BREAK) {
1942 if (ext_depth(inode) != depth) {
1943 /* depth was changed. we have to realloc path */
1948 block = cbex.ec_block + cbex.ec_len;
1952 ext4_ext_drop_refs(path);
1960 ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
1961 __u32 len, ext4_fsblk_t start)
1963 struct ext4_ext_cache *cex;
1965 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1966 trace_ext4_ext_put_in_cache(inode, block, len, start);
1967 cex = &EXT4_I(inode)->i_cached_extent;
1968 cex->ec_block = block;
1970 cex->ec_start = start;
1971 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1975 * ext4_ext_put_gap_in_cache:
1976 * calculate boundaries of the gap that the requested block fits into
1977 * and cache this gap
1980 ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
1983 int depth = ext_depth(inode);
1986 struct ext4_extent *ex;
1988 ex = path[depth].p_ext;
1990 /* there is no extent yet, so gap is [0;-] */
1992 len = EXT_MAX_BLOCKS;
1993 ext_debug("cache gap(whole file):");
1994 } else if (block < le32_to_cpu(ex->ee_block)) {
1996 len = le32_to_cpu(ex->ee_block) - block;
1997 ext_debug("cache gap(before): %u [%u:%u]",
1999 le32_to_cpu(ex->ee_block),
2000 ext4_ext_get_actual_len(ex));
2001 } else if (block >= le32_to_cpu(ex->ee_block)
2002 + ext4_ext_get_actual_len(ex)) {
2004 lblock = le32_to_cpu(ex->ee_block)
2005 + ext4_ext_get_actual_len(ex);
2007 next = ext4_ext_next_allocated_block(path);
2008 ext_debug("cache gap(after): [%u:%u] %u",
2009 le32_to_cpu(ex->ee_block),
2010 ext4_ext_get_actual_len(ex),
2012 BUG_ON(next == lblock);
2013 len = next - lblock;
2019 ext_debug(" -> %u:%lu\n", lblock, len);
2020 ext4_ext_put_in_cache(inode, lblock, len, 0);
2024 * ext4_ext_check_cache()
2025 * Checks to see if the given block is in the cache.
2026 * If it is, the cached extent is stored in the given
2027 * cache extent pointer. If the cached extent is a hole,
2028 * this routine should be used instead of
2029 * ext4_ext_in_cache if the calling function needs to
2030 * know the size of the hole.
2032 * @inode: The files inode
2033 * @block: The block to look for in the cache
2034 * @ex: Pointer where the cached extent will be stored
2035 * if it contains block
2037 * Return 0 if cache is invalid; 1 if the cache is valid
2039 static int ext4_ext_check_cache(struct inode *inode, ext4_lblk_t block,
2040 struct ext4_ext_cache *ex){
2041 struct ext4_ext_cache *cex;
2042 struct ext4_sb_info *sbi;
2046 * We borrow i_block_reservation_lock to protect i_cached_extent
2048 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
2049 cex = &EXT4_I(inode)->i_cached_extent;
2050 sbi = EXT4_SB(inode->i_sb);
2052 /* has cache valid data? */
2053 if (cex->ec_len == 0)
2056 if (in_range(block, cex->ec_block, cex->ec_len)) {
2057 memcpy(ex, cex, sizeof(struct ext4_ext_cache));
2058 ext_debug("%u cached by %u:%u:%llu\n",
2060 cex->ec_block, cex->ec_len, cex->ec_start);
2065 sbi->extent_cache_misses++;
2067 sbi->extent_cache_hits++;
2068 trace_ext4_ext_in_cache(inode, block, ret);
2069 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
2074 * ext4_ext_in_cache()
2075 * Checks to see if the given block is in the cache.
2076 * If it is, the cached extent is stored in the given
2079 * @inode: The files inode
2080 * @block: The block to look for in the cache
2081 * @ex: Pointer where the cached extent will be stored
2082 * if it contains block
2084 * Return 0 if cache is invalid; 1 if the cache is valid
2087 ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
2088 struct ext4_extent *ex)
2090 struct ext4_ext_cache cex;
2093 if (ext4_ext_check_cache(inode, block, &cex)) {
2094 ex->ee_block = cpu_to_le32(cex.ec_block);
2095 ext4_ext_store_pblock(ex, cex.ec_start);
2096 ex->ee_len = cpu_to_le16(cex.ec_len);
2106 * removes index from the index block.
2108 static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
2109 struct ext4_ext_path *path)
2114 /* free index block */
2116 leaf = ext4_idx_pblock(path->p_idx);
2117 if (unlikely(path->p_hdr->eh_entries == 0)) {
2118 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
2121 err = ext4_ext_get_access(handle, inode, path);
2125 if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) {
2126 int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx;
2127 len *= sizeof(struct ext4_extent_idx);
2128 memmove(path->p_idx, path->p_idx + 1, len);
2131 le16_add_cpu(&path->p_hdr->eh_entries, -1);
2132 err = ext4_ext_dirty(handle, inode, path);
2135 ext_debug("index is empty, remove it, free block %llu\n", leaf);
2136 trace_ext4_ext_rm_idx(inode, leaf);
2138 ext4_free_blocks(handle, inode, NULL, leaf, 1,
2139 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
2144 * ext4_ext_calc_credits_for_single_extent:
2145 * This routine returns max. credits that needed to insert an extent
2146 * to the extent tree.
2147 * When pass the actual path, the caller should calculate credits
2150 int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
2151 struct ext4_ext_path *path)
2154 int depth = ext_depth(inode);
2157 /* probably there is space in leaf? */
2158 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
2159 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2162 * There are some space in the leaf tree, no
2163 * need to account for leaf block credit
2165 * bitmaps and block group descriptor blocks
2166 * and other metadata blocks still need to be
2169 /* 1 bitmap, 1 block group descriptor */
2170 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
2175 return ext4_chunk_trans_blocks(inode, nrblocks);
2179 * How many index/leaf blocks need to change/allocate to modify nrblocks?
2181 * if nrblocks are fit in a single extent (chunk flag is 1), then
2182 * in the worse case, each tree level index/leaf need to be changed
2183 * if the tree split due to insert a new extent, then the old tree
2184 * index/leaf need to be updated too
2186 * If the nrblocks are discontiguous, they could cause
2187 * the whole tree split more than once, but this is really rare.
2189 int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
2192 int depth = ext_depth(inode);
2202 static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
2203 struct ext4_extent *ex,
2204 ext4_fsblk_t *partial_cluster,
2205 ext4_lblk_t from, ext4_lblk_t to)
2207 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2208 unsigned short ee_len = ext4_ext_get_actual_len(ex);
2210 int flags = EXT4_FREE_BLOCKS_FORGET;
2212 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2213 flags |= EXT4_FREE_BLOCKS_METADATA;
2215 * For bigalloc file systems, we never free a partial cluster
2216 * at the beginning of the extent. Instead, we make a note
2217 * that we tried freeing the cluster, and check to see if we
2218 * need to free it on a subsequent call to ext4_remove_blocks,
2219 * or at the end of the ext4_truncate() operation.
2221 flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER;
2223 trace_ext4_remove_blocks(inode, ex, from, to, *partial_cluster);
2225 * If we have a partial cluster, and it's different from the
2226 * cluster of the last block, we need to explicitly free the
2227 * partial cluster here.
2229 pblk = ext4_ext_pblock(ex) + ee_len - 1;
2230 if (*partial_cluster && (EXT4_B2C(sbi, pblk) != *partial_cluster)) {
2231 ext4_free_blocks(handle, inode, NULL,
2232 EXT4_C2B(sbi, *partial_cluster),
2233 sbi->s_cluster_ratio, flags);
2234 *partial_cluster = 0;
2237 #ifdef EXTENTS_STATS
2239 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2240 spin_lock(&sbi->s_ext_stats_lock);
2241 sbi->s_ext_blocks += ee_len;
2242 sbi->s_ext_extents++;
2243 if (ee_len < sbi->s_ext_min)
2244 sbi->s_ext_min = ee_len;
2245 if (ee_len > sbi->s_ext_max)
2246 sbi->s_ext_max = ee_len;
2247 if (ext_depth(inode) > sbi->s_depth_max)
2248 sbi->s_depth_max = ext_depth(inode);
2249 spin_unlock(&sbi->s_ext_stats_lock);
2252 if (from >= le32_to_cpu(ex->ee_block)
2253 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
2257 num = le32_to_cpu(ex->ee_block) + ee_len - from;
2258 pblk = ext4_ext_pblock(ex) + ee_len - num;
2259 ext_debug("free last %u blocks starting %llu\n", num, pblk);
2260 ext4_free_blocks(handle, inode, NULL, pblk, num, flags);
2262 * If the block range to be freed didn't start at the
2263 * beginning of a cluster, and we removed the entire
2264 * extent, save the partial cluster here, since we
2265 * might need to delete if we determine that the
2266 * truncate operation has removed all of the blocks in
2269 if (pblk & (sbi->s_cluster_ratio - 1) &&
2271 *partial_cluster = EXT4_B2C(sbi, pblk);
2273 *partial_cluster = 0;
2274 } else if (from == le32_to_cpu(ex->ee_block)
2275 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
2281 start = ext4_ext_pblock(ex);
2283 ext_debug("free first %u blocks starting %llu\n", num, start);
2284 ext4_free_blocks(handle, inode, NULL, start, num, flags);
2287 printk(KERN_INFO "strange request: removal(2) "
2288 "%u-%u from %u:%u\n",
2289 from, to, le32_to_cpu(ex->ee_block), ee_len);
2296 * ext4_ext_rm_leaf() Removes the extents associated with the
2297 * blocks appearing between "start" and "end", and splits the extents
2298 * if "start" and "end" appear in the same extent
2300 * @handle: The journal handle
2301 * @inode: The files inode
2302 * @path: The path to the leaf
2303 * @start: The first block to remove
2304 * @end: The last block to remove
2307 ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
2308 struct ext4_ext_path *path, ext4_fsblk_t *partial_cluster,
2309 ext4_lblk_t start, ext4_lblk_t end)
2311 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2312 int err = 0, correct_index = 0;
2313 int depth = ext_depth(inode), credits;
2314 struct ext4_extent_header *eh;
2317 ext4_lblk_t ex_ee_block;
2318 unsigned short ex_ee_len;
2319 unsigned uninitialized = 0;
2320 struct ext4_extent *ex;
2322 /* the header must be checked already in ext4_ext_remove_space() */
2323 ext_debug("truncate since %u in leaf\n", start);
2324 if (!path[depth].p_hdr)
2325 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2326 eh = path[depth].p_hdr;
2327 if (unlikely(path[depth].p_hdr == NULL)) {
2328 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
2331 /* find where to start removing */
2332 ex = EXT_LAST_EXTENT(eh);
2334 ex_ee_block = le32_to_cpu(ex->ee_block);
2335 ex_ee_len = ext4_ext_get_actual_len(ex);
2337 trace_ext4_ext_rm_leaf(inode, start, ex, *partial_cluster);
2339 while (ex >= EXT_FIRST_EXTENT(eh) &&
2340 ex_ee_block + ex_ee_len > start) {
2342 if (ext4_ext_is_uninitialized(ex))
2347 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
2348 uninitialized, ex_ee_len);
2349 path[depth].p_ext = ex;
2351 a = ex_ee_block > start ? ex_ee_block : start;
2352 b = ex_ee_block+ex_ee_len - 1 < end ?
2353 ex_ee_block+ex_ee_len - 1 : end;
2355 ext_debug(" border %u:%u\n", a, b);
2357 /* If this extent is beyond the end of the hole, skip it */
2358 if (end <= ex_ee_block) {
2360 ex_ee_block = le32_to_cpu(ex->ee_block);
2361 ex_ee_len = ext4_ext_get_actual_len(ex);
2363 } else if (b != ex_ee_block + ex_ee_len - 1) {
2364 EXT4_ERROR_INODE(inode," bad truncate %u:%u\n",
2368 } else if (a != ex_ee_block) {
2369 /* remove tail of the extent */
2370 num = a - ex_ee_block;
2372 /* remove whole extent: excellent! */
2376 * 3 for leaf, sb, and inode plus 2 (bmap and group
2377 * descriptor) for each block group; assume two block
2378 * groups plus ex_ee_len/blocks_per_block_group for
2381 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
2382 if (ex == EXT_FIRST_EXTENT(eh)) {
2384 credits += (ext_depth(inode)) + 1;
2386 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
2388 err = ext4_ext_truncate_extend_restart(handle, inode, credits);
2392 err = ext4_ext_get_access(handle, inode, path + depth);
2396 err = ext4_remove_blocks(handle, inode, ex, partial_cluster,
2402 /* this extent is removed; mark slot entirely unused */
2403 ext4_ext_store_pblock(ex, 0);
2405 ex->ee_len = cpu_to_le16(num);
2407 * Do not mark uninitialized if all the blocks in the
2408 * extent have been removed.
2410 if (uninitialized && num)
2411 ext4_ext_mark_uninitialized(ex);
2413 * If the extent was completely released,
2414 * we need to remove it from the leaf
2417 if (end != EXT_MAX_BLOCKS - 1) {
2419 * For hole punching, we need to scoot all the
2420 * extents up when an extent is removed so that
2421 * we dont have blank extents in the middle
2423 memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) *
2424 sizeof(struct ext4_extent));
2426 /* Now get rid of the one at the end */
2427 memset(EXT_LAST_EXTENT(eh), 0,
2428 sizeof(struct ext4_extent));
2430 le16_add_cpu(&eh->eh_entries, -1);
2432 *partial_cluster = 0;
2434 err = ext4_ext_dirty(handle, inode, path + depth);
2438 ext_debug("new extent: %u:%u:%llu\n", block, num,
2439 ext4_ext_pblock(ex));
2441 ex_ee_block = le32_to_cpu(ex->ee_block);
2442 ex_ee_len = ext4_ext_get_actual_len(ex);
2445 if (correct_index && eh->eh_entries)
2446 err = ext4_ext_correct_indexes(handle, inode, path);
2449 * If there is still a entry in the leaf node, check to see if
2450 * it references the partial cluster. This is the only place
2451 * where it could; if it doesn't, we can free the cluster.
2453 if (*partial_cluster && ex >= EXT_FIRST_EXTENT(eh) &&
2454 (EXT4_B2C(sbi, ext4_ext_pblock(ex) + ex_ee_len - 1) !=
2455 *partial_cluster)) {
2456 int flags = EXT4_FREE_BLOCKS_FORGET;
2458 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2459 flags |= EXT4_FREE_BLOCKS_METADATA;
2461 ext4_free_blocks(handle, inode, NULL,
2462 EXT4_C2B(sbi, *partial_cluster),
2463 sbi->s_cluster_ratio, flags);
2464 *partial_cluster = 0;
2467 /* if this leaf is free, then we should
2468 * remove it from index block above */
2469 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
2470 err = ext4_ext_rm_idx(handle, inode, path + depth);
2477 * ext4_ext_more_to_rm:
2478 * returns 1 if current index has to be freed (even partial)
2481 ext4_ext_more_to_rm(struct ext4_ext_path *path)
2483 BUG_ON(path->p_idx == NULL);
2485 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2489 * if truncate on deeper level happened, it wasn't partial,
2490 * so we have to consider current index for truncation
2492 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2497 static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
2499 struct super_block *sb = inode->i_sb;
2500 int depth = ext_depth(inode);
2501 struct ext4_ext_path *path;
2502 ext4_fsblk_t partial_cluster = 0;
2506 ext_debug("truncate since %u\n", start);
2508 /* probably first extent we're gonna free will be last in block */
2509 handle = ext4_journal_start(inode, depth + 1);
2511 return PTR_ERR(handle);
2514 ext4_ext_invalidate_cache(inode);
2516 trace_ext4_ext_remove_space(inode, start, depth);
2519 * We start scanning from right side, freeing all the blocks
2520 * after i_size and walking into the tree depth-wise.
2522 depth = ext_depth(inode);
2523 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
2525 ext4_journal_stop(handle);
2528 path[0].p_depth = depth;
2529 path[0].p_hdr = ext_inode_hdr(inode);
2530 if (ext4_ext_check(inode, path[0].p_hdr, depth)) {
2536 while (i >= 0 && err == 0) {
2538 /* this is leaf block */
2539 err = ext4_ext_rm_leaf(handle, inode, path,
2540 &partial_cluster, start,
2541 EXT_MAX_BLOCKS - 1);
2542 /* root level has p_bh == NULL, brelse() eats this */
2543 brelse(path[i].p_bh);
2544 path[i].p_bh = NULL;
2549 /* this is index block */
2550 if (!path[i].p_hdr) {
2551 ext_debug("initialize header\n");
2552 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
2555 if (!path[i].p_idx) {
2556 /* this level hasn't been touched yet */
2557 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2558 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2559 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2561 le16_to_cpu(path[i].p_hdr->eh_entries));
2563 /* we were already here, see at next index */
2567 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2568 i, EXT_FIRST_INDEX(path[i].p_hdr),
2570 if (ext4_ext_more_to_rm(path + i)) {
2571 struct buffer_head *bh;
2572 /* go to the next level */
2573 ext_debug("move to level %d (block %llu)\n",
2574 i + 1, ext4_idx_pblock(path[i].p_idx));
2575 memset(path + i + 1, 0, sizeof(*path));
2576 bh = sb_bread(sb, ext4_idx_pblock(path[i].p_idx));
2578 /* should we reset i_size? */
2582 if (WARN_ON(i + 1 > depth)) {
2586 if (ext4_ext_check(inode, ext_block_hdr(bh),
2591 path[i + 1].p_bh = bh;
2593 /* save actual number of indexes since this
2594 * number is changed at the next iteration */
2595 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2598 /* we finished processing this index, go up */
2599 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
2600 /* index is empty, remove it;
2601 * handle must be already prepared by the
2602 * truncatei_leaf() */
2603 err = ext4_ext_rm_idx(handle, inode, path + i);
2605 /* root level has p_bh == NULL, brelse() eats this */
2606 brelse(path[i].p_bh);
2607 path[i].p_bh = NULL;
2609 ext_debug("return to level %d\n", i);
2613 trace_ext4_ext_remove_space_done(inode, start, depth, partial_cluster,
2614 path->p_hdr->eh_entries);
2616 /* If we still have something in the partial cluster and we have removed
2617 * even the first extent, then we should free the blocks in the partial
2618 * cluster as well. */
2619 if (partial_cluster && path->p_hdr->eh_entries == 0) {
2620 int flags = EXT4_FREE_BLOCKS_FORGET;
2622 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2623 flags |= EXT4_FREE_BLOCKS_METADATA;
2625 ext4_free_blocks(handle, inode, NULL,
2626 EXT4_C2B(EXT4_SB(sb), partial_cluster),
2627 EXT4_SB(sb)->s_cluster_ratio, flags);
2628 partial_cluster = 0;
2631 /* TODO: flexible tree reduction should be here */
2632 if (path->p_hdr->eh_entries == 0) {
2634 * truncate to zero freed all the tree,
2635 * so we need to correct eh_depth
2637 err = ext4_ext_get_access(handle, inode, path);
2639 ext_inode_hdr(inode)->eh_depth = 0;
2640 ext_inode_hdr(inode)->eh_max =
2641 cpu_to_le16(ext4_ext_space_root(inode, 0));
2642 err = ext4_ext_dirty(handle, inode, path);
2646 ext4_ext_drop_refs(path);
2650 ext4_journal_stop(handle);
2656 * called at mount time
2658 void ext4_ext_init(struct super_block *sb)
2661 * possible initialization would be here
2664 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
2665 #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
2666 printk(KERN_INFO "EXT4-fs: file extents enabled");
2667 #ifdef AGGRESSIVE_TEST
2668 printk(", aggressive tests");
2670 #ifdef CHECK_BINSEARCH
2671 printk(", check binsearch");
2673 #ifdef EXTENTS_STATS
2678 #ifdef EXTENTS_STATS
2679 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2680 EXT4_SB(sb)->s_ext_min = 1 << 30;
2681 EXT4_SB(sb)->s_ext_max = 0;
2687 * called at umount time
2689 void ext4_ext_release(struct super_block *sb)
2691 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
2694 #ifdef EXTENTS_STATS
2695 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2696 struct ext4_sb_info *sbi = EXT4_SB(sb);
2697 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2698 sbi->s_ext_blocks, sbi->s_ext_extents,
2699 sbi->s_ext_blocks / sbi->s_ext_extents);
2700 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2701 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2706 /* FIXME!! we need to try to merge to left or right after zero-out */
2707 static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2709 ext4_fsblk_t ee_pblock;
2710 unsigned int ee_len;
2713 ee_len = ext4_ext_get_actual_len(ex);
2714 ee_pblock = ext4_ext_pblock(ex);
2716 ret = sb_issue_zeroout(inode->i_sb, ee_pblock, ee_len, GFP_NOFS);
2724 * used by extent splitting.
2726 #define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \
2728 #define EXT4_EXT_MARK_UNINIT1 0x2 /* mark first half uninitialized */
2729 #define EXT4_EXT_MARK_UNINIT2 0x4 /* mark second half uninitialized */
2732 * ext4_split_extent_at() splits an extent at given block.
2734 * @handle: the journal handle
2735 * @inode: the file inode
2736 * @path: the path to the extent
2737 * @split: the logical block where the extent is splitted.
2738 * @split_flags: indicates if the extent could be zeroout if split fails, and
2739 * the states(init or uninit) of new extents.
2740 * @flags: flags used to insert new extent to extent tree.
2743 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
2744 * of which are deterimined by split_flag.
2746 * There are two cases:
2747 * a> the extent are splitted into two extent.
2748 * b> split is not needed, and just mark the extent.
2750 * return 0 on success.
2752 static int ext4_split_extent_at(handle_t *handle,
2753 struct inode *inode,
2754 struct ext4_ext_path *path,
2759 ext4_fsblk_t newblock;
2760 ext4_lblk_t ee_block;
2761 struct ext4_extent *ex, newex, orig_ex;
2762 struct ext4_extent *ex2 = NULL;
2763 unsigned int ee_len, depth;
2766 ext_debug("ext4_split_extents_at: inode %lu, logical"
2767 "block %llu\n", inode->i_ino, (unsigned long long)split);
2769 ext4_ext_show_leaf(inode, path);
2771 depth = ext_depth(inode);
2772 ex = path[depth].p_ext;
2773 ee_block = le32_to_cpu(ex->ee_block);
2774 ee_len = ext4_ext_get_actual_len(ex);
2775 newblock = split - ee_block + ext4_ext_pblock(ex);
2777 BUG_ON(split < ee_block || split >= (ee_block + ee_len));
2779 err = ext4_ext_get_access(handle, inode, path + depth);
2783 if (split == ee_block) {
2785 * case b: block @split is the block that the extent begins with
2786 * then we just change the state of the extent, and splitting
2789 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2790 ext4_ext_mark_uninitialized(ex);
2792 ext4_ext_mark_initialized(ex);
2794 if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
2795 ext4_ext_try_to_merge(inode, path, ex);
2797 err = ext4_ext_dirty(handle, inode, path + depth);
2802 memcpy(&orig_ex, ex, sizeof(orig_ex));
2803 ex->ee_len = cpu_to_le16(split - ee_block);
2804 if (split_flag & EXT4_EXT_MARK_UNINIT1)
2805 ext4_ext_mark_uninitialized(ex);
2808 * path may lead to new leaf, not to original leaf any more
2809 * after ext4_ext_insert_extent() returns,
2811 err = ext4_ext_dirty(handle, inode, path + depth);
2813 goto fix_extent_len;
2816 ex2->ee_block = cpu_to_le32(split);
2817 ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
2818 ext4_ext_store_pblock(ex2, newblock);
2819 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2820 ext4_ext_mark_uninitialized(ex2);
2822 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
2823 if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
2824 err = ext4_ext_zeroout(inode, &orig_ex);
2826 goto fix_extent_len;
2827 /* update the extent length and mark as initialized */
2828 ex->ee_len = cpu_to_le32(ee_len);
2829 ext4_ext_try_to_merge(inode, path, ex);
2830 err = ext4_ext_dirty(handle, inode, path + depth);
2833 goto fix_extent_len;
2836 ext4_ext_show_leaf(inode, path);
2840 ex->ee_len = orig_ex.ee_len;
2841 ext4_ext_dirty(handle, inode, path + depth);
2846 * ext4_split_extents() splits an extent and mark extent which is covered
2847 * by @map as split_flags indicates
2849 * It may result in splitting the extent into multiple extents (upto three)
2850 * There are three possibilities:
2851 * a> There is no split required
2852 * b> Splits in two extents: Split is happening at either end of the extent
2853 * c> Splits in three extents: Somone is splitting in middle of the extent
2856 static int ext4_split_extent(handle_t *handle,
2857 struct inode *inode,
2858 struct ext4_ext_path *path,
2859 struct ext4_map_blocks *map,
2863 ext4_lblk_t ee_block;
2864 struct ext4_extent *ex;
2865 unsigned int ee_len, depth;
2868 int split_flag1, flags1;
2870 depth = ext_depth(inode);
2871 ex = path[depth].p_ext;
2872 ee_block = le32_to_cpu(ex->ee_block);
2873 ee_len = ext4_ext_get_actual_len(ex);
2874 uninitialized = ext4_ext_is_uninitialized(ex);
2876 if (map->m_lblk + map->m_len < ee_block + ee_len) {
2877 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ?
2878 EXT4_EXT_MAY_ZEROOUT : 0;
2879 flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
2881 split_flag1 |= EXT4_EXT_MARK_UNINIT1 |
2882 EXT4_EXT_MARK_UNINIT2;
2883 err = ext4_split_extent_at(handle, inode, path,
2884 map->m_lblk + map->m_len, split_flag1, flags1);
2889 ext4_ext_drop_refs(path);
2890 path = ext4_ext_find_extent(inode, map->m_lblk, path);
2892 return PTR_ERR(path);
2894 if (map->m_lblk >= ee_block) {
2895 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ?
2896 EXT4_EXT_MAY_ZEROOUT : 0;
2898 split_flag1 |= EXT4_EXT_MARK_UNINIT1;
2899 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2900 split_flag1 |= EXT4_EXT_MARK_UNINIT2;
2901 err = ext4_split_extent_at(handle, inode, path,
2902 map->m_lblk, split_flag1, flags);
2907 ext4_ext_show_leaf(inode, path);
2909 return err ? err : map->m_len;
2912 #define EXT4_EXT_ZERO_LEN 7
2914 * This function is called by ext4_ext_map_blocks() if someone tries to write
2915 * to an uninitialized extent. It may result in splitting the uninitialized
2916 * extent into multiple extents (up to three - one initialized and two
2918 * There are three possibilities:
2919 * a> There is no split required: Entire extent should be initialized
2920 * b> Splits in two extents: Write is happening at either end of the extent
2921 * c> Splits in three extents: Somone is writing in middle of the extent
2923 static int ext4_ext_convert_to_initialized(handle_t *handle,
2924 struct inode *inode,
2925 struct ext4_map_blocks *map,
2926 struct ext4_ext_path *path)
2928 struct ext4_map_blocks split_map;
2929 struct ext4_extent zero_ex;
2930 struct ext4_extent *ex;
2931 ext4_lblk_t ee_block, eof_block;
2932 unsigned int ee_len, depth;
2937 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
2938 "block %llu, max_blocks %u\n", inode->i_ino,
2939 (unsigned long long)map->m_lblk, map->m_len);
2941 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
2942 inode->i_sb->s_blocksize_bits;
2943 if (eof_block < map->m_lblk + map->m_len)
2944 eof_block = map->m_lblk + map->m_len;
2946 depth = ext_depth(inode);
2947 ex = path[depth].p_ext;
2948 ee_block = le32_to_cpu(ex->ee_block);
2949 ee_len = ext4_ext_get_actual_len(ex);
2950 allocated = ee_len - (map->m_lblk - ee_block);
2952 WARN_ON(map->m_lblk < ee_block);
2954 * It is safe to convert extent to initialized via explicit
2955 * zeroout only if extent is fully insde i_size or new_size.
2957 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
2959 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
2960 if (ee_len <= 2*EXT4_EXT_ZERO_LEN &&
2961 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
2962 err = ext4_ext_zeroout(inode, ex);
2966 err = ext4_ext_get_access(handle, inode, path + depth);
2969 ext4_ext_mark_initialized(ex);
2970 ext4_ext_try_to_merge(inode, path, ex);
2971 err = ext4_ext_dirty(handle, inode, path + depth);
2977 * 1. split the extent into three extents.
2978 * 2. split the extent into two extents, zeroout the first half.
2979 * 3. split the extent into two extents, zeroout the second half.
2980 * 4. split the extent into two extents with out zeroout.
2982 split_map.m_lblk = map->m_lblk;
2983 split_map.m_len = map->m_len;
2985 if (allocated > map->m_len) {
2986 if (allocated <= EXT4_EXT_ZERO_LEN &&
2987 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
2990 cpu_to_le32(map->m_lblk);
2991 zero_ex.ee_len = cpu_to_le16(allocated);
2992 ext4_ext_store_pblock(&zero_ex,
2993 ext4_ext_pblock(ex) + map->m_lblk - ee_block);
2994 err = ext4_ext_zeroout(inode, &zero_ex);
2997 split_map.m_lblk = map->m_lblk;
2998 split_map.m_len = allocated;
2999 } else if ((map->m_lblk - ee_block + map->m_len <
3000 EXT4_EXT_ZERO_LEN) &&
3001 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
3003 if (map->m_lblk != ee_block) {
3004 zero_ex.ee_block = ex->ee_block;
3005 zero_ex.ee_len = cpu_to_le16(map->m_lblk -
3007 ext4_ext_store_pblock(&zero_ex,
3008 ext4_ext_pblock(ex));
3009 err = ext4_ext_zeroout(inode, &zero_ex);
3014 split_map.m_lblk = ee_block;
3015 split_map.m_len = map->m_lblk - ee_block + map->m_len;
3016 allocated = map->m_len;
3020 allocated = ext4_split_extent(handle, inode, path,
3021 &split_map, split_flag, 0);
3026 return err ? err : allocated;
3030 * This function is called by ext4_ext_map_blocks() from
3031 * ext4_get_blocks_dio_write() when DIO to write
3032 * to an uninitialized extent.
3034 * Writing to an uninitialized extent may result in splitting the uninitialized
3035 * extent into multiple /initialized uninitialized extents (up to three)
3036 * There are three possibilities:
3037 * a> There is no split required: Entire extent should be uninitialized
3038 * b> Splits in two extents: Write is happening at either end of the extent
3039 * c> Splits in three extents: Somone is writing in middle of the extent
3041 * One of more index blocks maybe needed if the extent tree grow after
3042 * the uninitialized extent split. To prevent ENOSPC occur at the IO
3043 * complete, we need to split the uninitialized extent before DIO submit
3044 * the IO. The uninitialized extent called at this time will be split
3045 * into three uninitialized extent(at most). After IO complete, the part
3046 * being filled will be convert to initialized by the end_io callback function
3047 * via ext4_convert_unwritten_extents().
3049 * Returns the size of uninitialized extent to be written on success.
3051 static int ext4_split_unwritten_extents(handle_t *handle,
3052 struct inode *inode,
3053 struct ext4_map_blocks *map,
3054 struct ext4_ext_path *path,
3057 ext4_lblk_t eof_block;
3058 ext4_lblk_t ee_block;
3059 struct ext4_extent *ex;
3060 unsigned int ee_len;
3061 int split_flag = 0, depth;
3063 ext_debug("ext4_split_unwritten_extents: inode %lu, logical"
3064 "block %llu, max_blocks %u\n", inode->i_ino,
3065 (unsigned long long)map->m_lblk, map->m_len);
3067 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3068 inode->i_sb->s_blocksize_bits;
3069 if (eof_block < map->m_lblk + map->m_len)
3070 eof_block = map->m_lblk + map->m_len;
3072 * It is safe to convert extent to initialized via explicit
3073 * zeroout only if extent is fully insde i_size or new_size.
3075 depth = ext_depth(inode);
3076 ex = path[depth].p_ext;
3077 ee_block = le32_to_cpu(ex->ee_block);
3078 ee_len = ext4_ext_get_actual_len(ex);
3080 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
3081 split_flag |= EXT4_EXT_MARK_UNINIT2;
3083 flags |= EXT4_GET_BLOCKS_PRE_IO;
3084 return ext4_split_extent(handle, inode, path, map, split_flag, flags);
3087 static int ext4_convert_unwritten_extents_endio(handle_t *handle,
3088 struct inode *inode,
3089 struct ext4_ext_path *path)
3091 struct ext4_extent *ex;
3095 depth = ext_depth(inode);
3096 ex = path[depth].p_ext;
3098 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
3099 "block %llu, max_blocks %u\n", inode->i_ino,
3100 (unsigned long long)le32_to_cpu(ex->ee_block),
3101 ext4_ext_get_actual_len(ex));
3103 err = ext4_ext_get_access(handle, inode, path + depth);
3106 /* first mark the extent as initialized */
3107 ext4_ext_mark_initialized(ex);
3109 /* note: ext4_ext_correct_indexes() isn't needed here because
3110 * borders are not changed
3112 ext4_ext_try_to_merge(inode, path, ex);
3114 /* Mark modified extent as dirty */
3115 err = ext4_ext_dirty(handle, inode, path + depth);
3117 ext4_ext_show_leaf(inode, path);
3121 static void unmap_underlying_metadata_blocks(struct block_device *bdev,
3122 sector_t block, int count)
3125 for (i = 0; i < count; i++)
3126 unmap_underlying_metadata(bdev, block + i);
3130 * Handle EOFBLOCKS_FL flag, clearing it if necessary
3132 static int check_eofblocks_fl(handle_t *handle, struct inode *inode,
3134 struct ext4_ext_path *path,
3138 struct ext4_extent_header *eh;
3139 struct ext4_extent *last_ex;
3141 if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
3144 depth = ext_depth(inode);
3145 eh = path[depth].p_hdr;
3147 if (unlikely(!eh->eh_entries)) {
3148 EXT4_ERROR_INODE(inode, "eh->eh_entries == 0 and "
3149 "EOFBLOCKS_FL set");
3152 last_ex = EXT_LAST_EXTENT(eh);
3154 * We should clear the EOFBLOCKS_FL flag if we are writing the
3155 * last block in the last extent in the file. We test this by
3156 * first checking to see if the caller to
3157 * ext4_ext_get_blocks() was interested in the last block (or
3158 * a block beyond the last block) in the current extent. If
3159 * this turns out to be false, we can bail out from this
3160 * function immediately.
3162 if (lblk + len < le32_to_cpu(last_ex->ee_block) +
3163 ext4_ext_get_actual_len(last_ex))
3166 * If the caller does appear to be planning to write at or
3167 * beyond the end of the current extent, we then test to see
3168 * if the current extent is the last extent in the file, by
3169 * checking to make sure it was reached via the rightmost node
3170 * at each level of the tree.
3172 for (i = depth-1; i >= 0; i--)
3173 if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr))
3175 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
3176 return ext4_mark_inode_dirty(handle, inode);
3180 * ext4_find_delalloc_range: find delayed allocated block in the given range.
3182 * Goes through the buffer heads in the range [lblk_start, lblk_end] and returns
3183 * whether there are any buffers marked for delayed allocation. It returns '1'
3184 * on the first delalloc'ed buffer head found. If no buffer head in the given
3185 * range is marked for delalloc, it returns 0.
3186 * lblk_start should always be <= lblk_end.
3187 * search_hint_reverse is to indicate that searching in reverse from lblk_end to
3188 * lblk_start might be more efficient (i.e., we will likely hit the delalloc'ed
3189 * block sooner). This is useful when blocks are truncated sequentially from
3190 * lblk_start towards lblk_end.
3192 static int ext4_find_delalloc_range(struct inode *inode,
3193 ext4_lblk_t lblk_start,
3194 ext4_lblk_t lblk_end,
3195 int search_hint_reverse)
3197 struct address_space *mapping = inode->i_mapping;
3198 struct buffer_head *head, *bh = NULL;
3200 ext4_lblk_t i, pg_lblk;
3203 /* reverse search wont work if fs block size is less than page size */
3204 if (inode->i_blkbits < PAGE_CACHE_SHIFT)
3205 search_hint_reverse = 0;
3207 if (search_hint_reverse)
3212 index = i >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
3214 while ((i >= lblk_start) && (i <= lblk_end)) {
3215 page = find_get_page(mapping, index);
3219 if (!page_has_buffers(page))
3222 head = page_buffers(page);
3227 pg_lblk = index << (PAGE_CACHE_SHIFT -
3230 if (unlikely(pg_lblk < lblk_start)) {
3232 * This is possible when fs block size is less
3233 * than page size and our cluster starts/ends in
3234 * middle of the page. So we need to skip the
3235 * initial few blocks till we reach the 'lblk'
3241 /* Check if the buffer is delayed allocated and that it
3242 * is not yet mapped. (when da-buffers are mapped during
3243 * their writeout, their da_mapped bit is set.)
3245 if (buffer_delay(bh) && !buffer_da_mapped(bh)) {
3246 page_cache_release(page);
3247 trace_ext4_find_delalloc_range(inode,
3248 lblk_start, lblk_end,
3249 search_hint_reverse,
3253 if (search_hint_reverse)
3257 } while ((i >= lblk_start) && (i <= lblk_end) &&
3258 ((bh = bh->b_this_page) != head));
3261 page_cache_release(page);
3263 * Move to next page. 'i' will be the first lblk in the next
3266 if (search_hint_reverse)
3270 i = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
3273 trace_ext4_find_delalloc_range(inode, lblk_start, lblk_end,
3274 search_hint_reverse, 0, 0);
3278 int ext4_find_delalloc_cluster(struct inode *inode, ext4_lblk_t lblk,
3279 int search_hint_reverse)
3281 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3282 ext4_lblk_t lblk_start, lblk_end;
3283 lblk_start = lblk & (~(sbi->s_cluster_ratio - 1));
3284 lblk_end = lblk_start + sbi->s_cluster_ratio - 1;
3286 return ext4_find_delalloc_range(inode, lblk_start, lblk_end,
3287 search_hint_reverse);
3291 * Determines how many complete clusters (out of those specified by the 'map')
3292 * are under delalloc and were reserved quota for.
3293 * This function is called when we are writing out the blocks that were
3294 * originally written with their allocation delayed, but then the space was
3295 * allocated using fallocate() before the delayed allocation could be resolved.
3296 * The cases to look for are:
3297 * ('=' indicated delayed allocated blocks
3298 * '-' indicates non-delayed allocated blocks)
3299 * (a) partial clusters towards beginning and/or end outside of allocated range
3300 * are not delalloc'ed.
3302 * |----c---=|====c====|====c====|===-c----|
3303 * |++++++ allocated ++++++|
3304 * ==> 4 complete clusters in above example
3306 * (b) partial cluster (outside of allocated range) towards either end is
3307 * marked for delayed allocation. In this case, we will exclude that
3310 * |----====c========|========c========|
3311 * |++++++ allocated ++++++|
3312 * ==> 1 complete clusters in above example
3315 * |================c================|
3316 * |++++++ allocated ++++++|
3317 * ==> 0 complete clusters in above example
3319 * The ext4_da_update_reserve_space will be called only if we
3320 * determine here that there were some "entire" clusters that span
3321 * this 'allocated' range.
3322 * In the non-bigalloc case, this function will just end up returning num_blks
3323 * without ever calling ext4_find_delalloc_range.
3326 get_reserved_cluster_alloc(struct inode *inode, ext4_lblk_t lblk_start,
3327 unsigned int num_blks)
3329 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3330 ext4_lblk_t alloc_cluster_start, alloc_cluster_end;
3331 ext4_lblk_t lblk_from, lblk_to, c_offset;
3332 unsigned int allocated_clusters = 0;
3334 alloc_cluster_start = EXT4_B2C(sbi, lblk_start);
3335 alloc_cluster_end = EXT4_B2C(sbi, lblk_start + num_blks - 1);
3337 /* max possible clusters for this allocation */
3338 allocated_clusters = alloc_cluster_end - alloc_cluster_start + 1;
3340 trace_ext4_get_reserved_cluster_alloc(inode, lblk_start, num_blks);
3342 /* Check towards left side */
3343 c_offset = lblk_start & (sbi->s_cluster_ratio - 1);
3345 lblk_from = lblk_start & (~(sbi->s_cluster_ratio - 1));
3346 lblk_to = lblk_from + c_offset - 1;
3348 if (ext4_find_delalloc_range(inode, lblk_from, lblk_to, 0))
3349 allocated_clusters--;
3352 /* Now check towards right. */
3353 c_offset = (lblk_start + num_blks) & (sbi->s_cluster_ratio - 1);
3354 if (allocated_clusters && c_offset) {
3355 lblk_from = lblk_start + num_blks;
3356 lblk_to = lblk_from + (sbi->s_cluster_ratio - c_offset) - 1;
3358 if (ext4_find_delalloc_range(inode, lblk_from, lblk_to, 0))
3359 allocated_clusters--;
3362 return allocated_clusters;
3366 ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode,
3367 struct ext4_map_blocks *map,
3368 struct ext4_ext_path *path, int flags,
3369 unsigned int allocated, ext4_fsblk_t newblock)
3373 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
3375 ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical"
3376 "block %llu, max_blocks %u, flags %d, allocated %u",
3377 inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
3379 ext4_ext_show_leaf(inode, path);
3381 trace_ext4_ext_handle_uninitialized_extents(inode, map, allocated,
3384 /* get_block() before submit the IO, split the extent */
3385 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
3386 ret = ext4_split_unwritten_extents(handle, inode, map,
3389 * Flag the inode(non aio case) or end_io struct (aio case)
3390 * that this IO needs to conversion to written when IO is
3393 if (io && !(io->flag & EXT4_IO_END_UNWRITTEN)) {
3394 io->flag = EXT4_IO_END_UNWRITTEN;
3395 atomic_inc(&EXT4_I(inode)->i_aiodio_unwritten);
3397 ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
3398 if (ext4_should_dioread_nolock(inode))
3399 map->m_flags |= EXT4_MAP_UNINIT;
3402 /* IO end_io complete, convert the filled extent to written */
3403 if ((flags & EXT4_GET_BLOCKS_CONVERT)) {
3404 ret = ext4_convert_unwritten_extents_endio(handle, inode,
3407 ext4_update_inode_fsync_trans(handle, inode, 1);
3408 err = check_eofblocks_fl(handle, inode, map->m_lblk,
3414 /* buffered IO case */
3416 * repeat fallocate creation request
3417 * we already have an unwritten extent
3419 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT)
3422 /* buffered READ or buffered write_begin() lookup */
3423 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3425 * We have blocks reserved already. We
3426 * return allocated blocks so that delalloc
3427 * won't do block reservation for us. But
3428 * the buffer head will be unmapped so that
3429 * a read from the block returns 0s.
3431 map->m_flags |= EXT4_MAP_UNWRITTEN;
3435 /* buffered write, writepage time, convert*/
3436 ret = ext4_ext_convert_to_initialized(handle, inode, map, path);
3438 ext4_update_inode_fsync_trans(handle, inode, 1);
3445 map->m_flags |= EXT4_MAP_NEW;
3447 * if we allocated more blocks than requested
3448 * we need to make sure we unmap the extra block
3449 * allocated. The actual needed block will get
3450 * unmapped later when we find the buffer_head marked
3453 if (allocated > map->m_len) {
3454 unmap_underlying_metadata_blocks(inode->i_sb->s_bdev,
3455 newblock + map->m_len,
3456 allocated - map->m_len);
3457 allocated = map->m_len;
3461 * If we have done fallocate with the offset that is already
3462 * delayed allocated, we would have block reservation
3463 * and quota reservation done in the delayed write path.
3464 * But fallocate would have already updated quota and block
3465 * count for this offset. So cancel these reservation
3467 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
3468 unsigned int reserved_clusters;
3469 reserved_clusters = get_reserved_cluster_alloc(inode,
3470 map->m_lblk, map->m_len);
3471 if (reserved_clusters)
3472 ext4_da_update_reserve_space(inode,
3478 map->m_flags |= EXT4_MAP_MAPPED;
3479 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0) {
3480 err = check_eofblocks_fl(handle, inode, map->m_lblk, path,
3486 if (allocated > map->m_len)
3487 allocated = map->m_len;
3488 ext4_ext_show_leaf(inode, path);
3489 map->m_pblk = newblock;
3490 map->m_len = allocated;
3493 ext4_ext_drop_refs(path);
3496 return err ? err : allocated;
3500 * get_implied_cluster_alloc - check to see if the requested
3501 * allocation (in the map structure) overlaps with a cluster already
3502 * allocated in an extent.
3503 * @sb The filesystem superblock structure
3504 * @map The requested lblk->pblk mapping
3505 * @ex The extent structure which might contain an implied
3506 * cluster allocation
3508 * This function is called by ext4_ext_map_blocks() after we failed to
3509 * find blocks that were already in the inode's extent tree. Hence,
3510 * we know that the beginning of the requested region cannot overlap
3511 * the extent from the inode's extent tree. There are three cases we
3512 * want to catch. The first is this case:
3514 * |--- cluster # N--|
3515 * |--- extent ---| |---- requested region ---|
3518 * The second case that we need to test for is this one:
3520 * |--------- cluster # N ----------------|
3521 * |--- requested region --| |------- extent ----|
3522 * |=======================|
3524 * The third case is when the requested region lies between two extents
3525 * within the same cluster:
3526 * |------------- cluster # N-------------|
3527 * |----- ex -----| |---- ex_right ----|
3528 * |------ requested region ------|
3529 * |================|
3531 * In each of the above cases, we need to set the map->m_pblk and
3532 * map->m_len so it corresponds to the return the extent labelled as
3533 * "|====|" from cluster #N, since it is already in use for data in
3534 * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to
3535 * signal to ext4_ext_map_blocks() that map->m_pblk should be treated
3536 * as a new "allocated" block region. Otherwise, we will return 0 and
3537 * ext4_ext_map_blocks() will then allocate one or more new clusters
3538 * by calling ext4_mb_new_blocks().
3540 static int get_implied_cluster_alloc(struct super_block *sb,
3541 struct ext4_map_blocks *map,
3542 struct ext4_extent *ex,
3543 struct ext4_ext_path *path)
3545 struct ext4_sb_info *sbi = EXT4_SB(sb);
3546 ext4_lblk_t c_offset = map->m_lblk & (sbi->s_cluster_ratio-1);
3547 ext4_lblk_t ex_cluster_start, ex_cluster_end;
3548 ext4_lblk_t rr_cluster_start, rr_cluster_end;
3549 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
3550 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
3551 unsigned short ee_len = ext4_ext_get_actual_len(ex);
3553 /* The extent passed in that we are trying to match */
3554 ex_cluster_start = EXT4_B2C(sbi, ee_block);
3555 ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1);
3557 /* The requested region passed into ext4_map_blocks() */
3558 rr_cluster_start = EXT4_B2C(sbi, map->m_lblk);
3559 rr_cluster_end = EXT4_B2C(sbi, map->m_lblk + map->m_len - 1);
3561 if ((rr_cluster_start == ex_cluster_end) ||
3562 (rr_cluster_start == ex_cluster_start)) {
3563 if (rr_cluster_start == ex_cluster_end)
3564 ee_start += ee_len - 1;
3565 map->m_pblk = (ee_start & ~(sbi->s_cluster_ratio - 1)) +
3567 map->m_len = min(map->m_len,
3568 (unsigned) sbi->s_cluster_ratio - c_offset);
3570 * Check for and handle this case:
3572 * |--------- cluster # N-------------|
3573 * |------- extent ----|
3574 * |--- requested region ---|
3578 if (map->m_lblk < ee_block)
3579 map->m_len = min(map->m_len, ee_block - map->m_lblk);
3582 * Check for the case where there is already another allocated
3583 * block to the right of 'ex' but before the end of the cluster.
3585 * |------------- cluster # N-------------|
3586 * |----- ex -----| |---- ex_right ----|
3587 * |------ requested region ------|
3588 * |================|
3590 if (map->m_lblk > ee_block) {
3591 ext4_lblk_t next = ext4_ext_next_allocated_block(path);
3592 map->m_len = min(map->m_len, next - map->m_lblk);
3595 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1);
3599 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0);
3605 * Block allocation/map/preallocation routine for extents based files
3608 * Need to be called with
3609 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
3610 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
3612 * return > 0, number of of blocks already mapped/allocated
3613 * if create == 0 and these are pre-allocated blocks
3614 * buffer head is unmapped
3615 * otherwise blocks are mapped
3617 * return = 0, if plain look up failed (blocks have not been allocated)
3618 * buffer head is unmapped
3620 * return < 0, error case.
3622 int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
3623 struct ext4_map_blocks *map, int flags)
3625 struct ext4_ext_path *path = NULL;
3626 struct ext4_extent newex, *ex, *ex2;
3627 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3628 ext4_fsblk_t newblock = 0;
3629 int free_on_err = 0, err = 0, depth, ret;
3630 unsigned int allocated = 0, offset = 0;
3631 unsigned int allocated_clusters = 0, reserved_clusters = 0;
3632 unsigned int punched_out = 0;
3633 unsigned int result = 0;
3634 struct ext4_allocation_request ar;
3635 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
3636 ext4_lblk_t cluster_offset;
3637 struct ext4_map_blocks punch_map;
3639 ext_debug("blocks %u/%u requested for inode %lu\n",
3640 map->m_lblk, map->m_len, inode->i_ino);
3641 trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
3643 /* check in cache */
3644 if (!(flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) &&
3645 ext4_ext_in_cache(inode, map->m_lblk, &newex)) {
3646 if (!newex.ee_start_lo && !newex.ee_start_hi) {
3647 if ((sbi->s_cluster_ratio > 1) &&
3648 ext4_find_delalloc_cluster(inode, map->m_lblk, 0))
3649 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
3651 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3653 * block isn't allocated yet and
3654 * user doesn't want to allocate it
3658 /* we should allocate requested block */
3660 /* block is already allocated */
3661 if (sbi->s_cluster_ratio > 1)
3662 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
3663 newblock = map->m_lblk
3664 - le32_to_cpu(newex.ee_block)
3665 + ext4_ext_pblock(&newex);
3666 /* number of remaining blocks in the extent */
3667 allocated = ext4_ext_get_actual_len(&newex) -
3668 (map->m_lblk - le32_to_cpu(newex.ee_block));
3673 /* find extent for this block */
3674 path = ext4_ext_find_extent(inode, map->m_lblk, NULL);
3676 err = PTR_ERR(path);
3681 depth = ext_depth(inode);
3684 * consistent leaf must not be empty;
3685 * this situation is possible, though, _during_ tree modification;
3686 * this is why assert can't be put in ext4_ext_find_extent()
3688 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
3689 EXT4_ERROR_INODE(inode, "bad extent address "
3690 "lblock: %lu, depth: %d pblock %lld",
3691 (unsigned long) map->m_lblk, depth,
3692 path[depth].p_block);
3697 ex = path[depth].p_ext;
3699 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
3700 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
3701 unsigned short ee_len;
3704 * Uninitialized extents are treated as holes, except that
3705 * we split out initialized portions during a write.
3707 ee_len = ext4_ext_get_actual_len(ex);
3709 trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len);
3711 /* if found extent covers block, simply return it */
3712 if (in_range(map->m_lblk, ee_block, ee_len)) {
3713 ext4_fsblk_t partial_cluster = 0;
3715 newblock = map->m_lblk - ee_block + ee_start;
3716 /* number of remaining blocks in the extent */
3717 allocated = ee_len - (map->m_lblk - ee_block);
3718 ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
3719 ee_block, ee_len, newblock);
3721 if ((flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) == 0) {
3723 * Do not put uninitialized extent
3726 if (!ext4_ext_is_uninitialized(ex)) {
3727 ext4_ext_put_in_cache(inode, ee_block,
3731 ret = ext4_ext_handle_uninitialized_extents(
3732 handle, inode, map, path, flags,
3733 allocated, newblock);
3738 * Punch out the map length, but only to the
3741 punched_out = allocated < map->m_len ?
3742 allocated : map->m_len;
3745 * Sense extents need to be converted to
3746 * uninitialized, they must fit in an
3747 * uninitialized extent
3749 if (punched_out > EXT_UNINIT_MAX_LEN)
3750 punched_out = EXT_UNINIT_MAX_LEN;
3752 punch_map.m_lblk = map->m_lblk;
3753 punch_map.m_pblk = newblock;
3754 punch_map.m_len = punched_out;
3755 punch_map.m_flags = 0;
3757 /* Check to see if the extent needs to be split */
3758 if (punch_map.m_len != ee_len ||
3759 punch_map.m_lblk != ee_block) {
3761 ret = ext4_split_extent(handle, inode,
3762 path, &punch_map, 0,
3763 EXT4_GET_BLOCKS_PUNCH_OUT_EXT |
3764 EXT4_GET_BLOCKS_PRE_IO);
3771 * find extent for the block at
3772 * the start of the hole
3774 ext4_ext_drop_refs(path);
3777 path = ext4_ext_find_extent(inode,
3780 err = PTR_ERR(path);
3785 depth = ext_depth(inode);
3786 ex = path[depth].p_ext;
3787 ee_len = ext4_ext_get_actual_len(ex);
3788 ee_block = le32_to_cpu(ex->ee_block);
3789 ee_start = ext4_ext_pblock(ex);
3793 ext4_ext_mark_uninitialized(ex);
3795 ext4_ext_invalidate_cache(inode);
3797 err = ext4_ext_rm_leaf(handle, inode, path,
3798 &partial_cluster, map->m_lblk,
3799 map->m_lblk + punched_out);
3801 if (!err && path->p_hdr->eh_entries == 0) {
3803 * Punch hole freed all of this sub tree,
3804 * so we need to correct eh_depth
3806 err = ext4_ext_get_access(handle, inode, path);
3808 ext_inode_hdr(inode)->eh_depth = 0;
3809 ext_inode_hdr(inode)->eh_max =
3810 cpu_to_le16(ext4_ext_space_root(
3813 err = ext4_ext_dirty(
3814 handle, inode, path);
3822 if ((sbi->s_cluster_ratio > 1) &&
3823 ext4_find_delalloc_cluster(inode, map->m_lblk, 0))
3824 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
3827 * requested block isn't allocated yet;
3828 * we couldn't try to create block if create flag is zero
3830 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3832 * put just found gap into cache to speed up
3833 * subsequent requests
3835 ext4_ext_put_gap_in_cache(inode, path, map->m_lblk);
3840 * Okay, we need to do block allocation.
3842 map->m_flags &= ~EXT4_MAP_FROM_CLUSTER;
3843 newex.ee_block = cpu_to_le32(map->m_lblk);
3844 cluster_offset = map->m_lblk & (sbi->s_cluster_ratio-1);
3847 * If we are doing bigalloc, check to see if the extent returned
3848 * by ext4_ext_find_extent() implies a cluster we can use.
3850 if (cluster_offset && ex &&
3851 get_implied_cluster_alloc(inode->i_sb, map, ex, path)) {
3852 ar.len = allocated = map->m_len;
3853 newblock = map->m_pblk;
3854 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
3855 goto got_allocated_blocks;
3858 /* find neighbour allocated blocks */
3859 ar.lleft = map->m_lblk;
3860 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
3863 ar.lright = map->m_lblk;
3865 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2);
3869 /* Check if the extent after searching to the right implies a
3870 * cluster we can use. */
3871 if ((sbi->s_cluster_ratio > 1) && ex2 &&
3872 get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) {
3873 ar.len = allocated = map->m_len;
3874 newblock = map->m_pblk;
3875 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
3876 goto got_allocated_blocks;
3880 * See if request is beyond maximum number of blocks we can have in
3881 * a single extent. For an initialized extent this limit is
3882 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
3883 * EXT_UNINIT_MAX_LEN.
3885 if (map->m_len > EXT_INIT_MAX_LEN &&
3886 !(flags & EXT4_GET_BLOCKS_UNINIT_EXT))
3887 map->m_len = EXT_INIT_MAX_LEN;
3888 else if (map->m_len > EXT_UNINIT_MAX_LEN &&
3889 (flags & EXT4_GET_BLOCKS_UNINIT_EXT))
3890 map->m_len = EXT_UNINIT_MAX_LEN;
3892 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
3893 newex.ee_len = cpu_to_le16(map->m_len);
3894 err = ext4_ext_check_overlap(sbi, inode, &newex, path);
3896 allocated = ext4_ext_get_actual_len(&newex);
3898 allocated = map->m_len;
3900 /* allocate new block */
3902 ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
3903 ar.logical = map->m_lblk;
3905 * We calculate the offset from the beginning of the cluster
3906 * for the logical block number, since when we allocate a
3907 * physical cluster, the physical block should start at the
3908 * same offset from the beginning of the cluster. This is
3909 * needed so that future calls to get_implied_cluster_alloc()
3912 offset = map->m_lblk & (sbi->s_cluster_ratio - 1);
3913 ar.len = EXT4_NUM_B2C(sbi, offset+allocated);
3915 ar.logical -= offset;
3916 if (S_ISREG(inode->i_mode))
3917 ar.flags = EXT4_MB_HINT_DATA;
3919 /* disable in-core preallocation for non-regular files */
3921 if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE)
3922 ar.flags |= EXT4_MB_HINT_NOPREALLOC;
3923 newblock = ext4_mb_new_blocks(handle, &ar, &err);
3926 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
3927 ar.goal, newblock, allocated);
3929 allocated_clusters = ar.len;
3930 ar.len = EXT4_C2B(sbi, ar.len) - offset;
3931 if (ar.len > allocated)
3934 got_allocated_blocks:
3935 /* try to insert new extent into found leaf and return */
3936 ext4_ext_store_pblock(&newex, newblock + offset);
3937 newex.ee_len = cpu_to_le16(ar.len);
3938 /* Mark uninitialized */
3939 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){
3940 ext4_ext_mark_uninitialized(&newex);
3942 * io_end structure was created for every IO write to an
3943 * uninitialized extent. To avoid unnecessary conversion,
3944 * here we flag the IO that really needs the conversion.
3945 * For non asycn direct IO case, flag the inode state
3946 * that we need to perform conversion when IO is done.
3948 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
3949 if (io && !(io->flag & EXT4_IO_END_UNWRITTEN)) {
3950 io->flag = EXT4_IO_END_UNWRITTEN;
3951 atomic_inc(&EXT4_I(inode)->i_aiodio_unwritten);
3953 ext4_set_inode_state(inode,
3954 EXT4_STATE_DIO_UNWRITTEN);
3956 if (ext4_should_dioread_nolock(inode))
3957 map->m_flags |= EXT4_MAP_UNINIT;
3961 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0)
3962 err = check_eofblocks_fl(handle, inode, map->m_lblk,
3965 err = ext4_ext_insert_extent(handle, inode, path,
3967 if (err && free_on_err) {
3968 int fb_flags = flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE ?
3969 EXT4_FREE_BLOCKS_NO_QUOT_UPDATE : 0;
3970 /* free data blocks we just allocated */
3971 /* not a good idea to call discard here directly,
3972 * but otherwise we'd need to call it every free() */
3973 ext4_discard_preallocations(inode);
3974 ext4_free_blocks(handle, inode, NULL, ext4_ext_pblock(&newex),
3975 ext4_ext_get_actual_len(&newex), fb_flags);
3979 /* previous routine could use block we allocated */
3980 newblock = ext4_ext_pblock(&newex);
3981 allocated = ext4_ext_get_actual_len(&newex);
3982 if (allocated > map->m_len)
3983 allocated = map->m_len;
3984 map->m_flags |= EXT4_MAP_NEW;
3987 * Update reserved blocks/metadata blocks after successful
3988 * block allocation which had been deferred till now.
3990 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
3992 * Check how many clusters we had reserved this allocted range.
3994 reserved_clusters = get_reserved_cluster_alloc(inode,
3995 map->m_lblk, allocated);
3996 if (map->m_flags & EXT4_MAP_FROM_CLUSTER) {
3997 if (reserved_clusters) {
3999 * We have clusters reserved for this range.
4000 * But since we are not doing actual allocation
4001 * and are simply using blocks from previously
4002 * allocated cluster, we should release the
4003 * reservation and not claim quota.
4005 ext4_da_update_reserve_space(inode,
4006 reserved_clusters, 0);
4009 BUG_ON(allocated_clusters < reserved_clusters);
4010 /* We will claim quota for all newly allocated blocks.*/
4011 ext4_da_update_reserve_space(inode, allocated_clusters,
4013 if (reserved_clusters < allocated_clusters) {
4014 struct ext4_inode_info *ei = EXT4_I(inode);
4015 int reservation = allocated_clusters -
4018 * It seems we claimed few clusters outside of
4019 * the range of this allocation. We should give
4020 * it back to the reservation pool. This can
4021 * happen in the following case:
4023 * * Suppose s_cluster_ratio is 4 (i.e., each
4024 * cluster has 4 blocks. Thus, the clusters
4025 * are [0-3],[4-7],[8-11]...
4026 * * First comes delayed allocation write for
4027 * logical blocks 10 & 11. Since there were no
4028 * previous delayed allocated blocks in the
4029 * range [8-11], we would reserve 1 cluster
4031 * * Next comes write for logical blocks 3 to 8.
4032 * In this case, we will reserve 2 clusters
4033 * (for [0-3] and [4-7]; and not for [8-11] as
4034 * that range has a delayed allocated blocks.
4035 * Thus total reserved clusters now becomes 3.
4036 * * Now, during the delayed allocation writeout
4037 * time, we will first write blocks [3-8] and
4038 * allocate 3 clusters for writing these
4039 * blocks. Also, we would claim all these
4040 * three clusters above.
4041 * * Now when we come here to writeout the
4042 * blocks [10-11], we would expect to claim
4043 * the reservation of 1 cluster we had made
4044 * (and we would claim it since there are no
4045 * more delayed allocated blocks in the range
4046 * [8-11]. But our reserved cluster count had
4047 * already gone to 0.
4049 * Thus, at the step 4 above when we determine
4050 * that there are still some unwritten delayed
4051 * allocated blocks outside of our current
4052 * block range, we should increment the
4053 * reserved clusters count so that when the
4054 * remaining blocks finally gets written, we
4057 dquot_reserve_block(inode,
4058 EXT4_C2B(sbi, reservation));
4059 spin_lock(&ei->i_block_reservation_lock);
4060 ei->i_reserved_data_blocks += reservation;
4061 spin_unlock(&ei->i_block_reservation_lock);
4067 * Cache the extent and update transaction to commit on fdatasync only
4068 * when it is _not_ an uninitialized extent.
4070 if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) {
4071 ext4_ext_put_in_cache(inode, map->m_lblk, allocated, newblock);
4072 ext4_update_inode_fsync_trans(handle, inode, 1);
4074 ext4_update_inode_fsync_trans(handle, inode, 0);
4076 if (allocated > map->m_len)
4077 allocated = map->m_len;
4078 ext4_ext_show_leaf(inode, path);
4079 map->m_flags |= EXT4_MAP_MAPPED;
4080 map->m_pblk = newblock;
4081 map->m_len = allocated;
4084 ext4_ext_drop_refs(path);
4087 trace_ext4_ext_map_blocks_exit(inode, map->m_lblk,
4088 newblock, map->m_len, err ? err : allocated);
4090 result = (flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) ?
4091 punched_out : allocated;
4093 return err ? err : result;
4096 void ext4_ext_truncate(struct inode *inode)
4098 struct address_space *mapping = inode->i_mapping;
4099 struct super_block *sb = inode->i_sb;
4100 ext4_lblk_t last_block;
4106 * finish any pending end_io work so we won't run the risk of
4107 * converting any truncated blocks to initialized later
4109 ext4_flush_completed_IO(inode);
4112 * probably first extent we're gonna free will be last in block
4114 err = ext4_writepage_trans_blocks(inode);
4115 handle = ext4_journal_start(inode, err);
4119 if (inode->i_size % PAGE_CACHE_SIZE != 0) {
4120 page_len = PAGE_CACHE_SIZE -
4121 (inode->i_size & (PAGE_CACHE_SIZE - 1));
4123 err = ext4_discard_partial_page_buffers(handle,
4124 mapping, inode->i_size, page_len, 0);
4130 if (ext4_orphan_add(handle, inode))
4133 down_write(&EXT4_I(inode)->i_data_sem);
4134 ext4_ext_invalidate_cache(inode);
4136 ext4_discard_preallocations(inode);
4139 * TODO: optimization is possible here.
4140 * Probably we need not scan at all,
4141 * because page truncation is enough.
4144 /* we have to know where to truncate from in crash case */
4145 EXT4_I(inode)->i_disksize = inode->i_size;
4146 ext4_mark_inode_dirty(handle, inode);
4148 last_block = (inode->i_size + sb->s_blocksize - 1)
4149 >> EXT4_BLOCK_SIZE_BITS(sb);
4150 err = ext4_ext_remove_space(inode, last_block);
4152 /* In a multi-transaction truncate, we only make the final
4153 * transaction synchronous.
4156 ext4_handle_sync(handle);
4158 up_write(&EXT4_I(inode)->i_data_sem);
4162 * If this was a simple ftruncate() and the file will remain alive,
4163 * then we need to clear up the orphan record which we created above.
4164 * However, if this was a real unlink then we were called by
4165 * ext4_delete_inode(), and we allow that function to clean up the
4166 * orphan info for us.
4169 ext4_orphan_del(handle, inode);
4171 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
4172 ext4_mark_inode_dirty(handle, inode);
4173 ext4_journal_stop(handle);
4176 static void ext4_falloc_update_inode(struct inode *inode,
4177 int mode, loff_t new_size, int update_ctime)
4179 struct timespec now;
4182 now = current_fs_time(inode->i_sb);
4183 if (!timespec_equal(&inode->i_ctime, &now))
4184 inode->i_ctime = now;
4187 * Update only when preallocation was requested beyond
4190 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
4191 if (new_size > i_size_read(inode))
4192 i_size_write(inode, new_size);
4193 if (new_size > EXT4_I(inode)->i_disksize)
4194 ext4_update_i_disksize(inode, new_size);
4197 * Mark that we allocate beyond EOF so the subsequent truncate
4198 * can proceed even if the new size is the same as i_size.
4200 if (new_size > i_size_read(inode))
4201 ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
4207 * preallocate space for a file. This implements ext4's fallocate file
4208 * operation, which gets called from sys_fallocate system call.
4209 * For block-mapped files, posix_fallocate should fall back to the method
4210 * of writing zeroes to the required new blocks (the same behavior which is
4211 * expected for file systems which do not support fallocate() system call).
4213 long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
4215 struct inode *inode = file->f_path.dentry->d_inode;
4218 unsigned int max_blocks;
4223 struct ext4_map_blocks map;
4224 unsigned int credits, blkbits = inode->i_blkbits;
4227 * currently supporting (pre)allocate mode for extent-based
4230 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
4233 /* Return error if mode is not supported */
4234 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
4237 if (mode & FALLOC_FL_PUNCH_HOLE)
4238 return ext4_punch_hole(file, offset, len);
4240 trace_ext4_fallocate_enter(inode, offset, len, mode);
4241 map.m_lblk = offset >> blkbits;
4243 * We can't just convert len to max_blocks because
4244 * If blocksize = 4096 offset = 3072 and len = 2048
4246 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
4249 * credits to insert 1 extent into extent tree
4251 credits = ext4_chunk_trans_blocks(inode, max_blocks);
4252 mutex_lock(&inode->i_mutex);
4253 ret = inode_newsize_ok(inode, (len + offset));
4255 mutex_unlock(&inode->i_mutex);
4256 trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
4259 flags = EXT4_GET_BLOCKS_CREATE_UNINIT_EXT |
4260 EXT4_GET_BLOCKS_NO_NORMALIZE;
4261 if (mode & FALLOC_FL_KEEP_SIZE)
4262 flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
4264 while (ret >= 0 && ret < max_blocks) {
4265 map.m_lblk = map.m_lblk + ret;
4266 map.m_len = max_blocks = max_blocks - ret;
4267 handle = ext4_journal_start(inode, credits);
4268 if (IS_ERR(handle)) {
4269 ret = PTR_ERR(handle);
4272 ret = ext4_map_blocks(handle, inode, &map, flags);
4276 printk(KERN_ERR "%s: ext4_ext_map_blocks "
4277 "returned error inode#%lu, block=%u, "
4278 "max_blocks=%u", __func__,
4279 inode->i_ino, map.m_lblk, max_blocks);
4281 ext4_mark_inode_dirty(handle, inode);
4282 ret2 = ext4_journal_stop(handle);
4285 if ((map.m_lblk + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
4286 blkbits) >> blkbits))
4287 new_size = offset + len;
4289 new_size = ((loff_t) map.m_lblk + ret) << blkbits;
4291 ext4_falloc_update_inode(inode, mode, new_size,
4292 (map.m_flags & EXT4_MAP_NEW));
4293 ext4_mark_inode_dirty(handle, inode);
4294 ret2 = ext4_journal_stop(handle);
4298 if (ret == -ENOSPC &&
4299 ext4_should_retry_alloc(inode->i_sb, &retries)) {
4303 mutex_unlock(&inode->i_mutex);
4304 trace_ext4_fallocate_exit(inode, offset, max_blocks,
4305 ret > 0 ? ret2 : ret);
4306 return ret > 0 ? ret2 : ret;
4310 * This function convert a range of blocks to written extents
4311 * The caller of this function will pass the start offset and the size.
4312 * all unwritten extents within this range will be converted to
4315 * This function is called from the direct IO end io call back
4316 * function, to convert the fallocated extents after IO is completed.
4317 * Returns 0 on success.
4319 int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset,
4323 unsigned int max_blocks;
4326 struct ext4_map_blocks map;
4327 unsigned int credits, blkbits = inode->i_blkbits;
4329 map.m_lblk = offset >> blkbits;
4331 * We can't just convert len to max_blocks because
4332 * If blocksize = 4096 offset = 3072 and len = 2048
4334 max_blocks = ((EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) -
4337 * credits to insert 1 extent into extent tree
4339 credits = ext4_chunk_trans_blocks(inode, max_blocks);
4340 while (ret >= 0 && ret < max_blocks) {
4342 map.m_len = (max_blocks -= ret);
4343 handle = ext4_journal_start(inode, credits);
4344 if (IS_ERR(handle)) {
4345 ret = PTR_ERR(handle);
4348 ret = ext4_map_blocks(handle, inode, &map,
4349 EXT4_GET_BLOCKS_IO_CONVERT_EXT);
4352 printk(KERN_ERR "%s: ext4_ext_map_blocks "
4353 "returned error inode#%lu, block=%u, "
4354 "max_blocks=%u", __func__,
4355 inode->i_ino, map.m_lblk, map.m_len);
4357 ext4_mark_inode_dirty(handle, inode);
4358 ret2 = ext4_journal_stop(handle);
4359 if (ret <= 0 || ret2 )
4362 return ret > 0 ? ret2 : ret;
4366 * Callback function called for each extent to gather FIEMAP information.
4368 static int ext4_ext_fiemap_cb(struct inode *inode, ext4_lblk_t next,
4369 struct ext4_ext_cache *newex, struct ext4_extent *ex,
4377 struct fiemap_extent_info *fieinfo = data;
4378 unsigned char blksize_bits;
4380 blksize_bits = inode->i_sb->s_blocksize_bits;
4381 logical = (__u64)newex->ec_block << blksize_bits;
4383 if (newex->ec_start == 0) {
4385 * No extent in extent-tree contains block @newex->ec_start,
4386 * then the block may stay in 1)a hole or 2)delayed-extent.
4388 * Holes or delayed-extents are processed as follows.
4389 * 1. lookup dirty pages with specified range in pagecache.
4390 * If no page is got, then there is no delayed-extent and
4391 * return with EXT_CONTINUE.
4392 * 2. find the 1st mapped buffer,
4393 * 3. check if the mapped buffer is both in the request range
4394 * and a delayed buffer. If not, there is no delayed-extent,
4396 * 4. a delayed-extent is found, the extent will be collected.
4398 ext4_lblk_t end = 0;
4399 pgoff_t last_offset;
4402 pgoff_t start_index = 0;
4403 struct page **pages = NULL;
4404 struct buffer_head *bh = NULL;
4405 struct buffer_head *head = NULL;
4406 unsigned int nr_pages = PAGE_SIZE / sizeof(struct page *);
4408 pages = kmalloc(PAGE_SIZE, GFP_KERNEL);
4412 offset = logical >> PAGE_SHIFT;
4414 last_offset = offset;
4416 ret = find_get_pages_tag(inode->i_mapping, &offset,
4417 PAGECACHE_TAG_DIRTY, nr_pages, pages);
4419 if (!(flags & FIEMAP_EXTENT_DELALLOC)) {
4420 /* First time, try to find a mapped buffer. */
4423 for (index = 0; index < ret; index++)
4424 page_cache_release(pages[index]);
4427 return EXT_CONTINUE;
4432 /* Try to find the 1st mapped buffer. */
4433 end = ((__u64)pages[index]->index << PAGE_SHIFT) >>
4435 if (!page_has_buffers(pages[index]))
4437 head = page_buffers(pages[index]);
4444 if (end >= newex->ec_block +
4446 /* The buffer is out of
4447 * the request range.
4451 if (buffer_mapped(bh) &&
4452 end >= newex->ec_block) {
4453 start_index = index - 1;
4454 /* get the 1st mapped buffer. */
4455 goto found_mapped_buffer;
4458 bh = bh->b_this_page;
4460 } while (bh != head);
4462 /* No mapped buffer in the range found in this page,
4463 * We need to look up next page.
4466 /* There is no page left, but we need to limit
4469 newex->ec_len = end - newex->ec_block;
4474 /*Find contiguous delayed buffers. */
4475 if (ret > 0 && pages[0]->index == last_offset)
4476 head = page_buffers(pages[0]);
4482 found_mapped_buffer:
4483 if (bh != NULL && buffer_delay(bh)) {
4484 /* 1st or contiguous delayed buffer found. */
4485 if (!(flags & FIEMAP_EXTENT_DELALLOC)) {
4487 * 1st delayed buffer found, record
4488 * the start of extent.
4490 flags |= FIEMAP_EXTENT_DELALLOC;
4491 newex->ec_block = end;
4492 logical = (__u64)end << blksize_bits;
4494 /* Find contiguous delayed buffers. */
4496 if (!buffer_delay(bh))
4497 goto found_delayed_extent;
4498 bh = bh->b_this_page;
4500 } while (bh != head);
4502 for (; index < ret; index++) {
4503 if (!page_has_buffers(pages[index])) {
4507 head = page_buffers(pages[index]);
4513 if (pages[index]->index !=
4514 pages[start_index]->index + index
4516 /* Blocks are not contiguous. */
4522 if (!buffer_delay(bh))
4523 /* Delayed-extent ends. */
4524 goto found_delayed_extent;
4525 bh = bh->b_this_page;
4527 } while (bh != head);
4529 } else if (!(flags & FIEMAP_EXTENT_DELALLOC))
4533 found_delayed_extent:
4534 newex->ec_len = min(end - newex->ec_block,
4535 (ext4_lblk_t)EXT_INIT_MAX_LEN);
4536 if (ret == nr_pages && bh != NULL &&
4537 newex->ec_len < EXT_INIT_MAX_LEN &&
4539 /* Have not collected an extent and continue. */
4540 for (index = 0; index < ret; index++)
4541 page_cache_release(pages[index]);
4545 for (index = 0; index < ret; index++)
4546 page_cache_release(pages[index]);
4550 physical = (__u64)newex->ec_start << blksize_bits;
4551 length = (__u64)newex->ec_len << blksize_bits;
4553 if (ex && ext4_ext_is_uninitialized(ex))
4554 flags |= FIEMAP_EXTENT_UNWRITTEN;
4556 if (next == EXT_MAX_BLOCKS)
4557 flags |= FIEMAP_EXTENT_LAST;
4559 ret = fiemap_fill_next_extent(fieinfo, logical, physical,
4565 return EXT_CONTINUE;
4567 /* fiemap flags we can handle specified here */
4568 #define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
4570 static int ext4_xattr_fiemap(struct inode *inode,
4571 struct fiemap_extent_info *fieinfo)
4575 __u32 flags = FIEMAP_EXTENT_LAST;
4576 int blockbits = inode->i_sb->s_blocksize_bits;
4580 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
4581 struct ext4_iloc iloc;
4582 int offset; /* offset of xattr in inode */
4584 error = ext4_get_inode_loc(inode, &iloc);
4587 physical = iloc.bh->b_blocknr << blockbits;
4588 offset = EXT4_GOOD_OLD_INODE_SIZE +
4589 EXT4_I(inode)->i_extra_isize;
4591 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
4592 flags |= FIEMAP_EXTENT_DATA_INLINE;
4594 } else { /* external block */
4595 physical = EXT4_I(inode)->i_file_acl << blockbits;
4596 length = inode->i_sb->s_blocksize;
4600 error = fiemap_fill_next_extent(fieinfo, 0, physical,
4602 return (error < 0 ? error : 0);
4606 * ext4_ext_punch_hole
4608 * Punches a hole of "length" bytes in a file starting
4611 * @inode: The inode of the file to punch a hole in
4612 * @offset: The starting byte offset of the hole
4613 * @length: The length of the hole
4615 * Returns the number of blocks removed or negative on err
4617 int ext4_ext_punch_hole(struct file *file, loff_t offset, loff_t length)
4619 struct inode *inode = file->f_path.dentry->d_inode;
4620 struct super_block *sb = inode->i_sb;
4621 struct ext4_ext_cache cache_ex;
4622 ext4_lblk_t first_block, last_block, num_blocks, iblock, max_blocks;
4623 struct address_space *mapping = inode->i_mapping;
4624 struct ext4_map_blocks map;
4626 loff_t first_page, last_page, page_len;
4627 loff_t first_page_offset, last_page_offset;
4628 int ret, credits, blocks_released, err = 0;
4630 /* No need to punch hole beyond i_size */
4631 if (offset >= inode->i_size)
4635 * If the hole extends beyond i_size, set the hole
4636 * to end after the page that contains i_size
4638 if (offset + length > inode->i_size) {
4639 length = inode->i_size +
4640 PAGE_CACHE_SIZE - (inode->i_size & (PAGE_CACHE_SIZE - 1)) -
4644 first_block = (offset + sb->s_blocksize - 1) >>
4645 EXT4_BLOCK_SIZE_BITS(sb);
4646 last_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb);
4648 first_page = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
4649 last_page = (offset + length) >> PAGE_CACHE_SHIFT;
4651 first_page_offset = first_page << PAGE_CACHE_SHIFT;
4652 last_page_offset = last_page << PAGE_CACHE_SHIFT;
4655 * Write out all dirty pages to avoid race conditions
4656 * Then release them.
4658 if (mapping->nrpages && mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
4659 err = filemap_write_and_wait_range(mapping,
4660 offset, offset + length - 1);
4666 /* Now release the pages */
4667 if (last_page_offset > first_page_offset) {
4668 truncate_inode_pages_range(mapping, first_page_offset,
4669 last_page_offset-1);
4672 /* finish any pending end_io work */
4673 ext4_flush_completed_IO(inode);
4675 credits = ext4_writepage_trans_blocks(inode);
4676 handle = ext4_journal_start(inode, credits);
4678 return PTR_ERR(handle);
4680 err = ext4_orphan_add(handle, inode);
4685 * Now we need to zero out the non-page-aligned data in the
4686 * pages at the start and tail of the hole, and unmap the buffer
4687 * heads for the block aligned regions of the page that were
4688 * completely zeroed.
4690 if (first_page > last_page) {
4692 * If the file space being truncated is contained within a page
4693 * just zero out and unmap the middle of that page
4695 err = ext4_discard_partial_page_buffers(handle,
4696 mapping, offset, length, 0);
4702 * zero out and unmap the partial page that contains
4703 * the start of the hole
4705 page_len = first_page_offset - offset;
4707 err = ext4_discard_partial_page_buffers(handle, mapping,
4708 offset, page_len, 0);
4714 * zero out and unmap the partial page that contains
4715 * the end of the hole
4717 page_len = offset + length - last_page_offset;
4719 err = ext4_discard_partial_page_buffers(handle, mapping,
4720 last_page_offset, page_len, 0);
4728 * If i_size is contained in the last page, we need to
4729 * unmap and zero the partial page after i_size
4731 if (inode->i_size >> PAGE_CACHE_SHIFT == last_page &&
4732 inode->i_size % PAGE_CACHE_SIZE != 0) {
4734 page_len = PAGE_CACHE_SIZE -
4735 (inode->i_size & (PAGE_CACHE_SIZE - 1));
4738 err = ext4_discard_partial_page_buffers(handle,
4739 mapping, inode->i_size, page_len, 0);
4746 /* If there are no blocks to remove, return now */
4747 if (first_block >= last_block)
4750 down_write(&EXT4_I(inode)->i_data_sem);
4751 ext4_ext_invalidate_cache(inode);
4752 ext4_discard_preallocations(inode);
4755 * Loop over all the blocks and identify blocks
4756 * that need to be punched out
4758 iblock = first_block;
4759 blocks_released = 0;
4760 while (iblock < last_block) {
4761 max_blocks = last_block - iblock;
4763 memset(&map, 0, sizeof(map));
4764 map.m_lblk = iblock;
4765 map.m_len = max_blocks;
4766 ret = ext4_ext_map_blocks(handle, inode, &map,
4767 EXT4_GET_BLOCKS_PUNCH_OUT_EXT);
4770 blocks_released += ret;
4772 } else if (ret == 0) {
4774 * If map blocks could not find the block,
4775 * then it is in a hole. If the hole was
4776 * not already cached, then map blocks should
4777 * put it in the cache. So we can get the hole
4780 memset(&cache_ex, 0, sizeof(cache_ex));
4781 if ((ext4_ext_check_cache(inode, iblock, &cache_ex)) &&
4782 !cache_ex.ec_start) {
4784 /* The hole is cached */
4785 num_blocks = cache_ex.ec_block +
4786 cache_ex.ec_len - iblock;
4789 /* The block could not be identified */
4794 /* Map blocks error */
4799 if (num_blocks == 0) {
4800 /* This condition should never happen */
4801 ext_debug("Block lookup failed");
4806 iblock += num_blocks;
4809 if (blocks_released > 0) {
4810 ext4_ext_invalidate_cache(inode);
4811 ext4_discard_preallocations(inode);
4815 ext4_handle_sync(handle);
4817 up_write(&EXT4_I(inode)->i_data_sem);
4820 ext4_orphan_del(handle, inode);
4821 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
4822 ext4_mark_inode_dirty(handle, inode);
4823 ext4_journal_stop(handle);
4826 int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4827 __u64 start, __u64 len)
4829 ext4_lblk_t start_blk;
4832 /* fallback to generic here if not in extents fmt */
4833 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
4834 return generic_block_fiemap(inode, fieinfo, start, len,
4837 if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
4840 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
4841 error = ext4_xattr_fiemap(inode, fieinfo);
4843 ext4_lblk_t len_blks;
4846 start_blk = start >> inode->i_sb->s_blocksize_bits;
4847 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
4848 if (last_blk >= EXT_MAX_BLOCKS)
4849 last_blk = EXT_MAX_BLOCKS-1;
4850 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
4853 * Walk the extent tree gathering extent information.
4854 * ext4_ext_fiemap_cb will push extents back to user.
4856 error = ext4_ext_walk_space(inode, start_blk, len_blks,
4857 ext4_ext_fiemap_cb, fieinfo);