fat_ent.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. #include "fat_ent.h"
  2. #include <common/errno.h>
  3. #include <driver/disk/ahci/ahci.h>
  4. #include <mm/slab.h>
  5. static const char unavailable_character_in_short_name[] = {0x22, 0x2a, 0x2b, 0x2c, 0x2e, 0x2f, 0x3a, 0x3b,
  6. 0x3c, 0x3d, 0x3e, 0x3f, 0x5b, 0x5c, 0x5d, 0x7c};
  7. /**
  8. * @brief 请求分配指定数量的簇
  9. *
  10. * @param inode 要分配簇的inode
  11. * @param clusters 返回的被分配的簇的簇号结构体
  12. * @param num_clusters 要分配的簇的数量
  13. * @return int 错误码
  14. */
  15. int fat32_alloc_clusters(struct vfs_index_node_t *inode, uint32_t *clusters, int32_t num_clusters)
  16. {
  17. int retval = 0;
  18. fat32_sb_info_t *fsbi = (fat32_sb_info_t *)inode->sb->private_sb_info;
  19. struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)inode->private_inode_info;
  20. struct block_device *blk = inode->sb->blk_device;
  21. uint64_t sec_per_fat = fsbi->sec_per_FAT;
  22. // todo: 对alloc的过程加锁
  23. // 申请1扇区的缓冲区
  24. uint32_t *buf = (uint32_t *)kmalloc(fsbi->bytes_per_sec, 0);
  25. int ent_per_sec = (fsbi->bytes_per_sec >> 2);
  26. int clus_idx = 0;
  27. for (int i = 0; i < sec_per_fat; ++i)
  28. {
  29. if (clus_idx >= num_clusters)
  30. goto done;
  31. memset(buf, 0, fsbi->bytes_per_sec);
  32. blk->bd_disk->fops->transfer(blk->bd_disk, AHCI_CMD_READ_DMA_EXT, fsbi->FAT1_base_sector + i, 1, (uint64_t)buf);
  33. // 依次检查簇是否空闲
  34. for (int j = 0; j < ent_per_sec; ++j)
  35. {
  36. if (clus_idx >= num_clusters)
  37. goto done;
  38. // 找到空闲簇
  39. if ((buf[j] & 0x0fffffff) == 0)
  40. {
  41. // kdebug("clus[%d] = %d", clus_idx, i * ent_per_sec + j);
  42. clusters[clus_idx] = i * ent_per_sec + j;
  43. ++clus_idx;
  44. }
  45. }
  46. }
  47. // 空间不足
  48. retval = -ENOSPC;
  49. done:;
  50. kfree(buf);
  51. if (retval == 0) // 成功
  52. {
  53. int cluster, idx;
  54. if (finode->first_clus == 0)
  55. {
  56. // 空文件
  57. finode->first_clus = clusters[0];
  58. cluster = finode->first_clus;
  59. // 写入inode到磁盘
  60. inode->sb->sb_ops->write_inode(inode);
  61. idx = 1;
  62. }
  63. else
  64. {
  65. // todo: 跳转到文件当前的最后一个簇
  66. idx = 0;
  67. int tmp_clus = finode->first_clus;
  68. cluster = tmp_clus;
  69. while (true)
  70. {
  71. tmp_clus = fat32_read_FAT_entry(blk, fsbi, cluster);
  72. if (tmp_clus <= 0x0ffffff7)
  73. cluster = tmp_clus;
  74. else
  75. break;
  76. }
  77. }
  78. // 写入fat表
  79. for (int i = idx; i < num_clusters; ++i)
  80. {
  81. // kdebug("write cluster i=%d : cluster=%d, value= %d", i, cluster, clusters[i]);
  82. fat32_write_FAT_entry(blk, fsbi, cluster, clusters[i]);
  83. cluster = clusters[i];
  84. }
  85. fat32_write_FAT_entry(blk, fsbi, cluster, 0x0ffffff8);
  86. return 0;
  87. }
  88. else // 出现错误
  89. {
  90. kwarn("err in alloc clusters");
  91. if (clus_idx < num_clusters)
  92. fat32_free_clusters(inode, clusters[0]);
  93. return retval;
  94. }
  95. return 0;
  96. }
  97. /**
  98. * @brief 释放从属于inode的,从cluster开始的所有簇
  99. *
  100. * @param inode 指定的文件的inode
  101. * @param cluster 指定簇
  102. * @return int 错误码
  103. */
  104. int fat32_free_clusters(struct vfs_index_node_t *inode, int32_t cluster)
  105. {
  106. // todo: 释放簇
  107. return 0;
  108. }
  109. /**
  110. * @brief 读取指定簇的FAT表项
  111. *
  112. * @param blk 块设备结构体
  113. * @param fsbi fat32超级块私有信息结构体
  114. * @param cluster 指定簇
  115. * @return uint32_t 下一个簇的簇号
  116. */
  117. uint32_t fat32_read_FAT_entry(struct block_device *blk, fat32_sb_info_t *fsbi, uint32_t cluster)
  118. {
  119. // 计算每个扇区内含有的FAT表项数
  120. // FAT每项4bytes
  121. uint32_t fat_ent_per_sec = (fsbi->bytes_per_sec >> 2); // 该值应为2的n次幂
  122. uint32_t buf[256];
  123. memset(buf, 0, fsbi->bytes_per_sec);
  124. // 读取一个sector的数据,
  125. blk->bd_disk->fops->transfer(blk->bd_disk, AHCI_CMD_READ_DMA_EXT,
  126. fsbi->FAT1_base_sector + (cluster / fat_ent_per_sec), 1, (uint64_t)&buf);
  127. // 返回下一个fat表项的值(也就是下一个cluster)
  128. return buf[cluster & (fat_ent_per_sec - 1)] & 0x0fffffff;
  129. }
  130. /**
  131. * @brief 写入指定簇的FAT表项
  132. *
  133. * @param blk 块设备结构体
  134. * @param fsbi fat32超级块私有信息结构体
  135. * @param cluster 指定簇
  136. * @param value 要写入该fat表项的值
  137. * @return uint32_t errcode
  138. */
  139. uint32_t fat32_write_FAT_entry(struct block_device *blk, fat32_sb_info_t *fsbi, uint32_t cluster, uint32_t value)
  140. {
  141. // 计算每个扇区内含有的FAT表项数
  142. // FAT每项4bytes
  143. uint32_t fat_ent_per_sec = (fsbi->bytes_per_sec >> 2); // 该值应为2的n次幂
  144. uint32_t *buf = kzalloc(fsbi->bytes_per_sec, 0);
  145. blk->bd_disk->fops->transfer(blk->bd_disk, AHCI_CMD_READ_DMA_EXT,
  146. fsbi->FAT1_base_sector + (cluster / fat_ent_per_sec), 1, (uint64_t)buf);
  147. buf[cluster & (fat_ent_per_sec - 1)] = (buf[cluster & (fat_ent_per_sec - 1)] & 0xf0000000) | (value & 0x0fffffff);
  148. // 向FAT1和FAT2写入数据
  149. blk->bd_disk->fops->transfer(blk->bd_disk, AHCI_CMD_WRITE_DMA_EXT,
  150. fsbi->FAT1_base_sector + (cluster / fat_ent_per_sec), 1, (uint64_t)buf);
  151. blk->bd_disk->fops->transfer(blk->bd_disk, AHCI_CMD_WRITE_DMA_EXT,
  152. fsbi->FAT2_base_sector + (cluster / fat_ent_per_sec), 1, (uint64_t)buf);
  153. kfree(buf);
  154. return 0;
  155. }
  156. /**
  157. * @brief 在父亲inode的目录项簇中,寻找连续num个空的目录项
  158. *
  159. * @param parent_inode 父inode
  160. * @param num 请求的目录项数量
  161. * @param mode 操作模式
  162. * @param res_sector 返回信息:缓冲区对应的扇区号
  163. * @param res_cluster 返回信息:缓冲区对应的簇号
  164. * @param res_data_buf_base 返回信息:缓冲区的内存基地址(记得要释放缓冲区内存!!!!)
  165. * @return struct fat32_Directory_t*
  166. * 符合要求的entry的指针(指向地址高处的空目录项,也就是说,有连续num个≤这个指针的空目录项)
  167. */
  168. struct fat32_Directory_t *fat32_find_empty_dentry(struct vfs_index_node_t *parent_inode, uint32_t num, uint32_t mode,
  169. uint32_t *res_sector, uint64_t *res_cluster,
  170. uint64_t *res_data_buf_base)
  171. {
  172. // kdebug("find empty_dentry");
  173. struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)parent_inode->private_inode_info;
  174. fat32_sb_info_t *fsbi = (fat32_sb_info_t *)parent_inode->sb->private_sb_info;
  175. uint8_t *buf = kzalloc(fsbi->bytes_per_clus, 0);
  176. struct block_device *blk = parent_inode->sb->blk_device;
  177. // 计算父目录项的起始簇号
  178. uint32_t cluster = finode->first_clus;
  179. struct fat32_Directory_t *tmp_dEntry = NULL;
  180. // 指向最终的有用的dentry的指针
  181. struct fat32_Directory_t *result_dEntry = NULL;
  182. while (true)
  183. {
  184. // 计算父目录项的起始LBA扇区号
  185. uint64_t sector = fsbi->first_data_sector + (cluster - 2) * fsbi->sec_per_clus;
  186. // 读取父目录项的起始簇数据
  187. blk->bd_disk->fops->transfer(blk->bd_disk, AHCI_CMD_READ_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)buf);
  188. tmp_dEntry = (struct fat32_Directory_t *)buf;
  189. // 计数连续的空目录项
  190. uint32_t count_continuity = 0;
  191. // 查找连续num个空闲目录项
  192. for (int i = 0; (i < fsbi->bytes_per_clus) && count_continuity < num; i += 32, ++tmp_dEntry)
  193. {
  194. if (!(tmp_dEntry->DIR_Name[0] == 0xe5 || tmp_dEntry->DIR_Name[0] == 0x00))
  195. {
  196. count_continuity = 0;
  197. continue;
  198. }
  199. if (count_continuity == 0)
  200. result_dEntry = tmp_dEntry;
  201. ++count_continuity;
  202. }
  203. // 成功查找到符合要求的目录项
  204. if (count_continuity == num)
  205. {
  206. result_dEntry += (num - 1);
  207. *res_sector = sector;
  208. *res_data_buf_base = (uint64_t)buf;
  209. *res_cluster = cluster;
  210. return result_dEntry;
  211. }
  212. // 当前簇没有发现符合条件的空闲目录项,寻找下一个簇
  213. uint64_t old_cluster = cluster;
  214. cluster = fat32_read_FAT_entry(blk, fsbi, cluster);
  215. if (cluster >= 0x0ffffff7) // 寻找完父目录的所有簇,都没有找到符合要求的空目录项
  216. {
  217. // 新增一个簇
  218. if (fat32_alloc_clusters(parent_inode, &cluster, 1) != 0)
  219. {
  220. kerror("Cannot allocate a new cluster!");
  221. while (1)
  222. pause();
  223. }
  224. // 将这个新的簇清空
  225. sector = fsbi->first_data_sector + (cluster - 2) * fsbi->sec_per_clus;
  226. void *tmp_buf = kzalloc(fsbi->bytes_per_clus, 0);
  227. blk->bd_disk->fops->transfer(blk->bd_disk, AHCI_CMD_WRITE_DMA_EXT, sector, fsbi->sec_per_clus,
  228. (uint64_t)tmp_buf);
  229. kfree(tmp_buf);
  230. }
  231. }
  232. }
  233. /**
  234. * @brief 检查文件名是否合法
  235. *
  236. * @param name 文件名
  237. * @param namelen 文件名长度
  238. * @param reserved 保留字段
  239. * @return int 合法:0, 其他:错误码
  240. */
  241. int fat32_check_name_available(const char *name, int namelen, int8_t reserved)
  242. {
  243. if (namelen > 255 || namelen <= 0)
  244. return -ENAMETOOLONG;
  245. // 首个字符不能是空格或者'.'
  246. if (name[0] == 0x20 || name[0] == '.')
  247. return -EINVAL;
  248. return 0;
  249. }
  250. /**
  251. * @brief 检查字符在短目录项中是否合法
  252. *
  253. * @param c 给定字符
  254. * @param index 字符在文件名中处于第几位
  255. * @return true 合法
  256. * @return false 不合法
  257. */
  258. bool fat32_check_char_available_in_short_name(const char c, int index)
  259. {
  260. // todo: 严格按照fat规范完善合法性检查功能
  261. if (index == 0)
  262. {
  263. if (c < 0x20)
  264. {
  265. if (c != 0x05)
  266. return false;
  267. return true;
  268. }
  269. }
  270. for (int i = 0; i < sizeof(unavailable_character_in_short_name) / sizeof(char); ++i)
  271. {
  272. if (c == unavailable_character_in_short_name[i])
  273. return false;
  274. }
  275. return true;
  276. }
  277. /**
  278. * @brief 填充短目录项的函数
  279. *
  280. * @param dEntry 目标dentry
  281. * @param target 目标dentry对应的短目录项
  282. * @param cluster 短目录项对应的文件/文件夹起始簇
  283. */
  284. void fat32_fill_shortname(struct vfs_dir_entry_t *dEntry, struct fat32_Directory_t *target, uint32_t cluster)
  285. {
  286. memset(target, 0, sizeof(struct fat32_Directory_t));
  287. {
  288. int tmp_index = 0;
  289. // kdebug("dEntry->name_length=%d", dEntry->name_length);
  290. for (tmp_index = 0; tmp_index < min(8, dEntry->name_length); ++tmp_index)
  291. {
  292. if (dEntry->name[tmp_index] == '.')
  293. break;
  294. if (fat32_check_char_available_in_short_name(dEntry->name[tmp_index], tmp_index))
  295. target->DIR_Name[tmp_index] = dEntry->name[tmp_index];
  296. else
  297. target->DIR_Name[tmp_index] = 0x20;
  298. }
  299. // 在字符串末尾加入\0
  300. if (tmp_index < 8 && tmp_index == dEntry->name_length)
  301. {
  302. target->DIR_Name[tmp_index] = '\0';
  303. ++tmp_index;
  304. }
  305. // 不满的部分使用0x20填充
  306. while (tmp_index < 8)
  307. {
  308. // kdebug("tmp index = %d", tmp_index);
  309. target->DIR_Name[tmp_index] = 0x20;
  310. ++tmp_index;
  311. }
  312. if (dEntry->dir_inode->attribute & VFS_IF_DIR)
  313. {
  314. while (tmp_index < 11)
  315. {
  316. // kdebug("tmp index = %d", tmp_index);
  317. target->DIR_Name[tmp_index] = 0x20;
  318. ++tmp_index;
  319. }
  320. }
  321. else
  322. {
  323. for (int j = 8; j < 11; ++j)
  324. {
  325. target->DIR_Name[j] = 'a';
  326. }
  327. }
  328. }
  329. struct vfs_index_node_t *inode = dEntry->dir_inode;
  330. target->DIR_Attr = 0;
  331. if (inode->attribute & VFS_IF_DIR)
  332. target->DIR_Attr |= ATTR_DIRECTORY;
  333. target->DIR_FileSize = dEntry->dir_inode->file_size;
  334. target->DIR_FstClusHI = (uint16_t)((cluster >> 16) & 0x0fff);
  335. target->DIR_FstClusLO = (uint16_t)(cluster & 0xffff);
  336. // todo: 填写短目录项中的时间信息
  337. }
  338. /**
  339. * @brief 填充长目录项的函数
  340. *
  341. * @param dEntry 目标dentry
  342. * @param target 起始长目录项
  343. * @param checksum 短目录项的校验和
  344. * @param cnt_longname 总的长目录项的个数
  345. */
  346. void fat32_fill_longname(struct vfs_dir_entry_t *dEntry, struct fat32_LongDirectory_t *target, uint8_t checksum,
  347. uint32_t cnt_longname)
  348. {
  349. uint32_t current_name_index = 0;
  350. struct fat32_LongDirectory_t *Ldentry = (struct fat32_LongDirectory_t *)(target + 1);
  351. // kdebug("filling long name, name=%s, namelen=%d", dEntry->name, dEntry->name_length);
  352. for (int i = 1; i <= cnt_longname; ++i)
  353. {
  354. --Ldentry;
  355. Ldentry->LDIR_Ord = i;
  356. for (int j = 0; j < 5; ++j, ++current_name_index)
  357. {
  358. if (current_name_index < dEntry->name_length)
  359. Ldentry->LDIR_Name1[j] = dEntry->name[current_name_index];
  360. else if (current_name_index == dEntry->name_length)
  361. Ldentry->LDIR_Name1[j] = '\0';
  362. else
  363. Ldentry->LDIR_Name1[j] = 0xffff;
  364. }
  365. for (int j = 0; j < 6; ++j, ++current_name_index)
  366. {
  367. if (current_name_index < dEntry->name_length)
  368. Ldentry->LDIR_Name2[j] = dEntry->name[current_name_index];
  369. else if (current_name_index == dEntry->name_length)
  370. Ldentry->LDIR_Name1[j] = '\0';
  371. else
  372. Ldentry->LDIR_Name2[j] = 0xffff;
  373. }
  374. for (int j = 0; j < 2; ++j, ++current_name_index)
  375. {
  376. if (current_name_index < dEntry->name_length)
  377. Ldentry->LDIR_Name3[j] = dEntry->name[current_name_index];
  378. else if (current_name_index == dEntry->name_length)
  379. Ldentry->LDIR_Name1[j] = '\0';
  380. else
  381. Ldentry->LDIR_Name3[j] = 0xffff;
  382. }
  383. Ldentry->LDIR_Attr = ATTR_LONG_NAME;
  384. Ldentry->LDIR_FstClusLO = 0;
  385. Ldentry->LDIR_Type = 0;
  386. Ldentry->LDIR_Chksum = checksum;
  387. }
  388. // 最后一个长目录项的ord要|=0x40
  389. Ldentry->LDIR_Ord |= 0x40;
  390. }