blob: 65c4ee7948c7ac9770ddf60279d2ce790d925ff8 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger1fd98e02005-05-09 22:10:42 +00002/*
3 * freefs.c --- free an ext2 filesystem
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00004 *
Mike Frysinger1fd98e02005-05-09 22:10:42 +00005 * 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
21static void ext2fs_free_inode_cache(struct ext2_inode_cache *icache);
22
23void 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 }
Rob Landleye7c43b62006-03-01 16:39:45 +000034 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);
Mike Frysinger1fd98e02005-05-09 22:10:42 +000040
Rob Landleye7c43b62006-03-01 16:39:45 +000041 ext2fs_badblocks_list_free(fs->badblocks);
Mike Frysinger1fd98e02005-05-09 22:10:42 +000042 fs->badblocks = 0;
43
Rob Landleye7c43b62006-03-01 16:39:45 +000044 ext2fs_free_dblist(fs->dblist);
Mike Frysinger1fd98e02005-05-09 22:10:42 +000045
46 if (fs->icache)
47 ext2fs_free_inode_cache(fs->icache);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000048
Mike Frysinger1fd98e02005-05-09 22:10:42 +000049 fs->magic = 0;
50
51 ext2fs_free_mem(&fs);
52}
53
54void 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;
Rob Landleye7c43b62006-03-01 16:39:45 +000060 ext2fs_free_mem(&bitmap->description);
61 ext2fs_free_mem(&bitmap->bitmap);
Mike Frysinger1fd98e02005-05-09 22:10:42 +000062 ext2fs_free_mem(&bitmap);
63}
64
65void 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
74void 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 */
86static void ext2fs_free_inode_cache(struct ext2_inode_cache *icache)
87{
88 if (--icache->refcount)
89 return;
Rob Landleye7c43b62006-03-01 16:39:45 +000090 ext2fs_free_mem(&icache->buffer);
91 ext2fs_free_mem(&icache->cache);
Mike Frysinger1fd98e02005-05-09 22:10:42 +000092 icache->buffer_blk = 0;
93 ext2fs_free_mem(&icache);
94}
95
96/*
97 * This procedure frees a badblocks list.
98 */
99void ext2fs_u32_list_free(ext2_u32_list bb)
100{
Rob Landleye7c43b62006-03-01 16:39:45 +0000101 if (!bb || bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000102 return;
103
Rob Landleye7c43b62006-03-01 16:39:45 +0000104 ext2fs_free_mem(&bb->list);
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000105 ext2fs_free_mem(&bb);
106}
107
108void 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 */
117void ext2fs_free_dblist(ext2_dblist dblist)
118{
119 if (!dblist || (dblist->magic != EXT2_ET_MAGIC_DBLIST))
120 return;
121
Rob Landleye7c43b62006-03-01 16:39:45 +0000122 ext2fs_free_mem(&dblist->list);
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000123 if (dblist->fs && dblist->fs->dblist == dblist)
124 dblist->fs->dblist = 0;
125 dblist->magic = 0;
126 ext2fs_free_mem(&dblist);
127}
128