blob: d0b528cf214b94088490b9426bf17fd6eb612614 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * inode.c --- utility routines to read and write inodes
Theodore Ts'oefc6f622008-08-27 23:07:54 -04003 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00004 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
5 *
6 * %Begin-Header%
Theodore Ts'o543547a2010-05-17 21:31:56 -04007 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00009 * %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'o03fa6f82008-11-16 10:03:00 -050020#include <time.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000021#if HAVE_SYS_STAT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000022#include <sys/stat.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000023#endif
24#if HAVE_SYS_TYPES_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000025#include <sys/types.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000026#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000027
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000028#include "ext2_fs.h"
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000029#include "ext2fsP.h"
Theodore Ts'oa78926e2001-05-03 04:02:29 +000030#include "e2image.h"
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000031
32struct ext2_struct_inode_scan {
Theodore Ts'o3cb6c501997-08-11 20:29:22 +000033 errcode_t magic;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000034 ext2_filsys fs;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000035 ext2_ino_t current_inode;
JP Abgralle0ed7402014-03-19 19:08:39 -070036 blk64_t current_block;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000037 dgrp_t current_group;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000038 ext2_ino_t inodes_left;
Theodore Ts'o3cb6c501997-08-11 20:29:22 +000039 blk_t blocks_left;
40 dgrp_t groups_left;
41 blk_t inode_buffer_blocks;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000042 char * inode_buffer;
43 int inode_size;
44 char * ptr;
45 int bytes_left;
46 char *temp_buffer;
47 errcode_t (*done_group)(ext2_filsys fs,
48 ext2_inode_scan scan,
49 dgrp_t group,
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000050 void * priv_data);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000051 void * done_group_data;
52 int bad_block_ptr;
53 int scan_flags;
54 int reserved[6];
55};
Theodore Ts'o3839e651997-04-26 13:21:57 +000056
Theodore Ts'o6a7f4552000-11-12 19:07:06 +000057/*
58 * This routine flushes the icache, if it exists.
59 */
60errcode_t ext2fs_flush_icache(ext2_filsys fs)
61{
62 int i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040063
Theodore Ts'o6a7f4552000-11-12 19:07:06 +000064 if (!fs->icache)
65 return 0;
66
67 for (i=0; i < fs->icache->cache_size; i++)
68 fs->icache->cache[i].ino = 0;
69
Theodore Ts'o71669d02004-12-23 21:49:05 -050070 fs->icache->buffer_blk = 0;
Theodore Ts'o6a7f4552000-11-12 19:07:06 +000071 return 0;
72}
73
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000074static errcode_t create_icache(ext2_filsys fs)
75{
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000076 errcode_t retval;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040077
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000078 if (fs->icache)
79 return 0;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040080 retval = ext2fs_get_mem(sizeof(struct ext2_inode_cache), &fs->icache);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000081 if (retval)
82 return retval;
83
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000084 memset(fs->icache, 0, sizeof(struct ext2_inode_cache));
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040085 retval = ext2fs_get_mem(fs->blocksize, &fs->icache->buffer);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000086 if (retval) {
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040087 ext2fs_free_mem(&fs->icache);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000088 return retval;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000089 }
90 fs->icache->buffer_blk = 0;
91 fs->icache->cache_last = -1;
92 fs->icache->cache_size = 4;
93 fs->icache->refcount = 1;
Theodore Ts'oee010792007-11-09 19:01:06 -050094 retval = ext2fs_get_array(fs->icache->cache_size,
95 sizeof(struct ext2_inode_cache_ent),
96 &fs->icache->cache);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000097 if (retval) {
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040098 ext2fs_free_mem(&fs->icache->buffer);
99 ext2fs_free_mem(&fs->icache);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000100 return retval;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000101 }
Theodore Ts'o6a7f4552000-11-12 19:07:06 +0000102 ext2fs_flush_icache(fs);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000103 return 0;
104}
105
Theodore Ts'o3839e651997-04-26 13:21:57 +0000106errcode_t ext2fs_open_inode_scan(ext2_filsys fs, int buffer_blocks,
107 ext2_inode_scan *ret_scan)
108{
109 ext2_inode_scan scan;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000110 errcode_t retval;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000111 errcode_t (*save_get_blocks)(ext2_filsys f, ext2_ino_t ino, blk_t *blocks);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000112
Theodore Ts'of3db3561997-04-26 13:34:30 +0000113 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
114
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000115 /*
116 * If fs->badblocks isn't set, then set it --- since the inode
117 * scanning functions require it.
118 */
119 if (fs->badblocks == 0) {
Theodore Ts'o521e3681997-04-29 17:48:10 +0000120 /*
121 * Temporarly save fs->get_blocks and set it to zero,
122 * for compatibility with old e2fsck's.
123 */
124 save_get_blocks = fs->get_blocks;
125 fs->get_blocks = 0;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000126 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
127 if (retval && fs->badblocks) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000128 ext2fs_badblocks_list_free(fs->badblocks);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000129 fs->badblocks = 0;
130 }
Theodore Ts'o521e3681997-04-29 17:48:10 +0000131 fs->get_blocks = save_get_blocks;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000132 }
133
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400134 retval = ext2fs_get_mem(sizeof(struct ext2_struct_inode_scan), &scan);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000135 if (retval)
136 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000137 memset(scan, 0, sizeof(struct ext2_struct_inode_scan));
138
Theodore Ts'of3db3561997-04-26 13:34:30 +0000139 scan->magic = EXT2_ET_MAGIC_INODE_SCAN;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000140 scan->fs = fs;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000141 scan->inode_size = EXT2_INODE_SIZE(fs->super);
142 scan->bytes_left = 0;
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +0000143 scan->current_group = 0;
144 scan->groups_left = fs->group_desc_count - 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000145 scan->inode_buffer_blocks = buffer_blocks ? buffer_blocks : 8;
JP Abgralle0ed7402014-03-19 19:08:39 -0700146 scan->current_block = ext2fs_inode_table_loc(scan->fs,
147 scan->current_group);
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +0000148 scan->inodes_left = EXT2_INODES_PER_GROUP(scan->fs->super);
149 scan->blocks_left = scan->fs->inode_blocks_per_group;
Theodore Ts'od11736c2008-04-22 23:22:17 -0400150 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
151 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
152 scan->inodes_left -=
JP Abgralle0ed7402014-03-19 19:08:39 -0700153 ext2fs_bg_itable_unused(fs, scan->current_group);
Theodore Ts'od11736c2008-04-22 23:22:17 -0400154 scan->blocks_left =
155 (scan->inodes_left +
156 (fs->blocksize / scan->inode_size - 1)) *
157 scan->inode_size / fs->blocksize;
158 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700159 retval = io_channel_alloc_buf(fs->io, scan->inode_buffer_blocks,
160 &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;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400187
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;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400205
Theodore Ts'of3db3561997-04-26 13:34:30 +0000206 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
JP Abgralle0ed7402014-03-19 19:08:39 -0700235 scan->current_block = ext2fs_inode_table_loc(scan->fs,
236 scan->current_group);
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 -=
JP Abgralle0ed7402014-03-19 19:08:39 -0700246 ext2fs_bg_itable_unused(fs, scan->current_group);
Jose R. Santosd4f34d42007-10-21 21:03:25 -0500247 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,
JP Abgralle0ed7402014-03-19 19:08:39 -0700272 blk64_t *num_blocks)
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000273{
JP Abgralle0ed7402014-03-19 19:08:39 -0700274 blk64_t blk = scan->current_block;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000275 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{
JP Abgralle0ed7402014-03-19 19:08:39 -0700331 blk64_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 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400361
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000362 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 {
JP Abgralle0ed7402014-03-19 19:08:39 -0700367 retval = io_channel_read_blk64(scan->fs->io,
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000368 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;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400393
Theodore Ts'o818180c1998-06-27 05:11:14 +0000394 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'oefc6f622008-08-27 23:07:54 -0400409
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'oefc6f622008-08-27 23:07:54 -0400433 * These checks are done outside the above if statement so
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400434 * 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) &&
JP Abgralle0ed7402014-03-19 19:08:39 -0700437 (ext2fs_bg_flags_test(scan->fs, scan->current_group, EXT2_BG_INODE_UNINIT)
438 ))
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400439 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 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400448
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'oefc6f622008-08-27 23:07:54 -0400480 ext2fs_swap_inode_full(scan->fs,
Theodore Ts'o126a2912007-08-11 01:56:48 -0400481 (struct ext2_inode_large *) inode,
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400482 (struct ext2_inode_large *) scan->temp_buffer,
Theodore Ts'o126a2912007-08-11 01:56:48 -0400483 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'oefc6f622008-08-27 23:07:54 -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{
JP Abgralle0ed7402014-03-19 19:08:39 -0700525 blk64_t block_nr;
526 unsigned long group, block, offset;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000527 char *ptr;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000528 errcode_t retval;
Theodore Ts'o73311962005-01-25 23:42:56 -0500529 int clen, i, inodes_per_block, length;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400530 io_channel io;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000531
Theodore Ts'of3db3561997-04-26 13:34:30 +0000532 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
533
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000534 /* Check to see if user has an override function */
JP Abgralle0ed7402014-03-19 19:08:39 -0700535 if (fs->read_inode &&
536 ((bufsize == sizeof(struct ext2_inode)) ||
537 (EXT2_INODE_SIZE(fs->super) == sizeof(struct ext2_inode)))) {
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000538 retval = (fs->read_inode)(fs, ino, inode);
539 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
540 return retval;
541 }
Manish Katiyar8eb3b8a2008-07-15 11:10:19 -0400542 if ((ino == 0) || (ino > fs->super->s_inodes_count))
543 return EXT2_ET_BAD_INODE_NUM;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000544 /* Create inode cache if not present */
545 if (!fs->icache) {
546 retval = create_icache(fs);
547 if (retval)
548 return retval;
549 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000550 /* Check to see if it's in the inode cache */
Theodore Ts'o73311962005-01-25 23:42:56 -0500551 if (bufsize == sizeof(struct ext2_inode)) {
Manish Katiyar8eb3b8a2008-07-15 11:10:19 -0400552 /* only old good inode can be retrieved from the cache */
Theodore Ts'o73311962005-01-25 23:42:56 -0500553 for (i=0; i < fs->icache->cache_size; i++) {
554 if (fs->icache->cache[i].ino == ino) {
555 *inode = fs->icache->cache[i].inode;
556 return 0;
557 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000558 }
559 }
Theodore Ts'oa78926e2001-05-03 04:02:29 +0000560 if (fs->flags & EXT2_FLAG_IMAGE_FILE) {
561 inodes_per_block = fs->blocksize / EXT2_INODE_SIZE(fs->super);
562 block_nr = fs->image_header->offset_inode / fs->blocksize;
563 block_nr += (ino - 1) / inodes_per_block;
564 offset = ((ino - 1) % inodes_per_block) *
565 EXT2_INODE_SIZE(fs->super);
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400566 io = fs->image_io;
Theodore Ts'oa78926e2001-05-03 04:02:29 +0000567 } else {
568 group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
Theodore Ts'oa7843582009-01-19 23:09:37 -0500569 if (group > fs->group_desc_count)
570 return EXT2_ET_BAD_INODE_NUM;
Theodore Ts'oa78926e2001-05-03 04:02:29 +0000571 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
572 EXT2_INODE_SIZE(fs->super);
573 block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
JP Abgralle0ed7402014-03-19 19:08:39 -0700574 if (!ext2fs_inode_table_loc(fs, (unsigned) group))
Theodore Ts'oa78926e2001-05-03 04:02:29 +0000575 return EXT2_ET_MISSING_INODE_TABLE;
JP Abgralle0ed7402014-03-19 19:08:39 -0700576 block_nr = ext2fs_inode_table_loc(fs, group) +
Theodore Ts'oa78926e2001-05-03 04:02:29 +0000577 block;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400578 io = fs->io;
Theodore Ts'oa78926e2001-05-03 04:02:29 +0000579 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000580 offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000581
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000582 length = EXT2_INODE_SIZE(fs->super);
Theodore Ts'o73311962005-01-25 23:42:56 -0500583 if (bufsize < length)
584 length = bufsize;
585
586 ptr = (char *) inode;
587 while (length) {
588 clen = length;
589 if ((offset + length) > fs->blocksize)
590 clen = fs->blocksize - offset;
591
592 if (block_nr != fs->icache->buffer_blk) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700593 retval = io_channel_read_blk64(io, block_nr, 1,
Theodore Ts'o73311962005-01-25 23:42:56 -0500594 fs->icache->buffer);
595 if (retval)
596 return retval;
597 fs->icache->buffer_blk = block_nr;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000598 }
Theodore Ts'o73311962005-01-25 23:42:56 -0500599
600 memcpy(ptr, ((char *) fs->icache->buffer) + (unsigned) offset,
601 clen);
602
603 offset = 0;
604 length -= clen;
605 ptr += clen;
606 block_nr++;
607 }
608
Theodore Ts'o126a2912007-08-11 01:56:48 -0400609#ifdef WORDS_BIGENDIAN
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400610 ext2fs_swap_inode_full(fs, (struct ext2_inode_large *) inode,
611 (struct ext2_inode_large *) inode,
Theodore Ts'o126a2912007-08-11 01:56:48 -0400612 0, bufsize);
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000613#endif
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000614
615 /* Update the inode cache */
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000616 fs->icache->cache_last = (fs->icache->cache_last + 1) %
617 fs->icache->cache_size;
618 fs->icache->cache[fs->icache->cache_last].ino = ino;
619 fs->icache->cache[fs->icache->cache_last].inode = *inode;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400620
Theodore Ts'o3839e651997-04-26 13:21:57 +0000621 return 0;
622}
623
Theodore Ts'o73311962005-01-25 23:42:56 -0500624errcode_t ext2fs_read_inode(ext2_filsys fs, ext2_ino_t ino,
625 struct ext2_inode * inode)
626{
627 return ext2fs_read_inode_full(fs, ino, inode,
628 sizeof(struct ext2_inode));
629}
630
631errcode_t ext2fs_write_inode_full(ext2_filsys fs, ext2_ino_t ino,
632 struct ext2_inode * inode, int bufsize)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000633{
JP Abgralle0ed7402014-03-19 19:08:39 -0700634 blk64_t block_nr;
635 unsigned long group, block, offset;
Theodore Ts'o73311962005-01-25 23:42:56 -0500636 errcode_t retval = 0;
637 struct ext2_inode_large temp_inode, *w_inode;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000638 char *ptr;
Theodore Ts'o73311962005-01-25 23:42:56 -0500639 int clen, i, length;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000640
Theodore Ts'of3db3561997-04-26 13:34:30 +0000641 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
642
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000643 /* Check to see if user provided an override function */
644 if (fs->write_inode) {
645 retval = (fs->write_inode)(fs, ino, inode);
646 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
647 return retval;
648 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000649
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000650 /* Check to see if the inode cache needs to be updated */
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000651 if (fs->icache) {
652 for (i=0; i < fs->icache->cache_size; i++) {
653 if (fs->icache->cache[i].ino == ino) {
654 fs->icache->cache[i].inode = *inode;
655 break;
656 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000657 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000658 } else {
659 retval = create_icache(fs);
660 if (retval)
661 return retval;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000662 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400663
Theodore Ts'o3839e651997-04-26 13:21:57 +0000664 if (!(fs->flags & EXT2_FLAG_RW))
665 return EXT2_ET_RO_FILSYS;
666
Theodore Ts'o665f7101999-01-08 13:33:39 +0000667 if ((ino == 0) || (ino > fs->super->s_inodes_count))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000668 return EXT2_ET_BAD_INODE_NUM;
669
Theodore Ts'o73311962005-01-25 23:42:56 -0500670 length = bufsize;
671 if (length < EXT2_INODE_SIZE(fs->super))
672 length = EXT2_INODE_SIZE(fs->super);
673
674 if (length > (int) sizeof(struct ext2_inode_large)) {
675 w_inode = malloc(length);
JP Abgralle0ed7402014-03-19 19:08:39 -0700676 if (!w_inode) {
677 retval = ENOMEM;
678 goto errout;
679 }
Theodore Ts'o73311962005-01-25 23:42:56 -0500680 } else
681 w_inode = &temp_inode;
682 memset(w_inode, 0, length);
683
Theodore Ts'o126a2912007-08-11 01:56:48 -0400684#ifdef WORDS_BIGENDIAN
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400685 ext2fs_swap_inode_full(fs, w_inode,
686 (struct ext2_inode_large *) inode,
Theodore Ts'o126a2912007-08-11 01:56:48 -0400687 1, bufsize);
688#else
689 memcpy(w_inode, inode, bufsize);
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000690#endif
Theodore Ts'o126a2912007-08-11 01:56:48 -0400691
Theodore Ts'o3839e651997-04-26 13:21:57 +0000692 group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000693 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
694 EXT2_INODE_SIZE(fs->super);
695 block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
JP Abgralle0ed7402014-03-19 19:08:39 -0700696 if (!ext2fs_inode_table_loc(fs, (unsigned) group)) {
Brian Behlendorfe649be92007-03-21 17:38:47 -0400697 retval = EXT2_ET_MISSING_INODE_TABLE;
698 goto errout;
699 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700700 block_nr = ext2fs_inode_table_loc(fs, (unsigned) group) + block;
Theodore Ts'o73311962005-01-25 23:42:56 -0500701
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000702 offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000703
704 length = EXT2_INODE_SIZE(fs->super);
Theodore Ts'o73311962005-01-25 23:42:56 -0500705 if (length > bufsize)
706 length = bufsize;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000707
Theodore Ts'o73311962005-01-25 23:42:56 -0500708 ptr = (char *) w_inode;
709
710 while (length) {
711 clen = length;
712 if ((offset + length) > fs->blocksize)
713 clen = fs->blocksize - offset;
714
715 if (fs->icache->buffer_blk != block_nr) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700716 retval = io_channel_read_blk64(fs->io, block_nr, 1,
Theodore Ts'o73311962005-01-25 23:42:56 -0500717 fs->icache->buffer);
718 if (retval)
719 goto errout;
720 fs->icache->buffer_blk = block_nr;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000721 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000722
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400723
724 memcpy((char *) fs->icache->buffer + (unsigned) offset,
Theodore Ts'o73311962005-01-25 23:42:56 -0500725 ptr, clen);
726
JP Abgralle0ed7402014-03-19 19:08:39 -0700727 retval = io_channel_write_blk64(fs->io, block_nr, 1,
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000728 fs->icache->buffer);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000729 if (retval)
Theodore Ts'o73311962005-01-25 23:42:56 -0500730 goto errout;
731
732 offset = 0;
733 ptr += clen;
734 length -= clen;
735 block_nr++;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000736 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400737
Theodore Ts'o3839e651997-04-26 13:21:57 +0000738 fs->flags |= EXT2_FLAG_CHANGED;
Theodore Ts'o73311962005-01-25 23:42:56 -0500739errout:
740 if (w_inode && w_inode != &temp_inode)
741 free(w_inode);
742 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000743}
744
Theodore Ts'o73311962005-01-25 23:42:56 -0500745errcode_t ext2fs_write_inode(ext2_filsys fs, ext2_ino_t ino,
746 struct ext2_inode *inode)
747{
748 return ext2fs_write_inode_full(fs, ino, inode,
749 sizeof(struct ext2_inode));
750}
Theodore Ts'o030970e2005-03-20 20:05:22 -0500751
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400752/*
Theodore Ts'o030970e2005-03-20 20:05:22 -0500753 * This function should be called when writing a new inode. It makes
Theodore Ts'oe27b4562005-03-21 01:02:53 -0500754 * sure that extra part of large inodes is initialized properly.
Theodore Ts'o030970e2005-03-20 20:05:22 -0500755 */
756errcode_t ext2fs_write_new_inode(ext2_filsys fs, ext2_ino_t ino,
757 struct ext2_inode *inode)
758{
759 struct ext2_inode *buf;
Theodore Ts'o030970e2005-03-20 20:05:22 -0500760 int size = EXT2_INODE_SIZE(fs->super);
Theodore Ts'oe27b4562005-03-21 01:02:53 -0500761 struct ext2_inode_large *large_inode;
Jim Garlickcc37e0d2007-04-06 08:50:15 -0400762 errcode_t retval;
Theodore Ts'o3bcc6272008-08-14 14:30:05 -0400763 __u32 t = fs->now ? fs->now : time(NULL);
764
765 if (!inode->i_ctime)
766 inode->i_ctime = t;
767 if (!inode->i_mtime)
768 inode->i_mtime = t;
769 if (!inode->i_atime)
770 inode->i_atime = t;
Theodore Ts'o030970e2005-03-20 20:05:22 -0500771
772 if (size == sizeof(struct ext2_inode))
773 return ext2fs_write_inode_full(fs, ino, inode,
774 sizeof(struct ext2_inode));
775
776 buf = malloc(size);
777 if (!buf)
778 return ENOMEM;
779
780 memset(buf, 0, size);
781 *buf = *inode;
782
Theodore Ts'oe27b4562005-03-21 01:02:53 -0500783 large_inode = (struct ext2_inode_large *) buf;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400784 large_inode->i_extra_isize = sizeof(struct ext2_inode_large) -
Theodore Ts'oe27b4562005-03-21 01:02:53 -0500785 EXT2_GOOD_OLD_INODE_SIZE;
Theodore Ts'o3bcc6272008-08-14 14:30:05 -0400786 if (!large_inode->i_crtime)
787 large_inode->i_crtime = t;
Theodore Ts'oe27b4562005-03-21 01:02:53 -0500788
Jim Garlickcc37e0d2007-04-06 08:50:15 -0400789 retval = ext2fs_write_inode_full(fs, ino, buf, size);
790 free(buf);
791 return retval;
Theodore Ts'o030970e2005-03-20 20:05:22 -0500792}
793
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400794
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000795errcode_t ext2fs_get_blocks(ext2_filsys fs, ext2_ino_t ino, blk_t *blocks)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000796{
797 struct ext2_inode inode;
798 int i;
799 errcode_t retval;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400800
Theodore Ts'of3db3561997-04-26 13:34:30 +0000801 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
802
Theodore Ts'o3839e651997-04-26 13:21:57 +0000803 if (ino > fs->super->s_inodes_count)
804 return EXT2_ET_BAD_INODE_NUM;
805
806 if (fs->get_blocks) {
807 if (!(*fs->get_blocks)(fs, ino, blocks))
808 return 0;
809 }
810 retval = ext2fs_read_inode(fs, ino, &inode);
811 if (retval)
812 return retval;
813 for (i=0; i < EXT2_N_BLOCKS; i++)
814 blocks[i] = inode.i_block[i];
815 return 0;
816}
817
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000818errcode_t ext2fs_check_directory(ext2_filsys fs, ext2_ino_t ino)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000819{
820 struct ext2_inode inode;
821 errcode_t retval;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400822
Theodore Ts'of3db3561997-04-26 13:34:30 +0000823 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
824
Theodore Ts'o3839e651997-04-26 13:21:57 +0000825 if (ino > fs->super->s_inodes_count)
826 return EXT2_ET_BAD_INODE_NUM;
827
Theodore Ts'od163b091997-10-03 17:42:28 +0000828 if (fs->check_directory) {
829 retval = (fs->check_directory)(fs, ino);
830 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
831 return retval;
832 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000833 retval = ext2fs_read_inode(fs, ino, &inode);
834 if (retval)
835 return retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000836 if (!LINUX_S_ISDIR(inode.i_mode))
Theodore Ts'o1f0b6c11997-10-31 06:07:47 +0000837 return EXT2_ET_NO_DIRECTORY;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000838 return 0;
839}
840