blob: 07805b10d8bd7e998a88d5a95d1e21e326e65578 [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{
Mike Frysinger7ad97802005-09-25 05:18:04 +0000241 ext2fs_set_bit(block - bitmap->start, bitmap->bitmap);
242}
243
244void ext2fs_fast_unmark_block_bitmap(ext2fs_block_bitmap bitmap,
245 blk_t block)
246{
Mike Frysinger7ad97802005-09-25 05:18:04 +0000247 ext2fs_clear_bit(block - bitmap->start, bitmap->bitmap);
248}
249
250int ext2fs_fast_test_block_bitmap(ext2fs_block_bitmap bitmap,
251 blk_t block)
252{
Mike Frysinger7ad97802005-09-25 05:18:04 +0000253 return ext2fs_test_bit(block - bitmap->start, bitmap->bitmap);
254}
255
256void ext2fs_fast_mark_inode_bitmap(ext2fs_inode_bitmap bitmap,
257 ext2_ino_t inode)
258{
Mike Frysinger7ad97802005-09-25 05:18:04 +0000259 ext2fs_set_bit(inode - bitmap->start, bitmap->bitmap);
260}
261
262void ext2fs_fast_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap,
263 ext2_ino_t inode)
264{
Mike Frysinger7ad97802005-09-25 05:18:04 +0000265 ext2fs_clear_bit(inode - bitmap->start, bitmap->bitmap);
266}
267
268int ext2fs_fast_test_inode_bitmap(ext2fs_inode_bitmap bitmap,
269 ext2_ino_t inode)
270{
Mike Frysinger7ad97802005-09-25 05:18:04 +0000271 return ext2fs_test_bit(inode - bitmap->start, bitmap->bitmap);
272}
273
274blk_t ext2fs_get_block_bitmap_start(ext2fs_block_bitmap bitmap)
275{
276 return bitmap->start;
277}
278
279ext2_ino_t ext2fs_get_inode_bitmap_start(ext2fs_inode_bitmap bitmap)
280{
281 return bitmap->start;
282}
283
284blk_t ext2fs_get_block_bitmap_end(ext2fs_block_bitmap bitmap)
285{
286 return bitmap->end;
287}
288
289ext2_ino_t ext2fs_get_inode_bitmap_end(ext2fs_inode_bitmap bitmap)
290{
291 return bitmap->end;
292}
293
294int ext2fs_test_block_bitmap_range(ext2fs_block_bitmap bitmap,
295 blk_t block, int num)
296{
297 int i;
298
299 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
300 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_TEST,
301 block, bitmap->description);
302 return 0;
303 }
304 for (i=0; i < num; i++) {
305 if (ext2fs_fast_test_block_bitmap(bitmap, block+i))
306 return 0;
307 }
308 return 1;
309}
310
311int ext2fs_fast_test_block_bitmap_range(ext2fs_block_bitmap bitmap,
312 blk_t block, int num)
313{
314 int i;
315
Mike Frysinger7ad97802005-09-25 05:18:04 +0000316 for (i=0; i < num; i++) {
317 if (ext2fs_fast_test_block_bitmap(bitmap, block+i))
318 return 0;
319 }
320 return 1;
321}
322
323void ext2fs_mark_block_bitmap_range(ext2fs_block_bitmap bitmap,
324 blk_t block, int num)
325{
326 int i;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000327
Mike Frysinger7ad97802005-09-25 05:18:04 +0000328 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
329 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_MARK, block,
330 bitmap->description);
331 return;
332 }
333 for (i=0; i < num; i++)
334 ext2fs_set_bit(block + i - bitmap->start, bitmap->bitmap);
335}
336
337void ext2fs_fast_mark_block_bitmap_range(ext2fs_block_bitmap bitmap,
338 blk_t block, int num)
339{
340 int i;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000341
Mike Frysinger7ad97802005-09-25 05:18:04 +0000342 for (i=0; i < num; i++)
343 ext2fs_set_bit(block + i - bitmap->start, bitmap->bitmap);
344}
345
346void ext2fs_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap,
347 blk_t block, int num)
348{
349 int i;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000350
Mike Frysinger7ad97802005-09-25 05:18:04 +0000351 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
352 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_UNMARK, block,
353 bitmap->description);
354 return;
355 }
356 for (i=0; i < num; i++)
357 ext2fs_clear_bit(block + i - bitmap->start, bitmap->bitmap);
358}
359
360void ext2fs_fast_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap,
361 blk_t block, int num)
362{
363 int i;
Mike Frysinger7ad97802005-09-25 05:18:04 +0000364 for (i=0; i < num; i++)
365 ext2fs_clear_bit(block + i - bitmap->start, bitmap->bitmap);
366}