3
0

freefs.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * freefs.c --- free an ext2 filesystem
  4. *
  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. #include <stdio.h>
  13. #if HAVE_UNISTD_H
  14. #include <unistd.h>
  15. #endif
  16. #include "ext2_fs.h"
  17. #include "ext2fsP.h"
  18. static void ext2fs_free_inode_cache(struct ext2_inode_cache *icache);
  19. void ext2fs_free(ext2_filsys fs)
  20. {
  21. if (!fs || (fs->magic != EXT2_ET_MAGIC_EXT2FS_FILSYS))
  22. return;
  23. if (fs->image_io != fs->io) {
  24. if (fs->image_io)
  25. io_channel_close(fs->image_io);
  26. }
  27. if (fs->io) {
  28. io_channel_close(fs->io);
  29. }
  30. ext2fs_free_mem(&fs->device_name);
  31. ext2fs_free_mem(&fs->super);
  32. ext2fs_free_mem(&fs->orig_super);
  33. ext2fs_free_mem(&fs->group_desc);
  34. ext2fs_free_block_bitmap(fs->block_map);
  35. ext2fs_free_inode_bitmap(fs->inode_map);
  36. ext2fs_badblocks_list_free(fs->badblocks);
  37. fs->badblocks = 0;
  38. ext2fs_free_dblist(fs->dblist);
  39. if (fs->icache)
  40. ext2fs_free_inode_cache(fs->icache);
  41. fs->magic = 0;
  42. ext2fs_free_mem(&fs);
  43. }
  44. void ext2fs_free_generic_bitmap(ext2fs_inode_bitmap bitmap)
  45. {
  46. if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_GENERIC_BITMAP))
  47. return;
  48. bitmap->magic = 0;
  49. ext2fs_free_mem(&bitmap->description);
  50. ext2fs_free_mem(&bitmap->bitmap);
  51. ext2fs_free_mem(&bitmap);
  52. }
  53. void ext2fs_free_inode_bitmap(ext2fs_inode_bitmap bitmap)
  54. {
  55. if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_INODE_BITMAP))
  56. return;
  57. bitmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
  58. ext2fs_free_generic_bitmap(bitmap);
  59. }
  60. void ext2fs_free_block_bitmap(ext2fs_block_bitmap bitmap)
  61. {
  62. if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_BLOCK_BITMAP))
  63. return;
  64. bitmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
  65. ext2fs_free_generic_bitmap(bitmap);
  66. }
  67. /*
  68. * Free the inode cache structure
  69. */
  70. static void ext2fs_free_inode_cache(struct ext2_inode_cache *icache)
  71. {
  72. if (--icache->refcount)
  73. return;
  74. ext2fs_free_mem(&icache->buffer);
  75. ext2fs_free_mem(&icache->cache);
  76. icache->buffer_blk = 0;
  77. ext2fs_free_mem(&icache);
  78. }
  79. /*
  80. * This procedure frees a badblocks list.
  81. */
  82. void ext2fs_u32_list_free(ext2_u32_list bb)
  83. {
  84. if (!bb || bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
  85. return;
  86. ext2fs_free_mem(&bb->list);
  87. ext2fs_free_mem(&bb);
  88. }
  89. void ext2fs_badblocks_list_free(ext2_badblocks_list bb)
  90. {
  91. ext2fs_u32_list_free((ext2_u32_list) bb);
  92. }
  93. /*
  94. * Free a directory block list
  95. */
  96. void ext2fs_free_dblist(ext2_dblist dblist)
  97. {
  98. if (!dblist || (dblist->magic != EXT2_ET_MAGIC_DBLIST))
  99. return;
  100. ext2fs_free_mem(&dblist->list);
  101. if (dblist->fs && dblist->fs->dblist == dblist)
  102. dblist->fs->dblist = 0;
  103. dblist->magic = 0;
  104. ext2fs_free_mem(&dblist);
  105. }