2 * Copyright (c) 2010 Zheng Liu <lz@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/types.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
34 #include <sys/vnode.h>
40 #include <fs/ext2fs/ext2_mount.h>
41 #include <fs/ext2fs/fs.h>
42 #include <fs/ext2fs/inode.h>
43 #include <fs/ext2fs/ext2fs.h>
44 #include <fs/ext2fs/ext2_extents.h>
45 #include <fs/ext2fs/ext2_extern.h>
47 static MALLOC_DEFINE(M_EXT2EXTENTS, "ext2_extents", "EXT2 extents");
51 ext4_ext_print_extent(struct ext4_extent *ep)
54 printf(" ext %p => (blk %u len %u start %lu)\n",
55 ep, ep->e_blk, ep->e_len,
56 (uint64_t)ep->e_start_hi << 32 | ep->e_start_lo);
59 static void ext4_ext_print_header(struct inode *ip, struct ext4_extent_header *ehp);
62 ext4_ext_print_index(struct inode *ip, struct ext4_extent_index *ex, int do_walk)
70 printf(" index %p => (blk %u pblk %lu)\n",
71 ex, ex->ei_blk, (uint64_t)ex->ei_leaf_hi << 32 | ex->ei_leaf_lo);
76 if ((error = bread(ip->i_devvp,
77 fsbtodb(fs, ((uint64_t)ex->ei_leaf_hi << 32 | ex->ei_leaf_lo)),
78 (int)fs->e2fs_bsize, NOCRED, &bp)) != 0) {
83 ext4_ext_print_header(ip, (struct ext4_extent_header *)bp->b_data);
90 ext4_ext_print_header(struct inode *ip, struct ext4_extent_header *ehp)
94 printf("header %p => (magic 0x%x entries %d max %d depth %d gen %d)\n",
95 ehp, ehp->eh_magic, ehp->eh_ecount, ehp->eh_max, ehp->eh_depth,
98 for (i = 0; i < ehp->eh_ecount; i++)
99 if (ehp->eh_depth != 0)
100 ext4_ext_print_index(ip,
101 (struct ext4_extent_index *)(ehp + 1 + i), 1);
103 ext4_ext_print_extent((struct ext4_extent *)(ehp + 1 + i));
107 ext4_ext_print_path(struct inode *ip, struct ext4_extent_path *path)
113 printf("ip=%d, Path:\n", ip->i_number);
114 for (k = 0; k <= l; k++, path++) {
115 if (path->ep_index) {
116 ext4_ext_print_index(ip, path->ep_index, 0);
117 } else if (path->ep_ext) {
118 ext4_ext_print_extent(path->ep_ext);
124 ext4_ext_print_extent_tree_status(struct inode * ip)
127 struct ext4_extent_header *ehp;
130 ehp = (struct ext4_extent_header *)(char *)ip->i_db;
132 printf("Extent status:ip=%d\n", ip->i_number);
133 if (!(ip->i_flag & IN_E4EXTENTS))
136 ext4_ext_print_header(ip, ehp);
142 static inline struct ext4_extent_header *
143 ext4_ext_inode_header(struct inode *ip)
146 return ((struct ext4_extent_header *)ip->i_db);
149 static inline struct ext4_extent_header *
150 ext4_ext_block_header(char *bdata)
153 return ((struct ext4_extent_header *)bdata);
156 static inline unsigned short
157 ext4_ext_inode_depth(struct inode *ip)
159 struct ext4_extent_header *ehp;
161 ehp = (struct ext4_extent_header *)ip->i_data;
162 return (ehp->eh_depth);
165 static inline e4fs_daddr_t
166 ext4_ext_index_pblock(struct ext4_extent_index *index)
170 blk = index->ei_leaf_lo;
171 blk |= (e4fs_daddr_t)index->ei_leaf_hi << 32;
177 ext4_index_store_pblock(struct ext4_extent_index *index, e4fs_daddr_t pb)
180 index->ei_leaf_lo = pb & 0xffffffff;
181 index->ei_leaf_hi = (pb >> 32) & 0xffff;
185 static inline e4fs_daddr_t
186 ext4_ext_extent_pblock(struct ext4_extent *extent)
190 blk = extent->e_start_lo;
191 blk |= (e4fs_daddr_t)extent->e_start_hi << 32;
197 ext4_ext_store_pblock(struct ext4_extent *ex, e4fs_daddr_t pb)
200 ex->e_start_lo = pb & 0xffffffff;
201 ex->e_start_hi = (pb >> 32) & 0xffff;
205 ext4_ext_in_cache(struct inode *ip, daddr_t lbn, struct ext4_extent *ep)
207 struct ext4_extent_cache *ecp;
208 int ret = EXT4_EXT_CACHE_NO;
210 ecp = &ip->i_ext_cache;
211 if (ecp->ec_type == EXT4_EXT_CACHE_NO)
214 if (lbn >= ecp->ec_blk && lbn < ecp->ec_blk + ecp->ec_len) {
215 ep->e_blk = ecp->ec_blk;
216 ep->e_start_lo = ecp->ec_start & 0xffffffff;
217 ep->e_start_hi = ecp->ec_start >> 32 & 0xffff;
218 ep->e_len = ecp->ec_len;
225 ext4_ext_check_header(struct inode *ip, struct ext4_extent_header *eh)
232 if (eh->eh_magic != EXT4_EXT_MAGIC) {
233 error_msg = "invalid magic";
236 if (eh->eh_max == 0) {
237 error_msg = "invalid eh_max";
240 if (eh->eh_ecount > eh->eh_max) {
241 error_msg = "invalid eh_entries";
248 ext2_fserr(fs, ip->i_uid, error_msg);
253 ext4_ext_binsearch_index(struct ext4_extent_path *path, int blk)
255 struct ext4_extent_header *eh;
256 struct ext4_extent_index *r, *l, *m;
258 eh = path->ep_header;
260 KASSERT(eh->eh_ecount <= eh->eh_max && eh->eh_ecount > 0,
261 ("ext4_ext_binsearch_index: bad args"));
263 l = EXT_FIRST_INDEX(eh) + 1;
264 r = EXT_FIRST_INDEX(eh) + eh->eh_ecount - 1;
273 path->ep_index = l - 1;
277 ext4_ext_binsearch_ext(struct ext4_extent_path *path, int blk)
279 struct ext4_extent_header *eh;
280 struct ext4_extent *r, *l, *m;
282 eh = path->ep_header;
284 KASSERT(eh->eh_ecount <= eh->eh_max,
285 ("ext4_ext_binsearch_ext: bad args"));
287 if (eh->eh_ecount == 0)
290 l = EXT_FIRST_EXTENT(eh) + 1;
291 r = EXT_FIRST_EXTENT(eh) + eh->eh_ecount - 1;
301 path->ep_ext = l - 1;
305 ext4_ext_fill_path_bdata(struct ext4_extent_path *path,
306 struct buf *bp, uint64_t blk)
309 KASSERT(path->ep_data == NULL,
310 ("ext4_ext_fill_path_bdata: bad ep_data"));
312 path->ep_data = malloc(bp->b_bufsize, M_EXT2EXTENTS, M_WAITOK);
316 memcpy(path->ep_data, bp->b_data, bp->b_bufsize);
323 ext4_ext_fill_path_buf(struct ext4_extent_path *path, struct buf *bp)
326 KASSERT(path->ep_data != NULL,
327 ("ext4_ext_fill_path_buf: bad ep_data"));
329 memcpy(bp->b_data, path->ep_data, bp->b_bufsize);
333 ext4_ext_drop_refs(struct ext4_extent_path *path)
340 depth = path->ep_depth;
341 for (i = 0; i <= depth; i++, path++)
343 free(path->ep_data, M_EXT2EXTENTS);
344 path->ep_data = NULL;
349 ext4_ext_path_free(struct ext4_extent_path *path)
355 ext4_ext_drop_refs(path);
356 free(path, M_EXT2EXTENTS);
360 ext4_ext_find_extent(struct inode *ip, daddr_t block,
361 struct ext4_extent_path **ppath)
364 struct ext4_extent_header *eh;
365 struct ext4_extent_path *path;
368 int error, depth, i, ppos, alloc;
371 eh = ext4_ext_inode_header(ip);
372 depth = ext4_ext_inode_depth(ip);
376 error = ext4_ext_check_header(ip, eh);
385 path = malloc(EXT4_EXT_DEPTH_MAX *
386 sizeof(struct ext4_extent_path),
387 M_EXT2EXTENTS, M_WAITOK | M_ZERO);
395 path[0].ep_header = eh;
396 path[0].ep_data = NULL;
398 /* Walk through the tree. */
401 ext4_ext_binsearch_index(&path[ppos], block);
402 blk = ext4_ext_index_pblock(path[ppos].ep_index);
403 path[ppos].ep_depth = i;
404 path[ppos].ep_ext = NULL;
406 error = bread(ip->i_devvp, fsbtodb(ip->i_e2fs, blk),
407 ip->i_e2fs->e2fs_bsize, NOCRED, &bp);
415 ext2_fserr(fs, ip->i_uid,
416 "ppos > depth => extent corrupted");
422 ext4_ext_fill_path_bdata(&path[ppos], bp, blk);
425 eh = ext4_ext_block_header(path[ppos].ep_data);
426 error = ext4_ext_check_header(ip, eh);
430 path[ppos].ep_header = eh;
435 error = ext4_ext_check_header(ip, eh);
440 path[ppos].ep_depth = i;
441 path[ppos].ep_header = eh;
442 path[ppos].ep_ext = NULL;
443 path[ppos].ep_index = NULL;
444 ext4_ext_binsearch_ext(&path[ppos], block);
448 ext4_ext_drop_refs(path);
450 free(path, M_EXT2EXTENTS);
458 ext4_ext_space_root(struct inode *ip)
462 size = sizeof(ip->i_data);
463 size -= sizeof(struct ext4_extent_header);
464 size /= sizeof(struct ext4_extent);
470 ext4_ext_space_block(struct inode *ip)
477 size = (fs->e2fs_bsize - sizeof(struct ext4_extent_header)) /
478 sizeof(struct ext4_extent);
484 ext4_ext_space_block_index(struct inode *ip)
491 size = (fs->e2fs_bsize - sizeof(struct ext4_extent_header)) /
492 sizeof(struct ext4_extent_index);
498 ext4_ext_tree_init(struct inode *ip)
500 struct ext4_extent_header *ehp;
502 ip->i_flag |= IN_E4EXTENTS;
504 memset(ip->i_data, 0, EXT2_NDADDR + EXT2_NIADDR);
505 ehp = (struct ext4_extent_header *)ip->i_data;
506 ehp->eh_magic = EXT4_EXT_MAGIC;
507 ehp->eh_max = ext4_ext_space_root(ip);
508 ip->i_ext_cache.ec_type = EXT4_EXT_CACHE_NO;
509 ip->i_flag |= IN_CHANGE | IN_UPDATE;
510 ext2_update(ip->i_vnode, 1);
514 ext4_ext_put_in_cache(struct inode *ip, uint32_t blk,
515 uint32_t len, uint32_t start, int type)
518 KASSERT(len != 0, ("ext4_ext_put_in_cache: bad input"));
520 ip->i_ext_cache.ec_type = type;
521 ip->i_ext_cache.ec_blk = blk;
522 ip->i_ext_cache.ec_len = len;
523 ip->i_ext_cache.ec_start = start;
527 ext4_ext_blkpref(struct inode *ip, struct ext4_extent_path *path,
531 struct ext4_extent *ex;
532 e4fs_daddr_t bg_start;
538 depth = path->ep_depth;
539 ex = path[depth].ep_ext;
541 e4fs_daddr_t pblk = ext4_ext_extent_pblock(ex);
542 e2fs_daddr_t blk = ex->e_blk;
545 return (pblk + (block - blk));
547 return (pblk - (blk - block));
550 /* Try to get block from index itself. */
551 if (path[depth].ep_data)
552 return (path[depth].ep_blk);
555 /* Use inode's group. */
556 bg_start = (ip->i_block_group * EXT2_BLOCKS_PER_GROUP(ip->i_e2fs)) +
557 fs->e2fs->e2fs_first_dblock;
559 return (bg_start + block);
563 ext4_can_extents_be_merged(struct ext4_extent *ex1,
564 struct ext4_extent *ex2)
567 if (ex1->e_blk + ex1->e_len != ex2->e_blk)
570 if (ex1->e_len + ex2->e_len > EXT4_MAX_LEN)
573 if (ext4_ext_extent_pblock(ex1) + ex1->e_len ==
574 ext4_ext_extent_pblock(ex2))
581 ext4_ext_next_leaf_block(struct inode *ip, struct ext4_extent_path *path)
583 int depth = path->ep_depth;
587 return (EXT4_MAX_BLOCKS);
593 if (path[depth].ep_index !=
594 EXT_LAST_INDEX(path[depth].ep_header))
595 return (path[depth].ep_index[1].ei_blk);
600 return (EXT4_MAX_BLOCKS);
604 ext4_ext_dirty(struct inode *ip, struct ext4_extent_path *path)
618 bp = getblk(ip->i_devvp, fsbtodb(fs, blk),
619 fs->e2fs_bsize, 0, 0, 0);
622 ext4_ext_fill_path_buf(path, bp);
625 ip->i_flag |= IN_CHANGE | IN_UPDATE;
626 error = ext2_update(ip->i_vnode, 1);
633 ext4_ext_insert_index(struct inode *ip, struct ext4_extent_path *path,
634 uint32_t lblk, e4fs_daddr_t blk)
637 struct ext4_extent_index *idx;
642 if (lblk == path->ep_index->ei_blk) {
643 ext2_fserr(fs, ip->i_uid,
644 "lblk == index blk => extent corrupted");
648 if (path->ep_header->eh_ecount >= path->ep_header->eh_max) {
649 ext2_fserr(fs, ip->i_uid,
650 "ecout > maxcount => extent corrupted");
654 if (lblk > path->ep_index->ei_blk) {
656 idx = path->ep_index + 1;
659 idx = path->ep_index;
662 len = EXT_LAST_INDEX(path->ep_header) - idx + 1;
664 memmove(idx + 1, idx, len * sizeof(struct ext4_extent_index));
666 if (idx > EXT_MAX_INDEX(path->ep_header)) {
667 ext2_fserr(fs, ip->i_uid,
668 "index is out of range => extent corrupted");
673 ext4_index_store_pblock(idx, blk);
674 path->ep_header->eh_ecount++;
676 return (ext4_ext_dirty(ip, path));
680 ext4_ext_alloc_meta(struct inode *ip)
682 e4fs_daddr_t blk = ext2_alloc_meta(ip);
684 ip->i_blocks += btodb(ip->i_e2fs->e2fs_bsize);
685 ip->i_flag |= IN_CHANGE | IN_UPDATE;
686 ext2_update(ip->i_vnode, 1);
693 ext4_ext_blkfree(struct inode *ip, uint64_t blk, int count, int flags)
696 int i, blocksreleased;
699 blocksreleased = count;
701 for(i = 0; i < count; i++)
702 ext2_blkfree(ip, blk + i, fs->e2fs_bsize);
704 if (ip->i_blocks >= blocksreleased)
705 ip->i_blocks -= (btodb(fs->e2fs_bsize)*blocksreleased);
709 ip->i_flag |= IN_CHANGE | IN_UPDATE;
710 ext2_update(ip->i_vnode, 1);
714 ext4_ext_split(struct inode *ip, struct ext4_extent_path *path,
715 struct ext4_extent *newext, int at)
719 int depth = ext4_ext_inode_depth(ip);
720 struct ext4_extent_header *neh;
721 struct ext4_extent_index *fidx;
722 struct ext4_extent *ex;
724 e4fs_daddr_t newblk, oldblk;
726 e4fs_daddr_t *ablks = NULL;
733 * We will split at current extent for now.
735 if (path[depth].ep_ext > EXT_MAX_EXTENT(path[depth].ep_header)) {
736 ext2_fserr(fs, ip->i_uid,
737 "extent is out of range => extent corrupted");
741 if (path[depth].ep_ext != EXT_MAX_EXTENT(path[depth].ep_header))
742 border = path[depth].ep_ext[1].e_blk;
744 border = newext->e_blk;
746 /* Allocate new blocks. */
747 ablks = malloc(sizeof(e4fs_daddr_t) * depth,
748 M_EXT2EXTENTS, M_WAITOK | M_ZERO);
751 for (a = 0; a < depth - at; a++) {
752 newblk = ext4_ext_alloc_meta(ip);
759 bp = getblk(ip->i_devvp, fsbtodb(fs, newblk), fs->e2fs_bsize, 0, 0, 0);
765 neh = ext4_ext_block_header(bp->b_data);
767 neh->eh_max = ext4_ext_space_block(ip);
768 neh->eh_magic = EXT4_EXT_MAGIC;
770 ex = EXT_FIRST_EXTENT(neh);
772 if (path[depth].ep_header->eh_ecount != path[depth].ep_header->eh_max) {
773 ext2_fserr(fs, ip->i_uid,
774 "extents count out of range => extent corrupted");
779 /* Start copy from next extent. */
781 path[depth].ep_ext++;
782 while (path[depth].ep_ext <= EXT_MAX_EXTENT(path[depth].ep_header)) {
783 path[depth].ep_ext++;
787 memmove(ex, path[depth].ep_ext - m,
788 sizeof(struct ext4_extent) * m);
789 neh->eh_ecount = neh->eh_ecount + m;
797 path[depth].ep_header->eh_ecount =
798 path[depth].ep_header->eh_ecount - m;
799 ext4_ext_dirty(ip, path + depth);
802 /* Create intermediate indexes. */
804 KASSERT(k >= 0, ("ext4_ext_split: negative k"));
806 /* Insert new index into current index block. */
811 error = bread(ip->i_devvp, fsbtodb(fs, newblk),
812 (int)fs->e2fs_bsize, NOCRED, &bp);
818 neh = (struct ext4_extent_header *)bp->b_data;
820 neh->eh_magic = EXT4_EXT_MAGIC;
821 neh->eh_max = ext4_ext_space_block_index(ip);
822 neh->eh_depth = depth - i;
823 fidx = EXT_FIRST_INDEX(neh);
824 fidx->ei_blk = border;
825 ext4_index_store_pblock(fidx, oldblk);
829 while (path[i].ep_index <= EXT_MAX_INDEX(path[i].ep_header)) {
834 memmove(++fidx, path[i].ep_index - m,
835 sizeof(struct ext4_extent_index) * m);
836 neh->eh_ecount = neh->eh_ecount + m;
844 path[i].ep_header->eh_ecount =
845 path[i].ep_header->eh_ecount - m;
846 ext4_ext_dirty(ip, path + i);
852 error = ext4_ext_insert_index(ip, path + at, border, newblk);
859 for (i = 0; i < depth; i++) {
862 ext4_ext_blkfree(ip, ablks[i], 1, 0);
866 free(ablks, M_EXT2EXTENTS);
872 ext4_ext_grow_indepth(struct inode *ip, struct ext4_extent_path *path,
873 struct ext4_extent *newext)
876 struct ext4_extent_path *curpath;
877 struct ext4_extent_header *neh;
878 struct ext4_extent_index *fidx;
886 newblk = ext4_ext_alloc_meta(ip);
890 bp = getblk(ip->i_devvp, fsbtodb(fs, newblk), fs->e2fs_bsize, 0, 0, 0);
894 /* Move top-level index/leaf into new block. */
895 memmove(bp->b_data, curpath->ep_header, sizeof(ip->i_data));
897 /* Set size of new block */
898 neh = ext4_ext_block_header(bp->b_data);
899 neh->eh_magic = EXT4_EXT_MAGIC;
901 if (ext4_ext_inode_depth(ip))
902 neh->eh_max = ext4_ext_space_block_index(ip);
904 neh->eh_max = ext4_ext_space_block(ip);
912 curpath->ep_header->eh_magic = EXT4_EXT_MAGIC;
913 curpath->ep_header->eh_max = ext4_ext_space_root(ip);
914 curpath->ep_header->eh_ecount = 1;
915 curpath->ep_index = EXT_FIRST_INDEX(curpath->ep_header);
916 curpath->ep_index->ei_blk = EXT_FIRST_EXTENT(path[0].ep_header)->e_blk;
917 ext4_index_store_pblock(curpath->ep_index, newblk);
919 neh = ext4_ext_inode_header(ip);
920 fidx = EXT_FIRST_INDEX(neh);
921 neh->eh_depth = path->ep_depth + 1;
922 ext4_ext_dirty(ip, curpath);
930 ext4_ext_create_new_leaf(struct inode *ip, struct ext4_extent_path *path,
931 struct ext4_extent *newext)
934 struct ext4_extent_path *curpath;
940 i = depth = ext4_ext_inode_depth(ip);
942 /* Look for free index entry int the tree */
943 curpath = path + depth;
944 while (i > 0 && !EXT_HAS_FREE_INDEX(curpath)) {
950 * We use already allocated block for index block,
951 * so subsequent data blocks should be contiguous.
953 if (EXT_HAS_FREE_INDEX(curpath)) {
954 error = ext4_ext_split(ip, path, newext, i);
959 ext4_ext_drop_refs(path);
960 error = ext4_ext_find_extent(ip, newext->e_blk, &path);
964 /* Tree is full, do grow in depth. */
965 error = ext4_ext_grow_indepth(ip, path, newext);
970 ext4_ext_drop_refs(path);
971 error = ext4_ext_find_extent(ip, newext->e_blk, &path);
975 /* Check and split tree if required. */
976 depth = ext4_ext_inode_depth(ip);
977 if (path[depth].ep_header->eh_ecount ==
978 path[depth].ep_header->eh_max)
987 ext4_ext_correct_indexes(struct inode *ip, struct ext4_extent_path *path)
989 struct ext4_extent_header *eh;
990 struct ext4_extent *ex;
994 depth = ext4_ext_inode_depth(ip);
995 eh = path[depth].ep_header;
996 ex = path[depth].ep_ext;
998 if (ex == NULL || eh == NULL)
1004 /* We will correct tree if first leaf got modified only. */
1005 if (ex != EXT_FIRST_EXTENT(eh))
1009 border = path[depth].ep_ext->e_blk;
1010 path[k].ep_index->ei_blk = border;
1011 ext4_ext_dirty(ip, path + k);
1013 /* Change all left-side indexes. */
1014 if (path[k+1].ep_index != EXT_FIRST_INDEX(path[k+1].ep_header))
1017 path[k].ep_index->ei_blk = border;
1018 ext4_ext_dirty(ip, path + k);
1025 ext4_ext_insert_extent(struct inode *ip, struct ext4_extent_path *path,
1026 struct ext4_extent *newext)
1028 struct m_ext2fs *fs;
1029 struct ext4_extent_header * eh;
1030 struct ext4_extent *ex, *nex, *nearex;
1031 struct ext4_extent_path *npath;
1032 int depth, len, error, next;
1035 depth = ext4_ext_inode_depth(ip);
1036 ex = path[depth].ep_ext;
1039 if (newext->e_len == 0 || path[depth].ep_header == NULL)
1042 /* Insert block into found extent. */
1043 if (ex && ext4_can_extents_be_merged(ex, newext)) {
1044 ex->e_len = ex->e_len + newext->e_len;
1045 eh = path[depth].ep_header;
1051 depth = ext4_ext_inode_depth(ip);
1052 eh = path[depth].ep_header;
1053 if (eh->eh_ecount < eh->eh_max)
1057 nex = EXT_LAST_EXTENT(eh);
1058 next = ext4_ext_next_leaf_block(ip, path);
1059 if (newext->e_blk > nex->e_blk && next != EXT4_MAX_BLOCKS) {
1060 KASSERT(npath == NULL,
1061 ("ext4_ext_insert_extent: bad path"));
1063 error = ext4_ext_find_extent(ip, next, &npath);
1067 if (npath->ep_depth != path->ep_depth) {
1072 eh = npath[depth].ep_header;
1073 if (eh->eh_ecount < eh->eh_max) {
1080 * There is no free space in the found leaf,
1081 * try to add a new leaf to the tree.
1083 error = ext4_ext_create_new_leaf(ip, path, newext);
1087 depth = ext4_ext_inode_depth(ip);
1088 eh = path[depth].ep_header;
1091 nearex = path[depth].ep_ext;
1093 /* Create new extent in the leaf. */
1094 path[depth].ep_ext = EXT_FIRST_EXTENT(eh);
1095 } else if (newext->e_blk > nearex->e_blk) {
1096 if (nearex != EXT_LAST_EXTENT(eh)) {
1097 len = EXT_MAX_EXTENT(eh) - nearex;
1098 len = (len - 1) * sizeof(struct ext4_extent);
1099 len = len < 0 ? 0 : len;
1100 memmove(nearex + 2, nearex + 1, len);
1102 path[depth].ep_ext = nearex + 1;
1104 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1105 len = len < 0 ? 0 : len;
1106 memmove(nearex + 1, nearex, len);
1107 path[depth].ep_ext = nearex;
1110 eh->eh_ecount = eh->eh_ecount + 1;
1111 nearex = path[depth].ep_ext;
1112 nearex->e_blk = newext->e_blk;
1113 nearex->e_start_lo = newext->e_start_lo;
1114 nearex->e_start_hi = newext->e_start_hi;
1115 nearex->e_len = newext->e_len;
1118 /* Try to merge extents to the right. */
1119 while (nearex < EXT_LAST_EXTENT(eh)) {
1120 if (!ext4_can_extents_be_merged(nearex, nearex + 1))
1123 /* Merge with next extent. */
1124 nearex->e_len = nearex->e_len + nearex[1].e_len;
1125 if (nearex + 1 < EXT_LAST_EXTENT(eh)) {
1126 len = (EXT_LAST_EXTENT(eh) - nearex - 1) *
1127 sizeof(struct ext4_extent);
1128 memmove(nearex + 1, nearex + 2, len);
1131 eh->eh_ecount = eh->eh_ecount - 1;
1132 KASSERT(eh->eh_ecount != 0,
1133 ("ext4_ext_insert_extent: bad ecount"));
1137 * Try to merge extents to the left,
1138 * start from inexes correction.
1140 error = ext4_ext_correct_indexes(ip, path);
1144 ext4_ext_dirty(ip, path + depth);
1148 ext4_ext_drop_refs(npath);
1149 free(npath, M_EXT2EXTENTS);
1152 ip->i_ext_cache.ec_type = EXT4_EXT_CACHE_NO;
1157 ext4_new_blocks(struct inode *ip, daddr_t lbn, e4fs_daddr_t pref,
1158 struct ucred *cred, unsigned long *count, int *perror)
1160 struct m_ext2fs *fs;
1161 struct ext2mount *ump;
1162 e4fs_daddr_t newblk;
1168 * We will allocate only single block for now.
1173 EXT2_LOCK(ip->i_ump);
1174 *perror = ext2_alloc(ip, lbn, pref, (int)fs->e2fs_bsize, cred, &newblk);
1179 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1180 ext2_update(ip->i_vnode, 1);
1187 ext4_ext_get_blocks(struct inode *ip, e4fs_daddr_t iblk,
1188 unsigned long max_blocks, struct ucred *cred, struct buf **bpp,
1189 int *pallocated, uint32_t *nb)
1191 struct m_ext2fs *fs;
1192 struct buf *bp = NULL;
1193 struct ext4_extent_path *path;
1194 struct ext4_extent newex, *ex;
1195 e4fs_daddr_t bpref, newblk = 0;
1196 unsigned long allocated = 0;
1197 int error = 0, depth;
1206 if ((bpref = ext4_ext_in_cache(ip, iblk, &newex))) {
1207 if (bpref == EXT4_EXT_CACHE_IN) {
1208 /* Block is already allocated. */
1209 newblk = iblk - newex.e_blk +
1210 ext4_ext_extent_pblock(&newex);
1211 allocated = newex.e_len - (iblk - newex.e_blk);
1219 error = ext4_ext_find_extent(ip, iblk, &path);
1224 depth = ext4_ext_inode_depth(ip);
1225 if (path[depth].ep_ext == NULL && depth != 0) {
1230 if ((ex = path[depth].ep_ext)) {
1231 uint64_t lblk = ex->e_blk;
1232 uint16_t e_len = ex->e_len;
1233 e4fs_daddr_t e_start = ext4_ext_extent_pblock(ex);
1235 if (e_len > EXT4_MAX_LEN)
1238 /* If we found extent covers block, simply return it. */
1239 if (iblk >= lblk && iblk < lblk + e_len) {
1240 newblk = iblk - lblk + e_start;
1241 allocated = e_len - (iblk - lblk);
1242 ext4_ext_put_in_cache(ip, lblk, e_len,
1243 e_start, EXT4_EXT_CACHE_IN);
1248 /* Allocate the new block. */
1249 if (S_ISREG(ip->i_mode) && (!ip->i_next_alloc_block)) {
1250 ip->i_next_alloc_goal = 0;
1253 bpref = ext4_ext_blkpref(ip, path, iblk);
1254 allocated = max_blocks;
1255 newblk = ext4_new_blocks(ip, iblk, bpref, cred, &allocated, &error);
1259 /* Try to insert new extent into found leaf and return. */
1261 ext4_ext_store_pblock(&newex, newblk);
1262 newex.e_len = allocated;
1263 error = ext4_ext_insert_extent(ip, path, &newex);
1267 newblk = ext4_ext_extent_pblock(&newex);
1268 ext4_ext_put_in_cache(ip, iblk, allocated, newblk, EXT4_EXT_CACHE_IN);
1272 if (allocated > max_blocks)
1273 allocated = max_blocks;
1277 error = bread(ip->i_devvp, fsbtodb(fs, newblk),
1278 fs->e2fs_bsize, cred, &bp);
1288 ext4_ext_drop_refs(path);
1289 free(path, M_EXT2EXTENTS);
1298 static inline uint16_t
1299 ext4_ext_get_actual_len(struct ext4_extent *ext)
1302 return (ext->e_len <= EXT_INIT_MAX_LEN ?
1303 ext->e_len : (ext->e_len - EXT_INIT_MAX_LEN));
1306 static inline struct ext4_extent_header *
1307 ext4_ext_header(struct inode *ip)
1310 return (struct ext4_extent_header *)ip->i_db;
1314 ext4_remove_blocks(struct inode *ip, struct ext4_extent *ex,
1315 unsigned long from, unsigned long to)
1317 unsigned long num, start;
1319 if (from >= ex->e_blk &&
1320 to == ex->e_blk + ext4_ext_get_actual_len(ex) - 1) {
1322 num = ex->e_blk + ext4_ext_get_actual_len(ex) - from;
1323 start = ext4_ext_extent_pblock(ex) +
1324 ext4_ext_get_actual_len(ex) - num;
1325 ext4_ext_blkfree(ip, start, num, 0);
1332 ext4_ext_rm_index(struct inode *ip, struct ext4_extent_path *path)
1336 /* Free index block. */
1338 leaf = ext4_ext_index_pblock(path->ep_index);
1339 KASSERT(path->ep_header->eh_ecount != 0,
1340 ("ext4_ext_rm_index: bad ecount"));
1341 path->ep_header->eh_ecount--;
1342 ext4_ext_dirty(ip, path);
1343 ext4_ext_blkfree(ip, leaf, 1, 0);
1348 ext4_ext_rm_leaf(struct inode *ip, struct ext4_extent_path *path,
1351 struct m_ext2fs *fs;
1353 struct ext4_extent_header *eh;
1354 unsigned int a, b, block, num;
1355 unsigned long ex_blk;
1356 unsigned short ex_len;
1357 struct ext4_extent *ex;
1358 int error, correct_index;
1361 depth = ext4_ext_inode_depth(ip);
1364 if (!path[depth].ep_header) {
1365 if (path[depth].ep_data == NULL)
1367 path[depth].ep_header =
1368 (struct ext4_extent_header* )path[depth].ep_data;
1371 eh = path[depth].ep_header;
1373 ext2_fserr(fs, ip->i_uid, "bad header => extent corrupted");
1377 ex = EXT_LAST_EXTENT(eh);
1379 ex_len = ext4_ext_get_actual_len(ex);
1381 while (ex >= EXT_FIRST_EXTENT(eh) && ex_blk + ex_len > start) {
1382 path[depth].ep_ext = ex;
1383 a = ex_blk > start ? ex_blk : start;
1384 b = (uint64_t)ex_blk + ex_len - 1 <
1385 EXT4_MAX_BLOCKS ? ex_blk + ex_len - 1 : EXT4_MAX_BLOCKS;
1387 if (a != ex_blk && b != ex_blk + ex_len - 1)
1389 else if (a != ex_blk) {
1390 /* Remove tail of the extent. */
1393 } else if (b != ex_blk + ex_len - 1) {
1394 /* Remove head of the extent, not implemented. */
1397 /* Remove whole extent. */
1402 if (ex == EXT_FIRST_EXTENT(eh))
1405 error = ext4_remove_blocks(ip, ex, a, b);
1410 ext4_ext_store_pblock(ex, 0);
1417 ext4_ext_dirty(ip, path + depth);
1421 ex_len = ext4_ext_get_actual_len(ex);
1424 if (correct_index && eh->eh_ecount)
1425 error = ext4_ext_correct_indexes(ip, path);
1428 * If this leaf is free, we should
1429 * remove it from index block above.
1431 if (error == 0 && eh->eh_ecount == 0 && path[depth].ep_data != NULL)
1432 error = ext4_ext_rm_index(ip, path + depth);
1439 ext4_read_extent_tree_block(struct inode *ip, e4fs_daddr_t pblk,
1440 int depth, int flags)
1442 struct m_ext2fs *fs;
1443 struct ext4_extent_header *eh;
1449 error = bread(ip->i_devvp, fsbtodb(fs, pblk),
1450 fs->e2fs_bsize, NOCRED, &bp);
1456 eh = ext4_ext_block_header(bp->b_data);
1457 if (eh->eh_depth != depth) {
1458 ext2_fserr(fs, ip->i_uid, "unexpected eh_depth");
1462 error = ext4_ext_check_header(ip, eh);
1475 ext4_ext_more_to_rm(struct ext4_extent_path *path)
1478 KASSERT(path->ep_index != NULL,
1479 ("ext4_ext_more_to_rm: bad index from path"));
1481 if (path->ep_index < EXT_FIRST_INDEX(path->ep_header))
1484 if (path->ep_header->eh_ecount == path->index_count)
1491 ext4_ext_remove_space(struct inode *ip, off_t length, int flags,
1492 struct ucred *cred, struct thread *td)
1495 struct ext4_extent_header *ehp;
1496 struct ext4_extent_path *path;
1500 ehp = (struct ext4_extent_header *)ip->i_db;
1501 depth = ext4_ext_inode_depth(ip);
1503 error = ext4_ext_check_header(ip, ehp);
1507 path = malloc(sizeof(struct ext4_extent_path) * (depth + 1),
1508 M_EXT2EXTENTS, M_WAITOK | M_ZERO);
1513 path[0].ep_header = ehp;
1514 path[0].ep_depth = depth;
1515 while (i >= 0 && error == 0) {
1518 error = ext4_ext_rm_leaf(ip, path, length);
1521 free(path[i].ep_data, M_EXT2EXTENTS);
1522 path[i].ep_data = NULL;
1527 /* This is index. */
1528 if (!path[i].ep_header)
1530 (struct ext4_extent_header *)path[i].ep_data;
1532 if (!path[i].ep_index) {
1533 /* This level hasn't touched yet. */
1534 path[i].ep_index = EXT_LAST_INDEX(path[i].ep_header);
1535 path[i].index_count = path[i].ep_header->eh_ecount + 1;
1537 /* We've already was here, see at next index. */
1541 if (ext4_ext_more_to_rm(path + i)) {
1542 memset(path + i + 1, 0, sizeof(*path));
1543 bp = ext4_read_extent_tree_block(ip,
1544 ext4_ext_index_pblock(path[i].ep_index),
1545 path[0].ep_depth - (i + 1), 0);
1551 ext4_ext_fill_path_bdata(&path[i+1], bp,
1552 ext4_ext_index_pblock(path[i].ep_index));
1554 path[i].index_count = path[i].ep_header->eh_ecount;
1557 if (path[i].ep_header->eh_ecount == 0 && i > 0) {
1558 /* Index is empty, remove it. */
1559 error = ext4_ext_rm_index(ip, path + i);
1561 free(path[i].ep_data, M_EXT2EXTENTS);
1562 path[i].ep_data = NULL;
1567 if (path->ep_header->eh_ecount == 0) {
1569 * Truncate the tree to zero.
1571 ext4_ext_header(ip)->eh_depth = 0;
1572 ext4_ext_header(ip)->eh_max = ext4_ext_space_root(ip);
1573 ext4_ext_dirty(ip, path);
1577 ext4_ext_drop_refs(path);
1578 free(path, M_EXT2EXTENTS);