"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame^] | 1 | /* vi: set sw=4 ts=4: */ |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 2 | /* |
| 3 | * freefs.c --- free an ext2 filesystem |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 4 | * |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 5 | * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o. |
| 6 | * |
| 7 | * %Begin-Header% |
| 8 | * This file may be redistributed under the terms of the GNU Public |
| 9 | * License. |
| 10 | * %End-Header% |
| 11 | */ |
| 12 | |
| 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 | |
| 21 | static void ext2fs_free_inode_cache(struct ext2_inode_cache *icache); |
| 22 | |
| 23 | void 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 | } |
Rob Landley | e7c43b6 | 2006-03-01 16:39:45 +0000 | [diff] [blame] | 34 | ext2fs_free_mem(&fs->device_name); |
| 35 | ext2fs_free_mem(&fs->super); |
| 36 | ext2fs_free_mem(&fs->orig_super); |
| 37 | ext2fs_free_mem(&fs->group_desc); |
| 38 | ext2fs_free_block_bitmap(fs->block_map); |
| 39 | ext2fs_free_inode_bitmap(fs->inode_map); |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 40 | |
Rob Landley | e7c43b6 | 2006-03-01 16:39:45 +0000 | [diff] [blame] | 41 | ext2fs_badblocks_list_free(fs->badblocks); |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 42 | fs->badblocks = 0; |
| 43 | |
Rob Landley | e7c43b6 | 2006-03-01 16:39:45 +0000 | [diff] [blame] | 44 | ext2fs_free_dblist(fs->dblist); |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 45 | |
| 46 | if (fs->icache) |
| 47 | ext2fs_free_inode_cache(fs->icache); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 48 | |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 49 | fs->magic = 0; |
| 50 | |
| 51 | ext2fs_free_mem(&fs); |
| 52 | } |
| 53 | |
| 54 | void ext2fs_free_generic_bitmap(ext2fs_inode_bitmap bitmap) |
| 55 | { |
| 56 | if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_GENERIC_BITMAP)) |
| 57 | return; |
| 58 | |
| 59 | bitmap->magic = 0; |
Rob Landley | e7c43b6 | 2006-03-01 16:39:45 +0000 | [diff] [blame] | 60 | ext2fs_free_mem(&bitmap->description); |
| 61 | ext2fs_free_mem(&bitmap->bitmap); |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 62 | ext2fs_free_mem(&bitmap); |
| 63 | } |
| 64 | |
| 65 | void ext2fs_free_inode_bitmap(ext2fs_inode_bitmap bitmap) |
| 66 | { |
| 67 | if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_INODE_BITMAP)) |
| 68 | return; |
| 69 | |
| 70 | bitmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP; |
| 71 | ext2fs_free_generic_bitmap(bitmap); |
| 72 | } |
| 73 | |
| 74 | void ext2fs_free_block_bitmap(ext2fs_block_bitmap bitmap) |
| 75 | { |
| 76 | if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_BLOCK_BITMAP)) |
| 77 | return; |
| 78 | |
| 79 | bitmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP; |
| 80 | ext2fs_free_generic_bitmap(bitmap); |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * Free the inode cache structure |
| 85 | */ |
| 86 | static void ext2fs_free_inode_cache(struct ext2_inode_cache *icache) |
| 87 | { |
| 88 | if (--icache->refcount) |
| 89 | return; |
Rob Landley | e7c43b6 | 2006-03-01 16:39:45 +0000 | [diff] [blame] | 90 | ext2fs_free_mem(&icache->buffer); |
| 91 | ext2fs_free_mem(&icache->cache); |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 92 | icache->buffer_blk = 0; |
| 93 | ext2fs_free_mem(&icache); |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * This procedure frees a badblocks list. |
| 98 | */ |
| 99 | void ext2fs_u32_list_free(ext2_u32_list bb) |
| 100 | { |
Rob Landley | e7c43b6 | 2006-03-01 16:39:45 +0000 | [diff] [blame] | 101 | if (!bb || bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST) |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 102 | return; |
| 103 | |
Rob Landley | e7c43b6 | 2006-03-01 16:39:45 +0000 | [diff] [blame] | 104 | ext2fs_free_mem(&bb->list); |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 105 | ext2fs_free_mem(&bb); |
| 106 | } |
| 107 | |
| 108 | void ext2fs_badblocks_list_free(ext2_badblocks_list bb) |
| 109 | { |
| 110 | ext2fs_u32_list_free((ext2_u32_list) bb); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | /* |
| 115 | * Free a directory block list |
| 116 | */ |
| 117 | void ext2fs_free_dblist(ext2_dblist dblist) |
| 118 | { |
| 119 | if (!dblist || (dblist->magic != EXT2_ET_MAGIC_DBLIST)) |
| 120 | return; |
| 121 | |
Rob Landley | e7c43b6 | 2006-03-01 16:39:45 +0000 | [diff] [blame] | 122 | ext2fs_free_mem(&dblist->list); |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 123 | if (dblist->fs && dblist->fs->dblist == dblist) |
| 124 | dblist->fs->dblist = 0; |
| 125 | dblist->magic = 0; |
| 126 | ext2fs_free_mem(&dblist); |
| 127 | } |
| 128 | |