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
62 static const bool print_extents_walk = true;
64 static int ext4_ext_check_header(struct inode *, struct ext4_extent_header *,
66 static int ext4_ext_walk_header(struct inode *, struct ext4_extent_header *,
68 static inline e4fs_daddr_t ext4_ext_index_pblock(struct ext4_extent_index *);
69 static inline e4fs_daddr_t ext4_ext_extent_pblock(struct ext4_extent *);
72 ext4_ext_blk_check(struct inode *ip, e4fs_daddr_t blk)
78 if (blk < fs->e2fs->e2fs_first_dblock || blk >= fs->e2fs_bcount)
85 ext4_ext_walk_index(struct inode *ip, struct ext4_extent_index *ex, int depth,
95 if (print_extents_walk)
96 printf(" index %p => (blk %u pblk %ju)\n", ex,
98 (uint64_t)le16toh(ex->ei_leaf_hi) << 32 |
99 le32toh(ex->ei_leaf_lo));
104 blk = ext4_ext_index_pblock(ex);
105 error = ext4_ext_blk_check(ip, blk);
109 if ((error = bread(ip->i_devvp,
110 fsbtodb(fs, blk), (int)fs->e2fs_bsize, NOCRED, &bp)) != 0) {
115 error = ext4_ext_walk_header(ip,
116 (struct ext4_extent_header *)bp->b_data, depth);
124 ext4_ext_walk_extent(struct inode *ip, struct ext4_extent *ep)
129 blk = ext4_ext_extent_pblock(ep);
130 error = ext4_ext_blk_check(ip, blk);
134 if (print_extents_walk)
135 printf(" ext %p => (blk %u len %u start %ju)\n",
136 ep, le32toh(ep->e_blk), le16toh(ep->e_len),
143 ext4_ext_walk_header(struct inode *ip, struct ext4_extent_header *eh, int depth)
147 error = ext4_ext_check_header(ip, eh, depth);
151 if (print_extents_walk)
152 printf("header %p => (entries %d max %d depth %d gen %d)\n",
153 eh, le16toh(eh->eh_ecount),
154 le16toh(eh->eh_max), le16toh(eh->eh_depth),
155 le32toh(eh->eh_gen));
157 for (i = 0; i < le16toh(eh->eh_ecount) && error == 0; i++)
158 if (eh->eh_depth != 0)
159 error = ext4_ext_walk_index(ip,
160 (struct ext4_extent_index *)(eh + 1 + i), depth - 1,
163 error = ext4_ext_walk_extent(ip,
164 (struct ext4_extent *)(eh + 1 + i));
170 ext4_ext_walk(struct inode *ip)
172 struct ext4_extent_header *ehp;
174 ehp = (struct ext4_extent_header *)ip->i_db;
176 if (print_extents_walk)
177 printf("Extent status:ip=%ju\n", ip->i_number);
179 if (!(ip->i_flag & IN_E4EXTENTS))
182 return (ext4_ext_walk_header(ip, ehp, 0));
186 ext4_ext_print_path(struct inode *ip, struct ext4_extent_path *path)
188 int k, depth, error = 0;
190 depth = path->ep_depth;
192 if (print_extents_walk)
193 printf("ip=%ju, Path:\n", ip->i_number);
195 for (k = 0; k <= depth && error == 0; k++, path++) {
196 if (path->ep_index) {
197 error = ext4_ext_walk_index(ip, path->ep_index,
199 } else if (path->ep_ext) {
200 error = ext4_ext_walk_extent(ip, path->ep_ext);
208 static inline struct ext4_extent_header *
209 ext4_ext_inode_header(struct inode *ip)
212 return ((struct ext4_extent_header *)ip->i_db);
215 static inline struct ext4_extent_header *
216 ext4_ext_block_header(char *bdata)
219 return ((struct ext4_extent_header *)bdata);
222 static inline unsigned short
223 ext4_ext_inode_depth(struct inode *ip)
225 struct ext4_extent_header *ehp;
227 ehp = (struct ext4_extent_header *)ip->i_data;
228 return (le16toh(ehp->eh_depth));
231 static inline e4fs_daddr_t
232 ext4_ext_index_pblock(struct ext4_extent_index *index)
236 blk = le32toh(index->ei_leaf_lo);
237 blk |= (e4fs_daddr_t)le16toh(index->ei_leaf_hi) << 32;
243 ext4_index_store_pblock(struct ext4_extent_index *index, e4fs_daddr_t pb)
246 index->ei_leaf_lo = htole32(pb & 0xffffffff);
247 index->ei_leaf_hi = htole16((pb >> 32) & 0xffff);
250 static inline e4fs_daddr_t
251 ext4_ext_extent_pblock(struct ext4_extent *extent)
255 blk = le32toh(extent->e_start_lo);
256 blk |= (e4fs_daddr_t)le16toh(extent->e_start_hi) << 32;
262 ext4_ext_store_pblock(struct ext4_extent *ex, e4fs_daddr_t pb)
265 ex->e_start_lo = htole32(pb & 0xffffffff);
266 ex->e_start_hi = htole16((pb >> 32) & 0xffff);
270 ext4_ext_in_cache(struct inode *ip, daddr_t lbn, struct ext4_extent *ep)
272 struct ext4_extent_cache *ecp;
273 int ret = EXT4_EXT_CACHE_NO;
275 ecp = &ip->i_ext_cache;
276 if (ecp->ec_type == EXT4_EXT_CACHE_NO)
279 if (lbn >= ecp->ec_blk && lbn < ecp->ec_blk + ecp->ec_len) {
280 ep->e_blk = htole32(ecp->ec_blk);
281 ep->e_start_lo = htole32(ecp->ec_start & 0xffffffff);
282 ep->e_start_hi = htole16(ecp->ec_start >> 32 & 0xffff);
283 ep->e_len = htole16(ecp->ec_len);
290 ext4_ext_space_root(struct inode *ip)
294 size = sizeof(ip->i_data);
295 size -= sizeof(struct ext4_extent_header);
296 size /= sizeof(struct ext4_extent);
302 ext4_ext_space_block(struct inode *ip)
309 size = (fs->e2fs_bsize - sizeof(struct ext4_extent_header)) /
310 sizeof(struct ext4_extent);
316 ext4_ext_space_root_idx(struct inode *ip)
320 size = sizeof(ip->i_data);
321 size -= sizeof(struct ext4_extent_header);
322 size /= sizeof(struct ext4_extent_index);
328 ext4_ext_space_block_idx(struct inode *ip)
335 size = (fs->e2fs_bsize - sizeof(struct ext4_extent_header)) /
336 sizeof(struct ext4_extent_index);
342 ext4_ext_max_entries(struct inode *ip, int depth)
345 if (depth == ext4_ext_inode_depth(ip)) {
347 return (ext4_ext_space_root(ip));
349 return (ext4_ext_space_root_idx(ip));
352 return (ext4_ext_space_block(ip));
354 return (ext4_ext_space_block_idx(ip));
358 static inline uint16_t
359 ext4_ext_get_actual_len(struct ext4_extent *ext)
362 return (le16toh(ext->e_len) <= EXT_INIT_MAX_LEN ?
363 le16toh(ext->e_len) : (le16toh(ext->e_len) - EXT_INIT_MAX_LEN));
368 ext4_inode_block_validate(struct inode *ip, e4fs_daddr_t start_blk,
375 if ((start_blk <= le32toh(fs->e2fs->e2fs_first_dblock)) ||
376 (start_blk + count < start_blk) ||
377 (start_blk + count > fs->e2fs_bcount))
384 ext4_validate_extent(struct inode *ip, struct ext4_extent *ext)
386 e4fs_daddr_t blk = ext4_ext_extent_pblock(ext);
387 uint32_t lblk = le32toh(ext->e_blk);
388 int len = ext4_ext_get_actual_len(ext);
390 if (lblk + len <= lblk)
393 return (ext4_inode_block_validate(ip, blk, len));
397 ext4_validate_extent_idx(struct inode *ip, struct ext4_extent_index *ext_idx)
399 e4fs_daddr_t blk = ext4_ext_index_pblock(ext_idx);
401 return (ext4_inode_block_validate(ip, blk, 1));
405 ext4_validate_extent_entries(struct inode *ip, struct ext4_extent_header *eh,
410 count = le16toh(eh->eh_ecount);
415 struct ext4_extent *ext = EXT_FIRST_EXTENT(eh);
421 if (ext4_validate_extent(ip, ext))
424 /* Check for overlapping extents */
425 lblk = le32toh(ext->e_blk);
426 len = ext4_ext_get_actual_len(ext);
427 if ((lblk <= prev) && prev)
432 prev = lblk + len - 1;
435 struct ext4_extent_index *ext_idx = EXT_FIRST_INDEX(eh);
437 if (ext4_validate_extent_idx(ip, ext_idx))
449 ext4_ext_check_header(struct inode *ip, struct ext4_extent_header *eh,
455 char *error_msg __unused;
458 if (le16toh(eh->eh_magic) != EXT4_EXT_MAGIC) {
459 error_msg = "header: invalid magic";
462 if (le16toh(eh->eh_depth) != depth ||
463 le16toh(eh->eh_depth) > EXT4_EXT_DEPTH_MAX)
465 error_msg = "header: invalid eh_depth";
468 if (eh->eh_max == 0) {
469 error_msg = "header: invalid eh_max";
472 if (le16toh(eh->eh_max) > ext4_ext_max_entries(ip, depth)) {
473 error_msg = "header: too large eh_max";
476 if (le16toh(eh->eh_ecount) > le16toh(eh->eh_max)) {
477 error_msg = "header: invalid eh_entries";
480 if (le16toh(eh->eh_depth) > EXT4_EXT_DEPTH_MAX) {
481 error_msg = "header: invalid eh_depth";
484 if (ext4_validate_extent_entries(ip, eh, depth)) {
485 error_msg = "header: invalid extent entries";
492 SDT_PROBE2(ext2fs, , trace, extents, 1, error_msg);
497 ext4_ext_binsearch_index(struct ext4_extent_path *path, int blk)
499 struct ext4_extent_header *eh;
500 struct ext4_extent_index *r, *l, *m;
502 eh = path->ep_header;
504 KASSERT(le16toh(eh->eh_ecount) <= le16toh(eh->eh_max) &&
505 le16toh(eh->eh_ecount) > 0,
506 ("ext4_ext_binsearch_index: bad args"));
508 l = EXT_FIRST_INDEX(eh) + 1;
509 r = EXT_FIRST_INDEX(eh) + le16toh(eh->eh_ecount) - 1;
512 if (blk < le32toh(m->ei_blk))
518 path->ep_index = l - 1;
522 ext4_ext_binsearch_ext(struct ext4_extent_path *path, int blk)
524 struct ext4_extent_header *eh;
525 struct ext4_extent *r, *l, *m;
527 eh = path->ep_header;
529 KASSERT(le16toh(eh->eh_ecount) <= le16toh(eh->eh_max),
530 ("ext4_ext_binsearch_ext: bad args"));
532 if (eh->eh_ecount == 0)
535 l = EXT_FIRST_EXTENT(eh) + 1;
536 r = EXT_FIRST_EXTENT(eh) + le16toh(eh->eh_ecount) - 1;
540 if (blk < le32toh(m->e_blk))
546 path->ep_ext = l - 1;
550 ext4_ext_fill_path_bdata(struct ext4_extent_path *path,
551 struct buf *bp, uint64_t blk)
554 KASSERT(path->ep_data == NULL,
555 ("ext4_ext_fill_path_bdata: bad ep_data"));
557 path->ep_data = malloc(bp->b_bufsize, M_EXT2EXTENTS, M_WAITOK);
558 memcpy(path->ep_data, bp->b_data, bp->b_bufsize);
565 ext4_ext_fill_path_buf(struct ext4_extent_path *path, struct buf *bp)
568 KASSERT(path->ep_data != NULL,
569 ("ext4_ext_fill_path_buf: bad ep_data"));
571 memcpy(bp->b_data, path->ep_data, bp->b_bufsize);
575 ext4_ext_drop_refs(struct ext4_extent_path *path)
582 depth = path->ep_depth;
583 for (i = 0; i <= depth; i++, path++)
585 free(path->ep_data, M_EXT2EXTENTS);
586 path->ep_data = NULL;
591 ext4_ext_path_free(struct ext4_extent_path *path)
597 ext4_ext_drop_refs(path);
598 free(path, M_EXT2EXTENTS);
602 ext4_ext_find_extent(struct inode *ip, daddr_t block,
603 struct ext4_extent_path **ppath)
605 struct ext4_extent_header *eh;
606 struct ext4_extent_path *path;
609 int error, depth, i, ppos, alloc;
611 eh = ext4_ext_inode_header(ip);
612 depth = ext4_ext_inode_depth(ip);
616 error = ext4_ext_check_header(ip, eh, depth);
625 path = malloc(EXT4_EXT_DEPTH_MAX *
626 sizeof(struct ext4_extent_path),
627 M_EXT2EXTENTS, M_WAITOK | M_ZERO);
632 path[0].ep_header = eh;
633 path[0].ep_data = NULL;
635 /* Walk through the tree. */
638 ext4_ext_binsearch_index(&path[ppos], block);
639 blk = ext4_ext_index_pblock(path[ppos].ep_index);
640 path[ppos].ep_depth = i;
641 path[ppos].ep_ext = NULL;
643 error = bread(ip->i_devvp, fsbtodb(ip->i_e2fs, blk),
644 ip->i_e2fs->e2fs_bsize, NOCRED, &bp);
651 SDT_PROBE2(ext2fs, , trace, extents, 1,
652 "ppos > depth => extent corrupted");
658 ext4_ext_fill_path_bdata(&path[ppos], bp, blk);
661 eh = ext4_ext_block_header(path[ppos].ep_data);
662 if (ext4_ext_check_header(ip, eh, i - 1) ||
663 ext2_extent_blk_csum_verify(ip, path[ppos].ep_data)) {
668 path[ppos].ep_header = eh;
673 error = ext4_ext_check_header(ip, eh, 0);
678 path[ppos].ep_depth = i;
679 path[ppos].ep_header = eh;
680 path[ppos].ep_ext = NULL;
681 path[ppos].ep_index = NULL;
682 ext4_ext_binsearch_ext(&path[ppos], block);
686 ext4_ext_drop_refs(path);
688 free(path, M_EXT2EXTENTS);
696 ext4_ext_space_block_index(struct inode *ip)
703 size = (fs->e2fs_bsize - sizeof(struct ext4_extent_header)) /
704 sizeof(struct ext4_extent_index);
710 ext4_ext_tree_init(struct inode *ip)
712 struct ext4_extent_header *ehp;
714 ip->i_flag |= IN_E4EXTENTS;
716 memset(ip->i_data, 0, EXT2_NDADDR + EXT2_NIADDR);
717 ehp = (struct ext4_extent_header *)ip->i_data;
718 ehp->eh_magic = htole16(EXT4_EXT_MAGIC);
719 ehp->eh_max = htole16(ext4_ext_space_root(ip));
720 ip->i_ext_cache.ec_type = EXT4_EXT_CACHE_NO;
721 ip->i_flag |= IN_CHANGE | IN_UPDATE;
722 ext2_update(ip->i_vnode, 1);
726 ext4_ext_put_in_cache(struct inode *ip, uint32_t blk,
727 uint32_t len, uint32_t start, int type)
730 KASSERT(len != 0, ("ext4_ext_put_in_cache: bad input"));
732 ip->i_ext_cache.ec_type = type;
733 ip->i_ext_cache.ec_blk = blk;
734 ip->i_ext_cache.ec_len = len;
735 ip->i_ext_cache.ec_start = start;
739 ext4_ext_blkpref(struct inode *ip, struct ext4_extent_path *path,
743 struct ext4_extent *ex;
744 e4fs_daddr_t bg_start;
750 depth = path->ep_depth;
751 ex = path[depth].ep_ext;
753 e4fs_daddr_t pblk = ext4_ext_extent_pblock(ex);
754 e2fs_daddr_t blk = le32toh(ex->e_blk);
757 return (pblk + (block - blk));
759 return (pblk - (blk - block));
762 /* Try to get block from index itself. */
763 if (path[depth].ep_data)
764 return (path[depth].ep_blk);
767 /* Use inode's group. */
768 bg_start = (ip->i_block_group * EXT2_BLOCKS_PER_GROUP(ip->i_e2fs)) +
769 le32toh(fs->e2fs->e2fs_first_dblock);
771 return (bg_start + block);
775 ext4_can_extents_be_merged(struct ext4_extent *ex1,
776 struct ext4_extent *ex2)
779 if (le32toh(ex1->e_blk) + le16toh(ex1->e_len) != le32toh(ex2->e_blk))
782 if (le16toh(ex1->e_len) + le16toh(ex2->e_len) > EXT4_MAX_LEN)
785 if (ext4_ext_extent_pblock(ex1) + le16toh(ex1->e_len) ==
786 ext4_ext_extent_pblock(ex2))
793 ext4_ext_next_leaf_block(struct inode *ip, struct ext4_extent_path *path)
795 int depth = path->ep_depth;
799 return (EXT4_MAX_BLOCKS);
805 if (path[depth].ep_index !=
806 EXT_LAST_INDEX(path[depth].ep_header))
807 return (le32toh(path[depth].ep_index[1].ei_blk));
812 return (EXT4_MAX_BLOCKS);
816 ext4_ext_dirty(struct inode *ip, struct ext4_extent_path *path)
830 bp = getblk(ip->i_devvp, fsbtodb(fs, blk),
831 fs->e2fs_bsize, 0, 0, 0);
834 ext4_ext_fill_path_buf(path, bp);
835 ext2_extent_blk_csum_set(ip, bp->b_data);
838 ip->i_flag |= IN_CHANGE | IN_UPDATE;
839 error = ext2_update(ip->i_vnode, 1);
846 ext4_ext_insert_index(struct inode *ip, struct ext4_extent_path *path,
847 uint32_t lblk, e4fs_daddr_t blk)
849 struct ext4_extent_index *idx;
852 if (lblk == le32toh(path->ep_index->ei_blk)) {
853 SDT_PROBE2(ext2fs, , trace, extents, 1,
854 "lblk == index blk => extent corrupted");
858 if (le16toh(path->ep_header->eh_ecount) >=
859 le16toh(path->ep_header->eh_max)) {
860 SDT_PROBE2(ext2fs, , trace, extents, 1,
861 "ecout > maxcount => extent corrupted");
865 if (lblk > le32toh(path->ep_index->ei_blk)) {
867 idx = path->ep_index + 1;
870 idx = path->ep_index;
873 len = EXT_LAST_INDEX(path->ep_header) - idx + 1;
875 memmove(idx + 1, idx, len * sizeof(struct ext4_extent_index));
877 if (idx > EXT_MAX_INDEX(path->ep_header)) {
878 SDT_PROBE2(ext2fs, , trace, extents, 1,
879 "index is out of range => extent corrupted");
883 idx->ei_blk = htole32(lblk);
884 ext4_index_store_pblock(idx, blk);
885 path->ep_header->eh_ecount =
886 htole16(le16toh(path->ep_header->eh_ecount) + 1);
888 return (ext4_ext_dirty(ip, path));
892 ext4_ext_alloc_meta(struct inode *ip)
894 e4fs_daddr_t blk = ext2_alloc_meta(ip);
896 ip->i_blocks += btodb(ip->i_e2fs->e2fs_bsize);
897 ip->i_flag |= IN_CHANGE | IN_UPDATE;
898 ext2_update(ip->i_vnode, 1);
905 ext4_ext_blkfree(struct inode *ip, uint64_t blk, int count, int flags)
908 int i, blocksreleased;
911 blocksreleased = count;
913 for(i = 0; i < count; i++)
914 ext2_blkfree(ip, blk + i, fs->e2fs_bsize);
916 if (ip->i_blocks >= blocksreleased)
917 ip->i_blocks -= (btodb(fs->e2fs_bsize)*blocksreleased);
921 ip->i_flag |= IN_CHANGE | IN_UPDATE;
922 ext2_update(ip->i_vnode, 1);
926 ext4_ext_split(struct inode *ip, struct ext4_extent_path *path,
927 struct ext4_extent *newext, int at)
931 int depth = ext4_ext_inode_depth(ip);
932 struct ext4_extent_header *neh;
933 struct ext4_extent_index *fidx;
934 struct ext4_extent *ex;
936 e4fs_daddr_t newblk, oldblk;
938 e4fs_daddr_t *ablks = NULL;
945 * We will split at current extent for now.
947 if (path[depth].ep_ext > EXT_MAX_EXTENT(path[depth].ep_header)) {
948 SDT_PROBE2(ext2fs, , trace, extents, 1,
949 "extent is out of range => extent corrupted");
953 if (path[depth].ep_ext != EXT_MAX_EXTENT(path[depth].ep_header))
954 border = le32toh(path[depth].ep_ext[1].e_blk);
956 border = le32toh(newext->e_blk);
958 /* Allocate new blocks. */
959 ablks = malloc(sizeof(e4fs_daddr_t) * depth,
960 M_EXT2EXTENTS, M_WAITOK | M_ZERO);
961 for (a = 0; a < depth - at; a++) {
962 newblk = ext4_ext_alloc_meta(ip);
969 bp = getblk(ip->i_devvp, fsbtodb(fs, newblk), fs->e2fs_bsize, 0, 0, 0);
975 neh = ext4_ext_block_header(bp->b_data);
977 neh->eh_max = le16toh(ext4_ext_space_block(ip));
978 neh->eh_magic = le16toh(EXT4_EXT_MAGIC);
980 ex = EXT_FIRST_EXTENT(neh);
982 if (le16toh(path[depth].ep_header->eh_ecount) !=
983 le16toh(path[depth].ep_header->eh_max)) {
984 SDT_PROBE2(ext2fs, , trace, extents, 1,
985 "extents count out of range => extent corrupted");
990 /* Start copy from next extent. */
992 path[depth].ep_ext++;
993 while (path[depth].ep_ext <= EXT_MAX_EXTENT(path[depth].ep_header)) {
994 path[depth].ep_ext++;
998 memmove(ex, path[depth].ep_ext - m,
999 sizeof(struct ext4_extent) * m);
1000 neh->eh_ecount = htole16(le16toh(neh->eh_ecount) + m);
1003 ext2_extent_blk_csum_set(ip, bp->b_data);
1009 path[depth].ep_header->eh_ecount =
1010 htole16(le16toh(path[depth].ep_header->eh_ecount) - m);
1011 ext4_ext_dirty(ip, path + depth);
1014 /* Create intermediate indexes. */
1016 KASSERT(k >= 0, ("ext4_ext_split: negative k"));
1018 /* Insert new index into current index block. */
1022 newblk = ablks[--a];
1023 error = bread(ip->i_devvp, fsbtodb(fs, newblk),
1024 (int)fs->e2fs_bsize, NOCRED, &bp);
1029 neh = (struct ext4_extent_header *)bp->b_data;
1030 neh->eh_ecount = htole16(1);
1031 neh->eh_magic = htole16(EXT4_EXT_MAGIC);
1032 neh->eh_max = htole16(ext4_ext_space_block_index(ip));
1033 neh->eh_depth = htole16(depth - i);
1034 fidx = EXT_FIRST_INDEX(neh);
1035 fidx->ei_blk = htole32(border);
1036 ext4_index_store_pblock(fidx, oldblk);
1040 while (path[i].ep_index <= EXT_MAX_INDEX(path[i].ep_header)) {
1045 memmove(++fidx, path[i].ep_index - m,
1046 sizeof(struct ext4_extent_index) * m);
1047 neh->eh_ecount = htole16(le16toh(neh->eh_ecount) + m);
1050 ext2_extent_blk_csum_set(ip, bp->b_data);
1054 /* Fix old index. */
1056 path[i].ep_header->eh_ecount =
1057 htole16(le16toh(path[i].ep_header->eh_ecount) - m);
1058 ext4_ext_dirty(ip, path + i);
1064 error = ext4_ext_insert_index(ip, path + at, border, newblk);
1071 for (i = 0; i < depth; i++) {
1074 ext4_ext_blkfree(ip, ablks[i], 1, 0);
1078 free(ablks, M_EXT2EXTENTS);
1084 ext4_ext_grow_indepth(struct inode *ip, struct ext4_extent_path *path,
1085 struct ext4_extent *newext)
1087 struct m_ext2fs *fs;
1088 struct ext4_extent_path *curpath;
1089 struct ext4_extent_header *neh;
1091 e4fs_daddr_t newblk;
1097 newblk = ext4_ext_alloc_meta(ip);
1101 bp = getblk(ip->i_devvp, fsbtodb(fs, newblk), fs->e2fs_bsize, 0, 0, 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);
1124 curpath->ep_header->eh_magic = htole16(EXT4_EXT_MAGIC);
1125 curpath->ep_header->eh_max = htole16(ext4_ext_space_root(ip));
1126 curpath->ep_header->eh_ecount = htole16(1);
1127 curpath->ep_index = EXT_FIRST_INDEX(curpath->ep_header);
1128 curpath->ep_index->ei_blk = EXT_FIRST_EXTENT(path[0].ep_header)->e_blk;
1129 ext4_index_store_pblock(curpath->ep_index, newblk);
1131 neh = ext4_ext_inode_header(ip);
1132 neh->eh_depth = htole16(path->ep_depth + 1);
1133 ext4_ext_dirty(ip, curpath);
1141 ext4_ext_create_new_leaf(struct inode *ip, struct ext4_extent_path *path,
1142 struct ext4_extent *newext)
1144 struct ext4_extent_path *curpath;
1145 int depth, i, error;
1148 i = depth = ext4_ext_inode_depth(ip);
1150 /* Look for free index entry int the tree */
1151 curpath = path + depth;
1152 while (i > 0 && !EXT_HAS_FREE_INDEX(curpath)) {
1158 * We use already allocated block for index block,
1159 * so subsequent data blocks should be contiguous.
1161 if (EXT_HAS_FREE_INDEX(curpath)) {
1162 error = ext4_ext_split(ip, path, newext, i);
1167 ext4_ext_drop_refs(path);
1168 error = ext4_ext_find_extent(ip, le32toh(newext->e_blk), &path);
1172 /* Tree is full, do grow in depth. */
1173 error = ext4_ext_grow_indepth(ip, path, newext);
1178 ext4_ext_drop_refs(path);
1179 error = ext4_ext_find_extent(ip, le32toh(newext->e_blk), &path);
1183 /* Check and split tree if required. */
1184 depth = ext4_ext_inode_depth(ip);
1185 if (le16toh(path[depth].ep_header->eh_ecount) ==
1186 le16toh(path[depth].ep_header->eh_max))
1195 ext4_ext_correct_indexes(struct inode *ip, struct ext4_extent_path *path)
1197 struct ext4_extent_header *eh;
1198 struct ext4_extent *ex;
1202 depth = ext4_ext_inode_depth(ip);
1203 eh = path[depth].ep_header;
1204 ex = path[depth].ep_ext;
1206 if (ex == NULL || eh == NULL)
1212 /* We will correct tree if first leaf got modified only. */
1213 if (ex != EXT_FIRST_EXTENT(eh))
1217 border = le32toh(path[depth].ep_ext->e_blk);
1218 path[k].ep_index->ei_blk = htole32(border);
1219 ext4_ext_dirty(ip, path + k);
1221 /* Change all left-side indexes. */
1222 if (path[k+1].ep_index != EXT_FIRST_INDEX(path[k+1].ep_header))
1225 path[k].ep_index->ei_blk = htole32(border);
1226 ext4_ext_dirty(ip, path + k);
1233 ext4_ext_insert_extent(struct inode *ip, struct ext4_extent_path *path,
1234 struct ext4_extent *newext)
1236 struct ext4_extent_header * eh;
1237 struct ext4_extent *ex, *nex, *nearex;
1238 struct ext4_extent_path *npath;
1239 int depth, len, error, next;
1241 depth = ext4_ext_inode_depth(ip);
1242 ex = path[depth].ep_ext;
1245 if (htole16(newext->e_len) == 0 || path[depth].ep_header == NULL)
1248 /* Insert block into found extent. */
1249 if (ex && ext4_can_extents_be_merged(ex, newext)) {
1250 ex->e_len = htole16(le16toh(ex->e_len) + le16toh(newext->e_len));
1251 eh = path[depth].ep_header;
1257 depth = ext4_ext_inode_depth(ip);
1258 eh = path[depth].ep_header;
1259 if (le16toh(eh->eh_ecount) < le16toh(eh->eh_max))
1263 nex = EXT_LAST_EXTENT(eh);
1264 next = ext4_ext_next_leaf_block(ip, path);
1265 if (le32toh(newext->e_blk) > le32toh(nex->e_blk) && next !=
1267 KASSERT(npath == NULL,
1268 ("ext4_ext_insert_extent: bad path"));
1270 error = ext4_ext_find_extent(ip, next, &npath);
1274 if (npath->ep_depth != path->ep_depth) {
1279 eh = npath[depth].ep_header;
1280 if (le16toh(eh->eh_ecount) < le16toh(eh->eh_max)) {
1287 * There is no free space in the found leaf,
1288 * try to add a new leaf to the tree.
1290 error = ext4_ext_create_new_leaf(ip, path, newext);
1294 depth = ext4_ext_inode_depth(ip);
1295 eh = path[depth].ep_header;
1298 nearex = path[depth].ep_ext;
1300 /* Create new extent in the leaf. */
1301 path[depth].ep_ext = EXT_FIRST_EXTENT(eh);
1302 } else if (le32toh(newext->e_blk) > le32toh(nearex->e_blk)) {
1303 if (nearex != EXT_LAST_EXTENT(eh)) {
1304 len = EXT_MAX_EXTENT(eh) - nearex;
1305 len = (len - 1) * sizeof(struct ext4_extent);
1306 len = len < 0 ? 0 : len;
1307 memmove(nearex + 2, nearex + 1, len);
1309 path[depth].ep_ext = nearex + 1;
1311 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1312 len = len < 0 ? 0 : len;
1313 memmove(nearex + 1, nearex, len);
1314 path[depth].ep_ext = nearex;
1317 eh->eh_ecount = htole16(le16toh(eh->eh_ecount) + 1);
1318 nearex = path[depth].ep_ext;
1319 nearex->e_blk = newext->e_blk;
1320 nearex->e_start_lo = newext->e_start_lo;
1321 nearex->e_start_hi = newext->e_start_hi;
1322 nearex->e_len = newext->e_len;
1325 /* Try to merge extents to the right. */
1326 while (nearex < EXT_LAST_EXTENT(eh)) {
1327 if (!ext4_can_extents_be_merged(nearex, nearex + 1))
1330 /* Merge with next extent. */
1331 nearex->e_len = htole16(le16toh(nearex->e_len) +
1332 le16toh(nearex[1].e_len));
1333 if (nearex + 1 < EXT_LAST_EXTENT(eh)) {
1334 len = (EXT_LAST_EXTENT(eh) - nearex - 1) *
1335 sizeof(struct ext4_extent);
1336 memmove(nearex + 1, nearex + 2, len);
1339 eh->eh_ecount = htole16(le16toh(eh->eh_ecount) - 1);
1340 KASSERT(le16toh(eh->eh_ecount) != 0,
1341 ("ext4_ext_insert_extent: bad ecount"));
1345 * Try to merge extents to the left,
1346 * start from inexes correction.
1348 error = ext4_ext_correct_indexes(ip, path);
1352 ext4_ext_dirty(ip, path + depth);
1356 ext4_ext_drop_refs(npath);
1357 free(npath, M_EXT2EXTENTS);
1360 ip->i_ext_cache.ec_type = EXT4_EXT_CACHE_NO;
1365 ext4_new_blocks(struct inode *ip, daddr_t lbn, e4fs_daddr_t pref,
1366 struct ucred *cred, unsigned long *count, int *perror)
1368 struct m_ext2fs *fs;
1369 e4fs_daddr_t newblk;
1372 * We will allocate only single block for now.
1378 EXT2_LOCK(ip->i_ump);
1379 *perror = ext2_alloc(ip, lbn, pref, (int)fs->e2fs_bsize, cred, &newblk);
1384 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1385 ext2_update(ip->i_vnode, 1);
1392 ext4_ext_get_blocks(struct inode *ip, e4fs_daddr_t iblk,
1393 unsigned long max_blocks, struct ucred *cred, struct buf **bpp,
1394 int *pallocated, daddr_t *nb)
1396 struct m_ext2fs *fs;
1397 struct buf *bp = NULL;
1398 struct ext4_extent_path *path;
1399 struct ext4_extent newex, *ex;
1400 e4fs_daddr_t bpref, newblk = 0;
1401 unsigned long allocated = 0;
1402 int error = 0, depth;
1410 if ((bpref = ext4_ext_in_cache(ip, iblk, &newex))) {
1411 if (bpref == EXT4_EXT_CACHE_IN) {
1412 /* Block is already allocated. */
1413 newblk = iblk - le32toh(newex.e_blk) +
1414 ext4_ext_extent_pblock(&newex);
1415 allocated = le16toh(newex.e_len) - (iblk - le32toh(newex.e_blk));
1423 error = ext4_ext_find_extent(ip, iblk, &path);
1428 depth = ext4_ext_inode_depth(ip);
1429 if (path[depth].ep_ext == NULL && depth != 0) {
1434 if ((ex = path[depth].ep_ext)) {
1435 uint64_t lblk = le32toh(ex->e_blk);
1436 uint16_t e_len = le16toh(ex->e_len);
1437 e4fs_daddr_t e_start = ext4_ext_extent_pblock(ex);
1439 if (e_len > EXT4_MAX_LEN)
1442 /* If we found extent covers block, simply return it. */
1443 if (iblk >= lblk && iblk < lblk + e_len) {
1444 newblk = iblk - lblk + e_start;
1445 allocated = e_len - (iblk - lblk);
1446 ext4_ext_put_in_cache(ip, lblk, e_len,
1447 e_start, EXT4_EXT_CACHE_IN);
1452 /* Allocate the new block. */
1453 if (S_ISREG(ip->i_mode) && (!ip->i_next_alloc_block)) {
1454 ip->i_next_alloc_goal = 0;
1457 bpref = ext4_ext_blkpref(ip, path, iblk);
1458 allocated = max_blocks;
1459 newblk = ext4_new_blocks(ip, iblk, bpref, cred, &allocated, &error);
1463 /* Try to insert new extent into found leaf and return. */
1464 newex.e_blk = htole32(iblk);
1465 ext4_ext_store_pblock(&newex, newblk);
1466 newex.e_len = htole16(allocated);
1467 error = ext4_ext_insert_extent(ip, path, &newex);
1471 newblk = ext4_ext_extent_pblock(&newex);
1472 ext4_ext_put_in_cache(ip, iblk, allocated, newblk, EXT4_EXT_CACHE_IN);
1476 if (allocated > max_blocks)
1477 allocated = max_blocks;
1482 error = bread(ip->i_devvp, fsbtodb(fs, newblk),
1483 fs->e2fs_bsize, cred, &bp);
1493 ext4_ext_drop_refs(path);
1494 free(path, M_EXT2EXTENTS);
1503 static inline struct ext4_extent_header *
1504 ext4_ext_header(struct inode *ip)
1507 return ((struct ext4_extent_header *)ip->i_db);
1511 ext4_remove_blocks(struct inode *ip, struct ext4_extent *ex,
1512 unsigned long from, unsigned long to)
1514 unsigned long num, start;
1516 if (from >= le32toh(ex->e_blk) &&
1517 to == le32toh(ex->e_blk) + ext4_ext_get_actual_len(ex) - 1) {
1519 num = le32toh(ex->e_blk) + ext4_ext_get_actual_len(ex) - from;
1520 start = ext4_ext_extent_pblock(ex) +
1521 ext4_ext_get_actual_len(ex) - num;
1522 ext4_ext_blkfree(ip, start, num, 0);
1529 ext4_ext_rm_index(struct inode *ip, struct ext4_extent_path *path)
1533 /* Free index block. */
1535 leaf = ext4_ext_index_pblock(path->ep_index);
1536 KASSERT(path->ep_header->eh_ecount != 0,
1537 ("ext4_ext_rm_index: bad ecount"));
1538 path->ep_header->eh_ecount =
1539 htole16(le16toh(path->ep_header->eh_ecount) - 1);
1540 ext4_ext_dirty(ip, path);
1541 ext4_ext_blkfree(ip, leaf, 1, 0);
1546 ext4_ext_rm_leaf(struct inode *ip, struct ext4_extent_path *path,
1549 struct ext4_extent_header *eh;
1550 struct ext4_extent *ex;
1551 unsigned int a, b, block, num;
1552 unsigned long ex_blk;
1553 unsigned short ex_len;
1555 int error, correct_index;
1557 depth = ext4_ext_inode_depth(ip);
1558 if (!path[depth].ep_header) {
1559 if (path[depth].ep_data == NULL)
1561 path[depth].ep_header =
1562 (struct ext4_extent_header* )path[depth].ep_data;
1565 eh = path[depth].ep_header;
1567 SDT_PROBE2(ext2fs, , trace, extents, 1,
1568 "bad header => extent corrupted");
1572 ex = EXT_LAST_EXTENT(eh);
1573 ex_blk = le32toh(ex->e_blk);
1574 ex_len = ext4_ext_get_actual_len(ex);
1578 while (ex >= EXT_FIRST_EXTENT(eh) && ex_blk + ex_len > start) {
1579 path[depth].ep_ext = ex;
1580 a = ex_blk > start ? ex_blk : start;
1581 b = (uint64_t)ex_blk + ex_len - 1 <
1582 EXT4_MAX_BLOCKS ? ex_blk + ex_len - 1 : EXT4_MAX_BLOCKS;
1584 if (a != ex_blk && b != ex_blk + ex_len - 1)
1586 else if (a != ex_blk) {
1587 /* Remove tail of the extent. */
1590 } else if (b != ex_blk + ex_len - 1) {
1591 /* Remove head of the extent, not implemented. */
1594 /* Remove whole extent. */
1599 if (ex == EXT_FIRST_EXTENT(eh))
1602 error = ext4_remove_blocks(ip, ex, a, b);
1607 ext4_ext_store_pblock(ex, 0);
1608 eh->eh_ecount = htole16(le16toh(eh->eh_ecount) - 1);
1611 ex->e_blk = htole32(block);
1612 ex->e_len = htole16(num);
1614 ext4_ext_dirty(ip, path + depth);
1617 ex_blk = htole32(ex->e_blk);
1618 ex_len = ext4_ext_get_actual_len(ex);
1621 if (correct_index && le16toh(eh->eh_ecount))
1622 error = ext4_ext_correct_indexes(ip, path);
1625 * If this leaf is free, we should
1626 * remove it from index block above.
1628 if (error == 0 && eh->eh_ecount == 0 &&
1629 path[depth].ep_data != NULL)
1630 error = ext4_ext_rm_index(ip, path + depth);
1637 ext4_read_extent_tree_block(struct inode *ip, e4fs_daddr_t pblk,
1638 int depth, int flags)
1640 struct m_ext2fs *fs;
1641 struct ext4_extent_header *eh;
1646 error = bread(ip->i_devvp, fsbtodb(fs, pblk),
1647 fs->e2fs_bsize, NOCRED, &bp);
1652 eh = ext4_ext_block_header(bp->b_data);
1653 if (le16toh(eh->eh_depth) != depth) {
1654 SDT_PROBE2(ext2fs, , trace, extents, 1,
1655 "unexpected eh_depth");
1659 error = ext4_ext_check_header(ip, eh, depth);
1672 ext4_ext_more_to_rm(struct ext4_extent_path *path)
1675 KASSERT(path->ep_index != NULL,
1676 ("ext4_ext_more_to_rm: bad index from path"));
1678 if (path->ep_index < EXT_FIRST_INDEX(path->ep_header))
1681 if (le16toh(path->ep_header->eh_ecount) == path->index_count)
1688 ext4_ext_remove_space(struct inode *ip, off_t length, int flags,
1689 struct ucred *cred, struct thread *td)
1692 struct ext4_extent_header *ehp;
1693 struct ext4_extent_path *path;
1697 ehp = (struct ext4_extent_header *)ip->i_db;
1698 depth = ext4_ext_inode_depth(ip);
1700 error = ext4_ext_check_header(ip, ehp, depth);
1704 path = malloc(sizeof(struct ext4_extent_path) * (depth + 1),
1705 M_EXT2EXTENTS, M_WAITOK | M_ZERO);
1706 path[0].ep_header = ehp;
1707 path[0].ep_depth = depth;
1709 while (error == 0 && i >= 0) {
1712 error = ext4_ext_rm_leaf(ip, path, length);
1715 free(path[i].ep_data, M_EXT2EXTENTS);
1716 path[i].ep_data = NULL;
1721 /* This is index. */
1722 if (!path[i].ep_header)
1724 (struct ext4_extent_header *)path[i].ep_data;
1726 if (!path[i].ep_index) {
1727 /* This level hasn't touched yet. */
1728 path[i].ep_index = EXT_LAST_INDEX(path[i].ep_header);
1729 path[i].index_count =
1730 le16toh(path[i].ep_header->eh_ecount) + 1;
1732 /* We've already was here, see at next index. */
1736 if (ext4_ext_more_to_rm(path + i)) {
1737 memset(path + i + 1, 0, sizeof(*path));
1738 bp = ext4_read_extent_tree_block(ip,
1739 ext4_ext_index_pblock(path[i].ep_index),
1740 path[0].ep_depth - (i + 1), 0);
1746 ext4_ext_fill_path_bdata(&path[i+1], bp,
1747 ext4_ext_index_pblock(path[i].ep_index));
1749 path[i].index_count =
1750 le16toh(path[i].ep_header->eh_ecount);
1753 if (path[i].ep_header->eh_ecount == 0 && i > 0) {
1754 /* Index is empty, remove it. */
1755 error = ext4_ext_rm_index(ip, path + i);
1757 free(path[i].ep_data, M_EXT2EXTENTS);
1758 path[i].ep_data = NULL;
1763 if (path->ep_header->eh_ecount == 0) {
1765 * Truncate the tree to zero.
1767 ext4_ext_header(ip)->eh_depth = 0;
1768 ext4_ext_header(ip)->eh_max = htole16(ext4_ext_space_root(ip));
1769 ext4_ext_dirty(ip, path);
1772 ext4_ext_drop_refs(path);
1773 free(path, M_EXT2EXTENTS);
1775 ip->i_ext_cache.ec_type = EXT4_EXT_CACHE_NO;