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