fat32.c 46 KB

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