Changes for kernel and Busybox
[tomato.git] / release / src / router / busybox / e2fsprogs / ext2fs / freefs.c
blob0c5d48b11345cba51920582f270e05b5027ed654
1 /* vi: set sw=4 ts=4: */
2 /*
3 * freefs.c --- free an ext2 filesystem
5 * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
7 * %Begin-Header%
8 * This file may be redistributed under the terms of the GNU Public
9 * License.
10 * %End-Header%
13 #include <stdio.h>
14 #if HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
18 #include "ext2_fs.h"
19 #include "ext2fsP.h"
21 static void ext2fs_free_inode_cache(struct ext2_inode_cache *icache);
23 void ext2fs_free(ext2_filsys fs)
25 if (!fs || (fs->magic != EXT2_ET_MAGIC_EXT2FS_FILSYS))
26 return;
27 if (fs->image_io != fs->io) {
28 if (fs->image_io)
29 io_channel_close(fs->image_io);
31 if (fs->io) {
32 io_channel_close(fs->io);
34 ext2fs_free_mem(&fs->device_name);
35 ext2fs_free_mem(&fs->super);
36 ext2fs_free_mem(&fs->orig_super);
37 ext2fs_free_mem(&fs->group_desc);
38 ext2fs_free_block_bitmap(fs->block_map);
39 ext2fs_free_inode_bitmap(fs->inode_map);
41 ext2fs_badblocks_list_free(fs->badblocks);
42 fs->badblocks = 0;
44 ext2fs_free_dblist(fs->dblist);
46 if (fs->icache)
47 ext2fs_free_inode_cache(fs->icache);
49 fs->magic = 0;
51 ext2fs_free_mem(&fs);
54 void ext2fs_free_generic_bitmap(ext2fs_inode_bitmap bitmap)
56 if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_GENERIC_BITMAP))
57 return;
59 bitmap->magic = 0;
60 ext2fs_free_mem(&bitmap->description);
61 ext2fs_free_mem(&bitmap->bitmap);
62 ext2fs_free_mem(&bitmap);
65 void ext2fs_free_inode_bitmap(ext2fs_inode_bitmap bitmap)
67 if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_INODE_BITMAP))
68 return;
70 bitmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
71 ext2fs_free_generic_bitmap(bitmap);
74 void ext2fs_free_block_bitmap(ext2fs_block_bitmap bitmap)
76 if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_BLOCK_BITMAP))
77 return;
79 bitmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
80 ext2fs_free_generic_bitmap(bitmap);
84 * Free the inode cache structure
86 static void ext2fs_free_inode_cache(struct ext2_inode_cache *icache)
88 if (--icache->refcount)
89 return;
90 ext2fs_free_mem(&icache->buffer);
91 ext2fs_free_mem(&icache->cache);
92 icache->buffer_blk = 0;
93 ext2fs_free_mem(&icache);
97 * This procedure frees a badblocks list.
99 void ext2fs_u32_list_free(ext2_u32_list bb)
101 if (!bb || bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
102 return;
104 ext2fs_free_mem(&bb->list);
105 ext2fs_free_mem(&bb);
108 void ext2fs_badblocks_list_free(ext2_badblocks_list bb)
110 ext2fs_u32_list_free((ext2_u32_list) bb);
115 * Free a directory block list
117 void ext2fs_free_dblist(ext2_dblist dblist)
119 if (!dblist || (dblist->magic != EXT2_ET_MAGIC_DBLIST))
120 return;
122 ext2fs_free_mem(&dblist->list);
123 if (dblist->fs && dblist->fs->dblist == dblist)
124 dblist->fs->dblist = 0;
125 dblist->magic = 0;
126 ext2fs_free_mem(&dblist);