2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010 Zheng Liu <lz@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/types.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/vnode.h>
42 #include <fs/ext2fs/ext2_mount.h>
43 #include <fs/ext2fs/fs.h>
44 #include <fs/ext2fs/inode.h>
45 #include <fs/ext2fs/ext2fs.h>
46 #include <fs/ext2fs/ext2_extents.h>
47 #include <fs/ext2fs/ext2_extern.h>
49 static MALLOC_DEFINE(M_EXT2EXTENTS, "ext2_extents", "EXT2 extents");
53 ext4_ext_print_extent(struct ext4_extent *ep)
56 printf(" ext %p => (blk %u len %u start %lu)\n",
57 ep, ep->e_blk, ep->e_len,
58 (uint64_t)ep->e_start_hi << 32 | ep->e_start_lo);
61 static void ext4_ext_print_header(struct inode *ip, struct ext4_extent_header *ehp);
64 ext4_ext_print_index(struct inode *ip, struct ext4_extent_index *ex, int do_walk)
72 printf(" index %p => (blk %u pblk %lu)\n",
73 ex, ex->ei_blk, (uint64_t)ex->ei_leaf_hi << 32 | ex->ei_leaf_lo);
78 if ((error = bread(ip->i_devvp,
79 fsbtodb(fs, ((uint64_t)ex->ei_leaf_hi << 32 | ex->ei_leaf_lo)),
80 (int)fs->e2fs_bsize, NOCRED, &bp)) != 0) {
85 ext4_ext_print_header(ip, (struct ext4_extent_header *)bp->b_data);
92 ext4_ext_print_header(struct inode *ip, struct ext4_extent_header *ehp)
96 printf("header %p => (magic 0x%x entries %d max %d depth %d gen %d)\n",
97 ehp, ehp->eh_magic, ehp->eh_ecount, ehp->eh_max, ehp->eh_depth,
100 for (i = 0; i < ehp->eh_ecount; i++)
101 if (ehp->eh_depth != 0)
102 ext4_ext_print_index(ip,
103 (struct ext4_extent_index *)(ehp + 1 + i), 1);
105 ext4_ext_print_extent((struct ext4_extent *)(ehp + 1 + i));
109 ext4_ext_print_path(struct inode *ip, struct ext4_extent_path *path)
115 printf("ip=%d, Path:\n", ip->i_number);
116 for (k = 0; k <= l; k++, path++) {
117 if (path->ep_index) {
118 ext4_ext_print_index(ip, path->ep_index, 0);
119 } else if (path->ep_ext) {
120 ext4_ext_print_extent(path->ep_ext);
126 ext4_ext_print_extent_tree_status(struct inode * ip)
129 struct ext4_extent_header *ehp;
132 ehp = (struct ext4_extent_header *)(char *)ip->i_db;
134 printf("Extent status:ip=%d\n", ip->i_number);
135 if (!(ip->i_flag & IN_E4EXTENTS))
138 ext4_ext_print_header(ip, ehp);
144 static inline struct ext4_extent_header *
145 ext4_ext_inode_header(struct inode *ip)
148 return ((struct ext4_extent_header *)ip->i_db);
151 static inline struct ext4_extent_header *
152 ext4_ext_block_header(char *bdata)
155 return ((struct ext4_extent_header *)bdata);
158 static inline unsigned short
159 ext4_ext_inode_depth(struct inode *ip)
161 struct ext4_extent_header *ehp;
163 ehp = (struct ext4_extent_header *)ip->i_data;
164 return (ehp->eh_depth);
167 static inline e4fs_daddr_t
168 ext4_ext_index_pblock(struct ext4_extent_index *index)
172 blk = index->ei_leaf_lo;
173 blk |= (e4fs_daddr_t)index->ei_leaf_hi << 32;
179 ext4_index_store_pblock(struct ext4_extent_index *index, e4fs_daddr_t pb)
182 index->ei_leaf_lo = pb & 0xffffffff;
183 index->ei_leaf_hi = (pb >> 32) & 0xffff;
187 static inline e4fs_daddr_t
188 ext4_ext_extent_pblock(struct ext4_extent *extent)
192 blk = extent->e_start_lo;
193 blk |= (e4fs_daddr_t)extent->e_start_hi << 32;
199 ext4_ext_store_pblock(struct ext4_extent *ex, e4fs_daddr_t pb)
202 ex->e_start_lo = pb & 0xffffffff;
203 ex->e_start_hi = (pb >> 32) & 0xffff;
207 ext4_ext_in_cache(struct inode *ip, daddr_t lbn, struct ext4_extent *ep)
209 struct ext4_extent_cache *ecp;
210 int ret = EXT4_EXT_CACHE_NO;
212 ecp = &ip->i_ext_cache;
213 if (ecp->ec_type == EXT4_EXT_CACHE_NO)
216 if (lbn >= ecp->ec_blk && lbn < ecp->ec_blk + ecp->ec_len) {
217 ep->e_blk = ecp->ec_blk;
218 ep->e_start_lo = ecp->ec_start & 0xffffffff;
219 ep->e_start_hi = ecp->ec_start >> 32 & 0xffff;
220 ep->e_len = ecp->ec_len;
227 ext4_ext_check_header(struct inode *ip, struct ext4_extent_header *eh)
234 if (eh->eh_magic != EXT4_EXT_MAGIC) {
235 error_msg = "invalid magic";
238 if (eh->eh_max == 0) {
239 error_msg = "invalid eh_max";
242 if (eh->eh_ecount > eh->eh_max) {
243 error_msg = "invalid eh_entries";
250 ext2_fserr(fs, ip->i_uid, error_msg);
255 ext4_ext_binsearch_index(struct ext4_extent_path *path, int blk)
257 struct ext4_extent_header *eh;
258 struct ext4_extent_index *r, *l, *m;
260 eh = path->ep_header;
262 KASSERT(eh->eh_ecount <= eh->eh_max && eh->eh_ecount > 0,
263 ("ext4_ext_binsearch_index: bad args"));
265 l = EXT_FIRST_INDEX(eh) + 1;
266 r = EXT_FIRST_INDEX(eh) + eh->eh_ecount - 1;
275 path->ep_index = l - 1;
279 ext4_ext_binsearch_ext(struct ext4_extent_path *path, int blk)
281 struct ext4_extent_header *eh;
282 struct ext4_extent *r, *l, *m;
284 eh = path->ep_header;
286 KASSERT(eh->eh_ecount <= eh->eh_max,
287 ("ext4_ext_binsearch_ext: bad args"));
289 if (eh->eh_ecount == 0)
292 l = EXT_FIRST_EXTENT(eh) + 1;
293 r = EXT_FIRST_EXTENT(eh) + eh->eh_ecount - 1;
303 path->ep_ext = l - 1;
307 ext4_ext_fill_path_bdata(struct ext4_extent_path *path,
308 struct buf *bp, uint64_t blk)
311 KASSERT(path->ep_data == NULL,
312 ("ext4_ext_fill_path_bdata: bad ep_data"));
314 path->ep_data = malloc(bp->b_bufsize, M_EXT2EXTENTS, M_WAITOK);
318 memcpy(path->ep_data, bp->b_data, bp->b_bufsize);
325 ext4_ext_fill_path_buf(struct ext4_extent_path *path, struct buf *bp)
328 KASSERT(path->ep_data != NULL,
329 ("ext4_ext_fill_path_buf: bad ep_data"));
331 memcpy(bp->b_data, path->ep_data, bp->b_bufsize);
335 ext4_ext_drop_refs(struct ext4_extent_path *path)
342 depth = path->ep_depth;
343 for (i = 0; i <= depth; i++, path++)
345 free(path->ep_data, M_EXT2EXTENTS);
346 path->ep_data = NULL;
351 ext4_ext_path_free(struct ext4_extent_path *path)
357 ext4_ext_drop_refs(path);
358 free(path, M_EXT2EXTENTS);
362 ext4_ext_find_extent(struct inode *ip, daddr_t block,
363 struct ext4_extent_path **ppath)
366 struct ext4_extent_header *eh;
367 struct ext4_extent_path *path;
370 int error, depth, i, ppos, alloc;
373 eh = ext4_ext_inode_header(ip);
374 depth = ext4_ext_inode_depth(ip);
378 error = ext4_ext_check_header(ip, eh);
387 path = malloc(EXT4_EXT_DEPTH_MAX *
388 sizeof(struct ext4_extent_path),
389 M_EXT2EXTENTS, M_WAITOK | M_ZERO);
397 path[0].ep_header = eh;
398 path[0].ep_data = NULL;
400 /* Walk through the tree. */
403 ext4_ext_binsearch_index(&path[ppos], block);
404 blk = ext4_ext_index_pblock(path[ppos].ep_index);
405 path[ppos].ep_depth = i;
406 path[ppos].ep_ext = NULL;
408 error = bread(ip->i_devvp, fsbtodb(ip->i_e2fs, blk),
409 ip->i_e2fs->e2fs_bsize, NOCRED, &bp);
417 ext2_fserr(fs, ip->i_uid,
418 "ppos > depth => extent corrupted");
424 ext4_ext_fill_path_bdata(&path[ppos], bp, blk);
427 eh = ext4_ext_block_header(path[ppos].ep_data);
428 if (ext4_ext_check_header(ip, eh) ||
429 ext2_extent_blk_csum_verify(ip, path[ppos].ep_data)) {
434 path[ppos].ep_header = eh;
439 error = ext4_ext_check_header(ip, eh);
444 path[ppos].ep_depth = i;
445 path[ppos].ep_header = eh;
446 path[ppos].ep_ext = NULL;
447 path[ppos].ep_index = NULL;
448 ext4_ext_binsearch_ext(&path[ppos], block);
452 ext4_ext_drop_refs(path);
454 free(path, M_EXT2EXTENTS);
462 ext4_ext_space_root(struct inode *ip)
466 size = sizeof(ip->i_data);
467 size -= sizeof(struct ext4_extent_header);
468 size /= sizeof(struct ext4_extent);
474 ext4_ext_space_block(struct inode *ip)
481 size = (fs->e2fs_bsize - sizeof(struct ext4_extent_header)) /
482 sizeof(struct ext4_extent);
488 ext4_ext_space_block_index(struct inode *ip)
495 size = (fs->e2fs_bsize - sizeof(struct ext4_extent_header)) /
496 sizeof(struct ext4_extent_index);
502 ext4_ext_tree_init(struct inode *ip)
504 struct ext4_extent_header *ehp;
506 ip->i_flag |= IN_E4EXTENTS;
508 memset(ip->i_data, 0, EXT2_NDADDR + EXT2_NIADDR);
509 ehp = (struct ext4_extent_header *)ip->i_data;
510 ehp->eh_magic = EXT4_EXT_MAGIC;
511 ehp->eh_max = ext4_ext_space_root(ip);
512 ip->i_ext_cache.ec_type = EXT4_EXT_CACHE_NO;
513 ip->i_flag |= IN_CHANGE | IN_UPDATE;
514 ext2_update(ip->i_vnode, 1);
518 ext4_ext_put_in_cache(struct inode *ip, uint32_t blk,
519 uint32_t len, uint32_t start, int type)
522 KASSERT(len != 0, ("ext4_ext_put_in_cache: bad input"));
524 ip->i_ext_cache.ec_type = type;
525 ip->i_ext_cache.ec_blk = blk;
526 ip->i_ext_cache.ec_len = len;
527 ip->i_ext_cache.ec_start = start;
531 ext4_ext_blkpref(struct inode *ip, struct ext4_extent_path *path,
535 struct ext4_extent *ex;
536 e4fs_daddr_t bg_start;
542 depth = path->ep_depth;
543 ex = path[depth].ep_ext;
545 e4fs_daddr_t pblk = ext4_ext_extent_pblock(ex);
546 e2fs_daddr_t blk = ex->e_blk;
549 return (pblk + (block - blk));
551 return (pblk - (blk - block));
554 /* Try to get block from index itself. */
555 if (path[depth].ep_data)
556 return (path[depth].ep_blk);
559 /* Use inode's group. */
560 bg_start = (ip->i_block_group * EXT2_BLOCKS_PER_GROUP(ip->i_e2fs)) +
561 fs->e2fs->e2fs_first_dblock;
563 return (bg_start + block);
567 ext4_can_extents_be_merged(struct ext4_extent *ex1,
568 struct ext4_extent *ex2)
571 if (ex1->e_blk + ex1->e_len != ex2->e_blk)
574 if (ex1->e_len + ex2->e_len > EXT4_MAX_LEN)
577 if (ext4_ext_extent_pblock(ex1) + ex1->e_len ==
578 ext4_ext_extent_pblock(ex2))
585 ext4_ext_next_leaf_block(struct inode *ip, struct ext4_extent_path *path)
587 int depth = path->ep_depth;
591 return (EXT4_MAX_BLOCKS);
597 if (path[depth].ep_index !=
598 EXT_LAST_INDEX(path[depth].ep_header))
599 return (path[depth].ep_index[1].ei_blk);
604 return (EXT4_MAX_BLOCKS);
608 ext4_ext_dirty(struct inode *ip, struct ext4_extent_path *path)
622 bp = getblk(ip->i_devvp, fsbtodb(fs, blk),
623 fs->e2fs_bsize, 0, 0, 0);
626 ext4_ext_fill_path_buf(path, bp);
627 ext2_extent_blk_csum_set(ip, bp->b_data);
630 ip->i_flag |= IN_CHANGE | IN_UPDATE;
631 error = ext2_update(ip->i_vnode, 1);
638 ext4_ext_insert_index(struct inode *ip, struct ext4_extent_path *path,
639 uint32_t lblk, e4fs_daddr_t blk)
642 struct ext4_extent_index *idx;
647 if (lblk == path->ep_index->ei_blk) {
648 ext2_fserr(fs, ip->i_uid,
649 "lblk == index blk => extent corrupted");
653 if (path->ep_header->eh_ecount >= path->ep_header->eh_max) {
654 ext2_fserr(fs, ip->i_uid,
655 "ecout > maxcount => extent corrupted");
659 if (lblk > path->ep_index->ei_blk) {
661 idx = path->ep_index + 1;
664 idx = path->ep_index;
667 len = EXT_LAST_INDEX(path->ep_header) - idx + 1;
669 memmove(idx + 1, idx, len * sizeof(struct ext4_extent_index));
671 if (idx > EXT_MAX_INDEX(path->ep_header)) {
672 ext2_fserr(fs, ip->i_uid,
673 "index is out of range => extent corrupted");
678 ext4_index_store_pblock(idx, blk);
679 path->ep_header->eh_ecount++;
681 return (ext4_ext_dirty(ip, path));
685 ext4_ext_alloc_meta(struct inode *ip)
687 e4fs_daddr_t blk = ext2_alloc_meta(ip);
689 ip->i_blocks += btodb(ip->i_e2fs->e2fs_bsize);
690 ip->i_flag |= IN_CHANGE | IN_UPDATE;
691 ext2_update(ip->i_vnode, 1);
698 ext4_ext_blkfree(struct inode *ip, uint64_t blk, int count, int flags)
701 int i, blocksreleased;
704 blocksreleased = count;
706 for(i = 0; i < count; i++)
707 ext2_blkfree(ip, blk + i, fs->e2fs_bsize);
709 if (ip->i_blocks >= blocksreleased)
710 ip->i_blocks -= (btodb(fs->e2fs_bsize)*blocksreleased);
714 ip->i_flag |= IN_CHANGE | IN_UPDATE;
715 ext2_update(ip->i_vnode, 1);
719 ext4_ext_split(struct inode *ip, struct ext4_extent_path *path,
720 struct ext4_extent *newext, int at)
724 int depth = ext4_ext_inode_depth(ip);
725 struct ext4_extent_header *neh;
726 struct ext4_extent_index *fidx;
727 struct ext4_extent *ex;
729 e4fs_daddr_t newblk, oldblk;
731 e4fs_daddr_t *ablks = NULL;
738 * We will split at current extent for now.
740 if (path[depth].ep_ext > EXT_MAX_EXTENT(path[depth].ep_header)) {
741 ext2_fserr(fs, ip->i_uid,
742 "extent is out of range => extent corrupted");
746 if (path[depth].ep_ext != EXT_MAX_EXTENT(path[depth].ep_header))
747 border = path[depth].ep_ext[1].e_blk;
749 border = newext->e_blk;
751 /* Allocate new blocks. */
752 ablks = malloc(sizeof(e4fs_daddr_t) * depth,
753 M_EXT2EXTENTS, M_WAITOK | M_ZERO);
756 for (a = 0; a < depth - at; a++) {
757 newblk = ext4_ext_alloc_meta(ip);
764 bp = getblk(ip->i_devvp, fsbtodb(fs, newblk), fs->e2fs_bsize, 0, 0, 0);
770 neh = ext4_ext_block_header(bp->b_data);
772 neh->eh_max = ext4_ext_space_block(ip);
773 neh->eh_magic = EXT4_EXT_MAGIC;
775 ex = EXT_FIRST_EXTENT(neh);
777 if (path[depth].ep_header->eh_ecount != path[depth].ep_header->eh_max) {
778 ext2_fserr(fs, ip->i_uid,
779 "extents count out of range => extent corrupted");
784 /* Start copy from next extent. */
786 path[depth].ep_ext++;
787 while (path[depth].ep_ext <= EXT_MAX_EXTENT(path[depth].ep_header)) {
788 path[depth].ep_ext++;
792 memmove(ex, path[depth].ep_ext - m,
793 sizeof(struct ext4_extent) * m);
794 neh->eh_ecount = neh->eh_ecount + m;
797 ext2_extent_blk_csum_set(ip, bp->b_data);
803 path[depth].ep_header->eh_ecount =
804 path[depth].ep_header->eh_ecount - m;
805 ext4_ext_dirty(ip, path + depth);
808 /* Create intermediate indexes. */
810 KASSERT(k >= 0, ("ext4_ext_split: negative k"));
812 /* Insert new index into current index block. */
817 error = bread(ip->i_devvp, fsbtodb(fs, newblk),
818 (int)fs->e2fs_bsize, NOCRED, &bp);
824 neh = (struct ext4_extent_header *)bp->b_data;
826 neh->eh_magic = EXT4_EXT_MAGIC;
827 neh->eh_max = ext4_ext_space_block_index(ip);
828 neh->eh_depth = depth - i;
829 fidx = EXT_FIRST_INDEX(neh);
830 fidx->ei_blk = border;
831 ext4_index_store_pblock(fidx, oldblk);
835 while (path[i].ep_index <= EXT_MAX_INDEX(path[i].ep_header)) {
840 memmove(++fidx, path[i].ep_index - m,
841 sizeof(struct ext4_extent_index) * m);
842 neh->eh_ecount = neh->eh_ecount + m;
845 ext2_extent_blk_csum_set(ip, bp->b_data);
851 path[i].ep_header->eh_ecount =
852 path[i].ep_header->eh_ecount - m;
853 ext4_ext_dirty(ip, path + i);
859 error = ext4_ext_insert_index(ip, path + at, border, newblk);
866 for (i = 0; i < depth; i++) {
869 ext4_ext_blkfree(ip, ablks[i], 1, 0);
873 free(ablks, M_EXT2EXTENTS);
879 ext4_ext_grow_indepth(struct inode *ip, struct ext4_extent_path *path,
880 struct ext4_extent *newext)
883 struct ext4_extent_path *curpath;
884 struct ext4_extent_header *neh;
885 struct ext4_extent_index *fidx;
893 newblk = ext4_ext_alloc_meta(ip);
897 bp = getblk(ip->i_devvp, fsbtodb(fs, newblk), fs->e2fs_bsize, 0, 0, 0);
901 /* Move top-level index/leaf into new block. */
902 memmove(bp->b_data, curpath->ep_header, sizeof(ip->i_data));
904 /* Set size of new block */
905 neh = ext4_ext_block_header(bp->b_data);
906 neh->eh_magic = EXT4_EXT_MAGIC;
908 if (ext4_ext_inode_depth(ip))
909 neh->eh_max = ext4_ext_space_block_index(ip);
911 neh->eh_max = ext4_ext_space_block(ip);
913 ext2_extent_blk_csum_set(ip, bp->b_data);
920 curpath->ep_header->eh_magic = EXT4_EXT_MAGIC;
921 curpath->ep_header->eh_max = ext4_ext_space_root(ip);
922 curpath->ep_header->eh_ecount = 1;
923 curpath->ep_index = EXT_FIRST_INDEX(curpath->ep_header);
924 curpath->ep_index->ei_blk = EXT_FIRST_EXTENT(path[0].ep_header)->e_blk;
925 ext4_index_store_pblock(curpath->ep_index, newblk);
927 neh = ext4_ext_inode_header(ip);
928 fidx = EXT_FIRST_INDEX(neh);
929 neh->eh_depth = path->ep_depth + 1;
930 ext4_ext_dirty(ip, curpath);
938 ext4_ext_create_new_leaf(struct inode *ip, struct ext4_extent_path *path,
939 struct ext4_extent *newext)
942 struct ext4_extent_path *curpath;
948 i = depth = ext4_ext_inode_depth(ip);
950 /* Look for free index entry int the tree */
951 curpath = path + depth;
952 while (i > 0 && !EXT_HAS_FREE_INDEX(curpath)) {
958 * We use already allocated block for index block,
959 * so subsequent data blocks should be contiguous.
961 if (EXT_HAS_FREE_INDEX(curpath)) {
962 error = ext4_ext_split(ip, path, newext, i);
967 ext4_ext_drop_refs(path);
968 error = ext4_ext_find_extent(ip, newext->e_blk, &path);
972 /* Tree is full, do grow in depth. */
973 error = ext4_ext_grow_indepth(ip, path, newext);
978 ext4_ext_drop_refs(path);
979 error = ext4_ext_find_extent(ip, newext->e_blk, &path);
983 /* Check and split tree if required. */
984 depth = ext4_ext_inode_depth(ip);
985 if (path[depth].ep_header->eh_ecount ==
986 path[depth].ep_header->eh_max)
995 ext4_ext_correct_indexes(struct inode *ip, struct ext4_extent_path *path)
997 struct ext4_extent_header *eh;
998 struct ext4_extent *ex;
1002 depth = ext4_ext_inode_depth(ip);
1003 eh = path[depth].ep_header;
1004 ex = path[depth].ep_ext;
1006 if (ex == NULL || eh == NULL)
1012 /* We will correct tree if first leaf got modified only. */
1013 if (ex != EXT_FIRST_EXTENT(eh))
1017 border = path[depth].ep_ext->e_blk;
1018 path[k].ep_index->ei_blk = border;
1019 ext4_ext_dirty(ip, path + k);
1021 /* Change all left-side indexes. */
1022 if (path[k+1].ep_index != EXT_FIRST_INDEX(path[k+1].ep_header))
1025 path[k].ep_index->ei_blk = border;
1026 ext4_ext_dirty(ip, path + k);
1033 ext4_ext_insert_extent(struct inode *ip, struct ext4_extent_path *path,
1034 struct ext4_extent *newext)
1036 struct m_ext2fs *fs;
1037 struct ext4_extent_header * eh;
1038 struct ext4_extent *ex, *nex, *nearex;
1039 struct ext4_extent_path *npath;
1040 int depth, len, error, next;
1043 depth = ext4_ext_inode_depth(ip);
1044 ex = path[depth].ep_ext;
1047 if (newext->e_len == 0 || path[depth].ep_header == NULL)
1050 /* Insert block into found extent. */
1051 if (ex && ext4_can_extents_be_merged(ex, newext)) {
1052 ex->e_len = ex->e_len + newext->e_len;
1053 eh = path[depth].ep_header;
1059 depth = ext4_ext_inode_depth(ip);
1060 eh = path[depth].ep_header;
1061 if (eh->eh_ecount < eh->eh_max)
1065 nex = EXT_LAST_EXTENT(eh);
1066 next = ext4_ext_next_leaf_block(ip, path);
1067 if (newext->e_blk > nex->e_blk && next != EXT4_MAX_BLOCKS) {
1068 KASSERT(npath == NULL,
1069 ("ext4_ext_insert_extent: bad path"));
1071 error = ext4_ext_find_extent(ip, next, &npath);
1075 if (npath->ep_depth != path->ep_depth) {
1080 eh = npath[depth].ep_header;
1081 if (eh->eh_ecount < eh->eh_max) {
1088 * There is no free space in the found leaf,
1089 * try to add a new leaf to the tree.
1091 error = ext4_ext_create_new_leaf(ip, path, newext);
1095 depth = ext4_ext_inode_depth(ip);
1096 eh = path[depth].ep_header;
1099 nearex = path[depth].ep_ext;
1101 /* Create new extent in the leaf. */
1102 path[depth].ep_ext = EXT_FIRST_EXTENT(eh);
1103 } else if (newext->e_blk > nearex->e_blk) {
1104 if (nearex != EXT_LAST_EXTENT(eh)) {
1105 len = EXT_MAX_EXTENT(eh) - nearex;
1106 len = (len - 1) * sizeof(struct ext4_extent);
1107 len = len < 0 ? 0 : len;
1108 memmove(nearex + 2, nearex + 1, len);
1110 path[depth].ep_ext = nearex + 1;
1112 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1113 len = len < 0 ? 0 : len;
1114 memmove(nearex + 1, nearex, len);
1115 path[depth].ep_ext = nearex;
1118 eh->eh_ecount = eh->eh_ecount + 1;
1119 nearex = path[depth].ep_ext;
1120 nearex->e_blk = newext->e_blk;
1121 nearex->e_start_lo = newext->e_start_lo;
1122 nearex->e_start_hi = newext->e_start_hi;
1123 nearex->e_len = newext->e_len;
1126 /* Try to merge extents to the right. */
1127 while (nearex < EXT_LAST_EXTENT(eh)) {
1128 if (!ext4_can_extents_be_merged(nearex, nearex + 1))
1131 /* Merge with next extent. */
1132 nearex->e_len = nearex->e_len + nearex[1].e_len;
1133 if (nearex + 1 < EXT_LAST_EXTENT(eh)) {
1134 len = (EXT_LAST_EXTENT(eh) - nearex - 1) *
1135 sizeof(struct ext4_extent);
1136 memmove(nearex + 1, nearex + 2, len);
1139 eh->eh_ecount = eh->eh_ecount - 1;
1140 KASSERT(eh->eh_ecount != 0,
1141 ("ext4_ext_insert_extent: bad ecount"));
1145 * Try to merge extents to the left,
1146 * start from inexes correction.
1148 error = ext4_ext_correct_indexes(ip, path);
1152 ext4_ext_dirty(ip, path + depth);
1156 ext4_ext_drop_refs(npath);
1157 free(npath, M_EXT2EXTENTS);
1160 ip->i_ext_cache.ec_type = EXT4_EXT_CACHE_NO;
1165 ext4_new_blocks(struct inode *ip, daddr_t lbn, e4fs_daddr_t pref,
1166 struct ucred *cred, unsigned long *count, int *perror)
1168 struct m_ext2fs *fs;
1169 struct ext2mount *ump;
1170 e4fs_daddr_t newblk;
1176 * We will allocate only single block for now.
1181 EXT2_LOCK(ip->i_ump);
1182 *perror = ext2_alloc(ip, lbn, pref, (int)fs->e2fs_bsize, cred, &newblk);
1187 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1188 ext2_update(ip->i_vnode, 1);
1195 ext4_ext_get_blocks(struct inode *ip, e4fs_daddr_t iblk,
1196 unsigned long max_blocks, struct ucred *cred, struct buf **bpp,
1197 int *pallocated, daddr_t *nb)
1199 struct m_ext2fs *fs;
1200 struct buf *bp = NULL;
1201 struct ext4_extent_path *path;
1202 struct ext4_extent newex, *ex;
1203 e4fs_daddr_t bpref, newblk = 0;
1204 unsigned long allocated = 0;
1205 int error = 0, depth;
1214 if ((bpref = ext4_ext_in_cache(ip, iblk, &newex))) {
1215 if (bpref == EXT4_EXT_CACHE_IN) {
1216 /* Block is already allocated. */
1217 newblk = iblk - newex.e_blk +
1218 ext4_ext_extent_pblock(&newex);
1219 allocated = newex.e_len - (iblk - newex.e_blk);
1227 error = ext4_ext_find_extent(ip, iblk, &path);
1232 depth = ext4_ext_inode_depth(ip);
1233 if (path[depth].ep_ext == NULL && depth != 0) {
1238 if ((ex = path[depth].ep_ext)) {
1239 uint64_t lblk = ex->e_blk;
1240 uint16_t e_len = ex->e_len;
1241 e4fs_daddr_t e_start = ext4_ext_extent_pblock(ex);
1243 if (e_len > EXT4_MAX_LEN)
1246 /* If we found extent covers block, simply return it. */
1247 if (iblk >= lblk && iblk < lblk + e_len) {
1248 newblk = iblk - lblk + e_start;
1249 allocated = e_len - (iblk - lblk);
1250 ext4_ext_put_in_cache(ip, lblk, e_len,
1251 e_start, EXT4_EXT_CACHE_IN);
1256 /* Allocate the new block. */
1257 if (S_ISREG(ip->i_mode) && (!ip->i_next_alloc_block)) {
1258 ip->i_next_alloc_goal = 0;
1261 bpref = ext4_ext_blkpref(ip, path, iblk);
1262 allocated = max_blocks;
1263 newblk = ext4_new_blocks(ip, iblk, bpref, cred, &allocated, &error);
1267 /* Try to insert new extent into found leaf and return. */
1269 ext4_ext_store_pblock(&newex, newblk);
1270 newex.e_len = allocated;
1271 error = ext4_ext_insert_extent(ip, path, &newex);
1275 newblk = ext4_ext_extent_pblock(&newex);
1276 ext4_ext_put_in_cache(ip, iblk, allocated, newblk, EXT4_EXT_CACHE_IN);
1280 if (allocated > max_blocks)
1281 allocated = max_blocks;
1285 error = bread(ip->i_devvp, fsbtodb(fs, newblk),
1286 fs->e2fs_bsize, cred, &bp);
1296 ext4_ext_drop_refs(path);
1297 free(path, M_EXT2EXTENTS);
1306 static inline uint16_t
1307 ext4_ext_get_actual_len(struct ext4_extent *ext)
1310 return (ext->e_len <= EXT_INIT_MAX_LEN ?
1311 ext->e_len : (ext->e_len - EXT_INIT_MAX_LEN));
1314 static inline struct ext4_extent_header *
1315 ext4_ext_header(struct inode *ip)
1318 return (struct ext4_extent_header *)ip->i_db;
1322 ext4_remove_blocks(struct inode *ip, struct ext4_extent *ex,
1323 unsigned long from, unsigned long to)
1325 unsigned long num, start;
1327 if (from >= ex->e_blk &&
1328 to == ex->e_blk + ext4_ext_get_actual_len(ex) - 1) {
1330 num = ex->e_blk + ext4_ext_get_actual_len(ex) - from;
1331 start = ext4_ext_extent_pblock(ex) +
1332 ext4_ext_get_actual_len(ex) - num;
1333 ext4_ext_blkfree(ip, start, num, 0);
1340 ext4_ext_rm_index(struct inode *ip, struct ext4_extent_path *path)
1344 /* Free index block. */
1346 leaf = ext4_ext_index_pblock(path->ep_index);
1347 KASSERT(path->ep_header->eh_ecount != 0,
1348 ("ext4_ext_rm_index: bad ecount"));
1349 path->ep_header->eh_ecount--;
1350 ext4_ext_dirty(ip, path);
1351 ext4_ext_blkfree(ip, leaf, 1, 0);
1356 ext4_ext_rm_leaf(struct inode *ip, struct ext4_extent_path *path,
1359 struct m_ext2fs *fs;
1361 struct ext4_extent_header *eh;
1362 unsigned int a, b, block, num;
1363 unsigned long ex_blk;
1364 unsigned short ex_len;
1365 struct ext4_extent *ex;
1366 int error, correct_index;
1369 depth = ext4_ext_inode_depth(ip);
1372 if (!path[depth].ep_header) {
1373 if (path[depth].ep_data == NULL)
1375 path[depth].ep_header =
1376 (struct ext4_extent_header* )path[depth].ep_data;
1379 eh = path[depth].ep_header;
1381 ext2_fserr(fs, ip->i_uid, "bad header => extent corrupted");
1385 ex = EXT_LAST_EXTENT(eh);
1387 ex_len = ext4_ext_get_actual_len(ex);
1389 while (ex >= EXT_FIRST_EXTENT(eh) && ex_blk + ex_len > start) {
1390 path[depth].ep_ext = ex;
1391 a = ex_blk > start ? ex_blk : start;
1392 b = (uint64_t)ex_blk + ex_len - 1 <
1393 EXT4_MAX_BLOCKS ? ex_blk + ex_len - 1 : EXT4_MAX_BLOCKS;
1395 if (a != ex_blk && b != ex_blk + ex_len - 1)
1397 else if (a != ex_blk) {
1398 /* Remove tail of the extent. */
1401 } else if (b != ex_blk + ex_len - 1) {
1402 /* Remove head of the extent, not implemented. */
1405 /* Remove whole extent. */
1410 if (ex == EXT_FIRST_EXTENT(eh))
1413 error = ext4_remove_blocks(ip, ex, a, b);
1418 ext4_ext_store_pblock(ex, 0);
1425 ext4_ext_dirty(ip, path + depth);
1429 ex_len = ext4_ext_get_actual_len(ex);
1432 if (correct_index && eh->eh_ecount)
1433 error = ext4_ext_correct_indexes(ip, path);
1436 * If this leaf is free, we should
1437 * remove it from index block above.
1439 if (error == 0 && eh->eh_ecount == 0 && path[depth].ep_data != NULL)
1440 error = ext4_ext_rm_index(ip, path + depth);
1447 ext4_read_extent_tree_block(struct inode *ip, e4fs_daddr_t pblk,
1448 int depth, int flags)
1450 struct m_ext2fs *fs;
1451 struct ext4_extent_header *eh;
1457 error = bread(ip->i_devvp, fsbtodb(fs, pblk),
1458 fs->e2fs_bsize, NOCRED, &bp);
1464 eh = ext4_ext_block_header(bp->b_data);
1465 if (eh->eh_depth != depth) {
1466 ext2_fserr(fs, ip->i_uid, "unexpected eh_depth");
1470 error = ext4_ext_check_header(ip, eh);
1483 ext4_ext_more_to_rm(struct ext4_extent_path *path)
1486 KASSERT(path->ep_index != NULL,
1487 ("ext4_ext_more_to_rm: bad index from path"));
1489 if (path->ep_index < EXT_FIRST_INDEX(path->ep_header))
1492 if (path->ep_header->eh_ecount == path->index_count)
1499 ext4_ext_remove_space(struct inode *ip, off_t length, int flags,
1500 struct ucred *cred, struct thread *td)
1503 struct ext4_extent_header *ehp;
1504 struct ext4_extent_path *path;
1508 ehp = (struct ext4_extent_header *)ip->i_db;
1509 depth = ext4_ext_inode_depth(ip);
1511 error = ext4_ext_check_header(ip, ehp);
1515 path = malloc(sizeof(struct ext4_extent_path) * (depth + 1),
1516 M_EXT2EXTENTS, M_WAITOK | M_ZERO);
1521 path[0].ep_header = ehp;
1522 path[0].ep_depth = depth;
1523 while (i >= 0 && error == 0) {
1526 error = ext4_ext_rm_leaf(ip, path, length);
1529 free(path[i].ep_data, M_EXT2EXTENTS);
1530 path[i].ep_data = NULL;
1535 /* This is index. */
1536 if (!path[i].ep_header)
1538 (struct ext4_extent_header *)path[i].ep_data;
1540 if (!path[i].ep_index) {
1541 /* This level hasn't touched yet. */
1542 path[i].ep_index = EXT_LAST_INDEX(path[i].ep_header);
1543 path[i].index_count = path[i].ep_header->eh_ecount + 1;
1545 /* We've already was here, see at next index. */
1549 if (ext4_ext_more_to_rm(path + i)) {
1550 memset(path + i + 1, 0, sizeof(*path));
1551 bp = ext4_read_extent_tree_block(ip,
1552 ext4_ext_index_pblock(path[i].ep_index),
1553 path[0].ep_depth - (i + 1), 0);
1559 ext4_ext_fill_path_bdata(&path[i+1], bp,
1560 ext4_ext_index_pblock(path[i].ep_index));
1562 path[i].index_count = path[i].ep_header->eh_ecount;
1565 if (path[i].ep_header->eh_ecount == 0 && i > 0) {
1566 /* Index is empty, remove it. */
1567 error = ext4_ext_rm_index(ip, path + i);
1569 free(path[i].ep_data, M_EXT2EXTENTS);
1570 path[i].ep_data = NULL;
1575 if (path->ep_header->eh_ecount == 0) {
1577 * Truncate the tree to zero.
1579 ext4_ext_header(ip)->eh_depth = 0;
1580 ext4_ext_header(ip)->eh_max = ext4_ext_space_root(ip);
1581 ext4_ext_dirty(ip, path);
1585 ext4_ext_drop_refs(path);
1586 free(path, M_EXT2EXTENTS);