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>
39 #include <sys/endian.h>
44 #include <fs/ext2fs/ext2_mount.h>
45 #include <fs/ext2fs/fs.h>
46 #include <fs/ext2fs/inode.h>
47 #include <fs/ext2fs/ext2fs.h>
48 #include <fs/ext2fs/ext2_extents.h>
49 #include <fs/ext2fs/ext2_extern.h>
51 SDT_PROVIDER_DECLARE(ext2fs);
54 * arg0: verbosity. Higher numbers give more verbose messages
55 * arg1: Textual message
57 SDT_PROBE_DEFINE2(ext2fs, , trace, extents, "int", "char*");
59 static MALLOC_DEFINE(M_EXT2EXTENTS, "ext2_extents", "EXT2 extents");
61 #ifdef EXT2FS_PRINT_EXTENTS
63 ext4_ext_print_extent(struct ext4_extent *ep)
66 printf(" ext %p => (blk %u len %u start %ju)\n",
67 ep, le32toh(ep->e_blk), le16toh(ep->e_len),
68 (uint64_t)le16toh(ep->e_start_hi) << 32 | le32toh(ep->e_start_lo));
71 static void ext4_ext_print_header(struct inode *ip, struct ext4_extent_header *ehp);
74 ext4_ext_print_index(struct inode *ip, struct ext4_extent_index *ex, int do_walk)
82 printf(" index %p => (blk %u pblk %ju)\n",
83 ex, le32toh(ex->ei_blk), (uint64_t)le16toh(ex->ei_leaf_hi) << 32 |
84 le32toh(ex->ei_leaf_lo));
89 if ((error = bread(ip->i_devvp,
90 fsbtodb(fs, ((uint64_t)le16toh(ex->ei_leaf_hi) << 32 |
91 le32toh(ex->ei_leaf_lo))), (int)fs->e2fs_bsize, NOCRED, &bp)) != 0) {
96 ext4_ext_print_header(ip, (struct ext4_extent_header *)bp->b_data);
103 ext4_ext_print_header(struct inode *ip, struct ext4_extent_header *ehp)
107 printf("header %p => (magic 0x%x entries %d max %d depth %d gen %d)\n",
108 ehp, le16toh(ehp->eh_magic), le16toh(ehp->eh_ecount),
109 le16toh(ehp->eh_max), le16toh(ehp->eh_depth), le32toh(ehp->eh_gen));
111 for (i = 0; i < le16toh(ehp->eh_ecount); i++)
112 if (ehp->eh_depth != 0)
113 ext4_ext_print_index(ip,
114 (struct ext4_extent_index *)(ehp + 1 + i), 1);
116 ext4_ext_print_extent((struct ext4_extent *)(ehp + 1 + i));
120 ext4_ext_print_path(struct inode *ip, struct ext4_extent_path *path)
126 printf("ip=%ju, Path:\n", ip->i_number);
127 for (k = 0; k <= l; k++, path++) {
128 if (path->ep_index) {
129 ext4_ext_print_index(ip, path->ep_index, 0);
130 } else if (path->ep_ext) {
131 ext4_ext_print_extent(path->ep_ext);
137 ext4_ext_print_extent_tree_status(struct inode *ip)
139 struct ext4_extent_header *ehp;
141 ehp = (struct ext4_extent_header *)(char *)ip->i_db;
143 printf("Extent status:ip=%ju\n", ip->i_number);
144 if (!(ip->i_flag & IN_E4EXTENTS))
147 ext4_ext_print_header(ip, ehp);
153 static inline struct ext4_extent_header *
154 ext4_ext_inode_header(struct inode *ip)
157 return ((struct ext4_extent_header *)ip->i_db);
160 static inline struct ext4_extent_header *
161 ext4_ext_block_header(char *bdata)
164 return ((struct ext4_extent_header *)bdata);
167 static inline unsigned short
168 ext4_ext_inode_depth(struct inode *ip)
170 struct ext4_extent_header *ehp;
172 ehp = (struct ext4_extent_header *)ip->i_data;
173 return (le16toh(ehp->eh_depth));
176 static inline e4fs_daddr_t
177 ext4_ext_index_pblock(struct ext4_extent_index *index)
181 blk = le32toh(index->ei_leaf_lo);
182 blk |= (e4fs_daddr_t)le16toh(index->ei_leaf_hi) << 32;
188 ext4_index_store_pblock(struct ext4_extent_index *index, e4fs_daddr_t pb)
191 index->ei_leaf_lo = htole32(pb & 0xffffffff);
192 index->ei_leaf_hi = htole16((pb >> 32) & 0xffff);
196 static inline e4fs_daddr_t
197 ext4_ext_extent_pblock(struct ext4_extent *extent)
201 blk = le32toh(extent->e_start_lo);
202 blk |= (e4fs_daddr_t)le16toh(extent->e_start_hi) << 32;
208 ext4_ext_store_pblock(struct ext4_extent *ex, e4fs_daddr_t pb)
211 ex->e_start_lo = htole32(pb & 0xffffffff);
212 ex->e_start_hi = htole16((pb >> 32) & 0xffff);
216 ext4_ext_in_cache(struct inode *ip, daddr_t lbn, struct ext4_extent *ep)
218 struct ext4_extent_cache *ecp;
219 int ret = EXT4_EXT_CACHE_NO;
221 ecp = &ip->i_ext_cache;
222 if (ecp->ec_type == EXT4_EXT_CACHE_NO)
225 if (lbn >= ecp->ec_blk && lbn < ecp->ec_blk + ecp->ec_len) {
226 ep->e_blk = htole32(ecp->ec_blk);
227 ep->e_start_lo = htole32(ecp->ec_start & 0xffffffff);
228 ep->e_start_hi = htole16(ecp->ec_start >> 32 & 0xffff);
229 ep->e_len = htole16(ecp->ec_len);
236 ext4_ext_check_header(struct inode *ip, struct ext4_extent_header *eh)
243 if (le16toh(eh->eh_magic) != EXT4_EXT_MAGIC) {
244 error_msg = "header: invalid magic";
247 if (eh->eh_max == 0) {
248 error_msg = "header: invalid eh_max";
251 if (le16toh(eh->eh_ecount) > le16toh(eh->eh_max)) {
252 error_msg = "header: invalid eh_entries";
259 SDT_PROBE2(ext2fs, , trace, extents, 1, error_msg);
264 ext4_ext_binsearch_index(struct ext4_extent_path *path, int blk)
266 struct ext4_extent_header *eh;
267 struct ext4_extent_index *r, *l, *m;
269 eh = path->ep_header;
271 KASSERT(le16toh(eh->eh_ecount) <= le16toh(eh->eh_max) &&
272 le16toh(eh->eh_ecount) > 0,
273 ("ext4_ext_binsearch_index: bad args"));
275 l = EXT_FIRST_INDEX(eh) + 1;
276 r = EXT_FIRST_INDEX(eh) + le16toh(eh->eh_ecount) - 1;
279 if (blk < le32toh(m->ei_blk))
285 path->ep_index = l - 1;
289 ext4_ext_binsearch_ext(struct ext4_extent_path *path, int blk)
291 struct ext4_extent_header *eh;
292 struct ext4_extent *r, *l, *m;
294 eh = path->ep_header;
296 KASSERT(le16toh(eh->eh_ecount) <= le16toh(eh->eh_max),
297 ("ext4_ext_binsearch_ext: bad args"));
299 if (eh->eh_ecount == 0)
302 l = EXT_FIRST_EXTENT(eh) + 1;
303 r = EXT_FIRST_EXTENT(eh) + le16toh(eh->eh_ecount) - 1;
307 if (blk < le32toh(m->e_blk))
313 path->ep_ext = l - 1;
317 ext4_ext_fill_path_bdata(struct ext4_extent_path *path,
318 struct buf *bp, uint64_t blk)
321 KASSERT(path->ep_data == NULL,
322 ("ext4_ext_fill_path_bdata: bad ep_data"));
324 path->ep_data = malloc(bp->b_bufsize, M_EXT2EXTENTS, M_WAITOK);
328 memcpy(path->ep_data, bp->b_data, bp->b_bufsize);
335 ext4_ext_fill_path_buf(struct ext4_extent_path *path, struct buf *bp)
338 KASSERT(path->ep_data != NULL,
339 ("ext4_ext_fill_path_buf: bad ep_data"));
341 memcpy(bp->b_data, path->ep_data, bp->b_bufsize);
345 ext4_ext_drop_refs(struct ext4_extent_path *path)
352 depth = path->ep_depth;
353 for (i = 0; i <= depth; i++, path++)
355 free(path->ep_data, M_EXT2EXTENTS);
356 path->ep_data = NULL;
361 ext4_ext_path_free(struct ext4_extent_path *path)
367 ext4_ext_drop_refs(path);
368 free(path, M_EXT2EXTENTS);
372 ext4_ext_find_extent(struct inode *ip, daddr_t block,
373 struct ext4_extent_path **ppath)
376 struct ext4_extent_header *eh;
377 struct ext4_extent_path *path;
380 int error, depth, i, ppos, alloc;
383 eh = ext4_ext_inode_header(ip);
384 depth = ext4_ext_inode_depth(ip);
388 error = ext4_ext_check_header(ip, eh);
397 path = malloc(EXT4_EXT_DEPTH_MAX *
398 sizeof(struct ext4_extent_path),
399 M_EXT2EXTENTS, M_WAITOK | M_ZERO);
407 path[0].ep_header = eh;
408 path[0].ep_data = NULL;
410 /* Walk through the tree. */
413 ext4_ext_binsearch_index(&path[ppos], block);
414 blk = ext4_ext_index_pblock(path[ppos].ep_index);
415 path[ppos].ep_depth = i;
416 path[ppos].ep_ext = NULL;
418 error = bread(ip->i_devvp, fsbtodb(ip->i_e2fs, blk),
419 ip->i_e2fs->e2fs_bsize, NOCRED, &bp);
426 SDT_PROBE2(ext2fs, , trace, extents, 1,
427 "ppos > depth => extent corrupted");
433 ext4_ext_fill_path_bdata(&path[ppos], bp, blk);
436 eh = ext4_ext_block_header(path[ppos].ep_data);
437 if (ext4_ext_check_header(ip, eh) ||
438 ext2_extent_blk_csum_verify(ip, path[ppos].ep_data)) {
443 path[ppos].ep_header = eh;
448 error = ext4_ext_check_header(ip, eh);
453 path[ppos].ep_depth = i;
454 path[ppos].ep_header = eh;
455 path[ppos].ep_ext = NULL;
456 path[ppos].ep_index = NULL;
457 ext4_ext_binsearch_ext(&path[ppos], block);
461 ext4_ext_drop_refs(path);
463 free(path, M_EXT2EXTENTS);
471 ext4_ext_space_root(struct inode *ip)
475 size = sizeof(ip->i_data);
476 size -= sizeof(struct ext4_extent_header);
477 size /= sizeof(struct ext4_extent);
483 ext4_ext_space_block(struct inode *ip)
490 size = (fs->e2fs_bsize - sizeof(struct ext4_extent_header)) /
491 sizeof(struct ext4_extent);
497 ext4_ext_space_block_index(struct inode *ip)
504 size = (fs->e2fs_bsize - sizeof(struct ext4_extent_header)) /
505 sizeof(struct ext4_extent_index);
511 ext4_ext_tree_init(struct inode *ip)
513 struct ext4_extent_header *ehp;
515 ip->i_flag |= IN_E4EXTENTS;
517 memset(ip->i_data, 0, EXT2_NDADDR + EXT2_NIADDR);
518 ehp = (struct ext4_extent_header *)ip->i_data;
519 ehp->eh_magic = htole16(EXT4_EXT_MAGIC);
520 ehp->eh_max = htole16(ext4_ext_space_root(ip));
521 ip->i_ext_cache.ec_type = EXT4_EXT_CACHE_NO;
522 ip->i_flag |= IN_CHANGE | IN_UPDATE;
523 ext2_update(ip->i_vnode, 1);
527 ext4_ext_put_in_cache(struct inode *ip, uint32_t blk,
528 uint32_t len, uint32_t start, int type)
531 KASSERT(len != 0, ("ext4_ext_put_in_cache: bad input"));
533 ip->i_ext_cache.ec_type = type;
534 ip->i_ext_cache.ec_blk = blk;
535 ip->i_ext_cache.ec_len = len;
536 ip->i_ext_cache.ec_start = start;
540 ext4_ext_blkpref(struct inode *ip, struct ext4_extent_path *path,
544 struct ext4_extent *ex;
545 e4fs_daddr_t bg_start;
551 depth = path->ep_depth;
552 ex = path[depth].ep_ext;
554 e4fs_daddr_t pblk = ext4_ext_extent_pblock(ex);
555 e2fs_daddr_t blk = le32toh(ex->e_blk);
558 return (pblk + (block - blk));
560 return (pblk - (blk - block));
563 /* Try to get block from index itself. */
564 if (path[depth].ep_data)
565 return (path[depth].ep_blk);
568 /* Use inode's group. */
569 bg_start = (ip->i_block_group * EXT2_BLOCKS_PER_GROUP(ip->i_e2fs)) +
570 le32toh(fs->e2fs->e2fs_first_dblock);
572 return (bg_start + block);
576 ext4_can_extents_be_merged(struct ext4_extent *ex1,
577 struct ext4_extent *ex2)
580 if (le32toh(ex1->e_blk) + le16toh(ex1->e_len) != le32toh(ex2->e_blk))
583 if (le16toh(ex1->e_len) + le16toh(ex2->e_len) > EXT4_MAX_LEN)
586 if (ext4_ext_extent_pblock(ex1) + le16toh(ex1->e_len) ==
587 ext4_ext_extent_pblock(ex2))
594 ext4_ext_next_leaf_block(struct inode *ip, struct ext4_extent_path *path)
596 int depth = path->ep_depth;
600 return (EXT4_MAX_BLOCKS);
606 if (path[depth].ep_index !=
607 EXT_LAST_INDEX(path[depth].ep_header))
608 return (le32toh(path[depth].ep_index[1].ei_blk));
613 return (EXT4_MAX_BLOCKS);
617 ext4_ext_dirty(struct inode *ip, struct ext4_extent_path *path)
631 bp = getblk(ip->i_devvp, fsbtodb(fs, blk),
632 fs->e2fs_bsize, 0, 0, 0);
635 ext4_ext_fill_path_buf(path, bp);
636 ext2_extent_blk_csum_set(ip, bp->b_data);
639 ip->i_flag |= IN_CHANGE | IN_UPDATE;
640 error = ext2_update(ip->i_vnode, 1);
647 ext4_ext_insert_index(struct inode *ip, struct ext4_extent_path *path,
648 uint32_t lblk, e4fs_daddr_t blk)
651 struct ext4_extent_index *idx;
656 if (lblk == le32toh(path->ep_index->ei_blk)) {
657 SDT_PROBE2(ext2fs, , trace, extents, 1,
658 "lblk == index blk => extent corrupted");
662 if (le16toh(path->ep_header->eh_ecount) >=
663 le16toh(path->ep_header->eh_max)) {
664 SDT_PROBE2(ext2fs, , trace, extents, 1,
665 "ecout > maxcount => extent corrupted");
669 if (lblk > le32toh(path->ep_index->ei_blk)) {
671 idx = path->ep_index + 1;
674 idx = path->ep_index;
677 len = EXT_LAST_INDEX(path->ep_header) - idx + 1;
679 memmove(idx + 1, idx, len * sizeof(struct ext4_extent_index));
681 if (idx > EXT_MAX_INDEX(path->ep_header)) {
682 SDT_PROBE2(ext2fs, , trace, extents, 1,
683 "index is out of range => extent corrupted");
687 idx->ei_blk = htole32(lblk);
688 ext4_index_store_pblock(idx, blk);
689 path->ep_header->eh_ecount =
690 htole16(le16toh(path->ep_header->eh_ecount) + 1);
692 return (ext4_ext_dirty(ip, path));
696 ext4_ext_alloc_meta(struct inode *ip)
698 e4fs_daddr_t blk = ext2_alloc_meta(ip);
700 ip->i_blocks += btodb(ip->i_e2fs->e2fs_bsize);
701 ip->i_flag |= IN_CHANGE | IN_UPDATE;
702 ext2_update(ip->i_vnode, 1);
709 ext4_ext_blkfree(struct inode *ip, uint64_t blk, int count, int flags)
712 int i, blocksreleased;
715 blocksreleased = count;
717 for(i = 0; i < count; i++)
718 ext2_blkfree(ip, blk + i, fs->e2fs_bsize);
720 if (ip->i_blocks >= blocksreleased)
721 ip->i_blocks -= (btodb(fs->e2fs_bsize)*blocksreleased);
725 ip->i_flag |= IN_CHANGE | IN_UPDATE;
726 ext2_update(ip->i_vnode, 1);
730 ext4_ext_split(struct inode *ip, struct ext4_extent_path *path,
731 struct ext4_extent *newext, int at)
735 int depth = ext4_ext_inode_depth(ip);
736 struct ext4_extent_header *neh;
737 struct ext4_extent_index *fidx;
738 struct ext4_extent *ex;
740 e4fs_daddr_t newblk, oldblk;
742 e4fs_daddr_t *ablks = NULL;
749 * We will split at current extent for now.
751 if (path[depth].ep_ext > EXT_MAX_EXTENT(path[depth].ep_header)) {
752 SDT_PROBE2(ext2fs, , trace, extents, 1,
753 "extent is out of range => extent corrupted");
757 if (path[depth].ep_ext != EXT_MAX_EXTENT(path[depth].ep_header))
758 border = le32toh(path[depth].ep_ext[1].e_blk);
760 border = le32toh(newext->e_blk);
762 /* Allocate new blocks. */
763 ablks = malloc(sizeof(e4fs_daddr_t) * depth,
764 M_EXT2EXTENTS, M_WAITOK | M_ZERO);
767 for (a = 0; a < depth - at; a++) {
768 newblk = ext4_ext_alloc_meta(ip);
775 bp = getblk(ip->i_devvp, fsbtodb(fs, newblk), fs->e2fs_bsize, 0, 0, 0);
781 neh = ext4_ext_block_header(bp->b_data);
783 neh->eh_max = le16toh(ext4_ext_space_block(ip));
784 neh->eh_magic = le16toh(EXT4_EXT_MAGIC);
786 ex = EXT_FIRST_EXTENT(neh);
788 if (le16toh(path[depth].ep_header->eh_ecount) !=
789 le16toh(path[depth].ep_header->eh_max)) {
790 SDT_PROBE2(ext2fs, , trace, extents, 1,
791 "extents count out of range => extent corrupted");
796 /* Start copy from next extent. */
798 path[depth].ep_ext++;
799 while (path[depth].ep_ext <= EXT_MAX_EXTENT(path[depth].ep_header)) {
800 path[depth].ep_ext++;
804 memmove(ex, path[depth].ep_ext - m,
805 sizeof(struct ext4_extent) * m);
806 neh->eh_ecount = htole16(le16toh(neh->eh_ecount) + m);
809 ext2_extent_blk_csum_set(ip, bp->b_data);
815 path[depth].ep_header->eh_ecount =
816 htole16(le16toh(path[depth].ep_header->eh_ecount) - m);
817 ext4_ext_dirty(ip, path + depth);
820 /* Create intermediate indexes. */
822 KASSERT(k >= 0, ("ext4_ext_split: negative k"));
824 /* Insert new index into current index block. */
829 error = bread(ip->i_devvp, fsbtodb(fs, newblk),
830 (int)fs->e2fs_bsize, NOCRED, &bp);
835 neh = (struct ext4_extent_header *)bp->b_data;
836 neh->eh_ecount = htole16(1);
837 neh->eh_magic = htole16(EXT4_EXT_MAGIC);
838 neh->eh_max = htole16(ext4_ext_space_block_index(ip));
839 neh->eh_depth = htole16(depth - i);
840 fidx = EXT_FIRST_INDEX(neh);
841 fidx->ei_blk = htole32(border);
842 ext4_index_store_pblock(fidx, oldblk);
846 while (path[i].ep_index <= EXT_MAX_INDEX(path[i].ep_header)) {
851 memmove(++fidx, path[i].ep_index - m,
852 sizeof(struct ext4_extent_index) * m);
853 neh->eh_ecount = htole16(le16toh(neh->eh_ecount) + m);
856 ext2_extent_blk_csum_set(ip, bp->b_data);
862 path[i].ep_header->eh_ecount =
863 htole16(le16toh(path[i].ep_header->eh_ecount) - m);
864 ext4_ext_dirty(ip, path + i);
870 error = ext4_ext_insert_index(ip, path + at, border, newblk);
877 for (i = 0; i < depth; i++) {
880 ext4_ext_blkfree(ip, ablks[i], 1, 0);
884 free(ablks, M_EXT2EXTENTS);
890 ext4_ext_grow_indepth(struct inode *ip, struct ext4_extent_path *path,
891 struct ext4_extent *newext)
894 struct ext4_extent_path *curpath;
895 struct ext4_extent_header *neh;
903 newblk = ext4_ext_alloc_meta(ip);
907 bp = getblk(ip->i_devvp, fsbtodb(fs, newblk), fs->e2fs_bsize, 0, 0, 0);
911 /* Move top-level index/leaf into new block. */
912 memmove(bp->b_data, curpath->ep_header, sizeof(ip->i_data));
914 /* Set size of new block */
915 neh = ext4_ext_block_header(bp->b_data);
916 neh->eh_magic = htole16(EXT4_EXT_MAGIC);
918 if (ext4_ext_inode_depth(ip))
919 neh->eh_max = htole16(ext4_ext_space_block_index(ip));
921 neh->eh_max = htole16(ext4_ext_space_block(ip));
923 ext2_extent_blk_csum_set(ip, bp->b_data);
930 curpath->ep_header->eh_magic = htole16(EXT4_EXT_MAGIC);
931 curpath->ep_header->eh_max = htole16(ext4_ext_space_root(ip));
932 curpath->ep_header->eh_ecount = htole16(1);
933 curpath->ep_index = EXT_FIRST_INDEX(curpath->ep_header);
934 curpath->ep_index->ei_blk = EXT_FIRST_EXTENT(path[0].ep_header)->e_blk;
935 ext4_index_store_pblock(curpath->ep_index, newblk);
937 neh = ext4_ext_inode_header(ip);
938 neh->eh_depth = htole16(path->ep_depth + 1);
939 ext4_ext_dirty(ip, curpath);
947 ext4_ext_create_new_leaf(struct inode *ip, struct ext4_extent_path *path,
948 struct ext4_extent *newext)
950 struct ext4_extent_path *curpath;
954 i = depth = ext4_ext_inode_depth(ip);
956 /* Look for free index entry int the tree */
957 curpath = path + depth;
958 while (i > 0 && !EXT_HAS_FREE_INDEX(curpath)) {
964 * We use already allocated block for index block,
965 * so subsequent data blocks should be contiguous.
967 if (EXT_HAS_FREE_INDEX(curpath)) {
968 error = ext4_ext_split(ip, path, newext, i);
973 ext4_ext_drop_refs(path);
974 error = ext4_ext_find_extent(ip, le32toh(newext->e_blk), &path);
978 /* Tree is full, do grow in depth. */
979 error = ext4_ext_grow_indepth(ip, path, newext);
984 ext4_ext_drop_refs(path);
985 error = ext4_ext_find_extent(ip, le32toh(newext->e_blk), &path);
989 /* Check and split tree if required. */
990 depth = ext4_ext_inode_depth(ip);
991 if (le16toh(path[depth].ep_header->eh_ecount) ==
992 le16toh(path[depth].ep_header->eh_max))
1001 ext4_ext_correct_indexes(struct inode *ip, struct ext4_extent_path *path)
1003 struct ext4_extent_header *eh;
1004 struct ext4_extent *ex;
1008 depth = ext4_ext_inode_depth(ip);
1009 eh = path[depth].ep_header;
1010 ex = path[depth].ep_ext;
1012 if (ex == NULL || eh == NULL)
1018 /* We will correct tree if first leaf got modified only. */
1019 if (ex != EXT_FIRST_EXTENT(eh))
1023 border = le32toh(path[depth].ep_ext->e_blk);
1024 path[k].ep_index->ei_blk = htole32(border);
1025 ext4_ext_dirty(ip, path + k);
1027 /* Change all left-side indexes. */
1028 if (path[k+1].ep_index != EXT_FIRST_INDEX(path[k+1].ep_header))
1031 path[k].ep_index->ei_blk = htole32(border);
1032 ext4_ext_dirty(ip, path + k);
1039 ext4_ext_insert_extent(struct inode *ip, struct ext4_extent_path *path,
1040 struct ext4_extent *newext)
1042 struct ext4_extent_header * eh;
1043 struct ext4_extent *ex, *nex, *nearex;
1044 struct ext4_extent_path *npath;
1045 int depth, len, error, next;
1047 depth = ext4_ext_inode_depth(ip);
1048 ex = path[depth].ep_ext;
1051 if (htole16(newext->e_len) == 0 || path[depth].ep_header == NULL)
1054 /* Insert block into found extent. */
1055 if (ex && ext4_can_extents_be_merged(ex, newext)) {
1056 ex->e_len = htole16(le16toh(ex->e_len) + le16toh(newext->e_len));
1057 eh = path[depth].ep_header;
1063 depth = ext4_ext_inode_depth(ip);
1064 eh = path[depth].ep_header;
1065 if (le16toh(eh->eh_ecount) < le16toh(eh->eh_max))
1069 nex = EXT_LAST_EXTENT(eh);
1070 next = ext4_ext_next_leaf_block(ip, path);
1071 if (le32toh(newext->e_blk) > le32toh(nex->e_blk) && next !=
1073 KASSERT(npath == NULL,
1074 ("ext4_ext_insert_extent: bad path"));
1076 error = ext4_ext_find_extent(ip, next, &npath);
1080 if (npath->ep_depth != path->ep_depth) {
1085 eh = npath[depth].ep_header;
1086 if (le16toh(eh->eh_ecount) < le16toh(eh->eh_max)) {
1093 * There is no free space in the found leaf,
1094 * try to add a new leaf to the tree.
1096 error = ext4_ext_create_new_leaf(ip, path, newext);
1100 depth = ext4_ext_inode_depth(ip);
1101 eh = path[depth].ep_header;
1104 nearex = path[depth].ep_ext;
1106 /* Create new extent in the leaf. */
1107 path[depth].ep_ext = EXT_FIRST_EXTENT(eh);
1108 } else if (le32toh(newext->e_blk) > le32toh(nearex->e_blk)) {
1109 if (nearex != EXT_LAST_EXTENT(eh)) {
1110 len = EXT_MAX_EXTENT(eh) - nearex;
1111 len = (len - 1) * sizeof(struct ext4_extent);
1112 len = len < 0 ? 0 : len;
1113 memmove(nearex + 2, nearex + 1, len);
1115 path[depth].ep_ext = nearex + 1;
1117 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1118 len = len < 0 ? 0 : len;
1119 memmove(nearex + 1, nearex, len);
1120 path[depth].ep_ext = nearex;
1123 eh->eh_ecount = htole16(le16toh(eh->eh_ecount) + 1);
1124 nearex = path[depth].ep_ext;
1125 nearex->e_blk = newext->e_blk;
1126 nearex->e_start_lo = newext->e_start_lo;
1127 nearex->e_start_hi = newext->e_start_hi;
1128 nearex->e_len = newext->e_len;
1131 /* Try to merge extents to the right. */
1132 while (nearex < EXT_LAST_EXTENT(eh)) {
1133 if (!ext4_can_extents_be_merged(nearex, nearex + 1))
1136 /* Merge with next extent. */
1137 nearex->e_len = htole16(le16toh(nearex->e_len) +
1138 le16toh(nearex[1].e_len));
1139 if (nearex + 1 < EXT_LAST_EXTENT(eh)) {
1140 len = (EXT_LAST_EXTENT(eh) - nearex - 1) *
1141 sizeof(struct ext4_extent);
1142 memmove(nearex + 1, nearex + 2, len);
1145 eh->eh_ecount = htole16(le16toh(eh->eh_ecount) - 1);
1146 KASSERT(le16toh(eh->eh_ecount) != 0,
1147 ("ext4_ext_insert_extent: bad ecount"));
1151 * Try to merge extents to the left,
1152 * start from inexes correction.
1154 error = ext4_ext_correct_indexes(ip, path);
1158 ext4_ext_dirty(ip, path + depth);
1162 ext4_ext_drop_refs(npath);
1163 free(npath, M_EXT2EXTENTS);
1166 ip->i_ext_cache.ec_type = EXT4_EXT_CACHE_NO;
1171 ext4_new_blocks(struct inode *ip, daddr_t lbn, e4fs_daddr_t pref,
1172 struct ucred *cred, unsigned long *count, int *perror)
1174 struct m_ext2fs *fs;
1175 e4fs_daddr_t newblk;
1178 * We will allocate only single block for now.
1184 EXT2_LOCK(ip->i_ump);
1185 *perror = ext2_alloc(ip, lbn, pref, (int)fs->e2fs_bsize, cred, &newblk);
1190 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1191 ext2_update(ip->i_vnode, 1);
1198 ext4_ext_get_blocks(struct inode *ip, e4fs_daddr_t iblk,
1199 unsigned long max_blocks, struct ucred *cred, struct buf **bpp,
1200 int *pallocated, daddr_t *nb)
1202 struct m_ext2fs *fs;
1203 struct buf *bp = NULL;
1204 struct ext4_extent_path *path;
1205 struct ext4_extent newex, *ex;
1206 e4fs_daddr_t bpref, newblk = 0;
1207 unsigned long allocated = 0;
1208 int error = 0, depth;
1216 if ((bpref = ext4_ext_in_cache(ip, iblk, &newex))) {
1217 if (bpref == EXT4_EXT_CACHE_IN) {
1218 /* Block is already allocated. */
1219 newblk = iblk - le32toh(newex.e_blk) +
1220 ext4_ext_extent_pblock(&newex);
1221 allocated = le16toh(newex.e_len) - (iblk - le32toh(newex.e_blk));
1229 error = ext4_ext_find_extent(ip, iblk, &path);
1234 depth = ext4_ext_inode_depth(ip);
1235 if (path[depth].ep_ext == NULL && depth != 0) {
1240 if ((ex = path[depth].ep_ext)) {
1241 uint64_t lblk = le32toh(ex->e_blk);
1242 uint16_t e_len = le16toh(ex->e_len);
1243 e4fs_daddr_t e_start = ext4_ext_extent_pblock(ex);
1245 if (e_len > EXT4_MAX_LEN)
1248 /* If we found extent covers block, simply return it. */
1249 if (iblk >= lblk && iblk < lblk + e_len) {
1250 newblk = iblk - lblk + e_start;
1251 allocated = e_len - (iblk - lblk);
1252 ext4_ext_put_in_cache(ip, lblk, e_len,
1253 e_start, EXT4_EXT_CACHE_IN);
1258 /* Allocate the new block. */
1259 if (S_ISREG(ip->i_mode) && (!ip->i_next_alloc_block)) {
1260 ip->i_next_alloc_goal = 0;
1263 bpref = ext4_ext_blkpref(ip, path, iblk);
1264 allocated = max_blocks;
1265 newblk = ext4_new_blocks(ip, iblk, bpref, cred, &allocated, &error);
1269 /* Try to insert new extent into found leaf and return. */
1270 newex.e_blk = htole32(iblk);
1271 ext4_ext_store_pblock(&newex, newblk);
1272 newex.e_len = htole16(allocated);
1273 error = ext4_ext_insert_extent(ip, path, &newex);
1277 newblk = ext4_ext_extent_pblock(&newex);
1278 ext4_ext_put_in_cache(ip, iblk, allocated, newblk, EXT4_EXT_CACHE_IN);
1282 if (allocated > max_blocks)
1283 allocated = max_blocks;
1288 error = bread(ip->i_devvp, fsbtodb(fs, newblk),
1289 fs->e2fs_bsize, cred, &bp);
1299 ext4_ext_drop_refs(path);
1300 free(path, M_EXT2EXTENTS);
1309 static inline uint16_t
1310 ext4_ext_get_actual_len(struct ext4_extent *ext)
1313 return (le16toh(ext->e_len) <= EXT_INIT_MAX_LEN ?
1314 le16toh(ext->e_len) : (le16toh(ext->e_len) - EXT_INIT_MAX_LEN));
1317 static inline struct ext4_extent_header *
1318 ext4_ext_header(struct inode *ip)
1321 return ((struct ext4_extent_header *)ip->i_db);
1325 ext4_remove_blocks(struct inode *ip, struct ext4_extent *ex,
1326 unsigned long from, unsigned long to)
1328 unsigned long num, start;
1330 if (from >= le32toh(ex->e_blk) &&
1331 to == le32toh(ex->e_blk) + ext4_ext_get_actual_len(ex) - 1) {
1333 num = le32toh(ex->e_blk) + ext4_ext_get_actual_len(ex) - from;
1334 start = ext4_ext_extent_pblock(ex) +
1335 ext4_ext_get_actual_len(ex) - num;
1336 ext4_ext_blkfree(ip, start, num, 0);
1343 ext4_ext_rm_index(struct inode *ip, struct ext4_extent_path *path)
1347 /* Free index block. */
1349 leaf = ext4_ext_index_pblock(path->ep_index);
1350 KASSERT(path->ep_header->eh_ecount != 0,
1351 ("ext4_ext_rm_index: bad ecount"));
1352 path->ep_header->eh_ecount =
1353 htole16(le16toh(path->ep_header->eh_ecount) - 1);
1354 ext4_ext_dirty(ip, path);
1355 ext4_ext_blkfree(ip, leaf, 1, 0);
1360 ext4_ext_rm_leaf(struct inode *ip, struct ext4_extent_path *path,
1363 struct ext4_extent_header *eh;
1364 struct ext4_extent *ex;
1365 unsigned int a, b, block, num;
1366 unsigned long ex_blk;
1367 unsigned short ex_len;
1369 int error, correct_index;
1371 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 SDT_PROBE2(ext2fs, , trace, extents, 1,
1382 "bad header => extent corrupted");
1386 ex = EXT_LAST_EXTENT(eh);
1387 ex_blk = le32toh(ex->e_blk);
1388 ex_len = ext4_ext_get_actual_len(ex);
1392 while (ex >= EXT_FIRST_EXTENT(eh) && ex_blk + ex_len > start) {
1393 path[depth].ep_ext = ex;
1394 a = ex_blk > start ? ex_blk : start;
1395 b = (uint64_t)ex_blk + ex_len - 1 <
1396 EXT4_MAX_BLOCKS ? ex_blk + ex_len - 1 : EXT4_MAX_BLOCKS;
1398 if (a != ex_blk && b != ex_blk + ex_len - 1)
1400 else if (a != ex_blk) {
1401 /* Remove tail of the extent. */
1404 } else if (b != ex_blk + ex_len - 1) {
1405 /* Remove head of the extent, not implemented. */
1408 /* Remove whole extent. */
1413 if (ex == EXT_FIRST_EXTENT(eh))
1416 error = ext4_remove_blocks(ip, ex, a, b);
1421 ext4_ext_store_pblock(ex, 0);
1422 eh->eh_ecount = htole16(le16toh(eh->eh_ecount) - 1);
1425 ex->e_blk = htole32(block);
1426 ex->e_len = htole16(num);
1428 ext4_ext_dirty(ip, path + depth);
1431 ex_blk = htole32(ex->e_blk);
1432 ex_len = ext4_ext_get_actual_len(ex);
1435 if (correct_index && le16toh(eh->eh_ecount))
1436 error = ext4_ext_correct_indexes(ip, path);
1439 * If this leaf is free, we should
1440 * remove it from index block above.
1442 if (error == 0 && eh->eh_ecount == 0 &&
1443 path[depth].ep_data != NULL)
1444 error = ext4_ext_rm_index(ip, path + depth);
1451 ext4_read_extent_tree_block(struct inode *ip, e4fs_daddr_t pblk,
1452 int depth, int flags)
1454 struct m_ext2fs *fs;
1455 struct ext4_extent_header *eh;
1460 error = bread(ip->i_devvp, fsbtodb(fs, pblk),
1461 fs->e2fs_bsize, NOCRED, &bp);
1466 eh = ext4_ext_block_header(bp->b_data);
1467 if (le16toh(eh->eh_depth) != depth) {
1468 SDT_PROBE2(ext2fs, , trace, extents, 1,
1469 "unexpected eh_depth");
1473 error = ext4_ext_check_header(ip, eh);
1486 ext4_ext_more_to_rm(struct ext4_extent_path *path)
1489 KASSERT(path->ep_index != NULL,
1490 ("ext4_ext_more_to_rm: bad index from path"));
1492 if (path->ep_index < EXT_FIRST_INDEX(path->ep_header))
1495 if (le16toh(path->ep_header->eh_ecount) == path->index_count)
1502 ext4_ext_remove_space(struct inode *ip, off_t length, int flags,
1503 struct ucred *cred, struct thread *td)
1506 struct ext4_extent_header *ehp;
1507 struct ext4_extent_path *path;
1511 ehp = (struct ext4_extent_header *)ip->i_db;
1512 depth = ext4_ext_inode_depth(ip);
1514 error = ext4_ext_check_header(ip, ehp);
1518 path = malloc(sizeof(struct ext4_extent_path) * (depth + 1),
1519 M_EXT2EXTENTS, M_WAITOK | M_ZERO);
1523 path[0].ep_header = ehp;
1524 path[0].ep_depth = depth;
1526 while (error == 0 && i >= 0) {
1529 error = ext4_ext_rm_leaf(ip, path, length);
1532 free(path[i].ep_data, M_EXT2EXTENTS);
1533 path[i].ep_data = NULL;
1538 /* This is index. */
1539 if (!path[i].ep_header)
1541 (struct ext4_extent_header *)path[i].ep_data;
1543 if (!path[i].ep_index) {
1544 /* This level hasn't touched yet. */
1545 path[i].ep_index = EXT_LAST_INDEX(path[i].ep_header);
1546 path[i].index_count =
1547 le16toh(path[i].ep_header->eh_ecount) + 1;
1549 /* We've already was here, see at next index. */
1553 if (ext4_ext_more_to_rm(path + i)) {
1554 memset(path + i + 1, 0, sizeof(*path));
1555 bp = ext4_read_extent_tree_block(ip,
1556 ext4_ext_index_pblock(path[i].ep_index),
1557 path[0].ep_depth - (i + 1), 0);
1563 ext4_ext_fill_path_bdata(&path[i+1], bp,
1564 ext4_ext_index_pblock(path[i].ep_index));
1566 path[i].index_count =
1567 le16toh(path[i].ep_header->eh_ecount);
1570 if (path[i].ep_header->eh_ecount == 0 && i > 0) {
1571 /* Index is empty, remove it. */
1572 error = ext4_ext_rm_index(ip, path + i);
1574 free(path[i].ep_data, M_EXT2EXTENTS);
1575 path[i].ep_data = NULL;
1580 if (path->ep_header->eh_ecount == 0) {
1582 * Truncate the tree to zero.
1584 ext4_ext_header(ip)->eh_depth = 0;
1585 ext4_ext_header(ip)->eh_max = htole16(ext4_ext_space_root(ip));
1586 ext4_ext_dirty(ip, path);
1589 ext4_ext_drop_refs(path);
1590 free(path, M_EXT2EXTENTS);