Whamcloud - gitweb
e2fsck: add support for xattrs in external inodes
[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 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         if (fs->device_name)
35                 ext2fs_free_mem(&fs->device_name);
36         if (fs->super)
37                 ext2fs_free_mem(&fs->super);
38         if (fs->orig_super)
39                 ext2fs_free_mem(&fs->orig_super);
40         if (fs->group_desc)
41                 ext2fs_free_mem(&fs->group_desc);
42         if (fs->block_map)
43                 ext2fs_free_block_bitmap(fs->block_map);
44         if (fs->inode_map)
45                 ext2fs_free_inode_bitmap(fs->inode_map);
46
47         if (fs->badblocks)
48                 ext2fs_badblocks_list_free(fs->badblocks);
49         fs->badblocks = 0;
50
51         if (fs->dblist)
52                 ext2fs_free_dblist(fs->dblist);
53
54         if (fs->icache)
55                 ext2fs_free_inode_cache(fs->icache);
56
57         if (fs->mmp_buf)
58                 ext2fs_free_mem(&fs->mmp_buf);
59         if (fs->mmp_cmp)
60                 ext2fs_free_mem(&fs->mmp_cmp);
61
62         fs->magic = 0;
63
64         ext2fs_free_mem(&fs);
65 }
66
67 /*
68  * Free the inode cache structure
69  */
70 static void ext2fs_free_inode_cache(struct ext2_inode_cache *icache)
71 {
72         if (--icache->refcount)
73                 return;
74         if (icache->buffer)
75                 ext2fs_free_mem(&icache->buffer);
76         if (icache->cache)
77                 ext2fs_free_mem(&icache->cache);
78         icache->buffer_blk = 0;
79         ext2fs_free_mem(&icache);
80 }
81
82 /*
83  * This procedure frees a badblocks list.
84  */
85 void ext2fs_u32_list_free(ext2_u32_list bb)
86 {
87         if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
88                 return;
89
90         if (bb->list)
91                 ext2fs_free_mem(&bb->list);
92         bb->list = 0;
93         ext2fs_free_mem(&bb);
94 }
95
96 void ext2fs_badblocks_list_free(ext2_badblocks_list bb)
97 {
98         ext2fs_u32_list_free((ext2_u32_list) bb);
99 }
100
101
102 /*
103  * Free a directory block list
104  */
105 void ext2fs_free_dblist(ext2_dblist dblist)
106 {
107         if (!dblist || (dblist->magic != EXT2_ET_MAGIC_DBLIST))
108                 return;
109
110         if (dblist->list)
111                 ext2fs_free_mem(&dblist->list);
112         dblist->list = 0;
113         if (dblist->fs && dblist->fs->dblist == dblist)
114                 dblist->fs->dblist = 0;
115         dblist->magic = 0;
116         ext2fs_free_mem(&dblist);
117 }
118