• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * inode.c --- utility routines to read and write inodes
3  *
4  * Copyright (C) 1993, 1994, 1995, 1996, 1997 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 #include <time.h>
22 #if HAVE_SYS_STAT_H
23 #include <sys/stat.h>
24 #endif
25 #if HAVE_SYS_TYPES_H
26 #include <sys/types.h>
27 #endif
28 
29 #include "ext2_fs.h"
30 #include "ext2fsP.h"
31 #include "e2image.h"
32 
33 #define IBLOCK_STATUS_CSUMS_OK	1
34 #define IBLOCK_STATUS_INSANE	2
35 #define SCAN_BLOCK_STATUS(scan)	((scan)->temp_buffer + (scan)->inode_size)
36 
37 struct ext2_struct_inode_scan {
38 	errcode_t		magic;
39 	ext2_filsys		fs;
40 	ext2_ino_t		current_inode;
41 	blk64_t			current_block;
42 	dgrp_t			current_group;
43 	ext2_ino_t		inodes_left;
44 	blk_t			blocks_left;
45 	dgrp_t			groups_left;
46 	blk_t			inode_buffer_blocks;
47 	char *			inode_buffer;
48 	int			inode_size;
49 	char *			ptr;
50 	int			bytes_left;
51 	char			*temp_buffer;
52 	errcode_t		(*done_group)(ext2_filsys fs,
53 					      ext2_inode_scan scan,
54 					      dgrp_t group,
55 					      void * priv_data);
56 	void *			done_group_data;
57 	int			bad_block_ptr;
58 	int			scan_flags;
59 	int			reserved[6];
60 };
61 
62 /*
63  * This routine flushes the icache, if it exists.
64  */
ext2fs_flush_icache(ext2_filsys fs)65 errcode_t ext2fs_flush_icache(ext2_filsys fs)
66 {
67 	unsigned	i;
68 
69 	if (!fs->icache)
70 		return 0;
71 
72 	for (i=0; i < fs->icache->cache_size; i++)
73 		fs->icache->cache[i].ino = 0;
74 
75 	fs->icache->buffer_blk = 0;
76 	return 0;
77 }
78 
79 /*
80  * Free the inode cache structure
81  */
ext2fs_free_inode_cache(struct ext2_inode_cache * icache)82 void ext2fs_free_inode_cache(struct ext2_inode_cache *icache)
83 {
84 	unsigned i;
85 
86 	if (--icache->refcount)
87 		return;
88 	if (icache->buffer)
89 		ext2fs_free_mem(&icache->buffer);
90 	for (i = 0; i < icache->cache_size; i++)
91 		ext2fs_free_mem(&icache->cache[i].inode);
92 	if (icache->cache)
93 		ext2fs_free_mem(&icache->cache);
94 	icache->buffer_blk = 0;
95 	ext2fs_free_mem(&icache);
96 }
97 
ext2fs_create_inode_cache(ext2_filsys fs,unsigned int cache_size)98 errcode_t ext2fs_create_inode_cache(ext2_filsys fs, unsigned int cache_size)
99 {
100 	unsigned	i;
101 	errcode_t	retval;
102 
103 	if (fs->icache)
104 		return 0;
105 	retval = ext2fs_get_mem(sizeof(struct ext2_inode_cache), &fs->icache);
106 	if (retval)
107 		return retval;
108 
109 	memset(fs->icache, 0, sizeof(struct ext2_inode_cache));
110 	retval = ext2fs_get_mem(fs->blocksize, &fs->icache->buffer);
111 	if (retval)
112 		goto errout;
113 
114 	fs->icache->buffer_blk = 0;
115 	fs->icache->cache_last = -1;
116 	fs->icache->cache_size = cache_size;
117 	fs->icache->refcount = 1;
118 	retval = ext2fs_get_array(fs->icache->cache_size,
119 				  sizeof(struct ext2_inode_cache_ent),
120 				  &fs->icache->cache);
121 	if (retval)
122 		goto errout;
123 
124 	for (i = 0; i < fs->icache->cache_size; i++) {
125 		retval = ext2fs_get_mem(EXT2_INODE_SIZE(fs->super),
126 					&fs->icache->cache[i].inode);
127 		if (retval)
128 			goto errout;
129 	}
130 
131 	ext2fs_flush_icache(fs);
132 	return 0;
133 errout:
134 	ext2fs_free_inode_cache(fs->icache);
135 	fs->icache = 0;
136 	return retval;
137 }
138 
ext2fs_open_inode_scan(ext2_filsys fs,int buffer_blocks,ext2_inode_scan * ret_scan)139 errcode_t ext2fs_open_inode_scan(ext2_filsys fs, int buffer_blocks,
140 				 ext2_inode_scan *ret_scan)
141 {
142 	ext2_inode_scan	scan;
143 	errcode_t	retval;
144 	errcode_t (*save_get_blocks)(ext2_filsys f, ext2_ino_t ino, blk_t *blocks);
145 
146 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
147 
148 	/*
149 	 * If fs->badblocks isn't set, then set it --- since the inode
150 	 * scanning functions require it.
151 	 */
152 	if (fs->badblocks == 0) {
153 		/*
154 		 * Temporarily save fs->get_blocks and set it to zero,
155 		 * for compatibility with old e2fsck's.
156 		 */
157 		save_get_blocks = fs->get_blocks;
158 		fs->get_blocks = 0;
159 		retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
160 		if (retval && fs->badblocks) {
161 			ext2fs_badblocks_list_free(fs->badblocks);
162 			fs->badblocks = 0;
163 		}
164 		fs->get_blocks = save_get_blocks;
165 	}
166 
167 	retval = ext2fs_get_mem(sizeof(struct ext2_struct_inode_scan), &scan);
168 	if (retval)
169 		return retval;
170 	memset(scan, 0, sizeof(struct ext2_struct_inode_scan));
171 
172 	scan->magic = EXT2_ET_MAGIC_INODE_SCAN;
173 	scan->fs = fs;
174 	scan->inode_size = EXT2_INODE_SIZE(fs->super);
175 	scan->bytes_left = 0;
176 	scan->current_group = 0;
177 	scan->groups_left = fs->group_desc_count - 1;
178 	scan->inode_buffer_blocks = buffer_blocks ? buffer_blocks :
179 				    EXT2_INODE_SCAN_DEFAULT_BUFFER_BLOCKS;
180 	scan->current_block = ext2fs_inode_table_loc(scan->fs,
181 						     scan->current_group);
182 	scan->inodes_left = EXT2_INODES_PER_GROUP(scan->fs->super);
183 	scan->blocks_left = scan->fs->inode_blocks_per_group;
184 	if (ext2fs_has_group_desc_csum(fs)) {
185 		__u32 unused = ext2fs_bg_itable_unused(fs, scan->current_group);
186 		if (scan->inodes_left > unused)
187 			scan->inodes_left -= unused;
188 		else
189 			scan->inodes_left = 0;
190 		scan->blocks_left =
191 			(scan->inodes_left +
192 			 (fs->blocksize / scan->inode_size - 1)) *
193 			scan->inode_size / fs->blocksize;
194 	}
195 	retval = io_channel_alloc_buf(fs->io, scan->inode_buffer_blocks,
196 				      &scan->inode_buffer);
197 	scan->done_group = 0;
198 	scan->done_group_data = 0;
199 	scan->bad_block_ptr = 0;
200 	if (retval) {
201 		ext2fs_free_mem(&scan);
202 		return retval;
203 	}
204 	retval = ext2fs_get_mem(scan->inode_size + scan->inode_buffer_blocks,
205 				&scan->temp_buffer);
206 	if (retval) {
207 		ext2fs_free_mem(&scan->inode_buffer);
208 		ext2fs_free_mem(&scan);
209 		return retval;
210 	}
211 	memset(SCAN_BLOCK_STATUS(scan), 0, scan->inode_buffer_blocks);
212 	if (scan->fs->badblocks && scan->fs->badblocks->num)
213 		scan->scan_flags |= EXT2_SF_CHK_BADBLOCKS;
214 	if (ext2fs_has_group_desc_csum(fs))
215 		scan->scan_flags |= EXT2_SF_DO_LAZY;
216 	*ret_scan = scan;
217 	return 0;
218 }
219 
ext2fs_close_inode_scan(ext2_inode_scan scan)220 void ext2fs_close_inode_scan(ext2_inode_scan scan)
221 {
222 	if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
223 		return;
224 
225 	ext2fs_free_mem(&scan->inode_buffer);
226 	scan->inode_buffer = NULL;
227 	ext2fs_free_mem(&scan->temp_buffer);
228 	scan->temp_buffer = NULL;
229 	ext2fs_free_mem(&scan);
230 	return;
231 }
232 
ext2fs_set_inode_callback(ext2_inode_scan scan,errcode_t (* done_group)(ext2_filsys fs,ext2_inode_scan scan,dgrp_t group,void * priv_data),void * done_group_data)233 void ext2fs_set_inode_callback(ext2_inode_scan scan,
234 			       errcode_t (*done_group)(ext2_filsys fs,
235 						       ext2_inode_scan scan,
236 						       dgrp_t group,
237 						       void * priv_data),
238 			       void *done_group_data)
239 {
240 	if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
241 		return;
242 
243 	scan->done_group = done_group;
244 	scan->done_group_data = done_group_data;
245 }
246 
ext2fs_inode_scan_flags(ext2_inode_scan scan,int set_flags,int clear_flags)247 int ext2fs_inode_scan_flags(ext2_inode_scan scan, int set_flags,
248 			    int clear_flags)
249 {
250 	int	old_flags;
251 
252 	if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
253 		return 0;
254 
255 	old_flags = scan->scan_flags;
256 	scan->scan_flags &= ~clear_flags;
257 	scan->scan_flags |= set_flags;
258 	return old_flags;
259 }
260 
261 /*
262  * This function is called by ext2fs_get_next_inode when it needs to
263  * get ready to read in a new blockgroup.
264  */
get_next_blockgroup(ext2_inode_scan scan)265 static errcode_t get_next_blockgroup(ext2_inode_scan scan)
266 {
267 	ext2_filsys fs = scan->fs;
268 
269 	scan->current_group++;
270 	scan->groups_left--;
271 
272 	scan->current_block = ext2fs_inode_table_loc(scan->fs,
273 						     scan->current_group);
274 	scan->current_inode = scan->current_group *
275 		EXT2_INODES_PER_GROUP(fs->super);
276 
277 	scan->bytes_left = 0;
278 	scan->inodes_left = EXT2_INODES_PER_GROUP(fs->super);
279 	scan->blocks_left = fs->inode_blocks_per_group;
280 	if (ext2fs_has_group_desc_csum(fs)) {
281 		__u32 unused = ext2fs_bg_itable_unused(fs, scan->current_group);
282 		if (scan->inodes_left > unused)
283 			scan->inodes_left -= unused;
284 		else
285 			scan->inodes_left = 0;
286 		scan->blocks_left =
287 			(scan->inodes_left +
288 			 (fs->blocksize / scan->inode_size - 1)) *
289 			scan->inode_size / fs->blocksize;
290 	}
291 
292 	return 0;
293 }
294 
ext2fs_inode_scan_goto_blockgroup(ext2_inode_scan scan,int group)295 errcode_t ext2fs_inode_scan_goto_blockgroup(ext2_inode_scan scan,
296 					    int	group)
297 {
298 	scan->current_group = group - 1;
299 	scan->groups_left = scan->fs->group_desc_count - group;
300 	return get_next_blockgroup(scan);
301 }
302 
303 /*
304  * This function is called by get_next_blocks() to check for bad
305  * blocks in the inode table.
306  *
307  * This function assumes that badblocks_list->list is sorted in
308  * increasing order.
309  */
check_for_inode_bad_blocks(ext2_inode_scan scan,blk64_t * num_blocks)310 static errcode_t check_for_inode_bad_blocks(ext2_inode_scan scan,
311 					    blk64_t *num_blocks)
312 {
313 	blk64_t	blk = scan->current_block;
314 	badblocks_list	bb = scan->fs->badblocks;
315 
316 	/*
317 	 * If the inode table is missing, then obviously there are no
318 	 * bad blocks.  :-)
319 	 */
320 	if (blk == 0)
321 		return 0;
322 
323 	/*
324 	 * If the current block is greater than the bad block listed
325 	 * in the bad block list, then advance the pointer until this
326 	 * is no longer the case.  If we run out of bad blocks, then
327 	 * we don't need to do any more checking!
328 	 */
329 	while (blk > bb->list[scan->bad_block_ptr]) {
330 		if (++scan->bad_block_ptr >= bb->num) {
331 			scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
332 			return 0;
333 		}
334 	}
335 
336 	/*
337 	 * If the current block is equal to the bad block listed in
338 	 * the bad block list, then handle that one block specially.
339 	 * (We could try to handle runs of bad blocks, but that
340 	 * only increases CPU efficiency by a small amount, at the
341 	 * expense of a huge expense of code complexity, and for an
342 	 * uncommon case at that.)
343 	 */
344 	if (blk == bb->list[scan->bad_block_ptr]) {
345 		scan->scan_flags |= EXT2_SF_BAD_INODE_BLK;
346 		*num_blocks = 1;
347 		if (++scan->bad_block_ptr >= bb->num)
348 			scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
349 		return 0;
350 	}
351 
352 	/*
353 	 * If there is a bad block in the range that we're about to
354 	 * read in, adjust the number of blocks to read so that we we
355 	 * don't read in the bad block.  (Then the next block to read
356 	 * will be the bad block, which is handled in the above case.)
357 	 */
358 	if ((blk + *num_blocks) > bb->list[scan->bad_block_ptr])
359 		*num_blocks = (int) (bb->list[scan->bad_block_ptr] - blk);
360 
361 	return 0;
362 }
363 
block_map_looks_insane(ext2_filsys fs,struct ext2_inode_large * inode)364 static int block_map_looks_insane(ext2_filsys fs,
365 				  struct ext2_inode_large *inode)
366 {
367 	unsigned int i, bad;
368 
369 	/* We're only interested in block mapped files, dirs, and symlinks */
370 	if ((inode->i_flags & EXT4_INLINE_DATA_FL) ||
371 	    (inode->i_flags & EXT4_EXTENTS_FL))
372 		return 0;
373 	if (!LINUX_S_ISREG(inode->i_mode) &&
374 	    !LINUX_S_ISLNK(inode->i_mode) &&
375 	    !LINUX_S_ISDIR(inode->i_mode))
376 		return 0;
377 	if (LINUX_S_ISLNK(inode->i_mode) &&
378 	    EXT2_I_SIZE(inode) <= sizeof(inode->i_block))
379 		return 0;
380 
381 	/* Unused inodes probably aren't insane */
382 	if (inode->i_links_count == 0)
383 		return 0;
384 
385 	/* See if more than half the block maps are insane */
386 	for (i = 0, bad = 0; i < EXT2_N_BLOCKS; i++)
387 		if (inode->i_block[i] != 0 &&
388 		    (inode->i_block[i] < fs->super->s_first_data_block ||
389 		     inode->i_block[i] >= ext2fs_blocks_count(fs->super)))
390 			bad++;
391 	return bad > EXT2_N_BLOCKS / 2;
392 }
393 
extent_head_looks_insane(struct ext2_inode_large * inode)394 static int extent_head_looks_insane(struct ext2_inode_large *inode)
395 {
396 	if (!(inode->i_flags & EXT4_EXTENTS_FL) ||
397 	    ext2fs_extent_header_verify(inode->i_block,
398 					sizeof(inode->i_block)) == 0)
399 		return 0;
400 	return 1;
401 }
402 
403 /*
404  * Check all the inodes that we just read into the buffer.  Record what we
405  * find here -- currently, we can observe that all checksums are ok; more
406  * than half the inodes are insane; or no conclusions at all.
407  */
check_inode_block_sanity(ext2_inode_scan scan,blk64_t num_blocks)408 static void check_inode_block_sanity(ext2_inode_scan scan, blk64_t num_blocks)
409 {
410 	ext2_ino_t	ino, inodes_to_scan;
411 	unsigned int	badness, checksum_failures;
412 	unsigned int	inodes_in_buf, inodes_per_block;
413 	char		*p;
414 	struct ext2_inode_large *inode;
415 	char		*block_status;
416 	unsigned int	blk, bad_csum;
417 
418 	if (!(scan->scan_flags & EXT2_SF_WARN_GARBAGE_INODES))
419 		return;
420 
421 	inodes_to_scan = scan->inodes_left;
422 	inodes_in_buf = num_blocks * scan->fs->blocksize / scan->inode_size;
423 	if (inodes_to_scan > inodes_in_buf)
424 		inodes_to_scan = inodes_in_buf;
425 
426 	p = (char *) scan->inode_buffer;
427 	ino = scan->current_inode + 1;
428 	checksum_failures = badness = 0;
429 	block_status = SCAN_BLOCK_STATUS(scan);
430 	memset(block_status, 0, scan->inode_buffer_blocks);
431 	inodes_per_block = EXT2_INODES_PER_BLOCK(scan->fs->super);
432 
433 	if (inodes_per_block < 2)
434 		return;
435 
436 #ifdef WORDS_BIGENDIAN
437 	if (ext2fs_get_mem(EXT2_INODE_SIZE(scan->fs->super), &inode))
438 		return;
439 #endif
440 
441 	while (inodes_to_scan > 0) {
442 		blk = (p - (char *)scan->inode_buffer) / scan->fs->blocksize;
443 		bad_csum = ext2fs_inode_csum_verify(scan->fs, ino,
444 				(struct ext2_inode_large *) p) == 0;
445 
446 #ifdef WORDS_BIGENDIAN
447 		ext2fs_swap_inode_full(scan->fs,
448 			       (struct ext2_inode_large *) inode,
449 			       (struct ext2_inode_large *) p,
450 			       0, EXT2_INODE_SIZE(scan->fs->super));
451 #else
452 		inode = (struct ext2_inode_large *) p;
453 #endif
454 
455 		/* Is this inode insane? */
456 		if (bad_csum) {
457 			checksum_failures++;
458 			badness++;
459 		} else if (extent_head_looks_insane(inode) ||
460 			   block_map_looks_insane(scan->fs, inode))
461 			badness++;
462 
463 		/* If more than half are insane, declare the whole block bad */
464 		if (badness > inodes_per_block / 2) {
465 			unsigned int ino_adj;
466 
467 			block_status[blk] |= IBLOCK_STATUS_INSANE;
468 			ino_adj = inodes_per_block -
469 						((ino - 1) % inodes_per_block);
470 			if (ino_adj > inodes_to_scan)
471 				ino_adj = inodes_to_scan;
472 			inodes_to_scan -= ino_adj;
473 			p += scan->inode_size * ino_adj;
474 			ino += ino_adj;
475 			checksum_failures = badness = 0;
476 			continue;
477 		}
478 
479 		if ((ino % inodes_per_block) == 0) {
480 			if (checksum_failures == 0)
481 				block_status[blk] |= IBLOCK_STATUS_CSUMS_OK;
482 			checksum_failures = badness = 0;
483 		}
484 		inodes_to_scan--;
485 		p += scan->inode_size;
486 		ino++;
487 	};
488 
489 #ifdef WORDS_BIGENDIAN
490 	ext2fs_free_mem(&inode);
491 #endif
492 }
493 
494 /*
495  * This function is called by ext2fs_get_next_inode when it needs to
496  * read in more blocks from the current blockgroup's inode table.
497  */
get_next_blocks(ext2_inode_scan scan)498 static errcode_t get_next_blocks(ext2_inode_scan scan)
499 {
500 	blk64_t		num_blocks;
501 	errcode_t	retval;
502 
503 	/*
504 	 * Figure out how many blocks to read; we read at most
505 	 * inode_buffer_blocks, and perhaps less if there aren't that
506 	 * many blocks left to read.
507 	 */
508 	num_blocks = scan->inode_buffer_blocks;
509 	if (num_blocks > scan->blocks_left)
510 		num_blocks = scan->blocks_left;
511 
512 	/*
513 	 * If the past block "read" was a bad block, then mark the
514 	 * left-over extra bytes as also being bad.
515 	 */
516 	if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK) {
517 		if (scan->bytes_left)
518 			scan->scan_flags |= EXT2_SF_BAD_EXTRA_BYTES;
519 		scan->scan_flags &= ~EXT2_SF_BAD_INODE_BLK;
520 	}
521 
522 	/*
523 	 * Do inode bad block processing, if necessary.
524 	 */
525 	if (scan->scan_flags & EXT2_SF_CHK_BADBLOCKS) {
526 		retval = check_for_inode_bad_blocks(scan, &num_blocks);
527 		if (retval)
528 			return retval;
529 	}
530 
531 	if ((scan->scan_flags & EXT2_SF_BAD_INODE_BLK) ||
532 	    (scan->current_block == 0)) {
533 		memset(scan->inode_buffer, 0,
534 		       (size_t) num_blocks * scan->fs->blocksize);
535 	} else {
536 		retval = io_channel_read_blk64(scan->fs->io,
537 					     scan->current_block,
538 					     (int) num_blocks,
539 					     scan->inode_buffer);
540 		if (retval)
541 			return EXT2_ET_NEXT_INODE_READ;
542 	}
543 	check_inode_block_sanity(scan, num_blocks);
544 
545 	scan->ptr = scan->inode_buffer;
546 	scan->bytes_left = num_blocks * scan->fs->blocksize;
547 
548 	scan->blocks_left -= num_blocks;
549 	if (scan->current_block)
550 		scan->current_block += num_blocks;
551 
552 	return 0;
553 }
554 
555 #if 0
556 /*
557  * Returns 1 if the entire inode_buffer has a non-zero size and
558  * contains all zeros.  (Not just deleted inodes, since that means
559  * that part of the inode table was used at one point; we want all
560  * zeros, which means that the inode table is pristine.)
561  */
562 static inline int is_empty_scan(ext2_inode_scan scan)
563 {
564 	int	i;
565 
566 	if (scan->bytes_left == 0)
567 		return 0;
568 
569 	for (i=0; i < scan->bytes_left; i++)
570 		if (scan->ptr[i])
571 			return 0;
572 	return 1;
573 }
574 #endif
575 
ext2fs_get_next_inode_full(ext2_inode_scan scan,ext2_ino_t * ino,struct ext2_inode * inode,int bufsize)576 errcode_t ext2fs_get_next_inode_full(ext2_inode_scan scan, ext2_ino_t *ino,
577 				     struct ext2_inode *inode, int bufsize)
578 {
579 	errcode_t	retval;
580 	int		extra_bytes = 0;
581 	int		length;
582 	struct ext2_inode_large	*iptr = (struct ext2_inode_large *)inode;
583 	char		*iblock_status;
584 	unsigned int	iblk;
585 
586 	EXT2_CHECK_MAGIC(scan, EXT2_ET_MAGIC_INODE_SCAN);
587 	length = EXT2_INODE_SIZE(scan->fs->super);
588 	iblock_status = SCAN_BLOCK_STATUS(scan);
589 
590 	/*
591 	 * Do we need to start reading a new block group?
592 	 */
593 	if (scan->inodes_left <= 0) {
594 	force_new_group:
595 		if (scan->done_group) {
596 			retval = (scan->done_group)
597 				(scan->fs, scan, scan->current_group,
598 				 scan->done_group_data);
599 			if (retval)
600 				return retval;
601 		}
602 		if (scan->groups_left <= 0) {
603 			*ino = 0;
604 			return 0;
605 		}
606 		retval = get_next_blockgroup(scan);
607 		if (retval)
608 			return retval;
609 	}
610 	/*
611 	 * These checks are done outside the above if statement so
612 	 * they can be done for block group #0.
613 	 */
614 	if ((scan->scan_flags & EXT2_SF_DO_LAZY) &&
615 	    (ext2fs_bg_flags_test(scan->fs, scan->current_group, EXT2_BG_INODE_UNINIT)
616 	     ))
617 		goto force_new_group;
618 	if (scan->inodes_left == 0)
619 		goto force_new_group;
620 	if (scan->current_block == 0) {
621 		if (scan->scan_flags & EXT2_SF_SKIP_MISSING_ITABLE) {
622 			goto force_new_group;
623 		} else
624 			return EXT2_ET_MISSING_INODE_TABLE;
625 	}
626 
627 
628 	/*
629 	 * Have we run out of space in the inode buffer?  If so, we
630 	 * need to read in more blocks.
631 	 */
632 	if (scan->bytes_left < scan->inode_size) {
633 		if (scan->bytes_left)
634 			memcpy(scan->temp_buffer, scan->ptr, scan->bytes_left);
635 		extra_bytes = scan->bytes_left;
636 
637 		retval = get_next_blocks(scan);
638 		if (retval)
639 			return retval;
640 #if 0
641 		/*
642 		 * XXX test  Need check for used inode somehow.
643 		 * (Note: this is hard.)
644 		 */
645 		if (is_empty_scan(scan))
646 			goto force_new_group;
647 #endif
648 	}
649 
650 	if (bufsize < length) {
651 		retval = ext2fs_get_mem(length, &iptr);
652 		if (retval)
653 			return retval;
654 	}
655 
656 	retval = 0;
657 	iblk = scan->current_inode % EXT2_INODES_PER_GROUP(scan->fs->super) /
658 				EXT2_INODES_PER_BLOCK(scan->fs->super) %
659 				scan->inode_buffer_blocks;
660 	if (extra_bytes) {
661 		memcpy(scan->temp_buffer+extra_bytes, scan->ptr,
662 		       scan->inode_size - extra_bytes);
663 		scan->ptr += scan->inode_size - extra_bytes;
664 		scan->bytes_left -= scan->inode_size - extra_bytes;
665 
666 		/* Verify the inode checksum. */
667 		if (!(iblock_status[iblk] & IBLOCK_STATUS_CSUMS_OK) &&
668 		    !(scan->fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) &&
669 		    !ext2fs_inode_csum_verify(scan->fs, scan->current_inode + 1,
670 				(struct ext2_inode_large *)scan->temp_buffer))
671 			retval = EXT2_ET_INODE_CSUM_INVALID;
672 
673 #ifdef WORDS_BIGENDIAN
674 		memset(iptr, 0, length);
675 		ext2fs_swap_inode_full(scan->fs,
676 			       (struct ext2_inode_large *) iptr,
677 			       (struct ext2_inode_large *) scan->temp_buffer,
678 			       0, length);
679 #else
680 		memcpy(iptr, scan->temp_buffer, length);
681 #endif
682 		if (scan->scan_flags & EXT2_SF_BAD_EXTRA_BYTES)
683 			retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
684 		scan->scan_flags &= ~EXT2_SF_BAD_EXTRA_BYTES;
685 	} else {
686 		/* Verify the inode checksum. */
687 		if (!(iblock_status[iblk] & IBLOCK_STATUS_CSUMS_OK) &&
688 		    !(scan->fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) &&
689 		    !ext2fs_inode_csum_verify(scan->fs, scan->current_inode + 1,
690 				(struct ext2_inode_large *)scan->ptr))
691 			retval = EXT2_ET_INODE_CSUM_INVALID;
692 
693 #ifdef WORDS_BIGENDIAN
694 		memset(iptr, 0, length);
695 		ext2fs_swap_inode_full(scan->fs,
696 				(struct ext2_inode_large *) iptr,
697 				(struct ext2_inode_large *) scan->ptr,
698 				0, length);
699 #else
700 		memcpy(iptr, scan->ptr, length);
701 #endif
702 		scan->ptr += scan->inode_size;
703 		scan->bytes_left -= scan->inode_size;
704 		if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK)
705 			retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
706 	}
707 	if ((iblock_status[iblk] & IBLOCK_STATUS_INSANE) &&
708 	    (retval == 0 || retval == EXT2_ET_INODE_CSUM_INVALID))
709 		retval = EXT2_ET_INODE_IS_GARBAGE;
710 
711 	scan->inodes_left--;
712 	scan->current_inode++;
713 	*ino = scan->current_inode;
714 	if (iptr != (struct ext2_inode_large *)inode) {
715 		memcpy(inode, iptr, bufsize);
716 		ext2fs_free_mem(&iptr);
717 	}
718 	return retval;
719 }
720 
ext2fs_get_next_inode(ext2_inode_scan scan,ext2_ino_t * ino,struct ext2_inode * inode)721 errcode_t ext2fs_get_next_inode(ext2_inode_scan scan, ext2_ino_t *ino,
722 				struct ext2_inode *inode)
723 {
724 	return ext2fs_get_next_inode_full(scan, ino, inode,
725 						sizeof(struct ext2_inode));
726 }
727 
728 /*
729  * Functions to read and write a single inode.
730  */
ext2fs_read_inode_full(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode,int bufsize)731 errcode_t ext2fs_read_inode_full(ext2_filsys fs, ext2_ino_t ino,
732 				 struct ext2_inode * inode, int bufsize)
733 {
734 	blk64_t		block_nr;
735 	unsigned long 	group, block, offset;
736 	char 		*ptr;
737 	errcode_t	retval;
738 	unsigned	i;
739 	int		clen, inodes_per_block;
740 	io_channel	io;
741 	int		length = EXT2_INODE_SIZE(fs->super);
742 	struct ext2_inode_large	*iptr;
743 	int		cache_slot, fail_csum;
744 
745 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
746 
747 	/* Check to see if user has an override function */
748 	if (fs->read_inode &&
749 	    ((bufsize == sizeof(struct ext2_inode)) ||
750 	     (EXT2_INODE_SIZE(fs->super) == sizeof(struct ext2_inode)))) {
751 		retval = (fs->read_inode)(fs, ino, inode);
752 		if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
753 			return retval;
754 	}
755 	if ((ino == 0) || (ino > fs->super->s_inodes_count))
756 		return EXT2_ET_BAD_INODE_NUM;
757 	/* Create inode cache if not present */
758 	if (!fs->icache) {
759 		retval = ext2fs_create_inode_cache(fs, 4);
760 		if (retval)
761 			return retval;
762 	}
763 	/* Check to see if it's in the inode cache */
764 	for (i = 0; i < fs->icache->cache_size; i++) {
765 		if (fs->icache->cache[i].ino == ino) {
766 			memcpy(inode, fs->icache->cache[i].inode,
767 			       (bufsize > length) ? length : bufsize);
768 			return 0;
769 		}
770 	}
771 	if (fs->flags & EXT2_FLAG_IMAGE_FILE) {
772 		inodes_per_block = fs->blocksize / EXT2_INODE_SIZE(fs->super);
773 		block_nr = ext2fs_le32_to_cpu(fs->image_header->offset_inode) / fs->blocksize;
774 		block_nr += (ino - 1) / inodes_per_block;
775 		offset = ((ino - 1) % inodes_per_block) *
776 			EXT2_INODE_SIZE(fs->super);
777 		io = fs->image_io;
778 	} else {
779 		group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
780 		if (group > fs->group_desc_count)
781 			return EXT2_ET_BAD_INODE_NUM;
782 		offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
783 			EXT2_INODE_SIZE(fs->super);
784 		block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
785 		if (!ext2fs_inode_table_loc(fs, (unsigned) group))
786 			return EXT2_ET_MISSING_INODE_TABLE;
787 		block_nr = ext2fs_inode_table_loc(fs, group) +
788 			block;
789 		io = fs->io;
790 	}
791 	offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
792 
793 	cache_slot = (fs->icache->cache_last + 1) % fs->icache->cache_size;
794 	iptr = (struct ext2_inode_large *)fs->icache->cache[cache_slot].inode;
795 
796 	ptr = (char *) iptr;
797 	while (length) {
798 		clen = length;
799 		if ((offset + length) > fs->blocksize)
800 			clen = fs->blocksize - offset;
801 
802 		if (block_nr != fs->icache->buffer_blk) {
803 			retval = io_channel_read_blk64(io, block_nr, 1,
804 						     fs->icache->buffer);
805 			if (retval)
806 				return retval;
807 			fs->icache->buffer_blk = block_nr;
808 		}
809 
810 		memcpy(ptr, ((char *) fs->icache->buffer) + (unsigned) offset,
811 		       clen);
812 
813 		offset = 0;
814 		length -= clen;
815 		ptr += clen;
816 		block_nr++;
817 	}
818 	length = EXT2_INODE_SIZE(fs->super);
819 
820 	/* Verify the inode checksum. */
821 	fail_csum = !ext2fs_inode_csum_verify(fs, ino, iptr);
822 
823 #ifdef WORDS_BIGENDIAN
824 	ext2fs_swap_inode_full(fs, (struct ext2_inode_large *) iptr,
825 			       (struct ext2_inode_large *) iptr,
826 			       0, length);
827 #endif
828 
829 	/* Update the inode cache bookkeeping */
830 	if (!fail_csum) {
831 		fs->icache->cache_last = cache_slot;
832 		fs->icache->cache[cache_slot].ino = ino;
833 	}
834 	memcpy(inode, iptr, (bufsize > length) ? length : bufsize);
835 
836 	if (!(fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) && fail_csum)
837 		return EXT2_ET_INODE_CSUM_INVALID;
838 
839 	return 0;
840 }
841 
ext2fs_read_inode(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode)842 errcode_t ext2fs_read_inode(ext2_filsys fs, ext2_ino_t ino,
843 			    struct ext2_inode * inode)
844 {
845 	return ext2fs_read_inode_full(fs, ino, inode,
846 					sizeof(struct ext2_inode));
847 }
848 
ext2fs_write_inode_full(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode,int bufsize)849 errcode_t ext2fs_write_inode_full(ext2_filsys fs, ext2_ino_t ino,
850 				  struct ext2_inode * inode, int bufsize)
851 {
852 	blk64_t block_nr;
853 	unsigned long group, block, offset;
854 	errcode_t retval = 0;
855 	struct ext2_inode_large *w_inode;
856 	char *ptr;
857 	unsigned i;
858 	int clen;
859 	int length = EXT2_INODE_SIZE(fs->super);
860 
861 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
862 
863 	/* Check to see if user provided an override function */
864 	if (fs->write_inode) {
865 		retval = (fs->write_inode)(fs, ino, inode);
866 		if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
867 			return retval;
868 	}
869 
870 	if ((ino == 0) || (ino > fs->super->s_inodes_count))
871 		return EXT2_ET_BAD_INODE_NUM;
872 
873 	/* Prepare our shadow buffer for read/modify/byteswap/write */
874 	retval = ext2fs_get_mem(length, &w_inode);
875 	if (retval)
876 		return retval;
877 
878 	if (bufsize < length) {
879 		int old_flags = fs->flags;
880 		fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
881 		retval = ext2fs_read_inode_full(fs, ino,
882 						(struct ext2_inode *)w_inode,
883 						length);
884 		fs->flags = (old_flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
885 			    (fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
886 		if (retval)
887 			goto errout;
888 	}
889 
890 	/* Check to see if the inode cache needs to be updated */
891 	if (fs->icache) {
892 		for (i=0; i < fs->icache->cache_size; i++) {
893 			if (fs->icache->cache[i].ino == ino) {
894 				memcpy(fs->icache->cache[i].inode, inode,
895 				       (bufsize > length) ? length : bufsize);
896 				break;
897 			}
898 		}
899 	} else {
900 		retval = ext2fs_create_inode_cache(fs, 4);
901 		if (retval)
902 			goto errout;
903 	}
904 	memcpy(w_inode, inode, (bufsize > length) ? length : bufsize);
905 
906 	if (!(fs->flags & EXT2_FLAG_RW)) {
907 		retval = EXT2_ET_RO_FILSYS;
908 		goto errout;
909 	}
910 
911 #ifdef WORDS_BIGENDIAN
912 	ext2fs_swap_inode_full(fs, w_inode, w_inode, 1, length);
913 #endif
914 
915 	retval = ext2fs_inode_csum_set(fs, ino, w_inode);
916 	if (retval)
917 		goto errout;
918 
919 	group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
920 	offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
921 		EXT2_INODE_SIZE(fs->super);
922 	block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
923 	if (!ext2fs_inode_table_loc(fs, (unsigned) group)) {
924 		retval = EXT2_ET_MISSING_INODE_TABLE;
925 		goto errout;
926 	}
927 	block_nr = ext2fs_inode_table_loc(fs, (unsigned) group) + block;
928 
929 	offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
930 
931 	ptr = (char *) w_inode;
932 
933 	while (length) {
934 		clen = length;
935 		if ((offset + length) > fs->blocksize)
936 			clen = fs->blocksize - offset;
937 
938 		if (fs->icache->buffer_blk != block_nr) {
939 			retval = io_channel_read_blk64(fs->io, block_nr, 1,
940 						     fs->icache->buffer);
941 			if (retval)
942 				goto errout;
943 			fs->icache->buffer_blk = block_nr;
944 		}
945 
946 
947 		memcpy((char *) fs->icache->buffer + (unsigned) offset,
948 		       ptr, clen);
949 
950 		retval = io_channel_write_blk64(fs->io, block_nr, 1,
951 					      fs->icache->buffer);
952 		if (retval)
953 			goto errout;
954 
955 		offset = 0;
956 		ptr += clen;
957 		length -= clen;
958 		block_nr++;
959 	}
960 
961 	fs->flags |= EXT2_FLAG_CHANGED;
962 errout:
963 	ext2fs_free_mem(&w_inode);
964 	return retval;
965 }
966 
ext2fs_write_inode(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode)967 errcode_t ext2fs_write_inode(ext2_filsys fs, ext2_ino_t ino,
968 			     struct ext2_inode *inode)
969 {
970 	return ext2fs_write_inode_full(fs, ino, inode,
971 				       sizeof(struct ext2_inode));
972 }
973 
974 /*
975  * This function should be called when writing a new inode.  It makes
976  * sure that extra part of large inodes is initialized properly.
977  */
ext2fs_write_new_inode(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode)978 errcode_t ext2fs_write_new_inode(ext2_filsys fs, ext2_ino_t ino,
979 				 struct ext2_inode *inode)
980 {
981 	struct ext2_inode	*buf;
982 	int 			size = EXT2_INODE_SIZE(fs->super);
983 	struct ext2_inode_large	*large_inode;
984 	errcode_t		retval;
985 	__u32 			t = fs->now ? fs->now : time(NULL);
986 
987 	if (!inode->i_ctime)
988 		inode->i_ctime = t;
989 	if (!inode->i_mtime)
990 		inode->i_mtime = t;
991 	if (!inode->i_atime)
992 		inode->i_atime = t;
993 
994 	if (size == sizeof(struct ext2_inode))
995 		return ext2fs_write_inode_full(fs, ino, inode,
996 					       sizeof(struct ext2_inode));
997 
998 	buf = malloc(size);
999 	if (!buf)
1000 		return ENOMEM;
1001 
1002 	memset(buf, 0, size);
1003 	*buf = *inode;
1004 
1005 	large_inode = (struct ext2_inode_large *) buf;
1006 	large_inode->i_extra_isize = sizeof(struct ext2_inode_large) -
1007 		EXT2_GOOD_OLD_INODE_SIZE;
1008 	if (!large_inode->i_crtime)
1009 		large_inode->i_crtime = t;
1010 
1011 	retval = ext2fs_write_inode_full(fs, ino, buf, size);
1012 	free(buf);
1013 	return retval;
1014 }
1015 
1016 
ext2fs_get_blocks(ext2_filsys fs,ext2_ino_t ino,blk_t * blocks)1017 errcode_t ext2fs_get_blocks(ext2_filsys fs, ext2_ino_t ino, blk_t *blocks)
1018 {
1019 	struct ext2_inode	inode;
1020 	int			i;
1021 	errcode_t		retval;
1022 
1023 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
1024 
1025 	if (ino > fs->super->s_inodes_count)
1026 		return EXT2_ET_BAD_INODE_NUM;
1027 
1028 	if (fs->get_blocks) {
1029 		if (!(*fs->get_blocks)(fs, ino, blocks))
1030 			return 0;
1031 	}
1032 	retval = ext2fs_read_inode(fs, ino, &inode);
1033 	if (retval)
1034 		return retval;
1035 	for (i=0; i < EXT2_N_BLOCKS; i++)
1036 		blocks[i] = inode.i_block[i];
1037 	return 0;
1038 }
1039 
ext2fs_check_directory(ext2_filsys fs,ext2_ino_t ino)1040 errcode_t ext2fs_check_directory(ext2_filsys fs, ext2_ino_t ino)
1041 {
1042 	struct	ext2_inode	inode;
1043 	errcode_t		retval;
1044 
1045 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
1046 
1047 	if (ino > fs->super->s_inodes_count)
1048 		return EXT2_ET_BAD_INODE_NUM;
1049 
1050 	if (fs->check_directory) {
1051 		retval = (fs->check_directory)(fs, ino);
1052 		if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
1053 			return retval;
1054 	}
1055 	retval = ext2fs_read_inode(fs, ino, &inode);
1056 	if (retval)
1057 		return retval;
1058 	if (!LINUX_S_ISDIR(inode.i_mode))
1059 		return EXT2_ET_NO_DIRECTORY;
1060 	return 0;
1061 }
1062 
1063