文件系统小册(Fuse&Posix&K8s csi)【2 Posix】
POSIX:可移植操作系统接口(标准)
1 概念
拓展:GNU、Linux、Unix关系
三者联系与区别:
参考文章:https://www.gnu.org/gnu/linux-and-gnu.html
2 POSIX的标准文件接口(常用部分)
open:打开文件
close:关闭文件
creat:创建文件
read:读文件内容
write:写内容到文件
lseek:改变文件指针的位置(文件定位)
fsync:将文件数据从系统缓存区写到磁盘
flock:文件锁,用于进程间同步
stat:获取文件的统计信息
truncate:截断文件
symlink:创建符号链接(如:软链,类比快捷方式)
readlink:读取符号链接的内容
opendir:打开目录
readdir:读取目录内容
mkdir:创建目录
rmdir: 删除目录
telldir:定位目录的当前位置
seekdir:定位目录
rename:重命名
chmod:动态修改文件访问权限
chown:动态修改文件所有者和组别
3 与FUSE的关系:FUSE实现了POSIX
fuse_operations 结构体定义了一系列的回调函数,这些函数对应于POSIX文件操作,例如:
- getattr: 实现stat操作。
- read: 实现read操作。
- write: 实现write操作。
- open: 实现open操作。
- create: 实现creat操作。
- unlink: 实现unlink操作。
- rename: 实现rename操作。
- mkdir: 实现mkdir操作。
- symlink: 实现symlink操作。
- readlink: 实现readlink操作。
- chmod: 实现chmod操作。
- chown: 实现chown操作。
- truncate: 实现truncate操作。
flush: 实现文件关闭前的清理操作。
…
源码:
- https://github.com/libfuse/libfuse/中的fuse.h文件
/** Get file attributes.
*
* Similar to stat(). The 'st_dev' and 'st_blksize' fields are
* ignored. The 'st_ino' field is ignored except if the 'use_ino'
* mount option is given. In that case it is passed to userspace,
* but libfuse and the kernel will still assign a different
* inode for internal use (called the "nodeid").
*
* `fi` will always be NULL if the file is not currently open, but
* may also be NULL if the file is open.
*/
int (*getattr) (const char *, struct stat *, struct fuse_file_info *fi);
/** Read the target of a symbolic link
*
* The buffer should be filled with a null terminated string. The
* buffer size argument includes the space for the terminating
* null character. If the linkname is too long to fit in the
* buffer, it should be truncated. The return value should be 0
* for success.
*/
int (*readlink) (const char *, char *, size_t);
/** Create a file node
*
* This is called for creation of all non-directory, non-symlink
* nodes. If the filesystem defines a create() method, then for
* regular files that will be called instead.
*/
int (*mknod) (const char *, mode_t, dev_t);
/** Create a directory
*
* Note that the mode argument may not have the type specification
* bits set, i.e. S_ISDIR(mode) can be false. To obtain the
* correct directory type bits use mode|S_IFDIR
* */
int (*mkdir) (const char *, mode_t);
/** Remove a file */
int (*unlink) (const char *);
/** Remove a directory */
int (*rmdir) (const char *);
/** Create a symbolic link */
int (*symlink) (const char *, const char *);
/** Rename a file
*
* *flags* may be `RENAME_EXCHANGE` or `RENAME_NOREPLACE`. If
* RENAME_NOREPLACE is specified, the filesystem must not
* overwrite *newname* if it exists and return an error
* instead. If `RENAME_EXCHANGE` is specified, the filesystem
* must atomically exchange the two files, i.e. both must
* exist and neither may be deleted.
*/
int (*rename) (const char *, const char *, unsigned int flags);
/** Create a hard link to a file */
int (*link) (const char *, const char *);
/** Change the permission bits of a file
*
* `fi` will always be NULL if the file is not currently open, but
* may also be NULL if the file is open.
*/
int (*chmod) (const char *, mode_t, struct fuse_file_info *fi);
/** Change the owner and group of a file
*
* `fi` will always be NULL if the file is not currently open, but
* may also be NULL if the file is open.
*
* Unless FUSE_CAP_HANDLE_KILLPRIV is disabled, this method is
* expected to reset the setuid and setgid bits.
*/
int (*chown) (const char *, uid_t, gid_t, struct fuse_file_info *fi);
/** Change the size of a file
*
* `fi` will always be NULL if the file is not currently open, but
* may also be NULL if the file is open.
*
* Unless FUSE_CAP_HANDLE_KILLPRIV is disabled, this method is
* expected to reset the setuid and setgid bits.
*/
int (*truncate) (const char *, off_t, struct fuse_file_info *fi);
...
4 测试系统是否满足POSIX语义 (pjdfstest)
#git clone源码
git clone https://github.com/pjd/pjdfstest.git
cd pjdfstest
# 编译源码
autoreconf -ifs
./configure
make pjdfstest
# 查看版本
prove --version
## 进入目标目录,执行pjdfstest脚本
cd /root/test (文件系统的挂载目录)
# 测试所有用例(-v显示进度)
prove -rv /root/pjdfstest/tests(pjdfstest里的tests目录)
# 测试单个语义
prove -r /root/pjdfstest/tests/open
参考:https://www.gnu.org/software/libc/manual/html_node/POSIX.html