fat32.c 46 KB

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