fat32.c 47 KB

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