Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | FUSE: Filesystem in Userspace |
Miklos Szeredi | 149f607 | 2005-01-10 12:29:28 +0000 | [diff] [blame] | 3 | Copyright (C) 2001-2005 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 | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 14 | /* IMPORTANT: you should define FUSE_USE_VERSION before including this |
| 15 | header. To use the new API define it to 22 (recommended for any |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 16 | new application), to use the old API define it to 21 (this is the |
| 17 | default), to use the even older 1.X API define it to 11. */ |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 18 | |
| 19 | #ifndef FUSE_USE_VERSION |
| 20 | #define FUSE_USE_VERSION 21 |
| 21 | #endif |
| 22 | |
Miklos Szeredi | 2f3d940 | 2003-12-15 12:11:33 +0000 | [diff] [blame] | 23 | /** Major version of FUSE library interface */ |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 24 | #define FUSE_MAJOR_VERSION 2 |
Miklos Szeredi | 2f3d940 | 2003-12-15 12:11:33 +0000 | [diff] [blame] | 25 | |
| 26 | /** Minor version of FUSE library interface */ |
Miklos Szeredi | f43f063 | 2005-02-28 11:46:56 +0000 | [diff] [blame] | 27 | #define FUSE_MINOR_VERSION 3 |
Miklos Szeredi | 2f3d940 | 2003-12-15 12:11:33 +0000 | [diff] [blame] | 28 | |
Miklos Szeredi | 87f30a9 | 2004-04-13 10:49:54 +0000 | [diff] [blame] | 29 | /* This interface uses 64 bit off_t */ |
| 30 | #if _FILE_OFFSET_BITS != 64 |
| 31 | #error Please add -D_FILE_OFFSET_BITS=64 to your compile flags! |
| 32 | #endif |
Miklos Szeredi | 5f05481 | 2002-12-03 18:45:21 +0000 | [diff] [blame] | 33 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 34 | #include <sys/types.h> |
| 35 | #include <sys/stat.h> |
Miklos Szeredi | 18e75e4 | 2004-02-19 14:23:27 +0000 | [diff] [blame] | 36 | #include <sys/statfs.h> |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 37 | #include <utime.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 38 | |
Miklos Szeredi | 8fb48fe | 2004-11-08 14:48:52 +0000 | [diff] [blame] | 39 | #ifdef __cplusplus |
| 40 | extern "C" { |
| 41 | #endif |
| 42 | |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 43 | /* ----------------------------------------------------------- * |
| 44 | * Basic FUSE API * |
| 45 | * ----------------------------------------------------------- */ |
| 46 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 47 | /** Handle for a FUSE filesystem */ |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 48 | struct fuse; |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 49 | |
| 50 | /** Handle for a getdir() operation */ |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 51 | typedef struct fuse_dirhandle *fuse_dirh_t; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 52 | |
Miklos Szeredi | f08ace0 | 2003-10-22 11:11:57 +0000 | [diff] [blame] | 53 | /** Function to add an entry in a getdir() operation |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 54 | * |
Miklos Szeredi | f08ace0 | 2003-10-22 11:11:57 +0000 | [diff] [blame] | 55 | * @param h the handle passed to the getdir() operation |
| 56 | * @param name the file name of the directory entry |
| 57 | * @param type the file type (0 if unknown) see <dirent.h> |
Miklos Szeredi | 8fb48fe | 2004-11-08 14:48:52 +0000 | [diff] [blame] | 58 | * @param ino the inode number, ignored if "use_ino" mount option is |
| 59 | * not specified |
Miklos Szeredi | f08ace0 | 2003-10-22 11:11:57 +0000 | [diff] [blame] | 60 | * @return 0 on success, -errno on error |
| 61 | */ |
Miklos Szeredi | d507c73 | 2004-11-08 17:32:25 +0000 | [diff] [blame] | 62 | typedef int (*fuse_dirfil_t) (fuse_dirh_t h, const char *name, int type, |
| 63 | ino_t ino); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 64 | |
Miklos Szeredi | 18fce98 | 2005-04-01 21:07:35 +0000 | [diff] [blame^] | 65 | /** Function to add an entry in a readdir() operation |
| 66 | * |
| 67 | * @param buf the buffer passed to the readdir() operation |
| 68 | * @param name the file name of the directory entry |
| 69 | * @param type the file type (0 if unknown) see <dirent.h> |
| 70 | * @param ino the inode number, ignored if "use_ino" mount option is |
| 71 | * not specified |
| 72 | * @param off offset of the next entry |
| 73 | * @return 0 on success, 1 if buffer is full |
| 74 | */ |
| 75 | typedef int (*fuse_dirfil5_t) (void *buf, const char *name, int type, |
| 76 | ino_t ino, off_t off); |
| 77 | |
Miklos Szeredi | fb28c5e | 2004-11-26 12:15:06 +0000 | [diff] [blame] | 78 | /** Information about open files */ |
| 79 | struct fuse_file_info { |
| 80 | /** Open flags. Available in open() and release() */ |
| 81 | int flags; |
| 82 | |
| 83 | /** File handle. May be filled in by filesystem in open(). |
| 84 | Available in all other file operations */ |
| 85 | unsigned long fh; |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 86 | |
| 87 | /** In case of a write operation indicates if this was caused by a |
| 88 | writepage */ |
| 89 | int writepage; |
Miklos Szeredi | fb28c5e | 2004-11-26 12:15:06 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 92 | /** |
| 93 | * The file system operations: |
| 94 | * |
| 95 | * Most of these should work very similarly to the well known UNIX |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 96 | * file system operations. A major exception is that instead of |
| 97 | * returning an error in 'errno', the operation should return the |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 98 | * negated error value (-errno) directly. |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 99 | * |
| 100 | * All methods are optional, but some are essential for a useful |
Miklos Szeredi | 18fce98 | 2005-04-01 21:07:35 +0000 | [diff] [blame^] | 101 | * filesystem (e.g. getattr). Flush, release, fsync, opendir, |
| 102 | * readdir, releasedir, fsyncdir, init and destroy are special purpose |
| 103 | * methods, without which a full featured filesystem can still be |
| 104 | * implemented. |
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 | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 107 | /** Get file attributes. |
| 108 | * |
| 109 | * Similar to stat(). The 'st_dev' and 'st_blksize' fields are |
| 110 | * ignored. The 'st_ino' field is ignored except if the 'use_ino' |
| 111 | * mount option is given. |
| 112 | */ |
| 113 | int (*getattr) (const char *, struct stat *); |
| 114 | |
| 115 | /** Read the target of a symbolic link |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 116 | * |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 117 | * The buffer should be filled with a null terminated string. The |
| 118 | * buffer size argument includes the space for the terminating |
| 119 | * null character. If the linkname is too long to fit in the |
| 120 | * buffer, it should be truncated. The return value should be 0 |
| 121 | * for success. |
| 122 | */ |
| 123 | int (*readlink) (const char *, char *, size_t); |
| 124 | |
| 125 | /** Read the contents of a directory |
| 126 | * |
| 127 | * This operation is the opendir(), readdir(), ..., closedir() |
| 128 | * sequence in one call. For each directory entry the filldir |
| 129 | * function should be called. |
| 130 | */ |
| 131 | int (*getdir) (const char *, fuse_dirh_t, fuse_dirfil_t); |
| 132 | |
| 133 | /** Create a file node |
| 134 | * |
| 135 | * There is no create() operation, mknod() will be called for |
| 136 | * creation of all non-directory, non-symlink nodes. |
| 137 | */ |
| 138 | int (*mknod) (const char *, mode_t, dev_t); |
| 139 | |
| 140 | /** Create a directory */ |
| 141 | int (*mkdir) (const char *, mode_t); |
| 142 | |
| 143 | /** Remove a file */ |
| 144 | int (*unlink) (const char *); |
| 145 | |
| 146 | /** Remove a directory */ |
| 147 | int (*rmdir) (const char *); |
| 148 | |
| 149 | /** Create a symbolic link */ |
| 150 | int (*symlink) (const char *, const char *); |
| 151 | |
| 152 | /** Rename a file */ |
| 153 | int (*rename) (const char *, const char *); |
| 154 | |
| 155 | /** Create a hard link to a file */ |
| 156 | int (*link) (const char *, const char *); |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 157 | |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 158 | /** Change the permission bits of a file */ |
| 159 | int (*chmod) (const char *, mode_t); |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 160 | |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 161 | /** Change the owner and group of a file */ |
| 162 | int (*chown) (const char *, uid_t, gid_t); |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 163 | |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 164 | /** Change the size of a file */ |
| 165 | int (*truncate) (const char *, off_t); |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 166 | |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 167 | /** Change the access and/or modification times of a file */ |
| 168 | int (*utime) (const char *, struct utimbuf *); |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 169 | |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 170 | /** File open operation |
| 171 | * |
| 172 | * No creation, or trunctation flags (O_CREAT, O_EXCL, O_TRUNC) |
| 173 | * will be passed to open(). Open should check if the operation |
| 174 | * is permitted for the given flags. Optionally open may also |
| 175 | * return an arbitary filehandle in the fuse_file_info structure, |
| 176 | * which will be passed to all file operations. |
| 177 | */ |
| 178 | int (*open) (const char *, struct fuse_file_info *); |
| 179 | |
| 180 | /** Read data from an open file |
| 181 | * |
| 182 | * Read should return exactly the number of bytes requested except |
| 183 | * on EOF or error, otherwise the rest of the data will be |
| 184 | * substituted with zeroes. An exception to this is when the |
| 185 | * 'direct_io' mount option is specified, in which case the return |
| 186 | * value of the read system call will reflect the return value of |
| 187 | * this operation. |
| 188 | */ |
| 189 | int (*read) (const char *, char *, size_t, off_t, struct fuse_file_info *); |
| 190 | |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 191 | /** Write data to an open file |
| 192 | * |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 193 | * Write should return exactly the number of bytes requested |
| 194 | * except on error. An exception to this is when the 'direct_io' |
| 195 | * mount option is specified (see read operation). |
| 196 | */ |
| 197 | int (*write) (const char *, const char *, size_t, off_t, |
| 198 | struct fuse_file_info *); |
| 199 | |
| 200 | /** Get file system statistics |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 201 | * |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 202 | * The 'f_type' and 'f_fsid' fields are ignored |
| 203 | */ |
| 204 | int (*statfs) (const char *, struct statfs *); |
| 205 | |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 206 | /** Possibly flush cached data |
| 207 | * |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 208 | * BIG NOTE: This is not equivalent to fsync(). It's not a |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 209 | * request to sync dirty data. |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 210 | * |
| 211 | * Flush is called on each close() of a file descriptor. So if a |
| 212 | * filesystem wants to return write errors in close() and the file |
| 213 | * has cached dirty data, this is a good place to write back data |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 214 | * and return any errors. Since many applications ignore close() |
| 215 | * errors this is not always useful. |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 216 | * |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 217 | * NOTE: The flush() method may be called more than once for each |
| 218 | * open(). This happens if more than one file descriptor refers |
| 219 | * to an opened file due to dup(), dup2() or fork() calls. It is |
| 220 | * not possible to determine if a flush is final, so each flush |
| 221 | * should be treated equally. Multiple write-flush sequences are |
| 222 | * relatively rare, so this shouldn't be a problem. |
| 223 | */ |
| 224 | int (*flush) (const char *, struct fuse_file_info *); |
| 225 | |
| 226 | /** Release an open file |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 227 | * |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 228 | * Release is called when there are no more references to an open |
| 229 | * file: all file descriptors are closed and all memory mappings |
| 230 | * are unmapped. |
| 231 | * |
| 232 | * For every open() call there will be exactly one release() call |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 233 | * with the same flags and file descriptor. It is possible to |
| 234 | * have a file opened more than once, in which case only the last |
| 235 | * release will mean, that no more reads/writes will happen on the |
| 236 | * file. The return value of release is ignored. |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 237 | */ |
| 238 | int (*release) (const char *, struct fuse_file_info *); |
| 239 | |
| 240 | /** Synchronize file contents |
| 241 | * |
| 242 | * If the datasync parameter is non-zero, then only the user data |
| 243 | * should be flushed, not the meta data. |
| 244 | */ |
| 245 | int (*fsync) (const char *, int, struct fuse_file_info *); |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 246 | |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 247 | /** Set extended attributes */ |
| 248 | int (*setxattr) (const char *, const char *, const char *, size_t, int); |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 249 | |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 250 | /** Get extended attributes */ |
| 251 | int (*getxattr) (const char *, const char *, char *, size_t); |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 252 | |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 253 | /** List extended attributes */ |
| 254 | int (*listxattr) (const char *, char *, size_t); |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 255 | |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 256 | /** Remove extended attributes */ |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 257 | int (*removexattr) (const char *, const char *); |
Miklos Szeredi | f43f063 | 2005-02-28 11:46:56 +0000 | [diff] [blame] | 258 | |
| 259 | /** Open direcory |
Miklos Szeredi | 18fce98 | 2005-04-01 21:07:35 +0000 | [diff] [blame^] | 260 | * |
Miklos Szeredi | f43f063 | 2005-02-28 11:46:56 +0000 | [diff] [blame] | 261 | * This method should check if the open operation is permitted for |
Miklos Szeredi | 18fce98 | 2005-04-01 21:07:35 +0000 | [diff] [blame^] | 262 | * this directory |
Miklos Szeredi | f43f063 | 2005-02-28 11:46:56 +0000 | [diff] [blame] | 263 | */ |
| 264 | int (*opendir) (const char *, struct fuse_file_info *); |
Miklos Szeredi | 159bd7e | 2005-02-28 17:32:16 +0000 | [diff] [blame] | 265 | |
Miklos Szeredi | 18fce98 | 2005-04-01 21:07:35 +0000 | [diff] [blame^] | 266 | /** Read directory |
| 267 | * |
| 268 | * This is an alternative inteface to getdir(), and if defined |
| 269 | * getdir() will not be called |
| 270 | */ |
| 271 | int (*readdir) (const char *, void *, fuse_dirfil5_t, off_t, |
| 272 | struct fuse_file_info *); |
| 273 | |
| 274 | /** Release directory */ |
| 275 | int (*releasedir) (const char *, struct fuse_file_info *); |
| 276 | |
Miklos Szeredi | 4283ee7 | 2005-03-21 12:09:04 +0000 | [diff] [blame] | 277 | /** Synchronize directory contents |
| 278 | * |
| 279 | * If the datasync parameter is non-zero, then only the user data |
Miklos Szeredi | 18fce98 | 2005-04-01 21:07:35 +0000 | [diff] [blame^] | 280 | * should be flushed, not the meta data |
Miklos Szeredi | 4283ee7 | 2005-03-21 12:09:04 +0000 | [diff] [blame] | 281 | */ |
| 282 | int (*fsyncdir) (const char *, int, struct fuse_file_info *); |
| 283 | |
Miklos Szeredi | 159bd7e | 2005-02-28 17:32:16 +0000 | [diff] [blame] | 284 | /** |
| 285 | * Initialize filesystem |
| 286 | * |
| 287 | * The return value will passed in the private_data field of |
| 288 | * fuse_context to all file operations and as a parameter to the |
| 289 | * destroy() method. |
| 290 | */ |
| 291 | void *(*init) (void); |
| 292 | |
| 293 | /** |
| 294 | * Clean up filesystem |
Miklos Szeredi | 18fce98 | 2005-04-01 21:07:35 +0000 | [diff] [blame^] | 295 | * |
Miklos Szeredi | 159bd7e | 2005-02-28 17:32:16 +0000 | [diff] [blame] | 296 | * Called on filesystem exit. |
| 297 | */ |
| 298 | void (*destroy) (void *); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 299 | }; |
| 300 | |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 301 | /** Extra context that may be needed by some filesystems |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 302 | * |
| 303 | * The uid, gid and pid fields are not filled in case of a writepage |
| 304 | * operation. |
| 305 | */ |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 306 | struct fuse_context { |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 307 | /** Pointer to the fuse object */ |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame] | 308 | struct fuse *fuse; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 309 | |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 310 | /** User ID of the calling process */ |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 311 | uid_t uid; |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 312 | |
| 313 | /** Group ID of the calling process */ |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 314 | gid_t gid; |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 315 | |
| 316 | /** Thread ID of the calling process */ |
Miklos Szeredi | 1f18db5 | 2004-09-27 06:54:49 +0000 | [diff] [blame] | 317 | pid_t pid; |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 318 | |
Miklos Szeredi | 159bd7e | 2005-02-28 17:32:16 +0000 | [diff] [blame] | 319 | /** Private filesystem data */ |
Miklos Szeredi | 8fb48fe | 2004-11-08 14:48:52 +0000 | [diff] [blame] | 320 | void *private_data; |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 321 | }; |
| 322 | |
Miklos Szeredi | a4b0c77 | 2002-04-19 15:57:02 +0000 | [diff] [blame] | 323 | /* |
Miklos Szeredi | 0e53508 | 2003-10-13 10:08:06 +0000 | [diff] [blame] | 324 | * Main function of FUSE. |
| 325 | * |
| 326 | * This is for the lazy. This is all that has to be called from the |
| 327 | * main() function. |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 328 | * |
Miklos Szeredi | 0e53508 | 2003-10-13 10:08:06 +0000 | [diff] [blame] | 329 | * This function does the following: |
Miklos Szeredi | 307242f | 2004-01-26 11:28:44 +0000 | [diff] [blame] | 330 | * - parses command line options (-d -s and -h) |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 331 | * - passes relevant mount options to the fuse_mount() |
Miklos Szeredi | 0e53508 | 2003-10-13 10:08:06 +0000 | [diff] [blame] | 332 | * - installs signal handlers for INT, HUP, TERM and PIPE |
| 333 | * - registers an exit handler to unmount the filesystem on program exit |
Miklos Szeredi | 0e53508 | 2003-10-13 10:08:06 +0000 | [diff] [blame] | 334 | * - creates a fuse handle |
| 335 | * - registers the operations |
| 336 | * - calls either the single-threaded or the multi-threaded event loop |
| 337 | * |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 338 | * Note: this is currently implemented as a macro. |
| 339 | * |
Miklos Szeredi | 0e53508 | 2003-10-13 10:08:06 +0000 | [diff] [blame] | 340 | * @param argc the argument counter passed to the main() function |
| 341 | * @param argv the argument vector passed to the main() function |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 342 | * @param op the file system operation |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 343 | * @return 0 on success, nonzero on failure |
Miklos Szeredi | 0e53508 | 2003-10-13 10:08:06 +0000 | [diff] [blame] | 344 | */ |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 345 | /* |
| 346 | int fuse_main(int argc, char *argv[], const struct fuse_operations *op); |
| 347 | */ |
| 348 | #define fuse_main(argc, argv, op) \ |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 349 | fuse_main_real(argc, argv, op, sizeof(*(op))) |
Miklos Szeredi | 891b874 | 2004-07-29 09:27:49 +0000 | [diff] [blame] | 350 | |
Miklos Szeredi | 0e53508 | 2003-10-13 10:08:06 +0000 | [diff] [blame] | 351 | /* ----------------------------------------------------------- * |
| 352 | * More detailed API * |
| 353 | * ----------------------------------------------------------- */ |
| 354 | |
| 355 | /* |
Miklos Szeredi | a4b0c77 | 2002-04-19 15:57:02 +0000 | [diff] [blame] | 356 | * Create a FUSE mountpoint |
| 357 | * |
| 358 | * Returns a control file descriptor suitable for passing to |
| 359 | * fuse_new() |
| 360 | * |
| 361 | * @param mountpoint the mount point path |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 362 | * @param opts a comma separated list of mount options. Can be NULL. |
Miklos Szeredi | a4b0c77 | 2002-04-19 15:57:02 +0000 | [diff] [blame] | 363 | * @return the control file descriptor on success, -1 on failure |
| 364 | */ |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 365 | int fuse_mount(const char *mountpoint, const char *opts); |
Miklos Szeredi | a4b0c77 | 2002-04-19 15:57:02 +0000 | [diff] [blame] | 366 | |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 367 | /* |
| 368 | * Umount a FUSE mountpoint |
| 369 | * |
| 370 | * @param mountpoint the mount point path |
| 371 | */ |
| 372 | void fuse_unmount(const char *mountpoint); |
| 373 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 374 | /** |
Miklos Szeredi | 8cffdb9 | 2001-11-09 14:49:18 +0000 | [diff] [blame] | 375 | * Create a new FUSE filesystem. |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 376 | * |
Miklos Szeredi | 8cffdb9 | 2001-11-09 14:49:18 +0000 | [diff] [blame] | 377 | * @param fd the control file descriptor |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 378 | * @param opts mount options to be used by the library |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 379 | * @param op the operations |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 380 | * @param op_size the size of the fuse_operations structure |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 381 | * @return the created FUSE handle |
| 382 | */ |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 383 | struct fuse *fuse_new(int fd, const char *opts, |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 384 | const struct fuse_operations *op, size_t op_size); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 385 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 386 | /** |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 387 | * Destroy the FUSE handle. |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 388 | * |
| 389 | * The filesystem is not unmounted. |
| 390 | * |
| 391 | * @param f the FUSE handle |
| 392 | */ |
| 393 | void fuse_destroy(struct fuse *f); |
| 394 | |
| 395 | /** |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 396 | * FUSE event loop. |
| 397 | * |
| 398 | * Requests from the kernel are processed, and the apropriate |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 399 | * operations are called. |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 400 | * |
| 401 | * @param f the FUSE handle |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 402 | * @return 0 if no error occured, -1 otherwise |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 403 | */ |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 404 | int fuse_loop(struct fuse *f); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 405 | |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 406 | /** |
| 407 | * Exit from event loop |
| 408 | * |
| 409 | * @param f the FUSE handle |
| 410 | */ |
| 411 | void fuse_exit(struct fuse *f); |
| 412 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 413 | /** |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 414 | * FUSE event loop with multiple threads |
| 415 | * |
| 416 | * Requests from the kernel are processed, and the apropriate |
| 417 | * operations are called. Request are processed in parallel by |
| 418 | * distributing them between multiple threads. |
| 419 | * |
| 420 | * Calling this function requires the pthreads library to be linked to |
| 421 | * the application. |
| 422 | * |
| 423 | * @param f the FUSE handle |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 424 | * @return 0 if no error occured, -1 otherwise |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 425 | */ |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 426 | int fuse_loop_mt(struct fuse *f); |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 427 | |
| 428 | /** |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 429 | * Get the current context |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 430 | * |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 431 | * The context is only valid for the duration of a filesystem |
| 432 | * operation, and thus must not be stored and used later. |
| 433 | * |
| 434 | * @param f the FUSE handle |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 435 | * @return the context |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 436 | */ |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame] | 437 | struct fuse_context *fuse_get_context(void); |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 438 | |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 439 | /** |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 440 | * Invalidate cached data of a file. |
| 441 | * |
| 442 | * Useful if the 'kernel_cache' mount option is given, since in that |
| 443 | * case the cache is not invalidated on file open. |
| 444 | * |
| 445 | * @return 0 on success or -errno on failure |
| 446 | */ |
| 447 | int fuse_invalidate(struct fuse *f, const char *path); |
| 448 | |
| 449 | /** |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 450 | * Check whether a mount option should be passed to the kernel or the |
| 451 | * library |
| 452 | * |
| 453 | * @param opt the option to check |
| 454 | * @return 1 if it is a library option, 0 otherwise |
| 455 | */ |
| 456 | int fuse_is_lib_option(const char *opt); |
| 457 | |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 458 | /** |
| 459 | * The real main function |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 460 | * |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 461 | * Do not call this directly, use fuse_main() |
| 462 | */ |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 463 | int fuse_main_real(int argc, char *argv[], const struct fuse_operations *op, |
| 464 | size_t op_size); |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 465 | |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 466 | /* ----------------------------------------------------------- * |
Miklos Szeredi | 680a69a | 2001-11-16 13:31:14 +0000 | [diff] [blame] | 467 | * Advanced API for event handling, don't worry about this... * |
| 468 | * ----------------------------------------------------------- */ |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 469 | |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 470 | /** Structure containing a raw command */ |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 471 | struct fuse_cmd; |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 472 | |
| 473 | /** Function type used to process commands */ |
Miklos Szeredi | f830a7f | 2001-11-16 17:46:45 +0000 | [diff] [blame] | 474 | typedef void (*fuse_processor_t)(struct fuse *, struct fuse_cmd *, void *); |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 475 | |
| 476 | /** This is the part of fuse_main() before the event loop */ |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 477 | struct fuse *fuse_setup(int argc, char *argv[], |
| 478 | const struct fuse_operations *op, size_t op_size, |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 479 | char **mountpoint, int *multithreaded, int *fd); |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 480 | |
| 481 | /** This is the part of fuse_main() after the event loop */ |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 482 | void fuse_teardown(struct fuse *fuse, int fd, char *mountpoint); |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 483 | |
| 484 | /** Read a single command. If none are read, return NULL */ |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 485 | struct fuse_cmd *fuse_read_cmd(struct fuse *f); |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 486 | |
| 487 | /** Process a single command */ |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 488 | void fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd); |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 489 | |
| 490 | /** Multi threaded event loop, which calls the custom command |
| 491 | processor function */ |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 492 | int fuse_loop_mt_proc(struct fuse *f, fuse_processor_t proc, void *data); |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 493 | |
| 494 | /** Return the exited flag, which indicates if fuse_exit() has been |
| 495 | called */ |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 496 | int fuse_exited(struct fuse* f); |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 497 | |
| 498 | /** Set function which can be used to get the current context */ |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 499 | void fuse_set_getcontext_func(struct fuse_context *(*func)(void)); |
Miklos Szeredi | f830a7f | 2001-11-16 17:46:45 +0000 | [diff] [blame] | 500 | |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 501 | /* ----------------------------------------------------------- * |
| 502 | * Compatibility stuff * |
| 503 | * ----------------------------------------------------------- */ |
| 504 | |
| 505 | #if FUSE_USE_VERSION == 21 || FUSE_USE_VERSION == 11 |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 506 | # include "fuse_compat.h" |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 507 | # define fuse_dirfil_t fuse_dirfil_t_compat |
| 508 | # define __fuse_read_cmd fuse_read_cmd |
| 509 | # define __fuse_process_cmd fuse_process_cmd |
| 510 | # define __fuse_loop_mt fuse_loop_mt_proc |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 511 | # undef fuse_main |
| 512 | # undef FUSE_MINOR_VERSION |
| 513 | # undef FUSE_MAJOR_VERSION |
| 514 | # if FUSE_USE_VERSION == 21 |
| 515 | # define FUSE_MAJOR_VERSION 2 |
| 516 | # define FUSE_MINOR_VERSION 1 |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 517 | # define fuse_operations fuse_operations_compat2 |
| 518 | # define fuse_main fuse_main_compat2 |
| 519 | # define fuse_new fuse_new_compat2 |
| 520 | # define __fuse_setup fuse_setup_compat2 |
| 521 | # define __fuse_teardown fuse_teardown |
| 522 | # define __fuse_exited fuse_exited |
| 523 | # define __fuse_set_getcontext_func fuse_set_getcontext_func |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 524 | # else |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 525 | # define FUSE_MAJOR_VERSION 1 |
| 526 | # define FUSE_MINOR_VERSION 1 |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 527 | # define fuse_statfs fuse_statfs_compat1 |
| 528 | # define fuse_operations fuse_operations_compat1 |
| 529 | # define fuse_main fuse_main_compat1 |
| 530 | # define fuse_new fuse_new_compat1 |
| 531 | # define fuse_mount fuse_mount_compat1 |
| 532 | # define FUSE_DEBUG FUSE_DEBUG_COMPAT1 |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 533 | # endif |
Miklos Szeredi | 799993c | 2004-12-04 21:20:05 +0000 | [diff] [blame] | 534 | #elif FUSE_USE_VERSION < 22 |
| 535 | # error Compatibility with API version other than 21 and 11 not supported |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 536 | #endif |
| 537 | |
Miklos Szeredi | acd4e06 | 2001-12-08 20:29:20 +0000 | [diff] [blame] | 538 | #ifdef __cplusplus |
| 539 | } |
| 540 | #endif |
| 541 | |
| 542 | #endif /* _FUSE_H_ */ |