• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * extent.c --- routines to implement extents support
3  *
4  * Copyright (C) 2007 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Library
8  * General Public License, version 2.
9  * %End-Header%
10  */
11 
12 #include "config.h"
13 #include <stdio.h>
14 #include <string.h>
15 #if HAVE_UNISTD_H
16 #include <unistd.h>
17 #endif
18 #if HAVE_ERRNO_H
19 #include <errno.h>
20 #endif
21 #if HAVE_SYS_STAT_H
22 #include <sys/stat.h>
23 #endif
24 #if HAVE_SYS_TYPES_H
25 #include <sys/types.h>
26 #endif
27 
28 #include "ext2_fs.h"
29 #include "ext2fsP.h"
30 #include "e2image.h"
31 
32 #undef DEBUG
33 
34 /*
35  * Definitions to be dropped in lib/ext2fs/ext2fs.h
36  */
37 
38 /*
39  * Private definitions
40  */
41 
42 struct extent_path {
43 	char		*buf;
44 	int		entries;
45 	int		max_entries;
46 	int		left;
47 	int		visit_num;
48 	int		flags;
49 	blk64_t		end_blk;
50 	void		*curr;
51 };
52 
53 
54 struct ext2_extent_handle {
55 	errcode_t		magic;
56 	ext2_filsys		fs;
57 	ext2_ino_t 		ino;
58 	struct ext2_inode	*inode;
59 	struct ext2_inode	inodebuf;
60 	int			type;
61 	int			level;
62 	int			max_depth;
63 	int			max_paths;
64 	struct extent_path	*path;
65 };
66 
67 struct ext2_extent_path {
68 	errcode_t		magic;
69 	int			leaf_height;
70 	blk64_t			lblk;
71 };
72 
73 /*
74  *  Useful Debugging stuff
75  */
76 
77 #ifdef DEBUG
dbg_show_header(struct ext3_extent_header * eh)78 static void dbg_show_header(struct ext3_extent_header *eh)
79 {
80 	printf("header: magic=%x entries=%u max=%u depth=%u generation=%u\n",
81 			ext2fs_le16_to_cpu(eh->eh_magic),
82 			ext2fs_le16_to_cpu(eh->eh_entries),
83 			ext2fs_le16_to_cpu(eh->eh_max),
84 			ext2fs_le16_to_cpu(eh->eh_depth),
85 			ext2fs_le32_to_cpu(eh->eh_generation));
86 }
87 
dbg_show_index(struct ext3_extent_idx * ix)88 static void dbg_show_index(struct ext3_extent_idx *ix)
89 {
90 	printf("index: block=%u leaf=%u leaf_hi=%u unused=%u\n",
91 			ext2fs_le32_to_cpu(ix->ei_block),
92 			ext2fs_le32_to_cpu(ix->ei_leaf),
93 			ext2fs_le16_to_cpu(ix->ei_leaf_hi),
94 			ext2fs_le16_to_cpu(ix->ei_unused));
95 }
96 
dbg_show_extent(struct ext3_extent * ex)97 static void dbg_show_extent(struct ext3_extent *ex)
98 {
99 	printf("extent: block=%u-%u len=%u start=%u start_hi=%u\n",
100 			ext2fs_le32_to_cpu(ex->ee_block),
101 			ext2fs_le32_to_cpu(ex->ee_block) +
102 			ext2fs_le16_to_cpu(ex->ee_len) - 1,
103 			ext2fs_le16_to_cpu(ex->ee_len),
104 			ext2fs_le32_to_cpu(ex->ee_start),
105 			ext2fs_le16_to_cpu(ex->ee_start_hi));
106 }
107 
dbg_print_extent(char * desc,struct ext2fs_extent * extent)108 static void dbg_print_extent(char *desc, struct ext2fs_extent *extent)
109 {
110 	if (desc)
111 		printf("%s: ", desc);
112 	printf("extent: lblk %llu--%llu, len %u, pblk %llu, flags: ",
113 	       extent->e_lblk, extent->e_lblk + extent->e_len - 1,
114 	       extent->e_len, extent->e_pblk);
115 	if (extent->e_flags & EXT2_EXTENT_FLAGS_LEAF)
116 		fputs("LEAF ", stdout);
117 	if (extent->e_flags & EXT2_EXTENT_FLAGS_UNINIT)
118 		fputs("UNINIT ", stdout);
119 	if (extent->e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
120 		fputs("2ND_VISIT ", stdout);
121 	if (!extent->e_flags)
122 		fputs("(none)", stdout);
123 	fputc('\n', stdout);
124 
125 }
126 
dump_path(const char * tag,struct ext2_extent_handle * handle,struct extent_path * path)127 static void dump_path(const char *tag, struct ext2_extent_handle *handle,
128 		      struct extent_path *path)
129 {
130 	struct extent_path *ppp = path;
131 	printf("%s: level=%d\n", tag, handle->level);
132 
133 	do {
134 		printf("%s: path=%ld buf=%p entries=%d max_entries=%d left=%d "
135 		       "visit_num=%d flags=0x%x end_blk=%llu curr=%p(%ld)\n",
136 		       tag, (ppp - handle->path), ppp->buf, ppp->entries,
137 		       ppp->max_entries, ppp->left, ppp->visit_num, ppp->flags,
138 		       ppp->end_blk, ppp->curr, ppp->curr - (void *)ppp->buf);
139 		printf("  ");
140 		dbg_show_header((struct ext3_extent_header *)ppp->buf);
141 		if (ppp->curr) {
142 			printf("  ");
143 			dbg_show_index(ppp->curr);
144 			printf("  ");
145 			dbg_show_extent(ppp->curr);
146 		}
147 		ppp--;
148 	} while (ppp >= handle->path);
149 	fflush(stdout);
150 
151 	return;
152 }
153 
154 #else
155 #define dbg_show_header(eh) do { } while (0)
156 #define dbg_show_index(ix) do { } while (0)
157 #define dbg_show_extent(ex) do { } while (0)
158 #define dbg_print_extent(desc, ex) do { } while (0)
159 #define dump_path(tag, handle, path) do { } while (0)
160 #endif
161 
162 /*
163  * Verify the extent header as being sane
164  */
ext2fs_extent_header_verify(void * ptr,int size)165 errcode_t ext2fs_extent_header_verify(void *ptr, int size)
166 {
167 	int eh_max, entry_size;
168 	struct ext3_extent_header *eh = ptr;
169 
170 	dbg_show_header(eh);
171 	if (ext2fs_le16_to_cpu(eh->eh_magic) != EXT3_EXT_MAGIC)
172 		return EXT2_ET_EXTENT_HEADER_BAD;
173 	if (ext2fs_le16_to_cpu(eh->eh_entries) > ext2fs_le16_to_cpu(eh->eh_max))
174 		return EXT2_ET_EXTENT_HEADER_BAD;
175 	if (eh->eh_depth == 0)
176 		entry_size = sizeof(struct ext3_extent);
177 	else
178 		entry_size = sizeof(struct ext3_extent_idx);
179 
180 	eh_max = (size - sizeof(*eh)) / entry_size;
181 	/* Allow two extent-sized items at the end of the block, for
182 	 * ext4_extent_tail with checksum in the future. */
183 	if ((ext2fs_le16_to_cpu(eh->eh_max) > eh_max) ||
184 	    (ext2fs_le16_to_cpu(eh->eh_max) < (eh_max - 2)))
185 		return EXT2_ET_EXTENT_HEADER_BAD;
186 
187 	return 0;
188 }
189 
190 
191 /*
192  * Begin functions to handle an inode's extent information
193  */
ext2fs_extent_free(ext2_extent_handle_t handle)194 void ext2fs_extent_free(ext2_extent_handle_t handle)
195 {
196 	int			i;
197 
198 	if (!handle)
199 		return;
200 
201 	if (handle->path) {
202 		for (i = 1; i < handle->max_paths; i++) {
203 			if (handle->path[i].buf)
204 				ext2fs_free_mem(&handle->path[i].buf);
205 		}
206 		ext2fs_free_mem(&handle->path);
207 	}
208 	ext2fs_free_mem(&handle);
209 }
210 
ext2fs_extent_open(ext2_filsys fs,ext2_ino_t ino,ext2_extent_handle_t * ret_handle)211 errcode_t ext2fs_extent_open(ext2_filsys fs, ext2_ino_t ino,
212 				    ext2_extent_handle_t *ret_handle)
213 {
214 	return ext2fs_extent_open2(fs, ino, NULL, ret_handle);
215 }
216 
ext2fs_extent_open2(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode,ext2_extent_handle_t * ret_handle)217 errcode_t ext2fs_extent_open2(ext2_filsys fs, ext2_ino_t ino,
218 				    struct ext2_inode *inode,
219 				    ext2_extent_handle_t *ret_handle)
220 {
221 	struct ext2_extent_handle	*handle;
222 	errcode_t			retval;
223 	int				i;
224 	struct ext3_extent_header	*eh;
225 
226 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
227 
228 	if (!inode)
229 		if ((ino == 0) || (ino > fs->super->s_inodes_count))
230 			return EXT2_ET_BAD_INODE_NUM;
231 
232 	retval = ext2fs_get_mem(sizeof(struct ext2_extent_handle), &handle);
233 	if (retval)
234 		return retval;
235 	memset(handle, 0, sizeof(struct ext2_extent_handle));
236 
237 	handle->ino = ino;
238 	handle->fs = fs;
239 
240 	if (inode) {
241 		handle->inode = inode;
242 	} else {
243 		handle->inode = &handle->inodebuf;
244 		retval = ext2fs_read_inode(fs, ino, handle->inode);
245 		if (retval)
246 			goto errout;
247 	}
248 
249 	eh = (struct ext3_extent_header *) &handle->inode->i_block[0];
250 
251 	for (i=0; i < EXT2_N_BLOCKS; i++)
252 		if (handle->inode->i_block[i])
253 			break;
254 	if (i >= EXT2_N_BLOCKS) {
255 		eh->eh_magic = ext2fs_cpu_to_le16(EXT3_EXT_MAGIC);
256 		eh->eh_depth = 0;
257 		eh->eh_entries = 0;
258 		i = (sizeof(handle->inode->i_block) - sizeof(*eh)) /
259 			sizeof(struct ext3_extent);
260 		eh->eh_max = ext2fs_cpu_to_le16(i);
261 		handle->inode->i_flags |= EXT4_EXTENTS_FL;
262 	}
263 
264 	if (!(handle->inode->i_flags & EXT4_EXTENTS_FL)) {
265 		retval = EXT2_ET_INODE_NOT_EXTENT;
266 		goto errout;
267 	}
268 
269 	retval = ext2fs_extent_header_verify(eh, sizeof(handle->inode->i_block));
270 	if (retval)
271 		goto errout;
272 
273 	handle->max_depth = ext2fs_le16_to_cpu(eh->eh_depth);
274 	handle->type = ext2fs_le16_to_cpu(eh->eh_magic);
275 
276 	handle->max_paths = handle->max_depth + 1;
277 	retval = ext2fs_get_memzero(handle->max_paths *
278 				    sizeof(struct extent_path),
279 				    &handle->path);
280 	handle->path[0].buf = (char *) handle->inode->i_block;
281 
282 	handle->path[0].left = handle->path[0].entries =
283 		ext2fs_le16_to_cpu(eh->eh_entries);
284 	handle->path[0].max_entries = ext2fs_le16_to_cpu(eh->eh_max);
285 	handle->path[0].curr = 0;
286 	handle->path[0].end_blk =
287 		(EXT2_I_SIZE(handle->inode) + fs->blocksize - 1) >>
288 		 EXT2_BLOCK_SIZE_BITS(fs->super);
289 	handle->path[0].visit_num = 1;
290 	handle->level = 0;
291 	handle->magic = EXT2_ET_MAGIC_EXTENT_HANDLE;
292 
293 	*ret_handle = handle;
294 	return 0;
295 
296 errout:
297 	ext2fs_extent_free(handle);
298 	return retval;
299 }
300 
301 /*
302  * This function is responsible for (optionally) moving through the
303  * extent tree and then returning the current extent
304  */
ext2fs_extent_get(ext2_extent_handle_t handle,int flags,struct ext2fs_extent * extent)305 errcode_t ext2fs_extent_get(ext2_extent_handle_t handle,
306 			    int flags, struct ext2fs_extent *extent)
307 {
308 	struct extent_path	*path, *newpath;
309 	struct ext3_extent_header	*eh;
310 	struct ext3_extent_idx		*ix = 0;
311 	struct ext3_extent		*ex;
312 	errcode_t			retval;
313 	blk64_t				blk;
314 	blk64_t				end_blk;
315 	int				orig_op, op;
316 	int				failed_csum = 0;
317 
318 	EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EXTENT_HANDLE);
319 
320 	if (!handle->path)
321 		return EXT2_ET_NO_CURRENT_NODE;
322 
323 	orig_op = op = flags & EXT2_EXTENT_MOVE_MASK;
324 
325 retry:
326 	path = handle->path + handle->level;
327 	if ((orig_op == EXT2_EXTENT_NEXT) ||
328 	    (orig_op == EXT2_EXTENT_NEXT_LEAF)) {
329 		if (handle->level < handle->max_depth) {
330 			/* interior node */
331 			if (path->visit_num == 0) {
332 				path->visit_num++;
333 				op = EXT2_EXTENT_DOWN;
334 			} else if (path->left > 0)
335 				op = EXT2_EXTENT_NEXT_SIB;
336 			else if (handle->level > 0)
337 				op = EXT2_EXTENT_UP;
338 			else
339 				return EXT2_ET_EXTENT_NO_NEXT;
340 		} else {
341 			/* leaf node */
342 			if (path->left > 0)
343 				op = EXT2_EXTENT_NEXT_SIB;
344 			else if (handle->level > 0)
345 				op = EXT2_EXTENT_UP;
346 			else
347 				return EXT2_ET_EXTENT_NO_NEXT;
348 		}
349 		if (op != EXT2_EXTENT_NEXT_SIB) {
350 #ifdef DEBUG_GET_EXTENT
351 			printf("<<<< OP = %s\n",
352 			       (op == EXT2_EXTENT_DOWN) ? "down" :
353 			       ((op == EXT2_EXTENT_UP) ? "up" : "unknown"));
354 #endif
355 		}
356 	}
357 
358 	if ((orig_op == EXT2_EXTENT_PREV) ||
359 	    (orig_op == EXT2_EXTENT_PREV_LEAF)) {
360 		if (handle->level < handle->max_depth) {
361 			/* interior node */
362 			if (path->visit_num > 0 ) {
363 				/* path->visit_num = 0; */
364 				op = EXT2_EXTENT_DOWN_AND_LAST;
365 			} else if (path->left < path->entries-1)
366 				op = EXT2_EXTENT_PREV_SIB;
367 			else if (handle->level > 0)
368 				op = EXT2_EXTENT_UP;
369 			else
370 				return EXT2_ET_EXTENT_NO_PREV;
371 		} else {
372 			/* leaf node */
373 			if (path->left < path->entries-1)
374 				op = EXT2_EXTENT_PREV_SIB;
375 			else if (handle->level > 0)
376 				op = EXT2_EXTENT_UP;
377 			else
378 				return EXT2_ET_EXTENT_NO_PREV;
379 		}
380 		if (op != EXT2_EXTENT_PREV_SIB) {
381 #ifdef DEBUG_GET_EXTENT
382 			printf("<<<< OP = %s\n",
383 			       (op == EXT2_EXTENT_DOWN_AND_LAST) ? "down/last" :
384 			       ((op == EXT2_EXTENT_UP) ? "up" : "unknown"));
385 #endif
386 		}
387 	}
388 
389 	if (orig_op == EXT2_EXTENT_LAST_LEAF) {
390 		if ((handle->level < handle->max_depth) &&
391 		    (path->left == 0))
392 			op = EXT2_EXTENT_DOWN;
393 		else
394 			op = EXT2_EXTENT_LAST_SIB;
395 #ifdef DEBUG_GET_EXTENT
396 		printf("<<<< OP = %s\n",
397 			   (op == EXT2_EXTENT_DOWN) ? "down" : "last_sib");
398 #endif
399 	}
400 
401 	switch (op) {
402 	case EXT2_EXTENT_CURRENT:
403 		ix = path->curr;
404 		break;
405 	case EXT2_EXTENT_ROOT:
406 		handle->level = 0;
407 		path = handle->path + handle->level;
408 		/* fallthrough */
409 	case EXT2_EXTENT_FIRST_SIB:
410 		path->left = path->entries;
411 		path->curr = 0;
412 		/* fallthrough */
413 	case EXT2_EXTENT_NEXT_SIB:
414 		if (path->left <= 0)
415 			return EXT2_ET_EXTENT_NO_NEXT;
416 		if (path->curr) {
417 			ix = path->curr;
418 			ix++;
419 		} else {
420 			eh = (struct ext3_extent_header *) path->buf;
421 			ix = EXT_FIRST_INDEX(eh);
422 		}
423 		path->left--;
424 		path->curr = ix;
425 		path->visit_num = 0;
426 		break;
427 	case EXT2_EXTENT_PREV_SIB:
428 		if (!path->curr ||
429 		    path->left+1 >= path->entries)
430 			return EXT2_ET_EXTENT_NO_PREV;
431 		ix = path->curr;
432 		ix--;
433 		path->curr = ix;
434 		path->left++;
435 		if (handle->level < handle->max_depth)
436 			path->visit_num = 1;
437 		break;
438 	case EXT2_EXTENT_LAST_SIB:
439 		eh = (struct ext3_extent_header *) path->buf;
440 		path->curr = EXT_LAST_EXTENT(eh);
441 		ix = path->curr;
442 		path->left = 0;
443 		path->visit_num = 0;
444 		break;
445 	case EXT2_EXTENT_UP:
446 		if (handle->level <= 0)
447 			return EXT2_ET_EXTENT_NO_UP;
448 		handle->level--;
449 		path--;
450 		ix = path->curr;
451 		if ((orig_op == EXT2_EXTENT_PREV) ||
452 		    (orig_op == EXT2_EXTENT_PREV_LEAF))
453 			path->visit_num = 0;
454 		break;
455 	case EXT2_EXTENT_DOWN:
456 	case EXT2_EXTENT_DOWN_AND_LAST:
457 		if (!path->curr ||(handle->level >= handle->max_depth))
458 			return EXT2_ET_EXTENT_NO_DOWN;
459 
460 		ix = path->curr;
461 		newpath = path + 1;
462 		if (!newpath->buf) {
463 			retval = ext2fs_get_mem(handle->fs->blocksize,
464 						&newpath->buf);
465 			if (retval)
466 				return retval;
467 		}
468 		blk = ext2fs_le32_to_cpu(ix->ei_leaf) +
469 			((__u64) ext2fs_le16_to_cpu(ix->ei_leaf_hi) << 32);
470 		if ((handle->fs->flags & EXT2_FLAG_IMAGE_FILE) &&
471 		    (handle->fs->io != handle->fs->image_io))
472 			memset(newpath->buf, 0, handle->fs->blocksize);
473 		else {
474 			retval = io_channel_read_blk64(handle->fs->io,
475 						     blk, 1, newpath->buf);
476 			if (retval)
477 				return retval;
478 		}
479 		handle->level++;
480 
481 		eh = (struct ext3_extent_header *) newpath->buf;
482 
483 		retval = ext2fs_extent_header_verify(eh, handle->fs->blocksize);
484 		if (retval) {
485 			handle->level--;
486 			return retval;
487 		}
488 
489 		if (!(handle->fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) &&
490 		    !ext2fs_extent_block_csum_verify(handle->fs, handle->ino,
491 						     eh))
492 			failed_csum = 1;
493 
494 		newpath->left = newpath->entries =
495 			ext2fs_le16_to_cpu(eh->eh_entries);
496 		newpath->max_entries = ext2fs_le16_to_cpu(eh->eh_max);
497 
498 		/* Make sure there is at least one extent present */
499 		if (newpath->left <= 0)
500 			return EXT2_ET_EXTENT_NO_DOWN;
501 
502 		if (path->left > 0) {
503 			ix++;
504 			newpath->end_blk = ext2fs_le32_to_cpu(ix->ei_block);
505 		} else
506 			newpath->end_blk = path->end_blk;
507 
508 		path = newpath;
509 		if (op == EXT2_EXTENT_DOWN) {
510 			ix = EXT_FIRST_INDEX((struct ext3_extent_header *) eh);
511 			path->curr = ix;
512 			path->left = path->entries - 1;
513 			path->visit_num = 0;
514 		} else {
515 			ix = EXT_LAST_INDEX((struct ext3_extent_header *) eh);
516 			path->curr = ix;
517 			path->left = 0;
518 			if (handle->level < handle->max_depth)
519 				path->visit_num = 1;
520 		}
521 #ifdef DEBUG_GET_EXTENT
522 		printf("Down to level %d/%d, end_blk=%llu\n",
523 			   handle->level, handle->max_depth,
524 			   path->end_blk);
525 #endif
526 		break;
527 	default:
528 		return EXT2_ET_OP_NOT_SUPPORTED;
529 	}
530 
531 	if (!ix)
532 		return EXT2_ET_NO_CURRENT_NODE;
533 
534 	extent->e_flags = 0;
535 #ifdef DEBUG_GET_EXTENT
536 	printf("(Left %d)\n", path->left);
537 #endif
538 
539 	if (handle->level == handle->max_depth) {
540 		ex = (struct ext3_extent *) ix;
541 
542 		extent->e_pblk = ext2fs_le32_to_cpu(ex->ee_start) +
543 			((__u64) ext2fs_le16_to_cpu(ex->ee_start_hi) << 32);
544 		extent->e_lblk = ext2fs_le32_to_cpu(ex->ee_block);
545 		extent->e_len = ext2fs_le16_to_cpu(ex->ee_len);
546 		extent->e_flags |= EXT2_EXTENT_FLAGS_LEAF;
547 		if (extent->e_len > EXT_INIT_MAX_LEN) {
548 			extent->e_len -= EXT_INIT_MAX_LEN;
549 			extent->e_flags |= EXT2_EXTENT_FLAGS_UNINIT;
550 		}
551 	} else {
552 		extent->e_pblk = ext2fs_le32_to_cpu(ix->ei_leaf) +
553 			((__u64) ext2fs_le16_to_cpu(ix->ei_leaf_hi) << 32);
554 		extent->e_lblk = ext2fs_le32_to_cpu(ix->ei_block);
555 		if (path->left > 0) {
556 			ix++;
557 			end_blk = ext2fs_le32_to_cpu(ix->ei_block);
558 		} else
559 			end_blk = path->end_blk;
560 
561 		extent->e_len = end_blk - extent->e_lblk;
562 	}
563 	if (path->visit_num)
564 		extent->e_flags |= EXT2_EXTENT_FLAGS_SECOND_VISIT;
565 
566 	if (((orig_op == EXT2_EXTENT_NEXT_LEAF) ||
567 	     (orig_op == EXT2_EXTENT_PREV_LEAF)) &&
568 	    (handle->level != handle->max_depth))
569 		goto retry;
570 
571 	if ((orig_op == EXT2_EXTENT_LAST_LEAF) &&
572 	    ((handle->level != handle->max_depth) ||
573 	     (path->left != 0)))
574 		goto retry;
575 
576 	if (failed_csum)
577 		return EXT2_ET_EXTENT_CSUM_INVALID;
578 
579 	return 0;
580 }
581 
update_path(ext2_extent_handle_t handle)582 static errcode_t update_path(ext2_extent_handle_t handle)
583 {
584 	blk64_t				blk;
585 	errcode_t			retval;
586 	struct ext3_extent_idx		*ix;
587 	struct ext3_extent_header	*eh;
588 
589 	if (handle->level == 0) {
590 		retval = ext2fs_write_inode(handle->fs, handle->ino,
591 					    handle->inode);
592 	} else {
593 		ix = handle->path[handle->level - 1].curr;
594 		blk = ext2fs_le32_to_cpu(ix->ei_leaf) +
595 			((__u64) ext2fs_le16_to_cpu(ix->ei_leaf_hi) << 32);
596 
597 		/* then update the checksum */
598 		eh = (struct ext3_extent_header *)
599 				handle->path[handle->level].buf;
600 		retval = ext2fs_extent_block_csum_set(handle->fs, handle->ino,
601 						      eh);
602 		if (retval)
603 			return retval;
604 
605 		retval = io_channel_write_blk64(handle->fs->io,
606 				      blk, 1, handle->path[handle->level].buf);
607 	}
608 	return retval;
609 }
610 
611 #if 0
612 errcode_t ext2fs_extent_save_path(ext2_extent_handle_t handle,
613 				  ext2_extent_path_t *ret_path)
614 {
615 	ext2_extent_path_t	save_path;
616 	struct ext2fs_extent	extent;
617 	struct ext2_extent_info	info;
618 	errcode_t		retval;
619 
620 	retval = ext2fs_extent_get(handle, EXT2_EXTENT_CURRENT, &extent);
621 	if (retval)
622 		return retval;
623 
624 	retval = ext2fs_extent_get_info(handle, &info);
625 	if (retval)
626 		return retval;
627 
628 	retval = ext2fs_get_mem(sizeof(struct ext2_extent_path), &save_path);
629 	if (retval)
630 		return retval;
631 	memset(save_path, 0, sizeof(struct ext2_extent_path));
632 
633 	save_path->magic = EXT2_ET_MAGIC_EXTENT_PATH;
634 	save_path->leaf_height = info.max_depth - info.curr_level - 1;
635 	save_path->lblk = extent.e_lblk;
636 
637 	*ret_path = save_path;
638 	return 0;
639 }
640 
641 errcode_t ext2fs_extent_free_path(ext2_extent_path_t path)
642 {
643 	EXT2_CHECK_MAGIC(path, EXT2_ET_MAGIC_EXTENT_PATH);
644 
645 	ext2fs_free_mem(&path);
646 	return 0;
647 }
648 #endif
649 
650 /*
651  * Go to the node at leaf_level which contains logical block blk.
652  *
653  * leaf_level is height from the leaf node level, i.e.
654  * leaf_level 0 is at leaf node, leaf_level 1 is 1 above etc.
655  *
656  * If "blk" has no mapping (hole) then handle is left at last
657  * extent before blk.
658  */
ext2fs_extent_goto2(ext2_extent_handle_t handle,int leaf_level,blk64_t blk)659 errcode_t ext2fs_extent_goto2(ext2_extent_handle_t handle,
660 			      int leaf_level, blk64_t blk)
661 {
662 	struct ext2fs_extent	extent;
663 	errcode_t		retval;
664 
665 	retval = ext2fs_extent_get(handle, EXT2_EXTENT_ROOT, &extent);
666 	if (retval) {
667 		if (retval == EXT2_ET_EXTENT_NO_NEXT)
668 			retval = EXT2_ET_EXTENT_NOT_FOUND;
669 		return retval;
670 	}
671 
672 	if (leaf_level > handle->max_depth) {
673 #ifdef DEBUG
674 		printf("leaf level %d greater than tree depth %d\n",
675 			leaf_level, handle->max_depth);
676 #endif
677 		return EXT2_ET_OP_NOT_SUPPORTED;
678 	}
679 
680 #ifdef DEBUG
681 	printf("goto extent ino %u, level %d, %llu\n", handle->ino,
682 	       leaf_level, blk);
683 #endif
684 
685 #ifdef DEBUG_GOTO_EXTENTS
686 	dbg_print_extent("root", &extent);
687 #endif
688 	while (1) {
689 		if (handle->max_depth - handle->level == leaf_level) {
690 			/* block is in this &extent */
691 			if ((blk >= extent.e_lblk) &&
692 			    (blk < extent.e_lblk + extent.e_len))
693 				return 0;
694 			if (blk < extent.e_lblk) {
695 				retval = ext2fs_extent_get(handle,
696 							   EXT2_EXTENT_PREV_SIB,
697 							   &extent);
698 				return EXT2_ET_EXTENT_NOT_FOUND;
699 			}
700 			retval = ext2fs_extent_get(handle,
701 						   EXT2_EXTENT_NEXT_SIB,
702 						   &extent);
703 			if (retval == EXT2_ET_EXTENT_NO_NEXT)
704 				return EXT2_ET_EXTENT_NOT_FOUND;
705 			if (retval)
706 				return retval;
707 			continue;
708 		}
709 
710 		retval = ext2fs_extent_get(handle, EXT2_EXTENT_NEXT_SIB,
711 					   &extent);
712 		if (retval == EXT2_ET_EXTENT_NO_NEXT)
713 			goto go_down;
714 		if (retval)
715 			return retval;
716 
717 #ifdef DEBUG_GOTO_EXTENTS
718 		dbg_print_extent("next", &extent);
719 #endif
720 		if (blk == extent.e_lblk)
721 			goto go_down;
722 		if (blk > extent.e_lblk)
723 			continue;
724 
725 		retval = ext2fs_extent_get(handle, EXT2_EXTENT_PREV_SIB,
726 					   &extent);
727 		if (retval)
728 			return retval;
729 
730 #ifdef DEBUG_GOTO_EXTENTS
731 		dbg_print_extent("prev", &extent);
732 #endif
733 
734 	go_down:
735 		retval = ext2fs_extent_get(handle, EXT2_EXTENT_DOWN,
736 					   &extent);
737 		if (retval)
738 			return retval;
739 
740 #ifdef DEBUG_GOTO_EXTENTS
741 		dbg_print_extent("down", &extent);
742 #endif
743 	}
744 }
745 
ext2fs_extent_goto(ext2_extent_handle_t handle,blk64_t blk)746 errcode_t ext2fs_extent_goto(ext2_extent_handle_t handle,
747 			     blk64_t blk)
748 {
749 	return ext2fs_extent_goto2(handle, 0, blk);
750 }
751 
752 /*
753  * Traverse back up to root fixing parents of current node as needed.
754  *
755  * If we changed start of first entry in a node, fix parent index start
756  * and so on.
757  *
758  * Safe to call for any position in node; if not at the first entry,
759  * it will simply return.
760  *
761  * Note a subtlety of this function -- if there happen to be two extents
762  * mapping the same lblk and someone calls fix_parents on the second of the two
763  * extents, the position of the extent handle after the call will be the second
764  * extent if nothing happened, or the first extent if something did.  A caller
765  * in this situation must use ext2fs_extent_goto() after calling this function.
766  * Or simply don't map the same lblk with two extents, ever.
767  */
ext2fs_extent_fix_parents(ext2_extent_handle_t handle)768 errcode_t ext2fs_extent_fix_parents(ext2_extent_handle_t handle)
769 {
770 	int				retval = 0;
771 	int				orig_height;
772 	blk64_t				start;
773 	struct extent_path		*path;
774 	struct ext2fs_extent		extent;
775 	struct ext2_extent_info		info;
776 
777 	EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EXTENT_HANDLE);
778 
779 	if (!(handle->fs->flags & EXT2_FLAG_RW))
780 		return EXT2_ET_RO_FILSYS;
781 
782 	if (!handle->path)
783 		return EXT2_ET_NO_CURRENT_NODE;
784 
785 	path = handle->path + handle->level;
786 	if (!path->curr)
787 		return EXT2_ET_NO_CURRENT_NODE;
788 
789 	retval = ext2fs_extent_get(handle, EXT2_EXTENT_CURRENT, &extent);
790 	if (retval)
791 		goto done;
792 
793 	/* modified node's start block */
794 	start = extent.e_lblk;
795 
796 	if ((retval = ext2fs_extent_get_info(handle, &info)))
797 		return retval;
798 	orig_height = info.max_depth - info.curr_level;
799 
800 	/* traverse up until index not first, or startblk matches, or top */
801 	while (handle->level > 0 &&
802 	       (path->left == path->entries - 1)) {
803 		retval = ext2fs_extent_get(handle, EXT2_EXTENT_UP, &extent);
804 		if (retval)
805 			goto done;
806 		if (extent.e_lblk == start)
807 			break;
808 		path = handle->path + handle->level;
809 		extent.e_len += (extent.e_lblk - start);
810 		extent.e_lblk = start;
811 		retval = ext2fs_extent_replace(handle, 0, &extent);
812 		if (retval)
813 			goto done;
814 		update_path(handle);
815 	}
816 
817 	/* put handle back to where we started */
818 	retval = ext2fs_extent_goto2(handle, orig_height, start);
819 done:
820 	return retval;
821 }
822 
ext2fs_extent_replace(ext2_extent_handle_t handle,int flags EXT2FS_ATTR ((unused)),struct ext2fs_extent * extent)823 errcode_t ext2fs_extent_replace(ext2_extent_handle_t handle,
824 				int flags EXT2FS_ATTR((unused)),
825 				struct ext2fs_extent *extent)
826 {
827 	struct extent_path		*path;
828 	struct ext3_extent_idx		*ix;
829 	struct ext3_extent		*ex;
830 
831 	EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EXTENT_HANDLE);
832 
833 	if (!(handle->fs->flags & EXT2_FLAG_RW))
834 		return EXT2_ET_RO_FILSYS;
835 
836 	if (!handle->path)
837 		return EXT2_ET_NO_CURRENT_NODE;
838 
839 	path = handle->path + handle->level;
840 	if (!path->curr)
841 		return EXT2_ET_NO_CURRENT_NODE;
842 
843 #ifdef DEBUG
844 	printf("extent replace: %u ", handle->ino);
845 	dbg_print_extent(0, extent);
846 #endif
847 
848 	if (handle->level == handle->max_depth) {
849 		ex = path->curr;
850 
851 		ex->ee_block = ext2fs_cpu_to_le32(extent->e_lblk);
852 		ex->ee_start = ext2fs_cpu_to_le32(extent->e_pblk & 0xFFFFFFFF);
853 		ex->ee_start_hi = ext2fs_cpu_to_le16(extent->e_pblk >> 32);
854 		if (extent->e_flags & EXT2_EXTENT_FLAGS_UNINIT) {
855 			if (extent->e_len > EXT_UNINIT_MAX_LEN)
856 				return EXT2_ET_EXTENT_INVALID_LENGTH;
857 			ex->ee_len = ext2fs_cpu_to_le16(extent->e_len +
858 							EXT_INIT_MAX_LEN);
859 		} else {
860 			if (extent->e_len > EXT_INIT_MAX_LEN)
861 				return EXT2_ET_EXTENT_INVALID_LENGTH;
862 			ex->ee_len = ext2fs_cpu_to_le16(extent->e_len);
863 		}
864 	} else {
865 		ix = path->curr;
866 
867 		ix->ei_leaf = ext2fs_cpu_to_le32(extent->e_pblk & 0xFFFFFFFF);
868 		ix->ei_leaf_hi = ext2fs_cpu_to_le16(extent->e_pblk >> 32);
869 		ix->ei_block = ext2fs_cpu_to_le32(extent->e_lblk);
870 		ix->ei_unused = 0;
871 	}
872 	update_path(handle);
873 	return 0;
874 }
875 
splitting_at_eof(struct ext2_extent_handle * handle,struct extent_path * path)876 static int splitting_at_eof(struct ext2_extent_handle *handle,
877 			    struct extent_path *path)
878 {
879 	struct extent_path *ppp = path;
880 	dump_path(__func__, handle, path);
881 
882 	if (handle->level == 0)
883 		return 0;
884 
885 	do {
886 		if (ppp->left)
887 			return 0;
888 		ppp--;
889 	} while (ppp >= handle->path);
890 
891 	return 1;
892 }
893 
894 /*
895  * allocate a new block, move half the current node to it, and update parent
896  *
897  * handle will be left pointing at original record.
898  */
extent_node_split(ext2_extent_handle_t handle,int expand_allowed)899 static errcode_t extent_node_split(ext2_extent_handle_t handle,
900 				   int expand_allowed)
901 {
902 	errcode_t			retval = 0;
903 	blk64_t				new_node_pblk;
904 	blk64_t				new_node_start;
905 	blk64_t				orig_lblk;
906 	blk64_t				goal_blk = 0;
907 	int				orig_height;
908 	char				*block_buf = NULL;
909 	struct ext2fs_extent		extent;
910 	struct extent_path		*path, *newpath = 0;
911 	struct ext3_extent_header	*eh, *neweh;
912 	int				tocopy;
913 	int				new_root = 0;
914 	struct ext2_extent_info		info;
915 	int				no_balance;
916 
917 	/* basic sanity */
918 	EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EXTENT_HANDLE);
919 
920 	if (!(handle->fs->flags & EXT2_FLAG_RW))
921 		return EXT2_ET_RO_FILSYS;
922 
923 	if (!handle->path)
924 		return EXT2_ET_NO_CURRENT_NODE;
925 
926 #ifdef DEBUG
927 	printf("splitting node at level %d\n", handle->level);
928 #endif
929 	retval = ext2fs_extent_get(handle, EXT2_EXTENT_CURRENT, &extent);
930 	if (retval)
931 		goto done;
932 
933 	retval = ext2fs_extent_get_info(handle, &info);
934 	if (retval)
935 		goto done;
936 
937 	/* save the position we were originally splitting... */
938 	orig_height = info.max_depth - info.curr_level;
939 	orig_lblk = extent.e_lblk;
940 
941 	/* Try to put the index block before the first extent */
942 	path = handle->path + handle->level;
943 	eh = (struct ext3_extent_header *) path->buf;
944 	if (handle->level == handle->max_depth) {
945 		struct ext3_extent	*ex;
946 
947 		ex = EXT_FIRST_EXTENT(eh);
948 		goal_blk = ext2fs_le32_to_cpu(ex->ee_start) +
949 			((__u64) ext2fs_le16_to_cpu(ex->ee_start_hi) << 32);
950 	} else {
951 		struct ext3_extent_idx	*ix;
952 
953 		ix = EXT_FIRST_INDEX(eh);
954 		goal_blk = ext2fs_le32_to_cpu(ix->ei_leaf) +
955 			((__u64) ext2fs_le16_to_cpu(ix->ei_leaf_hi) << 32);
956 	}
957 	goal_blk -= EXT2FS_CLUSTER_RATIO(handle->fs);
958 	goal_blk &= ~EXT2FS_CLUSTER_MASK(handle->fs);
959 
960 	/* Is there room in the parent for a new entry? */
961 	if (handle->level &&
962 			(handle->path[handle->level - 1].entries >=
963 			 handle->path[handle->level - 1].max_entries)) {
964 
965 #ifdef DEBUG
966 		printf("parent level %d full; splitting it too\n",
967 							handle->level - 1);
968 #endif
969 		/* split the parent */
970 		retval = ext2fs_extent_get(handle, EXT2_EXTENT_UP, &extent);
971 		if (retval)
972 			goto done;
973 
974 		retval = extent_node_split(handle, expand_allowed);
975 		if (retval)
976 			goto done;
977 
978 		/* get handle back to our original split position */
979 		retval = ext2fs_extent_goto2(handle, orig_height, orig_lblk);
980 		if (retval)
981 			goto done;
982 	}
983 
984 	/* At this point, parent should have room for this split */
985 	path = handle->path + handle->level;
986 	if (!path->curr)
987 		return EXT2_ET_NO_CURRENT_NODE;
988 
989 	/*
990 	 * Normally, we try to split a full node in half.  This doesn't turn
991 	 * out so well if we're tacking extents on the end of the file because
992 	 * then we're stuck with a tree of half-full extent blocks.  This of
993 	 * course doesn't apply to the root level.
994 	 */
995 	no_balance = expand_allowed ? splitting_at_eof(handle, path) : 0;
996 
997 	/* extent header of the current node we'll split */
998 	eh = (struct ext3_extent_header *)path->buf;
999 
1000 	/* splitting root level means moving them all out */
1001 	if (handle->level == 0) {
1002 		new_root = 1;
1003 		tocopy = ext2fs_le16_to_cpu(eh->eh_entries);
1004 		retval = ext2fs_get_memzero((handle->max_paths + 1) *
1005 					    sizeof(struct extent_path),
1006 					    &newpath);
1007 		if (retval)
1008 			goto done;
1009 	} else {
1010 		if (no_balance)
1011 			tocopy = 1;
1012 		else
1013 			tocopy = ext2fs_le16_to_cpu(eh->eh_entries) / 2;
1014 	}
1015 
1016 #ifdef DEBUG
1017 	printf("will copy out %d of %d entries at level %d\n",
1018 				tocopy, ext2fs_le16_to_cpu(eh->eh_entries),
1019 				handle->level);
1020 #endif
1021 
1022 	if (!tocopy && !no_balance) {
1023 #ifdef DEBUG
1024 		printf("Nothing to copy to new block!\n");
1025 #endif
1026 		retval = EXT2_ET_CANT_SPLIT_EXTENT;
1027 		goto done;
1028 	}
1029 
1030 	/* first we need a new block, or can do nothing. */
1031 	block_buf = malloc(handle->fs->blocksize);
1032 	if (!block_buf) {
1033 		retval = ENOMEM;
1034 		goto done;
1035 	}
1036 
1037 	if (!goal_blk)
1038 		goal_blk = ext2fs_find_inode_goal(handle->fs, handle->ino,
1039 						  handle->inode, 0);
1040 	retval = ext2fs_alloc_block2(handle->fs, goal_blk, block_buf,
1041 				    &new_node_pblk);
1042 	if (retval)
1043 		goto done;
1044 
1045 #ifdef DEBUG
1046 	printf("will copy to new node at block %lu\n",
1047 	       (unsigned long) new_node_pblk);
1048 #endif
1049 
1050 	/* Copy data into new block buffer */
1051 	/* First the header for the new block... */
1052 	neweh = (struct ext3_extent_header *) block_buf;
1053 	memcpy(neweh, eh, sizeof(struct ext3_extent_header));
1054 	neweh->eh_entries = ext2fs_cpu_to_le16(tocopy);
1055 	neweh->eh_max = ext2fs_cpu_to_le16((handle->fs->blocksize -
1056 			 sizeof(struct ext3_extent_header)) /
1057 				sizeof(struct ext3_extent));
1058 
1059 	/* then the entries for the new block... */
1060 	memcpy(EXT_FIRST_INDEX(neweh),
1061 		EXT_FIRST_INDEX(eh) +
1062 			(ext2fs_le16_to_cpu(eh->eh_entries) - tocopy),
1063 		sizeof(struct ext3_extent_idx) * tocopy);
1064 
1065 	new_node_start = ext2fs_le32_to_cpu(EXT_FIRST_INDEX(neweh)->ei_block);
1066 
1067 	/* then update the checksum */
1068 	retval = ext2fs_extent_block_csum_set(handle->fs, handle->ino, neweh);
1069 	if (retval)
1070 		goto done;
1071 
1072 	/* ...and write the new node block out to disk. */
1073 	retval = io_channel_write_blk64(handle->fs->io, new_node_pblk, 1,
1074 					block_buf);
1075 
1076 	if (retval)
1077 		goto done;
1078 
1079 	/* OK! we've created the new node; now adjust the tree */
1080 
1081 	/* current path now has fewer active entries, we copied some out */
1082 	if (handle->level == 0) {
1083 		memcpy(newpath, path,
1084 		       sizeof(struct extent_path) * handle->max_paths);
1085 		handle->path = newpath;
1086 		newpath = path;
1087 		path = handle->path;
1088 		path->entries = 1;
1089 		path->left = path->max_entries - 1;
1090 		handle->max_depth++;
1091 		handle->max_paths++;
1092 		eh->eh_depth = ext2fs_cpu_to_le16(handle->max_depth);
1093 	} else {
1094 		path->entries -= tocopy;
1095 		path->left -= tocopy;
1096 	}
1097 
1098 	eh->eh_entries = ext2fs_cpu_to_le16(path->entries);
1099 	/* this writes out the node, incl. the modified header */
1100 	retval = update_path(handle);
1101 	if (retval)
1102 		goto done;
1103 
1104 	/* now go up and insert/replace index for new node we created */
1105 	if (new_root) {
1106 		retval = ext2fs_extent_get(handle, EXT2_EXTENT_FIRST_SIB, &extent);
1107 		if (retval)
1108 			goto done;
1109 
1110 		extent.e_lblk = new_node_start;
1111 		extent.e_pblk = new_node_pblk;
1112 		extent.e_len = handle->path[0].end_blk - extent.e_lblk;
1113 		retval = ext2fs_extent_replace(handle, 0, &extent);
1114 		if (retval)
1115 			goto done;
1116 	} else {
1117 		__u32 new_node_length;
1118 
1119 		retval = ext2fs_extent_get(handle, EXT2_EXTENT_UP, &extent);
1120 		/* will insert after this one; it's length is shorter now */
1121 		new_node_length = new_node_start - extent.e_lblk;
1122 		extent.e_len -= new_node_length;
1123 		retval = ext2fs_extent_replace(handle, 0, &extent);
1124 		if (retval)
1125 			goto done;
1126 
1127 		/* now set up the new extent and insert it */
1128 		extent.e_lblk = new_node_start;
1129 		extent.e_pblk = new_node_pblk;
1130 		extent.e_len = new_node_length;
1131 		retval = ext2fs_extent_insert(handle, EXT2_EXTENT_INSERT_AFTER, &extent);
1132 		if (retval)
1133 			goto done;
1134 	}
1135 
1136 	/* get handle back to our original position */
1137 	retval = ext2fs_extent_goto2(handle, orig_height, orig_lblk);
1138 	if (retval)
1139 		goto done;
1140 
1141 	/* new node hooked in, so update inode block count (do this here?) */
1142 	ext2fs_iblk_add_blocks(handle->fs, handle->inode, 1);
1143 	retval = ext2fs_write_inode(handle->fs, handle->ino,
1144 				    handle->inode);
1145 	if (retval)
1146 		goto done;
1147 
1148 done:
1149 	if (newpath)
1150 		ext2fs_free_mem(&newpath);
1151 	free(block_buf);
1152 
1153 	return retval;
1154 }
1155 
ext2fs_extent_node_split(ext2_extent_handle_t handle)1156 errcode_t ext2fs_extent_node_split(ext2_extent_handle_t handle)
1157 {
1158 	return extent_node_split(handle, 0);
1159 }
1160 
ext2fs_extent_insert(ext2_extent_handle_t handle,int flags,struct ext2fs_extent * extent)1161 errcode_t ext2fs_extent_insert(ext2_extent_handle_t handle, int flags,
1162 				      struct ext2fs_extent *extent)
1163 {
1164 	struct extent_path		*path;
1165 	struct ext3_extent_idx		*ix;
1166 	struct ext3_extent_header	*eh;
1167 	errcode_t			retval;
1168 
1169 	EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EXTENT_HANDLE);
1170 
1171 	if (!(handle->fs->flags & EXT2_FLAG_RW))
1172 		return EXT2_ET_RO_FILSYS;
1173 
1174 	if (!handle->path)
1175 		return EXT2_ET_NO_CURRENT_NODE;
1176 
1177 #ifdef DEBUG
1178 	printf("extent insert: %u ", handle->ino);
1179 	dbg_print_extent(0, extent);
1180 #endif
1181 
1182 	path = handle->path + handle->level;
1183 
1184 	if (path->entries >= path->max_entries) {
1185 		if (flags & EXT2_EXTENT_INSERT_NOSPLIT) {
1186 			return EXT2_ET_CANT_INSERT_EXTENT;
1187 		} else {
1188 #ifdef DEBUG
1189 			printf("node full (level %d) - splitting\n",
1190 				   handle->level);
1191 #endif
1192 			retval = extent_node_split(handle, 1);
1193 			if (retval)
1194 				return retval;
1195 			path = handle->path + handle->level;
1196 		}
1197 	}
1198 
1199 	eh = (struct ext3_extent_header *) path->buf;
1200 	if (path->curr) {
1201 		ix = path->curr;
1202 		if (flags & EXT2_EXTENT_INSERT_AFTER) {
1203 			ix++;
1204 			path->left--;
1205 		}
1206 	} else {
1207 		ix = EXT_FIRST_INDEX(eh);
1208 		path->left = -1;
1209 	}
1210 
1211 	path->curr = ix;
1212 
1213 	if (path->left >= 0)
1214 		memmove(ix + 1, ix,
1215 			(path->left+1) * sizeof(struct ext3_extent_idx));
1216 	path->left++;
1217 	path->entries++;
1218 
1219 	eh = (struct ext3_extent_header *) path->buf;
1220 	eh->eh_entries = ext2fs_cpu_to_le16(path->entries);
1221 
1222 	retval = ext2fs_extent_replace(handle, 0, extent);
1223 	if (retval)
1224 		goto errout;
1225 
1226 	retval = update_path(handle);
1227 	if (retval)
1228 		goto errout;
1229 
1230 	return 0;
1231 
1232 errout:
1233 	ext2fs_extent_delete(handle, 0);
1234 	return retval;
1235 }
1236 
1237 /*
1238  * Sets the physical block for a logical file block in the extent tree.
1239  *
1240  * May: map unmapped, unmap mapped, or remap mapped blocks.
1241  *
1242  * Mapping an unmapped block adds a single-block extent.
1243  *
1244  * Unmapping first or last block modifies extent in-place
1245  *  - But may need to fix parent's starts too in first-block case
1246  *
1247  * Mapping any unmapped block requires adding a (single-block) extent
1248  * and inserting into proper point in tree.
1249  *
1250  * Modifying (unmapping or remapping) a block in the middle
1251  * of an extent requires splitting the extent.
1252  *  - Remapping case requires new single-block extent.
1253  *
1254  * Remapping first or last block adds an extent.
1255  *
1256  * We really need extent adding to be smart about merging.
1257  */
1258 
ext2fs_extent_set_bmap(ext2_extent_handle_t handle,blk64_t logical,blk64_t physical,int flags)1259 errcode_t ext2fs_extent_set_bmap(ext2_extent_handle_t handle,
1260 				 blk64_t logical, blk64_t physical, int flags)
1261 {
1262 	errcode_t		ec, retval = 0;
1263 	int			mapped = 1; /* logical is mapped? */
1264 	int			orig_height;
1265 	int			extent_uninit = 0;
1266 	int			prev_uninit = 0;
1267 	int			next_uninit = 0;
1268 	int			new_uninit = 0;
1269 	int			max_len = EXT_INIT_MAX_LEN;
1270 	int			has_prev, has_next;
1271 	blk64_t			orig_lblk;
1272 	struct extent_path	*path;
1273 	struct ext2fs_extent	extent, next_extent, prev_extent;
1274 	struct ext2fs_extent	newextent;
1275 	struct ext2_extent_info	info;
1276 
1277 	EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EXTENT_HANDLE);
1278 
1279 #ifdef DEBUG
1280 	printf("set_bmap ino %u log %lld phys %lld flags %d\n",
1281 	       handle->ino, logical, physical, flags);
1282 #endif
1283 
1284 	if (!(handle->fs->flags & EXT2_FLAG_RW))
1285 		return EXT2_ET_RO_FILSYS;
1286 
1287 	if (!handle->path)
1288 		return EXT2_ET_NO_CURRENT_NODE;
1289 
1290 	path = handle->path + handle->level;
1291 
1292 	if (flags & EXT2_EXTENT_SET_BMAP_UNINIT) {
1293 		new_uninit = 1;
1294 		max_len = EXT_UNINIT_MAX_LEN;
1295 	}
1296 
1297 	/* if (re)mapping, set up new extent to insert */
1298 	if (physical) {
1299 		newextent.e_len = 1;
1300 		newextent.e_pblk = physical;
1301 		newextent.e_lblk = logical;
1302 		newextent.e_flags = EXT2_EXTENT_FLAGS_LEAF;
1303 		if (new_uninit)
1304 			newextent.e_flags |= EXT2_EXTENT_FLAGS_UNINIT;
1305 	}
1306 
1307 	/* special case if the extent tree is completely empty */
1308 	if ((handle->max_depth == 0) && (path->entries == 0)) {
1309 		retval = ext2fs_extent_insert(handle, 0, &newextent);
1310 		return retval;
1311 	}
1312 
1313 	/* save our original location in the extent tree */
1314 	if ((retval = ext2fs_extent_get(handle, EXT2_EXTENT_CURRENT,
1315 					&extent))) {
1316 		if (retval != EXT2_ET_NO_CURRENT_NODE)
1317 			return retval;
1318 		memset(&extent, 0, sizeof(extent));
1319 	}
1320 	if ((retval = ext2fs_extent_get_info(handle, &info)))
1321 		return retval;
1322 	orig_height = info.max_depth - info.curr_level;
1323 	orig_lblk = extent.e_lblk;
1324 
1325 	/* go to the logical spot we want to (re/un)map */
1326 	retval = ext2fs_extent_goto(handle, logical);
1327 	if (retval) {
1328 		if (retval == EXT2_ET_EXTENT_NOT_FOUND) {
1329 			retval = 0;
1330 			mapped = 0;
1331 			if (!physical) {
1332 #ifdef DEBUG
1333 				printf("block %llu already unmapped\n",
1334 					logical);
1335 #endif
1336 				goto done;
1337 			}
1338 		} else
1339 			goto done;
1340 	}
1341 
1342 	/*
1343 	 * This may be the extent *before* the requested logical,
1344 	 * if it's currently unmapped.
1345 	 *
1346 	 * Get the previous and next leaf extents, if they are present.
1347 	 */
1348 	retval = ext2fs_extent_get(handle, EXT2_EXTENT_CURRENT, &extent);
1349 	if (retval)
1350 		goto done;
1351 	if (extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT)
1352 		extent_uninit = 1;
1353 	retval = ext2fs_extent_get(handle, EXT2_EXTENT_NEXT_LEAF, &next_extent);
1354 	if (retval) {
1355 		has_next = 0;
1356 		if (retval != EXT2_ET_EXTENT_NO_NEXT)
1357 			goto done;
1358 	} else {
1359 		dbg_print_extent("set_bmap: next_extent",
1360 				 &next_extent);
1361 		has_next = 1;
1362 		if (next_extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT)
1363 			next_uninit = 1;
1364 	}
1365 	retval = ext2fs_extent_goto(handle, logical);
1366 	if (retval && retval != EXT2_ET_EXTENT_NOT_FOUND)
1367 		goto done;
1368 	retval = ext2fs_extent_get(handle, EXT2_EXTENT_PREV_LEAF, &prev_extent);
1369 	if (retval) {
1370 		has_prev = 0;
1371 		if (retval != EXT2_ET_EXTENT_NO_PREV)
1372 			goto done;
1373 	} else {
1374 		has_prev = 1;
1375 		dbg_print_extent("set_bmap: prev_extent",
1376 				 &prev_extent);
1377 		if (prev_extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT)
1378 			prev_uninit = 1;
1379 	}
1380 	retval = ext2fs_extent_goto(handle, logical);
1381 	if (retval && retval != EXT2_ET_EXTENT_NOT_FOUND)
1382 		goto done;
1383 
1384 	/* check if already pointing to the requested physical */
1385 	if (mapped && (new_uninit == extent_uninit) &&
1386 	    (extent.e_pblk + (logical - extent.e_lblk) == physical)) {
1387 #ifdef DEBUG
1388 		printf("physical block (at %llu) unchanged\n", logical);
1389 #endif
1390 		goto done;
1391 	}
1392 
1393 	if (!mapped) {
1394 #ifdef DEBUG
1395 		printf("mapping unmapped logical block %llu\n", logical);
1396 #endif
1397 		if ((logical == extent.e_lblk + extent.e_len) &&
1398 		    (physical == extent.e_pblk + extent.e_len) &&
1399 		    (new_uninit == extent_uninit) &&
1400 		    ((int) extent.e_len < max_len-1)) {
1401 			extent.e_len++;
1402 			retval = ext2fs_extent_replace(handle, 0, &extent);
1403 		} else if ((logical == extent.e_lblk - 1) &&
1404 			   (physical == extent.e_pblk - 1) &&
1405 			   (new_uninit == extent_uninit) &&
1406 			   ((int) extent.e_len < max_len - 1)) {
1407 			extent.e_len++;
1408 			extent.e_lblk--;
1409 			extent.e_pblk--;
1410 			retval = ext2fs_extent_replace(handle, 0, &extent);
1411 		} else if (has_next &&
1412 			   (logical == next_extent.e_lblk - 1) &&
1413 			   (physical == next_extent.e_pblk - 1) &&
1414 			   (new_uninit == next_uninit) &&
1415 			   ((int) next_extent.e_len < max_len - 1)) {
1416 			retval = ext2fs_extent_get(handle,
1417 						   EXT2_EXTENT_NEXT_LEAF,
1418 						   &next_extent);
1419 			if (retval)
1420 				goto done;
1421 			next_extent.e_len++;
1422 			next_extent.e_lblk--;
1423 			next_extent.e_pblk--;
1424 			retval = ext2fs_extent_replace(handle, 0, &next_extent);
1425 		} else if (logical < extent.e_lblk)
1426 			retval = ext2fs_extent_insert(handle, 0, &newextent);
1427 		else
1428 			retval = ext2fs_extent_insert(handle,
1429 				      EXT2_EXTENT_INSERT_AFTER, &newextent);
1430 		if (retval)
1431 			goto done;
1432 		retval = ext2fs_extent_fix_parents(handle);
1433 		if (retval)
1434 			goto done;
1435 	} else if ((logical == extent.e_lblk) && (extent.e_len == 1))  {
1436 #ifdef DEBUG
1437 		printf("(re/un)mapping only block in extent\n");
1438 #endif
1439 		if (physical) {
1440 			retval = ext2fs_extent_replace(handle, 0, &newextent);
1441 		} else {
1442 			retval = ext2fs_extent_delete(handle, 0);
1443 			if (retval)
1444 				goto done;
1445 			ec = ext2fs_extent_fix_parents(handle);
1446 			if (ec != EXT2_ET_NO_CURRENT_NODE)
1447 				retval = ec;
1448 		}
1449 
1450 		if (retval)
1451 			goto done;
1452 	} else if (logical == extent.e_lblk + extent.e_len - 1)  {
1453 #ifdef DEBUG
1454 		printf("(re/un)mapping last block in extent\n");
1455 #endif
1456 		if (physical) {
1457 			if (has_next &&
1458 			    (logical == (next_extent.e_lblk - 1)) &&
1459 			    (physical == (next_extent.e_pblk - 1)) &&
1460 			    (new_uninit == next_uninit) &&
1461 			    ((int) next_extent.e_len < max_len - 1)) {
1462 				retval = ext2fs_extent_get(handle,
1463 					EXT2_EXTENT_NEXT_LEAF, &next_extent);
1464 				if (retval)
1465 					goto done;
1466 				next_extent.e_len++;
1467 				next_extent.e_lblk--;
1468 				next_extent.e_pblk--;
1469 				retval = ext2fs_extent_replace(handle, 0,
1470 							       &next_extent);
1471 				if (retval)
1472 					goto done;
1473 			} else
1474 				retval = ext2fs_extent_insert(handle,
1475 				      EXT2_EXTENT_INSERT_AFTER, &newextent);
1476 			if (retval)
1477 				goto done;
1478 			retval = ext2fs_extent_fix_parents(handle);
1479 			if (retval)
1480 				goto done;
1481 			/*
1482 			 * Now pointing at inserted extent; move back to prev.
1483 			 *
1484 			 * We cannot use EXT2_EXTENT_PREV to go back; note the
1485 			 * subtlety in the comment for fix_parents().
1486 			 */
1487 			retval = ext2fs_extent_goto(handle, logical);
1488 			if (retval)
1489 				goto done;
1490 			retval = ext2fs_extent_get(handle,
1491 						   EXT2_EXTENT_CURRENT,
1492 						   &extent);
1493 			if (retval)
1494 				goto done;
1495 		}
1496 		extent.e_len--;
1497 		retval = ext2fs_extent_replace(handle, 0, &extent);
1498 		if (retval)
1499 			goto done;
1500 	} else if (logical == extent.e_lblk) {
1501 #ifdef DEBUG
1502 		printf("(re/un)mapping first block in extent\n");
1503 #endif
1504 		if (physical) {
1505 			if (has_prev &&
1506 			    (logical == (prev_extent.e_lblk +
1507 					 prev_extent.e_len)) &&
1508 			    (physical == (prev_extent.e_pblk +
1509 					  prev_extent.e_len)) &&
1510 			    (new_uninit == prev_uninit) &&
1511 			    ((int) prev_extent.e_len < max_len-1)) {
1512 				retval = ext2fs_extent_get(handle,
1513 					EXT2_EXTENT_PREV_LEAF, &prev_extent);
1514 				if (retval)
1515 					goto done;
1516 				prev_extent.e_len++;
1517 				retval = ext2fs_extent_replace(handle, 0,
1518 							       &prev_extent);
1519 			} else
1520 				retval = ext2fs_extent_insert(handle,
1521 							      0, &newextent);
1522 			if (retval)
1523 				goto done;
1524 			retval = ext2fs_extent_fix_parents(handle);
1525 			if (retval)
1526 				goto done;
1527 			retval = ext2fs_extent_get(handle,
1528 						   EXT2_EXTENT_NEXT_LEAF,
1529 						   &extent);
1530 			if (retval)
1531 				goto done;
1532 		}
1533 		extent.e_pblk++;
1534 		extent.e_lblk++;
1535 		extent.e_len--;
1536 		retval = ext2fs_extent_replace(handle, 0, &extent);
1537 		if (retval)
1538 			goto done;
1539 		retval = ext2fs_extent_fix_parents(handle);
1540 		if (retval)
1541 			goto done;
1542 	} else {
1543 		__u32	save_length;
1544 		blk64_t	save_lblk;
1545 		struct ext2fs_extent save_extent;
1546 		errcode_t r2;
1547 
1548 #ifdef DEBUG
1549 		printf("(re/un)mapping in middle of extent\n");
1550 #endif
1551 		/* need to split this extent; later */
1552 		save_lblk = extent.e_lblk;
1553 		save_length = extent.e_len;
1554 		save_extent = extent;
1555 
1556 		/* shorten pre-split extent */
1557 		extent.e_len = (logical - extent.e_lblk);
1558 		retval = ext2fs_extent_replace(handle, 0, &extent);
1559 		if (retval)
1560 			goto done;
1561 		/* insert our new extent, if any */
1562 		if (physical) {
1563 			/* insert new extent after current */
1564 			retval = ext2fs_extent_insert(handle,
1565 					EXT2_EXTENT_INSERT_AFTER, &newextent);
1566 			if (retval) {
1567 				r2 = ext2fs_extent_goto(handle, save_lblk);
1568 				if (r2 == 0)
1569 					(void)ext2fs_extent_replace(handle, 0,
1570 							      &save_extent);
1571 				goto done;
1572 			}
1573 		}
1574 		/* add post-split extent */
1575 		extent.e_pblk += extent.e_len + 1;
1576 		extent.e_lblk += extent.e_len + 1;
1577 		extent.e_len = save_length - extent.e_len - 1;
1578 		retval = ext2fs_extent_insert(handle,
1579 				EXT2_EXTENT_INSERT_AFTER, &extent);
1580 		if (retval) {
1581 			if (physical) {
1582 				r2 = ext2fs_extent_goto(handle,
1583 							newextent.e_lblk);
1584 				if (r2 == 0)
1585 					(void)ext2fs_extent_delete(handle, 0);
1586 			}
1587 			r2 = ext2fs_extent_goto(handle, save_lblk);
1588 			if (r2 == 0)
1589 				(void)ext2fs_extent_replace(handle, 0,
1590 							    &save_extent);
1591 			goto done;
1592 		}
1593 	}
1594 
1595 done:
1596 	/* get handle back to its position */
1597 	if (orig_height > handle->max_depth)
1598 		orig_height = handle->max_depth; /* In case we shortened the tree */
1599 	ext2fs_extent_goto2(handle, orig_height, orig_lblk);
1600 	return retval;
1601 }
1602 
ext2fs_extent_delete(ext2_extent_handle_t handle,int flags)1603 errcode_t ext2fs_extent_delete(ext2_extent_handle_t handle, int flags)
1604 {
1605 	struct extent_path		*path;
1606 	char 				*cp;
1607 	struct ext3_extent_header	*eh;
1608 	errcode_t			retval = 0;
1609 
1610 	EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EXTENT_HANDLE);
1611 
1612 	if (!(handle->fs->flags & EXT2_FLAG_RW))
1613 		return EXT2_ET_RO_FILSYS;
1614 
1615 	if (!handle->path)
1616 		return EXT2_ET_NO_CURRENT_NODE;
1617 
1618 #ifdef DEBUG
1619 	{
1620 		struct ext2fs_extent	extent;
1621 
1622 		retval = ext2fs_extent_get(handle, EXT2_EXTENT_CURRENT,
1623 					   &extent);
1624 		if (retval == 0) {
1625 			printf("extent delete %u ", handle->ino);
1626 			dbg_print_extent(0, &extent);
1627 		}
1628 	}
1629 #endif
1630 
1631 	path = handle->path + handle->level;
1632 	if (!path->curr)
1633 		return EXT2_ET_NO_CURRENT_NODE;
1634 
1635 	cp = path->curr;
1636 
1637 	/* Sanity check before memmove() */
1638 	if (path->left < 0)
1639 		return EXT2_ET_EXTENT_LEAF_BAD;
1640 
1641 	if (path->left) {
1642 		memmove(cp, cp + sizeof(struct ext3_extent_idx),
1643 			path->left * sizeof(struct ext3_extent_idx));
1644 		path->left--;
1645 	} else {
1646 		struct ext3_extent_idx	*ix = path->curr;
1647 		ix--;
1648 		path->curr = ix;
1649 	}
1650 	if (--path->entries == 0)
1651 		path->curr = 0;
1652 
1653 	/* if non-root node has no entries left, remove it & parent ptr to it */
1654 	if (path->entries == 0 && handle->level) {
1655 		if (!(flags & EXT2_EXTENT_DELETE_KEEP_EMPTY)) {
1656 			struct ext2fs_extent	extent;
1657 
1658 			retval = ext2fs_extent_get(handle, EXT2_EXTENT_UP,
1659 								&extent);
1660 			if (retval)
1661 				return retval;
1662 
1663 			retval = ext2fs_extent_delete(handle, flags);
1664 			handle->inode->i_blocks -=
1665 				(handle->fs->blocksize *
1666 				 EXT2FS_CLUSTER_RATIO(handle->fs)) / 512;
1667 			retval = ext2fs_write_inode(handle->fs, handle->ino,
1668 						    handle->inode);
1669 			ext2fs_block_alloc_stats2(handle->fs,
1670 						  extent.e_pblk, -1);
1671 		}
1672 	} else {
1673 		eh = (struct ext3_extent_header *) path->buf;
1674 		eh->eh_entries = ext2fs_cpu_to_le16(path->entries);
1675 		if ((path->entries == 0) && (handle->level == 0)) {
1676 			eh->eh_depth = 0;
1677 			handle->max_depth = 0;
1678 		}
1679 		retval = update_path(handle);
1680 	}
1681 	return retval;
1682 }
1683 
ext2fs_extent_get_info(ext2_extent_handle_t handle,struct ext2_extent_info * info)1684 errcode_t ext2fs_extent_get_info(ext2_extent_handle_t handle,
1685 				 struct ext2_extent_info *info)
1686 {
1687 	struct extent_path		*path;
1688 
1689 	EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EXTENT_HANDLE);
1690 
1691 	memset(info, 0, sizeof(struct ext2_extent_info));
1692 
1693 	path = handle->path + handle->level;
1694 	if (path) {
1695 		if (path->curr)
1696 			info->curr_entry = ((char *) path->curr - path->buf) /
1697 				sizeof(struct ext3_extent_idx);
1698 		else
1699 			info->curr_entry = 0;
1700 		info->num_entries = path->entries;
1701 		info->max_entries = path->max_entries;
1702 		info->bytes_avail = (path->max_entries - path->entries) *
1703 			sizeof(struct ext3_extent);
1704 	}
1705 
1706 	info->curr_level = handle->level;
1707 	info->max_depth = handle->max_depth;
1708 	info->max_lblk = EXT_MAX_EXTENT_LBLK;
1709 	info->max_pblk = EXT_MAX_EXTENT_PBLK;
1710 	info->max_len = EXT_INIT_MAX_LEN;
1711 	info->max_uninit_len = EXT_UNINIT_MAX_LEN;
1712 
1713 	return 0;
1714 }
1715 
ul_log2(unsigned long arg)1716 static int ul_log2(unsigned long arg)
1717 {
1718 	int	l = 0;
1719 
1720 	arg >>= 1;
1721 	while (arg) {
1722 		l++;
1723 		arg >>= 1;
1724 	}
1725 	return l;
1726 }
1727 
ext2fs_max_extent_depth(ext2_extent_handle_t handle)1728 size_t ext2fs_max_extent_depth(ext2_extent_handle_t handle)
1729 {
1730 	size_t iblock_sz = sizeof(((struct ext2_inode *)NULL)->i_block);
1731 	size_t iblock_extents = (iblock_sz - sizeof(struct ext3_extent_header)) /
1732 				sizeof(struct ext3_extent);
1733 	size_t extents_per_block = (handle->fs->blocksize -
1734 				    sizeof(struct ext3_extent_header)) /
1735 				   sizeof(struct ext3_extent);
1736 	static unsigned int last_blocksize = 0;
1737 	static size_t last_result = 0;
1738 
1739 	if (last_blocksize && last_blocksize == handle->fs->blocksize)
1740 		return last_result;
1741 
1742 	last_result = 1 + ((ul_log2(EXT_MAX_EXTENT_LBLK) - ul_log2(iblock_extents)) /
1743 		    ul_log2(extents_per_block));
1744 	last_blocksize = handle->fs->blocksize;
1745 	return last_result;
1746 }
1747 
ext2fs_fix_extents_checksums(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode)1748 errcode_t ext2fs_fix_extents_checksums(ext2_filsys fs, ext2_ino_t ino,
1749 				       struct ext2_inode *inode)
1750 {
1751 	ext2_extent_handle_t	handle;
1752 	struct ext2fs_extent	extent;
1753 	errcode_t		errcode;
1754 	int    			save_flags = fs->flags;
1755 
1756 	if (!ext2fs_has_feature_metadata_csum(fs->super) ||
1757 	    (inode && !(inode->i_flags & EXT4_EXTENTS_FL)))
1758 		return 0;
1759 
1760 	errcode = ext2fs_extent_open2(fs, ino, inode, &handle);
1761 	if (errcode) {
1762 		if (errcode == EXT2_ET_INODE_NOT_EXTENT)
1763 			errcode = 0;
1764 		return errcode;
1765 	}
1766 
1767 	fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
1768 	errcode = ext2fs_extent_get(handle, EXT2_EXTENT_ROOT, &extent);
1769 	if (errcode)
1770 		goto out;
1771 
1772 	do {
1773 		/* Skip to the end of a block of leaf nodes */
1774 		if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
1775 			errcode = ext2fs_extent_get(handle,
1776 						    EXT2_EXTENT_LAST_SIB,
1777 						    &extent);
1778 			if (errcode)
1779 				break;
1780 		}
1781 
1782 		errcode = ext2fs_extent_get(handle, EXT2_EXTENT_NEXT, &extent);
1783 		if (errcode == EXT2_ET_EXTENT_CSUM_INVALID)
1784 			errcode = update_path(handle);
1785 	} while (errcode == 0);
1786 
1787 out:
1788 	/* Ok if we run off the end */
1789 	if (errcode == EXT2_ET_EXTENT_NO_NEXT)
1790 		errcode = 0;
1791 	ext2fs_extent_free(handle);
1792 	fs->flags = save_flags;
1793 	return errcode;
1794 }
1795 
ext2fs_decode_extent(struct ext2fs_extent * to,void * addr,int len)1796 errcode_t ext2fs_decode_extent(struct ext2fs_extent *to, void *addr, int len)
1797 {
1798 	struct ext3_extent *from = (struct ext3_extent *)addr;
1799 
1800 	if (len != sizeof(struct ext3_extent))
1801 		return EXT2_ET_INVALID_ARGUMENT;
1802 
1803 	to->e_pblk = ext2fs_le32_to_cpu(from->ee_start) +
1804 		((__u64) ext2fs_le16_to_cpu(from->ee_start_hi)
1805 			<< 32);
1806 	to->e_lblk = ext2fs_le32_to_cpu(from->ee_block);
1807 	to->e_len = ext2fs_le16_to_cpu(from->ee_len);
1808 	to->e_flags = EXT2_EXTENT_FLAGS_LEAF;
1809 	if (to->e_len > EXT_INIT_MAX_LEN) {
1810 		to->e_len -= EXT_INIT_MAX_LEN;
1811 		to->e_flags |= EXT2_EXTENT_FLAGS_UNINIT;
1812 	}
1813 
1814 	return 0;
1815 }
1816 
ext2fs_count_blocks(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode,blk64_t * ret_count)1817 errcode_t ext2fs_count_blocks(ext2_filsys fs, ext2_ino_t ino,
1818 			      struct ext2_inode *inode, blk64_t *ret_count)
1819 {
1820 	ext2_extent_handle_t	handle = NULL;
1821 	struct ext2fs_extent	extent;
1822 	errcode_t		errcode;
1823 	int			i;
1824 	blk64_t			blkcount = 0;
1825 	blk64_t			*intermediate_nodes;
1826 
1827 	errcode = ext2fs_extent_open2(fs, ino, inode, &handle);
1828 	if (errcode)
1829 		goto out;
1830 
1831 	errcode = ext2fs_extent_get(handle, EXT2_EXTENT_ROOT, &extent);
1832 	if (errcode)
1833 		goto out;
1834 
1835 	errcode = ext2fs_get_array(handle->max_depth, sizeof(blk64_t),
1836 				   &intermediate_nodes);
1837 	if (errcode)
1838 		goto out;
1839 
1840 	blkcount = handle->level;
1841 	while (!errcode) {
1842 		if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
1843 			blkcount += extent.e_len;
1844 			for (i = 0; i < handle->level; i++) {
1845 				if (intermediate_nodes[i] !=
1846 					handle->path[i].end_blk) {
1847 					blkcount++;
1848 					intermediate_nodes[i] =
1849 						handle->path[i].end_blk;
1850 				}
1851 			}
1852 		}
1853 		errcode = ext2fs_extent_get(handle, EXT2_EXTENT_NEXT, &extent);
1854 	}
1855 	ext2fs_free_mem(&intermediate_nodes);
1856 out:
1857 	*ret_count = blkcount;
1858 	ext2fs_extent_free(handle);
1859 
1860 	return 0;
1861 }
1862 
1863 #ifdef DEBUG
1864 /*
1865  * Override debugfs's prompt
1866  */
1867 const char *debug_prog_name = "tst_extents";
1868 
1869 #endif
1870 
1871