Changes to update Tomato RAF.
[tomato.git] / release / src / router / busybox / e2fsprogs / old_e2fsprogs / ext2fs / ext2fs_inline.c
blobb9aab440a8c1318adc0c8442ee4a3dff3b58d38d
1 /* vi: set sw=4 ts=4: */
2 /*
3 * ext2fs.h --- ext2fs
5 * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
7 * %Begin-Header%
8 * This file may be redistributed under the terms of the GNU Public
9 * License.
10 * %End-Header%
13 #include "ext2fs.h"
14 #include "bitops.h"
15 #include <string.h>
18 * Allocate memory
20 errcode_t ext2fs_get_mem(unsigned long size, void *ptr)
22 void **pp = (void **)ptr;
24 *pp = malloc(size);
25 if (!*pp)
26 return EXT2_ET_NO_MEMORY;
27 return 0;
31 * Free memory
33 errcode_t ext2fs_free_mem(void *ptr)
35 void **pp = (void **)ptr;
37 free(*pp);
38 *pp = 0;
39 return 0;
43 * Resize memory
45 errcode_t ext2fs_resize_mem(unsigned long EXT2FS_ATTR((unused)) old_size,
46 unsigned long size, void *ptr)
48 void *p;
50 /* Use "memcpy" for pointer assignments here to avoid problems
51 * with C99 strict type aliasing rules. */
52 memcpy(&p, ptr, sizeof (p));
53 p = xrealloc(p, size);
54 memcpy(ptr, &p, sizeof (p));
55 return 0;
59 * Mark a filesystem superblock as dirty
61 void ext2fs_mark_super_dirty(ext2_filsys fs)
63 fs->flags |= EXT2_FLAG_DIRTY | EXT2_FLAG_CHANGED;
67 * Mark a filesystem as changed
69 void ext2fs_mark_changed(ext2_filsys fs)
71 fs->flags |= EXT2_FLAG_CHANGED;
75 * Check to see if a filesystem has changed
77 int ext2fs_test_changed(ext2_filsys fs)
79 return (fs->flags & EXT2_FLAG_CHANGED);
83 * Mark a filesystem as valid
85 void ext2fs_mark_valid(ext2_filsys fs)
87 fs->flags |= EXT2_FLAG_VALID;
91 * Mark a filesystem as NOT valid
93 void ext2fs_unmark_valid(ext2_filsys fs)
95 fs->flags &= ~EXT2_FLAG_VALID;
99 * Check to see if a filesystem is valid
101 int ext2fs_test_valid(ext2_filsys fs)
103 return (fs->flags & EXT2_FLAG_VALID);
107 * Mark the inode bitmap as dirty
109 void ext2fs_mark_ib_dirty(ext2_filsys fs)
111 fs->flags |= EXT2_FLAG_IB_DIRTY | EXT2_FLAG_CHANGED;
115 * Mark the block bitmap as dirty
117 void ext2fs_mark_bb_dirty(ext2_filsys fs)
119 fs->flags |= EXT2_FLAG_BB_DIRTY | EXT2_FLAG_CHANGED;
123 * Check to see if a filesystem's inode bitmap is dirty
125 int ext2fs_test_ib_dirty(ext2_filsys fs)
127 return (fs->flags & EXT2_FLAG_IB_DIRTY);
131 * Check to see if a filesystem's block bitmap is dirty
133 int ext2fs_test_bb_dirty(ext2_filsys fs)
135 return (fs->flags & EXT2_FLAG_BB_DIRTY);
139 * Return the group # of a block
141 int ext2fs_group_of_blk(ext2_filsys fs, blk_t blk)
143 return (blk - fs->super->s_first_data_block) /
144 fs->super->s_blocks_per_group;
148 * Return the group # of an inode number
150 int ext2fs_group_of_ino(ext2_filsys fs, ext2_ino_t ino)
152 return (ino - 1) / fs->super->s_inodes_per_group;
155 blk_t ext2fs_inode_data_blocks(ext2_filsys fs,
156 struct ext2_inode *inode)
158 return inode->i_blocks -
159 (inode->i_file_acl ? fs->blocksize >> 9 : 0);
170 __u16 ext2fs_swab16(__u16 val)
172 return (val >> 8) | (val << 8);
175 __u32 ext2fs_swab32(__u32 val)
177 return ((val>>24) | ((val>>8)&0xFF00) |
178 ((val<<8)&0xFF0000) | (val<<24));
181 int ext2fs_test_generic_bitmap(ext2fs_generic_bitmap bitmap,
182 blk_t bitno);
184 int ext2fs_test_generic_bitmap(ext2fs_generic_bitmap bitmap,
185 blk_t bitno)
187 if ((bitno < bitmap->start) || (bitno > bitmap->end)) {
188 ext2fs_warn_bitmap2(bitmap, EXT2FS_TEST_ERROR, bitno);
189 return 0;
191 return ext2fs_test_bit(bitno - bitmap->start, bitmap->bitmap);
194 int ext2fs_mark_block_bitmap(ext2fs_block_bitmap bitmap,
195 blk_t block)
197 return ext2fs_mark_generic_bitmap((ext2fs_generic_bitmap)
198 bitmap,
199 block);
202 int ext2fs_unmark_block_bitmap(ext2fs_block_bitmap bitmap,
203 blk_t block)
205 return ext2fs_unmark_generic_bitmap((ext2fs_generic_bitmap) bitmap,
206 block);
209 int ext2fs_test_block_bitmap(ext2fs_block_bitmap bitmap,
210 blk_t block)
212 return ext2fs_test_generic_bitmap((ext2fs_generic_bitmap) bitmap,
213 block);
216 int ext2fs_mark_inode_bitmap(ext2fs_inode_bitmap bitmap,
217 ext2_ino_t inode)
219 return ext2fs_mark_generic_bitmap((ext2fs_generic_bitmap) bitmap,
220 inode);
223 int ext2fs_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap,
224 ext2_ino_t inode)
226 return ext2fs_unmark_generic_bitmap((ext2fs_generic_bitmap) bitmap,
227 inode);
230 int ext2fs_test_inode_bitmap(ext2fs_inode_bitmap bitmap,
231 ext2_ino_t inode)
233 return ext2fs_test_generic_bitmap((ext2fs_generic_bitmap) bitmap,
234 inode);
237 void ext2fs_fast_mark_block_bitmap(ext2fs_block_bitmap bitmap,
238 blk_t block)
240 ext2fs_set_bit(block - bitmap->start, bitmap->bitmap);
243 void ext2fs_fast_unmark_block_bitmap(ext2fs_block_bitmap bitmap,
244 blk_t block)
246 ext2fs_clear_bit(block - bitmap->start, bitmap->bitmap);
249 int ext2fs_fast_test_block_bitmap(ext2fs_block_bitmap bitmap,
250 blk_t block)
252 return ext2fs_test_bit(block - bitmap->start, bitmap->bitmap);
255 void ext2fs_fast_mark_inode_bitmap(ext2fs_inode_bitmap bitmap,
256 ext2_ino_t inode)
258 ext2fs_set_bit(inode - bitmap->start, bitmap->bitmap);
261 void ext2fs_fast_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap,
262 ext2_ino_t inode)
264 ext2fs_clear_bit(inode - bitmap->start, bitmap->bitmap);
267 int ext2fs_fast_test_inode_bitmap(ext2fs_inode_bitmap bitmap,
268 ext2_ino_t inode)
270 return ext2fs_test_bit(inode - bitmap->start, bitmap->bitmap);
273 blk_t ext2fs_get_block_bitmap_start(ext2fs_block_bitmap bitmap)
275 return bitmap->start;
278 ext2_ino_t ext2fs_get_inode_bitmap_start(ext2fs_inode_bitmap bitmap)
280 return bitmap->start;
283 blk_t ext2fs_get_block_bitmap_end(ext2fs_block_bitmap bitmap)
285 return bitmap->end;
288 ext2_ino_t ext2fs_get_inode_bitmap_end(ext2fs_inode_bitmap bitmap)
290 return bitmap->end;
293 int ext2fs_test_block_bitmap_range(ext2fs_block_bitmap bitmap,
294 blk_t block, int num)
296 int i;
298 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
299 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_TEST,
300 block, bitmap->description);
301 return 0;
303 for (i=0; i < num; i++) {
304 if (ext2fs_fast_test_block_bitmap(bitmap, block+i))
305 return 0;
307 return 1;
310 int ext2fs_fast_test_block_bitmap_range(ext2fs_block_bitmap bitmap,
311 blk_t block, int num)
313 int i;
315 for (i=0; i < num; i++) {
316 if (ext2fs_fast_test_block_bitmap(bitmap, block+i))
317 return 0;
319 return 1;
322 void ext2fs_mark_block_bitmap_range(ext2fs_block_bitmap bitmap,
323 blk_t block, int num)
325 int i;
327 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
328 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_MARK, block,
329 bitmap->description);
330 return;
332 for (i=0; i < num; i++)
333 ext2fs_set_bit(block + i - bitmap->start, bitmap->bitmap);
336 void ext2fs_fast_mark_block_bitmap_range(ext2fs_block_bitmap bitmap,
337 blk_t block, int num)
339 int i;
341 for (i=0; i < num; i++)
342 ext2fs_set_bit(block + i - bitmap->start, bitmap->bitmap);
345 void ext2fs_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap,
346 blk_t block, int num)
348 int i;
350 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
351 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_UNMARK, block,
352 bitmap->description);
353 return;
355 for (i=0; i < num; i++)
356 ext2fs_clear_bit(block + i - bitmap->start, bitmap->bitmap);
359 void ext2fs_fast_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap,
360 blk_t block, int num)
362 int i;
363 for (i=0; i < num; i++)
364 ext2fs_clear_bit(block + i - bitmap->start, bitmap->bitmap);