2 * Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com)
3 * Copyright (c) 2015 Kaho Ng (ngkaho1234@gmail.com)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "ext4_config.h"
30 #include "ext4_blockdev.h"
32 #include "ext4_super.h"
33 #include "ext4_crc32c.h"
34 #include "ext4_balloc.h"
35 #include "ext4_debug.h"
42 #include "ext4_extent.h"
44 #if CONFIG_EXTENT_FULL
47 * used by extent splitting.
49 #define EXT4_EXT_MARK_UNWRIT1 0x02 /* mark first half unwritten */
50 #define EXT4_EXT_MARK_UNWRIT2 0x04 /* mark second half unwritten */
51 #define EXT4_EXT_DATA_VALID1 0x08 /* first half contains valid data */
52 #define EXT4_EXT_DATA_VALID2 0x10 /* second half contains valid data */
53 #define EXT4_EXT_NO_COMBINE 0x20 /* do not combine two extents */
55 static struct ext4_extent_tail *
56 find_ext4_extent_tail(struct ext4_extent_header *eh)
58 return (struct ext4_extent_tail *)(((char *)eh) +
59 EXT4_EXTENT_TAIL_OFFSET(eh));
62 static struct ext4_extent_header *ext_inode_hdr(struct ext4_inode *inode)
64 return (struct ext4_extent_header *)inode->blocks;
67 static struct ext4_extent_header *ext_block_hdr(struct ext4_block *block)
69 return (struct ext4_extent_header *)block->data;
72 static uint16_t ext_depth(struct ext4_inode *inode)
74 return to_le16(ext_inode_hdr(inode)->depth);
77 static uint16_t ext4_ext_get_actual_len(struct ext4_extent *ext)
79 return (to_le16(ext->block_count) <= EXT_INIT_MAX_LEN
80 ? to_le16(ext->block_count)
81 : (to_le16(ext->block_count) - EXT_INIT_MAX_LEN));
84 static void ext4_ext_mark_initialized(struct ext4_extent *ext)
86 ext->block_count = to_le16(ext4_ext_get_actual_len(ext));
89 static void ext4_ext_mark_unwritten(struct ext4_extent *ext)
91 ext->block_count |= to_le16(EXT_INIT_MAX_LEN);
94 static int ext4_ext_is_unwritten(struct ext4_extent *ext)
96 /* Extent with ee_len of 0x8000 is treated as an initialized extent */
97 return (to_le16(ext->block_count) > EXT_INIT_MAX_LEN);
102 * combine low and high parts of physical block number into ext4_fsblk_t
104 static ext4_fsblk_t ext4_ext_pblock(struct ext4_extent *ex)
108 block = to_le32(ex->start_lo);
109 block |= ((ext4_fsblk_t)to_le16(ex->start_hi) << 31) << 1;
115 * combine low and high parts of a leaf physical block number into ext4_fsblk_t
117 static ext4_fsblk_t ext4_idx_pblock(struct ext4_extent_index *ix)
121 block = to_le32(ix->leaf_lo);
122 block |= ((ext4_fsblk_t)to_le16(ix->leaf_hi) << 31) << 1;
127 * ext4_ext_store_pblock:
128 * stores a large physical block number into an extent struct,
129 * breaking it into parts
131 static void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb)
133 ex->start_lo = to_le32((uint32_t)(pb & 0xffffffff));
134 ex->start_hi = to_le16((uint16_t)((pb >> 32)) & 0xffff);
138 * ext4_idx_store_pblock:
139 * stores a large physical block number into an index struct,
140 * breaking it into parts
142 static void ext4_idx_store_pblock(struct ext4_extent_index *ix, ext4_fsblk_t pb)
144 ix->leaf_lo = to_le32((uint32_t)(pb & 0xffffffff));
145 ix->leaf_hi = to_le16((uint16_t)((pb >> 32)) & 0xffff);
148 static int ext4_allocate_single_block(struct ext4_inode_ref *inode_ref,
150 ext4_fsblk_t *blockp)
152 return ext4_balloc_alloc_block(inode_ref, goal, blockp);
155 static ext4_fsblk_t ext4_new_meta_blocks(struct ext4_inode_ref *inode_ref,
157 uint32_t flags __unused,
158 uint32_t *count, int *errp)
160 ext4_fsblk_t block = 0;
162 *errp = ext4_allocate_single_block(inode_ref, goal, &block);
168 static void ext4_ext_free_blocks(struct ext4_inode_ref *inode_ref,
169 ext4_fsblk_t block, uint32_t count,
170 uint32_t flags __unused)
172 ext4_balloc_free_blocks(inode_ref, block, count);
175 static size_t ext4_ext_space_block(struct ext4_inode_ref *inode_ref)
178 uint32_t block_size = ext4_sb_get_block_size(&inode_ref->fs->sb);
180 size = (block_size - sizeof(struct ext4_extent_header)) /
181 sizeof(struct ext4_extent);
182 #ifdef AGGRESSIVE_TEST
189 static size_t ext4_ext_space_block_idx(struct ext4_inode_ref *inode_ref)
192 uint32_t block_size = ext4_sb_get_block_size(&inode_ref->fs->sb);
194 size = (block_size - sizeof(struct ext4_extent_header)) /
195 sizeof(struct ext4_extent_index);
196 #ifdef AGGRESSIVE_TEST
203 static size_t ext4_ext_space_root(struct ext4_inode_ref *inode_ref)
207 size = sizeof(inode_ref->inode->blocks);
208 size -= sizeof(struct ext4_extent_header);
209 size /= sizeof(struct ext4_extent);
210 #ifdef AGGRESSIVE_TEST
217 static size_t ext4_ext_space_root_idx(struct ext4_inode_ref *inode_ref)
221 size = sizeof(inode_ref->inode->blocks);
222 size -= sizeof(struct ext4_extent_header);
223 size /= sizeof(struct ext4_extent_index);
224 #ifdef AGGRESSIVE_TEST
231 static size_t ext4_ext_max_entries(struct ext4_inode_ref *inode_ref,
236 if (depth == ext_depth(inode_ref->inode)) {
238 max = ext4_ext_space_root(inode_ref);
240 max = ext4_ext_space_root_idx(inode_ref);
243 max = ext4_ext_space_block(inode_ref);
245 max = ext4_ext_space_block_idx(inode_ref);
251 static ext4_fsblk_t ext4_ext_find_goal(struct ext4_inode_ref *inode_ref,
252 struct ext4_extent_path *path,
256 uint32_t depth = path->depth;
257 struct ext4_extent *ex;
260 * Try to predict block placement assuming that we are
261 * filling in a file which will eventually be
262 * non-sparse --- i.e., in the case of libbfd writing
263 * an ELF object sections out-of-order but in a way
264 * the eventually results in a contiguous object or
265 * executable file, or some database extending a table
266 * space file. However, this is actually somewhat
267 * non-ideal if we are writing a sparse file such as
268 * qemu or KVM writing a raw image file that is going
269 * to stay fairly sparse, since it will end up
270 * fragmenting the file system's free space. Maybe we
271 * should have some hueristics or some way to allow
272 * userspace to pass a hint to file system,
273 * especially if the latter case turns out to be
276 ex = path[depth].extent;
278 ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex);
279 ext4_lblk_t ext_block = to_le32(ex->first_block);
281 if (block > ext_block)
282 return ext_pblk + (block - ext_block);
284 return ext_pblk - (ext_block - block);
287 /* it looks like index is empty;
288 * try to find starting block from index itself */
289 if (path[depth].block.lb_id)
290 return path[depth].block.lb_id;
293 /* OK. use inode's group */
294 return ext4_fs_inode_to_goal_block(inode_ref);
298 * Allocation for a meta data block
300 static ext4_fsblk_t ext4_ext_new_meta_block(struct ext4_inode_ref *inode_ref,
301 struct ext4_extent_path *path,
302 struct ext4_extent *ex, int *err,
305 ext4_fsblk_t goal, newblock;
307 goal = ext4_ext_find_goal(inode_ref, path, to_le32(ex->first_block));
308 newblock = ext4_new_meta_blocks(inode_ref, goal, flags, NULL, err);
312 static uint32_t ext4_ext_block_csum(struct ext4_inode_ref *inode_ref,
313 struct ext4_extent_header *eh)
315 uint32_t checksum = 0;
316 struct ext4_sblock *sb = &inode_ref->fs->sb;
318 if (ext4_sb_has_feature_read_only(sb,
319 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
320 uint32_t ino_index = to_le32(inode_ref->index);
322 to_le32(ext4_inode_get_generation(inode_ref->inode));
323 /* First calculate crc32 checksum against fs uuid */
324 checksum = ext4_crc32c(~0, sb->uuid, sizeof(sb->uuid));
325 /* Then calculate crc32 checksum against inode number
326 * and inode generation */
327 checksum = ext4_crc32c(checksum, &ino_index,
329 checksum = ext4_crc32c(checksum, &ino_gen,
331 /* Finally calculate crc32 checksum against
332 * the entire extent block up to the checksum field */
333 checksum = ext4_crc32c(checksum, eh,
334 EXT4_EXTENT_TAIL_OFFSET(eh));
339 static void ext4_extent_block_csum_set(struct ext4_inode_ref *inode_ref,
340 struct ext4_extent_header *eh)
342 struct ext4_extent_tail *tail;
344 tail = find_ext4_extent_tail(eh);
345 tail->et_checksum = to_le32(ext4_ext_block_csum(inode_ref, eh));
348 static int ext4_ext_dirty(struct ext4_inode_ref *inode_ref,
349 struct ext4_extent_path *path)
351 if (path->block.lb_id)
352 path->block.dirty = true;
354 inode_ref->dirty = true;
359 static void ext4_ext_drop_refs(struct ext4_inode_ref *inode_ref,
360 struct ext4_extent_path *path, bool keep_other)
371 for (i = 0; i <= depth; i++, path++) {
372 if (path->block.lb_id) {
373 if (path->block.dirty)
374 ext4_extent_block_csum_set(inode_ref,
377 ext4_block_set(inode_ref->fs->bdev, &path->block);
383 * Check that whether the basic information inside the extent header
386 static int ext4_ext_check(struct ext4_inode_ref *inode_ref,
387 struct ext4_extent_header *eh, uint16_t depth,
388 ext4_fsblk_t pblk __unused)
390 struct ext4_extent_tail *tail;
391 const char *error_msg;
394 if (to_le16(eh->magic) != EXT4_EXTENT_MAGIC) {
395 error_msg = "invalid magic";
398 if (to_le16(eh->depth) != depth) {
399 error_msg = "unexpected eh_depth";
402 if (eh->max_entries_count == 0) {
403 error_msg = "invalid eh_max";
406 if (to_le16(eh->entries_count) > to_le16(eh->max_entries_count)) {
407 error_msg = "invalid eh_entries";
411 tail = find_ext4_extent_tail(eh);
412 if (tail->et_checksum != to_le32(ext4_ext_block_csum(inode_ref, eh))) {
413 /* FIXME: Warning: extent checksum damaged? */
419 ext4_dbg(DEBUG_EXTENT, "Bad extents B+ tree block: %s. "
420 "Blocknr: %" PRId64 "\n",
425 static int read_extent_tree_block(struct ext4_inode_ref *inode_ref,
426 ext4_fsblk_t pblk, int32_t depth,
427 struct ext4_block *bh,
428 uint32_t flags __unused)
432 err = ext4_block_get(inode_ref->fs->bdev, bh, pblk);
436 err = ext4_ext_check(inode_ref, ext_block_hdr(bh), depth, pblk);
443 ext4_block_set(inode_ref->fs->bdev, bh);
449 * ext4_ext_binsearch_idx:
450 * binary search for the closest index of the given block
451 * the header must be checked before calling this
453 static void ext4_ext_binsearch_idx(struct ext4_extent_path *path,
456 struct ext4_extent_header *eh = path->header;
457 struct ext4_extent_index *r, *l, *m;
459 l = EXT_FIRST_INDEX(eh) + 1;
460 r = EXT_LAST_INDEX(eh);
463 if (block < to_le32(m->first_block))
473 * ext4_ext_binsearch:
474 * binary search for closest extent of the given block
475 * the header must be checked before calling this
477 static void ext4_ext_binsearch(struct ext4_extent_path *path, ext4_lblk_t block)
479 struct ext4_extent_header *eh = path->header;
480 struct ext4_extent *r, *l, *m;
482 if (eh->entries_count == 0) {
484 * this leaf is empty:
485 * we get such a leaf in split/add case
490 l = EXT_FIRST_EXTENT(eh) + 1;
491 r = EXT_LAST_EXTENT(eh);
495 if (block < to_le32(m->first_block))
501 path->extent = l - 1;
504 static int ext4_find_extent(struct ext4_inode_ref *inode_ref, ext4_lblk_t block,
505 struct ext4_extent_path **orig_path, uint32_t flags)
507 struct ext4_extent_header *eh;
508 struct ext4_block bh = EXT4_BLOCK_ZERO();
509 ext4_fsblk_t buf_block = 0;
510 struct ext4_extent_path *path = *orig_path;
511 int32_t depth, ppos = 0;
515 eh = ext_inode_hdr(inode_ref->inode);
516 depth = ext_depth(inode_ref->inode);
519 ext4_ext_drop_refs(inode_ref, path, 0);
520 if (depth > path[0].maxdepth) {
522 *orig_path = path = NULL;
526 int32_t path_depth = depth + 1;
527 /* account possible depth increase */
528 path = calloc(1, sizeof(struct ext4_extent_path) *
532 path[0].maxdepth = path_depth;
538 /* walk through the tree */
540 ext4_ext_binsearch_idx(path + ppos, block);
541 path[ppos].p_block = ext4_idx_pblock(path[ppos].index);
542 path[ppos].depth = i;
543 path[ppos].extent = NULL;
544 buf_block = path[ppos].p_block;
548 if (!path[ppos].block.lb_id ||
549 path[ppos].block.lb_id != buf_block) {
550 ret = read_extent_tree_block(inode_ref, buf_block, i,
556 ext4_block_set(inode_ref->fs->bdev, &bh);
561 eh = ext_block_hdr(&bh);
562 path[ppos].block = bh;
563 path[ppos].header = eh;
567 path[ppos].depth = i;
568 path[ppos].extent = NULL;
569 path[ppos].index = NULL;
572 ext4_ext_binsearch(path + ppos, block);
573 /* if not an empty leaf */
574 if (path[ppos].extent)
575 path[ppos].p_block = ext4_ext_pblock(path[ppos].extent);
583 ext4_ext_drop_refs(inode_ref, path, 0);
590 static void ext4_ext_init_header(struct ext4_inode_ref *inode_ref,
591 struct ext4_extent_header *eh, int32_t depth)
593 eh->entries_count = 0;
594 eh->max_entries_count = to_le16(ext4_ext_max_entries(inode_ref, depth));
595 eh->magic = to_le16(EXT4_EXTENT_MAGIC);
600 * Be cautious, the buffer_head returned is not yet mark dirtied. */
601 static int ext4_ext_split_node(struct ext4_inode_ref *inode_ref,
602 struct ext4_extent_path *path, int32_t at,
603 struct ext4_extent *newext,
604 ext4_fsblk_t *sibling, struct ext4_block *new_bh)
607 ext4_fsblk_t newblock;
608 struct ext4_block bh = EXT4_BLOCK_ZERO();
609 int32_t depth = ext_depth(inode_ref->inode);
611 ext4_assert(sibling);
613 /* FIXME: currently we split at the point after the current extent. */
614 newblock = ext4_ext_new_meta_block(inode_ref, path, newext, &ret, 0);
618 /* For write access.# */
619 ret = ext4_block_get(inode_ref->fs->bdev, &bh, newblock);
624 /* start copy from next extent */
625 ptrdiff_t m = EXT_MAX_EXTENT(path[at].header) - path[at].extent;
626 struct ext4_extent_header *neh;
627 neh = ext_block_hdr(&bh);
628 ext4_ext_init_header(inode_ref, neh, 0);
630 struct ext4_extent *ex;
631 ex = EXT_FIRST_EXTENT(neh);
632 memmove(ex, path[at].extent + 1,
633 sizeof(struct ext4_extent) * m);
635 to_le16(to_le16(neh->entries_count) + m);
636 path[at].header->entries_count = to_le16(
637 to_le16(path[at].header->entries_count) - m);
638 ret = ext4_ext_dirty(inode_ref, path + at);
643 ptrdiff_t m = EXT_MAX_INDEX(path[at].header) - path[at].index;
644 struct ext4_extent_header *neh;
645 neh = ext_block_hdr(&bh);
646 ext4_ext_init_header(inode_ref, neh, depth - at);
648 struct ext4_extent_index *ix;
649 ix = EXT_FIRST_INDEX(neh);
650 memmove(ix, path[at].index + 1,
651 sizeof(struct ext4_extent) * m);
653 to_le16(to_le16(neh->entries_count) + m);
654 path[at].header->entries_count = to_le16(
655 to_le16(path[at].header->entries_count) - m);
656 ret = ext4_ext_dirty(inode_ref, path + at);
664 ext4_block_set(inode_ref->fs->bdev, &bh);
667 ext4_ext_free_blocks(inode_ref, newblock, 1, 0);
676 static ext4_lblk_t ext4_ext_block_index(struct ext4_extent_header *eh)
679 return to_le32(EXT_FIRST_INDEX(eh)->first_block);
681 return to_le32(EXT_FIRST_EXTENT(eh)->first_block);
684 struct ext_split_trans {
686 struct ext4_extent_path path;
690 static int ext4_ext_insert_index(struct ext4_inode_ref *inode_ref,
691 struct ext4_extent_path *path,
693 struct ext4_extent *newext,
694 ext4_lblk_t insert_index,
695 ext4_fsblk_t insert_block,
696 struct ext_split_trans *spt,
699 struct ext4_extent_index *ix;
700 struct ext4_extent_path *curp = path + at;
701 struct ext4_block bh = EXT4_BLOCK_ZERO();
704 struct ext4_extent_header *eh;
708 if (curp->index && insert_index == to_le32(curp->index->first_block))
711 if (to_le16(curp->header->entries_count) ==
712 to_le16(curp->header->max_entries_count)) {
714 struct ext4_extent_header *neh;
715 err = ext4_ext_split_node(inode_ref, path, at, newext,
720 neh = ext_block_hdr(&bh);
721 if (insert_index > to_le32(curp->index->first_block)) {
722 /* Make decision which node should be used to
723 * insert the index.*/
724 if (to_le16(neh->entries_count) >
725 to_le16(curp->header->entries_count)) {
728 ix = EXT_LAST_INDEX(eh) + 1;
731 ix = EXT_FIRST_INDEX(eh);
736 ix = EXT_LAST_INDEX(eh);
745 if (curp->index == NULL) {
746 ix = EXT_FIRST_INDEX(eh);
748 } else if (insert_index > to_le32(curp->index->first_block)) {
750 ix = curp->index + 1;
757 len = EXT_LAST_INDEX(eh) - ix + 1;
758 ext4_assert(len >= 0);
760 memmove(ix + 1, ix, len * sizeof(struct ext4_extent_index));
762 if (ix > EXT_MAX_INDEX(eh)) {
767 ix->first_block = to_le32(insert_index);
768 ext4_idx_store_pblock(ix, insert_block);
769 eh->entries_count = to_le16(to_le16(eh->entries_count) + 1);
771 if (ix > EXT_LAST_INDEX(eh)) {
776 if (eh == curp->header)
777 err = ext4_ext_dirty(inode_ref, curp);
782 if (err != EOK || *need_grow) {
784 ext4_block_set(inode_ref->fs->bdev, &bh);
787 } else if (bh.lb_id) {
788 /* If we got a sibling leaf. */
789 ext4_extent_block_csum_set(inode_ref, ext_block_hdr(&bh));
792 spt->path.p_block = ext4_idx_pblock(ix);
793 spt->path.depth = to_le16(eh->depth);
794 spt->path.maxdepth = 0;
795 spt->path.extent = NULL;
796 spt->path.index = ix;
797 spt->path.header = eh;
798 spt->path.block = bh;
801 * If newext->ee_block can be included into the
804 if (to_le32(newext->first_block) >=
805 ext4_ext_block_index(ext_block_hdr(&bh)))
809 curp->p_block = ext4_idx_pblock(ix);
815 curp->p_block = ext4_idx_pblock(ix);
821 * ext4_ext_correct_indexes:
822 * if leaf gets modified and modified extent is first in the leaf,
823 * then we have to correct all indexes above.
825 static int ext4_ext_correct_indexes(struct ext4_inode_ref *inode_ref,
826 struct ext4_extent_path *path)
828 struct ext4_extent_header *eh;
829 int32_t depth = ext_depth(inode_ref->inode);
830 struct ext4_extent *ex;
835 eh = path[depth].header;
836 ex = path[depth].extent;
838 if (ex == NULL || eh == NULL) {
843 /* there is no tree at all */
847 if (ex != EXT_FIRST_EXTENT(eh)) {
848 /* we correct tree if first leaf got modified only */
853 * TODO: we need correction if border is smaller than current one
856 border = path[depth].extent->first_block;
857 path[k].index->first_block = border;
858 err = ext4_ext_dirty(inode_ref, path + k);
863 /* change all left-side indexes */
864 if (path[k + 1].index != EXT_FIRST_INDEX(path[k + 1].header))
866 path[k].index->first_block = border;
867 err = ext4_ext_dirty(inode_ref, path + k);
875 static bool ext4_ext_can_prepend(struct ext4_extent *ex1,
876 struct ext4_extent *ex2)
878 if (ext4_ext_pblock(ex2) + ext4_ext_get_actual_len(ex2) !=
879 ext4_ext_pblock(ex1))
882 #ifdef AGGRESSIVE_TEST
883 if (ext4_ext_get_actual_len(ex1) + ext4_ext_get_actual_len(ex2) > 4)
886 if (ext4_ext_is_unwritten(ex1)) {
887 if (ext4_ext_get_actual_len(ex1) +
888 ext4_ext_get_actual_len(ex2) >
889 EXT_UNWRITTEN_MAX_LEN)
891 } else if (ext4_ext_get_actual_len(ex1) + ext4_ext_get_actual_len(ex2) >
896 if (to_le32(ex2->first_block) + ext4_ext_get_actual_len(ex2) !=
897 to_le32(ex1->first_block))
903 static bool ext4_ext_can_append(struct ext4_extent *ex1,
904 struct ext4_extent *ex2)
906 if (ext4_ext_pblock(ex1) + ext4_ext_get_actual_len(ex1) !=
907 ext4_ext_pblock(ex2))
910 #ifdef AGGRESSIVE_TEST
911 if (ext4_ext_get_actual_len(ex1) + ext4_ext_get_actual_len(ex2) > 4)
914 if (ext4_ext_is_unwritten(ex1)) {
915 if (ext4_ext_get_actual_len(ex1) +
916 ext4_ext_get_actual_len(ex2) >
917 EXT_UNWRITTEN_MAX_LEN)
919 } else if (ext4_ext_get_actual_len(ex1) + ext4_ext_get_actual_len(ex2) >
924 if (to_le32(ex1->first_block) + ext4_ext_get_actual_len(ex1) !=
925 to_le32(ex2->first_block))
931 static int ext4_ext_insert_leaf(struct ext4_inode_ref *inode_ref,
932 struct ext4_extent_path *path,
934 struct ext4_extent *newext,
935 struct ext_split_trans *spt,
939 struct ext4_extent_path *curp = path + at;
940 struct ext4_extent *ex = curp->extent;
941 struct ext4_block bh = EXT4_BLOCK_ZERO();
945 struct ext4_extent_header *eh = NULL;
950 to_le32(newext->first_block) == to_le32(curp->extent->first_block))
953 if (!(flags & EXT4_EXT_NO_COMBINE)) {
954 if (curp->extent && ext4_ext_can_append(curp->extent, newext)) {
955 unwritten = ext4_ext_is_unwritten(curp->extent);
956 curp->extent->block_count =
957 to_le16(ext4_ext_get_actual_len(curp->extent) +
958 ext4_ext_get_actual_len(newext));
960 ext4_ext_mark_unwritten(curp->extent);
961 err = ext4_ext_dirty(inode_ref, curp);
966 ext4_ext_can_prepend(curp->extent, newext)) {
967 unwritten = ext4_ext_is_unwritten(curp->extent);
968 curp->extent->first_block = newext->first_block;
969 curp->extent->block_count =
970 to_le16(ext4_ext_get_actual_len(curp->extent) +
971 ext4_ext_get_actual_len(newext));
973 ext4_ext_mark_unwritten(curp->extent);
974 err = ext4_ext_dirty(inode_ref, curp);
979 if (to_le16(curp->header->entries_count) ==
980 to_le16(curp->header->max_entries_count)) {
982 struct ext4_extent_header *neh;
983 err = ext4_ext_split_node(inode_ref, path, at, newext,
988 neh = ext_block_hdr(&bh);
989 if (to_le32(newext->first_block) >
990 to_le32(curp->extent->first_block)) {
991 if (to_le16(neh->entries_count) >
992 to_le16(curp->header->entries_count)) {
995 ex = EXT_LAST_EXTENT(eh) + 1;
998 ex = EXT_FIRST_EXTENT(eh);
1003 ex = EXT_LAST_EXTENT(eh);
1012 if (curp->extent == NULL) {
1013 ex = EXT_FIRST_EXTENT(eh);
1015 } else if (to_le32(newext->first_block) >
1016 to_le32(curp->extent->first_block)) {
1018 ex = curp->extent + 1;
1025 len = EXT_LAST_EXTENT(eh) - ex + 1;
1026 ext4_assert(len >= 0);
1028 memmove(ex + 1, ex, len * sizeof(struct ext4_extent));
1030 if (ex > EXT_MAX_EXTENT(eh)) {
1035 ex->first_block = newext->first_block;
1036 ex->block_count = newext->block_count;
1037 ext4_ext_store_pblock(ex, ext4_ext_pblock(newext));
1038 eh->entries_count = to_le16(to_le16(eh->entries_count) + 1);
1040 if (ex > EXT_LAST_EXTENT(eh)) {
1045 if (eh == curp->header) {
1046 err = ext4_ext_correct_indexes(inode_ref, path);
1049 err = ext4_ext_dirty(inode_ref, curp);
1054 if (err != EOK || *need_grow) {
1056 ext4_block_set(inode_ref->fs->bdev, &bh);
1059 } else if (bh.lb_id) {
1060 /* If we got a sibling leaf. */
1061 ext4_extent_block_csum_set(inode_ref, ext_block_hdr(&bh));
1064 spt->path.p_block = ext4_ext_pblock(ex);
1065 spt->path.depth = to_le16(eh->depth);
1066 spt->path.maxdepth = 0;
1067 spt->path.extent = ex;
1068 spt->path.index = NULL;
1069 spt->path.header = eh;
1070 spt->path.block = bh;
1073 * If newext->ee_block can be included into the
1076 if (to_le32(newext->first_block) >=
1077 ext4_ext_block_index(ext_block_hdr(&bh)))
1081 curp->p_block = ext4_ext_pblock(ex);
1087 curp->p_block = ext4_ext_pblock(ex);
1094 * ext4_ext_grow_indepth:
1095 * implements tree growing procedure:
1096 * - allocates new block
1097 * - moves top-level data (index block or leaf) into the new block
1098 * - initializes new top-level, creating index that points to the
1099 * just created block
1101 static int ext4_ext_grow_indepth(struct ext4_inode_ref *inode_ref,
1104 struct ext4_extent_header *neh;
1105 struct ext4_block bh = EXT4_BLOCK_ZERO();
1106 ext4_fsblk_t newblock, goal = 0;
1109 /* Try to prepend new index to old one */
1110 if (ext_depth(inode_ref->inode))
1111 goal = ext4_idx_pblock(
1112 EXT_FIRST_INDEX(ext_inode_hdr(inode_ref->inode)));
1114 goal = ext4_fs_inode_to_goal_block(inode_ref);
1116 newblock = ext4_new_meta_blocks(inode_ref, goal, flags, NULL, &err);
1121 err = ext4_block_get(inode_ref->fs->bdev, &bh, newblock);
1123 ext4_ext_free_blocks(inode_ref, newblock, 1, 0);
1127 /* move top-level index/leaf into new block */
1128 memmove(bh.data, inode_ref->inode->blocks,
1129 sizeof(inode_ref->inode->blocks));
1131 /* set size of new block */
1132 neh = ext_block_hdr(&bh);
1133 /* old root could have indexes or leaves
1134 * so calculate e_max right way */
1135 if (ext_depth(inode_ref->inode))
1136 neh->max_entries_count =
1137 to_le16(ext4_ext_space_block_idx(inode_ref));
1139 neh->max_entries_count =
1140 to_le16(ext4_ext_space_block(inode_ref));
1142 neh->magic = to_le16(EXT4_EXTENT_MAGIC);
1144 /* Update top-level index: num,max,pointer */
1145 neh = ext_inode_hdr(inode_ref->inode);
1146 neh->entries_count = to_le16(1);
1147 ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock);
1148 if (neh->depth == 0) {
1149 /* Root extent block becomes index block */
1150 neh->max_entries_count =
1151 to_le16(ext4_ext_space_root_idx(inode_ref));
1152 EXT_FIRST_INDEX(neh)
1153 ->first_block = EXT_FIRST_EXTENT(neh)->first_block;
1155 neh->depth = to_le16(to_le16(neh->depth) + 1);
1157 ext4_extent_block_csum_set(inode_ref, neh);
1159 inode_ref->dirty = true;
1160 ext4_block_set(inode_ref->fs->bdev, &bh);
1165 __unused static void print_path(struct ext4_extent_path *path)
1167 int32_t i = path->depth;
1172 ? (path->extent - EXT_FIRST_EXTENT(path->header))
1176 ? (path->index - EXT_FIRST_INDEX(path->header))
1181 ext4_dbg(DEBUG_EXTENT,
1182 "depth %" PRId32 ", p_block: %" PRIu64 ","
1183 "p_ext offset: %td, p_idx offset: %td\n",
1184 i, path->p_block, a, b);
1190 static void ext4_ext_replace_path(struct ext4_inode_ref *inode_ref,
1191 struct ext4_extent_path *path,
1192 struct ext_split_trans *spt,
1195 int32_t depth = ext_depth(inode_ref->inode);
1196 int32_t i = depth - level;
1197 ext4_ext_drop_refs(inode_ref, path + i, 1);
1198 path[i] = spt[level].path;
1201 static int ext4_ext_insert_extent(struct ext4_inode_ref *inode_ref,
1202 struct ext4_extent_path **ppath,
1203 struct ext4_extent *newext, uint32_t flags)
1205 int32_t i, depth, level;
1207 ext4_fsblk_t ptr = 0;
1208 bool need_grow = false;
1209 struct ext4_extent_path *path = *ppath;
1210 struct ext_split_trans *spt = NULL;
1211 struct ext_split_trans newblock;
1213 memset(&newblock, 0, sizeof(newblock));
1215 depth = ext_depth(inode_ref->inode);
1216 for (i = depth, level = 0; i >= 0; i--, level++)
1217 if (EXT_HAS_FREE_INDEX(path + i))
1221 spt = calloc(1, sizeof(struct ext_split_trans) * (level));
1229 depth = ext_depth(inode_ref->inode);
1233 ret = ext4_ext_insert_leaf(inode_ref, path, depth - i,
1234 newext, &newblock, flags,
1237 ret = ext4_ext_insert_index(
1238 inode_ref, path, depth - i, newext,
1239 ext4_ext_block_index(
1240 ext_block_hdr(&spt[i - 1].path.block)),
1241 spt[i - 1].ptr, &newblock,
1249 else if (spt && ptr && !ret) {
1250 /* Prepare for the next iteration after splitting. */
1255 } while (ptr != 0 && i <= depth);
1258 ret = ext4_ext_grow_indepth(inode_ref, 0);
1261 ret = ext4_find_extent(inode_ref, to_le32(newext->first_block),
1272 ext4_ext_drop_refs(inode_ref, path, 0);
1274 while (--level >= 0 && spt) {
1275 if (spt[level].ptr) {
1276 ext4_ext_free_blocks(inode_ref, spt[level].ptr,
1278 ext4_ext_drop_refs(inode_ref, &spt[level].path,
1283 while (--level >= 0 && spt) {
1284 if (spt[level].switch_to)
1285 ext4_ext_replace_path(inode_ref, path, spt,
1287 else if (spt[level].ptr)
1288 ext4_ext_drop_refs(inode_ref, &spt[level].path,
1298 static void ext4_ext_remove_blocks(struct ext4_inode_ref *inode_ref,
1299 struct ext4_extent *ex, ext4_lblk_t from,
1302 ext4_lblk_t len = to - from + 1;
1305 num = from - to_le32(ex->first_block);
1306 start = ext4_ext_pblock(ex) + num;
1307 ext4_dbg(DEBUG_EXTENT,
1308 "Freeing %" PRIu32 " at %" PRIu64 ", %" PRIu32 "\n", from,
1311 ext4_ext_free_blocks(inode_ref, start, len, 0);
1314 static int ext4_ext_remove_idx(struct ext4_inode_ref *inode_ref,
1315 struct ext4_extent_path *path, int32_t depth)
1321 /* free index block */
1322 leaf = ext4_idx_pblock(path[i].index);
1324 if (path[i].index != EXT_LAST_INDEX(path[i].header)) {
1325 ptrdiff_t len = EXT_LAST_INDEX(path[i].header) - path[i].index;
1326 memmove(path[i].index, path[i].index + 1,
1327 len * sizeof(struct ext4_extent_index));
1330 path[i].header->entries_count =
1331 to_le16(to_le16(path[i].header->entries_count) - 1);
1332 err = ext4_ext_dirty(inode_ref, path + i);
1336 ext4_dbg(DEBUG_EXTENT, "IDX: Freeing %" PRIu32 " at %" PRIu64 ", %d\n",
1337 to_le32(path[i].index->first_block), leaf, 1);
1338 ext4_ext_free_blocks(inode_ref, leaf, 1, 0);
1341 if (path[i].index != EXT_FIRST_INDEX(path[i].header))
1344 path[i - 1].index->first_block = path[i].index->first_block;
1345 err = ext4_ext_dirty(inode_ref, path + i - 1);
1354 static int ext4_ext_remove_leaf(struct ext4_inode_ref *inode_ref,
1355 struct ext4_extent_path *path, ext4_lblk_t from,
1359 int32_t depth = ext_depth(inode_ref->inode);
1360 struct ext4_extent *ex = path[depth].extent;
1361 struct ext4_extent *start_ex, *ex2 = NULL;
1362 struct ext4_extent_header *eh = path[depth].header;
1365 uint16_t new_entries;
1368 new_entries = to_le16(eh->entries_count);
1369 while (ex <= EXT_LAST_EXTENT(path[depth].header) &&
1370 to_le32(ex->first_block) <= to) {
1371 int32_t new_len = 0;
1373 ext4_lblk_t start, new_start;
1374 ext4_fsblk_t newblock;
1375 new_start = start = to_le32(ex->first_block);
1376 len = ext4_ext_get_actual_len(ex);
1377 newblock = ext4_ext_pblock(ex);
1379 len -= from - start;
1380 new_len = from - start;
1384 if (start + len - 1 > to) {
1385 len -= start + len - 1 - to;
1386 new_len = start + len - 1 - to;
1388 newblock += to + 1 - start;
1393 ext4_ext_remove_blocks(inode_ref, ex, start, start + len - 1);
1394 ex->first_block = to_le32(new_start);
1398 unwritten = ext4_ext_is_unwritten(ex);
1399 ex->block_count = to_le16(new_len);
1400 ext4_ext_store_pblock(ex, newblock);
1402 ext4_ext_mark_unwritten(ex);
1411 if (ex2 <= EXT_LAST_EXTENT(eh))
1412 memmove(start_ex, ex2, EXT_LAST_EXTENT(eh) - ex2 + 1);
1414 eh->entries_count = to_le16(new_entries);
1415 ext4_ext_dirty(inode_ref, path + depth);
1416 if (path[depth].extent == EXT_FIRST_EXTENT(eh) && eh->entries_count)
1417 err = ext4_ext_correct_indexes(inode_ref, path);
1419 /* if this leaf is free, then we should
1420 * remove it from index block above */
1421 if (err == EOK && eh->entries_count == 0 && path[depth].block.lb_id)
1422 err = ext4_ext_remove_idx(inode_ref, path, depth - 1);
1424 path[depth - 1].index++;
1429 static bool ext4_ext_more_to_rm(struct ext4_extent_path *path, ext4_lblk_t to)
1431 if (!to_le16(path->header->entries_count))
1434 if (path->index > EXT_LAST_INDEX(path->header))
1437 if (to_le32(path->index->first_block) > to)
1443 int ext4_extent_remove_space(struct ext4_inode_ref *inode_ref, ext4_lblk_t from,
1446 struct ext4_extent_path *path = NULL;
1448 int32_t depth = ext_depth(inode_ref->inode);
1451 ret = ext4_find_extent(inode_ref, from, &path, 0);
1455 if (!path[depth].extent) {
1460 bool in_range = IN_RANGE(from, to_le32(path[depth].extent->first_block),
1461 ext4_ext_get_actual_len(path[depth].extent));
1468 /* If we do remove_space inside the range of an extent */
1469 if ((to_le32(path[depth].extent->first_block) < from) &&
1470 (to < to_le32(path[depth].extent->first_block) +
1471 ext4_ext_get_actual_len(path[depth].extent) - 1)) {
1473 struct ext4_extent *ex = path[depth].extent, newex;
1474 int unwritten = ext4_ext_is_unwritten(ex);
1475 ext4_lblk_t ee_block = to_le32(ex->first_block);
1476 int32_t len = ext4_ext_get_actual_len(ex);
1477 ext4_fsblk_t newblock =
1478 to + 1 - ee_block + ext4_ext_pblock(ex);
1480 ex->block_count = to_le16(from - ee_block);
1482 ext4_ext_mark_unwritten(ex);
1484 ext4_ext_dirty(inode_ref, path + depth);
1486 newex.first_block = to_le32(to + 1);
1487 newex.block_count = to_le16(ee_block + len - 1 - to);
1488 ext4_ext_store_pblock(&newex, newblock);
1490 ext4_ext_mark_unwritten(&newex);
1492 ret = ext4_ext_insert_extent(inode_ref, &path, &newex, 0);
1499 struct ext4_extent_header *eh;
1500 struct ext4_extent *first_ex, *last_ex;
1501 ext4_lblk_t leaf_from, leaf_to;
1502 eh = path[i].header;
1503 ext4_assert(to_le16(eh->entries_count) > 0);
1504 first_ex = EXT_FIRST_EXTENT(eh);
1505 last_ex = EXT_LAST_EXTENT(eh);
1506 leaf_from = to_le32(first_ex->first_block);
1507 leaf_to = to_le32(last_ex->first_block) +
1508 ext4_ext_get_actual_len(last_ex) - 1;
1509 if (leaf_from < from)
1515 ext4_ext_remove_leaf(inode_ref, path, leaf_from,
1517 ext4_ext_drop_refs(inode_ref, path + i, 0);
1522 struct ext4_extent_header *eh;
1523 eh = path[i].header;
1524 if (ext4_ext_more_to_rm(path + i, to)) {
1525 struct ext4_block bh = EXT4_BLOCK_ZERO();
1526 if (path[i + 1].block.lb_id)
1527 ext4_ext_drop_refs(inode_ref, path + i + 1, 0);
1529 ret = read_extent_tree_block(inode_ref,
1530 ext4_idx_pblock(path[i].index),
1531 depth - i - 1, &bh, 0);
1536 ext4_idx_pblock(path[i].index);
1537 path[i + 1].block = bh;
1538 path[i + 1].header = ext_block_hdr(&bh);
1539 path[i + 1].depth = depth - i - 1;
1541 path[i + 1].extent = EXT_FIRST_EXTENT(
1542 path[i + 1].header);
1545 EXT_FIRST_INDEX(path[i + 1].header);
1550 if (!eh->entries_count)
1551 ret = ext4_ext_remove_idx(inode_ref, path,
1554 path[i - 1].index++;
1559 ext4_block_set(inode_ref->fs->bdev,
1568 /* TODO: flexible tree reduction should be here */
1569 if (path->header->entries_count == 0) {
1571 * truncate to zero freed all the tree,
1572 * so we need to correct eh_depth
1574 ext_inode_hdr(inode_ref->inode)->depth = 0;
1575 ext_inode_hdr(inode_ref->inode)->max_entries_count =
1576 to_le16(ext4_ext_space_root(inode_ref));
1577 ret = ext4_ext_dirty(inode_ref, path);
1581 ext4_ext_drop_refs(inode_ref, path, 0);
1587 static int ext4_ext_split_extent_at(struct ext4_inode_ref *inode_ref,
1588 struct ext4_extent_path **ppath,
1589 ext4_lblk_t split, uint32_t split_flag)
1591 struct ext4_extent *ex, newex;
1592 ext4_fsblk_t newblock;
1593 ext4_lblk_t ee_block;
1595 int32_t depth = ext_depth(inode_ref->inode);
1598 ex = (*ppath)[depth].extent;
1599 ee_block = to_le32(ex->first_block);
1600 ee_len = ext4_ext_get_actual_len(ex);
1601 newblock = split - ee_block + ext4_ext_pblock(ex);
1603 if (split == ee_block) {
1605 * case b: block @split is the block that the extent begins with
1606 * then we just change the state of the extent, and splitting
1609 if (split_flag & EXT4_EXT_MARK_UNWRIT2)
1610 ext4_ext_mark_unwritten(ex);
1612 ext4_ext_mark_initialized(ex);
1614 err = ext4_ext_dirty(inode_ref, *ppath + depth);
1618 ex->block_count = to_le16(split - ee_block);
1619 if (split_flag & EXT4_EXT_MARK_UNWRIT1)
1620 ext4_ext_mark_unwritten(ex);
1622 err = ext4_ext_dirty(inode_ref, *ppath + depth);
1626 newex.first_block = to_le32(split);
1627 newex.block_count = to_le16(ee_len - (split - ee_block));
1628 ext4_ext_store_pblock(&newex, newblock);
1629 if (split_flag & EXT4_EXT_MARK_UNWRIT2)
1630 ext4_ext_mark_unwritten(&newex);
1631 err = ext4_ext_insert_extent(inode_ref, ppath, &newex,
1632 EXT4_EXT_NO_COMBINE);
1634 goto restore_extent_len;
1639 ex->block_count = to_le16(ee_len);
1640 err = ext4_ext_dirty(inode_ref, *ppath + depth);
1644 static int ext4_ext_convert_to_initialized(struct ext4_inode_ref *inode_ref,
1645 struct ext4_extent_path **ppath,
1646 ext4_lblk_t split, uint32_t blocks)
1648 int32_t depth = ext_depth(inode_ref->inode), err = EOK;
1649 struct ext4_extent *ex = (*ppath)[depth].extent;
1651 ext4_assert(to_le32(ex->first_block) <= split);
1653 if (split + blocks ==
1654 to_le32(ex->first_block) + ext4_ext_get_actual_len(ex)) {
1655 /* split and initialize right part */
1656 err = ext4_ext_split_extent_at(inode_ref, ppath, split,
1657 EXT4_EXT_MARK_UNWRIT1);
1658 } else if (to_le32(ex->first_block) == split) {
1659 /* split and initialize left part */
1660 err = ext4_ext_split_extent_at(inode_ref, ppath, split + blocks,
1661 EXT4_EXT_MARK_UNWRIT2);
1663 /* split 1 extent to 3 and initialize the 2nd */
1664 err = ext4_ext_split_extent_at(inode_ref, ppath, split + blocks,
1665 EXT4_EXT_MARK_UNWRIT1 |
1666 EXT4_EXT_MARK_UNWRIT2);
1668 err = ext4_ext_split_extent_at(inode_ref, ppath, split,
1669 EXT4_EXT_MARK_UNWRIT1);
1676 static ext4_lblk_t ext4_ext_next_allocated_block(struct ext4_extent_path *path)
1680 depth = path->depth;
1682 if (depth == 0 && path->extent == NULL)
1683 return EXT_MAX_BLOCKS;
1685 while (depth >= 0) {
1686 if (depth == path->depth) {
1688 if (path[depth].extent &&
1689 path[depth].extent !=
1690 EXT_LAST_EXTENT(path[depth].header))
1692 path[depth].extent[1].first_block);
1695 if (path[depth].index !=
1696 EXT_LAST_INDEX(path[depth].header))
1698 path[depth].index[1].first_block);
1703 return EXT_MAX_BLOCKS;
1706 static int ext4_ext_zero_unwritten_range(struct ext4_inode_ref *inode_ref,
1708 uint32_t blocks_count)
1712 uint32_t block_size = ext4_sb_get_block_size(&inode_ref->fs->sb);
1713 for (i = 0; i < blocks_count; i++) {
1714 struct ext4_block bh = EXT4_BLOCK_ZERO();
1715 err = ext4_block_get(inode_ref->fs->bdev, &bh, block + i);
1719 memset(bh.data, 0, block_size);
1721 err = ext4_block_set(inode_ref->fs->bdev, &bh);
1728 int ext4_extent_get_blocks(struct ext4_inode_ref *inode_ref, ext4_fsblk_t iblock,
1729 uint32_t max_blocks, ext4_fsblk_t *result, bool create,
1730 uint32_t *blocks_count)
1732 struct ext4_extent_path *path = NULL;
1733 struct ext4_extent newex, *ex;
1737 uint32_t allocated = 0;
1738 ext4_fsblk_t next, newblock;
1746 /* find extent for this block */
1747 err = ext4_find_extent(inode_ref, iblock, &path, 0);
1753 depth = ext_depth(inode_ref->inode);
1756 * consistent leaf must not be empty
1757 * this situations is possible, though, _during_ tree modification
1758 * this is why assert can't be put in ext4_ext_find_extent()
1760 ex = path[depth].extent;
1762 ext4_lblk_t ee_block = to_le32(ex->first_block);
1763 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
1764 uint16_t ee_len = ext4_ext_get_actual_len(ex);
1765 /* if found exent covers block, simple return it */
1766 if (IN_RANGE(iblock, ee_block, ee_len)) {
1767 /* number of remain blocks in the extent */
1768 allocated = ee_len - (iblock - ee_block);
1770 if (!ext4_ext_is_unwritten(ex)) {
1771 newblock = iblock - ee_block + ee_start;
1780 uint32_t zero_range;
1781 zero_range = allocated;
1782 if (zero_range > max_blocks)
1783 zero_range = max_blocks;
1785 newblock = iblock - ee_block + ee_start;
1786 err = ext4_ext_zero_unwritten_range(inode_ref, newblock,
1791 err = ext4_ext_convert_to_initialized(inode_ref, &path,
1792 iblock, zero_range);
1801 * requested block isn't allocated yet
1802 * we couldn't try to create block if create flag is zero
1808 /* find next allocated block so that we know how many
1809 * blocks we can allocate without ovelapping next extent */
1810 next = ext4_ext_next_allocated_block(path);
1811 allocated = next - iblock;
1812 if (allocated > max_blocks)
1813 allocated = max_blocks;
1815 /* allocate new block */
1816 goal = ext4_ext_find_goal(inode_ref, path, iblock);
1817 newblock = ext4_new_meta_blocks(inode_ref, goal, 0, &allocated, &err);
1821 /* try to insert new extent into found leaf and return */
1822 newex.first_block = to_le32(iblock);
1823 ext4_ext_store_pblock(&newex, newblock);
1824 newex.block_count = to_le16(allocated);
1825 err = ext4_ext_insert_extent(inode_ref, &path, &newex, 0);
1827 /* free data blocks we just allocated */
1828 ext4_ext_free_blocks(inode_ref, ext4_ext_pblock(&newex),
1829 to_le16(newex.block_count), 0);
1833 /* previous routine could use block we allocated */
1834 newblock = ext4_ext_pblock(&newex);
1837 if (allocated > max_blocks)
1838 allocated = max_blocks;
1844 *blocks_count = allocated;
1848 ext4_ext_drop_refs(inode_ref, path, 0);