blob: 8a0fd3ccd386129b2202e7fbb59b58c2722e1a17 [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 Szeredi2f3d9402003-12-15 12:11:33 +000014/** Major version of FUSE library interface */
Miklos Szeredi18e75e42004-02-19 14:23:27 +000015#define FUSE_MAJOR_VERSION 2
Miklos Szeredi2f3d9402003-12-15 12:11:33 +000016
17/** Minor version of FUSE library interface */
Miklos Szeredi5dc8a802004-10-21 09:35:10 +000018#define FUSE_MINOR_VERSION 1
Miklos Szeredi2f3d9402003-12-15 12:11:33 +000019
Miklos Szeredi87f30a92004-04-13 10:49:54 +000020/* 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 Szeredi5f054812002-12-03 18:45:21 +000024
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000025#include <sys/types.h>
26#include <sys/stat.h>
Miklos Szeredi18e75e42004-02-19 14:23:27 +000027#include <sys/statfs.h>
Miklos Szeredi5e183482001-10-31 14:52:35 +000028#include <utime.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000029
Miklos Szeredi8fb48fe2004-11-08 14:48:52 +000030#ifdef __cplusplus
31extern "C" {
32#endif
33
Miklos Szeredicc8c9752001-11-21 10:03:39 +000034/* ----------------------------------------------------------- *
35 * Basic FUSE API *
36 * ----------------------------------------------------------- */
37
Miklos Szeredi2df1c042001-11-06 15:07:17 +000038/** Handle for a FUSE filesystem */
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000039struct fuse;
Miklos Szeredi2df1c042001-11-06 15:07:17 +000040
41/** Handle for a getdir() operation */
Miklos Szeredia181e612001-11-06 12:03:23 +000042typedef struct fuse_dirhandle *fuse_dirh_t;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000043
Miklos Szeredif08ace02003-10-22 11:11:57 +000044/** Function to add an entry in a getdir() operation
45 *
46 * @param h the handle passed to the getdir() operation
47 * @param name the file name of the directory entry
48 * @param type the file type (0 if unknown) see <dirent.h>
Miklos Szeredi8fb48fe2004-11-08 14:48:52 +000049 * @param ino the inode number, ignored if "use_ino" mount option is
50 * not specified
Miklos Szeredif08ace02003-10-22 11:11:57 +000051 * @return 0 on success, -errno on error
52 */
Miklos Szeredi8fb48fe2004-11-08 14:48:52 +000053typedef int (*fuse_dirfil2_t) (fuse_dirh_t h, const char *name, int type,
54 ino_t ino);
55
56/** Obsolete version of the above function */
Miklos Szeredif08ace02003-10-22 11:11:57 +000057typedef int (*fuse_dirfil_t) (fuse_dirh_t h, const char *name, int type);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000058
Miklos Szeredi2df1c042001-11-06 15:07:17 +000059/**
60 * The file system operations:
61 *
62 * Most of these should work very similarly to the well known UNIX
63 * file system operations. Exceptions are:
64 *
Miklos Szeredi2df1c042001-11-06 15:07:17 +000065 * - All operations should return the negated error value (-errno) on
66 * error.
67 *
Miklos Szeredi05033042001-11-13 16:11:35 +000068 * - Getattr() doesn't need to fill in the following fields:
69 * st_ino
70 * st_dev
71 * st_blksize
72 *
Miklos Szeredi0a7077f2001-11-11 18:20:17 +000073 * - readlink() should fill the buffer with a null terminated string. The
74 * buffer size argument includes the space for the terminating null
75 * character. If the linkname is too long to fit in the buffer, it should
76 * be truncated. The return value should be 0 for success.
Miklos Szeredi2df1c042001-11-06 15:07:17 +000077 *
78 * - getdir() is the opendir(), readdir(), ..., closedir() sequence
79 * in one call. For each directory entry the filldir parameter should
80 * be called.
81 *
82 * - There is no create() operation, mknod() will be called for
83 * creation of all non directory, non symlink nodes.
84 *
85 * - open() should not return a filehandle, but 0 on success. No
86 * creation, or trunctation flags (O_CREAT, O_EXCL, O_TRUNC) will be
87 * passed to open(). Open should only check if the operation is
88 * permitted for the given flags.
89 *
90 * - read(), write() are not passed a filehandle, but rather a
91 * pathname. The offset of the read and write is passed as the last
Miklos Szeredi307242f2004-01-26 11:28:44 +000092 * argument, like the pread() and pwrite() system calls. (NOTE:
93 * read() should always return the number of bytes requested, except
94 * at end of file)
Miklos Szeredic8ba2372002-12-10 12:26:00 +000095 *
96 * - release() is called when an open file has:
97 * 1) all file descriptors closed
98 * 2) all memory mappings unmapped
Miklos Szeredie2e4ac22004-05-18 08:45:28 +000099 * For every open() call there will be exactly one release() call
100 * with the same flags. It is possible to have a file opened more
101 * than once, in which case only the last release will mean, that no
102 * more reads/writes will happen on the file. The return value of
Miklos Szeredibd7661b2004-07-23 17:16:29 +0000103 * release is ignored. Implementing this method is optional.
Miklos Szeredie2e4ac22004-05-18 08:45:28 +0000104 *
105 * - flush() is called when close() has been called on an open file.
106 * NOTE: this does not mean that the file is released (e.g. after
107 * fork() an open file will have two references which both must be
Miklos Szeredibd7661b2004-07-23 17:16:29 +0000108 * closed before the file is released). The flush() method may be
Miklos Szeredie2e4ac22004-05-18 08:45:28 +0000109 * called more than once for each open(). The return value of
110 * flush() is passed on to the close() system call. Implementing
Miklos Szeredibd7661b2004-07-23 17:16:29 +0000111 * this method is optional.
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +0000112 *
113 * - fsync() has a boolean 'datasync' parameter which if TRUE then do
Miklos Szeredibd7661b2004-07-23 17:16:29 +0000114 * an fdatasync() operation. Implementing this method is optional.
Miklos Szeredie2e4ac22004-05-18 08:45:28 +0000115 */
Miklos Szeredia181e612001-11-06 12:03:23 +0000116struct fuse_operations {
Miklos Szeredi3ed84232004-03-30 15:17:26 +0000117 int (*getattr) (const char *, struct stat *);
118 int (*readlink) (const char *, char *, size_t);
Miklos Szeredi8fb48fe2004-11-08 14:48:52 +0000119 int (*getdir) (const char *, fuse_dirh_t, fuse_dirfil2_t);
Miklos Szeredi3ed84232004-03-30 15:17:26 +0000120 int (*mknod) (const char *, mode_t, dev_t);
121 int (*mkdir) (const char *, mode_t);
122 int (*unlink) (const char *);
123 int (*rmdir) (const char *);
124 int (*symlink) (const char *, const char *);
125 int (*rename) (const char *, const char *);
126 int (*link) (const char *, const char *);
127 int (*chmod) (const char *, mode_t);
128 int (*chown) (const char *, uid_t, gid_t);
129 int (*truncate) (const char *, off_t);
130 int (*utime) (const char *, struct utimbuf *);
131 int (*open) (const char *, int);
132 int (*read) (const char *, char *, size_t, off_t);
133 int (*write) (const char *, const char *, size_t, off_t);
134 int (*statfs) (const char *, struct statfs *);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +0000135 int (*flush) (const char *);
Miklos Szeredi3ed84232004-03-30 15:17:26 +0000136 int (*release) (const char *, int);
137 int (*fsync) (const char *, int);
138 int (*setxattr) (const char *, const char *, const char *, size_t, int);
139 int (*getxattr) (const char *, const char *, char *, size_t);
140 int (*listxattr) (const char *, char *, size_t);
141 int (*removexattr) (const char *, const char *);
Miklos Szeredia181e612001-11-06 12:03:23 +0000142};
143
Miklos Szeredi2e50d432001-12-20 12:17:25 +0000144/** Extra context that may be needed by some filesystems */
145struct fuse_context {
Miklos Szeredid169f312004-09-22 08:48:26 +0000146 struct fuse *fuse;
Miklos Szeredi2e50d432001-12-20 12:17:25 +0000147 uid_t uid;
148 gid_t gid;
Miklos Szeredi1f18db52004-09-27 06:54:49 +0000149 pid_t pid;
Miklos Szeredi8fb48fe2004-11-08 14:48:52 +0000150 void *private_data;
Miklos Szeredi2e50d432001-12-20 12:17:25 +0000151};
152
Miklos Szeredia4b0c772002-04-19 15:57:02 +0000153/*
Miklos Szeredi0e535082003-10-13 10:08:06 +0000154 * Main function of FUSE.
155 *
156 * This is for the lazy. This is all that has to be called from the
157 * main() function.
158 *
159 * This function does the following:
Miklos Szeredi307242f2004-01-26 11:28:44 +0000160 * - parses command line options (-d -s and -h)
Miklos Szeredibd7661b2004-07-23 17:16:29 +0000161 * - passes relevant mount options to the fuse_mount()
Miklos Szeredi0e535082003-10-13 10:08:06 +0000162 * - installs signal handlers for INT, HUP, TERM and PIPE
163 * - registers an exit handler to unmount the filesystem on program exit
Miklos Szeredi0e535082003-10-13 10:08:06 +0000164 * - creates a fuse handle
165 * - registers the operations
166 * - calls either the single-threaded or the multi-threaded event loop
167 *
168 * @param argc the argument counter passed to the main() function
169 * @param argv the argument vector passed to the main() function
170 * @param op the file system operation
Miklos Szeredi5dc8a802004-10-21 09:35:10 +0000171 * @return 0 on success, nonzero on failure
Miklos Szeredi0e535082003-10-13 10:08:06 +0000172 */
Miklos Szeredi5dc8a802004-10-21 09:35:10 +0000173int fuse_main(int argc, char *argv[], const struct fuse_operations *op);
Miklos Szeredi891b8742004-07-29 09:27:49 +0000174
Miklos Szeredi0e535082003-10-13 10:08:06 +0000175/* ----------------------------------------------------------- *
176 * More detailed API *
177 * ----------------------------------------------------------- */
178
179/*
Miklos Szeredia4b0c772002-04-19 15:57:02 +0000180 * Create a FUSE mountpoint
181 *
182 * Returns a control file descriptor suitable for passing to
183 * fuse_new()
184 *
185 * @param mountpoint the mount point path
Miklos Szeredibd7661b2004-07-23 17:16:29 +0000186 * @param opts a comma separated list of mount options. Can be NULL.
Miklos Szeredia4b0c772002-04-19 15:57:02 +0000187 * @return the control file descriptor on success, -1 on failure
188 */
Miklos Szeredibd7661b2004-07-23 17:16:29 +0000189int fuse_mount(const char *mountpoint, const char *opts);
Miklos Szeredia4b0c772002-04-19 15:57:02 +0000190
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000191/*
192 * Umount a FUSE mountpoint
193 *
194 * @param mountpoint the mount point path
195 */
196void fuse_unmount(const char *mountpoint);
197
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000198/**
Miklos Szeredi8cffdb92001-11-09 14:49:18 +0000199 * Create a new FUSE filesystem.
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000200 *
Miklos Szeredi8cffdb92001-11-09 14:49:18 +0000201 * @param fd the control file descriptor
Miklos Szeredibd7661b2004-07-23 17:16:29 +0000202 * @param opts mount options to be used by the library
Miklos Szeredi2e50d432001-12-20 12:17:25 +0000203 * @param op the operations
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000204 * @return the created FUSE handle
205 */
Miklos Szeredibd7661b2004-07-23 17:16:29 +0000206struct fuse *fuse_new(int fd, const char *opts,
207 const struct fuse_operations *op);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000208
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000209/**
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000210 * Destroy the FUSE handle.
211 *
212 * The filesystem is not unmounted.
213 *
214 * @param f the FUSE handle
215 */
216void fuse_destroy(struct fuse *f);
217
218/**
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000219 * FUSE event loop.
220 *
221 * Requests from the kernel are processed, and the apropriate
222 * operations are called.
223 *
224 * @param f the FUSE handle
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000225 * @return 0 if no error occured, -1 otherwise
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000226 */
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000227int fuse_loop(struct fuse *f);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000228
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000229/**
230 * Exit from event loop
231 *
232 * @param f the FUSE handle
233 */
234void fuse_exit(struct fuse *f);
235
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000236/**
Miklos Szeredifff56ab2001-11-16 10:12:59 +0000237 * FUSE event loop with multiple threads
238 *
239 * Requests from the kernel are processed, and the apropriate
240 * operations are called. Request are processed in parallel by
241 * distributing them between multiple threads.
242 *
243 * Calling this function requires the pthreads library to be linked to
244 * the application.
245 *
246 * @param f the FUSE handle
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000247 * @return 0 if no error occured, -1 otherwise
Miklos Szeredifff56ab2001-11-16 10:12:59 +0000248 */
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000249int fuse_loop_mt(struct fuse *f);
Miklos Szeredifff56ab2001-11-16 10:12:59 +0000250
251/**
Miklos Szeredi2e50d432001-12-20 12:17:25 +0000252 * Get the current context
253 *
254 * The context is only valid for the duration of a filesystem
255 * operation, and thus must not be stored and used later.
256 *
257 * @param f the FUSE handle
258 * @return the context
259 */
Miklos Szeredid169f312004-09-22 08:48:26 +0000260struct fuse_context *fuse_get_context(void);
Miklos Szeredi2e50d432001-12-20 12:17:25 +0000261
Miklos Szeredibd7661b2004-07-23 17:16:29 +0000262/**
Miklos Szeredi5dc8a802004-10-21 09:35:10 +0000263 * Invalidate cached data of a file.
264 *
265 * Useful if the 'kernel_cache' mount option is given, since in that
266 * case the cache is not invalidated on file open.
267 *
268 * @return 0 on success or -errno on failure
269 */
270int fuse_invalidate(struct fuse *f, const char *path);
271
272/**
Miklos Szeredibd7661b2004-07-23 17:16:29 +0000273 * Check whether a mount option should be passed to the kernel or the
274 * library
275 *
276 * @param opt the option to check
277 * @return 1 if it is a library option, 0 otherwise
278 */
279int fuse_is_lib_option(const char *opt);
280
Miklos Szeredicc8c9752001-11-21 10:03:39 +0000281/* ----------------------------------------------------------- *
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000282 * Advanced API for event handling, don't worry about this... *
283 * ----------------------------------------------------------- */
Miklos Szeredifff56ab2001-11-16 10:12:59 +0000284
285struct fuse_cmd;
Miklos Szeredif830a7f2001-11-16 17:46:45 +0000286typedef void (*fuse_processor_t)(struct fuse *, struct fuse_cmd *, void *);
Miklos Szeredi5dc8a802004-10-21 09:35:10 +0000287struct fuse *__fuse_setup(int argc, char *argv[],
288 const struct fuse_operations *op,
289 char **mountpoint, int *multithreaded, int *fd);
290void __fuse_teardown(struct fuse *fuse, int fd, char *mountpoint);
Miklos Szeredifff56ab2001-11-16 10:12:59 +0000291struct fuse_cmd *__fuse_read_cmd(struct fuse *f);
Miklos Szeredifff56ab2001-11-16 10:12:59 +0000292void __fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000293int __fuse_loop_mt(struct fuse *f, fuse_processor_t proc, void *data);
Miklos Szeredi65cf7c72004-06-30 11:34:56 +0000294int __fuse_exited(struct fuse* f);
Miklos Szeredid169f312004-09-22 08:48:26 +0000295void __fuse_set_getcontext_func(struct fuse_context *(*func)(void));
Miklos Szeredif830a7f2001-11-16 17:46:45 +0000296
Miklos Szerediacd4e062001-12-08 20:29:20 +0000297#ifdef __cplusplus
298}
299#endif
300
301#endif /* _FUSE_H_ */