fat32.c 45 KB

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