blob: 076b03078760f661ab90ac96eebd7e39e8911f5d [file] [log] [blame]
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi2e6b6f22004-07-07 19:19:53 +00003 Copyright (C) 2001-2004 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00004
Miklos Szeredi8b39a9f2002-10-25 12:41:16 +00005 This program can be distributed under the terms of the GNU LGPL.
6 See the file COPYING.LIB.
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00007*/
8
Miklos Szerediacd4e062001-12-08 20:29:20 +00009#ifndef _FUSE_H_
10#define _FUSE_H_
11
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000012/* This file defines the library interface of FUSE */
13
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +000014/* 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 Szeredid59bb9d2004-12-07 10:04:24 +000016 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 Szeredi0b6a0ad2004-12-04 00:40:50 +000018
19#ifndef FUSE_USE_VERSION
20#define FUSE_USE_VERSION 21
21#endif
22
Miklos Szeredi2f3d9402003-12-15 12:11:33 +000023/** Major version of FUSE library interface */
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +000024#define FUSE_MAJOR_VERSION 2
Miklos Szeredi2f3d9402003-12-15 12:11:33 +000025
26/** Minor version of FUSE library interface */
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +000027#define FUSE_MINOR_VERSION 2
Miklos Szeredi2f3d9402003-12-15 12:11:33 +000028
Miklos Szeredi87f30a92004-04-13 10:49:54 +000029/* 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 Szeredi5f054812002-12-03 18:45:21 +000033
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000034#include <sys/types.h>
35#include <sys/stat.h>
Miklos Szeredi18e75e42004-02-19 14:23:27 +000036#include <sys/statfs.h>
Miklos Szeredi5e183482001-10-31 14:52:35 +000037#include <utime.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000038
Miklos Szeredi8fb48fe2004-11-08 14:48:52 +000039#ifdef __cplusplus
40extern "C" {
41#endif
42
Miklos Szeredicc8c9752001-11-21 10:03:39 +000043/* ----------------------------------------------------------- *
44 * Basic FUSE API *
45 * ----------------------------------------------------------- */
46
Miklos Szeredi2df1c042001-11-06 15:07:17 +000047/** Handle for a FUSE filesystem */
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000048struct fuse;
Miklos Szeredi2df1c042001-11-06 15:07:17 +000049
50/** Handle for a getdir() operation */
Miklos Szeredia181e612001-11-06 12:03:23 +000051typedef struct fuse_dirhandle *fuse_dirh_t;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000052
Miklos Szeredif08ace02003-10-22 11:11:57 +000053/** Function to add an entry in a getdir() operation
54 *
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 Szeredi8fb48fe2004-11-08 14:48:52 +000058 * @param ino the inode number, ignored if "use_ino" mount option is
59 * not specified
Miklos Szeredif08ace02003-10-22 11:11:57 +000060 * @return 0 on success, -errno on error
61 */
Miklos Szeredid507c732004-11-08 17:32:25 +000062typedef int (*fuse_dirfil_t) (fuse_dirh_t h, const char *name, int type,
63 ino_t ino);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000064
Miklos Szeredifb28c5e2004-11-26 12:15:06 +000065/** Information about open files */
66struct fuse_file_info {
67 /** Open flags. Available in open() and release() */
68 int flags;
69
70 /** File handle. May be filled in by filesystem in open().
71 Available in all other file operations */
72 unsigned long fh;
Miklos Szeredid59bb9d2004-12-07 10:04:24 +000073
74 /** In case of a write operation indicates if this was caused by a
75 writepage */
76 int writepage;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +000077};
78
Miklos Szeredi2df1c042001-11-06 15:07:17 +000079/**
80 * The file system operations:
81 *
82 * Most of these should work very similarly to the well known UNIX
Miklos Szeredid59bb9d2004-12-07 10:04:24 +000083 * file system operations. A major exception is that instead of
84 * returning an error in 'errno', the operation should return the
85 * negated error value (-errno) directly.
Miklos Szeredie2e4ac22004-05-18 08:45:28 +000086 */
Miklos Szeredia181e612001-11-06 12:03:23 +000087struct fuse_operations {
Miklos Szeredid59bb9d2004-12-07 10:04:24 +000088 /** Get file attributes.
89 *
90 * Similar to stat(). The 'st_dev' and 'st_blksize' fields are
91 * ignored. The 'st_ino' field is ignored except if the 'use_ino'
92 * mount option is given.
93 */
94 int (*getattr) (const char *, struct stat *);
95
96 /** Read the target of a symbolic link
97 *
98 * The buffer should be filled with a null terminated string. The
99 * buffer size argument includes the space for the terminating
100 * null character. If the linkname is too long to fit in the
101 * buffer, it should be truncated. The return value should be 0
102 * for success.
103 */
104 int (*readlink) (const char *, char *, size_t);
105
106 /** Read the contents of a directory
107 *
108 * This operation is the opendir(), readdir(), ..., closedir()
109 * sequence in one call. For each directory entry the filldir
110 * function should be called.
111 */
112 int (*getdir) (const char *, fuse_dirh_t, fuse_dirfil_t);
113
114 /** Create a file node
115 *
116 * There is no create() operation, mknod() will be called for
117 * creation of all non-directory, non-symlink nodes.
118 */
119 int (*mknod) (const char *, mode_t, dev_t);
120
121 /** Create a directory */
122 int (*mkdir) (const char *, mode_t);
123
124 /** Remove a file */
125 int (*unlink) (const char *);
126
127 /** Remove a directory */
128 int (*rmdir) (const char *);
129
130 /** Create a symbolic link */
131 int (*symlink) (const char *, const char *);
132
133 /** Rename a file */
134 int (*rename) (const char *, const char *);
135
136 /** Create a hard link to a file */
137 int (*link) (const char *, const char *);
138
139 /** Change the permission bits of a file */
140 int (*chmod) (const char *, mode_t);
141
142 /** Change the owner and group of a file */
143 int (*chown) (const char *, uid_t, gid_t);
144
145 /** Change the size of a file */
146 int (*truncate) (const char *, off_t);
147
148 /** Change the access and/or modification times of a file */
149 int (*utime) (const char *, struct utimbuf *);
150
151 /** File open operation
152 *
153 * No creation, or trunctation flags (O_CREAT, O_EXCL, O_TRUNC)
154 * will be passed to open(). Open should check if the operation
155 * is permitted for the given flags. Optionally open may also
156 * return an arbitary filehandle in the fuse_file_info structure,
157 * which will be passed to all file operations.
158 */
159 int (*open) (const char *, struct fuse_file_info *);
160
161 /** Read data from an open file
162 *
163 * Read should return exactly the number of bytes requested except
164 * on EOF or error, otherwise the rest of the data will be
165 * substituted with zeroes. An exception to this is when the
166 * 'direct_io' mount option is specified, in which case the return
167 * value of the read system call will reflect the return value of
168 * this operation.
169 */
170 int (*read) (const char *, char *, size_t, off_t, struct fuse_file_info *);
171
172 /** Write data to an open file
173 *
174 * Write should return exactly the number of bytes requested
175 * except on error. An exception to this is when the 'direct_io'
176 * mount option is specified (see read operation).
177 */
178 int (*write) (const char *, const char *, size_t, off_t,
179 struct fuse_file_info *);
180
181 /** Get file system statistics
182 *
183 * The 'f_type' and 'f_fsid' fields are ignored
184 */
185 int (*statfs) (const char *, struct statfs *);
186
187 /** Possibly flush cached data
188 *
189 * BIG NOTE: This is not equivalent to fsync(). It's not a
190 * request to sync dirty data, and can safely be ignored.
191 *
192 * Flush is called on each close() of a file descriptor. So if a
193 * filesystem wants to return write errors in close() and the file
194 * has cached dirty data, this is a good place to write back data
195 * and return any errors.
196 *
197 * NOTE: The flush() method may be called more than once for each
198 * open(). This happens if more than one file descriptor refers
199 * to an opened file due to dup(), dup2() or fork() calls. It is
200 * not possible to determine if a flush is final, so each flush
201 * should be treated equally. Multiple write-flush sequences are
202 * relatively rare, so this shouldn't be a problem.
203 */
204 int (*flush) (const char *, struct fuse_file_info *);
205
206 /** Release an open file
207 *
208 * Release is called when there are no more references to an open
209 * file: all file descriptors are closed and all memory mappings
210 * are unmapped.
211 *
212 * For every open() call there will be exactly one release() call
213 * with the same flags. It is possible to have a file opened more
214 * than once, in which case only the last release will mean, that
215 * no more reads/writes will happen on the file. The return value
216 * of release is ignored. Implementing this method is optional.
217 */
218 int (*release) (const char *, struct fuse_file_info *);
219
220 /** Synchronize file contents
221 *
222 * If the datasync parameter is non-zero, then only the user data
223 * should be flushed, not the meta data.
224 */
225 int (*fsync) (const char *, int, struct fuse_file_info *);
226
227 /** Set extended attributes */
228 int (*setxattr) (const char *, const char *, const char *, size_t, int);
229
230 /** Get extended attributes */
231 int (*getxattr) (const char *, const char *, char *, size_t);
232
233 /** List extended attributes */
234 int (*listxattr) (const char *, char *, size_t);
235
236 /** Remove extended attributes */
Miklos Szeredi3ed84232004-03-30 15:17:26 +0000237 int (*removexattr) (const char *, const char *);
Miklos Szeredia181e612001-11-06 12:03:23 +0000238};
239
Miklos Szeredid59bb9d2004-12-07 10:04:24 +0000240/** Extra context that may be needed by some filesystems
241 *
242 * The uid, gid and pid fields are not filled in case of a writepage
243 * operation.
244 */
Miklos Szeredi2e50d432001-12-20 12:17:25 +0000245struct fuse_context {
Miklos Szeredid59bb9d2004-12-07 10:04:24 +0000246 /** Pointer to the fuse object */
Miklos Szeredid169f312004-09-22 08:48:26 +0000247 struct fuse *fuse;
Miklos Szeredid59bb9d2004-12-07 10:04:24 +0000248
249 /** User ID of the calling process */
Miklos Szeredi2e50d432001-12-20 12:17:25 +0000250 uid_t uid;
Miklos Szeredid59bb9d2004-12-07 10:04:24 +0000251
252 /** Group ID of the calling process */
Miklos Szeredi2e50d432001-12-20 12:17:25 +0000253 gid_t gid;
Miklos Szeredid59bb9d2004-12-07 10:04:24 +0000254
255 /** Thread ID of the calling process */
Miklos Szeredi1f18db52004-09-27 06:54:49 +0000256 pid_t pid;
Miklos Szeredid59bb9d2004-12-07 10:04:24 +0000257
258 /** Currently unused */
Miklos Szeredi8fb48fe2004-11-08 14:48:52 +0000259 void *private_data;
Miklos Szeredi2e50d432001-12-20 12:17:25 +0000260};
261
Miklos Szeredia4b0c772002-04-19 15:57:02 +0000262/*
Miklos Szeredi0e535082003-10-13 10:08:06 +0000263 * Main function of FUSE.
264 *
265 * This is for the lazy. This is all that has to be called from the
266 * main() function.
267 *
268 * This function does the following:
Miklos Szeredi307242f2004-01-26 11:28:44 +0000269 * - parses command line options (-d -s and -h)
Miklos Szeredibd7661b2004-07-23 17:16:29 +0000270 * - passes relevant mount options to the fuse_mount()
Miklos Szeredi0e535082003-10-13 10:08:06 +0000271 * - installs signal handlers for INT, HUP, TERM and PIPE
272 * - registers an exit handler to unmount the filesystem on program exit
Miklos Szeredi0e535082003-10-13 10:08:06 +0000273 * - creates a fuse handle
274 * - registers the operations
275 * - calls either the single-threaded or the multi-threaded event loop
276 *
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +0000277 * Note: this is currently implemented as a macro.
278 *
Miklos Szeredi0e535082003-10-13 10:08:06 +0000279 * @param argc the argument counter passed to the main() function
280 * @param argv the argument vector passed to the main() function
281 * @param op the file system operation
Miklos Szeredi5dc8a802004-10-21 09:35:10 +0000282 * @return 0 on success, nonzero on failure
Miklos Szeredi0e535082003-10-13 10:08:06 +0000283 */
Miklos Szeredid59bb9d2004-12-07 10:04:24 +0000284/*
285int fuse_main(int argc, char *argv[], const struct fuse_operations *op);
286*/
287#define fuse_main(argc, argv, op) \
288 __fuse_main(argc, argv, op, sizeof(*(op)))
Miklos Szeredi891b8742004-07-29 09:27:49 +0000289
Miklos Szeredi0e535082003-10-13 10:08:06 +0000290/* ----------------------------------------------------------- *
291 * More detailed API *
292 * ----------------------------------------------------------- */
293
294/*
Miklos Szeredia4b0c772002-04-19 15:57:02 +0000295 * Create a FUSE mountpoint
296 *
297 * Returns a control file descriptor suitable for passing to
298 * fuse_new()
299 *
300 * @param mountpoint the mount point path
Miklos Szeredibd7661b2004-07-23 17:16:29 +0000301 * @param opts a comma separated list of mount options. Can be NULL.
Miklos Szeredia4b0c772002-04-19 15:57:02 +0000302 * @return the control file descriptor on success, -1 on failure
303 */
Miklos Szeredibd7661b2004-07-23 17:16:29 +0000304int fuse_mount(const char *mountpoint, const char *opts);
Miklos Szeredia4b0c772002-04-19 15:57:02 +0000305
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000306/*
307 * Umount a FUSE mountpoint
308 *
309 * @param mountpoint the mount point path
310 */
311void fuse_unmount(const char *mountpoint);
312
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000313/**
Miklos Szeredi8cffdb92001-11-09 14:49:18 +0000314 * Create a new FUSE filesystem.
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000315 *
Miklos Szeredi8cffdb92001-11-09 14:49:18 +0000316 * @param fd the control file descriptor
Miklos Szeredibd7661b2004-07-23 17:16:29 +0000317 * @param opts mount options to be used by the library
Miklos Szeredi2e50d432001-12-20 12:17:25 +0000318 * @param op the operations
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +0000319 * @param op_size the size of the fuse_operations structure
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000320 * @return the created FUSE handle
321 */
Miklos Szeredibd7661b2004-07-23 17:16:29 +0000322struct fuse *fuse_new(int fd, const char *opts,
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +0000323 const struct fuse_operations *op, size_t op_size);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000324
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000325/**
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000326 * Destroy the FUSE handle.
327 *
328 * The filesystem is not unmounted.
329 *
330 * @param f the FUSE handle
331 */
332void fuse_destroy(struct fuse *f);
333
334/**
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000335 * FUSE event loop.
336 *
337 * Requests from the kernel are processed, and the apropriate
338 * operations are called.
339 *
340 * @param f the FUSE handle
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000341 * @return 0 if no error occured, -1 otherwise
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000342 */
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000343int fuse_loop(struct fuse *f);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000344
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000345/**
346 * Exit from event loop
347 *
348 * @param f the FUSE handle
349 */
350void fuse_exit(struct fuse *f);
351
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000352/**
Miklos Szeredifff56ab2001-11-16 10:12:59 +0000353 * FUSE event loop with multiple threads
354 *
355 * Requests from the kernel are processed, and the apropriate
356 * operations are called. Request are processed in parallel by
357 * distributing them between multiple threads.
358 *
359 * Calling this function requires the pthreads library to be linked to
360 * the application.
361 *
362 * @param f the FUSE handle
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000363 * @return 0 if no error occured, -1 otherwise
Miklos Szeredifff56ab2001-11-16 10:12:59 +0000364 */
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000365int fuse_loop_mt(struct fuse *f);
Miklos Szeredifff56ab2001-11-16 10:12:59 +0000366
367/**
Miklos Szeredi2e50d432001-12-20 12:17:25 +0000368 * Get the current context
369 *
370 * The context is only valid for the duration of a filesystem
371 * operation, and thus must not be stored and used later.
372 *
373 * @param f the FUSE handle
374 * @return the context
375 */
Miklos Szeredid169f312004-09-22 08:48:26 +0000376struct fuse_context *fuse_get_context(void);
Miklos Szeredi2e50d432001-12-20 12:17:25 +0000377
Miklos Szeredibd7661b2004-07-23 17:16:29 +0000378/**
Miklos Szeredi5dc8a802004-10-21 09:35:10 +0000379 * Invalidate cached data of a file.
380 *
381 * Useful if the 'kernel_cache' mount option is given, since in that
382 * case the cache is not invalidated on file open.
383 *
384 * @return 0 on success or -errno on failure
385 */
386int fuse_invalidate(struct fuse *f, const char *path);
387
388/**
Miklos Szeredibd7661b2004-07-23 17:16:29 +0000389 * Check whether a mount option should be passed to the kernel or the
390 * library
391 *
392 * @param opt the option to check
393 * @return 1 if it is a library option, 0 otherwise
394 */
395int fuse_is_lib_option(const char *opt);
396
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +0000397/**
398 * The real main function
399 *
400 * Do not call this directly, use fuse_main()
401 */
402int __fuse_main(int argc, char *argv[], const struct fuse_operations *op,
403 size_t op_size);
404
Miklos Szeredicc8c9752001-11-21 10:03:39 +0000405/* ----------------------------------------------------------- *
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000406 * Advanced API for event handling, don't worry about this... *
407 * ----------------------------------------------------------- */
Miklos Szeredifff56ab2001-11-16 10:12:59 +0000408
Miklos Szeredid59bb9d2004-12-07 10:04:24 +0000409/** Structure containing a raw command */
Miklos Szeredifff56ab2001-11-16 10:12:59 +0000410struct fuse_cmd;
Miklos Szeredid59bb9d2004-12-07 10:04:24 +0000411
412/** Function type used to process commands */
Miklos Szeredif830a7f2001-11-16 17:46:45 +0000413typedef void (*fuse_processor_t)(struct fuse *, struct fuse_cmd *, void *);
Miklos Szeredid59bb9d2004-12-07 10:04:24 +0000414
415/** This is the part of fuse_main() before the event loop */
Miklos Szeredi5dc8a802004-10-21 09:35:10 +0000416struct fuse *__fuse_setup(int argc, char *argv[],
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +0000417 const struct fuse_operations *op, size_t op_size,
Miklos Szeredi5dc8a802004-10-21 09:35:10 +0000418 char **mountpoint, int *multithreaded, int *fd);
Miklos Szeredid59bb9d2004-12-07 10:04:24 +0000419
420/** This is the part of fuse_main() after the event loop */
Miklos Szeredi5dc8a802004-10-21 09:35:10 +0000421void __fuse_teardown(struct fuse *fuse, int fd, char *mountpoint);
Miklos Szeredid59bb9d2004-12-07 10:04:24 +0000422
423/** Read a single command. If none are read, return NULL */
Miklos Szeredifff56ab2001-11-16 10:12:59 +0000424struct fuse_cmd *__fuse_read_cmd(struct fuse *f);
Miklos Szeredid59bb9d2004-12-07 10:04:24 +0000425
426/** Process a single command */
Miklos Szeredifff56ab2001-11-16 10:12:59 +0000427void __fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd);
Miklos Szeredid59bb9d2004-12-07 10:04:24 +0000428
429/** Multi threaded event loop, which calls the custom command
430 processor function */
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000431int __fuse_loop_mt(struct fuse *f, fuse_processor_t proc, void *data);
Miklos Szeredid59bb9d2004-12-07 10:04:24 +0000432
433/** Return the exited flag, which indicates if fuse_exit() has been
434 called */
Miklos Szeredi65cf7c72004-06-30 11:34:56 +0000435int __fuse_exited(struct fuse* f);
Miklos Szeredid59bb9d2004-12-07 10:04:24 +0000436
437/** Set function which can be used to get the current context */
Miklos Szeredid169f312004-09-22 08:48:26 +0000438void __fuse_set_getcontext_func(struct fuse_context *(*func)(void));
Miklos Szeredif830a7f2001-11-16 17:46:45 +0000439
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +0000440/* ----------------------------------------------------------- *
441 * Compatibility stuff *
442 * ----------------------------------------------------------- */
443
444#if FUSE_USE_VERSION == 21 || FUSE_USE_VERSION == 11
445# include <fuse_compat.h>
446# define fuse_dirfil_t _fuse_dirfil_t_compat
447# undef fuse_main
448# undef FUSE_MINOR_VERSION
449# undef FUSE_MAJOR_VERSION
450# if FUSE_USE_VERSION == 21
451# define FUSE_MAJOR_VERSION 2
452# define FUSE_MINOR_VERSION 1
453# define fuse_operations _fuse_operations_compat2
454# define fuse_main _fuse_main_compat2
455# define fuse_new _fuse_new_compat2
456# define __fuse_setup _fuse_setup_compat2
457# else
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +0000458# define FUSE_MAJOR_VERSION 1
459# define FUSE_MINOR_VERSION 1
460# define fuse_statfs _fuse_statfs_compat1
461# define fuse_operations _fuse_operations_compat1
462# define fuse_main _fuse_main_compat1
463# define fuse_new _fuse_new_compat1
464# define fuse_mount _fuse_mount_compat1
465# define FUSE_DEBUG _FUSE_DEBUG_COMPAT1
466# endif
Miklos Szeredi799993c2004-12-04 21:20:05 +0000467#elif FUSE_USE_VERSION < 22
468# error Compatibility with API version other than 21 and 11 not supported
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +0000469#endif
470
Miklos Szerediacd4e062001-12-08 20:29:20 +0000471#ifdef __cplusplus
472}
473#endif
474
475#endif /* _FUSE_H_ */