blob: 91c749fa4989c04c63a9d7327d53e1cb5174427a [file] [log] [blame]
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi149f6072005-01-10 12:29:28 +00003 Copyright (C) 2001-2005 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
9#include "fuse_i.h"
Miklos Szeredi0f62d722005-01-04 12:45:54 +000010#include "fuse_compat.h"
Miklos Szeredib9b94cd2004-12-01 18:56:39 +000011#include "fuse_kernel.h"
Miklos Szeredi30e093a2005-04-03 17:44:54 +000012#include "fuse_kernel_compat5.h"
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000013
Miklos Szeredi0f62d722005-01-04 12:45:54 +000014#include <stdio.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000015#include <string.h>
Miklos Szeredi97c61e92001-11-07 12:09:43 +000016#include <stdlib.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000017#include <unistd.h>
Miklos Szeredi97c61e92001-11-07 12:09:43 +000018#include <limits.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000019#include <errno.h>
Miklos Szeredi0f62d722005-01-04 12:45:54 +000020#include <assert.h>
21#include <stdint.h>
Miklos Szeredi019b4e92001-12-26 18:08:09 +000022#include <sys/param.h>
Miklos Szerediab974562005-04-07 15:40:21 +000023#include <sys/uio.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000024
Miklos Szeredi0f62d722005-01-04 12:45:54 +000025/* FUSE flags: */
26
27/** Enable debuging output */
28#define FUSE_DEBUG (1 << 1)
29
30/** If a file is removed but it's still open, don't hide the file but
31 remove it immediately */
32#define FUSE_HARD_REMOVE (1 << 2)
33
34/** Use st_ino field in getattr instead of generating inode numbers */
35#define FUSE_USE_INO (1 << 3)
36
Miklos Szeredi0111f9d2005-04-22 12:04:55 +000037/** Only allow root or the owner to access the filesystem */
38#define FUSE_ALLOW_ROOT (1 << 4)
39
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +000040#define FUSE_KERNEL_MINOR_VERSION_NEED 1
Miklos Szeredia25d4c22004-11-23 22:32:16 +000041#define FUSE_VERSION_FILE_OLD "/proc/fs/fuse/version"
Miklos Szeredi162bcbb2004-11-29 23:43:44 +000042#define FUSE_VERSION_FILE_NEW "/sys/fs/fuse/version"
Miklos Szeredia25d4c22004-11-23 22:32:16 +000043#define FUSE_DEV_OLD "/proc/fs/fuse/dev"
44
Miklos Szeredi97c61e92001-11-07 12:09:43 +000045#define FUSE_MAX_PATH 4096
Miklos Szeredi30e093a2005-04-03 17:44:54 +000046#define PARAM_T(inarg, type) (((char *)(inarg)) + sizeof(type))
47#define PARAM(inarg) PARAM_T(inarg, *(inarg))
48#define PARAM_COMPAT(f, inarg, type) \
49 ((f)->major == 5 ? PARAM_T(inarg, struct type ## _compat5) : PARAM(inarg))
50
51#define MEMBER_COMPAT(f, ptr, memb, type) \
52 ((f)->major == 5 ? &((struct type ## _compat5 *) (ptr))->memb : &ptr->memb)
53
54#define SIZEOF_COMPAT(f, type) \
55 ((f)->major == 5 ? sizeof(struct type ## _compat5) : sizeof(struct type))
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000056
Miklos Szeredi254d5ed2004-03-02 11:11:24 +000057#define ENTRY_REVALIDATE_TIME 1 /* sec */
58#define ATTR_REVALIDATE_TIME 1 /* sec */
59
Miklos Szeredi0f62d722005-01-04 12:45:54 +000060
61struct node {
62 struct node *name_next;
63 struct node *id_next;
64 nodeid_t nodeid;
65 unsigned int generation;
66 int refctr;
67 nodeid_t parent;
68 char *name;
69 uint64_t version;
Miklos Szeredi38009022005-05-08 19:47:22 +000070 uint64_t nlookup;
Miklos Szeredi0f62d722005-01-04 12:45:54 +000071 int open_count;
72 int is_hidden;
73};
74
75struct fuse_dirhandle {
Miklos Szerediab974562005-04-07 15:40:21 +000076 pthread_mutex_t lock;
Miklos Szeredi0f62d722005-01-04 12:45:54 +000077 struct fuse *fuse;
Miklos Szeredib92d9782005-02-07 16:10:49 +000078 unsigned char *contents;
Miklos Szerediab974562005-04-07 15:40:21 +000079 int allocated;
Miklos Szeredib92d9782005-02-07 16:10:49 +000080 unsigned len;
Miklos Szerediab974562005-04-07 15:40:21 +000081 unsigned needlen;
Miklos Szeredi3ead28e2005-01-18 21:23:41 +000082 int filled;
Miklos Szerediede1f7a2005-04-01 21:08:57 +000083 unsigned long fh;
Miklos Szerediab974562005-04-07 15:40:21 +000084 int error;
Miklos Szeredi0f62d722005-01-04 12:45:54 +000085};
86
87struct fuse_cmd {
88 char *buf;
89 size_t buflen;
90};
91
92
Miklos Szeredid169f312004-09-22 08:48:26 +000093static struct fuse_context *(*fuse_getcontext)(void) = NULL;
94
Miklos Szerediab974562005-04-07 15:40:21 +000095#ifndef USE_UCLIBC
96#define mutex_init(mut) pthread_mutex_init(mut, NULL)
97#else
98static void mutex_init(pthread_mutex_t mut)
99{
100 pthread_mutexattr_t attr;
101 pthread_mutexattr_init(&attr);
102 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
103 pthread_mutex_init(mut, &attr);
104 pthread_mutexattr_destroy(&attr);
105}
106#endif
107
Miklos Szeredic8ba2372002-12-10 12:26:00 +0000108static const char *opname(enum fuse_opcode opcode)
109{
Miklos Szeredie5183742005-02-02 11:14:04 +0000110 switch (opcode) {
Miklos Szeredi3ed84232004-03-30 15:17:26 +0000111 case FUSE_LOOKUP: return "LOOKUP";
112 case FUSE_FORGET: return "FORGET";
113 case FUSE_GETATTR: return "GETATTR";
114 case FUSE_SETATTR: return "SETATTR";
115 case FUSE_READLINK: return "READLINK";
116 case FUSE_SYMLINK: return "SYMLINK";
Miklos Szeredi3ed84232004-03-30 15:17:26 +0000117 case FUSE_MKNOD: return "MKNOD";
118 case FUSE_MKDIR: return "MKDIR";
119 case FUSE_UNLINK: return "UNLINK";
120 case FUSE_RMDIR: return "RMDIR";
121 case FUSE_RENAME: return "RENAME";
122 case FUSE_LINK: return "LINK";
123 case FUSE_OPEN: return "OPEN";
124 case FUSE_READ: return "READ";
125 case FUSE_WRITE: return "WRITE";
126 case FUSE_STATFS: return "STATFS";
Miklos Szeredi99f20742004-05-19 08:01:10 +0000127 case FUSE_FLUSH: return "FLUSH";
Miklos Szeredi3ed84232004-03-30 15:17:26 +0000128 case FUSE_RELEASE: return "RELEASE";
129 case FUSE_FSYNC: return "FSYNC";
130 case FUSE_SETXATTR: return "SETXATTR";
131 case FUSE_GETXATTR: return "GETXATTR";
132 case FUSE_LISTXATTR: return "LISTXATTR";
133 case FUSE_REMOVEXATTR: return "REMOVEXATTR";
Miklos Szeredi3f0005f2005-01-04 19:24:31 +0000134 case FUSE_INIT: return "INIT";
Miklos Szeredi3ead28e2005-01-18 21:23:41 +0000135 case FUSE_OPENDIR: return "OPENDIR";
136 case FUSE_READDIR: return "READDIR";
137 case FUSE_RELEASEDIR: return "RELEASEDIR";
Miklos Szeredi4283ee72005-03-21 12:09:04 +0000138 case FUSE_FSYNCDIR: return "FSYNCDIR";
Miklos Szeredi99f20742004-05-19 08:01:10 +0000139 default: return "???";
Miklos Szeredic8ba2372002-12-10 12:26:00 +0000140 }
141}
142
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000143static inline void fuse_dec_avail(struct fuse *f)
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000144{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000145 pthread_mutex_lock(&f->worker_lock);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000146 f->numavail --;
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000147 pthread_mutex_unlock(&f->worker_lock);
148}
149
150static inline void fuse_inc_avail(struct fuse *f)
151{
152 pthread_mutex_lock(&f->worker_lock);
153 f->numavail ++;
154 pthread_mutex_unlock(&f->worker_lock);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000155}
156
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000157static struct node *get_node_nocheck(struct fuse *f, nodeid_t nodeid)
Miklos Szeredia181e612001-11-06 12:03:23 +0000158{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000159 size_t hash = nodeid % f->id_table_size;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000160 struct node *node;
161
Miklos Szeredia13d9002004-11-02 17:32:03 +0000162 for (node = f->id_table[hash]; node != NULL; node = node->id_next)
163 if (node->nodeid == nodeid)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000164 return node;
Miklos Szeredie5183742005-02-02 11:14:04 +0000165
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000166 return NULL;
Miklos Szeredia181e612001-11-06 12:03:23 +0000167}
168
Miklos Szeredia13d9002004-11-02 17:32:03 +0000169static struct node *get_node(struct fuse *f, nodeid_t nodeid)
Miklos Szeredia181e612001-11-06 12:03:23 +0000170{
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000171 struct node *node = get_node_nocheck(f, nodeid);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000172 if (!node) {
173 fprintf(stderr, "fuse internal error: node %lu not found\n",
174 nodeid);
175 abort();
176 }
177 return node;
Miklos Szeredia181e612001-11-06 12:03:23 +0000178}
179
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000180static void free_node(struct node *node)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000181{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000182 free(node->name);
183 free(node);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000184}
185
Miklos Szeredia13d9002004-11-02 17:32:03 +0000186static void unhash_id(struct fuse *f, struct node *node)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000187{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000188 size_t hash = node->nodeid % f->id_table_size;
189 struct node **nodep = &f->id_table[hash];
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000190
Miklos Szeredie5183742005-02-02 11:14:04 +0000191 for (; *nodep != NULL; nodep = &(*nodep)->id_next)
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000192 if (*nodep == node) {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000193 *nodep = node->id_next;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000194 return;
195 }
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000196}
197
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000198static void hash_id(struct fuse *f, struct node *node)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000199{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000200 size_t hash = node->nodeid % f->id_table_size;
201 node->id_next = f->id_table[hash];
Miklos Szeredie5183742005-02-02 11:14:04 +0000202 f->id_table[hash] = node;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000203}
204
Miklos Szeredia13d9002004-11-02 17:32:03 +0000205static unsigned int name_hash(struct fuse *f, nodeid_t parent, const char *name)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000206{
207 unsigned int hash = *name;
208
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000209 if (hash)
210 for (name += 1; *name != '\0'; name++)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000211 hash = (hash << 5) - hash + *name;
212
213 return (hash + parent) % f->name_table_size;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000214}
215
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000216static void unref_node(struct fuse *f, struct node *node);
217
218static void unhash_name(struct fuse *f, struct node *node)
219{
220 if (node->name) {
221 size_t hash = name_hash(f, node->parent, node->name);
222 struct node **nodep = &f->name_table[hash];
Miklos Szeredie5183742005-02-02 11:14:04 +0000223
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000224 for (; *nodep != NULL; nodep = &(*nodep)->name_next)
225 if (*nodep == node) {
226 *nodep = node->name_next;
227 node->name_next = NULL;
228 unref_node(f, get_node(f, node->parent));
229 free(node->name);
230 node->name = NULL;
231 node->parent = 0;
232 return;
233 }
234 fprintf(stderr, "fuse internal error: unable to unhash node: %lu\n",
235 node->nodeid);
236 abort();
237 }
238}
239
240static int hash_name(struct fuse *f, struct node *node, nodeid_t parent,
241 const char *name)
242{
243 size_t hash = name_hash(f, parent, name);
244 node->name = strdup(name);
245 if (node->name == NULL)
246 return -1;
247
248 get_node(f, parent)->refctr ++;
249 node->parent = parent;
250 node->name_next = f->name_table[hash];
251 f->name_table[hash] = node;
252 return 0;
253}
254
255static void delete_node(struct fuse *f, struct node *node)
256{
Miklos Szeredi38009022005-05-08 19:47:22 +0000257 if (f->flags & FUSE_DEBUG) {
258 printf("delete: %lu\n", node->nodeid);
259 fflush(stdout);
260 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000261 assert(!node->name);
262 unhash_id(f, node);
263 free_node(node);
264}
265
266static void unref_node(struct fuse *f, struct node *node)
267{
268 assert(node->refctr > 0);
269 node->refctr --;
270 if (!node->refctr)
271 delete_node(f, node);
272}
273
274static nodeid_t next_id(struct fuse *f)
275{
276 do {
277 f->ctr++;
278 if (!f->ctr)
279 f->generation ++;
280 } while (f->ctr == 0 || get_node_nocheck(f, f->ctr) != NULL);
281 return f->ctr;
282}
283
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000284static struct node *lookup_node(struct fuse *f, nodeid_t parent,
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000285 const char *name)
286{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000287 size_t hash = name_hash(f, parent, name);
288 struct node *node;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000289
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000290 for (node = f->name_table[hash]; node != NULL; node = node->name_next)
291 if (node->parent == parent && strcmp(node->name, name) == 0)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000292 return node;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000293
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000294 return NULL;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000295}
296
Miklos Szeredia13d9002004-11-02 17:32:03 +0000297static struct node *find_node(struct fuse *f, nodeid_t parent, char *name,
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000298 struct fuse_attr *attr, uint64_t version)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000299{
300 struct node *node;
Miklos Szeredia181e612001-11-06 12:03:23 +0000301 int mode = attr->mode & S_IFMT;
302 int rdev = 0;
Miklos Szeredie5183742005-02-02 11:14:04 +0000303
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000304 if (S_ISCHR(mode) || S_ISBLK(mode))
Miklos Szeredia181e612001-11-06 12:03:23 +0000305 rdev = attr->rdev;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000306
Miklos Szeredia181e612001-11-06 12:03:23 +0000307 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000308 node = lookup_node(f, parent, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000309 if (node != NULL) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000310 if (!(f->flags & FUSE_USE_INO))
Miklos Szeredie5183742005-02-02 11:14:04 +0000311 attr->ino = node->nodeid;
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000312 } else {
313 node = (struct node *) calloc(1, sizeof(struct node));
314 if (node == NULL)
315 goto out_err;
Miklos Szeredie5183742005-02-02 11:14:04 +0000316
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000317 node->refctr = 1;
318 node->nodeid = next_id(f);
319 if (!(f->flags & FUSE_USE_INO))
320 attr->ino = node->nodeid;
321 node->open_count = 0;
322 node->is_hidden = 0;
323 node->generation = f->generation;
324 if (hash_name(f, node, parent, name) == -1) {
325 free(node);
326 node = NULL;
327 goto out_err;
328 }
329 hash_id(f, node);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000330 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000331 node->version = version;
Miklos Szeredi38009022005-05-08 19:47:22 +0000332 node->nlookup ++;
Miklos Szeredic2309912004-09-21 13:40:38 +0000333 out_err:
Miklos Szeredia181e612001-11-06 12:03:23 +0000334 pthread_mutex_unlock(&f->lock);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000335 return node;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000336}
337
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000338static char *add_name(char *buf, char *s, const char *name)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000339{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000340 size_t len = strlen(name);
341 s -= len;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000342 if (s <= buf) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000343 fprintf(stderr, "fuse: path too long: ...%s\n", s + len);
344 return NULL;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000345 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000346 strncpy(s, name, len);
347 s--;
348 *s = '/';
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000349
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000350 return s;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000351}
352
Miklos Szeredia13d9002004-11-02 17:32:03 +0000353static char *get_path_name(struct fuse *f, nodeid_t nodeid, const char *name)
Miklos Szeredib483c932001-10-29 14:57:57 +0000354{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000355 char buf[FUSE_MAX_PATH];
356 char *s = buf + FUSE_MAX_PATH - 1;
357 struct node *node;
Miklos Szeredie5183742005-02-02 11:14:04 +0000358
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000359 *s = '\0';
Miklos Szeredia181e612001-11-06 12:03:23 +0000360
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000361 if (name != NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000362 s = add_name(buf, s, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000363 if (s == NULL)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000364 return NULL;
365 }
366
367 pthread_mutex_lock(&f->lock);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000368 for (node = get_node(f, nodeid); node && node->nodeid != FUSE_ROOT_ID;
369 node = get_node(f, node->parent)) {
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000370 if (node->name == NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000371 s = NULL;
372 break;
373 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000374
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000375 s = add_name(buf, s, node->name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000376 if (s == NULL)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000377 break;
378 }
379 pthread_mutex_unlock(&f->lock);
380
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000381 if (node == NULL || s == NULL)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000382 return NULL;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000383 else if (*s == '\0')
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000384 return strdup("/");
385 else
386 return strdup(s);
387}
Miklos Szeredia181e612001-11-06 12:03:23 +0000388
Miklos Szeredia13d9002004-11-02 17:32:03 +0000389static char *get_path(struct fuse *f, nodeid_t nodeid)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000390{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000391 return get_path_name(f, nodeid, NULL);
Miklos Szeredib483c932001-10-29 14:57:57 +0000392}
393
Miklos Szeredi38009022005-05-08 19:47:22 +0000394static void forget_node(struct fuse *f, nodeid_t nodeid, uint64_t nlookup)
395{
396 struct node *node;
397 if (nodeid == FUSE_ROOT_ID)
398 return;
399 pthread_mutex_lock(&f->lock);
400 node = get_node(f, nodeid);
401 assert(node->nlookup >= nlookup);
402 node->nlookup -= nlookup;
403 if (!node->nlookup) {
404 unhash_name(f, node);
405 unref_node(f, node);
406 }
407 pthread_mutex_unlock(&f->lock);
408}
409
410static void forget_node_old(struct fuse *f, nodeid_t nodeid, uint64_t version)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000411{
Miklos Szeredia181e612001-11-06 12:03:23 +0000412 struct node *node;
413
414 pthread_mutex_lock(&f->lock);
Miklos Szeredid0cf1fb2005-05-06 10:10:38 +0000415 node = get_node_nocheck(f, nodeid);
416 if (node && node->version == version && nodeid != FUSE_ROOT_ID) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000417 node->version = 0;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000418 unhash_name(f, node);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000419 unref_node(f, node);
Miklos Szeredia181e612001-11-06 12:03:23 +0000420 }
421 pthread_mutex_unlock(&f->lock);
Miklos Szeredi38009022005-05-08 19:47:22 +0000422}
Miklos Szeredia181e612001-11-06 12:03:23 +0000423
Miklos Szeredi38009022005-05-08 19:47:22 +0000424static void cancel_lookup(struct fuse *f, nodeid_t nodeid, uint64_t version)
425{
426 if (f->major <= 6)
427 forget_node_old(f, nodeid, version);
428 else
429 forget_node(f, nodeid, 1);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000430}
431
Miklos Szeredia13d9002004-11-02 17:32:03 +0000432static void remove_node(struct fuse *f, nodeid_t dir, const char *name)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000433{
Miklos Szeredia181e612001-11-06 12:03:23 +0000434 struct node *node;
435
436 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000437 node = lookup_node(f, dir, name);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000438 if (node != NULL)
439 unhash_name(f, node);
Miklos Szeredia181e612001-11-06 12:03:23 +0000440 pthread_mutex_unlock(&f->lock);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000441}
442
Miklos Szeredia13d9002004-11-02 17:32:03 +0000443static int rename_node(struct fuse *f, nodeid_t olddir, const char *oldname,
444 nodeid_t newdir, const char *newname, int hide)
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000445{
Miklos Szeredia181e612001-11-06 12:03:23 +0000446 struct node *node;
447 struct node *newnode;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000448 int err = 0;
Miklos Szeredie5183742005-02-02 11:14:04 +0000449
Miklos Szeredia181e612001-11-06 12:03:23 +0000450 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000451 node = lookup_node(f, olddir, oldname);
452 newnode = lookup_node(f, newdir, newname);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000453 if (node == NULL)
454 goto out;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000455
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000456 if (newnode != NULL) {
457 if (hide) {
458 fprintf(stderr, "fuse: hidden file got created during hiding\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000459 err = -EBUSY;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000460 goto out;
461 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000462 unhash_name(f, newnode);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000463 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000464
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000465 unhash_name(f, node);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000466 if (hash_name(f, node, newdir, newname) == -1) {
467 err = -ENOMEM;
468 goto out;
469 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000470
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000471 if (hide)
472 node->is_hidden = 1;
473
474 out:
Miklos Szeredia181e612001-11-06 12:03:23 +0000475 pthread_mutex_unlock(&f->lock);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000476 return err;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000477}
478
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000479static void convert_stat(struct stat *stbuf, struct fuse_attr *attr)
480{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000481 attr->ino = stbuf->st_ino;
Miklos Szeredib5958612004-02-20 14:10:49 +0000482 attr->mode = stbuf->st_mode;
483 attr->nlink = stbuf->st_nlink;
484 attr->uid = stbuf->st_uid;
485 attr->gid = stbuf->st_gid;
486 attr->rdev = stbuf->st_rdev;
487 attr->size = stbuf->st_size;
488 attr->blocks = stbuf->st_blocks;
489 attr->atime = stbuf->st_atime;
Miklos Szeredib5958612004-02-20 14:10:49 +0000490 attr->mtime = stbuf->st_mtime;
Miklos Szeredib5958612004-02-20 14:10:49 +0000491 attr->ctime = stbuf->st_ctime;
Miklos Szeredicb264512004-06-23 18:52:50 +0000492#ifdef HAVE_STRUCT_STAT_ST_ATIM
493 attr->atimensec = stbuf->st_atim.tv_nsec;
494 attr->mtimensec = stbuf->st_mtim.tv_nsec;
Miklos Szeredib5958612004-02-20 14:10:49 +0000495 attr->ctimensec = stbuf->st_ctim.tv_nsec;
Miklos Szeredicb264512004-06-23 18:52:50 +0000496#endif
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000497}
498
Miklos Szerediab974562005-04-07 15:40:21 +0000499static size_t iov_length(const struct iovec *iov, size_t count)
Miklos Szerediede1f7a2005-04-01 21:08:57 +0000500{
Miklos Szerediab974562005-04-07 15:40:21 +0000501 size_t seg;
502 size_t ret = 0;
Miklos Szerediede1f7a2005-04-01 21:08:57 +0000503
Miklos Szerediab974562005-04-07 15:40:21 +0000504 for (seg = 0; seg < count; seg++)
505 ret += iov[seg].iov_len;
506 return ret;
Miklos Szerediede1f7a2005-04-01 21:08:57 +0000507}
508
Miklos Szerediab974562005-04-07 15:40:21 +0000509static int send_reply_raw(struct fuse *f, const struct iovec iov[],
510 size_t count)
Miklos Szeredi43696432001-11-18 19:15:05 +0000511{
512 int res;
Miklos Szerediab974562005-04-07 15:40:21 +0000513 unsigned outsize = iov_length(iov, count);
514 struct fuse_out_header *out = (struct fuse_out_header *) iov[0].iov_base;
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000515 out->len = outsize;
Miklos Szeredi43696432001-11-18 19:15:05 +0000516
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000517 if ((f->flags & FUSE_DEBUG)) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000518 printf(" unique: %llu, error: %i (%s), outsize: %i\n",
519 out->unique, out->error, strerror(-out->error), outsize);
Miklos Szeredi43696432001-11-18 19:15:05 +0000520 fflush(stdout);
521 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000522
Miklos Szeredi4e71c9f2004-02-09 12:05:14 +0000523 /* This needs to be done before the reply, otherwise the scheduler
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000524 could play tricks with us, and only let the counter be
525 increased long after the operation is done */
526 fuse_inc_avail(f);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000527
Miklos Szerediab974562005-04-07 15:40:21 +0000528 res = writev(f->fd, iov, count);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000529 if (res == -1) {
Miklos Szeredi43696432001-11-18 19:15:05 +0000530 /* ENOENT means the operation was interrupted */
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000531 if (!f->exited && errno != ENOENT)
Miklos Szeredi96249982001-11-21 12:21:19 +0000532 perror("fuse: writing device");
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000533 return -errno;
Miklos Szeredi43696432001-11-18 19:15:05 +0000534 }
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000535 return 0;
Miklos Szeredi43696432001-11-18 19:15:05 +0000536}
537
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000538static int send_reply(struct fuse *f, struct fuse_in_header *in, int error,
539 void *arg, size_t argsize)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000540{
Miklos Szerediab974562005-04-07 15:40:21 +0000541 struct fuse_out_header out;
542 struct iovec iov[2];
543 size_t count;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000544
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000545 if (error <= -1000 || error > 0) {
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000546 fprintf(stderr, "fuse: bad error value: %i\n", error);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000547 error = -ERANGE;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000548 }
549
Miklos Szerediab974562005-04-07 15:40:21 +0000550 out.unique = in->unique;
551 out.error = error;
552 count = 1;
553 iov[0].iov_base = &out;
554 iov[0].iov_len = sizeof(struct fuse_out_header);
555 if (argsize && !error) {
556 count++;
557 iov[1].iov_base = arg;
558 iov[1].iov_len = argsize;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000559 }
Miklos Szerediab974562005-04-07 15:40:21 +0000560 return send_reply_raw(f, iov, count);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000561}
562
Miklos Szeredia13d9002004-11-02 17:32:03 +0000563static int is_open(struct fuse *f, nodeid_t dir, const char *name)
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000564{
565 struct node *node;
566 int isopen = 0;
567 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000568 node = lookup_node(f, dir, name);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000569 if (node && node->open_count > 0)
570 isopen = 1;
571 pthread_mutex_unlock(&f->lock);
572 return isopen;
573}
574
Miklos Szeredia13d9002004-11-02 17:32:03 +0000575static char *hidden_name(struct fuse *f, nodeid_t dir, const char *oldname,
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000576 char *newname, size_t bufsize)
577{
578 struct stat buf;
579 struct node *node;
580 struct node *newnode;
581 char *newpath;
582 int res;
583 int failctr = 10;
584
585 if (!f->op.getattr)
586 return NULL;
587
588 do {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000589 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000590 node = lookup_node(f, dir, oldname);
591 if (node == NULL) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000592 pthread_mutex_unlock(&f->lock);
593 return NULL;
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000594 }
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000595 do {
596 f->hidectr ++;
597 snprintf(newname, bufsize, ".fuse_hidden%08x%08x",
Miklos Szeredia13d9002004-11-02 17:32:03 +0000598 (unsigned int) node->nodeid, f->hidectr);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000599 newnode = lookup_node(f, dir, newname);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000600 } while(newnode);
601 pthread_mutex_unlock(&f->lock);
Miklos Szeredie5183742005-02-02 11:14:04 +0000602
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000603 newpath = get_path_name(f, dir, newname);
604 if (!newpath)
605 break;
Miklos Szeredie5183742005-02-02 11:14:04 +0000606
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000607 res = f->op.getattr(newpath, &buf);
608 if (res != 0)
609 break;
610 free(newpath);
611 newpath = NULL;
612 } while(--failctr);
613
614 return newpath;
615}
616
Miklos Szeredia13d9002004-11-02 17:32:03 +0000617static int hide_node(struct fuse *f, const char *oldpath, nodeid_t dir,
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000618 const char *oldname)
619{
620 char newname[64];
621 char *newpath;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000622 int err = -EBUSY;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000623
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000624 if (f->op.rename && f->op.unlink) {
625 newpath = hidden_name(f, dir, oldname, newname, sizeof(newname));
626 if (newpath) {
627 int res = f->op.rename(oldpath, newpath);
628 if (res == 0)
629 err = rename_node(f, dir, oldname, dir, newname, 1);
630 free(newpath);
631 }
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000632 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000633 return err;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000634}
635
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000636static int lookup_path(struct fuse *f, nodeid_t nodeid, uint64_t version,
637 char *name, const char *path,
638 struct fuse_entry_out *arg)
Miklos Szeredi76f65782004-02-19 16:55:40 +0000639{
640 int res;
641 struct stat buf;
642
643 res = f->op.getattr(path, &buf);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000644 if (res == 0) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000645 struct node *node;
646
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000647 memset(arg, 0, sizeof(struct fuse_entry_out));
Miklos Szeredi76f65782004-02-19 16:55:40 +0000648 convert_stat(&buf, &arg->attr);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000649 node = find_node(f, nodeid, name, &arg->attr, version);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000650 if (node == NULL)
651 res = -ENOMEM;
652 else {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000653 arg->nodeid = node->nodeid;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000654 arg->generation = node->generation;
655 arg->entry_valid = ENTRY_REVALIDATE_TIME;
656 arg->entry_valid_nsec = 0;
657 arg->attr_valid = ATTR_REVALIDATE_TIME;
658 arg->attr_valid_nsec = 0;
659 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000660 printf(" NODEID: %lu\n", (unsigned long) arg->nodeid);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000661 fflush(stdout);
662 }
Miklos Szeredi76f65782004-02-19 16:55:40 +0000663 }
664 }
665 return res;
666}
667
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000668static void do_lookup(struct fuse *f, struct fuse_in_header *in, char *name)
669{
670 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000671 int res2;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000672 char *path;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000673 struct fuse_entry_out arg;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000674
Miklos Szeredi5e183482001-10-31 14:52:35 +0000675 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000676 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000677 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000678 if (path != NULL) {
679 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi6ebe2342002-01-06 21:44:16 +0000680 printf("LOOKUP %s\n", path);
681 fflush(stdout);
682 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000683 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000684 if (f->op.getattr)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000685 res = lookup_path(f, in->nodeid, in->unique, name, path, &arg);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000686 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000687 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000688 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000689 res2 = send_reply(f, in, res, &arg, sizeof(arg));
690 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi38009022005-05-08 19:47:22 +0000691 cancel_lookup(f, arg.nodeid, in->unique);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000692}
693
Miklos Szeredia181e612001-11-06 12:03:23 +0000694static void do_forget(struct fuse *f, struct fuse_in_header *in,
695 struct fuse_forget_in *arg)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000696{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000697 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000698 printf("FORGET %lu/%llu\n", (unsigned long) in->nodeid,
Miklos Szeredi38009022005-05-08 19:47:22 +0000699 arg->nlookup);
Miklos Szeredi43696432001-11-18 19:15:05 +0000700 fflush(stdout);
701 }
Miklos Szeredi38009022005-05-08 19:47:22 +0000702 if (f->major <= 6)
703 forget_node_old(f, in->nodeid, arg->nlookup);
704 else
705 forget_node(f, in->nodeid, arg->nlookup);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000706}
707
708static void do_getattr(struct fuse *f, struct fuse_in_header *in)
709{
710 int res;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000711 char *path;
712 struct stat buf;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000713 struct fuse_attr_out arg;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000714
Miklos Szeredi5e183482001-10-31 14:52:35 +0000715 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000716 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000717 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000718 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +0000719 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000720 if (f->op.getattr)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000721 res = f->op.getattr(path, &buf);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000722 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000723 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000724 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000725
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000726 if (res == 0) {
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000727 memset(&arg, 0, sizeof(struct fuse_attr_out));
728 arg.attr_valid = ATTR_REVALIDATE_TIME;
729 arg.attr_valid_nsec = 0;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000730 convert_stat(&buf, &arg.attr);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000731 if (!(f->flags & FUSE_USE_INO))
732 arg.attr.ino = in->nodeid;
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000733 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000734
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000735 send_reply(f, in, res, &arg, sizeof(arg));
736}
737
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000738static int do_chmod(struct fuse *f, const char *path, struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000739{
740 int res;
741
742 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000743 if (f->op.chmod)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000744 res = f->op.chmod(path, attr->mode);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000745
746 return res;
Miklos Szeredie5183742005-02-02 11:14:04 +0000747}
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000748
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000749static int do_chown(struct fuse *f, const char *path, struct fuse_attr *attr,
Miklos Szeredi2e50d432001-12-20 12:17:25 +0000750 int valid)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000751{
752 int res;
753 uid_t uid = (valid & FATTR_UID) ? attr->uid : (uid_t) -1;
754 gid_t gid = (valid & FATTR_GID) ? attr->gid : (gid_t) -1;
Miklos Szeredie5183742005-02-02 11:14:04 +0000755
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000756 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000757 if (f->op.chown)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000758 res = f->op.chown(path, uid, gid);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000759
760 return res;
761}
762
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000763static int do_truncate(struct fuse *f, const char *path,
764 struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000765{
766 int res;
767
768 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000769 if (f->op.truncate)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000770 res = f->op.truncate(path, attr->size);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000771
772 return res;
773}
774
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000775static int do_utime(struct fuse *f, const char *path, struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000776{
777 int res;
778 struct utimbuf buf;
779 buf.actime = attr->atime;
780 buf.modtime = attr->mtime;
781 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000782 if (f->op.utime)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000783 res = f->op.utime(path, &buf);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000784
785 return res;
786}
787
Miklos Szeredi5e183482001-10-31 14:52:35 +0000788static void do_setattr(struct fuse *f, struct fuse_in_header *in,
789 struct fuse_setattr_in *arg)
790{
791 int res;
792 char *path;
793 int valid = arg->valid;
Miklos Szeredi30e093a2005-04-03 17:44:54 +0000794 struct fuse_attr *attr = MEMBER_COMPAT(f, arg, attr, fuse_setattr_in);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000795 struct fuse_attr_out outarg;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000796
797 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000798 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000799 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000800 if (path != NULL) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000801 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000802 if (f->op.getattr) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000803 res = 0;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000804 if (!res && (valid & FATTR_MODE))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000805 res = do_chmod(f, path, attr);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000806 if (!res && (valid & (FATTR_UID | FATTR_GID)))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000807 res = do_chown(f, path, attr, valid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000808 if (!res && (valid & FATTR_SIZE))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000809 res = do_truncate(f, path, attr);
Miklos Szeredie5183742005-02-02 11:14:04 +0000810 if (!res && (valid & (FATTR_ATIME | FATTR_MTIME)) ==
Miklos Szeredib5958612004-02-20 14:10:49 +0000811 (FATTR_ATIME | FATTR_MTIME))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000812 res = do_utime(f, path, attr);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000813 if (!res) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000814 struct stat buf;
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000815 res = f->op.getattr(path, &buf);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000816 if (!res) {
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000817 memset(&outarg, 0, sizeof(struct fuse_attr_out));
818 outarg.attr_valid = ATTR_REVALIDATE_TIME;
819 outarg.attr_valid_nsec = 0;
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000820 convert_stat(&buf, &outarg.attr);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000821 if (!(f->flags & FUSE_USE_INO))
822 outarg.attr.ino = in->nodeid;
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000823 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000824 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000825 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000826 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000827 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000828 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredia181e612001-11-06 12:03:23 +0000829 send_reply(f, in, res, &outarg, sizeof(outarg));
Miklos Szeredi5e183482001-10-31 14:52:35 +0000830}
831
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000832static void do_readlink(struct fuse *f, struct fuse_in_header *in)
833{
834 int res;
835 char link[PATH_MAX + 1];
Miklos Szeredib483c932001-10-29 14:57:57 +0000836 char *path;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000837
Miklos Szeredi5e183482001-10-31 14:52:35 +0000838 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000839 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000840 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000841 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +0000842 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000843 if (f->op.readlink)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000844 res = f->op.readlink(path, link, sizeof(link));
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000845 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000846 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000847 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000848 link[PATH_MAX] = '\0';
Miklos Szeredi4b2bef42002-01-09 12:23:27 +0000849 send_reply(f, in, res, link, res == 0 ? strlen(link) : 0);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000850}
851
Miklos Szeredib483c932001-10-29 14:57:57 +0000852static void do_mknod(struct fuse *f, struct fuse_in_header *in,
853 struct fuse_mknod_in *inarg)
854{
855 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000856 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000857 char *path;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000858 char *name = PARAM(inarg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000859 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000860
Miklos Szeredi5e183482001-10-31 14:52:35 +0000861 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000862 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000863 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000864 if (path != NULL) {
865 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000866 printf("MKNOD %s\n", path);
867 fflush(stdout);
868 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000869 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000870 if (f->op.mknod && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000871 res = f->op.mknod(path, inarg->mode, inarg->rdev);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000872 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000873 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000874 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000875 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000876 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000877 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000878 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
879 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi38009022005-05-08 19:47:22 +0000880 cancel_lookup(f, outarg.nodeid, in->unique);
Miklos Szeredib483c932001-10-29 14:57:57 +0000881}
882
883static void do_mkdir(struct fuse *f, struct fuse_in_header *in,
884 struct fuse_mkdir_in *inarg)
885{
886 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000887 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000888 char *path;
Miklos Szeredi30e093a2005-04-03 17:44:54 +0000889 char *name = PARAM_COMPAT(f, inarg, fuse_mkdir_in);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000890 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000891
Miklos Szeredi5e183482001-10-31 14:52:35 +0000892 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000893 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000894 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000895 if (path != NULL) {
896 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000897 printf("MKDIR %s\n", path);
898 fflush(stdout);
899 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000900 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000901 if (f->op.mkdir && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000902 res = f->op.mkdir(path, inarg->mode);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000903 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000904 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000905 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000906 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000907 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000908 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000909 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
910 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi38009022005-05-08 19:47:22 +0000911 cancel_lookup(f, outarg.nodeid, in->unique);
Miklos Szeredib483c932001-10-29 14:57:57 +0000912}
913
Miklos Szeredib5958612004-02-20 14:10:49 +0000914static void do_unlink(struct fuse *f, struct fuse_in_header *in, char *name)
Miklos Szeredib483c932001-10-29 14:57:57 +0000915{
916 int res;
917 char *path;
918
Miklos Szeredi5e183482001-10-31 14:52:35 +0000919 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000920 pthread_rwlock_wrlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000921 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000922 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000923 if (f->flags & FUSE_DEBUG) {
924 printf("UNLINK %s\n", path);
925 fflush(stdout);
926 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000927 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000928 if (f->op.unlink) {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000929 if (!(f->flags & FUSE_HARD_REMOVE) && is_open(f, in->nodeid, name))
930 res = hide_node(f, path, in->nodeid, name);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000931 else {
932 res = f->op.unlink(path);
933 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000934 remove_node(f, in->nodeid, name);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000935
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000936 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000937 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000938 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000939 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000940 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredib5958612004-02-20 14:10:49 +0000941 send_reply(f, in, res, NULL, 0);
942}
943
944static void do_rmdir(struct fuse *f, struct fuse_in_header *in, char *name)
945{
946 int res;
947 char *path;
948
949 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000950 pthread_rwlock_wrlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000951 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000952 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000953 if (f->flags & FUSE_DEBUG) {
954 printf("RMDIR %s\n", path);
955 fflush(stdout);
956 }
Miklos Szeredib5958612004-02-20 14:10:49 +0000957 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000958 if (f->op.rmdir) {
Miklos Szeredib5958612004-02-20 14:10:49 +0000959 res = f->op.rmdir(path);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000960 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000961 remove_node(f, in->nodeid, name);
Miklos Szeredib5958612004-02-20 14:10:49 +0000962 }
963 free(path);
964 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000965 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredib483c932001-10-29 14:57:57 +0000966 send_reply(f, in, res, NULL, 0);
967}
968
969static void do_symlink(struct fuse *f, struct fuse_in_header *in, char *name,
970 char *link)
971{
972 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000973 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000974 char *path;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000975 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000976
Miklos Szeredi5e183482001-10-31 14:52:35 +0000977 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000978 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000979 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000980 if (path != NULL) {
981 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000982 printf("SYMLINK %s\n", path);
983 fflush(stdout);
984 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000985 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000986 if (f->op.symlink && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000987 res = f->op.symlink(link, path);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000988 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000989 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000990 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000991 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000992 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000993 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000994 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
995 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi38009022005-05-08 19:47:22 +0000996 cancel_lookup(f, outarg.nodeid, in->unique);
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000997
Miklos Szeredib483c932001-10-29 14:57:57 +0000998}
999
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001000static void do_rename(struct fuse *f, struct fuse_in_header *in,
1001 struct fuse_rename_in *inarg)
1002{
1003 int res;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001004 nodeid_t olddir = in->nodeid;
1005 nodeid_t newdir = inarg->newdir;
Miklos Szeredi6bf8b682002-10-28 08:49:39 +00001006 char *oldname = PARAM(inarg);
1007 char *newname = oldname + strlen(oldname) + 1;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001008 char *oldpath;
1009 char *newpath;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001010
Miklos Szeredi5e183482001-10-31 14:52:35 +00001011 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001012 pthread_rwlock_wrlock(&f->tree_lock);
Miklos Szeredia181e612001-11-06 12:03:23 +00001013 oldpath = get_path_name(f, olddir, oldname);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001014 if (oldpath != NULL) {
Miklos Szeredia181e612001-11-06 12:03:23 +00001015 newpath = get_path_name(f, newdir, newname);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001016 if (newpath != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001017 if (f->flags & FUSE_DEBUG) {
1018 printf("RENAME %s -> %s\n", oldpath, newpath);
1019 fflush(stdout);
1020 }
Miklos Szeredi5e183482001-10-31 14:52:35 +00001021 res = -ENOSYS;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001022 if (f->op.rename) {
1023 res = 0;
Miklos Szeredie5183742005-02-02 11:14:04 +00001024 if (!(f->flags & FUSE_HARD_REMOVE) &&
Miklos Szeredi2529ca22004-07-13 15:36:52 +00001025 is_open(f, newdir, newname))
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001026 res = hide_node(f, newpath, newdir, newname);
1027 if (res == 0) {
1028 res = f->op.rename(oldpath, newpath);
1029 if (res == 0)
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001030 res = rename_node(f, olddir, oldname, newdir, newname, 0);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001031 }
1032 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001033 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001034 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001035 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001036 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001037 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredie5183742005-02-02 11:14:04 +00001038 send_reply(f, in, res, NULL, 0);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001039}
1040
1041static void do_link(struct fuse *f, struct fuse_in_header *in,
Miklos Szeredi5e183482001-10-31 14:52:35 +00001042 struct fuse_link_in *arg)
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001043{
1044 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +00001045 int res2;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001046 char *oldpath;
1047 char *newpath;
Miklos Szeredi76f65782004-02-19 16:55:40 +00001048 char *name = PARAM(arg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +00001049 struct fuse_entry_out outarg;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001050
Miklos Szeredi5e183482001-10-31 14:52:35 +00001051 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001052 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredied6b5dd2005-01-26 17:07:59 +00001053 oldpath = get_path(f, arg->oldnodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001054 if (oldpath != NULL) {
Miklos Szeredied6b5dd2005-01-26 17:07:59 +00001055 newpath = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001056 if (newpath != NULL) {
1057 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +00001058 printf("LINK %s\n", newpath);
1059 fflush(stdout);
1060 }
Miklos Szeredi5e183482001-10-31 14:52:35 +00001061 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001062 if (f->op.link && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +00001063 res = f->op.link(oldpath, newpath);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001064 if (res == 0)
Miklos Szeredied6b5dd2005-01-26 17:07:59 +00001065 res = lookup_path(f, in->nodeid, in->unique, name,
Miklos Szeredi76f65782004-02-19 16:55:40 +00001066 newpath, &outarg);
1067 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001068 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001069 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001070 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001071 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001072 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi2778f6c2004-06-21 09:45:30 +00001073 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
1074 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi38009022005-05-08 19:47:22 +00001075 cancel_lookup(f, outarg.nodeid, in->unique);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001076}
1077
Miklos Szeredi5e183482001-10-31 14:52:35 +00001078static void do_open(struct fuse *f, struct fuse_in_header *in,
1079 struct fuse_open_in *arg)
1080{
1081 int res;
1082 char *path;
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001083 struct fuse_open_out outarg;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001084 struct fuse_file_info fi;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001085
Miklos Szeredied3c97c2005-02-15 17:04:50 +00001086 memset(&outarg, 0, sizeof(outarg));
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001087 memset(&fi, 0, sizeof(fi));
1088 fi.flags = arg->flags;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001089 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001090 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001091 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001092 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +00001093 res = -ENOSYS;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001094 if (f->op.open) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001095 if (!f->compat)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001096 res = f->op.open(path, &fi);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001097 else
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001098 res = ((struct fuse_operations_compat2 *) &f->op)->open(path, fi.flags);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001099 }
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +00001100 }
Miklos Szeredi73798f92004-07-12 15:55:11 +00001101 if (res == 0) {
1102 int res2;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001103 outarg.fh = fi.fh;
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001104 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001105 printf("OPEN[%lu] flags: 0x%x\n", fi.fh, arg->flags);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001106 fflush(stdout);
1107 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001108
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001109 pthread_mutex_lock(&f->lock);
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001110 res2 = send_reply(f, in, res, &outarg, SIZEOF_COMPAT(f, fuse_open_out));
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001111 if(res2 == -ENOENT) {
1112 /* The open syscall was interrupted, so it must be cancelled */
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001113 if(f->op.release) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001114 if (!f->compat)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001115 f->op.release(path, &fi);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001116 else
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001117 ((struct fuse_operations_compat2 *) &f->op)->release(path, fi.flags);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001118 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001119 } else {
1120 struct node *node = get_node(f, in->nodeid);
1121 node->open_count ++;
1122 }
Miklos Szeredi73798f92004-07-12 15:55:11 +00001123 pthread_mutex_unlock(&f->lock);
Miklos Szeredi73798f92004-07-12 15:55:11 +00001124 } else
1125 send_reply(f, in, res, NULL, 0);
1126
Miklos Szeredi9a31cca2004-06-26 21:11:25 +00001127 if (path)
1128 free(path);
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001129 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001130}
1131
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001132static void do_flush(struct fuse *f, struct fuse_in_header *in,
1133 struct fuse_flush_in *arg)
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001134{
1135 char *path;
1136 int res;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001137 struct fuse_file_info fi;
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001138
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001139 memset(&fi, 0, sizeof(fi));
1140 fi.fh = arg->fh;
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001141 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001142 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001143 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001144 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001145 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001146 printf("FLUSH[%lu]\n", (unsigned long) arg->fh);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001147 fflush(stdout);
1148 }
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001149 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001150 if (f->op.flush)
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001151 res = f->op.flush(path, &fi);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001152 free(path);
1153 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001154 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001155 send_reply(f, in, res, NULL, 0);
1156}
1157
Miklos Szeredi9478e862002-12-11 09:50:26 +00001158static void do_release(struct fuse *f, struct fuse_in_header *in,
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001159 struct fuse_release_in *arg)
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001160{
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001161 struct node *node;
1162 char *path;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001163 struct fuse_file_info fi;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001164 int unlink_hidden;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001165
1166 memset(&fi, 0, sizeof(fi));
1167 fi.flags = arg->flags;
1168 fi.fh = arg->fh;
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001169
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001170 pthread_mutex_lock(&f->lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001171 node = get_node(f, in->nodeid);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001172 assert(node->open_count > 0);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001173 --node->open_count;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001174 unlink_hidden = (node->is_hidden && !node->open_count);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001175 pthread_mutex_unlock(&f->lock);
1176
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001177 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001178 path = get_path(f, in->nodeid);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001179 if (f->flags & FUSE_DEBUG) {
1180 printf("RELEASE[%lu] flags: 0x%x\n", fi.fh, fi.flags);
1181 fflush(stdout);
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001182 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001183 if (f->op.release) {
1184 if (!f->compat)
1185 f->op.release(path ? path : "-", &fi);
1186 else if (path)
1187 ((struct fuse_operations_compat2 *) &f->op)->release(path, fi.flags);
1188 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001189
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001190 if(unlink_hidden && path)
1191 f->op.unlink(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001192
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001193 if (path)
1194 free(path);
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001195 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001196
Miklos Szeredi556d03d2004-06-30 11:13:41 +00001197 send_reply(f, in, 0, NULL, 0);
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001198}
1199
Miklos Szeredi5e183482001-10-31 14:52:35 +00001200static void do_read(struct fuse *f, struct fuse_in_header *in,
1201 struct fuse_read_in *arg)
1202{
1203 int res;
1204 char *path;
Miklos Szerediab974562005-04-07 15:40:21 +00001205 size_t size;
1206 char *buf;
1207 struct fuse_file_info fi;
1208
1209 buf = (char *) malloc(arg->size);
1210 if (buf == NULL) {
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001211 send_reply(f, in, -ENOMEM, NULL, 0);
Miklos Szerediab974562005-04-07 15:40:21 +00001212 return;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001213 }
Miklos Szerediab974562005-04-07 15:40:21 +00001214
1215 memset(&fi, 0, sizeof(fi));
1216 fi.fh = arg->fh;
1217
1218 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001219 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szerediab974562005-04-07 15:40:21 +00001220 path = get_path(f, in->nodeid);
1221 if (path != NULL) {
1222 if (f->flags & FUSE_DEBUG) {
1223 printf("READ[%lu] %u bytes from %llu\n",
1224 (unsigned long) arg->fh, arg->size, arg->offset);
1225 fflush(stdout);
1226 }
1227
1228 res = -ENOSYS;
1229 if (f->op.read)
1230 res = f->op.read(path, buf, arg->size, arg->offset, &fi);
1231 free(path);
1232 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001233 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szerediab974562005-04-07 15:40:21 +00001234
1235 size = 0;
1236 if (res >= 0) {
1237 size = res;
1238 res = 0;
1239 if (f->flags & FUSE_DEBUG) {
1240 printf(" READ[%lu] %u bytes\n", (unsigned long) arg->fh,
1241 size);
1242 fflush(stdout);
1243 }
1244 }
1245
1246 send_reply(f, in, res, buf, size);
1247 free(buf);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001248}
Miklos Szeredib483c932001-10-29 14:57:57 +00001249
Miklos Szeredia181e612001-11-06 12:03:23 +00001250static void do_write(struct fuse *f, struct fuse_in_header *in,
1251 struct fuse_write_in *arg)
1252{
1253 int res;
1254 char *path;
Miklos Szerediad051c32004-07-02 09:22:50 +00001255 struct fuse_write_out outarg;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001256 struct fuse_file_info fi;
1257
1258 memset(&fi, 0, sizeof(fi));
1259 fi.fh = arg->fh;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001260 fi.writepage = arg->write_flags & 1;
Miklos Szeredia181e612001-11-06 12:03:23 +00001261
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001262 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001263 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001264 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001265 if (path != NULL) {
1266 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi1eea0322004-09-27 18:50:11 +00001267 printf("WRITE%s[%lu] %u bytes to %llu\n",
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001268 (arg->write_flags & 1) ? "PAGE" : "",
1269 (unsigned long) arg->fh, arg->size, arg->offset);
Miklos Szeredi6ebe2342002-01-06 21:44:16 +00001270 fflush(stdout);
1271 }
1272
Miklos Szeredia181e612001-11-06 12:03:23 +00001273 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001274 if (f->op.write)
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001275 res = f->op.write(path, PARAM(arg), arg->size, arg->offset, &fi);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001276 free(path);
Miklos Szeredia181e612001-11-06 12:03:23 +00001277 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001278 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredie5183742005-02-02 11:14:04 +00001279
Miklos Szerediab974562005-04-07 15:40:21 +00001280 memset(&outarg, 0, sizeof(outarg));
Miklos Szeredie5183742005-02-02 11:14:04 +00001281 if (res >= 0) {
Miklos Szerediad051c32004-07-02 09:22:50 +00001282 outarg.size = res;
1283 res = 0;
Miklos Szeredia181e612001-11-06 12:03:23 +00001284 }
1285
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001286 send_reply(f, in, res, &outarg, SIZEOF_COMPAT(f, fuse_write_out));
Miklos Szeredia181e612001-11-06 12:03:23 +00001287}
1288
Miklos Szeredi77f39942004-03-25 11:17:52 +00001289static int default_statfs(struct statfs *buf)
1290{
1291 buf->f_namelen = 255;
1292 buf->f_bsize = 512;
1293 return 0;
1294}
1295
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001296static void convert_statfs_compat(struct fuse_statfs_compat1 *compatbuf,
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001297 struct statfs *statfs)
1298{
1299 statfs->f_bsize = compatbuf->block_size;
1300 statfs->f_blocks = compatbuf->blocks;
1301 statfs->f_bfree = compatbuf->blocks_free;
1302 statfs->f_bavail = compatbuf->blocks_free;
1303 statfs->f_files = compatbuf->files;
1304 statfs->f_ffree = compatbuf->files_free;
1305 statfs->f_namelen = compatbuf->namelen;
1306}
1307
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001308static void convert_statfs(struct statfs *statfs, struct fuse_kstatfs *kstatfs)
1309{
1310 kstatfs->bsize = statfs->f_bsize;
1311 kstatfs->blocks = statfs->f_blocks;
1312 kstatfs->bfree = statfs->f_bfree;
1313 kstatfs->bavail = statfs->f_bavail;
1314 kstatfs->files = statfs->f_files;
1315 kstatfs->ffree = statfs->f_ffree;
1316 kstatfs->namelen = statfs->f_namelen;
1317}
1318
Mark Glinesd84b39a2002-01-07 16:32:02 +00001319static void do_statfs(struct fuse *f, struct fuse_in_header *in)
1320{
1321 int res;
Mark Glinesd84b39a2002-01-07 16:32:02 +00001322 struct fuse_statfs_out arg;
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001323 struct statfs buf;
Mark Glinesd84b39a2002-01-07 16:32:02 +00001324
Miklos Szeredi77f39942004-03-25 11:17:52 +00001325 memset(&buf, 0, sizeof(struct statfs));
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001326 if (f->op.statfs) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001327 if (!f->compat || f->compat > 11)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001328 res = f->op.statfs("/", &buf);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001329 else {
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001330 struct fuse_statfs_compat1 compatbuf;
1331 memset(&compatbuf, 0, sizeof(struct fuse_statfs_compat1));
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001332 res = ((struct fuse_operations_compat1 *) &f->op)->statfs(&compatbuf);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001333 if (res == 0)
1334 convert_statfs_compat(&compatbuf, &buf);
1335 }
1336 }
Miklos Szeredi77f39942004-03-25 11:17:52 +00001337 else
1338 res = default_statfs(&buf);
1339
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001340 if (res == 0)
Miklos Szeredi77f39942004-03-25 11:17:52 +00001341 convert_statfs(&buf, &arg.st);
Miklos Szeredi4b2bef42002-01-09 12:23:27 +00001342
Mark Glinesd84b39a2002-01-07 16:32:02 +00001343 send_reply(f, in, res, &arg, sizeof(arg));
1344}
1345
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001346static void do_fsync(struct fuse *f, struct fuse_in_header *in,
1347 struct fuse_fsync_in *inarg)
1348{
1349 int res;
1350 char *path;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001351 struct fuse_file_info fi;
1352
1353 memset(&fi, 0, sizeof(fi));
1354 fi.fh = inarg->fh;
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001355
1356 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001357 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001358 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001359 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001360 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001361 printf("FSYNC[%lu]\n", (unsigned long) inarg->fh);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001362 fflush(stdout);
1363 }
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001364 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001365 if (f->op.fsync)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001366 res = f->op.fsync(path, inarg->fsync_flags & 1, &fi);
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001367 free(path);
1368 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001369 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001370 send_reply(f, in, res, NULL, 0);
1371}
1372
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001373static void do_setxattr(struct fuse *f, struct fuse_in_header *in,
1374 struct fuse_setxattr_in *arg)
1375{
1376 int res;
1377 char *path;
1378 char *name = PARAM(arg);
1379 unsigned char *value = name + strlen(name) + 1;
1380
1381 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001382 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001383 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001384 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001385 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001386 if (f->op.setxattr)
1387 res = f->op.setxattr(path, name, value, arg->size, arg->flags);
1388 free(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001389 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001390 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001391 send_reply(f, in, res, NULL, 0);
1392}
1393
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001394static int common_getxattr(struct fuse *f, struct fuse_in_header *in,
1395 const char *name, char *value, size_t size)
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001396{
1397 int res;
1398 char *path;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001399
1400 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001401 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001402 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001403 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001404 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001405 if (f->op.getxattr)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001406 res = f->op.getxattr(path, name, value, size);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001407 free(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001408 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001409 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001410 return res;
1411}
1412
1413static void do_getxattr_read(struct fuse *f, struct fuse_in_header *in,
1414 const char *name, size_t size)
1415{
1416 int res;
Miklos Szerediab974562005-04-07 15:40:21 +00001417 char *value = (char *) malloc(size);
1418 if (value == NULL) {
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001419 send_reply(f, in, -ENOMEM, NULL, 0);
Miklos Szerediab974562005-04-07 15:40:21 +00001420 return;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001421 }
Miklos Szerediab974562005-04-07 15:40:21 +00001422 res = common_getxattr(f, in, name, value, size);
1423 size = 0;
1424 if (res > 0) {
1425 size = res;
1426 res = 0;
1427 }
1428 send_reply(f, in, res, value, size);
1429 free(value);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001430}
1431
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001432static void do_getxattr_size(struct fuse *f, struct fuse_in_header *in,
1433 const char *name)
1434{
1435 int res;
1436 struct fuse_getxattr_out arg;
1437
Miklos Szerediab974562005-04-07 15:40:21 +00001438 memset(&arg, 0, sizeof(arg));
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001439 res = common_getxattr(f, in, name, NULL, 0);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001440 if (res >= 0) {
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001441 arg.size = res;
1442 res = 0;
1443 }
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001444 send_reply(f, in, res, &arg, SIZEOF_COMPAT(f, fuse_getxattr_out));
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001445}
1446
1447static void do_getxattr(struct fuse *f, struct fuse_in_header *in,
1448 struct fuse_getxattr_in *arg)
1449{
1450 char *name = PARAM(arg);
Miklos Szeredie5183742005-02-02 11:14:04 +00001451
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001452 if (arg->size)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001453 do_getxattr_read(f, in, name, arg->size);
1454 else
1455 do_getxattr_size(f, in, name);
1456}
1457
1458static int common_listxattr(struct fuse *f, struct fuse_in_header *in,
1459 char *list, size_t size)
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001460{
1461 int res;
1462 char *path;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001463
1464 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001465 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001466 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001467 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001468 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001469 if (f->op.listxattr)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001470 res = f->op.listxattr(path, list, size);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001471 free(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001472 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001473 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001474 return res;
1475}
1476
1477static void do_listxattr_read(struct fuse *f, struct fuse_in_header *in,
1478 size_t size)
1479{
1480 int res;
Miklos Szerediab974562005-04-07 15:40:21 +00001481 char *list = (char *) malloc(size);
1482 if (list == NULL) {
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001483 send_reply(f, in, -ENOMEM, NULL, 0);
Miklos Szerediab974562005-04-07 15:40:21 +00001484 return;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001485 }
Miklos Szerediab974562005-04-07 15:40:21 +00001486 res = common_listxattr(f, in, list, size);
1487 size = 0;
1488 if (res > 0) {
1489 size = res;
1490 res = 0;
1491 }
1492 send_reply(f, in, res, list, size);
1493 free(list);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001494}
1495
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001496static void do_listxattr_size(struct fuse *f, struct fuse_in_header *in)
1497{
1498 int res;
1499 struct fuse_getxattr_out arg;
1500
Miklos Szerediab974562005-04-07 15:40:21 +00001501 memset(&arg, 0, sizeof(arg));
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001502 res = common_listxattr(f, in, NULL, 0);
1503 if (res >= 0) {
1504 arg.size = res;
1505 res = 0;
1506 }
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001507 send_reply(f, in, res, &arg, SIZEOF_COMPAT(f, fuse_getxattr_out));
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001508}
1509
1510static void do_listxattr(struct fuse *f, struct fuse_in_header *in,
1511 struct fuse_getxattr_in *arg)
1512{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001513 if (arg->size)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001514 do_listxattr_read(f, in, arg->size);
1515 else
1516 do_listxattr_size(f, in);
1517}
1518
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001519static void do_removexattr(struct fuse *f, struct fuse_in_header *in,
1520 char *name)
1521{
1522 int res;
1523 char *path;
1524
1525 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001526 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001527 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001528 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001529 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001530 if (f->op.removexattr)
1531 res = f->op.removexattr(path, name);
1532 free(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001533 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001534 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001535 send_reply(f, in, res, NULL, 0);
1536}
1537
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001538static void do_init(struct fuse *f, struct fuse_in_header *in,
1539 struct fuse_init_in_out *arg)
1540{
1541 struct fuse_init_in_out outarg;
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001542
1543 if (in->padding == 5) {
1544 arg->minor = arg->major;
1545 arg->major = in->padding;
1546 }
1547
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001548 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001549 printf("INIT: %u.%u\n", arg->major, arg->minor);
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001550 fflush(stdout);
1551 }
1552 f->got_init = 1;
Miklos Szeredi159bd7e2005-02-28 17:32:16 +00001553 if (f->op.init)
1554 f->user_data = f->op.init();
1555
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001556 if (arg->major == 5) {
1557 f->major = 5;
1558 f->minor = 1;
Miklos Szeredi38009022005-05-08 19:47:22 +00001559 } else if (arg->major == 6) {
1560 f->major = 6;
1561 f->minor = 1;
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001562 } else {
1563 f->major = FUSE_KERNEL_VERSION;
1564 f->minor = FUSE_KERNEL_MINOR_VERSION;
1565 }
1566 memset(&outarg, 0, sizeof(outarg));
1567 outarg.major = f->major;
1568 outarg.minor = f->minor;
1569
1570 if (f->flags & FUSE_DEBUG) {
1571 printf(" INIT: %u.%u\n", outarg.major, outarg.minor);
1572 fflush(stdout);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001573 }
1574
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001575 send_reply(f, in, 0, &outarg, sizeof(outarg));
1576}
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001577
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001578static struct fuse_dirhandle *get_dirhandle(unsigned long fh)
1579{
1580 return (struct fuse_dirhandle *) fh;
1581}
1582
1583static void do_opendir(struct fuse *f, struct fuse_in_header *in,
1584 struct fuse_open_in *arg)
1585{
1586 int res;
1587 struct fuse_open_out outarg;
1588 struct fuse_dirhandle *dh;
1589
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001590 dh = (struct fuse_dirhandle *) malloc(sizeof(struct fuse_dirhandle));
1591 if (dh == NULL) {
1592 send_reply(f, in, -ENOMEM, NULL, 0);
1593 return;
1594 }
1595 memset(dh, 0, sizeof(struct fuse_dirhandle));
1596 dh->fuse = f;
1597 dh->contents = NULL;
1598 dh->len = 0;
1599 dh->filled = 0;
Miklos Szerediab974562005-04-07 15:40:21 +00001600 mutex_init(&dh->lock);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001601
1602 memset(&outarg, 0, sizeof(outarg));
1603 outarg.fh = (unsigned long) dh;
1604
Miklos Szeredif43f0632005-02-28 11:46:56 +00001605 if (f->op.opendir) {
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001606 struct fuse_file_info fi;
Miklos Szeredif43f0632005-02-28 11:46:56 +00001607 char *path;
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001608
1609 memset(&fi, 0, sizeof(fi));
1610 fi.flags = arg->flags;
1611
Miklos Szeredif43f0632005-02-28 11:46:56 +00001612 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001613 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredif43f0632005-02-28 11:46:56 +00001614 path = get_path(f, in->nodeid);
1615 if (path != NULL) {
Miklos Szeredif43f0632005-02-28 11:46:56 +00001616 res = f->op.opendir(path, &fi);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001617 dh->fh = fi.fh;
Miklos Szeredif43f0632005-02-28 11:46:56 +00001618 }
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001619 if (res == 0) {
1620 int res2;
1621 pthread_mutex_lock(&f->lock);
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001622 res2 = send_reply(f, in, res, &outarg, SIZEOF_COMPAT(f, fuse_open_out));
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001623 if(res2 == -ENOENT) {
1624 /* The opendir syscall was interrupted, so it must be
1625 cancelled */
1626 if(f->op.releasedir)
1627 f->op.releasedir(path, &fi);
Miklos Szerediab974562005-04-07 15:40:21 +00001628 pthread_mutex_destroy(&dh->lock);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001629 free(dh);
1630 }
1631 pthread_mutex_unlock(&f->lock);
1632 } else {
Miklos Szeredif43f0632005-02-28 11:46:56 +00001633 send_reply(f, in, res, NULL, 0);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001634 free(dh);
Miklos Szeredif43f0632005-02-28 11:46:56 +00001635 }
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001636 free(path);
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001637 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szerediab974562005-04-07 15:40:21 +00001638 } else
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001639 send_reply(f, in, 0, &outarg, SIZEOF_COMPAT(f, fuse_open_out));
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001640}
1641
Miklos Szerediab974562005-04-07 15:40:21 +00001642static int fill_dir_common(struct fuse_dirhandle *dh, const char *name,
1643 int type, ino_t ino, off_t off)
1644{
1645 unsigned namelen = strlen(name);
1646 unsigned entlen;
1647 unsigned entsize;
1648 unsigned padlen;
1649 unsigned newlen;
1650 unsigned char *newptr;
1651
1652 if (!(dh->fuse->flags & FUSE_USE_INO))
1653 ino = (ino_t) -1;
1654
1655 if (namelen > FUSE_NAME_MAX)
1656 namelen = FUSE_NAME_MAX;
1657 else if (!namelen) {
1658 dh->error = -EIO;
1659 return 1;
1660 }
1661
1662 entlen = (dh->fuse->major == 5 ?
1663 FUSE_NAME_OFFSET_COMPAT5 : FUSE_NAME_OFFSET) + namelen;
1664 entsize = FUSE_DIRENT_ALIGN(entlen);
1665 padlen = entsize - entlen;
1666 newlen = dh->len + entsize;
1667 if (off && dh->fuse->major != 5) {
1668 dh->filled = 0;
1669 if (newlen > dh->needlen)
1670 return 1;
1671 }
Miklos Szeredi21019c92005-05-09 11:22:41 +00001672
Miklos Szerediab974562005-04-07 15:40:21 +00001673 newptr = realloc(dh->contents, newlen);
1674 if (!newptr) {
1675 dh->error = -ENOMEM;
1676 return 1;
1677 }
1678 dh->contents = newptr;
1679 if (dh->fuse->major == 5) {
1680 struct fuse_dirent_compat5 *dirent;
1681 dirent = (struct fuse_dirent_compat5 *) (dh->contents + dh->len);
1682 dirent->ino = ino;
1683 dirent->namelen = namelen;
1684 dirent->type = type;
1685 strncpy(dirent->name, name, namelen);
1686 } else {
1687 struct fuse_dirent *dirent;
1688 dirent = (struct fuse_dirent *) (dh->contents + dh->len);
1689 dirent->ino = ino;
1690 dirent->off = off ? off : newlen;
1691 dirent->namelen = namelen;
1692 dirent->type = type;
1693 strncpy(dirent->name, name, namelen);
1694 }
1695 if (padlen)
1696 memset(dh->contents + dh->len + entlen, 0, padlen);
1697 dh->len = newlen;
1698 return 0;
1699}
1700
1701static int fill_dir(void *buf, const char *name, const struct stat *stat,
1702 off_t off)
1703{
1704 int type = stat ? (stat->st_mode & 0170000) >> 12 : 0;
1705 ino_t ino = stat ? stat->st_ino : (ino_t) -1;
1706 return fill_dir_common(buf, name, type, ino, off);
1707}
1708
1709static int fill_dir_old(struct fuse_dirhandle *dh, const char *name, int type,
1710 ino_t ino)
1711{
1712 fill_dir_common(dh, name, type, ino, 0);
1713 return dh->error;
1714}
1715
1716static int readdir_fill(struct fuse *f, struct fuse_in_header *in,
1717 struct fuse_read_in *arg, struct fuse_dirhandle *dh)
1718{
1719 int err = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001720 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szerediab974562005-04-07 15:40:21 +00001721 char *path = get_path(f, in->nodeid);
1722 if (path != NULL) {
1723 struct fuse_file_info fi;
1724
1725 memset(&fi, 0, sizeof(fi));
1726 fi.fh = dh->fh;
1727
1728 dh->len = 0;
1729 dh->error = 0;
1730 dh->needlen = arg->size;
1731 dh->filled = 1;
1732 err = -ENOSYS;
1733 if (f->op.readdir) {
1734 off_t offset = f->major == 5 ? 0 : arg->offset;
1735 err = f->op.readdir(path, dh, fill_dir, offset, &fi);
1736 } else if (f->op.getdir)
1737 err = f->op.getdir(path, dh, fill_dir_old);
1738 if (!err)
1739 err = dh->error;
1740 if (err)
1741 dh->filled = 0;
1742 free(path);
1743 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001744 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szerediab974562005-04-07 15:40:21 +00001745 return err;
1746}
1747
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001748static void do_readdir(struct fuse *f, struct fuse_in_header *in,
1749 struct fuse_read_in *arg)
1750{
Miklos Szerediab974562005-04-07 15:40:21 +00001751 int err = 0;
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001752 struct fuse_dirhandle *dh = get_dirhandle(arg->fh);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001753 size_t size = 0;
Miklos Szerediab974562005-04-07 15:40:21 +00001754 unsigned char *buf = NULL;
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001755
Miklos Szerediab974562005-04-07 15:40:21 +00001756 pthread_mutex_lock(&dh->lock);
1757 if (!dh->filled) {
1758 err = readdir_fill(f, in, arg, dh);
1759 if (err)
1760 goto out;
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001761 }
Miklos Szerediab974562005-04-07 15:40:21 +00001762 if (dh->filled) {
Miklos Szeredib92d9782005-02-07 16:10:49 +00001763 if (arg->offset < dh->len) {
1764 size = arg->size;
1765 if (arg->offset + size > dh->len)
1766 size = dh->len - arg->offset;
Miklos Szerediab974562005-04-07 15:40:21 +00001767 buf = dh->contents + arg->offset;
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001768 }
Miklos Szerediab974562005-04-07 15:40:21 +00001769 } else {
1770 size = dh->len;
1771 buf = dh->contents;
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001772 }
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001773
Miklos Szerediab974562005-04-07 15:40:21 +00001774 out:
1775 send_reply(f, in, err, buf, size);
1776 pthread_mutex_unlock(&dh->lock);
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001777}
1778
1779static void do_releasedir(struct fuse *f, struct fuse_in_header *in,
1780 struct fuse_release_in *arg)
1781{
1782 struct fuse_dirhandle *dh = get_dirhandle(arg->fh);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001783 if (f->op.releasedir) {
1784 char *path;
1785 struct fuse_file_info fi;
Miklos Szerediab974562005-04-07 15:40:21 +00001786
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001787 memset(&fi, 0, sizeof(fi));
1788 fi.fh = dh->fh;
1789
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001790 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001791 path = get_path(f, in->nodeid);
1792 f->op.releasedir(path ? path : "-", &fi);
1793 free(path);
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001794 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001795 }
Miklos Szerediab974562005-04-07 15:40:21 +00001796 pthread_mutex_lock(&dh->lock);
1797 pthread_mutex_unlock(&dh->lock);
1798 pthread_mutex_destroy(&dh->lock);
Miklos Szeredib92d9782005-02-07 16:10:49 +00001799 free(dh->contents);
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001800 free(dh);
1801 send_reply(f, in, 0, NULL, 0);
1802}
1803
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001804static void do_fsyncdir(struct fuse *f, struct fuse_in_header *in,
1805 struct fuse_fsync_in *inarg)
1806{
1807 int res;
1808 char *path;
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001809 struct fuse_dirhandle *dh = get_dirhandle(inarg->fh);
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001810 struct fuse_file_info fi;
1811
1812 memset(&fi, 0, sizeof(fi));
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001813 fi.fh = dh->fh;
1814
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001815 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001816 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001817 path = get_path(f, in->nodeid);
1818 if (path != NULL) {
1819 res = -ENOSYS;
1820 if (f->op.fsyncdir)
1821 res = f->op.fsyncdir(path, inarg->fsync_flags & 1, &fi);
1822 free(path);
1823 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001824 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001825 send_reply(f, in, res, NULL, 0);
1826}
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001827
Miklos Szeredi43696432001-11-18 19:15:05 +00001828static void free_cmd(struct fuse_cmd *cmd)
1829{
1830 free(cmd->buf);
1831 free(cmd);
1832}
1833
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001834void fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd)
Miklos Szeredia181e612001-11-06 12:03:23 +00001835{
Miklos Szeredia181e612001-11-06 12:03:23 +00001836 struct fuse_in_header *in = (struct fuse_in_header *) cmd->buf;
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001837 void *inarg = cmd->buf + SIZEOF_COMPAT(f, fuse_in_header);
Miklos Szeredid169f312004-09-22 08:48:26 +00001838 struct fuse_context *ctx = fuse_get_context();
Miklos Szeredia181e612001-11-06 12:03:23 +00001839
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001840 fuse_dec_avail(f);
Miklos Szeredi33232032001-11-19 17:55:51 +00001841
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001842 if ((f->flags & FUSE_DEBUG)) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001843 printf("unique: %llu, opcode: %s (%i), nodeid: %lu, insize: %i\n",
1844 in->unique, opname(in->opcode), in->opcode,
1845 (unsigned long) in->nodeid, cmd->buflen);
Miklos Szeredic0938ea2001-11-07 12:35:06 +00001846 fflush(stdout);
1847 }
Miklos Szeredife25def2001-12-20 15:38:05 +00001848
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001849 if (!f->got_init && in->opcode != FUSE_INIT) {
1850 /* Old kernel version probably */
1851 send_reply(f, in, -EPROTO, NULL, 0);
1852 goto out;
1853 }
1854
Miklos Szeredi0111f9d2005-04-22 12:04:55 +00001855 if ((f->flags & FUSE_ALLOW_ROOT) && in->uid != f->owner && in->uid != 0 &&
1856 in->opcode != FUSE_INIT && in->opcode != FUSE_READ &&
Miklos Szeredi21019c92005-05-09 11:22:41 +00001857 in->opcode != FUSE_WRITE && in->opcode != FUSE_FSYNC &&
Miklos Szeredi0111f9d2005-04-22 12:04:55 +00001858 in->opcode != FUSE_RELEASE && in->opcode != FUSE_READDIR &&
1859 in->opcode != FUSE_FSYNCDIR && in->opcode != FUSE_RELEASEDIR) {
1860 send_reply(f, in, -EACCES, NULL, 0);
1861 goto out;
1862 }
1863
Miklos Szeredid169f312004-09-22 08:48:26 +00001864 ctx->fuse = f;
Miklos Szeredife25def2001-12-20 15:38:05 +00001865 ctx->uid = in->uid;
1866 ctx->gid = in->gid;
Miklos Szeredi1f18db52004-09-27 06:54:49 +00001867 ctx->pid = in->pid;
Miklos Szeredi159bd7e2005-02-28 17:32:16 +00001868 ctx->private_data = f->user_data;
Miklos Szeredie5183742005-02-02 11:14:04 +00001869
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001870 switch (in->opcode) {
Miklos Szeredia181e612001-11-06 12:03:23 +00001871 case FUSE_LOOKUP:
1872 do_lookup(f, in, (char *) inarg);
1873 break;
1874
Miklos Szeredia181e612001-11-06 12:03:23 +00001875 case FUSE_GETATTR:
1876 do_getattr(f, in);
1877 break;
1878
1879 case FUSE_SETATTR:
1880 do_setattr(f, in, (struct fuse_setattr_in *) inarg);
1881 break;
1882
1883 case FUSE_READLINK:
1884 do_readlink(f, in);
1885 break;
1886
Miklos Szeredia181e612001-11-06 12:03:23 +00001887 case FUSE_MKNOD:
1888 do_mknod(f, in, (struct fuse_mknod_in *) inarg);
1889 break;
Miklos Szeredie5183742005-02-02 11:14:04 +00001890
Miklos Szeredia181e612001-11-06 12:03:23 +00001891 case FUSE_MKDIR:
1892 do_mkdir(f, in, (struct fuse_mkdir_in *) inarg);
1893 break;
Miklos Szeredie5183742005-02-02 11:14:04 +00001894
Miklos Szeredia181e612001-11-06 12:03:23 +00001895 case FUSE_UNLINK:
Miklos Szeredib5958612004-02-20 14:10:49 +00001896 do_unlink(f, in, (char *) inarg);
1897 break;
1898
Miklos Szeredia181e612001-11-06 12:03:23 +00001899 case FUSE_RMDIR:
Miklos Szeredib5958612004-02-20 14:10:49 +00001900 do_rmdir(f, in, (char *) inarg);
Miklos Szeredia181e612001-11-06 12:03:23 +00001901 break;
1902
1903 case FUSE_SYMLINK:
Miklos Szeredie5183742005-02-02 11:14:04 +00001904 do_symlink(f, in, (char *) inarg,
Miklos Szeredia181e612001-11-06 12:03:23 +00001905 ((char *) inarg) + strlen((char *) inarg) + 1);
1906 break;
1907
1908 case FUSE_RENAME:
1909 do_rename(f, in, (struct fuse_rename_in *) inarg);
1910 break;
Miklos Szeredie5183742005-02-02 11:14:04 +00001911
Miklos Szeredia181e612001-11-06 12:03:23 +00001912 case FUSE_LINK:
1913 do_link(f, in, (struct fuse_link_in *) inarg);
1914 break;
1915
1916 case FUSE_OPEN:
1917 do_open(f, in, (struct fuse_open_in *) inarg);
1918 break;
1919
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001920 case FUSE_FLUSH:
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001921 do_flush(f, in, (struct fuse_flush_in *) inarg);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001922 break;
1923
Miklos Szeredi9478e862002-12-11 09:50:26 +00001924 case FUSE_RELEASE:
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001925 do_release(f, in, (struct fuse_release_in *) inarg);
Miklos Szeredi9478e862002-12-11 09:50:26 +00001926 break;
1927
Miklos Szeredia181e612001-11-06 12:03:23 +00001928 case FUSE_READ:
1929 do_read(f, in, (struct fuse_read_in *) inarg);
1930 break;
1931
1932 case FUSE_WRITE:
1933 do_write(f, in, (struct fuse_write_in *) inarg);
1934 break;
1935
Mark Glinesd84b39a2002-01-07 16:32:02 +00001936 case FUSE_STATFS:
1937 do_statfs(f, in);
1938 break;
1939
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001940 case FUSE_FSYNC:
1941 do_fsync(f, in, (struct fuse_fsync_in *) inarg);
1942 break;
1943
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001944 case FUSE_SETXATTR:
1945 do_setxattr(f, in, (struct fuse_setxattr_in *) inarg);
1946 break;
1947
1948 case FUSE_GETXATTR:
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001949 do_getxattr(f, in, (struct fuse_getxattr_in *) inarg);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001950 break;
1951
1952 case FUSE_LISTXATTR:
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001953 do_listxattr(f, in, (struct fuse_getxattr_in *) inarg);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001954 break;
1955
1956 case FUSE_REMOVEXATTR:
1957 do_removexattr(f, in, (char *) inarg);
1958 break;
1959
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001960 case FUSE_INIT:
1961 do_init(f, in, (struct fuse_init_in_out *) inarg);
1962 break;
1963
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001964 case FUSE_OPENDIR:
1965 do_opendir(f, in, (struct fuse_open_in *) inarg);
1966 break;
1967
1968 case FUSE_READDIR:
1969 do_readdir(f, in, (struct fuse_read_in *) inarg);
1970 break;
1971
1972 case FUSE_RELEASEDIR:
1973 do_releasedir(f, in, (struct fuse_release_in *) inarg);
1974 break;
1975
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001976 case FUSE_FSYNCDIR:
1977 do_fsyncdir(f, in, (struct fuse_fsync_in *) inarg);
1978 break;
1979
Miklos Szeredia181e612001-11-06 12:03:23 +00001980 default:
Miklos Szeredi43696432001-11-18 19:15:05 +00001981 send_reply(f, in, -ENOSYS, NULL, 0);
Miklos Szeredia181e612001-11-06 12:03:23 +00001982 }
Miklos Szeredi43696432001-11-18 19:15:05 +00001983
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001984 out:
Miklos Szeredi43696432001-11-18 19:15:05 +00001985 free_cmd(cmd);
Miklos Szeredia181e612001-11-06 12:03:23 +00001986}
1987
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001988int fuse_exited(struct fuse* f)
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001989{
1990 return f->exited;
1991}
1992
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001993struct fuse_cmd *fuse_read_cmd(struct fuse *f)
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001994{
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001995 ssize_t res;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001996 struct fuse_cmd *cmd;
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001997 struct fuse_in_header *in;
1998 void *inarg;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001999
Miklos Szeredi43696432001-11-18 19:15:05 +00002000 cmd = (struct fuse_cmd *) malloc(sizeof(struct fuse_cmd));
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002001 if (cmd == NULL) {
2002 fprintf(stderr, "fuse: failed to allocate cmd in read\n");
2003 return NULL;
2004 }
Miklos Szeredi43696432001-11-18 19:15:05 +00002005 cmd->buf = (char *) malloc(FUSE_MAX_IN);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002006 if (cmd->buf == NULL) {
2007 fprintf(stderr, "fuse: failed to allocate read buffer\n");
2008 free(cmd);
2009 return NULL;
2010 }
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00002011 in = (struct fuse_in_header *) cmd->buf;
Miklos Szeredi30e093a2005-04-03 17:44:54 +00002012 inarg = cmd->buf + SIZEOF_COMPAT(f, fuse_in_header);
Miklos Szeredi43696432001-11-18 19:15:05 +00002013
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00002014 res = read(f->fd, cmd->buf, FUSE_MAX_IN);
2015 if (res == -1) {
2016 free_cmd(cmd);
Miklos Szeredie56818b2004-12-12 11:45:24 +00002017 if (fuse_exited(f) || errno == EINTR || errno == ENOENT)
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00002018 return NULL;
Miklos Szeredie5183742005-02-02 11:14:04 +00002019
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00002020 /* ENODEV means we got unmounted, so we silenty return failure */
2021 if (errno != ENODEV) {
2022 /* BAD... This will happen again */
2023 perror("fuse: reading device");
2024 }
Miklos Szeredie5183742005-02-02 11:14:04 +00002025
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00002026 fuse_exit(f);
2027 return NULL;
2028 }
Miklos Szeredi30e093a2005-04-03 17:44:54 +00002029 if ((size_t) res < SIZEOF_COMPAT(f, fuse_in_header)) {
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00002030 free_cmd(cmd);
2031 /* Cannot happen */
2032 fprintf(stderr, "short read on fuse device\n");
2033 fuse_exit(f);
2034 return NULL;
2035 }
2036 cmd->buflen = res;
Miklos Szeredie5183742005-02-02 11:14:04 +00002037
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00002038 /* Forget is special, it can be done without messing with threads. */
2039 if (in->opcode == FUSE_FORGET) {
2040 do_forget(f, in, (struct fuse_forget_in *) inarg);
2041 free_cmd(cmd);
2042 return NULL;
2043 }
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00002044
Miklos Szeredifff56ab2001-11-16 10:12:59 +00002045 return cmd;
Miklos Szeredi2df1c042001-11-06 15:07:17 +00002046}
2047
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002048int fuse_loop(struct fuse *f)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002049{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00002050 if (f == NULL)
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002051 return -1;
Miklos Szeredic40748a2004-02-20 16:38:45 +00002052
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00002053 while (1) {
Miklos Szeredi0f48a262002-12-05 14:23:01 +00002054 struct fuse_cmd *cmd;
2055
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002056 if (fuse_exited(f))
Miklos Szeredi874e3c12004-11-01 23:15:20 +00002057 break;
Miklos Szeredi0f48a262002-12-05 14:23:01 +00002058
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002059 cmd = fuse_read_cmd(f);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00002060 if (cmd == NULL)
Miklos Szeredi0f48a262002-12-05 14:23:01 +00002061 continue;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002062
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002063 fuse_process_cmd(f, cmd);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002064 }
Miklos Szeredi874e3c12004-11-01 23:15:20 +00002065 f->exited = 0;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002066 return 0;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002067}
2068
Miklos Szeredi891b8742004-07-29 09:27:49 +00002069int fuse_invalidate(struct fuse *f, const char *path)
2070{
Miklos Szeredie56818b2004-12-12 11:45:24 +00002071 (void) f;
2072 (void) path;
2073 return -EINVAL;
Miklos Szeredi891b8742004-07-29 09:27:49 +00002074}
2075
Miklos Szeredi0f48a262002-12-05 14:23:01 +00002076void fuse_exit(struct fuse *f)
2077{
2078 f->exited = 1;
2079}
2080
Miklos Szeredid169f312004-09-22 08:48:26 +00002081struct fuse_context *fuse_get_context()
Miklos Szeredi2e50d432001-12-20 12:17:25 +00002082{
Miklos Szeredid169f312004-09-22 08:48:26 +00002083 static struct fuse_context context;
2084 if (fuse_getcontext)
2085 return fuse_getcontext();
Miklos Szeredi2e50d432001-12-20 12:17:25 +00002086 else
Miklos Szeredid169f312004-09-22 08:48:26 +00002087 return &context;
2088}
2089
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002090void fuse_set_getcontext_func(struct fuse_context *(*func)(void))
Miklos Szeredid169f312004-09-22 08:48:26 +00002091{
2092 fuse_getcontext = func;
Miklos Szeredi2e50d432001-12-20 12:17:25 +00002093}
2094
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002095int fuse_is_lib_option(const char *opt)
2096{
2097 if (strcmp(opt, "debug") == 0 ||
Miklos Szeredia13d9002004-11-02 17:32:03 +00002098 strcmp(opt, "hard_remove") == 0 ||
Miklos Szeredi0111f9d2005-04-22 12:04:55 +00002099 strcmp(opt, "use_ino") == 0 ||
2100 strcmp(opt, "allow_root") == 0)
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002101 return 1;
2102 else
2103 return 0;
2104}
2105
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002106static int parse_lib_opts(struct fuse *f, const char *opts)
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002107{
2108 if (opts) {
2109 char *xopts = strdup(opts);
2110 char *s = xopts;
2111 char *opt;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002112
Miklos Szeredie56818b2004-12-12 11:45:24 +00002113 if (xopts == NULL) {
2114 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002115 return -1;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002116 }
Miklos Szeredie5183742005-02-02 11:14:04 +00002117
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002118 while((opt = strsep(&s, ","))) {
2119 if (strcmp(opt, "debug") == 0)
2120 f->flags |= FUSE_DEBUG;
2121 else if (strcmp(opt, "hard_remove") == 0)
2122 f->flags |= FUSE_HARD_REMOVE;
Miklos Szeredia13d9002004-11-02 17:32:03 +00002123 else if (strcmp(opt, "use_ino") == 0)
2124 f->flags |= FUSE_USE_INO;
Miklos Szeredi0111f9d2005-04-22 12:04:55 +00002125 else if (strcmp(opt, "allow_root") == 0)
2126 f->flags |= FUSE_ALLOW_ROOT;
Miklos Szeredie5183742005-02-02 11:14:04 +00002127 else
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002128 fprintf(stderr, "fuse: warning: unknown option `%s'\n", opt);
2129 }
2130 free(xopts);
2131 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002132 return 0;
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002133}
2134
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002135struct fuse *fuse_new_common(int fd, const char *opts,
2136 const struct fuse_operations *op,
2137 size_t op_size, int compat)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002138{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002139 struct fuse *f;
2140 struct node *root;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002141
Miklos Szeredi0f62d722005-01-04 12:45:54 +00002142 if (sizeof(struct fuse_operations) < op_size) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002143 fprintf(stderr, "fuse: warning: library too old, some operations may not not work\n");
Miklos Szeredi0f62d722005-01-04 12:45:54 +00002144 op_size = sizeof(struct fuse_operations);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002145 }
2146
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002147 f = (struct fuse *) calloc(1, sizeof(struct fuse));
Miklos Szeredie56818b2004-12-12 11:45:24 +00002148 if (f == NULL) {
2149 fprintf(stderr, "fuse: failed to allocate fuse object\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002150 goto out;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002151 }
Miklos Szeredi2df1c042001-11-06 15:07:17 +00002152
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002153 if (parse_lib_opts(f, opts) == -1)
2154 goto out_free;
2155
Miklos Szeredi8cffdb92001-11-09 14:49:18 +00002156 f->fd = fd;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002157 f->ctr = 0;
Miklos Szeredi76f65782004-02-19 16:55:40 +00002158 f->generation = 0;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00002159 /* FIXME: Dynamic hash table */
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002160 f->name_table_size = 14057;
2161 f->name_table = (struct node **)
2162 calloc(1, sizeof(struct node *) * f->name_table_size);
Miklos Szeredie56818b2004-12-12 11:45:24 +00002163 if (f->name_table == NULL) {
2164 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002165 goto out_free;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002166 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002167
Miklos Szeredia13d9002004-11-02 17:32:03 +00002168 f->id_table_size = 14057;
2169 f->id_table = (struct node **)
2170 calloc(1, sizeof(struct node *) * f->id_table_size);
Miklos Szeredie56818b2004-12-12 11:45:24 +00002171 if (f->id_table == NULL) {
2172 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002173 goto out_free_name_table;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002174 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002175
Miklos Szerediab974562005-04-07 15:40:21 +00002176 mutex_init(&f->lock);
2177 mutex_init(&f->worker_lock);
Miklos Szeredi33232032001-11-19 17:55:51 +00002178 f->numworker = 0;
2179 f->numavail = 0;
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002180 memcpy(&f->op, op, op_size);
2181 f->compat = compat;
Miklos Szeredi0f48a262002-12-05 14:23:01 +00002182 f->exited = 0;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002183
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002184 root = (struct node *) calloc(1, sizeof(struct node));
Miklos Szeredie56818b2004-12-12 11:45:24 +00002185 if (root == NULL) {
2186 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredia13d9002004-11-02 17:32:03 +00002187 goto out_free_id_table;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002188 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002189
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002190 root->name = strdup("/");
Miklos Szeredie56818b2004-12-12 11:45:24 +00002191 if (root->name == NULL) {
2192 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002193 goto out_free_root;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002194 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002195
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002196 root->parent = 0;
Miklos Szeredia13d9002004-11-02 17:32:03 +00002197 root->nodeid = FUSE_ROOT_ID;
Miklos Szeredi76f65782004-02-19 16:55:40 +00002198 root->generation = 0;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00002199 root->refctr = 1;
Miklos Szeredi38009022005-05-08 19:47:22 +00002200 root->nlookup = 1;
Miklos Szeredia13d9002004-11-02 17:32:03 +00002201 hash_id(f, root);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002202
Miklos Szeredi0111f9d2005-04-22 12:04:55 +00002203 f->owner = getuid();
2204
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002205 return f;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002206
2207 out_free_root:
2208 free(root);
Miklos Szeredia13d9002004-11-02 17:32:03 +00002209 out_free_id_table:
2210 free(f->id_table);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002211 out_free_name_table:
2212 free(f->name_table);
2213 out_free:
2214 free(f);
2215 out:
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002216 return NULL;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002217}
2218
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002219struct fuse *fuse_new(int fd, const char *opts,
2220 const struct fuse_operations *op, size_t op_size)
2221{
2222 return fuse_new_common(fd, opts, op, op_size, 0);
2223}
2224
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002225struct fuse *fuse_new_compat2(int fd, const char *opts,
2226 const struct fuse_operations_compat2 *op)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002227{
2228 return fuse_new_common(fd, opts, (struct fuse_operations *) op,
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002229 sizeof(struct fuse_operations_compat2), 21);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002230}
2231
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002232struct fuse *fuse_new_compat1(int fd, int flags,
2233 const struct fuse_operations_compat1 *op)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002234{
2235 char *opts = NULL;
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002236 if (flags & FUSE_DEBUG_COMPAT1)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002237 opts = "debug";
2238 return fuse_new_common(fd, opts, (struct fuse_operations *) op,
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002239 sizeof(struct fuse_operations_compat1), 11);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002240}
2241
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002242void fuse_destroy(struct fuse *f)
2243{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002244 size_t i;
Miklos Szeredia13d9002004-11-02 17:32:03 +00002245 for (i = 0; i < f->id_table_size; i++) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002246 struct node *node;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002247
Miklos Szeredia13d9002004-11-02 17:32:03 +00002248 for (node = f->id_table[i]; node != NULL; node = node->id_next) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002249 if (node->is_hidden) {
Miklos Szeredia13d9002004-11-02 17:32:03 +00002250 char *path = get_path(f, node->nodeid);
Miklos Szeredi21019c92005-05-09 11:22:41 +00002251 if (path) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002252 f->op.unlink(path);
Miklos Szeredi21019c92005-05-09 11:22:41 +00002253 free(path);
2254 }
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002255 }
2256 }
2257 }
Miklos Szeredia13d9002004-11-02 17:32:03 +00002258 for (i = 0; i < f->id_table_size; i++) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002259 struct node *node;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002260 struct node *next;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002261
Miklos Szeredia13d9002004-11-02 17:32:03 +00002262 for (node = f->id_table[i]; node != NULL; node = next) {
2263 next = node->id_next;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002264 free_node(node);
2265 }
2266 }
Miklos Szeredia13d9002004-11-02 17:32:03 +00002267 free(f->id_table);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002268 free(f->name_table);
Miklos Szeredia181e612001-11-06 12:03:23 +00002269 pthread_mutex_destroy(&f->lock);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00002270 pthread_mutex_destroy(&f->worker_lock);
Miklos Szeredi159bd7e2005-02-28 17:32:16 +00002271 if (f->op.destroy)
2272 f->op.destroy(f->user_data);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002273 free(f);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002274}
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002275
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002276__asm__(".symver fuse_exited,__fuse_exited@");
2277__asm__(".symver fuse_process_cmd,__fuse_process_cmd@");
2278__asm__(".symver fuse_read_cmd,__fuse_read_cmd@");
2279__asm__(".symver fuse_set_getcontext_func,__fuse_set_getcontext_func@");
2280__asm__(".symver fuse_new_compat2,fuse_new@");