1 /* 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 <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 ext2fs_free(ext2_filsys fs)22void 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 } 33 if (fs->device_name) 34 ext2fs_free_mem(&fs->device_name); 35 if (fs->super) 36 ext2fs_free_mem(&fs->super); 37 if (fs->orig_super) 38 ext2fs_free_mem(&fs->orig_super); 39 if (fs->group_desc) 40 ext2fs_free_mem(&fs->group_desc); 41 if (fs->block_map) 42 ext2fs_free_block_bitmap(fs->block_map); 43 if (fs->inode_map) 44 ext2fs_free_inode_bitmap(fs->inode_map); 45 if (fs->image_header) 46 ext2fs_free_mem(&fs->image_header); 47 48 if (fs->badblocks) 49 ext2fs_badblocks_list_free(fs->badblocks); 50 fs->badblocks = 0; 51 52 if (fs->dblist) 53 ext2fs_free_dblist(fs->dblist); 54 55 if (fs->icache) 56 ext2fs_free_inode_cache(fs->icache); 57 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 63 fs->magic = 0; 64 65 ext2fs_free_mem(&fs); 66 } 67 68 /* 69 * Free the inode cache structure 70 */ ext2fs_free_inode_cache(struct ext2_inode_cache * icache)71static void ext2fs_free_inode_cache(struct ext2_inode_cache *icache) 72 { 73 if (--icache->refcount) 74 return; 75 if (icache->buffer) 76 ext2fs_free_mem(&icache->buffer); 77 if (icache->cache) 78 ext2fs_free_mem(&icache->cache); 79 icache->buffer_blk = 0; 80 ext2fs_free_mem(&icache); 81 } 82 83 /* 84 * This procedure frees a badblocks list. 85 */ ext2fs_u32_list_free(ext2_u32_list bb)86void ext2fs_u32_list_free(ext2_u32_list bb) 87 { 88 if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST) 89 return; 90 91 if (bb->list) 92 ext2fs_free_mem(&bb->list); 93 bb->list = 0; 94 ext2fs_free_mem(&bb); 95 } 96 ext2fs_badblocks_list_free(ext2_badblocks_list bb)97void ext2fs_badblocks_list_free(ext2_badblocks_list bb) 98 { 99 ext2fs_u32_list_free((ext2_u32_list) bb); 100 } 101 102 103 /* 104 * Free a directory block list 105 */ ext2fs_free_dblist(ext2_dblist dblist)106void ext2fs_free_dblist(ext2_dblist dblist) 107 { 108 if (!dblist || (dblist->magic != EXT2_ET_MAGIC_DBLIST)) 109 return; 110 111 if (dblist->list) 112 ext2fs_free_mem(&dblist->list); 113 dblist->list = 0; 114 if (dblist->fs && dblist->fs->dblist == dblist) 115 dblist->fs->dblist = 0; 116 dblist->magic = 0; 117 ext2fs_free_mem(&dblist); 118 } 119 120