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