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