blob: 1202b35268a3da96b437c407883ef8cc2bda9f91 [file] [log] [blame]
Mike Frysinger7ad97802005-09-25 05:18:04 +00001/*
2 * ext2fs.h --- ext2fs
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00003 *
Mike Frysinger7ad97802005-09-25 05:18:04 +00004 * Copyright (C) 1993, 1994, 1995, 1996 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
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +000012#include "ext2fs.h"
13#include "bitops.h"
Rob Landley43ac8882006-04-01 00:40:33 +000014#include <string.h>
Mike Frysinger7ad97802005-09-25 05:18:04 +000015
16/*
17 * Allocate memory
18 */
19errcode_t ext2fs_get_mem(unsigned long size, void *ptr)
20{
21 void **pp = (void **)ptr;
22
23 *pp = malloc(size);
24 if (!*pp)
25 return EXT2_ET_NO_MEMORY;
26 return 0;
27}
28
29/*
30 * Free memory
31 */
32errcode_t ext2fs_free_mem(void *ptr)
33{
34 void **pp = (void **)ptr;
35
36 free(*pp);
37 *pp = 0;
38 return 0;
39}
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000040
Mike Frysinger7ad97802005-09-25 05:18:04 +000041/*
42 * Resize memory
43 */
44errcode_t ext2fs_resize_mem(unsigned long EXT2FS_ATTR((unused)) old_size,
45 unsigned long size, void *ptr)
46{
47 void *p;
Mike Frysinger7ad97802005-09-25 05:18:04 +000048
Mike Frysinger874af852006-03-08 07:03:27 +000049 /* Use "memcpy" for pointer assignments here to avoid problems
50 * with C99 strict type aliasing rules. */
51 memcpy(&p, ptr, sizeof (p));
52 p = realloc(p, size);
Mike Frysinger7ad97802005-09-25 05:18:04 +000053 if (!p)
54 return EXT2_ET_NO_MEMORY;
Mike Frysinger874af852006-03-08 07:03:27 +000055 memcpy(ptr, &p, sizeof (p));
Mike Frysinger7ad97802005-09-25 05:18:04 +000056 return 0;
57}
58
59/*
60 * Mark a filesystem superblock as dirty
61 */
62void ext2fs_mark_super_dirty(ext2_filsys fs)
63{
64 fs->flags |= EXT2_FLAG_DIRTY | EXT2_FLAG_CHANGED;
65}
66
67/*
68 * Mark a filesystem as changed
69 */
70void ext2fs_mark_changed(ext2_filsys fs)
71{
72 fs->flags |= EXT2_FLAG_CHANGED;
73}
74
75/*
76 * Check to see if a filesystem has changed
77 */
78int ext2fs_test_changed(ext2_filsys fs)
79{
80 return (fs->flags & EXT2_FLAG_CHANGED);
81}
82
83/*
84 * Mark a filesystem as valid
85 */
86void ext2fs_mark_valid(ext2_filsys fs)
87{
88 fs->flags |= EXT2_FLAG_VALID;
89}
90
91/*
92 * Mark a filesystem as NOT valid
93 */
94void ext2fs_unmark_valid(ext2_filsys fs)
95{
96 fs->flags &= ~EXT2_FLAG_VALID;
97}
98
99/*
100 * Check to see if a filesystem is valid
101 */
102int ext2fs_test_valid(ext2_filsys fs)
103{
104 return (fs->flags & EXT2_FLAG_VALID);
105}
106
107/*
108 * Mark the inode bitmap as dirty
109 */
110void ext2fs_mark_ib_dirty(ext2_filsys fs)
111{
112 fs->flags |= EXT2_FLAG_IB_DIRTY | EXT2_FLAG_CHANGED;
113}
114
115/*
116 * Mark the block bitmap as dirty
117 */
118void ext2fs_mark_bb_dirty(ext2_filsys fs)
119{
120 fs->flags |= EXT2_FLAG_BB_DIRTY | EXT2_FLAG_CHANGED;
121}
122
123/*
124 * Check to see if a filesystem's inode bitmap is dirty
125 */
126int ext2fs_test_ib_dirty(ext2_filsys fs)
127{
128 return (fs->flags & EXT2_FLAG_IB_DIRTY);
129}
130
131/*
132 * Check to see if a filesystem's block bitmap is dirty
133 */
134int ext2fs_test_bb_dirty(ext2_filsys fs)
135{
136 return (fs->flags & EXT2_FLAG_BB_DIRTY);
137}
138
139/*
140 * Return the group # of a block
141 */
142int ext2fs_group_of_blk(ext2_filsys fs, blk_t blk)
143{
144 return (blk - fs->super->s_first_data_block) /
145 fs->super->s_blocks_per_group;
146}
147
148/*
149 * Return the group # of an inode number
150 */
151int ext2fs_group_of_ino(ext2_filsys fs, ext2_ino_t ino)
152{
153 return (ino - 1) / fs->super->s_inodes_per_group;
154}
155
156blk_t ext2fs_inode_data_blocks(ext2_filsys fs,
157 struct ext2_inode *inode)
158{
159 return inode->i_blocks -
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000160 (inode->i_file_acl ? fs->blocksize >> 9 : 0);
Mike Frysinger7ad97802005-09-25 05:18:04 +0000161}
162
163
164
165
166
167
168
169
170
171__u16 ext2fs_swab16(__u16 val)
172{
173 return (val >> 8) | (val << 8);
174}
175
176__u32 ext2fs_swab32(__u32 val)
177{
178 return ((val>>24) | ((val>>8)&0xFF00) |
179 ((val<<8)&0xFF0000) | (val<<24));
180}
181
Mike Frysinger7ad97802005-09-25 05:18:04 +0000182int ext2fs_test_generic_bitmap(ext2fs_generic_bitmap bitmap,
183 blk_t bitno);
184
185int ext2fs_test_generic_bitmap(ext2fs_generic_bitmap bitmap,
186 blk_t bitno)
187{
188 if ((bitno < bitmap->start) || (bitno > bitmap->end)) {
189 ext2fs_warn_bitmap2(bitmap, EXT2FS_TEST_ERROR, bitno);
190 return 0;
191 }
192 return ext2fs_test_bit(bitno - bitmap->start, bitmap->bitmap);
193}
194
195int ext2fs_mark_block_bitmap(ext2fs_block_bitmap bitmap,
196 blk_t block)
197{
198 return ext2fs_mark_generic_bitmap((ext2fs_generic_bitmap)
199 bitmap,
200 block);
201}
202
203int ext2fs_unmark_block_bitmap(ext2fs_block_bitmap bitmap,
204 blk_t block)
205{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000206 return ext2fs_unmark_generic_bitmap((ext2fs_generic_bitmap) bitmap,
Mike Frysinger7ad97802005-09-25 05:18:04 +0000207 block);
208}
209
210int ext2fs_test_block_bitmap(ext2fs_block_bitmap bitmap,
211 blk_t block)
212{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000213 return ext2fs_test_generic_bitmap((ext2fs_generic_bitmap) bitmap,
Mike Frysinger7ad97802005-09-25 05:18:04 +0000214 block);
215}
216
217int ext2fs_mark_inode_bitmap(ext2fs_inode_bitmap bitmap,
218 ext2_ino_t inode)
219{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000220 return ext2fs_mark_generic_bitmap((ext2fs_generic_bitmap) bitmap,
Mike Frysinger7ad97802005-09-25 05:18:04 +0000221 inode);
222}
223
224int ext2fs_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap,
225 ext2_ino_t inode)
226{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000227 return ext2fs_unmark_generic_bitmap((ext2fs_generic_bitmap) bitmap,
Mike Frysinger7ad97802005-09-25 05:18:04 +0000228 inode);
229}
230
231int ext2fs_test_inode_bitmap(ext2fs_inode_bitmap bitmap,
232 ext2_ino_t inode)
233{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000234 return ext2fs_test_generic_bitmap((ext2fs_generic_bitmap) bitmap,
Mike Frysinger7ad97802005-09-25 05:18:04 +0000235 inode);
236}
237
238void ext2fs_fast_mark_block_bitmap(ext2fs_block_bitmap bitmap,
239 blk_t block)
240{
241#ifdef EXT2FS_DEBUG_FAST_OPS
242 if ((block < bitmap->start) || (block > bitmap->end)) {
243 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_MARK, block,
244 bitmap->description);
245 return;
246 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000247#endif
Mike Frysinger7ad97802005-09-25 05:18:04 +0000248 ext2fs_set_bit(block - bitmap->start, bitmap->bitmap);
249}
250
251void ext2fs_fast_unmark_block_bitmap(ext2fs_block_bitmap bitmap,
252 blk_t block)
253{
254#ifdef EXT2FS_DEBUG_FAST_OPS
255 if ((block < bitmap->start) || (block > bitmap->end)) {
256 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_UNMARK,
257 block, bitmap->description);
258 return;
259 }
260#endif
261 ext2fs_clear_bit(block - bitmap->start, bitmap->bitmap);
262}
263
264int ext2fs_fast_test_block_bitmap(ext2fs_block_bitmap bitmap,
265 blk_t block)
266{
267#ifdef EXT2FS_DEBUG_FAST_OPS
268 if ((block < bitmap->start) || (block > bitmap->end)) {
269 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_TEST,
270 block, bitmap->description);
271 return 0;
272 }
273#endif
274 return ext2fs_test_bit(block - bitmap->start, bitmap->bitmap);
275}
276
277void ext2fs_fast_mark_inode_bitmap(ext2fs_inode_bitmap bitmap,
278 ext2_ino_t inode)
279{
280#ifdef EXT2FS_DEBUG_FAST_OPS
281 if ((inode < bitmap->start) || (inode > bitmap->end)) {
282 ext2fs_warn_bitmap(EXT2_ET_BAD_INODE_MARK,
283 inode, bitmap->description);
284 return;
285 }
286#endif
287 ext2fs_set_bit(inode - bitmap->start, bitmap->bitmap);
288}
289
290void ext2fs_fast_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap,
291 ext2_ino_t inode)
292{
293#ifdef EXT2FS_DEBUG_FAST_OPS
294 if ((inode < bitmap->start) || (inode > bitmap->end)) {
295 ext2fs_warn_bitmap(EXT2_ET_BAD_INODE_UNMARK,
296 inode, bitmap->description);
297 return;
298 }
299#endif
300 ext2fs_clear_bit(inode - bitmap->start, bitmap->bitmap);
301}
302
303int ext2fs_fast_test_inode_bitmap(ext2fs_inode_bitmap bitmap,
304 ext2_ino_t inode)
305{
306#ifdef EXT2FS_DEBUG_FAST_OPS
307 if ((inode < bitmap->start) || (inode > bitmap->end)) {
308 ext2fs_warn_bitmap(EXT2_ET_BAD_INODE_TEST,
309 inode, bitmap->description);
310 return 0;
311 }
312#endif
313 return ext2fs_test_bit(inode - bitmap->start, bitmap->bitmap);
314}
315
316blk_t ext2fs_get_block_bitmap_start(ext2fs_block_bitmap bitmap)
317{
318 return bitmap->start;
319}
320
321ext2_ino_t ext2fs_get_inode_bitmap_start(ext2fs_inode_bitmap bitmap)
322{
323 return bitmap->start;
324}
325
326blk_t ext2fs_get_block_bitmap_end(ext2fs_block_bitmap bitmap)
327{
328 return bitmap->end;
329}
330
331ext2_ino_t ext2fs_get_inode_bitmap_end(ext2fs_inode_bitmap bitmap)
332{
333 return bitmap->end;
334}
335
336int ext2fs_test_block_bitmap_range(ext2fs_block_bitmap bitmap,
337 blk_t block, int num)
338{
339 int i;
340
341 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
342 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_TEST,
343 block, bitmap->description);
344 return 0;
345 }
346 for (i=0; i < num; i++) {
347 if (ext2fs_fast_test_block_bitmap(bitmap, block+i))
348 return 0;
349 }
350 return 1;
351}
352
353int ext2fs_fast_test_block_bitmap_range(ext2fs_block_bitmap bitmap,
354 blk_t block, int num)
355{
356 int i;
357
358#ifdef EXT2FS_DEBUG_FAST_OPS
359 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
360 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_TEST,
361 block, bitmap->description);
362 return 0;
363 }
364#endif
365 for (i=0; i < num; i++) {
366 if (ext2fs_fast_test_block_bitmap(bitmap, block+i))
367 return 0;
368 }
369 return 1;
370}
371
372void ext2fs_mark_block_bitmap_range(ext2fs_block_bitmap bitmap,
373 blk_t block, int num)
374{
375 int i;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000376
Mike Frysinger7ad97802005-09-25 05:18:04 +0000377 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
378 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_MARK, block,
379 bitmap->description);
380 return;
381 }
382 for (i=0; i < num; i++)
383 ext2fs_set_bit(block + i - bitmap->start, bitmap->bitmap);
384}
385
386void ext2fs_fast_mark_block_bitmap_range(ext2fs_block_bitmap bitmap,
387 blk_t block, int num)
388{
389 int i;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000390
Mike Frysinger7ad97802005-09-25 05:18:04 +0000391#ifdef EXT2FS_DEBUG_FAST_OPS
392 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
393 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_MARK, block,
394 bitmap->description);
395 return;
396 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000397#endif
Mike Frysinger7ad97802005-09-25 05:18:04 +0000398 for (i=0; i < num; i++)
399 ext2fs_set_bit(block + i - bitmap->start, bitmap->bitmap);
400}
401
402void ext2fs_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap,
403 blk_t block, int num)
404{
405 int i;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000406
Mike Frysinger7ad97802005-09-25 05:18:04 +0000407 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
408 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_UNMARK, block,
409 bitmap->description);
410 return;
411 }
412 for (i=0; i < num; i++)
413 ext2fs_clear_bit(block + i - bitmap->start, bitmap->bitmap);
414}
415
416void ext2fs_fast_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap,
417 blk_t block, int num)
418{
419 int i;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000420
Mike Frysinger7ad97802005-09-25 05:18:04 +0000421#ifdef EXT2FS_DEBUG_FAST_OPS
422 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
423 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_UNMARK, block,
424 bitmap->description);
425 return;
426 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000427#endif
Mike Frysinger7ad97802005-09-25 05:18:04 +0000428 for (i=0; i < num; i++)
429 ext2fs_clear_bit(block + i - bitmap->start, bitmap->bitmap);
430}