blob: f0f7b3eec566da4554782deb29c27fe68cfcaca7 [file] [log] [blame]
Mike Frysinger1fd98e02005-05-09 22:10:42 +00001/*
2 * freefs.c --- free an ext2 filesystem
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00003 *
Mike Frysinger1fd98e02005-05-09 22:10:42 +00004 * 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
20static void ext2fs_free_inode_cache(struct ext2_inode_cache *icache);
21
22void 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 }
Rob Landleye7c43b62006-03-01 16:39:45 +000033 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);
Mike Frysinger1fd98e02005-05-09 22:10:42 +000039
Rob Landleye7c43b62006-03-01 16:39:45 +000040 ext2fs_badblocks_list_free(fs->badblocks);
Mike Frysinger1fd98e02005-05-09 22:10:42 +000041 fs->badblocks = 0;
42
Rob Landleye7c43b62006-03-01 16:39:45 +000043 ext2fs_free_dblist(fs->dblist);
Mike Frysinger1fd98e02005-05-09 22:10:42 +000044
45 if (fs->icache)
46 ext2fs_free_inode_cache(fs->icache);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000047
Mike Frysinger1fd98e02005-05-09 22:10:42 +000048 fs->magic = 0;
49
50 ext2fs_free_mem(&fs);
51}
52
53void 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;
Rob Landleye7c43b62006-03-01 16:39:45 +000059 ext2fs_free_mem(&bitmap->description);
60 ext2fs_free_mem(&bitmap->bitmap);
Mike Frysinger1fd98e02005-05-09 22:10:42 +000061 ext2fs_free_mem(&bitmap);
62}
63
64void 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
73void 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 */
85static void ext2fs_free_inode_cache(struct ext2_inode_cache *icache)
86{
87 if (--icache->refcount)
88 return;
Rob Landleye7c43b62006-03-01 16:39:45 +000089 ext2fs_free_mem(&icache->buffer);
90 ext2fs_free_mem(&icache->cache);
Mike Frysinger1fd98e02005-05-09 22:10:42 +000091 icache->buffer_blk = 0;
92 ext2fs_free_mem(&icache);
93}
94
95/*
96 * This procedure frees a badblocks list.
97 */
98void ext2fs_u32_list_free(ext2_u32_list bb)
99{
Rob Landleye7c43b62006-03-01 16:39:45 +0000100 if (!bb || bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000101 return;
102
Rob Landleye7c43b62006-03-01 16:39:45 +0000103 ext2fs_free_mem(&bb->list);
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000104 ext2fs_free_mem(&bb);
105}
106
107void 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 */
116void ext2fs_free_dblist(ext2_dblist dblist)
117{
118 if (!dblist || (dblist->magic != EXT2_ET_MAGIC_DBLIST))
119 return;
120
Rob Landleye7c43b62006-03-01 16:39:45 +0000121 ext2fs_free_mem(&dblist->list);
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000122 if (dblist->fs && dblist->fs->dblist == dblist)
123 dblist->fs->dblist = 0;
124 dblist->magic = 0;
125 ext2fs_free_mem(&dblist);
126}
127