Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | FUSE: Filesystem in Userspace |
| 3 | Copyright (C) 2001 Miklos Szeredi (mszeredi@inf.bme.hu) |
| 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 */ |
| 15 | #define FUSE_MAJOR_VERSION 1 |
| 16 | |
| 17 | /** Minor version of FUSE library interface */ |
| 18 | #define FUSE_MINOR_VERSION 1 |
| 19 | |
Miklos Szeredi | 5f05481 | 2002-12-03 18:45:21 +0000 | [diff] [blame] | 20 | /* Now and forever: this interface uses 64 bit off_t */ |
| 21 | #define _FILE_OFFSET_BITS 64 |
| 22 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 23 | #include <sys/types.h> |
| 24 | #include <sys/stat.h> |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 25 | #include <utime.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 26 | |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 27 | /* ----------------------------------------------------------- * |
| 28 | * Basic FUSE API * |
| 29 | * ----------------------------------------------------------- */ |
| 30 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 31 | /** Handle for a FUSE filesystem */ |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 32 | struct fuse; |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 33 | |
Miklos Szeredi | 24ed945 | 2002-10-07 10:24:26 +0000 | [diff] [blame] | 34 | /* Statfs structure used by FUSE */ |
| 35 | struct fuse_statfs { |
| 36 | long block_size; |
| 37 | long blocks; |
| 38 | long blocks_free; |
| 39 | long files; |
| 40 | long files_free; |
| 41 | long namelen; |
| 42 | }; |
| 43 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 44 | /** Handle for a getdir() operation */ |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 45 | typedef struct fuse_dirhandle *fuse_dirh_t; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 46 | |
Miklos Szeredi | f08ace0 | 2003-10-22 11:11:57 +0000 | [diff] [blame] | 47 | /** Function to add an entry in a getdir() operation |
| 48 | * |
| 49 | * @param h the handle passed to the getdir() operation |
| 50 | * @param name the file name of the directory entry |
| 51 | * @param type the file type (0 if unknown) see <dirent.h> |
| 52 | * @return 0 on success, -errno on error |
| 53 | */ |
| 54 | 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] | 55 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 56 | /** |
| 57 | * The file system operations: |
| 58 | * |
| 59 | * Most of these should work very similarly to the well known UNIX |
| 60 | * file system operations. Exceptions are: |
| 61 | * |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 62 | * - All operations should return the negated error value (-errno) on |
| 63 | * error. |
| 64 | * |
Miklos Szeredi | 0503304 | 2001-11-13 16:11:35 +0000 | [diff] [blame] | 65 | * - Getattr() doesn't need to fill in the following fields: |
| 66 | * st_ino |
| 67 | * st_dev |
| 68 | * st_blksize |
| 69 | * |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 70 | * - readlink() should fill the buffer with a null terminated string. The |
| 71 | * buffer size argument includes the space for the terminating null |
| 72 | * character. If the linkname is too long to fit in the buffer, it should |
| 73 | * be truncated. The return value should be 0 for success. |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 74 | * |
| 75 | * - getdir() is the opendir(), readdir(), ..., closedir() sequence |
| 76 | * in one call. For each directory entry the filldir parameter should |
| 77 | * be called. |
| 78 | * |
| 79 | * - There is no create() operation, mknod() will be called for |
| 80 | * creation of all non directory, non symlink nodes. |
| 81 | * |
| 82 | * - open() should not return a filehandle, but 0 on success. No |
| 83 | * creation, or trunctation flags (O_CREAT, O_EXCL, O_TRUNC) will be |
| 84 | * passed to open(). Open should only check if the operation is |
| 85 | * permitted for the given flags. |
| 86 | * |
| 87 | * - read(), write() are not passed a filehandle, but rather a |
| 88 | * 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^] | 89 | * argument, like the pread() and pwrite() system calls. (NOTE: |
| 90 | * read() should always return the number of bytes requested, except |
| 91 | * at end of file) |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 92 | * |
| 93 | * - release() is called when an open file has: |
| 94 | * 1) all file descriptors closed |
| 95 | * 2) all memory mappings unmapped |
| 96 | * This call need only be implemented if this information is required, |
| 97 | * otherwise set this function to NULL. |
Miklos Szeredi | 5e43f2c | 2003-12-12 14:06:41 +0000 | [diff] [blame] | 98 | * |
| 99 | * - fsync() has a boolean 'datasync' parameter which if TRUE then do |
| 100 | * an fdatasync() operation. |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 101 | */ |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 102 | struct fuse_operations { |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 103 | int (*getattr) (const char *, struct stat *); |
| 104 | int (*readlink) (const char *, char *, size_t); |
| 105 | int (*getdir) (const char *, fuse_dirh_t, fuse_dirfil_t); |
| 106 | int (*mknod) (const char *, mode_t, dev_t); |
| 107 | int (*mkdir) (const char *, mode_t); |
| 108 | int (*unlink) (const char *); |
| 109 | int (*rmdir) (const char *); |
| 110 | int (*symlink) (const char *, const char *); |
| 111 | int (*rename) (const char *, const char *); |
| 112 | int (*link) (const char *, const char *); |
| 113 | int (*chmod) (const char *, mode_t); |
| 114 | int (*chown) (const char *, uid_t, gid_t); |
| 115 | int (*truncate) (const char *, off_t); |
| 116 | int (*utime) (const char *, struct utimbuf *); |
| 117 | int (*open) (const char *, int); |
| 118 | int (*read) (const char *, char *, size_t, off_t); |
| 119 | int (*write) (const char *, const char *, size_t, off_t); |
Mark Glines | 85801bb | 2002-03-17 06:58:33 +0000 | [diff] [blame] | 120 | int (*statfs) (struct fuse_statfs *); |
Miklos Szeredi | 9478e86 | 2002-12-11 09:50:26 +0000 | [diff] [blame] | 121 | int (*release) (const char *, int); |
Miklos Szeredi | 5e43f2c | 2003-12-12 14:06:41 +0000 | [diff] [blame] | 122 | int (*fsync) (const char *, int); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 125 | /** Extra context that may be needed by some filesystems */ |
| 126 | struct fuse_context { |
| 127 | uid_t uid; |
| 128 | gid_t gid; |
| 129 | }; |
| 130 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 131 | /* FUSE flags: */ |
Miklos Szeredi | c0938ea | 2001-11-07 12:35:06 +0000 | [diff] [blame] | 132 | |
Miklos Szeredi | c0938ea | 2001-11-07 12:35:06 +0000 | [diff] [blame] | 133 | /** Enable debuging output */ |
| 134 | #define FUSE_DEBUG (1 << 1) |
| 135 | |
Miklos Szeredi | acd4e06 | 2001-12-08 20:29:20 +0000 | [diff] [blame] | 136 | #ifdef __cplusplus |
| 137 | extern "C" { |
| 138 | #endif |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 139 | |
Miklos Szeredi | a4b0c77 | 2002-04-19 15:57:02 +0000 | [diff] [blame] | 140 | /* |
Miklos Szeredi | 0e53508 | 2003-10-13 10:08:06 +0000 | [diff] [blame] | 141 | * Main function of FUSE. |
| 142 | * |
| 143 | * This is for the lazy. This is all that has to be called from the |
| 144 | * main() function. |
| 145 | * |
| 146 | * This function does the following: |
Miklos Szeredi | 307242f | 2004-01-26 11:28:44 +0000 | [diff] [blame^] | 147 | * - parses command line options (-d -s and -h) |
| 148 | * - passes all options after '--' to the fusermount program |
| 149 | * - mounts the filesystem by calling fusermount |
Miklos Szeredi | 0e53508 | 2003-10-13 10:08:06 +0000 | [diff] [blame] | 150 | * - installs signal handlers for INT, HUP, TERM and PIPE |
| 151 | * - registers an exit handler to unmount the filesystem on program exit |
Miklos Szeredi | 0e53508 | 2003-10-13 10:08:06 +0000 | [diff] [blame] | 152 | * - creates a fuse handle |
| 153 | * - registers the operations |
| 154 | * - calls either the single-threaded or the multi-threaded event loop |
| 155 | * |
| 156 | * @param argc the argument counter passed to the main() function |
| 157 | * @param argv the argument vector passed to the main() function |
| 158 | * @param op the file system operation |
| 159 | */ |
| 160 | void fuse_main(int argc, char *argv[], const struct fuse_operations *op); |
| 161 | |
| 162 | /* ----------------------------------------------------------- * |
| 163 | * More detailed API * |
| 164 | * ----------------------------------------------------------- */ |
| 165 | |
| 166 | /* |
Miklos Szeredi | a4b0c77 | 2002-04-19 15:57:02 +0000 | [diff] [blame] | 167 | * Create a FUSE mountpoint |
| 168 | * |
| 169 | * Returns a control file descriptor suitable for passing to |
| 170 | * fuse_new() |
| 171 | * |
| 172 | * @param mountpoint the mount point path |
Miklos Szeredi | d6e9f88 | 2002-10-25 11:40:14 +0000 | [diff] [blame] | 173 | * @param args array of arguments to be passed to fusermount (NULL |
| 174 | * terminated). Can be NULL if no arguments are needed. |
Miklos Szeredi | a4b0c77 | 2002-04-19 15:57:02 +0000 | [diff] [blame] | 175 | * @return the control file descriptor on success, -1 on failure |
| 176 | */ |
Miklos Szeredi | d6e9f88 | 2002-10-25 11:40:14 +0000 | [diff] [blame] | 177 | int fuse_mount(const char *mountpoint, const char *args[]); |
Miklos Szeredi | a4b0c77 | 2002-04-19 15:57:02 +0000 | [diff] [blame] | 178 | |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 179 | /* |
| 180 | * Umount a FUSE mountpoint |
| 181 | * |
| 182 | * @param mountpoint the mount point path |
| 183 | */ |
| 184 | void fuse_unmount(const char *mountpoint); |
| 185 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 186 | /** |
Miklos Szeredi | 8cffdb9 | 2001-11-09 14:49:18 +0000 | [diff] [blame] | 187 | * Create a new FUSE filesystem. |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 188 | * |
Miklos Szeredi | 8cffdb9 | 2001-11-09 14:49:18 +0000 | [diff] [blame] | 189 | * @param fd the control file descriptor |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 190 | * @param flags any combination of the FUSE flags defined above, or 0 |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 191 | * @param op the operations |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 192 | * @return the created FUSE handle |
| 193 | */ |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 194 | struct fuse *fuse_new(int fd, int flags, const struct fuse_operations *op); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 195 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 196 | /** |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 197 | * Destroy the FUSE handle. |
| 198 | * |
| 199 | * The filesystem is not unmounted. |
| 200 | * |
| 201 | * @param f the FUSE handle |
| 202 | */ |
| 203 | void fuse_destroy(struct fuse *f); |
| 204 | |
| 205 | /** |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 206 | * FUSE event loop. |
| 207 | * |
| 208 | * Requests from the kernel are processed, and the apropriate |
| 209 | * operations are called. |
| 210 | * |
| 211 | * @param f the FUSE handle |
| 212 | */ |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 213 | void fuse_loop(struct fuse *f); |
| 214 | |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 215 | /** |
| 216 | * Exit from event loop |
| 217 | * |
| 218 | * @param f the FUSE handle |
| 219 | */ |
| 220 | void fuse_exit(struct fuse *f); |
| 221 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 222 | /** |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 223 | * FUSE event loop with multiple threads |
| 224 | * |
| 225 | * Requests from the kernel are processed, and the apropriate |
| 226 | * operations are called. Request are processed in parallel by |
| 227 | * distributing them between multiple threads. |
| 228 | * |
| 229 | * Calling this function requires the pthreads library to be linked to |
| 230 | * the application. |
| 231 | * |
| 232 | * @param f the FUSE handle |
| 233 | */ |
| 234 | void fuse_loop_mt(struct fuse *f); |
| 235 | |
| 236 | /** |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 237 | * Get the current context |
| 238 | * |
| 239 | * The context is only valid for the duration of a filesystem |
| 240 | * operation, and thus must not be stored and used later. |
| 241 | * |
| 242 | * @param f the FUSE handle |
| 243 | * @return the context |
| 244 | */ |
| 245 | struct fuse_context *fuse_get_context(struct fuse *f); |
| 246 | |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 247 | /* ----------------------------------------------------------- * |
Miklos Szeredi | 680a69a | 2001-11-16 13:31:14 +0000 | [diff] [blame] | 248 | * Advanced API for event handling, don't worry about this... * |
| 249 | * ----------------------------------------------------------- */ |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 250 | |
| 251 | struct fuse_cmd; |
Miklos Szeredi | f830a7f | 2001-11-16 17:46:45 +0000 | [diff] [blame] | 252 | typedef void (*fuse_processor_t)(struct fuse *, struct fuse_cmd *, void *); |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 253 | struct fuse_cmd *__fuse_read_cmd(struct fuse *f); |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 254 | void __fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd); |
Miklos Szeredi | f830a7f | 2001-11-16 17:46:45 +0000 | [diff] [blame] | 255 | void __fuse_loop_mt(struct fuse *f, fuse_processor_t proc, void *data); |
| 256 | |
Miklos Szeredi | acd4e06 | 2001-12-08 20:29:20 +0000 | [diff] [blame] | 257 | #ifdef __cplusplus |
| 258 | } |
| 259 | #endif |
| 260 | |
| 261 | #endif /* _FUSE_H_ */ |