freefs.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 Public
  8. * License.
  9. * %End-Header%
  10. */
  11. #include <stdio.h>
  12. #if HAVE_UNISTD_H
  13. #include <unistd.h>
  14. #endif
  15. #include "ext2_fs.h"
  16. #include "ext2fsP.h"
  17. static void ext2fs_free_inode_cache(struct ext2_inode_cache *icache);
  18. void ext2fs_free(ext2_filsys fs)
  19. {
  20. if (!fs || (fs->magic != EXT2_ET_MAGIC_EXT2FS_FILSYS))
  21. return;
  22. if (fs->image_io != fs->io) {
  23. if (fs->image_io)
  24. io_channel_close(fs->image_io);
  25. }
  26. if (fs->io) {
  27. io_channel_close(fs->io);
  28. }
  29. if (fs->device_name)
  30. ext2fs_free_mem(&fs->device_name);
  31. if (fs->super)
  32. ext2fs_free_mem(&fs->super);
  33. if (fs->orig_super)
  34. ext2fs_free_mem(&fs->orig_super);
  35. if (fs->group_desc)
  36. ext2fs_free_mem(&fs->group_desc);
  37. if (fs->block_map)
  38. ext2fs_free_block_bitmap(fs->block_map);
  39. if (fs->inode_map)
  40. ext2fs_free_inode_bitmap(fs->inode_map);
  41. if (fs->badblocks)
  42. ext2fs_badblocks_list_free(fs->badblocks);
  43. fs->badblocks = 0;
  44. if (fs->dblist)
  45. ext2fs_free_dblist(fs->dblist);
  46. if (fs->icache)
  47. ext2fs_free_inode_cache(fs->icache);
  48. fs->magic = 0;
  49. ext2fs_free_mem(&fs);
  50. }
  51. void ext2fs_free_generic_bitmap(ext2fs_inode_bitmap bitmap)
  52. {
  53. if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_GENERIC_BITMAP))
  54. return;
  55. bitmap->magic = 0;
  56. if (bitmap->description) {
  57. ext2fs_free_mem(&bitmap->description);
  58. bitmap->description = 0;
  59. }
  60. if (bitmap->bitmap) {
  61. ext2fs_free_mem(&bitmap->bitmap);
  62. bitmap->bitmap = 0;
  63. }
  64. ext2fs_free_mem(&bitmap);
  65. }
  66. void ext2fs_free_inode_bitmap(ext2fs_inode_bitmap bitmap)
  67. {
  68. if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_INODE_BITMAP))
  69. return;
  70. bitmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
  71. ext2fs_free_generic_bitmap(bitmap);
  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. bitmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
  78. ext2fs_free_generic_bitmap(bitmap);
  79. }
  80. /*
  81. * Free the inode cache structure
  82. */
  83. static void ext2fs_free_inode_cache(struct ext2_inode_cache *icache)
  84. {
  85. if (--icache->refcount)
  86. return;
  87. if (icache->buffer)
  88. ext2fs_free_mem(&icache->buffer);
  89. if (icache->cache)
  90. ext2fs_free_mem(&icache->cache);
  91. icache->buffer_blk = 0;
  92. ext2fs_free_mem(&icache);
  93. }
  94. /*
  95. * This procedure frees a badblocks list.
  96. */
  97. void ext2fs_u32_list_free(ext2_u32_list bb)
  98. {
  99. if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
  100. return;
  101. if (bb->list)
  102. ext2fs_free_mem(&bb->list);
  103. bb->list = 0;
  104. ext2fs_free_mem(&bb);
  105. }
  106. void ext2fs_badblocks_list_free(ext2_badblocks_list bb)
  107. {
  108. ext2fs_u32_list_free((ext2_u32_list) bb);
  109. }
  110. /*
  111. * Free a directory block list
  112. */
  113. void ext2fs_free_dblist(ext2_dblist dblist)
  114. {
  115. if (!dblist || (dblist->magic != EXT2_ET_MAGIC_DBLIST))
  116. return;
  117. if (dblist->list)
  118. ext2fs_free_mem(&dblist->list);
  119. dblist->list = 0;
  120. if (dblist->fs && dblist->fs->dblist == dblist)
  121. dblist->fs->dblist = 0;
  122. dblist->magic = 0;
  123. ext2fs_free_mem(&dblist);
  124. }