Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * freefs.c --- free an ext2 filesystem |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 3 | * |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 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 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 | |
| 20 | static void ext2fs_free_inode_cache(struct ext2_inode_cache *icache); |
| 21 | |
| 22 | void 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 Landley | e7c43b6 | 2006-03-01 16:39:45 +0000 | [diff] [blame^] | 33 | 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 Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 39 | |
Rob Landley | e7c43b6 | 2006-03-01 16:39:45 +0000 | [diff] [blame^] | 40 | ext2fs_badblocks_list_free(fs->badblocks); |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 41 | fs->badblocks = 0; |
| 42 | |
Rob Landley | e7c43b6 | 2006-03-01 16:39:45 +0000 | [diff] [blame^] | 43 | ext2fs_free_dblist(fs->dblist); |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 44 | |
| 45 | if (fs->icache) |
| 46 | ext2fs_free_inode_cache(fs->icache); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 47 | |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 48 | fs->magic = 0; |
| 49 | |
| 50 | ext2fs_free_mem(&fs); |
| 51 | } |
| 52 | |
| 53 | void 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 Landley | e7c43b6 | 2006-03-01 16:39:45 +0000 | [diff] [blame^] | 59 | ext2fs_free_mem(&bitmap->description); |
| 60 | ext2fs_free_mem(&bitmap->bitmap); |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 61 | ext2fs_free_mem(&bitmap); |
| 62 | } |
| 63 | |
| 64 | void 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 | |
| 73 | void 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 | */ |
| 85 | static void ext2fs_free_inode_cache(struct ext2_inode_cache *icache) |
| 86 | { |
| 87 | if (--icache->refcount) |
| 88 | return; |
Rob Landley | e7c43b6 | 2006-03-01 16:39:45 +0000 | [diff] [blame^] | 89 | ext2fs_free_mem(&icache->buffer); |
| 90 | ext2fs_free_mem(&icache->cache); |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 91 | icache->buffer_blk = 0; |
| 92 | ext2fs_free_mem(&icache); |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * This procedure frees a badblocks list. |
| 97 | */ |
| 98 | void ext2fs_u32_list_free(ext2_u32_list bb) |
| 99 | { |
Rob Landley | e7c43b6 | 2006-03-01 16:39:45 +0000 | [diff] [blame^] | 100 | if (!bb || bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST) |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 101 | return; |
| 102 | |
Rob Landley | e7c43b6 | 2006-03-01 16:39:45 +0000 | [diff] [blame^] | 103 | ext2fs_free_mem(&bb->list); |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 104 | ext2fs_free_mem(&bb); |
| 105 | } |
| 106 | |
| 107 | void 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 | */ |
| 116 | void ext2fs_free_dblist(ext2_dblist dblist) |
| 117 | { |
| 118 | if (!dblist || (dblist->magic != EXT2_ET_MAGIC_DBLIST)) |
| 119 | return; |
| 120 | |
Rob Landley | e7c43b6 | 2006-03-01 16:39:45 +0000 | [diff] [blame^] | 121 | ext2fs_free_mem(&dblist->list); |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 122 | if (dblist->fs && dblist->fs->dblist == dblist) |
| 123 | dblist->fs->dblist = 0; |
| 124 | dblist->magic = 0; |
| 125 | ext2fs_free_mem(&dblist); |
| 126 | } |
| 127 | |