inode.c revision 1ad54a940c499a66241f624882f1ffa03ce56d90
1/*
2 * inode.c --- utility routines to read and write inodes
3 *
4 * 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%
10 */
11
12#include <stdio.h>
13#include <string.h>
14#if HAVE_UNISTD_H
15#include <unistd.h>
16#endif
17#if HAVE_SYS_STAT_H
18#include <sys/stat.h>
19#endif
20#if HAVE_SYS_TYPES_H
21#include <sys/types.h>
22#endif
23
24#include "ext2_fs.h"
25#include "ext2fsP.h"
26#include "e2image.h"
27
28struct ext2_struct_inode_scan {
29	errcode_t		magic;
30	ext2_filsys		fs;
31	ext2_ino_t		current_inode;
32	blk_t			current_block;
33	dgrp_t			current_group;
34	ext2_ino_t		inodes_left;
35	blk_t			blocks_left;
36	dgrp_t			groups_left;
37	blk_t			inode_buffer_blocks;
38	char *			inode_buffer;
39	int			inode_size;
40	char *			ptr;
41	int			bytes_left;
42	char			*temp_buffer;
43	errcode_t		(*done_group)(ext2_filsys fs,
44					      ext2_inode_scan scan,
45					      dgrp_t group,
46					      void * priv_data);
47	void *			done_group_data;
48	int			bad_block_ptr;
49	int			scan_flags;
50	int			reserved[6];
51};
52
53/*
54 * This routine flushes the icache, if it exists.
55 */
56errcode_t ext2fs_flush_icache(ext2_filsys fs)
57{
58	int	i;
59
60	if (!fs->icache)
61		return 0;
62
63	for (i=0; i < fs->icache->cache_size; i++)
64		fs->icache->cache[i].ino = 0;
65
66	return 0;
67}
68
69static errcode_t create_icache(ext2_filsys fs)
70{
71	errcode_t	retval;
72
73	if (fs->icache)
74		return 0;
75	retval = ext2fs_get_mem(sizeof(struct ext2_inode_cache), &fs->icache);
76	if (retval)
77		return retval;
78
79	memset(fs->icache, 0, sizeof(struct ext2_inode_cache));
80	retval = ext2fs_get_mem(fs->blocksize, &fs->icache->buffer);
81	if (retval) {
82		ext2fs_free_mem(&fs->icache);
83		return retval;
84	}
85	fs->icache->buffer_blk = 0;
86	fs->icache->cache_last = -1;
87	fs->icache->cache_size = 4;
88	fs->icache->refcount = 1;
89	retval = ext2fs_get_mem(sizeof(struct ext2_inode_cache_ent)
90				* fs->icache->cache_size,
91				&fs->icache->cache);
92	if (retval) {
93		ext2fs_free_mem(&fs->icache->buffer);
94		ext2fs_free_mem(&fs->icache);
95		return retval;
96	}
97	ext2fs_flush_icache(fs);
98	return 0;
99}
100
101errcode_t ext2fs_open_inode_scan(ext2_filsys fs, int buffer_blocks,
102				 ext2_inode_scan *ret_scan)
103{
104	ext2_inode_scan	scan;
105	errcode_t	retval;
106	errcode_t (*save_get_blocks)(ext2_filsys f, ext2_ino_t ino, blk_t *blocks);
107
108	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
109
110	/*
111	 * If fs->badblocks isn't set, then set it --- since the inode
112	 * scanning functions require it.
113	 */
114	if (fs->badblocks == 0) {
115		/*
116		 * Temporarly save fs->get_blocks and set it to zero,
117		 * for compatibility with old e2fsck's.
118		 */
119		save_get_blocks = fs->get_blocks;
120		fs->get_blocks = 0;
121		retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
122		if (retval && fs->badblocks) {
123			ext2fs_badblocks_list_free(fs->badblocks);
124			fs->badblocks = 0;
125		}
126		fs->get_blocks = save_get_blocks;
127	}
128
129	retval = ext2fs_get_mem(sizeof(struct ext2_struct_inode_scan), &scan);
130	if (retval)
131		return retval;
132	memset(scan, 0, sizeof(struct ext2_struct_inode_scan));
133
134	scan->magic = EXT2_ET_MAGIC_INODE_SCAN;
135	scan->fs = fs;
136	scan->inode_size = EXT2_INODE_SIZE(fs->super);
137	scan->bytes_left = 0;
138	scan->current_group = 0;
139	scan->groups_left = fs->group_desc_count - 1;
140	scan->inode_buffer_blocks = buffer_blocks ? buffer_blocks : 8;
141	scan->current_block = scan->fs->
142		group_desc[scan->current_group].bg_inode_table;
143	scan->inodes_left = EXT2_INODES_PER_GROUP(scan->fs->super);
144	scan->blocks_left = scan->fs->inode_blocks_per_group;
145	retval = ext2fs_get_mem((size_t) (scan->inode_buffer_blocks *
146					  fs->blocksize),
147				&scan->inode_buffer);
148	scan->done_group = 0;
149	scan->done_group_data = 0;
150	scan->bad_block_ptr = 0;
151	if (retval) {
152		ext2fs_free_mem(&scan);
153		return retval;
154	}
155	retval = ext2fs_get_mem(scan->inode_size, &scan->temp_buffer);
156	if (retval) {
157		ext2fs_free_mem(&scan->inode_buffer);
158		ext2fs_free_mem(&scan);
159		return retval;
160	}
161	if (scan->fs->badblocks && scan->fs->badblocks->num)
162		scan->scan_flags |= EXT2_SF_CHK_BADBLOCKS;
163	*ret_scan = scan;
164	return 0;
165}
166
167void ext2fs_close_inode_scan(ext2_inode_scan scan)
168{
169	if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
170		return;
171
172	ext2fs_free_mem(&scan->inode_buffer);
173	scan->inode_buffer = NULL;
174	ext2fs_free_mem(&scan->temp_buffer);
175	scan->temp_buffer = NULL;
176	ext2fs_free_mem(&scan);
177	return;
178}
179
180void ext2fs_set_inode_callback(ext2_inode_scan scan,
181			       errcode_t (*done_group)(ext2_filsys fs,
182						       ext2_inode_scan scan,
183						       dgrp_t group,
184						       void * priv_data),
185			       void *done_group_data)
186{
187	if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
188		return;
189
190	scan->done_group = done_group;
191	scan->done_group_data = done_group_data;
192}
193
194int ext2fs_inode_scan_flags(ext2_inode_scan scan, int set_flags,
195			    int clear_flags)
196{
197	int	old_flags;
198
199	if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
200		return 0;
201
202	old_flags = scan->scan_flags;
203	scan->scan_flags &= ~clear_flags;
204	scan->scan_flags |= set_flags;
205	return old_flags;
206}
207
208/*
209 * This function is called by ext2fs_get_next_inode when it needs to
210 * get ready to read in a new blockgroup.
211 */
212static errcode_t get_next_blockgroup(ext2_inode_scan scan)
213{
214	scan->current_group++;
215	scan->groups_left--;
216
217	scan->current_block = scan->fs->
218		group_desc[scan->current_group].bg_inode_table;
219
220	scan->current_inode = scan->current_group *
221		EXT2_INODES_PER_GROUP(scan->fs->super);
222
223	scan->bytes_left = 0;
224	scan->inodes_left = EXT2_INODES_PER_GROUP(scan->fs->super);
225	scan->blocks_left = scan->fs->inode_blocks_per_group;
226	return 0;
227}
228
229errcode_t ext2fs_inode_scan_goto_blockgroup(ext2_inode_scan scan,
230					    int	group)
231{
232	scan->current_group = group - 1;
233	scan->groups_left = scan->fs->group_desc_count - group;
234	return get_next_blockgroup(scan);
235}
236
237/*
238 * This function is called by get_next_blocks() to check for bad
239 * blocks in the inode table.
240 *
241 * This function assumes that badblocks_list->list is sorted in
242 * increasing order.
243 */
244static errcode_t check_for_inode_bad_blocks(ext2_inode_scan scan,
245					    blk_t *num_blocks)
246{
247	blk_t	blk = scan->current_block;
248	badblocks_list	bb = scan->fs->badblocks;
249
250	/*
251	 * If the inode table is missing, then obviously there are no
252	 * bad blocks.  :-)
253	 */
254	if (blk == 0)
255		return 0;
256
257	/*
258	 * If the current block is greater than the bad block listed
259	 * in the bad block list, then advance the pointer until this
260	 * is no longer the case.  If we run out of bad blocks, then
261	 * we don't need to do any more checking!
262	 */
263	while (blk > bb->list[scan->bad_block_ptr]) {
264		if (++scan->bad_block_ptr >= bb->num) {
265			scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
266			return 0;
267		}
268	}
269
270	/*
271	 * If the current block is equal to the bad block listed in
272	 * the bad block list, then handle that one block specially.
273	 * (We could try to handle runs of bad blocks, but that
274	 * only increases CPU efficiency by a small amount, at the
275	 * expense of a huge expense of code complexity, and for an
276	 * uncommon case at that.)
277	 */
278	if (blk == bb->list[scan->bad_block_ptr]) {
279		scan->scan_flags |= EXT2_SF_BAD_INODE_BLK;
280		*num_blocks = 1;
281		if (++scan->bad_block_ptr >= bb->num)
282			scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
283		return 0;
284	}
285
286	/*
287	 * If there is a bad block in the range that we're about to
288	 * read in, adjust the number of blocks to read so that we we
289	 * don't read in the bad block.  (Then the next block to read
290	 * will be the bad block, which is handled in the above case.)
291	 */
292	if ((blk + *num_blocks) > bb->list[scan->bad_block_ptr])
293		*num_blocks = (int) (bb->list[scan->bad_block_ptr] - blk);
294
295	return 0;
296}
297
298/*
299 * This function is called by ext2fs_get_next_inode when it needs to
300 * read in more blocks from the current blockgroup's inode table.
301 */
302static errcode_t get_next_blocks(ext2_inode_scan scan)
303{
304	blk_t		num_blocks;
305	errcode_t	retval;
306
307	/*
308	 * Figure out how many blocks to read; we read at most
309	 * inode_buffer_blocks, and perhaps less if there aren't that
310	 * many blocks left to read.
311	 */
312	num_blocks = scan->inode_buffer_blocks;
313	if (num_blocks > scan->blocks_left)
314		num_blocks = scan->blocks_left;
315
316	/*
317	 * If the past block "read" was a bad block, then mark the
318	 * left-over extra bytes as also being bad.
319	 */
320	if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK) {
321		if (scan->bytes_left)
322			scan->scan_flags |= EXT2_SF_BAD_EXTRA_BYTES;
323		scan->scan_flags &= ~EXT2_SF_BAD_INODE_BLK;
324	}
325
326	/*
327	 * Do inode bad block processing, if necessary.
328	 */
329	if (scan->scan_flags & EXT2_SF_CHK_BADBLOCKS) {
330		retval = check_for_inode_bad_blocks(scan, &num_blocks);
331		if (retval)
332			return retval;
333	}
334
335	if ((scan->scan_flags & EXT2_SF_BAD_INODE_BLK) ||
336	    (scan->current_block == 0)) {
337		memset(scan->inode_buffer, 0,
338		       (size_t) num_blocks * scan->fs->blocksize);
339	} else {
340		retval = io_channel_read_blk(scan->fs->io,
341					     scan->current_block,
342					     (int) num_blocks,
343					     scan->inode_buffer);
344		if (retval)
345			return EXT2_ET_NEXT_INODE_READ;
346	}
347	scan->ptr = scan->inode_buffer;
348	scan->bytes_left = num_blocks * scan->fs->blocksize;
349
350	scan->blocks_left -= num_blocks;
351	if (scan->current_block)
352		scan->current_block += num_blocks;
353	return 0;
354}
355
356#if 0
357/*
358 * Returns 1 if the entire inode_buffer has a non-zero size and
359 * contains all zeros.  (Not just deleted inodes, since that means
360 * that part of the inode table was used at one point; we want all
361 * zeros, which means that the inode table is pristine.)
362 */
363static inline int is_empty_scan(ext2_inode_scan scan)
364{
365	int	i;
366
367	if (scan->bytes_left == 0)
368		return 0;
369
370	for (i=0; i < scan->bytes_left; i++)
371		if (scan->ptr[i])
372			return 0;
373	return 1;
374}
375#endif
376
377errcode_t ext2fs_get_next_inode(ext2_inode_scan scan, ext2_ino_t *ino,
378				struct ext2_inode *inode)
379{
380	errcode_t	retval;
381	int		extra_bytes = 0;
382
383	EXT2_CHECK_MAGIC(scan, EXT2_ET_MAGIC_INODE_SCAN);
384
385	/*
386	 * Do we need to start reading a new block group?
387	 */
388	if (scan->inodes_left <= 0) {
389	force_new_group:
390		if (scan->done_group) {
391			retval = (scan->done_group)
392				(scan->fs, scan, scan->current_group,
393				 scan->done_group_data);
394			if (retval)
395				return retval;
396		}
397		if (scan->groups_left <= 0) {
398			*ino = 0;
399			return 0;
400		}
401		retval = get_next_blockgroup(scan);
402		if (retval)
403			return retval;
404	}
405	/*
406	 * This is done outside the above if statement so that the
407	 * check can be done for block group #0.
408	 */
409	if (scan->current_block == 0) {
410		if (scan->scan_flags & EXT2_SF_SKIP_MISSING_ITABLE) {
411			goto force_new_group;
412		} else
413			return EXT2_ET_MISSING_INODE_TABLE;
414	}
415
416
417	/*
418	 * Have we run out of space in the inode buffer?  If so, we
419	 * need to read in more blocks.
420	 */
421	if (scan->bytes_left < scan->inode_size) {
422		memcpy(scan->temp_buffer, scan->ptr, scan->bytes_left);
423		extra_bytes = scan->bytes_left;
424
425		retval = get_next_blocks(scan);
426		if (retval)
427			return retval;
428#if 0
429		/*
430		 * XXX test  Need check for used inode somehow.
431		 * (Note: this is hard.)
432		 */
433		if (is_empty_scan(scan))
434			goto force_new_group;
435#endif
436	}
437
438	retval = 0;
439	if (extra_bytes) {
440		memcpy(scan->temp_buffer+extra_bytes, scan->ptr,
441		       scan->inode_size - extra_bytes);
442		scan->ptr += scan->inode_size - extra_bytes;
443		scan->bytes_left -= scan->inode_size - extra_bytes;
444
445#ifdef EXT2FS_ENABLE_SWAPFS
446		if ((scan->fs->flags & EXT2_FLAG_SWAP_BYTES) ||
447		    (scan->fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
448			ext2fs_swap_inode(scan->fs, inode,
449				 (struct ext2_inode *) scan->temp_buffer, 0);
450		else
451#endif
452			*inode = *((struct ext2_inode *) scan->temp_buffer);
453		if (scan->scan_flags & EXT2_SF_BAD_EXTRA_BYTES)
454			retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
455		scan->scan_flags &= ~EXT2_SF_BAD_EXTRA_BYTES;
456	} else {
457#ifdef EXT2FS_ENABLE_SWAPFS
458		if ((scan->fs->flags & EXT2_FLAG_SWAP_BYTES) ||
459		    (scan->fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
460			ext2fs_swap_inode(scan->fs, inode,
461				 (struct ext2_inode *) scan->ptr, 0);
462		else
463#endif
464			*inode = *((struct ext2_inode *) scan->ptr);
465		scan->ptr += scan->inode_size;
466		scan->bytes_left -= scan->inode_size;
467		if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK)
468			retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
469	}
470
471	scan->inodes_left--;
472	scan->current_inode++;
473	*ino = scan->current_inode;
474	return retval;
475}
476
477/*
478 * Functions to read and write a single inode.
479 */
480errcode_t ext2fs_read_inode (ext2_filsys fs, ext2_ino_t ino,
481			     struct ext2_inode * inode)
482{
483	unsigned long 	group, block, block_nr, offset;
484	char 		*ptr;
485	errcode_t	retval;
486	int 		clen, i, inodes_per_block;
487	unsigned int	length;
488	io_channel	io;
489
490	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
491
492	/* Check to see if user has an override function */
493	if (fs->read_inode) {
494		retval = (fs->read_inode)(fs, ino, inode);
495		if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
496			return retval;
497	}
498	/* Create inode cache if not present */
499	if (!fs->icache) {
500		retval = create_icache(fs);
501		if (retval)
502			return retval;
503	}
504	/* Check to see if it's in the inode cache */
505	for (i=0; i < fs->icache->cache_size; i++) {
506		if (fs->icache->cache[i].ino == ino) {
507			*inode = fs->icache->cache[i].inode;
508			return 0;
509		}
510	}
511	if ((ino == 0) || (ino > fs->super->s_inodes_count))
512		return EXT2_ET_BAD_INODE_NUM;
513	if (fs->flags & EXT2_FLAG_IMAGE_FILE) {
514		inodes_per_block = fs->blocksize / EXT2_INODE_SIZE(fs->super);
515		block_nr = fs->image_header->offset_inode / fs->blocksize;
516		block_nr += (ino - 1) / inodes_per_block;
517		offset = ((ino - 1) % inodes_per_block) *
518			EXT2_INODE_SIZE(fs->super);
519		io = fs->image_io;
520	} else {
521		group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
522		offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
523			EXT2_INODE_SIZE(fs->super);
524		block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
525		if (!fs->group_desc[(unsigned)group].bg_inode_table)
526			return EXT2_ET_MISSING_INODE_TABLE;
527		block_nr = fs->group_desc[(unsigned)group].bg_inode_table +
528			block;
529		io = fs->io;
530	}
531	if (block_nr != fs->icache->buffer_blk) {
532		retval = io_channel_read_blk(io, block_nr, 1,
533					     fs->icache->buffer);
534		if (retval)
535			return retval;
536		fs->icache->buffer_blk = block_nr;
537	}
538	offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
539	ptr = ((char *) fs->icache->buffer) + (unsigned) offset;
540
541	memset(inode, 0, sizeof(struct ext2_inode));
542	length = EXT2_INODE_SIZE(fs->super);
543	if (length > sizeof(struct ext2_inode))
544		length = sizeof(struct ext2_inode);
545
546	if ((offset + length) > (unsigned) EXT2_BLOCK_SIZE(fs->super)) {
547		clen = (int) (EXT2_BLOCK_SIZE(fs->super) - offset);
548		memcpy((char *) inode, ptr, clen);
549		length -= clen;
550
551		retval = io_channel_read_blk(io, block_nr+1, 1,
552					     fs->icache->buffer);
553		if (retval) {
554			fs->icache->buffer_blk = 0;
555			return retval;
556		}
557		fs->icache->buffer_blk = block_nr+1;
558
559		memcpy(((char *) inode) + clen,
560		       fs->icache->buffer, length);
561	} else
562		memcpy((char *) inode, ptr, length);
563
564#ifdef EXT2FS_ENABLE_SWAPFS
565	if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
566	    (fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
567		ext2fs_swap_inode(fs, inode, inode, 0);
568#endif
569
570	/* Update the inode cache */
571	fs->icache->cache_last = (fs->icache->cache_last + 1) %
572		fs->icache->cache_size;
573	fs->icache->cache[fs->icache->cache_last].ino = ino;
574	fs->icache->cache[fs->icache->cache_last].inode = *inode;
575
576	return 0;
577}
578
579errcode_t ext2fs_write_inode(ext2_filsys fs, ext2_ino_t ino,
580			     struct ext2_inode * inode)
581{
582	unsigned long group, block, block_nr, offset;
583	errcode_t	retval;
584	struct ext2_inode temp_inode;
585	char *ptr;
586	int clen, i;
587	unsigned int length;
588
589	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
590
591	/* Check to see if user provided an override function */
592	if (fs->write_inode) {
593		retval = (fs->write_inode)(fs, ino, inode);
594		if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
595			return retval;
596	}
597
598	/* Check to see if the inode cache needs to be updated */
599	if (fs->icache) {
600		for (i=0; i < fs->icache->cache_size; i++) {
601			if (fs->icache->cache[i].ino == ino) {
602				fs->icache->cache[i].inode = *inode;
603				break;
604			}
605		}
606	} else {
607		retval = create_icache(fs);
608		if (retval)
609			return retval;
610	}
611
612	if (!(fs->flags & EXT2_FLAG_RW))
613		return EXT2_ET_RO_FILSYS;
614
615	if ((ino == 0) || (ino > fs->super->s_inodes_count))
616		return EXT2_ET_BAD_INODE_NUM;
617
618#ifdef EXT2FS_ENABLE_SWAPFS
619	if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
620	    (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE))
621		ext2fs_swap_inode(fs, &temp_inode, inode, 1);
622	else
623#endif
624		memcpy(&temp_inode, inode, sizeof(struct ext2_inode));
625
626	group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
627	offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
628		EXT2_INODE_SIZE(fs->super);
629	block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
630	if (!fs->group_desc[(unsigned) group].bg_inode_table)
631		return EXT2_ET_MISSING_INODE_TABLE;
632	block_nr = fs->group_desc[(unsigned) group].bg_inode_table + block;
633	offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
634	ptr = (char *) fs->icache->buffer + (unsigned) offset;
635
636	length = EXT2_INODE_SIZE(fs->super);
637	clen = length;
638	if (length > sizeof(struct ext2_inode))
639		length = sizeof(struct ext2_inode);
640
641	if (fs->icache->buffer_blk != block_nr) {
642		retval = io_channel_read_blk(fs->io, block_nr, 1,
643					     fs->icache->buffer);
644		if (retval)
645			return retval;
646		fs->icache->buffer_blk = block_nr;
647	}
648
649	if ((offset + length) > (unsigned) EXT2_BLOCK_SIZE(fs->super)) {
650		clen = (int) (EXT2_BLOCK_SIZE(fs->super) - offset);
651		length -= clen;
652	} else {
653		length = 0;
654	}
655	memcpy(ptr, &temp_inode, clen);
656	retval = io_channel_write_blk(fs->io, block_nr, 1, fs->icache->buffer);
657	if (retval)
658		return retval;
659
660	if (length) {
661		retval = io_channel_read_blk(fs->io, ++block_nr, 1,
662					     fs->icache->buffer);
663		if (retval) {
664			fs->icache->buffer_blk = 0;
665			return retval;
666		}
667		fs->icache->buffer_blk = block_nr;
668		memcpy(fs->icache->buffer, ((char *) &temp_inode) + clen,
669		       length);
670
671		retval = io_channel_write_blk(fs->io, block_nr, 1,
672					      fs->icache->buffer);
673		if (retval)
674			return retval;
675	}
676
677	fs->flags |= EXT2_FLAG_CHANGED;
678	return 0;
679}
680
681errcode_t ext2fs_get_blocks(ext2_filsys fs, ext2_ino_t ino, blk_t *blocks)
682{
683	struct ext2_inode	inode;
684	int			i;
685	errcode_t		retval;
686
687	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
688
689	if (ino > fs->super->s_inodes_count)
690		return EXT2_ET_BAD_INODE_NUM;
691
692	if (fs->get_blocks) {
693		if (!(*fs->get_blocks)(fs, ino, blocks))
694			return 0;
695	}
696	retval = ext2fs_read_inode(fs, ino, &inode);
697	if (retval)
698		return retval;
699	for (i=0; i < EXT2_N_BLOCKS; i++)
700		blocks[i] = inode.i_block[i];
701	return 0;
702}
703
704errcode_t ext2fs_check_directory(ext2_filsys fs, ext2_ino_t ino)
705{
706	struct	ext2_inode	inode;
707	errcode_t		retval;
708
709	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
710
711	if (ino > fs->super->s_inodes_count)
712		return EXT2_ET_BAD_INODE_NUM;
713
714	if (fs->check_directory) {
715		retval = (fs->check_directory)(fs, ino);
716		if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
717			return retval;
718	}
719	retval = ext2fs_read_inode(fs, ino, &inode);
720	if (retval)
721		return retval;
722	if (!LINUX_S_ISDIR(inode.i_mode))
723		return EXT2_ET_NO_DIRECTORY;
724	return 0;
725}
726
727