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