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