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