blob: 7194fb0c347a897ec4c196f5d0d016ae3ca1a8ab [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>
14#include <unistd.h>
15#include <stdlib.h>
16#include <sys/stat.h>
17#include <sys/types.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000018#if HAVE_ERRNO_H
19#include <errno.h>
20#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000021
Theodore Ts'o3839e651997-04-26 13:21:57 +000022#include <linux/ext2_fs.h>
23
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000024#include "ext2fsP.h"
25
26struct ext2_struct_inode_scan {
27 int magic;
28 ext2_filsys fs;
29 ino_t current_inode;
30 blk_t current_block;
31 dgrp_t current_group;
32 int inodes_left, blocks_left, groups_left;
33 int inode_buffer_blocks;
34 char * inode_buffer;
35 int inode_size;
36 char * ptr;
37 int bytes_left;
38 char *temp_buffer;
39 errcode_t (*done_group)(ext2_filsys fs,
40 ext2_inode_scan scan,
41 dgrp_t group,
42 void * private);
43 void * done_group_data;
44 int bad_block_ptr;
45 int scan_flags;
46 int reserved[6];
47};
Theodore Ts'o3839e651997-04-26 13:21:57 +000048
49errcode_t ext2fs_open_inode_scan(ext2_filsys fs, int buffer_blocks,
50 ext2_inode_scan *ret_scan)
51{
52 ext2_inode_scan scan;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000053 errcode_t retval;
Theodore Ts'o521e3681997-04-29 17:48:10 +000054 errcode_t (*save_get_blocks)(ext2_filsys fs, ino_t ino, blk_t *blocks);
Theodore Ts'o3839e651997-04-26 13:21:57 +000055
Theodore Ts'of3db3561997-04-26 13:34:30 +000056 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
57
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000058 /*
59 * If fs->badblocks isn't set, then set it --- since the inode
60 * scanning functions require it.
61 */
62 if (fs->badblocks == 0) {
Theodore Ts'o521e3681997-04-29 17:48:10 +000063 /*
64 * Temporarly save fs->get_blocks and set it to zero,
65 * for compatibility with old e2fsck's.
66 */
67 save_get_blocks = fs->get_blocks;
68 fs->get_blocks = 0;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000069 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
70 if (retval && fs->badblocks) {
71 badblocks_list_free(fs->badblocks);
72 fs->badblocks = 0;
73 }
Theodore Ts'o521e3681997-04-29 17:48:10 +000074 fs->get_blocks = save_get_blocks;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000075 }
76
Theodore Ts'o3839e651997-04-26 13:21:57 +000077 scan = (ext2_inode_scan) malloc(sizeof(struct ext2_struct_inode_scan));
78 if (!scan)
79 return ENOMEM;
80 memset(scan, 0, sizeof(struct ext2_struct_inode_scan));
81
Theodore Ts'of3db3561997-04-26 13:34:30 +000082 scan->magic = EXT2_ET_MAGIC_INODE_SCAN;
Theodore Ts'o3839e651997-04-26 13:21:57 +000083 scan->fs = fs;
Theodore Ts'o7f88b041997-04-26 14:48:50 +000084 scan->inode_size = EXT2_INODE_SIZE(fs->super);
85 scan->bytes_left = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000086 scan->current_group = -1;
87 scan->inode_buffer_blocks = buffer_blocks ? buffer_blocks : 8;
88 scan->groups_left = fs->group_desc_count;
89 scan->inode_buffer = malloc(scan->inode_buffer_blocks * fs->blocksize);
Theodore Ts'of3db3561997-04-26 13:34:30 +000090 scan->done_group = 0;
91 scan->done_group_data = 0;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000092 scan->bad_block_ptr = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000093 if (!scan->inode_buffer) {
94 free(scan);
95 return ENOMEM;
96 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +000097 scan->temp_buffer = malloc(scan->inode_size);
98 if (!scan->temp_buffer) {
99 free(scan->inode_buffer);
100 free(scan);
101 return ENOMEM;
102 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000103 if (scan->fs->badblocks && scan->fs->badblocks->num)
104 scan->scan_flags |= EXT2_SF_CHK_BADBLOCKS;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000105 *ret_scan = scan;
106 return 0;
107}
108
109void ext2fs_close_inode_scan(ext2_inode_scan scan)
110{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000111 if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
112 return;
113
Theodore Ts'o3839e651997-04-26 13:21:57 +0000114 free(scan->inode_buffer);
115 scan->inode_buffer = NULL;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000116 free(scan->temp_buffer);
117 scan->temp_buffer = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000118 free(scan);
119 return;
120}
121
Theodore Ts'of3db3561997-04-26 13:34:30 +0000122void ext2fs_set_inode_callback(ext2_inode_scan scan,
123 errcode_t (*done_group)(ext2_filsys fs,
124 ext2_inode_scan scan,
125 dgrp_t group,
126 void * private),
127 void *done_group_data)
128{
129 if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
130 return;
131
132 scan->done_group = done_group;
133 scan->done_group_data = done_group_data;
134}
135
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000136int ext2fs_inode_scan_flags(ext2_inode_scan scan, int set_flags,
137 int clear_flags)
138{
139 int old_flags;
140
141 if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
142 return 0;
143
144 old_flags = scan->scan_flags;
145 scan->scan_flags &= ~clear_flags;
146 scan->scan_flags |= set_flags;
147 return old_flags;
148}
149
150/*
151 * This function is called by ext2fs_get_next_inode when it needs to
152 * get ready to read in a new blockgroup.
153 */
154static errcode_t get_next_blockgroup(ext2_inode_scan scan)
155{
156 scan->current_group++;
157 scan->groups_left--;
158
159 scan->current_block = scan->fs->
160 group_desc[scan->current_group].bg_inode_table;
161
162 scan->bytes_left = 0;
163 scan->inodes_left = EXT2_INODES_PER_GROUP(scan->fs->super);
164 scan->blocks_left = scan->fs->inode_blocks_per_group;
165 return 0;
166}
167
168errcode_t ext2fs_inode_scan_goto_blockgroup(ext2_inode_scan scan,
169 int group)
170{
171 scan->current_group = group - 1;
172 scan->groups_left = scan->fs->group_desc_count - group;
173 return get_next_blockgroup(scan);
174}
175
176/*
177 * This function is called by get_next_blocks() to check for bad
178 * blocks in the inode table.
179 *
180 * This function assumes that badblocks_list->list is sorted in
181 * increasing order.
182 */
183static errcode_t check_for_inode_bad_blocks(ext2_inode_scan scan,
184 int *num_blocks)
185{
186 blk_t blk = scan->current_block;
187 badblocks_list bb = scan->fs->badblocks;
188
189 /*
190 * If the inode table is missing, then obviously there are no
191 * bad blocks. :-)
192 */
193 if (blk == 0)
194 return 0;
195
196 /*
197 * If the current block is greater than the bad block listed
198 * in the bad block list, then advance the pointer until this
199 * is no longer the case. If we run out of bad blocks, then
200 * we don't need to do any more checking!
201 */
202 while (blk > bb->list[scan->bad_block_ptr]) {
203 if (++scan->bad_block_ptr >= bb->num) {
204 scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
205 return 0;
206 }
207 }
208
209 /*
210 * If the current block is equal to the bad block listed in
211 * the bad block list, then handle that one block specially.
212 * (We could try to handle runs of bad blocks, but that
213 * only increases CPU efficiency by a small amount, at the
214 * expense of a huge expense of code complexity, and for an
215 * uncommon case at that.)
216 */
217 if (blk == bb->list[scan->bad_block_ptr]) {
218 scan->scan_flags |= EXT2_SF_BAD_INODE_BLK;
219 *num_blocks = 1;
220 if (++scan->bad_block_ptr >= bb->num)
221 scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
222 return 0;
223 }
224
225 /*
226 * If there is a bad block in the range that we're about to
227 * read in, adjust the number of blocks to read so that we we
228 * don't read in the bad block. (Then the next block to read
229 * will be the bad block, which is handled in the above case.)
230 */
231 if ((blk + *num_blocks) > bb->list[scan->bad_block_ptr])
232 *num_blocks = bb->list[scan->bad_block_ptr] - blk;
233
234 return 0;
235}
236
237/*
238 * This function is called by ext2fs_get_next_inode when it needs to
239 * read in more blocks from the current blockgroup's inode table.
240 */
241static errcode_t get_next_blocks(ext2_inode_scan scan)
242{
243 int num_blocks;
244 errcode_t retval;
245
246 /*
247 * Figure out how many blocks to read; we read at most
248 * inode_buffer_blocks, and perhaps less if there aren't that
249 * many blocks left to read.
250 */
251 num_blocks = scan->inode_buffer_blocks;
252 if (num_blocks > scan->blocks_left)
253 num_blocks = scan->blocks_left;
254
255 /*
256 * If the past block "read" was a bad block, then mark the
257 * left-over extra bytes as also being bad.
258 */
259 if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK) {
260 if (scan->bytes_left)
261 scan->scan_flags |= EXT2_SF_BAD_EXTRA_BYTES;
262 scan->scan_flags &= ~EXT2_SF_BAD_INODE_BLK;
263 }
264
265 /*
266 * Do inode bad block processing, if necessary.
267 */
268 if (scan->scan_flags & EXT2_SF_CHK_BADBLOCKS) {
269 retval = check_for_inode_bad_blocks(scan, &num_blocks);
270 if (retval)
271 return retval;
272 }
273
274 if ((scan->scan_flags & EXT2_SF_BAD_INODE_BLK) ||
275 (scan->current_block == 0)) {
276 memset(scan->inode_buffer, 0,
277 num_blocks * scan->fs->blocksize);
278 } else {
279 retval = io_channel_read_blk(scan->fs->io,
280 scan->current_block,
281 num_blocks, scan->inode_buffer);
282 if (retval)
283 return EXT2_ET_NEXT_INODE_READ;
284 }
285 scan->ptr = scan->inode_buffer;
286 scan->bytes_left = num_blocks * scan->fs->blocksize;
287
288 scan->blocks_left -= num_blocks;
289 if (scan->current_block)
290 scan->current_block += num_blocks;
291 return 0;
292}
293
Theodore Ts'o3839e651997-04-26 13:21:57 +0000294errcode_t ext2fs_get_next_inode(ext2_inode_scan scan, ino_t *ino,
295 struct ext2_inode *inode)
296{
297 errcode_t retval;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000298 int extra_bytes = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000299
Theodore Ts'of3db3561997-04-26 13:34:30 +0000300 EXT2_CHECK_MAGIC(scan, EXT2_ET_MAGIC_INODE_SCAN);
301
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000302 /*
303 * Do we need to start reading a new block group?
304 */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000305 if (scan->inodes_left <= 0) {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000306 retry:
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000307 if (scan->done_group) {
308 retval = (scan->done_group)
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000309 (scan->fs, scan, scan->current_group,
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000310 scan->done_group_data);
311 if (retval)
312 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000313 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000314 if (scan->groups_left <= 0) {
315 *ino = 0;
316 return 0;
317 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000318 retval = get_next_blockgroup(scan);
319 if (retval)
320 return retval;
321 if (scan->current_block == 0) {
322 if (scan->scan_flags & EXT2_SF_SKIP_MISSING_ITABLE) {
323 goto retry;
324 } else
325 return EXT2_ET_MISSING_INODE_TABLE;
326 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000327 }
328
329 /*
330 * Have we run out of space in the inode buffer? If so, we
331 * need to read in more blocks.
332 */
333 if (scan->bytes_left < scan->inode_size) {
334 memcpy(scan->temp_buffer, scan->ptr, scan->bytes_left);
335 extra_bytes = scan->bytes_left;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000336
337 retval = get_next_blocks(scan);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000338 if (retval)
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000339 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000340 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000341
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000342 retval = 0;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000343 if (extra_bytes) {
344 memcpy(scan->temp_buffer+extra_bytes, scan->ptr,
345 scan->inode_size - extra_bytes);
346 scan->ptr += scan->inode_size - extra_bytes;
347 scan->bytes_left -= scan->inode_size - extra_bytes;
348
Theodore Ts'o5c576471997-04-29 15:29:49 +0000349 if ((scan->fs->flags & EXT2_FLAG_SWAP_BYTES) ||
350 (scan->fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000351 ext2fs_swap_inode(scan->fs, inode,
352 (struct ext2_inode *) scan->temp_buffer, 0);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000353 else
354 *inode = *((struct ext2_inode *) scan->temp_buffer);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000355 if (scan->scan_flags & EXT2_SF_BAD_EXTRA_BYTES)
356 retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
357 scan->scan_flags &= ~EXT2_SF_BAD_EXTRA_BYTES;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000358 } else {
Theodore Ts'o5c576471997-04-29 15:29:49 +0000359 if ((scan->fs->flags & EXT2_FLAG_SWAP_BYTES) ||
360 (scan->fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000361 ext2fs_swap_inode(scan->fs, inode,
362 (struct ext2_inode *) scan->ptr, 0);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000363 else
364 *inode = *((struct ext2_inode *) scan->ptr);
365 scan->ptr += scan->inode_size;
366 scan->bytes_left -= scan->inode_size;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000367 if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK)
368 retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000369 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000370
Theodore Ts'o3839e651997-04-26 13:21:57 +0000371 scan->inodes_left--;
372 scan->current_inode++;
373 *ino = scan->current_inode;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000374 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000375}
376
377/*
378 * Functions to read and write a single inode.
379 */
380static char *inode_buffer = 0;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000381static blk_t inode_buffer_block = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000382static int inode_buffer_size = 0;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000383#define INODE_CACHE_SIZE 4
384#ifdef INODE_CACHE_SIZE
385static int cache_last = -1;
386static struct {
387 ino_t inode;
388 struct ext2_inode value;
389} inode_cache[INODE_CACHE_SIZE];
390#endif
391
Theodore Ts'o3839e651997-04-26 13:21:57 +0000392
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000393errcode_t ext2fs_read_inode (ext2_filsys fs, ino_t ino,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000394 struct ext2_inode * inode)
395{
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000396 unsigned long group, block, block_nr, offset;
397 char *ptr;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000398 errcode_t retval;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000399 int clen, length, i;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000400
Theodore Ts'of3db3561997-04-26 13:34:30 +0000401 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
402
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000403 /* Check to see if user has an override function */
404 if (fs->read_inode) {
405 retval = (fs->read_inode)(fs, ino, inode);
406 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
407 return retval;
408 }
409 /* Check to see if it's in the inode cache */
410#ifdef INODE_CACHE_SIZE
411 if (cache_last == -1) {
412 for (i=0; i < INODE_CACHE_SIZE; i++)
413 inode_cache[i].inode = 0;
414 cache_last = INODE_CACHE_SIZE-1;
415 } else for (i=0; i < INODE_CACHE_SIZE; i++) {
416 if (inode_cache[i].inode == ino) {
417 *inode = inode_cache[i].value;
418 return 0;
419 }
420 }
421#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000422 if (ino > fs->super->s_inodes_count)
423 return EXT2_ET_BAD_INODE_NUM;
424 if (inode_buffer_size != fs->blocksize) {
425 if (inode_buffer)
426 free(inode_buffer);
427 inode_buffer_size = 0;
428 inode_buffer = malloc(fs->blocksize);
429 if (!inode_buffer)
430 return ENOMEM;
431 inode_buffer_size = fs->blocksize;
432 inode_buffer_block = 0;
433 }
434
435 group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000436 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
437 EXT2_INODE_SIZE(fs->super);
438 block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000439 if (!fs->group_desc[group].bg_inode_table)
440 return EXT2_ET_MISSING_INODE_TABLE;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000441 block_nr = fs->group_desc[group].bg_inode_table + block;
442 if (block_nr != inode_buffer_block) {
443 retval = io_channel_read_blk(fs->io, block_nr, 1,
444 inode_buffer);
445 if (retval)
446 return retval;
447 inode_buffer_block = block_nr;
448 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000449 offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
450 ptr = ((char *) inode_buffer) + offset;
451
452 memset(inode, 0, sizeof(struct ext2_inode));
453 length = EXT2_INODE_SIZE(fs->super);
454 if (length > sizeof(struct ext2_inode))
455 length = sizeof(struct ext2_inode);
456
457 if ((offset + length) > EXT2_BLOCK_SIZE(fs->super)) {
458 clen = EXT2_BLOCK_SIZE(fs->super) - offset;
459 memcpy((char *) inode, ptr, clen);
460 length -= clen;
461
462 retval = io_channel_read_blk(fs->io, block_nr+1, 1,
463 inode_buffer);
464 if (retval)
465 return retval;
466 inode_buffer_block = block_nr+1;
467
468 memcpy(((char *) inode) + clen,
469 inode_buffer, length);
470 } else
471 memcpy((char *) inode, ptr, length);
472
Theodore Ts'o5c576471997-04-29 15:29:49 +0000473 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
474 (fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000475 ext2fs_swap_inode(fs, inode, inode, 0);
476
477 /* Update the inode cache */
478#ifdef INODE_CACHE_SIZE
479 cache_last = (cache_last + 1) % INODE_CACHE_SIZE;
480 inode_cache[cache_last].inode = ino;
481 inode_cache[cache_last].value = *inode;
482#endif
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000483
Theodore Ts'o3839e651997-04-26 13:21:57 +0000484 return 0;
485}
486
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000487errcode_t ext2fs_write_inode(ext2_filsys fs, ino_t ino,
488 struct ext2_inode * inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000489{
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000490 unsigned long group, block, block_nr, offset;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000491 errcode_t retval;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000492 struct ext2_inode temp_inode;
493 char *ptr;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000494 int clen, length, i;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000495
Theodore Ts'of3db3561997-04-26 13:34:30 +0000496 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
497
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000498 /* Check to see if user provided an override function */
499 if (fs->write_inode) {
500 retval = (fs->write_inode)(fs, ino, inode);
501 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
502 return retval;
503 }
504 /* Check to see if the inode cache needs to be updated */
505#ifdef INODE_CACHE_SIZE
506 for (i=0; i < INODE_CACHE_SIZE; i++) {
507 if (inode_cache[i].inode == ino) {
508 inode_cache[i].value = *inode;
509 break;
510 }
511 }
512#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000513 if (!(fs->flags & EXT2_FLAG_RW))
514 return EXT2_ET_RO_FILSYS;
515
516 if (ino > fs->super->s_inodes_count)
517 return EXT2_ET_BAD_INODE_NUM;
518
519 if (inode_buffer_size != fs->blocksize) {
520 if (inode_buffer)
521 free(inode_buffer);
522 inode_buffer_size = 0;
523 inode_buffer = malloc(fs->blocksize);
524 if (!inode_buffer)
525 return ENOMEM;
526 inode_buffer_size = fs->blocksize;
527 inode_buffer_block = 0;
528 }
Theodore Ts'o5c576471997-04-29 15:29:49 +0000529 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
530 (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE))
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000531 ext2fs_swap_inode(fs, &temp_inode, inode, 1);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000532 else
533 memcpy(&temp_inode, inode, sizeof(struct ext2_inode));
534
Theodore Ts'o3839e651997-04-26 13:21:57 +0000535 group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000536 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
537 EXT2_INODE_SIZE(fs->super);
538 block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000539 if (!fs->group_desc[group].bg_inode_table)
540 return EXT2_ET_MISSING_INODE_TABLE;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000541 block_nr = fs->group_desc[group].bg_inode_table + block;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000542 offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
543 ptr = (char *) inode_buffer + offset;
544
545 length = EXT2_INODE_SIZE(fs->super);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000546 clen = length;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000547 if (length > sizeof(struct ext2_inode))
548 length = sizeof(struct ext2_inode);
549
Theodore Ts'o3839e651997-04-26 13:21:57 +0000550 if (inode_buffer_block != block_nr) {
551 retval = io_channel_read_blk(fs->io, block_nr, 1,
552 inode_buffer);
553 if (retval)
554 return retval;
555 inode_buffer_block = block_nr;
556 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000557
558 if ((offset + length) > EXT2_BLOCK_SIZE(fs->super)) {
559 clen = EXT2_BLOCK_SIZE(fs->super) - offset;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000560 length -= clen;
561 } else {
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000562 length = 0;
563 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000564 memcpy(ptr, &temp_inode, clen);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000565 retval = io_channel_write_blk(fs->io, block_nr, 1, inode_buffer);
566 if (retval)
567 return retval;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000568
569 if (length) {
570 retval = io_channel_read_blk(fs->io, ++block_nr, 1,
571 inode_buffer);
572 if (retval) {
573 inode_buffer_block = 0;
574 return retval;
575 }
576 inode_buffer_block = block_nr;
577 memcpy(inode_buffer, ((char *) &temp_inode) + clen, length);
578
579 retval = io_channel_write_blk(fs->io, block_nr, 1,
580 inode_buffer);
581 if (retval)
582 return retval;
583 }
584
Theodore Ts'o3839e651997-04-26 13:21:57 +0000585 fs->flags |= EXT2_FLAG_CHANGED;
586 return 0;
587}
588
589errcode_t ext2fs_get_blocks(ext2_filsys fs, ino_t ino, blk_t *blocks)
590{
591 struct ext2_inode inode;
592 int i;
593 errcode_t retval;
594
Theodore Ts'of3db3561997-04-26 13:34:30 +0000595 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
596
Theodore Ts'o3839e651997-04-26 13:21:57 +0000597 if (ino > fs->super->s_inodes_count)
598 return EXT2_ET_BAD_INODE_NUM;
599
600 if (fs->get_blocks) {
601 if (!(*fs->get_blocks)(fs, ino, blocks))
602 return 0;
603 }
604 retval = ext2fs_read_inode(fs, ino, &inode);
605 if (retval)
606 return retval;
607 for (i=0; i < EXT2_N_BLOCKS; i++)
608 blocks[i] = inode.i_block[i];
609 return 0;
610}
611
612errcode_t ext2fs_check_directory(ext2_filsys fs, ino_t ino)
613{
614 struct ext2_inode inode;
615 errcode_t retval;
616
Theodore Ts'of3db3561997-04-26 13:34:30 +0000617 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
618
Theodore Ts'o3839e651997-04-26 13:21:57 +0000619 if (ino > fs->super->s_inodes_count)
620 return EXT2_ET_BAD_INODE_NUM;
621
622 if (fs->check_directory)
623 return (fs->check_directory)(fs, ino);
624 retval = ext2fs_read_inode(fs, ino, &inode);
625 if (retval)
626 return retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000627 if (!LINUX_S_ISDIR(inode.i_mode))
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000628 return ENOTDIR;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000629 return 0;
630}
631