Convert a chunk of usage.h to USE_ and SKIP_ (more to do there), and fix a
[oweals/busybox.git] / e2fsprogs / 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 "ext2_fs.h"
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->image_io != fs->io) {
27                 if (fs->image_io)
28                         io_channel_close(fs->image_io);
29         }
30         if (fs->io) {
31                 io_channel_close(fs->io);
32         }
33         ext2fs_free_mem(&fs->device_name);
34         ext2fs_free_mem(&fs->super);
35         ext2fs_free_mem(&fs->orig_super);
36         ext2fs_free_mem(&fs->group_desc);
37         ext2fs_free_block_bitmap(fs->block_map);
38         ext2fs_free_inode_bitmap(fs->inode_map);
39
40         ext2fs_badblocks_list_free(fs->badblocks);
41         fs->badblocks = 0;
42
43         ext2fs_free_dblist(fs->dblist);
44
45         if (fs->icache)
46                 ext2fs_free_inode_cache(fs->icache);
47
48         fs->magic = 0;
49
50         ext2fs_free_mem(&fs);
51 }
52
53 void ext2fs_free_generic_bitmap(ext2fs_inode_bitmap bitmap)
54 {
55         if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_GENERIC_BITMAP))
56                 return;
57
58         bitmap->magic = 0;
59         ext2fs_free_mem(&bitmap->description);
60         ext2fs_free_mem(&bitmap->bitmap);
61         ext2fs_free_mem(&bitmap);
62 }
63
64 void ext2fs_free_inode_bitmap(ext2fs_inode_bitmap bitmap)
65 {
66         if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_INODE_BITMAP))
67                 return;
68
69         bitmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
70         ext2fs_free_generic_bitmap(bitmap);
71 }
72
73 void ext2fs_free_block_bitmap(ext2fs_block_bitmap bitmap)
74 {
75         if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_BLOCK_BITMAP))
76                 return;
77
78         bitmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
79         ext2fs_free_generic_bitmap(bitmap);
80 }
81
82 /*
83  * Free the inode cache structure
84  */
85 static void ext2fs_free_inode_cache(struct ext2_inode_cache *icache)
86 {
87         if (--icache->refcount)
88                 return;
89         ext2fs_free_mem(&icache->buffer);
90         ext2fs_free_mem(&icache->cache);
91         icache->buffer_blk = 0;
92         ext2fs_free_mem(&icache);
93 }
94
95 /*
96  * This procedure frees a badblocks list.
97  */
98 void ext2fs_u32_list_free(ext2_u32_list bb)
99 {
100         if (!bb || bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
101                 return;
102
103         ext2fs_free_mem(&bb->list);
104         ext2fs_free_mem(&bb);
105 }
106
107 void ext2fs_badblocks_list_free(ext2_badblocks_list bb)
108 {
109         ext2fs_u32_list_free((ext2_u32_list) bb);
110 }
111
112
113 /*
114  * Free a directory block list
115  */
116 void ext2fs_free_dblist(ext2_dblist dblist)
117 {
118         if (!dblist || (dblist->magic != EXT2_ET_MAGIC_DBLIST))
119                 return;
120
121         ext2fs_free_mem(&dblist->list);
122         if (dblist->fs && dblist->fs->dblist == dblist)
123                 dblist->fs->dblist = 0;
124         dblist->magic = 0;
125         ext2fs_free_mem(&dblist);
126 }
127