fat_ent.c 14 KB

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