From 0e2caea773e6b1574ee351a9bf561db85ffae96b Mon Sep 17 00:00:00 2001 From: qcloud Date: Thu, 6 Jun 2024 18:48:40 +0800 Subject: [PATCH 1/7] =?UTF-8?q?fuse=E6=96=87=E4=BB=B6=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fast_fuse/difuse/README.md | 32 +++ .../fast_fuse/difuse/src/Makefile | 29 +++ .../fast_fuse/difuse/src/difuse.c | 231 ++++++++++++++++++ .../fast_fuse/difuse/test/test.sh | 87 +++++++ 4 files changed, 379 insertions(+) create mode 100644 eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/README.md create mode 100644 eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/Makefile create mode 100644 eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse.c create mode 100755 eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/test/test.sh diff --git a/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/README.md b/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/README.md new file mode 100644 index 000000000..1e0eb6f70 --- /dev/null +++ b/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/README.md @@ -0,0 +1,32 @@ +# DIFUSE + +## 代码结构 + +``` +. +├── README.md +├── src +│ ├── difuse.c 源码 +│ ├── Makefile +│ └── mountpoints 挂载目录 +└── test + └── test.sh 测试脚本 +``` + + + +## Build + +在`src`目录下执行`make` + +```bash +cd src +make +``` + +在`test`目录下执行`test`脚本 + +```bash +cd test +./test.sh +``` \ No newline at end of file diff --git a/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/Makefile b/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/Makefile new file mode 100644 index 000000000..aa3c52834 --- /dev/null +++ b/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/Makefile @@ -0,0 +1,29 @@ +# Makefile for FUSE difuse example + +# Compiler +CC = gcc + +# Compiler flags +CFLAGS = -Wall `pkg-config fuse3 --cflags` + +# Linker flags +LDFLAGS = `pkg-config fuse3 --libs` + +# Source files +SRCS = difuse.c + +# Output executable +TARGET = difuse + +# Build target +all: $(TARGET) + +# Rule to build the target +$(TARGET): $(SRCS) + $(CC) $(CFLAGS) -o $(TARGET) $(SRCS) $(LDFLAGS) + +# Clean rule +clean: + rm -f $(TARGET) + +.PHONY: all clean diff --git a/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse.c b/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse.c new file mode 100644 index 000000000..a7393dd6b --- /dev/null +++ b/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse.c @@ -0,0 +1,231 @@ +#define FUSE_USE_VERSION 31 + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/*相关数据结构*/ + +#define FILE_TYPE 1 +#define DIRECTORY_TYPE 2 + + +struct dfs_data +{ + int chunk_size; + size_t size; + struct dfs_data *next; +}; + +struct dfs_inode +{ + uint32_t ino; //inode编号 + int size; //文件大小 + int dir_cnt; // 如果是目录类型文件,下面有几个目录项 + struct dfs_data *data_pointer; //指向数据块的指针 +}; + +struct dfs_dentry +{ + char fname[255]; + int ftype; + struct dfs_dentry *parent; + struct dfs_dentry *brother; + struct dfs_dentry *child; + struct dfs_inode *inode; //指向对应的inode +}; + +struct dfs_dentry *root; //根节点 + +/*过程函数*/ +static struct dfs_inode *new_inode(uint32_t ino, int size, int dir_cnt) +{ + struct dfs_inode *inode = (struct dfs_inode *)malloc(sizeof(struct dfs_inode)); + inode->ino = ino; + inode->size = size; + inode->dir_cnt = dir_cnt; + inode->data_pointer = NULL; + return inode; +} + +static struct dfs_dentry *new_dentry(char *fname, int ftype, struct dfs_dentry *parent, struct dfs_inode *inode) +{ + struct dfs_dentry *dentry = (struct dfs_dentry *)malloc(sizeof(struct dfs_dentry)); + strcpy(dentry->fname, fname); + dentry->inode = inode; + dentry->brother = NULL; + dentry->parent = parent; + dentry->child = NULL; + dentry->ftype = ftype; + return dentry; +} + +void add_child_dentry(struct dfs_dentry *parent, struct dfs_dentry *child) +{ + child->brother = parent->child; + parent->child = child; +} + +struct dfs_dentry *look_up(struct dfs_dentry *dentrys, const char *path) +{ + struct dfs_dentry *dentry = dentrys; + char *path_copy = strdup(path); + char *token = strtok(path_copy, "/"); + while (token != NULL && dentry != NULL) + { + struct dfs_dentry *child = dentry->child; + while (child != NULL && strcmp(child->fname, token) != 0) + { + child = child->brother; + } + dentry = child; + token = strtok(NULL, "/"); + } + + free(path_copy); + return dentry; +} + + +/*功能函数*/ + +static int di_getattr(const char *path, struct stat *di_stat, + struct fuse_file_info *fi) +{ + (void)fi; + int ret = 0; + memset(di_stat, 0, sizeof(struct stat)); + + struct dfs_dentry *dentry = look_up(root, path); + if (dentry == NULL) + return -ENOENT; + + if (dentry->ftype == DIRECTORY_TYPE) + { + di_stat->st_mode = S_IFDIR | 0755; + di_stat->st_nlink = 2; + } + else if (dentry->ftype == FILE_TYPE) + { + di_stat->st_mode = S_IFREG | 0644; + di_stat->st_nlink = 1; + di_stat->st_size = dentry->inode->size; + } + + return ret; +} + +/*遍历目录项*/ +static int di_readdir(const char *path, void *buf, fuse_fill_dir_t filler, + off_t offset, struct fuse_file_info *fi) +{ + (void)fi; + (void)offset; + struct dfs_dentry *dentry = look_up(root, path); + + if (dentry == NULL) + return -ENOENT; + + if (dentry->ftype != DIRECTORY_TYPE) + return -ENOTDIR; + + filler(buf, ".", NULL, 0, 0); + filler(buf, "..", NULL, 0, 0); + + struct dfs_dentry *child = dentry->child; + while (child != NULL) + { + filler(buf, child->fname, NULL, 0, 0); + child = child->brother; + } + + return 0; +} + +static int di_open(const char *path, struct fuse_file_info *fi) +{ + struct dfs_dentry *dentry = look_up(root, path); + + if (dentry == NULL) + return -ENOENT; + + if (dentry->ftype != FILE_TYPE) + return -EISDIR; + + return 0; +} + +/* +确保我们尝试从文件中读取的起始位置(偏移量)在文件范围内。 +如果偏移量超出了文件内容的长度,说明请求读取的位置在文件的末尾或之后,这种情况下不能读取任何数据。 +*/ +static int di_read(const char *path, char *buf, size_t size, off_t offset, + struct fuse_file_info *fi) +{ + struct dfs_dentry *dentry = look_up(root, path); + + if (dentry == NULL) + return -ENOENT; + + if (dentry->ftype != FILE_TYPE) + return -EISDIR; + + if (offset < dentry->inode->size) + { + if (offset + size > dentry->inode->size) + size = dentry->inode->size - offset; + memcpy(buf, "dummy_content", size); // Replace with actual file data handling + } + else + size = 0; + + return size; +} + + +static struct fuse_operations difs_ops = { + .readdir = di_readdir, + .getattr = di_getattr, + .open = di_open, + .read = di_read, + //.mkdir = di_mkdir, +}; + +int main(int argc, char *argv[]) +{ + struct dfs_inode *root_inode = new_inode(1, 0, 2); + root = new_dentry("/", DIRECTORY_TYPE, NULL, root_inode); + + // 创建dir1目录 + struct dfs_inode *dir1_inode = new_inode(2, 0, 2); + struct dfs_dentry *dir1 = new_dentry("dir1", DIRECTORY_TYPE, root, dir1_inode); + add_child_dentry(root, dir1); + + // 创建dir2目录 + struct dfs_inode *dir2_inode = new_inode(3, 0, 1); + struct dfs_dentry *dir2 = new_dentry("dir2", DIRECTORY_TYPE, root, dir2_inode); + add_child_dentry(root, dir2); + + // 创建file1文件 + struct dfs_inode *file1_inode = new_inode(4, 100, 0); + struct dfs_dentry *file1 = new_dentry("file1", FILE_TYPE, dir1, file1_inode); + add_child_dentry(dir1, file1); + + // 创建file2文件 + struct dfs_inode *file2_inode = new_inode(5, 200, 0); + struct dfs_dentry *file2 = new_dentry("file2", FILE_TYPE, dir1, file2_inode); + add_child_dentry(dir1, file2); + + // 创建file3文件 + struct dfs_inode *file3_inode = new_inode(6, 150, 0); + struct dfs_dentry *file3 = new_dentry("file3", FILE_TYPE, dir2, file3_inode); + add_child_dentry(dir2, file3); + + return fuse_main(argc, argv, &difs_ops, NULL); +} \ No newline at end of file diff --git a/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/test/test.sh b/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/test/test.sh new file mode 100755 index 000000000..5ad7744d0 --- /dev/null +++ b/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/test/test.sh @@ -0,0 +1,87 @@ +#!/bin/bash + +# 挂载点目录 +MOUNT_POINT="../src/mountpoints" +SRC_DIR="../src" + +# FUSE 可执行文件 +FUSE_EXEC="../src/difuse" + +echo "Compiling FUSE filesystem..." +make -C "$SRC_DIR" clean +make -C "$SRC_DIR" all + +# 检查编译是否成功 +if [ ! -f "$FUSE_EXEC" ]; then + echo "Compilation failed. Exiting." + exit 1 +fi + +# 创建挂载点目录(如果不存在) +if [ ! -d "$MOUNT_POINT" ]; then + mkdir -p "$MOUNT_POINT" +fi + +# 挂载 FUSE 文件系统(前台运行并显示调试信息) +echo "Mounting FUSE filesystem..." +$FUSE_EXEC -f -d "$MOUNT_POINT" & +FUSE_PID=$! +sleep 1 # 等待文件系统完全挂载 + +# 测试读取根目录 +echo "Testing readdir on root..." +ls "$MOUNT_POINT" + +# 测试读取子目录 dir1 +echo "Testing readdir on dir1..." +ls "$MOUNT_POINT/dir1" + +# 测试读取子目录 dir2 +echo "Testing readdir on dir2..." +ls "$MOUNT_POINT/dir2" + +# 测试读取文件 file1 +echo "Testing read file1..." +cat "$MOUNT_POINT/dir1/file1" + +# 测试读取文件 file2 +echo "Testing read file2..." +cat "$MOUNT_POINT/dir1/file2" + +# 测试读取文件 file3 +echo "Testing read file3..." +cat "$MOUNT_POINT/dir2/file3" + +# 测试打开文件 +echo "Testing open file..." +if [ -e "$MOUNT_POINT/dir1/file1" ]; then + echo "File 'file1' exists in dir1" +else + echo "File 'file1' does not exist in dir1" +fi + +if [ -e "$MOUNT_POINT/dir1/file2" ]; then + echo "File 'file2' exists in dir1" +else + echo "File 'file2' does not exist in dir1" +fi + +if [ -e "$MOUNT_POINT/dir2/file3" ]; then + echo "File 'file3' exists in dir2" +else + echo "File 'file3' does not exist in dir2" +fi + +# 显示调试日志 +echo "FUSE debug log:" +kill -USR1 $FUSE_PID +sleep 1 # 等待日志输出 + +# 卸载 FUSE 文件系统 +echo "Unmounting FUSE filesystem..." +sudo fusermount -u "$MOUNT_POINT" + +# 等待 FUSE 进程终止 +wait $FUSE_PID + +echo "All tests completed." From 4fffe9abaa615cba880e25394c571652d166b817 Mon Sep 17 00:00:00 2001 From: qcloud Date: Fri, 14 Jun 2024 18:05:15 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fast_fuse/difuse/src/difuse | Bin 0 -> 17184 bytes .../fast_fuse/difuse/src/difuse.c | 86 +++++++++++++++--- 2 files changed, 74 insertions(+), 12 deletions(-) create mode 100755 eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse diff --git a/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse b/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse new file mode 100755 index 0000000000000000000000000000000000000000..44ac4e9ddd6c75119c1e5e0fea009c450cef6419 GIT binary patch literal 17184 zcmeHOeQ;dWb-ycnu^^T#gAKAEcy>s{ zy#*;jR79cUHyag^q15R(8ABkzgm#pE3d#f-QVW{E$mvWfChd$$ppsqw7WP$gW@8^=mt( zLYK#s<4JDl+NtB|r;U(NV(Pc+)^>inN0|wxR0gHPZ&~pVQ(i0Eg`^v?(?=gxuwcsN zeFkTE7cDtbUc|-e^Y8mCEIswYe>cGQ_0?3e|`VX9rZi5h5ORsEmBVP z3gMzLweR*e>W&G*bQnd&kmA`N$<}E(>A&;*oj>{G=jz&j{QBE(oU9$X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}2DdGQJ;G34z+*~!* z$IC15l;yi$t^>Xn{azDMsvLiDxFc&dh0{2N5o=GHR;)_^=(BR2o#76#bpFG%ddv%3W z2~%#*!^Fdx(6owF<4HQtOU0B&8~zM zvn-%VI(`zD3Q04aN%S%V3X61x8nyRO%fYA_y?qazXxVe;?JWoQ?*@zmOJ>y2Qe)2{ zVeUI{$F7zG<{f+Y9*!O{kF@MM5H;noln9?Qp%Wz+ooaEVIC}ES4%g7(m&|~xEqx{8 zF)Joy1V!OvQ0pNOr3_*5g{J2K2^7Hl4OmmG-_qT@!iM$I8a; zPGoURC=;`~^;w@HTWUluW2UpZXSo>D#F-@SFFLHW>PHTZwR2Z!Vn4Vq|5l0T0xkLs;sQaSgTX+Fwr- zOsaRs`np9pKG2II6varD5(-B$-CM+%kYy987#ZkXCS{4RtZx|Ea2J9rGku8;5w_3- z5vCaj?X(;{;Y7EdW8HChV@~-{lTBH09!14^l5nQX(QhJ*S>8kcqW!NEz1LyM# z{`0c@UZ=%Um-9q_6BoTpv3;p?p`=TR*L}qau>%>ez5jeD{eR|7?s};~dM@nq^%FO-c+d0!GefAStz?Aja0rO19eD+gXz;u)iHDx=d&-(0->h~$88!jz&z;=w% zw`|__^YboK-WOthfBfiuhwQokMM!IEx?QtO{PwRx;I-eW?U+8yhMM~Af8w*B()LVm zax11B@c7a0#9PQzg%posg+_DnBino = ino; + inode->ino = next_ino++; inode->size = size; inode->dir_cnt = dir_cnt; inode->data_pointer = NULL; @@ -72,18 +73,35 @@ void add_child_dentry(struct dfs_dentry *parent, struct dfs_dentry *child) parent->child = child; } -struct dfs_dentry *look_up(struct dfs_dentry *dentrys, const char *path) +struct dfs_dentry *traverse_path(struct dfs_dentry *start_dentry, const char *path, int ftype, int create) { - struct dfs_dentry *dentry = dentrys; + struct dfs_dentry *dentry = start_dentry; char *path_copy = strdup(path); char *token = strtok(path_copy, "/"); - while (token != NULL && dentry != NULL) + + while (token != NULL) { struct dfs_dentry *child = dentry->child; while (child != NULL && strcmp(child->fname, token) != 0) { child = child->brother; } + + if (child == NULL) + { + if (create) + { + struct dfs_inode *new_inodes = new_inode(0, 0); // 创建新的 inode + child = new_dentry(token, ftype, dentry, new_inodes); // 创建新的目录项 + add_child_dentry(dentry, child); // 将新目录项添加到父目录项的子目录列表中 + } + else + { + free(path_copy); + return NULL; + } + } + dentry = child; token = strtok(NULL, "/"); } @@ -92,9 +110,48 @@ struct dfs_dentry *look_up(struct dfs_dentry *dentrys, const char *path) return dentry; } +struct dfs_dentry *look_up(struct dfs_dentry *dentrys, const char *path) +{ + return traverse_path(dentrys, path, 0, 0); +} + +struct dfs_dentry *lookup_or_create_dentry(const char *path, struct dfs_dentry *start_dentry, int ftype) +{ + return traverse_path(start_dentry, path, ftype, 1); +} + + /*功能函数*/ +static int di_mkdir(const char *path, mode_t mode) +{ + (void)mode; + struct dfs_dentry *dentry = lookup_or_create_dentry(path, root, DIRECTORY_TYPE); + if (dentry == NULL) + { + return -ENOENT; + } + + return 0; +} + + +static int dfs_create(const char *path, mode_t mode, struct fuse_file_info *fi) +{ + (void)mode; + (void)fi; + struct dfs_dentry *dentry = lookup_or_create_dentry(path, root, FILE_TYPE); + if (dentry == NULL) + { + return -ENOENT; + } + + return 0; +} + + + static int di_getattr(const char *path, struct stat *di_stat, struct fuse_file_info *fi) { @@ -189,41 +246,46 @@ static int di_read(const char *path, char *buf, size_t size, off_t offset, } + + + + static struct fuse_operations difs_ops = { .readdir = di_readdir, .getattr = di_getattr, .open = di_open, .read = di_read, - //.mkdir = di_mkdir, + .mkdir = di_mkdir, + .create = dfs_create, }; int main(int argc, char *argv[]) { - struct dfs_inode *root_inode = new_inode(1, 0, 2); + struct dfs_inode *root_inode = new_inode(0, 0); root = new_dentry("/", DIRECTORY_TYPE, NULL, root_inode); // 创建dir1目录 - struct dfs_inode *dir1_inode = new_inode(2, 0, 2); + struct dfs_inode *dir1_inode = new_inode(0, 0); struct dfs_dentry *dir1 = new_dentry("dir1", DIRECTORY_TYPE, root, dir1_inode); add_child_dentry(root, dir1); // 创建dir2目录 - struct dfs_inode *dir2_inode = new_inode(3, 0, 1); + struct dfs_inode *dir2_inode = new_inode(0, 0); struct dfs_dentry *dir2 = new_dentry("dir2", DIRECTORY_TYPE, root, dir2_inode); add_child_dentry(root, dir2); // 创建file1文件 - struct dfs_inode *file1_inode = new_inode(4, 100, 0); + struct dfs_inode *file1_inode = new_inode(100, 0); struct dfs_dentry *file1 = new_dentry("file1", FILE_TYPE, dir1, file1_inode); add_child_dentry(dir1, file1); // 创建file2文件 - struct dfs_inode *file2_inode = new_inode(5, 200, 0); + struct dfs_inode *file2_inode = new_inode(200, 0); struct dfs_dentry *file2 = new_dentry("file2", FILE_TYPE, dir1, file2_inode); add_child_dentry(dir1, file2); // 创建file3文件 - struct dfs_inode *file3_inode = new_inode(6, 150, 0); + struct dfs_inode *file3_inode = new_inode(150, 0); struct dfs_dentry *file3 = new_dentry("file3", FILE_TYPE, dir2, file3_inode); add_child_dentry(dir2, file3); From 051481ba80c0d0ecf764835ae4b1a66f8a39ad95 Mon Sep 17 00:00:00 2001 From: qcloud Date: Wed, 10 Jul 2024 15:13:46 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E6=B7=BB=E5=8A=A0touch=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fast_fuse/difuse/src/difuse | Bin 17184 -> 0 bytes .../fast_fuse/difuse/src/difuse.c | 62 +++++++++++++----- 2 files changed, 46 insertions(+), 16 deletions(-) delete mode 100755 eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse diff --git a/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse b/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse deleted file mode 100755 index 44ac4e9ddd6c75119c1e5e0fea009c450cef6419..0000000000000000000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 Jul 2024 16:49:34 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fast_fuse/difuse/src/difuse | Bin 0 -> 17520 bytes .../fast_fuse/difuse/src/difuse.c | 158 ++++++++++++------ .../fast_fuse/difuse/test/test.sh | 68 ++------ 3 files changed, 119 insertions(+), 107 deletions(-) create mode 100755 eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse diff --git a/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse b/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse new file mode 100755 index 0000000000000000000000000000000000000000..02094cfdae543c5bd3357c5503788493355417e5 GIT binary patch literal 17520 zcmeHP3vgW3c|N<=Vi{yh20O?P@Qw|Xm)4SB5jZX@+iR~=3j~%@NRwVx+O@UuYIoVa zYs<89#TYu?St|%aD48~`Nke&NMo9@{6A#G2AiH&fO;ReRnW>UMW>q;w7N)VO@%H=A zIset&>s`{Jna*^oGur*n|NXCX{&ViRkG)5K7TCPO?Q#h&)5Px!;_jWT5Wf}-d`?$D z{Gw6J#P=d`wYUU)g~XKHuLz{747-bIrN*a$l3guTO3;N07EHN?M9HpPshpuCOht|2 z$<9Mn%{ysRmC|F%ax0!!$%TWS(d}nyx1-#2)+xKrHN~`2cjE4A6wY>(F0$*^cHP>J zsnGdk%K0RB^l8-XangFos4#Wfb!s~&U9ZdpQ;I=l%X?<{)5L4hb_>1Ijd0TA3KmQ` z-kq?cy!>|y5A$6*Ub%62Lg$kyjW?C0v1t3M6-#5`x>z)x>8|T;TvfMfg)g1(Et7I; zmlq$JQyXt@qv04NOs7$}dlk?ANH#~yNnflUKlg?1zB>;cS=ZKk=$*Z%zcV&KI@E5m zkvdwRha9XN_F15$qw?_p!GbU^plI%0~gsYryNa3)Dm2NYmVftJ%OUQS#G z)m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%V&eAK3T>OJ@c)haSSPa{=P-~;r(gXX~99@H17MG6u{R3^dlJgapwOYw4aalh0{vy z=S}Trz~P7Xv0V;N`*i!|vrE|gO5)nsM$T#dF+DEwStaDNu`W|So$a{_xYuUS)z6YX z+W(BBJ$~S&=E)Y|i`m#l=~?~dZd_G5t`FkU|2$FSSIRtIVoOKLZ15$p>m<}3HO$a9 z0U&K=wr=%xh{^pfhS_Cw(2kchG{XsFTP)EYiWy-ukxCn(Ot%jPmm?W z#+^^vnnYWNlDjNz=_?WrnIR#^Llo~w@>^Sk)b64pyI3Nz-M}s*j4tfe{v^FMsYuVc z8_Ec$6Gmq!9!AI14kb$+8<31mcfeH`TcR+)VS!v59i7ow7<;YcBvG59EDP0vcvz$o z2@_3ePp5TD~Tq-Gi@q`)iZHs4o z$rL7D%G_mx+A~N|T{NtrrnQ^u=+Kqw@(}Tbcf}DxNQ#LpC6tk9B3{H8kfkEA5Gm?f zGG+>&oZCKBecO;|SxHAagwI45gpZD8XeZ=M@rPt|L^aD+*`I z9AhYaSU+$;(obo>lS0cqmO@-}sp8y!%=gJ!EOog~bO}Dw@nQRN`)o;<3ZI7*C&VgL z?Do$6w)DHBy;$q92I>0NZqL{GOucqv)SdQ!2n;_x(dkoMI8 zdwLeYJU+?E42ZD+$&hh^%pmuw&wqv?qnOWa_xxn;khy9=yFl9YxsNbt7e>+$)8}|6K{tpA|u{}R`V4QTR z*eTB)B~jn|b^LC9qB|Wzj?22UQE{k9(Zj2i#(al? HgBAY;!vt+2 literal 0 HcmV?d00001 diff --git a/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse.c b/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse.c index b8302a865..387b0304c 100644 --- a/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse.c +++ b/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse.c @@ -8,12 +8,13 @@ #include #include #include -#include /*相关数据结构*/ #define FILE_TYPE 1 #define DIRECTORY_TYPE 2 +#define MAX_INODES 1000 //最大 inode 数量 +#define HASH_SIZE 1024 uint32_t next_ino = 1; @@ -30,6 +31,8 @@ struct dfs_inode int size; //文件大小 int dir_cnt; // 如果是目录类型文件,下面有几个目录项 struct dfs_data *data_pointer; //指向数据块的指针 + struct dfs_inode *prev; // LRU 链表前驱指针 + struct dfs_inode *next; // LRU 链表后继指针 }; struct dfs_dentry @@ -40,9 +43,80 @@ struct dfs_dentry struct dfs_dentry *brother; struct dfs_dentry *child; struct dfs_inode *inode; //指向对应的inode + struct dfs_dentry *prev; //LRU 链表前驱指针 + struct dfs_dentry *next; //LRU 链表后继指针 }; struct dfs_dentry *root; //根节点 +struct dfs_dentry *lru_head = NULL; //LRU 链表头 +struct dfs_dentry *lru_tail = NULL; //LRU 链表尾 +struct dfs_dentry *hash_table[HASH_SIZE]; //哈希表 + +/*缓存管理*/ + +static unsigned int hash(const char *path) +{ + unsigned int hash = 0; + while (*path) + { + hash = (hash << 5) + *path++; + } + return hash % HASH_SIZE; +} + +static void lru_remove(struct dfs_dentry *dentry) +{ + if (dentry->prev) + { + dentry->prev->next = dentry->next; + } + else + { + lru_head = dentry->next; + } + if (dentry->next) + { + dentry->next->prev = dentry->prev; + } + else + { + lru_tail = dentry->prev; + } +} + +static void lru_insert(struct dfs_dentry *dentry) +{ + dentry->next = lru_head; + dentry->prev = NULL; + if (lru_head) + { + lru_head->prev = dentry; + } + lru_head = dentry; + if (!lru_tail) + { + lru_tail = dentry; + } +} + +static void lru_access(struct dfs_dentry *dentry) +{ + lru_remove(dentry); + lru_insert(dentry); +} + +static void lru_evict() +{ + if (lru_tail) + { + struct dfs_dentry *evict = lru_tail; + lru_remove(evict); + unsigned int index = hash(evict->fname); + hash_table[index] = NULL; + free(evict->inode); + free(evict); + } +} /*过程函数*/ static struct dfs_inode *new_inode(int size, int dir_cnt) @@ -52,6 +126,8 @@ static struct dfs_inode *new_inode(int size, int dir_cnt) inode->size = size; inode->dir_cnt = dir_cnt; inode->data_pointer = NULL; + inode->prev = NULL; + inode->next = NULL; return inode; } @@ -64,6 +140,8 @@ static struct dfs_dentry *new_dentry(char *fname, int ftype, struct dfs_dentry * dentry->parent = parent; dentry->child = NULL; dentry->ftype = ftype; + dentry->prev = NULL; + dentry->next = NULL; return dentry; } @@ -117,13 +195,31 @@ struct dfs_dentry *look_up(struct dfs_dentry *dentrys, const char *path) struct dfs_dentry *lookup_or_create_dentry(const char *path, struct dfs_dentry *start_dentry, int ftype) { - return traverse_path(start_dentry, path, ftype, 1); -} + unsigned int index = hash(path); + struct dfs_dentry *dentry = hash_table[index]; + if (dentry) + { + lru_access(dentry); + return dentry; + } + dentry = traverse_path(start_dentry, path, ftype, 1); + if (dentry) + { + lru_insert(dentry); + hash_table[index] = dentry; + if (next_ino > MAX_INODES) + { + lru_evict(); + } + } + + return dentry; +} -/*功能函数*/ +/*功能函数*/ static int di_utimens(const char *path, const struct timespec ts[2], struct fuse_file_info *fi) { (void)fi; @@ -136,7 +232,6 @@ static int di_utimens(const char *path, const struct timespec ts[2], struct fuse return 0; } - static int di_mkdir(const char *path, mode_t mode) { (void)mode; @@ -149,7 +244,6 @@ static int di_mkdir(const char *path, mode_t mode) return 0; } - static int dfs_create(const char *path, mode_t mode, struct fuse_file_info *fi) { (void)mode; @@ -168,10 +262,7 @@ static int dfs_create(const char *path, mode_t mode, struct fuse_file_info *fi) return 0; } - - -static int di_getattr(const char *path, struct stat *di_stat, - struct fuse_file_info *fi) +static int di_getattr(const char *path, struct stat *di_stat, struct fuse_file_info *fi) { (void)fi; int ret = 0; @@ -197,8 +288,7 @@ static int di_getattr(const char *path, struct stat *di_stat, } /*遍历目录项*/ -static int di_readdir(const char *path, void *buf, fuse_fill_dir_t filler, - off_t offset, struct fuse_file_info *fi, enum fuse_readdir_flags flags) +static int di_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi, enum fuse_readdir_flags flags) { (void)fi; (void)offset; @@ -237,12 +327,7 @@ static int di_open(const char *path, struct fuse_file_info *fi) return 0; } -/* -确保我们尝试从文件中读取的起始位置(偏移量)在文件范围内。 -如果偏移量超出了文件内容的长度,说明请求读取的位置在文件的末尾或之后,这种情况下不能读取任何数据。 -*/ -static int di_read(const char *path, char *buf, size_t size, off_t offset, - struct fuse_file_info *fi) +static int di_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) { struct dfs_dentry *dentry = look_up(root, path); @@ -256,7 +341,7 @@ static int di_read(const char *path, char *buf, size_t size, off_t offset, { if (offset + size > dentry->inode->size) size = dentry->inode->size - offset; - memcpy(buf, "dummy_content", size); // Replace with actual file data handling + memcpy(buf, "dummy_content", size); } else size = 0; @@ -266,43 +351,15 @@ static int di_read(const char *path, char *buf, size_t size, off_t offset, static void *di_init(struct fuse_conn_info *conn, struct fuse_config *cfg) { - (void)conn; // 如果不使用这个参数,可以忽略它 + (void)conn; // 创建并初始化根目录的 inode 和 dentry struct dfs_inode *root_inode = new_inode(0, 0); root = new_dentry("/", DIRECTORY_TYPE, NULL, root_inode); - // 创建dir1目录 - struct dfs_inode *dir1_inode = new_inode(0, 0); - struct dfs_dentry *dir1 = new_dentry("dir1", DIRECTORY_TYPE, root, dir1_inode); - add_child_dentry(root, dir1); - - // 创建dir2目录 - struct dfs_inode *dir2_inode = new_inode(0, 0); - struct dfs_dentry *dir2 = new_dentry("dir2", DIRECTORY_TYPE, root, dir2_inode); - add_child_dentry(root, dir2); - - // 创建file1文件 - struct dfs_inode *file1_inode = new_inode(100, 0); - struct dfs_dentry *file1 = new_dentry("file1", FILE_TYPE, dir1, file1_inode); - add_child_dentry(dir1, file1); - - // 创建file2文件 - struct dfs_inode *file2_inode = new_inode(200, 0); - struct dfs_dentry *file2 = new_dentry("file2", FILE_TYPE, dir1, file2_inode); - add_child_dentry(dir1, file2); - - // 创建file3文件 - struct dfs_inode *file3_inode = new_inode(150, 0); - struct dfs_dentry *file3 = new_dentry("file3", FILE_TYPE, dir2, file3_inode); - add_child_dentry(dir2, file3); - - // 可以在此进行其他初始化操作 - - return 0; // 可以返回一个自定义的结构体,如果不需要则返回 NULL + return 0; } - static struct fuse_operations difs_ops = { .init = di_init, .readdir = di_readdir, @@ -316,8 +373,5 @@ static struct fuse_operations difs_ops = { int main(int argc, char *argv[]) { - - - return fuse_main(argc, argv, &difs_ops, NULL); } \ No newline at end of file diff --git a/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/test/test.sh b/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/test/test.sh index 5ad7744d0..55967cbd7 100755 --- a/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/test/test.sh +++ b/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/test/test.sh @@ -26,62 +26,20 @@ fi echo "Mounting FUSE filesystem..." $FUSE_EXEC -f -d "$MOUNT_POINT" & FUSE_PID=$! -sleep 1 # 等待文件系统完全挂载 +sleep 2 # 等待文件系统完全挂载 -# 测试读取根目录 -echo "Testing readdir on root..." -ls "$MOUNT_POINT" +# 创建目录 +mkdir $MOUNT_POINT/dir1 +mkdir $MOUNT_POINT/dir2 -# 测试读取子目录 dir1 -echo "Testing readdir on dir1..." -ls "$MOUNT_POINT/dir1" +# 创建文件 +touch $MOUNT_POINT/dir1/file1 +touch $MOUNT_POINT/dir1/file2 +touch $MOUNT_POINT/dir2/file3 -# 测试读取子目录 dir2 -echo "Testing readdir on dir2..." -ls "$MOUNT_POINT/dir2" +# 验证结构 +echo "创建的目录和文件结构:" +ls -l $MOUNT_POINT -# 测试读取文件 file1 -echo "Testing read file1..." -cat "$MOUNT_POINT/dir1/file1" - -# 测试读取文件 file2 -echo "Testing read file2..." -cat "$MOUNT_POINT/dir1/file2" - -# 测试读取文件 file3 -echo "Testing read file3..." -cat "$MOUNT_POINT/dir2/file3" - -# 测试打开文件 -echo "Testing open file..." -if [ -e "$MOUNT_POINT/dir1/file1" ]; then - echo "File 'file1' exists in dir1" -else - echo "File 'file1' does not exist in dir1" -fi - -if [ -e "$MOUNT_POINT/dir1/file2" ]; then - echo "File 'file2' exists in dir1" -else - echo "File 'file2' does not exist in dir1" -fi - -if [ -e "$MOUNT_POINT/dir2/file3" ]; then - echo "File 'file3' exists in dir2" -else - echo "File 'file3' does not exist in dir2" -fi - -# 显示调试日志 -echo "FUSE debug log:" -kill -USR1 $FUSE_PID -sleep 1 # 等待日志输出 - -# 卸载 FUSE 文件系统 -echo "Unmounting FUSE filesystem..." -sudo fusermount -u "$MOUNT_POINT" - -# 等待 FUSE 进程终止 -wait $FUSE_PID - -echo "All tests completed." +# 确保脚本退出时卸载文件系统 +trap "fusermount -u $MOUNT_POINT" EXIT From e1082f598949023803b7ea6626ef89328769107a Mon Sep 17 00:00:00 2001 From: qcloud Date: Fri, 26 Jul 2024 16:22:42 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fast_fuse/difuse/src/difuse | Bin 17520 -> 17624 bytes .../fast_fuse/difuse/src/difuse.c | 66 ++++++++++++++++++ .../fast_fuse/difuse/test/test.sh | 30 +++++++- 3 files changed, 94 insertions(+), 2 deletions(-) diff --git a/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse b/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse index 02094cfdae543c5bd3357c5503788493355417e5..8b255be73fa02d7c7da25c92ddd0ad14ec5f1558 100755 GIT binary patch delta 3107 zcmaJ@3s6+o89w*I0=q6|7hzvK+{Fa6L6H_C@lj!9%}qlyF^a7*6R^Q3?fM9UjkXkK zm@wHaPQ<@)hBVEPrqg6POj6v;6rI+Zu?;%fbg+-XG$zEV-OMt{TB$A%bNiih&y6n0 z^v>M-{pb51=l}n6?!71RoH80#4lPtYt9!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;_jW~ds z>E%4XP~@aDdB5^P>d4RJ2dF!L2cJpq0!cWPM%{u{7#7K6TxHY`%zkovo~K?RQE0Tu zIzLJ7LNQf8?&O{2(J6ba`>equv~+4mu+nx}L-hhnVfxXm^G1!=Ln&>BxkcmkBVK7Q z_)Lx0k7lK-;Bz%z%eb5=?S!yM6ZA7)X(RY@jn~d{rnC?I3ffszV5)}7nT18Nsk11D z*V90eXnP4HW3!plCF7H^n|QHk3qmzBJL~moic7^~U31z-WTVYv@2jXHj#ws;S z4F+W!&wa#6f!d(Q(bRG9se+jm$#32CK^=&nCeOBoh@1-V^H$ZMP_tW1?j<6xR4g~r zh9#&4mS(}m5-X>NVr+*J=Pg-9B&4YfoLb5_Ox=j{i4v!W?jTN?w&NDFYJuZW8Ry7c zB5wT#Y6^N1Tgwlc*JJfZ%8E6weq~=f>(XkMKT>(z((K#jsOtjsLLPs~nRv!Dbx5mP z`Qbq|pc3)MYv#i+oiJjcKJSvxnZOD&dEyyK4S_#Xes7 diff --git a/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse.c b/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse.c index 387b0304c..03cdec001 100644 --- a/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse.c +++ b/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse.c @@ -151,6 +151,25 @@ void add_child_dentry(struct dfs_dentry *parent, struct dfs_dentry *child) parent->child = child; } +static int remove_child_dentry(struct dfs_dentry *parent, struct dfs_dentry *child) +{ + struct dfs_dentry *prev_child = NULL; + struct dfs_dentry *cur_child = parent->child; + + while (cur_child != NULL && cur_child != child) + { + prev_child = cur_child; + cur_child = cur_child->brother; + } + if (cur_child == NULL) + return 0; + + if (prev_child == NULL) + parent->child = cur_child->brother; + else prev_child->brother = cur_child->brother; + return 1; +} + struct dfs_dentry *traverse_path(struct dfs_dentry *start_dentry, const char *path, int ftype, int create) { struct dfs_dentry *dentry = start_dentry; @@ -220,6 +239,51 @@ struct dfs_dentry *lookup_or_create_dentry(const char *path, struct dfs_dentry * /*功能函数*/ +static int di_unlink(const char *path) +{ + struct dfs_dentry *dentry = look_up(root, path); + + if (dentry == NULL) + return -ENOENT; + if (dentry->ftype != FILE_TYPE) + return -EISDIR; + + if (remove_child_dentry(dentry->parent, dentry)) + { + lru_remove(dentry); + unsigned int index = hash(dentry->fname); + hash_table[index] = NULL; + free(dentry->inode); + free(dentry); + return 0; + } + return -ENOENT; +} + +static int di_rmdir(const char *path) +{ + struct dfs_dentry *dentry = look_up(root, path); + + if (dentry == NULL) + return -ENOENT; + if (dentry->ftype != DIRECTORY_TYPE) + return -ENOTDIR; + if (dentry->child != NULL) + return -ENOTEMPTY; + + // 移除子目录项 + if (remove_child_dentry(dentry->parent, dentry)) + { + lru_remove(dentry); + unsigned int index = hash(dentry->fname); + hash_table[index] = NULL; + free(dentry->inode); + free(dentry); + return 0; + } + return -ENOENT; +} + static int di_utimens(const char *path, const struct timespec ts[2], struct fuse_file_info *fi) { (void)fi; @@ -369,6 +433,8 @@ static struct fuse_operations difs_ops = { .mkdir = di_mkdir, .create = dfs_create, .utimens = di_utimens, + .unlink = di_unlink, + .rmdir = di_rmdir, }; int main(int argc, char *argv[]) diff --git a/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/test/test.sh b/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/test/test.sh index 55967cbd7..cb237ae43 100755 --- a/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/test/test.sh +++ b/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/test/test.sh @@ -28,6 +28,9 @@ $FUSE_EXEC -f -d "$MOUNT_POINT" & FUSE_PID=$! sleep 2 # 等待文件系统完全挂载 +# 确保脚本退出时卸载文件系统 +trap "fusermount -u $MOUNT_POINT" EXIT + # 创建目录 mkdir $MOUNT_POINT/dir1 mkdir $MOUNT_POINT/dir2 @@ -41,5 +44,28 @@ touch $MOUNT_POINT/dir2/file3 echo "创建的目录和文件结构:" ls -l $MOUNT_POINT -# 确保脚本退出时卸载文件系统 -trap "fusermount -u $MOUNT_POINT" EXIT +# 删除文件 +echo "删除文件 $MOUNT_POINT/dir1/file1 和 $MOUNT_POINT/dir2/file3..." +rm $MOUNT_POINT/dir1/file1 +rm $MOUNT_POINT/dir2/file3 + +# 验证文件删除 +echo "验证文件删除后的结构:" +ls -l $MOUNT_POINT/dir1 +ls -l $MOUNT_POINT/dir2 + +# 尝试删除非空目录 +echo "尝试删除非空目录 $MOUNT_POINT/dir1 (应失败)..." +rmdir $MOUNT_POINT/dir1 || echo "无法删除非空目录 $MOUNT_POINT/dir1, 操作成功。" + +# 删除剩余文件 +rm $MOUNT_POINT/dir1/file2 + +# 删除空目录 +echo "删除空目录 $MOUNT_POINT/dir1 和 $MOUNT_POINT/dir2..." +rmdir $MOUNT_POINT/dir1 +rmdir $MOUNT_POINT/dir2 + +# 验证目录删除 +echo "验证目录删除后的结构:" +ls -l $MOUNT_POINT From 615fe2c59e84e6aaac12245bb83b9dfc3a2c29c5 Mon Sep 17 00:00:00 2001 From: qcloud Date: Fri, 26 Jul 2024 19:11:26 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Filesystem_Subsystem/fast_fuse/difuse/src/difuse.c | 9 --------- 1 file changed, 9 deletions(-) diff --git a/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse.c b/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse.c index eb1d245b1..03cdec001 100644 --- a/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse.c +++ b/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse.c @@ -151,7 +151,6 @@ void add_child_dentry(struct dfs_dentry *parent, struct dfs_dentry *child) parent->child = child; } -<<<<<<< HEAD static int remove_child_dentry(struct dfs_dentry *parent, struct dfs_dentry *child) { struct dfs_dentry *prev_child = NULL; @@ -171,8 +170,6 @@ static int remove_child_dentry(struct dfs_dentry *parent, struct dfs_dentry *chi return 1; } -======= ->>>>>>> e040be4f7b8f162b7a4c9b0c5f5bb40158e2b6fb struct dfs_dentry *traverse_path(struct dfs_dentry *start_dentry, const char *path, int ftype, int create) { struct dfs_dentry *dentry = start_dentry; @@ -242,7 +239,6 @@ struct dfs_dentry *lookup_or_create_dentry(const char *path, struct dfs_dentry * /*功能函数*/ -<<<<<<< HEAD static int di_unlink(const char *path) { struct dfs_dentry *dentry = look_up(root, path); @@ -288,8 +284,6 @@ static int di_rmdir(const char *path) return -ENOENT; } -======= ->>>>>>> e040be4f7b8f162b7a4c9b0c5f5bb40158e2b6fb static int di_utimens(const char *path, const struct timespec ts[2], struct fuse_file_info *fi) { (void)fi; @@ -439,11 +433,8 @@ static struct fuse_operations difs_ops = { .mkdir = di_mkdir, .create = dfs_create, .utimens = di_utimens, -<<<<<<< HEAD .unlink = di_unlink, .rmdir = di_rmdir, -======= ->>>>>>> e040be4f7b8f162b7a4c9b0c5f5bb40158e2b6fb }; int main(int argc, char *argv[]) From 491653c4f0aa6673779cc740e222de94a07093a2 Mon Sep 17 00:00:00 2001 From: qcloud Date: Thu, 5 Sep 2024 15:29:30 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E5=AE=8C=E5=96=84=E8=AF=BB=E5=86=99?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fast_fuse/difuse/src/difuse | Bin 17624 -> 0 bytes .../fast_fuse/difuse/src/difuse.c | 116 ++++++++++++++++-- 2 files changed, 108 insertions(+), 8 deletions(-) delete mode 100755 eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse diff --git a/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse b/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse deleted file mode 100755 index 8b255be73fa02d7c7da25c92ddd0ad14ec5f1558..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 17624 zcmeHPeQ;dWb-%mTV%e6l9I%lMjhAl-_FD4SI8cWb_S)-K24P!m2!!=oyS5fqTCw}q z*t8;I3=BJKO#}@T8j?{y7@%%FIU^|esbTCc~WBu!CYQp9bUZIB=f>Q-?EB&b%Z znfP6xKCey#U!^f44;ligNrT=}TFv+rP_nC|Oc{EPft66HAyKlclvYkN5}~Tr@MPzs zEc0&q(CJ1`sL1W|{6?-^G|80_>a?TSv@J4rZ5%(L)ol0W3)nB$i^4^AJK1h0+X+=1 zPoWY|a!1!nj;EW}LqN}JxYV*j1;n%V9gzjmYC^q7H_P$}

$mLb)hX*;HLaDzgElwu3u2{A>8C#T0bYyof+PQMYq7};mnN(njmQ%g_ zxM)nRySjzCqn|Jiqw;neUi^{lOqP>=`=jq%w{88F(;mL%f`J+T`lpc}z3V+dI#h46 zAstHOPr96V%F|FK9TSf`kTV$3qw!bTiB?JvLGZaU!ka7LRF1!l{+$){*H*yqtAKY^ z!2bxiAD25l0-#*|V-@gx1$-fJKQ4FrHh^;esW;AZD-$1Czj;F>7Ei~wCo)z%y?Mjx zWU3>+Il46&SCPo}_EbkCV@1Som*Z_mUnwJjZw6BNsK64VL>m{UPZX%sId zu9b2t)Y_I!p~%uey;|3_{;JiHC4q)zc6RARc1d74HQ0+W>A`OmE-!LEro_>>^r*d{ zG>-ka`p)}{>9jJvfIKA+te*ju`T31|AHXp7sh1ghO7m0HAoJPz39RT|^(J^q_WEg9 z739w9Tx9@R*Q67xgQd9eei!aN+vu%y;o@9MX_ z#UtU}FH}FTn^j!?6~N=g3$F*6wLp=68%f58|E*Xo_G+Fs_VMAPHcvMgX(T@+`YxA_hj}PBr^R%:q<*bVD*e92Fk5Tcm5ZM(O&2|8an7 zJHh{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zyf{IS={(l&#)85Z^Li>!F{VTT3^cR$^_&niKjZ7^l5Dm!(XTJRikI#*grCA#*w|QIq$Sat`0E zFTBwAU^~lUFYgaJ7Xd^PJ;P1#g8vQz8oN@yJP*qMRgl2a{G%jx(vLp?M(wxl`90>& z#iXs&J%-r7VlVVG7(4A%kX7pq?IfWi`5k%6)<90aqdata = (char *)malloc(CHUNK_SIZE); + new_data->next = NULL; + return new_data; +} + struct dfs_inode { uint32_t ino; //inode编号 @@ -401,16 +410,106 @@ static int di_read(const char *path, char *buf, size_t size, off_t offset, struc if (dentry->ftype != FILE_TYPE) return -EISDIR; - if (offset < dentry->inode->size) + struct dfs_inode *inode = dentry->inode; + size_t file_size = inode->size; + + if (offset >= file_size) + return 0; + + if (offset + size > file_size) + size = file_size - offset; + + // 初始化缓冲区 + memset(buf, 0, size); + + // 从数据块中读取数据 + size_t bytes_read = 0; + struct dfs_data *data_block = inode->data_pointer; + + // 遍历数据块 + while (data_block != NULL && bytes_read < size) { - if (offset + size > dentry->inode->size) - size = dentry->inode->size - offset; - memcpy(buf, "dummy_content", size); + // 计算当前块的有效数据长度 + size_t block_size = data_block->size; + if (offset >= block_size) + { + offset -= block_size; + } + else + { + // 从当前块读取数据 + size_t to_read = block_size - offset; + if (to_read > size - bytes_read) + to_read = size - bytes_read; + + // 复制数据到缓冲区 + memcpy(buf + bytes_read, ((char *)data_block) + offset, to_read); + bytes_read += to_read; + offset = 0; + } + + data_block = data_block->next; + } + + return bytes_read; +} + +static int di_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi) +{ + (void)fi; + struct dfs_dentry *dentry = look_up(root, path); + + if (dentry == NULL) return -ENOENT; + if (dentry->ftype != FILE_TYPE) return -EISDIR; + + struct dfs_inode *inode = dentry->inode; + struct dfs_data *data_block = inode->data_pointer; + + if (data_block == NULL) + { + data_block = allocate_data_block(); + inode->data_pointer = data_block; + } + + size_t bytes_written = 0; + size_t total_offset = offset; + + while (data_block != NULL && total_offset >= data_block->size) + { + total_offset -= data_block->size; + if (data_block->next == NULL) + { + data_block->next = allocate_data_block(); + } + data_block = data_block->next; + } + + while (bytes_written < size) + { + size_t space_in_block = CHUNK_SIZE - total_offset; + size_t to_write = size - bytes_written; + + if (to_write > space_in_block) to_write = space_in_block; + + memcpy(data_block->data + total_offset, buf + bytes_written, to_write); + + total_offset = 0; + bytes_written += to_write; + data_block->size += to_write; + + if (bytes_written < size && data_block->next == NULL) + { + data_block->next = allocate_data_block(); + } + data_block = data_block->next; + } + + if (offset + bytes_written > inode->size) + { + inode->size = offset + bytes_written; } - else - size = 0; - return size; + return bytes_written; } static void *di_init(struct fuse_conn_info *conn, struct fuse_config *cfg) @@ -430,6 +529,7 @@ static struct fuse_operations difs_ops = { .getattr = di_getattr, .open = di_open, .read = di_read, + .write = di_write, .mkdir = di_mkdir, .create = dfs_create, .utimens = di_utimens,