fat32.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262
  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 = (struct vfs_dir_entry_t *)kmalloc(sizeof(struct vfs_dir_entry_t), 0);
  330. memset(sb_ptr->root, 0, sizeof(struct vfs_dir_entry_t));
  331. list_init(&sb_ptr->root->child_node_list);
  332. list_init(&sb_ptr->root->subdirs_list);
  333. sb_ptr->root->parent = sb_ptr->root;
  334. sb_ptr->root->dir_ops = &fat32_dEntry_ops;
  335. // 分配2个字节的name
  336. sb_ptr->root->name = (char *)(kmalloc(2, 0));
  337. sb_ptr->root->name[0] = '/';
  338. sb_ptr->root->name_length = 1;
  339. // 为root目录项分配index node
  340. sb_ptr->root->dir_inode = vfs_alloc_inode();
  341. sb_ptr->root->dir_inode->inode_ops = &fat32_inode_ops;
  342. sb_ptr->root->dir_inode->file_ops = &fat32_file_ops;
  343. sb_ptr->root->dir_inode->file_size = 0;
  344. // 计算文件占用的扇区数, 由于最小存储单位是簇,因此需要按照簇的大小来对齐扇区
  345. sb_ptr->root->dir_inode->blocks = (sb_ptr->root->dir_inode->file_size + fsbi->bytes_per_clus - 1) / fsbi->bytes_per_sec;
  346. sb_ptr->root->dir_inode->attribute = VFS_IF_DIR;
  347. sb_ptr->root->dir_inode->sb = sb_ptr; // 反向绑定对应的超级块
  348. // 初始化inode信息
  349. sb_ptr->root->dir_inode->private_inode_info = kmalloc(sizeof(struct fat32_inode_info_t), 0);
  350. memset(sb_ptr->root->dir_inode->private_inode_info, 0, sizeof(struct fat32_inode_info_t));
  351. struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)sb_ptr->root->dir_inode->private_inode_info;
  352. finode->first_clus = fbs->BPB_RootClus;
  353. finode->dEntry_location_clus = 0;
  354. finode->dEntry_location_clus_offset = 0;
  355. finode->create_time = 0;
  356. finode->create_date = 0;
  357. finode->write_date = 0;
  358. finode->write_time;
  359. return sb_ptr;
  360. }
  361. /**
  362. * @brief todo: 写入superblock
  363. *
  364. * @param sb
  365. */
  366. void fat32_write_superblock(struct vfs_superblock_t *sb)
  367. {
  368. }
  369. /**
  370. * @brief 释放superblock的内存空间
  371. *
  372. * @param sb 要被释放的superblock
  373. */
  374. void fat32_put_superblock(struct vfs_superblock_t *sb)
  375. {
  376. kfree(sb->private_sb_info);
  377. kfree(sb->root->dir_inode->private_inode_info);
  378. kfree(sb->root->dir_inode);
  379. kfree(sb->root);
  380. kfree(sb);
  381. }
  382. /**
  383. * @brief 写入inode到硬盘上
  384. *
  385. * @param inode
  386. */
  387. void fat32_write_inode(struct vfs_index_node_t *inode)
  388. {
  389. fat32_inode_info_t *finode = inode->private_inode_info;
  390. if (finode->dEntry_location_clus == 0)
  391. {
  392. kerror("FAT32 error: Attempt to write the root inode");
  393. return;
  394. }
  395. fat32_sb_info_t *fsbi = (fat32_sb_info_t *)inode->sb->private_sb_info;
  396. // 计算目标inode对应数据区的LBA地址
  397. uint64_t fLBA = fsbi->first_data_sector + (finode->dEntry_location_clus - 2) * fsbi->sec_per_clus;
  398. struct fat32_Directory_t *buf = (struct fat32_Directory_t *)kmalloc(fsbi->bytes_per_clus, 0);
  399. memset(buf, 0, fsbi->bytes_per_clus);
  400. 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);
  401. // 计算目标dEntry所在的位置
  402. struct fat32_Directory_t *fdEntry = buf + finode->dEntry_location_clus_offset;
  403. // 写入fat32文件系统的dir_entry
  404. fdEntry->DIR_FileSize = inode->file_size;
  405. fdEntry->DIR_FstClusLO = finode->first_clus & 0xffff;
  406. fdEntry->DIR_FstClusHI = (finode->first_clus >> 16) | (fdEntry->DIR_FstClusHI & 0xf000);
  407. // 将dir entry写回磁盘
  408. 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);
  409. kfree(buf);
  410. }
  411. struct vfs_super_block_operations_t fat32_sb_ops =
  412. {
  413. .write_superblock = fat32_write_superblock,
  414. .put_superblock = fat32_put_superblock,
  415. .write_inode = fat32_write_inode,
  416. };
  417. // todo: compare
  418. long fat32_compare(struct vfs_dir_entry_t *parent_dEntry, char *source_filename, char *dest_filename)
  419. {
  420. }
  421. // todo: hash
  422. long fat32_hash(struct vfs_dir_entry_t *dEntry, char *filename)
  423. {
  424. }
  425. // todo: release
  426. long fat32_release(struct vfs_dir_entry_t *dEntry)
  427. {
  428. }
  429. // todo: iput
  430. long fat32_iput(struct vfs_dir_entry_t *dEntry, struct vfs_index_node_t *inode)
  431. {
  432. }
  433. /**
  434. * @brief fat32文件系统对于dEntry的操作
  435. *
  436. */
  437. struct vfs_dir_entry_operations_t fat32_dEntry_ops =
  438. {
  439. .compare = fat32_compare,
  440. .hash = fat32_hash,
  441. .release = fat32_release,
  442. .iput = fat32_iput,
  443. };
  444. // todo: open
  445. long fat32_open(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr)
  446. {
  447. return VFS_SUCCESS;
  448. }
  449. // todo: close
  450. long fat32_close(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr)
  451. {
  452. return VFS_SUCCESS;
  453. }
  454. /**
  455. * @brief 从fat32文件系统读取数据
  456. *
  457. * @param file_ptr 文件描述符
  458. * @param buf 输出缓冲区
  459. * @param count 要读取的字节数
  460. * @param position 文件指针位置
  461. * @return long 执行成功:传输的字节数量 执行失败:错误码(小于0)
  462. */
  463. long fat32_read(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *position)
  464. {
  465. struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)(file_ptr->dEntry->dir_inode->private_inode_info);
  466. fat32_sb_info_t *fsbi = (fat32_sb_info_t *)(file_ptr->dEntry->dir_inode->sb->private_sb_info);
  467. struct block_device *blk = file_ptr->dEntry->dir_inode->sb->blk_device;
  468. // First cluster num of the file
  469. uint64_t cluster = finode->first_clus;
  470. // 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);
  471. // kdebug("fsbi->bytes_per_clus=%d", fsbi->bytes_per_clus);
  472. // clus offset in file
  473. uint64_t clus_offset_in_file = (*position) / fsbi->bytes_per_clus;
  474. // bytes offset in clus
  475. uint64_t bytes_offset = (*position) % fsbi->bytes_per_clus;
  476. if (!cluster)
  477. return -EFAULT;
  478. // find the actual cluster on disk of the specified position
  479. for (int i = 0; i < clus_offset_in_file; ++i)
  480. cluster = fat32_read_FAT_entry(blk, fsbi, cluster);
  481. // 如果需要读取的数据边界大于文件大小
  482. if (*position + count > file_ptr->dEntry->dir_inode->file_size)
  483. count = file_ptr->dEntry->dir_inode->file_size - *position;
  484. // 剩余还需要传输的字节数量
  485. int64_t bytes_remain = count;
  486. // alloc buffer memory space for ahci transfer
  487. void *tmp_buffer = kmalloc(fsbi->bytes_per_clus, 0);
  488. int64_t retval = 0;
  489. do
  490. {
  491. memset(tmp_buffer, 0, fsbi->bytes_per_clus);
  492. uint64_t sector = fsbi->first_data_sector + (cluster - 2) * fsbi->sec_per_clus;
  493. // 读取一个簇的数据
  494. int errno = blk->bd_disk->fops->transfer(blk->bd_disk, AHCI_CMD_READ_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)tmp_buffer);
  495. if (errno != AHCI_SUCCESS)
  496. {
  497. kerror("FAT32 FS(read) error!");
  498. retval = -EIO;
  499. break;
  500. }
  501. int64_t step_trans_len = 0; // 当前循环传输的字节数
  502. if (bytes_remain > (fsbi->bytes_per_clus - bytes_offset))
  503. step_trans_len = (fsbi->bytes_per_clus - bytes_offset);
  504. else
  505. step_trans_len = bytes_remain;
  506. if (((uint64_t)buf) < USER_MAX_LINEAR_ADDR)
  507. copy_to_user(buf, tmp_buffer + bytes_offset, step_trans_len);
  508. else
  509. memcpy(buf, tmp_buffer + bytes_offset, step_trans_len);
  510. bytes_remain -= step_trans_len;
  511. buf += step_trans_len;
  512. bytes_offset -= bytes_offset;
  513. *position += step_trans_len; // 更新文件指针
  514. cluster = fat32_read_FAT_entry(blk, fsbi, cluster);
  515. } while (bytes_remain && (cluster < 0x0ffffff8) && cluster != 0);
  516. kfree(tmp_buffer);
  517. if (!bytes_remain)
  518. retval = count;
  519. return retval;
  520. }
  521. /**
  522. * @brief 向fat32文件系统写入数据
  523. *
  524. * @param file_ptr 文件描述符
  525. * @param buf 输入写入的字节数
  526. * @param position 文件指针位置
  527. * @return long 执行成功:传输的字节数量 执行失败:错误码(小于0)
  528. */
  529. long fat32_write(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *position)
  530. {
  531. struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)file_ptr->dEntry->dir_inode->private_inode_info;
  532. fat32_sb_info_t *fsbi = (fat32_sb_info_t *)(file_ptr->dEntry->dir_inode->sb->private_sb_info);
  533. struct block_device *blk = file_ptr->dEntry->dir_inode->sb->blk_device;
  534. // First cluster num of the file
  535. uint32_t cluster = finode->first_clus;
  536. int64_t flags = 0;
  537. // clus offset in file
  538. uint64_t clus_offset_in_file = (*position) / fsbi->bytes_per_clus;
  539. // bytes offset in clus
  540. uint64_t bytes_offset = (*position) % fsbi->bytes_per_clus;
  541. if (!cluster) // 起始簇号为0,说明是空文件
  542. {
  543. // 分配空闲簇
  544. if (fat32_alloc_clusters(file_ptr->dEntry->dir_inode, &cluster, 1) != 0)
  545. return -ENOSPC;
  546. }
  547. else
  548. {
  549. // 跳转到position所在的簇
  550. for (uint64_t i = 0; i < clus_offset_in_file; ++i)
  551. cluster = fat32_read_FAT_entry(blk, fsbi, cluster);
  552. }
  553. // kdebug("cluster(start)=%d", cluster);
  554. // 没有可用的磁盘空间
  555. if (!cluster)
  556. return -ENOSPC;
  557. int64_t bytes_remain = count;
  558. if (count < 0) // 要写入的字节数小于0
  559. return -EINVAL;
  560. uint64_t sector;
  561. int64_t retval = 0;
  562. void *tmp_buffer = kmalloc(fsbi->bytes_per_clus, 0);
  563. do
  564. {
  565. memset(tmp_buffer, 0, fsbi->bytes_per_clus);
  566. sector = fsbi->first_data_sector + (cluster - 2) * fsbi->sec_per_clus; // 计算对应的扇区
  567. if (!flags) // 当前簇已分配
  568. {
  569. // kdebug("read existed sec=%ld", sector);
  570. // 读取一个簇的数据
  571. int errno = blk->bd_disk->fops->transfer(blk->bd_disk, AHCI_CMD_READ_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)tmp_buffer);
  572. if (errno != AHCI_SUCCESS)
  573. {
  574. // kerror("FAT32 FS(write) read disk error!");
  575. retval = -EIO;
  576. break;
  577. }
  578. }
  579. int64_t step_trans_len = 0; // 当前循环传输的字节数
  580. if (bytes_remain > (fsbi->bytes_per_clus - bytes_offset))
  581. step_trans_len = (fsbi->bytes_per_clus - bytes_offset);
  582. else
  583. step_trans_len = bytes_remain;
  584. // kdebug("step_trans_len=%d, bytes_offset=%d", step_trans_len, bytes_offset);
  585. if (((uint64_t)buf) < USER_MAX_LINEAR_ADDR)
  586. copy_from_user(tmp_buffer + bytes_offset, buf, step_trans_len);
  587. else
  588. memcpy(tmp_buffer + bytes_offset, buf, step_trans_len);
  589. // 写入数据到对应的簇
  590. int errno = blk->bd_disk->fops->transfer(blk->bd_disk, AHCI_CMD_WRITE_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)tmp_buffer);
  591. if (errno != AHCI_SUCCESS)
  592. {
  593. kerror("FAT32 FS(write) write disk error!");
  594. retval = -EIO;
  595. break;
  596. }
  597. bytes_remain -= step_trans_len;
  598. buf += step_trans_len;
  599. bytes_offset -= bytes_offset;
  600. *position += step_trans_len; // 更新文件指针
  601. // kdebug("step_trans_len=%d", step_trans_len);
  602. int next_clus = 0;
  603. if (bytes_remain)
  604. next_clus = fat32_read_FAT_entry(blk, fsbi, cluster);
  605. else
  606. break;
  607. if (next_clus >= 0x0ffffff8) // 已经到达了最后一个簇,需要分配新簇
  608. {
  609. if (fat32_alloc_clusters(file_ptr->dEntry->dir_inode, &next_clus, 1) != 0)
  610. {
  611. // 没有空闲簇
  612. kfree(tmp_buffer);
  613. return -ENOSPC;
  614. }
  615. cluster = next_clus; // 切换当前簇
  616. flags = 1; // 标记当前簇是新分配的簇
  617. }
  618. } while (bytes_remain);
  619. // 文件大小有增长
  620. if (*position > (file_ptr->dEntry->dir_inode->file_size))
  621. {
  622. file_ptr->dEntry->dir_inode->file_size = *position;
  623. file_ptr->dEntry->dir_inode->sb->sb_ops->write_inode(file_ptr->dEntry->dir_inode);
  624. // kdebug("new file size=%ld", *position);
  625. }
  626. kfree(tmp_buffer);
  627. if (!bytes_remain)
  628. retval = count;
  629. // kdebug("retval=%lld", retval);
  630. return retval;
  631. }
  632. /**
  633. * @brief 调整文件的当前访问位置
  634. *
  635. * @param file_ptr vfs文件指针
  636. * @param offset 调整的偏移量
  637. * @param whence 调整方法
  638. * @return long 更新后的指针位置
  639. */
  640. long fat32_lseek(struct vfs_file_t *file_ptr, long offset, long whence)
  641. {
  642. struct vfs_index_node_t *inode = file_ptr->dEntry->dir_inode;
  643. long pos = 0;
  644. switch (whence)
  645. {
  646. case SEEK_SET: // 相对于文件头
  647. pos = offset;
  648. break;
  649. case SEEK_CUR: // 相对于当前位置
  650. pos = file_ptr->position + offset;
  651. break;
  652. case SEEK_END: // 相对于文件末尾
  653. pos = file_ptr->dEntry->dir_inode->file_size + offset;
  654. break;
  655. default:
  656. return -EINVAL;
  657. break;
  658. }
  659. if (pos < 0 || pos > file_ptr->dEntry->dir_inode->file_size)
  660. return -EOVERFLOW;
  661. file_ptr->position = pos;
  662. // kdebug("fat32 lseek -> position=%d", file_ptr->position);
  663. return pos;
  664. }
  665. // todo: ioctl
  666. long fat32_ioctl(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr, uint64_t cmd, uint64_t arg)
  667. {
  668. }
  669. /**
  670. * @brief fat32文件系统,关于文件的操作
  671. *
  672. */
  673. struct vfs_file_operations_t fat32_file_ops =
  674. {
  675. .open = fat32_open,
  676. .close = fat32_close,
  677. .read = fat32_read,
  678. .write = fat32_write,
  679. .lseek = fat32_lseek,
  680. .ioctl = fat32_ioctl,
  681. .readdir = fat32_readdir,
  682. };
  683. /**
  684. * @brief 创建新的文件
  685. * @param parent_inode 父目录的inode结构体
  686. * @param dest_dEntry 新文件的dentry
  687. * @param mode 创建模式
  688. */
  689. long fat32_create(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_t *dest_dEntry, int mode)
  690. {
  691. // 文件系统超级块信息
  692. fat32_sb_info_t *fsbi = (fat32_sb_info_t *)parent_inode->sb->private_sb_info;
  693. // 父目录项的inode的私有信息
  694. struct fat32_inode_info_t *parent_inode_info = (struct fat32_inode_info_t *)parent_inode->private_inode_info;
  695. int64_t retval = 0;
  696. // ======== 检验名称的合法性
  697. retval = fat32_check_name_available(dest_dEntry->name, dest_dEntry->name_length, 0);
  698. if (retval != 0)
  699. return retval;
  700. if (dest_dEntry->dir_inode != NULL)
  701. return -EEXIST;
  702. struct vfs_index_node_t *inode = vfs_alloc_inode();
  703. dest_dEntry->dir_inode = inode;
  704. dest_dEntry->dir_ops = &fat32_dEntry_ops;
  705. struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)kzalloc(sizeof(struct fat32_inode_info_t), 0);
  706. inode->attribute = VFS_IF_FILE;
  707. inode->file_ops = &fat32_file_ops;
  708. inode->file_size = 0;
  709. inode->sb = parent_inode->sb;
  710. inode->inode_ops = &fat32_inode_ops;
  711. inode->private_inode_info = (void *)finode;
  712. inode->blocks = fsbi->sec_per_clus;
  713. struct block_device *blk = inode->sb->blk_device;
  714. // 计算总共需要多少个目录项
  715. uint32_t cnt_longname = (dest_dEntry->name_length + 25) / 26;
  716. // 默认都是创建长目录项来存储
  717. if (cnt_longname == 0)
  718. cnt_longname = 1;
  719. // 空闲dentry所在的扇区号
  720. uint32_t tmp_dentry_sector = 0;
  721. // 空闲dentry所在的缓冲区的基地址
  722. uint64_t tmp_dentry_clus_buf_addr = 0;
  723. uint64_t tmp_parent_dentry_clus = 0;
  724. // 寻找空闲目录项
  725. 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);
  726. // kdebug("found empty dentry, cnt_longname=%ld", cnt_longname);
  727. finode->first_clus = 0;
  728. finode->dEntry_location_clus = tmp_parent_dentry_clus;
  729. finode->dEntry_location_clus_offset = empty_fat32_dentry - (struct fat32_Directory_t *)tmp_dentry_clus_buf_addr;
  730. // ====== 为新的文件分配一个簇 =======
  731. uint32_t new_dir_clus;
  732. if (fat32_alloc_clusters(inode, &new_dir_clus, 1) != 0)
  733. {
  734. retval = -ENOSPC;
  735. goto fail;
  736. }
  737. // kdebug("new dir clus=%ld", new_dir_clus);
  738. // kdebug("dest_dEntry->name=%s",dest_dEntry->name);
  739. // ====== 填写短目录项
  740. fat32_fill_shortname(dest_dEntry, empty_fat32_dentry, new_dir_clus);
  741. // kdebug("dest_dEntry->name=%s",dest_dEntry->name);
  742. // 计算校验和
  743. uint8_t short_dentry_ChkSum = fat32_ChkSum(empty_fat32_dentry->DIR_Name);
  744. // kdebug("dest_dEntry->name=%s",dest_dEntry->name);
  745. // ======== 填写长目录项
  746. fat32_fill_longname(dest_dEntry, (struct fat32_LongDirectory_t *)(empty_fat32_dentry - 1), short_dentry_ChkSum, cnt_longname);
  747. // ====== 将目录项写回磁盘
  748. // kdebug("tmp_dentry_sector=%ld", tmp_dentry_sector);
  749. 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);
  750. // 注意:parent字段需要在调用函数的地方进行设置
  751. // 释放在find empty dentry中动态申请的缓冲区
  752. kfree((void *)tmp_dentry_clus_buf_addr);
  753. return 0;
  754. fail:;
  755. // 释放在find empty dentry中动态申请的缓冲区
  756. kfree((void *)tmp_dentry_clus_buf_addr);
  757. dest_dEntry->dir_inode = NULL;
  758. dest_dEntry->dir_ops = NULL;
  759. kfree(finode);
  760. kfree(inode);
  761. return retval;
  762. }
  763. /**
  764. * @brief 创建文件夹
  765. * @param inode 父目录的inode
  766. * @param dEntry 新的文件夹的dentry
  767. * @param mode 创建文件夹的mode
  768. * @return long 错误码
  769. */
  770. int64_t fat32_mkdir(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_t *dEntry, int mode)
  771. {
  772. int64_t retval = 0;
  773. // 文件系统超级块信息
  774. fat32_sb_info_t *fsbi = (fat32_sb_info_t *)parent_inode->sb->private_sb_info;
  775. // 父目录项的inode私有信息
  776. struct fat32_inode_info_t *parent_inode_info = (struct fat32_inode_info_t *)parent_inode->private_inode_info;
  777. // ======== 检验名称的合法性
  778. retval = fat32_check_name_available(dEntry->name, dEntry->name_length, 0);
  779. if (retval != 0)
  780. return retval;
  781. // ====== 找一块连续的区域放置新的目录项 =====
  782. // 计算总共需要多少个目录项
  783. uint32_t cnt_longname = (dEntry->name_length + 25) / 26;
  784. // 默认都是创建长目录项来存储
  785. if (cnt_longname == 0)
  786. cnt_longname = 1;
  787. // 空闲dentry所在的扇区号
  788. uint32_t tmp_dentry_sector = 0;
  789. // 空闲dentry所在的缓冲区的基地址
  790. uint64_t tmp_dentry_clus_buf_addr = 0;
  791. uint64_t tmp_parent_dentry_clus = 0;
  792. // 寻找空闲目录项
  793. 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);
  794. // ====== 初始化inode =======
  795. struct vfs_index_node_t *inode = vfs_alloc_inode();
  796. inode->attribute = VFS_IF_DIR;
  797. inode->blocks = fsbi->sec_per_clus;
  798. inode->file_ops = &fat32_file_ops;
  799. inode->file_size = 0;
  800. inode->inode_ops = &fat32_inode_ops;
  801. inode->sb = parent_inode->sb;
  802. struct block_device *blk = inode->sb->blk_device;
  803. // ===== 初始化inode的文件系统私有信息 ====
  804. inode->private_inode_info = (fat32_inode_info_t *)kmalloc(sizeof(fat32_inode_info_t), 0);
  805. memset(inode->private_inode_info, 0, sizeof(fat32_inode_info_t));
  806. fat32_inode_info_t *p = (fat32_inode_info_t *)inode->private_inode_info;
  807. p->first_clus = 0;
  808. p->dEntry_location_clus = tmp_parent_dentry_clus;
  809. p->dEntry_location_clus_offset = empty_fat32_dentry - (struct fat32_Directory_t *)tmp_dentry_clus_buf_addr;
  810. // kdebug(" p->dEntry_location_clus_offset=%d", p->dEntry_location_clus_offset);
  811. // todo: 填写完全fat32_inode_info的信息
  812. // 初始化dentry信息
  813. list_init(&dEntry->child_node_list);
  814. list_init(&dEntry->subdirs_list);
  815. dEntry->dir_ops = &fat32_dEntry_ops;
  816. dEntry->dir_inode = inode;
  817. // ====== 为新的文件夹分配一个簇 =======
  818. uint32_t new_dir_clus;
  819. if (fat32_alloc_clusters(inode, &new_dir_clus, 1) != 0)
  820. {
  821. retval = -ENOSPC;
  822. goto fail;
  823. }
  824. // kdebug("new dir clus=%ld", new_dir_clus);
  825. // ====== 填写短目录项
  826. fat32_fill_shortname(dEntry, empty_fat32_dentry, new_dir_clus);
  827. // 计算校验和
  828. uint8_t short_dentry_ChkSum = fat32_ChkSum(empty_fat32_dentry->DIR_Name);
  829. // ======== 填写长目录项
  830. fat32_fill_longname(dEntry, (struct fat32_LongDirectory_t *)(empty_fat32_dentry - 1), short_dentry_ChkSum, cnt_longname);
  831. // ====== 将目录项写回磁盘
  832. // kdebug("tmp_dentry_sector=%ld", tmp_dentry_sector);
  833. 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);
  834. // ====== 初始化新的文件夹的目录项 =====
  835. {
  836. // kdebug("to create dot and dot dot.");
  837. void *buf = kmalloc(fsbi->bytes_per_clus, 0);
  838. struct fat32_Directory_t *new_dir_dentries = (struct fat32_Directory_t *)buf;
  839. memset((void *)new_dir_dentries, 0, fsbi->bytes_per_clus);
  840. // 新增 . 目录项
  841. new_dir_dentries->DIR_Attr = ATTR_DIRECTORY;
  842. new_dir_dentries->DIR_FileSize = 0;
  843. new_dir_dentries->DIR_Name[0] = '.';
  844. for (int i = 1; i < 11; ++i)
  845. new_dir_dentries->DIR_Name[i] = 0x20;
  846. new_dir_dentries->DIR_FstClusHI = empty_fat32_dentry->DIR_FstClusHI;
  847. new_dir_dentries->DIR_FstClusLO = empty_fat32_dentry->DIR_FstClusLO;
  848. // 新增 .. 目录项
  849. ++new_dir_dentries;
  850. new_dir_dentries->DIR_Attr = ATTR_DIRECTORY;
  851. new_dir_dentries->DIR_FileSize = 0;
  852. new_dir_dentries->DIR_Name[0] = '.';
  853. new_dir_dentries->DIR_Name[1] = '.';
  854. for (int i = 2; i < 11; ++i)
  855. new_dir_dentries->DIR_Name[i] = 0x20;
  856. new_dir_dentries->DIR_FstClusHI = (unsigned short)(parent_inode_info->first_clus >> 16) & 0x0fff;
  857. new_dir_dentries->DIR_FstClusLO = (unsigned short)(parent_inode_info->first_clus) & 0xffff;
  858. // 写入磁盘
  859. uint64_t sector = fsbi->first_data_sector + (new_dir_clus - 2) * fsbi->sec_per_clus;
  860. // kdebug("add dot and dot dot: sector=%ld", sector);
  861. blk->bd_disk->fops->transfer(blk->bd_disk, AHCI_CMD_WRITE_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)buf);
  862. }
  863. // 注意:parent字段需要在调用函数的地方进行设置
  864. // 注意:需要将当前dentry加入父目录的subdirs_list
  865. // 释放在find empty dentry中动态申请的缓冲区
  866. kfree((void *)tmp_dentry_clus_buf_addr);
  867. return 0;
  868. fail:;
  869. // 释放在find empty dentry中动态申请的缓冲区
  870. kfree((void *)tmp_dentry_clus_buf_addr);
  871. return retval;
  872. }
  873. // todo: rmdir
  874. int64_t fat32_rmdir(struct vfs_index_node_t *inode, struct vfs_dir_entry_t *dEntry)
  875. {
  876. }
  877. // todo: rename
  878. 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)
  879. {
  880. }
  881. // todo: getAttr
  882. int64_t fat32_getAttr(struct vfs_dir_entry_t *dEntry, uint64_t *attr)
  883. {
  884. }
  885. // todo: setAttr
  886. int64_t fat32_setAttr(struct vfs_dir_entry_t *dEntry, uint64_t *attr)
  887. {
  888. }
  889. /**
  890. * @brief 读取文件夹(在指定目录中找出有效目录项)
  891. *
  892. * @param file_ptr 文件结构体指针
  893. * @param dirent 返回的dirent
  894. * @param filler 填充dirent的函数
  895. * @return uint64_t dirent的总大小
  896. */
  897. int64_t fat32_readdir(struct vfs_file_t *file_ptr, void *dirent, vfs_filldir_t filler)
  898. {
  899. struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)file_ptr->dEntry->dir_inode->private_inode_info;
  900. fat32_sb_info_t *fsbi = (fat32_sb_info_t *)file_ptr->dEntry->dir_inode->sb->private_sb_info;
  901. struct block_device *blk = file_ptr->dEntry->dir_inode->sb->blk_device;
  902. unsigned char *buf = (unsigned char *)kzalloc(fsbi->bytes_per_clus, 0);
  903. uint32_t cluster = finode->first_clus;
  904. // 当前文件指针所在位置的簇号(文件内偏移量)
  905. int clus_num = file_ptr->position / fsbi->bytes_per_clus;
  906. // 循环读取fat entry,直到读取到文件当前位置的所在簇号
  907. for (int i = 0; i < clus_num; ++i)
  908. {
  909. cluster = fat32_read_FAT_entry(blk, fsbi, cluster);
  910. if (cluster > 0x0ffffff7) // 文件结尾
  911. {
  912. kerror("file position out of range! (cluster not exists)");
  913. return NULL;
  914. }
  915. }
  916. uint64_t dentry_type = 0; // 传递给filler的dentry类型数据
  917. char *dir_name = NULL;
  918. int name_len = 0;
  919. // ==== 此时已经将文件夹的目录项起始簇的簇号读取到cluster变量中 ===
  920. while (cluster <= 0x0ffffff7) // cluster在循环末尾更新(如果当前簇已经没有短目录项的话)
  921. {
  922. // 计算文件夹当前位置所在簇的起始扇区号
  923. uint64_t sector = fsbi->first_data_sector + (cluster - 2) * fsbi->sec_per_clus;
  924. // 读取文件夹目录项当前位置起始扇区的数据
  925. if (AHCI_SUCCESS != blk->bd_disk->fops->transfer(blk->bd_disk, AHCI_CMD_READ_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)buf))
  926. {
  927. // 读取失败
  928. kerror("Failed to read the file's first sector.");
  929. kfree(buf);
  930. return NULL;
  931. }
  932. struct fat32_Directory_t *dentry = NULL;
  933. struct fat32_LongDirectory_t *long_dentry = NULL;
  934. // 找到当前短目录项
  935. dentry = (struct fat32_Directory_t *)(buf + file_ptr->position % fsbi->bytes_per_clus);
  936. name_len = 0;
  937. // 逐个查找短目录项
  938. for (int i = file_ptr->position % fsbi->bytes_per_clus; i < fsbi->bytes_per_clus; i += 32, file_ptr->position += 32, ++dentry)
  939. {
  940. // 若是长目录项则跳过
  941. if (dentry->DIR_Attr == ATTR_LONG_NAME)
  942. continue;
  943. // 跳过无效表项、空闲表项
  944. if (dentry->DIR_Name[0] == 0xe5 || dentry->DIR_Name[0] == 0x00 || dentry->DIR_Name[0] == 0x05)
  945. continue;
  946. // 找到短目录项
  947. // 该短目录项对应的第一个长目录项
  948. long_dentry = (struct fat32_LongDirectory_t *)(dentry - 1);
  949. // 如果长目录项有效,则读取长目录项
  950. if (long_dentry->LDIR_Attr == ATTR_LONG_NAME && long_dentry->LDIR_Ord != 0xe5 && long_dentry->LDIR_Ord != 0x00 && long_dentry->LDIR_Ord != 0x05)
  951. {
  952. int count_long_dentry = 0;
  953. // 统计长目录项的个数
  954. while (long_dentry->LDIR_Attr == ATTR_LONG_NAME && long_dentry->LDIR_Ord != 0xe5 && long_dentry->LDIR_Ord != 0x00 && long_dentry->LDIR_Ord != 0x05)
  955. {
  956. ++count_long_dentry;
  957. if (long_dentry->LDIR_Ord & 0x40) // 最后一个长目录项
  958. break;
  959. --long_dentry;
  960. }
  961. // 为目录名分配空间
  962. dir_name = (char *)kmalloc(count_long_dentry * 26 + 1, 0);
  963. memset(dir_name, 0, count_long_dentry * 26 + 1);
  964. // 重新将长目录项指针指向第一个长目录项
  965. long_dentry = (struct fat32_LongDirectory_t *)(dentry - 1);
  966. name_len = 0;
  967. // 逐个存储文件名
  968. for (int j = 0; j < count_long_dentry; ++j, --long_dentry)
  969. {
  970. // 存储name1
  971. for (int k = 0; k < 5; ++k)
  972. {
  973. if (long_dentry->LDIR_Name1[k] != 0xffff && long_dentry->LDIR_Name1[k] != 0x0000)
  974. dir_name[name_len++] = (char)long_dentry->LDIR_Name1[k];
  975. }
  976. // 存储name2
  977. for (int k = 0; k < 6; ++k)
  978. {
  979. if (long_dentry->LDIR_Name2[k] != 0xffff && long_dentry->LDIR_Name2[k] != 0x0000)
  980. dir_name[name_len++] = (char)long_dentry->LDIR_Name2[k];
  981. }
  982. // 存储name3
  983. for (int k = 0; k < 2; ++k)
  984. {
  985. if (long_dentry->LDIR_Name3[k] != 0xffff && long_dentry->LDIR_Name3[k] != 0x0000)
  986. dir_name[name_len++] = (char)long_dentry->LDIR_Name3[k];
  987. }
  988. }
  989. // 读取目录项成功,返回
  990. dentry_type = dentry->DIR_Attr;
  991. goto find_dir_success;
  992. }
  993. else // 不存在长目录项
  994. {
  995. dir_name = (char *)kmalloc(15, 0);
  996. memset(dir_name, 0, 15);
  997. name_len = 0;
  998. int total_len = 0;
  999. // 读取基础名
  1000. for (int j = 0; j < 8; ++j, ++total_len)
  1001. {
  1002. if (dentry->DIR_Name[j] == ' ')
  1003. break;
  1004. if (dentry->DIR_NTRes & LOWERCASE_BASE) // 如果标记了文件名小写,则转换为小写字符
  1005. dir_name[name_len++] = dentry->DIR_Name[j] + 32;
  1006. else
  1007. dir_name[name_len++] = dentry->DIR_Name[j];
  1008. }
  1009. // 如果当前短目录项为文件夹,则直接返回,不需要读取扩展名
  1010. if (dentry->DIR_Attr & ATTR_DIRECTORY)
  1011. {
  1012. dentry_type = dentry->DIR_Attr;
  1013. goto find_dir_success;
  1014. }
  1015. // 是文件,增加 .
  1016. dir_name[name_len++] = '.';
  1017. // 读取扩展名
  1018. // 读取基础名
  1019. for (int j = 0; j < 3; ++j, ++total_len)
  1020. {
  1021. if (dentry->DIR_Name[j] == ' ')
  1022. break;
  1023. if (dentry->DIR_NTRes & LOWERCASE_BASE) // 如果标记了文件名小写,则转换为小写字符
  1024. dir_name[name_len++] = dentry->DIR_Name[j] + 32;
  1025. else
  1026. dir_name[name_len++] = dentry->DIR_Name[j];
  1027. }
  1028. if (total_len == 8) // 没有扩展名
  1029. dir_name[--name_len] = '\0';
  1030. dentry_type = dentry->DIR_Attr;
  1031. goto find_dir_success;
  1032. }
  1033. }
  1034. // 当前簇不存在目录项
  1035. cluster = fat32_read_FAT_entry(blk, fsbi, cluster);
  1036. }
  1037. kfree(buf);
  1038. // 在上面的循环中读取到目录项结尾了,仍没有找到
  1039. return NULL;
  1040. find_dir_success:;
  1041. // 将文件夹位置坐标加32(即指向下一个目录项)
  1042. file_ptr->position += 32;
  1043. // todo: 计算ino_t
  1044. if (dentry_type & ATTR_DIRECTORY)
  1045. dentry_type = VFS_IF_DIR;
  1046. else
  1047. dentry_type = VFS_IF_FILE;
  1048. return filler(dirent, 0, dir_name, name_len, dentry_type, 0);
  1049. }
  1050. struct vfs_inode_operations_t fat32_inode_ops =
  1051. {
  1052. .create = fat32_create,
  1053. .mkdir = fat32_mkdir,
  1054. .rmdir = fat32_rmdir,
  1055. .lookup = fat32_lookup,
  1056. .rename = fat32_rename,
  1057. .getAttr = fat32_getAttr,
  1058. .setAttr = fat32_setAttr,
  1059. };
  1060. struct vfs_filesystem_type_t fat32_fs_type =
  1061. {
  1062. .name = "FAT32",
  1063. .fs_flags = 0,
  1064. .read_superblock = fat32_read_superblock,
  1065. .next = NULL,
  1066. };
  1067. void fat32_init()
  1068. {
  1069. kinfo("Initializing FAT32...");
  1070. // 在VFS中注册fat32文件系统
  1071. vfs_register_filesystem(&fat32_fs_type);
  1072. // 挂载根文件系统
  1073. fat32_register_partition(ahci_gendisk0.partition + 0, 0);
  1074. kinfo("FAT32 initialized.");
  1075. }