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