blob: be882ad54608194f5aeeaba7b44a53891b3b5692 [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
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000049static errcode_t create_icache(ext2_filsys fs)
50{
51 int i;
52
53 if (fs->icache)
54 return 0;
55 fs->icache = malloc(sizeof(struct ext2_inode_cache));
56 memset(fs->icache, 0, sizeof(struct ext2_inode_cache));
57 fs->icache->buffer = malloc(fs->blocksize);
58 if (!fs->icache->buffer) {
59 free(fs->icache);
60 return ENOMEM;
61 }
62 fs->icache->buffer_blk = 0;
63 fs->icache->cache_last = -1;
64 fs->icache->cache_size = 4;
65 fs->icache->refcount = 1;
66 fs->icache->cache = malloc(sizeof(struct ext2_inode_cache_ent)
67 * fs->icache->cache_size);
68 if (!fs->icache->cache) {
69 free(fs->icache->buffer);
70 free(fs->icache);
71 return ENOMEM;
72 }
73 for (i=0; i < fs->icache->cache_size; i++)
74 fs->icache->cache[i].ino = 0;
75 return 0;
76}
77
Theodore Ts'o3839e651997-04-26 13:21:57 +000078errcode_t ext2fs_open_inode_scan(ext2_filsys fs, int buffer_blocks,
79 ext2_inode_scan *ret_scan)
80{
81 ext2_inode_scan scan;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000082 errcode_t retval;
Theodore Ts'o521e3681997-04-29 17:48:10 +000083 errcode_t (*save_get_blocks)(ext2_filsys fs, ino_t ino, blk_t *blocks);
Theodore Ts'o3839e651997-04-26 13:21:57 +000084
Theodore Ts'of3db3561997-04-26 13:34:30 +000085 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
86
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000087 /*
88 * If fs->badblocks isn't set, then set it --- since the inode
89 * scanning functions require it.
90 */
91 if (fs->badblocks == 0) {
Theodore Ts'o521e3681997-04-29 17:48:10 +000092 /*
93 * Temporarly save fs->get_blocks and set it to zero,
94 * for compatibility with old e2fsck's.
95 */
96 save_get_blocks = fs->get_blocks;
97 fs->get_blocks = 0;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000098 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
99 if (retval && fs->badblocks) {
100 badblocks_list_free(fs->badblocks);
101 fs->badblocks = 0;
102 }
Theodore Ts'o521e3681997-04-29 17:48:10 +0000103 fs->get_blocks = save_get_blocks;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000104 }
105
Theodore Ts'o3839e651997-04-26 13:21:57 +0000106 scan = (ext2_inode_scan) malloc(sizeof(struct ext2_struct_inode_scan));
107 if (!scan)
108 return ENOMEM;
109 memset(scan, 0, sizeof(struct ext2_struct_inode_scan));
110
Theodore Ts'of3db3561997-04-26 13:34:30 +0000111 scan->magic = EXT2_ET_MAGIC_INODE_SCAN;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000112 scan->fs = fs;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000113 scan->inode_size = EXT2_INODE_SIZE(fs->super);
114 scan->bytes_left = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000115 scan->current_group = -1;
116 scan->inode_buffer_blocks = buffer_blocks ? buffer_blocks : 8;
117 scan->groups_left = fs->group_desc_count;
118 scan->inode_buffer = malloc(scan->inode_buffer_blocks * fs->blocksize);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000119 scan->done_group = 0;
120 scan->done_group_data = 0;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000121 scan->bad_block_ptr = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000122 if (!scan->inode_buffer) {
123 free(scan);
124 return ENOMEM;
125 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000126 scan->temp_buffer = malloc(scan->inode_size);
127 if (!scan->temp_buffer) {
128 free(scan->inode_buffer);
129 free(scan);
130 return ENOMEM;
131 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000132 if (scan->fs->badblocks && scan->fs->badblocks->num)
133 scan->scan_flags |= EXT2_SF_CHK_BADBLOCKS;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000134 *ret_scan = scan;
135 return 0;
136}
137
138void ext2fs_close_inode_scan(ext2_inode_scan scan)
139{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000140 if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
141 return;
142
Theodore Ts'o3839e651997-04-26 13:21:57 +0000143 free(scan->inode_buffer);
144 scan->inode_buffer = NULL;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000145 free(scan->temp_buffer);
146 scan->temp_buffer = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000147 free(scan);
148 return;
149}
150
Theodore Ts'of3db3561997-04-26 13:34:30 +0000151void ext2fs_set_inode_callback(ext2_inode_scan scan,
152 errcode_t (*done_group)(ext2_filsys fs,
153 ext2_inode_scan scan,
154 dgrp_t group,
155 void * private),
156 void *done_group_data)
157{
158 if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
159 return;
160
161 scan->done_group = done_group;
162 scan->done_group_data = done_group_data;
163}
164
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000165int ext2fs_inode_scan_flags(ext2_inode_scan scan, int set_flags,
166 int clear_flags)
167{
168 int old_flags;
169
170 if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
171 return 0;
172
173 old_flags = scan->scan_flags;
174 scan->scan_flags &= ~clear_flags;
175 scan->scan_flags |= set_flags;
176 return old_flags;
177}
178
179/*
180 * This function is called by ext2fs_get_next_inode when it needs to
181 * get ready to read in a new blockgroup.
182 */
183static errcode_t get_next_blockgroup(ext2_inode_scan scan)
184{
185 scan->current_group++;
186 scan->groups_left--;
187
188 scan->current_block = scan->fs->
189 group_desc[scan->current_group].bg_inode_table;
190
191 scan->bytes_left = 0;
192 scan->inodes_left = EXT2_INODES_PER_GROUP(scan->fs->super);
193 scan->blocks_left = scan->fs->inode_blocks_per_group;
194 return 0;
195}
196
197errcode_t ext2fs_inode_scan_goto_blockgroup(ext2_inode_scan scan,
198 int group)
199{
200 scan->current_group = group - 1;
201 scan->groups_left = scan->fs->group_desc_count - group;
202 return get_next_blockgroup(scan);
203}
204
205/*
206 * This function is called by get_next_blocks() to check for bad
207 * blocks in the inode table.
208 *
209 * This function assumes that badblocks_list->list is sorted in
210 * increasing order.
211 */
212static errcode_t check_for_inode_bad_blocks(ext2_inode_scan scan,
213 int *num_blocks)
214{
215 blk_t blk = scan->current_block;
216 badblocks_list bb = scan->fs->badblocks;
217
218 /*
219 * If the inode table is missing, then obviously there are no
220 * bad blocks. :-)
221 */
222 if (blk == 0)
223 return 0;
224
225 /*
226 * If the current block is greater than the bad block listed
227 * in the bad block list, then advance the pointer until this
228 * is no longer the case. If we run out of bad blocks, then
229 * we don't need to do any more checking!
230 */
231 while (blk > bb->list[scan->bad_block_ptr]) {
232 if (++scan->bad_block_ptr >= bb->num) {
233 scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
234 return 0;
235 }
236 }
237
238 /*
239 * If the current block is equal to the bad block listed in
240 * the bad block list, then handle that one block specially.
241 * (We could try to handle runs of bad blocks, but that
242 * only increases CPU efficiency by a small amount, at the
243 * expense of a huge expense of code complexity, and for an
244 * uncommon case at that.)
245 */
246 if (blk == bb->list[scan->bad_block_ptr]) {
247 scan->scan_flags |= EXT2_SF_BAD_INODE_BLK;
248 *num_blocks = 1;
249 if (++scan->bad_block_ptr >= bb->num)
250 scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
251 return 0;
252 }
253
254 /*
255 * If there is a bad block in the range that we're about to
256 * read in, adjust the number of blocks to read so that we we
257 * don't read in the bad block. (Then the next block to read
258 * will be the bad block, which is handled in the above case.)
259 */
260 if ((blk + *num_blocks) > bb->list[scan->bad_block_ptr])
261 *num_blocks = bb->list[scan->bad_block_ptr] - blk;
262
263 return 0;
264}
265
266/*
267 * This function is called by ext2fs_get_next_inode when it needs to
268 * read in more blocks from the current blockgroup's inode table.
269 */
270static errcode_t get_next_blocks(ext2_inode_scan scan)
271{
272 int num_blocks;
273 errcode_t retval;
274
275 /*
276 * Figure out how many blocks to read; we read at most
277 * inode_buffer_blocks, and perhaps less if there aren't that
278 * many blocks left to read.
279 */
280 num_blocks = scan->inode_buffer_blocks;
281 if (num_blocks > scan->blocks_left)
282 num_blocks = scan->blocks_left;
283
284 /*
285 * If the past block "read" was a bad block, then mark the
286 * left-over extra bytes as also being bad.
287 */
288 if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK) {
289 if (scan->bytes_left)
290 scan->scan_flags |= EXT2_SF_BAD_EXTRA_BYTES;
291 scan->scan_flags &= ~EXT2_SF_BAD_INODE_BLK;
292 }
293
294 /*
295 * Do inode bad block processing, if necessary.
296 */
297 if (scan->scan_flags & EXT2_SF_CHK_BADBLOCKS) {
298 retval = check_for_inode_bad_blocks(scan, &num_blocks);
299 if (retval)
300 return retval;
301 }
302
303 if ((scan->scan_flags & EXT2_SF_BAD_INODE_BLK) ||
304 (scan->current_block == 0)) {
305 memset(scan->inode_buffer, 0,
306 num_blocks * scan->fs->blocksize);
307 } else {
308 retval = io_channel_read_blk(scan->fs->io,
309 scan->current_block,
310 num_blocks, scan->inode_buffer);
311 if (retval)
312 return EXT2_ET_NEXT_INODE_READ;
313 }
314 scan->ptr = scan->inode_buffer;
315 scan->bytes_left = num_blocks * scan->fs->blocksize;
316
317 scan->blocks_left -= num_blocks;
318 if (scan->current_block)
319 scan->current_block += num_blocks;
320 return 0;
321}
322
Theodore Ts'o3839e651997-04-26 13:21:57 +0000323errcode_t ext2fs_get_next_inode(ext2_inode_scan scan, ino_t *ino,
324 struct ext2_inode *inode)
325{
326 errcode_t retval;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000327 int extra_bytes = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000328
Theodore Ts'of3db3561997-04-26 13:34:30 +0000329 EXT2_CHECK_MAGIC(scan, EXT2_ET_MAGIC_INODE_SCAN);
330
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000331 /*
332 * Do we need to start reading a new block group?
333 */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000334 if (scan->inodes_left <= 0) {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000335 retry:
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000336 if (scan->done_group) {
337 retval = (scan->done_group)
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000338 (scan->fs, scan, scan->current_group,
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000339 scan->done_group_data);
340 if (retval)
341 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000342 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000343 if (scan->groups_left <= 0) {
344 *ino = 0;
345 return 0;
346 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000347 retval = get_next_blockgroup(scan);
348 if (retval)
349 return retval;
350 if (scan->current_block == 0) {
351 if (scan->scan_flags & EXT2_SF_SKIP_MISSING_ITABLE) {
352 goto retry;
353 } else
354 return EXT2_ET_MISSING_INODE_TABLE;
355 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000356 }
357
358 /*
359 * Have we run out of space in the inode buffer? If so, we
360 * need to read in more blocks.
361 */
362 if (scan->bytes_left < scan->inode_size) {
363 memcpy(scan->temp_buffer, scan->ptr, scan->bytes_left);
364 extra_bytes = scan->bytes_left;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000365
366 retval = get_next_blocks(scan);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000367 if (retval)
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000368 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000369 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000370
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000371 retval = 0;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000372 if (extra_bytes) {
373 memcpy(scan->temp_buffer+extra_bytes, scan->ptr,
374 scan->inode_size - extra_bytes);
375 scan->ptr += scan->inode_size - extra_bytes;
376 scan->bytes_left -= scan->inode_size - extra_bytes;
377
Theodore Ts'o5c576471997-04-29 15:29:49 +0000378 if ((scan->fs->flags & EXT2_FLAG_SWAP_BYTES) ||
379 (scan->fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000380 ext2fs_swap_inode(scan->fs, inode,
381 (struct ext2_inode *) scan->temp_buffer, 0);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000382 else
383 *inode = *((struct ext2_inode *) scan->temp_buffer);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000384 if (scan->scan_flags & EXT2_SF_BAD_EXTRA_BYTES)
385 retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
386 scan->scan_flags &= ~EXT2_SF_BAD_EXTRA_BYTES;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000387 } else {
Theodore Ts'o5c576471997-04-29 15:29:49 +0000388 if ((scan->fs->flags & EXT2_FLAG_SWAP_BYTES) ||
389 (scan->fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000390 ext2fs_swap_inode(scan->fs, inode,
391 (struct ext2_inode *) scan->ptr, 0);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000392 else
393 *inode = *((struct ext2_inode *) scan->ptr);
394 scan->ptr += scan->inode_size;
395 scan->bytes_left -= scan->inode_size;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000396 if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK)
397 retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000398 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000399
Theodore Ts'o3839e651997-04-26 13:21:57 +0000400 scan->inodes_left--;
401 scan->current_inode++;
402 *ino = scan->current_inode;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000403 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000404}
405
406/*
407 * Functions to read and write a single inode.
408 */
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000409errcode_t ext2fs_read_inode (ext2_filsys fs, ino_t ino,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000410 struct ext2_inode * inode)
411{
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000412 unsigned long group, block, block_nr, offset;
413 char *ptr;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000414 errcode_t retval;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000415 int clen, length, i;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000416
Theodore Ts'of3db3561997-04-26 13:34:30 +0000417 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
418
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000419 /* Check to see if user has an override function */
420 if (fs->read_inode) {
421 retval = (fs->read_inode)(fs, ino, inode);
422 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
423 return retval;
424 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000425 /* Create inode cache if not present */
426 if (!fs->icache) {
427 retval = create_icache(fs);
428 if (retval)
429 return retval;
430 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000431 /* Check to see if it's in the inode cache */
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000432 for (i=0; i < fs->icache->cache_size; i++) {
433 if (fs->icache->cache[i].ino == ino) {
434 *inode = fs->icache->cache[i].inode;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000435 return 0;
436 }
437 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000438 if (ino > fs->super->s_inodes_count)
439 return EXT2_ET_BAD_INODE_NUM;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000440 group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000441 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
442 EXT2_INODE_SIZE(fs->super);
443 block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000444 if (!fs->group_desc[group].bg_inode_table)
445 return EXT2_ET_MISSING_INODE_TABLE;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000446 block_nr = fs->group_desc[group].bg_inode_table + block;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000447 if (block_nr != fs->icache->buffer_blk) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000448 retval = io_channel_read_blk(fs->io, block_nr, 1,
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000449 fs->icache->buffer);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000450 if (retval)
451 return retval;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000452 fs->icache->buffer_blk = block_nr;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000453 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000454 offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000455 ptr = ((char *) fs->icache->buffer) + offset;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000456
457 memset(inode, 0, sizeof(struct ext2_inode));
458 length = EXT2_INODE_SIZE(fs->super);
459 if (length > sizeof(struct ext2_inode))
460 length = sizeof(struct ext2_inode);
461
462 if ((offset + length) > EXT2_BLOCK_SIZE(fs->super)) {
463 clen = EXT2_BLOCK_SIZE(fs->super) - offset;
464 memcpy((char *) inode, ptr, clen);
465 length -= clen;
466
467 retval = io_channel_read_blk(fs->io, block_nr+1, 1,
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000468 fs->icache->buffer);
469 if (retval) {
470 fs->icache->buffer_blk = 0;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000471 return retval;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000472 }
473 fs->icache->buffer_blk = block_nr+1;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000474
475 memcpy(((char *) inode) + clen,
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000476 fs->icache->buffer, length);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000477 } else
478 memcpy((char *) inode, ptr, length);
479
Theodore Ts'o5c576471997-04-29 15:29:49 +0000480 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
481 (fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000482 ext2fs_swap_inode(fs, inode, inode, 0);
483
484 /* Update the inode cache */
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000485 fs->icache->cache_last = (fs->icache->cache_last + 1) %
486 fs->icache->cache_size;
487 fs->icache->cache[fs->icache->cache_last].ino = ino;
488 fs->icache->cache[fs->icache->cache_last].inode = *inode;
489
Theodore Ts'o3839e651997-04-26 13:21:57 +0000490 return 0;
491}
492
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000493errcode_t ext2fs_write_inode(ext2_filsys fs, ino_t ino,
494 struct ext2_inode * inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000495{
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000496 unsigned long group, block, block_nr, offset;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000497 errcode_t retval;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000498 struct ext2_inode temp_inode;
499 char *ptr;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000500 int clen, length, i;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000501
Theodore Ts'of3db3561997-04-26 13:34:30 +0000502 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
503
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000504 /* Check to see if user provided an override function */
505 if (fs->write_inode) {
506 retval = (fs->write_inode)(fs, ino, inode);
507 if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
508 return retval;
509 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000510
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000511 /* Check to see if the inode cache needs to be updated */
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000512 if (fs->icache) {
513 for (i=0; i < fs->icache->cache_size; i++) {
514 if (fs->icache->cache[i].ino == ino) {
515 fs->icache->cache[i].inode = *inode;
516 break;
517 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000518 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000519 } else {
520 retval = create_icache(fs);
521 if (retval)
522 return retval;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000523 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000524
Theodore Ts'o3839e651997-04-26 13:21:57 +0000525 if (!(fs->flags & EXT2_FLAG_RW))
526 return EXT2_ET_RO_FILSYS;
527
528 if (ino > fs->super->s_inodes_count)
529 return EXT2_ET_BAD_INODE_NUM;
530
Theodore Ts'o5c576471997-04-29 15:29:49 +0000531 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
532 (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE))
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000533 ext2fs_swap_inode(fs, &temp_inode, inode, 1);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000534 else
535 memcpy(&temp_inode, inode, sizeof(struct ext2_inode));
536
Theodore Ts'o3839e651997-04-26 13:21:57 +0000537 group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000538 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
539 EXT2_INODE_SIZE(fs->super);
540 block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000541 if (!fs->group_desc[group].bg_inode_table)
542 return EXT2_ET_MISSING_INODE_TABLE;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000543 block_nr = fs->group_desc[group].bg_inode_table + block;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000544 offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000545 ptr = (char *) fs->icache->buffer + offset;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000546
547 length = EXT2_INODE_SIZE(fs->super);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000548 clen = length;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000549 if (length > sizeof(struct ext2_inode))
550 length = sizeof(struct ext2_inode);
551
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000552 if (fs->icache->buffer_blk != block_nr) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000553 retval = io_channel_read_blk(fs->io, block_nr, 1,
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000554 fs->icache->buffer);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000555 if (retval)
556 return retval;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000557 fs->icache->buffer_blk = block_nr;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000558 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000559
560 if ((offset + length) > EXT2_BLOCK_SIZE(fs->super)) {
561 clen = EXT2_BLOCK_SIZE(fs->super) - offset;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000562 length -= clen;
563 } else {
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000564 length = 0;
565 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000566 memcpy(ptr, &temp_inode, clen);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000567 retval = io_channel_write_blk(fs->io, block_nr, 1, fs->icache->buffer);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000568 if (retval)
569 return retval;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000570
571 if (length) {
572 retval = io_channel_read_blk(fs->io, ++block_nr, 1,
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000573 fs->icache->buffer);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000574 if (retval) {
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000575 fs->icache->buffer_blk = 0;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000576 return retval;
577 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000578 fs->icache->buffer_blk = block_nr;
579 memcpy(fs->icache->buffer, ((char *) &temp_inode) + clen,
580 length);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000581
582 retval = io_channel_write_blk(fs->io, block_nr, 1,
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000583 fs->icache->buffer);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000584 if (retval)
585 return retval;
586 }
587
Theodore Ts'o3839e651997-04-26 13:21:57 +0000588 fs->flags |= EXT2_FLAG_CHANGED;
589 return 0;
590}
591
592errcode_t ext2fs_get_blocks(ext2_filsys fs, ino_t ino, blk_t *blocks)
593{
594 struct ext2_inode inode;
595 int i;
596 errcode_t retval;
597
Theodore Ts'of3db3561997-04-26 13:34:30 +0000598 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
599
Theodore Ts'o3839e651997-04-26 13:21:57 +0000600 if (ino > fs->super->s_inodes_count)
601 return EXT2_ET_BAD_INODE_NUM;
602
603 if (fs->get_blocks) {
604 if (!(*fs->get_blocks)(fs, ino, blocks))
605 return 0;
606 }
607 retval = ext2fs_read_inode(fs, ino, &inode);
608 if (retval)
609 return retval;
610 for (i=0; i < EXT2_N_BLOCKS; i++)
611 blocks[i] = inode.i_block[i];
612 return 0;
613}
614
615errcode_t ext2fs_check_directory(ext2_filsys fs, ino_t ino)
616{
617 struct ext2_inode inode;
618 errcode_t retval;
619
Theodore Ts'of3db3561997-04-26 13:34:30 +0000620 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
621
Theodore Ts'o3839e651997-04-26 13:21:57 +0000622 if (ino > fs->super->s_inodes_count)
623 return EXT2_ET_BAD_INODE_NUM;
624
625 if (fs->check_directory)
626 return (fs->check_directory)(fs, ino);
627 retval = ext2fs_read_inode(fs, ino, &inode);
628 if (retval)
629 return retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000630 if (!LINUX_S_ISDIR(inode.i_mode))
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000631 return ENOTDIR;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000632 return 0;
633}
634