OSDN Git Service

Many files:
[android-x86/external-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 Public
8  * License.
9  * %End-Header%
10  */
11
12 #include <stdio.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include <stdlib.h>
16 #include <sys/stat.h>
17 #include <sys/types.h>
18 #if HAVE_ERRNO_H
19 #include <errno.h>
20 #endif
21
22 #include <linux/ext2_fs.h>
23
24 #include "ext2fsP.h"
25
26 struct ext2_struct_inode_scan {
27         int                     magic;
28         ext2_filsys             fs;
29         ino_t                   current_inode;
30         blk_t                   current_block;
31         dgrp_t                  current_group;
32         int                     inodes_left, blocks_left, groups_left;
33         int                     inode_buffer_blocks;
34         char *                  inode_buffer;
35         int                     inode_size;
36         char *                  ptr;
37         int                     bytes_left;
38         char                    *temp_buffer;
39         errcode_t               (*done_group)(ext2_filsys fs,
40                                               ext2_inode_scan scan,
41                                               dgrp_t group,
42                                               void * private);
43         void *                  done_group_data;
44         int                     bad_block_ptr;
45         int                     scan_flags;
46         int                     reserved[6];
47 };
48
49 static errcode_t create_icache(ext2_filsys fs)
50 {
51         int     i;
52         
53         if (fs->icache)
54                 return 0;
55         fs->icache = malloc(sizeof(struct ext2_inode_cache));
56         memset(fs->icache, 0, sizeof(struct ext2_inode_cache));
57         fs->icache->buffer = malloc(fs->blocksize);
58         if (!fs->icache->buffer) {
59                 free(fs->icache);
60                 return ENOMEM;
61         }
62         fs->icache->buffer_blk = 0;
63         fs->icache->cache_last = -1;
64         fs->icache->cache_size = 4;
65         fs->icache->refcount = 1;
66         fs->icache->cache = malloc(sizeof(struct ext2_inode_cache_ent)
67                                    * fs->icache->cache_size);
68         if (!fs->icache->cache) {
69                 free(fs->icache->buffer);
70                 free(fs->icache);
71                 return ENOMEM;
72         }
73         for (i=0; i < fs->icache->cache_size; i++)
74                 fs->icache->cache[i].ino = 0;
75         return 0;
76 }
77
78 errcode_t ext2fs_open_inode_scan(ext2_filsys fs, int buffer_blocks,
79                                  ext2_inode_scan *ret_scan)
80 {
81         ext2_inode_scan scan;
82         errcode_t       retval;
83         errcode_t (*save_get_blocks)(ext2_filsys fs, ino_t ino, blk_t *blocks);
84
85         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
86
87         /*
88          * If fs->badblocks isn't set, then set it --- since the inode
89          * scanning functions require it.
90          */
91         if (fs->badblocks == 0) {
92                 /*
93                  * Temporarly save fs->get_blocks and set it to zero,
94                  * for compatibility with old e2fsck's.
95                  */
96                 save_get_blocks = fs->get_blocks;
97                 fs->get_blocks = 0;
98                 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
99                 if (retval && fs->badblocks) {
100                         badblocks_list_free(fs->badblocks);
101                         fs->badblocks = 0;
102                 }
103                 fs->get_blocks = save_get_blocks;
104         }
105
106         scan = (ext2_inode_scan) malloc(sizeof(struct ext2_struct_inode_scan));
107         if (!scan)
108                 return ENOMEM;
109         memset(scan, 0, sizeof(struct ext2_struct_inode_scan));
110
111         scan->magic = EXT2_ET_MAGIC_INODE_SCAN;
112         scan->fs = fs;
113         scan->inode_size = EXT2_INODE_SIZE(fs->super);
114         scan->bytes_left = 0;
115         scan->current_group = -1;
116         scan->inode_buffer_blocks = buffer_blocks ? buffer_blocks : 8;
117         scan->groups_left = fs->group_desc_count;
118         scan->inode_buffer = malloc(scan->inode_buffer_blocks * fs->blocksize);
119         scan->done_group = 0;
120         scan->done_group_data = 0;
121         scan->bad_block_ptr = 0;
122         if (!scan->inode_buffer) {
123                 free(scan);
124                 return ENOMEM;
125         }
126         scan->temp_buffer = malloc(scan->inode_size);
127         if (!scan->temp_buffer) {
128                 free(scan->inode_buffer);
129                 free(scan);
130                 return ENOMEM;
131         }
132         if (scan->fs->badblocks && scan->fs->badblocks->num)
133                 scan->scan_flags |= EXT2_SF_CHK_BADBLOCKS;
134         *ret_scan = scan;
135         return 0;
136 }
137
138 void ext2fs_close_inode_scan(ext2_inode_scan scan)
139 {
140         if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
141                 return;
142         
143         free(scan->inode_buffer);
144         scan->inode_buffer = NULL;
145         free(scan->temp_buffer);
146         scan->temp_buffer = NULL;
147         free(scan);
148         return;
149 }
150
151 void ext2fs_set_inode_callback(ext2_inode_scan scan,
152                                errcode_t (*done_group)(ext2_filsys fs,
153                                                        ext2_inode_scan scan,
154                                                        dgrp_t group,
155                                                        void * private),
156                                void *done_group_data)
157 {
158         if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
159                 return;
160         
161         scan->done_group = done_group;
162         scan->done_group_data = done_group_data;
163 }
164
165 int ext2fs_inode_scan_flags(ext2_inode_scan scan, int set_flags,
166                             int clear_flags)
167 {
168         int     old_flags;
169
170         if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
171                 return 0;
172
173         old_flags = scan->scan_flags;
174         scan->scan_flags &= ~clear_flags;
175         scan->scan_flags |= set_flags;
176         return old_flags;
177 }
178
179 /*
180  * This function is called by ext2fs_get_next_inode when it needs to
181  * get ready to read in a new blockgroup.
182  */
183 static errcode_t get_next_blockgroup(ext2_inode_scan scan)
184 {
185         scan->current_group++;
186         scan->groups_left--;
187                         
188         scan->current_block = scan->fs->
189                 group_desc[scan->current_group].bg_inode_table;
190
191         scan->bytes_left = 0;
192         scan->inodes_left = EXT2_INODES_PER_GROUP(scan->fs->super);
193         scan->blocks_left = scan->fs->inode_blocks_per_group;
194         return 0;
195 }
196
197 errcode_t ext2fs_inode_scan_goto_blockgroup(ext2_inode_scan scan,
198                                             int group)
199 {
200         scan->current_group = group - 1;
201         scan->groups_left = scan->fs->group_desc_count - group;
202         return get_next_blockgroup(scan);
203 }
204
205 /*
206  * This function is called by get_next_blocks() to check for bad
207  * blocks in the inode table.
208  *
209  * This function assumes that badblocks_list->list is sorted in
210  * increasing order.
211  */
212 static errcode_t check_for_inode_bad_blocks(ext2_inode_scan scan,
213                                             int *num_blocks)
214 {
215         blk_t   blk = scan->current_block;
216         badblocks_list  bb = scan->fs->badblocks;
217
218         /*
219          * If the inode table is missing, then obviously there are no
220          * bad blocks.  :-)
221          */
222         if (blk == 0)
223                 return 0;
224
225         /*
226          * If the current block is greater than the bad block listed
227          * in the bad block list, then advance the pointer until this
228          * is no longer the case.  If we run out of bad blocks, then
229          * we don't need to do any more checking!
230          */
231         while (blk > bb->list[scan->bad_block_ptr]) {
232                 if (++scan->bad_block_ptr >= bb->num) {
233                         scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
234                         return 0;
235                 }
236         }
237
238         /*
239          * If the current block is equal to the bad block listed in
240          * the bad block list, then handle that one block specially.
241          * (We could try to handle runs of bad blocks, but that
242          * only increases CPU efficiency by a small amount, at the
243          * expense of a huge expense of code complexity, and for an
244          * uncommon case at that.)
245          */
246         if (blk == bb->list[scan->bad_block_ptr]) {
247                 scan->scan_flags |= EXT2_SF_BAD_INODE_BLK;
248                 *num_blocks = 1;
249                 if (++scan->bad_block_ptr >= bb->num)
250                         scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
251                 return 0;
252         }
253
254         /*
255          * If there is a bad block in the range that we're about to
256          * read in, adjust the number of blocks to read so that we we
257          * don't read in the bad block.  (Then the next block to read
258          * will be the bad block, which is handled in the above case.)
259          */
260         if ((blk + *num_blocks) > bb->list[scan->bad_block_ptr])
261                 *num_blocks = bb->list[scan->bad_block_ptr] - blk;
262
263         return 0;
264 }
265
266 /*
267  * This function is called by ext2fs_get_next_inode when it needs to
268  * read in more blocks from the current blockgroup's inode table.
269  */
270 static errcode_t get_next_blocks(ext2_inode_scan scan)
271 {
272         int             num_blocks;
273         errcode_t       retval;
274
275         /*
276          * Figure out how many blocks to read; we read at most
277          * inode_buffer_blocks, and perhaps less if there aren't that
278          * many blocks left to read.
279          */
280         num_blocks = scan->inode_buffer_blocks;
281         if (num_blocks > scan->blocks_left)
282                 num_blocks = scan->blocks_left;
283
284         /*
285          * If the past block "read" was a bad block, then mark the
286          * left-over extra bytes as also being bad.
287          */
288         if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK) {
289                 if (scan->bytes_left)
290                         scan->scan_flags |= EXT2_SF_BAD_EXTRA_BYTES;
291                 scan->scan_flags &= ~EXT2_SF_BAD_INODE_BLK;
292         }
293
294         /*
295          * Do inode bad block processing, if necessary.
296          */
297         if (scan->scan_flags & EXT2_SF_CHK_BADBLOCKS) {
298                 retval = check_for_inode_bad_blocks(scan, &num_blocks);
299                 if (retval)
300                         return retval;
301         }
302                 
303         if ((scan->scan_flags & EXT2_SF_BAD_INODE_BLK) ||
304             (scan->current_block == 0)) {
305                 memset(scan->inode_buffer, 0,
306                        num_blocks * scan->fs->blocksize);
307         } else {
308                 retval = io_channel_read_blk(scan->fs->io,
309                                              scan->current_block,
310                                              num_blocks, scan->inode_buffer);
311                 if (retval)
312                         return EXT2_ET_NEXT_INODE_READ;
313         }
314         scan->ptr = scan->inode_buffer;
315         scan->bytes_left = num_blocks * scan->fs->blocksize;
316
317         scan->blocks_left -= num_blocks;
318         if (scan->current_block)
319                 scan->current_block += num_blocks;
320         return 0;
321 }
322
323 errcode_t ext2fs_get_next_inode(ext2_inode_scan scan, ino_t *ino,
324                                 struct ext2_inode *inode)
325 {
326         errcode_t       retval;
327         int             extra_bytes = 0;
328         
329         EXT2_CHECK_MAGIC(scan, EXT2_ET_MAGIC_INODE_SCAN);
330
331         /*
332          * Do we need to start reading a new block group?
333          */
334         if (scan->inodes_left <= 0) {
335         retry:
336                 if (scan->done_group) {
337                         retval = (scan->done_group)
338                                 (scan->fs, scan, scan->current_group,
339                                  scan->done_group_data);
340                         if (retval)
341                                 return retval;
342                 }
343                 if (scan->groups_left <= 0) {
344                         *ino = 0;
345                         return 0;
346                 }
347                 retval = get_next_blockgroup(scan);
348                 if (retval)
349                         return retval;
350                 if (scan->current_block == 0) {
351                         if (scan->scan_flags & EXT2_SF_SKIP_MISSING_ITABLE) {
352                                 goto retry;
353                         } else
354                                 return EXT2_ET_MISSING_INODE_TABLE;
355                 }
356         }
357
358         /*
359          * Have we run out of space in the inode buffer?  If so, we
360          * need to read in more blocks.
361          */
362         if (scan->bytes_left < scan->inode_size) {
363                 memcpy(scan->temp_buffer, scan->ptr, scan->bytes_left);
364                 extra_bytes = scan->bytes_left;
365
366                 retval = get_next_blocks(scan);
367                 if (retval)
368                         return retval;
369         }
370
371         retval = 0;
372         if (extra_bytes) {
373                 memcpy(scan->temp_buffer+extra_bytes, scan->ptr,
374                        scan->inode_size - extra_bytes);
375                 scan->ptr += scan->inode_size - extra_bytes;
376                 scan->bytes_left -= scan->inode_size - extra_bytes;
377
378                 if ((scan->fs->flags & EXT2_FLAG_SWAP_BYTES) ||
379                     (scan->fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
380                         ext2fs_swap_inode(scan->fs, inode,
381                                  (struct ext2_inode *) scan->temp_buffer, 0);
382                 else
383                         *inode = *((struct ext2_inode *) scan->temp_buffer);
384                 if (scan->scan_flags & EXT2_SF_BAD_EXTRA_BYTES)
385                         retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
386                 scan->scan_flags &= ~EXT2_SF_BAD_EXTRA_BYTES;
387         } else {
388                 if ((scan->fs->flags & EXT2_FLAG_SWAP_BYTES) ||
389                     (scan->fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
390                         ext2fs_swap_inode(scan->fs, inode,
391                                  (struct ext2_inode *) scan->ptr, 0);
392                 else
393                         *inode = *((struct ext2_inode *) scan->ptr);
394                 scan->ptr += scan->inode_size;
395                 scan->bytes_left -= scan->inode_size;
396                 if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK)
397                         retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
398         }
399
400         scan->inodes_left--;
401         scan->current_inode++;
402         *ino = scan->current_inode;
403         return retval;
404 }
405
406 /*
407  * Functions to read and write a single inode.
408  */
409 errcode_t ext2fs_read_inode (ext2_filsys fs, ino_t ino,
410                              struct ext2_inode * inode)
411 {
412         unsigned long   group, block, block_nr, offset;
413         char            *ptr;
414         errcode_t       retval;
415         int             clen, length, i;
416
417         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
418
419         /* Check to see if user has an override function */
420         if (fs->read_inode) {
421                 retval = (fs->read_inode)(fs, ino, inode);
422                 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
423                         return retval;
424         }
425         /* Create inode cache if not present */
426         if (!fs->icache) {
427                 retval = create_icache(fs);
428                 if (retval)
429                         return retval;
430         }
431         /* Check to see if it's in the inode cache */
432         for (i=0; i < fs->icache->cache_size; i++) {
433                 if (fs->icache->cache[i].ino == ino) {
434                         *inode = fs->icache->cache[i].inode;
435                         return 0;
436                 }
437         }
438         if (ino > fs->super->s_inodes_count)
439                 return EXT2_ET_BAD_INODE_NUM;
440         group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
441         offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
442                 EXT2_INODE_SIZE(fs->super);
443         block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
444         if (!fs->group_desc[group].bg_inode_table)
445                 return EXT2_ET_MISSING_INODE_TABLE;
446         block_nr = fs->group_desc[group].bg_inode_table + block;
447         if (block_nr != fs->icache->buffer_blk) {
448                 retval = io_channel_read_blk(fs->io, block_nr, 1,
449                                              fs->icache->buffer);
450                 if (retval)
451                         return retval;
452                 fs->icache->buffer_blk = block_nr;
453         }
454         offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
455         ptr = ((char *) fs->icache->buffer) + offset;
456
457         memset(inode, 0, sizeof(struct ext2_inode));
458         length = EXT2_INODE_SIZE(fs->super);
459         if (length > sizeof(struct ext2_inode))
460                 length = sizeof(struct ext2_inode);
461         
462         if ((offset + length) > EXT2_BLOCK_SIZE(fs->super)) {
463                 clen = EXT2_BLOCK_SIZE(fs->super) - offset;
464                 memcpy((char *) inode, ptr, clen);
465                 length -= clen;
466                 
467                 retval = io_channel_read_blk(fs->io, block_nr+1, 1,
468                                              fs->icache->buffer);
469                 if (retval) {
470                         fs->icache->buffer_blk = 0;
471                         return retval;
472                 }
473                 fs->icache->buffer_blk = block_nr+1;
474                 
475                 memcpy(((char *) inode) + clen,
476                        fs->icache->buffer, length);
477         } else
478                 memcpy((char *) inode, ptr, length);
479         
480         if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
481             (fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
482                 ext2fs_swap_inode(fs, inode, inode, 0);
483
484         /* Update the inode cache */
485         fs->icache->cache_last = (fs->icache->cache_last + 1) %
486                 fs->icache->cache_size;
487         fs->icache->cache[fs->icache->cache_last].ino = ino;
488         fs->icache->cache[fs->icache->cache_last].inode = *inode;
489         
490         return 0;
491 }
492
493 errcode_t ext2fs_write_inode(ext2_filsys fs, ino_t ino,
494                              struct ext2_inode * inode)
495 {
496         unsigned long group, block, block_nr, offset;
497         errcode_t       retval;
498         struct ext2_inode temp_inode;
499         char *ptr;
500         int clen, length, i;
501
502         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
503
504         /* Check to see if user provided an override function */
505         if (fs->write_inode) {
506                 retval = (fs->write_inode)(fs, ino, inode);
507                 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
508                         return retval;
509         }
510
511         /* Check to see if the inode cache needs to be updated */
512         if (fs->icache) {
513                 for (i=0; i < fs->icache->cache_size; i++) {
514                         if (fs->icache->cache[i].ino == ino) {
515                                 fs->icache->cache[i].inode = *inode;
516                                 break;
517                         }
518                 }
519         } else {
520                 retval = create_icache(fs);
521                 if (retval)
522                         return retval;
523         }
524                 
525         if (!(fs->flags & EXT2_FLAG_RW))
526                 return EXT2_ET_RO_FILSYS;
527
528         if (ino > fs->super->s_inodes_count)
529                 return EXT2_ET_BAD_INODE_NUM;
530
531         if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
532             (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE))
533                 ext2fs_swap_inode(fs, &temp_inode, inode, 1);
534         else
535                 memcpy(&temp_inode, inode, sizeof(struct ext2_inode));
536         
537         group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
538         offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
539                 EXT2_INODE_SIZE(fs->super);
540         block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
541         if (!fs->group_desc[group].bg_inode_table)
542                 return EXT2_ET_MISSING_INODE_TABLE;
543         block_nr = fs->group_desc[group].bg_inode_table + block;
544         offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
545         ptr = (char *) fs->icache->buffer + offset;
546
547         length = EXT2_INODE_SIZE(fs->super);
548         clen = length;
549         if (length > sizeof(struct ext2_inode))
550                 length = sizeof(struct ext2_inode);
551         
552         if (fs->icache->buffer_blk != block_nr) {
553                 retval = io_channel_read_blk(fs->io, block_nr, 1,
554                                              fs->icache->buffer);
555                 if (retval)
556                         return retval;
557                 fs->icache->buffer_blk = block_nr;
558         }
559         
560         if ((offset + length) > EXT2_BLOCK_SIZE(fs->super)) {
561                 clen = EXT2_BLOCK_SIZE(fs->super) - offset;
562                 length -= clen;
563         } else {
564                 length = 0;
565         }
566         memcpy(ptr, &temp_inode, clen);
567         retval = io_channel_write_blk(fs->io, block_nr, 1, fs->icache->buffer);
568         if (retval)
569                 return retval;
570
571         if (length) {
572                 retval = io_channel_read_blk(fs->io, ++block_nr, 1,
573                                              fs->icache->buffer);
574                 if (retval) {
575                         fs->icache->buffer_blk = 0;
576                         return retval;
577                 }
578                 fs->icache->buffer_blk = block_nr;
579                 memcpy(fs->icache->buffer, ((char *) &temp_inode) + clen,
580                        length);
581
582                 retval = io_channel_write_blk(fs->io, block_nr, 1,
583                                               fs->icache->buffer);
584                 if (retval)
585                         return retval;
586         }
587         
588         fs->flags |= EXT2_FLAG_CHANGED;
589         return 0;
590 }
591
592 errcode_t ext2fs_get_blocks(ext2_filsys fs, ino_t ino, blk_t *blocks)
593 {
594         struct ext2_inode       inode;
595         int                     i;
596         errcode_t               retval;
597         
598         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
599
600         if (ino > fs->super->s_inodes_count)
601                 return EXT2_ET_BAD_INODE_NUM;
602
603         if (fs->get_blocks) {
604                 if (!(*fs->get_blocks)(fs, ino, blocks))
605                         return 0;
606         }
607         retval = ext2fs_read_inode(fs, ino, &inode);
608         if (retval)
609                 return retval;
610         for (i=0; i < EXT2_N_BLOCKS; i++)
611                 blocks[i] = inode.i_block[i];
612         return 0;
613 }
614
615 errcode_t ext2fs_check_directory(ext2_filsys fs, ino_t ino)
616 {
617         struct  ext2_inode      inode;
618         errcode_t               retval;
619         
620         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
621
622         if (ino > fs->super->s_inodes_count)
623                 return EXT2_ET_BAD_INODE_NUM;
624
625         if (fs->check_directory)
626                 return (fs->check_directory)(fs, ino);
627         retval = ext2fs_read_inode(fs, ino, &inode);
628         if (retval)
629                 return retval;
630         if (!LINUX_S_ISDIR(inode.i_mode))
631                 return ENOTDIR;
632         return 0;
633 }
634