blob: 25b62a01caeb533be80b6cca671ba42dbc0cdd8a [file] [log] [blame]
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi149f6072005-01-10 12:29:28 +00003 Copyright (C) 2001-2005 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00004
Miklos Szeredi8b39a9f2002-10-25 12:41:16 +00005 This program can be distributed under the terms of the GNU LGPL.
6 See the file COPYING.LIB
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00007*/
8
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 Szeredi3f0005f2005-01-04 19:24:31 +0000102 case FUSE_INIT: return "INIT";
Miklos Szeredi99f20742004-05-19 08:01:10 +0000103 default: return "???";
Miklos Szeredic8ba2372002-12-10 12:26:00 +0000104 }
105}
106
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000107static inline void fuse_dec_avail(struct fuse *f)
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000108{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000109 pthread_mutex_lock(&f->worker_lock);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000110 f->numavail --;
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000111 pthread_mutex_unlock(&f->worker_lock);
112}
113
114static inline void fuse_inc_avail(struct fuse *f)
115{
116 pthread_mutex_lock(&f->worker_lock);
117 f->numavail ++;
118 pthread_mutex_unlock(&f->worker_lock);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000119}
120
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000121static struct node *get_node_nocheck(struct fuse *f, nodeid_t nodeid)
Miklos Szeredia181e612001-11-06 12:03:23 +0000122{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000123 size_t hash = nodeid % f->id_table_size;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000124 struct node *node;
125
Miklos Szeredia13d9002004-11-02 17:32:03 +0000126 for (node = f->id_table[hash]; node != NULL; node = node->id_next)
127 if (node->nodeid == nodeid)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000128 return node;
129
130 return NULL;
Miklos Szeredia181e612001-11-06 12:03:23 +0000131}
132
Miklos Szeredia13d9002004-11-02 17:32:03 +0000133static struct node *get_node(struct fuse *f, nodeid_t nodeid)
Miklos Szeredia181e612001-11-06 12:03:23 +0000134{
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000135 struct node *node = get_node_nocheck(f, nodeid);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000136 if (!node) {
137 fprintf(stderr, "fuse internal error: node %lu not found\n",
138 nodeid);
139 abort();
140 }
141 return node;
Miklos Szeredia181e612001-11-06 12:03:23 +0000142}
143
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000144static void free_node(struct node *node)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000145{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000146 free(node->name);
147 free(node);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000148}
149
Miklos Szeredia13d9002004-11-02 17:32:03 +0000150static void unhash_id(struct fuse *f, struct node *node)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000151{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000152 size_t hash = node->nodeid % f->id_table_size;
153 struct node **nodep = &f->id_table[hash];
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000154
Miklos Szeredia13d9002004-11-02 17:32:03 +0000155 for (; *nodep != NULL; nodep = &(*nodep)->id_next)
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000156 if (*nodep == node) {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000157 *nodep = node->id_next;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000158 return;
159 }
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000160}
161
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000162static void hash_id(struct fuse *f, struct node *node)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000163{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000164 size_t hash = node->nodeid % f->id_table_size;
165 node->id_next = f->id_table[hash];
166 f->id_table[hash] = node;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000167}
168
Miklos Szeredia13d9002004-11-02 17:32:03 +0000169static unsigned int name_hash(struct fuse *f, nodeid_t parent, const char *name)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000170{
171 unsigned int hash = *name;
172
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000173 if (hash)
174 for (name += 1; *name != '\0'; name++)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000175 hash = (hash << 5) - hash + *name;
176
177 return (hash + parent) % f->name_table_size;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000178}
179
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000180static void unref_node(struct fuse *f, struct node *node);
181
182static void unhash_name(struct fuse *f, struct node *node)
183{
184 if (node->name) {
185 size_t hash = name_hash(f, node->parent, node->name);
186 struct node **nodep = &f->name_table[hash];
187
188 for (; *nodep != NULL; nodep = &(*nodep)->name_next)
189 if (*nodep == node) {
190 *nodep = node->name_next;
191 node->name_next = NULL;
192 unref_node(f, get_node(f, node->parent));
193 free(node->name);
194 node->name = NULL;
195 node->parent = 0;
196 return;
197 }
198 fprintf(stderr, "fuse internal error: unable to unhash node: %lu\n",
199 node->nodeid);
200 abort();
201 }
202}
203
204static int hash_name(struct fuse *f, struct node *node, nodeid_t parent,
205 const char *name)
206{
207 size_t hash = name_hash(f, parent, name);
208 node->name = strdup(name);
209 if (node->name == NULL)
210 return -1;
211
212 get_node(f, parent)->refctr ++;
213 node->parent = parent;
214 node->name_next = f->name_table[hash];
215 f->name_table[hash] = node;
216 return 0;
217}
218
219static void delete_node(struct fuse *f, struct node *node)
220{
221 assert(!node->name);
222 unhash_id(f, node);
223 free_node(node);
224}
225
226static void unref_node(struct fuse *f, struct node *node)
227{
228 assert(node->refctr > 0);
229 node->refctr --;
230 if (!node->refctr)
231 delete_node(f, node);
232}
233
234static nodeid_t next_id(struct fuse *f)
235{
236 do {
237 f->ctr++;
238 if (!f->ctr)
239 f->generation ++;
240 } while (f->ctr == 0 || get_node_nocheck(f, f->ctr) != NULL);
241 return f->ctr;
242}
243
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000244static struct node *lookup_node(struct fuse *f, nodeid_t parent,
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000245 const char *name)
246{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000247 size_t hash = name_hash(f, parent, name);
248 struct node *node;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000249
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000250 for (node = f->name_table[hash]; node != NULL; node = node->name_next)
251 if (node->parent == parent && strcmp(node->name, name) == 0)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000252 return node;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000253
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000254 return NULL;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000255}
256
Miklos Szeredia13d9002004-11-02 17:32:03 +0000257static struct node *find_node(struct fuse *f, nodeid_t parent, char *name,
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000258 struct fuse_attr *attr, uint64_t version)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000259{
260 struct node *node;
Miklos Szeredia181e612001-11-06 12:03:23 +0000261 int mode = attr->mode & S_IFMT;
262 int rdev = 0;
263
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000264 if (S_ISCHR(mode) || S_ISBLK(mode))
Miklos Szeredia181e612001-11-06 12:03:23 +0000265 rdev = attr->rdev;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000266
Miklos Szeredia181e612001-11-06 12:03:23 +0000267 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000268 node = lookup_node(f, parent, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000269 if (node != NULL) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000270 if (!(f->flags & FUSE_USE_INO))
271 attr->ino = node->nodeid;
272 } else {
273 node = (struct node *) calloc(1, sizeof(struct node));
274 if (node == NULL)
275 goto out_err;
Miklos Szeredia181e612001-11-06 12:03:23 +0000276
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000277 node->refctr = 1;
278 node->nodeid = next_id(f);
279 if (!(f->flags & FUSE_USE_INO))
280 attr->ino = node->nodeid;
281 node->open_count = 0;
282 node->is_hidden = 0;
283 node->generation = f->generation;
284 if (hash_name(f, node, parent, name) == -1) {
285 free(node);
286 node = NULL;
287 goto out_err;
288 }
289 hash_id(f, node);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000290 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000291 node->version = version;
Miklos Szeredic2309912004-09-21 13:40:38 +0000292 out_err:
Miklos Szeredia181e612001-11-06 12:03:23 +0000293 pthread_mutex_unlock(&f->lock);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000294 return node;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000295}
296
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000297static char *add_name(char *buf, char *s, const char *name)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000298{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000299 size_t len = strlen(name);
300 s -= len;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000301 if (s <= buf) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000302 fprintf(stderr, "fuse: path too long: ...%s\n", s + len);
303 return NULL;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000304 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000305 strncpy(s, name, len);
306 s--;
307 *s = '/';
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000308
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000309 return s;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000310}
311
Miklos Szeredia13d9002004-11-02 17:32:03 +0000312static char *get_path_name(struct fuse *f, nodeid_t nodeid, const char *name)
Miklos Szeredib483c932001-10-29 14:57:57 +0000313{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000314 char buf[FUSE_MAX_PATH];
315 char *s = buf + FUSE_MAX_PATH - 1;
316 struct node *node;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000317
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000318 *s = '\0';
Miklos Szeredia181e612001-11-06 12:03:23 +0000319
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000320 if (name != NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000321 s = add_name(buf, s, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000322 if (s == NULL)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000323 return NULL;
324 }
325
326 pthread_mutex_lock(&f->lock);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000327 for (node = get_node(f, nodeid); node && node->nodeid != FUSE_ROOT_ID;
328 node = get_node(f, node->parent)) {
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000329 if (node->name == NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000330 s = NULL;
331 break;
332 }
333
334 s = add_name(buf, s, node->name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000335 if (s == NULL)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000336 break;
337 }
338 pthread_mutex_unlock(&f->lock);
339
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000340 if (node == NULL || s == NULL)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000341 return NULL;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000342 else if (*s == '\0')
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000343 return strdup("/");
344 else
345 return strdup(s);
346}
Miklos Szeredia181e612001-11-06 12:03:23 +0000347
Miklos Szeredia13d9002004-11-02 17:32:03 +0000348static char *get_path(struct fuse *f, nodeid_t nodeid)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000349{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000350 return get_path_name(f, nodeid, NULL);
Miklos Szeredib483c932001-10-29 14:57:57 +0000351}
352
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000353static void forget_node(struct fuse *f, nodeid_t nodeid, uint64_t version)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000354{
Miklos Szeredia181e612001-11-06 12:03:23 +0000355 struct node *node;
356
357 pthread_mutex_lock(&f->lock);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000358 node = get_node(f, nodeid);
359 if (node->version == version && nodeid != FUSE_ROOT_ID) {
360 node->version = 0;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000361 unhash_name(f, node);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000362 unref_node(f, node);
Miklos Szeredia181e612001-11-06 12:03:23 +0000363 }
364 pthread_mutex_unlock(&f->lock);
365
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000366}
367
Miklos Szeredia13d9002004-11-02 17:32:03 +0000368static void remove_node(struct fuse *f, nodeid_t dir, const char *name)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000369{
Miklos Szeredia181e612001-11-06 12:03:23 +0000370 struct node *node;
371
372 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000373 node = lookup_node(f, dir, name);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000374 if (node != NULL)
375 unhash_name(f, node);
Miklos Szeredia181e612001-11-06 12:03:23 +0000376 pthread_mutex_unlock(&f->lock);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000377}
378
Miklos Szeredia13d9002004-11-02 17:32:03 +0000379static int rename_node(struct fuse *f, nodeid_t olddir, const char *oldname,
380 nodeid_t newdir, const char *newname, int hide)
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000381{
Miklos Szeredia181e612001-11-06 12:03:23 +0000382 struct node *node;
383 struct node *newnode;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000384 int err = 0;
Miklos Szeredia181e612001-11-06 12:03:23 +0000385
386 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000387 node = lookup_node(f, olddir, oldname);
388 newnode = lookup_node(f, newdir, newname);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000389 if (node == NULL)
390 goto out;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000391
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000392 if (newnode != NULL) {
393 if (hide) {
394 fprintf(stderr, "fuse: hidden file got created during hiding\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000395 err = -EBUSY;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000396 goto out;
397 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000398 unhash_name(f, newnode);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000399 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000400
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000401 unhash_name(f, node);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000402 if (hash_name(f, node, newdir, newname) == -1) {
403 err = -ENOMEM;
404 goto out;
405 }
406
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000407 if (hide)
408 node->is_hidden = 1;
409
410 out:
Miklos Szeredia181e612001-11-06 12:03:23 +0000411 pthread_mutex_unlock(&f->lock);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000412 return err;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000413}
414
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000415static void convert_stat(struct stat *stbuf, struct fuse_attr *attr)
416{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000417 attr->ino = stbuf->st_ino;
Miklos Szeredib5958612004-02-20 14:10:49 +0000418 attr->mode = stbuf->st_mode;
419 attr->nlink = stbuf->st_nlink;
420 attr->uid = stbuf->st_uid;
421 attr->gid = stbuf->st_gid;
422 attr->rdev = stbuf->st_rdev;
423 attr->size = stbuf->st_size;
424 attr->blocks = stbuf->st_blocks;
425 attr->atime = stbuf->st_atime;
Miklos Szeredib5958612004-02-20 14:10:49 +0000426 attr->mtime = stbuf->st_mtime;
Miklos Szeredib5958612004-02-20 14:10:49 +0000427 attr->ctime = stbuf->st_ctime;
Miklos Szeredicb264512004-06-23 18:52:50 +0000428#ifdef HAVE_STRUCT_STAT_ST_ATIM
429 attr->atimensec = stbuf->st_atim.tv_nsec;
430 attr->mtimensec = stbuf->st_mtim.tv_nsec;
Miklos Szeredib5958612004-02-20 14:10:49 +0000431 attr->ctimensec = stbuf->st_ctim.tv_nsec;
Miklos Szeredicb264512004-06-23 18:52:50 +0000432#endif
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000433}
434
Miklos Szeredi8fb48fe2004-11-08 14:48:52 +0000435static int fill_dir(struct fuse_dirhandle *dh, const char *name, int type,
436 ino_t ino)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000437{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000438 size_t namelen = strlen(name);
439 struct fuse_dirent *dirent;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000440 size_t reclen;
441 size_t res;
442
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000443 if (namelen > FUSE_NAME_MAX)
444 namelen = FUSE_NAME_MAX;
445
446 dirent = calloc(1, sizeof(struct fuse_dirent) + namelen + 8);
447 if (dirent == NULL)
448 return -ENOMEM;
449
Miklos Szeredi8fb48fe2004-11-08 14:48:52 +0000450 if ((dh->fuse->flags & FUSE_USE_INO))
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000451 dirent->ino = ino;
Miklos Szeredi8fb48fe2004-11-08 14:48:52 +0000452 else
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000453 dirent->ino = (unsigned long) -1;
454 dirent->namelen = namelen;
455 strncpy(dirent->name, name, namelen);
456 dirent->type = type;
457 reclen = FUSE_DIRENT_SIZE(dirent);
458 res = fwrite(dirent, reclen, 1, dh->fp);
459 free(dirent);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000460 if (res == 0) {
Miklos Szeredi96249982001-11-21 12:21:19 +0000461 perror("fuse: writing directory file");
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000462 return -EIO;
463 }
464 return 0;
465}
466
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000467static int send_reply_raw(struct fuse *f, char *outbuf, size_t outsize)
Miklos Szeredi43696432001-11-18 19:15:05 +0000468{
469 int res;
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000470 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
471 out->len = outsize;
Miklos Szeredi43696432001-11-18 19:15:05 +0000472
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000473 if ((f->flags & FUSE_DEBUG)) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000474 printf(" unique: %llu, error: %i (%s), outsize: %i\n",
475 out->unique, out->error, strerror(-out->error), outsize);
Miklos Szeredi43696432001-11-18 19:15:05 +0000476 fflush(stdout);
477 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000478
Miklos Szeredi4e71c9f2004-02-09 12:05:14 +0000479 /* This needs to be done before the reply, otherwise the scheduler
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000480 could play tricks with us, and only let the counter be
481 increased long after the operation is done */
482 fuse_inc_avail(f);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000483
Miklos Szeredi43696432001-11-18 19:15:05 +0000484 res = write(f->fd, outbuf, outsize);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000485 if (res == -1) {
Miklos Szeredi43696432001-11-18 19:15:05 +0000486 /* ENOENT means the operation was interrupted */
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000487 if (!f->exited && errno != ENOENT)
Miklos Szeredi96249982001-11-21 12:21:19 +0000488 perror("fuse: writing device");
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000489 return -errno;
Miklos Szeredi43696432001-11-18 19:15:05 +0000490 }
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000491 return 0;
Miklos Szeredi43696432001-11-18 19:15:05 +0000492}
493
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000494static int send_reply(struct fuse *f, struct fuse_in_header *in, int error,
495 void *arg, size_t argsize)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000496{
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000497 int res;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000498 char *outbuf;
499 size_t outsize;
500 struct fuse_out_header *out;
501
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000502 if (error <= -1000 || error > 0) {
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000503 fprintf(stderr, "fuse: bad error value: %i\n", error);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000504 error = -ERANGE;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000505 }
506
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000507 if (error)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000508 argsize = 0;
509
510 outsize = sizeof(struct fuse_out_header) + argsize;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000511 outbuf = (char *) malloc(outsize);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000512 if (outbuf == NULL) {
513 fprintf(stderr, "fuse: failed to allocate reply buffer\n");
514 res = -ENOMEM;
515 } else {
516 out = (struct fuse_out_header *) outbuf;
517 memset(out, 0, sizeof(struct fuse_out_header));
518 out->unique = in->unique;
519 out->error = error;
520 if (argsize != 0)
521 memcpy(outbuf + sizeof(struct fuse_out_header), arg, argsize);
522
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000523 res = send_reply_raw(f, outbuf, outsize);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000524 free(outbuf);
525 }
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000526
527 return res;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000528}
529
Miklos Szeredia13d9002004-11-02 17:32:03 +0000530static int is_open(struct fuse *f, nodeid_t dir, const char *name)
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000531{
532 struct node *node;
533 int isopen = 0;
534 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000535 node = lookup_node(f, dir, name);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000536 if (node && node->open_count > 0)
537 isopen = 1;
538 pthread_mutex_unlock(&f->lock);
539 return isopen;
540}
541
Miklos Szeredia13d9002004-11-02 17:32:03 +0000542static char *hidden_name(struct fuse *f, nodeid_t dir, const char *oldname,
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000543 char *newname, size_t bufsize)
544{
545 struct stat buf;
546 struct node *node;
547 struct node *newnode;
548 char *newpath;
549 int res;
550 int failctr = 10;
551
552 if (!f->op.getattr)
553 return NULL;
554
555 do {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000556 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000557 node = lookup_node(f, dir, oldname);
558 if (node == NULL) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000559 pthread_mutex_unlock(&f->lock);
560 return NULL;
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000561 }
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000562 do {
563 f->hidectr ++;
564 snprintf(newname, bufsize, ".fuse_hidden%08x%08x",
Miklos Szeredia13d9002004-11-02 17:32:03 +0000565 (unsigned int) node->nodeid, f->hidectr);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000566 newnode = lookup_node(f, dir, newname);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000567 } while(newnode);
568 pthread_mutex_unlock(&f->lock);
569
570 newpath = get_path_name(f, dir, newname);
571 if (!newpath)
572 break;
573
574 res = f->op.getattr(newpath, &buf);
575 if (res != 0)
576 break;
577 free(newpath);
578 newpath = NULL;
579 } while(--failctr);
580
581 return newpath;
582}
583
Miklos Szeredia13d9002004-11-02 17:32:03 +0000584static int hide_node(struct fuse *f, const char *oldpath, nodeid_t dir,
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000585 const char *oldname)
586{
587 char newname[64];
588 char *newpath;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000589 int err = -EBUSY;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000590
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000591 if (f->op.rename && f->op.unlink) {
592 newpath = hidden_name(f, dir, oldname, newname, sizeof(newname));
593 if (newpath) {
594 int res = f->op.rename(oldpath, newpath);
595 if (res == 0)
596 err = rename_node(f, dir, oldname, dir, newname, 1);
597 free(newpath);
598 }
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000599 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000600 return err;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000601}
602
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000603static int lookup_path(struct fuse *f, nodeid_t nodeid, uint64_t version,
604 char *name, const char *path,
605 struct fuse_entry_out *arg)
Miklos Szeredi76f65782004-02-19 16:55:40 +0000606{
607 int res;
608 struct stat buf;
609
610 res = f->op.getattr(path, &buf);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000611 if (res == 0) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000612 struct node *node;
613
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000614 memset(arg, 0, sizeof(struct fuse_entry_out));
Miklos Szeredi76f65782004-02-19 16:55:40 +0000615 convert_stat(&buf, &arg->attr);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000616 node = find_node(f, nodeid, name, &arg->attr, version);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000617 if (node == NULL)
618 res = -ENOMEM;
619 else {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000620 arg->nodeid = node->nodeid;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000621 arg->generation = node->generation;
622 arg->entry_valid = ENTRY_REVALIDATE_TIME;
623 arg->entry_valid_nsec = 0;
624 arg->attr_valid = ATTR_REVALIDATE_TIME;
625 arg->attr_valid_nsec = 0;
626 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000627 printf(" NODEID: %lu\n", (unsigned long) arg->nodeid);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000628 fflush(stdout);
629 }
Miklos Szeredi76f65782004-02-19 16:55:40 +0000630 }
631 }
632 return res;
633}
634
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000635static void do_lookup(struct fuse *f, struct fuse_in_header *in, char *name)
636{
637 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000638 int res2;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000639 char *path;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000640 struct fuse_entry_out arg;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000641
Miklos Szeredi5e183482001-10-31 14:52:35 +0000642 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000643 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000644 if (path != NULL) {
645 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi6ebe2342002-01-06 21:44:16 +0000646 printf("LOOKUP %s\n", path);
647 fflush(stdout);
648 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000649 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000650 if (f->op.getattr)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000651 res = lookup_path(f, in->nodeid, in->unique, name, path, &arg);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000652 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000653 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000654 res2 = send_reply(f, in, res, &arg, sizeof(arg));
655 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000656 forget_node(f, arg.nodeid, in->unique);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000657}
658
Miklos Szeredia181e612001-11-06 12:03:23 +0000659static void do_forget(struct fuse *f, struct fuse_in_header *in,
660 struct fuse_forget_in *arg)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000661{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000662 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000663 printf("FORGET %lu/%llu\n", (unsigned long) in->nodeid,
664 arg->version);
Miklos Szeredi43696432001-11-18 19:15:05 +0000665 fflush(stdout);
666 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000667 forget_node(f, in->nodeid, arg->version);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000668}
669
670static void do_getattr(struct fuse *f, struct fuse_in_header *in)
671{
672 int res;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000673 char *path;
674 struct stat buf;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000675 struct fuse_attr_out arg;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000676
Miklos Szeredi5e183482001-10-31 14:52:35 +0000677 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000678 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000679 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +0000680 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000681 if (f->op.getattr)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000682 res = f->op.getattr(path, &buf);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000683 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000684 }
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000685
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000686 if (res == 0) {
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000687 memset(&arg, 0, sizeof(struct fuse_attr_out));
688 arg.attr_valid = ATTR_REVALIDATE_TIME;
689 arg.attr_valid_nsec = 0;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000690 convert_stat(&buf, &arg.attr);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000691 if (!(f->flags & FUSE_USE_INO))
692 arg.attr.ino = in->nodeid;
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000693 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000694
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000695 send_reply(f, in, res, &arg, sizeof(arg));
696}
697
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000698static int do_chmod(struct fuse *f, const char *path, struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000699{
700 int res;
701
702 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000703 if (f->op.chmod)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000704 res = f->op.chmod(path, attr->mode);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000705
706 return res;
707}
708
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000709static int do_chown(struct fuse *f, const char *path, struct fuse_attr *attr,
Miklos Szeredi2e50d432001-12-20 12:17:25 +0000710 int valid)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000711{
712 int res;
713 uid_t uid = (valid & FATTR_UID) ? attr->uid : (uid_t) -1;
714 gid_t gid = (valid & FATTR_GID) ? attr->gid : (gid_t) -1;
715
716 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000717 if (f->op.chown)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000718 res = f->op.chown(path, uid, gid);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000719
720 return res;
721}
722
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000723static int do_truncate(struct fuse *f, const char *path,
724 struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000725{
726 int res;
727
728 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000729 if (f->op.truncate)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000730 res = f->op.truncate(path, attr->size);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000731
732 return res;
733}
734
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000735static int do_utime(struct fuse *f, const char *path, struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000736{
737 int res;
738 struct utimbuf buf;
739 buf.actime = attr->atime;
740 buf.modtime = attr->mtime;
741 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000742 if (f->op.utime)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000743 res = f->op.utime(path, &buf);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000744
745 return res;
746}
747
Miklos Szeredi5e183482001-10-31 14:52:35 +0000748static void do_setattr(struct fuse *f, struct fuse_in_header *in,
749 struct fuse_setattr_in *arg)
750{
751 int res;
752 char *path;
753 int valid = arg->valid;
754 struct fuse_attr *attr = &arg->attr;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000755 struct fuse_attr_out outarg;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000756
757 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000758 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000759 if (path != NULL) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000760 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000761 if (f->op.getattr) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000762 res = 0;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000763 if (!res && (valid & FATTR_MODE))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000764 res = do_chmod(f, path, attr);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000765 if (!res && (valid & (FATTR_UID | FATTR_GID)))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000766 res = do_chown(f, path, attr, valid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000767 if (!res && (valid & FATTR_SIZE))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000768 res = do_truncate(f, path, attr);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000769 if (!res && (valid & (FATTR_ATIME | FATTR_MTIME)) ==
Miklos Szeredib5958612004-02-20 14:10:49 +0000770 (FATTR_ATIME | FATTR_MTIME))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000771 res = do_utime(f, path, attr);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000772 if (!res) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000773 struct stat buf;
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000774 res = f->op.getattr(path, &buf);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000775 if (!res) {
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000776 memset(&outarg, 0, sizeof(struct fuse_attr_out));
777 outarg.attr_valid = ATTR_REVALIDATE_TIME;
778 outarg.attr_valid_nsec = 0;
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000779 convert_stat(&buf, &outarg.attr);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000780 if (!(f->flags & FUSE_USE_INO))
781 outarg.attr.ino = in->nodeid;
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000782 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000783 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000784 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000785 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000786 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000787 send_reply(f, in, res, &outarg, sizeof(outarg));
Miklos Szeredi5e183482001-10-31 14:52:35 +0000788}
789
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000790static void do_readlink(struct fuse *f, struct fuse_in_header *in)
791{
792 int res;
793 char link[PATH_MAX + 1];
Miklos Szeredib483c932001-10-29 14:57:57 +0000794 char *path;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000795
Miklos Szeredi5e183482001-10-31 14:52:35 +0000796 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000797 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000798 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +0000799 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000800 if (f->op.readlink)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000801 res = f->op.readlink(path, link, sizeof(link));
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000802 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000803 }
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000804 link[PATH_MAX] = '\0';
Miklos Szeredi4b2bef42002-01-09 12:23:27 +0000805 send_reply(f, in, res, link, res == 0 ? strlen(link) : 0);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000806}
807
808static void do_getdir(struct fuse *f, struct fuse_in_header *in)
809{
810 int res;
811 struct fuse_getdir_out arg;
Miklos Szeredia181e612001-11-06 12:03:23 +0000812 struct fuse_dirhandle dh;
Miklos Szeredib483c932001-10-29 14:57:57 +0000813 char *path;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000814
Miklos Szeredib483c932001-10-29 14:57:57 +0000815 dh.fuse = f;
816 dh.fp = tmpfile();
Miklos Szeredia13d9002004-11-02 17:32:03 +0000817 dh.dir = in->nodeid;
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000818
Miklos Szeredi65afea12004-09-14 07:13:45 +0000819 res = -EIO;
820 if (dh.fp == NULL)
821 perror("fuse: failed to create temporary file");
822 else {
823 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000824 path = get_path(f, in->nodeid);
Miklos Szeredi65afea12004-09-14 07:13:45 +0000825 if (path != NULL) {
826 res = -ENOSYS;
827 if (f->op.getdir)
Miklos Szeredi8fb48fe2004-11-08 14:48:52 +0000828 res = f->op.getdir(path, &dh, fill_dir);
Miklos Szeredi65afea12004-09-14 07:13:45 +0000829 free(path);
830 }
831 fflush(dh.fp);
832 }
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000833 memset(&arg, 0, sizeof(struct fuse_getdir_out));
Miklos Szeredi65afea12004-09-14 07:13:45 +0000834 if (res == 0)
835 arg.fd = fileno(dh.fp);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000836 send_reply(f, in, res, &arg, sizeof(arg));
Miklos Szeredi65afea12004-09-14 07:13:45 +0000837 if (dh.fp != NULL)
838 fclose(dh.fp);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000839}
840
Miklos Szeredib483c932001-10-29 14:57:57 +0000841static void do_mknod(struct fuse *f, struct fuse_in_header *in,
842 struct fuse_mknod_in *inarg)
843{
844 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000845 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000846 char *path;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000847 char *name = PARAM(inarg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000848 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000849
Miklos Szeredi5e183482001-10-31 14:52:35 +0000850 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000851 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000852 if (path != NULL) {
853 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000854 printf("MKNOD %s\n", path);
855 fflush(stdout);
856 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000857 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000858 if (f->op.mknod && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000859 res = f->op.mknod(path, inarg->mode, inarg->rdev);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000860 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000861 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000862 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000863 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000864 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000865 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
866 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000867 forget_node(f, outarg.nodeid, in->unique);
Miklos Szeredib483c932001-10-29 14:57:57 +0000868}
869
870static void do_mkdir(struct fuse *f, struct fuse_in_header *in,
871 struct fuse_mkdir_in *inarg)
872{
873 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000874 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000875 char *path;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000876 char *name = PARAM(inarg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000877 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000878
Miklos Szeredi5e183482001-10-31 14:52:35 +0000879 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000880 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000881 if (path != NULL) {
882 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000883 printf("MKDIR %s\n", path);
884 fflush(stdout);
885 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000886 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000887 if (f->op.mkdir && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000888 res = f->op.mkdir(path, inarg->mode);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000889 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000890 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000891 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000892 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000893 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000894 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
895 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000896 forget_node(f, outarg.nodeid, in->unique);
Miklos Szeredib483c932001-10-29 14:57:57 +0000897}
898
Miklos Szeredib5958612004-02-20 14:10:49 +0000899static void do_unlink(struct fuse *f, struct fuse_in_header *in, char *name)
Miklos Szeredib483c932001-10-29 14:57:57 +0000900{
901 int res;
902 char *path;
903
Miklos Szeredi5e183482001-10-31 14:52:35 +0000904 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000905 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000906 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000907 if (f->flags & FUSE_DEBUG) {
908 printf("UNLINK %s\n", path);
909 fflush(stdout);
910 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000911 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000912 if (f->op.unlink) {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000913 if (!(f->flags & FUSE_HARD_REMOVE) && is_open(f, in->nodeid, name))
914 res = hide_node(f, path, in->nodeid, name);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000915 else {
916 res = f->op.unlink(path);
917 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000918 remove_node(f, in->nodeid, name);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000919
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000920 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000921 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000922 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000923 }
Miklos Szeredib5958612004-02-20 14:10:49 +0000924 send_reply(f, in, res, NULL, 0);
925}
926
927static void do_rmdir(struct fuse *f, struct fuse_in_header *in, char *name)
928{
929 int res;
930 char *path;
931
932 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000933 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000934 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000935 if (f->flags & FUSE_DEBUG) {
936 printf("RMDIR %s\n", path);
937 fflush(stdout);
938 }
Miklos Szeredib5958612004-02-20 14:10:49 +0000939 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000940 if (f->op.rmdir) {
Miklos Szeredib5958612004-02-20 14:10:49 +0000941 res = f->op.rmdir(path);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000942 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000943 remove_node(f, in->nodeid, name);
Miklos Szeredib5958612004-02-20 14:10:49 +0000944 }
945 free(path);
946 }
Miklos Szeredib483c932001-10-29 14:57:57 +0000947 send_reply(f, in, res, NULL, 0);
948}
949
950static void do_symlink(struct fuse *f, struct fuse_in_header *in, char *name,
951 char *link)
952{
953 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000954 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000955 char *path;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000956 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000957
Miklos Szeredi5e183482001-10-31 14:52:35 +0000958 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000959 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000960 if (path != NULL) {
961 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000962 printf("SYMLINK %s\n", path);
963 fflush(stdout);
964 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000965 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000966 if (f->op.symlink && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000967 res = f->op.symlink(link, path);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000968 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000969 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000970 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000971 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000972 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000973 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
974 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000975 forget_node(f, outarg.nodeid, in->unique);
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000976
Miklos Szeredib483c932001-10-29 14:57:57 +0000977}
978
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000979static void do_rename(struct fuse *f, struct fuse_in_header *in,
980 struct fuse_rename_in *inarg)
981{
982 int res;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000983 nodeid_t olddir = in->nodeid;
984 nodeid_t newdir = inarg->newdir;
Miklos Szeredi6bf8b682002-10-28 08:49:39 +0000985 char *oldname = PARAM(inarg);
986 char *newname = oldname + strlen(oldname) + 1;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000987 char *oldpath;
988 char *newpath;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000989
Miklos Szeredi5e183482001-10-31 14:52:35 +0000990 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000991 oldpath = get_path_name(f, olddir, oldname);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000992 if (oldpath != NULL) {
Miklos Szeredia181e612001-11-06 12:03:23 +0000993 newpath = get_path_name(f, newdir, newname);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000994 if (newpath != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000995 if (f->flags & FUSE_DEBUG) {
996 printf("RENAME %s -> %s\n", oldpath, newpath);
997 fflush(stdout);
998 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000999 res = -ENOSYS;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001000 if (f->op.rename) {
1001 res = 0;
Miklos Szeredi2529ca22004-07-13 15:36:52 +00001002 if (!(f->flags & FUSE_HARD_REMOVE) &&
1003 is_open(f, newdir, newname))
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001004 res = hide_node(f, newpath, newdir, newname);
1005 if (res == 0) {
1006 res = f->op.rename(oldpath, newpath);
1007 if (res == 0)
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001008 res = rename_node(f, olddir, oldname, newdir, newname, 0);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001009 }
1010 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001011 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001012 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001013 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001014 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001015 send_reply(f, in, res, NULL, 0);
1016}
1017
1018static void do_link(struct fuse *f, struct fuse_in_header *in,
Miklos Szeredi5e183482001-10-31 14:52:35 +00001019 struct fuse_link_in *arg)
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001020{
1021 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +00001022 int res2;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001023 char *oldpath;
1024 char *newpath;
Miklos Szeredi76f65782004-02-19 16:55:40 +00001025 char *name = PARAM(arg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +00001026 struct fuse_entry_out outarg;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001027
Miklos Szeredi5e183482001-10-31 14:52:35 +00001028 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001029 oldpath = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001030 if (oldpath != NULL) {
Miklos Szeredi76f65782004-02-19 16:55:40 +00001031 newpath = get_path_name(f, arg->newdir, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001032 if (newpath != NULL) {
1033 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +00001034 printf("LINK %s\n", newpath);
1035 fflush(stdout);
1036 }
Miklos Szeredi5e183482001-10-31 14:52:35 +00001037 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001038 if (f->op.link && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +00001039 res = f->op.link(oldpath, newpath);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001040 if (res == 0)
Miklos Szeredi76f65782004-02-19 16:55:40 +00001041 res = lookup_path(f, arg->newdir, in->unique, name,
1042 newpath, &outarg);
1043 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001044 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001045 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001046 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001047 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +00001048 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
1049 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001050 forget_node(f, outarg.nodeid, in->unique);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001051}
1052
Miklos Szeredi5e183482001-10-31 14:52:35 +00001053static void do_open(struct fuse *f, struct fuse_in_header *in,
1054 struct fuse_open_in *arg)
1055{
1056 int res;
1057 char *path;
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001058 struct fuse_open_out outarg;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001059 struct fuse_file_info fi;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001060
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001061 memset(&fi, 0, sizeof(fi));
1062 fi.flags = arg->flags;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001063 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001064 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001065 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +00001066 res = -ENOSYS;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001067 if (f->op.open) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001068 if (!f->compat)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001069 res = f->op.open(path, &fi);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001070 else
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001071 res = ((struct fuse_operations_compat2 *) &f->op)->open(path, fi.flags);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001072 }
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +00001073 }
Miklos Szeredi73798f92004-07-12 15:55:11 +00001074 if (res == 0) {
1075 int res2;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001076 outarg.fh = fi.fh;
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001077 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001078 printf("OPEN[%lu] flags: 0x%x\n", fi.fh, arg->flags);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001079 fflush(stdout);
1080 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001081
1082 pthread_mutex_lock(&f->lock);
1083 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001084 if(res2 == -ENOENT) {
1085 /* The open syscall was interrupted, so it must be cancelled */
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001086 if(f->op.release) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001087 if (!f->compat)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001088 f->op.release(path, &fi);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001089 else
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001090 ((struct fuse_operations_compat2 *) &f->op)->release(path, fi.flags);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001091 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001092 } else {
1093 struct node *node = get_node(f, in->nodeid);
1094 node->open_count ++;
1095 }
Miklos Szeredi73798f92004-07-12 15:55:11 +00001096 pthread_mutex_unlock(&f->lock);
Miklos Szeredi73798f92004-07-12 15:55:11 +00001097 } else
1098 send_reply(f, in, res, NULL, 0);
1099
Miklos Szeredi9a31cca2004-06-26 21:11:25 +00001100 if (path)
1101 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001102}
1103
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001104static void do_flush(struct fuse *f, struct fuse_in_header *in,
1105 struct fuse_flush_in *arg)
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001106{
1107 char *path;
1108 int res;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001109 struct fuse_file_info fi;
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001110
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001111 memset(&fi, 0, sizeof(fi));
1112 fi.fh = arg->fh;
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001113 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001114 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001115 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001116 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001117 printf("FLUSH[%lu]\n", (unsigned long) arg->fh);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001118 fflush(stdout);
1119 }
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001120 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001121 if (f->op.flush)
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001122 res = f->op.flush(path, &fi);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001123 free(path);
1124 }
1125 send_reply(f, in, res, NULL, 0);
1126}
1127
Miklos Szeredi9478e862002-12-11 09:50:26 +00001128static void do_release(struct fuse *f, struct fuse_in_header *in,
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001129 struct fuse_release_in *arg)
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001130{
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001131 struct node *node;
1132 char *path;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001133 struct fuse_file_info fi;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001134 int unlink_hidden;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001135
1136 memset(&fi, 0, sizeof(fi));
1137 fi.flags = arg->flags;
1138 fi.fh = arg->fh;
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001139
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001140 pthread_mutex_lock(&f->lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001141 node = get_node(f, in->nodeid);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001142 assert(node->open_count > 0);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001143 --node->open_count;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001144 unlink_hidden = (node->is_hidden && !node->open_count);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001145 pthread_mutex_unlock(&f->lock);
1146
Miklos Szeredia13d9002004-11-02 17:32:03 +00001147 path = get_path(f, in->nodeid);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001148 if (f->flags & FUSE_DEBUG) {
1149 printf("RELEASE[%lu] flags: 0x%x\n", fi.fh, fi.flags);
1150 fflush(stdout);
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001151 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001152 if (f->op.release) {
1153 if (!f->compat)
1154 f->op.release(path ? path : "-", &fi);
1155 else if (path)
1156 ((struct fuse_operations_compat2 *) &f->op)->release(path, fi.flags);
1157 }
1158
1159 if(unlink_hidden && path)
1160 f->op.unlink(path);
1161
1162 if (path)
1163 free(path);
1164
Miklos Szeredi556d03d2004-06-30 11:13:41 +00001165 send_reply(f, in, 0, NULL, 0);
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001166}
1167
Miklos Szeredi5e183482001-10-31 14:52:35 +00001168static void do_read(struct fuse *f, struct fuse_in_header *in,
1169 struct fuse_read_in *arg)
1170{
1171 int res;
1172 char *path;
Miklos Szeredi43696432001-11-18 19:15:05 +00001173 char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + arg->size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001174 if (outbuf == NULL)
1175 send_reply(f, in, -ENOMEM, NULL, 0);
1176 else {
1177 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
1178 char *buf = outbuf + sizeof(struct fuse_out_header);
1179 size_t size;
1180 size_t outsize;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001181 struct fuse_file_info fi;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001182
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001183 memset(&fi, 0, sizeof(fi));
1184 fi.fh = arg->fh;
1185
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001186 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001187 path = get_path(f, in->nodeid);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001188 if (path != NULL) {
1189 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001190 printf("READ[%lu] %u bytes from %llu\n",
1191 (unsigned long) arg->fh, arg->size, arg->offset);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001192 fflush(stdout);
1193 }
1194
1195 res = -ENOSYS;
1196 if (f->op.read)
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001197 res = f->op.read(path, buf, arg->size, arg->offset, &fi);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001198 free(path);
Miklos Szeredi6ebe2342002-01-06 21:44:16 +00001199 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001200
1201 size = 0;
1202 if (res >= 0) {
1203 size = res;
1204 res = 0;
1205 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001206 printf(" READ[%lu] %u bytes\n", (unsigned long) arg->fh,
1207 size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001208 fflush(stdout);
1209 }
Miklos Szeredi6ebe2342002-01-06 21:44:16 +00001210 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001211 memset(out, 0, sizeof(struct fuse_out_header));
1212 out->unique = in->unique;
1213 out->error = res;
1214 outsize = sizeof(struct fuse_out_header) + size;
1215
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001216 send_reply_raw(f, outbuf, outsize);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001217 free(outbuf);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001218 }
Miklos Szeredi5e183482001-10-31 14:52:35 +00001219}
Miklos Szeredib483c932001-10-29 14:57:57 +00001220
Miklos Szeredia181e612001-11-06 12:03:23 +00001221static void do_write(struct fuse *f, struct fuse_in_header *in,
1222 struct fuse_write_in *arg)
1223{
1224 int res;
1225 char *path;
Miklos Szerediad051c32004-07-02 09:22:50 +00001226 struct fuse_write_out outarg;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001227 struct fuse_file_info fi;
1228
1229 memset(&fi, 0, sizeof(fi));
1230 fi.fh = arg->fh;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001231 fi.writepage = arg->write_flags & 1;
Miklos Szeredia181e612001-11-06 12:03:23 +00001232
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001233 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001234 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001235 if (path != NULL) {
1236 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi1eea0322004-09-27 18:50:11 +00001237 printf("WRITE%s[%lu] %u bytes to %llu\n",
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001238 (arg->write_flags & 1) ? "PAGE" : "",
1239 (unsigned long) arg->fh, arg->size, arg->offset);
Miklos Szeredi6ebe2342002-01-06 21:44:16 +00001240 fflush(stdout);
1241 }
1242
Miklos Szeredia181e612001-11-06 12:03:23 +00001243 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001244 if (f->op.write)
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001245 res = f->op.write(path, PARAM(arg), arg->size, arg->offset, &fi);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001246 free(path);
Miklos Szeredia181e612001-11-06 12:03:23 +00001247 }
1248
Miklos Szerediad051c32004-07-02 09:22:50 +00001249 if (res >= 0) {
1250 outarg.size = res;
1251 res = 0;
Miklos Szeredia181e612001-11-06 12:03:23 +00001252 }
1253
Miklos Szerediad051c32004-07-02 09:22:50 +00001254 send_reply(f, in, res, &outarg, sizeof(outarg));
Miklos Szeredia181e612001-11-06 12:03:23 +00001255}
1256
Miklos Szeredi77f39942004-03-25 11:17:52 +00001257static int default_statfs(struct statfs *buf)
1258{
1259 buf->f_namelen = 255;
1260 buf->f_bsize = 512;
1261 return 0;
1262}
1263
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001264static void convert_statfs_compat(struct fuse_statfs_compat1 *compatbuf,
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001265 struct statfs *statfs)
1266{
1267 statfs->f_bsize = compatbuf->block_size;
1268 statfs->f_blocks = compatbuf->blocks;
1269 statfs->f_bfree = compatbuf->blocks_free;
1270 statfs->f_bavail = compatbuf->blocks_free;
1271 statfs->f_files = compatbuf->files;
1272 statfs->f_ffree = compatbuf->files_free;
1273 statfs->f_namelen = compatbuf->namelen;
1274}
1275
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001276static void convert_statfs(struct statfs *statfs, struct fuse_kstatfs *kstatfs)
1277{
1278 kstatfs->bsize = statfs->f_bsize;
1279 kstatfs->blocks = statfs->f_blocks;
1280 kstatfs->bfree = statfs->f_bfree;
1281 kstatfs->bavail = statfs->f_bavail;
1282 kstatfs->files = statfs->f_files;
1283 kstatfs->ffree = statfs->f_ffree;
1284 kstatfs->namelen = statfs->f_namelen;
1285}
1286
Mark Glinesd84b39a2002-01-07 16:32:02 +00001287static void do_statfs(struct fuse *f, struct fuse_in_header *in)
1288{
1289 int res;
Mark Glinesd84b39a2002-01-07 16:32:02 +00001290 struct fuse_statfs_out arg;
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001291 struct statfs buf;
Mark Glinesd84b39a2002-01-07 16:32:02 +00001292
Miklos Szeredi77f39942004-03-25 11:17:52 +00001293 memset(&buf, 0, sizeof(struct statfs));
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001294 if (f->op.statfs) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001295 if (!f->compat || f->compat > 11)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001296 res = f->op.statfs("/", &buf);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001297 else {
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001298 struct fuse_statfs_compat1 compatbuf;
1299 memset(&compatbuf, 0, sizeof(struct fuse_statfs_compat1));
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001300 res = ((struct fuse_operations_compat1 *) &f->op)->statfs(&compatbuf);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001301 if (res == 0)
1302 convert_statfs_compat(&compatbuf, &buf);
1303 }
1304 }
Miklos Szeredi77f39942004-03-25 11:17:52 +00001305 else
1306 res = default_statfs(&buf);
1307
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001308 if (res == 0)
Miklos Szeredi77f39942004-03-25 11:17:52 +00001309 convert_statfs(&buf, &arg.st);
Miklos Szeredi4b2bef42002-01-09 12:23:27 +00001310
Mark Glinesd84b39a2002-01-07 16:32:02 +00001311 send_reply(f, in, res, &arg, sizeof(arg));
1312}
1313
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001314static void do_fsync(struct fuse *f, struct fuse_in_header *in,
1315 struct fuse_fsync_in *inarg)
1316{
1317 int res;
1318 char *path;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001319 struct fuse_file_info fi;
1320
1321 memset(&fi, 0, sizeof(fi));
1322 fi.fh = inarg->fh;
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001323
1324 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001325 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001326 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001327 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001328 printf("FSYNC[%lu]\n", (unsigned long) inarg->fh);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001329 fflush(stdout);
1330 }
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001331 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001332 if (f->op.fsync)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001333 res = f->op.fsync(path, inarg->fsync_flags & 1, &fi);
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001334 free(path);
1335 }
1336 send_reply(f, in, res, NULL, 0);
1337}
1338
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001339static void do_setxattr(struct fuse *f, struct fuse_in_header *in,
1340 struct fuse_setxattr_in *arg)
1341{
1342 int res;
1343 char *path;
1344 char *name = PARAM(arg);
1345 unsigned char *value = name + strlen(name) + 1;
1346
1347 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001348 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001349 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001350 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001351 if (f->op.setxattr)
1352 res = f->op.setxattr(path, name, value, arg->size, arg->flags);
1353 free(path);
1354 }
1355 send_reply(f, in, res, NULL, 0);
1356}
1357
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001358static int common_getxattr(struct fuse *f, struct fuse_in_header *in,
1359 const char *name, char *value, size_t size)
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001360{
1361 int res;
1362 char *path;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001363
1364 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001365 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001366 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001367 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001368 if (f->op.getxattr)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001369 res = f->op.getxattr(path, name, value, size);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001370 free(path);
1371 }
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001372 return res;
1373}
1374
1375static void do_getxattr_read(struct fuse *f, struct fuse_in_header *in,
1376 const char *name, size_t size)
1377{
1378 int res;
1379 char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001380 if (outbuf == NULL)
1381 send_reply(f, in, -ENOMEM, NULL, 0);
1382 else {
1383 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
1384 char *value = outbuf + sizeof(struct fuse_out_header);
1385
1386 res = common_getxattr(f, in, name, value, size);
1387 size = 0;
1388 if (res > 0) {
1389 size = res;
1390 res = 0;
1391 }
1392 memset(out, 0, sizeof(struct fuse_out_header));
1393 out->unique = in->unique;
1394 out->error = res;
1395
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001396 send_reply_raw(f, outbuf, sizeof(struct fuse_out_header) + size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001397 free(outbuf);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001398 }
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001399}
1400
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001401static void do_getxattr_size(struct fuse *f, struct fuse_in_header *in,
1402 const char *name)
1403{
1404 int res;
1405 struct fuse_getxattr_out arg;
1406
1407 res = common_getxattr(f, in, name, NULL, 0);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001408 if (res >= 0) {
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001409 arg.size = res;
1410 res = 0;
1411 }
1412 send_reply(f, in, res, &arg, sizeof(arg));
1413}
1414
1415static void do_getxattr(struct fuse *f, struct fuse_in_header *in,
1416 struct fuse_getxattr_in *arg)
1417{
1418 char *name = PARAM(arg);
1419
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001420 if (arg->size)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001421 do_getxattr_read(f, in, name, arg->size);
1422 else
1423 do_getxattr_size(f, in, name);
1424}
1425
1426static int common_listxattr(struct fuse *f, struct fuse_in_header *in,
1427 char *list, size_t size)
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001428{
1429 int res;
1430 char *path;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001431
1432 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001433 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001434 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001435 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001436 if (f->op.listxattr)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001437 res = f->op.listxattr(path, list, size);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001438 free(path);
1439 }
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001440 return res;
1441}
1442
1443static void do_listxattr_read(struct fuse *f, struct fuse_in_header *in,
1444 size_t size)
1445{
1446 int res;
1447 char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001448 if (outbuf == NULL)
1449 send_reply(f, in, -ENOMEM, NULL, 0);
1450 else {
1451 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
1452 char *list = outbuf + sizeof(struct fuse_out_header);
1453
1454 res = common_listxattr(f, in, list, size);
1455 size = 0;
1456 if (res > 0) {
1457 size = res;
1458 res = 0;
1459 }
1460 memset(out, 0, sizeof(struct fuse_out_header));
1461 out->unique = in->unique;
1462 out->error = res;
1463
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001464 send_reply_raw(f, outbuf, sizeof(struct fuse_out_header) + size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001465 free(outbuf);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001466 }
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001467}
1468
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001469static void do_listxattr_size(struct fuse *f, struct fuse_in_header *in)
1470{
1471 int res;
1472 struct fuse_getxattr_out arg;
1473
1474 res = common_listxattr(f, in, NULL, 0);
1475 if (res >= 0) {
1476 arg.size = res;
1477 res = 0;
1478 }
1479 send_reply(f, in, res, &arg, sizeof(arg));
1480}
1481
1482static void do_listxattr(struct fuse *f, struct fuse_in_header *in,
1483 struct fuse_getxattr_in *arg)
1484{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001485 if (arg->size)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001486 do_listxattr_read(f, in, arg->size);
1487 else
1488 do_listxattr_size(f, in);
1489}
1490
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001491static void do_removexattr(struct fuse *f, struct fuse_in_header *in,
1492 char *name)
1493{
1494 int res;
1495 char *path;
1496
1497 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001498 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001499 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001500 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001501 if (f->op.removexattr)
1502 res = f->op.removexattr(path, name);
1503 free(path);
1504 }
1505 send_reply(f, in, res, NULL, 0);
1506}
1507
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001508static void do_init(struct fuse *f, struct fuse_in_header *in,
1509 struct fuse_init_in_out *arg)
1510{
1511 struct fuse_init_in_out outarg;
1512 if (f->flags & FUSE_DEBUG) {
1513 printf(" INIT: %u.%u\n", arg->major, arg->minor);
1514 fflush(stdout);
1515 }
1516 f->got_init = 1;
1517 memset(&outarg, 0, sizeof(outarg));
1518 outarg.major = FUSE_KERNEL_VERSION;
1519 outarg.minor = FUSE_KERNEL_MINOR_VERSION;
1520 send_reply(f, in, 0, &outarg, sizeof(outarg));
1521}
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001522
Miklos Szeredi43696432001-11-18 19:15:05 +00001523static void free_cmd(struct fuse_cmd *cmd)
1524{
1525 free(cmd->buf);
1526 free(cmd);
1527}
1528
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001529void fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd)
Miklos Szeredia181e612001-11-06 12:03:23 +00001530{
Miklos Szeredia181e612001-11-06 12:03:23 +00001531 struct fuse_in_header *in = (struct fuse_in_header *) cmd->buf;
1532 void *inarg = cmd->buf + sizeof(struct fuse_in_header);
1533 size_t argsize;
Miklos Szeredid169f312004-09-22 08:48:26 +00001534 struct fuse_context *ctx = fuse_get_context();
Miklos Szeredia181e612001-11-06 12:03:23 +00001535
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001536 fuse_dec_avail(f);
Miklos Szeredi33232032001-11-19 17:55:51 +00001537
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001538 if ((f->flags & FUSE_DEBUG)) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001539 printf("unique: %llu, opcode: %s (%i), nodeid: %lu, insize: %i\n",
1540 in->unique, opname(in->opcode), in->opcode,
1541 (unsigned long) in->nodeid, cmd->buflen);
Miklos Szeredic0938ea2001-11-07 12:35:06 +00001542 fflush(stdout);
1543 }
Miklos Szeredife25def2001-12-20 15:38:05 +00001544
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001545 if (!f->got_init && in->opcode != FUSE_INIT) {
1546 /* Old kernel version probably */
1547 send_reply(f, in, -EPROTO, NULL, 0);
1548 goto out;
1549 }
1550
Miklos Szeredid169f312004-09-22 08:48:26 +00001551 ctx->fuse = f;
Miklos Szeredife25def2001-12-20 15:38:05 +00001552 ctx->uid = in->uid;
1553 ctx->gid = in->gid;
Miklos Szeredi1f18db52004-09-27 06:54:49 +00001554 ctx->pid = in->pid;
Miklos Szeredia181e612001-11-06 12:03:23 +00001555
1556 argsize = cmd->buflen - sizeof(struct fuse_in_header);
1557
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001558 switch (in->opcode) {
Miklos Szeredia181e612001-11-06 12:03:23 +00001559 case FUSE_LOOKUP:
1560 do_lookup(f, in, (char *) inarg);
1561 break;
1562
Miklos Szeredia181e612001-11-06 12:03:23 +00001563 case FUSE_GETATTR:
1564 do_getattr(f, in);
1565 break;
1566
1567 case FUSE_SETATTR:
1568 do_setattr(f, in, (struct fuse_setattr_in *) inarg);
1569 break;
1570
1571 case FUSE_READLINK:
1572 do_readlink(f, in);
1573 break;
1574
1575 case FUSE_GETDIR:
1576 do_getdir(f, in);
1577 break;
1578
1579 case FUSE_MKNOD:
1580 do_mknod(f, in, (struct fuse_mknod_in *) inarg);
1581 break;
1582
1583 case FUSE_MKDIR:
1584 do_mkdir(f, in, (struct fuse_mkdir_in *) inarg);
1585 break;
1586
1587 case FUSE_UNLINK:
Miklos Szeredib5958612004-02-20 14:10:49 +00001588 do_unlink(f, in, (char *) inarg);
1589 break;
1590
Miklos Szeredia181e612001-11-06 12:03:23 +00001591 case FUSE_RMDIR:
Miklos Szeredib5958612004-02-20 14:10:49 +00001592 do_rmdir(f, in, (char *) inarg);
Miklos Szeredia181e612001-11-06 12:03:23 +00001593 break;
1594
1595 case FUSE_SYMLINK:
1596 do_symlink(f, in, (char *) inarg,
1597 ((char *) inarg) + strlen((char *) inarg) + 1);
1598 break;
1599
1600 case FUSE_RENAME:
1601 do_rename(f, in, (struct fuse_rename_in *) inarg);
1602 break;
1603
1604 case FUSE_LINK:
1605 do_link(f, in, (struct fuse_link_in *) inarg);
1606 break;
1607
1608 case FUSE_OPEN:
1609 do_open(f, in, (struct fuse_open_in *) inarg);
1610 break;
1611
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001612 case FUSE_FLUSH:
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001613 do_flush(f, in, (struct fuse_flush_in *) inarg);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001614 break;
1615
Miklos Szeredi9478e862002-12-11 09:50:26 +00001616 case FUSE_RELEASE:
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001617 do_release(f, in, (struct fuse_release_in *) inarg);
Miklos Szeredi9478e862002-12-11 09:50:26 +00001618 break;
1619
Miklos Szeredia181e612001-11-06 12:03:23 +00001620 case FUSE_READ:
1621 do_read(f, in, (struct fuse_read_in *) inarg);
1622 break;
1623
1624 case FUSE_WRITE:
1625 do_write(f, in, (struct fuse_write_in *) inarg);
1626 break;
1627
Mark Glinesd84b39a2002-01-07 16:32:02 +00001628 case FUSE_STATFS:
1629 do_statfs(f, in);
1630 break;
1631
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001632 case FUSE_FSYNC:
1633 do_fsync(f, in, (struct fuse_fsync_in *) inarg);
1634 break;
1635
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001636 case FUSE_SETXATTR:
1637 do_setxattr(f, in, (struct fuse_setxattr_in *) inarg);
1638 break;
1639
1640 case FUSE_GETXATTR:
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001641 do_getxattr(f, in, (struct fuse_getxattr_in *) inarg);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001642 break;
1643
1644 case FUSE_LISTXATTR:
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001645 do_listxattr(f, in, (struct fuse_getxattr_in *) inarg);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001646 break;
1647
1648 case FUSE_REMOVEXATTR:
1649 do_removexattr(f, in, (char *) inarg);
1650 break;
1651
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001652 case FUSE_INIT:
1653 do_init(f, in, (struct fuse_init_in_out *) inarg);
1654 break;
1655
Miklos Szeredia181e612001-11-06 12:03:23 +00001656 default:
Miklos Szeredi43696432001-11-18 19:15:05 +00001657 send_reply(f, in, -ENOSYS, NULL, 0);
Miklos Szeredia181e612001-11-06 12:03:23 +00001658 }
Miklos Szeredi43696432001-11-18 19:15:05 +00001659
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001660 out:
Miklos Szeredi43696432001-11-18 19:15:05 +00001661 free_cmd(cmd);
Miklos Szeredia181e612001-11-06 12:03:23 +00001662}
1663
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001664int fuse_exited(struct fuse* f)
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001665{
1666 return f->exited;
1667}
1668
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001669struct fuse_cmd *fuse_read_cmd(struct fuse *f)
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001670{
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001671 ssize_t res;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001672 struct fuse_cmd *cmd;
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001673 struct fuse_in_header *in;
1674 void *inarg;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001675
Miklos Szeredi43696432001-11-18 19:15:05 +00001676 cmd = (struct fuse_cmd *) malloc(sizeof(struct fuse_cmd));
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001677 if (cmd == NULL) {
1678 fprintf(stderr, "fuse: failed to allocate cmd in read\n");
1679 return NULL;
1680 }
Miklos Szeredi43696432001-11-18 19:15:05 +00001681 cmd->buf = (char *) malloc(FUSE_MAX_IN);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001682 if (cmd->buf == NULL) {
1683 fprintf(stderr, "fuse: failed to allocate read buffer\n");
1684 free(cmd);
1685 return NULL;
1686 }
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001687 in = (struct fuse_in_header *) cmd->buf;
1688 inarg = cmd->buf + sizeof(struct fuse_in_header);
Miklos Szeredi43696432001-11-18 19:15:05 +00001689
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001690 res = read(f->fd, cmd->buf, FUSE_MAX_IN);
1691 if (res == -1) {
1692 free_cmd(cmd);
Miklos Szeredie56818b2004-12-12 11:45:24 +00001693 if (fuse_exited(f) || errno == EINTR || errno == ENOENT)
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001694 return NULL;
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001695
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001696 /* ENODEV means we got unmounted, so we silenty return failure */
1697 if (errno != ENODEV) {
1698 /* BAD... This will happen again */
1699 perror("fuse: reading device");
1700 }
1701
1702 fuse_exit(f);
1703 return NULL;
1704 }
1705 if ((size_t) res < sizeof(struct fuse_in_header)) {
1706 free_cmd(cmd);
1707 /* Cannot happen */
1708 fprintf(stderr, "short read on fuse device\n");
1709 fuse_exit(f);
1710 return NULL;
1711 }
1712 cmd->buflen = res;
1713
1714 /* Forget is special, it can be done without messing with threads. */
1715 if (in->opcode == FUSE_FORGET) {
1716 do_forget(f, in, (struct fuse_forget_in *) inarg);
1717 free_cmd(cmd);
1718 return NULL;
1719 }
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001720
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001721 return cmd;
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001722}
1723
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001724int fuse_loop(struct fuse *f)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001725{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001726 if (f == NULL)
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001727 return -1;
Miklos Szeredic40748a2004-02-20 16:38:45 +00001728
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001729 while (1) {
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001730 struct fuse_cmd *cmd;
1731
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001732 if (fuse_exited(f))
Miklos Szeredi874e3c12004-11-01 23:15:20 +00001733 break;
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001734
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001735 cmd = fuse_read_cmd(f);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001736 if (cmd == NULL)
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001737 continue;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001738
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001739 fuse_process_cmd(f, cmd);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001740 }
Miklos Szeredi874e3c12004-11-01 23:15:20 +00001741 f->exited = 0;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001742 return 0;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001743}
1744
Miklos Szeredi891b8742004-07-29 09:27:49 +00001745int fuse_invalidate(struct fuse *f, const char *path)
1746{
Miklos Szeredie56818b2004-12-12 11:45:24 +00001747 (void) f;
1748 (void) path;
1749 return -EINVAL;
Miklos Szeredi891b8742004-07-29 09:27:49 +00001750}
1751
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001752void fuse_exit(struct fuse *f)
1753{
1754 f->exited = 1;
1755}
1756
Miklos Szeredid169f312004-09-22 08:48:26 +00001757struct fuse_context *fuse_get_context()
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001758{
Miklos Szeredid169f312004-09-22 08:48:26 +00001759 static struct fuse_context context;
1760 if (fuse_getcontext)
1761 return fuse_getcontext();
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001762 else
Miklos Szeredid169f312004-09-22 08:48:26 +00001763 return &context;
1764}
1765
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001766void fuse_set_getcontext_func(struct fuse_context *(*func)(void))
Miklos Szeredid169f312004-09-22 08:48:26 +00001767{
1768 fuse_getcontext = func;
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001769}
1770
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001771int fuse_is_lib_option(const char *opt)
1772{
1773 if (strcmp(opt, "debug") == 0 ||
Miklos Szeredia13d9002004-11-02 17:32:03 +00001774 strcmp(opt, "hard_remove") == 0 ||
1775 strcmp(opt, "use_ino") == 0)
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001776 return 1;
1777 else
1778 return 0;
1779}
1780
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001781static int parse_lib_opts(struct fuse *f, const char *opts)
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001782{
1783 if (opts) {
1784 char *xopts = strdup(opts);
1785 char *s = xopts;
1786 char *opt;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001787
Miklos Szeredie56818b2004-12-12 11:45:24 +00001788 if (xopts == NULL) {
1789 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001790 return -1;
Miklos Szeredie56818b2004-12-12 11:45:24 +00001791 }
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001792
1793 while((opt = strsep(&s, ","))) {
1794 if (strcmp(opt, "debug") == 0)
1795 f->flags |= FUSE_DEBUG;
1796 else if (strcmp(opt, "hard_remove") == 0)
1797 f->flags |= FUSE_HARD_REMOVE;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001798 else if (strcmp(opt, "use_ino") == 0)
1799 f->flags |= FUSE_USE_INO;
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001800 else
1801 fprintf(stderr, "fuse: warning: unknown option `%s'\n", opt);
1802 }
1803 free(xopts);
1804 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001805 return 0;
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001806}
1807
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001808struct fuse *fuse_new_common(int fd, const char *opts,
1809 const struct fuse_operations *op,
1810 size_t op_size, int compat)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001811{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001812 struct fuse *f;
1813 struct node *root;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001814
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001815 if (sizeof(struct fuse_operations) < op_size) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001816 fprintf(stderr, "fuse: warning: library too old, some operations may not not work\n");
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001817 op_size = sizeof(struct fuse_operations);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001818 }
1819
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001820 f = (struct fuse *) calloc(1, sizeof(struct fuse));
Miklos Szeredie56818b2004-12-12 11:45:24 +00001821 if (f == NULL) {
1822 fprintf(stderr, "fuse: failed to allocate fuse object\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001823 goto out;
Miklos Szeredie56818b2004-12-12 11:45:24 +00001824 }
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001825
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001826 if (parse_lib_opts(f, opts) == -1)
1827 goto out_free;
1828
Miklos Szeredi8cffdb92001-11-09 14:49:18 +00001829 f->fd = fd;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001830 f->ctr = 0;
Miklos Szeredi76f65782004-02-19 16:55:40 +00001831 f->generation = 0;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001832 /* FIXME: Dynamic hash table */
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001833 f->name_table_size = 14057;
1834 f->name_table = (struct node **)
1835 calloc(1, sizeof(struct node *) * f->name_table_size);
Miklos Szeredie56818b2004-12-12 11:45:24 +00001836 if (f->name_table == NULL) {
1837 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001838 goto out_free;
Miklos Szeredie56818b2004-12-12 11:45:24 +00001839 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001840
Miklos Szeredia13d9002004-11-02 17:32:03 +00001841 f->id_table_size = 14057;
1842 f->id_table = (struct node **)
1843 calloc(1, sizeof(struct node *) * f->id_table_size);
Miklos Szeredie56818b2004-12-12 11:45:24 +00001844 if (f->id_table == NULL) {
1845 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001846 goto out_free_name_table;
Miklos Szeredie56818b2004-12-12 11:45:24 +00001847 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001848
Miklos Szeredia25d4c22004-11-23 22:32:16 +00001849#ifndef USE_UCLIBC
1850 pthread_mutex_init(&f->lock, NULL);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001851 pthread_mutex_init(&f->worker_lock, NULL);
Miklos Szeredia25d4c22004-11-23 22:32:16 +00001852#else
1853 {
1854 pthread_mutexattr_t attr;
1855 pthread_mutexattr_init(&attr);
1856 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
1857 pthread_mutex_init(&f->lock, &attr);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001858 pthread_mutex_init(&f->worker_lock, &attr);
Miklos Szeredia25d4c22004-11-23 22:32:16 +00001859 pthread_mutexattr_destroy(&attr);
1860 }
1861#endif
Miklos Szeredi33232032001-11-19 17:55:51 +00001862 f->numworker = 0;
1863 f->numavail = 0;
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001864 memcpy(&f->op, op, op_size);
1865 f->compat = compat;
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001866 f->exited = 0;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001867
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001868 root = (struct node *) calloc(1, sizeof(struct node));
Miklos Szeredie56818b2004-12-12 11:45:24 +00001869 if (root == NULL) {
1870 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredia13d9002004-11-02 17:32:03 +00001871 goto out_free_id_table;
Miklos Szeredie56818b2004-12-12 11:45:24 +00001872 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001873
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001874 root->name = strdup("/");
Miklos Szeredie56818b2004-12-12 11:45:24 +00001875 if (root->name == NULL) {
1876 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001877 goto out_free_root;
Miklos Szeredie56818b2004-12-12 11:45:24 +00001878 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001879
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001880 root->parent = 0;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001881 root->nodeid = FUSE_ROOT_ID;
Miklos Szeredi76f65782004-02-19 16:55:40 +00001882 root->generation = 0;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001883 root->refctr = 1;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001884 hash_id(f, root);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001885
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001886 return f;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001887
1888 out_free_root:
1889 free(root);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001890 out_free_id_table:
1891 free(f->id_table);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001892 out_free_name_table:
1893 free(f->name_table);
1894 out_free:
1895 free(f);
1896 out:
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001897 return NULL;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001898}
1899
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001900struct fuse *fuse_new(int fd, const char *opts,
1901 const struct fuse_operations *op, size_t op_size)
1902{
1903 return fuse_new_common(fd, opts, op, op_size, 0);
1904}
1905
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001906struct fuse *fuse_new_compat2(int fd, const char *opts,
1907 const struct fuse_operations_compat2 *op)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001908{
1909 return fuse_new_common(fd, opts, (struct fuse_operations *) op,
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001910 sizeof(struct fuse_operations_compat2), 21);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001911}
1912
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001913struct fuse *fuse_new_compat1(int fd, int flags,
1914 const struct fuse_operations_compat1 *op)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001915{
1916 char *opts = NULL;
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001917 if (flags & FUSE_DEBUG_COMPAT1)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001918 opts = "debug";
1919 return fuse_new_common(fd, opts, (struct fuse_operations *) op,
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001920 sizeof(struct fuse_operations_compat1), 11);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001921}
1922
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001923void fuse_destroy(struct fuse *f)
1924{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001925 size_t i;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001926 for (i = 0; i < f->id_table_size; i++) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001927 struct node *node;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001928
Miklos Szeredia13d9002004-11-02 17:32:03 +00001929 for (node = f->id_table[i]; node != NULL; node = node->id_next) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001930 if (node->is_hidden) {
Miklos Szeredia13d9002004-11-02 17:32:03 +00001931 char *path = get_path(f, node->nodeid);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001932 if (path)
1933 f->op.unlink(path);
1934 }
1935 }
1936 }
Miklos Szeredia13d9002004-11-02 17:32:03 +00001937 for (i = 0; i < f->id_table_size; i++) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001938 struct node *node;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001939 struct node *next;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001940
Miklos Szeredia13d9002004-11-02 17:32:03 +00001941 for (node = f->id_table[i]; node != NULL; node = next) {
1942 next = node->id_next;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001943 free_node(node);
1944 }
1945 }
Miklos Szeredia13d9002004-11-02 17:32:03 +00001946 free(f->id_table);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001947 free(f->name_table);
Miklos Szeredia181e612001-11-06 12:03:23 +00001948 pthread_mutex_destroy(&f->lock);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001949 pthread_mutex_destroy(&f->worker_lock);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001950 free(f);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001951}
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001952
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001953__asm__(".symver fuse_exited,__fuse_exited@");
1954__asm__(".symver fuse_process_cmd,__fuse_process_cmd@");
1955__asm__(".symver fuse_read_cmd,__fuse_read_cmd@");
1956__asm__(".symver fuse_set_getcontext_func,__fuse_set_getcontext_func@");
1957__asm__(".symver fuse_new_compat2,fuse_new@");