Whamcloud - gitweb
Many files:
[tools/e2fsprogs.git] / lib / ext2fs / ext2fs.h
1 /*
2  * ext2fs.h --- ext2fs
3  * 
4  * Copyright (C) 1993 Theodore Ts'o.  This file may be redistributed
5  * under the terms of the GNU Public License.
6  */
7
8 /*
9  * Where the master copy of the superblock is located, and how big
10  * superblocks are supposed to be.  We define SUPERBLOCK_SIZE because
11  * the size of the superblock structure is not necessarily trustworthy
12  * (some versions have the padding set up so that the superblock is
13  * 1032 bytes long).
14  */
15 #define SUPERBLOCK_OFFSET       1024
16 #define SUPERBLOCK_SIZE         1024
17
18 /*
19  * The last ext2fs revision level that this version of the library is
20  * able to support.
21  */
22 #define EXT2_LIB_CURRENT_REV    0
23
24 typedef unsigned long   blk_t;
25 typedef unsigned int    dgrp_t;
26
27 #include "et/com_err.h"
28 #include "ext2fs/io.h"
29 #include "ext2fs/ext2_err.h"
30
31 typedef struct struct_ext2_filsys *ext2_filsys;
32
33 struct ext2fs_struct_inode_bitmap {
34         int     magic;
35         ext2_filsys fs;
36         ino_t   start, end;
37         ino_t   real_end;
38         char    *description;
39         char    *bitmap;
40         int     reserved[8];
41 };
42
43 typedef struct ext2fs_struct_inode_bitmap *ext2fs_inode_bitmap;
44
45 struct ext2fs_struct_block_bitmap {
46         int     magic;
47         ext2_filsys fs;
48         blk_t   start, end;
49         ino_t   real_end;
50         char    *description;
51         char    *bitmap;
52         int     reserved[8];
53 };
54
55 typedef struct ext2fs_struct_block_bitmap *ext2fs_block_bitmap;
56
57 /*
58  * Flags for the ext2_filsys structure
59  */
60
61 #define EXT2_FLAG_RW            0x01
62 #define EXT2_FLAG_CHANGED       0x02
63 #define EXT2_FLAG_DIRTY         0x04
64 #define EXT2_FLAG_VALID         0x08
65 #define EXT2_FLAG_IB_DIRTY      0x10
66 #define EXT2_FLAG_BB_DIRTY      0x20
67
68 struct struct_ext2_filsys {
69         int                             magic;
70         io_channel                      io;
71         int                             flags;
72         char *                          device_name;
73         struct ext2_super_block *       super;
74         int                             blocksize;
75         int                             fragsize;
76         unsigned long                   group_desc_count;
77         unsigned long                   desc_blocks;
78         struct ext2_group_desc *        group_desc;
79         int                             inode_blocks_per_group;
80         ext2fs_inode_bitmap             inode_map;
81         ext2fs_block_bitmap             block_map;
82         errcode_t (*get_blocks)(ext2_filsys fs, ino_t ino, blk_t *blocks);
83         errcode_t (*check_directory)(ext2_filsys fs, ino_t ino);
84         errcode_t (*write_bitmaps)(ext2_filsys fs);
85         int                             reserved[16];
86
87         /*
88          * Not used by ext2fs library; reserved for the use of the
89          * calling application.
90          */
91         void *                          private; 
92 };
93
94 /*
95  * badblocks list definitions
96  */
97
98 typedef struct struct_badblocks_list *badblocks_list;
99
100 struct struct_badblocks_list {
101         int     magic;
102         int     num;
103         int     size;
104         blk_t   *list;
105         int     badblocks_flags;
106         int     reserved[8];
107 };
108
109 #define BADBLOCKS_FLAG_DIRTY    1
110
111 typedef struct struct_badblocks_iterate *badblocks_iterate;
112
113 struct struct_badblocks_iterate {
114         int             magic;
115         badblocks_list  bb;
116         int             ptr;
117         int     reserved[8];
118 };
119
120 #include "ext2fs/bitops.h"
121         
122 /*
123  * Return flags for the block iterator functions
124  */
125 #define BLOCK_CHANGED   1
126 #define BLOCK_ABORT     2
127 #define BLOCK_ERROR     4
128
129 /*
130  * Block interate flags
131  */
132 #define BLOCK_FLAG_APPEND       1
133 #define BLOCK_FLAG_DEPTH_TRAVERSE       2
134
135 /*
136  * Return flags for the directory iterator functions
137  */
138 #define DIRENT_CHANGED  1
139 #define DIRENT_ABORT    2
140 #define DIRENT_ERROR    3
141
142 /*
143  * Directory iterator flags
144  */
145
146 #define DIRENT_FLAG_INCLUDE_EMPTY       1
147
148 /*
149  * Inode scan definitions
150  */
151 typedef struct ext2_struct_inode_scan *ext2_inode_scan;
152
153 struct ext2_struct_inode_scan {
154         int                     magic;
155         ext2_filsys             fs;
156         ino_t                   current_inode;
157         blk_t                   current_block;
158         dgrp_t                  current_group;
159         int                     inodes_left, blocks_left, groups_left;
160         int                     inode_buffer_blocks;
161         char *                  inode_buffer;
162         struct ext2_inode *     inode_scan_ptr;
163         errcode_t               (*done_group)(ext2_filsys fs,
164                                               ext2_inode_scan scan,
165                                               dgrp_t group,
166                                               void * private);
167         void *                  done_group_data;
168         int                     reserved[8];
169 };
170
171 /*
172  * For checking structure magic numbers...
173  */
174
175 #define EXT2_CHECK_MAGIC(struct, code) \
176           if ((struct)->magic != (code)) return (code)
177   
178 /*
179  * function prototypes
180  */
181
182 /* alloc.c */
183 extern errcode_t ext2fs_new_inode(ext2_filsys fs, ino_t dir, int mode,
184                                   ext2fs_inode_bitmap map, ino_t *ret);
185 extern errcode_t ext2fs_new_block(ext2_filsys fs, blk_t goal,
186                                   ext2fs_block_bitmap map, blk_t *ret);
187 extern errcode_t ext2fs_get_free_blocks(ext2_filsys fs, blk_t start,
188                                         blk_t finish, int num,
189                                         ext2fs_block_bitmap map,
190                                         blk_t *ret);
191
192 /* badblocks.c */
193 extern errcode_t badblocks_list_create(badblocks_list *ret, int size);
194 extern void badblocks_list_free(badblocks_list bb);
195 extern errcode_t badblocks_list_add(badblocks_list bb, blk_t blk);
196 extern int badblocks_list_test(badblocks_list bb, blk_t blk);
197 extern errcode_t badblocks_list_iterate_begin(badblocks_list bb,
198                                               badblocks_iterate *ret);
199 extern int badblocks_list_iterate(badblocks_iterate iter, blk_t *blk);
200 extern void badblocks_list_iterate_end(badblocks_iterate iter);
201
202 /* bb_inode.c */
203 extern errcode_t ext2fs_update_bb_inode(ext2_filsys fs,
204                                         badblocks_list bb_list);
205
206 /* bitmaps.c */
207 extern errcode_t ext2fs_write_inode_bitmap(ext2_filsys fs);
208 extern errcode_t ext2fs_write_block_bitmap (ext2_filsys fs);
209 extern errcode_t ext2fs_read_inode_bitmap (ext2_filsys fs);
210 extern errcode_t ext2fs_read_block_bitmap(ext2_filsys fs);
211 errcode_t ext2fs_allocate_block_bitmap(ext2_filsys fs,
212                                        const char *descr,
213                                        ext2fs_block_bitmap *ret);
214 errcode_t ext2fs_allocate_inode_bitmap(ext2_filsys fs,
215                                        const char *descr,
216                                        ext2fs_inode_bitmap *ret);
217 errcode_t ext2fs_fudge_inode_bitmap_end(ext2fs_inode_bitmap bitmap,
218                                         ino_t end, ino_t *oend);
219 errcode_t ext2fs_fudge_block_bitmap_end(ext2fs_block_bitmap bitmap,
220                                         blk_t end, blk_t *oend);
221 void ext2fs_clear_inode_bitmap(ext2fs_inode_bitmap bitmap);
222 void ext2fs_clear_block_bitmap(ext2fs_block_bitmap bitmap);
223 void ext2fs_free_block_bitmap(ext2fs_block_bitmap bitmap);
224 void ext2fs_free_inode_bitmap(ext2fs_inode_bitmap bitmap);
225 extern errcode_t ext2fs_read_bitmaps(ext2_filsys fs);
226 extern errcode_t ext2fs_write_bitmaps(ext2_filsys fs);
227
228 /* block.c */
229 extern errcode_t ext2fs_block_iterate(ext2_filsys fs,
230                                       ino_t     ino,
231                                       int       flags,
232                                       char *block_buf,
233                                       int (*func)(ext2_filsys fs,
234                                                   blk_t *blocknr,
235                                                   int   blockcnt,
236                                                   void  *private),
237                                       void *private);
238
239 /* check_desc.c */
240 extern errcode_t ext2fs_check_desc(ext2_filsys fs);
241
242 /* closefs.c */
243 extern errcode_t ext2fs_close(ext2_filsys fs);
244 extern errcode_t ext2fs_flush(ext2_filsys fs);
245
246 /* expanddir.c */
247 extern errcode_t ext2fs_expand_dir(ext2_filsys fs, ino_t dir);
248
249 /* freefs.c */
250 extern void ext2fs_free(ext2_filsys fs);
251
252 /* initialize.c */
253 extern errcode_t ext2fs_initialize(const char *name, int flags,
254                                    struct ext2_super_block *param,
255                                    io_manager manager, ext2_filsys *ret_fs);
256
257 /* inode.c */
258 extern errcode_t ext2fs_open_inode_scan(ext2_filsys fs, int buffer_blocks,
259                                   ext2_inode_scan *ret_scan);
260 extern void ext2fs_close_inode_scan(ext2_inode_scan scan);
261 extern errcode_t ext2fs_get_next_inode(ext2_inode_scan scan, ino_t *ino,
262                                struct ext2_inode *inode);
263 void ext2fs_set_inode_callback(ext2_inode_scan scan,
264                                errcode_t (*done_group)(ext2_filsys fs,
265                                                        ext2_inode_scan scan,
266                                                        dgrp_t group,
267                                                        void * private),
268                                void *done_group_data);
269 void ext2fs_set_inode_callback(ext2_inode_scan scan,
270                                errcode_t (*done_group)(ext2_filsys fs,
271                                                        ext2_inode_scan scan,
272                                                        dgrp_t group,
273                                                        void * private),
274                                void *done_group_data);
275 extern errcode_t ext2fs_read_inode (ext2_filsys fs, unsigned long ino,
276                             struct ext2_inode * inode);
277 extern errcode_t ext2fs_write_inode(ext2_filsys fs, unsigned long ino,
278                             struct ext2_inode * inode);
279 extern errcode_t ext2fs_get_blocks(ext2_filsys fs, ino_t ino, blk_t *blocks);
280 extern errcode_t ext2fs_check_directory(ext2_filsys fs, ino_t ino);
281
282 /* namei.c */
283 extern errcode_t ext2fs_dir_iterate(ext2_filsys fs, 
284                               ino_t dir,
285                               int flags,
286                               char *block_buf,
287                               int (*func)(struct ext2_dir_entry *dirent,
288                                           int   offset,
289                                           int   blocksize,
290                                           char  *buf,
291                                           void  *private),
292                               void *private);
293 extern errcode_t ext2fs_lookup(ext2_filsys fs, ino_t dir, const char *name,
294                          int namelen, char *buf, ino_t *inode);
295 extern errcode_t ext2fs_namei(ext2_filsys fs, ino_t root, ino_t cwd,
296                         const char *name, ino_t *inode);
297
298 /* newdir.c */
299 extern errcode_t ext2fs_new_dir_block(ext2_filsys fs, ino_t dir_ino,
300                                 ino_t parent_ino, char **block);
301
302 /* mkdir.c */
303 extern errcode_t ext2fs_mkdir(ext2_filsys fs, ino_t parent, ino_t inum,
304                               const char *name);
305
306 /* openfs.c */
307 extern errcode_t ext2fs_open(const char *name, int flags, int superblock,
308                              int block_size, io_manager manager,
309                              ext2_filsys *ret_fs);
310
311 /* get_pathname.c */
312 extern errcode_t ext2fs_get_pathname(ext2_filsys fs, ino_t dir, ino_t ino,
313                                char **name);
314
315 /* link.c */
316 errcode_t ext2fs_link(ext2_filsys fs, ino_t dir, const char *name,
317                       ino_t ino, int flags);
318 errcode_t ext2fs_unlink(ext2_filsys fs, ino_t dir, const char *name,
319                         ino_t ino, int flags);
320
321 /* read_bb.c */
322 extern errcode_t ext2fs_read_bb_inode(ext2_filsys fs, badblocks_list *bb_list);
323
324 /* read_bb_file.c */
325 extern errcode_t ext2fs_read_bb_FILE(ext2_filsys fs, FILE *f, 
326                                      badblocks_list *bb_list,
327                                      void (*invalid)(ext2_filsys fs,
328                                                      blk_t blk));
329
330 /* inline functions */
331 extern void ext2fs_mark_super_dirty(ext2_filsys fs);
332 extern void ext2fs_mark_changed(ext2_filsys fs);
333 extern int ext2fs_test_changed(ext2_filsys fs);
334 extern void ext2fs_mark_valid(ext2_filsys fs);
335 extern void ext2fs_unmark_valid(ext2_filsys fs);
336 extern int ext2fs_test_valid(ext2_filsys fs);
337 extern void ext2fs_mark_ib_dirty(ext2_filsys fs);
338 extern void ext2fs_mark_bb_dirty(ext2_filsys fs);
339 extern int ext2fs_test_ib_dirty(ext2_filsys fs);
340 extern int ext2fs_test_bb_dirty(ext2_filsys fs);
341 extern int ext2fs_group_of_blk(ext2_filsys fs, blk_t blk);
342 extern int ext2fs_group_of_ino(ext2_filsys fs, ino_t ino);
343
344 /*
345  * The actual inlined functions definitions themselves...
346  *
347  * If NO_INLINE_FUNCS is defined, then we won't try to do inline
348  * functions at all!
349  */
350 #if (defined(INCLUDE_INLINE_FUNCS) || !defined(NO_INLINE_FUNCS))
351 #ifdef INCLUDE_INLINE_FUNCS
352 #define _INLINE_ extern
353 #else
354 #define _INLINE_ extern __inline__
355 #endif
356
357 /*
358  * Mark a filesystem superblock as dirty
359  */
360 _INLINE_ void ext2fs_mark_super_dirty(ext2_filsys fs)
361 {
362         fs->flags |= EXT2_FLAG_DIRTY | EXT2_FLAG_CHANGED;
363 }
364
365 /*
366  * Mark a filesystem as changed
367  */
368 _INLINE_ void ext2fs_mark_changed(ext2_filsys fs)
369 {
370         fs->flags |= EXT2_FLAG_CHANGED;
371 }
372
373 /*
374  * Check to see if a filesystem has changed
375  */
376 _INLINE_ int ext2fs_test_changed(ext2_filsys fs)
377 {
378         return (fs->flags & EXT2_FLAG_CHANGED);
379 }
380
381 /*
382  * Mark a filesystem as valid
383  */
384 _INLINE_ void ext2fs_mark_valid(ext2_filsys fs)
385 {
386         fs->flags |= EXT2_FLAG_VALID;
387 }
388
389 /*
390  * Mark a filesystem as NOT valid
391  */
392 _INLINE_ void ext2fs_unmark_valid(ext2_filsys fs)
393 {
394         fs->flags &= ~EXT2_FLAG_VALID;
395 }
396
397 /*
398  * Check to see if a filesystem is valid
399  */
400 _INLINE_ int ext2fs_test_valid(ext2_filsys fs)
401 {
402         return (fs->flags & EXT2_FLAG_VALID);
403 }
404
405 /*
406  * Mark the inode bitmap as dirty
407  */
408 _INLINE_ void ext2fs_mark_ib_dirty(ext2_filsys fs)
409 {
410         fs->flags |= EXT2_FLAG_IB_DIRTY | EXT2_FLAG_CHANGED;
411 }
412
413 /*
414  * Mark the block bitmap as dirty
415  */
416 _INLINE_ void ext2fs_mark_bb_dirty(ext2_filsys fs)
417 {
418         fs->flags |= EXT2_FLAG_BB_DIRTY | EXT2_FLAG_CHANGED;
419 }
420
421 /*
422  * Check to see if a filesystem's inode bitmap is dirty
423  */
424 _INLINE_ int ext2fs_test_ib_dirty(ext2_filsys fs)
425 {
426         return (fs->flags & EXT2_FLAG_IB_DIRTY);
427 }
428
429 /*
430  * Check to see if a filesystem's block bitmap is dirty
431  */
432 _INLINE_ int ext2fs_test_bb_dirty(ext2_filsys fs)
433 {
434         return (fs->flags & EXT2_FLAG_BB_DIRTY);
435 }
436
437 /*
438  * Return the group # of a block
439  */
440 _INLINE_ int ext2fs_group_of_blk(ext2_filsys fs, blk_t blk)
441 {
442         return (blk - fs->super->s_first_data_block) /
443                 fs->super->s_blocks_per_group;
444 }
445
446 /*
447  * Return the group # of an inode number
448  */
449 _INLINE_ int ext2fs_group_of_ino(ext2_filsys fs, ino_t ino)
450 {
451         return (ino - 1) / fs->super->s_inodes_per_group;
452 }
453 #undef _INLINE_
454 #endif
455