fat32.c 50 KB

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