2 * SPDX-License-Identifier: BSD-2-Clause
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
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>
37 #include <sys/endian.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 SDT_PROVIDER_DECLARE(ext2fs);
52 * arg0: verbosity. Higher numbers give more verbose messages
53 * arg1: Textual message
55 SDT_PROBE_DEFINE2(ext2fs, , trace, extents, "int", "char*");
57 static MALLOC_DEFINE(M_EXT2EXTENTS, "ext2_extents", "EXT2 extents");
59 #ifdef EXT2FS_PRINT_EXTENTS
60 static const bool print_extents_walk = true;
62 static int ext4_ext_check_header(struct inode *, struct ext4_extent_header *,
64 static int ext4_ext_walk_header(struct inode *, struct ext4_extent_header *,
66 static inline e4fs_daddr_t ext4_ext_index_pblock(struct ext4_extent_index *);
67 static inline e4fs_daddr_t ext4_ext_extent_pblock(struct ext4_extent *);
70 ext4_ext_blk_check(struct inode *ip, e4fs_daddr_t blk)
76 if (blk < fs->e2fs->e2fs_first_dblock || blk >= fs->e2fs_bcount)
83 ext4_ext_walk_index(struct inode *ip, struct ext4_extent_index *ex, int depth,
93 if (print_extents_walk)
94 printf(" index %p => (blk %u pblk %ju)\n", ex,
96 (uint64_t)le16toh(ex->ei_leaf_hi) << 32 |
97 le32toh(ex->ei_leaf_lo));
102 blk = ext4_ext_index_pblock(ex);
103 error = ext4_ext_blk_check(ip, blk);
107 if ((error = bread(ip->i_devvp,
108 fsbtodb(fs, blk), (int)fs->e2fs_bsize, NOCRED, &bp)) != 0) {
113 error = ext4_ext_walk_header(ip,
114 (struct ext4_extent_header *)bp->b_data, depth);
122 ext4_ext_walk_extent(struct inode *ip, struct ext4_extent *ep)
127 blk = ext4_ext_extent_pblock(ep);
128 error = ext4_ext_blk_check(ip, blk);
132 if (print_extents_walk)
133 printf(" ext %p => (blk %u len %u start %ju)\n",
134 ep, le32toh(ep->e_blk), le16toh(ep->e_len),
141 ext4_ext_walk_header(struct inode *ip, struct ext4_extent_header *eh, int depth)
145 error = ext4_ext_check_header(ip, eh, depth);
149 if (print_extents_walk)
150 printf("header %p => (entries %d max %d depth %d gen %d)\n",
151 eh, le16toh(eh->eh_ecount),
152 le16toh(eh->eh_max), le16toh(eh->eh_depth),
153 le32toh(eh->eh_gen));
155 for (i = 0; i < le16toh(eh->eh_ecount) && error == 0; i++)
156 if (eh->eh_depth != 0)
157 error = ext4_ext_walk_index(ip,
158 (struct ext4_extent_index *)(eh + 1 + i), depth - 1,
161 error = ext4_ext_walk_extent(ip,
162 (struct ext4_extent *)(eh + 1 + i));
168 ext4_ext_walk(struct inode *ip)
170 struct ext4_extent_header *ehp;
172 ehp = (struct ext4_extent_header *)ip->i_db;
174 if (print_extents_walk)
175 printf("Extent status:ip=%ju\n", ip->i_number);
177 if (!(ip->i_flag & IN_E4EXTENTS))
180 return (ext4_ext_walk_header(ip, ehp, 0));
184 ext4_ext_print_path(struct inode *ip, struct ext4_extent_path *path)
186 int k, depth, error = 0;
188 depth = path->ep_depth;
190 if (print_extents_walk)
191 printf("ip=%ju, Path:\n", ip->i_number);
193 for (k = 0; k <= depth && error == 0; k++, path++) {
194 if (path->ep_index) {
195 error = ext4_ext_walk_index(ip, path->ep_index,
197 } else if (path->ep_ext) {
198 error = ext4_ext_walk_extent(ip, path->ep_ext);
206 static inline struct ext4_extent_header *
207 ext4_ext_inode_header(struct inode *ip)
210 return ((struct ext4_extent_header *)ip->i_db);
213 static inline struct ext4_extent_header *
214 ext4_ext_block_header(char *bdata)
217 return ((struct ext4_extent_header *)bdata);
220 static inline unsigned short
221 ext4_ext_inode_depth(struct inode *ip)
223 struct ext4_extent_header *ehp;
225 ehp = (struct ext4_extent_header *)ip->i_data;
226 return (le16toh(ehp->eh_depth));
229 static inline e4fs_daddr_t
230 ext4_ext_index_pblock(struct ext4_extent_index *index)
234 blk = le32toh(index->ei_leaf_lo);
235 blk |= (e4fs_daddr_t)le16toh(index->ei_leaf_hi) << 32;
241 ext4_index_store_pblock(struct ext4_extent_index *index, e4fs_daddr_t pb)
244 index->ei_leaf_lo = htole32(pb & 0xffffffff);
245 index->ei_leaf_hi = htole16((pb >> 32) & 0xffff);
248 static inline e4fs_daddr_t
249 ext4_ext_extent_pblock(struct ext4_extent *extent)
253 blk = le32toh(extent->e_start_lo);
254 blk |= (e4fs_daddr_t)le16toh(extent->e_start_hi) << 32;
260 ext4_ext_store_pblock(struct ext4_extent *ex, e4fs_daddr_t pb)
263 ex->e_start_lo = htole32(pb & 0xffffffff);
264 ex->e_start_hi = htole16((pb >> 32) & 0xffff);
268 ext4_ext_in_cache(struct inode *ip, daddr_t lbn, struct ext4_extent *ep)
270 struct ext4_extent_cache *ecp;
271 int ret = EXT4_EXT_CACHE_NO;
273 ecp = &ip->i_ext_cache;
274 if (ecp->ec_type == EXT4_EXT_CACHE_NO)
277 if (lbn >= ecp->ec_blk && lbn < ecp->ec_blk + ecp->ec_len) {
278 ep->e_blk = htole32(ecp->ec_blk);
279 ep->e_start_lo = htole32(ecp->ec_start & 0xffffffff);
280 ep->e_start_hi = htole16(ecp->ec_start >> 32 & 0xffff);
281 ep->e_len = htole16(ecp->ec_len);
288 ext4_ext_space_root(struct inode *ip)
292 size = sizeof(ip->i_data);
293 size -= sizeof(struct ext4_extent_header);
294 size /= sizeof(struct ext4_extent);
300 ext4_ext_space_block(struct inode *ip)
307 size = (fs->e2fs_bsize - sizeof(struct ext4_extent_header)) /
308 sizeof(struct ext4_extent);
314 ext4_ext_space_root_idx(struct inode *ip)
318 size = sizeof(ip->i_data);
319 size -= sizeof(struct ext4_extent_header);
320 size /= sizeof(struct ext4_extent_index);
326 ext4_ext_space_block_idx(struct inode *ip)
333 size = (fs->e2fs_bsize - sizeof(struct ext4_extent_header)) /
334 sizeof(struct ext4_extent_index);
340 ext4_ext_max_entries(struct inode *ip, int depth)
343 if (depth == ext4_ext_inode_depth(ip)) {
345 return (ext4_ext_space_root(ip));
347 return (ext4_ext_space_root_idx(ip));
350 return (ext4_ext_space_block(ip));
352 return (ext4_ext_space_block_idx(ip));
356 static inline uint16_t
357 ext4_ext_get_actual_len(struct ext4_extent *ext)
360 return (le16toh(ext->e_len) <= EXT_INIT_MAX_LEN ?
361 le16toh(ext->e_len) : (le16toh(ext->e_len) - EXT_INIT_MAX_LEN));
366 ext4_inode_block_validate(struct inode *ip, e4fs_daddr_t start_blk,
373 if ((start_blk <= le32toh(fs->e2fs->e2fs_first_dblock)) ||
374 (start_blk + count < start_blk) ||
375 (start_blk + count > fs->e2fs_bcount))
382 ext4_validate_extent(struct inode *ip, struct ext4_extent *ext)
384 e4fs_daddr_t blk = ext4_ext_extent_pblock(ext);
385 uint32_t lblk = le32toh(ext->e_blk);
386 int len = ext4_ext_get_actual_len(ext);
388 if (lblk + len <= lblk)
391 return (ext4_inode_block_validate(ip, blk, len));
395 ext4_validate_extent_idx(struct inode *ip, struct ext4_extent_index *ext_idx)
397 e4fs_daddr_t blk = ext4_ext_index_pblock(ext_idx);
399 return (ext4_inode_block_validate(ip, blk, 1));
403 ext4_validate_extent_entries(struct inode *ip, struct ext4_extent_header *eh,
408 count = le16toh(eh->eh_ecount);
413 struct ext4_extent *ext = EXT_FIRST_EXTENT(eh);
419 if (ext4_validate_extent(ip, ext))
422 /* Check for overlapping extents */
423 lblk = le32toh(ext->e_blk);
424 len = ext4_ext_get_actual_len(ext);
425 if ((lblk <= prev) && prev)
430 prev = lblk + len - 1;
433 struct ext4_extent_index *ext_idx = EXT_FIRST_INDEX(eh);
435 if (ext4_validate_extent_idx(ip, ext_idx))
447 ext4_ext_check_header(struct inode *ip, struct ext4_extent_header *eh,
453 char *error_msg __unused;
456 if (le16toh(eh->eh_magic) != EXT4_EXT_MAGIC) {
457 error_msg = "header: invalid magic";
460 if (le16toh(eh->eh_depth) != depth ||
461 le16toh(eh->eh_depth) > EXT4_EXT_DEPTH_MAX)
463 error_msg = "header: invalid eh_depth";
466 if (eh->eh_max == 0) {
467 error_msg = "header: invalid eh_max";
470 if (le16toh(eh->eh_max) > ext4_ext_max_entries(ip, depth)) {
471 error_msg = "header: too large eh_max";
474 if (le16toh(eh->eh_ecount) > le16toh(eh->eh_max)) {
475 error_msg = "header: invalid eh_entries";
478 if (le16toh(eh->eh_depth) > EXT4_EXT_DEPTH_MAX) {
479 error_msg = "header: invalid eh_depth";
482 if (ext4_validate_extent_entries(ip, eh, depth)) {
483 error_msg = "header: invalid extent entries";
490 SDT_PROBE2(ext2fs, , trace, extents, 1, error_msg);
495 ext4_ext_binsearch_index(struct ext4_extent_path *path, int blk)
497 struct ext4_extent_header *eh;
498 struct ext4_extent_index *r, *l, *m;
500 eh = path->ep_header;
502 KASSERT(le16toh(eh->eh_ecount) <= le16toh(eh->eh_max) &&
503 le16toh(eh->eh_ecount) > 0,
504 ("ext4_ext_binsearch_index: bad args"));
506 l = EXT_FIRST_INDEX(eh) + 1;
507 r = EXT_FIRST_INDEX(eh) + le16toh(eh->eh_ecount) - 1;
510 if (blk < le32toh(m->ei_blk))
516 path->ep_index = l - 1;
520 ext4_ext_binsearch_ext(struct ext4_extent_path *path, int blk)
522 struct ext4_extent_header *eh;
523 struct ext4_extent *r, *l, *m;
525 eh = path->ep_header;
527 KASSERT(le16toh(eh->eh_ecount) <= le16toh(eh->eh_max),
528 ("ext4_ext_binsearch_ext: bad args"));
530 if (eh->eh_ecount == 0)
533 l = EXT_FIRST_EXTENT(eh) + 1;
534 r = EXT_FIRST_EXTENT(eh) + le16toh(eh->eh_ecount) - 1;
538 if (blk < le32toh(m->e_blk))
544 path->ep_ext = l - 1;
548 ext4_ext_fill_path_bdata(struct ext4_extent_path *path,
549 struct buf *bp, uint64_t blk)
552 KASSERT(path->ep_data == NULL,
553 ("ext4_ext_fill_path_bdata: bad ep_data"));
555 path->ep_data = malloc(bp->b_bufsize, M_EXT2EXTENTS, M_WAITOK);
556 memcpy(path->ep_data, bp->b_data, bp->b_bufsize);
563 ext4_ext_fill_path_buf(struct ext4_extent_path *path, struct buf *bp)
566 KASSERT(path->ep_data != NULL,
567 ("ext4_ext_fill_path_buf: bad ep_data"));
569 memcpy(bp->b_data, path->ep_data, bp->b_bufsize);
573 ext4_ext_drop_refs(struct ext4_extent_path *path)
580 depth = path->ep_depth;
581 for (i = 0; i <= depth; i++, path++)
583 free(path->ep_data, M_EXT2EXTENTS);
584 path->ep_data = NULL;
589 ext4_ext_path_free(struct ext4_extent_path *path)
595 ext4_ext_drop_refs(path);
596 free(path, M_EXT2EXTENTS);
600 ext4_ext_find_extent(struct inode *ip, daddr_t block,
601 struct ext4_extent_path **ppath)
603 struct ext4_extent_header *eh;
604 struct ext4_extent_path *path;
607 int error, depth, i, ppos, alloc;
609 eh = ext4_ext_inode_header(ip);
610 depth = ext4_ext_inode_depth(ip);
614 error = ext4_ext_check_header(ip, eh, depth);
623 path = malloc(EXT4_EXT_DEPTH_MAX *
624 sizeof(struct ext4_extent_path),
625 M_EXT2EXTENTS, M_WAITOK | M_ZERO);
630 path[0].ep_header = eh;
631 path[0].ep_data = NULL;
633 /* Walk through the tree. */
636 ext4_ext_binsearch_index(&path[ppos], block);
637 blk = ext4_ext_index_pblock(path[ppos].ep_index);
638 path[ppos].ep_depth = i;
639 path[ppos].ep_ext = NULL;
641 error = bread(ip->i_devvp, fsbtodb(ip->i_e2fs, blk),
642 ip->i_e2fs->e2fs_bsize, NOCRED, &bp);
649 SDT_PROBE2(ext2fs, , trace, extents, 1,
650 "ppos > depth => extent corrupted");
656 ext4_ext_fill_path_bdata(&path[ppos], bp, blk);
659 eh = ext4_ext_block_header(path[ppos].ep_data);
660 if (ext4_ext_check_header(ip, eh, i - 1) ||
661 ext2_extent_blk_csum_verify(ip, path[ppos].ep_data)) {
666 path[ppos].ep_header = eh;
671 error = ext4_ext_check_header(ip, eh, 0);
676 path[ppos].ep_depth = i;
677 path[ppos].ep_header = eh;
678 path[ppos].ep_ext = NULL;
679 path[ppos].ep_index = NULL;
680 ext4_ext_binsearch_ext(&path[ppos], block);
684 ext4_ext_drop_refs(path);
686 free(path, M_EXT2EXTENTS);
694 ext4_ext_space_block_index(struct inode *ip)
701 size = (fs->e2fs_bsize - sizeof(struct ext4_extent_header)) /
702 sizeof(struct ext4_extent_index);
708 ext4_ext_tree_init(struct inode *ip)
710 struct ext4_extent_header *ehp;
712 ip->i_flag |= IN_E4EXTENTS;
714 memset(ip->i_data, 0, EXT2_NDADDR + EXT2_NIADDR);
715 ehp = (struct ext4_extent_header *)ip->i_data;
716 ehp->eh_magic = htole16(EXT4_EXT_MAGIC);
717 ehp->eh_max = htole16(ext4_ext_space_root(ip));
718 ip->i_ext_cache.ec_type = EXT4_EXT_CACHE_NO;
719 ip->i_flag |= IN_CHANGE | IN_UPDATE;
720 ext2_update(ip->i_vnode, 1);
724 ext4_ext_put_in_cache(struct inode *ip, uint32_t blk,
725 uint32_t len, uint32_t start, int type)
728 KASSERT(len != 0, ("ext4_ext_put_in_cache: bad input"));
730 ip->i_ext_cache.ec_type = type;
731 ip->i_ext_cache.ec_blk = blk;
732 ip->i_ext_cache.ec_len = len;
733 ip->i_ext_cache.ec_start = start;
737 ext4_ext_blkpref(struct inode *ip, struct ext4_extent_path *path,
741 struct ext4_extent *ex;
742 e4fs_daddr_t bg_start;
748 depth = path->ep_depth;
749 ex = path[depth].ep_ext;
751 e4fs_daddr_t pblk = ext4_ext_extent_pblock(ex);
752 e2fs_daddr_t blk = le32toh(ex->e_blk);
755 return (pblk + (block - blk));
757 return (pblk - (blk - block));
760 /* Try to get block from index itself. */
761 if (path[depth].ep_data)
762 return (path[depth].ep_blk);
765 /* Use inode's group. */
766 bg_start = (ip->i_block_group * EXT2_BLOCKS_PER_GROUP(ip->i_e2fs)) +
767 le32toh(fs->e2fs->e2fs_first_dblock);
769 return (bg_start + block);
773 ext4_can_extents_be_merged(struct ext4_extent *ex1,
774 struct ext4_extent *ex2)
777 if (le32toh(ex1->e_blk) + le16toh(ex1->e_len) != le32toh(ex2->e_blk))
780 if (le16toh(ex1->e_len) + le16toh(ex2->e_len) > EXT4_MAX_LEN)
783 if (ext4_ext_extent_pblock(ex1) + le16toh(ex1->e_len) ==
784 ext4_ext_extent_pblock(ex2))
791 ext4_ext_next_leaf_block(struct inode *ip, struct ext4_extent_path *path)
793 int depth = path->ep_depth;
797 return (EXT4_MAX_BLOCKS);
803 if (path[depth].ep_index !=
804 EXT_LAST_INDEX(path[depth].ep_header))
805 return (le32toh(path[depth].ep_index[1].ei_blk));
810 return (EXT4_MAX_BLOCKS);
814 ext4_ext_dirty(struct inode *ip, struct ext4_extent_path *path)
828 bp = getblk(ip->i_devvp, fsbtodb(fs, blk),
829 fs->e2fs_bsize, 0, 0, 0);
832 ext4_ext_fill_path_buf(path, bp);
833 ext2_extent_blk_csum_set(ip, bp->b_data);
836 ip->i_flag |= IN_CHANGE | IN_UPDATE;
837 error = ext2_update(ip->i_vnode, 1);
844 ext4_ext_insert_index(struct inode *ip, struct ext4_extent_path *path,
845 uint32_t lblk, e4fs_daddr_t blk)
847 struct ext4_extent_index *idx;
850 if (lblk == le32toh(path->ep_index->ei_blk)) {
851 SDT_PROBE2(ext2fs, , trace, extents, 1,
852 "lblk == index blk => extent corrupted");
856 if (le16toh(path->ep_header->eh_ecount) >=
857 le16toh(path->ep_header->eh_max)) {
858 SDT_PROBE2(ext2fs, , trace, extents, 1,
859 "ecout > maxcount => extent corrupted");
863 if (lblk > le32toh(path->ep_index->ei_blk)) {
865 idx = path->ep_index + 1;
868 idx = path->ep_index;
871 len = EXT_LAST_INDEX(path->ep_header) - idx + 1;
873 memmove(idx + 1, idx, len * sizeof(struct ext4_extent_index));
875 if (idx > EXT_MAX_INDEX(path->ep_header)) {
876 SDT_PROBE2(ext2fs, , trace, extents, 1,
877 "index is out of range => extent corrupted");
881 idx->ei_blk = htole32(lblk);
882 ext4_index_store_pblock(idx, blk);
883 path->ep_header->eh_ecount =
884 htole16(le16toh(path->ep_header->eh_ecount) + 1);
886 return (ext4_ext_dirty(ip, path));
890 ext4_ext_alloc_meta(struct inode *ip)
892 e4fs_daddr_t blk = ext2_alloc_meta(ip);
894 ip->i_blocks += btodb(ip->i_e2fs->e2fs_bsize);
895 ip->i_flag |= IN_CHANGE | IN_UPDATE;
896 ext2_update(ip->i_vnode, 1);
903 ext4_ext_blkfree(struct inode *ip, uint64_t blk, int count, int flags)
906 int i, blocksreleased;
909 blocksreleased = count;
911 for(i = 0; i < count; i++)
912 ext2_blkfree(ip, blk + i, fs->e2fs_bsize);
914 if (ip->i_blocks >= blocksreleased)
915 ip->i_blocks -= (btodb(fs->e2fs_bsize)*blocksreleased);
919 ip->i_flag |= IN_CHANGE | IN_UPDATE;
920 ext2_update(ip->i_vnode, 1);
924 ext4_ext_split(struct inode *ip, struct ext4_extent_path *path,
925 struct ext4_extent *newext, int at)
929 int depth = ext4_ext_inode_depth(ip);
930 struct ext4_extent_header *neh;
931 struct ext4_extent_index *fidx;
932 struct ext4_extent *ex;
934 e4fs_daddr_t newblk, oldblk;
936 e4fs_daddr_t *ablks = NULL;
943 * We will split at current extent for now.
945 if (path[depth].ep_ext > EXT_MAX_EXTENT(path[depth].ep_header)) {
946 SDT_PROBE2(ext2fs, , trace, extents, 1,
947 "extent is out of range => extent corrupted");
951 if (path[depth].ep_ext != EXT_MAX_EXTENT(path[depth].ep_header))
952 border = le32toh(path[depth].ep_ext[1].e_blk);
954 border = le32toh(newext->e_blk);
956 /* Allocate new blocks. */
957 ablks = malloc(sizeof(e4fs_daddr_t) * depth,
958 M_EXT2EXTENTS, M_WAITOK | M_ZERO);
959 for (a = 0; a < depth - at; a++) {
960 newblk = ext4_ext_alloc_meta(ip);
967 bp = getblk(ip->i_devvp, fsbtodb(fs, newblk), fs->e2fs_bsize, 0, 0, 0);
973 neh = ext4_ext_block_header(bp->b_data);
975 neh->eh_max = le16toh(ext4_ext_space_block(ip));
976 neh->eh_magic = le16toh(EXT4_EXT_MAGIC);
978 ex = EXT_FIRST_EXTENT(neh);
980 if (le16toh(path[depth].ep_header->eh_ecount) !=
981 le16toh(path[depth].ep_header->eh_max)) {
982 SDT_PROBE2(ext2fs, , trace, extents, 1,
983 "extents count out of range => extent corrupted");
988 /* Start copy from next extent. */
990 path[depth].ep_ext++;
991 while (path[depth].ep_ext <= EXT_MAX_EXTENT(path[depth].ep_header)) {
992 path[depth].ep_ext++;
996 memmove(ex, path[depth].ep_ext - m,
997 sizeof(struct ext4_extent) * m);
998 neh->eh_ecount = htole16(le16toh(neh->eh_ecount) + m);
1001 ext2_extent_blk_csum_set(ip, bp->b_data);
1007 path[depth].ep_header->eh_ecount =
1008 htole16(le16toh(path[depth].ep_header->eh_ecount) - m);
1009 ext4_ext_dirty(ip, path + depth);
1012 /* Create intermediate indexes. */
1014 KASSERT(k >= 0, ("ext4_ext_split: negative k"));
1016 /* Insert new index into current index block. */
1020 newblk = ablks[--a];
1021 error = bread(ip->i_devvp, fsbtodb(fs, newblk),
1022 (int)fs->e2fs_bsize, NOCRED, &bp);
1027 neh = (struct ext4_extent_header *)bp->b_data;
1028 neh->eh_ecount = htole16(1);
1029 neh->eh_magic = htole16(EXT4_EXT_MAGIC);
1030 neh->eh_max = htole16(ext4_ext_space_block_index(ip));
1031 neh->eh_depth = htole16(depth - i);
1032 fidx = EXT_FIRST_INDEX(neh);
1033 fidx->ei_blk = htole32(border);
1034 ext4_index_store_pblock(fidx, oldblk);
1038 while (path[i].ep_index <= EXT_MAX_INDEX(path[i].ep_header)) {
1043 memmove(++fidx, path[i].ep_index - m,
1044 sizeof(struct ext4_extent_index) * m);
1045 neh->eh_ecount = htole16(le16toh(neh->eh_ecount) + m);
1048 ext2_extent_blk_csum_set(ip, bp->b_data);
1052 /* Fix old index. */
1054 path[i].ep_header->eh_ecount =
1055 htole16(le16toh(path[i].ep_header->eh_ecount) - m);
1056 ext4_ext_dirty(ip, path + i);
1062 error = ext4_ext_insert_index(ip, path + at, border, newblk);
1069 for (i = 0; i < depth; i++) {
1072 ext4_ext_blkfree(ip, ablks[i], 1, 0);
1076 free(ablks, M_EXT2EXTENTS);
1082 ext4_ext_grow_indepth(struct inode *ip, struct ext4_extent_path *path,
1083 struct ext4_extent *newext)
1085 struct m_ext2fs *fs;
1086 struct ext4_extent_path *curpath;
1087 struct ext4_extent_header *neh;
1089 e4fs_daddr_t newblk;
1095 newblk = ext4_ext_alloc_meta(ip);
1099 bp = getblk(ip->i_devvp, fsbtodb(fs, newblk), fs->e2fs_bsize, 0, 0, 0);
1101 ext4_ext_blkfree(ip, newblk, 1, 0);
1105 /* Move top-level index/leaf into new block. */
1106 memmove(bp->b_data, curpath->ep_header, sizeof(ip->i_data));
1108 /* Set size of new block */
1109 neh = ext4_ext_block_header(bp->b_data);
1110 neh->eh_magic = htole16(EXT4_EXT_MAGIC);
1112 if (ext4_ext_inode_depth(ip))
1113 neh->eh_max = htole16(ext4_ext_space_block_index(ip));
1115 neh->eh_max = htole16(ext4_ext_space_block(ip));
1117 ext2_extent_blk_csum_set(ip, bp->b_data);
1120 ext4_ext_blkfree(ip, newblk, 1, 0);
1126 curpath->ep_header->eh_magic = htole16(EXT4_EXT_MAGIC);
1127 curpath->ep_header->eh_max = htole16(ext4_ext_space_root(ip));
1128 curpath->ep_header->eh_ecount = htole16(1);
1129 curpath->ep_index = EXT_FIRST_INDEX(curpath->ep_header);
1130 curpath->ep_index->ei_blk = EXT_FIRST_EXTENT(path[0].ep_header)->e_blk;
1131 ext4_index_store_pblock(curpath->ep_index, newblk);
1133 neh = ext4_ext_inode_header(ip);
1134 neh->eh_depth = htole16(path->ep_depth + 1);
1135 ext4_ext_dirty(ip, curpath);
1143 ext4_ext_create_new_leaf(struct inode *ip, struct ext4_extent_path *path,
1144 struct ext4_extent *newext)
1146 struct ext4_extent_path *curpath;
1147 int depth, i, error;
1150 i = depth = ext4_ext_inode_depth(ip);
1152 /* Look for free index entry int the tree */
1153 curpath = path + depth;
1154 while (i > 0 && !EXT_HAS_FREE_INDEX(curpath)) {
1160 * We use already allocated block for index block,
1161 * so subsequent data blocks should be contiguous.
1163 if (EXT_HAS_FREE_INDEX(curpath)) {
1164 error = ext4_ext_split(ip, path, newext, i);
1169 ext4_ext_drop_refs(path);
1170 error = ext4_ext_find_extent(ip, le32toh(newext->e_blk), &path);
1174 /* Tree is full, do grow in depth. */
1175 error = ext4_ext_grow_indepth(ip, path, newext);
1180 ext4_ext_drop_refs(path);
1181 error = ext4_ext_find_extent(ip, le32toh(newext->e_blk), &path);
1185 /* Check and split tree if required. */
1186 depth = ext4_ext_inode_depth(ip);
1187 if (le16toh(path[depth].ep_header->eh_ecount) ==
1188 le16toh(path[depth].ep_header->eh_max))
1197 ext4_ext_correct_indexes(struct inode *ip, struct ext4_extent_path *path)
1199 struct ext4_extent_header *eh;
1200 struct ext4_extent *ex;
1204 depth = ext4_ext_inode_depth(ip);
1205 eh = path[depth].ep_header;
1206 ex = path[depth].ep_ext;
1208 if (ex == NULL || eh == NULL)
1214 /* We will correct tree if first leaf got modified only. */
1215 if (ex != EXT_FIRST_EXTENT(eh))
1219 border = le32toh(path[depth].ep_ext->e_blk);
1220 path[k].ep_index->ei_blk = htole32(border);
1221 ext4_ext_dirty(ip, path + k);
1223 /* Change all left-side indexes. */
1224 if (path[k+1].ep_index != EXT_FIRST_INDEX(path[k+1].ep_header))
1227 path[k].ep_index->ei_blk = htole32(border);
1228 ext4_ext_dirty(ip, path + k);
1235 ext4_ext_insert_extent(struct inode *ip, struct ext4_extent_path *path,
1236 struct ext4_extent *newext)
1238 struct ext4_extent_header * eh;
1239 struct ext4_extent *ex, *nex, *nearex;
1240 struct ext4_extent_path *npath;
1241 int depth, len, error, next;
1243 depth = ext4_ext_inode_depth(ip);
1244 ex = path[depth].ep_ext;
1247 if (htole16(newext->e_len) == 0 || path[depth].ep_header == NULL)
1250 /* Insert block into found extent. */
1251 if (ex && ext4_can_extents_be_merged(ex, newext)) {
1252 ex->e_len = htole16(le16toh(ex->e_len) + le16toh(newext->e_len));
1253 eh = path[depth].ep_header;
1259 depth = ext4_ext_inode_depth(ip);
1260 eh = path[depth].ep_header;
1261 if (le16toh(eh->eh_ecount) < le16toh(eh->eh_max))
1265 nex = EXT_LAST_EXTENT(eh);
1266 next = ext4_ext_next_leaf_block(ip, path);
1267 if (le32toh(newext->e_blk) > le32toh(nex->e_blk) && next !=
1269 KASSERT(npath == NULL,
1270 ("ext4_ext_insert_extent: bad path"));
1272 error = ext4_ext_find_extent(ip, next, &npath);
1276 if (npath->ep_depth != path->ep_depth) {
1281 eh = npath[depth].ep_header;
1282 if (le16toh(eh->eh_ecount) < le16toh(eh->eh_max)) {
1289 * There is no free space in the found leaf,
1290 * try to add a new leaf to the tree.
1292 error = ext4_ext_create_new_leaf(ip, path, newext);
1296 depth = ext4_ext_inode_depth(ip);
1297 eh = path[depth].ep_header;
1300 nearex = path[depth].ep_ext;
1302 /* Create new extent in the leaf. */
1303 path[depth].ep_ext = EXT_FIRST_EXTENT(eh);
1304 } else if (le32toh(newext->e_blk) > le32toh(nearex->e_blk)) {
1305 if (nearex != EXT_LAST_EXTENT(eh)) {
1306 len = EXT_MAX_EXTENT(eh) - nearex;
1307 len = (len - 1) * sizeof(struct ext4_extent);
1308 len = len < 0 ? 0 : len;
1309 memmove(nearex + 2, nearex + 1, len);
1311 path[depth].ep_ext = nearex + 1;
1313 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1314 len = len < 0 ? 0 : len;
1315 memmove(nearex + 1, nearex, len);
1316 path[depth].ep_ext = nearex;
1319 eh->eh_ecount = htole16(le16toh(eh->eh_ecount) + 1);
1320 nearex = path[depth].ep_ext;
1321 nearex->e_blk = newext->e_blk;
1322 nearex->e_start_lo = newext->e_start_lo;
1323 nearex->e_start_hi = newext->e_start_hi;
1324 nearex->e_len = newext->e_len;
1327 /* Try to merge extents to the right. */
1328 while (nearex < EXT_LAST_EXTENT(eh)) {
1329 if (!ext4_can_extents_be_merged(nearex, nearex + 1))
1332 /* Merge with next extent. */
1333 nearex->e_len = htole16(le16toh(nearex->e_len) +
1334 le16toh(nearex[1].e_len));
1335 if (nearex + 1 < EXT_LAST_EXTENT(eh)) {
1336 len = (EXT_LAST_EXTENT(eh) - nearex - 1) *
1337 sizeof(struct ext4_extent);
1338 memmove(nearex + 1, nearex + 2, len);
1341 eh->eh_ecount = htole16(le16toh(eh->eh_ecount) - 1);
1342 KASSERT(le16toh(eh->eh_ecount) != 0,
1343 ("ext4_ext_insert_extent: bad ecount"));
1347 * Try to merge extents to the left,
1348 * start from inexes correction.
1350 error = ext4_ext_correct_indexes(ip, path);
1354 ext4_ext_dirty(ip, path + depth);
1358 ext4_ext_drop_refs(npath);
1359 free(npath, M_EXT2EXTENTS);
1362 ip->i_ext_cache.ec_type = EXT4_EXT_CACHE_NO;
1367 ext4_new_blocks(struct inode *ip, daddr_t lbn, e4fs_daddr_t pref,
1368 struct ucred *cred, unsigned long *count, int *perror)
1370 struct m_ext2fs *fs;
1371 e4fs_daddr_t newblk;
1374 * We will allocate only single block for now.
1380 EXT2_LOCK(ip->i_ump);
1381 *perror = ext2_alloc(ip, lbn, pref, (int)fs->e2fs_bsize, cred, &newblk);
1386 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1387 ext2_update(ip->i_vnode, 1);
1394 ext4_ext_get_blocks(struct inode *ip, e4fs_daddr_t iblk,
1395 unsigned long max_blocks, struct ucred *cred, struct buf **bpp,
1396 int *pallocated, daddr_t *nb)
1398 struct m_ext2fs *fs;
1399 struct buf *bp = NULL;
1400 struct ext4_extent_path *path;
1401 struct ext4_extent newex, *ex;
1402 e4fs_daddr_t bpref, newblk = 0;
1403 unsigned long allocated = 0;
1404 int error = 0, depth;
1412 if ((bpref = ext4_ext_in_cache(ip, iblk, &newex))) {
1413 if (bpref == EXT4_EXT_CACHE_IN) {
1414 /* Block is already allocated. */
1415 newblk = iblk - le32toh(newex.e_blk) +
1416 ext4_ext_extent_pblock(&newex);
1417 allocated = le16toh(newex.e_len) - (iblk - le32toh(newex.e_blk));
1425 error = ext4_ext_find_extent(ip, iblk, &path);
1430 depth = ext4_ext_inode_depth(ip);
1431 if (path[depth].ep_ext == NULL && depth != 0) {
1436 if ((ex = path[depth].ep_ext)) {
1437 uint64_t lblk = le32toh(ex->e_blk);
1438 uint16_t e_len = le16toh(ex->e_len);
1439 e4fs_daddr_t e_start = ext4_ext_extent_pblock(ex);
1441 if (e_len > EXT4_MAX_LEN)
1444 /* If we found extent covers block, simply return it. */
1445 if (iblk >= lblk && iblk < lblk + e_len) {
1446 newblk = iblk - lblk + e_start;
1447 allocated = e_len - (iblk - lblk);
1448 ext4_ext_put_in_cache(ip, lblk, e_len,
1449 e_start, EXT4_EXT_CACHE_IN);
1454 /* Allocate the new block. */
1455 if (S_ISREG(ip->i_mode) && (!ip->i_next_alloc_block)) {
1456 ip->i_next_alloc_goal = 0;
1459 bpref = ext4_ext_blkpref(ip, path, iblk);
1460 allocated = max_blocks;
1461 newblk = ext4_new_blocks(ip, iblk, bpref, cred, &allocated, &error);
1465 /* Try to insert new extent into found leaf and return. */
1466 newex.e_blk = htole32(iblk);
1467 ext4_ext_store_pblock(&newex, newblk);
1468 newex.e_len = htole16(allocated);
1469 error = ext4_ext_insert_extent(ip, path, &newex);
1473 newblk = ext4_ext_extent_pblock(&newex);
1474 ext4_ext_put_in_cache(ip, iblk, allocated, newblk, EXT4_EXT_CACHE_IN);
1478 if (allocated > max_blocks)
1479 allocated = max_blocks;
1484 error = bread(ip->i_devvp, fsbtodb(fs, newblk),
1485 fs->e2fs_bsize, cred, &bp);
1495 ext4_ext_drop_refs(path);
1496 free(path, M_EXT2EXTENTS);
1505 static inline struct ext4_extent_header *
1506 ext4_ext_header(struct inode *ip)
1509 return ((struct ext4_extent_header *)ip->i_db);
1513 ext4_remove_blocks(struct inode *ip, struct ext4_extent *ex,
1514 unsigned long from, unsigned long to)
1516 unsigned long num, start;
1518 if (from >= le32toh(ex->e_blk) &&
1519 to == le32toh(ex->e_blk) + ext4_ext_get_actual_len(ex) - 1) {
1521 num = le32toh(ex->e_blk) + ext4_ext_get_actual_len(ex) - from;
1522 start = ext4_ext_extent_pblock(ex) +
1523 ext4_ext_get_actual_len(ex) - num;
1524 ext4_ext_blkfree(ip, start, num, 0);
1531 ext4_ext_rm_index(struct inode *ip, struct ext4_extent_path *path)
1535 /* Free index block. */
1537 leaf = ext4_ext_index_pblock(path->ep_index);
1538 KASSERT(path->ep_header->eh_ecount != 0,
1539 ("ext4_ext_rm_index: bad ecount"));
1540 path->ep_header->eh_ecount =
1541 htole16(le16toh(path->ep_header->eh_ecount) - 1);
1542 ext4_ext_dirty(ip, path);
1543 ext4_ext_blkfree(ip, leaf, 1, 0);
1548 ext4_ext_rm_leaf(struct inode *ip, struct ext4_extent_path *path,
1551 struct ext4_extent_header *eh;
1552 struct ext4_extent *ex;
1553 unsigned int a, b, block, num;
1554 unsigned long ex_blk;
1555 unsigned short ex_len;
1557 int error, correct_index;
1559 depth = ext4_ext_inode_depth(ip);
1560 if (!path[depth].ep_header) {
1561 if (path[depth].ep_data == NULL)
1563 path[depth].ep_header =
1564 (struct ext4_extent_header* )path[depth].ep_data;
1567 eh = path[depth].ep_header;
1569 SDT_PROBE2(ext2fs, , trace, extents, 1,
1570 "bad header => extent corrupted");
1574 ex = EXT_LAST_EXTENT(eh);
1575 ex_blk = le32toh(ex->e_blk);
1576 ex_len = ext4_ext_get_actual_len(ex);
1580 while (ex >= EXT_FIRST_EXTENT(eh) && ex_blk + ex_len > start) {
1581 path[depth].ep_ext = ex;
1582 a = ex_blk > start ? ex_blk : start;
1583 b = (uint64_t)ex_blk + ex_len - 1 <
1584 EXT4_MAX_BLOCKS ? ex_blk + ex_len - 1 : EXT4_MAX_BLOCKS;
1586 if (a != ex_blk && b != ex_blk + ex_len - 1)
1588 else if (a != ex_blk) {
1589 /* Remove tail of the extent. */
1592 } else if (b != ex_blk + ex_len - 1) {
1593 /* Remove head of the extent, not implemented. */
1596 /* Remove whole extent. */
1601 if (ex == EXT_FIRST_EXTENT(eh))
1604 error = ext4_remove_blocks(ip, ex, a, b);
1609 ext4_ext_store_pblock(ex, 0);
1610 eh->eh_ecount = htole16(le16toh(eh->eh_ecount) - 1);
1613 ex->e_blk = htole32(block);
1614 ex->e_len = htole16(num);
1616 ext4_ext_dirty(ip, path + depth);
1619 ex_blk = htole32(ex->e_blk);
1620 ex_len = ext4_ext_get_actual_len(ex);
1623 if (correct_index && le16toh(eh->eh_ecount))
1624 error = ext4_ext_correct_indexes(ip, path);
1627 * If this leaf is free, we should
1628 * remove it from index block above.
1630 if (error == 0 && eh->eh_ecount == 0 &&
1631 path[depth].ep_data != NULL)
1632 error = ext4_ext_rm_index(ip, path + depth);
1639 ext4_read_extent_tree_block(struct inode *ip, e4fs_daddr_t pblk,
1640 int depth, int flags)
1642 struct m_ext2fs *fs;
1643 struct ext4_extent_header *eh;
1648 error = bread(ip->i_devvp, fsbtodb(fs, pblk),
1649 fs->e2fs_bsize, NOCRED, &bp);
1654 eh = ext4_ext_block_header(bp->b_data);
1655 if (le16toh(eh->eh_depth) != depth) {
1656 SDT_PROBE2(ext2fs, , trace, extents, 1,
1657 "unexpected eh_depth");
1661 error = ext4_ext_check_header(ip, eh, depth);
1674 ext4_ext_more_to_rm(struct ext4_extent_path *path)
1677 KASSERT(path->ep_index != NULL,
1678 ("ext4_ext_more_to_rm: bad index from path"));
1680 if (path->ep_index < EXT_FIRST_INDEX(path->ep_header))
1683 if (le16toh(path->ep_header->eh_ecount) == path->index_count)
1690 ext4_ext_remove_space(struct inode *ip, off_t length, int flags,
1691 struct ucred *cred, struct thread *td)
1694 struct ext4_extent_header *ehp;
1695 struct ext4_extent_path *path;
1699 ehp = (struct ext4_extent_header *)ip->i_db;
1700 depth = ext4_ext_inode_depth(ip);
1702 error = ext4_ext_check_header(ip, ehp, depth);
1706 path = malloc(sizeof(struct ext4_extent_path) * (depth + 1),
1707 M_EXT2EXTENTS, M_WAITOK | M_ZERO);
1708 path[0].ep_header = ehp;
1709 path[0].ep_depth = depth;
1711 while (error == 0 && i >= 0) {
1714 error = ext4_ext_rm_leaf(ip, path, length);
1717 free(path[i].ep_data, M_EXT2EXTENTS);
1718 path[i].ep_data = NULL;
1723 /* This is index. */
1724 if (!path[i].ep_header)
1726 (struct ext4_extent_header *)path[i].ep_data;
1728 if (!path[i].ep_index) {
1729 /* This level hasn't touched yet. */
1730 path[i].ep_index = EXT_LAST_INDEX(path[i].ep_header);
1731 path[i].index_count =
1732 le16toh(path[i].ep_header->eh_ecount) + 1;
1734 /* We've already was here, see at next index. */
1738 if (ext4_ext_more_to_rm(path + i)) {
1739 memset(path + i + 1, 0, sizeof(*path));
1740 bp = ext4_read_extent_tree_block(ip,
1741 ext4_ext_index_pblock(path[i].ep_index),
1742 path[0].ep_depth - (i + 1), 0);
1748 ext4_ext_fill_path_bdata(&path[i+1], bp,
1749 ext4_ext_index_pblock(path[i].ep_index));
1751 path[i].index_count =
1752 le16toh(path[i].ep_header->eh_ecount);
1755 if (path[i].ep_header->eh_ecount == 0 && i > 0) {
1756 /* Index is empty, remove it. */
1757 error = ext4_ext_rm_index(ip, path + i);
1759 free(path[i].ep_data, M_EXT2EXTENTS);
1760 path[i].ep_data = NULL;
1765 if (path->ep_header->eh_ecount == 0) {
1767 * Truncate the tree to zero.
1769 ext4_ext_header(ip)->eh_depth = 0;
1770 ext4_ext_header(ip)->eh_max = htole16(ext4_ext_space_root(ip));
1771 ext4_ext_dirty(ip, path);
1774 ext4_ext_drop_refs(path);
1775 free(path, M_EXT2EXTENTS);
1777 ip->i_ext_cache.ec_type = EXT4_EXT_CACHE_NO;