Introduction to FUSE File system in USEr space














- Slides: 14
Introduction to FUSE (File system in USEr space) Speaker: Zong-shuo Jheng Date: March 14, 2008
Agenda • • Brief to VFS (Virtual File System) FUSE (File system in USEr space) “Hello world file system” programming tutorial Demo: a simple fuse file system example
Brief to VFS (Virtual File System) (1/3) • Why VFS is needed #cp /mnt/ext 2/data /mnt/FAT 32/data User AP 2 Kernel 1 FAT 32 User space Kernel space mount as /mnt/FAT 32 Ext 2/3 mount as /mnt/ext 2 1. Call ext 2_read( ) to get data from /mnt/ext 2 2. Call FAT 32_write( ) to write data to /mnt/FAT 32 User AP should know about every IO functions of each file systems to feed the need completing every IO operations. A common interface for user AP to call IO functions is necessary.
Brief to VFS (Virtual File System) (2/3) • How VFS works #cp /mnt/ext 2/data /mnt/FAT 32/data User AP Kernel 2 VFS FAT 32 mount as /mnt/FAT 32 1 Ext 2/3 mount as /mnt/ext 2 Providing a common interface for every IO functions. User space Kernel space register_filesystem( ) 1. Call read( ) to get data from /mnt/ext 2 2. Call write( ) to write data to /mnt/FAT 32
Brief to VFS (Virtual File System) (3/3) • Issues developers shall address • Hard work for kernel-programming (as drivers) User AP Kernel FAT 32 mount as /mnt/FAT 32 VFS Ext 2/3 mount as /mnt/ext 2 Special-purpose File System User space FAT 32_read( ) Kernel space FAT 32_write( ) FAT 32_open( ) Other necessary IO functions ext 2_read( ) ext 2_write( ) ext 2_open( ) Other necessary IO functions New. FS_read( ) New. FS_write( ) New. FS_open( ) Other necessary IO functions
FUSE (File system in USEr space) (1/4) • The role FUSE plays • Programming a file system in user-space User AP Kernel VFS User-level program User space Kernel space FAT 32 Ext 2/3 FUSE register_filesystem( ) FUSE_read( ) FUSE_write( ) FUSE_open( ) Left for developers to complete the IO functions Other necessary IO functions Been implemented by FUSE
FUSE (File system in USEr space) (2/4) • How user-level file system works #cat /mnt/user_level_fs/data User-level program 4 User AP 1 Kernel VFS 2 FAT 32 Ext 2/3 5 3 FUSE mount as /mnt/user_level_fs User space Kernel space 1. Call open( ) & read( ) 2. Bypass the IO call to FUSE module 3. Diver the IO call to upper level 4. User-level program open( ) & read( ) are invoked 5. Return the result to user AP
FUSE (File system in USEr space) (3/4) • FUSE-based file system: FTPFS • Scenario: list the files on the remote FTP server User command: ls /mnt/ftpfs Connection establish User AP Kernel VFS FTP command: LS user-level Return requested data program User space Kernel space Bypass to user-level program FUSE register_filesystem( ) mount as /mnt/ftpfs Remote FTP server
FUSE (File system in USEr space) (4/4) • How to install FUSE package # tar zxvf fuse-2. 7. 3. tar. gz # cd fuse-2. 7. 3 #. /configure # make install • How to build and execute FUSE program # gcc fuse_example. c –o fuse_example -D_FILE_OFFSET_BITS=64 –lfuse #. /fuse_example /mnt/mount_point
“Hello world file system” programming tutorial (1/) • Main function and function pointers static struct fuse_operations hello_oper = {. getattr = hello_getattr, . readdir = hello_readdir, Been implemented by developer. open = hello_open, . read = hello_read, }; int main(int argc, char *argv[]) { return fuse_main(argc, argv, &hello_oper, NULL); } IO operations left for developers to implement
“Hello world file system” programming tutorial (2/) static const char *hello_path = "/hello"; static int hello_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi) { Relative path Buffer storing items in directory (void) offset; (void) fi; if (strcmp(path, "/") != 0) return -ENOENT; filler(buf, ". ", NULL, 0); filler(buf, ". . ", NULL, 0); filler(buf, hello_path + 1, NULL, 0); return 0; } # cd /mnt/mount_point # ls getattr( ), access( ), and readdir( ) are called. hello. . . #
“Hello world file system” programming tutorial (3/) static const char *hello_path = "/hello"; static const char *hello_str = "Hello World!n"; static int hello_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi){ size_t len; Relative path Buffer storing read-out data (void) fi; if(strcmp(path, hello_path) != 0) return -ENOENT; len = strlen(hello_str); # cd /mnt/mount_point if (offset < len) { # cat hello getattr( ), access( ), if (offset + size > len) and read( ) are called. size = len - offset; Hello World! memcpy(buf, hello_str + offset, size); # } else size = 0; return size; }
Demo: a simple fuse file system example
File system operations interface FUSE specified Common operations: init destroy statfs mknod create open read write access lock truncate ftruncate bmap release fsync unlink flush mkdir opendir readdir releasedir rmdir fsyncdir utimens rename link symlink readlink getattr fgetattr setxattr getxattr listxattr removexattr chmod chown Low-level operations: init destroy statfs lookup mknod create open read write access bmap getlk setlk release fsync unlink flush mkdir opendir readdir releasedir rmdir fsyncdir chmod chown link symlink readlink getattr setxattr getxattr listxattr removexattr forget