blob: b5be472384b65db03a256f77b8e5ced60da83d89 [file] [log] [blame]
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi2e6b6f22004-07-07 19:19:53 +00003 Copyright (C) 2001-2004 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00004
Miklos Szeredi8b39a9f2002-10-25 12:41:16 +00005 This program can be distributed under the terms of the GNU LGPL.
6 See the file COPYING.LIB
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00007*/
8
Miklos Szeredicb264512004-06-23 18:52:50 +00009#include <config.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000010#include "fuse_i.h"
Miklos Szeredi0f62d722005-01-04 12:45:54 +000011#include "fuse_compat.h"
Miklos Szeredib9b94cd2004-12-01 18:56:39 +000012#include "fuse_kernel.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 Szeredi85c74fc2001-10-28 19:44:14 +000023
Miklos Szeredi0f62d722005-01-04 12:45:54 +000024/* FUSE flags: */
25
26/** Enable debuging output */
27#define FUSE_DEBUG (1 << 1)
28
29/** If a file is removed but it's still open, don't hide the file but
30 remove it immediately */
31#define FUSE_HARD_REMOVE (1 << 2)
32
33/** Use st_ino field in getattr instead of generating inode numbers */
34#define FUSE_USE_INO (1 << 3)
35
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +000036#define FUSE_KERNEL_MINOR_VERSION_NEED 1
Miklos Szeredia25d4c22004-11-23 22:32:16 +000037#define FUSE_VERSION_FILE_OLD "/proc/fs/fuse/version"
Miklos Szeredi162bcbb2004-11-29 23:43:44 +000038#define FUSE_VERSION_FILE_NEW "/sys/fs/fuse/version"
Miklos Szeredia25d4c22004-11-23 22:32:16 +000039#define FUSE_DEV_OLD "/proc/fs/fuse/dev"
40
Miklos Szeredi97c61e92001-11-07 12:09:43 +000041#define FUSE_MAX_PATH 4096
Miklos Szeredi6bf8b682002-10-28 08:49:39 +000042#define PARAM(inarg) (((char *)(inarg)) + sizeof(*inarg))
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000043
Miklos Szeredi254d5ed2004-03-02 11:11:24 +000044#define ENTRY_REVALIDATE_TIME 1 /* sec */
45#define ATTR_REVALIDATE_TIME 1 /* sec */
46
Miklos Szeredi0f62d722005-01-04 12:45:54 +000047
48struct node {
49 struct node *name_next;
50 struct node *id_next;
51 nodeid_t nodeid;
52 unsigned int generation;
53 int refctr;
54 nodeid_t parent;
55 char *name;
56 uint64_t version;
57 int open_count;
58 int is_hidden;
59};
60
61struct fuse_dirhandle {
62 struct fuse *fuse;
63 nodeid_t dir;
64 FILE *fp;
65};
66
67struct fuse_cmd {
68 char *buf;
69 size_t buflen;
70};
71
72
Miklos Szeredid169f312004-09-22 08:48:26 +000073static struct fuse_context *(*fuse_getcontext)(void) = NULL;
74
Miklos Szeredic8ba2372002-12-10 12:26:00 +000075static const char *opname(enum fuse_opcode opcode)
76{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +000077 switch (opcode) {
Miklos Szeredi3ed84232004-03-30 15:17:26 +000078 case FUSE_LOOKUP: return "LOOKUP";
79 case FUSE_FORGET: return "FORGET";
80 case FUSE_GETATTR: return "GETATTR";
81 case FUSE_SETATTR: return "SETATTR";
82 case FUSE_READLINK: return "READLINK";
83 case FUSE_SYMLINK: return "SYMLINK";
84 case FUSE_GETDIR: return "GETDIR";
85 case FUSE_MKNOD: return "MKNOD";
86 case FUSE_MKDIR: return "MKDIR";
87 case FUSE_UNLINK: return "UNLINK";
88 case FUSE_RMDIR: return "RMDIR";
89 case FUSE_RENAME: return "RENAME";
90 case FUSE_LINK: return "LINK";
91 case FUSE_OPEN: return "OPEN";
92 case FUSE_READ: return "READ";
93 case FUSE_WRITE: return "WRITE";
94 case FUSE_STATFS: return "STATFS";
Miklos Szeredi99f20742004-05-19 08:01:10 +000095 case FUSE_FLUSH: return "FLUSH";
Miklos Szeredi3ed84232004-03-30 15:17:26 +000096 case FUSE_RELEASE: return "RELEASE";
97 case FUSE_FSYNC: return "FSYNC";
98 case FUSE_SETXATTR: return "SETXATTR";
99 case FUSE_GETXATTR: return "GETXATTR";
100 case FUSE_LISTXATTR: return "LISTXATTR";
101 case FUSE_REMOVEXATTR: return "REMOVEXATTR";
Miklos Szeredi99f20742004-05-19 08:01:10 +0000102 default: return "???";
Miklos Szeredic8ba2372002-12-10 12:26:00 +0000103 }
104}
105
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000106static inline void fuse_dec_avail(struct fuse *f)
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000107{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000108 pthread_mutex_lock(&f->worker_lock);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000109 f->numavail --;
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000110 pthread_mutex_unlock(&f->worker_lock);
111}
112
113static inline void fuse_inc_avail(struct fuse *f)
114{
115 pthread_mutex_lock(&f->worker_lock);
116 f->numavail ++;
117 pthread_mutex_unlock(&f->worker_lock);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000118}
119
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000120static struct node *get_node_nocheck(struct fuse *f, nodeid_t nodeid)
Miklos Szeredia181e612001-11-06 12:03:23 +0000121{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000122 size_t hash = nodeid % f->id_table_size;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000123 struct node *node;
124
Miklos Szeredia13d9002004-11-02 17:32:03 +0000125 for (node = f->id_table[hash]; node != NULL; node = node->id_next)
126 if (node->nodeid == nodeid)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000127 return node;
128
129 return NULL;
Miklos Szeredia181e612001-11-06 12:03:23 +0000130}
131
Miklos Szeredia13d9002004-11-02 17:32:03 +0000132static struct node *get_node(struct fuse *f, nodeid_t nodeid)
Miklos Szeredia181e612001-11-06 12:03:23 +0000133{
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000134 struct node *node = get_node_nocheck(f, nodeid);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000135 if (!node) {
136 fprintf(stderr, "fuse internal error: node %lu not found\n",
137 nodeid);
138 abort();
139 }
140 return node;
Miklos Szeredia181e612001-11-06 12:03:23 +0000141}
142
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000143static void free_node(struct node *node)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000144{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000145 free(node->name);
146 free(node);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000147}
148
Miklos Szeredia13d9002004-11-02 17:32:03 +0000149static void unhash_id(struct fuse *f, struct node *node)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000150{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000151 size_t hash = node->nodeid % f->id_table_size;
152 struct node **nodep = &f->id_table[hash];
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000153
Miklos Szeredia13d9002004-11-02 17:32:03 +0000154 for (; *nodep != NULL; nodep = &(*nodep)->id_next)
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000155 if (*nodep == node) {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000156 *nodep = node->id_next;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000157 return;
158 }
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000159}
160
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000161static void hash_id(struct fuse *f, struct node *node)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000162{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000163 size_t hash = node->nodeid % f->id_table_size;
164 node->id_next = f->id_table[hash];
165 f->id_table[hash] = node;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000166}
167
Miklos Szeredia13d9002004-11-02 17:32:03 +0000168static unsigned int name_hash(struct fuse *f, nodeid_t parent, const char *name)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000169{
170 unsigned int hash = *name;
171
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000172 if (hash)
173 for (name += 1; *name != '\0'; name++)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000174 hash = (hash << 5) - hash + *name;
175
176 return (hash + parent) % f->name_table_size;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000177}
178
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000179static void unref_node(struct fuse *f, struct node *node);
180
181static void unhash_name(struct fuse *f, struct node *node)
182{
183 if (node->name) {
184 size_t hash = name_hash(f, node->parent, node->name);
185 struct node **nodep = &f->name_table[hash];
186
187 for (; *nodep != NULL; nodep = &(*nodep)->name_next)
188 if (*nodep == node) {
189 *nodep = node->name_next;
190 node->name_next = NULL;
191 unref_node(f, get_node(f, node->parent));
192 free(node->name);
193 node->name = NULL;
194 node->parent = 0;
195 return;
196 }
197 fprintf(stderr, "fuse internal error: unable to unhash node: %lu\n",
198 node->nodeid);
199 abort();
200 }
201}
202
203static int hash_name(struct fuse *f, struct node *node, nodeid_t parent,
204 const char *name)
205{
206 size_t hash = name_hash(f, parent, name);
207 node->name = strdup(name);
208 if (node->name == NULL)
209 return -1;
210
211 get_node(f, parent)->refctr ++;
212 node->parent = parent;
213 node->name_next = f->name_table[hash];
214 f->name_table[hash] = node;
215 return 0;
216}
217
218static void delete_node(struct fuse *f, struct node *node)
219{
220 assert(!node->name);
221 unhash_id(f, node);
222 free_node(node);
223}
224
225static void unref_node(struct fuse *f, struct node *node)
226{
227 assert(node->refctr > 0);
228 node->refctr --;
229 if (!node->refctr)
230 delete_node(f, node);
231}
232
233static nodeid_t next_id(struct fuse *f)
234{
235 do {
236 f->ctr++;
237 if (!f->ctr)
238 f->generation ++;
239 } while (f->ctr == 0 || get_node_nocheck(f, f->ctr) != NULL);
240 return f->ctr;
241}
242
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000243static struct node *lookup_node(struct fuse *f, nodeid_t parent,
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000244 const char *name)
245{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000246 size_t hash = name_hash(f, parent, name);
247 struct node *node;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000248
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000249 for (node = f->name_table[hash]; node != NULL; node = node->name_next)
250 if (node->parent == parent && strcmp(node->name, name) == 0)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000251 return node;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000252
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000253 return NULL;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000254}
255
Miklos Szeredia13d9002004-11-02 17:32:03 +0000256static struct node *find_node(struct fuse *f, nodeid_t parent, char *name,
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000257 struct fuse_attr *attr, uint64_t version)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000258{
259 struct node *node;
Miklos Szeredia181e612001-11-06 12:03:23 +0000260 int mode = attr->mode & S_IFMT;
261 int rdev = 0;
262
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000263 if (S_ISCHR(mode) || S_ISBLK(mode))
Miklos Szeredia181e612001-11-06 12:03:23 +0000264 rdev = attr->rdev;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000265
Miklos Szeredia181e612001-11-06 12:03:23 +0000266 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000267 node = lookup_node(f, parent, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000268 if (node != NULL) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000269 if (!(f->flags & FUSE_USE_INO))
270 attr->ino = node->nodeid;
271 } else {
272 node = (struct node *) calloc(1, sizeof(struct node));
273 if (node == NULL)
274 goto out_err;
Miklos Szeredia181e612001-11-06 12:03:23 +0000275
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000276 node->refctr = 1;
277 node->nodeid = next_id(f);
278 if (!(f->flags & FUSE_USE_INO))
279 attr->ino = node->nodeid;
280 node->open_count = 0;
281 node->is_hidden = 0;
282 node->generation = f->generation;
283 if (hash_name(f, node, parent, name) == -1) {
284 free(node);
285 node = NULL;
286 goto out_err;
287 }
288 hash_id(f, node);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000289 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000290 node->version = version;
Miklos Szeredic2309912004-09-21 13:40:38 +0000291 out_err:
Miklos Szeredia181e612001-11-06 12:03:23 +0000292 pthread_mutex_unlock(&f->lock);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000293 return node;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000294}
295
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000296static char *add_name(char *buf, char *s, const char *name)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000297{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000298 size_t len = strlen(name);
299 s -= len;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000300 if (s <= buf) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000301 fprintf(stderr, "fuse: path too long: ...%s\n", s + len);
302 return NULL;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000303 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000304 strncpy(s, name, len);
305 s--;
306 *s = '/';
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000307
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000308 return s;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000309}
310
Miklos Szeredia13d9002004-11-02 17:32:03 +0000311static char *get_path_name(struct fuse *f, nodeid_t nodeid, const char *name)
Miklos Szeredib483c932001-10-29 14:57:57 +0000312{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000313 char buf[FUSE_MAX_PATH];
314 char *s = buf + FUSE_MAX_PATH - 1;
315 struct node *node;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000316
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000317 *s = '\0';
Miklos Szeredia181e612001-11-06 12:03:23 +0000318
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000319 if (name != NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000320 s = add_name(buf, s, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000321 if (s == NULL)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000322 return NULL;
323 }
324
325 pthread_mutex_lock(&f->lock);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000326 for (node = get_node(f, nodeid); node && node->nodeid != FUSE_ROOT_ID;
327 node = get_node(f, node->parent)) {
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000328 if (node->name == NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000329 s = NULL;
330 break;
331 }
332
333 s = add_name(buf, s, node->name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000334 if (s == NULL)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000335 break;
336 }
337 pthread_mutex_unlock(&f->lock);
338
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000339 if (node == NULL || s == NULL)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000340 return NULL;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000341 else if (*s == '\0')
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000342 return strdup("/");
343 else
344 return strdup(s);
345}
Miklos Szeredia181e612001-11-06 12:03:23 +0000346
Miklos Szeredia13d9002004-11-02 17:32:03 +0000347static char *get_path(struct fuse *f, nodeid_t nodeid)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000348{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000349 return get_path_name(f, nodeid, NULL);
Miklos Szeredib483c932001-10-29 14:57:57 +0000350}
351
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000352static void forget_node(struct fuse *f, nodeid_t nodeid, uint64_t version)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000353{
Miklos Szeredia181e612001-11-06 12:03:23 +0000354 struct node *node;
355
356 pthread_mutex_lock(&f->lock);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000357 node = get_node(f, nodeid);
358 if (node->version == version && nodeid != FUSE_ROOT_ID) {
359 node->version = 0;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000360 unhash_name(f, node);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000361 unref_node(f, node);
Miklos Szeredia181e612001-11-06 12:03:23 +0000362 }
363 pthread_mutex_unlock(&f->lock);
364
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000365}
366
Miklos Szeredia13d9002004-11-02 17:32:03 +0000367static void remove_node(struct fuse *f, nodeid_t dir, const char *name)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000368{
Miklos Szeredia181e612001-11-06 12:03:23 +0000369 struct node *node;
370
371 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000372 node = lookup_node(f, dir, name);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000373 if (node != NULL)
374 unhash_name(f, node);
Miklos Szeredia181e612001-11-06 12:03:23 +0000375 pthread_mutex_unlock(&f->lock);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000376}
377
Miklos Szeredia13d9002004-11-02 17:32:03 +0000378static int rename_node(struct fuse *f, nodeid_t olddir, const char *oldname,
379 nodeid_t newdir, const char *newname, int hide)
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000380{
Miklos Szeredia181e612001-11-06 12:03:23 +0000381 struct node *node;
382 struct node *newnode;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000383 int err = 0;
Miklos Szeredia181e612001-11-06 12:03:23 +0000384
385 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000386 node = lookup_node(f, olddir, oldname);
387 newnode = lookup_node(f, newdir, newname);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000388 if (node == NULL)
389 goto out;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000390
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000391 if (newnode != NULL) {
392 if (hide) {
393 fprintf(stderr, "fuse: hidden file got created during hiding\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000394 err = -EBUSY;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000395 goto out;
396 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000397 unhash_name(f, newnode);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000398 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000399
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000400 unhash_name(f, node);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000401 if (hash_name(f, node, newdir, newname) == -1) {
402 err = -ENOMEM;
403 goto out;
404 }
405
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000406 if (hide)
407 node->is_hidden = 1;
408
409 out:
Miklos Szeredia181e612001-11-06 12:03:23 +0000410 pthread_mutex_unlock(&f->lock);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000411 return err;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000412}
413
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000414static void convert_stat(struct stat *stbuf, struct fuse_attr *attr)
415{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000416 attr->ino = stbuf->st_ino;
Miklos Szeredib5958612004-02-20 14:10:49 +0000417 attr->mode = stbuf->st_mode;
418 attr->nlink = stbuf->st_nlink;
419 attr->uid = stbuf->st_uid;
420 attr->gid = stbuf->st_gid;
421 attr->rdev = stbuf->st_rdev;
422 attr->size = stbuf->st_size;
423 attr->blocks = stbuf->st_blocks;
424 attr->atime = stbuf->st_atime;
Miklos Szeredib5958612004-02-20 14:10:49 +0000425 attr->mtime = stbuf->st_mtime;
Miklos Szeredib5958612004-02-20 14:10:49 +0000426 attr->ctime = stbuf->st_ctime;
Miklos Szeredicb264512004-06-23 18:52:50 +0000427#ifdef HAVE_STRUCT_STAT_ST_ATIM
428 attr->atimensec = stbuf->st_atim.tv_nsec;
429 attr->mtimensec = stbuf->st_mtim.tv_nsec;
Miklos Szeredib5958612004-02-20 14:10:49 +0000430 attr->ctimensec = stbuf->st_ctim.tv_nsec;
Miklos Szeredicb264512004-06-23 18:52:50 +0000431#endif
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000432}
433
Miklos Szeredi8fb48fe2004-11-08 14:48:52 +0000434static int fill_dir(struct fuse_dirhandle *dh, const char *name, int type,
435 ino_t ino)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000436{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000437 size_t namelen = strlen(name);
438 struct fuse_dirent *dirent;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000439 size_t reclen;
440 size_t res;
441
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000442 if (namelen > FUSE_NAME_MAX)
443 namelen = FUSE_NAME_MAX;
444
445 dirent = calloc(1, sizeof(struct fuse_dirent) + namelen + 8);
446 if (dirent == NULL)
447 return -ENOMEM;
448
Miklos Szeredi8fb48fe2004-11-08 14:48:52 +0000449 if ((dh->fuse->flags & FUSE_USE_INO))
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000450 dirent->ino = ino;
Miklos Szeredi8fb48fe2004-11-08 14:48:52 +0000451 else
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000452 dirent->ino = (unsigned long) -1;
453 dirent->namelen = namelen;
454 strncpy(dirent->name, name, namelen);
455 dirent->type = type;
456 reclen = FUSE_DIRENT_SIZE(dirent);
457 res = fwrite(dirent, reclen, 1, dh->fp);
458 free(dirent);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000459 if (res == 0) {
Miklos Szeredi96249982001-11-21 12:21:19 +0000460 perror("fuse: writing directory file");
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000461 return -EIO;
462 }
463 return 0;
464}
465
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000466static int send_reply_raw(struct fuse *f, char *outbuf, size_t outsize)
Miklos Szeredi43696432001-11-18 19:15:05 +0000467{
468 int res;
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000469 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
470 out->len = outsize;
Miklos Szeredi43696432001-11-18 19:15:05 +0000471
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000472 if ((f->flags & FUSE_DEBUG)) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000473 printf(" unique: %llu, error: %i (%s), outsize: %i\n",
474 out->unique, out->error, strerror(-out->error), outsize);
Miklos Szeredi43696432001-11-18 19:15:05 +0000475 fflush(stdout);
476 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000477
Miklos Szeredi4e71c9f2004-02-09 12:05:14 +0000478 /* This needs to be done before the reply, otherwise the scheduler
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000479 could play tricks with us, and only let the counter be
480 increased long after the operation is done */
481 fuse_inc_avail(f);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000482
Miklos Szeredi43696432001-11-18 19:15:05 +0000483 res = write(f->fd, outbuf, outsize);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000484 if (res == -1) {
Miklos Szeredi43696432001-11-18 19:15:05 +0000485 /* ENOENT means the operation was interrupted */
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000486 if (!f->exited && errno != ENOENT)
Miklos Szeredi96249982001-11-21 12:21:19 +0000487 perror("fuse: writing device");
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000488 return -errno;
Miklos Szeredi43696432001-11-18 19:15:05 +0000489 }
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000490 return 0;
Miklos Szeredi43696432001-11-18 19:15:05 +0000491}
492
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000493static int send_reply(struct fuse *f, struct fuse_in_header *in, int error,
494 void *arg, size_t argsize)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000495{
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000496 int res;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000497 char *outbuf;
498 size_t outsize;
499 struct fuse_out_header *out;
500
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000501 if (error <= -1000 || error > 0) {
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000502 fprintf(stderr, "fuse: bad error value: %i\n", error);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000503 error = -ERANGE;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000504 }
505
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000506 if (error)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000507 argsize = 0;
508
509 outsize = sizeof(struct fuse_out_header) + argsize;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000510 outbuf = (char *) malloc(outsize);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000511 if (outbuf == NULL) {
512 fprintf(stderr, "fuse: failed to allocate reply buffer\n");
513 res = -ENOMEM;
514 } else {
515 out = (struct fuse_out_header *) outbuf;
516 memset(out, 0, sizeof(struct fuse_out_header));
517 out->unique = in->unique;
518 out->error = error;
519 if (argsize != 0)
520 memcpy(outbuf + sizeof(struct fuse_out_header), arg, argsize);
521
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000522 res = send_reply_raw(f, outbuf, outsize);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000523 free(outbuf);
524 }
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000525
526 return res;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000527}
528
Miklos Szeredia13d9002004-11-02 17:32:03 +0000529static int is_open(struct fuse *f, nodeid_t dir, const char *name)
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000530{
531 struct node *node;
532 int isopen = 0;
533 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000534 node = lookup_node(f, dir, name);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000535 if (node && node->open_count > 0)
536 isopen = 1;
537 pthread_mutex_unlock(&f->lock);
538 return isopen;
539}
540
Miklos Szeredia13d9002004-11-02 17:32:03 +0000541static char *hidden_name(struct fuse *f, nodeid_t dir, const char *oldname,
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000542 char *newname, size_t bufsize)
543{
544 struct stat buf;
545 struct node *node;
546 struct node *newnode;
547 char *newpath;
548 int res;
549 int failctr = 10;
550
551 if (!f->op.getattr)
552 return NULL;
553
554 do {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000555 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000556 node = lookup_node(f, dir, oldname);
557 if (node == NULL) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000558 pthread_mutex_unlock(&f->lock);
559 return NULL;
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000560 }
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000561 do {
562 f->hidectr ++;
563 snprintf(newname, bufsize, ".fuse_hidden%08x%08x",
Miklos Szeredia13d9002004-11-02 17:32:03 +0000564 (unsigned int) node->nodeid, f->hidectr);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000565 newnode = lookup_node(f, dir, newname);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000566 } while(newnode);
567 pthread_mutex_unlock(&f->lock);
568
569 newpath = get_path_name(f, dir, newname);
570 if (!newpath)
571 break;
572
573 res = f->op.getattr(newpath, &buf);
574 if (res != 0)
575 break;
576 free(newpath);
577 newpath = NULL;
578 } while(--failctr);
579
580 return newpath;
581}
582
Miklos Szeredia13d9002004-11-02 17:32:03 +0000583static int hide_node(struct fuse *f, const char *oldpath, nodeid_t dir,
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000584 const char *oldname)
585{
586 char newname[64];
587 char *newpath;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000588 int err = -EBUSY;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000589
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000590 if (f->op.rename && f->op.unlink) {
591 newpath = hidden_name(f, dir, oldname, newname, sizeof(newname));
592 if (newpath) {
593 int res = f->op.rename(oldpath, newpath);
594 if (res == 0)
595 err = rename_node(f, dir, oldname, dir, newname, 1);
596 free(newpath);
597 }
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000598 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000599 return err;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000600}
601
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000602static int lookup_path(struct fuse *f, nodeid_t nodeid, uint64_t version,
603 char *name, const char *path,
604 struct fuse_entry_out *arg)
Miklos Szeredi76f65782004-02-19 16:55:40 +0000605{
606 int res;
607 struct stat buf;
608
609 res = f->op.getattr(path, &buf);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000610 if (res == 0) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000611 struct node *node;
612
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000613 memset(arg, 0, sizeof(struct fuse_entry_out));
Miklos Szeredi76f65782004-02-19 16:55:40 +0000614 convert_stat(&buf, &arg->attr);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000615 node = find_node(f, nodeid, name, &arg->attr, version);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000616 if (node == NULL)
617 res = -ENOMEM;
618 else {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000619 arg->nodeid = node->nodeid;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000620 arg->generation = node->generation;
621 arg->entry_valid = ENTRY_REVALIDATE_TIME;
622 arg->entry_valid_nsec = 0;
623 arg->attr_valid = ATTR_REVALIDATE_TIME;
624 arg->attr_valid_nsec = 0;
625 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000626 printf(" NODEID: %lu\n", (unsigned long) arg->nodeid);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000627 fflush(stdout);
628 }
Miklos Szeredi76f65782004-02-19 16:55:40 +0000629 }
630 }
631 return res;
632}
633
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000634static void do_lookup(struct fuse *f, struct fuse_in_header *in, char *name)
635{
636 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000637 int res2;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000638 char *path;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000639 struct fuse_entry_out arg;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000640
Miklos Szeredi5e183482001-10-31 14:52:35 +0000641 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000642 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000643 if (path != NULL) {
644 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi6ebe2342002-01-06 21:44:16 +0000645 printf("LOOKUP %s\n", path);
646 fflush(stdout);
647 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000648 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000649 if (f->op.getattr)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000650 res = lookup_path(f, in->nodeid, in->unique, name, path, &arg);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000651 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000652 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000653 res2 = send_reply(f, in, res, &arg, sizeof(arg));
654 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000655 forget_node(f, arg.nodeid, in->unique);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000656}
657
Miklos Szeredia181e612001-11-06 12:03:23 +0000658static void do_forget(struct fuse *f, struct fuse_in_header *in,
659 struct fuse_forget_in *arg)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000660{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000661 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000662 printf("FORGET %lu/%llu\n", (unsigned long) in->nodeid,
663 arg->version);
Miklos Szeredi43696432001-11-18 19:15:05 +0000664 fflush(stdout);
665 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000666 forget_node(f, in->nodeid, arg->version);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000667}
668
669static void do_getattr(struct fuse *f, struct fuse_in_header *in)
670{
671 int res;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000672 char *path;
673 struct stat buf;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000674 struct fuse_attr_out arg;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000675
Miklos Szeredi5e183482001-10-31 14:52:35 +0000676 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000677 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000678 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +0000679 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000680 if (f->op.getattr)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000681 res = f->op.getattr(path, &buf);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000682 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000683 }
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000684
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000685 if (res == 0) {
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000686 memset(&arg, 0, sizeof(struct fuse_attr_out));
687 arg.attr_valid = ATTR_REVALIDATE_TIME;
688 arg.attr_valid_nsec = 0;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000689 convert_stat(&buf, &arg.attr);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000690 if (!(f->flags & FUSE_USE_INO))
691 arg.attr.ino = in->nodeid;
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000692 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000693
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000694 send_reply(f, in, res, &arg, sizeof(arg));
695}
696
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000697static int do_chmod(struct fuse *f, const char *path, struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000698{
699 int res;
700
701 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000702 if (f->op.chmod)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000703 res = f->op.chmod(path, attr->mode);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000704
705 return res;
706}
707
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000708static int do_chown(struct fuse *f, const char *path, struct fuse_attr *attr,
Miklos Szeredi2e50d432001-12-20 12:17:25 +0000709 int valid)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000710{
711 int res;
712 uid_t uid = (valid & FATTR_UID) ? attr->uid : (uid_t) -1;
713 gid_t gid = (valid & FATTR_GID) ? attr->gid : (gid_t) -1;
714
715 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000716 if (f->op.chown)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000717 res = f->op.chown(path, uid, gid);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000718
719 return res;
720}
721
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000722static int do_truncate(struct fuse *f, const char *path,
723 struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000724{
725 int res;
726
727 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000728 if (f->op.truncate)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000729 res = f->op.truncate(path, attr->size);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000730
731 return res;
732}
733
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000734static int do_utime(struct fuse *f, const char *path, struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000735{
736 int res;
737 struct utimbuf buf;
738 buf.actime = attr->atime;
739 buf.modtime = attr->mtime;
740 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000741 if (f->op.utime)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000742 res = f->op.utime(path, &buf);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000743
744 return res;
745}
746
Miklos Szeredi5e183482001-10-31 14:52:35 +0000747static void do_setattr(struct fuse *f, struct fuse_in_header *in,
748 struct fuse_setattr_in *arg)
749{
750 int res;
751 char *path;
752 int valid = arg->valid;
753 struct fuse_attr *attr = &arg->attr;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000754 struct fuse_attr_out outarg;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000755
756 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000757 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000758 if (path != NULL) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000759 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000760 if (f->op.getattr) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000761 res = 0;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000762 if (!res && (valid & FATTR_MODE))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000763 res = do_chmod(f, path, attr);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000764 if (!res && (valid & (FATTR_UID | FATTR_GID)))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000765 res = do_chown(f, path, attr, valid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000766 if (!res && (valid & FATTR_SIZE))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000767 res = do_truncate(f, path, attr);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000768 if (!res && (valid & (FATTR_ATIME | FATTR_MTIME)) ==
Miklos Szeredib5958612004-02-20 14:10:49 +0000769 (FATTR_ATIME | FATTR_MTIME))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000770 res = do_utime(f, path, attr);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000771 if (!res) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000772 struct stat buf;
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000773 res = f->op.getattr(path, &buf);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000774 if (!res) {
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000775 memset(&outarg, 0, sizeof(struct fuse_attr_out));
776 outarg.attr_valid = ATTR_REVALIDATE_TIME;
777 outarg.attr_valid_nsec = 0;
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000778 convert_stat(&buf, &outarg.attr);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000779 if (!(f->flags & FUSE_USE_INO))
780 outarg.attr.ino = in->nodeid;
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000781 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000782 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000783 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000784 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000785 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000786 send_reply(f, in, res, &outarg, sizeof(outarg));
Miklos Szeredi5e183482001-10-31 14:52:35 +0000787}
788
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000789static void do_readlink(struct fuse *f, struct fuse_in_header *in)
790{
791 int res;
792 char link[PATH_MAX + 1];
Miklos Szeredib483c932001-10-29 14:57:57 +0000793 char *path;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000794
Miklos Szeredi5e183482001-10-31 14:52:35 +0000795 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000796 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000797 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +0000798 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000799 if (f->op.readlink)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000800 res = f->op.readlink(path, link, sizeof(link));
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000801 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000802 }
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000803 link[PATH_MAX] = '\0';
Miklos Szeredi4b2bef42002-01-09 12:23:27 +0000804 send_reply(f, in, res, link, res == 0 ? strlen(link) : 0);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000805}
806
807static void do_getdir(struct fuse *f, struct fuse_in_header *in)
808{
809 int res;
810 struct fuse_getdir_out arg;
Miklos Szeredia181e612001-11-06 12:03:23 +0000811 struct fuse_dirhandle dh;
Miklos Szeredib483c932001-10-29 14:57:57 +0000812 char *path;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000813
Miklos Szeredib483c932001-10-29 14:57:57 +0000814 dh.fuse = f;
815 dh.fp = tmpfile();
Miklos Szeredia13d9002004-11-02 17:32:03 +0000816 dh.dir = in->nodeid;
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000817
Miklos Szeredi65afea12004-09-14 07:13:45 +0000818 res = -EIO;
819 if (dh.fp == NULL)
820 perror("fuse: failed to create temporary file");
821 else {
822 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000823 path = get_path(f, in->nodeid);
Miklos Szeredi65afea12004-09-14 07:13:45 +0000824 if (path != NULL) {
825 res = -ENOSYS;
826 if (f->op.getdir)
Miklos Szeredi8fb48fe2004-11-08 14:48:52 +0000827 res = f->op.getdir(path, &dh, fill_dir);
Miklos Szeredi65afea12004-09-14 07:13:45 +0000828 free(path);
829 }
830 fflush(dh.fp);
831 }
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000832 memset(&arg, 0, sizeof(struct fuse_getdir_out));
Miklos Szeredi65afea12004-09-14 07:13:45 +0000833 if (res == 0)
834 arg.fd = fileno(dh.fp);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000835 send_reply(f, in, res, &arg, sizeof(arg));
Miklos Szeredi65afea12004-09-14 07:13:45 +0000836 if (dh.fp != NULL)
837 fclose(dh.fp);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000838}
839
Miklos Szeredib483c932001-10-29 14:57:57 +0000840static void do_mknod(struct fuse *f, struct fuse_in_header *in,
841 struct fuse_mknod_in *inarg)
842{
843 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000844 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000845 char *path;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000846 char *name = PARAM(inarg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000847 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000848
Miklos Szeredi5e183482001-10-31 14:52:35 +0000849 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000850 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000851 if (path != NULL) {
852 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000853 printf("MKNOD %s\n", path);
854 fflush(stdout);
855 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000856 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000857 if (f->op.mknod && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000858 res = f->op.mknod(path, inarg->mode, inarg->rdev);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000859 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000860 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000861 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000862 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000863 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000864 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
865 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000866 forget_node(f, outarg.nodeid, in->unique);
Miklos Szeredib483c932001-10-29 14:57:57 +0000867}
868
869static void do_mkdir(struct fuse *f, struct fuse_in_header *in,
870 struct fuse_mkdir_in *inarg)
871{
872 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000873 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000874 char *path;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000875 char *name = PARAM(inarg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000876 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000877
Miklos Szeredi5e183482001-10-31 14:52:35 +0000878 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000879 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000880 if (path != NULL) {
881 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000882 printf("MKDIR %s\n", path);
883 fflush(stdout);
884 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000885 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000886 if (f->op.mkdir && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000887 res = f->op.mkdir(path, inarg->mode);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000888 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000889 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000890 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000891 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000892 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000893 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
894 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000895 forget_node(f, outarg.nodeid, in->unique);
Miklos Szeredib483c932001-10-29 14:57:57 +0000896}
897
Miklos Szeredib5958612004-02-20 14:10:49 +0000898static void do_unlink(struct fuse *f, struct fuse_in_header *in, char *name)
Miklos Szeredib483c932001-10-29 14:57:57 +0000899{
900 int res;
901 char *path;
902
Miklos Szeredi5e183482001-10-31 14:52:35 +0000903 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000904 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000905 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000906 if (f->flags & FUSE_DEBUG) {
907 printf("UNLINK %s\n", path);
908 fflush(stdout);
909 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000910 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000911 if (f->op.unlink) {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000912 if (!(f->flags & FUSE_HARD_REMOVE) && is_open(f, in->nodeid, name))
913 res = hide_node(f, path, in->nodeid, name);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000914 else {
915 res = f->op.unlink(path);
916 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000917 remove_node(f, in->nodeid, name);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000918
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000919 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000920 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000921 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000922 }
Miklos Szeredib5958612004-02-20 14:10:49 +0000923 send_reply(f, in, res, NULL, 0);
924}
925
926static void do_rmdir(struct fuse *f, struct fuse_in_header *in, char *name)
927{
928 int res;
929 char *path;
930
931 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000932 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000933 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000934 if (f->flags & FUSE_DEBUG) {
935 printf("RMDIR %s\n", path);
936 fflush(stdout);
937 }
Miklos Szeredib5958612004-02-20 14:10:49 +0000938 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000939 if (f->op.rmdir) {
Miklos Szeredib5958612004-02-20 14:10:49 +0000940 res = f->op.rmdir(path);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000941 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000942 remove_node(f, in->nodeid, name);
Miklos Szeredib5958612004-02-20 14:10:49 +0000943 }
944 free(path);
945 }
Miklos Szeredib483c932001-10-29 14:57:57 +0000946 send_reply(f, in, res, NULL, 0);
947}
948
949static void do_symlink(struct fuse *f, struct fuse_in_header *in, char *name,
950 char *link)
951{
952 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000953 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000954 char *path;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000955 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000956
Miklos Szeredi5e183482001-10-31 14:52:35 +0000957 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000958 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000959 if (path != NULL) {
960 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000961 printf("SYMLINK %s\n", path);
962 fflush(stdout);
963 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000964 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000965 if (f->op.symlink && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000966 res = f->op.symlink(link, path);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000967 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000968 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000969 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000970 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000971 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000972 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
973 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000974 forget_node(f, outarg.nodeid, in->unique);
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000975
Miklos Szeredib483c932001-10-29 14:57:57 +0000976}
977
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000978static void do_rename(struct fuse *f, struct fuse_in_header *in,
979 struct fuse_rename_in *inarg)
980{
981 int res;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000982 nodeid_t olddir = in->nodeid;
983 nodeid_t newdir = inarg->newdir;
Miklos Szeredi6bf8b682002-10-28 08:49:39 +0000984 char *oldname = PARAM(inarg);
985 char *newname = oldname + strlen(oldname) + 1;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000986 char *oldpath;
987 char *newpath;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000988
Miklos Szeredi5e183482001-10-31 14:52:35 +0000989 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000990 oldpath = get_path_name(f, olddir, oldname);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000991 if (oldpath != NULL) {
Miklos Szeredia181e612001-11-06 12:03:23 +0000992 newpath = get_path_name(f, newdir, newname);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000993 if (newpath != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000994 if (f->flags & FUSE_DEBUG) {
995 printf("RENAME %s -> %s\n", oldpath, newpath);
996 fflush(stdout);
997 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000998 res = -ENOSYS;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000999 if (f->op.rename) {
1000 res = 0;
Miklos Szeredi2529ca22004-07-13 15:36:52 +00001001 if (!(f->flags & FUSE_HARD_REMOVE) &&
1002 is_open(f, newdir, newname))
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001003 res = hide_node(f, newpath, newdir, newname);
1004 if (res == 0) {
1005 res = f->op.rename(oldpath, newpath);
1006 if (res == 0)
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001007 res = rename_node(f, olddir, oldname, newdir, newname, 0);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001008 }
1009 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001010 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001011 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001012 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001013 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001014 send_reply(f, in, res, NULL, 0);
1015}
1016
1017static void do_link(struct fuse *f, struct fuse_in_header *in,
Miklos Szeredi5e183482001-10-31 14:52:35 +00001018 struct fuse_link_in *arg)
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001019{
1020 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +00001021 int res2;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001022 char *oldpath;
1023 char *newpath;
Miklos Szeredi76f65782004-02-19 16:55:40 +00001024 char *name = PARAM(arg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +00001025 struct fuse_entry_out outarg;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001026
Miklos Szeredi5e183482001-10-31 14:52:35 +00001027 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001028 oldpath = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001029 if (oldpath != NULL) {
Miklos Szeredi76f65782004-02-19 16:55:40 +00001030 newpath = get_path_name(f, arg->newdir, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001031 if (newpath != NULL) {
1032 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +00001033 printf("LINK %s\n", newpath);
1034 fflush(stdout);
1035 }
Miklos Szeredi5e183482001-10-31 14:52:35 +00001036 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001037 if (f->op.link && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +00001038 res = f->op.link(oldpath, newpath);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001039 if (res == 0)
Miklos Szeredi76f65782004-02-19 16:55:40 +00001040 res = lookup_path(f, arg->newdir, in->unique, name,
1041 newpath, &outarg);
1042 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001043 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001044 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001045 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001046 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +00001047 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
1048 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001049 forget_node(f, outarg.nodeid, in->unique);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001050}
1051
Miklos Szeredi5e183482001-10-31 14:52:35 +00001052static void do_open(struct fuse *f, struct fuse_in_header *in,
1053 struct fuse_open_in *arg)
1054{
1055 int res;
1056 char *path;
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001057 struct fuse_open_out outarg;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001058 struct fuse_file_info fi;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001059
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001060 memset(&fi, 0, sizeof(fi));
1061 fi.flags = arg->flags;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001062 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001063 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001064 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +00001065 res = -ENOSYS;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001066 if (f->op.open) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001067 if (!f->compat)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001068 res = f->op.open(path, &fi);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001069 else
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001070 res = ((struct fuse_operations_compat2 *) &f->op)->open(path, fi.flags);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001071 }
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +00001072 }
Miklos Szeredi73798f92004-07-12 15:55:11 +00001073 if (res == 0) {
1074 int res2;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001075 outarg.fh = fi.fh;
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001076 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001077 printf("OPEN[%lu] flags: 0x%x\n", fi.fh, arg->flags);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001078 fflush(stdout);
1079 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001080
1081 pthread_mutex_lock(&f->lock);
1082 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001083 if(res2 == -ENOENT) {
1084 /* The open syscall was interrupted, so it must be cancelled */
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001085 if(f->op.release) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001086 if (!f->compat)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001087 f->op.release(path, &fi);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001088 else
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001089 ((struct fuse_operations_compat2 *) &f->op)->release(path, fi.flags);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001090 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001091 } else {
1092 struct node *node = get_node(f, in->nodeid);
1093 node->open_count ++;
1094 }
Miklos Szeredi73798f92004-07-12 15:55:11 +00001095 pthread_mutex_unlock(&f->lock);
Miklos Szeredi73798f92004-07-12 15:55:11 +00001096 } else
1097 send_reply(f, in, res, NULL, 0);
1098
Miklos Szeredi9a31cca2004-06-26 21:11:25 +00001099 if (path)
1100 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001101}
1102
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001103static void do_flush(struct fuse *f, struct fuse_in_header *in,
1104 struct fuse_flush_in *arg)
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001105{
1106 char *path;
1107 int res;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001108 struct fuse_file_info fi;
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001109
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001110 memset(&fi, 0, sizeof(fi));
1111 fi.fh = arg->fh;
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001112 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001113 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001114 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001115 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001116 printf("FLUSH[%lu]\n", (unsigned long) arg->fh);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001117 fflush(stdout);
1118 }
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001119 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001120 if (f->op.flush)
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001121 res = f->op.flush(path, &fi);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001122 free(path);
1123 }
1124 send_reply(f, in, res, NULL, 0);
1125}
1126
Miklos Szeredi9478e862002-12-11 09:50:26 +00001127static void do_release(struct fuse *f, struct fuse_in_header *in,
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001128 struct fuse_release_in *arg)
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001129{
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001130 struct node *node;
1131 char *path;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001132 struct fuse_file_info fi;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001133 int unlink_hidden;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001134
1135 memset(&fi, 0, sizeof(fi));
1136 fi.flags = arg->flags;
1137 fi.fh = arg->fh;
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001138
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001139 pthread_mutex_lock(&f->lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001140 node = get_node(f, in->nodeid);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001141 assert(node->open_count > 0);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001142 --node->open_count;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001143 unlink_hidden = (node->is_hidden && !node->open_count);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001144 pthread_mutex_unlock(&f->lock);
1145
Miklos Szeredia13d9002004-11-02 17:32:03 +00001146 path = get_path(f, in->nodeid);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001147 if (f->flags & FUSE_DEBUG) {
1148 printf("RELEASE[%lu] flags: 0x%x\n", fi.fh, fi.flags);
1149 fflush(stdout);
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001150 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001151 if (f->op.release) {
1152 if (!f->compat)
1153 f->op.release(path ? path : "-", &fi);
1154 else if (path)
1155 ((struct fuse_operations_compat2 *) &f->op)->release(path, fi.flags);
1156 }
1157
1158 if(unlink_hidden && path)
1159 f->op.unlink(path);
1160
1161 if (path)
1162 free(path);
1163
Miklos Szeredi556d03d2004-06-30 11:13:41 +00001164 send_reply(f, in, 0, NULL, 0);
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001165}
1166
Miklos Szeredi5e183482001-10-31 14:52:35 +00001167static void do_read(struct fuse *f, struct fuse_in_header *in,
1168 struct fuse_read_in *arg)
1169{
1170 int res;
1171 char *path;
Miklos Szeredi43696432001-11-18 19:15:05 +00001172 char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + arg->size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001173 if (outbuf == NULL)
1174 send_reply(f, in, -ENOMEM, NULL, 0);
1175 else {
1176 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
1177 char *buf = outbuf + sizeof(struct fuse_out_header);
1178 size_t size;
1179 size_t outsize;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001180 struct fuse_file_info fi;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001181
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001182 memset(&fi, 0, sizeof(fi));
1183 fi.fh = arg->fh;
1184
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001185 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001186 path = get_path(f, in->nodeid);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001187 if (path != NULL) {
1188 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001189 printf("READ[%lu] %u bytes from %llu\n",
1190 (unsigned long) arg->fh, arg->size, arg->offset);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001191 fflush(stdout);
1192 }
1193
1194 res = -ENOSYS;
1195 if (f->op.read)
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001196 res = f->op.read(path, buf, arg->size, arg->offset, &fi);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001197 free(path);
Miklos Szeredi6ebe2342002-01-06 21:44:16 +00001198 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001199
1200 size = 0;
1201 if (res >= 0) {
1202 size = res;
1203 res = 0;
1204 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001205 printf(" READ[%lu] %u bytes\n", (unsigned long) arg->fh,
1206 size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001207 fflush(stdout);
1208 }
Miklos Szeredi6ebe2342002-01-06 21:44:16 +00001209 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001210 memset(out, 0, sizeof(struct fuse_out_header));
1211 out->unique = in->unique;
1212 out->error = res;
1213 outsize = sizeof(struct fuse_out_header) + size;
1214
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001215 send_reply_raw(f, outbuf, outsize);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001216 free(outbuf);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001217 }
Miklos Szeredi5e183482001-10-31 14:52:35 +00001218}
Miklos Szeredib483c932001-10-29 14:57:57 +00001219
Miklos Szeredia181e612001-11-06 12:03:23 +00001220static void do_write(struct fuse *f, struct fuse_in_header *in,
1221 struct fuse_write_in *arg)
1222{
1223 int res;
1224 char *path;
Miklos Szerediad051c32004-07-02 09:22:50 +00001225 struct fuse_write_out outarg;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001226 struct fuse_file_info fi;
1227
1228 memset(&fi, 0, sizeof(fi));
1229 fi.fh = arg->fh;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001230 fi.writepage = arg->write_flags & 1;
Miklos Szeredia181e612001-11-06 12:03:23 +00001231
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001232 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001233 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001234 if (path != NULL) {
1235 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi1eea0322004-09-27 18:50:11 +00001236 printf("WRITE%s[%lu] %u bytes to %llu\n",
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001237 (arg->write_flags & 1) ? "PAGE" : "",
1238 (unsigned long) arg->fh, arg->size, arg->offset);
Miklos Szeredi6ebe2342002-01-06 21:44:16 +00001239 fflush(stdout);
1240 }
1241
Miklos Szeredia181e612001-11-06 12:03:23 +00001242 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001243 if (f->op.write)
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001244 res = f->op.write(path, PARAM(arg), arg->size, arg->offset, &fi);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001245 free(path);
Miklos Szeredia181e612001-11-06 12:03:23 +00001246 }
1247
Miklos Szerediad051c32004-07-02 09:22:50 +00001248 if (res >= 0) {
1249 outarg.size = res;
1250 res = 0;
Miklos Szeredia181e612001-11-06 12:03:23 +00001251 }
1252
Miklos Szerediad051c32004-07-02 09:22:50 +00001253 send_reply(f, in, res, &outarg, sizeof(outarg));
Miklos Szeredia181e612001-11-06 12:03:23 +00001254}
1255
Miklos Szeredi77f39942004-03-25 11:17:52 +00001256static int default_statfs(struct statfs *buf)
1257{
1258 buf->f_namelen = 255;
1259 buf->f_bsize = 512;
1260 return 0;
1261}
1262
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001263static void convert_statfs_compat(struct fuse_statfs_compat1 *compatbuf,
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001264 struct statfs *statfs)
1265{
1266 statfs->f_bsize = compatbuf->block_size;
1267 statfs->f_blocks = compatbuf->blocks;
1268 statfs->f_bfree = compatbuf->blocks_free;
1269 statfs->f_bavail = compatbuf->blocks_free;
1270 statfs->f_files = compatbuf->files;
1271 statfs->f_ffree = compatbuf->files_free;
1272 statfs->f_namelen = compatbuf->namelen;
1273}
1274
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001275static void convert_statfs(struct statfs *statfs, struct fuse_kstatfs *kstatfs)
1276{
1277 kstatfs->bsize = statfs->f_bsize;
1278 kstatfs->blocks = statfs->f_blocks;
1279 kstatfs->bfree = statfs->f_bfree;
1280 kstatfs->bavail = statfs->f_bavail;
1281 kstatfs->files = statfs->f_files;
1282 kstatfs->ffree = statfs->f_ffree;
1283 kstatfs->namelen = statfs->f_namelen;
1284}
1285
Mark Glinesd84b39a2002-01-07 16:32:02 +00001286static void do_statfs(struct fuse *f, struct fuse_in_header *in)
1287{
1288 int res;
Mark Glinesd84b39a2002-01-07 16:32:02 +00001289 struct fuse_statfs_out arg;
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001290 struct statfs buf;
Mark Glinesd84b39a2002-01-07 16:32:02 +00001291
Miklos Szeredi77f39942004-03-25 11:17:52 +00001292 memset(&buf, 0, sizeof(struct statfs));
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001293 if (f->op.statfs) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001294 if (!f->compat || f->compat > 11)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001295 res = f->op.statfs("/", &buf);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001296 else {
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001297 struct fuse_statfs_compat1 compatbuf;
1298 memset(&compatbuf, 0, sizeof(struct fuse_statfs_compat1));
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001299 res = ((struct fuse_operations_compat1 *) &f->op)->statfs(&compatbuf);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001300 if (res == 0)
1301 convert_statfs_compat(&compatbuf, &buf);
1302 }
1303 }
Miklos Szeredi77f39942004-03-25 11:17:52 +00001304 else
1305 res = default_statfs(&buf);
1306
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001307 if (res == 0)
Miklos Szeredi77f39942004-03-25 11:17:52 +00001308 convert_statfs(&buf, &arg.st);
Miklos Szeredi4b2bef42002-01-09 12:23:27 +00001309
Mark Glinesd84b39a2002-01-07 16:32:02 +00001310 send_reply(f, in, res, &arg, sizeof(arg));
1311}
1312
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001313static void do_fsync(struct fuse *f, struct fuse_in_header *in,
1314 struct fuse_fsync_in *inarg)
1315{
1316 int res;
1317 char *path;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001318 struct fuse_file_info fi;
1319
1320 memset(&fi, 0, sizeof(fi));
1321 fi.fh = inarg->fh;
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001322
1323 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001324 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001325 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001326 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001327 printf("FSYNC[%lu]\n", (unsigned long) inarg->fh);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001328 fflush(stdout);
1329 }
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001330 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001331 if (f->op.fsync)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001332 res = f->op.fsync(path, inarg->fsync_flags & 1, &fi);
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001333 free(path);
1334 }
1335 send_reply(f, in, res, NULL, 0);
1336}
1337
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001338static void do_setxattr(struct fuse *f, struct fuse_in_header *in,
1339 struct fuse_setxattr_in *arg)
1340{
1341 int res;
1342 char *path;
1343 char *name = PARAM(arg);
1344 unsigned char *value = name + strlen(name) + 1;
1345
1346 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001347 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001348 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001349 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001350 if (f->op.setxattr)
1351 res = f->op.setxattr(path, name, value, arg->size, arg->flags);
1352 free(path);
1353 }
1354 send_reply(f, in, res, NULL, 0);
1355}
1356
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001357static int common_getxattr(struct fuse *f, struct fuse_in_header *in,
1358 const char *name, char *value, size_t size)
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001359{
1360 int res;
1361 char *path;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001362
1363 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001364 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001365 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001366 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001367 if (f->op.getxattr)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001368 res = f->op.getxattr(path, name, value, size);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001369 free(path);
1370 }
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001371 return res;
1372}
1373
1374static void do_getxattr_read(struct fuse *f, struct fuse_in_header *in,
1375 const char *name, size_t size)
1376{
1377 int res;
1378 char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001379 if (outbuf == NULL)
1380 send_reply(f, in, -ENOMEM, NULL, 0);
1381 else {
1382 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
1383 char *value = outbuf + sizeof(struct fuse_out_header);
1384
1385 res = common_getxattr(f, in, name, value, size);
1386 size = 0;
1387 if (res > 0) {
1388 size = res;
1389 res = 0;
1390 }
1391 memset(out, 0, sizeof(struct fuse_out_header));
1392 out->unique = in->unique;
1393 out->error = res;
1394
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001395 send_reply_raw(f, outbuf, sizeof(struct fuse_out_header) + size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001396 free(outbuf);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001397 }
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001398}
1399
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001400static void do_getxattr_size(struct fuse *f, struct fuse_in_header *in,
1401 const char *name)
1402{
1403 int res;
1404 struct fuse_getxattr_out arg;
1405
1406 res = common_getxattr(f, in, name, NULL, 0);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001407 if (res >= 0) {
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001408 arg.size = res;
1409 res = 0;
1410 }
1411 send_reply(f, in, res, &arg, sizeof(arg));
1412}
1413
1414static void do_getxattr(struct fuse *f, struct fuse_in_header *in,
1415 struct fuse_getxattr_in *arg)
1416{
1417 char *name = PARAM(arg);
1418
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001419 if (arg->size)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001420 do_getxattr_read(f, in, name, arg->size);
1421 else
1422 do_getxattr_size(f, in, name);
1423}
1424
1425static int common_listxattr(struct fuse *f, struct fuse_in_header *in,
1426 char *list, size_t size)
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001427{
1428 int res;
1429 char *path;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001430
1431 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001432 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001433 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001434 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001435 if (f->op.listxattr)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001436 res = f->op.listxattr(path, list, size);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001437 free(path);
1438 }
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001439 return res;
1440}
1441
1442static void do_listxattr_read(struct fuse *f, struct fuse_in_header *in,
1443 size_t size)
1444{
1445 int res;
1446 char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001447 if (outbuf == NULL)
1448 send_reply(f, in, -ENOMEM, NULL, 0);
1449 else {
1450 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
1451 char *list = outbuf + sizeof(struct fuse_out_header);
1452
1453 res = common_listxattr(f, in, list, size);
1454 size = 0;
1455 if (res > 0) {
1456 size = res;
1457 res = 0;
1458 }
1459 memset(out, 0, sizeof(struct fuse_out_header));
1460 out->unique = in->unique;
1461 out->error = res;
1462
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001463 send_reply_raw(f, outbuf, sizeof(struct fuse_out_header) + size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001464 free(outbuf);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001465 }
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001466}
1467
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001468static void do_listxattr_size(struct fuse *f, struct fuse_in_header *in)
1469{
1470 int res;
1471 struct fuse_getxattr_out arg;
1472
1473 res = common_listxattr(f, in, NULL, 0);
1474 if (res >= 0) {
1475 arg.size = res;
1476 res = 0;
1477 }
1478 send_reply(f, in, res, &arg, sizeof(arg));
1479}
1480
1481static void do_listxattr(struct fuse *f, struct fuse_in_header *in,
1482 struct fuse_getxattr_in *arg)
1483{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001484 if (arg->size)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001485 do_listxattr_read(f, in, arg->size);
1486 else
1487 do_listxattr_size(f, in);
1488}
1489
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001490static void do_removexattr(struct fuse *f, struct fuse_in_header *in,
1491 char *name)
1492{
1493 int res;
1494 char *path;
1495
1496 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001497 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001498 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001499 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001500 if (f->op.removexattr)
1501 res = f->op.removexattr(path, name);
1502 free(path);
1503 }
1504 send_reply(f, in, res, NULL, 0);
1505}
1506
1507
Miklos Szeredi43696432001-11-18 19:15:05 +00001508static void free_cmd(struct fuse_cmd *cmd)
1509{
1510 free(cmd->buf);
1511 free(cmd);
1512}
1513
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001514void fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd)
Miklos Szeredia181e612001-11-06 12:03:23 +00001515{
Miklos Szeredia181e612001-11-06 12:03:23 +00001516 struct fuse_in_header *in = (struct fuse_in_header *) cmd->buf;
1517 void *inarg = cmd->buf + sizeof(struct fuse_in_header);
1518 size_t argsize;
Miklos Szeredid169f312004-09-22 08:48:26 +00001519 struct fuse_context *ctx = fuse_get_context();
Miklos Szeredia181e612001-11-06 12:03:23 +00001520
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001521 fuse_dec_avail(f);
Miklos Szeredi33232032001-11-19 17:55:51 +00001522
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001523 if ((f->flags & FUSE_DEBUG)) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001524 printf("unique: %llu, opcode: %s (%i), nodeid: %lu, insize: %i\n",
1525 in->unique, opname(in->opcode), in->opcode,
1526 (unsigned long) in->nodeid, cmd->buflen);
Miklos Szeredic0938ea2001-11-07 12:35:06 +00001527 fflush(stdout);
1528 }
Miklos Szeredife25def2001-12-20 15:38:05 +00001529
Miklos Szeredid169f312004-09-22 08:48:26 +00001530 ctx->fuse = f;
Miklos Szeredife25def2001-12-20 15:38:05 +00001531 ctx->uid = in->uid;
1532 ctx->gid = in->gid;
Miklos Szeredi1f18db52004-09-27 06:54:49 +00001533 ctx->pid = in->pid;
Miklos Szeredia181e612001-11-06 12:03:23 +00001534
1535 argsize = cmd->buflen - sizeof(struct fuse_in_header);
1536
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001537 switch (in->opcode) {
Miklos Szeredia181e612001-11-06 12:03:23 +00001538 case FUSE_LOOKUP:
1539 do_lookup(f, in, (char *) inarg);
1540 break;
1541
Miklos Szeredia181e612001-11-06 12:03:23 +00001542 case FUSE_GETATTR:
1543 do_getattr(f, in);
1544 break;
1545
1546 case FUSE_SETATTR:
1547 do_setattr(f, in, (struct fuse_setattr_in *) inarg);
1548 break;
1549
1550 case FUSE_READLINK:
1551 do_readlink(f, in);
1552 break;
1553
1554 case FUSE_GETDIR:
1555 do_getdir(f, in);
1556 break;
1557
1558 case FUSE_MKNOD:
1559 do_mknod(f, in, (struct fuse_mknod_in *) inarg);
1560 break;
1561
1562 case FUSE_MKDIR:
1563 do_mkdir(f, in, (struct fuse_mkdir_in *) inarg);
1564 break;
1565
1566 case FUSE_UNLINK:
Miklos Szeredib5958612004-02-20 14:10:49 +00001567 do_unlink(f, in, (char *) inarg);
1568 break;
1569
Miklos Szeredia181e612001-11-06 12:03:23 +00001570 case FUSE_RMDIR:
Miklos Szeredib5958612004-02-20 14:10:49 +00001571 do_rmdir(f, in, (char *) inarg);
Miklos Szeredia181e612001-11-06 12:03:23 +00001572 break;
1573
1574 case FUSE_SYMLINK:
1575 do_symlink(f, in, (char *) inarg,
1576 ((char *) inarg) + strlen((char *) inarg) + 1);
1577 break;
1578
1579 case FUSE_RENAME:
1580 do_rename(f, in, (struct fuse_rename_in *) inarg);
1581 break;
1582
1583 case FUSE_LINK:
1584 do_link(f, in, (struct fuse_link_in *) inarg);
1585 break;
1586
1587 case FUSE_OPEN:
1588 do_open(f, in, (struct fuse_open_in *) inarg);
1589 break;
1590
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001591 case FUSE_FLUSH:
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001592 do_flush(f, in, (struct fuse_flush_in *) inarg);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001593 break;
1594
Miklos Szeredi9478e862002-12-11 09:50:26 +00001595 case FUSE_RELEASE:
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001596 do_release(f, in, (struct fuse_release_in *) inarg);
Miklos Szeredi9478e862002-12-11 09:50:26 +00001597 break;
1598
Miklos Szeredia181e612001-11-06 12:03:23 +00001599 case FUSE_READ:
1600 do_read(f, in, (struct fuse_read_in *) inarg);
1601 break;
1602
1603 case FUSE_WRITE:
1604 do_write(f, in, (struct fuse_write_in *) inarg);
1605 break;
1606
Mark Glinesd84b39a2002-01-07 16:32:02 +00001607 case FUSE_STATFS:
1608 do_statfs(f, in);
1609 break;
1610
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001611 case FUSE_FSYNC:
1612 do_fsync(f, in, (struct fuse_fsync_in *) inarg);
1613 break;
1614
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001615 case FUSE_SETXATTR:
1616 do_setxattr(f, in, (struct fuse_setxattr_in *) inarg);
1617 break;
1618
1619 case FUSE_GETXATTR:
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001620 do_getxattr(f, in, (struct fuse_getxattr_in *) inarg);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001621 break;
1622
1623 case FUSE_LISTXATTR:
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001624 do_listxattr(f, in, (struct fuse_getxattr_in *) inarg);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001625 break;
1626
1627 case FUSE_REMOVEXATTR:
1628 do_removexattr(f, in, (char *) inarg);
1629 break;
1630
Miklos Szeredia181e612001-11-06 12:03:23 +00001631 default:
Miklos Szeredi43696432001-11-18 19:15:05 +00001632 send_reply(f, in, -ENOSYS, NULL, 0);
Miklos Szeredia181e612001-11-06 12:03:23 +00001633 }
Miklos Szeredi43696432001-11-18 19:15:05 +00001634
1635 free_cmd(cmd);
Miklos Szeredia181e612001-11-06 12:03:23 +00001636}
1637
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001638int fuse_exited(struct fuse* f)
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001639{
1640 return f->exited;
1641}
1642
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001643struct fuse_cmd *fuse_read_cmd(struct fuse *f)
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001644{
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001645 ssize_t res;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001646 struct fuse_cmd *cmd;
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001647 struct fuse_in_header *in;
1648 void *inarg;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001649
Miklos Szeredi43696432001-11-18 19:15:05 +00001650 cmd = (struct fuse_cmd *) malloc(sizeof(struct fuse_cmd));
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001651 if (cmd == NULL) {
1652 fprintf(stderr, "fuse: failed to allocate cmd in read\n");
1653 return NULL;
1654 }
Miklos Szeredi43696432001-11-18 19:15:05 +00001655 cmd->buf = (char *) malloc(FUSE_MAX_IN);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001656 if (cmd->buf == NULL) {
1657 fprintf(stderr, "fuse: failed to allocate read buffer\n");
1658 free(cmd);
1659 return NULL;
1660 }
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001661 in = (struct fuse_in_header *) cmd->buf;
1662 inarg = cmd->buf + sizeof(struct fuse_in_header);
Miklos Szeredi43696432001-11-18 19:15:05 +00001663
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001664 res = read(f->fd, cmd->buf, FUSE_MAX_IN);
1665 if (res == -1) {
1666 free_cmd(cmd);
Miklos Szeredie56818b2004-12-12 11:45:24 +00001667 if (fuse_exited(f) || errno == EINTR || errno == ENOENT)
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001668 return NULL;
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001669
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001670 /* ENODEV means we got unmounted, so we silenty return failure */
1671 if (errno != ENODEV) {
1672 /* BAD... This will happen again */
1673 perror("fuse: reading device");
1674 }
1675
1676 fuse_exit(f);
1677 return NULL;
1678 }
1679 if ((size_t) res < sizeof(struct fuse_in_header)) {
1680 free_cmd(cmd);
1681 /* Cannot happen */
1682 fprintf(stderr, "short read on fuse device\n");
1683 fuse_exit(f);
1684 return NULL;
1685 }
1686 cmd->buflen = res;
1687
1688 /* Forget is special, it can be done without messing with threads. */
1689 if (in->opcode == FUSE_FORGET) {
1690 do_forget(f, in, (struct fuse_forget_in *) inarg);
1691 free_cmd(cmd);
1692 return NULL;
1693 }
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001694
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001695 return cmd;
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001696}
1697
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001698int fuse_loop(struct fuse *f)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001699{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001700 if (f == NULL)
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001701 return -1;
Miklos Szeredic40748a2004-02-20 16:38:45 +00001702
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001703 while (1) {
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001704 struct fuse_cmd *cmd;
1705
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001706 if (fuse_exited(f))
Miklos Szeredi874e3c12004-11-01 23:15:20 +00001707 break;
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001708
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001709 cmd = fuse_read_cmd(f);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001710 if (cmd == NULL)
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001711 continue;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001712
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001713 fuse_process_cmd(f, cmd);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001714 }
Miklos Szeredi874e3c12004-11-01 23:15:20 +00001715 f->exited = 0;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001716 return 0;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001717}
1718
Miklos Szeredi891b8742004-07-29 09:27:49 +00001719int fuse_invalidate(struct fuse *f, const char *path)
1720{
Miklos Szeredie56818b2004-12-12 11:45:24 +00001721 (void) f;
1722 (void) path;
1723 return -EINVAL;
Miklos Szeredi891b8742004-07-29 09:27:49 +00001724}
1725
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001726void fuse_exit(struct fuse *f)
1727{
1728 f->exited = 1;
1729}
1730
Miklos Szeredid169f312004-09-22 08:48:26 +00001731struct fuse_context *fuse_get_context()
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001732{
Miklos Szeredid169f312004-09-22 08:48:26 +00001733 static struct fuse_context context;
1734 if (fuse_getcontext)
1735 return fuse_getcontext();
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001736 else
Miklos Szeredid169f312004-09-22 08:48:26 +00001737 return &context;
1738}
1739
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001740void fuse_set_getcontext_func(struct fuse_context *(*func)(void))
Miklos Szeredid169f312004-09-22 08:48:26 +00001741{
1742 fuse_getcontext = func;
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001743}
1744
Miklos Szeredic40748a2004-02-20 16:38:45 +00001745static int check_version(struct fuse *f)
1746{
1747 int res;
Miklos Szeredi162bcbb2004-11-29 23:43:44 +00001748 const char *version_file = FUSE_VERSION_FILE_NEW;
Miklos Szeredif3845c42004-11-20 11:18:34 +00001749 FILE *vf = fopen(version_file, "r");
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001750 if (vf == NULL) {
Miklos Szeredia25d4c22004-11-23 22:32:16 +00001751 version_file = FUSE_VERSION_FILE_OLD;
Miklos Szeredif3845c42004-11-20 11:18:34 +00001752 vf = fopen(version_file, "r");
1753 if (vf == NULL) {
Miklos Szeredia25d4c22004-11-23 22:32:16 +00001754 struct stat tmp;
1755 if (stat(FUSE_DEV_OLD, &tmp) != -1) {
1756 fprintf(stderr, "fuse: kernel interface too old, need >= %i.%i\n",
1757 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
1758 return -1;
1759 } else {
1760 fprintf(stderr, "fuse: warning: version of kernel interface unknown\n");
1761 return 0;
1762 }
Miklos Szeredif3845c42004-11-20 11:18:34 +00001763 }
Miklos Szeredic40748a2004-02-20 16:38:45 +00001764 }
1765 res = fscanf(vf, "%i.%i", &f->majorver, &f->minorver);
1766 fclose(vf);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001767 if (res != 2) {
Miklos Szeredif3845c42004-11-20 11:18:34 +00001768 fprintf(stderr, "fuse: error reading %s\n", version_file);
Miklos Szeredic40748a2004-02-20 16:38:45 +00001769 return -1;
1770 }
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001771 if (f->majorver != FUSE_KERNEL_VERSION) {
Miklos Szeredic40748a2004-02-20 16:38:45 +00001772 fprintf(stderr, "fuse: bad kernel interface major version: needs %i\n",
1773 FUSE_KERNEL_VERSION);
1774 return -1;
1775 }
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001776 if (f->minorver < FUSE_KERNEL_MINOR_VERSION_NEED) {
Miklos Szeredi256739a2004-11-20 12:22:37 +00001777 fprintf(stderr, "fuse: kernel interface too old: need >= %i.%i\n",
Miklos Szeredic40748a2004-02-20 16:38:45 +00001778 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
1779 return -1;
1780 }
1781
1782 return 0;
1783}
1784
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001785
1786int fuse_is_lib_option(const char *opt)
1787{
1788 if (strcmp(opt, "debug") == 0 ||
Miklos Szeredia13d9002004-11-02 17:32:03 +00001789 strcmp(opt, "hard_remove") == 0 ||
1790 strcmp(opt, "use_ino") == 0)
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001791 return 1;
1792 else
1793 return 0;
1794}
1795
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001796static int parse_lib_opts(struct fuse *f, const char *opts)
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001797{
1798 if (opts) {
1799 char *xopts = strdup(opts);
1800 char *s = xopts;
1801 char *opt;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001802
Miklos Szeredie56818b2004-12-12 11:45:24 +00001803 if (xopts == NULL) {
1804 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001805 return -1;
Miklos Szeredie56818b2004-12-12 11:45:24 +00001806 }
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001807
1808 while((opt = strsep(&s, ","))) {
1809 if (strcmp(opt, "debug") == 0)
1810 f->flags |= FUSE_DEBUG;
1811 else if (strcmp(opt, "hard_remove") == 0)
1812 f->flags |= FUSE_HARD_REMOVE;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001813 else if (strcmp(opt, "use_ino") == 0)
1814 f->flags |= FUSE_USE_INO;
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001815 else
1816 fprintf(stderr, "fuse: warning: unknown option `%s'\n", opt);
1817 }
1818 free(xopts);
1819 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001820 return 0;
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001821}
1822
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001823struct fuse *fuse_new_common(int fd, const char *opts,
1824 const struct fuse_operations *op,
1825 size_t op_size, int compat)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001826{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001827 struct fuse *f;
1828 struct node *root;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001829
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001830 if (sizeof(struct fuse_operations) < op_size) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001831 fprintf(stderr, "fuse: warning: library too old, some operations may not not work\n");
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001832 op_size = sizeof(struct fuse_operations);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001833 }
1834
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001835 f = (struct fuse *) calloc(1, sizeof(struct fuse));
Miklos Szeredie56818b2004-12-12 11:45:24 +00001836 if (f == NULL) {
1837 fprintf(stderr, "fuse: failed to allocate fuse object\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001838 goto out;
Miklos Szeredie56818b2004-12-12 11:45:24 +00001839 }
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001840
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001841 if (check_version(f) == -1)
1842 goto out_free;
Miklos Szeredic40748a2004-02-20 16:38:45 +00001843
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001844 if (parse_lib_opts(f, opts) == -1)
1845 goto out_free;
1846
Miklos Szeredi8cffdb92001-11-09 14:49:18 +00001847 f->fd = fd;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001848 f->ctr = 0;
Miklos Szeredi76f65782004-02-19 16:55:40 +00001849 f->generation = 0;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001850 /* FIXME: Dynamic hash table */
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001851 f->name_table_size = 14057;
1852 f->name_table = (struct node **)
1853 calloc(1, sizeof(struct node *) * f->name_table_size);
Miklos Szeredie56818b2004-12-12 11:45:24 +00001854 if (f->name_table == NULL) {
1855 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001856 goto out_free;
Miklos Szeredie56818b2004-12-12 11:45:24 +00001857 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001858
Miklos Szeredia13d9002004-11-02 17:32:03 +00001859 f->id_table_size = 14057;
1860 f->id_table = (struct node **)
1861 calloc(1, sizeof(struct node *) * f->id_table_size);
Miklos Szeredie56818b2004-12-12 11:45:24 +00001862 if (f->id_table == NULL) {
1863 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001864 goto out_free_name_table;
Miklos Szeredie56818b2004-12-12 11:45:24 +00001865 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001866
Miklos Szeredia25d4c22004-11-23 22:32:16 +00001867#ifndef USE_UCLIBC
1868 pthread_mutex_init(&f->lock, NULL);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001869 pthread_mutex_init(&f->worker_lock, NULL);
Miklos Szeredia25d4c22004-11-23 22:32:16 +00001870#else
1871 {
1872 pthread_mutexattr_t attr;
1873 pthread_mutexattr_init(&attr);
1874 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
1875 pthread_mutex_init(&f->lock, &attr);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001876 pthread_mutex_init(&f->worker_lock, &attr);
Miklos Szeredia25d4c22004-11-23 22:32:16 +00001877 pthread_mutexattr_destroy(&attr);
1878 }
1879#endif
Miklos Szeredi33232032001-11-19 17:55:51 +00001880 f->numworker = 0;
1881 f->numavail = 0;
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001882 memcpy(&f->op, op, op_size);
1883 f->compat = compat;
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001884 f->exited = 0;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001885
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001886 root = (struct node *) calloc(1, sizeof(struct node));
Miklos Szeredie56818b2004-12-12 11:45:24 +00001887 if (root == NULL) {
1888 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredia13d9002004-11-02 17:32:03 +00001889 goto out_free_id_table;
Miklos Szeredie56818b2004-12-12 11:45:24 +00001890 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001891
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001892 root->name = strdup("/");
Miklos Szeredie56818b2004-12-12 11:45:24 +00001893 if (root->name == NULL) {
1894 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001895 goto out_free_root;
Miklos Szeredie56818b2004-12-12 11:45:24 +00001896 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001897
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001898 root->parent = 0;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001899 root->nodeid = FUSE_ROOT_ID;
Miklos Szeredi76f65782004-02-19 16:55:40 +00001900 root->generation = 0;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001901 root->refctr = 1;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001902 hash_id(f, root);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001903
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001904 return f;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001905
1906 out_free_root:
1907 free(root);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001908 out_free_id_table:
1909 free(f->id_table);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001910 out_free_name_table:
1911 free(f->name_table);
1912 out_free:
1913 free(f);
1914 out:
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001915 return NULL;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001916}
1917
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001918struct fuse *fuse_new(int fd, const char *opts,
1919 const struct fuse_operations *op, size_t op_size)
1920{
1921 return fuse_new_common(fd, opts, op, op_size, 0);
1922}
1923
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001924struct fuse *fuse_new_compat2(int fd, const char *opts,
1925 const struct fuse_operations_compat2 *op)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001926{
1927 return fuse_new_common(fd, opts, (struct fuse_operations *) op,
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001928 sizeof(struct fuse_operations_compat2), 21);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001929}
1930
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001931struct fuse *fuse_new_compat1(int fd, int flags,
1932 const struct fuse_operations_compat1 *op)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001933{
1934 char *opts = NULL;
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001935 if (flags & FUSE_DEBUG_COMPAT1)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001936 opts = "debug";
1937 return fuse_new_common(fd, opts, (struct fuse_operations *) op,
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001938 sizeof(struct fuse_operations_compat1), 11);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001939}
1940
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001941void fuse_destroy(struct fuse *f)
1942{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001943 size_t i;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001944 for (i = 0; i < f->id_table_size; i++) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001945 struct node *node;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001946
Miklos Szeredia13d9002004-11-02 17:32:03 +00001947 for (node = f->id_table[i]; node != NULL; node = node->id_next) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001948 if (node->is_hidden) {
Miklos Szeredia13d9002004-11-02 17:32:03 +00001949 char *path = get_path(f, node->nodeid);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001950 if (path)
1951 f->op.unlink(path);
1952 }
1953 }
1954 }
Miklos Szeredia13d9002004-11-02 17:32:03 +00001955 for (i = 0; i < f->id_table_size; i++) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001956 struct node *node;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001957 struct node *next;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001958
Miklos Szeredia13d9002004-11-02 17:32:03 +00001959 for (node = f->id_table[i]; node != NULL; node = next) {
1960 next = node->id_next;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001961 free_node(node);
1962 }
1963 }
Miklos Szeredia13d9002004-11-02 17:32:03 +00001964 free(f->id_table);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001965 free(f->name_table);
Miklos Szeredia181e612001-11-06 12:03:23 +00001966 pthread_mutex_destroy(&f->lock);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001967 pthread_mutex_destroy(&f->worker_lock);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001968 free(f);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001969}
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001970
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001971__asm__(".symver fuse_exited,__fuse_exited@");
1972__asm__(".symver fuse_process_cmd,__fuse_process_cmd@");
1973__asm__(".symver fuse_read_cmd,__fuse_read_cmd@");
1974__asm__(".symver fuse_set_getcontext_func,__fuse_set_getcontext_func@");
1975__asm__(".symver fuse_new_compat2,fuse_new@");