inode.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #include "debug.h"
  2. #include "general.h"
  3. #include "file.h"
  4. #include "bitmap.h"
  5. #include "inode.h"
  6. int MxfsReadInode(MINIX_FS *FileSys, int FileIdx, minix_inode *Result)
  7. {
  8. if(FileIdx <= 0 || FileIdx > FileSys->Super.s_ninodes)
  9. {
  10. ERR("MxfsReadInode: Invalid inode %d\n", FileIdx);
  11. return -ERROR_INVALID_INDEX;
  12. }
  13. --FileIdx; // make it zero based
  14. unsigned InodesFirstBlock = SUPER_BLOCK + 1 + FileSys->Super.s_imap_blocks + FileSys->Super.s_zmap_blocks;
  15. unsigned Block = InodesFirstBlock + FileIdx * sizeof(minix_inode) / MINIX_BLOCK_SIZE;
  16. unsigned Offset = (FileIdx * sizeof(minix_inode)) % MINIX_BLOCK_SIZE;
  17. return MxfsCacheRead(FileSys, Result, Block, Offset, sizeof(minix_inode));
  18. }
  19. int MxfsWriteInode(MINIX_FS *FileSys, int FileIdx, minix_inode *Info)
  20. {
  21. if(FileIdx <= 0 || FileIdx > FileSys->Super.s_ninodes)
  22. {
  23. ERR("MxfsWriteInode: Invalid inode %d\n", FileIdx);
  24. return -ERROR_INVALID_INDEX;
  25. }
  26. // Update inode modified time
  27. Info->d2_ctime = time(NULL);
  28. --FileIdx; // make it zero based
  29. unsigned InodesFirstBlock = SUPER_BLOCK + 1 + FileSys->Super.s_imap_blocks + FileSys->Super.s_zmap_blocks;
  30. unsigned Block = InodesFirstBlock + FileIdx * sizeof(minix_inode) / MINIX_BLOCK_SIZE;
  31. unsigned Offset = (FileIdx * sizeof(minix_inode)) % MINIX_BLOCK_SIZE;
  32. return MxfsCacheWrite(FileSys, Info, Block, Offset, sizeof(minix_inode));
  33. }
  34. int MxfsAllocZone(MINIX_FS *FileSys)
  35. {
  36. int Bit = MxfsFindClearBit(&FileSys->ZoneMap);
  37. if(Bit < 0)
  38. {
  39. WARN("MxfsAllocZone: disk is full\n");
  40. return -ERROR_DISK_FULL;
  41. }
  42. unsigned Zone = MxfsZoneFromBit(FileSys, Bit);
  43. if(Zone > FileSys->Super.s_zones)
  44. {
  45. WARN("MxfsAllocZone: invalid zone %u > %lu\n", Zone, FileSys->Super.s_zones);
  46. return -ERROR_DISK_FULL;
  47. }
  48. int Result = MxfsSetBit(&FileSys->ZoneMap, Bit);
  49. ASSERT(Result >= 0);
  50. unsigned BitmapOffset = ALIGN_DOWN(Bit/8, MINIX_BLOCK_SIZE);
  51. unsigned BlockIdx = SUPER_BLOCK + 1 + FileSys->Super.s_imap_blocks + BitmapOffset/MINIX_BLOCK_SIZE;
  52. BYTE *Buffer = ((BYTE*)FileSys->ZoneMap.Buffer) + BitmapOffset;
  53. Result = MxfsCacheWrite(FileSys, Buffer, BlockIdx, 0, MINIX_BLOCK_SIZE);
  54. if(Result < 0)
  55. {
  56. WARN("MxfsAllocZone: MxfsCacheWrite failed\n");
  57. return Result;
  58. }
  59. WARN("Alloc zone %u\n", Zone);
  60. return Zone;
  61. }
  62. int MxfsFreeZone(MINIX_FS *FileSys, unsigned Zone)
  63. {
  64. WARN("Free zone %u\n", Zone);
  65. if(Zone > FileSys->Super.s_zones)
  66. {
  67. WARN("MxfsFreeZone: invalid zone %u > %lu\n", Zone, FileSys->Super.s_zones);
  68. return -ERROR_INVALID_PARAMETER;
  69. }
  70. unsigned Bit = MxfsBitFromZone(FileSys, Zone);
  71. int Result = MxfsClearBit(&FileSys->ZoneMap, Bit);
  72. if(Result < 0)
  73. {
  74. WARN("MxfsFreeZone: MxfsClearBit failed\n");
  75. return Result;
  76. }
  77. unsigned BitmapOffset = ALIGN_DOWN(Bit/8, MINIX_BLOCK_SIZE);
  78. unsigned BlockIdx = SUPER_BLOCK + 1 + FileSys->Super.s_imap_blocks + BitmapOffset/MINIX_BLOCK_SIZE;
  79. BYTE *Buffer = ((BYTE*)FileSys->ZoneMap.Buffer) + BitmapOffset;
  80. return MxfsCacheWrite(FileSys, Buffer, BlockIdx, 0, MINIX_BLOCK_SIZE);
  81. }
  82. int MxfsGetBlockFromFileOffset(MINIX_FS *FileSys, unsigned FileIdx, minix_inode *FileInfo, unsigned Offset, BOOL Alloc)
  83. {
  84. unsigned Block;
  85. zone_t Zone;
  86. unsigned OffsetInBlocks = Offset / MINIX_BLOCK_SIZE;
  87. unsigned OffsetInZones = OffsetInBlocks >> FileSys->Super.s_log_zone_size;
  88. unsigned FirstZoneBlockOffset = OffsetInZones << FileSys->Super.s_log_zone_size;
  89. if(OffsetInZones < V2_NR_DZONES)
  90. {
  91. // Use direct zone
  92. Zone = FileInfo->d2_zone[OffsetInZones];
  93. if(!Zone && Alloc)
  94. {
  95. Zone = MxfsAllocZone(FileSys);
  96. if(Zone)
  97. {
  98. FileInfo->d2_zone[OffsetInZones] = Zone;
  99. MxfsWriteInode(FileSys, FileIdx, FileInfo);
  100. }
  101. }
  102. }
  103. else
  104. {
  105. zone_t IndirectZone;
  106. unsigned IndirectBlock, IndIndex;
  107. int Result;
  108. IndIndex = OffsetInZones - V2_NR_DZONES;
  109. if(IndIndex < V2_INDIRECTS)
  110. {
  111. // Use single indirect zone
  112. IndirectZone = FileInfo->d2_zone[V2_NR_DZONES];
  113. if(!IndirectZone && Alloc)
  114. {
  115. IndirectZone = MxfsAllocZone(FileSys);
  116. if(IndirectZone)
  117. {
  118. MxfsCacheZero(FileSys, IndirectZone << FileSys->Super.s_log_zone_size);
  119. FileInfo->d2_zone[V2_NR_DZONES] = IndirectZone;
  120. MxfsWriteInode(FileSys, FileIdx, FileInfo);
  121. }
  122. }
  123. }
  124. else // use double-indirect
  125. {
  126. zone_t DblIndZone;
  127. unsigned DblIndBlock, DblIndIndex;
  128. DblIndZone = FileInfo->d2_zone[V2_NR_DZONES + 1];
  129. if(!DblIndZone && Alloc)
  130. {
  131. DblIndZone = MxfsAllocZone(FileSys);
  132. if(DblIndZone)
  133. {
  134. MxfsCacheZero(FileSys, DblIndZone << FileSys->Super.s_log_zone_size);
  135. FileInfo->d2_zone[V2_NR_DZONES + 1] = DblIndZone;
  136. MxfsWriteInode(FileSys, FileIdx, FileInfo);
  137. }
  138. }
  139. if(!DblIndZone)
  140. return -ERROR_NOT_FOUND;
  141. DblIndBlock = DblIndZone << FileSys->Super.s_log_zone_size;
  142. IndIndex -= V2_INDIRECTS;
  143. DblIndIndex = IndIndex / V2_INDIRECTS;
  144. IndIndex = IndIndex % V2_INDIRECTS;
  145. if(DblIndIndex > V2_INDIRECTS)
  146. {
  147. ERR("Too big offset!\n");
  148. return -ERROR_INVALID_PARAMETER;
  149. }
  150. Result = MxfsCacheRead(FileSys, &IndirectZone, DblIndBlock, DblIndIndex*sizeof(zone_t), sizeof(zone_t));
  151. if(Result < 0)
  152. return Result;
  153. if(!IndirectZone && Alloc)
  154. {
  155. IndirectZone = MxfsAllocZone(FileSys);
  156. if(IndirectZone)
  157. {
  158. MxfsCacheZero(FileSys, IndirectZone << FileSys->Super.s_log_zone_size);
  159. MxfsCacheWrite(FileSys, &IndirectZone, DblIndBlock, DblIndIndex*sizeof(zone_t), sizeof(zone_t));
  160. }
  161. }
  162. }
  163. if(!IndirectZone)
  164. return -ERROR_NOT_FOUND;
  165. IndirectBlock = IndirectZone << FileSys->Super.s_log_zone_size;
  166. Result = MxfsCacheRead(FileSys, &Zone, IndirectBlock, IndIndex*sizeof(zone_t), sizeof(zone_t));
  167. if(Result < 0)
  168. return Result;
  169. if(!Zone && Alloc)
  170. {
  171. Zone = MxfsAllocZone(FileSys);
  172. if(Zone)
  173. {
  174. MxfsCacheZero(FileSys, Zone << FileSys->Super.s_log_zone_size);
  175. MxfsCacheWrite(FileSys, &Zone, IndirectBlock, IndIndex*sizeof(zone_t), sizeof(zone_t));
  176. }
  177. }
  178. }
  179. if(!Zone)
  180. return -ERROR_NOT_FOUND;
  181. unsigned Bit = MxfsBitFromZone(FileSys, Zone);
  182. if(!MxfsGetBit(&FileSys->ZoneMap, Bit))
  183. {
  184. WARN("NOT GOOD! Bit %u is not set\n", Bit);
  185. }
  186. Block = Zone << FileSys->Super.s_log_zone_size;
  187. Block += OffsetInBlocks - FirstZoneBlockOffset; // add offset in zone
  188. return Block;
  189. }
  190. int DOKAN_CALLBACK MxfsSetEndOfFile(
  191. LPCWSTR FileName,
  192. LONGLONG Length,
  193. PDOKAN_FILE_INFO FileInfo)
  194. {
  195. TRACE("%ls", FileName);
  196. MINIX_FS *FileSys = (MINIX_FS*)(LONG_PTR)FileInfo->DokanOptions->GlobalContext;
  197. FILE_CTX *FileCtx = (FILE_CTX*)(LONG_PTR)FileInfo->Context;
  198. ASSERT(FileCtx && Length >= 0);
  199. minix_inode Info;
  200. int Result = MxfsReadInode(FileSys, FileCtx->Index, &Info);
  201. if(Result < 0)
  202. return Result;
  203. unsigned ZoneSize = MINIX_BLOCK_SIZE << FileSys->Super.s_log_zone_size;
  204. unsigned NewFirstFree = ALIGN_UP(Length, ZoneSize)/ZoneSize;
  205. unsigned FirstFree = ALIGN_UP(Info.d2_size, ZoneSize)/ZoneSize;
  206. if(NewFirstFree >= FirstFree)
  207. return 0; // done
  208. unsigned i = NewFirstFree;
  209. while(i < V2_NR_DZONES)
  210. {
  211. if(Info.d2_zone[i])
  212. MxfsFreeZone(FileSys, Info.d2_zone[i]);
  213. Info.d2_zone[i] = 0;
  214. ++i;
  215. }
  216. i -= V2_NR_DZONES;
  217. unsigned IndirZone = Info.d2_zone[V2_NR_DZONES];
  218. if(IndirZone && i < V2_INDIRECTS)
  219. {
  220. unsigned IndirBlock = IndirZone << FileSys->Super.s_log_zone_size;
  221. zone_t IndirTbl[V2_INDIRECTS];
  222. BOOL FreeIndirZone = (i == 0);
  223. Result = MxfsCacheRead(FileSys, IndirTbl, IndirBlock, 0, MINIX_BLOCK_SIZE);
  224. if(Result >= 0)
  225. {
  226. while(i < V2_INDIRECTS)
  227. {
  228. if(IndirTbl[i])
  229. MxfsFreeZone(FileSys, IndirTbl[i]);
  230. IndirTbl[i] = 0;
  231. ++i;
  232. }
  233. if(FreeIndirZone)
  234. MxfsFreeZone(FileSys, IndirZone);
  235. else
  236. MxfsCacheWrite(FileSys, IndirTbl, IndirBlock, 0, MINIX_BLOCK_SIZE);
  237. } else
  238. ERR("Failed to read indirect block\n");
  239. }
  240. // TODO: Free dbl indirect block zones!
  241. Info.d2_size = Length;
  242. Info.d2_mtime = time(NULL);
  243. MxfsWriteInode(FileSys, FileCtx->Index, &Info);
  244. return 0;
  245. }