blob: 89084237402934b0f6f6d06b473e9ecd5a20d97a [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'oee010792007-11-09 19:01:06 -050093 retval = ext2fs_get_array(fs->icache->cache_size,
94 sizeof(struct ext2_inode_cache_ent),
95 &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'od11736c2008-04-22 23:22:17 -0400149 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
150 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
151 scan->inodes_left -=
152 fs->group_desc[scan->current_group].bg_itable_unused;
153 scan->blocks_left =
154 (scan->inodes_left +
155 (fs->blocksize / scan->inode_size - 1)) *
156 scan->inode_size / fs->blocksize;
157 }
Theodore Ts'oee010792007-11-09 19:01:06 -0500158 retval = ext2fs_get_array(scan->inode_buffer_blocks,
159 fs->blocksize,
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400160 &scan->inode_buffer);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000161 scan->done_group = 0;
162 scan->done_group_data = 0;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000163 scan->bad_block_ptr = 0;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000164 if (retval) {
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400165 ext2fs_free_mem(&scan);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000166 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000167 }
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400168 retval = ext2fs_get_mem(scan->inode_size, &scan->temp_buffer);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000169 if (retval) {
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400170 ext2fs_free_mem(&scan->inode_buffer);
171 ext2fs_free_mem(&scan);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000172 return retval;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000173 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000174 if (scan->fs->badblocks && scan->fs->badblocks->num)
175 scan->scan_flags |= EXT2_SF_CHK_BADBLOCKS;
Jose R. Santosd4f34d42007-10-21 21:03:25 -0500176 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
177 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
178 scan->scan_flags |= EXT2_SF_DO_LAZY;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000179 *ret_scan = scan;
180 return 0;
181}
182
183void ext2fs_close_inode_scan(ext2_inode_scan scan)
184{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000185 if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
186 return;
187
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400188 ext2fs_free_mem(&scan->inode_buffer);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000189 scan->inode_buffer = NULL;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400190 ext2fs_free_mem(&scan->temp_buffer);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000191 scan->temp_buffer = NULL;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400192 ext2fs_free_mem(&scan);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000193 return;
194}
195
Theodore Ts'of3db3561997-04-26 13:34:30 +0000196void 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,
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +0000200 void * priv_data),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000201 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
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000210int 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 */
228static errcode_t get_next_blockgroup(ext2_inode_scan scan)
229{
Jose R. Santosd4f34d42007-10-21 21:03:25 -0500230 ext2_filsys fs = scan->fs;
231
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000232 scan->current_group++;
233 scan->groups_left--;
Jose R. Santosd4f34d42007-10-21 21:03:25 -0500234
235 scan->current_block =fs->group_desc[scan->current_group].bg_inode_table;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000236
Theodore Ts'o818180c1998-06-27 05:11:14 +0000237 scan->current_inode = scan->current_group *
Jose R. Santosd4f34d42007-10-21 21:03:25 -0500238 EXT2_INODES_PER_GROUP(fs->super);
Theodore Ts'o818180c1998-06-27 05:11:14 +0000239
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000240 scan->bytes_left = 0;
Jose R. Santosd4f34d42007-10-21 21:03:25 -0500241 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 =
Andreas Dilger6f19f442008-03-31 10:57:38 -0400248 (scan->inodes_left +
249 (fs->blocksize / scan->inode_size - 1)) *
Jose R. Santosd4f34d42007-10-21 21:03:25 -0500250 scan->inode_size / fs->blocksize;
251 }
252
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000253 return 0;
254}
255
256errcode_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 */
271static errcode_t check_for_inode_bad_blocks(ext2_inode_scan scan,
Theodore Ts'o3cb6c501997-08-11 20:29:22 +0000272 blk_t *num_blocks)
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000273{
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])
Theodore Ts'o3cb6c501997-08-11 20:29:22 +0000320 *num_blocks = (int) (bb->list[scan->bad_block_ptr] - blk);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000321
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 */
329static errcode_t get_next_blocks(ext2_inode_scan scan)
330{
Theodore Ts'o3cb6c501997-08-11 20:29:22 +0000331 blk_t num_blocks;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000332 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,
Theodore Ts'o3cb6c501997-08-11 20:29:22 +0000365 (size_t) num_blocks * scan->fs->blocksize);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000366 } else {
367 retval = io_channel_read_blk(scan->fs->io,
368 scan->current_block,
Theodore Ts'o3cb6c501997-08-11 20:29:22 +0000369 (int) num_blocks,
370 scan->inode_buffer);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000371 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
Theodore Ts'o818180c1998-06-27 05:11:14 +0000383#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 */
390static 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
Theodore Ts'o73311962005-01-25 23:42:56 -0500404errcode_t ext2fs_get_next_inode_full(ext2_inode_scan scan, ext2_ino_t *ino,
405 struct ext2_inode *inode, int bufsize)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000406{
407 errcode_t retval;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000408 int extra_bytes = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000409
Theodore Ts'of3db3561997-04-26 13:34:30 +0000410 EXT2_CHECK_MAGIC(scan, EXT2_ET_MAGIC_INODE_SCAN);
411
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000412 /*
413 * Do we need to start reading a new block group?
414 */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000415 if (scan->inodes_left <= 0) {
Theodore Ts'o818180c1998-06-27 05:11:14 +0000416 force_new_group:
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000417 if (scan->done_group) {
418 retval = (scan->done_group)
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000419 (scan->fs, scan, scan->current_group,
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000420 scan->done_group_data);
421 if (retval)
422 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000423 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000424 if (scan->groups_left <= 0) {
425 *ino = 0;
426 return 0;
427 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000428 retval = get_next_blockgroup(scan);
429 if (retval)
430 return retval;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000431 }
Theodore Ts'o218a4861998-02-21 01:41:39 +0000432 /*
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400433 * These checks are done outside the above if statement so
434 * they can be done for block group #0.
Theodore Ts'o218a4861998-02-21 01:41:39 +0000435 */
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400436 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;
Jose R. Santosd4f34d42007-10-21 21:03:25 -0500440 if (scan->inodes_left == 0)
441 goto force_new_group;
Theodore Ts'o218a4861998-02-21 01:41:39 +0000442 if (scan->current_block == 0) {
443 if (scan->scan_flags & EXT2_SF_SKIP_MISSING_ITABLE) {
Theodore Ts'o818180c1998-06-27 05:11:14 +0000444 goto force_new_group;
Theodore Ts'o218a4861998-02-21 01:41:39 +0000445 } else
446 return EXT2_ET_MISSING_INODE_TABLE;
447 }
448
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000449
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;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000457
458 retval = get_next_blocks(scan);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000459 if (retval)
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000460 return retval;
Theodore Ts'o818180c1998-06-27 05:11:14 +0000461#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
Theodore Ts'o3839e651997-04-26 13:21:57 +0000469 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000470
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000471 retval = 0;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000472 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
Theodore Ts'o126a2912007-08-11 01:56:48 -0400478#ifdef WORDS_BIGENDIAN
Kalpak Shah1ed49d22007-06-29 21:40:19 -0400479 memset(inode, 0, bufsize);
Theodore Ts'o126a2912007-08-11 01:56:48 -0400480 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);
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000486#endif
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000487 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;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000490 } else {
Theodore Ts'o126a2912007-08-11 01:56:48 -0400491#ifdef WORDS_BIGENDIAN
Kalpak Shah1ed49d22007-06-29 21:40:19 -0400492 memset(inode, 0, bufsize);
Theodore Ts'o126a2912007-08-11 01:56:48 -0400493 ext2fs_swap_inode_full(scan->fs,
Theodore Ts'o73311962005-01-25 23:42:56 -0500494 (struct ext2_inode_large *) inode,
495 (struct ext2_inode_large *) scan->ptr,
496 0, bufsize);
Theodore Ts'o126a2912007-08-11 01:56:48 -0400497#else
498 memcpy(inode, scan->ptr, bufsize);
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000499#endif
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000500 scan->ptr += scan->inode_size;
501 scan->bytes_left -= scan->inode_size;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000502 if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK)
503 retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000504 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000505
Theodore Ts'o3839e651997-04-26 13:21:57 +0000506 scan->inodes_left--;
507 scan->current_inode++;
508 *ino = scan->current_inode;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000509 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000510}
511
Theodore Ts'o73311962005-01-25 23:42:56 -0500512errcode_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
Theodore Ts'o3839e651997-04-26 13:21:57 +0000519/*
520 * Functions to read and write a single inode.
521 */
Theodore Ts'o73311962005-01-25 23:42:56 -0500522errcode_t ext2fs_read_inode_full(ext2_filsys fs, ext2_ino_t ino,
523 struct ext2_inode * inode, int bufsize)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000524{
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000525 unsigned long group, block, block_nr, offset;
526 char *ptr;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000527 errcode_t retval;
Theodore Ts'o73311962005-01-25 23:42:56 -0500528 int clen, i, inodes_per_block, length;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400529 io_channel io;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000530
Theodore Ts'of3db3561997-04-26 13:34:30 +0000531 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
532
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000533 /* 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 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000539 /* Create inode cache if not present */
540 if (!fs->icache) {
541 retval = create_icache(fs);
542 if (retval)
543 return retval;
544 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000545 /* Check to see if it's in the inode cache */
Theodore Ts'o73311962005-01-25 23:42:56 -0500546 if (bufsize == sizeof(struct ext2_inode)) {
547 /* only old good inode can be retrieve from the cache */
548 for (i=0; i < fs->icache->cache_size; i++) {
549 if (fs->icache->cache[i].ino == ino) {
550 *inode = fs->icache->cache[i].inode;
551 return 0;
552 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000553 }
554 }
Theodore Ts'o665f7101999-01-08 13:33:39 +0000555 if ((ino == 0) || (ino > fs->super->s_inodes_count))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000556 return EXT2_ET_BAD_INODE_NUM;
Theodore Ts'oa78926e2001-05-03 04:02:29 +0000557 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);
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400563 io = fs->image_io;
Theodore Ts'oa78926e2001-05-03 04:02:29 +0000564 } else {
565 group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
566 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
567 EXT2_INODE_SIZE(fs->super);
568 block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
569 if (!fs->group_desc[(unsigned)group].bg_inode_table)
570 return EXT2_ET_MISSING_INODE_TABLE;
571 block_nr = fs->group_desc[(unsigned)group].bg_inode_table +
572 block;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400573 io = fs->io;
Theodore Ts'oa78926e2001-05-03 04:02:29 +0000574 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000575 offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000576
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000577 length = EXT2_INODE_SIZE(fs->super);
Theodore Ts'o73311962005-01-25 23:42:56 -0500578 if (bufsize < length)
579 length = bufsize;
580
581 ptr = (char *) inode;
582 while (length) {
583 clen = length;
584 if ((offset + length) > fs->blocksize)
585 clen = fs->blocksize - offset;
586
587 if (block_nr != fs->icache->buffer_blk) {
588 retval = io_channel_read_blk(io, block_nr, 1,
589 fs->icache->buffer);
590 if (retval)
591 return retval;
592 fs->icache->buffer_blk = block_nr;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000593 }
Theodore Ts'o73311962005-01-25 23:42:56 -0500594
595 memcpy(ptr, ((char *) fs->icache->buffer) + (unsigned) offset,
596 clen);
597
598 offset = 0;
599 length -= clen;
600 ptr += clen;
601 block_nr++;
602 }
603
Theodore Ts'o126a2912007-08-11 01:56:48 -0400604#ifdef WORDS_BIGENDIAN
605 ext2fs_swap_inode_full(fs, (struct ext2_inode_large *) inode,
606 (struct ext2_inode_large *) inode,
607 0, bufsize);
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000608#endif
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000609
610 /* Update the inode cache */
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000611 fs->icache->cache_last = (fs->icache->cache_last + 1) %
612 fs->icache->cache_size;
613 fs->icache->cache[fs->icache->cache_last].ino = ino;
614 fs->icache->cache[fs->icache->cache_last].inode = *inode;
615
Theodore Ts'o3839e651997-04-26 13:21:57 +0000616 return 0;
617}
618
Theodore Ts'o73311962005-01-25 23:42:56 -0500619errcode_t ext2fs_read_inode(ext2_filsys fs, ext2_ino_t ino,
620 struct ext2_inode * inode)
621{
622 return ext2fs_read_inode_full(fs, ino, inode,
623 sizeof(struct ext2_inode));
624}
625
626errcode_t ext2fs_write_inode_full(ext2_filsys fs, ext2_ino_t ino,
627 struct ext2_inode * inode, int bufsize)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000628{
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000629 unsigned long group, block, block_nr, offset;
Theodore Ts'o73311962005-01-25 23:42:56 -0500630 errcode_t retval = 0;
631 struct ext2_inode_large temp_inode, *w_inode;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000632 char *ptr;
Theodore Ts'o73311962005-01-25 23:42:56 -0500633 int clen, i, length;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000634
Theodore Ts'of3db3561997-04-26 13:34:30 +0000635 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
636
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000637 /* Check to see if user provided an override function */
638 if (fs->write_inode) {
639 retval = (fs->write_inode)(fs, ino, inode);
640 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
641 return retval;
642 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000643
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000644 /* Check to see if the inode cache needs to be updated */
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000645 if (fs->icache) {
646 for (i=0; i < fs->icache->cache_size; i++) {
647 if (fs->icache->cache[i].ino == ino) {
648 fs->icache->cache[i].inode = *inode;
649 break;
650 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000651 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000652 } else {
653 retval = create_icache(fs);
654 if (retval)
655 return retval;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000656 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000657
Theodore Ts'o3839e651997-04-26 13:21:57 +0000658 if (!(fs->flags & EXT2_FLAG_RW))
659 return EXT2_ET_RO_FILSYS;
660
Theodore Ts'o665f7101999-01-08 13:33:39 +0000661 if ((ino == 0) || (ino > fs->super->s_inodes_count))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000662 return EXT2_ET_BAD_INODE_NUM;
663
Theodore Ts'o73311962005-01-25 23:42:56 -0500664 length = bufsize;
665 if (length < EXT2_INODE_SIZE(fs->super))
666 length = EXT2_INODE_SIZE(fs->super);
667
668 if (length > (int) sizeof(struct ext2_inode_large)) {
669 w_inode = malloc(length);
670 if (!w_inode)
671 return ENOMEM;
672 } else
673 w_inode = &temp_inode;
674 memset(w_inode, 0, length);
675
Theodore Ts'o126a2912007-08-11 01:56:48 -0400676#ifdef WORDS_BIGENDIAN
677 ext2fs_swap_inode_full(fs, w_inode,
678 (struct ext2_inode_large *) inode,
679 1, bufsize);
680#else
681 memcpy(w_inode, inode, bufsize);
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000682#endif
Theodore Ts'o126a2912007-08-11 01:56:48 -0400683
Theodore Ts'o3839e651997-04-26 13:21:57 +0000684 group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000685 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
686 EXT2_INODE_SIZE(fs->super);
687 block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
Brian Behlendorfe649be92007-03-21 17:38:47 -0400688 if (!fs->group_desc[(unsigned) group].bg_inode_table) {
689 retval = EXT2_ET_MISSING_INODE_TABLE;
690 goto errout;
691 }
Theodore Ts'o3cb6c501997-08-11 20:29:22 +0000692 block_nr = fs->group_desc[(unsigned) group].bg_inode_table + block;
Theodore Ts'o73311962005-01-25 23:42:56 -0500693
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000694 offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000695
696 length = EXT2_INODE_SIZE(fs->super);
Theodore Ts'o73311962005-01-25 23:42:56 -0500697 if (length > bufsize)
698 length = bufsize;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000699
Theodore Ts'o73311962005-01-25 23:42:56 -0500700 ptr = (char *) w_inode;
701
702 while (length) {
703 clen = length;
704 if ((offset + length) > fs->blocksize)
705 clen = fs->blocksize - offset;
706
707 if (fs->icache->buffer_blk != block_nr) {
708 retval = io_channel_read_blk(fs->io, block_nr, 1,
709 fs->icache->buffer);
710 if (retval)
711 goto errout;
712 fs->icache->buffer_blk = block_nr;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000713 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000714
Theodore Ts'o73311962005-01-25 23:42:56 -0500715
716 memcpy((char *) fs->icache->buffer + (unsigned) offset,
717 ptr, clen);
718
719 retval = io_channel_write_blk(fs->io, block_nr, 1,
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000720 fs->icache->buffer);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000721 if (retval)
Theodore Ts'o73311962005-01-25 23:42:56 -0500722 goto errout;
723
724 offset = 0;
725 ptr += clen;
726 length -= clen;
727 block_nr++;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000728 }
Theodore Ts'o73311962005-01-25 23:42:56 -0500729
Theodore Ts'o3839e651997-04-26 13:21:57 +0000730 fs->flags |= EXT2_FLAG_CHANGED;
Theodore Ts'o73311962005-01-25 23:42:56 -0500731errout:
732 if (w_inode && w_inode != &temp_inode)
733 free(w_inode);
734 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000735}
736
Theodore Ts'o73311962005-01-25 23:42:56 -0500737errcode_t ext2fs_write_inode(ext2_filsys fs, ext2_ino_t ino,
738 struct ext2_inode *inode)
739{
740 return ext2fs_write_inode_full(fs, ino, inode,
741 sizeof(struct ext2_inode));
742}
Theodore Ts'o030970e2005-03-20 20:05:22 -0500743
744/*
745 * This function should be called when writing a new inode. It makes
Theodore Ts'oe27b4562005-03-21 01:02:53 -0500746 * sure that extra part of large inodes is initialized properly.
Theodore Ts'o030970e2005-03-20 20:05:22 -0500747 */
748errcode_t ext2fs_write_new_inode(ext2_filsys fs, ext2_ino_t ino,
749 struct ext2_inode *inode)
750{
751 struct ext2_inode *buf;
Theodore Ts'o030970e2005-03-20 20:05:22 -0500752 int size = EXT2_INODE_SIZE(fs->super);
Theodore Ts'oe27b4562005-03-21 01:02:53 -0500753 struct ext2_inode_large *large_inode;
Jim Garlickcc37e0d2007-04-06 08:50:15 -0400754 errcode_t retval;
Theodore Ts'o030970e2005-03-20 20:05:22 -0500755
756 if (size == sizeof(struct ext2_inode))
757 return ext2fs_write_inode_full(fs, ino, inode,
758 sizeof(struct ext2_inode));
759
760 buf = malloc(size);
761 if (!buf)
762 return ENOMEM;
763
764 memset(buf, 0, size);
765 *buf = *inode;
766
Theodore Ts'oe27b4562005-03-21 01:02:53 -0500767 large_inode = (struct ext2_inode_large *) buf;
768 large_inode->i_extra_isize = sizeof(struct ext2_inode_large) -
769 EXT2_GOOD_OLD_INODE_SIZE;
770
Jim Garlickcc37e0d2007-04-06 08:50:15 -0400771 retval = ext2fs_write_inode_full(fs, ino, buf, size);
772 free(buf);
773 return retval;
Theodore Ts'o030970e2005-03-20 20:05:22 -0500774}
775
Theodore Ts'o73311962005-01-25 23:42:56 -0500776
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000777errcode_t ext2fs_get_blocks(ext2_filsys fs, ext2_ino_t ino, blk_t *blocks)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000778{
779 struct ext2_inode inode;
780 int i;
781 errcode_t retval;
782
Theodore Ts'of3db3561997-04-26 13:34:30 +0000783 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
784
Theodore Ts'o3839e651997-04-26 13:21:57 +0000785 if (ino > fs->super->s_inodes_count)
786 return EXT2_ET_BAD_INODE_NUM;
787
788 if (fs->get_blocks) {
789 if (!(*fs->get_blocks)(fs, ino, blocks))
790 return 0;
791 }
792 retval = ext2fs_read_inode(fs, ino, &inode);
793 if (retval)
794 return retval;
795 for (i=0; i < EXT2_N_BLOCKS; i++)
796 blocks[i] = inode.i_block[i];
797 return 0;
798}
799
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000800errcode_t ext2fs_check_directory(ext2_filsys fs, ext2_ino_t ino)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000801{
802 struct ext2_inode inode;
803 errcode_t retval;
804
Theodore Ts'of3db3561997-04-26 13:34:30 +0000805 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
806
Theodore Ts'o3839e651997-04-26 13:21:57 +0000807 if (ino > fs->super->s_inodes_count)
808 return EXT2_ET_BAD_INODE_NUM;
809
Theodore Ts'od163b091997-10-03 17:42:28 +0000810 if (fs->check_directory) {
811 retval = (fs->check_directory)(fs, ino);
812 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
813 return retval;
814 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000815 retval = ext2fs_read_inode(fs, ino, &inode);
816 if (retval)
817 return retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000818 if (!LINUX_S_ISDIR(inode.i_mode))
Theodore Ts'o1f0b6c11997-10-31 06:07:47 +0000819 return EXT2_ET_NO_DIRECTORY;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000820 return 0;
821}
822