blob: 45ea1b35293c863e305841baddf377ba41ee1c50 [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 Szeredi33be22d2005-05-27 09:12:43 +000040/** Make a best effort to fill in inode number in a readdir **/
41#define FUSE_READDIR_INO (1 << 5)
Miklos Szeredia25d4c22004-11-23 22:32:16 +000042
Miklos Szeredie331c4b2005-07-06 13:34:02 +000043/** Ignore file mode supplied by the filesystem, and create one based
44 on the 'umask' option */
45#define FUSE_SET_MODE (1 << 6)
46
47/** Ignore st_uid supplied by the filesystem and set it based on the
48 'uid' option*/
49#define FUSE_SET_UID (1 << 7)
50
51/** Ignore st_gid supplied by the filesystem and set it based on the
52 'gid' option*/
53#define FUSE_SET_GID (1 << 8)
54
55
Miklos Szeredi97c61e92001-11-07 12:09:43 +000056#define FUSE_MAX_PATH 4096
Miklos Szeredi30e093a2005-04-03 17:44:54 +000057#define PARAM_T(inarg, type) (((char *)(inarg)) + sizeof(type))
58#define PARAM(inarg) PARAM_T(inarg, *(inarg))
59#define PARAM_COMPAT(f, inarg, type) \
60 ((f)->major == 5 ? PARAM_T(inarg, struct type ## _compat5) : PARAM(inarg))
61
62#define MEMBER_COMPAT(f, ptr, memb, type) \
63 ((f)->major == 5 ? &((struct type ## _compat5 *) (ptr))->memb : &ptr->memb)
64
65#define SIZEOF_COMPAT(f, type) \
66 ((f)->major == 5 ? sizeof(struct type ## _compat5) : sizeof(struct type))
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000067
Miklos Szeredi254d5ed2004-03-02 11:11:24 +000068#define ENTRY_REVALIDATE_TIME 1 /* sec */
69#define ATTR_REVALIDATE_TIME 1 /* sec */
70
Miklos Szeredi0f62d722005-01-04 12:45:54 +000071
72struct node {
73 struct node *name_next;
74 struct node *id_next;
75 nodeid_t nodeid;
76 unsigned int generation;
77 int refctr;
78 nodeid_t parent;
79 char *name;
80 uint64_t version;
Miklos Szeredi38009022005-05-08 19:47:22 +000081 uint64_t nlookup;
Miklos Szeredi0f62d722005-01-04 12:45:54 +000082 int open_count;
83 int is_hidden;
84};
85
86struct fuse_dirhandle {
Miklos Szerediab974562005-04-07 15:40:21 +000087 pthread_mutex_t lock;
Miklos Szeredi0f62d722005-01-04 12:45:54 +000088 struct fuse *fuse;
Miklos Szeredib92d9782005-02-07 16:10:49 +000089 unsigned char *contents;
Miklos Szerediab974562005-04-07 15:40:21 +000090 int allocated;
Miklos Szeredib92d9782005-02-07 16:10:49 +000091 unsigned len;
Miklos Szerediab974562005-04-07 15:40:21 +000092 unsigned needlen;
Miklos Szeredi3ead28e2005-01-18 21:23:41 +000093 int filled;
Miklos Szerediede1f7a2005-04-01 21:08:57 +000094 unsigned long fh;
Miklos Szerediab974562005-04-07 15:40:21 +000095 int error;
Miklos Szeredi340d21f2005-07-06 10:07:52 +000096 nodeid_t nodeid;
Miklos Szeredi0f62d722005-01-04 12:45:54 +000097};
98
99struct fuse_cmd {
100 char *buf;
101 size_t buflen;
102};
103
104
Miklos Szeredid169f312004-09-22 08:48:26 +0000105static struct fuse_context *(*fuse_getcontext)(void) = NULL;
106
Miklos Szerediab974562005-04-07 15:40:21 +0000107#ifndef USE_UCLIBC
108#define mutex_init(mut) pthread_mutex_init(mut, NULL)
109#else
Miklos Szeredi129ef8f2005-06-20 13:48:42 +0000110static void mutex_init(pthread_mutex_t *mut)
Miklos Szerediab974562005-04-07 15:40:21 +0000111{
112 pthread_mutexattr_t attr;
113 pthread_mutexattr_init(&attr);
114 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
115 pthread_mutex_init(mut, &attr);
116 pthread_mutexattr_destroy(&attr);
117}
118#endif
119
Miklos Szeredic8ba2372002-12-10 12:26:00 +0000120static const char *opname(enum fuse_opcode opcode)
121{
Miklos Szeredie5183742005-02-02 11:14:04 +0000122 switch (opcode) {
Miklos Szeredi3ed84232004-03-30 15:17:26 +0000123 case FUSE_LOOKUP: return "LOOKUP";
124 case FUSE_FORGET: return "FORGET";
125 case FUSE_GETATTR: return "GETATTR";
126 case FUSE_SETATTR: return "SETATTR";
127 case FUSE_READLINK: return "READLINK";
128 case FUSE_SYMLINK: return "SYMLINK";
Miklos Szeredi3ed84232004-03-30 15:17:26 +0000129 case FUSE_MKNOD: return "MKNOD";
130 case FUSE_MKDIR: return "MKDIR";
131 case FUSE_UNLINK: return "UNLINK";
132 case FUSE_RMDIR: return "RMDIR";
133 case FUSE_RENAME: return "RENAME";
134 case FUSE_LINK: return "LINK";
135 case FUSE_OPEN: return "OPEN";
136 case FUSE_READ: return "READ";
137 case FUSE_WRITE: return "WRITE";
138 case FUSE_STATFS: return "STATFS";
Miklos Szeredi99f20742004-05-19 08:01:10 +0000139 case FUSE_FLUSH: return "FLUSH";
Miklos Szeredi3ed84232004-03-30 15:17:26 +0000140 case FUSE_RELEASE: return "RELEASE";
141 case FUSE_FSYNC: return "FSYNC";
142 case FUSE_SETXATTR: return "SETXATTR";
143 case FUSE_GETXATTR: return "GETXATTR";
144 case FUSE_LISTXATTR: return "LISTXATTR";
145 case FUSE_REMOVEXATTR: return "REMOVEXATTR";
Miklos Szeredi3f0005f2005-01-04 19:24:31 +0000146 case FUSE_INIT: return "INIT";
Miklos Szeredi3ead28e2005-01-18 21:23:41 +0000147 case FUSE_OPENDIR: return "OPENDIR";
148 case FUSE_READDIR: return "READDIR";
149 case FUSE_RELEASEDIR: return "RELEASEDIR";
Miklos Szeredi4283ee72005-03-21 12:09:04 +0000150 case FUSE_FSYNCDIR: return "FSYNCDIR";
Miklos Szeredi99f20742004-05-19 08:01:10 +0000151 default: return "???";
Miklos Szeredic8ba2372002-12-10 12:26:00 +0000152 }
153}
154
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000155static inline void fuse_dec_avail(struct fuse *f)
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000156{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000157 pthread_mutex_lock(&f->worker_lock);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000158 f->numavail --;
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000159 pthread_mutex_unlock(&f->worker_lock);
160}
161
162static inline void fuse_inc_avail(struct fuse *f)
163{
164 pthread_mutex_lock(&f->worker_lock);
165 f->numavail ++;
166 pthread_mutex_unlock(&f->worker_lock);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000167}
168
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000169static struct node *get_node_nocheck(struct fuse *f, nodeid_t nodeid)
Miklos Szeredia181e612001-11-06 12:03:23 +0000170{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000171 size_t hash = nodeid % f->id_table_size;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000172 struct node *node;
173
Miklos Szeredia13d9002004-11-02 17:32:03 +0000174 for (node = f->id_table[hash]; node != NULL; node = node->id_next)
175 if (node->nodeid == nodeid)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000176 return node;
Miklos Szeredie5183742005-02-02 11:14:04 +0000177
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000178 return NULL;
Miklos Szeredia181e612001-11-06 12:03:23 +0000179}
180
Miklos Szeredia13d9002004-11-02 17:32:03 +0000181static struct node *get_node(struct fuse *f, nodeid_t nodeid)
Miklos Szeredia181e612001-11-06 12:03:23 +0000182{
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000183 struct node *node = get_node_nocheck(f, nodeid);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000184 if (!node) {
185 fprintf(stderr, "fuse internal error: node %lu not found\n",
186 nodeid);
187 abort();
188 }
189 return node;
Miklos Szeredia181e612001-11-06 12:03:23 +0000190}
191
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000192static void free_node(struct node *node)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000193{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000194 free(node->name);
195 free(node);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000196}
197
Miklos Szeredia13d9002004-11-02 17:32:03 +0000198static void unhash_id(struct fuse *f, struct node *node)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000199{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000200 size_t hash = node->nodeid % f->id_table_size;
201 struct node **nodep = &f->id_table[hash];
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000202
Miklos Szeredie5183742005-02-02 11:14:04 +0000203 for (; *nodep != NULL; nodep = &(*nodep)->id_next)
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000204 if (*nodep == node) {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000205 *nodep = node->id_next;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000206 return;
207 }
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000208}
209
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000210static void hash_id(struct fuse *f, struct node *node)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000211{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000212 size_t hash = node->nodeid % f->id_table_size;
213 node->id_next = f->id_table[hash];
Miklos Szeredie5183742005-02-02 11:14:04 +0000214 f->id_table[hash] = node;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000215}
216
Miklos Szeredia13d9002004-11-02 17:32:03 +0000217static unsigned int name_hash(struct fuse *f, nodeid_t parent, const char *name)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000218{
219 unsigned int hash = *name;
220
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000221 if (hash)
222 for (name += 1; *name != '\0'; name++)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000223 hash = (hash << 5) - hash + *name;
224
225 return (hash + parent) % f->name_table_size;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000226}
227
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000228static void unref_node(struct fuse *f, struct node *node);
229
230static void unhash_name(struct fuse *f, struct node *node)
231{
232 if (node->name) {
233 size_t hash = name_hash(f, node->parent, node->name);
234 struct node **nodep = &f->name_table[hash];
Miklos Szeredie5183742005-02-02 11:14:04 +0000235
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000236 for (; *nodep != NULL; nodep = &(*nodep)->name_next)
237 if (*nodep == node) {
238 *nodep = node->name_next;
239 node->name_next = NULL;
240 unref_node(f, get_node(f, node->parent));
241 free(node->name);
242 node->name = NULL;
243 node->parent = 0;
244 return;
245 }
246 fprintf(stderr, "fuse internal error: unable to unhash node: %lu\n",
247 node->nodeid);
248 abort();
249 }
250}
251
252static int hash_name(struct fuse *f, struct node *node, nodeid_t parent,
253 const char *name)
254{
255 size_t hash = name_hash(f, parent, name);
256 node->name = strdup(name);
257 if (node->name == NULL)
258 return -1;
259
260 get_node(f, parent)->refctr ++;
261 node->parent = parent;
262 node->name_next = f->name_table[hash];
263 f->name_table[hash] = node;
264 return 0;
265}
266
267static void delete_node(struct fuse *f, struct node *node)
268{
Miklos Szeredi38009022005-05-08 19:47:22 +0000269 if (f->flags & FUSE_DEBUG) {
270 printf("delete: %lu\n", node->nodeid);
271 fflush(stdout);
272 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000273 assert(!node->name);
274 unhash_id(f, node);
275 free_node(node);
276}
277
278static void unref_node(struct fuse *f, struct node *node)
279{
280 assert(node->refctr > 0);
281 node->refctr --;
282 if (!node->refctr)
283 delete_node(f, node);
284}
285
286static nodeid_t next_id(struct fuse *f)
287{
288 do {
289 f->ctr++;
290 if (!f->ctr)
291 f->generation ++;
292 } while (f->ctr == 0 || get_node_nocheck(f, f->ctr) != NULL);
293 return f->ctr;
294}
295
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000296static struct node *lookup_node(struct fuse *f, nodeid_t parent,
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000297 const char *name)
298{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000299 size_t hash = name_hash(f, parent, name);
300 struct node *node;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000301
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000302 for (node = f->name_table[hash]; node != NULL; node = node->name_next)
303 if (node->parent == parent && strcmp(node->name, name) == 0)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000304 return node;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000305
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000306 return NULL;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000307}
308
Miklos Szeredia13d9002004-11-02 17:32:03 +0000309static struct node *find_node(struct fuse *f, nodeid_t parent, char *name,
Miklos Szeredie331c4b2005-07-06 13:34:02 +0000310 uint64_t version)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000311{
312 struct node *node;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000313
Miklos Szeredia181e612001-11-06 12:03:23 +0000314 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000315 node = lookup_node(f, parent, name);
Miklos Szeredie331c4b2005-07-06 13:34:02 +0000316 if (node == NULL) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000317 node = (struct node *) calloc(1, sizeof(struct node));
318 if (node == NULL)
319 goto out_err;
Miklos Szeredie5183742005-02-02 11:14:04 +0000320
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000321 node->refctr = 1;
322 node->nodeid = next_id(f);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000323 node->open_count = 0;
324 node->is_hidden = 0;
325 node->generation = f->generation;
326 if (hash_name(f, node, parent, name) == -1) {
327 free(node);
328 node = NULL;
329 goto out_err;
330 }
331 hash_id(f, node);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000332 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000333 node->version = version;
Miklos Szeredi38009022005-05-08 19:47:22 +0000334 node->nlookup ++;
Miklos Szeredic2309912004-09-21 13:40:38 +0000335 out_err:
Miklos Szeredia181e612001-11-06 12:03:23 +0000336 pthread_mutex_unlock(&f->lock);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000337 return node;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000338}
339
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000340static char *add_name(char *buf, char *s, const char *name)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000341{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000342 size_t len = strlen(name);
343 s -= len;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000344 if (s <= buf) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000345 fprintf(stderr, "fuse: path too long: ...%s\n", s + len);
346 return NULL;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000347 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000348 strncpy(s, name, len);
349 s--;
350 *s = '/';
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000351
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000352 return s;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000353}
354
Miklos Szeredia13d9002004-11-02 17:32:03 +0000355static char *get_path_name(struct fuse *f, nodeid_t nodeid, const char *name)
Miklos Szeredib483c932001-10-29 14:57:57 +0000356{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000357 char buf[FUSE_MAX_PATH];
358 char *s = buf + FUSE_MAX_PATH - 1;
359 struct node *node;
Miklos Szeredie5183742005-02-02 11:14:04 +0000360
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000361 *s = '\0';
Miklos Szeredia181e612001-11-06 12:03:23 +0000362
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000363 if (name != NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000364 s = add_name(buf, s, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000365 if (s == NULL)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000366 return NULL;
367 }
368
369 pthread_mutex_lock(&f->lock);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000370 for (node = get_node(f, nodeid); node && node->nodeid != FUSE_ROOT_ID;
371 node = get_node(f, node->parent)) {
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000372 if (node->name == NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000373 s = NULL;
374 break;
375 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000376
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000377 s = add_name(buf, s, node->name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000378 if (s == NULL)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000379 break;
380 }
381 pthread_mutex_unlock(&f->lock);
382
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000383 if (node == NULL || s == NULL)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000384 return NULL;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000385 else if (*s == '\0')
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000386 return strdup("/");
387 else
388 return strdup(s);
389}
Miklos Szeredia181e612001-11-06 12:03:23 +0000390
Miklos Szeredia13d9002004-11-02 17:32:03 +0000391static char *get_path(struct fuse *f, nodeid_t nodeid)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000392{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000393 return get_path_name(f, nodeid, NULL);
Miklos Szeredib483c932001-10-29 14:57:57 +0000394}
395
Miklos Szeredi38009022005-05-08 19:47:22 +0000396static void forget_node(struct fuse *f, nodeid_t nodeid, uint64_t nlookup)
397{
398 struct node *node;
399 if (nodeid == FUSE_ROOT_ID)
400 return;
401 pthread_mutex_lock(&f->lock);
402 node = get_node(f, nodeid);
403 assert(node->nlookup >= nlookup);
404 node->nlookup -= nlookup;
405 if (!node->nlookup) {
406 unhash_name(f, node);
407 unref_node(f, node);
408 }
409 pthread_mutex_unlock(&f->lock);
410}
411
412static void forget_node_old(struct fuse *f, nodeid_t nodeid, uint64_t version)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000413{
Miklos Szeredia181e612001-11-06 12:03:23 +0000414 struct node *node;
415
416 pthread_mutex_lock(&f->lock);
Miklos Szeredid0cf1fb2005-05-06 10:10:38 +0000417 node = get_node_nocheck(f, nodeid);
418 if (node && node->version == version && nodeid != FUSE_ROOT_ID) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000419 node->version = 0;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000420 unhash_name(f, node);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000421 unref_node(f, node);
Miklos Szeredia181e612001-11-06 12:03:23 +0000422 }
423 pthread_mutex_unlock(&f->lock);
Miklos Szeredi38009022005-05-08 19:47:22 +0000424}
Miklos Szeredia181e612001-11-06 12:03:23 +0000425
Miklos Szeredi38009022005-05-08 19:47:22 +0000426static void cancel_lookup(struct fuse *f, nodeid_t nodeid, uint64_t version)
427{
428 if (f->major <= 6)
429 forget_node_old(f, nodeid, version);
430 else
431 forget_node(f, nodeid, 1);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000432}
433
Miklos Szeredia13d9002004-11-02 17:32:03 +0000434static void remove_node(struct fuse *f, nodeid_t dir, const char *name)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000435{
Miklos Szeredia181e612001-11-06 12:03:23 +0000436 struct node *node;
437
438 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000439 node = lookup_node(f, dir, name);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000440 if (node != NULL)
441 unhash_name(f, node);
Miklos Szeredia181e612001-11-06 12:03:23 +0000442 pthread_mutex_unlock(&f->lock);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000443}
444
Miklos Szeredia13d9002004-11-02 17:32:03 +0000445static int rename_node(struct fuse *f, nodeid_t olddir, const char *oldname,
446 nodeid_t newdir, const char *newname, int hide)
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000447{
Miklos Szeredia181e612001-11-06 12:03:23 +0000448 struct node *node;
449 struct node *newnode;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000450 int err = 0;
Miklos Szeredie5183742005-02-02 11:14:04 +0000451
Miklos Szeredia181e612001-11-06 12:03:23 +0000452 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000453 node = lookup_node(f, olddir, oldname);
454 newnode = lookup_node(f, newdir, newname);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000455 if (node == NULL)
456 goto out;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000457
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000458 if (newnode != NULL) {
459 if (hide) {
460 fprintf(stderr, "fuse: hidden file got created during hiding\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000461 err = -EBUSY;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000462 goto out;
463 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000464 unhash_name(f, newnode);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000465 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000466
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000467 unhash_name(f, node);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000468 if (hash_name(f, node, newdir, newname) == -1) {
469 err = -ENOMEM;
470 goto out;
471 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000472
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000473 if (hide)
474 node->is_hidden = 1;
475
476 out:
Miklos Szeredia181e612001-11-06 12:03:23 +0000477 pthread_mutex_unlock(&f->lock);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000478 return err;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000479}
480
Miklos Szeredie331c4b2005-07-06 13:34:02 +0000481static void convert_stat(struct fuse *f, nodeid_t nodeid, struct stat *stbuf,
482 struct fuse_attr *attr)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000483{
Miklos Szeredie331c4b2005-07-06 13:34:02 +0000484 attr->ino = (f->flags & FUSE_USE_INO) ? stbuf->st_ino : nodeid;
Miklos Szeredib5958612004-02-20 14:10:49 +0000485 attr->mode = stbuf->st_mode;
Miklos Szeredie331c4b2005-07-06 13:34:02 +0000486 if (f->flags & FUSE_SET_MODE)
487 attr->mode = (attr->mode & S_IFMT) | (0777 & ~f->umask);
Miklos Szeredib5958612004-02-20 14:10:49 +0000488 attr->nlink = stbuf->st_nlink;
Miklos Szeredie331c4b2005-07-06 13:34:02 +0000489 attr->uid = (f->flags & FUSE_SET_UID) ? f->uid : stbuf->st_uid;
490 attr->gid = (f->flags & FUSE_SET_GID) ? f->gid : stbuf->st_gid;
Miklos Szeredib5958612004-02-20 14:10:49 +0000491 attr->rdev = stbuf->st_rdev;
492 attr->size = stbuf->st_size;
493 attr->blocks = stbuf->st_blocks;
494 attr->atime = stbuf->st_atime;
Miklos Szeredib5958612004-02-20 14:10:49 +0000495 attr->mtime = stbuf->st_mtime;
Miklos Szeredib5958612004-02-20 14:10:49 +0000496 attr->ctime = stbuf->st_ctime;
Miklos Szeredicb264512004-06-23 18:52:50 +0000497#ifdef HAVE_STRUCT_STAT_ST_ATIM
498 attr->atimensec = stbuf->st_atim.tv_nsec;
499 attr->mtimensec = stbuf->st_mtim.tv_nsec;
Miklos Szeredib5958612004-02-20 14:10:49 +0000500 attr->ctimensec = stbuf->st_ctim.tv_nsec;
Miklos Szeredicb264512004-06-23 18:52:50 +0000501#endif
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000502}
503
Miklos Szerediab974562005-04-07 15:40:21 +0000504static size_t iov_length(const struct iovec *iov, size_t count)
Miklos Szerediede1f7a2005-04-01 21:08:57 +0000505{
Miklos Szerediab974562005-04-07 15:40:21 +0000506 size_t seg;
507 size_t ret = 0;
Miklos Szerediede1f7a2005-04-01 21:08:57 +0000508
Miklos Szerediab974562005-04-07 15:40:21 +0000509 for (seg = 0; seg < count; seg++)
510 ret += iov[seg].iov_len;
511 return ret;
Miklos Szerediede1f7a2005-04-01 21:08:57 +0000512}
513
Miklos Szerediab974562005-04-07 15:40:21 +0000514static int send_reply_raw(struct fuse *f, const struct iovec iov[],
515 size_t count)
Miklos Szeredi43696432001-11-18 19:15:05 +0000516{
517 int res;
Miklos Szerediab974562005-04-07 15:40:21 +0000518 unsigned outsize = iov_length(iov, count);
519 struct fuse_out_header *out = (struct fuse_out_header *) iov[0].iov_base;
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000520 out->len = outsize;
Miklos Szeredi43696432001-11-18 19:15:05 +0000521
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000522 if ((f->flags & FUSE_DEBUG)) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000523 printf(" unique: %llu, error: %i (%s), outsize: %i\n",
524 out->unique, out->error, strerror(-out->error), outsize);
Miklos Szeredi43696432001-11-18 19:15:05 +0000525 fflush(stdout);
526 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000527
Miklos Szeredi4e71c9f2004-02-09 12:05:14 +0000528 /* This needs to be done before the reply, otherwise the scheduler
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000529 could play tricks with us, and only let the counter be
530 increased long after the operation is done */
531 fuse_inc_avail(f);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000532
Miklos Szerediab974562005-04-07 15:40:21 +0000533 res = writev(f->fd, iov, count);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000534 if (res == -1) {
Miklos Szeredi43696432001-11-18 19:15:05 +0000535 /* ENOENT means the operation was interrupted */
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000536 if (!f->exited && errno != ENOENT)
Miklos Szeredi96249982001-11-21 12:21:19 +0000537 perror("fuse: writing device");
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000538 return -errno;
Miklos Szeredi43696432001-11-18 19:15:05 +0000539 }
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000540 return 0;
Miklos Szeredi43696432001-11-18 19:15:05 +0000541}
542
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000543static int send_reply(struct fuse *f, struct fuse_in_header *in, int error,
544 void *arg, size_t argsize)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000545{
Miklos Szerediab974562005-04-07 15:40:21 +0000546 struct fuse_out_header out;
547 struct iovec iov[2];
548 size_t count;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000549
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000550 if (error <= -1000 || error > 0) {
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000551 fprintf(stderr, "fuse: bad error value: %i\n", error);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000552 error = -ERANGE;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000553 }
554
Miklos Szerediab974562005-04-07 15:40:21 +0000555 out.unique = in->unique;
556 out.error = error;
557 count = 1;
558 iov[0].iov_base = &out;
559 iov[0].iov_len = sizeof(struct fuse_out_header);
560 if (argsize && !error) {
561 count++;
562 iov[1].iov_base = arg;
563 iov[1].iov_len = argsize;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000564 }
Miklos Szerediab974562005-04-07 15:40:21 +0000565 return send_reply_raw(f, iov, count);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000566}
567
Miklos Szeredia13d9002004-11-02 17:32:03 +0000568static int is_open(struct fuse *f, nodeid_t dir, const char *name)
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000569{
570 struct node *node;
571 int isopen = 0;
572 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000573 node = lookup_node(f, dir, name);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000574 if (node && node->open_count > 0)
575 isopen = 1;
576 pthread_mutex_unlock(&f->lock);
577 return isopen;
578}
579
Miklos Szeredia13d9002004-11-02 17:32:03 +0000580static char *hidden_name(struct fuse *f, nodeid_t dir, const char *oldname,
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000581 char *newname, size_t bufsize)
582{
583 struct stat buf;
584 struct node *node;
585 struct node *newnode;
586 char *newpath;
587 int res;
588 int failctr = 10;
589
590 if (!f->op.getattr)
591 return NULL;
592
593 do {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000594 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000595 node = lookup_node(f, dir, oldname);
596 if (node == NULL) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000597 pthread_mutex_unlock(&f->lock);
598 return NULL;
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000599 }
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000600 do {
601 f->hidectr ++;
602 snprintf(newname, bufsize, ".fuse_hidden%08x%08x",
Miklos Szeredia13d9002004-11-02 17:32:03 +0000603 (unsigned int) node->nodeid, f->hidectr);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000604 newnode = lookup_node(f, dir, newname);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000605 } while(newnode);
606 pthread_mutex_unlock(&f->lock);
Miklos Szeredie5183742005-02-02 11:14:04 +0000607
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000608 newpath = get_path_name(f, dir, newname);
609 if (!newpath)
610 break;
Miklos Szeredie5183742005-02-02 11:14:04 +0000611
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000612 res = f->op.getattr(newpath, &buf);
613 if (res != 0)
614 break;
615 free(newpath);
616 newpath = NULL;
617 } while(--failctr);
618
619 return newpath;
620}
621
Miklos Szeredia13d9002004-11-02 17:32:03 +0000622static int hide_node(struct fuse *f, const char *oldpath, nodeid_t dir,
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000623 const char *oldname)
624{
625 char newname[64];
626 char *newpath;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000627 int err = -EBUSY;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000628
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000629 if (f->op.rename && f->op.unlink) {
630 newpath = hidden_name(f, dir, oldname, newname, sizeof(newname));
631 if (newpath) {
632 int res = f->op.rename(oldpath, newpath);
633 if (res == 0)
634 err = rename_node(f, dir, oldname, dir, newname, 1);
635 free(newpath);
636 }
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000637 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000638 return err;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000639}
640
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000641static int lookup_path(struct fuse *f, nodeid_t nodeid, uint64_t version,
642 char *name, const char *path,
643 struct fuse_entry_out *arg)
Miklos Szeredi76f65782004-02-19 16:55:40 +0000644{
645 int res;
646 struct stat buf;
647
648 res = f->op.getattr(path, &buf);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000649 if (res == 0) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000650 struct node *node;
651
Miklos Szeredie331c4b2005-07-06 13:34:02 +0000652 node = find_node(f, nodeid, name, version);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000653 if (node == NULL)
654 res = -ENOMEM;
655 else {
Miklos Szeredie331c4b2005-07-06 13:34:02 +0000656 memset(arg, 0, sizeof(struct fuse_entry_out));
657 convert_stat(f, node->nodeid, &buf, &arg->attr);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000658 arg->nodeid = node->nodeid;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000659 arg->generation = node->generation;
660 arg->entry_valid = ENTRY_REVALIDATE_TIME;
661 arg->entry_valid_nsec = 0;
662 arg->attr_valid = ATTR_REVALIDATE_TIME;
663 arg->attr_valid_nsec = 0;
664 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000665 printf(" NODEID: %lu\n", (unsigned long) arg->nodeid);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000666 fflush(stdout);
667 }
Miklos Szeredi76f65782004-02-19 16:55:40 +0000668 }
669 }
670 return res;
671}
672
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000673static void do_lookup(struct fuse *f, struct fuse_in_header *in, char *name)
674{
675 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000676 int res2;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000677 char *path;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000678 struct fuse_entry_out arg;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000679
Miklos Szeredi5e183482001-10-31 14:52:35 +0000680 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000681 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000682 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000683 if (path != NULL) {
684 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi6ebe2342002-01-06 21:44:16 +0000685 printf("LOOKUP %s\n", path);
686 fflush(stdout);
687 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000688 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000689 if (f->op.getattr)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000690 res = lookup_path(f, in->nodeid, in->unique, name, path, &arg);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000691 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000692 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000693 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000694 res2 = send_reply(f, in, res, &arg, sizeof(arg));
695 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi38009022005-05-08 19:47:22 +0000696 cancel_lookup(f, arg.nodeid, in->unique);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000697}
698
Miklos Szeredia181e612001-11-06 12:03:23 +0000699static void do_forget(struct fuse *f, struct fuse_in_header *in,
700 struct fuse_forget_in *arg)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000701{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000702 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000703 printf("FORGET %lu/%llu\n", (unsigned long) in->nodeid,
Miklos Szeredi38009022005-05-08 19:47:22 +0000704 arg->nlookup);
Miklos Szeredi43696432001-11-18 19:15:05 +0000705 fflush(stdout);
706 }
Miklos Szeredi38009022005-05-08 19:47:22 +0000707 if (f->major <= 6)
708 forget_node_old(f, in->nodeid, arg->nlookup);
709 else
710 forget_node(f, in->nodeid, arg->nlookup);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000711}
712
713static void do_getattr(struct fuse *f, struct fuse_in_header *in)
714{
715 int res;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000716 char *path;
717 struct stat buf;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000718 struct fuse_attr_out arg;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000719
Miklos Szeredi5e183482001-10-31 14:52:35 +0000720 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000721 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000722 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000723 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +0000724 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000725 if (f->op.getattr)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000726 res = f->op.getattr(path, &buf);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000727 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000728 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000729 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000730
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000731 if (res == 0) {
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000732 memset(&arg, 0, sizeof(struct fuse_attr_out));
733 arg.attr_valid = ATTR_REVALIDATE_TIME;
734 arg.attr_valid_nsec = 0;
Miklos Szeredie331c4b2005-07-06 13:34:02 +0000735 convert_stat(f, in->nodeid, &buf, &arg.attr);
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000736 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000737
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000738 send_reply(f, in, res, &arg, sizeof(arg));
739}
740
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000741static int do_chmod(struct fuse *f, const char *path, struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000742{
743 int res;
744
745 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000746 if (f->op.chmod)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000747 res = f->op.chmod(path, attr->mode);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000748
749 return res;
Miklos Szeredie5183742005-02-02 11:14:04 +0000750}
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000751
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000752static int do_chown(struct fuse *f, const char *path, struct fuse_attr *attr,
Miklos Szeredi2e50d432001-12-20 12:17:25 +0000753 int valid)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000754{
755 int res;
756 uid_t uid = (valid & FATTR_UID) ? attr->uid : (uid_t) -1;
757 gid_t gid = (valid & FATTR_GID) ? attr->gid : (gid_t) -1;
Miklos Szeredie5183742005-02-02 11:14:04 +0000758
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000759 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000760 if (f->op.chown)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000761 res = f->op.chown(path, uid, gid);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000762
763 return res;
764}
765
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000766static int do_truncate(struct fuse *f, const char *path,
767 struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000768{
769 int res;
770
771 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000772 if (f->op.truncate)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000773 res = f->op.truncate(path, attr->size);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000774
775 return res;
776}
777
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000778static int do_utime(struct fuse *f, const char *path, struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000779{
780 int res;
781 struct utimbuf buf;
782 buf.actime = attr->atime;
783 buf.modtime = attr->mtime;
784 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000785 if (f->op.utime)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000786 res = f->op.utime(path, &buf);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000787
788 return res;
789}
790
Miklos Szeredi5e183482001-10-31 14:52:35 +0000791static void do_setattr(struct fuse *f, struct fuse_in_header *in,
792 struct fuse_setattr_in *arg)
793{
794 int res;
795 char *path;
796 int valid = arg->valid;
Miklos Szeredi30e093a2005-04-03 17:44:54 +0000797 struct fuse_attr *attr = MEMBER_COMPAT(f, arg, attr, fuse_setattr_in);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000798 struct fuse_attr_out outarg;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000799
800 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000801 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000802 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000803 if (path != NULL) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000804 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000805 if (f->op.getattr) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000806 res = 0;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000807 if (!res && (valid & FATTR_MODE))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000808 res = do_chmod(f, path, attr);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000809 if (!res && (valid & (FATTR_UID | FATTR_GID)))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000810 res = do_chown(f, path, attr, valid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000811 if (!res && (valid & FATTR_SIZE))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000812 res = do_truncate(f, path, attr);
Miklos Szeredie5183742005-02-02 11:14:04 +0000813 if (!res && (valid & (FATTR_ATIME | FATTR_MTIME)) ==
Miklos Szeredib5958612004-02-20 14:10:49 +0000814 (FATTR_ATIME | FATTR_MTIME))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000815 res = do_utime(f, path, attr);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000816 if (!res) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000817 struct stat buf;
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000818 res = f->op.getattr(path, &buf);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000819 if (!res) {
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000820 memset(&outarg, 0, sizeof(struct fuse_attr_out));
821 outarg.attr_valid = ATTR_REVALIDATE_TIME;
822 outarg.attr_valid_nsec = 0;
Miklos Szeredie331c4b2005-07-06 13:34:02 +0000823 convert_stat(f, in->nodeid, &buf, &outarg.attr);
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000824 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000825 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000826 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000827 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000828 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000829 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredia181e612001-11-06 12:03:23 +0000830 send_reply(f, in, res, &outarg, sizeof(outarg));
Miklos Szeredi5e183482001-10-31 14:52:35 +0000831}
832
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000833static void do_readlink(struct fuse *f, struct fuse_in_header *in)
834{
835 int res;
836 char link[PATH_MAX + 1];
Miklos Szeredib483c932001-10-29 14:57:57 +0000837 char *path;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000838
Miklos Szeredi5e183482001-10-31 14:52:35 +0000839 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000840 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000841 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000842 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +0000843 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000844 if (f->op.readlink)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000845 res = f->op.readlink(path, link, sizeof(link));
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000846 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000847 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000848 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000849 link[PATH_MAX] = '\0';
Miklos Szeredi4b2bef42002-01-09 12:23:27 +0000850 send_reply(f, in, res, link, res == 0 ? strlen(link) : 0);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000851}
852
Miklos Szeredib483c932001-10-29 14:57:57 +0000853static void do_mknod(struct fuse *f, struct fuse_in_header *in,
854 struct fuse_mknod_in *inarg)
855{
856 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000857 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000858 char *path;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000859 char *name = PARAM(inarg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000860 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000861
Miklos Szeredi5e183482001-10-31 14:52:35 +0000862 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000863 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000864 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000865 if (path != NULL) {
866 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000867 printf("MKNOD %s\n", path);
868 fflush(stdout);
869 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000870 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000871 if (f->op.mknod && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000872 res = f->op.mknod(path, inarg->mode, inarg->rdev);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000873 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000874 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000875 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000876 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000877 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000878 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000879 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
880 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi38009022005-05-08 19:47:22 +0000881 cancel_lookup(f, outarg.nodeid, in->unique);
Miklos Szeredib483c932001-10-29 14:57:57 +0000882}
883
884static void do_mkdir(struct fuse *f, struct fuse_in_header *in,
885 struct fuse_mkdir_in *inarg)
886{
887 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000888 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000889 char *path;
Miklos Szeredi30e093a2005-04-03 17:44:54 +0000890 char *name = PARAM_COMPAT(f, inarg, fuse_mkdir_in);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000891 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000892
Miklos Szeredi5e183482001-10-31 14:52:35 +0000893 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000894 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000895 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000896 if (path != NULL) {
897 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000898 printf("MKDIR %s\n", path);
899 fflush(stdout);
900 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000901 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000902 if (f->op.mkdir && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000903 res = f->op.mkdir(path, inarg->mode);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000904 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000905 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000906 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000907 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000908 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000909 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000910 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
911 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi38009022005-05-08 19:47:22 +0000912 cancel_lookup(f, outarg.nodeid, in->unique);
Miklos Szeredib483c932001-10-29 14:57:57 +0000913}
914
Miklos Szeredib5958612004-02-20 14:10:49 +0000915static void do_unlink(struct fuse *f, struct fuse_in_header *in, char *name)
Miklos Szeredib483c932001-10-29 14:57:57 +0000916{
917 int res;
918 char *path;
919
Miklos Szeredi5e183482001-10-31 14:52:35 +0000920 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000921 pthread_rwlock_wrlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000922 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000923 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000924 if (f->flags & FUSE_DEBUG) {
925 printf("UNLINK %s\n", path);
926 fflush(stdout);
927 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000928 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000929 if (f->op.unlink) {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000930 if (!(f->flags & FUSE_HARD_REMOVE) && is_open(f, in->nodeid, name))
931 res = hide_node(f, path, in->nodeid, name);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000932 else {
933 res = f->op.unlink(path);
934 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000935 remove_node(f, in->nodeid, name);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000936
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000937 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000938 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000939 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000940 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000941 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredib5958612004-02-20 14:10:49 +0000942 send_reply(f, in, res, NULL, 0);
943}
944
945static void do_rmdir(struct fuse *f, struct fuse_in_header *in, char *name)
946{
947 int res;
948 char *path;
949
950 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000951 pthread_rwlock_wrlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000952 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000953 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000954 if (f->flags & FUSE_DEBUG) {
955 printf("RMDIR %s\n", path);
956 fflush(stdout);
957 }
Miklos Szeredib5958612004-02-20 14:10:49 +0000958 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000959 if (f->op.rmdir) {
Miklos Szeredib5958612004-02-20 14:10:49 +0000960 res = f->op.rmdir(path);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000961 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000962 remove_node(f, in->nodeid, name);
Miklos Szeredib5958612004-02-20 14:10:49 +0000963 }
964 free(path);
965 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000966 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredib483c932001-10-29 14:57:57 +0000967 send_reply(f, in, res, NULL, 0);
968}
969
970static void do_symlink(struct fuse *f, struct fuse_in_header *in, char *name,
971 char *link)
972{
973 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000974 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000975 char *path;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000976 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000977
Miklos Szeredi5e183482001-10-31 14:52:35 +0000978 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000979 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000980 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000981 if (path != NULL) {
982 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000983 printf("SYMLINK %s\n", path);
984 fflush(stdout);
985 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000986 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000987 if (f->op.symlink && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000988 res = f->op.symlink(link, path);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000989 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000990 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000991 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000992 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000993 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000994 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000995 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
996 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi38009022005-05-08 19:47:22 +0000997 cancel_lookup(f, outarg.nodeid, in->unique);
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000998
Miklos Szeredib483c932001-10-29 14:57:57 +0000999}
1000
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001001static void do_rename(struct fuse *f, struct fuse_in_header *in,
1002 struct fuse_rename_in *inarg)
1003{
1004 int res;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001005 nodeid_t olddir = in->nodeid;
1006 nodeid_t newdir = inarg->newdir;
Miklos Szeredi6bf8b682002-10-28 08:49:39 +00001007 char *oldname = PARAM(inarg);
1008 char *newname = oldname + strlen(oldname) + 1;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001009 char *oldpath;
1010 char *newpath;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001011
Miklos Szeredi5e183482001-10-31 14:52:35 +00001012 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001013 pthread_rwlock_wrlock(&f->tree_lock);
Miklos Szeredia181e612001-11-06 12:03:23 +00001014 oldpath = get_path_name(f, olddir, oldname);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001015 if (oldpath != NULL) {
Miklos Szeredia181e612001-11-06 12:03:23 +00001016 newpath = get_path_name(f, newdir, newname);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001017 if (newpath != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001018 if (f->flags & FUSE_DEBUG) {
1019 printf("RENAME %s -> %s\n", oldpath, newpath);
1020 fflush(stdout);
1021 }
Miklos Szeredi5e183482001-10-31 14:52:35 +00001022 res = -ENOSYS;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001023 if (f->op.rename) {
1024 res = 0;
Miklos Szeredie5183742005-02-02 11:14:04 +00001025 if (!(f->flags & FUSE_HARD_REMOVE) &&
Miklos Szeredi2529ca22004-07-13 15:36:52 +00001026 is_open(f, newdir, newname))
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001027 res = hide_node(f, newpath, newdir, newname);
1028 if (res == 0) {
1029 res = f->op.rename(oldpath, newpath);
1030 if (res == 0)
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001031 res = rename_node(f, olddir, oldname, newdir, newname, 0);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001032 }
1033 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001034 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001035 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001036 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001037 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001038 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredie5183742005-02-02 11:14:04 +00001039 send_reply(f, in, res, NULL, 0);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001040}
1041
1042static void do_link(struct fuse *f, struct fuse_in_header *in,
Miklos Szeredi5e183482001-10-31 14:52:35 +00001043 struct fuse_link_in *arg)
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001044{
1045 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +00001046 int res2;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001047 char *oldpath;
1048 char *newpath;
Miklos Szeredi76f65782004-02-19 16:55:40 +00001049 char *name = PARAM(arg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +00001050 struct fuse_entry_out outarg;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001051
Miklos Szeredi5e183482001-10-31 14:52:35 +00001052 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001053 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredied6b5dd2005-01-26 17:07:59 +00001054 oldpath = get_path(f, arg->oldnodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001055 if (oldpath != NULL) {
Miklos Szeredied6b5dd2005-01-26 17:07:59 +00001056 newpath = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001057 if (newpath != NULL) {
1058 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +00001059 printf("LINK %s\n", newpath);
1060 fflush(stdout);
1061 }
Miklos Szeredi5e183482001-10-31 14:52:35 +00001062 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001063 if (f->op.link && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +00001064 res = f->op.link(oldpath, newpath);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001065 if (res == 0)
Miklos Szeredied6b5dd2005-01-26 17:07:59 +00001066 res = lookup_path(f, in->nodeid, in->unique, name,
Miklos Szeredi76f65782004-02-19 16:55:40 +00001067 newpath, &outarg);
1068 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001069 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001070 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001071 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001072 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001073 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi2778f6c2004-06-21 09:45:30 +00001074 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
1075 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi38009022005-05-08 19:47:22 +00001076 cancel_lookup(f, outarg.nodeid, in->unique);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001077}
1078
Miklos Szeredi5e183482001-10-31 14:52:35 +00001079static void do_open(struct fuse *f, struct fuse_in_header *in,
1080 struct fuse_open_in *arg)
1081{
1082 int res;
1083 char *path;
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001084 struct fuse_open_out outarg;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001085 struct fuse_file_info fi;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001086
Miklos Szeredied3c97c2005-02-15 17:04:50 +00001087 memset(&outarg, 0, sizeof(outarg));
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001088 memset(&fi, 0, sizeof(fi));
1089 fi.flags = arg->flags;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001090 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001091 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001092 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001093 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +00001094 res = -ENOSYS;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001095 if (f->op.open) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001096 if (!f->compat)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001097 res = f->op.open(path, &fi);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001098 else
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001099 res = ((struct fuse_operations_compat2 *) &f->op)->open(path, fi.flags);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001100 }
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +00001101 }
Miklos Szeredi73798f92004-07-12 15:55:11 +00001102 if (res == 0) {
1103 int res2;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001104 outarg.fh = fi.fh;
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001105 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001106 printf("OPEN[%lu] flags: 0x%x\n", fi.fh, arg->flags);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001107 fflush(stdout);
1108 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001109
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001110 pthread_mutex_lock(&f->lock);
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001111 res2 = send_reply(f, in, res, &outarg, SIZEOF_COMPAT(f, fuse_open_out));
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001112 if(res2 == -ENOENT) {
1113 /* The open syscall was interrupted, so it must be cancelled */
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001114 if(f->op.release) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001115 if (!f->compat)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001116 f->op.release(path, &fi);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001117 else
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001118 ((struct fuse_operations_compat2 *) &f->op)->release(path, fi.flags);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001119 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001120 } else {
1121 struct node *node = get_node(f, in->nodeid);
1122 node->open_count ++;
1123 }
Miklos Szeredi73798f92004-07-12 15:55:11 +00001124 pthread_mutex_unlock(&f->lock);
Miklos Szeredi73798f92004-07-12 15:55:11 +00001125 } else
1126 send_reply(f, in, res, NULL, 0);
1127
Miklos Szeredi9a31cca2004-06-26 21:11:25 +00001128 if (path)
1129 free(path);
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001130 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001131}
1132
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001133static void do_flush(struct fuse *f, struct fuse_in_header *in,
1134 struct fuse_flush_in *arg)
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001135{
1136 char *path;
1137 int res;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001138 struct fuse_file_info fi;
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001139
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001140 memset(&fi, 0, sizeof(fi));
1141 fi.fh = arg->fh;
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001142 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001143 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001144 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001145 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001146 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001147 printf("FLUSH[%lu]\n", (unsigned long) arg->fh);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001148 fflush(stdout);
1149 }
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001150 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001151 if (f->op.flush)
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001152 res = f->op.flush(path, &fi);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001153 free(path);
1154 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001155 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001156 send_reply(f, in, res, NULL, 0);
1157}
1158
Miklos Szeredi9478e862002-12-11 09:50:26 +00001159static void do_release(struct fuse *f, struct fuse_in_header *in,
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001160 struct fuse_release_in *arg)
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001161{
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001162 struct node *node;
1163 char *path;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001164 struct fuse_file_info fi;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001165 int unlink_hidden;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001166
1167 memset(&fi, 0, sizeof(fi));
1168 fi.flags = arg->flags;
1169 fi.fh = arg->fh;
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001170
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001171 pthread_mutex_lock(&f->lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001172 node = get_node(f, in->nodeid);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001173 assert(node->open_count > 0);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001174 --node->open_count;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001175 unlink_hidden = (node->is_hidden && !node->open_count);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001176 pthread_mutex_unlock(&f->lock);
1177
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001178 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001179 path = get_path(f, in->nodeid);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001180 if (f->flags & FUSE_DEBUG) {
1181 printf("RELEASE[%lu] flags: 0x%x\n", fi.fh, fi.flags);
1182 fflush(stdout);
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001183 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001184 if (f->op.release) {
1185 if (!f->compat)
1186 f->op.release(path ? path : "-", &fi);
1187 else if (path)
1188 ((struct fuse_operations_compat2 *) &f->op)->release(path, fi.flags);
1189 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001190
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001191 if(unlink_hidden && path)
1192 f->op.unlink(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001193
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001194 if (path)
1195 free(path);
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001196 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001197
Miklos Szeredi556d03d2004-06-30 11:13:41 +00001198 send_reply(f, in, 0, NULL, 0);
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001199}
1200
Miklos Szeredi5e183482001-10-31 14:52:35 +00001201static void do_read(struct fuse *f, struct fuse_in_header *in,
1202 struct fuse_read_in *arg)
1203{
1204 int res;
1205 char *path;
Miklos Szerediab974562005-04-07 15:40:21 +00001206 size_t size;
1207 char *buf;
1208 struct fuse_file_info fi;
1209
1210 buf = (char *) malloc(arg->size);
1211 if (buf == NULL) {
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001212 send_reply(f, in, -ENOMEM, NULL, 0);
Miklos Szerediab974562005-04-07 15:40:21 +00001213 return;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001214 }
Miklos Szerediab974562005-04-07 15:40:21 +00001215
1216 memset(&fi, 0, sizeof(fi));
1217 fi.fh = arg->fh;
1218
1219 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001220 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szerediab974562005-04-07 15:40:21 +00001221 path = get_path(f, in->nodeid);
1222 if (path != NULL) {
1223 if (f->flags & FUSE_DEBUG) {
1224 printf("READ[%lu] %u bytes from %llu\n",
1225 (unsigned long) arg->fh, arg->size, arg->offset);
1226 fflush(stdout);
1227 }
1228
1229 res = -ENOSYS;
1230 if (f->op.read)
1231 res = f->op.read(path, buf, arg->size, arg->offset, &fi);
1232 free(path);
1233 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001234 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szerediab974562005-04-07 15:40:21 +00001235
1236 size = 0;
1237 if (res >= 0) {
1238 size = res;
1239 res = 0;
1240 if (f->flags & FUSE_DEBUG) {
1241 printf(" READ[%lu] %u bytes\n", (unsigned long) arg->fh,
1242 size);
1243 fflush(stdout);
1244 }
1245 }
1246
1247 send_reply(f, in, res, buf, size);
1248 free(buf);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001249}
Miklos Szeredib483c932001-10-29 14:57:57 +00001250
Miklos Szeredia181e612001-11-06 12:03:23 +00001251static void do_write(struct fuse *f, struct fuse_in_header *in,
1252 struct fuse_write_in *arg)
1253{
1254 int res;
1255 char *path;
Miklos Szerediad051c32004-07-02 09:22:50 +00001256 struct fuse_write_out outarg;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001257 struct fuse_file_info fi;
1258
1259 memset(&fi, 0, sizeof(fi));
1260 fi.fh = arg->fh;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001261 fi.writepage = arg->write_flags & 1;
Miklos Szeredia181e612001-11-06 12:03:23 +00001262
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001263 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001264 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001265 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001266 if (path != NULL) {
1267 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi1eea0322004-09-27 18:50:11 +00001268 printf("WRITE%s[%lu] %u bytes to %llu\n",
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001269 (arg->write_flags & 1) ? "PAGE" : "",
1270 (unsigned long) arg->fh, arg->size, arg->offset);
Miklos Szeredi6ebe2342002-01-06 21:44:16 +00001271 fflush(stdout);
1272 }
1273
Miklos Szeredia181e612001-11-06 12:03:23 +00001274 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001275 if (f->op.write)
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001276 res = f->op.write(path, PARAM(arg), arg->size, arg->offset, &fi);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001277 free(path);
Miklos Szeredia181e612001-11-06 12:03:23 +00001278 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001279 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredie5183742005-02-02 11:14:04 +00001280
Miklos Szerediab974562005-04-07 15:40:21 +00001281 memset(&outarg, 0, sizeof(outarg));
Miklos Szeredie5183742005-02-02 11:14:04 +00001282 if (res >= 0) {
Miklos Szerediad051c32004-07-02 09:22:50 +00001283 outarg.size = res;
1284 res = 0;
Miklos Szeredia181e612001-11-06 12:03:23 +00001285 }
1286
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001287 send_reply(f, in, res, &outarg, SIZEOF_COMPAT(f, fuse_write_out));
Miklos Szeredia181e612001-11-06 12:03:23 +00001288}
1289
Miklos Szeredi77f39942004-03-25 11:17:52 +00001290static int default_statfs(struct statfs *buf)
1291{
1292 buf->f_namelen = 255;
1293 buf->f_bsize = 512;
1294 return 0;
1295}
1296
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001297static void convert_statfs_compat(struct fuse_statfs_compat1 *compatbuf,
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001298 struct statfs *statfs)
1299{
1300 statfs->f_bsize = compatbuf->block_size;
1301 statfs->f_blocks = compatbuf->blocks;
1302 statfs->f_bfree = compatbuf->blocks_free;
1303 statfs->f_bavail = compatbuf->blocks_free;
1304 statfs->f_files = compatbuf->files;
1305 statfs->f_ffree = compatbuf->files_free;
1306 statfs->f_namelen = compatbuf->namelen;
1307}
1308
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001309static void convert_statfs(struct statfs *statfs, struct fuse_kstatfs *kstatfs)
1310{
1311 kstatfs->bsize = statfs->f_bsize;
1312 kstatfs->blocks = statfs->f_blocks;
1313 kstatfs->bfree = statfs->f_bfree;
1314 kstatfs->bavail = statfs->f_bavail;
1315 kstatfs->files = statfs->f_files;
1316 kstatfs->ffree = statfs->f_ffree;
1317 kstatfs->namelen = statfs->f_namelen;
1318}
1319
Mark Glinesd84b39a2002-01-07 16:32:02 +00001320static void do_statfs(struct fuse *f, struct fuse_in_header *in)
1321{
1322 int res;
Mark Glinesd84b39a2002-01-07 16:32:02 +00001323 struct fuse_statfs_out arg;
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001324 struct statfs buf;
Mark Glinesd84b39a2002-01-07 16:32:02 +00001325
Miklos Szeredi77f39942004-03-25 11:17:52 +00001326 memset(&buf, 0, sizeof(struct statfs));
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001327 if (f->op.statfs) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001328 if (!f->compat || f->compat > 11)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001329 res = f->op.statfs("/", &buf);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001330 else {
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001331 struct fuse_statfs_compat1 compatbuf;
1332 memset(&compatbuf, 0, sizeof(struct fuse_statfs_compat1));
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001333 res = ((struct fuse_operations_compat1 *) &f->op)->statfs(&compatbuf);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001334 if (res == 0)
1335 convert_statfs_compat(&compatbuf, &buf);
1336 }
1337 }
Miklos Szeredi77f39942004-03-25 11:17:52 +00001338 else
1339 res = default_statfs(&buf);
1340
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001341 if (res == 0)
Miklos Szeredi77f39942004-03-25 11:17:52 +00001342 convert_statfs(&buf, &arg.st);
Miklos Szeredi4b2bef42002-01-09 12:23:27 +00001343
Mark Glinesd84b39a2002-01-07 16:32:02 +00001344 send_reply(f, in, res, &arg, sizeof(arg));
1345}
1346
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001347static void do_fsync(struct fuse *f, struct fuse_in_header *in,
1348 struct fuse_fsync_in *inarg)
1349{
1350 int res;
1351 char *path;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001352 struct fuse_file_info fi;
1353
1354 memset(&fi, 0, sizeof(fi));
1355 fi.fh = inarg->fh;
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001356
1357 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001358 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001359 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001360 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001361 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001362 printf("FSYNC[%lu]\n", (unsigned long) inarg->fh);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001363 fflush(stdout);
1364 }
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001365 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001366 if (f->op.fsync)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001367 res = f->op.fsync(path, inarg->fsync_flags & 1, &fi);
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001368 free(path);
1369 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001370 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001371 send_reply(f, in, res, NULL, 0);
1372}
1373
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001374static void do_setxattr(struct fuse *f, struct fuse_in_header *in,
1375 struct fuse_setxattr_in *arg)
1376{
1377 int res;
1378 char *path;
1379 char *name = PARAM(arg);
1380 unsigned char *value = name + strlen(name) + 1;
1381
1382 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001383 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001384 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001385 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001386 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001387 if (f->op.setxattr)
1388 res = f->op.setxattr(path, name, value, arg->size, arg->flags);
1389 free(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001390 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001391 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001392 send_reply(f, in, res, NULL, 0);
1393}
1394
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001395static int common_getxattr(struct fuse *f, struct fuse_in_header *in,
1396 const char *name, char *value, size_t size)
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001397{
1398 int res;
1399 char *path;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001400
1401 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001402 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001403 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001404 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001405 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001406 if (f->op.getxattr)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001407 res = f->op.getxattr(path, name, value, size);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001408 free(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001409 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001410 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001411 return res;
1412}
1413
1414static void do_getxattr_read(struct fuse *f, struct fuse_in_header *in,
1415 const char *name, size_t size)
1416{
1417 int res;
Miklos Szerediab974562005-04-07 15:40:21 +00001418 char *value = (char *) malloc(size);
1419 if (value == NULL) {
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001420 send_reply(f, in, -ENOMEM, NULL, 0);
Miklos Szerediab974562005-04-07 15:40:21 +00001421 return;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001422 }
Miklos Szerediab974562005-04-07 15:40:21 +00001423 res = common_getxattr(f, in, name, value, size);
1424 size = 0;
1425 if (res > 0) {
1426 size = res;
1427 res = 0;
1428 }
1429 send_reply(f, in, res, value, size);
1430 free(value);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001431}
1432
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001433static void do_getxattr_size(struct fuse *f, struct fuse_in_header *in,
1434 const char *name)
1435{
1436 int res;
1437 struct fuse_getxattr_out arg;
1438
Miklos Szerediab974562005-04-07 15:40:21 +00001439 memset(&arg, 0, sizeof(arg));
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001440 res = common_getxattr(f, in, name, NULL, 0);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001441 if (res >= 0) {
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001442 arg.size = res;
1443 res = 0;
1444 }
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001445 send_reply(f, in, res, &arg, SIZEOF_COMPAT(f, fuse_getxattr_out));
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001446}
1447
1448static void do_getxattr(struct fuse *f, struct fuse_in_header *in,
1449 struct fuse_getxattr_in *arg)
1450{
1451 char *name = PARAM(arg);
Miklos Szeredie5183742005-02-02 11:14:04 +00001452
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001453 if (arg->size)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001454 do_getxattr_read(f, in, name, arg->size);
1455 else
1456 do_getxattr_size(f, in, name);
1457}
1458
1459static int common_listxattr(struct fuse *f, struct fuse_in_header *in,
1460 char *list, size_t size)
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001461{
1462 int res;
1463 char *path;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001464
1465 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001466 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001467 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001468 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001469 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001470 if (f->op.listxattr)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001471 res = f->op.listxattr(path, list, size);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001472 free(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001473 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001474 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001475 return res;
1476}
1477
1478static void do_listxattr_read(struct fuse *f, struct fuse_in_header *in,
1479 size_t size)
1480{
1481 int res;
Miklos Szerediab974562005-04-07 15:40:21 +00001482 char *list = (char *) malloc(size);
1483 if (list == NULL) {
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001484 send_reply(f, in, -ENOMEM, NULL, 0);
Miklos Szerediab974562005-04-07 15:40:21 +00001485 return;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001486 }
Miklos Szerediab974562005-04-07 15:40:21 +00001487 res = common_listxattr(f, in, list, size);
1488 size = 0;
1489 if (res > 0) {
1490 size = res;
1491 res = 0;
1492 }
1493 send_reply(f, in, res, list, size);
1494 free(list);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001495}
1496
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001497static void do_listxattr_size(struct fuse *f, struct fuse_in_header *in)
1498{
1499 int res;
1500 struct fuse_getxattr_out arg;
1501
Miklos Szerediab974562005-04-07 15:40:21 +00001502 memset(&arg, 0, sizeof(arg));
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001503 res = common_listxattr(f, in, NULL, 0);
1504 if (res >= 0) {
1505 arg.size = res;
1506 res = 0;
1507 }
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001508 send_reply(f, in, res, &arg, SIZEOF_COMPAT(f, fuse_getxattr_out));
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001509}
1510
1511static void do_listxattr(struct fuse *f, struct fuse_in_header *in,
1512 struct fuse_getxattr_in *arg)
1513{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001514 if (arg->size)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001515 do_listxattr_read(f, in, arg->size);
1516 else
1517 do_listxattr_size(f, in);
1518}
1519
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001520static void do_removexattr(struct fuse *f, struct fuse_in_header *in,
1521 char *name)
1522{
1523 int res;
1524 char *path;
1525
1526 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001527 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001528 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001529 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001530 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001531 if (f->op.removexattr)
1532 res = f->op.removexattr(path, name);
1533 free(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001534 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001535 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001536 send_reply(f, in, res, NULL, 0);
1537}
1538
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001539static void do_init(struct fuse *f, struct fuse_in_header *in,
1540 struct fuse_init_in_out *arg)
1541{
1542 struct fuse_init_in_out outarg;
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001543
1544 if (in->padding == 5) {
1545 arg->minor = arg->major;
1546 arg->major = in->padding;
1547 }
1548
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001549 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001550 printf("INIT: %u.%u\n", arg->major, arg->minor);
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001551 fflush(stdout);
1552 }
1553 f->got_init = 1;
Miklos Szeredi159bd7e2005-02-28 17:32:16 +00001554 if (f->op.init)
1555 f->user_data = f->op.init();
1556
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001557 if (arg->major == 5) {
1558 f->major = 5;
1559 f->minor = 1;
Miklos Szeredi38009022005-05-08 19:47:22 +00001560 } else if (arg->major == 6) {
1561 f->major = 6;
1562 f->minor = 1;
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001563 } else {
1564 f->major = FUSE_KERNEL_VERSION;
1565 f->minor = FUSE_KERNEL_MINOR_VERSION;
1566 }
1567 memset(&outarg, 0, sizeof(outarg));
1568 outarg.major = f->major;
1569 outarg.minor = f->minor;
1570
1571 if (f->flags & FUSE_DEBUG) {
1572 printf(" INIT: %u.%u\n", outarg.major, outarg.minor);
1573 fflush(stdout);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001574 }
1575
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001576 send_reply(f, in, 0, &outarg, sizeof(outarg));
1577}
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001578
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001579static struct fuse_dirhandle *get_dirhandle(unsigned long fh)
1580{
1581 return (struct fuse_dirhandle *) fh;
1582}
1583
1584static void do_opendir(struct fuse *f, struct fuse_in_header *in,
1585 struct fuse_open_in *arg)
1586{
1587 int res;
1588 struct fuse_open_out outarg;
1589 struct fuse_dirhandle *dh;
1590
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001591 dh = (struct fuse_dirhandle *) malloc(sizeof(struct fuse_dirhandle));
1592 if (dh == NULL) {
1593 send_reply(f, in, -ENOMEM, NULL, 0);
1594 return;
1595 }
1596 memset(dh, 0, sizeof(struct fuse_dirhandle));
1597 dh->fuse = f;
1598 dh->contents = NULL;
1599 dh->len = 0;
1600 dh->filled = 0;
Miklos Szeredi340d21f2005-07-06 10:07:52 +00001601 dh->nodeid = in->nodeid;
Miklos Szerediab974562005-04-07 15:40:21 +00001602 mutex_init(&dh->lock);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001603
1604 memset(&outarg, 0, sizeof(outarg));
1605 outarg.fh = (unsigned long) dh;
1606
Miklos Szeredif43f0632005-02-28 11:46:56 +00001607 if (f->op.opendir) {
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001608 struct fuse_file_info fi;
Miklos Szeredif43f0632005-02-28 11:46:56 +00001609 char *path;
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001610
1611 memset(&fi, 0, sizeof(fi));
1612 fi.flags = arg->flags;
1613
Miklos Szeredif43f0632005-02-28 11:46:56 +00001614 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001615 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredif43f0632005-02-28 11:46:56 +00001616 path = get_path(f, in->nodeid);
1617 if (path != NULL) {
Miklos Szeredif43f0632005-02-28 11:46:56 +00001618 res = f->op.opendir(path, &fi);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001619 dh->fh = fi.fh;
Miklos Szeredif43f0632005-02-28 11:46:56 +00001620 }
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001621 if (res == 0) {
1622 int res2;
1623 pthread_mutex_lock(&f->lock);
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001624 res2 = send_reply(f, in, res, &outarg, SIZEOF_COMPAT(f, fuse_open_out));
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001625 if(res2 == -ENOENT) {
1626 /* The opendir syscall was interrupted, so it must be
1627 cancelled */
1628 if(f->op.releasedir)
1629 f->op.releasedir(path, &fi);
Miklos Szerediab974562005-04-07 15:40:21 +00001630 pthread_mutex_destroy(&dh->lock);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001631 free(dh);
1632 }
1633 pthread_mutex_unlock(&f->lock);
1634 } else {
Miklos Szeredif43f0632005-02-28 11:46:56 +00001635 send_reply(f, in, res, NULL, 0);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001636 free(dh);
Miklos Szeredif43f0632005-02-28 11:46:56 +00001637 }
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001638 free(path);
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001639 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szerediab974562005-04-07 15:40:21 +00001640 } else
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001641 send_reply(f, in, 0, &outarg, SIZEOF_COMPAT(f, fuse_open_out));
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001642}
1643
Miklos Szerediab974562005-04-07 15:40:21 +00001644static int fill_dir_common(struct fuse_dirhandle *dh, const char *name,
1645 int type, ino_t ino, off_t off)
1646{
1647 unsigned namelen = strlen(name);
1648 unsigned entlen;
1649 unsigned entsize;
1650 unsigned padlen;
1651 unsigned newlen;
1652 unsigned char *newptr;
1653
Miklos Szeredi33be22d2005-05-27 09:12:43 +00001654 if (!(dh->fuse->flags & FUSE_USE_INO)) {
Miklos Szerediab974562005-04-07 15:40:21 +00001655 ino = (ino_t) -1;
Miklos Szeredi33be22d2005-05-27 09:12:43 +00001656 if (dh->fuse->flags & FUSE_READDIR_INO) {
1657 struct node *node;
1658 pthread_mutex_lock(&dh->fuse->lock);
Miklos Szeredi340d21f2005-07-06 10:07:52 +00001659 node = lookup_node(dh->fuse, dh->nodeid, name);
Miklos Szeredi33be22d2005-05-27 09:12:43 +00001660 if (node)
1661 ino = (ino_t) node->nodeid;
1662 pthread_mutex_unlock(&dh->fuse->lock);
1663 }
1664 }
Miklos Szerediab974562005-04-07 15:40:21 +00001665
1666 if (namelen > FUSE_NAME_MAX)
1667 namelen = FUSE_NAME_MAX;
1668 else if (!namelen) {
1669 dh->error = -EIO;
1670 return 1;
1671 }
1672
1673 entlen = (dh->fuse->major == 5 ?
1674 FUSE_NAME_OFFSET_COMPAT5 : FUSE_NAME_OFFSET) + namelen;
1675 entsize = FUSE_DIRENT_ALIGN(entlen);
1676 padlen = entsize - entlen;
1677 newlen = dh->len + entsize;
1678 if (off && dh->fuse->major != 5) {
1679 dh->filled = 0;
1680 if (newlen > dh->needlen)
1681 return 1;
1682 }
Miklos Szeredi21019c92005-05-09 11:22:41 +00001683
Miklos Szerediab974562005-04-07 15:40:21 +00001684 newptr = realloc(dh->contents, newlen);
1685 if (!newptr) {
1686 dh->error = -ENOMEM;
1687 return 1;
1688 }
1689 dh->contents = newptr;
1690 if (dh->fuse->major == 5) {
1691 struct fuse_dirent_compat5 *dirent;
1692 dirent = (struct fuse_dirent_compat5 *) (dh->contents + dh->len);
1693 dirent->ino = ino;
1694 dirent->namelen = namelen;
1695 dirent->type = type;
1696 strncpy(dirent->name, name, namelen);
1697 } else {
1698 struct fuse_dirent *dirent;
1699 dirent = (struct fuse_dirent *) (dh->contents + dh->len);
1700 dirent->ino = ino;
1701 dirent->off = off ? off : newlen;
1702 dirent->namelen = namelen;
1703 dirent->type = type;
1704 strncpy(dirent->name, name, namelen);
1705 }
1706 if (padlen)
1707 memset(dh->contents + dh->len + entlen, 0, padlen);
1708 dh->len = newlen;
1709 return 0;
1710}
1711
1712static int fill_dir(void *buf, const char *name, const struct stat *stat,
1713 off_t off)
1714{
1715 int type = stat ? (stat->st_mode & 0170000) >> 12 : 0;
1716 ino_t ino = stat ? stat->st_ino : (ino_t) -1;
1717 return fill_dir_common(buf, name, type, ino, off);
1718}
1719
1720static int fill_dir_old(struct fuse_dirhandle *dh, const char *name, int type,
1721 ino_t ino)
1722{
1723 fill_dir_common(dh, name, type, ino, 0);
1724 return dh->error;
1725}
1726
1727static int readdir_fill(struct fuse *f, struct fuse_in_header *in,
1728 struct fuse_read_in *arg, struct fuse_dirhandle *dh)
1729{
1730 int err = -ENOENT;
Miklos Szeredi129ef8f2005-06-20 13:48:42 +00001731 char *path;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001732 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredi129ef8f2005-06-20 13:48:42 +00001733 path = get_path(f, in->nodeid);
Miklos Szerediab974562005-04-07 15:40:21 +00001734 if (path != NULL) {
1735 struct fuse_file_info fi;
1736
1737 memset(&fi, 0, sizeof(fi));
1738 fi.fh = dh->fh;
1739
1740 dh->len = 0;
1741 dh->error = 0;
1742 dh->needlen = arg->size;
1743 dh->filled = 1;
1744 err = -ENOSYS;
1745 if (f->op.readdir) {
1746 off_t offset = f->major == 5 ? 0 : arg->offset;
1747 err = f->op.readdir(path, dh, fill_dir, offset, &fi);
1748 } else if (f->op.getdir)
1749 err = f->op.getdir(path, dh, fill_dir_old);
1750 if (!err)
1751 err = dh->error;
1752 if (err)
1753 dh->filled = 0;
1754 free(path);
1755 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001756 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szerediab974562005-04-07 15:40:21 +00001757 return err;
1758}
1759
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001760static void do_readdir(struct fuse *f, struct fuse_in_header *in,
1761 struct fuse_read_in *arg)
1762{
Miklos Szerediab974562005-04-07 15:40:21 +00001763 int err = 0;
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001764 struct fuse_dirhandle *dh = get_dirhandle(arg->fh);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001765 size_t size = 0;
Miklos Szerediab974562005-04-07 15:40:21 +00001766 unsigned char *buf = NULL;
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001767
Miklos Szerediab974562005-04-07 15:40:21 +00001768 pthread_mutex_lock(&dh->lock);
1769 if (!dh->filled) {
1770 err = readdir_fill(f, in, arg, dh);
1771 if (err)
1772 goto out;
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001773 }
Miklos Szerediab974562005-04-07 15:40:21 +00001774 if (dh->filled) {
Miklos Szeredib92d9782005-02-07 16:10:49 +00001775 if (arg->offset < dh->len) {
1776 size = arg->size;
1777 if (arg->offset + size > dh->len)
1778 size = dh->len - arg->offset;
Miklos Szerediab974562005-04-07 15:40:21 +00001779 buf = dh->contents + arg->offset;
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001780 }
Miklos Szerediab974562005-04-07 15:40:21 +00001781 } else {
1782 size = dh->len;
1783 buf = dh->contents;
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001784 }
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001785
Miklos Szerediab974562005-04-07 15:40:21 +00001786 out:
1787 send_reply(f, in, err, buf, size);
1788 pthread_mutex_unlock(&dh->lock);
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001789}
1790
1791static void do_releasedir(struct fuse *f, struct fuse_in_header *in,
1792 struct fuse_release_in *arg)
1793{
1794 struct fuse_dirhandle *dh = get_dirhandle(arg->fh);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001795 if (f->op.releasedir) {
1796 char *path;
1797 struct fuse_file_info fi;
Miklos Szerediab974562005-04-07 15:40:21 +00001798
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001799 memset(&fi, 0, sizeof(fi));
1800 fi.fh = dh->fh;
1801
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001802 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001803 path = get_path(f, in->nodeid);
1804 f->op.releasedir(path ? path : "-", &fi);
1805 free(path);
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001806 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001807 }
Miklos Szerediab974562005-04-07 15:40:21 +00001808 pthread_mutex_lock(&dh->lock);
1809 pthread_mutex_unlock(&dh->lock);
1810 pthread_mutex_destroy(&dh->lock);
Miklos Szeredib92d9782005-02-07 16:10:49 +00001811 free(dh->contents);
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001812 free(dh);
1813 send_reply(f, in, 0, NULL, 0);
1814}
1815
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001816static void do_fsyncdir(struct fuse *f, struct fuse_in_header *in,
1817 struct fuse_fsync_in *inarg)
1818{
1819 int res;
1820 char *path;
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001821 struct fuse_dirhandle *dh = get_dirhandle(inarg->fh);
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001822 struct fuse_file_info fi;
1823
1824 memset(&fi, 0, sizeof(fi));
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001825 fi.fh = dh->fh;
1826
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001827 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001828 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001829 path = get_path(f, in->nodeid);
1830 if (path != NULL) {
1831 res = -ENOSYS;
1832 if (f->op.fsyncdir)
1833 res = f->op.fsyncdir(path, inarg->fsync_flags & 1, &fi);
1834 free(path);
1835 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001836 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001837 send_reply(f, in, res, NULL, 0);
1838}
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001839
Miklos Szeredi43696432001-11-18 19:15:05 +00001840static void free_cmd(struct fuse_cmd *cmd)
1841{
1842 free(cmd->buf);
1843 free(cmd);
1844}
1845
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001846void fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd)
Miklos Szeredia181e612001-11-06 12:03:23 +00001847{
Miklos Szeredia181e612001-11-06 12:03:23 +00001848 struct fuse_in_header *in = (struct fuse_in_header *) cmd->buf;
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001849 void *inarg = cmd->buf + SIZEOF_COMPAT(f, fuse_in_header);
Miklos Szeredid169f312004-09-22 08:48:26 +00001850 struct fuse_context *ctx = fuse_get_context();
Miklos Szeredia181e612001-11-06 12:03:23 +00001851
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001852 fuse_dec_avail(f);
Miklos Szeredi33232032001-11-19 17:55:51 +00001853
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001854 if ((f->flags & FUSE_DEBUG)) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001855 printf("unique: %llu, opcode: %s (%i), nodeid: %lu, insize: %i\n",
1856 in->unique, opname(in->opcode), in->opcode,
1857 (unsigned long) in->nodeid, cmd->buflen);
Miklos Szeredic0938ea2001-11-07 12:35:06 +00001858 fflush(stdout);
1859 }
Miklos Szeredife25def2001-12-20 15:38:05 +00001860
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001861 if (!f->got_init && in->opcode != FUSE_INIT) {
1862 /* Old kernel version probably */
1863 send_reply(f, in, -EPROTO, NULL, 0);
1864 goto out;
1865 }
1866
Miklos Szeredi0111f9d2005-04-22 12:04:55 +00001867 if ((f->flags & FUSE_ALLOW_ROOT) && in->uid != f->owner && in->uid != 0 &&
1868 in->opcode != FUSE_INIT && in->opcode != FUSE_READ &&
Miklos Szeredi21019c92005-05-09 11:22:41 +00001869 in->opcode != FUSE_WRITE && in->opcode != FUSE_FSYNC &&
Miklos Szeredi0111f9d2005-04-22 12:04:55 +00001870 in->opcode != FUSE_RELEASE && in->opcode != FUSE_READDIR &&
1871 in->opcode != FUSE_FSYNCDIR && in->opcode != FUSE_RELEASEDIR) {
1872 send_reply(f, in, -EACCES, NULL, 0);
1873 goto out;
1874 }
1875
Miklos Szeredid169f312004-09-22 08:48:26 +00001876 ctx->fuse = f;
Miklos Szeredife25def2001-12-20 15:38:05 +00001877 ctx->uid = in->uid;
1878 ctx->gid = in->gid;
Miklos Szeredi1f18db52004-09-27 06:54:49 +00001879 ctx->pid = in->pid;
Miklos Szeredi159bd7e2005-02-28 17:32:16 +00001880 ctx->private_data = f->user_data;
Miklos Szeredie5183742005-02-02 11:14:04 +00001881
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001882 switch (in->opcode) {
Miklos Szeredia181e612001-11-06 12:03:23 +00001883 case FUSE_LOOKUP:
1884 do_lookup(f, in, (char *) inarg);
1885 break;
1886
Miklos Szeredia181e612001-11-06 12:03:23 +00001887 case FUSE_GETATTR:
1888 do_getattr(f, in);
1889 break;
1890
1891 case FUSE_SETATTR:
1892 do_setattr(f, in, (struct fuse_setattr_in *) inarg);
1893 break;
1894
1895 case FUSE_READLINK:
1896 do_readlink(f, in);
1897 break;
1898
Miklos Szeredia181e612001-11-06 12:03:23 +00001899 case FUSE_MKNOD:
1900 do_mknod(f, in, (struct fuse_mknod_in *) inarg);
1901 break;
Miklos Szeredie5183742005-02-02 11:14:04 +00001902
Miklos Szeredia181e612001-11-06 12:03:23 +00001903 case FUSE_MKDIR:
1904 do_mkdir(f, in, (struct fuse_mkdir_in *) inarg);
1905 break;
Miklos Szeredie5183742005-02-02 11:14:04 +00001906
Miklos Szeredia181e612001-11-06 12:03:23 +00001907 case FUSE_UNLINK:
Miklos Szeredib5958612004-02-20 14:10:49 +00001908 do_unlink(f, in, (char *) inarg);
1909 break;
1910
Miklos Szeredia181e612001-11-06 12:03:23 +00001911 case FUSE_RMDIR:
Miklos Szeredib5958612004-02-20 14:10:49 +00001912 do_rmdir(f, in, (char *) inarg);
Miklos Szeredia181e612001-11-06 12:03:23 +00001913 break;
1914
1915 case FUSE_SYMLINK:
Miklos Szeredie5183742005-02-02 11:14:04 +00001916 do_symlink(f, in, (char *) inarg,
Miklos Szeredia181e612001-11-06 12:03:23 +00001917 ((char *) inarg) + strlen((char *) inarg) + 1);
1918 break;
1919
1920 case FUSE_RENAME:
1921 do_rename(f, in, (struct fuse_rename_in *) inarg);
1922 break;
Miklos Szeredie5183742005-02-02 11:14:04 +00001923
Miklos Szeredia181e612001-11-06 12:03:23 +00001924 case FUSE_LINK:
1925 do_link(f, in, (struct fuse_link_in *) inarg);
1926 break;
1927
1928 case FUSE_OPEN:
1929 do_open(f, in, (struct fuse_open_in *) inarg);
1930 break;
1931
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001932 case FUSE_FLUSH:
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001933 do_flush(f, in, (struct fuse_flush_in *) inarg);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001934 break;
1935
Miklos Szeredi9478e862002-12-11 09:50:26 +00001936 case FUSE_RELEASE:
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001937 do_release(f, in, (struct fuse_release_in *) inarg);
Miklos Szeredi9478e862002-12-11 09:50:26 +00001938 break;
1939
Miklos Szeredia181e612001-11-06 12:03:23 +00001940 case FUSE_READ:
1941 do_read(f, in, (struct fuse_read_in *) inarg);
1942 break;
1943
1944 case FUSE_WRITE:
1945 do_write(f, in, (struct fuse_write_in *) inarg);
1946 break;
1947
Mark Glinesd84b39a2002-01-07 16:32:02 +00001948 case FUSE_STATFS:
1949 do_statfs(f, in);
1950 break;
1951
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001952 case FUSE_FSYNC:
1953 do_fsync(f, in, (struct fuse_fsync_in *) inarg);
1954 break;
1955
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001956 case FUSE_SETXATTR:
1957 do_setxattr(f, in, (struct fuse_setxattr_in *) inarg);
1958 break;
1959
1960 case FUSE_GETXATTR:
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001961 do_getxattr(f, in, (struct fuse_getxattr_in *) inarg);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001962 break;
1963
1964 case FUSE_LISTXATTR:
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001965 do_listxattr(f, in, (struct fuse_getxattr_in *) inarg);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001966 break;
1967
1968 case FUSE_REMOVEXATTR:
1969 do_removexattr(f, in, (char *) inarg);
1970 break;
1971
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001972 case FUSE_INIT:
1973 do_init(f, in, (struct fuse_init_in_out *) inarg);
1974 break;
1975
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001976 case FUSE_OPENDIR:
1977 do_opendir(f, in, (struct fuse_open_in *) inarg);
1978 break;
1979
1980 case FUSE_READDIR:
1981 do_readdir(f, in, (struct fuse_read_in *) inarg);
1982 break;
1983
1984 case FUSE_RELEASEDIR:
1985 do_releasedir(f, in, (struct fuse_release_in *) inarg);
1986 break;
1987
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001988 case FUSE_FSYNCDIR:
1989 do_fsyncdir(f, in, (struct fuse_fsync_in *) inarg);
1990 break;
1991
Miklos Szeredia181e612001-11-06 12:03:23 +00001992 default:
Miklos Szeredi43696432001-11-18 19:15:05 +00001993 send_reply(f, in, -ENOSYS, NULL, 0);
Miklos Szeredia181e612001-11-06 12:03:23 +00001994 }
Miklos Szeredi43696432001-11-18 19:15:05 +00001995
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001996 out:
Miklos Szeredi43696432001-11-18 19:15:05 +00001997 free_cmd(cmd);
Miklos Szeredia181e612001-11-06 12:03:23 +00001998}
1999
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002000int fuse_exited(struct fuse* f)
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00002001{
2002 return f->exited;
2003}
2004
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002005struct fuse_cmd *fuse_read_cmd(struct fuse *f)
Miklos Szeredi2df1c042001-11-06 15:07:17 +00002006{
Miklos Szeredifff56ab2001-11-16 10:12:59 +00002007 ssize_t res;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00002008 struct fuse_cmd *cmd;
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00002009 struct fuse_in_header *in;
2010 void *inarg;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00002011
Miklos Szeredi43696432001-11-18 19:15:05 +00002012 cmd = (struct fuse_cmd *) malloc(sizeof(struct fuse_cmd));
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002013 if (cmd == NULL) {
2014 fprintf(stderr, "fuse: failed to allocate cmd in read\n");
2015 return NULL;
2016 }
Miklos Szeredi43696432001-11-18 19:15:05 +00002017 cmd->buf = (char *) malloc(FUSE_MAX_IN);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002018 if (cmd->buf == NULL) {
2019 fprintf(stderr, "fuse: failed to allocate read buffer\n");
2020 free(cmd);
2021 return NULL;
2022 }
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00002023 in = (struct fuse_in_header *) cmd->buf;
Miklos Szeredi30e093a2005-04-03 17:44:54 +00002024 inarg = cmd->buf + SIZEOF_COMPAT(f, fuse_in_header);
Miklos Szeredi43696432001-11-18 19:15:05 +00002025
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00002026 res = read(f->fd, cmd->buf, FUSE_MAX_IN);
2027 if (res == -1) {
2028 free_cmd(cmd);
Miklos Szeredie56818b2004-12-12 11:45:24 +00002029 if (fuse_exited(f) || errno == EINTR || errno == ENOENT)
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00002030 return NULL;
Miklos Szeredie5183742005-02-02 11:14:04 +00002031
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00002032 /* ENODEV means we got unmounted, so we silenty return failure */
2033 if (errno != ENODEV) {
2034 /* BAD... This will happen again */
2035 perror("fuse: reading device");
2036 }
Miklos Szeredie5183742005-02-02 11:14:04 +00002037
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00002038 fuse_exit(f);
2039 return NULL;
2040 }
Miklos Szeredi30e093a2005-04-03 17:44:54 +00002041 if ((size_t) res < SIZEOF_COMPAT(f, fuse_in_header)) {
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00002042 free_cmd(cmd);
2043 /* Cannot happen */
2044 fprintf(stderr, "short read on fuse device\n");
2045 fuse_exit(f);
2046 return NULL;
2047 }
2048 cmd->buflen = res;
Miklos Szeredie5183742005-02-02 11:14:04 +00002049
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00002050 /* Forget is special, it can be done without messing with threads. */
2051 if (in->opcode == FUSE_FORGET) {
2052 do_forget(f, in, (struct fuse_forget_in *) inarg);
2053 free_cmd(cmd);
2054 return NULL;
2055 }
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00002056
Miklos Szeredifff56ab2001-11-16 10:12:59 +00002057 return cmd;
Miklos Szeredi2df1c042001-11-06 15:07:17 +00002058}
2059
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002060int fuse_loop(struct fuse *f)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002061{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00002062 if (f == NULL)
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002063 return -1;
Miklos Szeredic40748a2004-02-20 16:38:45 +00002064
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00002065 while (1) {
Miklos Szeredi0f48a262002-12-05 14:23:01 +00002066 struct fuse_cmd *cmd;
2067
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002068 if (fuse_exited(f))
Miklos Szeredi874e3c12004-11-01 23:15:20 +00002069 break;
Miklos Szeredi0f48a262002-12-05 14:23:01 +00002070
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002071 cmd = fuse_read_cmd(f);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00002072 if (cmd == NULL)
Miklos Szeredi0f48a262002-12-05 14:23:01 +00002073 continue;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002074
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002075 fuse_process_cmd(f, cmd);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002076 }
Miklos Szeredi874e3c12004-11-01 23:15:20 +00002077 f->exited = 0;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002078 return 0;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002079}
2080
Miklos Szeredi891b8742004-07-29 09:27:49 +00002081int fuse_invalidate(struct fuse *f, const char *path)
2082{
Miklos Szeredie56818b2004-12-12 11:45:24 +00002083 (void) f;
2084 (void) path;
2085 return -EINVAL;
Miklos Szeredi891b8742004-07-29 09:27:49 +00002086}
2087
Miklos Szeredi0f48a262002-12-05 14:23:01 +00002088void fuse_exit(struct fuse *f)
2089{
2090 f->exited = 1;
2091}
2092
Miklos Szeredid169f312004-09-22 08:48:26 +00002093struct fuse_context *fuse_get_context()
Miklos Szeredi2e50d432001-12-20 12:17:25 +00002094{
Miklos Szeredid169f312004-09-22 08:48:26 +00002095 static struct fuse_context context;
2096 if (fuse_getcontext)
2097 return fuse_getcontext();
Miklos Szeredi2e50d432001-12-20 12:17:25 +00002098 else
Miklos Szeredid169f312004-09-22 08:48:26 +00002099 return &context;
2100}
2101
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002102void fuse_set_getcontext_func(struct fuse_context *(*func)(void))
Miklos Szeredid169f312004-09-22 08:48:26 +00002103{
2104 fuse_getcontext = func;
Miklos Szeredi2e50d432001-12-20 12:17:25 +00002105}
2106
Miklos Szeredie331c4b2005-07-06 13:34:02 +00002107static int begins_with(const char *s, const char *beg)
2108{
2109 if (strncmp(s, beg, strlen(beg)) == 0)
2110 return 1;
2111 else
2112 return 0;
2113}
2114
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002115int fuse_is_lib_option(const char *opt)
2116{
2117 if (strcmp(opt, "debug") == 0 ||
Miklos Szeredia13d9002004-11-02 17:32:03 +00002118 strcmp(opt, "hard_remove") == 0 ||
Miklos Szeredi0111f9d2005-04-22 12:04:55 +00002119 strcmp(opt, "use_ino") == 0 ||
Miklos Szeredi33be22d2005-05-27 09:12:43 +00002120 strcmp(opt, "allow_root") == 0 ||
Miklos Szeredie331c4b2005-07-06 13:34:02 +00002121 strcmp(opt, "readdir_ino") == 0 ||
2122 begins_with(opt, "umask=") ||
2123 begins_with(opt, "uid=") ||
2124 begins_with(opt, "gid="))
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002125 return 1;
2126 else
2127 return 0;
2128}
2129
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002130static int parse_lib_opts(struct fuse *f, const char *opts)
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002131{
2132 if (opts) {
2133 char *xopts = strdup(opts);
2134 char *s = xopts;
2135 char *opt;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002136
Miklos Szeredie56818b2004-12-12 11:45:24 +00002137 if (xopts == NULL) {
2138 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002139 return -1;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002140 }
Miklos Szeredie5183742005-02-02 11:14:04 +00002141
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002142 while((opt = strsep(&s, ","))) {
2143 if (strcmp(opt, "debug") == 0)
2144 f->flags |= FUSE_DEBUG;
2145 else if (strcmp(opt, "hard_remove") == 0)
2146 f->flags |= FUSE_HARD_REMOVE;
Miklos Szeredia13d9002004-11-02 17:32:03 +00002147 else if (strcmp(opt, "use_ino") == 0)
2148 f->flags |= FUSE_USE_INO;
Miklos Szeredi0111f9d2005-04-22 12:04:55 +00002149 else if (strcmp(opt, "allow_root") == 0)
2150 f->flags |= FUSE_ALLOW_ROOT;
Miklos Szeredi33be22d2005-05-27 09:12:43 +00002151 else if (strcmp(opt, "readdir_ino") == 0)
2152 f->flags |= FUSE_READDIR_INO;
Miklos Szeredie331c4b2005-07-06 13:34:02 +00002153 else if (sscanf(opt, "umask=%o", &f->umask) == 1)
2154 f->flags |= FUSE_SET_MODE;
2155 else if (sscanf(opt, "uid=%u", &f->uid) == 1)
2156 f->flags |= FUSE_SET_UID;
2157 else if(sscanf(opt, "gid=%u", &f->gid) == 1)
2158 f->flags |= FUSE_SET_GID;
Miklos Szeredie5183742005-02-02 11:14:04 +00002159 else
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002160 fprintf(stderr, "fuse: warning: unknown option `%s'\n", opt);
2161 }
2162 free(xopts);
2163 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002164 return 0;
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002165}
2166
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002167struct fuse *fuse_new_common(int fd, const char *opts,
2168 const struct fuse_operations *op,
2169 size_t op_size, int compat)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002170{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002171 struct fuse *f;
2172 struct node *root;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002173
Miklos Szeredi0f62d722005-01-04 12:45:54 +00002174 if (sizeof(struct fuse_operations) < op_size) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002175 fprintf(stderr, "fuse: warning: library too old, some operations may not not work\n");
Miklos Szeredi0f62d722005-01-04 12:45:54 +00002176 op_size = sizeof(struct fuse_operations);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002177 }
2178
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002179 f = (struct fuse *) calloc(1, sizeof(struct fuse));
Miklos Szeredie56818b2004-12-12 11:45:24 +00002180 if (f == NULL) {
2181 fprintf(stderr, "fuse: failed to allocate fuse object\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002182 goto out;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002183 }
Miklos Szeredi2df1c042001-11-06 15:07:17 +00002184
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002185 if (parse_lib_opts(f, opts) == -1)
2186 goto out_free;
2187
Miklos Szeredi8cffdb92001-11-09 14:49:18 +00002188 f->fd = fd;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002189 f->ctr = 0;
Miklos Szeredi76f65782004-02-19 16:55:40 +00002190 f->generation = 0;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00002191 /* FIXME: Dynamic hash table */
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002192 f->name_table_size = 14057;
2193 f->name_table = (struct node **)
2194 calloc(1, sizeof(struct node *) * f->name_table_size);
Miklos Szeredie56818b2004-12-12 11:45:24 +00002195 if (f->name_table == NULL) {
2196 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002197 goto out_free;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002198 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002199
Miklos Szeredia13d9002004-11-02 17:32:03 +00002200 f->id_table_size = 14057;
2201 f->id_table = (struct node **)
2202 calloc(1, sizeof(struct node *) * f->id_table_size);
Miklos Szeredie56818b2004-12-12 11:45:24 +00002203 if (f->id_table == NULL) {
2204 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002205 goto out_free_name_table;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002206 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002207
Miklos Szerediab974562005-04-07 15:40:21 +00002208 mutex_init(&f->lock);
2209 mutex_init(&f->worker_lock);
Miklos Szeredi33232032001-11-19 17:55:51 +00002210 f->numworker = 0;
2211 f->numavail = 0;
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002212 memcpy(&f->op, op, op_size);
2213 f->compat = compat;
Miklos Szeredi0f48a262002-12-05 14:23:01 +00002214 f->exited = 0;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002215
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002216 root = (struct node *) calloc(1, sizeof(struct node));
Miklos Szeredie56818b2004-12-12 11:45:24 +00002217 if (root == NULL) {
2218 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredia13d9002004-11-02 17:32:03 +00002219 goto out_free_id_table;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002220 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002221
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002222 root->name = strdup("/");
Miklos Szeredie56818b2004-12-12 11:45:24 +00002223 if (root->name == NULL) {
2224 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002225 goto out_free_root;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002226 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002227
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002228 root->parent = 0;
Miklos Szeredia13d9002004-11-02 17:32:03 +00002229 root->nodeid = FUSE_ROOT_ID;
Miklos Szeredi76f65782004-02-19 16:55:40 +00002230 root->generation = 0;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00002231 root->refctr = 1;
Miklos Szeredi38009022005-05-08 19:47:22 +00002232 root->nlookup = 1;
Miklos Szeredia13d9002004-11-02 17:32:03 +00002233 hash_id(f, root);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002234
Miklos Szeredi0111f9d2005-04-22 12:04:55 +00002235 f->owner = getuid();
2236
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002237 return f;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002238
2239 out_free_root:
2240 free(root);
Miklos Szeredia13d9002004-11-02 17:32:03 +00002241 out_free_id_table:
2242 free(f->id_table);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002243 out_free_name_table:
2244 free(f->name_table);
2245 out_free:
2246 free(f);
2247 out:
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002248 return NULL;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002249}
2250
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002251struct fuse *fuse_new(int fd, const char *opts,
2252 const struct fuse_operations *op, size_t op_size)
2253{
2254 return fuse_new_common(fd, opts, op, op_size, 0);
2255}
2256
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002257struct fuse *fuse_new_compat2(int fd, const char *opts,
2258 const struct fuse_operations_compat2 *op)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002259{
2260 return fuse_new_common(fd, opts, (struct fuse_operations *) op,
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002261 sizeof(struct fuse_operations_compat2), 21);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002262}
2263
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002264struct fuse *fuse_new_compat1(int fd, int flags,
2265 const struct fuse_operations_compat1 *op)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002266{
2267 char *opts = NULL;
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002268 if (flags & FUSE_DEBUG_COMPAT1)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002269 opts = "debug";
2270 return fuse_new_common(fd, opts, (struct fuse_operations *) op,
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002271 sizeof(struct fuse_operations_compat1), 11);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002272}
2273
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002274void fuse_destroy(struct fuse *f)
2275{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002276 size_t i;
Miklos Szeredia13d9002004-11-02 17:32:03 +00002277 for (i = 0; i < f->id_table_size; i++) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002278 struct node *node;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002279
Miklos Szeredia13d9002004-11-02 17:32:03 +00002280 for (node = f->id_table[i]; node != NULL; node = node->id_next) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002281 if (node->is_hidden) {
Miklos Szeredia13d9002004-11-02 17:32:03 +00002282 char *path = get_path(f, node->nodeid);
Miklos Szeredi21019c92005-05-09 11:22:41 +00002283 if (path) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002284 f->op.unlink(path);
Miklos Szeredi21019c92005-05-09 11:22:41 +00002285 free(path);
2286 }
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002287 }
2288 }
2289 }
Miklos Szeredia13d9002004-11-02 17:32:03 +00002290 for (i = 0; i < f->id_table_size; i++) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002291 struct node *node;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002292 struct node *next;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002293
Miklos Szeredia13d9002004-11-02 17:32:03 +00002294 for (node = f->id_table[i]; node != NULL; node = next) {
2295 next = node->id_next;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002296 free_node(node);
2297 }
2298 }
Miklos Szeredia13d9002004-11-02 17:32:03 +00002299 free(f->id_table);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002300 free(f->name_table);
Miklos Szeredia181e612001-11-06 12:03:23 +00002301 pthread_mutex_destroy(&f->lock);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00002302 pthread_mutex_destroy(&f->worker_lock);
Miklos Szeredi159bd7e2005-02-28 17:32:16 +00002303 if (f->op.destroy)
2304 f->op.destroy(f->user_data);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002305 free(f);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002306}
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002307
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002308__asm__(".symver fuse_exited,__fuse_exited@");
2309__asm__(".symver fuse_process_cmd,__fuse_process_cmd@");
2310__asm__(".symver fuse_read_cmd,__fuse_read_cmd@");
2311__asm__(".symver fuse_set_getcontext_func,__fuse_set_getcontext_func@");
2312__asm__(".symver fuse_new_compat2,fuse_new@");