覚えたら書く

IT関係のデベロッパとして日々覚えたことを書き残したいです。twitter: @yyoshikaw

FUSE

FUSEとは

  • Filesystem in Userspace (FUSE) は、Unix系オペレーティングシステムのローダブル・カーネル・モジュールの一種で、一般ユーザーがカーネルコードを修正することなく独自のファイルシステムを作成できる機能を提供します。
  • 詳細は、Filesystem in Userspaceを参照。
  • FUSEのイメージは以下の通り(参照:Wikipedia)
    http://upload.wikimedia.org/wikipedia/commons/0/08/FUSE_structure.svg

FUSE-API

FUSEを使って構築するファイルシステムの各種ファイル操作は、FUSEのライブラリで提供される(fuse.hで定義される)以下の関数(FUSE-API)をオーバーライドすることで実現する。

No. 関数名 概要 関連付くシステムコール 備考
1 getattr Get file attributes. stat  
2 readlink Read the target of a symbolic link readlink  
3 getdir Deprecated, use readdir() instead   Deprecated
4 mknod Create a file node
If the filesystem defines a create() method, then for regular files that will be called instead.
mknod  
5 mkdir Create a directory mkdir  
6 unlink Remove a file unlink  
7 rmdir Remove a directory rmdir  
8 symlink Create a symbolic link symlink  
9 rename Rename a file rename  
10 link Create a hard link to a file link  
11 chmod Change the permission bits of a file chmod  
12 chown Change the owner and group of a file chown  
13 truncate Change the size of a file truncate  
14 utime Change the access and/or modification times of a file(Deprecated, use utimens() instead.) utime Deprecated
15 open File open operation
No creation (O_CREAT, O_EXCL) and by default also no truncation (O_TRUNC) flags will be passed to open(). If an application specifies O_TRUNC, fuse first calls truncate() and then open(). Only if 'atomic_o_trunc' has been specified and kernel version is 2.6.24 or later, O_TRUNC is passed on to open.
open  
16 read Read data from an open file read  
17 write Write data to an open file write  
18 statfs Get file system statistics statfs  
19 flush Possibly flush cached data(※This is not equivalent to fsync(). It's not a request to sync dirty data.)    
20 release Release an open file close  
21 fsync Synchronize file contents fsync, fdatasync  
22 setxattr Set extended attributes setxattr  
23 getxattr Get extended attributes getxattr  
24 listxattr List extended attributes listxattr  
25 removexattr Remove extended attributes removexattr  
26 opendir Open directory    
27 readdir Read directory(This supersedes the old getdir() interface. New applications should use this.) readdir  
28 releasedir Release directory    
29 fsyncdir Synchronize directory contents    
30 init Initialize filesystem    
31 destroy Clean up filesystem(Called on filesystem exit.)    
32 access Check file access permissions(This method is not called under Linux kernel versions 2.4.x)
If the 'default_permissions' mount option is given, this method is not called.
access  
33 create Create and open a file
(If this method is not implemented or under Linux kernel versions earlier than 2.6.15,
the mknod() and open() methods will be called instead.)
creat  
34 ftruncate Change the size of an open file
If this method is not implemented or under Linux kernel versions earlier than 2.6.15, the truncate() method will be called instead.
ftruncate  
35 fgetattr Get attributes from an open file    
36 lock Perform POSIX file locking operation fcntl  
37 utimens Change the access and modification times of a file with nanosecond resolution utimensat  
38 bmap Map block index within file to block index within device -  
39 ioctl Ioctl ioctl  
40 poll Poll for IO readiness events poll  
41 write_buf Write contents of buffer to an open file    
42 read_buf Store data from an open file in a buffer    
43 flock Perform BSD file locking operation flock  
44 fallocate Allocates space for an open file fallocate  

(構築するFileSystem次第で実装すべきAPIは異なる(実装しないAPIが存在しても良い))
FUSE-APIについての詳細なリファレンスは、fuse_operationsを参照

FUSEのオプション


FUSEの設定

FUSEのインストール

  • CentOSへのfuseのインストールは以下を実行する
    yum install fuse
    
    yum install fuse-libs.x86_64
    

root以外のユーザでのmount実行

  • fuseでのmountを行う際に実行される /bin/fusermount は権限等がデフォルトで以下のようになっている
    -rwsr-x---. 1 root fuse 32336 Dec  7  2011 /bin/fusermount
    
    そのため、root以外のユーザでfuseのmountをする際は対象ユーザをfuseグループにも所属させる必要がある
  • /etc/fuse.conf に以下の設定を記述する
    user_allow_other
    
  • fuseのmount時に以下のオプションを指定する
    -o allow_other

その他

  • closeは非同期で呼び出される
    • fuse-apiのclose関数は非同期で呼び出されます。close関数内で実装した処理の完了を待たずに次のファイル操作が呼び出されます。
      この動きを把握しておかないと実装したファイル操作の整合性が取れなくなる場合があります
  • getattrはできるだけ高速に応答
    • ファイル属性を取得するこの関数は、最も頻繁に呼び出され、基本的には高速に応答する必要があります。ファイルシステムの特性にもよりますが、1ミリ秒未満(できれば100マイクロ秒未満)で応答するのが望ましいと思われます
  • unlinkシステムコールについて(参考)
    • unlinkシステムコールは、ファイルシステム上の名前を削除する。どのプロセスもそのファイルをopenしていなければ、ファイルは削除される。
      どれかのプロセスがそのファイルをまだオープンしている場合、そのファイルを参照している最後のファイルディスクリプタがcloseされるまでファイルは存在し続ける。
      closeシステムコール実行時にそのディスクリプタが、unlinkを使用して削除されたファイルに対する最後の参照だった場合には、そのファイルは削除される)
    • FUSE-APIのunlink関数については"Remove a file"という説明しかないため、unlinkに対してシステムコールがやっているような実装をすべきかどうかは不明(実装するファイルシステム任せという感じ?)

 


 

関連エントリ