source: MondoRescue/branches/stable/mindi-busybox/e2fsprogs/ext2fs/inode.c@ 821

Last change on this file since 821 was 821, checked in by Bruno Cornec, 18 years ago

Addition of busybox 1.2.1 as a mindi-busybox new package
This should avoid delivering binary files in mindi not built there (Fedora and Debian are quite serious about that)

File size: 19.6 KB
Line 
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 Public
8 * License.
9 * %End-Header%
10 */
11
12#include <stdio.h>
13#include <string.h>
14#if HAVE_UNISTD_H
15#include <unistd.h>
16#endif
17#if HAVE_ERRNO_H
18#include <errno.h>
19#endif
20#if HAVE_SYS_STAT_H
21#include <sys/stat.h>
22#endif
23#if HAVE_SYS_TYPES_H
24#include <sys/types.h>
25#endif
26
27#include "ext2_fs.h"
28#include "ext2fsP.h"
29#include "e2image.h"
30
31struct ext2_struct_inode_scan {
32 errcode_t magic;
33 ext2_filsys fs;
34 ext2_ino_t current_inode;
35 blk_t current_block;
36 dgrp_t current_group;
37 ext2_ino_t inodes_left;
38 blk_t blocks_left;
39 dgrp_t groups_left;
40 blk_t inode_buffer_blocks;
41 char * inode_buffer;
42 int inode_size;
43 char * ptr;
44 int bytes_left;
45 char *temp_buffer;
46 errcode_t (*done_group)(ext2_filsys fs,
47 dgrp_t group,
48 void * priv_data);
49 void * done_group_data;
50 int bad_block_ptr;
51 int scan_flags;
52 int reserved[6];
53};
54
55/*
56 * This routine flushes the icache, if it exists.
57 */
58errcode_t ext2fs_flush_icache(ext2_filsys fs)
59{
60 int i;
61
62 if (!fs->icache)
63 return 0;
64
65 for (i=0; i < fs->icache->cache_size; i++)
66 fs->icache->cache[i].ino = 0;
67
68 fs->icache->buffer_blk = 0;
69 return 0;
70}
71
72static errcode_t create_icache(ext2_filsys fs)
73{
74 errcode_t retval;
75
76 if (fs->icache)
77 return 0;
78 retval = ext2fs_get_mem(sizeof(struct ext2_inode_cache), &fs->icache);
79 if (retval)
80 return retval;
81
82 memset(fs->icache, 0, sizeof(struct ext2_inode_cache));
83 retval = ext2fs_get_mem(fs->blocksize, &fs->icache->buffer);
84 if (retval) {
85 ext2fs_free_mem(&fs->icache);
86 return retval;
87 }
88 fs->icache->buffer_blk = 0;
89 fs->icache->cache_last = -1;
90 fs->icache->cache_size = 4;
91 fs->icache->refcount = 1;
92 retval = ext2fs_get_mem(sizeof(struct ext2_inode_cache_ent)
93 * fs->icache->cache_size,
94 &fs->icache->cache);
95 if (retval) {
96 ext2fs_free_mem(&fs->icache->buffer);
97 ext2fs_free_mem(&fs->icache);
98 return retval;
99 }
100 ext2fs_flush_icache(fs);
101 return 0;
102}
103
104errcode_t ext2fs_open_inode_scan(ext2_filsys fs, int buffer_blocks,
105 ext2_inode_scan *ret_scan)
106{
107 ext2_inode_scan scan;
108 errcode_t retval;
109 errcode_t (*save_get_blocks)(ext2_filsys f, ext2_ino_t ino, blk_t *blocks);
110
111 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
112
113 /*
114 * If fs->badblocks isn't set, then set it --- since the inode
115 * scanning functions require it.
116 */
117 if (fs->badblocks == 0) {
118 /*
119 * Temporarly save fs->get_blocks and set it to zero,
120 * for compatibility with old e2fsck's.
121 */
122 save_get_blocks = fs->get_blocks;
123 fs->get_blocks = 0;
124 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
125 if (retval) {
126 ext2fs_badblocks_list_free(fs->badblocks);
127 fs->badblocks = 0;
128 }
129 fs->get_blocks = save_get_blocks;
130 }
131
132 retval = ext2fs_get_mem(sizeof(struct ext2_struct_inode_scan), &scan);
133 if (retval)
134 return retval;
135 memset(scan, 0, sizeof(struct ext2_struct_inode_scan));
136
137 scan->magic = EXT2_ET_MAGIC_INODE_SCAN;
138 scan->fs = fs;
139 scan->inode_size = EXT2_INODE_SIZE(fs->super);
140 scan->bytes_left = 0;
141 scan->current_group = 0;
142 scan->groups_left = fs->group_desc_count - 1;
143 scan->inode_buffer_blocks = buffer_blocks ? buffer_blocks : 8;
144 scan->current_block = scan->fs->
145 group_desc[scan->current_group].bg_inode_table;
146 scan->inodes_left = EXT2_INODES_PER_GROUP(scan->fs->super);
147 scan->blocks_left = scan->fs->inode_blocks_per_group;
148 retval = ext2fs_get_mem((size_t) (scan->inode_buffer_blocks *
149 fs->blocksize),
150 &scan->inode_buffer);
151 scan->done_group = 0;
152 scan->done_group_data = 0;
153 scan->bad_block_ptr = 0;
154 if (retval) {
155 ext2fs_free_mem(&scan);
156 return retval;
157 }
158 retval = ext2fs_get_mem(scan->inode_size, &scan->temp_buffer);
159 if (retval) {
160 ext2fs_free_mem(&scan->inode_buffer);
161 ext2fs_free_mem(&scan);
162 return retval;
163 }
164 if (scan->fs->badblocks && scan->fs->badblocks->num)
165 scan->scan_flags |= EXT2_SF_CHK_BADBLOCKS;
166 *ret_scan = scan;
167 return 0;
168}
169
170void ext2fs_close_inode_scan(ext2_inode_scan scan)
171{
172 if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
173 return;
174
175 ext2fs_free_mem(&scan->inode_buffer);
176 scan->inode_buffer = NULL;
177 ext2fs_free_mem(&scan->temp_buffer);
178 scan->temp_buffer = NULL;
179 ext2fs_free_mem(&scan);
180 return;
181}
182
183void 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
196int 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 */
214static 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
231errcode_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 */
246static 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 */
304static 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#if 0
359/*
360 * Returns 1 if the entire inode_buffer has a non-zero size and
361 * contains all zeros. (Not just deleted inodes, since that means
362 * that part of the inode table was used at one point; we want all
363 * zeros, which means that the inode table is pristine.)
364 */
365static inline int is_empty_scan(ext2_inode_scan scan)
366{
367 int i;
368
369 if (scan->bytes_left == 0)
370 return 0;
371
372 for (i=0; i < scan->bytes_left; i++)
373 if (scan->ptr[i])
374 return 0;
375 return 1;
376}
377#endif
378
379errcode_t ext2fs_get_next_inode_full(ext2_inode_scan scan, ext2_ino_t *ino,
380 struct ext2_inode *inode, int bufsize)
381{
382 errcode_t retval;
383 int extra_bytes = 0;
384
385 EXT2_CHECK_MAGIC(scan, EXT2_ET_MAGIC_INODE_SCAN);
386
387 /*
388 * Do we need to start reading a new block group?
389 */
390 if (scan->inodes_left <= 0) {
391 force_new_group:
392 if (scan->done_group) {
393 retval = (scan->done_group)
394 (scan->fs, scan->current_group,
395 scan->done_group_data);
396 if (retval)
397 return retval;
398 }
399 if (scan->groups_left <= 0) {
400 *ino = 0;
401 return 0;
402 }
403 retval = get_next_blockgroup(scan);
404 if (retval)
405 return retval;
406 }
407 /*
408 * This is done outside the above if statement so that the
409 * check can be done for block group #0.
410 */
411 if (scan->current_block == 0) {
412 if (scan->scan_flags & EXT2_SF_SKIP_MISSING_ITABLE) {
413 goto force_new_group;
414 } else
415 return EXT2_ET_MISSING_INODE_TABLE;
416 }
417
418
419 /*
420 * Have we run out of space in the inode buffer? If so, we
421 * need to read in more blocks.
422 */
423 if (scan->bytes_left < scan->inode_size) {
424 memcpy(scan->temp_buffer, scan->ptr, scan->bytes_left);
425 extra_bytes = scan->bytes_left;
426
427 retval = get_next_blocks(scan);
428 if (retval)
429 return retval;
430#if 0
431 /*
432 * XXX test Need check for used inode somehow.
433 * (Note: this is hard.)
434 */
435 if (is_empty_scan(scan))
436 goto force_new_group;
437#endif
438 }
439
440 retval = 0;
441 if (extra_bytes) {
442 memcpy(scan->temp_buffer+extra_bytes, scan->ptr,
443 scan->inode_size - extra_bytes);
444 scan->ptr += scan->inode_size - extra_bytes;
445 scan->bytes_left -= scan->inode_size - extra_bytes;
446
447#if BB_BIG_ENDIAN
448 if ((scan->fs->flags & EXT2_FLAG_SWAP_BYTES) ||
449 (scan->fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
450 ext2fs_swap_inode_full(scan->fs,
451 (struct ext2_inode_large *) inode,
452 (struct ext2_inode_large *) scan->temp_buffer,
453 0, bufsize);
454 else
455#endif
456 *inode = *((struct ext2_inode *) scan->temp_buffer);
457 if (scan->scan_flags & EXT2_SF_BAD_EXTRA_BYTES)
458 retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
459 scan->scan_flags &= ~EXT2_SF_BAD_EXTRA_BYTES;
460 } else {
461#if BB_BIG_ENDIAN
462 if ((scan->fs->flags & EXT2_FLAG_SWAP_BYTES) ||
463 (scan->fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
464 ext2fs_swap_inode_full(scan->fs,
465 (struct ext2_inode_large *) inode,
466 (struct ext2_inode_large *) scan->ptr,
467 0, bufsize);
468 else
469#endif
470 memcpy(inode, scan->ptr, bufsize);
471 scan->ptr += scan->inode_size;
472 scan->bytes_left -= scan->inode_size;
473 if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK)
474 retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
475 }
476
477 scan->inodes_left--;
478 scan->current_inode++;
479 *ino = scan->current_inode;
480 return retval;
481}
482
483errcode_t ext2fs_get_next_inode(ext2_inode_scan scan, ext2_ino_t *ino,
484 struct ext2_inode *inode)
485{
486 return ext2fs_get_next_inode_full(scan, ino, inode,
487 sizeof(struct ext2_inode));
488}
489
490/*
491 * Functions to read and write a single inode.
492 */
493errcode_t ext2fs_read_inode_full(ext2_filsys fs, ext2_ino_t ino,
494 struct ext2_inode * inode, int bufsize)
495{
496 unsigned long group, block, block_nr, offset;
497 char *ptr;
498 errcode_t retval;
499 int clen, i, inodes_per_block, length;
500 io_channel io;
501
502 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
503
504 /* Check to see if user has an override function */
505 if (fs->read_inode) {
506 retval = (fs->read_inode)(fs, ino, inode);
507 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
508 return retval;
509 }
510 /* Create inode cache if not present */
511 if (!fs->icache) {
512 retval = create_icache(fs);
513 if (retval)
514 return retval;
515 }
516 /* Check to see if it's in the inode cache */
517 if (bufsize == sizeof(struct ext2_inode)) {
518 /* only old good inode can be retrieve from the cache */
519 for (i=0; i < fs->icache->cache_size; i++) {
520 if (fs->icache->cache[i].ino == ino) {
521 *inode = fs->icache->cache[i].inode;
522 return 0;
523 }
524 }
525 }
526 if ((ino == 0) || (ino > fs->super->s_inodes_count))
527 return EXT2_ET_BAD_INODE_NUM;
528 if (fs->flags & EXT2_FLAG_IMAGE_FILE) {
529 inodes_per_block = fs->blocksize / EXT2_INODE_SIZE(fs->super);
530 block_nr = fs->image_header->offset_inode / fs->blocksize;
531 block_nr += (ino - 1) / inodes_per_block;
532 offset = ((ino - 1) % inodes_per_block) *
533 EXT2_INODE_SIZE(fs->super);
534 io = fs->image_io;
535 } else {
536 group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
537 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
538 EXT2_INODE_SIZE(fs->super);
539 block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
540 if (!fs->group_desc[(unsigned)group].bg_inode_table)
541 return EXT2_ET_MISSING_INODE_TABLE;
542 block_nr = fs->group_desc[(unsigned)group].bg_inode_table +
543 block;
544 io = fs->io;
545 }
546 offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
547
548 length = EXT2_INODE_SIZE(fs->super);
549 if (bufsize < length)
550 length = bufsize;
551
552 ptr = (char *) inode;
553 while (length) {
554 clen = length;
555 if ((offset + length) > fs->blocksize)
556 clen = fs->blocksize - offset;
557
558 if (block_nr != fs->icache->buffer_blk) {
559 retval = io_channel_read_blk(io, block_nr, 1,
560 fs->icache->buffer);
561 if (retval)
562 return retval;
563 fs->icache->buffer_blk = block_nr;
564 }
565
566 memcpy(ptr, ((char *) fs->icache->buffer) + (unsigned) offset,
567 clen);
568
569 offset = 0;
570 length -= clen;
571 ptr += clen;
572 block_nr++;
573 }
574
575#if BB_BIG_ENDIAN
576 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
577 (fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
578 ext2fs_swap_inode_full(fs, (struct ext2_inode_large *) inode,
579 (struct ext2_inode_large *) inode,
580 0, length);
581#endif
582
583 /* Update the inode cache */
584 fs->icache->cache_last = (fs->icache->cache_last + 1) %
585 fs->icache->cache_size;
586 fs->icache->cache[fs->icache->cache_last].ino = ino;
587 fs->icache->cache[fs->icache->cache_last].inode = *inode;
588
589 return 0;
590}
591
592errcode_t ext2fs_read_inode(ext2_filsys fs, ext2_ino_t ino,
593 struct ext2_inode * inode)
594{
595 return ext2fs_read_inode_full(fs, ino, inode,
596 sizeof(struct ext2_inode));
597}
598
599errcode_t ext2fs_write_inode_full(ext2_filsys fs, ext2_ino_t ino,
600 struct ext2_inode * inode, int bufsize)
601{
602 unsigned long group, block, block_nr, offset;
603 errcode_t retval = 0;
604 struct ext2_inode_large temp_inode, *w_inode;
605 char *ptr;
606 int clen, i, length;
607
608 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
609
610 /* Check to see if user provided an override function */
611 if (fs->write_inode) {
612 retval = (fs->write_inode)(fs, ino, inode);
613 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
614 return retval;
615 }
616
617 /* Check to see if the inode cache needs to be updated */
618 if (fs->icache) {
619 for (i=0; i < fs->icache->cache_size; i++) {
620 if (fs->icache->cache[i].ino == ino) {
621 fs->icache->cache[i].inode = *inode;
622 break;
623 }
624 }
625 } else {
626 retval = create_icache(fs);
627 if (retval)
628 return retval;
629 }
630
631 if (!(fs->flags & EXT2_FLAG_RW))
632 return EXT2_ET_RO_FILSYS;
633
634 if ((ino == 0) || (ino > fs->super->s_inodes_count))
635 return EXT2_ET_BAD_INODE_NUM;
636
637 length = bufsize;
638 if (length < EXT2_INODE_SIZE(fs->super))
639 length = EXT2_INODE_SIZE(fs->super);
640
641 if (length > (int) sizeof(struct ext2_inode_large)) {
642 w_inode = xmalloc(length);
643 } else
644 w_inode = &temp_inode;
645 memset(w_inode, 0, length);
646
647#if BB_BIG_ENDIAN
648 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
649 (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE))
650 ext2fs_swap_inode_full(fs, w_inode,
651 (struct ext2_inode_large *) inode,
652 1, bufsize);
653 else
654#endif
655 memcpy(w_inode, inode, bufsize);
656
657 group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
658 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
659 EXT2_INODE_SIZE(fs->super);
660 block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
661 if (!fs->group_desc[(unsigned) group].bg_inode_table)
662 return EXT2_ET_MISSING_INODE_TABLE;
663 block_nr = fs->group_desc[(unsigned) group].bg_inode_table + block;
664
665 offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
666
667 length = EXT2_INODE_SIZE(fs->super);
668 if (length > bufsize)
669 length = bufsize;
670
671 ptr = (char *) w_inode;
672
673 while (length) {
674 clen = length;
675 if ((offset + length) > fs->blocksize)
676 clen = fs->blocksize - offset;
677
678 if (fs->icache->buffer_blk != block_nr) {
679 retval = io_channel_read_blk(fs->io, block_nr, 1,
680 fs->icache->buffer);
681 if (retval)
682 goto errout;
683 fs->icache->buffer_blk = block_nr;
684 }
685
686
687 memcpy((char *) fs->icache->buffer + (unsigned) offset,
688 ptr, clen);
689
690 retval = io_channel_write_blk(fs->io, block_nr, 1,
691 fs->icache->buffer);
692 if (retval)
693 goto errout;
694
695 offset = 0;
696 ptr += clen;
697 length -= clen;
698 block_nr++;
699 }
700
701 fs->flags |= EXT2_FLAG_CHANGED;
702errout:
703 if (w_inode && w_inode != &temp_inode)
704 free(w_inode);
705 return retval;
706}
707
708errcode_t ext2fs_write_inode(ext2_filsys fs, ext2_ino_t ino,
709 struct ext2_inode *inode)
710{
711 return ext2fs_write_inode_full(fs, ino, inode,
712 sizeof(struct ext2_inode));
713}
714
715/*
716 * This function should be called when writing a new inode. It makes
717 * sure that extra part of large inodes is initialized properly.
718 */
719errcode_t ext2fs_write_new_inode(ext2_filsys fs, ext2_ino_t ino,
720 struct ext2_inode *inode)
721{
722 struct ext2_inode *buf;
723 int size = EXT2_INODE_SIZE(fs->super);
724 struct ext2_inode_large *large_inode;
725
726 if (size == sizeof(struct ext2_inode))
727 return ext2fs_write_inode_full(fs, ino, inode,
728 sizeof(struct ext2_inode));
729
730 buf = xmalloc(size);
731
732 memset(buf, 0, size);
733 *buf = *inode;
734
735 large_inode = (struct ext2_inode_large *) buf;
736 large_inode->i_extra_isize = sizeof(struct ext2_inode_large) -
737 EXT2_GOOD_OLD_INODE_SIZE;
738
739 return ext2fs_write_inode_full(fs, ino, buf, size);
740}
741
742
743errcode_t ext2fs_get_blocks(ext2_filsys fs, ext2_ino_t ino, blk_t *blocks)
744{
745 struct ext2_inode inode;
746 int i;
747 errcode_t retval;
748
749 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
750
751 if (ino > fs->super->s_inodes_count)
752 return EXT2_ET_BAD_INODE_NUM;
753
754 if (fs->get_blocks) {
755 if (!(*fs->get_blocks)(fs, ino, blocks))
756 return 0;
757 }
758 retval = ext2fs_read_inode(fs, ino, &inode);
759 if (retval)
760 return retval;
761 for (i=0; i < EXT2_N_BLOCKS; i++)
762 blocks[i] = inode.i_block[i];
763 return 0;
764}
765
766errcode_t ext2fs_check_directory(ext2_filsys fs, ext2_ino_t ino)
767{
768 struct ext2_inode inode;
769 errcode_t retval;
770
771 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
772
773 if (ino > fs->super->s_inodes_count)
774 return EXT2_ET_BAD_INODE_NUM;
775
776 if (fs->check_directory) {
777 retval = (fs->check_directory)(fs, ino);
778 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
779 return retval;
780 }
781 retval = ext2fs_read_inode(fs, ino, &inode);
782 if (retval)
783 return retval;
784 if (!LINUX_S_ISDIR(inode.i_mode))
785 return EXT2_ET_NO_DIRECTORY;
786 return 0;
787}
788
Note: See TracBrowser for help on using the repository browser.