fat32.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  1. #include "fat32.h"
  2. #include <common/kprint.h>
  3. #include <driver/disk/ahci/ahci.h>
  4. #include <filesystem/MBR.h>
  5. #include <process/spinlock.h>
  6. #include <mm/slab.h>
  7. #include <common/errno.h>
  8. #include <common/stdio.h>
  9. struct vfs_super_block_operations_t fat32_sb_ops;
  10. struct vfs_dir_entry_operations_t fat32_dEntry_ops;
  11. struct vfs_file_operations_t fat32_file_ops;
  12. struct vfs_inode_operations_t fat32_inode_ops;
  13. /**
  14. * @brief 注册指定磁盘上的指定分区的fat32文件系统
  15. *
  16. * @param ahci_ctrl_num ahci控制器编号
  17. * @param ahci_port_num ahci控制器端口编号
  18. * @param part_num 磁盘分区编号
  19. *
  20. * @return struct vfs_super_block_t * 文件系统的超级块
  21. */
  22. struct vfs_superblock_t *fat32_register_partition(uint8_t ahci_ctrl_num, uint8_t ahci_port_num, uint8_t part_num)
  23. {
  24. struct MBR_disk_partition_table_t *DPT = MBR_read_partition_table(ahci_ctrl_num, ahci_port_num);
  25. // for(i = 0 ;i < 512 ; i++)
  26. // color_printk(PURPLE,WHITE,"%02x",buf[i]);
  27. printk_color(ORANGE, BLACK, "DPTE[0] start_LBA:%#018lx\ttype:%#018lx\n", DPT->DPTE[part_num].starting_LBA, DPT->DPTE[part_num].type);
  28. uint8_t buf[512] = {0};
  29. // 读取文件系统的boot扇区
  30. ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, DPT->DPTE[part_num].starting_LBA, 1, (uint64_t)&buf, ahci_ctrl_num, ahci_port_num);
  31. // 挂载文件系统到vfs
  32. return vfs_mount_fs("FAT32", (void *)(&DPT->DPTE[part_num]), VFS_DPT_MBR, buf, ahci_ctrl_num, ahci_port_num, part_num);
  33. }
  34. /**
  35. * @brief 计算短目录项文件名的校验和
  36. *
  37. * @param name 短目录项文件名字符串(长度为11)
  38. * @return uint8_t 校验和
  39. */
  40. static uint8_t fat32_ChkSum(uint8_t *name)
  41. {
  42. uint8_t chksum = 0;
  43. for (uint8_t i = 0; i < 11; ++i)
  44. {
  45. chksum = ((chksum & 1) ? 0x80 : 0) + (chksum >> 1) + *name;
  46. ++name;
  47. }
  48. return chksum;
  49. }
  50. /**
  51. * @brief 读取指定簇的FAT表项
  52. *
  53. * @param fsbi fat32超级块私有信息结构体
  54. * @param cluster 指定簇
  55. * @return uint32_t 下一个簇的簇号
  56. */
  57. uint32_t fat32_read_FAT_entry(fat32_sb_info_t *fsbi, uint32_t cluster)
  58. {
  59. // 计算每个扇区内含有的FAT表项数
  60. // FAT每项4bytes
  61. uint32_t fat_ent_per_sec = (fsbi->bytes_per_sec >> 2); // 该值应为2的n次幂
  62. uint32_t buf[256];
  63. memset(buf, 0, fsbi->bytes_per_sec);
  64. // 读取一个sector的数据,
  65. ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, fsbi->FAT1_base_sector + (cluster / fat_ent_per_sec), 1,
  66. (uint64_t)&buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  67. // 返回下一个fat表项的值(也就是下一个cluster)
  68. return buf[cluster & (fat_ent_per_sec - 1)] & 0x0fffffff;
  69. }
  70. /**
  71. * @brief 写入指定簇的FAT表项
  72. *
  73. * @param fsbi fat32超级块私有信息结构体
  74. * @param cluster 指定簇
  75. * @param value 要写入该fat表项的值
  76. * @return uint32_t errcode
  77. */
  78. uint32_t fat32_write_FAT_entry(fat32_sb_info_t *fsbi, uint32_t cluster, uint32_t value)
  79. {
  80. // 计算每个扇区内含有的FAT表项数
  81. // FAT每项4bytes
  82. uint32_t fat_ent_per_sec = (fsbi->bytes_per_sec >> 2); // 该值应为2的n次幂
  83. uint32_t *buf = kmalloc(fsbi->bytes_per_sec, 0);
  84. memset(buf, 0, fsbi->bytes_per_sec);
  85. ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, fsbi->FAT1_base_sector + (cluster / fat_ent_per_sec), 1,
  86. (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  87. buf[cluster & (fat_ent_per_sec - 1)] = (buf[cluster & (fat_ent_per_sec - 1)] & 0xf0000000) | (value & 0x0fffffff);
  88. // 向FAT1和FAT2写入数据
  89. ahci_operation.transfer(AHCI_CMD_WRITE_DMA_EXT, fsbi->FAT1_base_sector + (cluster / fat_ent_per_sec), 1,
  90. (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  91. ahci_operation.transfer(AHCI_CMD_WRITE_DMA_EXT, fsbi->FAT2_base_sector + (cluster / fat_ent_per_sec), 1,
  92. (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  93. kfree(buf);
  94. return 0;
  95. }
  96. /**
  97. * @brief 在父目录中寻找指定的目录项
  98. *
  99. * @param parent_inode 父目录项的inode
  100. * @param dest_dentry 搜索目标目录项
  101. * @return struct vfs_dir_entry_t* 目标目录项
  102. */
  103. struct vfs_dir_entry_t *fat32_lookup(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_t *dest_dentry)
  104. {
  105. int errcode = 0;
  106. struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)parent_inode->private_inode_info;
  107. fat32_sb_info_t *fsbi = (fat32_sb_info_t *)parent_inode->sb->private_sb_info;
  108. uint8_t *buf = kmalloc(fsbi->bytes_per_clus, 0);
  109. memset(buf, 0, fsbi->bytes_per_clus);
  110. // 计算父目录项的起始簇号
  111. uint32_t cluster = finode->first_clus;
  112. struct fat32_Directory_t *tmp_dEntry = NULL;
  113. while (true)
  114. {
  115. // 计算父目录项的起始LBA扇区号
  116. uint64_t sector = fsbi->first_data_sector + (cluster - 2) * fsbi->sec_per_clus;
  117. // kdebug("fat32_part_info[part_id].bootsector.BPB_SecPerClus=%d",fat32_part_info[part_id].bootsector.BPB_SecPerClus);
  118. // kdebug("sector=%d",sector);
  119. // 读取父目录项的起始簇数据
  120. ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  121. // ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, sector, fat32_part_info[part_id].bootsector.BPB_SecPerClus, (uint64_t)buf, fat32_part_info[part_id].ahci_ctrl_num, fat32_part_info[part_id].ahci_port_num);
  122. tmp_dEntry = (struct fat32_Directory_t *)buf;
  123. // 查找短目录项
  124. for (int i = 0; i < fsbi->bytes_per_clus; i += 32, ++tmp_dEntry)
  125. {
  126. // 跳过长目录项
  127. if (tmp_dEntry->DIR_Attr == ATTR_LONG_NAME)
  128. continue;
  129. // 跳过无效目录项、空闲目录项
  130. if (tmp_dEntry->DIR_Name[0] == 0xe5 || tmp_dEntry->DIR_Name[0] == 0x00 || tmp_dEntry->DIR_Name[0] == 0x05)
  131. continue;
  132. // kdebug("short name [%d] %s\n 33333==[%#02x]", i / 32, tmp_dEntry->DIR_Name, tmp_dEntry->DIR_Name[3]);
  133. // 找到长目录项,位于短目录项之前
  134. struct fat32_LongDirectory_t *tmp_ldEntry = (struct fat32_LongDirectory_t *)tmp_dEntry - 1;
  135. int js = 0;
  136. // 遍历每个长目录项
  137. while (tmp_ldEntry->LDIR_Attr == ATTR_LONG_NAME && tmp_ldEntry->LDIR_Ord != 0xe5)
  138. {
  139. // 比较name1
  140. for (int x = 0; x < 5; ++x)
  141. {
  142. if (js > dest_dentry->name_length && tmp_ldEntry->LDIR_Name1[x] == 0xffff)
  143. continue;
  144. else if (js > dest_dentry->name_length || tmp_ldEntry->LDIR_Name1[x] != (uint16_t)(dest_dentry->name[js++])) // 文件名不匹配,检索下一个短目录项
  145. goto continue_cmp_fail;
  146. }
  147. // 比较name2
  148. for (int x = 0; x < 6; ++x)
  149. {
  150. if (js > dest_dentry->name_length && tmp_ldEntry->LDIR_Name2[x] == 0xffff)
  151. continue;
  152. else if (js > dest_dentry->name_length || tmp_ldEntry->LDIR_Name2[x] != (uint16_t)(dest_dentry->name[js++])) // 文件名不匹配,检索下一个短目录项
  153. goto continue_cmp_fail;
  154. }
  155. // 比较name3
  156. for (int x = 0; x < 2; ++x)
  157. {
  158. if (js > dest_dentry->name_length && tmp_ldEntry->LDIR_Name3[x] == 0xffff)
  159. continue;
  160. else if (js > dest_dentry->name_length || tmp_ldEntry->LDIR_Name3[x] != (uint16_t)(dest_dentry->name[js++])) // 文件名不匹配,检索下一个短目录项
  161. goto continue_cmp_fail;
  162. }
  163. if (js >= dest_dentry->name_length) // 找到需要的目录项,返回
  164. {
  165. goto find_lookup_success;
  166. }
  167. --tmp_ldEntry; // 检索下一个长目录项
  168. }
  169. // 不存在长目录项,匹配短目录项的基础名
  170. js = 0;
  171. for (int x = 0; x < 8; ++x)
  172. {
  173. // kdebug("value = %#02x", tmp_dEntry->DIR_Name[x]);
  174. switch (tmp_dEntry->DIR_Name[x])
  175. {
  176. case ' ':
  177. if (!(tmp_dEntry->DIR_Attr & ATTR_DIRECTORY)) // 不是文件夹(是文件)
  178. {
  179. if (dest_dentry->name[js] == '.')
  180. continue;
  181. else if (tmp_dEntry->DIR_Name[x] == dest_dentry->name[js])
  182. {
  183. ++js;
  184. break;
  185. }
  186. else
  187. goto continue_cmp_fail;
  188. }
  189. else // 是文件夹
  190. {
  191. if (js < dest_dentry->name_length && tmp_dEntry->DIR_Name[x] == dest_dentry->name[js]) // 当前位正确匹配
  192. {
  193. ++js;
  194. break; // 进行下一位的匹配
  195. }
  196. else if (js == dest_dentry->name_length)
  197. continue;
  198. else
  199. goto continue_cmp_fail;
  200. }
  201. break;
  202. // 当前位是字母
  203. case 'A' ... 'Z':
  204. case 'a' ... 'z':
  205. if (tmp_dEntry->DIR_NTRes & LOWERCASE_BASE) // 为兼容windows系统,检测DIR_NTRes字段
  206. {
  207. if (js < dest_dentry->name_length && (tmp_dEntry->DIR_Name[x] + 32 == dest_dentry->name[js]))
  208. {
  209. ++js;
  210. break;
  211. }
  212. else
  213. goto continue_cmp_fail;
  214. }
  215. else
  216. {
  217. if (js < dest_dentry->name_length && tmp_dEntry->DIR_Name[x] == dest_dentry->name[js])
  218. {
  219. ++js;
  220. break;
  221. }
  222. else
  223. goto continue_cmp_fail;
  224. }
  225. break;
  226. case '0' ... '9':
  227. if (js < dest_dentry->name_length && tmp_dEntry->DIR_Name[x] == dest_dentry->name[js])
  228. {
  229. ++js;
  230. break;
  231. }
  232. else
  233. goto continue_cmp_fail;
  234. break;
  235. default:
  236. ++js;
  237. break;
  238. }
  239. }
  240. // 若短目录项为文件,则匹配扩展名
  241. if (!(tmp_dEntry->DIR_Attr & ATTR_DIRECTORY))
  242. {
  243. ++js;
  244. for (int x = 8; x < 11; ++x)
  245. {
  246. switch (tmp_dEntry->DIR_Name[x])
  247. {
  248. // 当前位是字母
  249. case 'A' ... 'Z':
  250. case 'a' ... 'z':
  251. if (tmp_dEntry->DIR_NTRes & LOWERCASE_EXT) // 为兼容windows系统,检测DIR_NTRes字段
  252. {
  253. if ((tmp_dEntry->DIR_Name[x] + 32 == dest_dentry->name[js]))
  254. {
  255. ++js;
  256. break;
  257. }
  258. else
  259. goto continue_cmp_fail;
  260. }
  261. else
  262. {
  263. if (tmp_dEntry->DIR_Name[x] == dest_dentry->name[js])
  264. {
  265. ++js;
  266. break;
  267. }
  268. else
  269. goto continue_cmp_fail;
  270. }
  271. break;
  272. case '0' ... '9':
  273. case ' ':
  274. if (tmp_dEntry->DIR_Name[x] == dest_dentry->name[js])
  275. {
  276. ++js;
  277. break;
  278. }
  279. else
  280. goto continue_cmp_fail;
  281. break;
  282. default:
  283. goto continue_cmp_fail;
  284. break;
  285. }
  286. }
  287. }
  288. goto find_lookup_success;
  289. continue_cmp_fail:;
  290. }
  291. // 当前簇没有发现目标文件名,寻找下一个簇
  292. cluster = fat32_read_FAT_entry(fsbi, cluster);
  293. if (cluster >= 0x0ffffff7) // 寻找完父目录的所有簇,都没有找到目标文件名
  294. {
  295. kfree(buf);
  296. return NULL;
  297. }
  298. }
  299. find_lookup_success:; // 找到目标dentry
  300. struct vfs_index_node_t *p = (struct vfs_index_node_t *)kmalloc(sizeof(struct vfs_index_node_t), 0);
  301. memset(p, 0, sizeof(struct vfs_index_node_t));
  302. p->file_size = tmp_dEntry->DIR_FileSize;
  303. // 计算文件占用的扇区数, 由于最小存储单位是簇,因此需要按照簇的大小来对齐扇区
  304. p->blocks = (p->file_size + fsbi->bytes_per_clus - 1) / fsbi->bytes_per_sec;
  305. p->attribute = (tmp_dEntry->DIR_Attr & ATTR_DIRECTORY) ? VFS_ATTR_DIR : VFS_ATTR_FILE;
  306. p->sb = parent_inode->sb;
  307. p->file_ops = &fat32_file_ops;
  308. p->inode_ops = &fat32_inode_ops;
  309. // 为inode的与文件系统相关的信息结构体分配空间
  310. p->private_inode_info = (void *)kmalloc(sizeof(fat32_inode_info_t), 0);
  311. memset(p->private_inode_info, 0, sizeof(fat32_inode_info_t));
  312. finode = (fat32_inode_info_t *)p->private_inode_info;
  313. finode->first_clus = ((tmp_dEntry->DIR_FstClusHI << 16) | tmp_dEntry->DIR_FstClusLO) & 0x0fffffff;
  314. finode->dEntry_location_clus = cluster;
  315. finode->dEntry_location_clus_offset = tmp_dEntry - (struct fat32_Directory_t *)buf; //计算dentry的偏移量
  316. // kdebug("finode->dEntry_location_clus=%#018lx", finode->dEntry_location_clus);
  317. // kdebug("finode->dEntry_location_clus_offset=%#018lx", finode->dEntry_location_clus_offset);
  318. finode->create_date = tmp_dEntry->DIR_CrtDate;
  319. finode->create_time = tmp_dEntry->DIR_CrtTime;
  320. finode->write_date = tmp_dEntry->DIR_WrtDate;
  321. finode->write_time = tmp_dEntry->DIR_WrtTime;
  322. // 暂时使用fat32的高4bit来标志设备文件
  323. // todo: 引入devfs后删除这段代码
  324. if ((tmp_dEntry->DIR_FstClusHI >> 12) && (p->attribute & VFS_ATTR_FILE))
  325. p->attribute |= VFS_ATTR_DEVICE;
  326. dest_dentry->dir_inode = p;
  327. dest_dentry->dir_ops = &fat32_dEntry_ops;
  328. list_init(&dest_dentry->child_node_list);
  329. list_init(&dest_dentry->subdirs_list);
  330. kfree(buf);
  331. return dest_dentry;
  332. }
  333. /**
  334. * @brief 创建fat32文件系统的超级块
  335. *
  336. * @param DPTE 磁盘分区表entry
  337. * @param DPT_type 磁盘分区表类型
  338. * @param buf fat32文件系统的引导扇区
  339. * @return struct vfs_superblock_t* 创建好的超级块
  340. */
  341. struct vfs_superblock_t *fat32_read_superblock(void *DPTE, uint8_t DPT_type, void *buf, int8_t ahci_ctrl_num, int8_t ahci_port_num, int8_t part_num)
  342. {
  343. if (DPT_type != VFS_DPT_MBR) // 暂时只支持MBR分区表
  344. {
  345. kerror("fat32_read_superblock(): Unsupported DPT!");
  346. return NULL;
  347. }
  348. // 分配超级块的空间
  349. struct vfs_superblock_t *sb_ptr = (struct vfs_superblock_t *)kmalloc(sizeof(struct vfs_superblock_t), 0);
  350. memset(sb_ptr, 0, sizeof(struct vfs_superblock_t));
  351. sb_ptr->sb_ops = &fat32_sb_ops;
  352. sb_ptr->private_sb_info = kmalloc(sizeof(fat32_sb_info_t), 0);
  353. memset(sb_ptr->private_sb_info, 0, sizeof(fat32_sb_info_t));
  354. struct fat32_BootSector_t *fbs = (struct fat32_BootSector_t *)buf;
  355. fat32_sb_info_t *fsbi = (fat32_sb_info_t *)(sb_ptr->private_sb_info);
  356. // MBR分区表entry
  357. struct MBR_disk_partition_table_entry_t *MBR_DPTE = (struct MBR_disk_partition_table_entry_t *)DPTE;
  358. fsbi->ahci_ctrl_num = ahci_ctrl_num;
  359. fsbi->ahci_port_num = ahci_port_num;
  360. fsbi->part_num = part_num;
  361. fsbi->starting_sector = MBR_DPTE->starting_LBA;
  362. fsbi->sector_count = MBR_DPTE->total_sectors;
  363. fsbi->sec_per_clus = fbs->BPB_SecPerClus;
  364. fsbi->bytes_per_clus = fbs->BPB_SecPerClus * fbs->BPB_BytesPerSec;
  365. fsbi->bytes_per_sec = fbs->BPB_BytesPerSec;
  366. fsbi->first_data_sector = MBR_DPTE->starting_LBA + fbs->BPB_RsvdSecCnt + fbs->BPB_FATSz32 * fbs->BPB_NumFATs;
  367. fsbi->FAT1_base_sector = MBR_DPTE->starting_LBA + fbs->BPB_RsvdSecCnt;
  368. fsbi->FAT2_base_sector = fsbi->FAT1_base_sector + fbs->BPB_FATSz32;
  369. fsbi->sec_per_FAT = fbs->BPB_FATSz32;
  370. fsbi->NumFATs = fbs->BPB_NumFATs;
  371. fsbi->fsinfo_sector_addr_infat = fbs->BPB_FSInfo;
  372. fsbi->bootsector_bak_sector_addr_infat = fbs->BPB_BkBootSec;
  373. printk_color(ORANGE, BLACK, "FAT32 Boot Sector\n\tBPB_FSInfo:%#018lx\n\tBPB_BkBootSec:%#018lx\n\tBPB_TotSec32:%#018lx\n", fbs->BPB_FSInfo, fbs->BPB_BkBootSec, fbs->BPB_TotSec32);
  374. // fsinfo扇区的信息
  375. memset(&fsbi->fsinfo, 0, sizeof(struct fat32_FSInfo_t));
  376. ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, MBR_DPTE->starting_LBA + fbs->BPB_FSInfo, 1, (uint64_t)&fsbi->fsinfo, ahci_ctrl_num, ahci_port_num);
  377. printk_color(BLUE, BLACK, "FAT32 FSInfo\n\tFSI_LeadSig:%#018lx\n\tFSI_StrucSig:%#018lx\n\tFSI_Free_Count:%#018lx\n", fsbi->fsinfo.FSI_LeadSig, fsbi->fsinfo.FSI_StrucSig, fsbi->fsinfo.FSI_Free_Count);
  378. // 初始化超级块的dir entry
  379. sb_ptr->root = (struct vfs_dir_entry_t *)kmalloc(sizeof(struct vfs_dir_entry_t), 0);
  380. memset(sb_ptr->root, 0, sizeof(struct vfs_dir_entry_t));
  381. list_init(&sb_ptr->root->child_node_list);
  382. list_init(&sb_ptr->root->subdirs_list);
  383. sb_ptr->root->parent = sb_ptr->root;
  384. sb_ptr->root->dir_ops = &fat32_dEntry_ops;
  385. // 分配2个字节的name
  386. sb_ptr->root->name = (char *)(kmalloc(2, 0));
  387. sb_ptr->root->name[0] = '/';
  388. sb_ptr->root->name_length = 1;
  389. // 为root目录项分配index node
  390. sb_ptr->root->dir_inode = (struct vfs_index_node_t *)kmalloc(sizeof(struct vfs_index_node_t), 0);
  391. memset(sb_ptr->root->dir_inode, 0, sizeof(struct vfs_index_node_t));
  392. sb_ptr->root->dir_inode->inode_ops = &fat32_inode_ops;
  393. sb_ptr->root->dir_inode->file_ops = &fat32_file_ops;
  394. sb_ptr->root->dir_inode->file_size = 0;
  395. // 计算文件占用的扇区数, 由于最小存储单位是簇,因此需要按照簇的大小来对齐扇区
  396. sb_ptr->root->dir_inode->blocks = (sb_ptr->root->dir_inode->file_size + fsbi->bytes_per_clus - 1) / fsbi->bytes_per_sec;
  397. sb_ptr->root->dir_inode->attribute = VFS_ATTR_DIR;
  398. sb_ptr->root->dir_inode->sb = sb_ptr; // 反向绑定对应的超级块
  399. // 初始化inode信息
  400. sb_ptr->root->dir_inode->private_inode_info = kmalloc(sizeof(struct fat32_inode_info_t), 0);
  401. memset(sb_ptr->root->dir_inode->private_inode_info, 0, sizeof(struct fat32_inode_info_t));
  402. struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)sb_ptr->root->dir_inode->private_inode_info;
  403. finode->first_clus = fbs->BPB_RootClus;
  404. finode->dEntry_location_clus = 0;
  405. finode->dEntry_location_clus_offset = 0;
  406. finode->create_time = 0;
  407. finode->create_date = 0;
  408. finode->write_date = 0;
  409. finode->write_time;
  410. return sb_ptr;
  411. }
  412. /**
  413. * @brief todo: 写入superblock
  414. *
  415. * @param sb
  416. */
  417. void fat32_write_superblock(struct vfs_superblock_t *sb)
  418. {
  419. }
  420. /**
  421. * @brief 释放superblock的内存空间
  422. *
  423. * @param sb 要被释放的superblock
  424. */
  425. void fat32_put_superblock(struct vfs_superblock_t *sb)
  426. {
  427. kfree(sb->private_sb_info);
  428. kfree(sb->root->dir_inode->private_inode_info);
  429. kfree(sb->root->dir_inode);
  430. kfree(sb->root);
  431. kfree(sb);
  432. }
  433. /**
  434. * @brief 写入inode到硬盘上
  435. *
  436. * @param inode
  437. */
  438. void fat32_write_inode(struct vfs_index_node_t *inode)
  439. {
  440. fat32_inode_info_t *finode = inode->private_inode_info;
  441. if (finode->dEntry_location_clus == 0)
  442. {
  443. kerror("FAT32 error: Attempt to write the root inode");
  444. return;
  445. }
  446. fat32_sb_info_t *fsbi = (fat32_sb_info_t *)inode->sb->private_sb_info;
  447. // 计算目标inode对应数据区的LBA地址
  448. uint64_t fLBA = fsbi->first_data_sector + (finode->dEntry_location_clus - 2) * fsbi->sec_per_clus;
  449. struct fat32_Directory_t *buf = (struct fat32_Directory_t *)kmalloc(fsbi->bytes_per_clus, 0);
  450. memset(buf, 0, fsbi->bytes_per_clus);
  451. ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, fLBA, fsbi->sec_per_clus, (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  452. // 计算目标dEntry所在的位置
  453. struct fat32_Directory_t *fdEntry = buf + finode->dEntry_location_clus_offset;
  454. // 写入fat32文件系统的dir_entry
  455. fdEntry->DIR_FileSize = inode->file_size;
  456. fdEntry->DIR_FstClusLO = finode->first_clus & 0xffff;
  457. fdEntry->DIR_FstClusHI = (finode->first_clus >> 16) | (fdEntry->DIR_FstClusHI & 0xf000);
  458. // 将dir entry写回磁盘
  459. ahci_operation.transfer(AHCI_CMD_WRITE_DMA_EXT, fLBA, fsbi->sec_per_clus, (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  460. kfree(buf);
  461. }
  462. struct vfs_super_block_operations_t fat32_sb_ops =
  463. {
  464. .write_superblock = fat32_write_superblock,
  465. .put_superblock = fat32_put_superblock,
  466. .write_inode = fat32_write_inode,
  467. };
  468. // todo: compare
  469. long fat32_compare(struct vfs_dir_entry_t *parent_dEntry, char *source_filename, char *dest_filename)
  470. {
  471. }
  472. // todo: hash
  473. long fat32_hash(struct vfs_dir_entry_t *dEntry, char *filename)
  474. {
  475. }
  476. // todo: release
  477. long fat32_release(struct vfs_dir_entry_t *dEntry)
  478. {
  479. }
  480. // todo: iput
  481. long fat32_iput(struct vfs_dir_entry_t *dEntry, struct vfs_index_node_t *inode)
  482. {
  483. }
  484. /**
  485. * @brief fat32文件系统对于dEntry的操作
  486. *
  487. */
  488. struct vfs_dir_entry_operations_t fat32_dEntry_ops =
  489. {
  490. .compare = fat32_compare,
  491. .hash = fat32_hash,
  492. .release = fat32_release,
  493. .iput = fat32_iput,
  494. };
  495. // todo: open
  496. long fat32_open(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr)
  497. {
  498. return VFS_SUCCESS;
  499. }
  500. // todo: close
  501. long fat32_close(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr)
  502. {
  503. return VFS_SUCCESS;
  504. }
  505. /**
  506. * @brief 从fat32文件系统读取数据
  507. *
  508. * @param file_ptr 文件描述符
  509. * @param buf 输出缓冲区
  510. * @param count 要读取的字节数
  511. * @param position 文件指针位置
  512. * @return long 执行成功:传输的字节数量 执行失败:错误码(小于0)
  513. */
  514. long fat32_read(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *position)
  515. {
  516. struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)(file_ptr->dEntry->dir_inode->private_inode_info);
  517. fat32_sb_info_t *fsbi = (fat32_sb_info_t *)(file_ptr->dEntry->dir_inode->sb->private_sb_info);
  518. // First cluster num of the file
  519. uint64_t cluster = finode->first_clus;
  520. // kdebug("fsbi->bytes_per_clus=%d fsbi->sec_per_clus=%d finode->first_clus=%d cluster=%d", fsbi->bytes_per_clus, fsbi->sec_per_clus, finode->first_clus, cluster);
  521. // kdebug("fsbi->bytes_per_clus=%d", fsbi->bytes_per_clus);
  522. // clus offset in file
  523. uint64_t clus_offset_in_file = (*position) / fsbi->bytes_per_clus;
  524. // bytes offset in clus
  525. uint64_t bytes_offset = (*position) % fsbi->bytes_per_clus;
  526. if (!cluster)
  527. return -EFAULT;
  528. // find the actual cluster on disk of the specified position
  529. for (int i = 0; i < clus_offset_in_file; ++i)
  530. cluster = fat32_read_FAT_entry(fsbi, cluster);
  531. // 如果需要读取的数据边界大于文件大小
  532. if (*position + count > file_ptr->dEntry->dir_inode->file_size)
  533. count = file_ptr->dEntry->dir_inode->file_size - *position;
  534. // 剩余还需要传输的字节数量
  535. int64_t bytes_remain = count;
  536. // alloc buffer memory space for ahci transfer
  537. void *tmp_buffer = kmalloc(fsbi->bytes_per_clus, 0);
  538. int64_t retval = 0;
  539. do
  540. {
  541. memset(tmp_buffer, 0, fsbi->bytes_per_clus);
  542. uint64_t sector = fsbi->first_data_sector + (cluster - 2) * fsbi->sec_per_clus;
  543. // 读取一个簇的数据
  544. int errno = ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)tmp_buffer, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  545. if (errno != AHCI_SUCCESS)
  546. {
  547. kerror("FAT32 FS(read) error!");
  548. retval = -EIO;
  549. break;
  550. }
  551. int64_t step_trans_len = 0; // 当前循环传输的字节数
  552. if (bytes_remain > (fsbi->bytes_per_clus - bytes_offset))
  553. step_trans_len = (fsbi->bytes_per_clus - bytes_offset);
  554. else
  555. step_trans_len = bytes_remain;
  556. if (((uint64_t)buf) < USER_MAX_LINEAR_ADDR)
  557. copy_to_user(buf, tmp_buffer + bytes_offset, step_trans_len);
  558. else
  559. memcpy(buf, tmp_buffer + bytes_offset, step_trans_len);
  560. bytes_remain -= step_trans_len;
  561. buf += step_trans_len;
  562. bytes_offset -= bytes_offset;
  563. *position += step_trans_len; // 更新文件指针
  564. cluster = fat32_read_FAT_entry(fsbi, cluster);
  565. } while (bytes_remain && (cluster < 0x0ffffff8) && cluster != 0);
  566. kfree(tmp_buffer);
  567. if (!bytes_remain)
  568. retval = count;
  569. return retval;
  570. }
  571. /**
  572. * @brief 在磁盘中寻找一个空闲的簇
  573. *
  574. * @param fsbi fat32超级块信息结构体
  575. * @return uint64_t 空闲簇号(找不到则返回0)
  576. */
  577. uint64_t fat32_find_available_cluster(fat32_sb_info_t *fsbi)
  578. {
  579. uint64_t sec_per_fat = fsbi->sec_per_FAT;
  580. // 申请1扇区的缓冲区
  581. uint32_t *buf = (uint32_t *)kmalloc(fsbi->bytes_per_sec, 0);
  582. int ent_per_sec = (fsbi->bytes_per_sec >> 2);
  583. for (int i = 0; i < sec_per_fat; ++i)
  584. {
  585. memset(buf, 0, fsbi->bytes_per_sec);
  586. ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, fsbi->FAT1_base_sector + i, 1, (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  587. // 依次检查簇是否空闲
  588. for (int j = 0; j < ent_per_sec; ++j)
  589. {
  590. // 找到空闲簇
  591. if ((buf[j] & 0x0fffffff) == 0)
  592. {
  593. kfree(buf);
  594. return i * ent_per_sec + j;
  595. }
  596. }
  597. }
  598. kfree(buf);
  599. return 0;
  600. }
  601. /**
  602. * @brief 向fat32文件系统写入数据
  603. *
  604. * @param file_ptr 文件描述符
  605. * @param buf 输入写入的字节数
  606. * @param position 文件指针位置
  607. * @return long 执行成功:传输的字节数量 执行失败:错误码(小于0)
  608. */
  609. long fat32_write(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *position)
  610. {
  611. struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)file_ptr->dEntry->dir_inode->private_inode_info;
  612. fat32_sb_info_t *fsbi = (fat32_sb_info_t *)(file_ptr->dEntry->dir_inode->sb->private_sb_info);
  613. // First cluster num of the file
  614. uint64_t cluster = finode->first_clus;
  615. int64_t flags = 0;
  616. // kdebug("fsbi->bytes_per_clus=%d fsbi->sec_per_clus=%d finode->first_clus=%d *position=%d", fsbi->bytes_per_clus, fsbi->sec_per_clus, finode->first_clus, *position);
  617. // kdebug("buf=%s", buf);
  618. // clus offset in file
  619. uint64_t clus_offset_in_file = (*position) / fsbi->bytes_per_clus;
  620. // bytes offset in clus
  621. uint64_t bytes_offset = (*position) % fsbi->bytes_per_clus;
  622. if (!cluster) // 起始簇号为0,说明是空文件
  623. {
  624. // 找一个可用的簇
  625. cluster = fat32_find_available_cluster(fsbi);
  626. flags = 1;
  627. }
  628. else
  629. {
  630. // 跳转到position所在的簇
  631. for (uint64_t i = 0; i < clus_offset_in_file; ++i)
  632. cluster = fat32_read_FAT_entry(fsbi, cluster);
  633. }
  634. // kdebug("cluster(start)=%d", cluster);
  635. // 没有可用的磁盘空间
  636. if (!cluster)
  637. return -ENOSPC;
  638. if (flags) // 空文件
  639. {
  640. // kdebug("empty file");
  641. finode->first_clus = cluster;
  642. // 写入目录项
  643. file_ptr->dEntry->dir_inode->sb->sb_ops->write_inode(file_ptr->dEntry->dir_inode);
  644. fat32_write_FAT_entry(fsbi, cluster, 0x0ffffff8); // 写入fat表项
  645. }
  646. int64_t bytes_remain = count;
  647. if (count < 0) // 要写入的字节数小于0
  648. return -EINVAL;
  649. uint64_t sector;
  650. int64_t retval = 0;
  651. void *tmp_buffer = kmalloc(fsbi->bytes_per_clus, 0);
  652. do
  653. {
  654. memset(tmp_buffer, 0, fsbi->bytes_per_clus);
  655. sector = fsbi->first_data_sector + (cluster - 2) * fsbi->sec_per_clus; // 计算对应的扇区
  656. if (!flags) // 当前簇已分配
  657. {
  658. // kdebug("read existed sec=%ld", sector);
  659. // 读取一个簇的数据
  660. int errno = ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)tmp_buffer, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  661. if (errno != AHCI_SUCCESS)
  662. {
  663. // kerror("FAT32 FS(write) read disk error!");
  664. retval = -EIO;
  665. break;
  666. }
  667. }
  668. int64_t step_trans_len = 0; // 当前循环传输的字节数
  669. if (bytes_remain > (fsbi->bytes_per_clus - bytes_offset))
  670. step_trans_len = (fsbi->bytes_per_clus - bytes_offset);
  671. else
  672. step_trans_len = bytes_remain;
  673. // kdebug("step_trans_len=%d, bytes_offset=%d", step_trans_len, bytes_offset);
  674. if (((uint64_t)buf) < USER_MAX_LINEAR_ADDR)
  675. copy_from_user(tmp_buffer + bytes_offset, buf, step_trans_len);
  676. else
  677. memcpy(tmp_buffer + bytes_offset, buf, step_trans_len);
  678. // 写入数据到对应的簇
  679. int errno = ahci_operation.transfer(AHCI_CMD_WRITE_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)tmp_buffer, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  680. if (errno != AHCI_SUCCESS)
  681. {
  682. kerror("FAT32 FS(write) write disk error!");
  683. retval = -EIO;
  684. break;
  685. }
  686. bytes_remain -= step_trans_len;
  687. buf += step_trans_len;
  688. bytes_offset -= bytes_offset;
  689. *position += step_trans_len; // 更新文件指针
  690. // kdebug("step_trans_len=%d", step_trans_len);
  691. int next_clus = 0;
  692. if (bytes_remain)
  693. next_clus = fat32_read_FAT_entry(fsbi, cluster);
  694. else
  695. break;
  696. if (next_clus >= 0x0ffffff8) // 已经到达了最后一个簇,需要分配新簇
  697. {
  698. next_clus = fat32_find_available_cluster(fsbi);
  699. if (!next_clus) // 没有空闲簇
  700. {
  701. kfree(tmp_buffer);
  702. return -ENOSPC;
  703. }
  704. // 将簇加入到文件末尾
  705. fat32_write_FAT_entry(fsbi, cluster, next_clus);
  706. fat32_write_FAT_entry(fsbi, next_clus, 0x0ffffff8);
  707. cluster = next_clus; // 切换当前簇
  708. flags = 1; // 标记当前簇是新分配的簇
  709. }
  710. } while (bytes_remain);
  711. // 文件大小有增长
  712. if (*position > (file_ptr->dEntry->dir_inode->file_size))
  713. {
  714. file_ptr->dEntry->dir_inode->file_size = *position;
  715. file_ptr->dEntry->dir_inode->sb->sb_ops->write_inode(file_ptr->dEntry->dir_inode);
  716. kdebug("new file size=%ld", *position);
  717. }
  718. kfree(tmp_buffer);
  719. if (!bytes_remain)
  720. retval = count;
  721. // kdebug("retval=%lld", retval);
  722. return retval;
  723. }
  724. /**
  725. * @brief 调整文件的当前访问位置
  726. *
  727. * @param file_ptr vfs文件指针
  728. * @param offset 调整的偏移量
  729. * @param whence 调整方法
  730. * @return long 更新后的指针位置
  731. */
  732. long fat32_lseek(struct vfs_file_t *file_ptr, long offset, long whence)
  733. {
  734. struct vfs_index_node_t *inode = file_ptr->dEntry->dir_inode;
  735. long pos = 0;
  736. switch (whence)
  737. {
  738. case SEEK_SET: // 相对于文件头
  739. pos = offset;
  740. break;
  741. case SEEK_CUR: // 相对于当前位置
  742. pos = file_ptr->position + offset;
  743. break;
  744. case SEEK_END: // 相对于文件末尾
  745. pos = file_ptr->dEntry->dir_inode->file_size + offset;
  746. break;
  747. default:
  748. return -EINVAL;
  749. break;
  750. }
  751. if (pos < 0 || pos > file_ptr->dEntry->dir_inode->file_size)
  752. return -EOVERFLOW;
  753. file_ptr->position = pos;
  754. // kdebug("fat32 lseek -> position=%d", file_ptr->position);
  755. return pos;
  756. }
  757. // todo: ioctl
  758. long fat32_ioctl(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr, uint64_t cmd, uint64_t arg)
  759. {
  760. }
  761. /**
  762. * @brief fat32文件系统,关于文件的操作
  763. *
  764. */
  765. struct vfs_file_operations_t fat32_file_ops =
  766. {
  767. .open = fat32_open,
  768. .close = fat32_close,
  769. .read = fat32_read,
  770. .write = fat32_write,
  771. .lseek = fat32_lseek,
  772. .ioctl = fat32_ioctl,
  773. .readdir = fat32_readdir,
  774. };
  775. // todo: create
  776. long fat32_create(struct vfs_index_node_t *inode, struct vfs_dir_entry_t *dentry, int mode)
  777. {
  778. }
  779. /**
  780. * @brief 在父亲inode的目录项簇中,寻找连续num个空的目录项
  781. *
  782. * @param parent_inode 父inode
  783. * @param num 请求的目录项数量
  784. * @param mode 操作模式
  785. * @param res_sector 返回信息:缓冲区对应的扇区号
  786. * @param res_cluster 返回信息:缓冲区对应的簇号
  787. * @param res_data_buf_base 返回信息:缓冲区的内存基地址(记得要释放缓冲区内存)
  788. * @return struct fat32_Directory_t* 符合要求的entry的指针(指向地址高处的空目录项,也就是说,有连续num个≤这个指针的空目录项)
  789. */
  790. struct fat32_Directory_t *fat32_find_empty_dentry(struct vfs_index_node_t *parent_inode, uint32_t num, uint32_t mode, uint32_t *res_sector, uint64_t *res_cluster, uint64_t *res_data_buf_base)
  791. {
  792. kdebug("find empty_dentry");
  793. struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)parent_inode->private_inode_info;
  794. fat32_sb_info_t *fsbi = (fat32_sb_info_t *)parent_inode->sb->private_sb_info;
  795. uint8_t *buf = kmalloc(fsbi->bytes_per_clus, 0);
  796. memset(buf, 0, fsbi->bytes_per_clus);
  797. // 计算父目录项的起始簇号
  798. uint32_t cluster = finode->first_clus;
  799. struct fat32_Directory_t *tmp_dEntry = NULL;
  800. // 指向最终的有用的dentry的指针
  801. struct fat32_Directory_t *result_dEntry = NULL;
  802. while (true)
  803. {
  804. // 计算父目录项的起始LBA扇区号
  805. uint64_t sector = fsbi->first_data_sector + (cluster - 2) * fsbi->sec_per_clus;
  806. // 读取父目录项的起始簇数据
  807. ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  808. tmp_dEntry = (struct fat32_Directory_t *)buf;
  809. // 计数连续的空目录项
  810. uint32_t count_continuity = 0;
  811. // 查找连续num个空闲目录项
  812. for (int i = 0; (i < fsbi->bytes_per_clus) && count_continuity < num; i += 32, ++tmp_dEntry)
  813. {
  814. if (!(tmp_dEntry->DIR_Name[0] == 0xe5 || tmp_dEntry->DIR_Name[0] == 0x00))
  815. {
  816. count_continuity = 0;
  817. continue;
  818. }
  819. if (count_continuity == 0)
  820. result_dEntry = tmp_dEntry;
  821. ++count_continuity;
  822. }
  823. // 成功查找到符合要求的目录项
  824. if (count_continuity == num)
  825. {
  826. result_dEntry += (num - 1);
  827. *res_sector = sector;
  828. *res_data_buf_base = (uint64_t)buf;
  829. *res_cluster = cluster;
  830. return result_dEntry;
  831. }
  832. // 当前簇没有发现符合条件的空闲目录项,寻找下一个簇
  833. uint old_cluster = cluster;
  834. cluster = fat32_read_FAT_entry(fsbi, cluster);
  835. if (cluster >= 0x0ffffff7) // 寻找完父目录的所有簇,都没有找到符合要求的空目录项
  836. {
  837. // 新增一个簇
  838. cluster = fat32_find_available_cluster(fsbi);
  839. kdebug("try to allocate a new cluster to parent dentry, cluster=%d, old_cluster=%d", cluster, old_cluster);
  840. if (cluster == 0)
  841. {
  842. kerror("Cannot allocate a new cluster!");
  843. while (1)
  844. pause();
  845. }
  846. fat32_write_FAT_entry(fsbi, old_cluster, cluster);
  847. fat32_write_FAT_entry(fsbi, cluster, 0x0ffffff8);
  848. // 将这个新的簇清空
  849. sector = fsbi->first_data_sector + (cluster - 2) * fsbi->sec_per_clus;
  850. void *tmp_buf = kmalloc(fsbi->bytes_per_clus, 0);
  851. memset(tmp_buf, 0, fsbi->bytes_per_clus);
  852. ahci_operation.transfer(AHCI_CMD_WRITE_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)tmp_buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  853. kfree(tmp_buf);
  854. }
  855. }
  856. }
  857. /**
  858. * @brief 创建文件夹
  859. * @param inode 父目录的inode
  860. * @param dEntry 新的文件夹的dentry
  861. * @param mode 创建文件夹的mode
  862. */
  863. int64_t fat32_mkdir(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_t *dEntry, int mode)
  864. {
  865. // 先检查是否有重名的目录项,然后分配一个簇
  866. // 文件系统超级块信息
  867. fat32_sb_info_t *fsbi = (fat32_sb_info_t *)parent_inode->sb->private_sb_info;
  868. // 父目录项的inode
  869. struct fat32_inode_info_t *parent_inode_info = (struct fat32_inode_info_t *)parent_inode->private_inode_info;
  870. // ======== todo:检验名称的合法性
  871. // ====== 找一块连续的区域放置新的目录项 =====
  872. // 计算总共需要多少个目录项
  873. uint32_t cnt_longname = (dEntry->name_length + 25) / 26;
  874. if (cnt_longname == 0)
  875. cnt_longname = 1;
  876. // 空闲dentry所在的扇区号
  877. uint32_t tmp_dentry_sector = 0;
  878. // 空闲dentry所在的缓冲区的基地址
  879. uint64_t tmp_dentry_clus_buf_addr = 0;
  880. uint64_t tmp_parent_dentry_clus = 0;
  881. // 寻找空闲目录项
  882. struct fat32_Directory_t *empty_fat32_dentry = fat32_find_empty_dentry(parent_inode, cnt_longname + 1, 0, &tmp_dentry_sector, &tmp_parent_dentry_clus, &tmp_dentry_clus_buf_addr);
  883. kdebug("found empty dentry");
  884. // ====== 为新的文件夹分配一个簇 =======
  885. uint32_t new_dir_clus = fat32_find_available_cluster(fsbi);
  886. kdebug("new_dir_clus=%d", new_dir_clus);
  887. fat32_write_FAT_entry(fsbi, new_dir_clus, 0x0ffffff8);
  888. // ====== 填写短目录项
  889. memset(empty_fat32_dentry, 0, sizeof(struct fat32_Directory_t));
  890. {
  891. int tmp_index = 0;
  892. // kdebug("dEntry->name_length=%d", dEntry->name_length);
  893. for (tmp_index = 0; tmp_index < min(8, dEntry->name_length); ++tmp_index)
  894. {
  895. if (dEntry->name[tmp_index] == '.')
  896. break;
  897. empty_fat32_dentry->DIR_Name[tmp_index] = dEntry->name[tmp_index];
  898. }
  899. // 不满的部分使用0x20填充
  900. while (tmp_index < 11)
  901. {
  902. // kdebug("tmp index = %d", tmp_index);
  903. dEntry->name[tmp_index] = 0x20;
  904. ++tmp_index;
  905. }
  906. }
  907. empty_fat32_dentry->DIR_Attr = ATTR_DIRECTORY;
  908. empty_fat32_dentry->DIR_FileSize = fsbi->bytes_per_clus;
  909. empty_fat32_dentry->DIR_FstClusHI = (uint16_t)((new_dir_clus >> 16) & 0x0fff);
  910. empty_fat32_dentry->DIR_FstClusLO = (uint16_t)(new_dir_clus & 0xffff);
  911. // 计算校验和
  912. uint8_t short_dentry_ChkSum = fat32_ChkSum(empty_fat32_dentry->DIR_Name);
  913. // todo: 填写短目录项中的时间信息
  914. // ======== 填写长目录项
  915. uint32_t current_name_index = 0;
  916. struct fat32_LongDirectory_t *Ldentry = (struct fat32_LongDirectory_t *)(empty_fat32_dentry - 1);
  917. for (int i = 1; i <= cnt_longname; ++i, --Ldentry)
  918. {
  919. Ldentry->LDIR_Ord = i;
  920. for (int j = 0; j < 5; ++j, ++current_name_index)
  921. {
  922. if (current_name_index < dEntry->name_length)
  923. Ldentry->LDIR_Name1[j] = dEntry->name[current_name_index];
  924. else
  925. Ldentry->LDIR_Name1[j] = 0xffff;
  926. }
  927. for (int j = 0; j < 6; ++j, ++current_name_index)
  928. {
  929. if (current_name_index < dEntry->name_length)
  930. Ldentry->LDIR_Name2[j] = dEntry->name[current_name_index];
  931. else
  932. Ldentry->LDIR_Name2[j] = 0xffff;
  933. }
  934. for (int j = 0; j < 2; ++j, ++current_name_index)
  935. {
  936. if (current_name_index < dEntry->name_length)
  937. Ldentry->LDIR_Name3[j] = dEntry->name[current_name_index];
  938. else
  939. Ldentry->LDIR_Name3[j] = 0xffff;
  940. }
  941. Ldentry->LDIR_Attr = ATTR_LONG_NAME;
  942. Ldentry->LDIR_FstClusLO = 0;
  943. Ldentry->LDIR_Type = 0;
  944. Ldentry->LDIR_Chksum = short_dentry_ChkSum;
  945. }
  946. // 最后一个长目录项的ord要|=0x40
  947. Ldentry->LDIR_Ord = 0xe5 | 0x40;
  948. // ====== 将目录项写回磁盘
  949. ahci_operation.transfer(AHCI_CMD_WRITE_DMA_EXT, tmp_dentry_sector, fsbi->sec_per_clus, tmp_dentry_clus_buf_addr, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  950. // ====== 初始化新的文件夹的目录项 =====
  951. {
  952. void *buf = kmalloc(fsbi->bytes_per_clus, 0);
  953. struct fat32_Directory_t *new_dir_dentries = (struct fat32_Directory_t *)buf;
  954. memset((void *)new_dir_dentries, 0, fsbi->bytes_per_clus);
  955. // 新增 . 目录项
  956. new_dir_dentries->DIR_Attr = ATTR_DIRECTORY;
  957. new_dir_dentries->DIR_FileSize = 0;
  958. new_dir_dentries->DIR_Name[0] = '.';
  959. for (int i = 1; i < 11; ++i)
  960. new_dir_dentries->DIR_Name[i] = 0x20;
  961. new_dir_dentries->DIR_FstClusHI = empty_fat32_dentry->DIR_FstClusHI;
  962. new_dir_dentries->DIR_FstClusLO = empty_fat32_dentry->DIR_FstClusLO;
  963. // 新增 .. 目录项
  964. ++new_dir_dentries;
  965. new_dir_dentries->DIR_Attr = ATTR_DIRECTORY;
  966. new_dir_dentries->DIR_FileSize = 0;
  967. new_dir_dentries->DIR_Name[0] = '.';
  968. new_dir_dentries->DIR_Name[1] = '.';
  969. for (int i = 2; i < 11; ++i)
  970. new_dir_dentries->DIR_Name[i] = 0x20;
  971. new_dir_dentries->DIR_FstClusHI = (unsigned short)(parent_inode_info->first_clus >> 16) & 0x0fff;
  972. new_dir_dentries->DIR_FstClusLO = (unsigned short)(parent_inode_info->first_clus) & 0xffff;
  973. // 写入磁盘
  974. uint64_t sector = fsbi->first_data_sector + (new_dir_clus - 2) * fsbi->sec_per_clus;
  975. ahci_operation.transfer(AHCI_CMD_WRITE_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  976. }
  977. // ===== 初始化inode ====
  978. struct vfs_index_node_t *finode = (struct vfs_index_node_t *)kmalloc(sizeof(struct vfs_index_node_t), 0);
  979. memset(finode, 0, sizeof(struct vfs_index_node_t));
  980. finode->attribute = VFS_ATTR_DIR;
  981. finode->blocks = fsbi->sec_per_clus;
  982. finode->file_ops = &fat32_file_ops;
  983. finode->file_size = fsbi->bytes_per_clus;
  984. finode->inode_ops = &fat32_inode_ops;
  985. finode->sb = parent_inode->sb;
  986. finode->private_inode_info = (fat32_inode_info_t *)kmalloc(sizeof(fat32_inode_info_t), 0);
  987. memset(finode->private_inode_info, 0, sizeof(fat32_inode_info_t));
  988. fat32_inode_info_t *p = (fat32_inode_info_t *)finode->private_inode_info;
  989. p->first_clus = new_dir_clus;
  990. p->dEntry_location_clus = tmp_parent_dentry_clus;
  991. p->dEntry_location_clus_offset = empty_fat32_dentry - (struct fat32_Directory_t *)tmp_dentry_clus_buf_addr;
  992. // todo: 填写fat32_inode_info的信息
  993. // 初始化dentry信息
  994. list_init(&dEntry->child_node_list);
  995. list_init(&dEntry->subdirs_list);
  996. dEntry->dir_ops = &fat32_dEntry_ops;
  997. dEntry->dir_inode = finode;
  998. // 注意:parent字段需要在调用函数的地方进行设置
  999. // 注意:需要将当前dentry加入父目录的subdirs_list
  1000. // 释放在find empty dentry中动态申请的缓冲区
  1001. kfree((void *)tmp_dentry_clus_buf_addr);
  1002. return 0;
  1003. }
  1004. // todo: rmdir
  1005. int64_t fat32_rmdir(struct vfs_index_node_t *inode, struct vfs_dir_entry_t *dEntry)
  1006. {
  1007. }
  1008. // todo: rename
  1009. int64_t fat32_rename(struct vfs_index_node_t *old_inode, struct vfs_dir_entry_t *old_dEntry, struct vfs_index_node_t *new_inode, struct vfs_dir_entry_t *new_dEntry)
  1010. {
  1011. }
  1012. // todo: getAttr
  1013. int64_t fat32_getAttr(struct vfs_dir_entry_t *dEntry, uint64_t *attr)
  1014. {
  1015. }
  1016. // todo: setAttr
  1017. int64_t fat32_setAttr(struct vfs_dir_entry_t *dEntry, uint64_t *attr)
  1018. {
  1019. }
  1020. /**
  1021. * @brief 读取文件夹(在指定目录中找出有效目录项)
  1022. *
  1023. * @param file_ptr 文件结构体指针
  1024. * @param dirent 返回的dirent
  1025. * @param filler 填充dirent的函数
  1026. * @return int64_t
  1027. */
  1028. int64_t fat32_readdir(struct vfs_file_t *file_ptr, void *dirent, vfs_filldir_t filler)
  1029. {
  1030. struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)file_ptr->dEntry->dir_inode->private_inode_info;
  1031. fat32_sb_info_t *fsbi = (fat32_sb_info_t *)file_ptr->dEntry->dir_inode->sb->private_sb_info;
  1032. unsigned char *buf = (unsigned char *)kmalloc(fsbi->bytes_per_clus, 0);
  1033. uint32_t cluster = finode->first_clus;
  1034. // 当前文件指针所在位置的簇号(文件内偏移量)
  1035. int clus_num = file_ptr->position / fsbi->bytes_per_clus;
  1036. // 循环读取fat entry,直到读取到文件当前位置的所在簇号
  1037. for (int i = 0; i < clus_num; ++i)
  1038. {
  1039. cluster = fat32_read_FAT_entry(fsbi, cluster);
  1040. if (cluster > 0x0ffffff7) // 文件结尾
  1041. {
  1042. kerror("file position out of range! (cluster not exists)");
  1043. return NULL;
  1044. }
  1045. }
  1046. uint64_t dentry_type = 0; // 传递给filler的dentry类型数据
  1047. char *dir_name = NULL;
  1048. int name_len = 0;
  1049. // ==== 此时已经将文件夹的目录项起始簇的簇号读取到cluster变量中 ===
  1050. while (cluster <= 0x0ffffff7) // cluster在循环末尾更新(如果当前簇已经没有短目录项的话)
  1051. {
  1052. // 计算文件夹当前位置所在簇的起始扇区号
  1053. uint64_t sector = fsbi->first_data_sector + (cluster - 2) * fsbi->sec_per_clus;
  1054. // 读取文件夹目录项当前位置起始扇区的数据
  1055. if (AHCI_SUCCESS != ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num))
  1056. {
  1057. // 读取失败
  1058. kerror("Failed to read the file's first sector.");
  1059. kfree(buf);
  1060. return NULL;
  1061. }
  1062. struct fat32_Directory_t *dentry = NULL;
  1063. struct fat32_LongDirectory_t *long_dentry = NULL;
  1064. // 找到当前短目录项
  1065. dentry = (struct fat32_Directory_t *)(buf + file_ptr->position % fsbi->bytes_per_clus);
  1066. name_len = 0;
  1067. // 逐个查找短目录项
  1068. for (int i = file_ptr->position % fsbi->bytes_per_clus; i < fsbi->bytes_per_clus; i += 32, file_ptr->position += 32, ++dentry)
  1069. {
  1070. // 若是长目录项则跳过
  1071. if (dentry->DIR_Attr == ATTR_LONG_NAME)
  1072. continue;
  1073. // 跳过无效表项、空闲表项
  1074. if (dentry->DIR_Name[0] == 0xe5 || dentry->DIR_Name[0] == 0x00 || dentry->DIR_Name[0] == 0x05)
  1075. continue;
  1076. // 找到短目录项
  1077. // 该短目录项对应的第一个长目录项
  1078. long_dentry = (struct fat32_LongDirectory_t *)(dentry - 1);
  1079. // 如果长目录项有效,则读取长目录项
  1080. if (long_dentry->LDIR_Attr == ATTR_LONG_NAME && long_dentry->LDIR_Ord != 0xe5 && long_dentry->LDIR_Ord != 0x00 && long_dentry->LDIR_Ord != 0x05)
  1081. {
  1082. int count_long_dentry = 0;
  1083. // 统计长目录项的个数
  1084. while (long_dentry->LDIR_Attr == ATTR_LONG_NAME && long_dentry->LDIR_Ord != 0xe5 && long_dentry->LDIR_Ord != 0x00 && long_dentry->LDIR_Ord != 0x05)
  1085. {
  1086. ++count_long_dentry;
  1087. if (long_dentry->LDIR_Ord & 0x40) // 最后一个长目录项
  1088. break;
  1089. --long_dentry;
  1090. }
  1091. // 为目录名分配空间
  1092. dir_name = (char *)kmalloc(count_long_dentry * 26 + 1, 0);
  1093. memset(dir_name, 0, count_long_dentry * 26 + 1);
  1094. // 重新将长目录项指针指向第一个长目录项
  1095. long_dentry = (struct fat32_LongDirectory_t *)(dentry - 1);
  1096. name_len = 0;
  1097. // 逐个存储文件名
  1098. for (int j = 0; j < count_long_dentry; ++j, --long_dentry)
  1099. {
  1100. // 存储name1
  1101. for (int k = 0; k < 5; ++k)
  1102. {
  1103. if (long_dentry->LDIR_Name1[k] != 0xffff && long_dentry->LDIR_Name1[k] != 0x0000)
  1104. dir_name[name_len++] = (char)long_dentry->LDIR_Name1[k];
  1105. }
  1106. // 存储name2
  1107. for (int k = 0; k < 6; ++k)
  1108. {
  1109. if (long_dentry->LDIR_Name2[k] != 0xffff && long_dentry->LDIR_Name2[k] != 0x0000)
  1110. dir_name[name_len++] = (char)long_dentry->LDIR_Name2[k];
  1111. }
  1112. // 存储name3
  1113. for (int k = 0; k < 2; ++k)
  1114. {
  1115. if (long_dentry->LDIR_Name3[k] != 0xffff && long_dentry->LDIR_Name3[k] != 0x0000)
  1116. dir_name[name_len++] = (char)long_dentry->LDIR_Name3[k];
  1117. }
  1118. }
  1119. // 读取目录项成功,返回
  1120. dentry_type = dentry->DIR_Attr;
  1121. goto find_dir_success;
  1122. }
  1123. else // 不存在长目录项
  1124. {
  1125. dir_name = (char *)kmalloc(15, 0);
  1126. memset(dir_name, 0, 15);
  1127. name_len = 0;
  1128. int total_len = 0;
  1129. // 读取基础名
  1130. for (int j = 0; j < 8; ++j, ++total_len)
  1131. {
  1132. if (dentry->DIR_Name[j] == ' ')
  1133. break;
  1134. if (dentry->DIR_NTRes & LOWERCASE_BASE) // 如果标记了文件名小写,则转换为小写字符
  1135. dir_name[name_len++] = dentry->DIR_Name[j] + 32;
  1136. else
  1137. dir_name[name_len++] = dentry->DIR_Name[j];
  1138. }
  1139. // 如果当前短目录项为文件夹,则直接返回,不需要读取扩展名
  1140. if (dentry->DIR_Attr & ATTR_DIRECTORY)
  1141. {
  1142. dentry_type = dentry->DIR_Attr;
  1143. goto find_dir_success;
  1144. }
  1145. // 是文件,增加 .
  1146. dir_name[name_len++] = '.';
  1147. // 读取扩展名
  1148. // 读取基础名
  1149. for (int j = 0; j < 3; ++j, ++total_len)
  1150. {
  1151. if (dentry->DIR_Name[j] == ' ')
  1152. break;
  1153. if (dentry->DIR_NTRes & LOWERCASE_BASE) // 如果标记了文件名小写,则转换为小写字符
  1154. dir_name[name_len++] = dentry->DIR_Name[j] + 32;
  1155. else
  1156. dir_name[name_len++] = dentry->DIR_Name[j];
  1157. }
  1158. if (total_len == 8) // 没有扩展名
  1159. dir_name[--name_len] = '\0';
  1160. dentry_type = dentry->DIR_Attr;
  1161. goto find_dir_success;
  1162. }
  1163. }
  1164. // 当前簇不存在目录项
  1165. cluster = fat32_read_FAT_entry(fsbi, cluster);
  1166. }
  1167. kfree(buf);
  1168. // 在上面的循环中读取到目录项结尾了,仍没有找到
  1169. return NULL;
  1170. find_dir_success:;
  1171. // 将文件夹位置坐标加32(即指向下一个目录项)
  1172. file_ptr->position += 32;
  1173. // todo: 计算ino_t
  1174. if (dentry_type & ATTR_DIRECTORY)
  1175. dentry_type = VFS_ATTR_DIR;
  1176. else
  1177. dentry_type = VFS_ATTR_FILE;
  1178. return filler(dirent, 0, dir_name, name_len, dentry_type, 0);
  1179. }
  1180. struct vfs_inode_operations_t fat32_inode_ops =
  1181. {
  1182. .create = fat32_create,
  1183. .mkdir = fat32_mkdir,
  1184. .rmdir = fat32_rmdir,
  1185. .lookup = fat32_lookup,
  1186. .rename = fat32_rename,
  1187. .getAttr = fat32_getAttr,
  1188. .setAttr = fat32_setAttr,
  1189. };
  1190. struct vfs_filesystem_type_t fat32_fs_type =
  1191. {
  1192. .name = "FAT32",
  1193. .fs_flags = 0,
  1194. .read_superblock = fat32_read_superblock,
  1195. .next = NULL,
  1196. };
  1197. void fat32_init()
  1198. {
  1199. kinfo("Initializing FAT32...");
  1200. // 在VFS中注册fat32文件系统
  1201. vfs_register_filesystem(&fat32_fs_type);
  1202. // 挂载根文件系统
  1203. vfs_root_sb = fat32_register_partition(0, 0, 0);
  1204. kinfo("FAT32 initialized.");
  1205. }