• 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 <stdio.h>
13 #include <string.h>
14 #if HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #if HAVE_ERRNO_H
18 #include <errno.h>
19 #endif
20 #include <time.h>
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 struct ext2_struct_inode_scan {
33 	errcode_t		magic;
34 	ext2_filsys		fs;
35 	ext2_ino_t		current_inode;
36 	blk64_t			current_block;
37 	dgrp_t			current_group;
38 	ext2_ino_t		inodes_left;
39 	blk_t			blocks_left;
40 	dgrp_t			groups_left;
41 	blk_t			inode_buffer_blocks;
42 	char *			inode_buffer;
43 	int			inode_size;
44 	char *			ptr;
45 	int			bytes_left;
46 	char			*temp_buffer;
47 	errcode_t		(*done_group)(ext2_filsys fs,
48 					      ext2_inode_scan scan,
49 					      dgrp_t group,
50 					      void * priv_data);
51 	void *			done_group_data;
52 	int			bad_block_ptr;
53 	int			scan_flags;
54 	int			reserved[6];
55 };
56 
57 /*
58  * This routine flushes the icache, if it exists.
59  */
ext2fs_flush_icache(ext2_filsys fs)60 errcode_t ext2fs_flush_icache(ext2_filsys fs)
61 {
62 	int	i;
63 
64 	if (!fs->icache)
65 		return 0;
66 
67 	for (i=0; i < fs->icache->cache_size; i++)
68 		fs->icache->cache[i].ino = 0;
69 
70 	fs->icache->buffer_blk = 0;
71 	return 0;
72 }
73 
create_icache(ext2_filsys fs)74 static errcode_t create_icache(ext2_filsys fs)
75 {
76 	errcode_t	retval;
77 
78 	if (fs->icache)
79 		return 0;
80 	retval = ext2fs_get_mem(sizeof(struct ext2_inode_cache), &fs->icache);
81 	if (retval)
82 		return retval;
83 
84 	memset(fs->icache, 0, sizeof(struct ext2_inode_cache));
85 	retval = ext2fs_get_mem(fs->blocksize, &fs->icache->buffer);
86 	if (retval) {
87 		ext2fs_free_mem(&fs->icache);
88 		return retval;
89 	}
90 	fs->icache->buffer_blk = 0;
91 	fs->icache->cache_last = -1;
92 	fs->icache->cache_size = 4;
93 	fs->icache->refcount = 1;
94 	retval = ext2fs_get_array(fs->icache->cache_size,
95 				  sizeof(struct ext2_inode_cache_ent),
96 				  &fs->icache->cache);
97 	if (retval) {
98 		ext2fs_free_mem(&fs->icache->buffer);
99 		ext2fs_free_mem(&fs->icache);
100 		return retval;
101 	}
102 	ext2fs_flush_icache(fs);
103 	return 0;
104 }
105 
ext2fs_open_inode_scan(ext2_filsys fs,int buffer_blocks,ext2_inode_scan * ret_scan)106 errcode_t ext2fs_open_inode_scan(ext2_filsys fs, int buffer_blocks,
107 				 ext2_inode_scan *ret_scan)
108 {
109 	ext2_inode_scan	scan;
110 	errcode_t	retval;
111 	errcode_t (*save_get_blocks)(ext2_filsys f, ext2_ino_t ino, blk_t *blocks);
112 
113 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
114 
115 	/*
116 	 * If fs->badblocks isn't set, then set it --- since the inode
117 	 * scanning functions require it.
118 	 */
119 	if (fs->badblocks == 0) {
120 		/*
121 		 * Temporarly save fs->get_blocks and set it to zero,
122 		 * for compatibility with old e2fsck's.
123 		 */
124 		save_get_blocks = fs->get_blocks;
125 		fs->get_blocks = 0;
126 		retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
127 		if (retval && fs->badblocks) {
128 			ext2fs_badblocks_list_free(fs->badblocks);
129 			fs->badblocks = 0;
130 		}
131 		fs->get_blocks = save_get_blocks;
132 	}
133 
134 	retval = ext2fs_get_mem(sizeof(struct ext2_struct_inode_scan), &scan);
135 	if (retval)
136 		return retval;
137 	memset(scan, 0, sizeof(struct ext2_struct_inode_scan));
138 
139 	scan->magic = EXT2_ET_MAGIC_INODE_SCAN;
140 	scan->fs = fs;
141 	scan->inode_size = EXT2_INODE_SIZE(fs->super);
142 	scan->bytes_left = 0;
143 	scan->current_group = 0;
144 	scan->groups_left = fs->group_desc_count - 1;
145 	scan->inode_buffer_blocks = buffer_blocks ? buffer_blocks : 8;
146 	scan->current_block = ext2fs_inode_table_loc(scan->fs,
147 						     scan->current_group);
148 	scan->inodes_left = EXT2_INODES_PER_GROUP(scan->fs->super);
149 	scan->blocks_left = scan->fs->inode_blocks_per_group;
150 	if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
151 				       EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
152 		scan->inodes_left -=
153 			ext2fs_bg_itable_unused(fs, scan->current_group);
154 		scan->blocks_left =
155 			(scan->inodes_left +
156 			 (fs->blocksize / scan->inode_size - 1)) *
157 			scan->inode_size / fs->blocksize;
158 	}
159 	retval = io_channel_alloc_buf(fs->io, scan->inode_buffer_blocks,
160 				      &scan->inode_buffer);
161 	scan->done_group = 0;
162 	scan->done_group_data = 0;
163 	scan->bad_block_ptr = 0;
164 	if (retval) {
165 		ext2fs_free_mem(&scan);
166 		return retval;
167 	}
168 	retval = ext2fs_get_mem(scan->inode_size, &scan->temp_buffer);
169 	if (retval) {
170 		ext2fs_free_mem(&scan->inode_buffer);
171 		ext2fs_free_mem(&scan);
172 		return retval;
173 	}
174 	if (scan->fs->badblocks && scan->fs->badblocks->num)
175 		scan->scan_flags |= EXT2_SF_CHK_BADBLOCKS;
176 	if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
177 				       EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
178 		scan->scan_flags |= EXT2_SF_DO_LAZY;
179 	*ret_scan = scan;
180 	return 0;
181 }
182 
ext2fs_close_inode_scan(ext2_inode_scan scan)183 void ext2fs_close_inode_scan(ext2_inode_scan scan)
184 {
185 	if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
186 		return;
187 
188 	ext2fs_free_mem(&scan->inode_buffer);
189 	scan->inode_buffer = NULL;
190 	ext2fs_free_mem(&scan->temp_buffer);
191 	scan->temp_buffer = NULL;
192 	ext2fs_free_mem(&scan);
193 	return;
194 }
195 
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)196 void ext2fs_set_inode_callback(ext2_inode_scan scan,
197 			       errcode_t (*done_group)(ext2_filsys fs,
198 						       ext2_inode_scan scan,
199 						       dgrp_t group,
200 						       void * priv_data),
201 			       void *done_group_data)
202 {
203 	if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
204 		return;
205 
206 	scan->done_group = done_group;
207 	scan->done_group_data = done_group_data;
208 }
209 
ext2fs_inode_scan_flags(ext2_inode_scan scan,int set_flags,int clear_flags)210 int ext2fs_inode_scan_flags(ext2_inode_scan scan, int set_flags,
211 			    int clear_flags)
212 {
213 	int	old_flags;
214 
215 	if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
216 		return 0;
217 
218 	old_flags = scan->scan_flags;
219 	scan->scan_flags &= ~clear_flags;
220 	scan->scan_flags |= set_flags;
221 	return old_flags;
222 }
223 
224 /*
225  * This function is called by ext2fs_get_next_inode when it needs to
226  * get ready to read in a new blockgroup.
227  */
get_next_blockgroup(ext2_inode_scan scan)228 static errcode_t get_next_blockgroup(ext2_inode_scan scan)
229 {
230 	ext2_filsys fs = scan->fs;
231 
232 	scan->current_group++;
233 	scan->groups_left--;
234 
235 	scan->current_block = ext2fs_inode_table_loc(scan->fs,
236 						     scan->current_group);
237 	scan->current_inode = scan->current_group *
238 		EXT2_INODES_PER_GROUP(fs->super);
239 
240 	scan->bytes_left = 0;
241 	scan->inodes_left = EXT2_INODES_PER_GROUP(fs->super);
242 	scan->blocks_left = fs->inode_blocks_per_group;
243 	if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
244 				       EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
245 		scan->inodes_left -=
246 			ext2fs_bg_itable_unused(fs, scan->current_group);
247 		scan->blocks_left =
248 			(scan->inodes_left +
249 			 (fs->blocksize / scan->inode_size - 1)) *
250 			scan->inode_size / fs->blocksize;
251 	}
252 
253 	return 0;
254 }
255 
ext2fs_inode_scan_goto_blockgroup(ext2_inode_scan scan,int group)256 errcode_t ext2fs_inode_scan_goto_blockgroup(ext2_inode_scan scan,
257 					    int	group)
258 {
259 	scan->current_group = group - 1;
260 	scan->groups_left = scan->fs->group_desc_count - group;
261 	return get_next_blockgroup(scan);
262 }
263 
264 /*
265  * This function is called by get_next_blocks() to check for bad
266  * blocks in the inode table.
267  *
268  * This function assumes that badblocks_list->list is sorted in
269  * increasing order.
270  */
check_for_inode_bad_blocks(ext2_inode_scan scan,blk64_t * num_blocks)271 static errcode_t check_for_inode_bad_blocks(ext2_inode_scan scan,
272 					    blk64_t *num_blocks)
273 {
274 	blk64_t	blk = scan->current_block;
275 	badblocks_list	bb = scan->fs->badblocks;
276 
277 	/*
278 	 * If the inode table is missing, then obviously there are no
279 	 * bad blocks.  :-)
280 	 */
281 	if (blk == 0)
282 		return 0;
283 
284 	/*
285 	 * If the current block is greater than the bad block listed
286 	 * in the bad block list, then advance the pointer until this
287 	 * is no longer the case.  If we run out of bad blocks, then
288 	 * we don't need to do any more checking!
289 	 */
290 	while (blk > bb->list[scan->bad_block_ptr]) {
291 		if (++scan->bad_block_ptr >= bb->num) {
292 			scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
293 			return 0;
294 		}
295 	}
296 
297 	/*
298 	 * If the current block is equal to the bad block listed in
299 	 * the bad block list, then handle that one block specially.
300 	 * (We could try to handle runs of bad blocks, but that
301 	 * only increases CPU efficiency by a small amount, at the
302 	 * expense of a huge expense of code complexity, and for an
303 	 * uncommon case at that.)
304 	 */
305 	if (blk == bb->list[scan->bad_block_ptr]) {
306 		scan->scan_flags |= EXT2_SF_BAD_INODE_BLK;
307 		*num_blocks = 1;
308 		if (++scan->bad_block_ptr >= bb->num)
309 			scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
310 		return 0;
311 	}
312 
313 	/*
314 	 * If there is a bad block in the range that we're about to
315 	 * read in, adjust the number of blocks to read so that we we
316 	 * don't read in the bad block.  (Then the next block to read
317 	 * will be the bad block, which is handled in the above case.)
318 	 */
319 	if ((blk + *num_blocks) > bb->list[scan->bad_block_ptr])
320 		*num_blocks = (int) (bb->list[scan->bad_block_ptr] - blk);
321 
322 	return 0;
323 }
324 
325 /*
326  * This function is called by ext2fs_get_next_inode when it needs to
327  * read in more blocks from the current blockgroup's inode table.
328  */
get_next_blocks(ext2_inode_scan scan)329 static errcode_t get_next_blocks(ext2_inode_scan scan)
330 {
331 	blk64_t		num_blocks;
332 	errcode_t	retval;
333 
334 	/*
335 	 * Figure out how many blocks to read; we read at most
336 	 * inode_buffer_blocks, and perhaps less if there aren't that
337 	 * many blocks left to read.
338 	 */
339 	num_blocks = scan->inode_buffer_blocks;
340 	if (num_blocks > scan->blocks_left)
341 		num_blocks = scan->blocks_left;
342 
343 	/*
344 	 * If the past block "read" was a bad block, then mark the
345 	 * left-over extra bytes as also being bad.
346 	 */
347 	if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK) {
348 		if (scan->bytes_left)
349 			scan->scan_flags |= EXT2_SF_BAD_EXTRA_BYTES;
350 		scan->scan_flags &= ~EXT2_SF_BAD_INODE_BLK;
351 	}
352 
353 	/*
354 	 * Do inode bad block processing, if necessary.
355 	 */
356 	if (scan->scan_flags & EXT2_SF_CHK_BADBLOCKS) {
357 		retval = check_for_inode_bad_blocks(scan, &num_blocks);
358 		if (retval)
359 			return retval;
360 	}
361 
362 	if ((scan->scan_flags & EXT2_SF_BAD_INODE_BLK) ||
363 	    (scan->current_block == 0)) {
364 		memset(scan->inode_buffer, 0,
365 		       (size_t) num_blocks * scan->fs->blocksize);
366 	} else {
367 		retval = io_channel_read_blk64(scan->fs->io,
368 					     scan->current_block,
369 					     (int) num_blocks,
370 					     scan->inode_buffer);
371 		if (retval)
372 			return EXT2_ET_NEXT_INODE_READ;
373 	}
374 	scan->ptr = scan->inode_buffer;
375 	scan->bytes_left = num_blocks * scan->fs->blocksize;
376 
377 	scan->blocks_left -= num_blocks;
378 	if (scan->current_block)
379 		scan->current_block += num_blocks;
380 	return 0;
381 }
382 
383 #if 0
384 /*
385  * Returns 1 if the entire inode_buffer has a non-zero size and
386  * contains all zeros.  (Not just deleted inodes, since that means
387  * that part of the inode table was used at one point; we want all
388  * zeros, which means that the inode table is pristine.)
389  */
390 static inline int is_empty_scan(ext2_inode_scan scan)
391 {
392 	int	i;
393 
394 	if (scan->bytes_left == 0)
395 		return 0;
396 
397 	for (i=0; i < scan->bytes_left; i++)
398 		if (scan->ptr[i])
399 			return 0;
400 	return 1;
401 }
402 #endif
403 
ext2fs_get_next_inode_full(ext2_inode_scan scan,ext2_ino_t * ino,struct ext2_inode * inode,int bufsize)404 errcode_t ext2fs_get_next_inode_full(ext2_inode_scan scan, ext2_ino_t *ino,
405 				     struct ext2_inode *inode, int bufsize)
406 {
407 	errcode_t	retval;
408 	int		extra_bytes = 0;
409 
410 	EXT2_CHECK_MAGIC(scan, EXT2_ET_MAGIC_INODE_SCAN);
411 
412 	/*
413 	 * Do we need to start reading a new block group?
414 	 */
415 	if (scan->inodes_left <= 0) {
416 	force_new_group:
417 		if (scan->done_group) {
418 			retval = (scan->done_group)
419 				(scan->fs, scan, scan->current_group,
420 				 scan->done_group_data);
421 			if (retval)
422 				return retval;
423 		}
424 		if (scan->groups_left <= 0) {
425 			*ino = 0;
426 			return 0;
427 		}
428 		retval = get_next_blockgroup(scan);
429 		if (retval)
430 			return retval;
431 	}
432 	/*
433 	 * These checks are done outside the above if statement so
434 	 * they can be done for block group #0.
435 	 */
436 	if ((scan->scan_flags & EXT2_SF_DO_LAZY) &&
437 	    (ext2fs_bg_flags_test(scan->fs, scan->current_group, EXT2_BG_INODE_UNINIT)
438 	     ))
439 		goto force_new_group;
440 	if (scan->inodes_left == 0)
441 		goto force_new_group;
442 	if (scan->current_block == 0) {
443 		if (scan->scan_flags & EXT2_SF_SKIP_MISSING_ITABLE) {
444 			goto force_new_group;
445 		} else
446 			return EXT2_ET_MISSING_INODE_TABLE;
447 	}
448 
449 
450 	/*
451 	 * Have we run out of space in the inode buffer?  If so, we
452 	 * need to read in more blocks.
453 	 */
454 	if (scan->bytes_left < scan->inode_size) {
455 		memcpy(scan->temp_buffer, scan->ptr, scan->bytes_left);
456 		extra_bytes = scan->bytes_left;
457 
458 		retval = get_next_blocks(scan);
459 		if (retval)
460 			return retval;
461 #if 0
462 		/*
463 		 * XXX test  Need check for used inode somehow.
464 		 * (Note: this is hard.)
465 		 */
466 		if (is_empty_scan(scan))
467 			goto force_new_group;
468 #endif
469 	}
470 
471 	retval = 0;
472 	if (extra_bytes) {
473 		memcpy(scan->temp_buffer+extra_bytes, scan->ptr,
474 		       scan->inode_size - extra_bytes);
475 		scan->ptr += scan->inode_size - extra_bytes;
476 		scan->bytes_left -= scan->inode_size - extra_bytes;
477 
478 #ifdef WORDS_BIGENDIAN
479 		memset(inode, 0, bufsize);
480 		ext2fs_swap_inode_full(scan->fs,
481 			       (struct ext2_inode_large *) inode,
482 			       (struct ext2_inode_large *) scan->temp_buffer,
483 			       0, bufsize);
484 #else
485 		*inode = *((struct ext2_inode *) scan->temp_buffer);
486 #endif
487 		if (scan->scan_flags & EXT2_SF_BAD_EXTRA_BYTES)
488 			retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
489 		scan->scan_flags &= ~EXT2_SF_BAD_EXTRA_BYTES;
490 	} else {
491 #ifdef WORDS_BIGENDIAN
492 		memset(inode, 0, bufsize);
493 		ext2fs_swap_inode_full(scan->fs,
494 				(struct ext2_inode_large *) inode,
495 				(struct ext2_inode_large *) scan->ptr,
496 				0, bufsize);
497 #else
498 		memcpy(inode, scan->ptr, bufsize);
499 #endif
500 		scan->ptr += scan->inode_size;
501 		scan->bytes_left -= scan->inode_size;
502 		if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK)
503 			retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
504 	}
505 
506 	scan->inodes_left--;
507 	scan->current_inode++;
508 	*ino = scan->current_inode;
509 	return retval;
510 }
511 
ext2fs_get_next_inode(ext2_inode_scan scan,ext2_ino_t * ino,struct ext2_inode * inode)512 errcode_t ext2fs_get_next_inode(ext2_inode_scan scan, ext2_ino_t *ino,
513 				struct ext2_inode *inode)
514 {
515 	return ext2fs_get_next_inode_full(scan, ino, inode,
516 						sizeof(struct ext2_inode));
517 }
518 
519 /*
520  * Functions to read and write a single inode.
521  */
ext2fs_read_inode_full(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode,int bufsize)522 errcode_t ext2fs_read_inode_full(ext2_filsys fs, ext2_ino_t ino,
523 				 struct ext2_inode * inode, int bufsize)
524 {
525 	blk64_t		block_nr;
526 	unsigned long 	group, block, offset;
527 	char 		*ptr;
528 	errcode_t	retval;
529 	int 		clen, i, inodes_per_block, length;
530 	io_channel	io;
531 
532 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
533 
534 	/* Check to see if user has an override function */
535 	if (fs->read_inode &&
536 	    ((bufsize == sizeof(struct ext2_inode)) ||
537 	     (EXT2_INODE_SIZE(fs->super) == sizeof(struct ext2_inode)))) {
538 		retval = (fs->read_inode)(fs, ino, inode);
539 		if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
540 			return retval;
541 	}
542 	if ((ino == 0) || (ino > fs->super->s_inodes_count))
543 		return EXT2_ET_BAD_INODE_NUM;
544 	/* Create inode cache if not present */
545 	if (!fs->icache) {
546 		retval = create_icache(fs);
547 		if (retval)
548 			return retval;
549 	}
550 	/* Check to see if it's in the inode cache */
551 	if (bufsize == sizeof(struct ext2_inode)) {
552 		/* only old good inode can be retrieved from the cache */
553 		for (i=0; i < fs->icache->cache_size; i++) {
554 			if (fs->icache->cache[i].ino == ino) {
555 				*inode = fs->icache->cache[i].inode;
556 				return 0;
557 			}
558 		}
559 	}
560 	if (fs->flags & EXT2_FLAG_IMAGE_FILE) {
561 		inodes_per_block = fs->blocksize / EXT2_INODE_SIZE(fs->super);
562 		block_nr = fs->image_header->offset_inode / fs->blocksize;
563 		block_nr += (ino - 1) / inodes_per_block;
564 		offset = ((ino - 1) % inodes_per_block) *
565 			EXT2_INODE_SIZE(fs->super);
566 		io = fs->image_io;
567 	} else {
568 		group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
569 		if (group > fs->group_desc_count)
570 			return EXT2_ET_BAD_INODE_NUM;
571 		offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
572 			EXT2_INODE_SIZE(fs->super);
573 		block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
574 		if (!ext2fs_inode_table_loc(fs, (unsigned) group))
575 			return EXT2_ET_MISSING_INODE_TABLE;
576 		block_nr = ext2fs_inode_table_loc(fs, group) +
577 			block;
578 		io = fs->io;
579 	}
580 	offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
581 
582 	length = EXT2_INODE_SIZE(fs->super);
583 	if (bufsize < length)
584 		length = bufsize;
585 
586 	ptr = (char *) inode;
587 	while (length) {
588 		clen = length;
589 		if ((offset + length) > fs->blocksize)
590 			clen = fs->blocksize - offset;
591 
592 		if (block_nr != fs->icache->buffer_blk) {
593 			retval = io_channel_read_blk64(io, block_nr, 1,
594 						     fs->icache->buffer);
595 			if (retval)
596 				return retval;
597 			fs->icache->buffer_blk = block_nr;
598 		}
599 
600 		memcpy(ptr, ((char *) fs->icache->buffer) + (unsigned) offset,
601 		       clen);
602 
603 		offset = 0;
604 		length -= clen;
605 		ptr += clen;
606 		block_nr++;
607 	}
608 
609 #ifdef WORDS_BIGENDIAN
610 	ext2fs_swap_inode_full(fs, (struct ext2_inode_large *) inode,
611 			       (struct ext2_inode_large *) inode,
612 			       0, bufsize);
613 #endif
614 
615 	/* Update the inode cache */
616 	fs->icache->cache_last = (fs->icache->cache_last + 1) %
617 		fs->icache->cache_size;
618 	fs->icache->cache[fs->icache->cache_last].ino = ino;
619 	fs->icache->cache[fs->icache->cache_last].inode = *inode;
620 
621 	return 0;
622 }
623 
ext2fs_read_inode(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode)624 errcode_t ext2fs_read_inode(ext2_filsys fs, ext2_ino_t ino,
625 			    struct ext2_inode * inode)
626 {
627 	return ext2fs_read_inode_full(fs, ino, inode,
628 					sizeof(struct ext2_inode));
629 }
630 
ext2fs_write_inode_full(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode,int bufsize)631 errcode_t ext2fs_write_inode_full(ext2_filsys fs, ext2_ino_t ino,
632 				  struct ext2_inode * inode, int bufsize)
633 {
634 	blk64_t block_nr;
635 	unsigned long group, block, offset;
636 	errcode_t retval = 0;
637 	struct ext2_inode_large temp_inode, *w_inode;
638 	char *ptr;
639 	int clen, i, length;
640 
641 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
642 
643 	/* Check to see if user provided an override function */
644 	if (fs->write_inode) {
645 		retval = (fs->write_inode)(fs, ino, inode);
646 		if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
647 			return retval;
648 	}
649 
650 	/* Check to see if the inode cache needs to be updated */
651 	if (fs->icache) {
652 		for (i=0; i < fs->icache->cache_size; i++) {
653 			if (fs->icache->cache[i].ino == ino) {
654 				fs->icache->cache[i].inode = *inode;
655 				break;
656 			}
657 		}
658 	} else {
659 		retval = create_icache(fs);
660 		if (retval)
661 			return retval;
662 	}
663 
664 	if (!(fs->flags & EXT2_FLAG_RW))
665 		return EXT2_ET_RO_FILSYS;
666 
667 	if ((ino == 0) || (ino > fs->super->s_inodes_count))
668 		return EXT2_ET_BAD_INODE_NUM;
669 
670 	length = bufsize;
671 	if (length < EXT2_INODE_SIZE(fs->super))
672 		length = EXT2_INODE_SIZE(fs->super);
673 
674 	if (length > (int) sizeof(struct ext2_inode_large)) {
675 		w_inode = malloc(length);
676 		if (!w_inode) {
677 			retval = ENOMEM;
678 			goto errout;
679 		}
680 	} else
681 		w_inode = &temp_inode;
682 	memset(w_inode, 0, length);
683 
684 #ifdef WORDS_BIGENDIAN
685 	ext2fs_swap_inode_full(fs, w_inode,
686 			       (struct ext2_inode_large *) inode,
687 			       1, bufsize);
688 #else
689 	memcpy(w_inode, inode, bufsize);
690 #endif
691 
692 	group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
693 	offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
694 		EXT2_INODE_SIZE(fs->super);
695 	block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
696 	if (!ext2fs_inode_table_loc(fs, (unsigned) group)) {
697 		retval = EXT2_ET_MISSING_INODE_TABLE;
698 		goto errout;
699 	}
700 	block_nr = ext2fs_inode_table_loc(fs, (unsigned) group) + block;
701 
702 	offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
703 
704 	length = EXT2_INODE_SIZE(fs->super);
705 	if (length > bufsize)
706 		length = bufsize;
707 
708 	ptr = (char *) w_inode;
709 
710 	while (length) {
711 		clen = length;
712 		if ((offset + length) > fs->blocksize)
713 			clen = fs->blocksize - offset;
714 
715 		if (fs->icache->buffer_blk != block_nr) {
716 			retval = io_channel_read_blk64(fs->io, block_nr, 1,
717 						     fs->icache->buffer);
718 			if (retval)
719 				goto errout;
720 			fs->icache->buffer_blk = block_nr;
721 		}
722 
723 
724 		memcpy((char *) fs->icache->buffer + (unsigned) offset,
725 		       ptr, clen);
726 
727 		retval = io_channel_write_blk64(fs->io, block_nr, 1,
728 					      fs->icache->buffer);
729 		if (retval)
730 			goto errout;
731 
732 		offset = 0;
733 		ptr += clen;
734 		length -= clen;
735 		block_nr++;
736 	}
737 
738 	fs->flags |= EXT2_FLAG_CHANGED;
739 errout:
740 	if (w_inode && w_inode != &temp_inode)
741 		free(w_inode);
742 	return retval;
743 }
744 
ext2fs_write_inode(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode)745 errcode_t ext2fs_write_inode(ext2_filsys fs, ext2_ino_t ino,
746 			     struct ext2_inode *inode)
747 {
748 	return ext2fs_write_inode_full(fs, ino, inode,
749 				       sizeof(struct ext2_inode));
750 }
751 
752 /*
753  * This function should be called when writing a new inode.  It makes
754  * sure that extra part of large inodes is initialized properly.
755  */
ext2fs_write_new_inode(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode)756 errcode_t ext2fs_write_new_inode(ext2_filsys fs, ext2_ino_t ino,
757 				 struct ext2_inode *inode)
758 {
759 	struct ext2_inode	*buf;
760 	int 			size = EXT2_INODE_SIZE(fs->super);
761 	struct ext2_inode_large	*large_inode;
762 	errcode_t		retval;
763 	__u32 			t = fs->now ? fs->now : time(NULL);
764 
765 	if (!inode->i_ctime)
766 		inode->i_ctime = t;
767 	if (!inode->i_mtime)
768 		inode->i_mtime = t;
769 	if (!inode->i_atime)
770 		inode->i_atime = t;
771 
772 	if (size == sizeof(struct ext2_inode))
773 		return ext2fs_write_inode_full(fs, ino, inode,
774 					       sizeof(struct ext2_inode));
775 
776 	buf = malloc(size);
777 	if (!buf)
778 		return ENOMEM;
779 
780 	memset(buf, 0, size);
781 	*buf = *inode;
782 
783 	large_inode = (struct ext2_inode_large *) buf;
784 	large_inode->i_extra_isize = sizeof(struct ext2_inode_large) -
785 		EXT2_GOOD_OLD_INODE_SIZE;
786 	if (!large_inode->i_crtime)
787 		large_inode->i_crtime = t;
788 
789 	retval = ext2fs_write_inode_full(fs, ino, buf, size);
790 	free(buf);
791 	return retval;
792 }
793 
794 
ext2fs_get_blocks(ext2_filsys fs,ext2_ino_t ino,blk_t * blocks)795 errcode_t ext2fs_get_blocks(ext2_filsys fs, ext2_ino_t ino, blk_t *blocks)
796 {
797 	struct ext2_inode	inode;
798 	int			i;
799 	errcode_t		retval;
800 
801 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
802 
803 	if (ino > fs->super->s_inodes_count)
804 		return EXT2_ET_BAD_INODE_NUM;
805 
806 	if (fs->get_blocks) {
807 		if (!(*fs->get_blocks)(fs, ino, blocks))
808 			return 0;
809 	}
810 	retval = ext2fs_read_inode(fs, ino, &inode);
811 	if (retval)
812 		return retval;
813 	for (i=0; i < EXT2_N_BLOCKS; i++)
814 		blocks[i] = inode.i_block[i];
815 	return 0;
816 }
817 
ext2fs_check_directory(ext2_filsys fs,ext2_ino_t ino)818 errcode_t ext2fs_check_directory(ext2_filsys fs, ext2_ino_t ino)
819 {
820 	struct	ext2_inode	inode;
821 	errcode_t		retval;
822 
823 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
824 
825 	if (ino > fs->super->s_inodes_count)
826 		return EXT2_ET_BAD_INODE_NUM;
827 
828 	if (fs->check_directory) {
829 		retval = (fs->check_directory)(fs, ino);
830 		if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
831 			return retval;
832 	}
833 	retval = ext2fs_read_inode(fs, ino, &inode);
834 	if (retval)
835 		return retval;
836 	if (!LINUX_S_ISDIR(inode.i_mode))
837 		return EXT2_ET_NO_DIRECTORY;
838 	return 0;
839 }
840 
841