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);
1103 ext4_ext_blkfree(ip, newblk, 1, 0);
1107 /* Move top-level index/leaf into new block. */
1108 memmove(bp->b_data, curpath->ep_header, sizeof(ip->i_data));
1110 /* Set size of new block */
1111 neh = ext4_ext_block_header(bp->b_data);
1112 neh->eh_magic = htole16(EXT4_EXT_MAGIC);
1114 if (ext4_ext_inode_depth(ip))
1115 neh->eh_max = htole16(ext4_ext_space_block_index(ip));
1117 neh->eh_max = htole16(ext4_ext_space_block(ip));
1119 ext2_extent_blk_csum_set(ip, bp->b_data);
1122 ext4_ext_blkfree(ip, newblk, 1, 0);
1128 curpath->ep_header->eh_magic = htole16(EXT4_EXT_MAGIC);
1129 curpath->ep_header->eh_max = htole16(ext4_ext_space_root(ip));
1130 curpath->ep_header->eh_ecount = htole16(1);
1131 curpath->ep_index = EXT_FIRST_INDEX(curpath->ep_header);
1132 curpath->ep_index->ei_blk = EXT_FIRST_EXTENT(path[0].ep_header)->e_blk;
1133 ext4_index_store_pblock(curpath->ep_index, newblk);
1135 neh = ext4_ext_inode_header(ip);
1136 neh->eh_depth = htole16(path->ep_depth + 1);
1137 ext4_ext_dirty(ip, curpath);
1145 ext4_ext_create_new_leaf(struct inode *ip, struct ext4_extent_path *path,
1146 struct ext4_extent *newext)
1148 struct ext4_extent_path *curpath;
1149 int depth, i, error;
1152 i = depth = ext4_ext_inode_depth(ip);
1154 /* Look for free index entry int the tree */
1155 curpath = path + depth;
1156 while (i > 0 && !EXT_HAS_FREE_INDEX(curpath)) {
1162 * We use already allocated block for index block,
1163 * so subsequent data blocks should be contiguous.
1165 if (EXT_HAS_FREE_INDEX(curpath)) {
1166 error = ext4_ext_split(ip, path, newext, i);
1171 ext4_ext_drop_refs(path);
1172 error = ext4_ext_find_extent(ip, le32toh(newext->e_blk), &path);
1176 /* Tree is full, do grow in depth. */
1177 error = ext4_ext_grow_indepth(ip, path, newext);
1182 ext4_ext_drop_refs(path);
1183 error = ext4_ext_find_extent(ip, le32toh(newext->e_blk), &path);
1187 /* Check and split tree if required. */
1188 depth = ext4_ext_inode_depth(ip);
1189 if (le16toh(path[depth].ep_header->eh_ecount) ==
1190 le16toh(path[depth].ep_header->eh_max))
1199 ext4_ext_correct_indexes(struct inode *ip, struct ext4_extent_path *path)
1201 struct ext4_extent_header *eh;
1202 struct ext4_extent *ex;
1206 depth = ext4_ext_inode_depth(ip);
1207 eh = path[depth].ep_header;
1208 ex = path[depth].ep_ext;
1210 if (ex == NULL || eh == NULL)
1216 /* We will correct tree if first leaf got modified only. */
1217 if (ex != EXT_FIRST_EXTENT(eh))
1221 border = le32toh(path[depth].ep_ext->e_blk);
1222 path[k].ep_index->ei_blk = htole32(border);
1223 ext4_ext_dirty(ip, path + k);
1225 /* Change all left-side indexes. */
1226 if (path[k+1].ep_index != EXT_FIRST_INDEX(path[k+1].ep_header))
1229 path[k].ep_index->ei_blk = htole32(border);
1230 ext4_ext_dirty(ip, path + k);
1237 ext4_ext_insert_extent(struct inode *ip, struct ext4_extent_path *path,
1238 struct ext4_extent *newext)
1240 struct ext4_extent_header * eh;
1241 struct ext4_extent *ex, *nex, *nearex;
1242 struct ext4_extent_path *npath;
1243 int depth, len, error, next;
1245 depth = ext4_ext_inode_depth(ip);
1246 ex = path[depth].ep_ext;
1249 if (htole16(newext->e_len) == 0 || path[depth].ep_header == NULL)
1252 /* Insert block into found extent. */
1253 if (ex && ext4_can_extents_be_merged(ex, newext)) {
1254 ex->e_len = htole16(le16toh(ex->e_len) + le16toh(newext->e_len));
1255 eh = path[depth].ep_header;
1261 depth = ext4_ext_inode_depth(ip);
1262 eh = path[depth].ep_header;
1263 if (le16toh(eh->eh_ecount) < le16toh(eh->eh_max))
1267 nex = EXT_LAST_EXTENT(eh);
1268 next = ext4_ext_next_leaf_block(ip, path);
1269 if (le32toh(newext->e_blk) > le32toh(nex->e_blk) && next !=
1271 KASSERT(npath == NULL,
1272 ("ext4_ext_insert_extent: bad path"));
1274 error = ext4_ext_find_extent(ip, next, &npath);
1278 if (npath->ep_depth != path->ep_depth) {
1283 eh = npath[depth].ep_header;
1284 if (le16toh(eh->eh_ecount) < le16toh(eh->eh_max)) {
1291 * There is no free space in the found leaf,
1292 * try to add a new leaf to the tree.
1294 error = ext4_ext_create_new_leaf(ip, path, newext);
1298 depth = ext4_ext_inode_depth(ip);
1299 eh = path[depth].ep_header;
1302 nearex = path[depth].ep_ext;
1304 /* Create new extent in the leaf. */
1305 path[depth].ep_ext = EXT_FIRST_EXTENT(eh);
1306 } else if (le32toh(newext->e_blk) > le32toh(nearex->e_blk)) {
1307 if (nearex != EXT_LAST_EXTENT(eh)) {
1308 len = EXT_MAX_EXTENT(eh) - nearex;
1309 len = (len - 1) * sizeof(struct ext4_extent);
1310 len = len < 0 ? 0 : len;
1311 memmove(nearex + 2, nearex + 1, len);
1313 path[depth].ep_ext = nearex + 1;
1315 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1316 len = len < 0 ? 0 : len;
1317 memmove(nearex + 1, nearex, len);
1318 path[depth].ep_ext = nearex;
1321 eh->eh_ecount = htole16(le16toh(eh->eh_ecount) + 1);
1322 nearex = path[depth].ep_ext;
1323 nearex->e_blk = newext->e_blk;
1324 nearex->e_start_lo = newext->e_start_lo;
1325 nearex->e_start_hi = newext->e_start_hi;
1326 nearex->e_len = newext->e_len;
1329 /* Try to merge extents to the right. */
1330 while (nearex < EXT_LAST_EXTENT(eh)) {
1331 if (!ext4_can_extents_be_merged(nearex, nearex + 1))
1334 /* Merge with next extent. */
1335 nearex->e_len = htole16(le16toh(nearex->e_len) +
1336 le16toh(nearex[1].e_len));
1337 if (nearex + 1 < EXT_LAST_EXTENT(eh)) {
1338 len = (EXT_LAST_EXTENT(eh) - nearex - 1) *
1339 sizeof(struct ext4_extent);
1340 memmove(nearex + 1, nearex + 2, len);
1343 eh->eh_ecount = htole16(le16toh(eh->eh_ecount) - 1);
1344 KASSERT(le16toh(eh->eh_ecount) != 0,
1345 ("ext4_ext_insert_extent: bad ecount"));
1349 * Try to merge extents to the left,
1350 * start from inexes correction.
1352 error = ext4_ext_correct_indexes(ip, path);
1356 ext4_ext_dirty(ip, path + depth);
1360 ext4_ext_drop_refs(npath);
1361 free(npath, M_EXT2EXTENTS);
1364 ip->i_ext_cache.ec_type = EXT4_EXT_CACHE_NO;
1369 ext4_new_blocks(struct inode *ip, daddr_t lbn, e4fs_daddr_t pref,
1370 struct ucred *cred, unsigned long *count, int *perror)
1372 struct m_ext2fs *fs;
1373 e4fs_daddr_t newblk;
1376 * We will allocate only single block for now.
1382 EXT2_LOCK(ip->i_ump);
1383 *perror = ext2_alloc(ip, lbn, pref, (int)fs->e2fs_bsize, cred, &newblk);
1388 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1389 ext2_update(ip->i_vnode, 1);
1396 ext4_ext_get_blocks(struct inode *ip, e4fs_daddr_t iblk,
1397 unsigned long max_blocks, struct ucred *cred, struct buf **bpp,
1398 int *pallocated, daddr_t *nb)
1400 struct m_ext2fs *fs;
1401 struct buf *bp = NULL;
1402 struct ext4_extent_path *path;
1403 struct ext4_extent newex, *ex;
1404 e4fs_daddr_t bpref, newblk = 0;
1405 unsigned long allocated = 0;
1406 int error = 0, depth;
1414 if ((bpref = ext4_ext_in_cache(ip, iblk, &newex))) {
1415 if (bpref == EXT4_EXT_CACHE_IN) {
1416 /* Block is already allocated. */
1417 newblk = iblk - le32toh(newex.e_blk) +
1418 ext4_ext_extent_pblock(&newex);
1419 allocated = le16toh(newex.e_len) - (iblk - le32toh(newex.e_blk));
1427 error = ext4_ext_find_extent(ip, iblk, &path);
1432 depth = ext4_ext_inode_depth(ip);
1433 if (path[depth].ep_ext == NULL && depth != 0) {
1438 if ((ex = path[depth].ep_ext)) {
1439 uint64_t lblk = le32toh(ex->e_blk);
1440 uint16_t e_len = le16toh(ex->e_len);
1441 e4fs_daddr_t e_start = ext4_ext_extent_pblock(ex);
1443 if (e_len > EXT4_MAX_LEN)
1446 /* If we found extent covers block, simply return it. */
1447 if (iblk >= lblk && iblk < lblk + e_len) {
1448 newblk = iblk - lblk + e_start;
1449 allocated = e_len - (iblk - lblk);
1450 ext4_ext_put_in_cache(ip, lblk, e_len,
1451 e_start, EXT4_EXT_CACHE_IN);
1456 /* Allocate the new block. */
1457 if (S_ISREG(ip->i_mode) && (!ip->i_next_alloc_block)) {
1458 ip->i_next_alloc_goal = 0;
1461 bpref = ext4_ext_blkpref(ip, path, iblk);
1462 allocated = max_blocks;
1463 newblk = ext4_new_blocks(ip, iblk, bpref, cred, &allocated, &error);
1467 /* Try to insert new extent into found leaf and return. */
1468 newex.e_blk = htole32(iblk);
1469 ext4_ext_store_pblock(&newex, newblk);
1470 newex.e_len = htole16(allocated);
1471 error = ext4_ext_insert_extent(ip, path, &newex);
1475 newblk = ext4_ext_extent_pblock(&newex);
1476 ext4_ext_put_in_cache(ip, iblk, allocated, newblk, EXT4_EXT_CACHE_IN);
1480 if (allocated > max_blocks)
1481 allocated = max_blocks;
1486 error = bread(ip->i_devvp, fsbtodb(fs, newblk),
1487 fs->e2fs_bsize, cred, &bp);
1497 ext4_ext_drop_refs(path);
1498 free(path, M_EXT2EXTENTS);
1507 static inline struct ext4_extent_header *
1508 ext4_ext_header(struct inode *ip)
1511 return ((struct ext4_extent_header *)ip->i_db);
1515 ext4_remove_blocks(struct inode *ip, struct ext4_extent *ex,
1516 unsigned long from, unsigned long to)
1518 unsigned long num, start;
1520 if (from >= le32toh(ex->e_blk) &&
1521 to == le32toh(ex->e_blk) + ext4_ext_get_actual_len(ex) - 1) {
1523 num = le32toh(ex->e_blk) + ext4_ext_get_actual_len(ex) - from;
1524 start = ext4_ext_extent_pblock(ex) +
1525 ext4_ext_get_actual_len(ex) - num;
1526 ext4_ext_blkfree(ip, start, num, 0);
1533 ext4_ext_rm_index(struct inode *ip, struct ext4_extent_path *path)
1537 /* Free index block. */
1539 leaf = ext4_ext_index_pblock(path->ep_index);
1540 KASSERT(path->ep_header->eh_ecount != 0,
1541 ("ext4_ext_rm_index: bad ecount"));
1542 path->ep_header->eh_ecount =
1543 htole16(le16toh(path->ep_header->eh_ecount) - 1);
1544 ext4_ext_dirty(ip, path);
1545 ext4_ext_blkfree(ip, leaf, 1, 0);
1550 ext4_ext_rm_leaf(struct inode *ip, struct ext4_extent_path *path,
1553 struct ext4_extent_header *eh;
1554 struct ext4_extent *ex;
1555 unsigned int a, b, block, num;
1556 unsigned long ex_blk;
1557 unsigned short ex_len;
1559 int error, correct_index;
1561 depth = ext4_ext_inode_depth(ip);
1562 if (!path[depth].ep_header) {
1563 if (path[depth].ep_data == NULL)
1565 path[depth].ep_header =
1566 (struct ext4_extent_header* )path[depth].ep_data;
1569 eh = path[depth].ep_header;
1571 SDT_PROBE2(ext2fs, , trace, extents, 1,
1572 "bad header => extent corrupted");
1576 ex = EXT_LAST_EXTENT(eh);
1577 ex_blk = le32toh(ex->e_blk);
1578 ex_len = ext4_ext_get_actual_len(ex);
1582 while (ex >= EXT_FIRST_EXTENT(eh) && ex_blk + ex_len > start) {
1583 path[depth].ep_ext = ex;
1584 a = ex_blk > start ? ex_blk : start;
1585 b = (uint64_t)ex_blk + ex_len - 1 <
1586 EXT4_MAX_BLOCKS ? ex_blk + ex_len - 1 : EXT4_MAX_BLOCKS;
1588 if (a != ex_blk && b != ex_blk + ex_len - 1)
1590 else if (a != ex_blk) {
1591 /* Remove tail of the extent. */
1594 } else if (b != ex_blk + ex_len - 1) {
1595 /* Remove head of the extent, not implemented. */
1598 /* Remove whole extent. */
1603 if (ex == EXT_FIRST_EXTENT(eh))
1606 error = ext4_remove_blocks(ip, ex, a, b);
1611 ext4_ext_store_pblock(ex, 0);
1612 eh->eh_ecount = htole16(le16toh(eh->eh_ecount) - 1);
1615 ex->e_blk = htole32(block);
1616 ex->e_len = htole16(num);
1618 ext4_ext_dirty(ip, path + depth);
1621 ex_blk = htole32(ex->e_blk);
1622 ex_len = ext4_ext_get_actual_len(ex);
1625 if (correct_index && le16toh(eh->eh_ecount))
1626 error = ext4_ext_correct_indexes(ip, path);
1629 * If this leaf is free, we should
1630 * remove it from index block above.
1632 if (error == 0 && eh->eh_ecount == 0 &&
1633 path[depth].ep_data != NULL)
1634 error = ext4_ext_rm_index(ip, path + depth);
1641 ext4_read_extent_tree_block(struct inode *ip, e4fs_daddr_t pblk,
1642 int depth, int flags)
1644 struct m_ext2fs *fs;
1645 struct ext4_extent_header *eh;
1650 error = bread(ip->i_devvp, fsbtodb(fs, pblk),
1651 fs->e2fs_bsize, NOCRED, &bp);
1656 eh = ext4_ext_block_header(bp->b_data);
1657 if (le16toh(eh->eh_depth) != depth) {
1658 SDT_PROBE2(ext2fs, , trace, extents, 1,
1659 "unexpected eh_depth");
1663 error = ext4_ext_check_header(ip, eh, depth);
1676 ext4_ext_more_to_rm(struct ext4_extent_path *path)
1679 KASSERT(path->ep_index != NULL,
1680 ("ext4_ext_more_to_rm: bad index from path"));
1682 if (path->ep_index < EXT_FIRST_INDEX(path->ep_header))
1685 if (le16toh(path->ep_header->eh_ecount) == path->index_count)
1692 ext4_ext_remove_space(struct inode *ip, off_t length, int flags,
1693 struct ucred *cred, struct thread *td)
1696 struct ext4_extent_header *ehp;
1697 struct ext4_extent_path *path;
1701 ehp = (struct ext4_extent_header *)ip->i_db;
1702 depth = ext4_ext_inode_depth(ip);
1704 error = ext4_ext_check_header(ip, ehp, depth);
1708 path = malloc(sizeof(struct ext4_extent_path) * (depth + 1),
1709 M_EXT2EXTENTS, M_WAITOK | M_ZERO);
1710 path[0].ep_header = ehp;
1711 path[0].ep_depth = depth;
1713 while (error == 0 && i >= 0) {
1716 error = ext4_ext_rm_leaf(ip, path, length);
1719 free(path[i].ep_data, M_EXT2EXTENTS);
1720 path[i].ep_data = NULL;
1725 /* This is index. */
1726 if (!path[i].ep_header)
1728 (struct ext4_extent_header *)path[i].ep_data;
1730 if (!path[i].ep_index) {
1731 /* This level hasn't touched yet. */
1732 path[i].ep_index = EXT_LAST_INDEX(path[i].ep_header);
1733 path[i].index_count =
1734 le16toh(path[i].ep_header->eh_ecount) + 1;
1736 /* We've already was here, see at next index. */
1740 if (ext4_ext_more_to_rm(path + i)) {
1741 memset(path + i + 1, 0, sizeof(*path));
1742 bp = ext4_read_extent_tree_block(ip,
1743 ext4_ext_index_pblock(path[i].ep_index),
1744 path[0].ep_depth - (i + 1), 0);
1750 ext4_ext_fill_path_bdata(&path[i+1], bp,
1751 ext4_ext_index_pblock(path[i].ep_index));
1753 path[i].index_count =
1754 le16toh(path[i].ep_header->eh_ecount);
1757 if (path[i].ep_header->eh_ecount == 0 && i > 0) {
1758 /* Index is empty, remove it. */
1759 error = ext4_ext_rm_index(ip, path + i);
1761 free(path[i].ep_data, M_EXT2EXTENTS);
1762 path[i].ep_data = NULL;
1767 if (path->ep_header->eh_ecount == 0) {
1769 * Truncate the tree to zero.
1771 ext4_ext_header(ip)->eh_depth = 0;
1772 ext4_ext_header(ip)->eh_max = htole16(ext4_ext_space_root(ip));
1773 ext4_ext_dirty(ip, path);
1776 ext4_ext_drop_refs(path);
1777 free(path, M_EXT2EXTENTS);
1779 ip->i_ext_cache.ec_type = EXT4_EXT_CACHE_NO;