Initial public busybox upstream commit
[busybox4maemo] / e2fsprogs / old_e2fsprogs / ext2fs / freefs.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * freefs.c --- free an ext2 filesystem
4  *
5  * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
6  *
7  * %Begin-Header%
8  * This file may be redistributed under the terms of the GNU Public
9  * License.
10  * %End-Header%
11  */
12
13 #include <stdio.h>
14 #if HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17
18 #include "ext2_fs.h"
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->image_io != fs->io) {
28                 if (fs->image_io)
29                         io_channel_close(fs->image_io);
30         }
31         if (fs->io) {
32                 io_channel_close(fs->io);
33         }
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);
40
41         ext2fs_badblocks_list_free(fs->badblocks);
42         fs->badblocks = 0;
43
44         ext2fs_free_dblist(fs->dblist);
45
46         if (fs->icache)
47                 ext2fs_free_inode_cache(fs->icache);
48
49         fs->magic = 0;
50
51         ext2fs_free_mem(&fs);
52 }
53
54 void ext2fs_free_generic_bitmap(ext2fs_inode_bitmap bitmap)
55 {
56         if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_GENERIC_BITMAP))
57                 return;
58
59         bitmap->magic = 0;
60         ext2fs_free_mem(&bitmap->description);
61         ext2fs_free_mem(&bitmap->bitmap);
62         ext2fs_free_mem(&bitmap);
63 }
64
65 void ext2fs_free_inode_bitmap(ext2fs_inode_bitmap bitmap)
66 {
67         if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_INODE_BITMAP))
68                 return;
69
70         bitmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
71         ext2fs_free_generic_bitmap(bitmap);
72 }
73
74 void ext2fs_free_block_bitmap(ext2fs_block_bitmap bitmap)
75 {
76         if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_BLOCK_BITMAP))
77                 return;
78
79         bitmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
80         ext2fs_free_generic_bitmap(bitmap);
81 }
82
83 /*
84  * Free the inode cache structure
85  */
86 static void ext2fs_free_inode_cache(struct ext2_inode_cache *icache)
87 {
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);
94 }
95
96 /*
97  * This procedure frees a badblocks list.
98  */
99 void ext2fs_u32_list_free(ext2_u32_list bb)
100 {
101         if (!bb || bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
102                 return;
103
104         ext2fs_free_mem(&bb->list);
105         ext2fs_free_mem(&bb);
106 }
107
108 void ext2fs_badblocks_list_free(ext2_badblocks_list bb)
109 {
110         ext2fs_u32_list_free((ext2_u32_list) bb);
111 }
112
113
114 /*
115  * Free a directory block list
116  */
117 void ext2fs_free_dblist(ext2_dblist dblist)
118 {
119         if (!dblist || (dblist->magic != EXT2_ET_MAGIC_DBLIST))
120                 return;
121
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);
127 }
128