Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * freefs.c --- free an ext2 filesystem |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 3 | * |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 4 | * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o. |
| 5 | * |
| 6 | * %Begin-Header% |
Theodore Ts'o | 543547a | 2010-05-17 21:31:56 -0400 | [diff] [blame] | 7 | * This file may be redistributed under the terms of the GNU Library |
| 8 | * General Public License, version 2. |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 9 | * %End-Header% |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <stdio.h> |
Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 13 | #if HAVE_UNISTD_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 14 | #include <unistd.h> |
Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 15 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 16 | |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 17 | #include "ext2_fs.h" |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 18 | #include "ext2fsP.h" |
| 19 | |
| 20 | static void ext2fs_free_inode_cache(struct ext2_inode_cache *icache); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 21 | |
| 22 | void ext2fs_free(ext2_filsys fs) |
| 23 | { |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 24 | if (!fs || (fs->magic != EXT2_ET_MAGIC_EXT2FS_FILSYS)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 25 | return; |
Theodore Ts'o | 1ad54a9 | 2004-07-28 21:11:48 -0400 | [diff] [blame] | 26 | if (fs->image_io != fs->io) { |
Theodore Ts'o | 2e8ca9a | 2004-11-30 14:07:11 -0500 | [diff] [blame] | 27 | if (fs->image_io) |
| 28 | io_channel_close(fs->image_io); |
Theodore Ts'o | 1ad54a9 | 2004-07-28 21:11:48 -0400 | [diff] [blame] | 29 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 30 | if (fs->io) { |
| 31 | io_channel_close(fs->io); |
| 32 | } |
| 33 | if (fs->device_name) |
Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 34 | ext2fs_free_mem(&fs->device_name); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 35 | if (fs->super) |
Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 36 | ext2fs_free_mem(&fs->super); |
Theodore Ts'o | c180ac8 | 2000-10-26 20:24:43 +0000 | [diff] [blame] | 37 | if (fs->orig_super) |
Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 38 | ext2fs_free_mem(&fs->orig_super); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 39 | if (fs->group_desc) |
Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 40 | ext2fs_free_mem(&fs->group_desc); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 41 | if (fs->block_map) |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 42 | ext2fs_free_block_bitmap(fs->block_map); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 43 | if (fs->inode_map) |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 44 | ext2fs_free_inode_bitmap(fs->inode_map); |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 45 | if (fs->image_header) |
| 46 | ext2fs_free_mem(&fs->image_header); |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 47 | |
| 48 | if (fs->badblocks) |
Theodore Ts'o | 9b9fe8a | 1999-11-08 22:05:04 +0000 | [diff] [blame] | 49 | ext2fs_badblocks_list_free(fs->badblocks); |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 50 | fs->badblocks = 0; |
| 51 | |
| 52 | if (fs->dblist) |
| 53 | ext2fs_free_dblist(fs->dblist); |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 54 | |
| 55 | if (fs->icache) |
| 56 | ext2fs_free_inode_cache(fs->icache); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 57 | |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 58 | 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'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 63 | fs->magic = 0; |
| 64 | |
Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 65 | ext2fs_free_mem(&fs); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 68 | /* |
| 69 | * Free the inode cache structure |
| 70 | */ |
| 71 | static void ext2fs_free_inode_cache(struct ext2_inode_cache *icache) |
| 72 | { |
| 73 | if (--icache->refcount) |
| 74 | return; |
| 75 | if (icache->buffer) |
Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 76 | ext2fs_free_mem(&icache->buffer); |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 77 | if (icache->cache) |
Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 78 | ext2fs_free_mem(&icache->cache); |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 79 | icache->buffer_blk = 0; |
Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 80 | ext2fs_free_mem(&icache); |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /* |
| 84 | * This procedure frees a badblocks list. |
| 85 | */ |
Theodore Ts'o | b7a0056 | 2002-07-20 00:28:07 -0400 | [diff] [blame] | 86 | void ext2fs_u32_list_free(ext2_u32_list bb) |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 87 | { |
| 88 | if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST) |
| 89 | return; |
| 90 | |
| 91 | if (bb->list) |
Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 92 | ext2fs_free_mem(&bb->list); |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 93 | bb->list = 0; |
Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 94 | ext2fs_free_mem(&bb); |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Theodore Ts'o | b7a0056 | 2002-07-20 00:28:07 -0400 | [diff] [blame] | 97 | void ext2fs_badblocks_list_free(ext2_badblocks_list bb) |
| 98 | { |
| 99 | ext2fs_u32_list_free((ext2_u32_list) bb); |
| 100 | } |
| 101 | |
| 102 | |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 103 | /* |
| 104 | * Free a directory block list |
| 105 | */ |
| 106 | void 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'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 112 | ext2fs_free_mem(&dblist->list); |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 113 | dblist->list = 0; |
| 114 | if (dblist->fs && dblist->fs->dblist == dblist) |
| 115 | dblist->fs->dblist = 0; |
| 116 | dblist->magic = 0; |
Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 117 | ext2fs_free_mem(&dblist); |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 118 | } |
| 119 | |