blob: da1cf5be5d9bd5f1d447200250974a5f2136e383 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger7ad97802005-09-25 05:18:04 +00002/*
3 * ext2fs.h --- ext2fs
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00004 *
Mike Frysinger7ad97802005-09-25 05:18:04 +00005 * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
6 *
7 * %Begin-Header%
8 * This file may be redistributed under the terms of the GNU Public
9 * License.
10 * %End-Header%
11 */
12
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +000013#include "ext2fs.h"
14#include "bitops.h"
Rob Landley43ac8882006-04-01 00:40:33 +000015#include <string.h>
Mike Frysinger7ad97802005-09-25 05:18:04 +000016
17/*
18 * Allocate memory
19 */
20errcode_t ext2fs_get_mem(unsigned long size, void *ptr)
21{
22 void **pp = (void **)ptr;
23
24 *pp = malloc(size);
25 if (!*pp)
26 return EXT2_ET_NO_MEMORY;
27 return 0;
28}
29
30/*
31 * Free memory
32 */
33errcode_t ext2fs_free_mem(void *ptr)
34{
35 void **pp = (void **)ptr;
36
37 free(*pp);
38 *pp = 0;
39 return 0;
40}
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000041
Mike Frysinger7ad97802005-09-25 05:18:04 +000042/*
43 * Resize memory
44 */
45errcode_t ext2fs_resize_mem(unsigned long EXT2FS_ATTR((unused)) old_size,
46 unsigned long size, void *ptr)
47{
48 void *p;
Mike Frysinger7ad97802005-09-25 05:18:04 +000049
Mike Frysinger874af852006-03-08 07:03:27 +000050 /* Use "memcpy" for pointer assignments here to avoid problems
51 * with C99 strict type aliasing rules. */
52 memcpy(&p, ptr, sizeof (p));
53 p = realloc(p, size);
Mike Frysinger7ad97802005-09-25 05:18:04 +000054 if (!p)
55 return EXT2_ET_NO_MEMORY;
Mike Frysinger874af852006-03-08 07:03:27 +000056 memcpy(ptr, &p, sizeof (p));
Mike Frysinger7ad97802005-09-25 05:18:04 +000057 return 0;
58}
59
60/*
61 * Mark a filesystem superblock as dirty
62 */
63void ext2fs_mark_super_dirty(ext2_filsys fs)
64{
65 fs->flags |= EXT2_FLAG_DIRTY | EXT2_FLAG_CHANGED;
66}
67
68/*
69 * Mark a filesystem as changed
70 */
71void ext2fs_mark_changed(ext2_filsys fs)
72{
73 fs->flags |= EXT2_FLAG_CHANGED;
74}
75
76/*
77 * Check to see if a filesystem has changed
78 */
79int ext2fs_test_changed(ext2_filsys fs)
80{
81 return (fs->flags & EXT2_FLAG_CHANGED);
82}
83
84/*
85 * Mark a filesystem as valid
86 */
87void ext2fs_mark_valid(ext2_filsys fs)
88{
89 fs->flags |= EXT2_FLAG_VALID;
90}
91
92/*
93 * Mark a filesystem as NOT valid
94 */
95void ext2fs_unmark_valid(ext2_filsys fs)
96{
97 fs->flags &= ~EXT2_FLAG_VALID;
98}
99
100/*
101 * Check to see if a filesystem is valid
102 */
103int ext2fs_test_valid(ext2_filsys fs)
104{
105 return (fs->flags & EXT2_FLAG_VALID);
106}
107
108/*
109 * Mark the inode bitmap as dirty
110 */
111void ext2fs_mark_ib_dirty(ext2_filsys fs)
112{
113 fs->flags |= EXT2_FLAG_IB_DIRTY | EXT2_FLAG_CHANGED;
114}
115
116/*
117 * Mark the block bitmap as dirty
118 */
119void ext2fs_mark_bb_dirty(ext2_filsys fs)
120{
121 fs->flags |= EXT2_FLAG_BB_DIRTY | EXT2_FLAG_CHANGED;
122}
123
124/*
125 * Check to see if a filesystem's inode bitmap is dirty
126 */
127int ext2fs_test_ib_dirty(ext2_filsys fs)
128{
129 return (fs->flags & EXT2_FLAG_IB_DIRTY);
130}
131
132/*
133 * Check to see if a filesystem's block bitmap is dirty
134 */
135int ext2fs_test_bb_dirty(ext2_filsys fs)
136{
137 return (fs->flags & EXT2_FLAG_BB_DIRTY);
138}
139
140/*
141 * Return the group # of a block
142 */
143int ext2fs_group_of_blk(ext2_filsys fs, blk_t blk)
144{
145 return (blk - fs->super->s_first_data_block) /
146 fs->super->s_blocks_per_group;
147}
148
149/*
150 * Return the group # of an inode number
151 */
152int ext2fs_group_of_ino(ext2_filsys fs, ext2_ino_t ino)
153{
154 return (ino - 1) / fs->super->s_inodes_per_group;
155}
156
157blk_t ext2fs_inode_data_blocks(ext2_filsys fs,
158 struct ext2_inode *inode)
159{
160 return inode->i_blocks -
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000161 (inode->i_file_acl ? fs->blocksize >> 9 : 0);
Mike Frysinger7ad97802005-09-25 05:18:04 +0000162}
163
164
165
166
167
168
169
170
171
172__u16 ext2fs_swab16(__u16 val)
173{
174 return (val >> 8) | (val << 8);
175}
176
177__u32 ext2fs_swab32(__u32 val)
178{
179 return ((val>>24) | ((val>>8)&0xFF00) |
180 ((val<<8)&0xFF0000) | (val<<24));
181}
182
Mike Frysinger7ad97802005-09-25 05:18:04 +0000183int ext2fs_test_generic_bitmap(ext2fs_generic_bitmap bitmap,
184 blk_t bitno);
185
186int ext2fs_test_generic_bitmap(ext2fs_generic_bitmap bitmap,
187 blk_t bitno)
188{
189 if ((bitno < bitmap->start) || (bitno > bitmap->end)) {
190 ext2fs_warn_bitmap2(bitmap, EXT2FS_TEST_ERROR, bitno);
191 return 0;
192 }
193 return ext2fs_test_bit(bitno - bitmap->start, bitmap->bitmap);
194}
195
196int ext2fs_mark_block_bitmap(ext2fs_block_bitmap bitmap,
197 blk_t block)
198{
199 return ext2fs_mark_generic_bitmap((ext2fs_generic_bitmap)
200 bitmap,
201 block);
202}
203
204int ext2fs_unmark_block_bitmap(ext2fs_block_bitmap bitmap,
205 blk_t block)
206{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000207 return ext2fs_unmark_generic_bitmap((ext2fs_generic_bitmap) bitmap,
Mike Frysinger7ad97802005-09-25 05:18:04 +0000208 block);
209}
210
211int ext2fs_test_block_bitmap(ext2fs_block_bitmap bitmap,
212 blk_t block)
213{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000214 return ext2fs_test_generic_bitmap((ext2fs_generic_bitmap) bitmap,
Mike Frysinger7ad97802005-09-25 05:18:04 +0000215 block);
216}
217
218int ext2fs_mark_inode_bitmap(ext2fs_inode_bitmap bitmap,
219 ext2_ino_t inode)
220{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000221 return ext2fs_mark_generic_bitmap((ext2fs_generic_bitmap) bitmap,
Mike Frysinger7ad97802005-09-25 05:18:04 +0000222 inode);
223}
224
225int ext2fs_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap,
226 ext2_ino_t inode)
227{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000228 return ext2fs_unmark_generic_bitmap((ext2fs_generic_bitmap) bitmap,
Mike Frysinger7ad97802005-09-25 05:18:04 +0000229 inode);
230}
231
232int ext2fs_test_inode_bitmap(ext2fs_inode_bitmap bitmap,
233 ext2_ino_t inode)
234{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000235 return ext2fs_test_generic_bitmap((ext2fs_generic_bitmap) bitmap,
Mike Frysinger7ad97802005-09-25 05:18:04 +0000236 inode);
237}
238
239void ext2fs_fast_mark_block_bitmap(ext2fs_block_bitmap bitmap,
240 blk_t block)
241{
Mike Frysinger7ad97802005-09-25 05:18:04 +0000242 ext2fs_set_bit(block - bitmap->start, bitmap->bitmap);
243}
244
245void ext2fs_fast_unmark_block_bitmap(ext2fs_block_bitmap bitmap,
246 blk_t block)
247{
Mike Frysinger7ad97802005-09-25 05:18:04 +0000248 ext2fs_clear_bit(block - bitmap->start, bitmap->bitmap);
249}
250
251int ext2fs_fast_test_block_bitmap(ext2fs_block_bitmap bitmap,
252 blk_t block)
253{
Mike Frysinger7ad97802005-09-25 05:18:04 +0000254 return ext2fs_test_bit(block - bitmap->start, bitmap->bitmap);
255}
256
257void ext2fs_fast_mark_inode_bitmap(ext2fs_inode_bitmap bitmap,
258 ext2_ino_t inode)
259{
Mike Frysinger7ad97802005-09-25 05:18:04 +0000260 ext2fs_set_bit(inode - bitmap->start, bitmap->bitmap);
261}
262
263void ext2fs_fast_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap,
264 ext2_ino_t inode)
265{
Mike Frysinger7ad97802005-09-25 05:18:04 +0000266 ext2fs_clear_bit(inode - bitmap->start, bitmap->bitmap);
267}
268
269int ext2fs_fast_test_inode_bitmap(ext2fs_inode_bitmap bitmap,
270 ext2_ino_t inode)
271{
Mike Frysinger7ad97802005-09-25 05:18:04 +0000272 return ext2fs_test_bit(inode - bitmap->start, bitmap->bitmap);
273}
274
275blk_t ext2fs_get_block_bitmap_start(ext2fs_block_bitmap bitmap)
276{
277 return bitmap->start;
278}
279
280ext2_ino_t ext2fs_get_inode_bitmap_start(ext2fs_inode_bitmap bitmap)
281{
282 return bitmap->start;
283}
284
285blk_t ext2fs_get_block_bitmap_end(ext2fs_block_bitmap bitmap)
286{
287 return bitmap->end;
288}
289
290ext2_ino_t ext2fs_get_inode_bitmap_end(ext2fs_inode_bitmap bitmap)
291{
292 return bitmap->end;
293}
294
295int ext2fs_test_block_bitmap_range(ext2fs_block_bitmap bitmap,
296 blk_t block, int num)
297{
298 int i;
299
300 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
301 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_TEST,
302 block, bitmap->description);
303 return 0;
304 }
305 for (i=0; i < num; i++) {
306 if (ext2fs_fast_test_block_bitmap(bitmap, block+i))
307 return 0;
308 }
309 return 1;
310}
311
312int ext2fs_fast_test_block_bitmap_range(ext2fs_block_bitmap bitmap,
313 blk_t block, int num)
314{
315 int i;
316
Mike Frysinger7ad97802005-09-25 05:18:04 +0000317 for (i=0; i < num; i++) {
318 if (ext2fs_fast_test_block_bitmap(bitmap, block+i))
319 return 0;
320 }
321 return 1;
322}
323
324void ext2fs_mark_block_bitmap_range(ext2fs_block_bitmap bitmap,
325 blk_t block, int num)
326{
327 int i;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000328
Mike Frysinger7ad97802005-09-25 05:18:04 +0000329 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
330 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_MARK, block,
331 bitmap->description);
332 return;
333 }
334 for (i=0; i < num; i++)
335 ext2fs_set_bit(block + i - bitmap->start, bitmap->bitmap);
336}
337
338void ext2fs_fast_mark_block_bitmap_range(ext2fs_block_bitmap bitmap,
339 blk_t block, int num)
340{
341 int i;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000342
Mike Frysinger7ad97802005-09-25 05:18:04 +0000343 for (i=0; i < num; i++)
344 ext2fs_set_bit(block + i - bitmap->start, bitmap->bitmap);
345}
346
347void ext2fs_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap,
348 blk_t block, int num)
349{
350 int i;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000351
Mike Frysinger7ad97802005-09-25 05:18:04 +0000352 if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
353 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_UNMARK, block,
354 bitmap->description);
355 return;
356 }
357 for (i=0; i < num; i++)
358 ext2fs_clear_bit(block + i - bitmap->start, bitmap->bitmap);
359}
360
361void ext2fs_fast_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap,
362 blk_t block, int num)
363{
364 int i;
Mike Frysinger7ad97802005-09-25 05:18:04 +0000365 for (i=0; i < num; i++)
366 ext2fs_clear_bit(block + i - bitmap->start, bitmap->bitmap);
367}