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 | |
| 5 | This program can be distributed under the terms of the GNU GPL. |
| 6 | See the file COPYING. |
| 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 | |
| 14 | #include <sys/types.h> |
| 15 | #include <sys/stat.h> |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 16 | #include <utime.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 17 | |
Roland Bauerschmidt | f0cd8e4 | 2002-04-12 14:32:43 +0000 | [diff] [blame] | 18 | #include <fusestat.h> |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 19 | /* ----------------------------------------------------------- * |
| 20 | * Basic FUSE API * |
| 21 | * ----------------------------------------------------------- */ |
| 22 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 23 | /** Handle for a FUSE filesystem */ |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 24 | struct fuse; |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 25 | |
| 26 | /** Handle for a getdir() operation */ |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 27 | typedef struct fuse_dirhandle *fuse_dirh_t; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 28 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 29 | /** Function to add an entry in a getdir() operation */ |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 30 | typedef int (*fuse_dirfil_t) (fuse_dirh_t, const char *, int type); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 31 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 32 | /** |
| 33 | * The file system operations: |
| 34 | * |
| 35 | * Most of these should work very similarly to the well known UNIX |
| 36 | * file system operations. Exceptions are: |
| 37 | * |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 38 | * - All operations should return the negated error value (-errno) on |
| 39 | * error. |
| 40 | * |
Miklos Szeredi | 0503304 | 2001-11-13 16:11:35 +0000 | [diff] [blame] | 41 | * - Getattr() doesn't need to fill in the following fields: |
| 42 | * st_ino |
| 43 | * st_dev |
| 44 | * st_blksize |
| 45 | * |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 46 | * - readlink() should fill the buffer with a null terminated string. The |
| 47 | * buffer size argument includes the space for the terminating null |
| 48 | * character. If the linkname is too long to fit in the buffer, it should |
| 49 | * be truncated. The return value should be 0 for success. |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 50 | * |
| 51 | * - getdir() is the opendir(), readdir(), ..., closedir() sequence |
| 52 | * in one call. For each directory entry the filldir parameter should |
| 53 | * be called. |
| 54 | * |
| 55 | * - There is no create() operation, mknod() will be called for |
| 56 | * creation of all non directory, non symlink nodes. |
| 57 | * |
| 58 | * - open() should not return a filehandle, but 0 on success. No |
| 59 | * creation, or trunctation flags (O_CREAT, O_EXCL, O_TRUNC) will be |
| 60 | * passed to open(). Open should only check if the operation is |
| 61 | * permitted for the given flags. |
| 62 | * |
| 63 | * - read(), write() are not passed a filehandle, but rather a |
| 64 | * pathname. The offset of the read and write is passed as the last |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 65 | * argument, like the pread() and pwrite() system calls. |
| 66 | */ |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 67 | struct fuse_operations { |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 68 | int (*getattr) (const char *, struct stat *); |
| 69 | int (*readlink) (const char *, char *, size_t); |
| 70 | int (*getdir) (const char *, fuse_dirh_t, fuse_dirfil_t); |
| 71 | int (*mknod) (const char *, mode_t, dev_t); |
| 72 | int (*mkdir) (const char *, mode_t); |
| 73 | int (*unlink) (const char *); |
| 74 | int (*rmdir) (const char *); |
| 75 | int (*symlink) (const char *, const char *); |
| 76 | int (*rename) (const char *, const char *); |
| 77 | int (*link) (const char *, const char *); |
| 78 | int (*chmod) (const char *, mode_t); |
| 79 | int (*chown) (const char *, uid_t, gid_t); |
| 80 | int (*truncate) (const char *, off_t); |
| 81 | int (*utime) (const char *, struct utimbuf *); |
| 82 | int (*open) (const char *, int); |
| 83 | int (*read) (const char *, char *, size_t, off_t); |
| 84 | int (*write) (const char *, const char *, size_t, off_t); |
Mark Glines | 85801bb | 2002-03-17 06:58:33 +0000 | [diff] [blame] | 85 | int (*statfs) (struct fuse_statfs *); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 88 | /** Extra context that may be needed by some filesystems */ |
| 89 | struct fuse_context { |
| 90 | uid_t uid; |
| 91 | gid_t gid; |
| 92 | }; |
| 93 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 94 | /* FUSE flags: */ |
Miklos Szeredi | c0938ea | 2001-11-07 12:35:06 +0000 | [diff] [blame] | 95 | |
Miklos Szeredi | c0938ea | 2001-11-07 12:35:06 +0000 | [diff] [blame] | 96 | /** Enable debuging output */ |
| 97 | #define FUSE_DEBUG (1 << 1) |
| 98 | |
Miklos Szeredi | acd4e06 | 2001-12-08 20:29:20 +0000 | [diff] [blame] | 99 | #ifdef __cplusplus |
| 100 | extern "C" { |
| 101 | #endif |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 102 | |
Miklos Szeredi | a4b0c77 | 2002-04-19 15:57:02 +0000 | [diff] [blame^] | 103 | /* |
| 104 | * Create a FUSE mountpoint |
| 105 | * |
| 106 | * Returns a control file descriptor suitable for passing to |
| 107 | * fuse_new() |
| 108 | * |
| 109 | * @param mountpoint the mount point path |
| 110 | * @param mount arguments (passed to the fusermount program) |
| 111 | * @return the control file descriptor on success, -1 on failure |
| 112 | */ |
| 113 | int fuse_mount(const char *mountpoint, const char *mount_args); |
| 114 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 115 | /** |
Miklos Szeredi | 8cffdb9 | 2001-11-09 14:49:18 +0000 | [diff] [blame] | 116 | * Create a new FUSE filesystem. |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 117 | * |
Miklos Szeredi | 8cffdb9 | 2001-11-09 14:49:18 +0000 | [diff] [blame] | 118 | * @param fd the control file descriptor |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 119 | * @param flags any combination of the FUSE flags defined above, or 0 |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 120 | * @param op the operations |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 121 | * @return the created FUSE handle |
| 122 | */ |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 123 | 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] | 124 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 125 | /** |
| 126 | * FUSE event loop. |
| 127 | * |
| 128 | * Requests from the kernel are processed, and the apropriate |
| 129 | * operations are called. |
| 130 | * |
| 131 | * @param f the FUSE handle |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 132 | * @prarm op the file system operations |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 133 | */ |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 134 | void fuse_loop(struct fuse *f); |
| 135 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 136 | /** |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 137 | * FUSE event loop with multiple threads |
| 138 | * |
| 139 | * Requests from the kernel are processed, and the apropriate |
| 140 | * operations are called. Request are processed in parallel by |
| 141 | * distributing them between multiple threads. |
| 142 | * |
| 143 | * Calling this function requires the pthreads library to be linked to |
| 144 | * the application. |
| 145 | * |
| 146 | * @param f the FUSE handle |
| 147 | */ |
| 148 | void fuse_loop_mt(struct fuse *f); |
| 149 | |
| 150 | /** |
Miklos Szeredi | 8cffdb9 | 2001-11-09 14:49:18 +0000 | [diff] [blame] | 151 | * Destroy the FUSE handle. |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 152 | * |
Miklos Szeredi | 8cffdb9 | 2001-11-09 14:49:18 +0000 | [diff] [blame] | 153 | * The filesystem is not unmounted. |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 154 | * |
| 155 | * @param f the FUSE handle |
| 156 | */ |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 157 | void fuse_destroy(struct fuse *f); |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 158 | |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 159 | /** |
| 160 | * Get the current context |
| 161 | * |
| 162 | * The context is only valid for the duration of a filesystem |
| 163 | * operation, and thus must not be stored and used later. |
| 164 | * |
| 165 | * @param f the FUSE handle |
| 166 | * @return the context |
| 167 | */ |
| 168 | struct fuse_context *fuse_get_context(struct fuse *f); |
| 169 | |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 170 | /* ----------------------------------------------------------- * |
| 171 | * Miscellaneous helper fuctions * |
| 172 | * ----------------------------------------------------------- */ |
| 173 | |
| 174 | /* |
| 175 | * Main function of FUSE. |
| 176 | * |
| 177 | * This is for the lazy. This is all that has to be called from the |
| 178 | * main() function. |
| 179 | * |
| 180 | * This function does the following: |
| 181 | * - mounts the filesystem |
| 182 | * - installs signal handlers for INT, HUP, TERM and PIPE |
| 183 | * - registers an exit handler to unmount the filesystem on program exit |
| 184 | * - parses command line options (-d -s and -h) |
| 185 | * - creates a fuse handle |
| 186 | * - registers the operations |
| 187 | * - calls either the single-threaded or the multi-threaded event loop |
| 188 | * |
| 189 | * @param argc the argument counter passed to the main() function |
| 190 | * @param argv the argument vector passed to the main() function |
| 191 | * @prarm op the file system operation |
| 192 | */ |
| 193 | void fuse_main(int argc, char *argv[], const struct fuse_operations *op); |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 194 | |
Miklos Szeredi | 680a69a | 2001-11-16 13:31:14 +0000 | [diff] [blame] | 195 | /* ----------------------------------------------------------- * |
| 196 | * Advanced API for event handling, don't worry about this... * |
| 197 | * ----------------------------------------------------------- */ |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 198 | |
| 199 | struct fuse_cmd; |
Miklos Szeredi | f830a7f | 2001-11-16 17:46:45 +0000 | [diff] [blame] | 200 | typedef void (*fuse_processor_t)(struct fuse *, struct fuse_cmd *, void *); |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 201 | struct fuse_cmd *__fuse_read_cmd(struct fuse *f); |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 202 | void __fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd); |
Miklos Szeredi | f830a7f | 2001-11-16 17:46:45 +0000 | [diff] [blame] | 203 | void __fuse_loop_mt(struct fuse *f, fuse_processor_t proc, void *data); |
| 204 | |
Miklos Szeredi | acd4e06 | 2001-12-08 20:29:20 +0000 | [diff] [blame] | 205 | #ifdef __cplusplus |
| 206 | } |
| 207 | #endif |
| 208 | |
| 209 | #endif /* _FUSE_H_ */ |