blob: 7d37d232d8c5e44215fb8a73a644da6c0c311dbc [file] [log] [blame]
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001/* vi: set sw=4 ts=4: */
2/*
3 * ext2fs.h --- ext2fs
4 *
5 * 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
13#include "ext2fs.h"
14#include "bitops.h"
15#include <string.h>
16
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}
41
42/*
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;
49
50 /* Use "memcpy" for pointer assignments here to avoid problems
51 * with C99 strict type aliasing rules. */
52 memcpy(&p, ptr, sizeof (p));
Denys Vlasenko90377872010-01-08 09:07:50 +010053 p = xrealloc(p, size);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000054 memcpy(ptr, &p, sizeof (p));
55 return 0;
56}
57
58/*
59 * Mark a filesystem superblock as dirty
60 */
61void ext2fs_mark_super_dirty(ext2_filsys fs)
62{
63 fs->flags |= EXT2_FLAG_DIRTY | EXT2_FLAG_CHANGED;
64}
65
66/*
67 * Mark a filesystem as changed
68 */
69void ext2fs_mark_changed(ext2_filsys fs)
70{
71 fs->flags |= EXT2_FLAG_CHANGED;
72}
73
74/*
75 * Check to see if a filesystem has changed
76 */
77int ext2fs_test_changed(ext2_filsys fs)
78{
79 return (fs->flags & EXT2_FLAG_CHANGED);
80}
81
82/*
83 * Mark a filesystem as valid
84 */
85void ext2fs_mark_valid(ext2_filsys fs)
86{
87 fs->flags |= EXT2_FLAG_VALID;
88}
89
90/*
91 * Mark a filesystem as NOT valid
92 */
93void ext2fs_unmark_valid(ext2_filsys fs)
94{
95 fs->flags &= ~EXT2_FLAG_VALID;
96}
97
98/*
99 * Check to see if a filesystem is valid
100 */
101int ext2fs_test_valid(ext2_filsys fs)
102{
103 return (fs->flags & EXT2_FLAG_VALID);
104}
105
106/*
107 * Mark the inode bitmap as dirty
108 */
109void ext2fs_mark_ib_dirty(ext2_filsys fs)
110{
111 fs->flags |= EXT2_FLAG_IB_DIRTY | EXT2_FLAG_CHANGED;
112}
113
114/*
115 * Mark the block bitmap as dirty
116 */
117void ext2fs_mark_bb_dirty(ext2_filsys fs)
118{
119 fs->flags |= EXT2_FLAG_BB_DIRTY | EXT2_FLAG_CHANGED;
120}
121
122/*
123 * Check to see if a filesystem's inode bitmap is dirty
124 */
125int ext2fs_test_ib_dirty(ext2_filsys fs)
126{
127 return (fs->flags & EXT2_FLAG_IB_DIRTY);
128}
129
130/*
131 * Check to see if a filesystem's block bitmap is dirty
132 */
133int ext2fs_test_bb_dirty(ext2_filsys fs)
134{
135 return (fs->flags & EXT2_FLAG_BB_DIRTY);
136}
137
138/*
139 * Return the group # of a block
140 */
141int ext2fs_group_of_blk(ext2_filsys fs, blk_t blk)
142{
143 return (blk - fs->super->s_first_data_block) /
144 fs->super->s_blocks_per_group;
145}
146
147/*
148 * Return the group # of an inode number
149 */
150int ext2fs_group_of_ino(ext2_filsys fs, ext2_ino_t ino)
151{
152 return (ino - 1) / fs->super->s_inodes_per_group;
153}
154
155blk_t ext2fs_inode_data_blocks(ext2_filsys fs,
156 struct ext2_inode *inode)
157{
Denys Vlasenkob7c9fb22011-02-03 00:05:48 +0100158 return inode->i_blocks -
159 (inode->i_file_acl ? fs->blocksize >> 9 : 0);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000160}
161
162
163
164
165
166
167
168
169
170__u16 ext2fs_swab16(__u16 val)
171{
172 return (val >> 8) | (val << 8);
173}
174
175__u32 ext2fs_swab32(__u32 val)
176{
177 return ((val>>24) | ((val>>8)&0xFF00) |
178 ((val<<8)&0xFF0000) | (val<<24));
179}
180
181int ext2fs_test_generic_bitmap(ext2fs_generic_bitmap bitmap,
182 blk_t bitno);
183
184int ext2fs_test_generic_bitmap(ext2fs_generic_bitmap bitmap,
185 blk_t bitno)
186{
187 if ((bitno < bitmap->start) || (bitno > bitmap->end)) {
188 ext2fs_warn_bitmap2(bitmap, EXT2FS_TEST_ERROR, bitno);
189 return 0;
190 }
191 return ext2fs_test_bit(bitno - bitmap->start, bitmap->bitmap);
192}
193
194int ext2fs_mark_block_bitmap(ext2fs_block_bitmap bitmap,
195 blk_t block)
196{
197 return ext2fs_mark_generic_bitmap((ext2fs_generic_bitmap)
198 bitmap,
199 block);
200}
201
202int ext2fs_unmark_block_bitmap(ext2fs_block_bitmap bitmap,
203 blk_t block)
204{
205 return ext2fs_unmark_generic_bitmap((ext2fs_generic_bitmap) bitmap,
206 block);
207}
208
209int ext2fs_test_block_bitmap(ext2fs_block_bitmap bitmap,
210 blk_t block)
211{
212 return ext2fs_test_generic_bitmap((ext2fs_generic_bitmap) bitmap,
213 block);
214}
215
216int ext2fs_mark_inode_bitmap(ext2fs_inode_bitmap bitmap,
217 ext2_ino_t inode)
218{
219 return ext2fs_mark_generic_bitmap((ext2fs_generic_bitmap) bitmap,
220 inode);
221}
222
223int ext2fs_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap,
224 ext2_ino_t inode)
225{
226 return ext2fs_unmark_generic_bitmap((ext2fs_generic_bitmap) bitmap,
227 inode);
228}
229
230int ext2fs_test_inode_bitmap(ext2fs_inode_bitmap bitmap,
231 ext2_ino_t inode)
232{
233 return ext2fs_test_generic_bitmap((ext2fs_generic_bitmap) bitmap,
234 inode);
235}
236
237void ext2fs_fast_mark_block_bitmap(ext2fs_block_bitmap bitmap,
238 blk_t block)
239{
240 ext2fs_set_bit(block - bitmap->start, bitmap->bitmap);
241}
242
243void ext2fs_fast_unmark_block_bitmap(ext2fs_block_bitmap bitmap,
244 blk_t block)
245{
246 ext2fs_clear_bit(block - bitmap->start, bitmap->bitmap);
247}
248
249int ext2fs_fast_test_block_bitmap(ext2fs_block_bitmap bitmap,
250 blk_t block)
251{
252 return ext2fs_test_bit(block - bitmap->start, bitmap->bitmap);
253}
254
255void ext2fs_fast_mark_inode_bitmap(ext2fs_inode_bitmap bitmap,
256 ext2_ino_t inode)
257{
258 ext2fs_set_bit(inode - bitmap->start, bitmap->bitmap);
259}
260
261void ext2fs_fast_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap,
262 ext2_ino_t inode)
263{
264 ext2fs_clear_bit(inode - bitmap->start, bitmap->bitmap);
265}
266
267int ext2fs_fast_test_inode_bitmap(ext2fs_inode_bitmap bitmap,
268 ext2_ino_t inode)
269{
270 return ext2fs_test_bit(inode - bitmap->start, bitmap->bitmap);
271}
272
273blk_t ext2fs_get_block_bitmap_start(ext2fs_block_bitmap bitmap)
274{
275 return bitmap->start;
276}
277
278ext2_ino_t ext2fs_get_inode_bitmap_start(ext2fs_inode_bitmap bitmap)
279{
280 return bitmap->start;
281}
282
283blk_t ext2fs_get_block_bitmap_end(ext2fs_block_bitmap bitmap)
284{
285 return bitmap->end;
286}
287
288ext2_ino_t ext2fs_get_inode_bitmap_end(ext2fs_inode_bitmap bitmap)
289{
290 return bitmap->end;
291}
292
293int ext2fs_test_block_bitmap_range(ext2fs_block_bitmap bitmap,
294 blk_t block, int num)
295{
296 int i;
297
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;
302 }
303 for (i=0; i < num; i++) {
304 if (ext2fs_fast_test_block_bitmap(bitmap, block+i))
305 return 0;
306 }
307 return 1;
308}
309
310int ext2fs_fast_test_block_bitmap_range(ext2fs_block_bitmap bitmap,
311 blk_t block, int num)
312{
313 int i;
314
315 for (i=0; i < num; i++) {
316 if (ext2fs_fast_test_block_bitmap(bitmap, block+i))
317 return 0;
318 }
319 return 1;
320}
321
322void ext2fs_mark_block_bitmap_range(ext2fs_block_bitmap bitmap,
323 blk_t block, int num)
324{
325 int i;
326
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;
331 }
332 for (i=0; i < num; i++)
333 ext2fs_set_bit(block + i - bitmap->start, bitmap->bitmap);
334}
335
336void ext2fs_fast_mark_block_bitmap_range(ext2fs_block_bitmap bitmap,
337 blk_t block, int num)
338{
339 int i;
340
341 for (i=0; i < num; i++)
342 ext2fs_set_bit(block + i - bitmap->start, bitmap->bitmap);
343}
344
345void ext2fs_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap,
346 blk_t block, int num)
347{
348 int i;
349
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;
354 }
355 for (i=0; i < num; i++)
356 ext2fs_clear_bit(block + i - bitmap->start, bitmap->bitmap);
357}
358
359void ext2fs_fast_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap,
360 blk_t block, int num)
361{
362 int i;
363 for (i=0; i < num; i++)
364 ext2fs_clear_bit(block + i - bitmap->start, bitmap->bitmap);
365}