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