Whamcloud - gitweb
Many files:
[tools/e2fsprogs.git] / lib / ext2fs / freefs.c
1 /*
2  * freefs.c --- free an ext2 filesystem
3  * 
4  * 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
12 #include <stdio.h>
13 #include <unistd.h>
14 #include <stdlib.h>
15
16 #include <linux/ext2_fs.h>
17
18 #include "ext2fsP.h"
19
20 static void ext2fs_free_inode_cache(struct ext2_inode_cache *icache);
21
22 void ext2fs_free(ext2_filsys fs)
23 {
24         if (!fs || (fs->magic != EXT2_ET_MAGIC_EXT2FS_FILSYS))
25                 return;
26         if (fs->io) {
27                 io_channel_close(fs->io);
28         }
29         if (fs->device_name)
30                 free(fs->device_name);
31         if (fs->super)
32                 free(fs->super);
33         if (fs->group_desc)
34                 free(fs->group_desc);
35         if (fs->block_map)
36                 ext2fs_free_block_bitmap(fs->block_map);
37         if (fs->inode_map)
38                 ext2fs_free_inode_bitmap(fs->inode_map);
39
40         if (fs->badblocks)
41                 badblocks_list_free(fs->badblocks);
42         fs->badblocks = 0;
43
44         if (fs->dblist)
45                 ext2fs_free_dblist(fs->dblist);
46
47         if (fs->icache)
48                 ext2fs_free_inode_cache(fs->icache);
49         
50         fs->magic = 0;
51
52         free(fs);
53 }
54
55 void ext2fs_free_generic_bitmap(ext2fs_inode_bitmap bitmap)
56 {
57         if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_GENERIC_BITMAP))
58                 return;
59
60         bitmap->magic = 0;
61         if (bitmap->description) {
62                 free(bitmap->description);
63                 bitmap->description = 0;
64         }
65         if (bitmap->bitmap) {
66                 free(bitmap->bitmap);
67                 bitmap->bitmap = 0;
68         }
69         free(bitmap);
70 }
71
72 void ext2fs_free_inode_bitmap(ext2fs_inode_bitmap bitmap)
73 {
74         if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_INODE_BITMAP))
75                 return;
76
77         bitmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
78         ext2fs_free_generic_bitmap(bitmap);
79 }
80
81 void ext2fs_free_block_bitmap(ext2fs_block_bitmap bitmap)
82 {
83         if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_BLOCK_BITMAP))
84                 return;
85
86         bitmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
87         ext2fs_free_generic_bitmap(bitmap);
88 }
89
90 /*
91  * Free the inode cache structure
92  */
93 static void ext2fs_free_inode_cache(struct ext2_inode_cache *icache)
94 {
95         if (--icache->refcount)
96                 return;
97         if (icache->buffer)
98                 free(icache->buffer);
99         if (icache->cache)
100                 free(icache->cache);
101         icache->buffer_blk = 0;
102         free(icache);
103 }
104
105 /*
106  * This procedure frees a badblocks list.
107  */
108 void ext2fs_badblocks_list_free(ext2_badblocks_list bb)
109 {
110         if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
111                 return;
112
113         if (bb->list)
114                 free(bb->list);
115         bb->list = 0;
116         free(bb);
117 }
118
119 /*
120  * Free a directory block list
121  */
122 void ext2fs_free_dblist(ext2_dblist dblist)
123 {
124         if (!dblist || (dblist->magic != EXT2_ET_MAGIC_DBLIST))
125                 return;
126
127         if (dblist->list)
128                 free(dblist->list);
129         dblist->list = 0;
130         if (dblist->fs && dblist->fs->dblist == dblist)
131                 dblist->fs->dblist = 0;
132         dblist->magic = 0;
133         free(dblist);
134 }
135