blob: 1e01ef5fd61e6d0eeca78ff33322c4b25ddf08b8 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * freefs.c --- free an ext2 filesystem
Theodore Ts'oefc6f622008-08-27 23:07:54 -04003 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00004 * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
5 *
6 * %Begin-Header%
Theodore Ts'o543547a2010-05-17 21:31:56 -04007 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00009 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000010 */
11
12#include <stdio.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000013#if HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000014#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000015#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000016
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000017#include "ext2_fs.h"
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000018#include "ext2fsP.h"
19
20static void ext2fs_free_inode_cache(struct ext2_inode_cache *icache);
Theodore Ts'o3839e651997-04-26 13:21:57 +000021
22void ext2fs_free(ext2_filsys fs)
23{
Theodore Ts'of3db3561997-04-26 13:34:30 +000024 if (!fs || (fs->magic != EXT2_ET_MAGIC_EXT2FS_FILSYS))
Theodore Ts'o3839e651997-04-26 13:21:57 +000025 return;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -040026 if (fs->image_io != fs->io) {
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -050027 if (fs->image_io)
28 io_channel_close(fs->image_io);
Theodore Ts'o1ad54a92004-07-28 21:11:48 -040029 }
Theodore Ts'o3839e651997-04-26 13:21:57 +000030 if (fs->io) {
31 io_channel_close(fs->io);
32 }
33 if (fs->device_name)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040034 ext2fs_free_mem(&fs->device_name);
Theodore Ts'o3839e651997-04-26 13:21:57 +000035 if (fs->super)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040036 ext2fs_free_mem(&fs->super);
Theodore Ts'oc180ac82000-10-26 20:24:43 +000037 if (fs->orig_super)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040038 ext2fs_free_mem(&fs->orig_super);
Theodore Ts'o3839e651997-04-26 13:21:57 +000039 if (fs->group_desc)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040040 ext2fs_free_mem(&fs->group_desc);
Theodore Ts'o3839e651997-04-26 13:21:57 +000041 if (fs->block_map)
Theodore Ts'o50e1e101997-04-26 13:58:21 +000042 ext2fs_free_block_bitmap(fs->block_map);
Theodore Ts'o3839e651997-04-26 13:21:57 +000043 if (fs->inode_map)
Theodore Ts'o50e1e101997-04-26 13:58:21 +000044 ext2fs_free_inode_bitmap(fs->inode_map);
JP Abgralle0ed7402014-03-19 19:08:39 -070045 if (fs->image_header)
46 ext2fs_free_mem(&fs->image_header);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000047
48 if (fs->badblocks)
Theodore Ts'o9b9fe8a1999-11-08 22:05:04 +000049 ext2fs_badblocks_list_free(fs->badblocks);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000050 fs->badblocks = 0;
51
52 if (fs->dblist)
53 ext2fs_free_dblist(fs->dblist);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000054
55 if (fs->icache)
56 ext2fs_free_inode_cache(fs->icache);
Theodore Ts'oefc6f622008-08-27 23:07:54 -040057
JP Abgralle0ed7402014-03-19 19:08:39 -070058 if (fs->mmp_buf)
59 ext2fs_free_mem(&fs->mmp_buf);
60 if (fs->mmp_cmp)
61 ext2fs_free_mem(&fs->mmp_cmp);
62
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000063 fs->magic = 0;
64
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040065 ext2fs_free_mem(&fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +000066}
67
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000068/*
69 * Free the inode cache structure
70 */
71static void ext2fs_free_inode_cache(struct ext2_inode_cache *icache)
72{
73 if (--icache->refcount)
74 return;
75 if (icache->buffer)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040076 ext2fs_free_mem(&icache->buffer);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000077 if (icache->cache)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040078 ext2fs_free_mem(&icache->cache);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000079 icache->buffer_blk = 0;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040080 ext2fs_free_mem(&icache);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000081}
82
83/*
84 * This procedure frees a badblocks list.
85 */
Theodore Ts'ob7a00562002-07-20 00:28:07 -040086void ext2fs_u32_list_free(ext2_u32_list bb)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000087{
88 if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
89 return;
90
91 if (bb->list)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040092 ext2fs_free_mem(&bb->list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000093 bb->list = 0;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040094 ext2fs_free_mem(&bb);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000095}
96
Theodore Ts'ob7a00562002-07-20 00:28:07 -040097void ext2fs_badblocks_list_free(ext2_badblocks_list bb)
98{
99 ext2fs_u32_list_free((ext2_u32_list) bb);
100}
101
102
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000103/*
104 * Free a directory block list
105 */
106void ext2fs_free_dblist(ext2_dblist dblist)
107{
108 if (!dblist || (dblist->magic != EXT2_ET_MAGIC_DBLIST))
109 return;
110
111 if (dblist->list)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400112 ext2fs_free_mem(&dblist->list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000113 dblist->list = 0;
114 if (dblist->fs && dblist->fs->dblist == dblist)
115 dblist->fs->dblist = 0;
116 dblist->magic = 0;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400117 ext2fs_free_mem(&dblist);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000118}
119