blob: 0bd4b86ba1d3fd0bd7b1aeee6cd0d39d724c1b3e [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 Szeredi97c61e92001-11-07 12:09:43 +000043#define FUSE_MAX_PATH 4096
Miklos Szeredi30e093a2005-04-03 17:44:54 +000044#define PARAM_T(inarg, type) (((char *)(inarg)) + sizeof(type))
45#define PARAM(inarg) PARAM_T(inarg, *(inarg))
46#define PARAM_COMPAT(f, inarg, type) \
47 ((f)->major == 5 ? PARAM_T(inarg, struct type ## _compat5) : PARAM(inarg))
48
49#define MEMBER_COMPAT(f, ptr, memb, type) \
50 ((f)->major == 5 ? &((struct type ## _compat5 *) (ptr))->memb : &ptr->memb)
51
52#define SIZEOF_COMPAT(f, type) \
53 ((f)->major == 5 ? sizeof(struct type ## _compat5) : sizeof(struct type))
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000054
Miklos Szeredi254d5ed2004-03-02 11:11:24 +000055#define ENTRY_REVALIDATE_TIME 1 /* sec */
56#define ATTR_REVALIDATE_TIME 1 /* sec */
57
Miklos Szeredi0f62d722005-01-04 12:45:54 +000058
59struct node {
60 struct node *name_next;
61 struct node *id_next;
62 nodeid_t nodeid;
63 unsigned int generation;
64 int refctr;
65 nodeid_t parent;
66 char *name;
67 uint64_t version;
Miklos Szeredi38009022005-05-08 19:47:22 +000068 uint64_t nlookup;
Miklos Szeredi0f62d722005-01-04 12:45:54 +000069 int open_count;
70 int is_hidden;
71};
72
73struct fuse_dirhandle {
Miklos Szerediab974562005-04-07 15:40:21 +000074 pthread_mutex_t lock;
Miklos Szeredi0f62d722005-01-04 12:45:54 +000075 struct fuse *fuse;
Miklos Szeredib92d9782005-02-07 16:10:49 +000076 unsigned char *contents;
Miklos Szerediab974562005-04-07 15:40:21 +000077 int allocated;
Miklos Szeredib92d9782005-02-07 16:10:49 +000078 unsigned len;
Miklos Szerediab974562005-04-07 15:40:21 +000079 unsigned needlen;
Miklos Szeredi3ead28e2005-01-18 21:23:41 +000080 int filled;
Miklos Szerediede1f7a2005-04-01 21:08:57 +000081 unsigned long fh;
Miklos Szerediab974562005-04-07 15:40:21 +000082 int error;
Miklos Szeredi33be22d2005-05-27 09:12:43 +000083 struct node *node;
Miklos Szeredi0f62d722005-01-04 12:45:54 +000084};
85
86struct fuse_cmd {
87 char *buf;
88 size_t buflen;
89};
90
91
Miklos Szeredid169f312004-09-22 08:48:26 +000092static struct fuse_context *(*fuse_getcontext)(void) = NULL;
93
Miklos Szerediab974562005-04-07 15:40:21 +000094#ifndef USE_UCLIBC
95#define mutex_init(mut) pthread_mutex_init(mut, NULL)
96#else
Miklos Szeredi129ef8f2005-06-20 13:48:42 +000097static void mutex_init(pthread_mutex_t *mut)
Miklos Szerediab974562005-04-07 15:40:21 +000098{
99 pthread_mutexattr_t attr;
100 pthread_mutexattr_init(&attr);
101 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
102 pthread_mutex_init(mut, &attr);
103 pthread_mutexattr_destroy(&attr);
104}
105#endif
106
Miklos Szeredic8ba2372002-12-10 12:26:00 +0000107static const char *opname(enum fuse_opcode opcode)
108{
Miklos Szeredie5183742005-02-02 11:14:04 +0000109 switch (opcode) {
Miklos Szeredi3ed84232004-03-30 15:17:26 +0000110 case FUSE_LOOKUP: return "LOOKUP";
111 case FUSE_FORGET: return "FORGET";
112 case FUSE_GETATTR: return "GETATTR";
113 case FUSE_SETATTR: return "SETATTR";
114 case FUSE_READLINK: return "READLINK";
115 case FUSE_SYMLINK: return "SYMLINK";
Miklos Szeredi3ed84232004-03-30 15:17:26 +0000116 case FUSE_MKNOD: return "MKNOD";
117 case FUSE_MKDIR: return "MKDIR";
118 case FUSE_UNLINK: return "UNLINK";
119 case FUSE_RMDIR: return "RMDIR";
120 case FUSE_RENAME: return "RENAME";
121 case FUSE_LINK: return "LINK";
122 case FUSE_OPEN: return "OPEN";
123 case FUSE_READ: return "READ";
124 case FUSE_WRITE: return "WRITE";
125 case FUSE_STATFS: return "STATFS";
Miklos Szeredi99f20742004-05-19 08:01:10 +0000126 case FUSE_FLUSH: return "FLUSH";
Miklos Szeredi3ed84232004-03-30 15:17:26 +0000127 case FUSE_RELEASE: return "RELEASE";
128 case FUSE_FSYNC: return "FSYNC";
129 case FUSE_SETXATTR: return "SETXATTR";
130 case FUSE_GETXATTR: return "GETXATTR";
131 case FUSE_LISTXATTR: return "LISTXATTR";
132 case FUSE_REMOVEXATTR: return "REMOVEXATTR";
Miklos Szeredi3f0005f2005-01-04 19:24:31 +0000133 case FUSE_INIT: return "INIT";
Miklos Szeredi3ead28e2005-01-18 21:23:41 +0000134 case FUSE_OPENDIR: return "OPENDIR";
135 case FUSE_READDIR: return "READDIR";
136 case FUSE_RELEASEDIR: return "RELEASEDIR";
Miklos Szeredi4283ee72005-03-21 12:09:04 +0000137 case FUSE_FSYNCDIR: return "FSYNCDIR";
Miklos Szeredi99f20742004-05-19 08:01:10 +0000138 default: return "???";
Miklos Szeredic8ba2372002-12-10 12:26:00 +0000139 }
140}
141
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000142static inline void fuse_dec_avail(struct fuse *f)
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000143{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000144 pthread_mutex_lock(&f->worker_lock);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000145 f->numavail --;
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000146 pthread_mutex_unlock(&f->worker_lock);
147}
148
149static inline void fuse_inc_avail(struct fuse *f)
150{
151 pthread_mutex_lock(&f->worker_lock);
152 f->numavail ++;
153 pthread_mutex_unlock(&f->worker_lock);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000154}
155
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000156static struct node *get_node_nocheck(struct fuse *f, nodeid_t nodeid)
Miklos Szeredia181e612001-11-06 12:03:23 +0000157{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000158 size_t hash = nodeid % f->id_table_size;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000159 struct node *node;
160
Miklos Szeredia13d9002004-11-02 17:32:03 +0000161 for (node = f->id_table[hash]; node != NULL; node = node->id_next)
162 if (node->nodeid == nodeid)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000163 return node;
Miklos Szeredie5183742005-02-02 11:14:04 +0000164
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000165 return NULL;
Miklos Szeredia181e612001-11-06 12:03:23 +0000166}
167
Miklos Szeredia13d9002004-11-02 17:32:03 +0000168static struct node *get_node(struct fuse *f, nodeid_t nodeid)
Miklos Szeredia181e612001-11-06 12:03:23 +0000169{
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000170 struct node *node = get_node_nocheck(f, nodeid);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000171 if (!node) {
172 fprintf(stderr, "fuse internal error: node %lu not found\n",
173 nodeid);
174 abort();
175 }
176 return node;
Miklos Szeredia181e612001-11-06 12:03:23 +0000177}
178
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000179static void free_node(struct node *node)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000180{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000181 free(node->name);
182 free(node);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000183}
184
Miklos Szeredia13d9002004-11-02 17:32:03 +0000185static void unhash_id(struct fuse *f, struct node *node)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000186{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000187 size_t hash = node->nodeid % f->id_table_size;
188 struct node **nodep = &f->id_table[hash];
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000189
Miklos Szeredie5183742005-02-02 11:14:04 +0000190 for (; *nodep != NULL; nodep = &(*nodep)->id_next)
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000191 if (*nodep == node) {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000192 *nodep = node->id_next;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000193 return;
194 }
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000195}
196
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000197static void hash_id(struct fuse *f, struct node *node)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000198{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000199 size_t hash = node->nodeid % f->id_table_size;
200 node->id_next = f->id_table[hash];
Miklos Szeredie5183742005-02-02 11:14:04 +0000201 f->id_table[hash] = node;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000202}
203
Miklos Szeredia13d9002004-11-02 17:32:03 +0000204static unsigned int name_hash(struct fuse *f, nodeid_t parent, const char *name)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000205{
206 unsigned int hash = *name;
207
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000208 if (hash)
209 for (name += 1; *name != '\0'; name++)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000210 hash = (hash << 5) - hash + *name;
211
212 return (hash + parent) % f->name_table_size;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000213}
214
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000215static void unref_node(struct fuse *f, struct node *node);
216
217static void unhash_name(struct fuse *f, struct node *node)
218{
219 if (node->name) {
220 size_t hash = name_hash(f, node->parent, node->name);
221 struct node **nodep = &f->name_table[hash];
Miklos Szeredie5183742005-02-02 11:14:04 +0000222
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000223 for (; *nodep != NULL; nodep = &(*nodep)->name_next)
224 if (*nodep == node) {
225 *nodep = node->name_next;
226 node->name_next = NULL;
227 unref_node(f, get_node(f, node->parent));
228 free(node->name);
229 node->name = NULL;
230 node->parent = 0;
231 return;
232 }
233 fprintf(stderr, "fuse internal error: unable to unhash node: %lu\n",
234 node->nodeid);
235 abort();
236 }
237}
238
239static int hash_name(struct fuse *f, struct node *node, nodeid_t parent,
240 const char *name)
241{
242 size_t hash = name_hash(f, parent, name);
243 node->name = strdup(name);
244 if (node->name == NULL)
245 return -1;
246
247 get_node(f, parent)->refctr ++;
248 node->parent = parent;
249 node->name_next = f->name_table[hash];
250 f->name_table[hash] = node;
251 return 0;
252}
253
254static void delete_node(struct fuse *f, struct node *node)
255{
Miklos Szeredi38009022005-05-08 19:47:22 +0000256 if (f->flags & FUSE_DEBUG) {
257 printf("delete: %lu\n", node->nodeid);
258 fflush(stdout);
259 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000260 assert(!node->name);
261 unhash_id(f, node);
262 free_node(node);
263}
264
265static void unref_node(struct fuse *f, struct node *node)
266{
267 assert(node->refctr > 0);
268 node->refctr --;
269 if (!node->refctr)
270 delete_node(f, node);
271}
272
273static nodeid_t next_id(struct fuse *f)
274{
275 do {
276 f->ctr++;
277 if (!f->ctr)
278 f->generation ++;
279 } while (f->ctr == 0 || get_node_nocheck(f, f->ctr) != NULL);
280 return f->ctr;
281}
282
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000283static struct node *lookup_node(struct fuse *f, nodeid_t parent,
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000284 const char *name)
285{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000286 size_t hash = name_hash(f, parent, name);
287 struct node *node;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000288
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000289 for (node = f->name_table[hash]; node != NULL; node = node->name_next)
290 if (node->parent == parent && strcmp(node->name, name) == 0)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000291 return node;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000292
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000293 return NULL;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000294}
295
Miklos Szeredia13d9002004-11-02 17:32:03 +0000296static struct node *find_node(struct fuse *f, nodeid_t parent, char *name,
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000297 struct fuse_attr *attr, uint64_t version)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000298{
299 struct node *node;
Miklos Szeredia181e612001-11-06 12:03:23 +0000300 int mode = attr->mode & S_IFMT;
301 int rdev = 0;
Miklos Szeredie5183742005-02-02 11:14:04 +0000302
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000303 if (S_ISCHR(mode) || S_ISBLK(mode))
Miklos Szeredia181e612001-11-06 12:03:23 +0000304 rdev = attr->rdev;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000305
Miklos Szeredia181e612001-11-06 12:03:23 +0000306 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000307 node = lookup_node(f, parent, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000308 if (node != NULL) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000309 if (!(f->flags & FUSE_USE_INO))
Miklos Szeredie5183742005-02-02 11:14:04 +0000310 attr->ino = node->nodeid;
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000311 } else {
312 node = (struct node *) calloc(1, sizeof(struct node));
313 if (node == NULL)
314 goto out_err;
Miklos Szeredie5183742005-02-02 11:14:04 +0000315
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000316 node->refctr = 1;
317 node->nodeid = next_id(f);
318 if (!(f->flags & FUSE_USE_INO))
319 attr->ino = node->nodeid;
320 node->open_count = 0;
321 node->is_hidden = 0;
322 node->generation = f->generation;
323 if (hash_name(f, node, parent, name) == -1) {
324 free(node);
325 node = NULL;
326 goto out_err;
327 }
328 hash_id(f, node);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000329 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000330 node->version = version;
Miklos Szeredi38009022005-05-08 19:47:22 +0000331 node->nlookup ++;
Miklos Szeredic2309912004-09-21 13:40:38 +0000332 out_err:
Miklos Szeredia181e612001-11-06 12:03:23 +0000333 pthread_mutex_unlock(&f->lock);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000334 return node;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000335}
336
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000337static char *add_name(char *buf, char *s, const char *name)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000338{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000339 size_t len = strlen(name);
340 s -= len;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000341 if (s <= buf) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000342 fprintf(stderr, "fuse: path too long: ...%s\n", s + len);
343 return NULL;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000344 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000345 strncpy(s, name, len);
346 s--;
347 *s = '/';
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000348
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000349 return s;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000350}
351
Miklos Szeredia13d9002004-11-02 17:32:03 +0000352static char *get_path_name(struct fuse *f, nodeid_t nodeid, const char *name)
Miklos Szeredib483c932001-10-29 14:57:57 +0000353{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000354 char buf[FUSE_MAX_PATH];
355 char *s = buf + FUSE_MAX_PATH - 1;
356 struct node *node;
Miklos Szeredie5183742005-02-02 11:14:04 +0000357
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000358 *s = '\0';
Miklos Szeredia181e612001-11-06 12:03:23 +0000359
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000360 if (name != NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000361 s = add_name(buf, s, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000362 if (s == NULL)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000363 return NULL;
364 }
365
366 pthread_mutex_lock(&f->lock);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000367 for (node = get_node(f, nodeid); node && node->nodeid != FUSE_ROOT_ID;
368 node = get_node(f, node->parent)) {
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000369 if (node->name == NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000370 s = NULL;
371 break;
372 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000373
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000374 s = add_name(buf, s, node->name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000375 if (s == NULL)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000376 break;
377 }
378 pthread_mutex_unlock(&f->lock);
379
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000380 if (node == NULL || s == NULL)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000381 return NULL;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000382 else if (*s == '\0')
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000383 return strdup("/");
384 else
385 return strdup(s);
386}
Miklos Szeredia181e612001-11-06 12:03:23 +0000387
Miklos Szeredia13d9002004-11-02 17:32:03 +0000388static char *get_path(struct fuse *f, nodeid_t nodeid)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000389{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000390 return get_path_name(f, nodeid, NULL);
Miklos Szeredib483c932001-10-29 14:57:57 +0000391}
392
Miklos Szeredi38009022005-05-08 19:47:22 +0000393static void forget_node(struct fuse *f, nodeid_t nodeid, uint64_t nlookup)
394{
395 struct node *node;
396 if (nodeid == FUSE_ROOT_ID)
397 return;
398 pthread_mutex_lock(&f->lock);
399 node = get_node(f, nodeid);
400 assert(node->nlookup >= nlookup);
401 node->nlookup -= nlookup;
402 if (!node->nlookup) {
403 unhash_name(f, node);
404 unref_node(f, node);
405 }
406 pthread_mutex_unlock(&f->lock);
407}
408
409static void forget_node_old(struct fuse *f, nodeid_t nodeid, uint64_t version)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000410{
Miklos Szeredia181e612001-11-06 12:03:23 +0000411 struct node *node;
412
413 pthread_mutex_lock(&f->lock);
Miklos Szeredid0cf1fb2005-05-06 10:10:38 +0000414 node = get_node_nocheck(f, nodeid);
415 if (node && node->version == version && nodeid != FUSE_ROOT_ID) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000416 node->version = 0;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000417 unhash_name(f, node);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000418 unref_node(f, node);
Miklos Szeredia181e612001-11-06 12:03:23 +0000419 }
420 pthread_mutex_unlock(&f->lock);
Miklos Szeredi38009022005-05-08 19:47:22 +0000421}
Miklos Szeredia181e612001-11-06 12:03:23 +0000422
Miklos Szeredi38009022005-05-08 19:47:22 +0000423static void cancel_lookup(struct fuse *f, nodeid_t nodeid, uint64_t version)
424{
425 if (f->major <= 6)
426 forget_node_old(f, nodeid, version);
427 else
428 forget_node(f, nodeid, 1);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000429}
430
Miklos Szeredia13d9002004-11-02 17:32:03 +0000431static void remove_node(struct fuse *f, nodeid_t dir, const char *name)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000432{
Miklos Szeredia181e612001-11-06 12:03:23 +0000433 struct node *node;
434
435 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000436 node = lookup_node(f, dir, name);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000437 if (node != NULL)
438 unhash_name(f, node);
Miklos Szeredia181e612001-11-06 12:03:23 +0000439 pthread_mutex_unlock(&f->lock);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000440}
441
Miklos Szeredia13d9002004-11-02 17:32:03 +0000442static int rename_node(struct fuse *f, nodeid_t olddir, const char *oldname,
443 nodeid_t newdir, const char *newname, int hide)
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000444{
Miklos Szeredia181e612001-11-06 12:03:23 +0000445 struct node *node;
446 struct node *newnode;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000447 int err = 0;
Miklos Szeredie5183742005-02-02 11:14:04 +0000448
Miklos Szeredia181e612001-11-06 12:03:23 +0000449 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000450 node = lookup_node(f, olddir, oldname);
451 newnode = lookup_node(f, newdir, newname);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000452 if (node == NULL)
453 goto out;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000454
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000455 if (newnode != NULL) {
456 if (hide) {
457 fprintf(stderr, "fuse: hidden file got created during hiding\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000458 err = -EBUSY;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000459 goto out;
460 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000461 unhash_name(f, newnode);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000462 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000463
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000464 unhash_name(f, node);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000465 if (hash_name(f, node, newdir, newname) == -1) {
466 err = -ENOMEM;
467 goto out;
468 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000469
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000470 if (hide)
471 node->is_hidden = 1;
472
473 out:
Miklos Szeredia181e612001-11-06 12:03:23 +0000474 pthread_mutex_unlock(&f->lock);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000475 return err;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000476}
477
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000478static void convert_stat(struct stat *stbuf, struct fuse_attr *attr)
479{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000480 attr->ino = stbuf->st_ino;
Miklos Szeredib5958612004-02-20 14:10:49 +0000481 attr->mode = stbuf->st_mode;
482 attr->nlink = stbuf->st_nlink;
483 attr->uid = stbuf->st_uid;
484 attr->gid = stbuf->st_gid;
485 attr->rdev = stbuf->st_rdev;
486 attr->size = stbuf->st_size;
487 attr->blocks = stbuf->st_blocks;
488 attr->atime = stbuf->st_atime;
Miklos Szeredib5958612004-02-20 14:10:49 +0000489 attr->mtime = stbuf->st_mtime;
Miklos Szeredib5958612004-02-20 14:10:49 +0000490 attr->ctime = stbuf->st_ctime;
Miklos Szeredicb264512004-06-23 18:52:50 +0000491#ifdef HAVE_STRUCT_STAT_ST_ATIM
492 attr->atimensec = stbuf->st_atim.tv_nsec;
493 attr->mtimensec = stbuf->st_mtim.tv_nsec;
Miklos Szeredib5958612004-02-20 14:10:49 +0000494 attr->ctimensec = stbuf->st_ctim.tv_nsec;
Miklos Szeredicb264512004-06-23 18:52:50 +0000495#endif
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000496}
497
Miklos Szerediab974562005-04-07 15:40:21 +0000498static size_t iov_length(const struct iovec *iov, size_t count)
Miklos Szerediede1f7a2005-04-01 21:08:57 +0000499{
Miklos Szerediab974562005-04-07 15:40:21 +0000500 size_t seg;
501 size_t ret = 0;
Miklos Szerediede1f7a2005-04-01 21:08:57 +0000502
Miklos Szerediab974562005-04-07 15:40:21 +0000503 for (seg = 0; seg < count; seg++)
504 ret += iov[seg].iov_len;
505 return ret;
Miklos Szerediede1f7a2005-04-01 21:08:57 +0000506}
507
Miklos Szerediab974562005-04-07 15:40:21 +0000508static int send_reply_raw(struct fuse *f, const struct iovec iov[],
509 size_t count)
Miklos Szeredi43696432001-11-18 19:15:05 +0000510{
511 int res;
Miklos Szerediab974562005-04-07 15:40:21 +0000512 unsigned outsize = iov_length(iov, count);
513 struct fuse_out_header *out = (struct fuse_out_header *) iov[0].iov_base;
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000514 out->len = outsize;
Miklos Szeredi43696432001-11-18 19:15:05 +0000515
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000516 if ((f->flags & FUSE_DEBUG)) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000517 printf(" unique: %llu, error: %i (%s), outsize: %i\n",
518 out->unique, out->error, strerror(-out->error), outsize);
Miklos Szeredi43696432001-11-18 19:15:05 +0000519 fflush(stdout);
520 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000521
Miklos Szeredi4e71c9f2004-02-09 12:05:14 +0000522 /* This needs to be done before the reply, otherwise the scheduler
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000523 could play tricks with us, and only let the counter be
524 increased long after the operation is done */
525 fuse_inc_avail(f);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000526
Miklos Szerediab974562005-04-07 15:40:21 +0000527 res = writev(f->fd, iov, count);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000528 if (res == -1) {
Miklos Szeredi43696432001-11-18 19:15:05 +0000529 /* ENOENT means the operation was interrupted */
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000530 if (!f->exited && errno != ENOENT)
Miklos Szeredi96249982001-11-21 12:21:19 +0000531 perror("fuse: writing device");
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000532 return -errno;
Miklos Szeredi43696432001-11-18 19:15:05 +0000533 }
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000534 return 0;
Miklos Szeredi43696432001-11-18 19:15:05 +0000535}
536
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000537static int send_reply(struct fuse *f, struct fuse_in_header *in, int error,
538 void *arg, size_t argsize)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000539{
Miklos Szerediab974562005-04-07 15:40:21 +0000540 struct fuse_out_header out;
541 struct iovec iov[2];
542 size_t count;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000543
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000544 if (error <= -1000 || error > 0) {
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000545 fprintf(stderr, "fuse: bad error value: %i\n", error);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000546 error = -ERANGE;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000547 }
548
Miklos Szerediab974562005-04-07 15:40:21 +0000549 out.unique = in->unique;
550 out.error = error;
551 count = 1;
552 iov[0].iov_base = &out;
553 iov[0].iov_len = sizeof(struct fuse_out_header);
554 if (argsize && !error) {
555 count++;
556 iov[1].iov_base = arg;
557 iov[1].iov_len = argsize;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000558 }
Miklos Szerediab974562005-04-07 15:40:21 +0000559 return send_reply_raw(f, iov, count);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000560}
561
Miklos Szeredia13d9002004-11-02 17:32:03 +0000562static int is_open(struct fuse *f, nodeid_t dir, const char *name)
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000563{
564 struct node *node;
565 int isopen = 0;
566 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000567 node = lookup_node(f, dir, name);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000568 if (node && node->open_count > 0)
569 isopen = 1;
570 pthread_mutex_unlock(&f->lock);
571 return isopen;
572}
573
Miklos Szeredia13d9002004-11-02 17:32:03 +0000574static char *hidden_name(struct fuse *f, nodeid_t dir, const char *oldname,
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000575 char *newname, size_t bufsize)
576{
577 struct stat buf;
578 struct node *node;
579 struct node *newnode;
580 char *newpath;
581 int res;
582 int failctr = 10;
583
584 if (!f->op.getattr)
585 return NULL;
586
587 do {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000588 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000589 node = lookup_node(f, dir, oldname);
590 if (node == NULL) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000591 pthread_mutex_unlock(&f->lock);
592 return NULL;
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000593 }
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000594 do {
595 f->hidectr ++;
596 snprintf(newname, bufsize, ".fuse_hidden%08x%08x",
Miklos Szeredia13d9002004-11-02 17:32:03 +0000597 (unsigned int) node->nodeid, f->hidectr);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000598 newnode = lookup_node(f, dir, newname);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000599 } while(newnode);
600 pthread_mutex_unlock(&f->lock);
Miklos Szeredie5183742005-02-02 11:14:04 +0000601
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000602 newpath = get_path_name(f, dir, newname);
603 if (!newpath)
604 break;
Miklos Szeredie5183742005-02-02 11:14:04 +0000605
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000606 res = f->op.getattr(newpath, &buf);
607 if (res != 0)
608 break;
609 free(newpath);
610 newpath = NULL;
611 } while(--failctr);
612
613 return newpath;
614}
615
Miklos Szeredia13d9002004-11-02 17:32:03 +0000616static int hide_node(struct fuse *f, const char *oldpath, nodeid_t dir,
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000617 const char *oldname)
618{
619 char newname[64];
620 char *newpath;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000621 int err = -EBUSY;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000622
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000623 if (f->op.rename && f->op.unlink) {
624 newpath = hidden_name(f, dir, oldname, newname, sizeof(newname));
625 if (newpath) {
626 int res = f->op.rename(oldpath, newpath);
627 if (res == 0)
628 err = rename_node(f, dir, oldname, dir, newname, 1);
629 free(newpath);
630 }
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000631 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000632 return err;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000633}
634
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000635static int lookup_path(struct fuse *f, nodeid_t nodeid, uint64_t version,
636 char *name, const char *path,
637 struct fuse_entry_out *arg)
Miklos Szeredi76f65782004-02-19 16:55:40 +0000638{
639 int res;
640 struct stat buf;
641
642 res = f->op.getattr(path, &buf);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000643 if (res == 0) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000644 struct node *node;
645
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000646 memset(arg, 0, sizeof(struct fuse_entry_out));
Miklos Szeredi76f65782004-02-19 16:55:40 +0000647 convert_stat(&buf, &arg->attr);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000648 node = find_node(f, nodeid, name, &arg->attr, version);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000649 if (node == NULL)
650 res = -ENOMEM;
651 else {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000652 arg->nodeid = node->nodeid;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000653 arg->generation = node->generation;
654 arg->entry_valid = ENTRY_REVALIDATE_TIME;
655 arg->entry_valid_nsec = 0;
656 arg->attr_valid = ATTR_REVALIDATE_TIME;
657 arg->attr_valid_nsec = 0;
658 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000659 printf(" NODEID: %lu\n", (unsigned long) arg->nodeid);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000660 fflush(stdout);
661 }
Miklos Szeredi76f65782004-02-19 16:55:40 +0000662 }
663 }
664 return res;
665}
666
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000667static void do_lookup(struct fuse *f, struct fuse_in_header *in, char *name)
668{
669 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000670 int res2;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000671 char *path;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000672 struct fuse_entry_out arg;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000673
Miklos Szeredi5e183482001-10-31 14:52:35 +0000674 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000675 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000676 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000677 if (path != NULL) {
678 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi6ebe2342002-01-06 21:44:16 +0000679 printf("LOOKUP %s\n", path);
680 fflush(stdout);
681 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000682 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000683 if (f->op.getattr)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000684 res = lookup_path(f, in->nodeid, in->unique, name, path, &arg);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000685 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000686 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000687 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000688 res2 = send_reply(f, in, res, &arg, sizeof(arg));
689 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi38009022005-05-08 19:47:22 +0000690 cancel_lookup(f, arg.nodeid, in->unique);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000691}
692
Miklos Szeredia181e612001-11-06 12:03:23 +0000693static void do_forget(struct fuse *f, struct fuse_in_header *in,
694 struct fuse_forget_in *arg)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000695{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000696 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000697 printf("FORGET %lu/%llu\n", (unsigned long) in->nodeid,
Miklos Szeredi38009022005-05-08 19:47:22 +0000698 arg->nlookup);
Miklos Szeredi43696432001-11-18 19:15:05 +0000699 fflush(stdout);
700 }
Miklos Szeredi38009022005-05-08 19:47:22 +0000701 if (f->major <= 6)
702 forget_node_old(f, in->nodeid, arg->nlookup);
703 else
704 forget_node(f, in->nodeid, arg->nlookup);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000705}
706
707static void do_getattr(struct fuse *f, struct fuse_in_header *in)
708{
709 int res;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000710 char *path;
711 struct stat buf;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000712 struct fuse_attr_out arg;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000713
Miklos Szeredi5e183482001-10-31 14:52:35 +0000714 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000715 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000716 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000717 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +0000718 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000719 if (f->op.getattr)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000720 res = f->op.getattr(path, &buf);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000721 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000722 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000723 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000724
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000725 if (res == 0) {
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000726 memset(&arg, 0, sizeof(struct fuse_attr_out));
727 arg.attr_valid = ATTR_REVALIDATE_TIME;
728 arg.attr_valid_nsec = 0;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000729 convert_stat(&buf, &arg.attr);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000730 if (!(f->flags & FUSE_USE_INO))
731 arg.attr.ino = in->nodeid;
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000732 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000733
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000734 send_reply(f, in, res, &arg, sizeof(arg));
735}
736
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000737static int do_chmod(struct fuse *f, const char *path, struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000738{
739 int res;
740
741 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000742 if (f->op.chmod)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000743 res = f->op.chmod(path, attr->mode);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000744
745 return res;
Miklos Szeredie5183742005-02-02 11:14:04 +0000746}
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000747
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000748static int do_chown(struct fuse *f, const char *path, struct fuse_attr *attr,
Miklos Szeredi2e50d432001-12-20 12:17:25 +0000749 int valid)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000750{
751 int res;
752 uid_t uid = (valid & FATTR_UID) ? attr->uid : (uid_t) -1;
753 gid_t gid = (valid & FATTR_GID) ? attr->gid : (gid_t) -1;
Miklos Szeredie5183742005-02-02 11:14:04 +0000754
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000755 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000756 if (f->op.chown)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000757 res = f->op.chown(path, uid, gid);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000758
759 return res;
760}
761
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000762static int do_truncate(struct fuse *f, const char *path,
763 struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000764{
765 int res;
766
767 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000768 if (f->op.truncate)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000769 res = f->op.truncate(path, attr->size);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000770
771 return res;
772}
773
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000774static int do_utime(struct fuse *f, const char *path, struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000775{
776 int res;
777 struct utimbuf buf;
778 buf.actime = attr->atime;
779 buf.modtime = attr->mtime;
780 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000781 if (f->op.utime)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000782 res = f->op.utime(path, &buf);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000783
784 return res;
785}
786
Miklos Szeredi5e183482001-10-31 14:52:35 +0000787static void do_setattr(struct fuse *f, struct fuse_in_header *in,
788 struct fuse_setattr_in *arg)
789{
790 int res;
791 char *path;
792 int valid = arg->valid;
Miklos Szeredi30e093a2005-04-03 17:44:54 +0000793 struct fuse_attr *attr = MEMBER_COMPAT(f, arg, attr, fuse_setattr_in);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000794 struct fuse_attr_out outarg;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000795
796 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000797 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000798 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000799 if (path != NULL) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000800 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000801 if (f->op.getattr) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000802 res = 0;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000803 if (!res && (valid & FATTR_MODE))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000804 res = do_chmod(f, path, attr);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000805 if (!res && (valid & (FATTR_UID | FATTR_GID)))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000806 res = do_chown(f, path, attr, valid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000807 if (!res && (valid & FATTR_SIZE))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000808 res = do_truncate(f, path, attr);
Miklos Szeredie5183742005-02-02 11:14:04 +0000809 if (!res && (valid & (FATTR_ATIME | FATTR_MTIME)) ==
Miklos Szeredib5958612004-02-20 14:10:49 +0000810 (FATTR_ATIME | FATTR_MTIME))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000811 res = do_utime(f, path, attr);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000812 if (!res) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000813 struct stat buf;
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000814 res = f->op.getattr(path, &buf);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000815 if (!res) {
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000816 memset(&outarg, 0, sizeof(struct fuse_attr_out));
817 outarg.attr_valid = ATTR_REVALIDATE_TIME;
818 outarg.attr_valid_nsec = 0;
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000819 convert_stat(&buf, &outarg.attr);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000820 if (!(f->flags & FUSE_USE_INO))
821 outarg.attr.ino = in->nodeid;
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000822 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000823 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000824 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000825 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000826 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000827 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredia181e612001-11-06 12:03:23 +0000828 send_reply(f, in, res, &outarg, sizeof(outarg));
Miklos Szeredi5e183482001-10-31 14:52:35 +0000829}
830
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000831static void do_readlink(struct fuse *f, struct fuse_in_header *in)
832{
833 int res;
834 char link[PATH_MAX + 1];
Miklos Szeredib483c932001-10-29 14:57:57 +0000835 char *path;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000836
Miklos Szeredi5e183482001-10-31 14:52:35 +0000837 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000838 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000839 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000840 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +0000841 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000842 if (f->op.readlink)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000843 res = f->op.readlink(path, link, sizeof(link));
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000844 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000845 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000846 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000847 link[PATH_MAX] = '\0';
Miklos Szeredi4b2bef42002-01-09 12:23:27 +0000848 send_reply(f, in, res, link, res == 0 ? strlen(link) : 0);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000849}
850
Miklos Szeredib483c932001-10-29 14:57:57 +0000851static void do_mknod(struct fuse *f, struct fuse_in_header *in,
852 struct fuse_mknod_in *inarg)
853{
854 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000855 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000856 char *path;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000857 char *name = PARAM(inarg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000858 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000859
Miklos Szeredi5e183482001-10-31 14:52:35 +0000860 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000861 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000862 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000863 if (path != NULL) {
864 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000865 printf("MKNOD %s\n", path);
866 fflush(stdout);
867 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000868 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000869 if (f->op.mknod && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000870 res = f->op.mknod(path, inarg->mode, inarg->rdev);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000871 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000872 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000873 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000874 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000875 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000876 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000877 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
878 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi38009022005-05-08 19:47:22 +0000879 cancel_lookup(f, outarg.nodeid, in->unique);
Miklos Szeredib483c932001-10-29 14:57:57 +0000880}
881
882static void do_mkdir(struct fuse *f, struct fuse_in_header *in,
883 struct fuse_mkdir_in *inarg)
884{
885 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000886 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000887 char *path;
Miklos Szeredi30e093a2005-04-03 17:44:54 +0000888 char *name = PARAM_COMPAT(f, inarg, fuse_mkdir_in);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000889 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000890
Miklos Szeredi5e183482001-10-31 14:52:35 +0000891 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000892 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000893 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000894 if (path != NULL) {
895 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000896 printf("MKDIR %s\n", path);
897 fflush(stdout);
898 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000899 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000900 if (f->op.mkdir && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000901 res = f->op.mkdir(path, inarg->mode);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000902 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000903 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000904 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000905 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000906 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000907 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000908 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
909 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi38009022005-05-08 19:47:22 +0000910 cancel_lookup(f, outarg.nodeid, in->unique);
Miklos Szeredib483c932001-10-29 14:57:57 +0000911}
912
Miklos Szeredib5958612004-02-20 14:10:49 +0000913static void do_unlink(struct fuse *f, struct fuse_in_header *in, char *name)
Miklos Szeredib483c932001-10-29 14:57:57 +0000914{
915 int res;
916 char *path;
917
Miklos Szeredi5e183482001-10-31 14:52:35 +0000918 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000919 pthread_rwlock_wrlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000920 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000921 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000922 if (f->flags & FUSE_DEBUG) {
923 printf("UNLINK %s\n", path);
924 fflush(stdout);
925 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000926 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000927 if (f->op.unlink) {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000928 if (!(f->flags & FUSE_HARD_REMOVE) && is_open(f, in->nodeid, name))
929 res = hide_node(f, path, in->nodeid, name);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000930 else {
931 res = f->op.unlink(path);
932 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000933 remove_node(f, in->nodeid, name);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000934
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000935 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000936 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000937 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000938 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000939 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredib5958612004-02-20 14:10:49 +0000940 send_reply(f, in, res, NULL, 0);
941}
942
943static void do_rmdir(struct fuse *f, struct fuse_in_header *in, char *name)
944{
945 int res;
946 char *path;
947
948 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000949 pthread_rwlock_wrlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000950 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000951 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000952 if (f->flags & FUSE_DEBUG) {
953 printf("RMDIR %s\n", path);
954 fflush(stdout);
955 }
Miklos Szeredib5958612004-02-20 14:10:49 +0000956 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000957 if (f->op.rmdir) {
Miklos Szeredib5958612004-02-20 14:10:49 +0000958 res = f->op.rmdir(path);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000959 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000960 remove_node(f, in->nodeid, name);
Miklos Szeredib5958612004-02-20 14:10:49 +0000961 }
962 free(path);
963 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000964 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredib483c932001-10-29 14:57:57 +0000965 send_reply(f, in, res, NULL, 0);
966}
967
968static void do_symlink(struct fuse *f, struct fuse_in_header *in, char *name,
969 char *link)
970{
971 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000972 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000973 char *path;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000974 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000975
Miklos Szeredi5e183482001-10-31 14:52:35 +0000976 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000977 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000978 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000979 if (path != NULL) {
980 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000981 printf("SYMLINK %s\n", path);
982 fflush(stdout);
983 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000984 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000985 if (f->op.symlink && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000986 res = f->op.symlink(link, path);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000987 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000988 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000989 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000990 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000991 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000992 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000993 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
994 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi38009022005-05-08 19:47:22 +0000995 cancel_lookup(f, outarg.nodeid, in->unique);
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000996
Miklos Szeredib483c932001-10-29 14:57:57 +0000997}
998
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000999static void do_rename(struct fuse *f, struct fuse_in_header *in,
1000 struct fuse_rename_in *inarg)
1001{
1002 int res;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001003 nodeid_t olddir = in->nodeid;
1004 nodeid_t newdir = inarg->newdir;
Miklos Szeredi6bf8b682002-10-28 08:49:39 +00001005 char *oldname = PARAM(inarg);
1006 char *newname = oldname + strlen(oldname) + 1;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001007 char *oldpath;
1008 char *newpath;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001009
Miklos Szeredi5e183482001-10-31 14:52:35 +00001010 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001011 pthread_rwlock_wrlock(&f->tree_lock);
Miklos Szeredia181e612001-11-06 12:03:23 +00001012 oldpath = get_path_name(f, olddir, oldname);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001013 if (oldpath != NULL) {
Miklos Szeredia181e612001-11-06 12:03:23 +00001014 newpath = get_path_name(f, newdir, newname);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001015 if (newpath != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001016 if (f->flags & FUSE_DEBUG) {
1017 printf("RENAME %s -> %s\n", oldpath, newpath);
1018 fflush(stdout);
1019 }
Miklos Szeredi5e183482001-10-31 14:52:35 +00001020 res = -ENOSYS;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001021 if (f->op.rename) {
1022 res = 0;
Miklos Szeredie5183742005-02-02 11:14:04 +00001023 if (!(f->flags & FUSE_HARD_REMOVE) &&
Miklos Szeredi2529ca22004-07-13 15:36:52 +00001024 is_open(f, newdir, newname))
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001025 res = hide_node(f, newpath, newdir, newname);
1026 if (res == 0) {
1027 res = f->op.rename(oldpath, newpath);
1028 if (res == 0)
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001029 res = rename_node(f, olddir, oldname, newdir, newname, 0);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001030 }
1031 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001032 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001033 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001034 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001035 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001036 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredie5183742005-02-02 11:14:04 +00001037 send_reply(f, in, res, NULL, 0);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001038}
1039
1040static void do_link(struct fuse *f, struct fuse_in_header *in,
Miklos Szeredi5e183482001-10-31 14:52:35 +00001041 struct fuse_link_in *arg)
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001042{
1043 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +00001044 int res2;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001045 char *oldpath;
1046 char *newpath;
Miklos Szeredi76f65782004-02-19 16:55:40 +00001047 char *name = PARAM(arg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +00001048 struct fuse_entry_out outarg;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001049
Miklos Szeredi5e183482001-10-31 14:52:35 +00001050 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001051 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredied6b5dd2005-01-26 17:07:59 +00001052 oldpath = get_path(f, arg->oldnodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001053 if (oldpath != NULL) {
Miklos Szeredied6b5dd2005-01-26 17:07:59 +00001054 newpath = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001055 if (newpath != NULL) {
1056 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +00001057 printf("LINK %s\n", newpath);
1058 fflush(stdout);
1059 }
Miklos Szeredi5e183482001-10-31 14:52:35 +00001060 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001061 if (f->op.link && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +00001062 res = f->op.link(oldpath, newpath);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001063 if (res == 0)
Miklos Szeredied6b5dd2005-01-26 17:07:59 +00001064 res = lookup_path(f, in->nodeid, in->unique, name,
Miklos Szeredi76f65782004-02-19 16:55:40 +00001065 newpath, &outarg);
1066 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001067 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001068 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001069 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001070 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001071 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi2778f6c2004-06-21 09:45:30 +00001072 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
1073 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi38009022005-05-08 19:47:22 +00001074 cancel_lookup(f, outarg.nodeid, in->unique);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001075}
1076
Miklos Szeredi5e183482001-10-31 14:52:35 +00001077static void do_open(struct fuse *f, struct fuse_in_header *in,
1078 struct fuse_open_in *arg)
1079{
1080 int res;
1081 char *path;
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001082 struct fuse_open_out outarg;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001083 struct fuse_file_info fi;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001084
Miklos Szeredied3c97c2005-02-15 17:04:50 +00001085 memset(&outarg, 0, sizeof(outarg));
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001086 memset(&fi, 0, sizeof(fi));
1087 fi.flags = arg->flags;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001088 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001089 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001090 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001091 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +00001092 res = -ENOSYS;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001093 if (f->op.open) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001094 if (!f->compat)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001095 res = f->op.open(path, &fi);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001096 else
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001097 res = ((struct fuse_operations_compat2 *) &f->op)->open(path, fi.flags);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001098 }
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +00001099 }
Miklos Szeredi73798f92004-07-12 15:55:11 +00001100 if (res == 0) {
1101 int res2;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001102 outarg.fh = fi.fh;
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001103 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001104 printf("OPEN[%lu] flags: 0x%x\n", fi.fh, arg->flags);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001105 fflush(stdout);
1106 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001107
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001108 pthread_mutex_lock(&f->lock);
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001109 res2 = send_reply(f, in, res, &outarg, SIZEOF_COMPAT(f, fuse_open_out));
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001110 if(res2 == -ENOENT) {
1111 /* The open syscall was interrupted, so it must be cancelled */
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001112 if(f->op.release) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001113 if (!f->compat)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001114 f->op.release(path, &fi);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001115 else
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001116 ((struct fuse_operations_compat2 *) &f->op)->release(path, fi.flags);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001117 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001118 } else {
1119 struct node *node = get_node(f, in->nodeid);
1120 node->open_count ++;
1121 }
Miklos Szeredi73798f92004-07-12 15:55:11 +00001122 pthread_mutex_unlock(&f->lock);
Miklos Szeredi73798f92004-07-12 15:55:11 +00001123 } else
1124 send_reply(f, in, res, NULL, 0);
1125
Miklos Szeredi9a31cca2004-06-26 21:11:25 +00001126 if (path)
1127 free(path);
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001128 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001129}
1130
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001131static void do_flush(struct fuse *f, struct fuse_in_header *in,
1132 struct fuse_flush_in *arg)
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001133{
1134 char *path;
1135 int res;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001136 struct fuse_file_info fi;
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001137
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001138 memset(&fi, 0, sizeof(fi));
1139 fi.fh = arg->fh;
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001140 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001141 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001142 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001143 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001144 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001145 printf("FLUSH[%lu]\n", (unsigned long) arg->fh);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001146 fflush(stdout);
1147 }
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001148 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001149 if (f->op.flush)
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001150 res = f->op.flush(path, &fi);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001151 free(path);
1152 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001153 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001154 send_reply(f, in, res, NULL, 0);
1155}
1156
Miklos Szeredi9478e862002-12-11 09:50:26 +00001157static void do_release(struct fuse *f, struct fuse_in_header *in,
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001158 struct fuse_release_in *arg)
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001159{
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001160 struct node *node;
1161 char *path;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001162 struct fuse_file_info fi;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001163 int unlink_hidden;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001164
1165 memset(&fi, 0, sizeof(fi));
1166 fi.flags = arg->flags;
1167 fi.fh = arg->fh;
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001168
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001169 pthread_mutex_lock(&f->lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001170 node = get_node(f, in->nodeid);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001171 assert(node->open_count > 0);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001172 --node->open_count;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001173 unlink_hidden = (node->is_hidden && !node->open_count);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001174 pthread_mutex_unlock(&f->lock);
1175
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001176 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001177 path = get_path(f, in->nodeid);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001178 if (f->flags & FUSE_DEBUG) {
1179 printf("RELEASE[%lu] flags: 0x%x\n", fi.fh, fi.flags);
1180 fflush(stdout);
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001181 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001182 if (f->op.release) {
1183 if (!f->compat)
1184 f->op.release(path ? path : "-", &fi);
1185 else if (path)
1186 ((struct fuse_operations_compat2 *) &f->op)->release(path, fi.flags);
1187 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001188
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001189 if(unlink_hidden && path)
1190 f->op.unlink(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001191
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001192 if (path)
1193 free(path);
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001194 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001195
Miklos Szeredi556d03d2004-06-30 11:13:41 +00001196 send_reply(f, in, 0, NULL, 0);
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001197}
1198
Miklos Szeredi5e183482001-10-31 14:52:35 +00001199static void do_read(struct fuse *f, struct fuse_in_header *in,
1200 struct fuse_read_in *arg)
1201{
1202 int res;
1203 char *path;
Miklos Szerediab974562005-04-07 15:40:21 +00001204 size_t size;
1205 char *buf;
1206 struct fuse_file_info fi;
1207
1208 buf = (char *) malloc(arg->size);
1209 if (buf == NULL) {
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001210 send_reply(f, in, -ENOMEM, NULL, 0);
Miklos Szerediab974562005-04-07 15:40:21 +00001211 return;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001212 }
Miklos Szerediab974562005-04-07 15:40:21 +00001213
1214 memset(&fi, 0, sizeof(fi));
1215 fi.fh = arg->fh;
1216
1217 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001218 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szerediab974562005-04-07 15:40:21 +00001219 path = get_path(f, in->nodeid);
1220 if (path != NULL) {
1221 if (f->flags & FUSE_DEBUG) {
1222 printf("READ[%lu] %u bytes from %llu\n",
1223 (unsigned long) arg->fh, arg->size, arg->offset);
1224 fflush(stdout);
1225 }
1226
1227 res = -ENOSYS;
1228 if (f->op.read)
1229 res = f->op.read(path, buf, arg->size, arg->offset, &fi);
1230 free(path);
1231 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001232 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szerediab974562005-04-07 15:40:21 +00001233
1234 size = 0;
1235 if (res >= 0) {
1236 size = res;
1237 res = 0;
1238 if (f->flags & FUSE_DEBUG) {
1239 printf(" READ[%lu] %u bytes\n", (unsigned long) arg->fh,
1240 size);
1241 fflush(stdout);
1242 }
1243 }
1244
1245 send_reply(f, in, res, buf, size);
1246 free(buf);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001247}
Miklos Szeredib483c932001-10-29 14:57:57 +00001248
Miklos Szeredia181e612001-11-06 12:03:23 +00001249static void do_write(struct fuse *f, struct fuse_in_header *in,
1250 struct fuse_write_in *arg)
1251{
1252 int res;
1253 char *path;
Miklos Szerediad051c32004-07-02 09:22:50 +00001254 struct fuse_write_out outarg;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001255 struct fuse_file_info fi;
1256
1257 memset(&fi, 0, sizeof(fi));
1258 fi.fh = arg->fh;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001259 fi.writepage = arg->write_flags & 1;
Miklos Szeredia181e612001-11-06 12:03:23 +00001260
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001261 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001262 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001263 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001264 if (path != NULL) {
1265 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi1eea0322004-09-27 18:50:11 +00001266 printf("WRITE%s[%lu] %u bytes to %llu\n",
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001267 (arg->write_flags & 1) ? "PAGE" : "",
1268 (unsigned long) arg->fh, arg->size, arg->offset);
Miklos Szeredi6ebe2342002-01-06 21:44:16 +00001269 fflush(stdout);
1270 }
1271
Miklos Szeredia181e612001-11-06 12:03:23 +00001272 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001273 if (f->op.write)
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001274 res = f->op.write(path, PARAM(arg), arg->size, arg->offset, &fi);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001275 free(path);
Miklos Szeredia181e612001-11-06 12:03:23 +00001276 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001277 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredie5183742005-02-02 11:14:04 +00001278
Miklos Szerediab974562005-04-07 15:40:21 +00001279 memset(&outarg, 0, sizeof(outarg));
Miklos Szeredie5183742005-02-02 11:14:04 +00001280 if (res >= 0) {
Miklos Szerediad051c32004-07-02 09:22:50 +00001281 outarg.size = res;
1282 res = 0;
Miklos Szeredia181e612001-11-06 12:03:23 +00001283 }
1284
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001285 send_reply(f, in, res, &outarg, SIZEOF_COMPAT(f, fuse_write_out));
Miklos Szeredia181e612001-11-06 12:03:23 +00001286}
1287
Miklos Szeredi77f39942004-03-25 11:17:52 +00001288static int default_statfs(struct statfs *buf)
1289{
1290 buf->f_namelen = 255;
1291 buf->f_bsize = 512;
1292 return 0;
1293}
1294
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001295static void convert_statfs_compat(struct fuse_statfs_compat1 *compatbuf,
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001296 struct statfs *statfs)
1297{
1298 statfs->f_bsize = compatbuf->block_size;
1299 statfs->f_blocks = compatbuf->blocks;
1300 statfs->f_bfree = compatbuf->blocks_free;
1301 statfs->f_bavail = compatbuf->blocks_free;
1302 statfs->f_files = compatbuf->files;
1303 statfs->f_ffree = compatbuf->files_free;
1304 statfs->f_namelen = compatbuf->namelen;
1305}
1306
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001307static void convert_statfs(struct statfs *statfs, struct fuse_kstatfs *kstatfs)
1308{
1309 kstatfs->bsize = statfs->f_bsize;
1310 kstatfs->blocks = statfs->f_blocks;
1311 kstatfs->bfree = statfs->f_bfree;
1312 kstatfs->bavail = statfs->f_bavail;
1313 kstatfs->files = statfs->f_files;
1314 kstatfs->ffree = statfs->f_ffree;
1315 kstatfs->namelen = statfs->f_namelen;
1316}
1317
Mark Glinesd84b39a2002-01-07 16:32:02 +00001318static void do_statfs(struct fuse *f, struct fuse_in_header *in)
1319{
1320 int res;
Mark Glinesd84b39a2002-01-07 16:32:02 +00001321 struct fuse_statfs_out arg;
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001322 struct statfs buf;
Mark Glinesd84b39a2002-01-07 16:32:02 +00001323
Miklos Szeredi77f39942004-03-25 11:17:52 +00001324 memset(&buf, 0, sizeof(struct statfs));
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001325 if (f->op.statfs) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001326 if (!f->compat || f->compat > 11)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001327 res = f->op.statfs("/", &buf);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001328 else {
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001329 struct fuse_statfs_compat1 compatbuf;
1330 memset(&compatbuf, 0, sizeof(struct fuse_statfs_compat1));
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001331 res = ((struct fuse_operations_compat1 *) &f->op)->statfs(&compatbuf);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001332 if (res == 0)
1333 convert_statfs_compat(&compatbuf, &buf);
1334 }
1335 }
Miklos Szeredi77f39942004-03-25 11:17:52 +00001336 else
1337 res = default_statfs(&buf);
1338
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001339 if (res == 0)
Miklos Szeredi77f39942004-03-25 11:17:52 +00001340 convert_statfs(&buf, &arg.st);
Miklos Szeredi4b2bef42002-01-09 12:23:27 +00001341
Mark Glinesd84b39a2002-01-07 16:32:02 +00001342 send_reply(f, in, res, &arg, sizeof(arg));
1343}
1344
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001345static void do_fsync(struct fuse *f, struct fuse_in_header *in,
1346 struct fuse_fsync_in *inarg)
1347{
1348 int res;
1349 char *path;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001350 struct fuse_file_info fi;
1351
1352 memset(&fi, 0, sizeof(fi));
1353 fi.fh = inarg->fh;
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001354
1355 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001356 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001357 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001358 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001359 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001360 printf("FSYNC[%lu]\n", (unsigned long) inarg->fh);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001361 fflush(stdout);
1362 }
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001363 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001364 if (f->op.fsync)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001365 res = f->op.fsync(path, inarg->fsync_flags & 1, &fi);
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001366 free(path);
1367 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001368 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001369 send_reply(f, in, res, NULL, 0);
1370}
1371
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001372static void do_setxattr(struct fuse *f, struct fuse_in_header *in,
1373 struct fuse_setxattr_in *arg)
1374{
1375 int res;
1376 char *path;
1377 char *name = PARAM(arg);
1378 unsigned char *value = name + strlen(name) + 1;
1379
1380 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001381 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001382 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001383 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001384 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001385 if (f->op.setxattr)
1386 res = f->op.setxattr(path, name, value, arg->size, arg->flags);
1387 free(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001388 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001389 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001390 send_reply(f, in, res, NULL, 0);
1391}
1392
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001393static int common_getxattr(struct fuse *f, struct fuse_in_header *in,
1394 const char *name, char *value, size_t size)
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001395{
1396 int res;
1397 char *path;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001398
1399 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001400 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001401 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001402 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001403 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001404 if (f->op.getxattr)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001405 res = f->op.getxattr(path, name, value, size);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001406 free(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001407 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001408 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001409 return res;
1410}
1411
1412static void do_getxattr_read(struct fuse *f, struct fuse_in_header *in,
1413 const char *name, size_t size)
1414{
1415 int res;
Miklos Szerediab974562005-04-07 15:40:21 +00001416 char *value = (char *) malloc(size);
1417 if (value == NULL) {
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001418 send_reply(f, in, -ENOMEM, NULL, 0);
Miklos Szerediab974562005-04-07 15:40:21 +00001419 return;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001420 }
Miklos Szerediab974562005-04-07 15:40:21 +00001421 res = common_getxattr(f, in, name, value, size);
1422 size = 0;
1423 if (res > 0) {
1424 size = res;
1425 res = 0;
1426 }
1427 send_reply(f, in, res, value, size);
1428 free(value);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001429}
1430
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001431static void do_getxattr_size(struct fuse *f, struct fuse_in_header *in,
1432 const char *name)
1433{
1434 int res;
1435 struct fuse_getxattr_out arg;
1436
Miklos Szerediab974562005-04-07 15:40:21 +00001437 memset(&arg, 0, sizeof(arg));
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001438 res = common_getxattr(f, in, name, NULL, 0);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001439 if (res >= 0) {
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001440 arg.size = res;
1441 res = 0;
1442 }
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001443 send_reply(f, in, res, &arg, SIZEOF_COMPAT(f, fuse_getxattr_out));
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001444}
1445
1446static void do_getxattr(struct fuse *f, struct fuse_in_header *in,
1447 struct fuse_getxattr_in *arg)
1448{
1449 char *name = PARAM(arg);
Miklos Szeredie5183742005-02-02 11:14:04 +00001450
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001451 if (arg->size)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001452 do_getxattr_read(f, in, name, arg->size);
1453 else
1454 do_getxattr_size(f, in, name);
1455}
1456
1457static int common_listxattr(struct fuse *f, struct fuse_in_header *in,
1458 char *list, size_t size)
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001459{
1460 int res;
1461 char *path;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001462
1463 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001464 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001465 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001466 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001467 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001468 if (f->op.listxattr)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001469 res = f->op.listxattr(path, list, size);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001470 free(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001471 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001472 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001473 return res;
1474}
1475
1476static void do_listxattr_read(struct fuse *f, struct fuse_in_header *in,
1477 size_t size)
1478{
1479 int res;
Miklos Szerediab974562005-04-07 15:40:21 +00001480 char *list = (char *) malloc(size);
1481 if (list == NULL) {
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001482 send_reply(f, in, -ENOMEM, NULL, 0);
Miklos Szerediab974562005-04-07 15:40:21 +00001483 return;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001484 }
Miklos Szerediab974562005-04-07 15:40:21 +00001485 res = common_listxattr(f, in, list, size);
1486 size = 0;
1487 if (res > 0) {
1488 size = res;
1489 res = 0;
1490 }
1491 send_reply(f, in, res, list, size);
1492 free(list);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001493}
1494
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001495static void do_listxattr_size(struct fuse *f, struct fuse_in_header *in)
1496{
1497 int res;
1498 struct fuse_getxattr_out arg;
1499
Miklos Szerediab974562005-04-07 15:40:21 +00001500 memset(&arg, 0, sizeof(arg));
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001501 res = common_listxattr(f, in, NULL, 0);
1502 if (res >= 0) {
1503 arg.size = res;
1504 res = 0;
1505 }
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001506 send_reply(f, in, res, &arg, SIZEOF_COMPAT(f, fuse_getxattr_out));
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001507}
1508
1509static void do_listxattr(struct fuse *f, struct fuse_in_header *in,
1510 struct fuse_getxattr_in *arg)
1511{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001512 if (arg->size)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001513 do_listxattr_read(f, in, arg->size);
1514 else
1515 do_listxattr_size(f, in);
1516}
1517
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001518static void do_removexattr(struct fuse *f, struct fuse_in_header *in,
1519 char *name)
1520{
1521 int res;
1522 char *path;
1523
1524 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001525 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001526 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001527 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001528 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001529 if (f->op.removexattr)
1530 res = f->op.removexattr(path, name);
1531 free(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001532 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001533 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001534 send_reply(f, in, res, NULL, 0);
1535}
1536
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001537static void do_init(struct fuse *f, struct fuse_in_header *in,
1538 struct fuse_init_in_out *arg)
1539{
1540 struct fuse_init_in_out outarg;
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001541
1542 if (in->padding == 5) {
1543 arg->minor = arg->major;
1544 arg->major = in->padding;
1545 }
1546
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001547 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001548 printf("INIT: %u.%u\n", arg->major, arg->minor);
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001549 fflush(stdout);
1550 }
1551 f->got_init = 1;
Miklos Szeredi159bd7e2005-02-28 17:32:16 +00001552 if (f->op.init)
1553 f->user_data = f->op.init();
1554
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001555 if (arg->major == 5) {
1556 f->major = 5;
1557 f->minor = 1;
Miklos Szeredi38009022005-05-08 19:47:22 +00001558 } else if (arg->major == 6) {
1559 f->major = 6;
1560 f->minor = 1;
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001561 } else {
1562 f->major = FUSE_KERNEL_VERSION;
1563 f->minor = FUSE_KERNEL_MINOR_VERSION;
1564 }
1565 memset(&outarg, 0, sizeof(outarg));
1566 outarg.major = f->major;
1567 outarg.minor = f->minor;
1568
1569 if (f->flags & FUSE_DEBUG) {
1570 printf(" INIT: %u.%u\n", outarg.major, outarg.minor);
1571 fflush(stdout);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001572 }
1573
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001574 send_reply(f, in, 0, &outarg, sizeof(outarg));
1575}
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001576
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001577static struct fuse_dirhandle *get_dirhandle(unsigned long fh)
1578{
1579 return (struct fuse_dirhandle *) fh;
1580}
1581
1582static void do_opendir(struct fuse *f, struct fuse_in_header *in,
1583 struct fuse_open_in *arg)
1584{
1585 int res;
1586 struct fuse_open_out outarg;
1587 struct fuse_dirhandle *dh;
1588
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001589 dh = (struct fuse_dirhandle *) malloc(sizeof(struct fuse_dirhandle));
1590 if (dh == NULL) {
1591 send_reply(f, in, -ENOMEM, NULL, 0);
1592 return;
1593 }
1594 memset(dh, 0, sizeof(struct fuse_dirhandle));
1595 dh->fuse = f;
1596 dh->contents = NULL;
1597 dh->len = 0;
1598 dh->filled = 0;
Miklos Szeredi33be22d2005-05-27 09:12:43 +00001599 if (f->flags & FUSE_READDIR_INO) {
1600 pthread_mutex_lock(&f->lock);
1601 dh->node = get_node(f, in->nodeid);
1602 pthread_mutex_unlock(&f->lock);
1603 }
Miklos Szerediab974562005-04-07 15:40:21 +00001604 mutex_init(&dh->lock);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001605
1606 memset(&outarg, 0, sizeof(outarg));
1607 outarg.fh = (unsigned long) dh;
1608
Miklos Szeredif43f0632005-02-28 11:46:56 +00001609 if (f->op.opendir) {
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001610 struct fuse_file_info fi;
Miklos Szeredif43f0632005-02-28 11:46:56 +00001611 char *path;
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001612
1613 memset(&fi, 0, sizeof(fi));
1614 fi.flags = arg->flags;
1615
Miklos Szeredif43f0632005-02-28 11:46:56 +00001616 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001617 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredif43f0632005-02-28 11:46:56 +00001618 path = get_path(f, in->nodeid);
1619 if (path != NULL) {
Miklos Szeredif43f0632005-02-28 11:46:56 +00001620 res = f->op.opendir(path, &fi);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001621 dh->fh = fi.fh;
Miklos Szeredif43f0632005-02-28 11:46:56 +00001622 }
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001623 if (res == 0) {
1624 int res2;
1625 pthread_mutex_lock(&f->lock);
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001626 res2 = send_reply(f, in, res, &outarg, SIZEOF_COMPAT(f, fuse_open_out));
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001627 if(res2 == -ENOENT) {
1628 /* The opendir syscall was interrupted, so it must be
1629 cancelled */
1630 if(f->op.releasedir)
1631 f->op.releasedir(path, &fi);
Miklos Szerediab974562005-04-07 15:40:21 +00001632 pthread_mutex_destroy(&dh->lock);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001633 free(dh);
1634 }
1635 pthread_mutex_unlock(&f->lock);
1636 } else {
Miklos Szeredif43f0632005-02-28 11:46:56 +00001637 send_reply(f, in, res, NULL, 0);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001638 free(dh);
Miklos Szeredif43f0632005-02-28 11:46:56 +00001639 }
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001640 free(path);
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001641 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szerediab974562005-04-07 15:40:21 +00001642 } else
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001643 send_reply(f, in, 0, &outarg, SIZEOF_COMPAT(f, fuse_open_out));
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001644}
1645
Miklos Szerediab974562005-04-07 15:40:21 +00001646static int fill_dir_common(struct fuse_dirhandle *dh, const char *name,
1647 int type, ino_t ino, off_t off)
1648{
1649 unsigned namelen = strlen(name);
1650 unsigned entlen;
1651 unsigned entsize;
1652 unsigned padlen;
1653 unsigned newlen;
1654 unsigned char *newptr;
1655
Miklos Szeredi33be22d2005-05-27 09:12:43 +00001656 if (!(dh->fuse->flags & FUSE_USE_INO)) {
Miklos Szerediab974562005-04-07 15:40:21 +00001657 ino = (ino_t) -1;
Miklos Szeredi33be22d2005-05-27 09:12:43 +00001658 if (dh->fuse->flags & FUSE_READDIR_INO) {
1659 struct node *node;
1660 pthread_mutex_lock(&dh->fuse->lock);
1661 node = lookup_node(dh->fuse, dh->node->nodeid, name);
1662 if (node)
1663 ino = (ino_t) node->nodeid;
1664 pthread_mutex_unlock(&dh->fuse->lock);
1665 }
1666 }
Miklos Szerediab974562005-04-07 15:40:21 +00001667
1668 if (namelen > FUSE_NAME_MAX)
1669 namelen = FUSE_NAME_MAX;
1670 else if (!namelen) {
1671 dh->error = -EIO;
1672 return 1;
1673 }
1674
1675 entlen = (dh->fuse->major == 5 ?
1676 FUSE_NAME_OFFSET_COMPAT5 : FUSE_NAME_OFFSET) + namelen;
1677 entsize = FUSE_DIRENT_ALIGN(entlen);
1678 padlen = entsize - entlen;
1679 newlen = dh->len + entsize;
1680 if (off && dh->fuse->major != 5) {
1681 dh->filled = 0;
1682 if (newlen > dh->needlen)
1683 return 1;
1684 }
Miklos Szeredi21019c92005-05-09 11:22:41 +00001685
Miklos Szerediab974562005-04-07 15:40:21 +00001686 newptr = realloc(dh->contents, newlen);
1687 if (!newptr) {
1688 dh->error = -ENOMEM;
1689 return 1;
1690 }
1691 dh->contents = newptr;
1692 if (dh->fuse->major == 5) {
1693 struct fuse_dirent_compat5 *dirent;
1694 dirent = (struct fuse_dirent_compat5 *) (dh->contents + dh->len);
1695 dirent->ino = ino;
1696 dirent->namelen = namelen;
1697 dirent->type = type;
1698 strncpy(dirent->name, name, namelen);
1699 } else {
1700 struct fuse_dirent *dirent;
1701 dirent = (struct fuse_dirent *) (dh->contents + dh->len);
1702 dirent->ino = ino;
1703 dirent->off = off ? off : newlen;
1704 dirent->namelen = namelen;
1705 dirent->type = type;
1706 strncpy(dirent->name, name, namelen);
1707 }
1708 if (padlen)
1709 memset(dh->contents + dh->len + entlen, 0, padlen);
1710 dh->len = newlen;
1711 return 0;
1712}
1713
1714static int fill_dir(void *buf, const char *name, const struct stat *stat,
1715 off_t off)
1716{
1717 int type = stat ? (stat->st_mode & 0170000) >> 12 : 0;
1718 ino_t ino = stat ? stat->st_ino : (ino_t) -1;
1719 return fill_dir_common(buf, name, type, ino, off);
1720}
1721
1722static int fill_dir_old(struct fuse_dirhandle *dh, const char *name, int type,
1723 ino_t ino)
1724{
1725 fill_dir_common(dh, name, type, ino, 0);
1726 return dh->error;
1727}
1728
1729static int readdir_fill(struct fuse *f, struct fuse_in_header *in,
1730 struct fuse_read_in *arg, struct fuse_dirhandle *dh)
1731{
1732 int err = -ENOENT;
Miklos Szeredi129ef8f2005-06-20 13:48:42 +00001733 char *path;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001734 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredi129ef8f2005-06-20 13:48:42 +00001735 path = get_path(f, in->nodeid);
Miklos Szerediab974562005-04-07 15:40:21 +00001736 if (path != NULL) {
1737 struct fuse_file_info fi;
1738
1739 memset(&fi, 0, sizeof(fi));
1740 fi.fh = dh->fh;
1741
1742 dh->len = 0;
1743 dh->error = 0;
1744 dh->needlen = arg->size;
1745 dh->filled = 1;
1746 err = -ENOSYS;
1747 if (f->op.readdir) {
1748 off_t offset = f->major == 5 ? 0 : arg->offset;
1749 err = f->op.readdir(path, dh, fill_dir, offset, &fi);
1750 } else if (f->op.getdir)
1751 err = f->op.getdir(path, dh, fill_dir_old);
1752 if (!err)
1753 err = dh->error;
1754 if (err)
1755 dh->filled = 0;
1756 free(path);
1757 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001758 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szerediab974562005-04-07 15:40:21 +00001759 return err;
1760}
1761
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001762static void do_readdir(struct fuse *f, struct fuse_in_header *in,
1763 struct fuse_read_in *arg)
1764{
Miklos Szerediab974562005-04-07 15:40:21 +00001765 int err = 0;
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001766 struct fuse_dirhandle *dh = get_dirhandle(arg->fh);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001767 size_t size = 0;
Miklos Szerediab974562005-04-07 15:40:21 +00001768 unsigned char *buf = NULL;
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001769
Miklos Szerediab974562005-04-07 15:40:21 +00001770 pthread_mutex_lock(&dh->lock);
1771 if (!dh->filled) {
1772 err = readdir_fill(f, in, arg, dh);
1773 if (err)
1774 goto out;
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001775 }
Miklos Szerediab974562005-04-07 15:40:21 +00001776 if (dh->filled) {
Miklos Szeredib92d9782005-02-07 16:10:49 +00001777 if (arg->offset < dh->len) {
1778 size = arg->size;
1779 if (arg->offset + size > dh->len)
1780 size = dh->len - arg->offset;
Miklos Szerediab974562005-04-07 15:40:21 +00001781 buf = dh->contents + arg->offset;
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001782 }
Miklos Szerediab974562005-04-07 15:40:21 +00001783 } else {
1784 size = dh->len;
1785 buf = dh->contents;
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001786 }
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001787
Miklos Szerediab974562005-04-07 15:40:21 +00001788 out:
1789 send_reply(f, in, err, buf, size);
1790 pthread_mutex_unlock(&dh->lock);
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001791}
1792
1793static void do_releasedir(struct fuse *f, struct fuse_in_header *in,
1794 struct fuse_release_in *arg)
1795{
1796 struct fuse_dirhandle *dh = get_dirhandle(arg->fh);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001797 if (f->op.releasedir) {
1798 char *path;
1799 struct fuse_file_info fi;
Miklos Szerediab974562005-04-07 15:40:21 +00001800
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001801 memset(&fi, 0, sizeof(fi));
1802 fi.fh = dh->fh;
1803
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001804 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001805 path = get_path(f, in->nodeid);
1806 f->op.releasedir(path ? path : "-", &fi);
1807 free(path);
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001808 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001809 }
Miklos Szerediab974562005-04-07 15:40:21 +00001810 pthread_mutex_lock(&dh->lock);
1811 pthread_mutex_unlock(&dh->lock);
1812 pthread_mutex_destroy(&dh->lock);
Miklos Szeredib92d9782005-02-07 16:10:49 +00001813 free(dh->contents);
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001814 free(dh);
1815 send_reply(f, in, 0, NULL, 0);
1816}
1817
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001818static void do_fsyncdir(struct fuse *f, struct fuse_in_header *in,
1819 struct fuse_fsync_in *inarg)
1820{
1821 int res;
1822 char *path;
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001823 struct fuse_dirhandle *dh = get_dirhandle(inarg->fh);
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001824 struct fuse_file_info fi;
1825
1826 memset(&fi, 0, sizeof(fi));
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001827 fi.fh = dh->fh;
1828
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001829 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001830 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001831 path = get_path(f, in->nodeid);
1832 if (path != NULL) {
1833 res = -ENOSYS;
1834 if (f->op.fsyncdir)
1835 res = f->op.fsyncdir(path, inarg->fsync_flags & 1, &fi);
1836 free(path);
1837 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001838 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001839 send_reply(f, in, res, NULL, 0);
1840}
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001841
Miklos Szeredi43696432001-11-18 19:15:05 +00001842static void free_cmd(struct fuse_cmd *cmd)
1843{
1844 free(cmd->buf);
1845 free(cmd);
1846}
1847
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001848void fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd)
Miklos Szeredia181e612001-11-06 12:03:23 +00001849{
Miklos Szeredia181e612001-11-06 12:03:23 +00001850 struct fuse_in_header *in = (struct fuse_in_header *) cmd->buf;
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001851 void *inarg = cmd->buf + SIZEOF_COMPAT(f, fuse_in_header);
Miklos Szeredid169f312004-09-22 08:48:26 +00001852 struct fuse_context *ctx = fuse_get_context();
Miklos Szeredia181e612001-11-06 12:03:23 +00001853
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001854 fuse_dec_avail(f);
Miklos Szeredi33232032001-11-19 17:55:51 +00001855
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001856 if ((f->flags & FUSE_DEBUG)) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001857 printf("unique: %llu, opcode: %s (%i), nodeid: %lu, insize: %i\n",
1858 in->unique, opname(in->opcode), in->opcode,
1859 (unsigned long) in->nodeid, cmd->buflen);
Miklos Szeredic0938ea2001-11-07 12:35:06 +00001860 fflush(stdout);
1861 }
Miklos Szeredife25def2001-12-20 15:38:05 +00001862
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001863 if (!f->got_init && in->opcode != FUSE_INIT) {
1864 /* Old kernel version probably */
1865 send_reply(f, in, -EPROTO, NULL, 0);
1866 goto out;
1867 }
1868
Miklos Szeredi0111f9d2005-04-22 12:04:55 +00001869 if ((f->flags & FUSE_ALLOW_ROOT) && in->uid != f->owner && in->uid != 0 &&
1870 in->opcode != FUSE_INIT && in->opcode != FUSE_READ &&
Miklos Szeredi21019c92005-05-09 11:22:41 +00001871 in->opcode != FUSE_WRITE && in->opcode != FUSE_FSYNC &&
Miklos Szeredi0111f9d2005-04-22 12:04:55 +00001872 in->opcode != FUSE_RELEASE && in->opcode != FUSE_READDIR &&
1873 in->opcode != FUSE_FSYNCDIR && in->opcode != FUSE_RELEASEDIR) {
1874 send_reply(f, in, -EACCES, NULL, 0);
1875 goto out;
1876 }
1877
Miklos Szeredid169f312004-09-22 08:48:26 +00001878 ctx->fuse = f;
Miklos Szeredife25def2001-12-20 15:38:05 +00001879 ctx->uid = in->uid;
1880 ctx->gid = in->gid;
Miklos Szeredi1f18db52004-09-27 06:54:49 +00001881 ctx->pid = in->pid;
Miklos Szeredi159bd7e2005-02-28 17:32:16 +00001882 ctx->private_data = f->user_data;
Miklos Szeredie5183742005-02-02 11:14:04 +00001883
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001884 switch (in->opcode) {
Miklos Szeredia181e612001-11-06 12:03:23 +00001885 case FUSE_LOOKUP:
1886 do_lookup(f, in, (char *) inarg);
1887 break;
1888
Miklos Szeredia181e612001-11-06 12:03:23 +00001889 case FUSE_GETATTR:
1890 do_getattr(f, in);
1891 break;
1892
1893 case FUSE_SETATTR:
1894 do_setattr(f, in, (struct fuse_setattr_in *) inarg);
1895 break;
1896
1897 case FUSE_READLINK:
1898 do_readlink(f, in);
1899 break;
1900
Miklos Szeredia181e612001-11-06 12:03:23 +00001901 case FUSE_MKNOD:
1902 do_mknod(f, in, (struct fuse_mknod_in *) inarg);
1903 break;
Miklos Szeredie5183742005-02-02 11:14:04 +00001904
Miklos Szeredia181e612001-11-06 12:03:23 +00001905 case FUSE_MKDIR:
1906 do_mkdir(f, in, (struct fuse_mkdir_in *) inarg);
1907 break;
Miklos Szeredie5183742005-02-02 11:14:04 +00001908
Miklos Szeredia181e612001-11-06 12:03:23 +00001909 case FUSE_UNLINK:
Miklos Szeredib5958612004-02-20 14:10:49 +00001910 do_unlink(f, in, (char *) inarg);
1911 break;
1912
Miklos Szeredia181e612001-11-06 12:03:23 +00001913 case FUSE_RMDIR:
Miklos Szeredib5958612004-02-20 14:10:49 +00001914 do_rmdir(f, in, (char *) inarg);
Miklos Szeredia181e612001-11-06 12:03:23 +00001915 break;
1916
1917 case FUSE_SYMLINK:
Miklos Szeredie5183742005-02-02 11:14:04 +00001918 do_symlink(f, in, (char *) inarg,
Miklos Szeredia181e612001-11-06 12:03:23 +00001919 ((char *) inarg) + strlen((char *) inarg) + 1);
1920 break;
1921
1922 case FUSE_RENAME:
1923 do_rename(f, in, (struct fuse_rename_in *) inarg);
1924 break;
Miklos Szeredie5183742005-02-02 11:14:04 +00001925
Miklos Szeredia181e612001-11-06 12:03:23 +00001926 case FUSE_LINK:
1927 do_link(f, in, (struct fuse_link_in *) inarg);
1928 break;
1929
1930 case FUSE_OPEN:
1931 do_open(f, in, (struct fuse_open_in *) inarg);
1932 break;
1933
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001934 case FUSE_FLUSH:
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001935 do_flush(f, in, (struct fuse_flush_in *) inarg);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001936 break;
1937
Miklos Szeredi9478e862002-12-11 09:50:26 +00001938 case FUSE_RELEASE:
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001939 do_release(f, in, (struct fuse_release_in *) inarg);
Miklos Szeredi9478e862002-12-11 09:50:26 +00001940 break;
1941
Miklos Szeredia181e612001-11-06 12:03:23 +00001942 case FUSE_READ:
1943 do_read(f, in, (struct fuse_read_in *) inarg);
1944 break;
1945
1946 case FUSE_WRITE:
1947 do_write(f, in, (struct fuse_write_in *) inarg);
1948 break;
1949
Mark Glinesd84b39a2002-01-07 16:32:02 +00001950 case FUSE_STATFS:
1951 do_statfs(f, in);
1952 break;
1953
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001954 case FUSE_FSYNC:
1955 do_fsync(f, in, (struct fuse_fsync_in *) inarg);
1956 break;
1957
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001958 case FUSE_SETXATTR:
1959 do_setxattr(f, in, (struct fuse_setxattr_in *) inarg);
1960 break;
1961
1962 case FUSE_GETXATTR:
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001963 do_getxattr(f, in, (struct fuse_getxattr_in *) inarg);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001964 break;
1965
1966 case FUSE_LISTXATTR:
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001967 do_listxattr(f, in, (struct fuse_getxattr_in *) inarg);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001968 break;
1969
1970 case FUSE_REMOVEXATTR:
1971 do_removexattr(f, in, (char *) inarg);
1972 break;
1973
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001974 case FUSE_INIT:
1975 do_init(f, in, (struct fuse_init_in_out *) inarg);
1976 break;
1977
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001978 case FUSE_OPENDIR:
1979 do_opendir(f, in, (struct fuse_open_in *) inarg);
1980 break;
1981
1982 case FUSE_READDIR:
1983 do_readdir(f, in, (struct fuse_read_in *) inarg);
1984 break;
1985
1986 case FUSE_RELEASEDIR:
1987 do_releasedir(f, in, (struct fuse_release_in *) inarg);
1988 break;
1989
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001990 case FUSE_FSYNCDIR:
1991 do_fsyncdir(f, in, (struct fuse_fsync_in *) inarg);
1992 break;
1993
Miklos Szeredia181e612001-11-06 12:03:23 +00001994 default:
Miklos Szeredi43696432001-11-18 19:15:05 +00001995 send_reply(f, in, -ENOSYS, NULL, 0);
Miklos Szeredia181e612001-11-06 12:03:23 +00001996 }
Miklos Szeredi43696432001-11-18 19:15:05 +00001997
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001998 out:
Miklos Szeredi43696432001-11-18 19:15:05 +00001999 free_cmd(cmd);
Miklos Szeredia181e612001-11-06 12:03:23 +00002000}
2001
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002002int fuse_exited(struct fuse* f)
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00002003{
2004 return f->exited;
2005}
2006
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002007struct fuse_cmd *fuse_read_cmd(struct fuse *f)
Miklos Szeredi2df1c042001-11-06 15:07:17 +00002008{
Miklos Szeredifff56ab2001-11-16 10:12:59 +00002009 ssize_t res;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00002010 struct fuse_cmd *cmd;
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00002011 struct fuse_in_header *in;
2012 void *inarg;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00002013
Miklos Szeredi43696432001-11-18 19:15:05 +00002014 cmd = (struct fuse_cmd *) malloc(sizeof(struct fuse_cmd));
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002015 if (cmd == NULL) {
2016 fprintf(stderr, "fuse: failed to allocate cmd in read\n");
2017 return NULL;
2018 }
Miklos Szeredi43696432001-11-18 19:15:05 +00002019 cmd->buf = (char *) malloc(FUSE_MAX_IN);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002020 if (cmd->buf == NULL) {
2021 fprintf(stderr, "fuse: failed to allocate read buffer\n");
2022 free(cmd);
2023 return NULL;
2024 }
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00002025 in = (struct fuse_in_header *) cmd->buf;
Miklos Szeredi30e093a2005-04-03 17:44:54 +00002026 inarg = cmd->buf + SIZEOF_COMPAT(f, fuse_in_header);
Miklos Szeredi43696432001-11-18 19:15:05 +00002027
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00002028 res = read(f->fd, cmd->buf, FUSE_MAX_IN);
2029 if (res == -1) {
2030 free_cmd(cmd);
Miklos Szeredie56818b2004-12-12 11:45:24 +00002031 if (fuse_exited(f) || errno == EINTR || errno == ENOENT)
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00002032 return NULL;
Miklos Szeredie5183742005-02-02 11:14:04 +00002033
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00002034 /* ENODEV means we got unmounted, so we silenty return failure */
2035 if (errno != ENODEV) {
2036 /* BAD... This will happen again */
2037 perror("fuse: reading device");
2038 }
Miklos Szeredie5183742005-02-02 11:14:04 +00002039
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00002040 fuse_exit(f);
2041 return NULL;
2042 }
Miklos Szeredi30e093a2005-04-03 17:44:54 +00002043 if ((size_t) res < SIZEOF_COMPAT(f, fuse_in_header)) {
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00002044 free_cmd(cmd);
2045 /* Cannot happen */
2046 fprintf(stderr, "short read on fuse device\n");
2047 fuse_exit(f);
2048 return NULL;
2049 }
2050 cmd->buflen = res;
Miklos Szeredie5183742005-02-02 11:14:04 +00002051
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00002052 /* Forget is special, it can be done without messing with threads. */
2053 if (in->opcode == FUSE_FORGET) {
2054 do_forget(f, in, (struct fuse_forget_in *) inarg);
2055 free_cmd(cmd);
2056 return NULL;
2057 }
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00002058
Miklos Szeredifff56ab2001-11-16 10:12:59 +00002059 return cmd;
Miklos Szeredi2df1c042001-11-06 15:07:17 +00002060}
2061
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002062int fuse_loop(struct fuse *f)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002063{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00002064 if (f == NULL)
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002065 return -1;
Miklos Szeredic40748a2004-02-20 16:38:45 +00002066
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00002067 while (1) {
Miklos Szeredi0f48a262002-12-05 14:23:01 +00002068 struct fuse_cmd *cmd;
2069
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002070 if (fuse_exited(f))
Miklos Szeredi874e3c12004-11-01 23:15:20 +00002071 break;
Miklos Szeredi0f48a262002-12-05 14:23:01 +00002072
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002073 cmd = fuse_read_cmd(f);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00002074 if (cmd == NULL)
Miklos Szeredi0f48a262002-12-05 14:23:01 +00002075 continue;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002076
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002077 fuse_process_cmd(f, cmd);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002078 }
Miklos Szeredi874e3c12004-11-01 23:15:20 +00002079 f->exited = 0;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002080 return 0;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002081}
2082
Miklos Szeredi891b8742004-07-29 09:27:49 +00002083int fuse_invalidate(struct fuse *f, const char *path)
2084{
Miklos Szeredie56818b2004-12-12 11:45:24 +00002085 (void) f;
2086 (void) path;
2087 return -EINVAL;
Miklos Szeredi891b8742004-07-29 09:27:49 +00002088}
2089
Miklos Szeredi0f48a262002-12-05 14:23:01 +00002090void fuse_exit(struct fuse *f)
2091{
2092 f->exited = 1;
2093}
2094
Miklos Szeredid169f312004-09-22 08:48:26 +00002095struct fuse_context *fuse_get_context()
Miklos Szeredi2e50d432001-12-20 12:17:25 +00002096{
Miklos Szeredid169f312004-09-22 08:48:26 +00002097 static struct fuse_context context;
2098 if (fuse_getcontext)
2099 return fuse_getcontext();
Miklos Szeredi2e50d432001-12-20 12:17:25 +00002100 else
Miklos Szeredid169f312004-09-22 08:48:26 +00002101 return &context;
2102}
2103
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002104void fuse_set_getcontext_func(struct fuse_context *(*func)(void))
Miklos Szeredid169f312004-09-22 08:48:26 +00002105{
2106 fuse_getcontext = func;
Miklos Szeredi2e50d432001-12-20 12:17:25 +00002107}
2108
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002109int fuse_is_lib_option(const char *opt)
2110{
2111 if (strcmp(opt, "debug") == 0 ||
Miklos Szeredia13d9002004-11-02 17:32:03 +00002112 strcmp(opt, "hard_remove") == 0 ||
Miklos Szeredi0111f9d2005-04-22 12:04:55 +00002113 strcmp(opt, "use_ino") == 0 ||
Miklos Szeredi33be22d2005-05-27 09:12:43 +00002114 strcmp(opt, "allow_root") == 0 ||
2115 strcmp(opt, "readdir_ino") == 0)
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002116 return 1;
2117 else
2118 return 0;
2119}
2120
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002121static int parse_lib_opts(struct fuse *f, const char *opts)
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002122{
2123 if (opts) {
2124 char *xopts = strdup(opts);
2125 char *s = xopts;
2126 char *opt;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002127
Miklos Szeredie56818b2004-12-12 11:45:24 +00002128 if (xopts == NULL) {
2129 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002130 return -1;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002131 }
Miklos Szeredie5183742005-02-02 11:14:04 +00002132
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002133 while((opt = strsep(&s, ","))) {
2134 if (strcmp(opt, "debug") == 0)
2135 f->flags |= FUSE_DEBUG;
2136 else if (strcmp(opt, "hard_remove") == 0)
2137 f->flags |= FUSE_HARD_REMOVE;
Miklos Szeredia13d9002004-11-02 17:32:03 +00002138 else if (strcmp(opt, "use_ino") == 0)
2139 f->flags |= FUSE_USE_INO;
Miklos Szeredi0111f9d2005-04-22 12:04:55 +00002140 else if (strcmp(opt, "allow_root") == 0)
2141 f->flags |= FUSE_ALLOW_ROOT;
Miklos Szeredi33be22d2005-05-27 09:12:43 +00002142 else if (strcmp(opt, "readdir_ino") == 0)
2143 f->flags |= FUSE_READDIR_INO;
Miklos Szeredie5183742005-02-02 11:14:04 +00002144 else
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002145 fprintf(stderr, "fuse: warning: unknown option `%s'\n", opt);
2146 }
2147 free(xopts);
2148 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002149 return 0;
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002150}
2151
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002152struct fuse *fuse_new_common(int fd, const char *opts,
2153 const struct fuse_operations *op,
2154 size_t op_size, int compat)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002155{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002156 struct fuse *f;
2157 struct node *root;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002158
Miklos Szeredi0f62d722005-01-04 12:45:54 +00002159 if (sizeof(struct fuse_operations) < op_size) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002160 fprintf(stderr, "fuse: warning: library too old, some operations may not not work\n");
Miklos Szeredi0f62d722005-01-04 12:45:54 +00002161 op_size = sizeof(struct fuse_operations);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002162 }
2163
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002164 f = (struct fuse *) calloc(1, sizeof(struct fuse));
Miklos Szeredie56818b2004-12-12 11:45:24 +00002165 if (f == NULL) {
2166 fprintf(stderr, "fuse: failed to allocate fuse object\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002167 goto out;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002168 }
Miklos Szeredi2df1c042001-11-06 15:07:17 +00002169
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002170 if (parse_lib_opts(f, opts) == -1)
2171 goto out_free;
2172
Miklos Szeredi8cffdb92001-11-09 14:49:18 +00002173 f->fd = fd;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002174 f->ctr = 0;
Miklos Szeredi76f65782004-02-19 16:55:40 +00002175 f->generation = 0;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00002176 /* FIXME: Dynamic hash table */
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002177 f->name_table_size = 14057;
2178 f->name_table = (struct node **)
2179 calloc(1, sizeof(struct node *) * f->name_table_size);
Miklos Szeredie56818b2004-12-12 11:45:24 +00002180 if (f->name_table == NULL) {
2181 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002182 goto out_free;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002183 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002184
Miklos Szeredia13d9002004-11-02 17:32:03 +00002185 f->id_table_size = 14057;
2186 f->id_table = (struct node **)
2187 calloc(1, sizeof(struct node *) * f->id_table_size);
Miklos Szeredie56818b2004-12-12 11:45:24 +00002188 if (f->id_table == NULL) {
2189 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002190 goto out_free_name_table;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002191 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002192
Miklos Szerediab974562005-04-07 15:40:21 +00002193 mutex_init(&f->lock);
2194 mutex_init(&f->worker_lock);
Miklos Szeredi33232032001-11-19 17:55:51 +00002195 f->numworker = 0;
2196 f->numavail = 0;
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002197 memcpy(&f->op, op, op_size);
2198 f->compat = compat;
Miklos Szeredi0f48a262002-12-05 14:23:01 +00002199 f->exited = 0;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002200
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002201 root = (struct node *) calloc(1, sizeof(struct node));
Miklos Szeredie56818b2004-12-12 11:45:24 +00002202 if (root == NULL) {
2203 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredia13d9002004-11-02 17:32:03 +00002204 goto out_free_id_table;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002205 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002206
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002207 root->name = strdup("/");
Miklos Szeredie56818b2004-12-12 11:45:24 +00002208 if (root->name == NULL) {
2209 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002210 goto out_free_root;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002211 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002212
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002213 root->parent = 0;
Miklos Szeredia13d9002004-11-02 17:32:03 +00002214 root->nodeid = FUSE_ROOT_ID;
Miklos Szeredi76f65782004-02-19 16:55:40 +00002215 root->generation = 0;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00002216 root->refctr = 1;
Miklos Szeredi38009022005-05-08 19:47:22 +00002217 root->nlookup = 1;
Miklos Szeredia13d9002004-11-02 17:32:03 +00002218 hash_id(f, root);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002219
Miklos Szeredi0111f9d2005-04-22 12:04:55 +00002220 f->owner = getuid();
2221
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002222 return f;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002223
2224 out_free_root:
2225 free(root);
Miklos Szeredia13d9002004-11-02 17:32:03 +00002226 out_free_id_table:
2227 free(f->id_table);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002228 out_free_name_table:
2229 free(f->name_table);
2230 out_free:
2231 free(f);
2232 out:
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002233 return NULL;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002234}
2235
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002236struct fuse *fuse_new(int fd, const char *opts,
2237 const struct fuse_operations *op, size_t op_size)
2238{
2239 return fuse_new_common(fd, opts, op, op_size, 0);
2240}
2241
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002242struct fuse *fuse_new_compat2(int fd, const char *opts,
2243 const struct fuse_operations_compat2 *op)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002244{
2245 return fuse_new_common(fd, opts, (struct fuse_operations *) op,
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002246 sizeof(struct fuse_operations_compat2), 21);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002247}
2248
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002249struct fuse *fuse_new_compat1(int fd, int flags,
2250 const struct fuse_operations_compat1 *op)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002251{
2252 char *opts = NULL;
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002253 if (flags & FUSE_DEBUG_COMPAT1)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002254 opts = "debug";
2255 return fuse_new_common(fd, opts, (struct fuse_operations *) op,
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002256 sizeof(struct fuse_operations_compat1), 11);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002257}
2258
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002259void fuse_destroy(struct fuse *f)
2260{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002261 size_t i;
Miklos Szeredia13d9002004-11-02 17:32:03 +00002262 for (i = 0; i < f->id_table_size; i++) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002263 struct node *node;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002264
Miklos Szeredia13d9002004-11-02 17:32:03 +00002265 for (node = f->id_table[i]; node != NULL; node = node->id_next) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002266 if (node->is_hidden) {
Miklos Szeredia13d9002004-11-02 17:32:03 +00002267 char *path = get_path(f, node->nodeid);
Miklos Szeredi21019c92005-05-09 11:22:41 +00002268 if (path) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002269 f->op.unlink(path);
Miklos Szeredi21019c92005-05-09 11:22:41 +00002270 free(path);
2271 }
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002272 }
2273 }
2274 }
Miklos Szeredia13d9002004-11-02 17:32:03 +00002275 for (i = 0; i < f->id_table_size; i++) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002276 struct node *node;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002277 struct node *next;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002278
Miklos Szeredia13d9002004-11-02 17:32:03 +00002279 for (node = f->id_table[i]; node != NULL; node = next) {
2280 next = node->id_next;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002281 free_node(node);
2282 }
2283 }
Miklos Szeredia13d9002004-11-02 17:32:03 +00002284 free(f->id_table);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002285 free(f->name_table);
Miklos Szeredia181e612001-11-06 12:03:23 +00002286 pthread_mutex_destroy(&f->lock);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00002287 pthread_mutex_destroy(&f->worker_lock);
Miklos Szeredi159bd7e2005-02-28 17:32:16 +00002288 if (f->op.destroy)
2289 f->op.destroy(f->user_data);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002290 free(f);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002291}
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002292
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002293__asm__(".symver fuse_exited,__fuse_exited@");
2294__asm__(".symver fuse_process_cmd,__fuse_process_cmd@");
2295__asm__(".symver fuse_read_cmd,__fuse_read_cmd@");
2296__asm__(".symver fuse_set_getcontext_func,__fuse_set_getcontext_func@");
2297__asm__(".symver fuse_new_compat2,fuse_new@");