blob: 6c845a63492a7ef8055552af2502a6140643aabb [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * inode.c --- utility routines to read and write inodes
3 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00004 * 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%
Theodore Ts'o3839e651997-04-26 13:21:57 +000010 */
11
12#include <stdio.h>
13#include <string.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000014#if HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000015#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000016#endif
Theodore Ts'o73311962005-01-25 23:42:56 -050017#if HAVE_ERRNO_H
18#include <errno.h>
19#endif
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000020#if HAVE_SYS_STAT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000021#include <sys/stat.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000022#endif
23#if HAVE_SYS_TYPES_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000024#include <sys/types.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000025#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000026
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000027#include "ext2_fs.h"
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000028#include "ext2fsP.h"
Theodore Ts'oa78926e2001-05-03 04:02:29 +000029#include "e2image.h"
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000030
31struct ext2_struct_inode_scan {
Theodore Ts'o3cb6c501997-08-11 20:29:22 +000032 errcode_t magic;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000033 ext2_filsys fs;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000034 ext2_ino_t current_inode;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000035 blk_t current_block;
36 dgrp_t current_group;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000037 ext2_ino_t inodes_left;
Theodore Ts'o3cb6c501997-08-11 20:29:22 +000038 blk_t blocks_left;
39 dgrp_t groups_left;
40 blk_t inode_buffer_blocks;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000041 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 ext2_inode_scan scan,
48 dgrp_t group,
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000049 void * priv_data);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000050 void * done_group_data;
51 int bad_block_ptr;
52 int scan_flags;
53 int reserved[6];
54};
Theodore Ts'o3839e651997-04-26 13:21:57 +000055
Theodore Ts'o6a7f4552000-11-12 19:07:06 +000056/*
57 * This routine flushes the icache, if it exists.
58 */
59errcode_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
Theodore Ts'o71669d02004-12-23 21:49:05 -050069 fs->icache->buffer_blk = 0;
Theodore Ts'o6a7f4552000-11-12 19:07:06 +000070 return 0;
71}
72
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000073static errcode_t create_icache(ext2_filsys fs)
74{
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000075 errcode_t retval;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000076
77 if (fs->icache)
78 return 0;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040079 retval = ext2fs_get_mem(sizeof(struct ext2_inode_cache), &fs->icache);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000080 if (retval)
81 return retval;
82
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000083 memset(fs->icache, 0, sizeof(struct ext2_inode_cache));
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040084 retval = ext2fs_get_mem(fs->blocksize, &fs->icache->buffer);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000085 if (retval) {
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040086 ext2fs_free_mem(&fs->icache);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000087 return retval;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000088 }
89 fs->icache->buffer_blk = 0;
90 fs->icache->cache_last = -1;
91 fs->icache->cache_size = 4;
92 fs->icache->refcount = 1;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000093 retval = ext2fs_get_mem(sizeof(struct ext2_inode_cache_ent)
94 * fs->icache->cache_size,
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040095 &fs->icache->cache);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000096 if (retval) {
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040097 ext2fs_free_mem(&fs->icache->buffer);
98 ext2fs_free_mem(&fs->icache);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000099 return retval;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000100 }
Theodore Ts'o6a7f4552000-11-12 19:07:06 +0000101 ext2fs_flush_icache(fs);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000102 return 0;
103}
104
Theodore Ts'o3839e651997-04-26 13:21:57 +0000105errcode_t ext2fs_open_inode_scan(ext2_filsys fs, int buffer_blocks,
106 ext2_inode_scan *ret_scan)
107{
108 ext2_inode_scan scan;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000109 errcode_t retval;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000110 errcode_t (*save_get_blocks)(ext2_filsys f, ext2_ino_t ino, blk_t *blocks);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000111
Theodore Ts'of3db3561997-04-26 13:34:30 +0000112 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
113
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000114 /*
115 * If fs->badblocks isn't set, then set it --- since the inode
116 * scanning functions require it.
117 */
118 if (fs->badblocks == 0) {
Theodore Ts'o521e3681997-04-29 17:48:10 +0000119 /*
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;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000125 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
126 if (retval && fs->badblocks) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000127 ext2fs_badblocks_list_free(fs->badblocks);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000128 fs->badblocks = 0;
129 }
Theodore Ts'o521e3681997-04-29 17:48:10 +0000130 fs->get_blocks = save_get_blocks;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000131 }
132
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400133 retval = ext2fs_get_mem(sizeof(struct ext2_struct_inode_scan), &scan);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000134 if (retval)
135 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000136 memset(scan, 0, sizeof(struct ext2_struct_inode_scan));
137
Theodore Ts'of3db3561997-04-26 13:34:30 +0000138 scan->magic = EXT2_ET_MAGIC_INODE_SCAN;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000139 scan->fs = fs;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000140 scan->inode_size = EXT2_INODE_SIZE(fs->super);
141 scan->bytes_left = 0;
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +0000142 scan->current_group = 0;
143 scan->groups_left = fs->group_desc_count - 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000144 scan->inode_buffer_blocks = buffer_blocks ? buffer_blocks : 8;
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +0000145 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;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000149 retval = ext2fs_get_mem((size_t) (scan->inode_buffer_blocks *
150 fs->blocksize),
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400151 &scan->inode_buffer);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000152 scan->done_group = 0;
153 scan->done_group_data = 0;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000154 scan->bad_block_ptr = 0;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000155 if (retval) {
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400156 ext2fs_free_mem(&scan);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000157 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000158 }
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400159 retval = ext2fs_get_mem(scan->inode_size, &scan->temp_buffer);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000160 if (retval) {
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400161 ext2fs_free_mem(&scan->inode_buffer);
162 ext2fs_free_mem(&scan);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000163 return retval;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000164 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000165 if (scan->fs->badblocks && scan->fs->badblocks->num)
166 scan->scan_flags |= EXT2_SF_CHK_BADBLOCKS;
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400167 if (EXT2_HAS_COMPAT_FEATURE(fs->super,
168 EXT2_FEATURE_COMPAT_LAZY_BG))
169 scan->scan_flags |= EXT2_SF_DO_LAZY;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000170 *ret_scan = scan;
171 return 0;
172}
173
174void ext2fs_close_inode_scan(ext2_inode_scan scan)
175{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000176 if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
177 return;
178
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400179 ext2fs_free_mem(&scan->inode_buffer);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000180 scan->inode_buffer = NULL;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400181 ext2fs_free_mem(&scan->temp_buffer);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000182 scan->temp_buffer = NULL;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400183 ext2fs_free_mem(&scan);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000184 return;
185}
186
Theodore Ts'of3db3561997-04-26 13:34:30 +0000187void ext2fs_set_inode_callback(ext2_inode_scan scan,
188 errcode_t (*done_group)(ext2_filsys fs,
189 ext2_inode_scan scan,
190 dgrp_t group,
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +0000191 void * priv_data),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000192 void *done_group_data)
193{
194 if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
195 return;
196
197 scan->done_group = done_group;
198 scan->done_group_data = done_group_data;
199}
200
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000201int ext2fs_inode_scan_flags(ext2_inode_scan scan, int set_flags,
202 int clear_flags)
203{
204 int old_flags;
205
206 if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
207 return 0;
208
209 old_flags = scan->scan_flags;
210 scan->scan_flags &= ~clear_flags;
211 scan->scan_flags |= set_flags;
212 return old_flags;
213}
214
215/*
216 * This function is called by ext2fs_get_next_inode when it needs to
217 * get ready to read in a new blockgroup.
218 */
219static errcode_t get_next_blockgroup(ext2_inode_scan scan)
220{
221 scan->current_group++;
222 scan->groups_left--;
223
224 scan->current_block = scan->fs->
225 group_desc[scan->current_group].bg_inode_table;
226
Theodore Ts'o818180c1998-06-27 05:11:14 +0000227 scan->current_inode = scan->current_group *
228 EXT2_INODES_PER_GROUP(scan->fs->super);
229
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000230 scan->bytes_left = 0;
231 scan->inodes_left = EXT2_INODES_PER_GROUP(scan->fs->super);
232 scan->blocks_left = scan->fs->inode_blocks_per_group;
233 return 0;
234}
235
236errcode_t ext2fs_inode_scan_goto_blockgroup(ext2_inode_scan scan,
237 int group)
238{
239 scan->current_group = group - 1;
240 scan->groups_left = scan->fs->group_desc_count - group;
241 return get_next_blockgroup(scan);
242}
243
244/*
245 * This function is called by get_next_blocks() to check for bad
246 * blocks in the inode table.
247 *
248 * This function assumes that badblocks_list->list is sorted in
249 * increasing order.
250 */
251static errcode_t check_for_inode_bad_blocks(ext2_inode_scan scan,
Theodore Ts'o3cb6c501997-08-11 20:29:22 +0000252 blk_t *num_blocks)
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000253{
254 blk_t blk = scan->current_block;
255 badblocks_list bb = scan->fs->badblocks;
256
257 /*
258 * If the inode table is missing, then obviously there are no
259 * bad blocks. :-)
260 */
261 if (blk == 0)
262 return 0;
263
264 /*
265 * If the current block is greater than the bad block listed
266 * in the bad block list, then advance the pointer until this
267 * is no longer the case. If we run out of bad blocks, then
268 * we don't need to do any more checking!
269 */
270 while (blk > bb->list[scan->bad_block_ptr]) {
271 if (++scan->bad_block_ptr >= bb->num) {
272 scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
273 return 0;
274 }
275 }
276
277 /*
278 * If the current block is equal to the bad block listed in
279 * the bad block list, then handle that one block specially.
280 * (We could try to handle runs of bad blocks, but that
281 * only increases CPU efficiency by a small amount, at the
282 * expense of a huge expense of code complexity, and for an
283 * uncommon case at that.)
284 */
285 if (blk == bb->list[scan->bad_block_ptr]) {
286 scan->scan_flags |= EXT2_SF_BAD_INODE_BLK;
287 *num_blocks = 1;
288 if (++scan->bad_block_ptr >= bb->num)
289 scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
290 return 0;
291 }
292
293 /*
294 * If there is a bad block in the range that we're about to
295 * read in, adjust the number of blocks to read so that we we
296 * don't read in the bad block. (Then the next block to read
297 * will be the bad block, which is handled in the above case.)
298 */
299 if ((blk + *num_blocks) > bb->list[scan->bad_block_ptr])
Theodore Ts'o3cb6c501997-08-11 20:29:22 +0000300 *num_blocks = (int) (bb->list[scan->bad_block_ptr] - blk);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000301
302 return 0;
303}
304
305/*
306 * This function is called by ext2fs_get_next_inode when it needs to
307 * read in more blocks from the current blockgroup's inode table.
308 */
309static errcode_t get_next_blocks(ext2_inode_scan scan)
310{
Theodore Ts'o3cb6c501997-08-11 20:29:22 +0000311 blk_t num_blocks;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000312 errcode_t retval;
313
314 /*
315 * Figure out how many blocks to read; we read at most
316 * inode_buffer_blocks, and perhaps less if there aren't that
317 * many blocks left to read.
318 */
319 num_blocks = scan->inode_buffer_blocks;
320 if (num_blocks > scan->blocks_left)
321 num_blocks = scan->blocks_left;
322
323 /*
324 * If the past block "read" was a bad block, then mark the
325 * left-over extra bytes as also being bad.
326 */
327 if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK) {
328 if (scan->bytes_left)
329 scan->scan_flags |= EXT2_SF_BAD_EXTRA_BYTES;
330 scan->scan_flags &= ~EXT2_SF_BAD_INODE_BLK;
331 }
332
333 /*
334 * Do inode bad block processing, if necessary.
335 */
336 if (scan->scan_flags & EXT2_SF_CHK_BADBLOCKS) {
337 retval = check_for_inode_bad_blocks(scan, &num_blocks);
338 if (retval)
339 return retval;
340 }
341
342 if ((scan->scan_flags & EXT2_SF_BAD_INODE_BLK) ||
343 (scan->current_block == 0)) {
344 memset(scan->inode_buffer, 0,
Theodore Ts'o3cb6c501997-08-11 20:29:22 +0000345 (size_t) num_blocks * scan->fs->blocksize);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000346 } else {
347 retval = io_channel_read_blk(scan->fs->io,
348 scan->current_block,
Theodore Ts'o3cb6c501997-08-11 20:29:22 +0000349 (int) num_blocks,
350 scan->inode_buffer);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000351 if (retval)
352 return EXT2_ET_NEXT_INODE_READ;
353 }
354 scan->ptr = scan->inode_buffer;
355 scan->bytes_left = num_blocks * scan->fs->blocksize;
356
357 scan->blocks_left -= num_blocks;
358 if (scan->current_block)
359 scan->current_block += num_blocks;
360 return 0;
361}
362
Theodore Ts'o818180c1998-06-27 05:11:14 +0000363#if 0
364/*
365 * Returns 1 if the entire inode_buffer has a non-zero size and
366 * contains all zeros. (Not just deleted inodes, since that means
367 * that part of the inode table was used at one point; we want all
368 * zeros, which means that the inode table is pristine.)
369 */
370static inline int is_empty_scan(ext2_inode_scan scan)
371{
372 int i;
373
374 if (scan->bytes_left == 0)
375 return 0;
376
377 for (i=0; i < scan->bytes_left; i++)
378 if (scan->ptr[i])
379 return 0;
380 return 1;
381}
382#endif
383
Theodore Ts'o73311962005-01-25 23:42:56 -0500384errcode_t ext2fs_get_next_inode_full(ext2_inode_scan scan, ext2_ino_t *ino,
385 struct ext2_inode *inode, int bufsize)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000386{
387 errcode_t retval;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000388 int extra_bytes = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000389
Theodore Ts'of3db3561997-04-26 13:34:30 +0000390 EXT2_CHECK_MAGIC(scan, EXT2_ET_MAGIC_INODE_SCAN);
391
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000392 /*
393 * Do we need to start reading a new block group?
394 */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000395 if (scan->inodes_left <= 0) {
Theodore Ts'o818180c1998-06-27 05:11:14 +0000396 force_new_group:
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000397 if (scan->done_group) {
398 retval = (scan->done_group)
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000399 (scan->fs, scan, scan->current_group,
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000400 scan->done_group_data);
401 if (retval)
402 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000403 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000404 if (scan->groups_left <= 0) {
405 *ino = 0;
406 return 0;
407 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000408 retval = get_next_blockgroup(scan);
409 if (retval)
410 return retval;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000411 }
Theodore Ts'o218a4861998-02-21 01:41:39 +0000412 /*
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400413 * These checks are done outside the above if statement so
414 * they can be done for block group #0.
Theodore Ts'o218a4861998-02-21 01:41:39 +0000415 */
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400416 if ((scan->scan_flags & EXT2_SF_DO_LAZY) &&
417 (scan->fs->group_desc[scan->current_group].bg_flags &
418 EXT2_BG_INODE_UNINIT))
419 goto force_new_group;
Theodore Ts'o218a4861998-02-21 01:41:39 +0000420 if (scan->current_block == 0) {
421 if (scan->scan_flags & EXT2_SF_SKIP_MISSING_ITABLE) {
Theodore Ts'o818180c1998-06-27 05:11:14 +0000422 goto force_new_group;
Theodore Ts'o218a4861998-02-21 01:41:39 +0000423 } else
424 return EXT2_ET_MISSING_INODE_TABLE;
425 }
426
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000427
428 /*
429 * Have we run out of space in the inode buffer? If so, we
430 * need to read in more blocks.
431 */
432 if (scan->bytes_left < scan->inode_size) {
433 memcpy(scan->temp_buffer, scan->ptr, scan->bytes_left);
434 extra_bytes = scan->bytes_left;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000435
436 retval = get_next_blocks(scan);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000437 if (retval)
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000438 return retval;
Theodore Ts'o818180c1998-06-27 05:11:14 +0000439#if 0
440 /*
441 * XXX test Need check for used inode somehow.
442 * (Note: this is hard.)
443 */
444 if (is_empty_scan(scan))
445 goto force_new_group;
446#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000447 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000448
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000449 retval = 0;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000450 if (extra_bytes) {
451 memcpy(scan->temp_buffer+extra_bytes, scan->ptr,
452 scan->inode_size - extra_bytes);
453 scan->ptr += scan->inode_size - extra_bytes;
454 scan->bytes_left -= scan->inode_size - extra_bytes;
455
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000456#ifdef EXT2FS_ENABLE_SWAPFS
Theodore Ts'o5c576471997-04-29 15:29:49 +0000457 if ((scan->fs->flags & EXT2_FLAG_SWAP_BYTES) ||
458 (scan->fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
Theodore Ts'o73311962005-01-25 23:42:56 -0500459 ext2fs_swap_inode_full(scan->fs,
460 (struct ext2_inode_large *) inode,
461 (struct ext2_inode_large *) scan->temp_buffer,
462 0, bufsize);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000463 else
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000464#endif
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000465 *inode = *((struct ext2_inode *) scan->temp_buffer);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000466 if (scan->scan_flags & EXT2_SF_BAD_EXTRA_BYTES)
467 retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
468 scan->scan_flags &= ~EXT2_SF_BAD_EXTRA_BYTES;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000469 } else {
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000470#ifdef EXT2FS_ENABLE_SWAPFS
Theodore Ts'o5c576471997-04-29 15:29:49 +0000471 if ((scan->fs->flags & EXT2_FLAG_SWAP_BYTES) ||
472 (scan->fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
Theodore Ts'o73311962005-01-25 23:42:56 -0500473 ext2fs_swap_inode_full(scan->fs,
474 (struct ext2_inode_large *) inode,
475 (struct ext2_inode_large *) scan->ptr,
476 0, bufsize);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000477 else
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000478#endif
Theodore Ts'o73311962005-01-25 23:42:56 -0500479 memcpy(inode, scan->ptr, bufsize);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000480 scan->ptr += scan->inode_size;
481 scan->bytes_left -= scan->inode_size;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000482 if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK)
483 retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000484 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000485
Theodore Ts'o3839e651997-04-26 13:21:57 +0000486 scan->inodes_left--;
487 scan->current_inode++;
488 *ino = scan->current_inode;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000489 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000490}
491
Theodore Ts'o73311962005-01-25 23:42:56 -0500492errcode_t ext2fs_get_next_inode(ext2_inode_scan scan, ext2_ino_t *ino,
493 struct ext2_inode *inode)
494{
495 return ext2fs_get_next_inode_full(scan, ino, inode,
496 sizeof(struct ext2_inode));
497}
498
Theodore Ts'o3839e651997-04-26 13:21:57 +0000499/*
500 * Functions to read and write a single inode.
501 */
Theodore Ts'o73311962005-01-25 23:42:56 -0500502errcode_t ext2fs_read_inode_full(ext2_filsys fs, ext2_ino_t ino,
503 struct ext2_inode * inode, int bufsize)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000504{
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000505 unsigned long group, block, block_nr, offset;
506 char *ptr;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000507 errcode_t retval;
Theodore Ts'o73311962005-01-25 23:42:56 -0500508 int clen, i, inodes_per_block, length;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400509 io_channel io;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000510
Theodore Ts'of3db3561997-04-26 13:34:30 +0000511 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
512
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000513 /* Check to see if user has an override function */
514 if (fs->read_inode) {
515 retval = (fs->read_inode)(fs, ino, inode);
516 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
517 return retval;
518 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000519 /* Create inode cache if not present */
520 if (!fs->icache) {
521 retval = create_icache(fs);
522 if (retval)
523 return retval;
524 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000525 /* Check to see if it's in the inode cache */
Theodore Ts'o73311962005-01-25 23:42:56 -0500526 if (bufsize == sizeof(struct ext2_inode)) {
527 /* only old good inode can be retrieve from the cache */
528 for (i=0; i < fs->icache->cache_size; i++) {
529 if (fs->icache->cache[i].ino == ino) {
530 *inode = fs->icache->cache[i].inode;
531 return 0;
532 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000533 }
534 }
Theodore Ts'o665f7101999-01-08 13:33:39 +0000535 if ((ino == 0) || (ino > fs->super->s_inodes_count))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000536 return EXT2_ET_BAD_INODE_NUM;
Theodore Ts'oa78926e2001-05-03 04:02:29 +0000537 if (fs->flags & EXT2_FLAG_IMAGE_FILE) {
538 inodes_per_block = fs->blocksize / EXT2_INODE_SIZE(fs->super);
539 block_nr = fs->image_header->offset_inode / fs->blocksize;
540 block_nr += (ino - 1) / inodes_per_block;
541 offset = ((ino - 1) % inodes_per_block) *
542 EXT2_INODE_SIZE(fs->super);
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400543 io = fs->image_io;
Theodore Ts'oa78926e2001-05-03 04:02:29 +0000544 } else {
545 group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
546 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
547 EXT2_INODE_SIZE(fs->super);
548 block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
549 if (!fs->group_desc[(unsigned)group].bg_inode_table)
550 return EXT2_ET_MISSING_INODE_TABLE;
551 block_nr = fs->group_desc[(unsigned)group].bg_inode_table +
552 block;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400553 io = fs->io;
Theodore Ts'oa78926e2001-05-03 04:02:29 +0000554 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000555 offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000556
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000557 length = EXT2_INODE_SIZE(fs->super);
Theodore Ts'o73311962005-01-25 23:42:56 -0500558 if (bufsize < length)
559 length = bufsize;
560
561 ptr = (char *) inode;
562 while (length) {
563 clen = length;
564 if ((offset + length) > fs->blocksize)
565 clen = fs->blocksize - offset;
566
567 if (block_nr != fs->icache->buffer_blk) {
568 retval = io_channel_read_blk(io, block_nr, 1,
569 fs->icache->buffer);
570 if (retval)
571 return retval;
572 fs->icache->buffer_blk = block_nr;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000573 }
Theodore Ts'o73311962005-01-25 23:42:56 -0500574
575 memcpy(ptr, ((char *) fs->icache->buffer) + (unsigned) offset,
576 clen);
577
578 offset = 0;
579 length -= clen;
580 ptr += clen;
581 block_nr++;
582 }
583
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000584#ifdef EXT2FS_ENABLE_SWAPFS
Theodore Ts'o5c576471997-04-29 15:29:49 +0000585 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
586 (fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
Theodore Ts'o73311962005-01-25 23:42:56 -0500587 ext2fs_swap_inode_full(fs, (struct ext2_inode_large *) inode,
588 (struct ext2_inode_large *) inode,
589 0, length);
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000590#endif
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000591
592 /* Update the inode cache */
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000593 fs->icache->cache_last = (fs->icache->cache_last + 1) %
594 fs->icache->cache_size;
595 fs->icache->cache[fs->icache->cache_last].ino = ino;
596 fs->icache->cache[fs->icache->cache_last].inode = *inode;
597
Theodore Ts'o3839e651997-04-26 13:21:57 +0000598 return 0;
599}
600
Theodore Ts'o73311962005-01-25 23:42:56 -0500601errcode_t ext2fs_read_inode(ext2_filsys fs, ext2_ino_t ino,
602 struct ext2_inode * inode)
603{
604 return ext2fs_read_inode_full(fs, ino, inode,
605 sizeof(struct ext2_inode));
606}
607
608errcode_t ext2fs_write_inode_full(ext2_filsys fs, ext2_ino_t ino,
609 struct ext2_inode * inode, int bufsize)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000610{
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000611 unsigned long group, block, block_nr, offset;
Theodore Ts'o73311962005-01-25 23:42:56 -0500612 errcode_t retval = 0;
613 struct ext2_inode_large temp_inode, *w_inode;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000614 char *ptr;
Theodore Ts'o73311962005-01-25 23:42:56 -0500615 int clen, i, length;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000616
Theodore Ts'of3db3561997-04-26 13:34:30 +0000617 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
618
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000619 /* Check to see if user provided an override function */
620 if (fs->write_inode) {
621 retval = (fs->write_inode)(fs, ino, inode);
622 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
623 return retval;
624 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000625
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000626 /* Check to see if the inode cache needs to be updated */
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000627 if (fs->icache) {
628 for (i=0; i < fs->icache->cache_size; i++) {
629 if (fs->icache->cache[i].ino == ino) {
630 fs->icache->cache[i].inode = *inode;
631 break;
632 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000633 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000634 } else {
635 retval = create_icache(fs);
636 if (retval)
637 return retval;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000638 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000639
Theodore Ts'o3839e651997-04-26 13:21:57 +0000640 if (!(fs->flags & EXT2_FLAG_RW))
641 return EXT2_ET_RO_FILSYS;
642
Theodore Ts'o665f7101999-01-08 13:33:39 +0000643 if ((ino == 0) || (ino > fs->super->s_inodes_count))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000644 return EXT2_ET_BAD_INODE_NUM;
645
Theodore Ts'o73311962005-01-25 23:42:56 -0500646 length = bufsize;
647 if (length < EXT2_INODE_SIZE(fs->super))
648 length = EXT2_INODE_SIZE(fs->super);
649
650 if (length > (int) sizeof(struct ext2_inode_large)) {
651 w_inode = malloc(length);
652 if (!w_inode)
653 return ENOMEM;
654 } else
655 w_inode = &temp_inode;
656 memset(w_inode, 0, length);
657
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000658#ifdef EXT2FS_ENABLE_SWAPFS
Theodore Ts'o5c576471997-04-29 15:29:49 +0000659 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
660 (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE))
Theodore Ts'o73311962005-01-25 23:42:56 -0500661 ext2fs_swap_inode_full(fs, w_inode,
662 (struct ext2_inode_large *) inode,
663 1, bufsize);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000664 else
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000665#endif
Theodore Ts'o73311962005-01-25 23:42:56 -0500666 memcpy(w_inode, inode, bufsize);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000667
Theodore Ts'o3839e651997-04-26 13:21:57 +0000668 group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000669 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
670 EXT2_INODE_SIZE(fs->super);
671 block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
Brian Behlendorfe649be92007-03-21 17:38:47 -0400672 if (!fs->group_desc[(unsigned) group].bg_inode_table) {
673 retval = EXT2_ET_MISSING_INODE_TABLE;
674 goto errout;
675 }
Theodore Ts'o3cb6c501997-08-11 20:29:22 +0000676 block_nr = fs->group_desc[(unsigned) group].bg_inode_table + block;
Theodore Ts'o73311962005-01-25 23:42:56 -0500677
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000678 offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000679
680 length = EXT2_INODE_SIZE(fs->super);
Theodore Ts'o73311962005-01-25 23:42:56 -0500681 if (length > bufsize)
682 length = bufsize;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000683
Theodore Ts'o73311962005-01-25 23:42:56 -0500684 ptr = (char *) w_inode;
685
686 while (length) {
687 clen = length;
688 if ((offset + length) > fs->blocksize)
689 clen = fs->blocksize - offset;
690
691 if (fs->icache->buffer_blk != block_nr) {
692 retval = io_channel_read_blk(fs->io, block_nr, 1,
693 fs->icache->buffer);
694 if (retval)
695 goto errout;
696 fs->icache->buffer_blk = block_nr;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000697 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000698
Theodore Ts'o73311962005-01-25 23:42:56 -0500699
700 memcpy((char *) fs->icache->buffer + (unsigned) offset,
701 ptr, clen);
702
703 retval = io_channel_write_blk(fs->io, block_nr, 1,
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000704 fs->icache->buffer);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000705 if (retval)
Theodore Ts'o73311962005-01-25 23:42:56 -0500706 goto errout;
707
708 offset = 0;
709 ptr += clen;
710 length -= clen;
711 block_nr++;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000712 }
Theodore Ts'o73311962005-01-25 23:42:56 -0500713
Theodore Ts'o3839e651997-04-26 13:21:57 +0000714 fs->flags |= EXT2_FLAG_CHANGED;
Theodore Ts'o73311962005-01-25 23:42:56 -0500715errout:
716 if (w_inode && w_inode != &temp_inode)
717 free(w_inode);
718 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000719}
720
Theodore Ts'o73311962005-01-25 23:42:56 -0500721errcode_t ext2fs_write_inode(ext2_filsys fs, ext2_ino_t ino,
722 struct ext2_inode *inode)
723{
724 return ext2fs_write_inode_full(fs, ino, inode,
725 sizeof(struct ext2_inode));
726}
Theodore Ts'o030970e2005-03-20 20:05:22 -0500727
728/*
729 * This function should be called when writing a new inode. It makes
Theodore Ts'oe27b4562005-03-21 01:02:53 -0500730 * sure that extra part of large inodes is initialized properly.
Theodore Ts'o030970e2005-03-20 20:05:22 -0500731 */
732errcode_t ext2fs_write_new_inode(ext2_filsys fs, ext2_ino_t ino,
733 struct ext2_inode *inode)
734{
735 struct ext2_inode *buf;
Theodore Ts'o030970e2005-03-20 20:05:22 -0500736 int size = EXT2_INODE_SIZE(fs->super);
Theodore Ts'oe27b4562005-03-21 01:02:53 -0500737 struct ext2_inode_large *large_inode;
Theodore Ts'o030970e2005-03-20 20:05:22 -0500738
739 if (size == sizeof(struct ext2_inode))
740 return ext2fs_write_inode_full(fs, ino, inode,
741 sizeof(struct ext2_inode));
742
743 buf = malloc(size);
744 if (!buf)
745 return ENOMEM;
746
747 memset(buf, 0, size);
748 *buf = *inode;
749
Theodore Ts'oe27b4562005-03-21 01:02:53 -0500750 large_inode = (struct ext2_inode_large *) buf;
751 large_inode->i_extra_isize = sizeof(struct ext2_inode_large) -
752 EXT2_GOOD_OLD_INODE_SIZE;
753
Theodore Ts'ob1ae1192005-04-09 01:21:21 -0400754 return ext2fs_write_inode_full(fs, ino, buf, size);
Theodore Ts'o030970e2005-03-20 20:05:22 -0500755}
756
Theodore Ts'o73311962005-01-25 23:42:56 -0500757
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000758errcode_t ext2fs_get_blocks(ext2_filsys fs, ext2_ino_t ino, blk_t *blocks)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000759{
760 struct ext2_inode inode;
761 int i;
762 errcode_t retval;
763
Theodore Ts'of3db3561997-04-26 13:34:30 +0000764 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
765
Theodore Ts'o3839e651997-04-26 13:21:57 +0000766 if (ino > fs->super->s_inodes_count)
767 return EXT2_ET_BAD_INODE_NUM;
768
769 if (fs->get_blocks) {
770 if (!(*fs->get_blocks)(fs, ino, blocks))
771 return 0;
772 }
773 retval = ext2fs_read_inode(fs, ino, &inode);
774 if (retval)
775 return retval;
776 for (i=0; i < EXT2_N_BLOCKS; i++)
777 blocks[i] = inode.i_block[i];
778 return 0;
779}
780
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000781errcode_t ext2fs_check_directory(ext2_filsys fs, ext2_ino_t ino)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000782{
783 struct ext2_inode inode;
784 errcode_t retval;
785
Theodore Ts'of3db3561997-04-26 13:34:30 +0000786 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
787
Theodore Ts'o3839e651997-04-26 13:21:57 +0000788 if (ino > fs->super->s_inodes_count)
789 return EXT2_ET_BAD_INODE_NUM;
790
Theodore Ts'od163b091997-10-03 17:42:28 +0000791 if (fs->check_directory) {
792 retval = (fs->check_directory)(fs, ino);
793 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
794 return retval;
795 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000796 retval = ext2fs_read_inode(fs, ino, &inode);
797 if (retval)
798 return retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000799 if (!LINUX_S_ISDIR(inode.i_mode))
Theodore Ts'o1f0b6c11997-10-31 06:07:47 +0000800 return EXT2_ET_NO_DIRECTORY;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000801 return 0;
802}
803