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
21void 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	if (fs->image_header)
45		ext2fs_free_mem(&fs->image_header);
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_zero_blocks2(NULL, 0, 0, NULL, NULL);
65	ext2fs_free_mem(&fs);
66}
67
68/*
69 * This procedure frees a badblocks list.
70 */
71void ext2fs_u32_list_free(ext2_u32_list bb)
72{
73	if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
74		return;
75
76	if (bb->list)
77		ext2fs_free_mem(&bb->list);
78	bb->list = 0;
79	ext2fs_free_mem(&bb);
80}
81
82void ext2fs_badblocks_list_free(ext2_badblocks_list bb)
83{
84	ext2fs_u32_list_free((ext2_u32_list) bb);
85}
86
87
88/*
89 * Free a directory block list
90 */
91void ext2fs_free_dblist(ext2_dblist dblist)
92{
93	if (!dblist || (dblist->magic != EXT2_ET_MAGIC_DBLIST))
94		return;
95
96	if (dblist->list)
97		ext2fs_free_mem(&dblist->list);
98	dblist->list = 0;
99	if (dblist->fs && dblist->fs->dblist == dblist)
100		dblist->fs->dblist = 0;
101	dblist->magic = 0;
102	ext2fs_free_mem(&dblist);
103}
104
105