xref: /src/sys/fs/ext2fs/ext2_extents.c (revision 83e5b79c7c98a7f8c94a99b102bc8a90c195769a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2010 Zheng Liu <lz@freebsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28 
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>
35 #include <sys/bio.h>
36 #include <sys/buf.h>
37 #include <sys/endian.h>
38 #include <sys/conf.h>
39 #include <sys/sdt.h>
40 #include <sys/stat.h>
41 
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>
48 
49 SDT_PROVIDER_DECLARE(ext2fs);
50 /*
51  * ext2fs trace probe:
52  * arg0: verbosity. Higher numbers give more verbose messages
53  * arg1: Textual message
54  */
55 SDT_PROBE_DEFINE2(ext2fs, , trace, extents, "int", "char*");
56 
57 static MALLOC_DEFINE(M_EXT2EXTENTS, "ext2_extents", "EXT2 extents");
58 
59 #ifdef EXT2FS_PRINT_EXTENTS
60 static const bool print_extents_walk = true;
61 
62 static int ext4_ext_check_header(struct inode *, struct ext4_extent_header *,
63     int);
64 static int ext4_ext_walk_header(struct inode *, struct ext4_extent_header *,
65     int);
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 *);
68 
69 static int
ext4_ext_blk_check(struct inode * ip,e4fs_daddr_t blk)70 ext4_ext_blk_check(struct inode *ip, e4fs_daddr_t blk)
71 {
72 	struct m_ext2fs *fs;
73 
74 	fs = ip->i_e2fs;
75 
76 	if (blk < fs->e2fs->e2fs_first_dblock || blk >= fs->e2fs_bcount)
77 		return (EIO);
78 
79 	return (0);
80 }
81 
82 static int
ext4_ext_walk_index(struct inode * ip,struct ext4_extent_index * ex,int depth,bool do_walk)83 ext4_ext_walk_index(struct inode *ip, struct ext4_extent_index *ex, int depth,
84     bool do_walk)
85 {
86 	struct m_ext2fs *fs;
87 	struct buf *bp;
88 	e4fs_daddr_t blk;
89 	int error;
90 
91 	fs = ip->i_e2fs;
92 
93 	if (print_extents_walk)
94 		printf("    index %p => (blk %u pblk %ju)\n", ex,
95 		    le32toh(ex->ei_blk),
96 		    (uint64_t)le16toh(ex->ei_leaf_hi) << 32 |
97 		    le32toh(ex->ei_leaf_lo));
98 
99 	if(!do_walk)
100 		return (0);
101 
102 	blk = ext4_ext_index_pblock(ex);
103 	error = ext4_ext_blk_check(ip, blk);
104 	if (error)
105 		return (error);
106 
107 	if ((error = bread(ip->i_devvp,
108 	    fsbtodb(fs, blk), (int)fs->e2fs_bsize, NOCRED, &bp)) != 0) {
109 		brelse(bp);
110 		return (error);
111 	}
112 
113 	error = ext4_ext_walk_header(ip,
114 	    (struct ext4_extent_header *)bp->b_data, depth);
115 
116 	brelse(bp);
117 
118 	return (error);
119 }
120 
121 static int
ext4_ext_walk_extent(struct inode * ip,struct ext4_extent * ep)122 ext4_ext_walk_extent(struct inode *ip, struct ext4_extent *ep)
123 {
124 	e4fs_daddr_t blk;
125 	int error;
126 
127 	blk = ext4_ext_extent_pblock(ep);
128 	error = ext4_ext_blk_check(ip, blk);
129 	if (error)
130 		return (error);
131 
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),
135 		    (uint64_t)blk);
136 
137 	return (0);
138 }
139 
140 static int
ext4_ext_walk_header(struct inode * ip,struct ext4_extent_header * eh,int depth)141 ext4_ext_walk_header(struct inode *ip, struct ext4_extent_header *eh, int depth)
142 {
143 	int i, error = 0;
144 
145 	error = ext4_ext_check_header(ip, eh, depth);
146 	if (error)
147 		return (error);
148 
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));
154 
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,
159 			    true);
160 		else
161 			error = ext4_ext_walk_extent(ip,
162 			    (struct ext4_extent *)(eh + 1 + i));
163 
164 	return (error);
165 }
166 
167 int
ext4_ext_walk(struct inode * ip)168 ext4_ext_walk(struct inode *ip)
169 {
170 	struct ext4_extent_header *ehp;
171 
172 	ehp = (struct ext4_extent_header *)ip->i_db;
173 
174 	if (print_extents_walk)
175 		printf("Extent status:ip=%ju\n", ip->i_number);
176 
177 	if (!(ip->i_flag & IN_E4EXTENTS))
178 		return (0);
179 
180 	return (ext4_ext_walk_header(ip, ehp, 0));
181 }
182 
183 static int
ext4_ext_print_path(struct inode * ip,struct ext4_extent_path * path)184 ext4_ext_print_path(struct inode *ip, struct ext4_extent_path *path)
185 {
186 	int k, depth, error = 0;
187 
188 	depth = path->ep_depth;
189 
190 	if (print_extents_walk)
191 		printf("ip=%ju, Path:\n", ip->i_number);
192 
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,
196 			    depth - 1, false);
197 		} else if (path->ep_ext) {
198 			error = ext4_ext_walk_extent(ip, path->ep_ext);
199 		}
200 	}
201 
202 	return (error);
203 }
204 #endif
205 
206 static inline struct ext4_extent_header *
ext4_ext_inode_header(struct inode * ip)207 ext4_ext_inode_header(struct inode *ip)
208 {
209 
210 	return ((struct ext4_extent_header *)ip->i_db);
211 }
212 
213 static inline struct ext4_extent_header *
ext4_ext_block_header(char * bdata)214 ext4_ext_block_header(char *bdata)
215 {
216 
217 	return ((struct ext4_extent_header *)bdata);
218 }
219 
220 static inline unsigned short
ext4_ext_inode_depth(struct inode * ip)221 ext4_ext_inode_depth(struct inode *ip)
222 {
223 	struct ext4_extent_header *ehp;
224 
225 	ehp = (struct ext4_extent_header *)ip->i_data;
226 	return (le16toh(ehp->eh_depth));
227 }
228 
229 static inline e4fs_daddr_t
ext4_ext_index_pblock(struct ext4_extent_index * index)230 ext4_ext_index_pblock(struct ext4_extent_index *index)
231 {
232 	e4fs_daddr_t blk;
233 
234 	blk = le32toh(index->ei_leaf_lo);
235 	blk |= (e4fs_daddr_t)le16toh(index->ei_leaf_hi) << 32;
236 
237 	return (blk);
238 }
239 
240 static inline void
ext4_index_store_pblock(struct ext4_extent_index * index,e4fs_daddr_t pb)241 ext4_index_store_pblock(struct ext4_extent_index *index, e4fs_daddr_t pb)
242 {
243 
244 	index->ei_leaf_lo = htole32(pb & 0xffffffff);
245 	index->ei_leaf_hi = htole16((pb >> 32) & 0xffff);
246 }
247 
248 static inline e4fs_daddr_t
ext4_ext_extent_pblock(struct ext4_extent * extent)249 ext4_ext_extent_pblock(struct ext4_extent *extent)
250 {
251 	e4fs_daddr_t blk;
252 
253 	blk = le32toh(extent->e_start_lo);
254 	blk |= (e4fs_daddr_t)le16toh(extent->e_start_hi) << 32;
255 
256 	return (blk);
257 }
258 
259 static inline void
ext4_ext_store_pblock(struct ext4_extent * ex,e4fs_daddr_t pb)260 ext4_ext_store_pblock(struct ext4_extent *ex, e4fs_daddr_t pb)
261 {
262 
263 	ex->e_start_lo = htole32(pb & 0xffffffff);
264 	ex->e_start_hi = htole16((pb >> 32) & 0xffff);
265 }
266 
267 int
ext4_ext_in_cache(struct inode * ip,daddr_t lbn,struct ext4_extent * ep)268 ext4_ext_in_cache(struct inode *ip, daddr_t lbn, struct ext4_extent *ep)
269 {
270 	struct ext4_extent_cache *ecp;
271 	int ret = EXT4_EXT_CACHE_NO;
272 
273 	ecp = &ip->i_ext_cache;
274 	if (ecp->ec_type == EXT4_EXT_CACHE_NO)
275 		return (ret);
276 
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);
282 		ret = ecp->ec_type;
283 	}
284 	return (ret);
285 }
286 
287 static inline int
ext4_ext_space_root(struct inode * ip)288 ext4_ext_space_root(struct inode *ip)
289 {
290 	int size;
291 
292 	size = sizeof(ip->i_data);
293 	size -= sizeof(struct ext4_extent_header);
294 	size /= sizeof(struct ext4_extent);
295 
296 	return (size);
297 }
298 
299 static inline int
ext4_ext_space_block(struct inode * ip)300 ext4_ext_space_block(struct inode *ip)
301 {
302 	struct m_ext2fs *fs;
303 	int size;
304 
305 	fs = ip->i_e2fs;
306 
307 	size = (fs->e2fs_bsize - sizeof(struct ext4_extent_header)) /
308 	    sizeof(struct ext4_extent);
309 
310 	return (size);
311 }
312 
313 static inline int
ext4_ext_space_root_idx(struct inode * ip)314 ext4_ext_space_root_idx(struct inode *ip)
315 {
316 	int size;
317 
318 	size = sizeof(ip->i_data);
319 	size -= sizeof(struct ext4_extent_header);
320 	size /= sizeof(struct ext4_extent_index);
321 
322 	return (size);
323 }
324 
325 static inline int
ext4_ext_space_block_idx(struct inode * ip)326 ext4_ext_space_block_idx(struct inode *ip)
327 {
328 	struct m_ext2fs *fs;
329 	int size;
330 
331 	fs = ip->i_e2fs;
332 
333 	size = (fs->e2fs_bsize - sizeof(struct ext4_extent_header)) /
334 	    sizeof(struct ext4_extent_index);
335 
336 	return (size);
337 }
338 
339 static int
ext4_ext_max_entries(struct inode * ip,int depth)340 ext4_ext_max_entries(struct inode *ip, int depth)
341 {
342 
343 	if (depth == ext4_ext_inode_depth(ip)) {
344 		if (depth == 0)
345 			return (ext4_ext_space_root(ip));
346 		else
347 			return (ext4_ext_space_root_idx(ip));
348 	} else {
349 		if (depth == 0)
350 			return (ext4_ext_space_block(ip));
351 		else
352 			return (ext4_ext_space_block_idx(ip));
353 	}
354 }
355 
356 static inline uint16_t
ext4_ext_get_actual_len(struct ext4_extent * ext)357 ext4_ext_get_actual_len(struct ext4_extent *ext)
358 {
359 
360 	return (le16toh(ext->e_len) <= EXT_INIT_MAX_LEN ?
361 	    le16toh(ext->e_len) : (le16toh(ext->e_len) - EXT_INIT_MAX_LEN));
362 }
363 
364 
365 static int
ext4_inode_block_validate(struct inode * ip,e4fs_daddr_t start_blk,unsigned int count)366 ext4_inode_block_validate(struct inode *ip, e4fs_daddr_t start_blk,
367     unsigned int count)
368 {
369 	struct m_ext2fs *fs;
370 
371 	fs = ip->i_e2fs;
372 
373 	if ((start_blk <= le32toh(fs->e2fs->e2fs_first_dblock)) ||
374 	    (start_blk + count < start_blk) ||
375 	    (start_blk + count > fs->e2fs_bcount))
376 		return (EIO);
377 
378 	return (0);
379 }
380 
381 static int
ext4_validate_extent(struct inode * ip,struct ext4_extent * ext)382 ext4_validate_extent(struct inode *ip, struct ext4_extent *ext)
383 {
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);
387 
388 	if (lblk + len <= lblk)
389 		return (EIO);
390 
391 	return (ext4_inode_block_validate(ip, blk, len));
392 }
393 
394 static int
ext4_validate_extent_idx(struct inode * ip,struct ext4_extent_index * ext_idx)395 ext4_validate_extent_idx(struct inode *ip, struct ext4_extent_index *ext_idx)
396 {
397 	e4fs_daddr_t blk = ext4_ext_index_pblock(ext_idx);
398 
399 	return (ext4_inode_block_validate(ip, blk, 1));
400 }
401 
402 static int
ext4_validate_extent_entries(struct inode * ip,struct ext4_extent_header * eh,int depth)403 ext4_validate_extent_entries(struct inode *ip, struct ext4_extent_header *eh,
404     int depth)
405 {
406 	unsigned int count;
407 
408 	count = le16toh(eh->eh_ecount);
409 	if (count == 0)
410 		return (0);
411 
412 	if (depth == 0) {
413 		struct ext4_extent *ext = EXT_FIRST_EXTENT(eh);
414 		uint32_t lblk = 0;
415 		uint32_t prev = 0;
416 		int len = 0;
417 		while (count) {
418 			/* leaf entries */
419 			if (ext4_validate_extent(ip, ext))
420 				return (EIO);
421 
422 			/* Check for overlapping extents */
423 			lblk = le32toh(ext->e_blk);
424 			len = ext4_ext_get_actual_len(ext);
425 			if ((lblk <= prev) && prev)
426 				return (EIO);
427 
428 			ext++;
429 			count--;
430 			prev = lblk + len - 1;
431 		}
432 	} else {
433 		struct ext4_extent_index *ext_idx = EXT_FIRST_INDEX(eh);
434 		while (count) {
435 			if (ext4_validate_extent_idx(ip, ext_idx))
436 				return (EIO);
437 
438 			ext_idx++;
439 			count--;
440 		}
441 	}
442 
443 	return (0);
444 }
445 
446 static int
ext4_ext_check_header(struct inode * ip,struct ext4_extent_header * eh,int depth)447 ext4_ext_check_header(struct inode *ip, struct ext4_extent_header *eh,
448     int depth)
449 {
450 	char *error_msg __sdt_used;
451 
452 	if (le16toh(eh->eh_magic) != EXT4_EXT_MAGIC) {
453 		error_msg = "header: invalid magic";
454 		goto corrupted;
455 	}
456 	if (le16toh(eh->eh_depth) != depth ||
457 	    le16toh(eh->eh_depth) > EXT4_EXT_DEPTH_MAX)
458 	{
459 		error_msg = "header: invalid eh_depth";
460 		goto corrupted;
461 	}
462 	if (eh->eh_max == 0) {
463 		error_msg = "header: invalid eh_max";
464 		goto corrupted;
465 	}
466 	if (le16toh(eh->eh_max) > ext4_ext_max_entries(ip, depth)) {
467 		error_msg = "header: too large eh_max";
468 		goto corrupted;
469 	}
470 	if (le16toh(eh->eh_ecount) > le16toh(eh->eh_max)) {
471 		error_msg = "header: invalid eh_entries";
472 		goto corrupted;
473 	}
474 	if (le16toh(eh->eh_depth) > EXT4_EXT_DEPTH_MAX) {
475 		error_msg = "header: invalid eh_depth";
476 		goto corrupted;
477 	}
478 	if (ext4_validate_extent_entries(ip, eh, depth)) {
479 		error_msg = "header: invalid extent entries";
480 		goto corrupted;
481 	}
482 
483 	return (0);
484 
485 corrupted:
486 	SDT_PROBE2(ext2fs, , trace, extents, 1, error_msg);
487 	return (EIO);
488 }
489 
490 static void
ext4_ext_binsearch_index(struct ext4_extent_path * path,int blk)491 ext4_ext_binsearch_index(struct ext4_extent_path *path, int blk)
492 {
493 	struct ext4_extent_header *eh;
494 	struct ext4_extent_index *r, *l, *m;
495 
496 	eh = path->ep_header;
497 
498 	KASSERT(le16toh(eh->eh_ecount) <= le16toh(eh->eh_max) &&
499 	    le16toh(eh->eh_ecount) > 0,
500 	    ("ext4_ext_binsearch_index: bad args"));
501 
502 	l = EXT_FIRST_INDEX(eh) + 1;
503 	r = EXT_FIRST_INDEX(eh) + le16toh(eh->eh_ecount) - 1;
504 	while (l <= r) {
505 		m = l + (r - l) / 2;
506 		if (blk < le32toh(m->ei_blk))
507 			r = m - 1;
508 		else
509 			l = m + 1;
510 	}
511 
512 	path->ep_index = l - 1;
513 }
514 
515 static void
ext4_ext_binsearch_ext(struct ext4_extent_path * path,int blk)516 ext4_ext_binsearch_ext(struct ext4_extent_path *path, int blk)
517 {
518 	struct ext4_extent_header *eh;
519 	struct ext4_extent *r, *l, *m;
520 
521 	eh = path->ep_header;
522 
523 	KASSERT(le16toh(eh->eh_ecount) <= le16toh(eh->eh_max),
524 	    ("ext4_ext_binsearch_ext: bad args"));
525 
526 	if (eh->eh_ecount == 0)
527 		return;
528 
529 	l = EXT_FIRST_EXTENT(eh) + 1;
530 	r = EXT_FIRST_EXTENT(eh) + le16toh(eh->eh_ecount) - 1;
531 
532 	while (l <= r) {
533 		m = l + (r - l) / 2;
534 		if (blk < le32toh(m->e_blk))
535 			r = m - 1;
536 		else
537 			l = m + 1;
538 	}
539 
540 	path->ep_ext = l - 1;
541 }
542 
543 static int
ext4_ext_fill_path_bdata(struct ext4_extent_path * path,struct buf * bp,uint64_t blk)544 ext4_ext_fill_path_bdata(struct ext4_extent_path *path,
545     struct buf *bp, uint64_t blk)
546 {
547 
548 	KASSERT(path->ep_data == NULL,
549 	    ("ext4_ext_fill_path_bdata: bad ep_data"));
550 
551 	path->ep_data = malloc(bp->b_bufsize, M_EXT2EXTENTS, M_WAITOK);
552 	memcpy(path->ep_data, bp->b_data, bp->b_bufsize);
553 	path->ep_blk = blk;
554 
555 	return (0);
556 }
557 
558 static void
ext4_ext_fill_path_buf(struct ext4_extent_path * path,struct buf * bp)559 ext4_ext_fill_path_buf(struct ext4_extent_path *path, struct buf *bp)
560 {
561 
562 	KASSERT(path->ep_data != NULL,
563 	    ("ext4_ext_fill_path_buf: bad ep_data"));
564 
565 	memcpy(bp->b_data, path->ep_data, bp->b_bufsize);
566 }
567 
568 static void
ext4_ext_drop_refs(struct ext4_extent_path * path)569 ext4_ext_drop_refs(struct ext4_extent_path *path)
570 {
571 	int depth, i;
572 
573 	if (!path)
574 		return;
575 
576 	depth = path->ep_depth;
577 	for (i = 0; i <= depth; i++, path++)
578 		if (path->ep_data) {
579 			free(path->ep_data, M_EXT2EXTENTS);
580 			path->ep_data = NULL;
581 		}
582 }
583 
584 void
ext4_ext_path_free(struct ext4_extent_path * path)585 ext4_ext_path_free(struct ext4_extent_path *path)
586 {
587 
588 	if (!path)
589 		return;
590 
591 	ext4_ext_drop_refs(path);
592 	free(path, M_EXT2EXTENTS);
593 }
594 
595 int
ext4_ext_find_extent(struct inode * ip,daddr_t block,struct ext4_extent_path ** ppath)596 ext4_ext_find_extent(struct inode *ip, daddr_t block,
597     struct ext4_extent_path **ppath)
598 {
599 	struct ext4_extent_header *eh;
600 	struct ext4_extent_path *path;
601 	struct buf *bp;
602 	uint64_t blk;
603 	int error, depth, i, ppos, alloc;
604 
605 	eh = ext4_ext_inode_header(ip);
606 	depth = ext4_ext_inode_depth(ip);
607 	ppos = 0;
608 	alloc = 0;
609 
610 	error = ext4_ext_check_header(ip, eh, depth);
611 	if (error)
612 		return (error);
613 
614 	if (ppath == NULL)
615 		return (EINVAL);
616 
617 	path = *ppath;
618 	if (path == NULL) {
619 		path = malloc(EXT4_EXT_DEPTH_MAX *
620 		    sizeof(struct ext4_extent_path),
621 		    M_EXT2EXTENTS, M_WAITOK | M_ZERO);
622 		*ppath = path;
623 		alloc = 1;
624 	}
625 
626 	path[0].ep_header = eh;
627 	path[0].ep_data = NULL;
628 
629 	/* Walk through the tree. */
630 	i = depth;
631 	while (i) {
632 		ext4_ext_binsearch_index(&path[ppos], block);
633 		blk = ext4_ext_index_pblock(path[ppos].ep_index);
634 		path[ppos].ep_depth = i;
635 		path[ppos].ep_ext = NULL;
636 
637 		error = bread(ip->i_devvp, fsbtodb(ip->i_e2fs, blk),
638 		    ip->i_e2fs->e2fs_bsize, NOCRED, &bp);
639 		if (error) {
640 			goto error;
641 		}
642 
643 		ppos++;
644 		if (ppos > depth) {
645 			SDT_PROBE2(ext2fs, , trace, extents, 1,
646 			    "ppos > depth => extent corrupted");
647 			error = EIO;
648 			brelse(bp);
649 			goto error;
650 		}
651 
652 		ext4_ext_fill_path_bdata(&path[ppos], bp, blk);
653 		bqrelse(bp);
654 
655 		eh = ext4_ext_block_header(path[ppos].ep_data);
656 		if (ext4_ext_check_header(ip, eh, i - 1) ||
657 		    ext2_extent_blk_csum_verify(ip, path[ppos].ep_data)) {
658 			error = EIO;
659 			goto error;
660 		}
661 
662 		path[ppos].ep_header = eh;
663 
664 		i--;
665 	}
666 
667 	error = ext4_ext_check_header(ip, eh, 0);
668 	if (error)
669 		goto error;
670 
671 	/* Find extent. */
672 	path[ppos].ep_depth = i;
673 	path[ppos].ep_header = eh;
674 	path[ppos].ep_ext = NULL;
675 	path[ppos].ep_index = NULL;
676 	ext4_ext_binsearch_ext(&path[ppos], block);
677 	return (0);
678 
679 error:
680 	ext4_ext_drop_refs(path);
681 	if (alloc)
682 		free(path, M_EXT2EXTENTS);
683 
684 	*ppath = NULL;
685 
686 	return (error);
687 }
688 
689 static inline int
ext4_ext_space_block_index(struct inode * ip)690 ext4_ext_space_block_index(struct inode *ip)
691 {
692 	struct m_ext2fs *fs;
693 	int size;
694 
695 	fs = ip->i_e2fs;
696 
697 	size = (fs->e2fs_bsize - sizeof(struct ext4_extent_header)) /
698 	    sizeof(struct ext4_extent_index);
699 
700 	return (size);
701 }
702 
703 void
ext4_ext_tree_init(struct inode * ip)704 ext4_ext_tree_init(struct inode *ip)
705 {
706 	struct ext4_extent_header *ehp;
707 
708 	ip->i_flag |= IN_E4EXTENTS;
709 
710 	memset(ip->i_data, 0, sizeof(ip->i_data));
711 	ehp = (struct ext4_extent_header *)ip->i_data;
712 	ehp->eh_magic = htole16(EXT4_EXT_MAGIC);
713 	ehp->eh_max = htole16(ext4_ext_space_root(ip));
714 	ip->i_ext_cache.ec_type = EXT4_EXT_CACHE_NO;
715 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
716 	ext2_update(ip->i_vnode, 1);
717 }
718 
719 static inline void
ext4_ext_put_in_cache(struct inode * ip,uint32_t blk,uint32_t len,uint32_t start,int type)720 ext4_ext_put_in_cache(struct inode *ip, uint32_t blk,
721 			uint32_t len, uint32_t start, int type)
722 {
723 
724 	KASSERT(len != 0, ("ext4_ext_put_in_cache: bad input"));
725 
726 	ip->i_ext_cache.ec_type = type;
727 	ip->i_ext_cache.ec_blk = blk;
728 	ip->i_ext_cache.ec_len = len;
729 	ip->i_ext_cache.ec_start = start;
730 }
731 
732 static e4fs_daddr_t
ext4_ext_blkpref(struct inode * ip,struct ext4_extent_path * path,e4fs_daddr_t block)733 ext4_ext_blkpref(struct inode *ip, struct ext4_extent_path *path,
734     e4fs_daddr_t block)
735 {
736 	struct m_ext2fs *fs;
737 	struct ext4_extent *ex;
738 	e4fs_daddr_t bg_start;
739 	int depth;
740 
741 	fs = ip->i_e2fs;
742 
743 	if (path) {
744 		depth = path->ep_depth;
745 		ex = path[depth].ep_ext;
746 		if (ex) {
747 			e4fs_daddr_t pblk = ext4_ext_extent_pblock(ex);
748 			e2fs_daddr_t blk = le32toh(ex->e_blk);
749 
750 			if (block > blk)
751 				return (pblk + (block - blk));
752 			else
753 				return (pblk - (blk - block));
754 		}
755 
756 		/* Try to get block from index itself. */
757 		if (path[depth].ep_data)
758 			return (path[depth].ep_blk);
759 	}
760 
761 	/* Use inode's group. */
762 	bg_start = (ip->i_block_group * EXT2_BLOCKS_PER_GROUP(ip->i_e2fs)) +
763 	    le32toh(fs->e2fs->e2fs_first_dblock);
764 
765 	return (bg_start + block);
766 }
767 
768 static int inline
ext4_can_extents_be_merged(struct ext4_extent * ex1,struct ext4_extent * ex2)769 ext4_can_extents_be_merged(struct ext4_extent *ex1,
770     struct ext4_extent *ex2)
771 {
772 
773 	if (le32toh(ex1->e_blk) + le16toh(ex1->e_len) != le32toh(ex2->e_blk))
774 		return (0);
775 
776 	if (le16toh(ex1->e_len) + le16toh(ex2->e_len) > EXT4_MAX_LEN)
777 		return (0);
778 
779 	if (ext4_ext_extent_pblock(ex1) + le16toh(ex1->e_len) ==
780 	    ext4_ext_extent_pblock(ex2))
781 		return (1);
782 
783 	return (0);
784 }
785 
786 static unsigned
ext4_ext_next_leaf_block(struct inode * ip,struct ext4_extent_path * path)787 ext4_ext_next_leaf_block(struct inode *ip, struct ext4_extent_path *path)
788 {
789 	int depth = path->ep_depth;
790 
791 	/* Empty tree */
792 	if (depth == 0)
793 		return (EXT4_MAX_BLOCKS);
794 
795 	/* Go to indexes. */
796 	depth--;
797 
798 	while (depth >= 0) {
799 		if (path[depth].ep_index !=
800 		    EXT_LAST_INDEX(path[depth].ep_header))
801 			return (le32toh(path[depth].ep_index[1].ei_blk));
802 
803 		depth--;
804 	}
805 
806 	return (EXT4_MAX_BLOCKS);
807 }
808 
809 static int
ext4_ext_dirty(struct inode * ip,struct ext4_extent_path * path)810 ext4_ext_dirty(struct inode *ip, struct ext4_extent_path *path)
811 {
812 	struct m_ext2fs *fs;
813 	struct buf *bp;
814 	uint64_t blk;
815 	int error;
816 
817 	fs = ip->i_e2fs;
818 
819 	if (!path)
820 		return (EINVAL);
821 
822 	if (path->ep_data) {
823 		blk = path->ep_blk;
824 		bp = getblk(ip->i_devvp, fsbtodb(fs, blk),
825 		    fs->e2fs_bsize, 0, 0, 0);
826 		if (!bp)
827 			return (EIO);
828 		ext4_ext_fill_path_buf(path, bp);
829 		ext2_extent_blk_csum_set(ip, bp->b_data);
830 		error = bwrite(bp);
831 	} else {
832 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
833 		error = ext2_update(ip->i_vnode, 1);
834 	}
835 
836 	return (error);
837 }
838 
839 static int
ext4_ext_insert_index(struct inode * ip,struct ext4_extent_path * path,uint32_t lblk,e4fs_daddr_t blk)840 ext4_ext_insert_index(struct inode *ip, struct ext4_extent_path *path,
841     uint32_t lblk, e4fs_daddr_t blk)
842 {
843 	struct ext4_extent_index *idx;
844 	int len;
845 
846 	if (lblk == le32toh(path->ep_index->ei_blk)) {
847 		SDT_PROBE2(ext2fs, , trace, extents, 1,
848 		    "lblk == index blk => extent corrupted");
849 		return (EIO);
850 	}
851 
852 	if (le16toh(path->ep_header->eh_ecount) >=
853 	    le16toh(path->ep_header->eh_max)) {
854 		SDT_PROBE2(ext2fs, , trace, extents, 1,
855 		    "ecout > maxcount => extent corrupted");
856 		return (EIO);
857 	}
858 
859 	if (lblk > le32toh(path->ep_index->ei_blk)) {
860 		/* Insert after. */
861 		idx = path->ep_index + 1;
862 	} else {
863 		/* Insert before. */
864 		idx = path->ep_index;
865 	}
866 
867 	len = EXT_LAST_INDEX(path->ep_header) - idx + 1;
868 	if (len > 0)
869 		memmove(idx + 1, idx, len * sizeof(struct ext4_extent_index));
870 
871 	if (idx > EXT_MAX_INDEX(path->ep_header)) {
872 		SDT_PROBE2(ext2fs, , trace, extents, 1,
873 		    "index is out of range => extent corrupted");
874 		return (EIO);
875 	}
876 
877 	idx->ei_blk = htole32(lblk);
878 	ext4_index_store_pblock(idx, blk);
879 	path->ep_header->eh_ecount =
880 	    htole16(le16toh(path->ep_header->eh_ecount) + 1);
881 
882 	return (ext4_ext_dirty(ip, path));
883 }
884 
885 static e4fs_daddr_t
ext4_ext_alloc_meta(struct inode * ip)886 ext4_ext_alloc_meta(struct inode *ip)
887 {
888 	e4fs_daddr_t blk = ext2_alloc_meta(ip);
889 	if (blk) {
890 		ip->i_blocks += btodb(ip->i_e2fs->e2fs_bsize);
891 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
892 		ext2_update(ip->i_vnode, 1);
893 	}
894 
895 	return (blk);
896 }
897 
898 static void
ext4_ext_blkfree(struct inode * ip,uint64_t blk,int count,int flags)899 ext4_ext_blkfree(struct inode *ip, uint64_t blk, int count, int flags)
900 {
901 	struct m_ext2fs *fs;
902 	int i, blocksreleased;
903 
904 	fs = ip->i_e2fs;
905 	blocksreleased = count;
906 
907 	for(i = 0; i < count; i++)
908 		ext2_blkfree(ip, blk + i, fs->e2fs_bsize);
909 
910 	if (ip->i_blocks >= blocksreleased)
911 		ip->i_blocks -= (btodb(fs->e2fs_bsize)*blocksreleased);
912 	else
913 		ip->i_blocks = 0;
914 
915 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
916 	ext2_update(ip->i_vnode, 1);
917 }
918 
919 static int
ext4_ext_split(struct inode * ip,struct ext4_extent_path * path,struct ext4_extent * newext,int at)920 ext4_ext_split(struct inode *ip, struct ext4_extent_path *path,
921     struct ext4_extent *newext, int at)
922 {
923 	struct m_ext2fs *fs;
924 	struct  buf *bp;
925 	int depth = ext4_ext_inode_depth(ip);
926 	struct ext4_extent_header *neh;
927 	struct ext4_extent_index *fidx;
928 	struct ext4_extent *ex;
929 	int i = at, k, m, a;
930 	e4fs_daddr_t newblk, oldblk;
931 	uint32_t border;
932 	e4fs_daddr_t *ablks = NULL;
933 	int error = 0;
934 
935 	fs = ip->i_e2fs;
936 	bp = NULL;
937 
938 	/*
939 	 * We will split at current extent for now.
940 	 */
941 	if (path[depth].ep_ext > EXT_MAX_EXTENT(path[depth].ep_header)) {
942 		SDT_PROBE2(ext2fs, , trace, extents, 1,
943 		    "extent is out of range => extent corrupted");
944 		return (EIO);
945 	}
946 
947 	if (path[depth].ep_ext != EXT_MAX_EXTENT(path[depth].ep_header))
948 		border = le32toh(path[depth].ep_ext[1].e_blk);
949 	else
950 		border = le32toh(newext->e_blk);
951 
952 	/* Allocate new blocks. */
953 	ablks = malloc(sizeof(e4fs_daddr_t) * depth,
954 	    M_EXT2EXTENTS, M_WAITOK | M_ZERO);
955 	for (a = 0; a < depth - at; a++) {
956 		newblk = ext4_ext_alloc_meta(ip);
957 		if (newblk == 0)
958 			goto cleanup;
959 		ablks[a] = newblk;
960 	}
961 
962 	newblk = ablks[--a];
963 	bp = getblk(ip->i_devvp, fsbtodb(fs, newblk), fs->e2fs_bsize, 0, 0, 0);
964 	if (!bp) {
965 		error = EIO;
966 		goto cleanup;
967 	}
968 
969 	neh = ext4_ext_block_header(bp->b_data);
970 	neh->eh_ecount = 0;
971 	neh->eh_max = le16toh(ext4_ext_space_block(ip));
972 	neh->eh_magic = le16toh(EXT4_EXT_MAGIC);
973 	neh->eh_depth = 0;
974 	ex = EXT_FIRST_EXTENT(neh);
975 
976 	if (le16toh(path[depth].ep_header->eh_ecount) !=
977 	    le16toh(path[depth].ep_header->eh_max)) {
978 		SDT_PROBE2(ext2fs, , trace, extents, 1,
979 		    "extents count out of range => extent corrupted");
980 		error = EIO;
981 		goto cleanup;
982 	}
983 
984 	/* Start copy from next extent. */
985 	m = 0;
986 	path[depth].ep_ext++;
987 	while (path[depth].ep_ext <= EXT_MAX_EXTENT(path[depth].ep_header)) {
988 		path[depth].ep_ext++;
989 		m++;
990 	}
991 	if (m) {
992 		memmove(ex, path[depth].ep_ext - m,
993 		    sizeof(struct ext4_extent) * m);
994 		neh->eh_ecount = htole16(le16toh(neh->eh_ecount) + m);
995 	}
996 
997 	ext2_extent_blk_csum_set(ip, bp->b_data);
998 	bwrite(bp);
999 	bp = NULL;
1000 
1001 	/* Fix old leaf. */
1002 	if (m) {
1003 		path[depth].ep_header->eh_ecount =
1004 		    htole16(le16toh(path[depth].ep_header->eh_ecount) - m);
1005 		ext4_ext_dirty(ip, path + depth);
1006 	}
1007 
1008 	/* Create intermediate indexes. */
1009 	k = depth - at - 1;
1010 	KASSERT(k >= 0, ("ext4_ext_split: negative k"));
1011 
1012 	/* Insert new index into current index block. */
1013 	i = depth - 1;
1014 	while (k--) {
1015 		oldblk = newblk;
1016 		newblk = ablks[--a];
1017 		error = bread(ip->i_devvp, fsbtodb(fs, newblk),
1018 		    (int)fs->e2fs_bsize, NOCRED, &bp);
1019 		if (error) {
1020 			goto cleanup;
1021 		}
1022 
1023 		neh = (struct ext4_extent_header *)bp->b_data;
1024 		neh->eh_ecount = htole16(1);
1025 		neh->eh_magic = htole16(EXT4_EXT_MAGIC);
1026 		neh->eh_max = htole16(ext4_ext_space_block_index(ip));
1027 		neh->eh_depth = htole16(depth - i);
1028 		fidx = EXT_FIRST_INDEX(neh);
1029 		fidx->ei_blk = htole32(border);
1030 		ext4_index_store_pblock(fidx, oldblk);
1031 
1032 		m = 0;
1033 		path[i].ep_index++;
1034 		while (path[i].ep_index <= EXT_MAX_INDEX(path[i].ep_header)) {
1035 			path[i].ep_index++;
1036 			m++;
1037 		}
1038 		if (m) {
1039 			memmove(++fidx, path[i].ep_index - m,
1040 			    sizeof(struct ext4_extent_index) * m);
1041 			neh->eh_ecount = htole16(le16toh(neh->eh_ecount) + m);
1042 		}
1043 
1044 		ext2_extent_blk_csum_set(ip, bp->b_data);
1045 		bwrite(bp);
1046 		bp = NULL;
1047 
1048 		/* Fix old index. */
1049 		if (m) {
1050 			path[i].ep_header->eh_ecount =
1051 			    htole16(le16toh(path[i].ep_header->eh_ecount) - m);
1052 			ext4_ext_dirty(ip, path + i);
1053 		}
1054 
1055 		i--;
1056 	}
1057 
1058 	error = ext4_ext_insert_index(ip, path + at, border, newblk);
1059 
1060 cleanup:
1061 	if (bp)
1062 		brelse(bp);
1063 
1064 	if (error) {
1065 		for (i = 0; i < depth; i++) {
1066 			if (!ablks[i])
1067 				continue;
1068 			ext4_ext_blkfree(ip, ablks[i], 1, 0);
1069 		}
1070 	}
1071 
1072 	free(ablks, M_EXT2EXTENTS);
1073 
1074 	return (error);
1075 }
1076 
1077 static int
ext4_ext_grow_indepth(struct inode * ip,struct ext4_extent_path * path,struct ext4_extent * newext)1078 ext4_ext_grow_indepth(struct inode *ip, struct ext4_extent_path *path,
1079     struct ext4_extent *newext)
1080 {
1081 	struct m_ext2fs *fs;
1082 	struct ext4_extent_path *curpath;
1083 	struct ext4_extent_header *neh;
1084 	struct buf *bp;
1085 	e4fs_daddr_t newblk;
1086 	int error = 0;
1087 
1088 	fs = ip->i_e2fs;
1089 	curpath = path;
1090 
1091 	newblk = ext4_ext_alloc_meta(ip);
1092 	if (newblk == 0)
1093 		return (error);
1094 
1095 	bp = getblk(ip->i_devvp, fsbtodb(fs, newblk), fs->e2fs_bsize, 0, 0, 0);
1096 	if (!bp) {
1097 		ext4_ext_blkfree(ip, newblk, 1, 0);
1098 		return (EIO);
1099 	}
1100 
1101 	/* Move top-level index/leaf into new block. */
1102 	memmove(bp->b_data, curpath->ep_header, sizeof(ip->i_data));
1103 
1104 	/* Set size of new block */
1105 	neh = ext4_ext_block_header(bp->b_data);
1106 	neh->eh_magic = htole16(EXT4_EXT_MAGIC);
1107 
1108 	if (ext4_ext_inode_depth(ip))
1109 		neh->eh_max = htole16(ext4_ext_space_block_index(ip));
1110 	else
1111 		neh->eh_max = htole16(ext4_ext_space_block(ip));
1112 
1113 	ext2_extent_blk_csum_set(ip, bp->b_data);
1114 	error = bwrite(bp);
1115 	if (error) {
1116 		ext4_ext_blkfree(ip, newblk, 1, 0);
1117 		goto out;
1118 	}
1119 
1120 	bp = NULL;
1121 
1122 	curpath->ep_header->eh_magic = htole16(EXT4_EXT_MAGIC);
1123 	curpath->ep_header->eh_max = htole16(ext4_ext_space_root(ip));
1124 	curpath->ep_header->eh_ecount = htole16(1);
1125 	curpath->ep_index = EXT_FIRST_INDEX(curpath->ep_header);
1126 	curpath->ep_index->ei_blk = EXT_FIRST_EXTENT(path[0].ep_header)->e_blk;
1127 	ext4_index_store_pblock(curpath->ep_index, newblk);
1128 
1129 	neh = ext4_ext_inode_header(ip);
1130 	neh->eh_depth = htole16(path->ep_depth + 1);
1131 	ext4_ext_dirty(ip, curpath);
1132 out:
1133 	brelse(bp);
1134 
1135 	return (error);
1136 }
1137 
1138 static int
ext4_ext_create_new_leaf(struct inode * ip,struct ext4_extent_path * path,struct ext4_extent * newext)1139 ext4_ext_create_new_leaf(struct inode *ip, struct ext4_extent_path *path,
1140     struct ext4_extent *newext)
1141 {
1142 	struct ext4_extent_path *curpath;
1143 	int depth, i, error;
1144 
1145 repeat:
1146 	i = depth = ext4_ext_inode_depth(ip);
1147 
1148 	/* Look for free index entry int the tree */
1149 	curpath = path + depth;
1150 	while (i > 0 && !EXT_HAS_FREE_INDEX(curpath)) {
1151 		i--;
1152 		curpath--;
1153 	}
1154 
1155 	/*
1156 	 * We use already allocated block for index block,
1157 	 * so subsequent data blocks should be contiguous.
1158 	 */
1159 	if (EXT_HAS_FREE_INDEX(curpath)) {
1160 		error = ext4_ext_split(ip, path, newext, i);
1161 		if (error)
1162 			goto out;
1163 
1164 		/* Refill path. */
1165 		ext4_ext_drop_refs(path);
1166 		error = ext4_ext_find_extent(ip, le32toh(newext->e_blk), &path);
1167 		if (error)
1168 			goto out;
1169 	} else {
1170 		/* Tree is full, do grow in depth. */
1171 		error = ext4_ext_grow_indepth(ip, path, newext);
1172 		if (error)
1173 			goto out;
1174 
1175 		/* Refill path. */
1176 		ext4_ext_drop_refs(path);
1177 		error = ext4_ext_find_extent(ip, le32toh(newext->e_blk), &path);
1178 		if (error)
1179 			goto out;
1180 
1181 		/* Check and split tree if required. */
1182 		depth = ext4_ext_inode_depth(ip);
1183 		if (le16toh(path[depth].ep_header->eh_ecount) ==
1184 		    le16toh(path[depth].ep_header->eh_max))
1185 			goto repeat;
1186 	}
1187 
1188 out:
1189 	return (error);
1190 }
1191 
1192 static int
ext4_ext_correct_indexes(struct inode * ip,struct ext4_extent_path * path)1193 ext4_ext_correct_indexes(struct inode *ip, struct ext4_extent_path *path)
1194 {
1195 	struct ext4_extent_header *eh;
1196 	struct ext4_extent *ex;
1197 	int32_t border;
1198 	int depth, k;
1199 
1200 	depth = ext4_ext_inode_depth(ip);
1201 	eh = path[depth].ep_header;
1202 	ex = path[depth].ep_ext;
1203 
1204 	if (ex == NULL || eh == NULL)
1205 		return (EIO);
1206 
1207 	if (!depth)
1208 		return (0);
1209 
1210 	/* We will correct tree if first leaf got modified only. */
1211 	if (ex != EXT_FIRST_EXTENT(eh))
1212 		return (0);
1213 
1214 	k = depth - 1;
1215 	border = le32toh(path[depth].ep_ext->e_blk);
1216 	path[k].ep_index->ei_blk = htole32(border);
1217 	ext4_ext_dirty(ip, path + k);
1218 	while (k--) {
1219 		/* Change all left-side indexes. */
1220 		if (path[k+1].ep_index != EXT_FIRST_INDEX(path[k+1].ep_header))
1221 			break;
1222 
1223 		path[k].ep_index->ei_blk = htole32(border);
1224 		ext4_ext_dirty(ip, path + k);
1225 	}
1226 
1227 	return (0);
1228 }
1229 
1230 static int
ext4_ext_insert_extent(struct inode * ip,struct ext4_extent_path * path,struct ext4_extent * newext)1231 ext4_ext_insert_extent(struct inode *ip, struct ext4_extent_path *path,
1232     struct ext4_extent *newext)
1233 {
1234 	struct ext4_extent_header * eh;
1235 	struct ext4_extent *ex, *nex, *nearex;
1236 	struct ext4_extent_path *npath;
1237 	int depth, len, error, next;
1238 
1239 	depth = ext4_ext_inode_depth(ip);
1240 	ex = path[depth].ep_ext;
1241 	npath = NULL;
1242 
1243 	if (htole16(newext->e_len) == 0 || path[depth].ep_header == NULL)
1244 		return (EINVAL);
1245 
1246 	/* Insert block into found extent. */
1247 	if (ex && ext4_can_extents_be_merged(ex, newext)) {
1248 		ex->e_len = htole16(le16toh(ex->e_len) + le16toh(newext->e_len));
1249 		eh = path[depth].ep_header;
1250 		nearex = ex;
1251 		goto merge;
1252 	}
1253 
1254 repeat:
1255 	depth = ext4_ext_inode_depth(ip);
1256 	eh = path[depth].ep_header;
1257 	if (le16toh(eh->eh_ecount) < le16toh(eh->eh_max))
1258 		goto has_space;
1259 
1260 	/* Try next leaf */
1261 	nex = EXT_LAST_EXTENT(eh);
1262 	next = ext4_ext_next_leaf_block(ip, path);
1263 	if (le32toh(newext->e_blk) > le32toh(nex->e_blk) && next !=
1264 	    EXT4_MAX_BLOCKS) {
1265 		KASSERT(npath == NULL,
1266 		    ("ext4_ext_insert_extent: bad path"));
1267 
1268 		error = ext4_ext_find_extent(ip, next, &npath);
1269 		if (error)
1270 			goto cleanup;
1271 
1272 		if (npath->ep_depth != path->ep_depth) {
1273 			error = EIO;
1274 			goto cleanup;
1275 		}
1276 
1277 		eh = npath[depth].ep_header;
1278 		if (le16toh(eh->eh_ecount) < le16toh(eh->eh_max)) {
1279 			path = npath;
1280 			goto repeat;
1281 		}
1282 	}
1283 
1284 	/*
1285 	 * There is no free space in the found leaf,
1286 	 * try to add a new leaf to the tree.
1287 	 */
1288 	error = ext4_ext_create_new_leaf(ip, path, newext);
1289 	if (error)
1290 		goto cleanup;
1291 
1292 	depth = ext4_ext_inode_depth(ip);
1293 	eh = path[depth].ep_header;
1294 
1295 has_space:
1296 	nearex = path[depth].ep_ext;
1297 	if (!nearex) {
1298 		/* Create new extent in the leaf. */
1299 		path[depth].ep_ext = EXT_FIRST_EXTENT(eh);
1300 	} else if (le32toh(newext->e_blk) > le32toh(nearex->e_blk)) {
1301 		if (nearex != EXT_LAST_EXTENT(eh)) {
1302 			len = EXT_MAX_EXTENT(eh) - nearex;
1303 			len = (len - 1) * sizeof(struct ext4_extent);
1304 			len = len < 0 ? 0 : len;
1305 			memmove(nearex + 2, nearex + 1, len);
1306 		}
1307 		path[depth].ep_ext = nearex + 1;
1308 	} else {
1309 		len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1310 		len = len < 0 ? 0 : len;
1311 		memmove(nearex + 1, nearex, len);
1312 		path[depth].ep_ext = nearex;
1313 	}
1314 
1315 	eh->eh_ecount = htole16(le16toh(eh->eh_ecount) + 1);
1316 	nearex = path[depth].ep_ext;
1317 	nearex->e_blk = newext->e_blk;
1318 	nearex->e_start_lo = newext->e_start_lo;
1319 	nearex->e_start_hi = newext->e_start_hi;
1320 	nearex->e_len = newext->e_len;
1321 
1322 merge:
1323 	/* Try to merge extents to the right. */
1324 	while (nearex < EXT_LAST_EXTENT(eh)) {
1325 		if (!ext4_can_extents_be_merged(nearex, nearex + 1))
1326 			break;
1327 
1328 		/* Merge with next extent. */
1329 		nearex->e_len = htole16(le16toh(nearex->e_len) +
1330 		    le16toh(nearex[1].e_len));
1331 		if (nearex + 1 < EXT_LAST_EXTENT(eh)) {
1332 			len = (EXT_LAST_EXTENT(eh) - nearex - 1) *
1333 			    sizeof(struct ext4_extent);
1334 			memmove(nearex + 1, nearex + 2, len);
1335 		}
1336 
1337 		eh->eh_ecount = htole16(le16toh(eh->eh_ecount) - 1);
1338 		KASSERT(le16toh(eh->eh_ecount) != 0,
1339 		    ("ext4_ext_insert_extent: bad ecount"));
1340 	}
1341 
1342 	/*
1343 	 * Try to merge extents to the left,
1344 	 * start from inexes correction.
1345 	 */
1346 	error = ext4_ext_correct_indexes(ip, path);
1347 	if (error)
1348 		goto cleanup;
1349 
1350 	ext4_ext_dirty(ip, path + depth);
1351 
1352 cleanup:
1353 	if (npath) {
1354 		ext4_ext_drop_refs(npath);
1355 		free(npath, M_EXT2EXTENTS);
1356 	}
1357 
1358 	ip->i_ext_cache.ec_type = EXT4_EXT_CACHE_NO;
1359 	return (error);
1360 }
1361 
1362 static e4fs_daddr_t
ext4_new_blocks(struct inode * ip,daddr_t lbn,e4fs_daddr_t pref,struct ucred * cred,unsigned long * count,int * perror)1363 ext4_new_blocks(struct inode *ip, daddr_t lbn, e4fs_daddr_t pref,
1364     struct ucred *cred, unsigned long *count, int *perror)
1365 {
1366 	struct m_ext2fs *fs;
1367 	e4fs_daddr_t newblk;
1368 
1369 	/*
1370 	 * We will allocate only single block for now.
1371 	 */
1372 	if (*count > 1)
1373 		return (0);
1374 
1375 	fs = ip->i_e2fs;
1376 	EXT2_LOCK(ip->i_ump);
1377 	*perror = ext2_alloc(ip, lbn, pref, (int)fs->e2fs_bsize, cred, &newblk);
1378 	if (*perror)
1379 		return (0);
1380 
1381 	if (newblk) {
1382 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
1383 		ext2_update(ip->i_vnode, 1);
1384 	}
1385 
1386 	return (newblk);
1387 }
1388 
1389 int
ext4_ext_get_blocks(struct inode * ip,e4fs_daddr_t iblk,unsigned long max_blocks,struct ucred * cred,struct buf ** bpp,int * pallocated,daddr_t * nb)1390 ext4_ext_get_blocks(struct inode *ip, e4fs_daddr_t iblk,
1391     unsigned long max_blocks, struct ucred *cred, struct buf **bpp,
1392     int *pallocated, daddr_t *nb)
1393 {
1394 	struct m_ext2fs *fs;
1395 	struct buf *bp = NULL;
1396 	struct ext4_extent_path *path;
1397 	struct ext4_extent newex, *ex;
1398 	e4fs_daddr_t bpref, newblk = 0;
1399 	unsigned long allocated = 0;
1400 	int error = 0, depth;
1401 
1402 	if(bpp)
1403 		*bpp = NULL;
1404 	*pallocated = 0;
1405 
1406 	/* Check cache. */
1407 	path = NULL;
1408 	if ((bpref = ext4_ext_in_cache(ip, iblk, &newex))) {
1409 		if (bpref == EXT4_EXT_CACHE_IN) {
1410 			/* Block is already allocated. */
1411 			newblk = iblk - le32toh(newex.e_blk) +
1412 			    ext4_ext_extent_pblock(&newex);
1413 			allocated = le16toh(newex.e_len) - (iblk - le32toh(newex.e_blk));
1414 			goto out;
1415 		} else {
1416 			error = EIO;
1417 			goto out2;
1418 		}
1419 	}
1420 
1421 	error = ext4_ext_find_extent(ip, iblk, &path);
1422 	if (error) {
1423 		goto out2;
1424 	}
1425 
1426 	depth = ext4_ext_inode_depth(ip);
1427 	if (path[depth].ep_ext == NULL && depth != 0) {
1428 		error = EIO;
1429 		goto out2;
1430 	}
1431 
1432 	if ((ex = path[depth].ep_ext)) {
1433 		uint64_t lblk = le32toh(ex->e_blk);
1434 		uint16_t e_len  = le16toh(ex->e_len);
1435 		e4fs_daddr_t e_start = ext4_ext_extent_pblock(ex);
1436 
1437 		if (e_len > EXT4_MAX_LEN)
1438 			goto out2;
1439 
1440 		/* If we found extent covers block, simply return it. */
1441 		if (iblk >= lblk && iblk < lblk + e_len) {
1442 			newblk = iblk - lblk + e_start;
1443 			allocated = e_len - (iblk - lblk);
1444 			ext4_ext_put_in_cache(ip, lblk, e_len,
1445 			    e_start, EXT4_EXT_CACHE_IN);
1446 			goto out;
1447 		}
1448 	}
1449 
1450 	/* Allocate the new block. */
1451 	if (S_ISREG(ip->i_mode) && (!ip->i_next_alloc_block)) {
1452 		ip->i_next_alloc_goal = 0;
1453 	}
1454 
1455 	bpref = ext4_ext_blkpref(ip, path, iblk);
1456 	allocated = max_blocks;
1457 	newblk = ext4_new_blocks(ip, iblk, bpref, cred, &allocated, &error);
1458 	if (!newblk)
1459 		goto out2;
1460 
1461 	/* Try to insert new extent into found leaf and return. */
1462 	newex.e_blk = htole32(iblk);
1463 	ext4_ext_store_pblock(&newex, newblk);
1464 	newex.e_len = htole16(allocated);
1465 	error = ext4_ext_insert_extent(ip, path, &newex);
1466 	if (error)
1467 		goto out2;
1468 
1469 	newblk = ext4_ext_extent_pblock(&newex);
1470 	ext4_ext_put_in_cache(ip, iblk, allocated, newblk, EXT4_EXT_CACHE_IN);
1471 	*pallocated = 1;
1472 
1473 out:
1474 	if (allocated > max_blocks)
1475 		allocated = max_blocks;
1476 
1477 	if (bpp)
1478 	{
1479 		fs = ip->i_e2fs;
1480 		error = bread(ip->i_devvp, fsbtodb(fs, newblk),
1481 		    fs->e2fs_bsize, cred, &bp);
1482 		if (error) {
1483 			brelse(bp);
1484 		} else {
1485 			*bpp = bp;
1486 		}
1487 	}
1488 
1489 out2:
1490 	if (path) {
1491 		ext4_ext_drop_refs(path);
1492 		free(path, M_EXT2EXTENTS);
1493 	}
1494 
1495 	if (nb)
1496 		*nb = newblk;
1497 
1498 	return (error);
1499 }
1500 
1501 static inline struct ext4_extent_header *
ext4_ext_header(struct inode * ip)1502 ext4_ext_header(struct inode *ip)
1503 {
1504 
1505 	return ((struct ext4_extent_header *)ip->i_db);
1506 }
1507 
1508 static int
ext4_remove_blocks(struct inode * ip,struct ext4_extent * ex,unsigned long from,unsigned long to)1509 ext4_remove_blocks(struct inode *ip, struct ext4_extent *ex,
1510     unsigned long from, unsigned long to)
1511 {
1512 	unsigned long num, start;
1513 
1514 	if (from >= le32toh(ex->e_blk) &&
1515 	    to == le32toh(ex->e_blk) + ext4_ext_get_actual_len(ex) - 1) {
1516 		/* Tail cleanup. */
1517 		num = le32toh(ex->e_blk) + ext4_ext_get_actual_len(ex) - from;
1518 		start = ext4_ext_extent_pblock(ex) +
1519 		    ext4_ext_get_actual_len(ex) - num;
1520 		ext4_ext_blkfree(ip, start, num, 0);
1521 	}
1522 
1523 	return (0);
1524 }
1525 
1526 static int
ext4_ext_rm_index(struct inode * ip,struct ext4_extent_path * path)1527 ext4_ext_rm_index(struct inode *ip, struct ext4_extent_path *path)
1528 {
1529 	e4fs_daddr_t leaf;
1530 
1531 	/* Free index block. */
1532 	path--;
1533 	leaf = ext4_ext_index_pblock(path->ep_index);
1534 	KASSERT(path->ep_header->eh_ecount != 0,
1535 	    ("ext4_ext_rm_index: bad ecount"));
1536 	path->ep_header->eh_ecount =
1537 	    htole16(le16toh(path->ep_header->eh_ecount) - 1);
1538 	ext4_ext_dirty(ip, path);
1539 	ext4_ext_blkfree(ip, leaf, 1, 0);
1540 	return (0);
1541 }
1542 
1543 static int
ext4_ext_rm_leaf(struct inode * ip,struct ext4_extent_path * path,uint64_t start)1544 ext4_ext_rm_leaf(struct inode *ip, struct ext4_extent_path *path,
1545     uint64_t start)
1546 {
1547 	struct ext4_extent_header *eh;
1548 	struct ext4_extent *ex;
1549 	unsigned int a, b, block, num;
1550 	unsigned long ex_blk;
1551 	unsigned short ex_len;
1552 	int depth;
1553 	int error, correct_index;
1554 
1555 	depth = ext4_ext_inode_depth(ip);
1556 	if (!path[depth].ep_header) {
1557 		if (path[depth].ep_data == NULL)
1558 			return (EINVAL);
1559 		path[depth].ep_header =
1560 		    (struct ext4_extent_header* )path[depth].ep_data;
1561 	}
1562 
1563 	eh = path[depth].ep_header;
1564 	if (!eh) {
1565 		SDT_PROBE2(ext2fs, , trace, extents, 1,
1566 		    "bad header => extent corrupted");
1567 		return (EIO);
1568 	}
1569 
1570 	ex = EXT_LAST_EXTENT(eh);
1571 	ex_blk = le32toh(ex->e_blk);
1572 	ex_len = ext4_ext_get_actual_len(ex);
1573 
1574 	error = 0;
1575 	correct_index = 0;
1576 	while (ex >= EXT_FIRST_EXTENT(eh) && ex_blk + ex_len > start) {
1577 		path[depth].ep_ext = ex;
1578 		a = ex_blk > start ? ex_blk : start;
1579 		b = (uint64_t)ex_blk + ex_len - 1 <
1580 		    EXT4_MAX_BLOCKS ? ex_blk + ex_len - 1 : EXT4_MAX_BLOCKS;
1581 
1582 		if (a != ex_blk && b != ex_blk + ex_len - 1)
1583 			return (EINVAL);
1584 		else if (a != ex_blk) {
1585 			/* Remove tail of the extent. */
1586 			block = ex_blk;
1587 			num = a - block;
1588 		} else if (b != ex_blk + ex_len - 1) {
1589 			/* Remove head of the extent, not implemented. */
1590 			return (EINVAL);
1591 		} else {
1592 			/* Remove whole extent. */
1593 			block = ex_blk;
1594 			num = 0;
1595 		}
1596 
1597 		if (ex == EXT_FIRST_EXTENT(eh))
1598 			correct_index = 1;
1599 
1600 		error = ext4_remove_blocks(ip, ex, a, b);
1601 		if (error)
1602 			goto out;
1603 
1604 		if (num == 0) {
1605 			ext4_ext_store_pblock(ex, 0);
1606 			eh->eh_ecount = htole16(le16toh(eh->eh_ecount) - 1);
1607 		}
1608 
1609 		ex->e_blk = htole32(block);
1610 		ex->e_len = htole16(num);
1611 
1612 		ext4_ext_dirty(ip, path + depth);
1613 
1614 		ex--;
1615 		ex_blk = htole32(ex->e_blk);
1616 		ex_len = ext4_ext_get_actual_len(ex);
1617 	};
1618 
1619 	if (correct_index && le16toh(eh->eh_ecount))
1620 		error = ext4_ext_correct_indexes(ip, path);
1621 
1622 	/*
1623 	 * If this leaf is free, we should
1624 	 * remove it from index block above.
1625 	 */
1626 	if (error == 0 && eh->eh_ecount == 0 &&
1627 	    path[depth].ep_data != NULL)
1628 		error = ext4_ext_rm_index(ip, path + depth);
1629 
1630 out:
1631 	return (error);
1632 }
1633 
1634 static struct buf *
ext4_read_extent_tree_block(struct inode * ip,e4fs_daddr_t pblk,int depth,int flags)1635 ext4_read_extent_tree_block(struct inode *ip, e4fs_daddr_t pblk,
1636     int depth, int flags)
1637 {
1638 	struct m_ext2fs *fs;
1639 	struct ext4_extent_header *eh;
1640 	struct buf *bp;
1641 	int error;
1642 
1643 	fs = ip->i_e2fs;
1644 	error = bread(ip->i_devvp, fsbtodb(fs, pblk),
1645 	    fs->e2fs_bsize, NOCRED, &bp);
1646 	if (error) {
1647 		return (NULL);
1648 	}
1649 
1650 	eh = ext4_ext_block_header(bp->b_data);
1651 	if (le16toh(eh->eh_depth) != depth) {
1652 		SDT_PROBE2(ext2fs, , trace, extents, 1,
1653 		    "unexpected eh_depth");
1654 		goto err;
1655 	}
1656 
1657 	error = ext4_ext_check_header(ip, eh, depth);
1658 	if (error)
1659 		goto err;
1660 
1661 	return (bp);
1662 
1663 err:
1664 	brelse(bp);
1665 	return (NULL);
1666 
1667 }
1668 
1669 static int inline
ext4_ext_more_to_rm(struct ext4_extent_path * path)1670 ext4_ext_more_to_rm(struct ext4_extent_path *path)
1671 {
1672 
1673 	KASSERT(path->ep_index != NULL,
1674 	    ("ext4_ext_more_to_rm: bad index from path"));
1675 
1676 	if (path->ep_index < EXT_FIRST_INDEX(path->ep_header))
1677 		return (0);
1678 
1679 	if (le16toh(path->ep_header->eh_ecount) == path->index_count)
1680 		return (0);
1681 
1682 	return (1);
1683 }
1684 
1685 int
ext4_ext_remove_space(struct inode * ip,off_t length,int flags,struct ucred * cred,struct thread * td)1686 ext4_ext_remove_space(struct inode *ip, off_t length, int flags,
1687     struct ucred *cred, struct thread *td)
1688 {
1689 	struct buf *bp;
1690 	struct ext4_extent_header *ehp;
1691 	struct ext4_extent_path *path;
1692 	int depth;
1693 	int i, error;
1694 
1695 	ehp = (struct ext4_extent_header *)ip->i_db;
1696 	depth = ext4_ext_inode_depth(ip);
1697 
1698 	error = ext4_ext_check_header(ip, ehp, depth);
1699 	if(error)
1700 		return (error);
1701 
1702 	path = malloc(sizeof(struct ext4_extent_path) * (depth + 1),
1703 	    M_EXT2EXTENTS, M_WAITOK | M_ZERO);
1704 	path[0].ep_header = ehp;
1705 	path[0].ep_depth = depth;
1706 	i = 0;
1707 	while (error == 0 && i >= 0) {
1708 		if (i == depth) {
1709 			/* This is leaf. */
1710 			error = ext4_ext_rm_leaf(ip, path, length);
1711 			if (error)
1712 				break;
1713 			free(path[i].ep_data, M_EXT2EXTENTS);
1714 			path[i].ep_data = NULL;
1715 			i--;
1716 			continue;
1717 		}
1718 
1719 		/* This is index. */
1720 		if (!path[i].ep_header)
1721 			path[i].ep_header =
1722 			    (struct ext4_extent_header *)path[i].ep_data;
1723 
1724 		if (!path[i].ep_index) {
1725 			/* This level hasn't touched yet. */
1726 			path[i].ep_index = EXT_LAST_INDEX(path[i].ep_header);
1727 			path[i].index_count =
1728 			    le16toh(path[i].ep_header->eh_ecount) + 1;
1729 		} else {
1730 			/* We've already was here, see at next index. */
1731 			path[i].ep_index--;
1732 		}
1733 
1734 		if (ext4_ext_more_to_rm(path + i)) {
1735 			memset(path + i + 1, 0, sizeof(*path));
1736 			bp = ext4_read_extent_tree_block(ip,
1737 			    ext4_ext_index_pblock(path[i].ep_index),
1738 			    path[0].ep_depth - (i + 1), 0);
1739 			if (!bp) {
1740 				error = EIO;
1741 				break;
1742 			}
1743 
1744 			ext4_ext_fill_path_bdata(&path[i+1], bp,
1745 			    ext4_ext_index_pblock(path[i].ep_index));
1746 			brelse(bp);
1747 			path[i].index_count =
1748 			    le16toh(path[i].ep_header->eh_ecount);
1749 			i++;
1750 		} else {
1751 			if (path[i].ep_header->eh_ecount == 0 && i > 0) {
1752 				/* Index is empty, remove it. */
1753 				error = ext4_ext_rm_index(ip, path + i);
1754 			}
1755 			free(path[i].ep_data, M_EXT2EXTENTS);
1756 			path[i].ep_data = NULL;
1757 			i--;
1758 		}
1759 	}
1760 
1761 	if (path->ep_header->eh_ecount == 0) {
1762 		/*
1763 		 * Truncate the tree to zero.
1764 		 */
1765 		 ext4_ext_header(ip)->eh_depth = 0;
1766 		 ext4_ext_header(ip)->eh_max = htole16(ext4_ext_space_root(ip));
1767 		 ext4_ext_dirty(ip, path);
1768 	}
1769 
1770 	ext4_ext_drop_refs(path);
1771 	free(path, M_EXT2EXTENTS);
1772 
1773 	ip->i_ext_cache.ec_type = EXT4_EXT_CACHE_NO;
1774 	return (error);
1775 }
1776