Whamcloud - gitweb
libext2fs: allocate separate memory regions for each inode in the cache
[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 Library
8  * General Public License, version 2.
9  * %End-Header%
10  */
11
12 #include "config.h"
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 void ext2fs_free(ext2_filsys fs)
22 {
23         if (!fs || (fs->magic != EXT2_ET_MAGIC_EXT2FS_FILSYS))
24                 return;
25         if (fs->image_io != fs->io) {
26                 if (fs->image_io)
27                         io_channel_close(fs->image_io);
28         }
29         if (fs->io) {
30                 io_channel_close(fs->io);
31         }
32         if (fs->device_name)
33                 ext2fs_free_mem(&fs->device_name);
34         if (fs->super)
35                 ext2fs_free_mem(&fs->super);
36         if (fs->orig_super)
37                 ext2fs_free_mem(&fs->orig_super);
38         if (fs->group_desc)
39                 ext2fs_free_mem(&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                 ext2fs_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         if (fs->mmp_buf)
56                 ext2fs_free_mem(&fs->mmp_buf);
57         if (fs->mmp_cmp)
58                 ext2fs_free_mem(&fs->mmp_cmp);
59
60         fs->magic = 0;
61
62         ext2fs_free_mem(&fs);
63 }
64
65 /*
66  * This procedure frees a badblocks list.
67  */
68 void ext2fs_u32_list_free(ext2_u32_list bb)
69 {
70         if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
71                 return;
72
73         if (bb->list)
74                 ext2fs_free_mem(&bb->list);
75         bb->list = 0;
76         ext2fs_free_mem(&bb);
77 }
78
79 void ext2fs_badblocks_list_free(ext2_badblocks_list bb)
80 {
81         ext2fs_u32_list_free((ext2_u32_list) bb);
82 }
83
84
85 /*
86  * Free a directory block list
87  */
88 void ext2fs_free_dblist(ext2_dblist dblist)
89 {
90         if (!dblist || (dblist->magic != EXT2_ET_MAGIC_DBLIST))
91                 return;
92
93         if (dblist->list)
94                 ext2fs_free_mem(&dblist->list);
95         dblist->list = 0;
96         if (dblist->fs && dblist->fs->dblist == dblist)
97                 dblist->fs->dblist = 0;
98         dblist->magic = 0;
99         ext2fs_free_mem(&dblist);
100 }
101