blob: 1ad2d916f490c931c76b1da42e266480b994adf0 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
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
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 }
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 if (fs->image_header)
47 ext2fs_free_mem(&fs->image_header);
48
49 if (fs->badblocks)
50 ext2fs_badblocks_list_free(fs->badblocks);
51 fs->badblocks = 0;
52
53 if (fs->dblist)
54 ext2fs_free_dblist(fs->dblist);
55
56 if (fs->icache)
57 ext2fs_free_inode_cache(fs->icache);
58
59 if (fs->mmp_buf)
60 ext2fs_free_mem(&fs->mmp_buf);
61 if (fs->mmp_cmp)
62 ext2fs_free_mem(&fs->mmp_cmp);
63
64 fs->magic = 0;
65
66 ext2fs_free_mem(&fs);
67}
68
69/*
70 * Free the inode cache structure
71 */
72static void ext2fs_free_inode_cache(struct ext2_inode_cache *icache)
73{
74 if (--icache->refcount)
75 return;
76 if (icache->buffer)
77 ext2fs_free_mem(&icache->buffer);
78 if (icache->cache)
79 ext2fs_free_mem(&icache->cache);
80 icache->buffer_blk = 0;
81 ext2fs_free_mem(&icache);
82}
83
84/*
85 * This procedure frees a badblocks list.
86 */
87void ext2fs_u32_list_free(ext2_u32_list bb)
88{
89 if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
90 return;
91
92 if (bb->list)
93 ext2fs_free_mem(&bb->list);
94 bb->list = 0;
95 ext2fs_free_mem(&bb);
96}
97
98void ext2fs_badblocks_list_free(ext2_badblocks_list bb)
99{
100 ext2fs_u32_list_free((ext2_u32_list) bb);
101}
102
103
104/*
105 * Free a directory block list
106 */
107void ext2fs_free_dblist(ext2_dblist dblist)
108{
109 if (!dblist || (dblist->magic != EXT2_ET_MAGIC_DBLIST))
110 return;
111
112 if (dblist->list)
113 ext2fs_free_mem(&dblist->list);
114 dblist->list = 0;
115 if (dblist->fs && dblist->fs->dblist == dblist)
116 dblist->fs->dblist = 0;
117 dblist->magic = 0;
118 ext2fs_free_mem(&dblist);
119}
120