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