fat32.c 46 KB

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