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);
195 static inline e4fs_daddr_t
196 ext4_ext_extent_pblock(struct ext4_extent *extent)
200 blk = le32toh(extent->e_start_lo);
201 blk |= (e4fs_daddr_t)le16toh(extent->e_start_hi) << 32;
207 ext4_ext_store_pblock(struct ext4_extent *ex, e4fs_daddr_t pb)
210 ex->e_start_lo = htole32(pb & 0xffffffff);
211 ex->e_start_hi = htole16((pb >> 32) & 0xffff);
215 ext4_ext_in_cache(struct inode *ip, daddr_t lbn, struct ext4_extent *ep)
217 struct ext4_extent_cache *ecp;
218 int ret = EXT4_EXT_CACHE_NO;
220 ecp = &ip->i_ext_cache;
221 if (ecp->ec_type == EXT4_EXT_CACHE_NO)
224 if (lbn >= ecp->ec_blk && lbn < ecp->ec_blk + ecp->ec_len) {
225 ep->e_blk = htole32(ecp->ec_blk);
226 ep->e_start_lo = htole32(ecp->ec_start & 0xffffffff);
227 ep->e_start_hi = htole16(ecp->ec_start >> 32 & 0xffff);
228 ep->e_len = htole16(ecp->ec_len);
235 ext4_ext_check_header(struct inode *ip, struct ext4_extent_header *eh)
242 if (le16toh(eh->eh_magic) != EXT4_EXT_MAGIC) {
243 error_msg = "header: invalid magic";
246 if (eh->eh_max == 0) {
247 error_msg = "header: invalid eh_max";
250 if (le16toh(eh->eh_ecount) > le16toh(eh->eh_max)) {
251 error_msg = "header: invalid eh_entries";
258 SDT_PROBE2(ext2fs, , trace, extents, 1, error_msg);
263 ext4_ext_binsearch_index(struct ext4_extent_path *path, int blk)
265 struct ext4_extent_header *eh;
266 struct ext4_extent_index *r, *l, *m;
268 eh = path->ep_header;
270 KASSERT(le16toh(eh->eh_ecount) <= le16toh(eh->eh_max) &&
271 le16toh(eh->eh_ecount) > 0,
272 ("ext4_ext_binsearch_index: bad args"));
274 l = EXT_FIRST_INDEX(eh) + 1;
275 r = EXT_FIRST_INDEX(eh) + le16toh(eh->eh_ecount) - 1;
278 if (blk < le32toh(m->ei_blk))
284 path->ep_index = l - 1;
288 ext4_ext_binsearch_ext(struct ext4_extent_path *path, int blk)
290 struct ext4_extent_header *eh;
291 struct ext4_extent *r, *l, *m;
293 eh = path->ep_header;
295 KASSERT(le16toh(eh->eh_ecount) <= le16toh(eh->eh_max),
296 ("ext4_ext_binsearch_ext: bad args"));
298 if (eh->eh_ecount == 0)
301 l = EXT_FIRST_EXTENT(eh) + 1;
302 r = EXT_FIRST_EXTENT(eh) + le16toh(eh->eh_ecount) - 1;
306 if (blk < le32toh(m->e_blk))
312 path->ep_ext = l - 1;
316 ext4_ext_fill_path_bdata(struct ext4_extent_path *path,
317 struct buf *bp, uint64_t blk)
320 KASSERT(path->ep_data == NULL,
321 ("ext4_ext_fill_path_bdata: bad ep_data"));
323 path->ep_data = malloc(bp->b_bufsize, M_EXT2EXTENTS, M_WAITOK);
324 memcpy(path->ep_data, bp->b_data, bp->b_bufsize);
331 ext4_ext_fill_path_buf(struct ext4_extent_path *path, struct buf *bp)
334 KASSERT(path->ep_data != NULL,
335 ("ext4_ext_fill_path_buf: bad ep_data"));
337 memcpy(bp->b_data, path->ep_data, bp->b_bufsize);
341 ext4_ext_drop_refs(struct ext4_extent_path *path)
348 depth = path->ep_depth;
349 for (i = 0; i <= depth; i++, path++)
351 free(path->ep_data, M_EXT2EXTENTS);
352 path->ep_data = NULL;
357 ext4_ext_path_free(struct ext4_extent_path *path)
363 ext4_ext_drop_refs(path);
364 free(path, M_EXT2EXTENTS);
368 ext4_ext_find_extent(struct inode *ip, daddr_t block,
369 struct ext4_extent_path **ppath)
372 struct ext4_extent_header *eh;
373 struct ext4_extent_path *path;
376 int error, depth, i, ppos, alloc;
379 eh = ext4_ext_inode_header(ip);
380 depth = ext4_ext_inode_depth(ip);
384 error = ext4_ext_check_header(ip, eh);
393 path = malloc(EXT4_EXT_DEPTH_MAX *
394 sizeof(struct ext4_extent_path),
395 M_EXT2EXTENTS, M_WAITOK | M_ZERO);
400 path[0].ep_header = eh;
401 path[0].ep_data = NULL;
403 /* Walk through the tree. */
406 ext4_ext_binsearch_index(&path[ppos], block);
407 blk = ext4_ext_index_pblock(path[ppos].ep_index);
408 path[ppos].ep_depth = i;
409 path[ppos].ep_ext = NULL;
411 error = bread(ip->i_devvp, fsbtodb(ip->i_e2fs, blk),
412 ip->i_e2fs->e2fs_bsize, NOCRED, &bp);
419 SDT_PROBE2(ext2fs, , trace, extents, 1,
420 "ppos > depth => extent corrupted");
426 ext4_ext_fill_path_bdata(&path[ppos], bp, blk);
429 eh = ext4_ext_block_header(path[ppos].ep_data);
430 if (ext4_ext_check_header(ip, eh) ||
431 ext2_extent_blk_csum_verify(ip, path[ppos].ep_data)) {
436 path[ppos].ep_header = eh;
441 error = ext4_ext_check_header(ip, eh);
446 path[ppos].ep_depth = i;
447 path[ppos].ep_header = eh;
448 path[ppos].ep_ext = NULL;
449 path[ppos].ep_index = NULL;
450 ext4_ext_binsearch_ext(&path[ppos], block);
454 ext4_ext_drop_refs(path);
456 free(path, M_EXT2EXTENTS);
464 ext4_ext_space_root(struct inode *ip)
468 size = sizeof(ip->i_data);
469 size -= sizeof(struct ext4_extent_header);
470 size /= sizeof(struct ext4_extent);
476 ext4_ext_space_block(struct inode *ip)
483 size = (fs->e2fs_bsize - sizeof(struct ext4_extent_header)) /
484 sizeof(struct ext4_extent);
490 ext4_ext_space_block_index(struct inode *ip)
497 size = (fs->e2fs_bsize - sizeof(struct ext4_extent_header)) /
498 sizeof(struct ext4_extent_index);
504 ext4_ext_tree_init(struct inode *ip)
506 struct ext4_extent_header *ehp;
508 ip->i_flag |= IN_E4EXTENTS;
510 memset(ip->i_data, 0, EXT2_NDADDR + EXT2_NIADDR);
511 ehp = (struct ext4_extent_header *)ip->i_data;
512 ehp->eh_magic = htole16(EXT4_EXT_MAGIC);
513 ehp->eh_max = htole16(ext4_ext_space_root(ip));
514 ip->i_ext_cache.ec_type = EXT4_EXT_CACHE_NO;
515 ip->i_flag |= IN_CHANGE | IN_UPDATE;
516 ext2_update(ip->i_vnode, 1);
520 ext4_ext_put_in_cache(struct inode *ip, uint32_t blk,
521 uint32_t len, uint32_t start, int type)
524 KASSERT(len != 0, ("ext4_ext_put_in_cache: bad input"));
526 ip->i_ext_cache.ec_type = type;
527 ip->i_ext_cache.ec_blk = blk;
528 ip->i_ext_cache.ec_len = len;
529 ip->i_ext_cache.ec_start = start;
533 ext4_ext_blkpref(struct inode *ip, struct ext4_extent_path *path,
537 struct ext4_extent *ex;
538 e4fs_daddr_t bg_start;
544 depth = path->ep_depth;
545 ex = path[depth].ep_ext;
547 e4fs_daddr_t pblk = ext4_ext_extent_pblock(ex);
548 e2fs_daddr_t blk = le32toh(ex->e_blk);
551 return (pblk + (block - blk));
553 return (pblk - (blk - block));
556 /* Try to get block from index itself. */
557 if (path[depth].ep_data)
558 return (path[depth].ep_blk);
561 /* Use inode's group. */
562 bg_start = (ip->i_block_group * EXT2_BLOCKS_PER_GROUP(ip->i_e2fs)) +
563 le32toh(fs->e2fs->e2fs_first_dblock);
565 return (bg_start + block);
569 ext4_can_extents_be_merged(struct ext4_extent *ex1,
570 struct ext4_extent *ex2)
573 if (le32toh(ex1->e_blk) + le16toh(ex1->e_len) != le32toh(ex2->e_blk))
576 if (le16toh(ex1->e_len) + le16toh(ex2->e_len) > EXT4_MAX_LEN)
579 if (ext4_ext_extent_pblock(ex1) + le16toh(ex1->e_len) ==
580 ext4_ext_extent_pblock(ex2))
587 ext4_ext_next_leaf_block(struct inode *ip, struct ext4_extent_path *path)
589 int depth = path->ep_depth;
593 return (EXT4_MAX_BLOCKS);
599 if (path[depth].ep_index !=
600 EXT_LAST_INDEX(path[depth].ep_header))
601 return (le32toh(path[depth].ep_index[1].ei_blk));
606 return (EXT4_MAX_BLOCKS);
610 ext4_ext_dirty(struct inode *ip, struct ext4_extent_path *path)
624 bp = getblk(ip->i_devvp, fsbtodb(fs, blk),
625 fs->e2fs_bsize, 0, 0, 0);
628 ext4_ext_fill_path_buf(path, bp);
629 ext2_extent_blk_csum_set(ip, bp->b_data);
632 ip->i_flag |= IN_CHANGE | IN_UPDATE;
633 error = ext2_update(ip->i_vnode, 1);
640 ext4_ext_insert_index(struct inode *ip, struct ext4_extent_path *path,
641 uint32_t lblk, e4fs_daddr_t blk)
644 struct ext4_extent_index *idx;
649 if (lblk == le32toh(path->ep_index->ei_blk)) {
650 SDT_PROBE2(ext2fs, , trace, extents, 1,
651 "lblk == index blk => extent corrupted");
655 if (le16toh(path->ep_header->eh_ecount) >=
656 le16toh(path->ep_header->eh_max)) {
657 SDT_PROBE2(ext2fs, , trace, extents, 1,
658 "ecout > maxcount => extent corrupted");
662 if (lblk > le32toh(path->ep_index->ei_blk)) {
664 idx = path->ep_index + 1;
667 idx = path->ep_index;
670 len = EXT_LAST_INDEX(path->ep_header) - idx + 1;
672 memmove(idx + 1, idx, len * sizeof(struct ext4_extent_index));
674 if (idx > EXT_MAX_INDEX(path->ep_header)) {
675 SDT_PROBE2(ext2fs, , trace, extents, 1,
676 "index is out of range => extent corrupted");
680 idx->ei_blk = htole32(lblk);
681 ext4_index_store_pblock(idx, blk);
682 path->ep_header->eh_ecount =
683 htole16(le16toh(path->ep_header->eh_ecount) + 1);
685 return (ext4_ext_dirty(ip, path));
689 ext4_ext_alloc_meta(struct inode *ip)
691 e4fs_daddr_t blk = ext2_alloc_meta(ip);
693 ip->i_blocks += btodb(ip->i_e2fs->e2fs_bsize);
694 ip->i_flag |= IN_CHANGE | IN_UPDATE;
695 ext2_update(ip->i_vnode, 1);
702 ext4_ext_blkfree(struct inode *ip, uint64_t blk, int count, int flags)
705 int i, blocksreleased;
708 blocksreleased = count;
710 for(i = 0; i < count; i++)
711 ext2_blkfree(ip, blk + i, fs->e2fs_bsize);
713 if (ip->i_blocks >= blocksreleased)
714 ip->i_blocks -= (btodb(fs->e2fs_bsize)*blocksreleased);
718 ip->i_flag |= IN_CHANGE | IN_UPDATE;
719 ext2_update(ip->i_vnode, 1);
723 ext4_ext_split(struct inode *ip, struct ext4_extent_path *path,
724 struct ext4_extent *newext, int at)
728 int depth = ext4_ext_inode_depth(ip);
729 struct ext4_extent_header *neh;
730 struct ext4_extent_index *fidx;
731 struct ext4_extent *ex;
733 e4fs_daddr_t newblk, oldblk;
735 e4fs_daddr_t *ablks = NULL;
742 * We will split at current extent for now.
744 if (path[depth].ep_ext > EXT_MAX_EXTENT(path[depth].ep_header)) {
745 SDT_PROBE2(ext2fs, , trace, extents, 1,
746 "extent is out of range => extent corrupted");
750 if (path[depth].ep_ext != EXT_MAX_EXTENT(path[depth].ep_header))
751 border = le32toh(path[depth].ep_ext[1].e_blk);
753 border = le32toh(newext->e_blk);
755 /* Allocate new blocks. */
756 ablks = malloc(sizeof(e4fs_daddr_t) * depth,
757 M_EXT2EXTENTS, M_WAITOK | M_ZERO);
758 for (a = 0; a < depth - at; a++) {
759 newblk = ext4_ext_alloc_meta(ip);
766 bp = getblk(ip->i_devvp, fsbtodb(fs, newblk), fs->e2fs_bsize, 0, 0, 0);
772 neh = ext4_ext_block_header(bp->b_data);
774 neh->eh_max = le16toh(ext4_ext_space_block(ip));
775 neh->eh_magic = le16toh(EXT4_EXT_MAGIC);
777 ex = EXT_FIRST_EXTENT(neh);
779 if (le16toh(path[depth].ep_header->eh_ecount) !=
780 le16toh(path[depth].ep_header->eh_max)) {
781 SDT_PROBE2(ext2fs, , trace, extents, 1,
782 "extents count out of range => extent corrupted");
787 /* Start copy from next extent. */
789 path[depth].ep_ext++;
790 while (path[depth].ep_ext <= EXT_MAX_EXTENT(path[depth].ep_header)) {
791 path[depth].ep_ext++;
795 memmove(ex, path[depth].ep_ext - m,
796 sizeof(struct ext4_extent) * m);
797 neh->eh_ecount = htole16(le16toh(neh->eh_ecount) + m);
800 ext2_extent_blk_csum_set(ip, bp->b_data);
806 path[depth].ep_header->eh_ecount =
807 htole16(le16toh(path[depth].ep_header->eh_ecount) - m);
808 ext4_ext_dirty(ip, path + depth);
811 /* Create intermediate indexes. */
813 KASSERT(k >= 0, ("ext4_ext_split: negative k"));
815 /* Insert new index into current index block. */
820 error = bread(ip->i_devvp, fsbtodb(fs, newblk),
821 (int)fs->e2fs_bsize, NOCRED, &bp);
826 neh = (struct ext4_extent_header *)bp->b_data;
827 neh->eh_ecount = htole16(1);
828 neh->eh_magic = htole16(EXT4_EXT_MAGIC);
829 neh->eh_max = htole16(ext4_ext_space_block_index(ip));
830 neh->eh_depth = htole16(depth - i);
831 fidx = EXT_FIRST_INDEX(neh);
832 fidx->ei_blk = htole32(border);
833 ext4_index_store_pblock(fidx, oldblk);
837 while (path[i].ep_index <= EXT_MAX_INDEX(path[i].ep_header)) {
842 memmove(++fidx, path[i].ep_index - m,
843 sizeof(struct ext4_extent_index) * m);
844 neh->eh_ecount = htole16(le16toh(neh->eh_ecount) + m);
847 ext2_extent_blk_csum_set(ip, bp->b_data);
853 path[i].ep_header->eh_ecount =
854 htole16(le16toh(path[i].ep_header->eh_ecount) - m);
855 ext4_ext_dirty(ip, path + i);
861 error = ext4_ext_insert_index(ip, path + at, border, newblk);
868 for (i = 0; i < depth; i++) {
871 ext4_ext_blkfree(ip, ablks[i], 1, 0);
875 free(ablks, M_EXT2EXTENTS);
881 ext4_ext_grow_indepth(struct inode *ip, struct ext4_extent_path *path,
882 struct ext4_extent *newext)
885 struct ext4_extent_path *curpath;
886 struct ext4_extent_header *neh;
894 newblk = ext4_ext_alloc_meta(ip);
898 bp = getblk(ip->i_devvp, fsbtodb(fs, newblk), fs->e2fs_bsize, 0, 0, 0);
902 /* Move top-level index/leaf into new block. */
903 memmove(bp->b_data, curpath->ep_header, sizeof(ip->i_data));
905 /* Set size of new block */
906 neh = ext4_ext_block_header(bp->b_data);
907 neh->eh_magic = htole16(EXT4_EXT_MAGIC);
909 if (ext4_ext_inode_depth(ip))
910 neh->eh_max = htole16(ext4_ext_space_block_index(ip));
912 neh->eh_max = htole16(ext4_ext_space_block(ip));
914 ext2_extent_blk_csum_set(ip, bp->b_data);
921 curpath->ep_header->eh_magic = htole16(EXT4_EXT_MAGIC);
922 curpath->ep_header->eh_max = htole16(ext4_ext_space_root(ip));
923 curpath->ep_header->eh_ecount = htole16(1);
924 curpath->ep_index = EXT_FIRST_INDEX(curpath->ep_header);
925 curpath->ep_index->ei_blk = EXT_FIRST_EXTENT(path[0].ep_header)->e_blk;
926 ext4_index_store_pblock(curpath->ep_index, newblk);
928 neh = ext4_ext_inode_header(ip);
929 neh->eh_depth = htole16(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)
941 struct ext4_extent_path *curpath;
945 i = depth = ext4_ext_inode_depth(ip);
947 /* Look for free index entry int the tree */
948 curpath = path + depth;
949 while (i > 0 && !EXT_HAS_FREE_INDEX(curpath)) {
955 * We use already allocated block for index block,
956 * so subsequent data blocks should be contiguous.
958 if (EXT_HAS_FREE_INDEX(curpath)) {
959 error = ext4_ext_split(ip, path, newext, i);
964 ext4_ext_drop_refs(path);
965 error = ext4_ext_find_extent(ip, le32toh(newext->e_blk), &path);
969 /* Tree is full, do grow in depth. */
970 error = ext4_ext_grow_indepth(ip, path, newext);
975 ext4_ext_drop_refs(path);
976 error = ext4_ext_find_extent(ip, le32toh(newext->e_blk), &path);
980 /* Check and split tree if required. */
981 depth = ext4_ext_inode_depth(ip);
982 if (le16toh(path[depth].ep_header->eh_ecount) ==
983 le16toh(path[depth].ep_header->eh_max))
992 ext4_ext_correct_indexes(struct inode *ip, struct ext4_extent_path *path)
994 struct ext4_extent_header *eh;
995 struct ext4_extent *ex;
999 depth = ext4_ext_inode_depth(ip);
1000 eh = path[depth].ep_header;
1001 ex = path[depth].ep_ext;
1003 if (ex == NULL || eh == NULL)
1009 /* We will correct tree if first leaf got modified only. */
1010 if (ex != EXT_FIRST_EXTENT(eh))
1014 border = le32toh(path[depth].ep_ext->e_blk);
1015 path[k].ep_index->ei_blk = htole32(border);
1016 ext4_ext_dirty(ip, path + k);
1018 /* Change all left-side indexes. */
1019 if (path[k+1].ep_index != EXT_FIRST_INDEX(path[k+1].ep_header))
1022 path[k].ep_index->ei_blk = htole32(border);
1023 ext4_ext_dirty(ip, path + k);
1030 ext4_ext_insert_extent(struct inode *ip, struct ext4_extent_path *path,
1031 struct ext4_extent *newext)
1033 struct ext4_extent_header * eh;
1034 struct ext4_extent *ex, *nex, *nearex;
1035 struct ext4_extent_path *npath;
1036 int depth, len, error, next;
1038 depth = ext4_ext_inode_depth(ip);
1039 ex = path[depth].ep_ext;
1042 if (htole16(newext->e_len) == 0 || path[depth].ep_header == NULL)
1045 /* Insert block into found extent. */
1046 if (ex && ext4_can_extents_be_merged(ex, newext)) {
1047 ex->e_len = htole16(le16toh(ex->e_len) + le16toh(newext->e_len));
1048 eh = path[depth].ep_header;
1054 depth = ext4_ext_inode_depth(ip);
1055 eh = path[depth].ep_header;
1056 if (le16toh(eh->eh_ecount) < le16toh(eh->eh_max))
1060 nex = EXT_LAST_EXTENT(eh);
1061 next = ext4_ext_next_leaf_block(ip, path);
1062 if (le32toh(newext->e_blk) > le32toh(nex->e_blk) && next !=
1064 KASSERT(npath == NULL,
1065 ("ext4_ext_insert_extent: bad path"));
1067 error = ext4_ext_find_extent(ip, next, &npath);
1071 if (npath->ep_depth != path->ep_depth) {
1076 eh = npath[depth].ep_header;
1077 if (le16toh(eh->eh_ecount) < le16toh(eh->eh_max)) {
1084 * There is no free space in the found leaf,
1085 * try to add a new leaf to the tree.
1087 error = ext4_ext_create_new_leaf(ip, path, newext);
1091 depth = ext4_ext_inode_depth(ip);
1092 eh = path[depth].ep_header;
1095 nearex = path[depth].ep_ext;
1097 /* Create new extent in the leaf. */
1098 path[depth].ep_ext = EXT_FIRST_EXTENT(eh);
1099 } else if (le32toh(newext->e_blk) > le32toh(nearex->e_blk)) {
1100 if (nearex != EXT_LAST_EXTENT(eh)) {
1101 len = EXT_MAX_EXTENT(eh) - nearex;
1102 len = (len - 1) * sizeof(struct ext4_extent);
1103 len = len < 0 ? 0 : len;
1104 memmove(nearex + 2, nearex + 1, len);
1106 path[depth].ep_ext = nearex + 1;
1108 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1109 len = len < 0 ? 0 : len;
1110 memmove(nearex + 1, nearex, len);
1111 path[depth].ep_ext = nearex;
1114 eh->eh_ecount = htole16(le16toh(eh->eh_ecount) + 1);
1115 nearex = path[depth].ep_ext;
1116 nearex->e_blk = newext->e_blk;
1117 nearex->e_start_lo = newext->e_start_lo;
1118 nearex->e_start_hi = newext->e_start_hi;
1119 nearex->e_len = newext->e_len;
1122 /* Try to merge extents to the right. */
1123 while (nearex < EXT_LAST_EXTENT(eh)) {
1124 if (!ext4_can_extents_be_merged(nearex, nearex + 1))
1127 /* Merge with next extent. */
1128 nearex->e_len = htole16(le16toh(nearex->e_len) +
1129 le16toh(nearex[1].e_len));
1130 if (nearex + 1 < EXT_LAST_EXTENT(eh)) {
1131 len = (EXT_LAST_EXTENT(eh) - nearex - 1) *
1132 sizeof(struct ext4_extent);
1133 memmove(nearex + 1, nearex + 2, len);
1136 eh->eh_ecount = htole16(le16toh(eh->eh_ecount) - 1);
1137 KASSERT(le16toh(eh->eh_ecount) != 0,
1138 ("ext4_ext_insert_extent: bad ecount"));
1142 * Try to merge extents to the left,
1143 * start from inexes correction.
1145 error = ext4_ext_correct_indexes(ip, path);
1149 ext4_ext_dirty(ip, path + depth);
1153 ext4_ext_drop_refs(npath);
1154 free(npath, M_EXT2EXTENTS);
1157 ip->i_ext_cache.ec_type = EXT4_EXT_CACHE_NO;
1162 ext4_new_blocks(struct inode *ip, daddr_t lbn, e4fs_daddr_t pref,
1163 struct ucred *cred, unsigned long *count, int *perror)
1165 struct m_ext2fs *fs;
1166 e4fs_daddr_t newblk;
1169 * We will allocate only single block for now.
1175 EXT2_LOCK(ip->i_ump);
1176 *perror = ext2_alloc(ip, lbn, pref, (int)fs->e2fs_bsize, cred, &newblk);
1181 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1182 ext2_update(ip->i_vnode, 1);
1189 ext4_ext_get_blocks(struct inode *ip, e4fs_daddr_t iblk,
1190 unsigned long max_blocks, struct ucred *cred, struct buf **bpp,
1191 int *pallocated, daddr_t *nb)
1193 struct m_ext2fs *fs;
1194 struct buf *bp = NULL;
1195 struct ext4_extent_path *path;
1196 struct ext4_extent newex, *ex;
1197 e4fs_daddr_t bpref, newblk = 0;
1198 unsigned long allocated = 0;
1199 int error = 0, depth;
1207 if ((bpref = ext4_ext_in_cache(ip, iblk, &newex))) {
1208 if (bpref == EXT4_EXT_CACHE_IN) {
1209 /* Block is already allocated. */
1210 newblk = iblk - le32toh(newex.e_blk) +
1211 ext4_ext_extent_pblock(&newex);
1212 allocated = le16toh(newex.e_len) - (iblk - le32toh(newex.e_blk));
1220 error = ext4_ext_find_extent(ip, iblk, &path);
1225 depth = ext4_ext_inode_depth(ip);
1226 if (path[depth].ep_ext == NULL && depth != 0) {
1231 if ((ex = path[depth].ep_ext)) {
1232 uint64_t lblk = le32toh(ex->e_blk);
1233 uint16_t e_len = le16toh(ex->e_len);
1234 e4fs_daddr_t e_start = ext4_ext_extent_pblock(ex);
1236 if (e_len > EXT4_MAX_LEN)
1239 /* If we found extent covers block, simply return it. */
1240 if (iblk >= lblk && iblk < lblk + e_len) {
1241 newblk = iblk - lblk + e_start;
1242 allocated = e_len - (iblk - lblk);
1243 ext4_ext_put_in_cache(ip, lblk, e_len,
1244 e_start, EXT4_EXT_CACHE_IN);
1249 /* Allocate the new block. */
1250 if (S_ISREG(ip->i_mode) && (!ip->i_next_alloc_block)) {
1251 ip->i_next_alloc_goal = 0;
1254 bpref = ext4_ext_blkpref(ip, path, iblk);
1255 allocated = max_blocks;
1256 newblk = ext4_new_blocks(ip, iblk, bpref, cred, &allocated, &error);
1260 /* Try to insert new extent into found leaf and return. */
1261 newex.e_blk = htole32(iblk);
1262 ext4_ext_store_pblock(&newex, newblk);
1263 newex.e_len = htole16(allocated);
1264 error = ext4_ext_insert_extent(ip, path, &newex);
1268 newblk = ext4_ext_extent_pblock(&newex);
1269 ext4_ext_put_in_cache(ip, iblk, allocated, newblk, EXT4_EXT_CACHE_IN);
1273 if (allocated > max_blocks)
1274 allocated = max_blocks;
1279 error = bread(ip->i_devvp, fsbtodb(fs, newblk),
1280 fs->e2fs_bsize, cred, &bp);
1290 ext4_ext_drop_refs(path);
1291 free(path, M_EXT2EXTENTS);
1300 static inline uint16_t
1301 ext4_ext_get_actual_len(struct ext4_extent *ext)
1304 return (le16toh(ext->e_len) <= EXT_INIT_MAX_LEN ?
1305 le16toh(ext->e_len) : (le16toh(ext->e_len) - EXT_INIT_MAX_LEN));
1308 static inline struct ext4_extent_header *
1309 ext4_ext_header(struct inode *ip)
1312 return ((struct ext4_extent_header *)ip->i_db);
1316 ext4_remove_blocks(struct inode *ip, struct ext4_extent *ex,
1317 unsigned long from, unsigned long to)
1319 unsigned long num, start;
1321 if (from >= le32toh(ex->e_blk) &&
1322 to == le32toh(ex->e_blk) + ext4_ext_get_actual_len(ex) - 1) {
1324 num = le32toh(ex->e_blk) + ext4_ext_get_actual_len(ex) - from;
1325 start = ext4_ext_extent_pblock(ex) +
1326 ext4_ext_get_actual_len(ex) - num;
1327 ext4_ext_blkfree(ip, start, num, 0);
1334 ext4_ext_rm_index(struct inode *ip, struct ext4_extent_path *path)
1338 /* Free index block. */
1340 leaf = ext4_ext_index_pblock(path->ep_index);
1341 KASSERT(path->ep_header->eh_ecount != 0,
1342 ("ext4_ext_rm_index: bad ecount"));
1343 path->ep_header->eh_ecount =
1344 htole16(le16toh(path->ep_header->eh_ecount) - 1);
1345 ext4_ext_dirty(ip, path);
1346 ext4_ext_blkfree(ip, leaf, 1, 0);
1351 ext4_ext_rm_leaf(struct inode *ip, struct ext4_extent_path *path,
1354 struct ext4_extent_header *eh;
1355 struct ext4_extent *ex;
1356 unsigned int a, b, block, num;
1357 unsigned long ex_blk;
1358 unsigned short ex_len;
1360 int error, correct_index;
1362 depth = ext4_ext_inode_depth(ip);
1363 if (!path[depth].ep_header) {
1364 if (path[depth].ep_data == NULL)
1366 path[depth].ep_header =
1367 (struct ext4_extent_header* )path[depth].ep_data;
1370 eh = path[depth].ep_header;
1372 SDT_PROBE2(ext2fs, , trace, extents, 1,
1373 "bad header => extent corrupted");
1377 ex = EXT_LAST_EXTENT(eh);
1378 ex_blk = le32toh(ex->e_blk);
1379 ex_len = ext4_ext_get_actual_len(ex);
1383 while (ex >= EXT_FIRST_EXTENT(eh) && ex_blk + ex_len > start) {
1384 path[depth].ep_ext = ex;
1385 a = ex_blk > start ? ex_blk : start;
1386 b = (uint64_t)ex_blk + ex_len - 1 <
1387 EXT4_MAX_BLOCKS ? ex_blk + ex_len - 1 : EXT4_MAX_BLOCKS;
1389 if (a != ex_blk && b != ex_blk + ex_len - 1)
1391 else if (a != ex_blk) {
1392 /* Remove tail of the extent. */
1395 } else if (b != ex_blk + ex_len - 1) {
1396 /* Remove head of the extent, not implemented. */
1399 /* Remove whole extent. */
1404 if (ex == EXT_FIRST_EXTENT(eh))
1407 error = ext4_remove_blocks(ip, ex, a, b);
1412 ext4_ext_store_pblock(ex, 0);
1413 eh->eh_ecount = htole16(le16toh(eh->eh_ecount) - 1);
1416 ex->e_blk = htole32(block);
1417 ex->e_len = htole16(num);
1419 ext4_ext_dirty(ip, path + depth);
1422 ex_blk = htole32(ex->e_blk);
1423 ex_len = ext4_ext_get_actual_len(ex);
1426 if (correct_index && le16toh(eh->eh_ecount))
1427 error = ext4_ext_correct_indexes(ip, path);
1430 * If this leaf is free, we should
1431 * remove it from index block above.
1433 if (error == 0 && eh->eh_ecount == 0 &&
1434 path[depth].ep_data != NULL)
1435 error = ext4_ext_rm_index(ip, path + depth);
1442 ext4_read_extent_tree_block(struct inode *ip, e4fs_daddr_t pblk,
1443 int depth, int flags)
1445 struct m_ext2fs *fs;
1446 struct ext4_extent_header *eh;
1451 error = bread(ip->i_devvp, fsbtodb(fs, pblk),
1452 fs->e2fs_bsize, NOCRED, &bp);
1457 eh = ext4_ext_block_header(bp->b_data);
1458 if (le16toh(eh->eh_depth) != depth) {
1459 SDT_PROBE2(ext2fs, , trace, extents, 1,
1460 "unexpected eh_depth");
1464 error = ext4_ext_check_header(ip, eh);
1477 ext4_ext_more_to_rm(struct ext4_extent_path *path)
1480 KASSERT(path->ep_index != NULL,
1481 ("ext4_ext_more_to_rm: bad index from path"));
1483 if (path->ep_index < EXT_FIRST_INDEX(path->ep_header))
1486 if (le16toh(path->ep_header->eh_ecount) == path->index_count)
1493 ext4_ext_remove_space(struct inode *ip, off_t length, int flags,
1494 struct ucred *cred, struct thread *td)
1497 struct ext4_extent_header *ehp;
1498 struct ext4_extent_path *path;
1502 ehp = (struct ext4_extent_header *)ip->i_db;
1503 depth = ext4_ext_inode_depth(ip);
1505 error = ext4_ext_check_header(ip, ehp);
1509 path = malloc(sizeof(struct ext4_extent_path) * (depth + 1),
1510 M_EXT2EXTENTS, M_WAITOK | M_ZERO);
1511 path[0].ep_header = ehp;
1512 path[0].ep_depth = depth;
1514 while (error == 0 && i >= 0) {
1517 error = ext4_ext_rm_leaf(ip, path, length);
1520 free(path[i].ep_data, M_EXT2EXTENTS);
1521 path[i].ep_data = NULL;
1526 /* This is index. */
1527 if (!path[i].ep_header)
1529 (struct ext4_extent_header *)path[i].ep_data;
1531 if (!path[i].ep_index) {
1532 /* This level hasn't touched yet. */
1533 path[i].ep_index = EXT_LAST_INDEX(path[i].ep_header);
1534 path[i].index_count =
1535 le16toh(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 =
1555 le16toh(path[i].ep_header->eh_ecount);
1558 if (path[i].ep_header->eh_ecount == 0 && i > 0) {
1559 /* Index is empty, remove it. */
1560 error = ext4_ext_rm_index(ip, path + i);
1562 free(path[i].ep_data, M_EXT2EXTENTS);
1563 path[i].ep_data = NULL;
1568 if (path->ep_header->eh_ecount == 0) {
1570 * Truncate the tree to zero.
1572 ext4_ext_header(ip)->eh_depth = 0;
1573 ext4_ext_header(ip)->eh_max = htole16(ext4_ext_space_root(ip));
1574 ext4_ext_dirty(ip, path);
1577 ext4_ext_drop_refs(path);
1578 free(path, M_EXT2EXTENTS);