fat32.c 46 KB

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