general.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include <stdlib.h>
  2. #include <stdint.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <assert.h>
  6. #include "general.h"
  7. #include "internal.h"
  8. #include "debug.h"
  9. #include "read_write.h"
  10. MINIX_FS *MxfsOpen(DISK_IMG *pImg, unsigned uOffset, unsigned cbLen)
  11. {
  12. MINIX_FS *FileSys;
  13. TRACE("");
  14. assert(sizeof(d2_inode) == 64);
  15. FileSys = MxfsAlloc(sizeof(*FileSys));
  16. if(!FileSys)
  17. return NULL;
  18. memset(FileSys, 0, sizeof(*FileSys));
  19. FileSys->pImg = pImg;
  20. FileSys->uOffset = uOffset;
  21. FileSys->cbLen = cbLen;
  22. InitializeCriticalSection(&FileSys->Lock);
  23. MxfsCacheInit(FileSys);
  24. if(MxfsCacheRead(FileSys, &FileSys->Super, SUPER_BLOCK, 0, sizeof(FileSys->Super)) != 0)
  25. {
  26. MxfsClose(FileSys);
  27. return NULL;
  28. }
  29. if(FileSys->Super.s_magic != SUPER_V2_REV)
  30. {
  31. ERR("Invalid magic in super block %x\n", FileSys->Super.s_magic);
  32. MxfsClose(FileSys);
  33. return NULL;
  34. }
  35. unsigned InodeMapSize = FileSys->Super.s_imap_blocks * MINIX_BLOCK_SIZE;
  36. uint32_t *InodeMapBuf = MxfsAlloc(InodeMapSize);
  37. if(!InodeMapBuf || MxfsReadBlocks(FileSys, InodeMapBuf, SUPER_BLOCK + 1, FileSys->Super.s_imap_blocks) != 0)
  38. {
  39. MxfsFree(InodeMapBuf);
  40. MxfsClose(FileSys);
  41. return NULL;
  42. }
  43. MxfsInitBitmap(&FileSys->InodeMap, InodeMapBuf, InodeMapSize * 8);
  44. unsigned ZoneMapBlock = SUPER_BLOCK + 1 + FileSys->Super.s_imap_blocks;
  45. unsigned ZoneMapSize = FileSys->Super.s_zmap_blocks * MINIX_BLOCK_SIZE;
  46. uint32_t *ZoneMapBuf = MxfsAlloc(ZoneMapSize);
  47. if(!ZoneMapBuf || MxfsReadBlocks(FileSys, ZoneMapBuf, ZoneMapBlock, FileSys->Super.s_zmap_blocks) != 0)
  48. {
  49. MxfsFree(ZoneMapBuf);
  50. MxfsClose(FileSys);
  51. return NULL;
  52. }
  53. MxfsInitBitmap(&FileSys->ZoneMap, ZoneMapBuf, ZoneMapSize * 8);
  54. return FileSys;
  55. }
  56. void MxfsClose(MINIX_FS *FileSys)
  57. {
  58. TRACE("");
  59. if(!FileSys) return;
  60. MxfsCacheDestroy(FileSys); // destroy cache first (flush)
  61. MxfsDestroyBitmap(&FileSys->InodeMap);
  62. MxfsDestroyBitmap(&FileSys->ZoneMap);
  63. memset(FileSys, 0, sizeof(*FileSys));
  64. MxfsFree(FileSys);
  65. }