Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | FUSE: Filesystem in Userspace |
Miklos Szeredi | 2e6b6f2 | 2004-07-07 19:19:53 +0000 | [diff] [blame] | 3 | Copyright (C) 2001-2004 Miklos Szeredi <miklos@szeredi.hu> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 4 | |
Miklos Szeredi | 8b39a9f | 2002-10-25 12:41:16 +0000 | [diff] [blame] | 5 | This program can be distributed under the terms of the GNU LGPL. |
| 6 | See the file COPYING.LIB. |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Miklos Szeredi | acd4e06 | 2001-12-08 20:29:20 +0000 | [diff] [blame] | 9 | #ifndef _FUSE_H_ |
| 10 | #define _FUSE_H_ |
| 11 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 12 | /* This file defines the library interface of FUSE */ |
| 13 | |
Miklos Szeredi | 2f3d940 | 2003-12-15 12:11:33 +0000 | [diff] [blame] | 14 | /** Major version of FUSE library interface */ |
Miklos Szeredi | 18e75e4 | 2004-02-19 14:23:27 +0000 | [diff] [blame] | 15 | #define FUSE_MAJOR_VERSION 2 |
Miklos Szeredi | 2f3d940 | 2003-12-15 12:11:33 +0000 | [diff] [blame] | 16 | |
| 17 | /** Minor version of FUSE library interface */ |
Miklos Szeredi | 203afbf | 2004-06-03 13:21:08 +0000 | [diff] [blame] | 18 | #define FUSE_MINOR_VERSION 0 |
Miklos Szeredi | 2f3d940 | 2003-12-15 12:11:33 +0000 | [diff] [blame] | 19 | |
Miklos Szeredi | 87f30a9 | 2004-04-13 10:49:54 +0000 | [diff] [blame] | 20 | /* This interface uses 64 bit off_t */ |
| 21 | #if _FILE_OFFSET_BITS != 64 |
| 22 | #error Please add -D_FILE_OFFSET_BITS=64 to your compile flags! |
| 23 | #endif |
Miklos Szeredi | 5f05481 | 2002-12-03 18:45:21 +0000 | [diff] [blame] | 24 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 25 | #include <sys/types.h> |
| 26 | #include <sys/stat.h> |
Miklos Szeredi | 18e75e4 | 2004-02-19 14:23:27 +0000 | [diff] [blame] | 27 | #include <sys/statfs.h> |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 28 | #include <utime.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 29 | |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 30 | /* ----------------------------------------------------------- * |
| 31 | * Basic FUSE API * |
| 32 | * ----------------------------------------------------------- */ |
| 33 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 34 | /** Handle for a FUSE filesystem */ |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 35 | struct fuse; |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 36 | |
| 37 | /** Handle for a getdir() operation */ |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 38 | typedef struct fuse_dirhandle *fuse_dirh_t; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 39 | |
Miklos Szeredi | f08ace0 | 2003-10-22 11:11:57 +0000 | [diff] [blame] | 40 | /** Function to add an entry in a getdir() operation |
| 41 | * |
| 42 | * @param h the handle passed to the getdir() operation |
| 43 | * @param name the file name of the directory entry |
| 44 | * @param type the file type (0 if unknown) see <dirent.h> |
| 45 | * @return 0 on success, -errno on error |
| 46 | */ |
| 47 | typedef int (*fuse_dirfil_t) (fuse_dirh_t h, const char *name, int type); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 48 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 49 | /** |
| 50 | * The file system operations: |
| 51 | * |
| 52 | * Most of these should work very similarly to the well known UNIX |
| 53 | * file system operations. Exceptions are: |
| 54 | * |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 55 | * - All operations should return the negated error value (-errno) on |
| 56 | * error. |
| 57 | * |
Miklos Szeredi | 0503304 | 2001-11-13 16:11:35 +0000 | [diff] [blame] | 58 | * - Getattr() doesn't need to fill in the following fields: |
| 59 | * st_ino |
| 60 | * st_dev |
| 61 | * st_blksize |
| 62 | * |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 63 | * - readlink() should fill the buffer with a null terminated string. The |
| 64 | * buffer size argument includes the space for the terminating null |
| 65 | * character. If the linkname is too long to fit in the buffer, it should |
| 66 | * be truncated. The return value should be 0 for success. |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 67 | * |
| 68 | * - getdir() is the opendir(), readdir(), ..., closedir() sequence |
| 69 | * in one call. For each directory entry the filldir parameter should |
| 70 | * be called. |
| 71 | * |
| 72 | * - There is no create() operation, mknod() will be called for |
| 73 | * creation of all non directory, non symlink nodes. |
| 74 | * |
| 75 | * - open() should not return a filehandle, but 0 on success. No |
| 76 | * creation, or trunctation flags (O_CREAT, O_EXCL, O_TRUNC) will be |
| 77 | * passed to open(). Open should only check if the operation is |
| 78 | * permitted for the given flags. |
| 79 | * |
| 80 | * - read(), write() are not passed a filehandle, but rather a |
| 81 | * pathname. The offset of the read and write is passed as the last |
Miklos Szeredi | 307242f | 2004-01-26 11:28:44 +0000 | [diff] [blame] | 82 | * argument, like the pread() and pwrite() system calls. (NOTE: |
| 83 | * read() should always return the number of bytes requested, except |
| 84 | * at end of file) |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 85 | * |
| 86 | * - release() is called when an open file has: |
| 87 | * 1) all file descriptors closed |
| 88 | * 2) all memory mappings unmapped |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 89 | * For every open() call there will be exactly one release() call |
| 90 | * with the same flags. It is possible to have a file opened more |
| 91 | * than once, in which case only the last release will mean, that no |
| 92 | * more reads/writes will happen on the file. The return value of |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 93 | * release is ignored. Implementing this method is optional. |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 94 | * |
| 95 | * - flush() is called when close() has been called on an open file. |
| 96 | * NOTE: this does not mean that the file is released (e.g. after |
| 97 | * fork() an open file will have two references which both must be |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 98 | * closed before the file is released). The flush() method may be |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 99 | * called more than once for each open(). The return value of |
| 100 | * flush() is passed on to the close() system call. Implementing |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 101 | * this method is optional. |
Miklos Szeredi | 5e43f2c | 2003-12-12 14:06:41 +0000 | [diff] [blame] | 102 | * |
| 103 | * - fsync() has a boolean 'datasync' parameter which if TRUE then do |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 104 | * an fdatasync() operation. Implementing this method is optional. |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 105 | */ |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 106 | struct fuse_operations { |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 107 | int (*getattr) (const char *, struct stat *); |
| 108 | int (*readlink) (const char *, char *, size_t); |
| 109 | int (*getdir) (const char *, fuse_dirh_t, fuse_dirfil_t); |
| 110 | int (*mknod) (const char *, mode_t, dev_t); |
| 111 | int (*mkdir) (const char *, mode_t); |
| 112 | int (*unlink) (const char *); |
| 113 | int (*rmdir) (const char *); |
| 114 | int (*symlink) (const char *, const char *); |
| 115 | int (*rename) (const char *, const char *); |
| 116 | int (*link) (const char *, const char *); |
| 117 | int (*chmod) (const char *, mode_t); |
| 118 | int (*chown) (const char *, uid_t, gid_t); |
| 119 | int (*truncate) (const char *, off_t); |
| 120 | int (*utime) (const char *, struct utimbuf *); |
| 121 | int (*open) (const char *, int); |
| 122 | int (*read) (const char *, char *, size_t, off_t); |
| 123 | int (*write) (const char *, const char *, size_t, off_t); |
| 124 | int (*statfs) (const char *, struct statfs *); |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 125 | int (*flush) (const char *); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 126 | int (*release) (const char *, int); |
| 127 | int (*fsync) (const char *, int); |
| 128 | int (*setxattr) (const char *, const char *, const char *, size_t, int); |
| 129 | int (*getxattr) (const char *, const char *, char *, size_t); |
| 130 | int (*listxattr) (const char *, char *, size_t); |
| 131 | int (*removexattr) (const char *, const char *); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 132 | }; |
| 133 | |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 134 | /** Extra context that may be needed by some filesystems */ |
| 135 | struct fuse_context { |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame^] | 136 | struct fuse *fuse; |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 137 | uid_t uid; |
| 138 | gid_t gid; |
| 139 | }; |
| 140 | |
Miklos Szeredi | acd4e06 | 2001-12-08 20:29:20 +0000 | [diff] [blame] | 141 | #ifdef __cplusplus |
| 142 | extern "C" { |
| 143 | #endif |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 144 | |
Miklos Szeredi | a4b0c77 | 2002-04-19 15:57:02 +0000 | [diff] [blame] | 145 | /* |
Miklos Szeredi | 0e53508 | 2003-10-13 10:08:06 +0000 | [diff] [blame] | 146 | * Main function of FUSE. |
| 147 | * |
| 148 | * This is for the lazy. This is all that has to be called from the |
| 149 | * main() function. |
| 150 | * |
| 151 | * This function does the following: |
Miklos Szeredi | 307242f | 2004-01-26 11:28:44 +0000 | [diff] [blame] | 152 | * - parses command line options (-d -s and -h) |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 153 | * - passes relevant mount options to the fuse_mount() |
Miklos Szeredi | 0e53508 | 2003-10-13 10:08:06 +0000 | [diff] [blame] | 154 | * - installs signal handlers for INT, HUP, TERM and PIPE |
| 155 | * - registers an exit handler to unmount the filesystem on program exit |
Miklos Szeredi | 0e53508 | 2003-10-13 10:08:06 +0000 | [diff] [blame] | 156 | * - creates a fuse handle |
| 157 | * - registers the operations |
| 158 | * - calls either the single-threaded or the multi-threaded event loop |
| 159 | * |
| 160 | * @param argc the argument counter passed to the main() function |
| 161 | * @param argv the argument vector passed to the main() function |
| 162 | * @param op the file system operation |
| 163 | */ |
| 164 | void fuse_main(int argc, char *argv[], const struct fuse_operations *op); |
| 165 | |
Miklos Szeredi | 891b874 | 2004-07-29 09:27:49 +0000 | [diff] [blame] | 166 | /** |
| 167 | * Invalidate cached data of a file. |
| 168 | * |
| 169 | * Useful if the 'kernel_cache' mount option is given, since in that |
| 170 | * case the cache is not invalidated on file open. |
| 171 | * |
| 172 | * @return 0 on success or -errno on failure |
| 173 | */ |
| 174 | int fuse_invalidate(struct fuse *f, const char *path); |
| 175 | |
Miklos Szeredi | 0e53508 | 2003-10-13 10:08:06 +0000 | [diff] [blame] | 176 | /* ----------------------------------------------------------- * |
| 177 | * More detailed API * |
| 178 | * ----------------------------------------------------------- */ |
| 179 | |
| 180 | /* |
Miklos Szeredi | a4b0c77 | 2002-04-19 15:57:02 +0000 | [diff] [blame] | 181 | * Create a FUSE mountpoint |
| 182 | * |
| 183 | * Returns a control file descriptor suitable for passing to |
| 184 | * fuse_new() |
| 185 | * |
| 186 | * @param mountpoint the mount point path |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 187 | * @param opts a comma separated list of mount options. Can be NULL. |
Miklos Szeredi | a4b0c77 | 2002-04-19 15:57:02 +0000 | [diff] [blame] | 188 | * @return the control file descriptor on success, -1 on failure |
| 189 | */ |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 190 | int fuse_mount(const char *mountpoint, const char *opts); |
Miklos Szeredi | a4b0c77 | 2002-04-19 15:57:02 +0000 | [diff] [blame] | 191 | |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 192 | /* |
| 193 | * Umount a FUSE mountpoint |
| 194 | * |
| 195 | * @param mountpoint the mount point path |
| 196 | */ |
| 197 | void fuse_unmount(const char *mountpoint); |
| 198 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 199 | /** |
Miklos Szeredi | 8cffdb9 | 2001-11-09 14:49:18 +0000 | [diff] [blame] | 200 | * Create a new FUSE filesystem. |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 201 | * |
Miklos Szeredi | 8cffdb9 | 2001-11-09 14:49:18 +0000 | [diff] [blame] | 202 | * @param fd the control file descriptor |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 203 | * @param opts mount options to be used by the library |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 204 | * @param op the operations |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 205 | * @return the created FUSE handle |
| 206 | */ |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 207 | struct fuse *fuse_new(int fd, const char *opts, |
| 208 | const struct fuse_operations *op); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 209 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 210 | /** |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 211 | * Destroy the FUSE handle. |
| 212 | * |
| 213 | * The filesystem is not unmounted. |
| 214 | * |
| 215 | * @param f the FUSE handle |
| 216 | */ |
| 217 | void fuse_destroy(struct fuse *f); |
| 218 | |
| 219 | /** |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 220 | * FUSE event loop. |
| 221 | * |
| 222 | * Requests from the kernel are processed, and the apropriate |
| 223 | * operations are called. |
| 224 | * |
| 225 | * @param f the FUSE handle |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 226 | * @return 0 if no error occured, -1 otherwise |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 227 | */ |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 228 | int fuse_loop(struct fuse *f); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 229 | |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 230 | /** |
| 231 | * Exit from event loop |
| 232 | * |
| 233 | * @param f the FUSE handle |
| 234 | */ |
| 235 | void fuse_exit(struct fuse *f); |
| 236 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 237 | /** |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 238 | * FUSE event loop with multiple threads |
| 239 | * |
| 240 | * Requests from the kernel are processed, and the apropriate |
| 241 | * operations are called. Request are processed in parallel by |
| 242 | * distributing them between multiple threads. |
| 243 | * |
| 244 | * Calling this function requires the pthreads library to be linked to |
| 245 | * the application. |
| 246 | * |
| 247 | * @param f the FUSE handle |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 248 | * @return 0 if no error occured, -1 otherwise |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 249 | */ |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 250 | int fuse_loop_mt(struct fuse *f); |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 251 | |
| 252 | /** |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 253 | * Get the current context |
| 254 | * |
| 255 | * The context is only valid for the duration of a filesystem |
| 256 | * operation, and thus must not be stored and used later. |
| 257 | * |
| 258 | * @param f the FUSE handle |
| 259 | * @return the context |
| 260 | */ |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame^] | 261 | struct fuse_context *fuse_get_context(void); |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 262 | |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 263 | /** |
| 264 | * Check whether a mount option should be passed to the kernel or the |
| 265 | * library |
| 266 | * |
| 267 | * @param opt the option to check |
| 268 | * @return 1 if it is a library option, 0 otherwise |
| 269 | */ |
| 270 | int fuse_is_lib_option(const char *opt); |
| 271 | |
| 272 | |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 273 | /* ----------------------------------------------------------- * |
Miklos Szeredi | 680a69a | 2001-11-16 13:31:14 +0000 | [diff] [blame] | 274 | * Advanced API for event handling, don't worry about this... * |
| 275 | * ----------------------------------------------------------- */ |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 276 | |
| 277 | struct fuse_cmd; |
Miklos Szeredi | f830a7f | 2001-11-16 17:46:45 +0000 | [diff] [blame] | 278 | typedef void (*fuse_processor_t)(struct fuse *, struct fuse_cmd *, void *); |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 279 | struct fuse_cmd *__fuse_read_cmd(struct fuse *f); |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 280 | void __fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 281 | int __fuse_loop_mt(struct fuse *f, fuse_processor_t proc, void *data); |
Miklos Szeredi | 65cf7c7 | 2004-06-30 11:34:56 +0000 | [diff] [blame] | 282 | int __fuse_exited(struct fuse* f); |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame^] | 283 | void __fuse_set_getcontext_func(struct fuse_context *(*func)(void)); |
Miklos Szeredi | f830a7f | 2001-11-16 17:46:45 +0000 | [diff] [blame] | 284 | |
Miklos Szeredi | acd4e06 | 2001-12-08 20:29:20 +0000 | [diff] [blame] | 285 | #ifdef __cplusplus |
| 286 | } |
| 287 | #endif |
| 288 | |
| 289 | #endif /* _FUSE_H_ */ |