blob: d61c6c8f498ef8e5f488285fda22ae8ccaadfed4 [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 Szeredib9b94cd2004-12-01 18:56:39 +000011#include "fuse_kernel.h"
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000012
13#include <string.h>
Miklos Szeredi97c61e92001-11-07 12:09:43 +000014#include <stdlib.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000015#include <unistd.h>
Miklos Szeredi97c61e92001-11-07 12:09:43 +000016#include <limits.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000017#include <errno.h>
Miklos Szeredi019b4e92001-12-26 18:08:09 +000018#include <sys/param.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000019
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +000020#define FUSE_KERNEL_MINOR_VERSION_NEED 1
Miklos Szeredia25d4c22004-11-23 22:32:16 +000021#define FUSE_VERSION_FILE_OLD "/proc/fs/fuse/version"
Miklos Szeredi162bcbb2004-11-29 23:43:44 +000022#define FUSE_VERSION_FILE_NEW "/sys/fs/fuse/version"
Miklos Szeredia25d4c22004-11-23 22:32:16 +000023#define FUSE_DEV_OLD "/proc/fs/fuse/dev"
24
Miklos Szeredi97c61e92001-11-07 12:09:43 +000025#define FUSE_MAX_PATH 4096
Miklos Szeredi6bf8b682002-10-28 08:49:39 +000026#define PARAM(inarg) (((char *)(inarg)) + sizeof(*inarg))
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000027
Miklos Szeredi254d5ed2004-03-02 11:11:24 +000028#define ENTRY_REVALIDATE_TIME 1 /* sec */
29#define ATTR_REVALIDATE_TIME 1 /* sec */
30
Miklos Szeredid169f312004-09-22 08:48:26 +000031static struct fuse_context *(*fuse_getcontext)(void) = NULL;
32
Miklos Szeredic8ba2372002-12-10 12:26:00 +000033static const char *opname(enum fuse_opcode opcode)
34{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +000035 switch (opcode) {
Miklos Szeredi3ed84232004-03-30 15:17:26 +000036 case FUSE_LOOKUP: return "LOOKUP";
37 case FUSE_FORGET: return "FORGET";
38 case FUSE_GETATTR: return "GETATTR";
39 case FUSE_SETATTR: return "SETATTR";
40 case FUSE_READLINK: return "READLINK";
41 case FUSE_SYMLINK: return "SYMLINK";
42 case FUSE_GETDIR: return "GETDIR";
43 case FUSE_MKNOD: return "MKNOD";
44 case FUSE_MKDIR: return "MKDIR";
45 case FUSE_UNLINK: return "UNLINK";
46 case FUSE_RMDIR: return "RMDIR";
47 case FUSE_RENAME: return "RENAME";
48 case FUSE_LINK: return "LINK";
49 case FUSE_OPEN: return "OPEN";
50 case FUSE_READ: return "READ";
51 case FUSE_WRITE: return "WRITE";
52 case FUSE_STATFS: return "STATFS";
Miklos Szeredi99f20742004-05-19 08:01:10 +000053 case FUSE_FLUSH: return "FLUSH";
Miklos Szeredi3ed84232004-03-30 15:17:26 +000054 case FUSE_RELEASE: return "RELEASE";
55 case FUSE_FSYNC: return "FSYNC";
56 case FUSE_SETXATTR: return "SETXATTR";
57 case FUSE_GETXATTR: return "GETXATTR";
58 case FUSE_LISTXATTR: return "LISTXATTR";
59 case FUSE_REMOVEXATTR: return "REMOVEXATTR";
Miklos Szeredi99f20742004-05-19 08:01:10 +000060 default: return "???";
Miklos Szeredic8ba2372002-12-10 12:26:00 +000061 }
62}
63
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +000064
65static inline void dec_avail(struct fuse *f)
66{
67 pthread_mutex_lock(&f->lock);
68 f->numavail --;
69 pthread_mutex_unlock(&f->lock);
70}
71
Miklos Szeredia13d9002004-11-02 17:32:03 +000072static struct node *__get_node(struct fuse *f, nodeid_t nodeid)
Miklos Szeredia181e612001-11-06 12:03:23 +000073{
Miklos Szeredia13d9002004-11-02 17:32:03 +000074 size_t hash = nodeid % f->id_table_size;
Miklos Szeredi97c61e92001-11-07 12:09:43 +000075 struct node *node;
76
Miklos Szeredia13d9002004-11-02 17:32:03 +000077 for (node = f->id_table[hash]; node != NULL; node = node->id_next)
78 if (node->nodeid == nodeid)
Miklos Szeredi97c61e92001-11-07 12:09:43 +000079 return node;
80
81 return NULL;
Miklos Szeredia181e612001-11-06 12:03:23 +000082}
83
Miklos Szeredia13d9002004-11-02 17:32:03 +000084static struct node *get_node(struct fuse *f, nodeid_t nodeid)
Miklos Szeredia181e612001-11-06 12:03:23 +000085{
Miklos Szeredia13d9002004-11-02 17:32:03 +000086 struct node *node = __get_node(f, nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +000087 if (node != NULL)
Miklos Szeredi97c61e92001-11-07 12:09:43 +000088 return node;
89
Miklos Szeredia13d9002004-11-02 17:32:03 +000090 fprintf(stderr, "fuse internal error: inode %lu not found\n", nodeid);
Miklos Szeredi97c61e92001-11-07 12:09:43 +000091 abort();
Miklos Szeredia181e612001-11-06 12:03:23 +000092}
93
Miklos Szeredia13d9002004-11-02 17:32:03 +000094static void hash_id(struct fuse *f, struct node *node)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000095{
Miklos Szeredia13d9002004-11-02 17:32:03 +000096 size_t hash = node->nodeid % f->id_table_size;
97 node->id_next = f->id_table[hash];
98 f->id_table[hash] = node;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000099}
100
Miklos Szeredia13d9002004-11-02 17:32:03 +0000101static void unhash_id(struct fuse *f, struct node *node)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000102{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000103 size_t hash = node->nodeid % f->id_table_size;
104 struct node **nodep = &f->id_table[hash];
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000105
Miklos Szeredia13d9002004-11-02 17:32:03 +0000106 for (; *nodep != NULL; nodep = &(*nodep)->id_next)
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000107 if (*nodep == node) {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000108 *nodep = node->id_next;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000109 return;
110 }
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000111}
112
Miklos Szeredia13d9002004-11-02 17:32:03 +0000113static nodeid_t next_id(struct fuse *f)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000114{
Miklos Szeredi76f65782004-02-19 16:55:40 +0000115 do {
116 f->ctr++;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000117 if (!f->ctr)
Miklos Szeredi76f65782004-02-19 16:55:40 +0000118 f->generation ++;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000119 } while (f->ctr == 0 || __get_node(f, f->ctr) != NULL);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000120 return f->ctr;
121}
122
123static void free_node(struct node *node)
124{
125 free(node->name);
126 free(node);
127}
128
Miklos Szeredia13d9002004-11-02 17:32:03 +0000129static unsigned int name_hash(struct fuse *f, nodeid_t parent, const char *name)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000130{
131 unsigned int hash = *name;
132
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000133 if (hash)
134 for (name += 1; *name != '\0'; name++)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000135 hash = (hash << 5) - hash + *name;
136
137 return (hash + parent) % f->name_table_size;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000138}
139
Miklos Szeredia13d9002004-11-02 17:32:03 +0000140static struct node *__lookup_node(struct fuse *f, nodeid_t parent,
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000141 const char *name)
142{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000143 size_t hash = name_hash(f, parent, name);
144 struct node *node;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000145
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000146 for (node = f->name_table[hash]; node != NULL; node = node->name_next)
147 if (node->parent == parent && strcmp(node->name, name) == 0)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000148 return node;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000149
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000150 return NULL;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000151}
152
Miklos Szeredia13d9002004-11-02 17:32:03 +0000153static struct node *lookup_node(struct fuse *f, nodeid_t parent,
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000154 const char *name)
155{
156 struct node *node;
157
158 pthread_mutex_lock(&f->lock);
159 node = __lookup_node(f, parent, name);
160 pthread_mutex_unlock(&f->lock);
161 if (node != NULL)
162 return node;
163
164 fprintf(stderr, "fuse internal error: node %lu/%s not found\n", parent,
165 name);
166 abort();
167}
168
Miklos Szeredia13d9002004-11-02 17:32:03 +0000169static int hash_name(struct fuse *f, struct node *node, nodeid_t parent,
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000170 const char *name)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000171{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000172 size_t hash = name_hash(f, parent, name);
Miklos Szeredia181e612001-11-06 12:03:23 +0000173 node->parent = parent;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000174 node->name = strdup(name);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000175 if (node->name == NULL)
176 return -1;
177
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000178 node->name_next = f->name_table[hash];
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000179 f->name_table[hash] = node;
180 return 0;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000181}
182
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000183static void unhash_name(struct fuse *f, struct node *node)
Miklos Szeredia181e612001-11-06 12:03:23 +0000184{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000185 if (node->name != NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000186 size_t hash = name_hash(f, node->parent, node->name);
187 struct node **nodep = &f->name_table[hash];
188
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000189 for (; *nodep != NULL; nodep = &(*nodep)->name_next)
190 if (*nodep == node) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000191 *nodep = node->name_next;
192 node->name_next = NULL;
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",
Miklos Szeredia13d9002004-11-02 17:32:03 +0000199 node->nodeid);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000200 abort();
Miklos Szeredia181e612001-11-06 12:03:23 +0000201 }
202}
203
Miklos Szeredia13d9002004-11-02 17:32:03 +0000204static struct node *find_node(struct fuse *f, nodeid_t parent, char *name,
Miklos Szeredi76f65782004-02-19 16:55:40 +0000205 struct fuse_attr *attr, int version)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000206{
207 struct node *node;
Miklos Szeredia181e612001-11-06 12:03:23 +0000208 int mode = attr->mode & S_IFMT;
209 int rdev = 0;
210
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000211 if (S_ISCHR(mode) || S_ISBLK(mode))
Miklos Szeredia181e612001-11-06 12:03:23 +0000212 rdev = attr->rdev;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000213
Miklos Szeredia181e612001-11-06 12:03:23 +0000214 pthread_mutex_lock(&f->lock);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000215 node = __lookup_node(f, parent, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000216 if (node != NULL) {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000217 if (node->mode == mode && node->rdev == rdev &&
218 (!(f->flags & FUSE_USE_INO) || node->ino == attr->ino)) {
219 if (!(f->flags & FUSE_USE_INO))
220 attr->ino = node->nodeid;
221
Miklos Szeredia181e612001-11-06 12:03:23 +0000222 goto out;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000223 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000224
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000225 unhash_name(f, node);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000226 }
227
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000228 node = (struct node *) calloc(1, sizeof(struct node));
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000229 if (node == NULL)
Miklos Szeredic2309912004-09-21 13:40:38 +0000230 goto out_err;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000231
Miklos Szeredia13d9002004-11-02 17:32:03 +0000232 node->nodeid = next_id(f);
233 if (!(f->flags & FUSE_USE_INO))
234 attr->ino = node->nodeid;
Miklos Szeredia181e612001-11-06 12:03:23 +0000235 node->mode = mode;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000236 node->rdev = rdev;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000237 node->ino = attr->ino;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000238 node->open_count = 0;
239 node->is_hidden = 0;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000240 node->generation = f->generation;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000241 if (hash_name(f, node, parent, name) == -1) {
242 free(node);
Miklos Szeredic2309912004-09-21 13:40:38 +0000243 node = NULL;
244 goto out_err;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000245 }
Miklos Szeredia13d9002004-11-02 17:32:03 +0000246 hash_id(f, node);
Miklos Szeredia181e612001-11-06 12:03:23 +0000247
Miklos Szeredic2309912004-09-21 13:40:38 +0000248 out:
Miklos Szeredia181e612001-11-06 12:03:23 +0000249 node->version = version;
Miklos Szeredic2309912004-09-21 13:40:38 +0000250 out_err:
Miklos Szeredia181e612001-11-06 12:03:23 +0000251 pthread_mutex_unlock(&f->lock);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000252 return node;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000253}
254
Miklos Szeredia13d9002004-11-02 17:32:03 +0000255static int path_lookup(struct fuse *f, const char *path, nodeid_t *nodeidp,
256 unsigned long *inop)
Miklos Szeredi891b8742004-07-29 09:27:49 +0000257{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000258 nodeid_t nodeid;
259 unsigned long ino;
Miklos Szeredi891b8742004-07-29 09:27:49 +0000260 int err;
261 char *s;
262 char *name;
263 char *tmp = strdup(path);
264 if (!tmp)
265 return -ENOMEM;
266
267 pthread_mutex_lock(&f->lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000268 nodeid = FUSE_ROOT_ID;
269 ino = nodeid;
Miklos Szeredi891b8742004-07-29 09:27:49 +0000270 err = 0;
271 for (s = tmp; (name = strsep(&s, "/")) != NULL; ) {
272 if (name[0]) {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000273 struct node *node = __lookup_node(f, nodeid, name);
Miklos Szeredi891b8742004-07-29 09:27:49 +0000274 if (node == NULL) {
275 err = -ENOENT;
276 break;
277 }
Miklos Szeredia13d9002004-11-02 17:32:03 +0000278 nodeid = node->nodeid;
Miklos Szeredi891b8742004-07-29 09:27:49 +0000279 ino = node->ino;
280 }
281 }
282 pthread_mutex_unlock(&f->lock);
283 free(tmp);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000284 if (!err) {
285 *nodeidp = nodeid;
Miklos Szeredi891b8742004-07-29 09:27:49 +0000286 *inop = ino;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000287 }
Miklos Szeredi891b8742004-07-29 09:27:49 +0000288
289 return err;
290}
291
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000292static char *add_name(char *buf, char *s, const char *name)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000293{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000294 size_t len = strlen(name);
295 s -= len;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000296 if (s <= buf) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000297 fprintf(stderr, "fuse: path too long: ...%s\n", s + len);
298 return NULL;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000299 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000300 strncpy(s, name, len);
301 s--;
302 *s = '/';
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000303
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000304 return s;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000305}
306
Miklos Szeredia13d9002004-11-02 17:32:03 +0000307static char *get_path_name(struct fuse *f, nodeid_t nodeid, const char *name)
Miklos Szeredib483c932001-10-29 14:57:57 +0000308{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000309 char buf[FUSE_MAX_PATH];
310 char *s = buf + FUSE_MAX_PATH - 1;
311 struct node *node;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000312
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000313 *s = '\0';
Miklos Szeredia181e612001-11-06 12:03:23 +0000314
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000315 if (name != NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000316 s = add_name(buf, s, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000317 if (s == NULL)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000318 return NULL;
319 }
320
321 pthread_mutex_lock(&f->lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000322 for (node = get_node(f, nodeid); node->nodeid != FUSE_ROOT_ID;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000323 node = get_node(f, node->parent)) {
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000324 if (node->name == NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000325 s = NULL;
326 break;
327 }
328
329 s = add_name(buf, s, node->name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000330 if (s == NULL)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000331 break;
332 }
333 pthread_mutex_unlock(&f->lock);
334
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000335 if (s == NULL)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000336 return NULL;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000337 else if (*s == '\0')
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000338 return strdup("/");
339 else
340 return strdup(s);
341}
Miklos Szeredia181e612001-11-06 12:03:23 +0000342
Miklos Szeredia13d9002004-11-02 17:32:03 +0000343static char *get_path(struct fuse *f, nodeid_t nodeid)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000344{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000345 return get_path_name(f, nodeid, NULL);
Miklos Szeredib483c932001-10-29 14:57:57 +0000346}
347
Miklos Szeredia13d9002004-11-02 17:32:03 +0000348static void destroy_node(struct fuse *f, nodeid_t nodeid, int version)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000349{
Miklos Szeredia181e612001-11-06 12:03:23 +0000350 struct node *node;
351
352 pthread_mutex_lock(&f->lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000353 node = __get_node(f, nodeid);
354 if (node && node->version == version && nodeid != FUSE_ROOT_ID) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000355 unhash_name(f, node);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000356 unhash_id(f, node);
Miklos Szeredia181e612001-11-06 12:03:23 +0000357 free_node(node);
358 }
359 pthread_mutex_unlock(&f->lock);
360
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000361}
362
Miklos Szeredia13d9002004-11-02 17:32:03 +0000363static void remove_node(struct fuse *f, nodeid_t dir, const char *name)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000364{
Miklos Szeredia181e612001-11-06 12:03:23 +0000365 struct node *node;
366
367 pthread_mutex_lock(&f->lock);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000368 node = __lookup_node(f, dir, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000369 if (node == NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000370 fprintf(stderr, "fuse internal error: unable to remove node %lu/%s\n",
371 dir, name);
372 abort();
373 }
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 Szeredi1ea9c962004-06-24 21:00:00 +0000386 node = __lookup_node(f, olddir, oldname);
387 newnode = __lookup_node(f, newdir, newname);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000388 if (node == NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000389 fprintf(stderr, "fuse internal error: unable to rename node %lu/%s\n",
390 olddir, oldname);
391 abort();
392 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000393
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000394 if (newnode != NULL) {
395 if (hide) {
396 fprintf(stderr, "fuse: hidden file got created during hiding\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000397 err = -EBUSY;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000398 goto out;
399 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000400 unhash_name(f, newnode);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000401 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000402
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000403 unhash_name(f, node);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000404 if (hash_name(f, node, newdir, newname) == -1) {
405 err = -ENOMEM;
406 goto out;
407 }
408
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000409 if (hide)
410 node->is_hidden = 1;
411
412 out:
Miklos Szeredia181e612001-11-06 12:03:23 +0000413 pthread_mutex_unlock(&f->lock);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000414 return err;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000415}
416
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000417static void convert_stat(struct stat *stbuf, struct fuse_attr *attr)
418{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000419 attr->ino = stbuf->st_ino;
Miklos Szeredib5958612004-02-20 14:10:49 +0000420 attr->mode = stbuf->st_mode;
421 attr->nlink = stbuf->st_nlink;
422 attr->uid = stbuf->st_uid;
423 attr->gid = stbuf->st_gid;
424 attr->rdev = stbuf->st_rdev;
425 attr->size = stbuf->st_size;
426 attr->blocks = stbuf->st_blocks;
427 attr->atime = stbuf->st_atime;
Miklos Szeredib5958612004-02-20 14:10:49 +0000428 attr->mtime = stbuf->st_mtime;
Miklos Szeredib5958612004-02-20 14:10:49 +0000429 attr->ctime = stbuf->st_ctime;
Miklos Szeredicb264512004-06-23 18:52:50 +0000430#ifdef HAVE_STRUCT_STAT_ST_ATIM
431 attr->atimensec = stbuf->st_atim.tv_nsec;
432 attr->mtimensec = stbuf->st_mtim.tv_nsec;
Miklos Szeredib5958612004-02-20 14:10:49 +0000433 attr->ctimensec = stbuf->st_ctim.tv_nsec;
Miklos Szeredicb264512004-06-23 18:52:50 +0000434#endif
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000435}
436
Miklos Szeredi8fb48fe2004-11-08 14:48:52 +0000437static int fill_dir(struct fuse_dirhandle *dh, const char *name, int type,
438 ino_t ino)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000439{
440 struct fuse_dirent dirent;
441 size_t reclen;
442 size_t res;
443
Miklos Szeredi8fb48fe2004-11-08 14:48:52 +0000444 if ((dh->fuse->flags & FUSE_USE_INO))
445 dirent.ino = ino;
446 else
447 dirent.ino = (unsigned long) -1;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000448 dirent.namelen = strlen(name);
449 strncpy(dirent.name, name, sizeof(dirent.name));
450 dirent.type = type;
451 reclen = FUSE_DIRENT_SIZE(&dirent);
452 res = fwrite(&dirent, reclen, 1, dh->fp);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000453 if (res == 0) {
Miklos Szeredi96249982001-11-21 12:21:19 +0000454 perror("fuse: writing directory file");
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000455 return -EIO;
456 }
457 return 0;
458}
459
Miklos Szeredi73798f92004-07-12 15:55:11 +0000460static int send_reply_raw(struct fuse *f, char *outbuf, size_t outsize,
461 int locked)
Miklos Szeredi43696432001-11-18 19:15:05 +0000462{
463 int res;
464
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000465 if ((f->flags & FUSE_DEBUG)) {
Miklos Szeredi43696432001-11-18 19:15:05 +0000466 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
467 printf(" unique: %i, error: %i (%s), outsize: %i\n", out->unique,
468 out->error, strerror(-out->error), outsize);
469 fflush(stdout);
470 }
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000471
Miklos Szeredi4e71c9f2004-02-09 12:05:14 +0000472 /* This needs to be done before the reply, otherwise the scheduler
473 could play tricks with us, and only let the counter be increased
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000474 long after the operation is done */
Miklos Szeredi73798f92004-07-12 15:55:11 +0000475 if (!locked)
476 pthread_mutex_lock(&f->lock);
477 f->numavail ++;
478 if (!locked)
479 pthread_mutex_unlock(&f->lock);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000480
Miklos Szeredi43696432001-11-18 19:15:05 +0000481 res = write(f->fd, outbuf, outsize);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000482 if (res == -1) {
Miklos Szeredi43696432001-11-18 19:15:05 +0000483 /* ENOENT means the operation was interrupted */
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000484 if (!f->exited && errno != ENOENT)
Miklos Szeredi96249982001-11-21 12:21:19 +0000485 perror("fuse: writing device");
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000486 return -errno;
Miklos Szeredi43696432001-11-18 19:15:05 +0000487 }
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000488 return 0;
Miklos Szeredi43696432001-11-18 19:15:05 +0000489}
490
Miklos Szeredi73798f92004-07-12 15:55:11 +0000491static int __send_reply(struct fuse *f, struct fuse_in_header *in, int error,
492 void *arg, size_t argsize, int locked)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000493{
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000494 int res;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000495 char *outbuf;
496 size_t outsize;
497 struct fuse_out_header *out;
498
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000499 if (error <= -1000 || error > 0) {
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000500 fprintf(stderr, "fuse: bad error value: %i\n", error);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000501 error = -ERANGE;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000502 }
503
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000504 if (error)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000505 argsize = 0;
506
507 outsize = sizeof(struct fuse_out_header) + argsize;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000508 outbuf = (char *) malloc(outsize);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000509 if (outbuf == NULL) {
510 fprintf(stderr, "fuse: failed to allocate reply buffer\n");
511 res = -ENOMEM;
512 } else {
513 out = (struct fuse_out_header *) outbuf;
514 memset(out, 0, sizeof(struct fuse_out_header));
515 out->unique = in->unique;
516 out->error = error;
517 if (argsize != 0)
518 memcpy(outbuf + sizeof(struct fuse_out_header), arg, argsize);
519
520 res = send_reply_raw(f, outbuf, outsize, locked);
521 free(outbuf);
522 }
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000523
524 return res;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000525}
526
Miklos Szeredi73798f92004-07-12 15:55:11 +0000527static int send_reply(struct fuse *f, struct fuse_in_header *in, int error,
528 void *arg, size_t argsize)
529{
530 return __send_reply(f, in, error, arg, argsize, 0);
531}
532
Miklos Szeredia13d9002004-11-02 17:32:03 +0000533static int is_open(struct fuse *f, nodeid_t dir, const char *name)
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000534{
535 struct node *node;
536 int isopen = 0;
537 pthread_mutex_lock(&f->lock);
538 node = __lookup_node(f, dir, name);
539 if (node && node->open_count > 0)
540 isopen = 1;
541 pthread_mutex_unlock(&f->lock);
542 return isopen;
543}
544
Miklos Szeredia13d9002004-11-02 17:32:03 +0000545static char *hidden_name(struct fuse *f, nodeid_t dir, const char *oldname,
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000546 char *newname, size_t bufsize)
547{
548 struct stat buf;
549 struct node *node;
550 struct node *newnode;
551 char *newpath;
552 int res;
553 int failctr = 10;
554
555 if (!f->op.getattr)
556 return NULL;
557
558 do {
559 node = lookup_node(f, dir, oldname);
560 pthread_mutex_lock(&f->lock);
561 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 Szeredi1ea9c962004-06-24 21:00:00 +0000565 newnode = __lookup_node(f, dir, newname);
566 } 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 Szeredia13d9002004-11-02 17:32:03 +0000602static int lookup_path(struct fuse *f, nodeid_t nodeid, int version, char *name,
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000603 const char *path, struct fuse_entry_out *arg)
Miklos Szeredi76f65782004-02-19 16:55:40 +0000604{
605 int res;
606 struct stat buf;
607
608 res = f->op.getattr(path, &buf);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000609 if (res == 0) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000610 struct node *node;
611
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000612 memset(arg, 0, sizeof(struct fuse_entry_out));
Miklos Szeredi76f65782004-02-19 16:55:40 +0000613 convert_stat(&buf, &arg->attr);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000614 node = find_node(f, nodeid, name, &arg->attr, version);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000615 if (node == NULL)
616 res = -ENOMEM;
617 else {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000618 arg->nodeid = node->nodeid;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000619 arg->generation = node->generation;
620 arg->entry_valid = ENTRY_REVALIDATE_TIME;
621 arg->entry_valid_nsec = 0;
622 arg->attr_valid = ATTR_REVALIDATE_TIME;
623 arg->attr_valid_nsec = 0;
624 if (f->flags & FUSE_DEBUG) {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000625 printf(" NODEID: %li\n", arg->nodeid);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000626 fflush(stdout);
627 }
Miklos Szeredi76f65782004-02-19 16:55:40 +0000628 }
629 }
630 return res;
631}
632
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000633static void do_lookup(struct fuse *f, struct fuse_in_header *in, char *name)
634{
635 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000636 int res2;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000637 char *path;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000638 struct fuse_entry_out arg;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000639
Miklos Szeredi5e183482001-10-31 14:52:35 +0000640 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000641 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000642 if (path != NULL) {
643 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi6ebe2342002-01-06 21:44:16 +0000644 printf("LOOKUP %s\n", path);
645 fflush(stdout);
646 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000647 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000648 if (f->op.getattr)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000649 res = lookup_path(f, in->nodeid, in->unique, name, path, &arg);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000650 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000651 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000652 res2 = send_reply(f, in, res, &arg, sizeof(arg));
653 if (res == 0 && res2 == -ENOENT)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000654 destroy_node(f, arg.nodeid, in->unique);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000655}
656
Miklos Szeredia181e612001-11-06 12:03:23 +0000657static void do_forget(struct fuse *f, struct fuse_in_header *in,
658 struct fuse_forget_in *arg)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000659{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000660 if (f->flags & FUSE_DEBUG) {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000661 printf("FORGET %li/%i\n", in->nodeid, arg->version);
Miklos Szeredi43696432001-11-18 19:15:05 +0000662 fflush(stdout);
663 }
Miklos Szeredia13d9002004-11-02 17:32:03 +0000664 destroy_node(f, in->nodeid, arg->version);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000665}
666
667static void do_getattr(struct fuse *f, struct fuse_in_header *in)
668{
669 int res;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000670 char *path;
671 struct stat buf;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000672 struct fuse_attr_out arg;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000673
Miklos Szeredi5e183482001-10-31 14:52:35 +0000674 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000675 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000676 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +0000677 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000678 if (f->op.getattr)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000679 res = f->op.getattr(path, &buf);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000680 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000681 }
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000682
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000683 if (res == 0) {
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000684 memset(&arg, 0, sizeof(struct fuse_attr_out));
685 arg.attr_valid = ATTR_REVALIDATE_TIME;
686 arg.attr_valid_nsec = 0;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000687 convert_stat(&buf, &arg.attr);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000688 if (!(f->flags & FUSE_USE_INO))
689 arg.attr.ino = in->nodeid;
690 else {
691 struct node *node = get_node(f, in->nodeid);
692 node->ino = arg.attr.ino;
693 }
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000694 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000695
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000696 send_reply(f, in, res, &arg, sizeof(arg));
697}
698
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000699static int do_chmod(struct fuse *f, const char *path, struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000700{
701 int res;
702
703 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000704 if (f->op.chmod)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000705 res = f->op.chmod(path, attr->mode);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000706
707 return res;
708}
709
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000710static int do_chown(struct fuse *f, const char *path, struct fuse_attr *attr,
Miklos Szeredi2e50d432001-12-20 12:17:25 +0000711 int valid)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000712{
713 int res;
714 uid_t uid = (valid & FATTR_UID) ? attr->uid : (uid_t) -1;
715 gid_t gid = (valid & FATTR_GID) ? attr->gid : (gid_t) -1;
716
717 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000718 if (f->op.chown)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000719 res = f->op.chown(path, uid, gid);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000720
721 return res;
722}
723
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000724static int do_truncate(struct fuse *f, const char *path,
725 struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000726{
727 int res;
728
729 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000730 if (f->op.truncate)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000731 res = f->op.truncate(path, attr->size);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000732
733 return res;
734}
735
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000736static int do_utime(struct fuse *f, const char *path, struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000737{
738 int res;
739 struct utimbuf buf;
740 buf.actime = attr->atime;
741 buf.modtime = attr->mtime;
742 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000743 if (f->op.utime)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000744 res = f->op.utime(path, &buf);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000745
746 return res;
747}
748
Miklos Szeredi5e183482001-10-31 14:52:35 +0000749static void do_setattr(struct fuse *f, struct fuse_in_header *in,
750 struct fuse_setattr_in *arg)
751{
752 int res;
753 char *path;
754 int valid = arg->valid;
755 struct fuse_attr *attr = &arg->attr;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000756 struct fuse_attr_out outarg;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000757
758 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000759 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000760 if (path != NULL) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000761 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000762 if (f->op.getattr) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000763 res = 0;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000764 if (!res && (valid & FATTR_MODE))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000765 res = do_chmod(f, path, attr);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000766 if (!res && (valid & (FATTR_UID | FATTR_GID)))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000767 res = do_chown(f, path, attr, valid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000768 if (!res && (valid & FATTR_SIZE))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000769 res = do_truncate(f, path, attr);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000770 if (!res && (valid & (FATTR_ATIME | FATTR_MTIME)) ==
Miklos Szeredib5958612004-02-20 14:10:49 +0000771 (FATTR_ATIME | FATTR_MTIME))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000772 res = do_utime(f, path, attr);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000773 if (!res) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000774 struct stat buf;
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000775 res = f->op.getattr(path, &buf);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000776 if (!res) {
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000777 memset(&outarg, 0, sizeof(struct fuse_attr_out));
778 outarg.attr_valid = ATTR_REVALIDATE_TIME;
779 outarg.attr_valid_nsec = 0;
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000780 convert_stat(&buf, &outarg.attr);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000781 if (!(f->flags & FUSE_USE_INO))
782 outarg.attr.ino = in->nodeid;
783 else {
784 struct node *node = get_node(f, in->nodeid);
785 node->ino = outarg.attr.ino;
786 }
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000787 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000788 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000789 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000790 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000791 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000792 send_reply(f, in, res, &outarg, sizeof(outarg));
Miklos Szeredi5e183482001-10-31 14:52:35 +0000793}
794
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000795static void do_readlink(struct fuse *f, struct fuse_in_header *in)
796{
797 int res;
798 char link[PATH_MAX + 1];
Miklos Szeredib483c932001-10-29 14:57:57 +0000799 char *path;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000800
Miklos Szeredi5e183482001-10-31 14:52:35 +0000801 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000802 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000803 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +0000804 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000805 if (f->op.readlink)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000806 res = f->op.readlink(path, link, sizeof(link));
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000807 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000808 }
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000809 link[PATH_MAX] = '\0';
Miklos Szeredi4b2bef42002-01-09 12:23:27 +0000810 send_reply(f, in, res, link, res == 0 ? strlen(link) : 0);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000811}
812
813static void do_getdir(struct fuse *f, struct fuse_in_header *in)
814{
815 int res;
816 struct fuse_getdir_out arg;
Miklos Szeredia181e612001-11-06 12:03:23 +0000817 struct fuse_dirhandle dh;
Miklos Szeredib483c932001-10-29 14:57:57 +0000818 char *path;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000819
Miklos Szeredib483c932001-10-29 14:57:57 +0000820 dh.fuse = f;
821 dh.fp = tmpfile();
Miklos Szeredia13d9002004-11-02 17:32:03 +0000822 dh.dir = in->nodeid;
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000823
Miklos Szeredi65afea12004-09-14 07:13:45 +0000824 res = -EIO;
825 if (dh.fp == NULL)
826 perror("fuse: failed to create temporary file");
827 else {
828 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000829 path = get_path(f, in->nodeid);
Miklos Szeredi65afea12004-09-14 07:13:45 +0000830 if (path != NULL) {
831 res = -ENOSYS;
832 if (f->op.getdir)
Miklos Szeredi8fb48fe2004-11-08 14:48:52 +0000833 res = f->op.getdir(path, &dh, fill_dir);
Miklos Szeredi65afea12004-09-14 07:13:45 +0000834 free(path);
835 }
836 fflush(dh.fp);
837 }
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000838 memset(&arg, 0, sizeof(struct fuse_getdir_out));
Miklos Szeredi65afea12004-09-14 07:13:45 +0000839 if (res == 0)
840 arg.fd = fileno(dh.fp);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000841 send_reply(f, in, res, &arg, sizeof(arg));
Miklos Szeredi65afea12004-09-14 07:13:45 +0000842 if (dh.fp != NULL)
843 fclose(dh.fp);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000844}
845
Miklos Szeredib483c932001-10-29 14:57:57 +0000846static void do_mknod(struct fuse *f, struct fuse_in_header *in,
847 struct fuse_mknod_in *inarg)
848{
849 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000850 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000851 char *path;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000852 char *name = PARAM(inarg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000853 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000854
Miklos Szeredi5e183482001-10-31 14:52:35 +0000855 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000856 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000857 if (path != NULL) {
858 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000859 printf("MKNOD %s\n", path);
860 fflush(stdout);
861 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000862 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000863 if (f->op.mknod && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000864 res = f->op.mknod(path, inarg->mode, inarg->rdev);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000865 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000866 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000867 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000868 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000869 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000870 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
871 if (res == 0 && res2 == -ENOENT)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000872 destroy_node(f, outarg.nodeid, in->unique);
Miklos Szeredib483c932001-10-29 14:57:57 +0000873}
874
875static void do_mkdir(struct fuse *f, struct fuse_in_header *in,
876 struct fuse_mkdir_in *inarg)
877{
878 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000879 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000880 char *path;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000881 char *name = PARAM(inarg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000882 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000883
Miklos Szeredi5e183482001-10-31 14:52:35 +0000884 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000885 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000886 if (path != NULL) {
887 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000888 printf("MKDIR %s\n", path);
889 fflush(stdout);
890 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000891 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000892 if (f->op.mkdir && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000893 res = f->op.mkdir(path, inarg->mode);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000894 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000895 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000896 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000897 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000898 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000899 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
900 if (res == 0 && res2 == -ENOENT)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000901 destroy_node(f, outarg.nodeid, in->unique);
Miklos Szeredib483c932001-10-29 14:57:57 +0000902}
903
Miklos Szeredib5958612004-02-20 14:10:49 +0000904static void do_unlink(struct fuse *f, struct fuse_in_header *in, char *name)
Miklos Szeredib483c932001-10-29 14:57:57 +0000905{
906 int res;
907 char *path;
908
Miklos Szeredi5e183482001-10-31 14:52:35 +0000909 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000910 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000911 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000912 if (f->flags & FUSE_DEBUG) {
913 printf("UNLINK %s\n", path);
914 fflush(stdout);
915 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000916 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000917 if (f->op.unlink) {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000918 if (!(f->flags & FUSE_HARD_REMOVE) && is_open(f, in->nodeid, name))
919 res = hide_node(f, path, in->nodeid, name);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000920 else {
921 res = f->op.unlink(path);
922 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000923 remove_node(f, in->nodeid, name);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000924 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000925 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000926 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000927 }
Miklos Szeredib5958612004-02-20 14:10:49 +0000928 send_reply(f, in, res, NULL, 0);
929}
930
931static void do_rmdir(struct fuse *f, struct fuse_in_header *in, char *name)
932{
933 int res;
934 char *path;
935
936 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000937 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000938 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000939 if (f->flags & FUSE_DEBUG) {
940 printf("RMDIR %s\n", path);
941 fflush(stdout);
942 }
Miklos Szeredib5958612004-02-20 14:10:49 +0000943 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000944 if (f->op.rmdir) {
Miklos Szeredib5958612004-02-20 14:10:49 +0000945 res = f->op.rmdir(path);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000946 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000947 remove_node(f, in->nodeid, name);
Miklos Szeredib5958612004-02-20 14:10:49 +0000948 }
949 free(path);
950 }
Miklos Szeredib483c932001-10-29 14:57:57 +0000951 send_reply(f, in, res, NULL, 0);
952}
953
954static void do_symlink(struct fuse *f, struct fuse_in_header *in, char *name,
955 char *link)
956{
957 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000958 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000959 char *path;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000960 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000961
Miklos Szeredi5e183482001-10-31 14:52:35 +0000962 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000963 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000964 if (path != NULL) {
965 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000966 printf("SYMLINK %s\n", path);
967 fflush(stdout);
968 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000969 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000970 if (f->op.symlink && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000971 res = f->op.symlink(link, path);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000972 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000973 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000974 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000975 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000976 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000977 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
978 if (res == 0 && res2 == -ENOENT)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000979 destroy_node(f, outarg.nodeid, in->unique);
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000980
Miklos Szeredib483c932001-10-29 14:57:57 +0000981}
982
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000983static void do_rename(struct fuse *f, struct fuse_in_header *in,
984 struct fuse_rename_in *inarg)
985{
986 int res;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000987 nodeid_t olddir = in->nodeid;
988 nodeid_t newdir = inarg->newdir;
Miklos Szeredi6bf8b682002-10-28 08:49:39 +0000989 char *oldname = PARAM(inarg);
990 char *newname = oldname + strlen(oldname) + 1;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000991 char *oldpath;
992 char *newpath;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000993
Miklos Szeredi5e183482001-10-31 14:52:35 +0000994 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000995 oldpath = get_path_name(f, olddir, oldname);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000996 if (oldpath != NULL) {
Miklos Szeredia181e612001-11-06 12:03:23 +0000997 newpath = get_path_name(f, newdir, newname);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000998 if (newpath != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000999 if (f->flags & FUSE_DEBUG) {
1000 printf("RENAME %s -> %s\n", oldpath, newpath);
1001 fflush(stdout);
1002 }
Miklos Szeredi5e183482001-10-31 14:52:35 +00001003 res = -ENOSYS;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001004 if (f->op.rename) {
1005 res = 0;
Miklos Szeredi2529ca22004-07-13 15:36:52 +00001006 if (!(f->flags & FUSE_HARD_REMOVE) &&
1007 is_open(f, newdir, newname))
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001008 res = hide_node(f, newpath, newdir, newname);
1009 if (res == 0) {
1010 res = f->op.rename(oldpath, newpath);
1011 if (res == 0)
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001012 res = rename_node(f, olddir, oldname, newdir, newname, 0);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001013 }
1014 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001015 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001016 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001017 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001018 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001019 send_reply(f, in, res, NULL, 0);
1020}
1021
1022static void do_link(struct fuse *f, struct fuse_in_header *in,
Miklos Szeredi5e183482001-10-31 14:52:35 +00001023 struct fuse_link_in *arg)
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001024{
1025 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +00001026 int res2;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001027 char *oldpath;
1028 char *newpath;
Miklos Szeredi76f65782004-02-19 16:55:40 +00001029 char *name = PARAM(arg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +00001030 struct fuse_entry_out outarg;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001031
Miklos Szeredi5e183482001-10-31 14:52:35 +00001032 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001033 oldpath = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001034 if (oldpath != NULL) {
Miklos Szeredi76f65782004-02-19 16:55:40 +00001035 newpath = get_path_name(f, arg->newdir, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001036 if (newpath != NULL) {
1037 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +00001038 printf("LINK %s\n", newpath);
1039 fflush(stdout);
1040 }
Miklos Szeredi5e183482001-10-31 14:52:35 +00001041 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001042 if (f->op.link && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +00001043 res = f->op.link(oldpath, newpath);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001044 if (res == 0)
Miklos Szeredi76f65782004-02-19 16:55:40 +00001045 res = lookup_path(f, arg->newdir, in->unique, name,
1046 newpath, &outarg);
1047 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001048 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001049 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001050 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001051 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +00001052 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
1053 if (res == 0 && res2 == -ENOENT)
Miklos Szeredia13d9002004-11-02 17:32:03 +00001054 destroy_node(f, outarg.nodeid, in->unique);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001055}
1056
Miklos Szeredi5e183482001-10-31 14:52:35 +00001057static void do_open(struct fuse *f, struct fuse_in_header *in,
1058 struct fuse_open_in *arg)
1059{
1060 int res;
1061 char *path;
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001062 struct fuse_open_out outarg;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001063 struct fuse_file_info fi;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001064
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001065 memset(&fi, 0, sizeof(fi));
1066 fi.flags = arg->flags;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001067 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001068 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001069 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +00001070 res = -ENOSYS;
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001071 if (f->op.open.curr) {
1072 if (!f->compat)
1073 res = f->op.open.curr(path, &fi);
1074 else
1075 res = f->op.open.compat2(path, fi.flags);
1076 }
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +00001077 }
Miklos Szeredi73798f92004-07-12 15:55:11 +00001078 if (res == 0) {
1079 int res2;
1080
1081 /* If the request is interrupted the lock must be held until
1082 the cancellation is finished. Otherwise there could be
1083 races with rename/unlink, against which the kernel can't
1084 protect */
1085 pthread_mutex_lock(&f->lock);
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001086 outarg.fh = fi.fh;
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001087 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi1eea0322004-09-27 18:50:11 +00001088 printf("OPEN[%lu] flags: 0x%x\n", outarg.fh, arg->flags);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001089 fflush(stdout);
1090 }
1091
1092 res2 = __send_reply(f, in, res, &outarg, sizeof(outarg), 1);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001093 if(res2 == -ENOENT) {
1094 /* The open syscall was interrupted, so it must be cancelled */
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001095 if(f->op.release.curr) {
1096 if (!f->compat)
1097 f->op.release.curr(path, &fi);
1098 else
1099 f->op.release.compat2(path, fi.flags);
1100 }
Miklos Szeredi73798f92004-07-12 15:55:11 +00001101 } else
Miklos Szeredia13d9002004-11-02 17:32:03 +00001102 get_node(f, in->nodeid)->open_count ++;
Miklos Szeredi73798f92004-07-12 15:55:11 +00001103 pthread_mutex_unlock(&f->lock);
1104
1105 } else
1106 send_reply(f, in, res, NULL, 0);
1107
Miklos Szeredi9a31cca2004-06-26 21:11:25 +00001108 if (path)
1109 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001110}
1111
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001112static void do_flush(struct fuse *f, struct fuse_in_header *in,
1113 struct fuse_flush_in *arg)
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001114{
1115 char *path;
1116 int res;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001117 struct fuse_file_info fi;
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001118
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001119 memset(&fi, 0, sizeof(fi));
1120 fi.fh = arg->fh;
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001121 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001122 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001123 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001124 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi1eea0322004-09-27 18:50:11 +00001125 printf("FLUSH[%lu]\n", arg->fh);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001126 fflush(stdout);
1127 }
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001128 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001129 if (f->op.flush)
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001130 res = f->op.flush(path, &fi);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001131 free(path);
1132 }
1133 send_reply(f, in, res, NULL, 0);
1134}
1135
Miklos Szeredi9478e862002-12-11 09:50:26 +00001136static void do_release(struct fuse *f, struct fuse_in_header *in,
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001137 struct fuse_release_in *arg)
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001138{
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001139 struct node *node;
1140 char *path;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001141 struct fuse_file_info fi;
1142
1143 memset(&fi, 0, sizeof(fi));
1144 fi.flags = arg->flags;
1145 fi.fh = arg->fh;
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001146
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001147 pthread_mutex_lock(&f->lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001148 node = get_node(f, in->nodeid);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001149 --node->open_count;
1150 pthread_mutex_unlock(&f->lock);
1151
Miklos Szeredia13d9002004-11-02 17:32:03 +00001152 path = get_path(f, in->nodeid);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001153 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001154 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi1eea0322004-09-27 18:50:11 +00001155 printf("RELEASE[%lu]\n", arg->fh);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001156 fflush(stdout);
1157 }
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001158 if (f->op.release.curr) {
1159 if (!f->compat)
1160 f->op.release.curr(path, &fi);
1161 else
1162 f->op.release.compat2(path, fi.flags);
1163 }
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001164
1165 if(node->is_hidden && node->open_count == 0)
1166 /* can now clean up this hidden file */
1167 f->op.unlink(path);
1168
1169 free(path);
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001170 }
Miklos Szeredi556d03d2004-06-30 11:13:41 +00001171 send_reply(f, in, 0, NULL, 0);
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001172}
1173
Miklos Szeredi5e183482001-10-31 14:52:35 +00001174static void do_read(struct fuse *f, struct fuse_in_header *in,
1175 struct fuse_read_in *arg)
1176{
1177 int res;
1178 char *path;
Miklos Szeredi43696432001-11-18 19:15:05 +00001179 char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + arg->size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001180 if (outbuf == NULL)
1181 send_reply(f, in, -ENOMEM, NULL, 0);
1182 else {
1183 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
1184 char *buf = outbuf + sizeof(struct fuse_out_header);
1185 size_t size;
1186 size_t outsize;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001187 struct fuse_file_info fi;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001188
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001189 memset(&fi, 0, sizeof(fi));
1190 fi.fh = arg->fh;
1191
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001192 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001193 path = get_path(f, in->nodeid);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001194 if (path != NULL) {
1195 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi1eea0322004-09-27 18:50:11 +00001196 printf("READ[%lu] %u bytes from %llu\n", arg->fh, arg->size,
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001197 arg->offset);
1198 fflush(stdout);
1199 }
1200
1201 res = -ENOSYS;
1202 if (f->op.read)
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001203 res = f->op.read(path, buf, arg->size, arg->offset, &fi);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001204 free(path);
Miklos Szeredi6ebe2342002-01-06 21:44:16 +00001205 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001206
1207 size = 0;
1208 if (res >= 0) {
1209 size = res;
1210 res = 0;
1211 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi1eea0322004-09-27 18:50:11 +00001212 printf(" READ[%lu] %u bytes\n", arg->fh, size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001213 fflush(stdout);
1214 }
Miklos Szeredi6ebe2342002-01-06 21:44:16 +00001215 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001216 memset(out, 0, sizeof(struct fuse_out_header));
1217 out->unique = in->unique;
1218 out->error = res;
1219 outsize = sizeof(struct fuse_out_header) + size;
1220
1221 send_reply_raw(f, outbuf, outsize, 0);
1222 free(outbuf);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001223 }
Miklos Szeredi5e183482001-10-31 14:52:35 +00001224}
Miklos Szeredib483c932001-10-29 14:57:57 +00001225
Miklos Szeredia181e612001-11-06 12:03:23 +00001226static void do_write(struct fuse *f, struct fuse_in_header *in,
1227 struct fuse_write_in *arg)
1228{
1229 int res;
1230 char *path;
Miklos Szerediad051c32004-07-02 09:22:50 +00001231 struct fuse_write_out outarg;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001232 struct fuse_file_info fi;
1233
1234 memset(&fi, 0, sizeof(fi));
1235 fi.fh = arg->fh;
Miklos Szeredid59bb9d2004-12-07 10:04:24 +00001236 fi.writepage = arg->writepage;
Miklos Szeredia181e612001-11-06 12:03:23 +00001237
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001238 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001239 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001240 if (path != NULL) {
1241 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi1eea0322004-09-27 18:50:11 +00001242 printf("WRITE%s[%lu] %u bytes to %llu\n",
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001243 arg->writepage ? "PAGE" : "", arg->fh, arg->size,
1244 arg->offset);
Miklos Szeredi6ebe2342002-01-06 21:44:16 +00001245 fflush(stdout);
1246 }
1247
Miklos Szeredia181e612001-11-06 12:03:23 +00001248 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001249 if (f->op.write)
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001250 res = f->op.write(path, PARAM(arg), arg->size, arg->offset, &fi);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001251 free(path);
Miklos Szeredia181e612001-11-06 12:03:23 +00001252 }
1253
Miklos Szerediad051c32004-07-02 09:22:50 +00001254 if (res >= 0) {
1255 outarg.size = res;
1256 res = 0;
Miklos Szeredia181e612001-11-06 12:03:23 +00001257 }
1258
Miklos Szerediad051c32004-07-02 09:22:50 +00001259 send_reply(f, in, res, &outarg, sizeof(outarg));
Miklos Szeredia181e612001-11-06 12:03:23 +00001260}
1261
Miklos Szeredi77f39942004-03-25 11:17:52 +00001262static int default_statfs(struct statfs *buf)
1263{
1264 buf->f_namelen = 255;
1265 buf->f_bsize = 512;
1266 return 0;
1267}
1268
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001269static void convert_statfs_compat(struct _fuse_statfs_compat1 *compatbuf,
1270 struct statfs *statfs)
1271{
1272 statfs->f_bsize = compatbuf->block_size;
1273 statfs->f_blocks = compatbuf->blocks;
1274 statfs->f_bfree = compatbuf->blocks_free;
1275 statfs->f_bavail = compatbuf->blocks_free;
1276 statfs->f_files = compatbuf->files;
1277 statfs->f_ffree = compatbuf->files_free;
1278 statfs->f_namelen = compatbuf->namelen;
1279}
1280
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001281static void convert_statfs(struct statfs *statfs, struct fuse_kstatfs *kstatfs)
1282{
1283 kstatfs->bsize = statfs->f_bsize;
1284 kstatfs->blocks = statfs->f_blocks;
1285 kstatfs->bfree = statfs->f_bfree;
1286 kstatfs->bavail = statfs->f_bavail;
1287 kstatfs->files = statfs->f_files;
1288 kstatfs->ffree = statfs->f_ffree;
1289 kstatfs->namelen = statfs->f_namelen;
1290}
1291
Mark Glinesd84b39a2002-01-07 16:32:02 +00001292static void do_statfs(struct fuse *f, struct fuse_in_header *in)
1293{
1294 int res;
Mark Glinesd84b39a2002-01-07 16:32:02 +00001295 struct fuse_statfs_out arg;
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001296 struct statfs buf;
Mark Glinesd84b39a2002-01-07 16:32:02 +00001297
Miklos Szeredi77f39942004-03-25 11:17:52 +00001298 memset(&buf, 0, sizeof(struct statfs));
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001299 if (f->op.statfs.curr) {
1300 if (!f->compat || f->compat > 11)
1301 res = f->op.statfs.curr("/", &buf);
1302 else {
1303 struct _fuse_statfs_compat1 compatbuf;
1304 memset(&compatbuf, 0, sizeof(struct _fuse_statfs_compat1));
1305 res = f->op.statfs.compat1(&compatbuf);
1306 if (res == 0)
1307 convert_statfs_compat(&compatbuf, &buf);
1308 }
1309 }
Miklos Szeredi77f39942004-03-25 11:17:52 +00001310 else
1311 res = default_statfs(&buf);
1312
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001313 if (res == 0)
Miklos Szeredi77f39942004-03-25 11:17:52 +00001314 convert_statfs(&buf, &arg.st);
Miklos Szeredi4b2bef42002-01-09 12:23:27 +00001315
Mark Glinesd84b39a2002-01-07 16:32:02 +00001316 send_reply(f, in, res, &arg, sizeof(arg));
1317}
1318
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001319static void do_fsync(struct fuse *f, struct fuse_in_header *in,
1320 struct fuse_fsync_in *inarg)
1321{
1322 int res;
1323 char *path;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001324 struct fuse_file_info fi;
1325
1326 memset(&fi, 0, sizeof(fi));
1327 fi.fh = inarg->fh;
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001328
1329 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001330 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001331 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001332 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi1eea0322004-09-27 18:50:11 +00001333 printf("FSYNC[%lu]\n", inarg->fh);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001334 fflush(stdout);
1335 }
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001336 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001337 if (f->op.fsync)
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001338 res = f->op.fsync(path, inarg->datasync, &fi);
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001339 free(path);
1340 }
1341 send_reply(f, in, res, NULL, 0);
1342}
1343
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001344static void do_setxattr(struct fuse *f, struct fuse_in_header *in,
1345 struct fuse_setxattr_in *arg)
1346{
1347 int res;
1348 char *path;
1349 char *name = PARAM(arg);
1350 unsigned char *value = name + strlen(name) + 1;
1351
1352 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001353 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001354 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001355 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001356 if (f->op.setxattr)
1357 res = f->op.setxattr(path, name, value, arg->size, arg->flags);
1358 free(path);
1359 }
1360 send_reply(f, in, res, NULL, 0);
1361}
1362
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001363static int common_getxattr(struct fuse *f, struct fuse_in_header *in,
1364 const char *name, char *value, size_t size)
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001365{
1366 int res;
1367 char *path;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001368
1369 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001370 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001371 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001372 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001373 if (f->op.getxattr)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001374 res = f->op.getxattr(path, name, value, size);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001375 free(path);
1376 }
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001377 return res;
1378}
1379
1380static void do_getxattr_read(struct fuse *f, struct fuse_in_header *in,
1381 const char *name, size_t size)
1382{
1383 int res;
1384 char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001385 if (outbuf == NULL)
1386 send_reply(f, in, -ENOMEM, NULL, 0);
1387 else {
1388 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
1389 char *value = outbuf + sizeof(struct fuse_out_header);
1390
1391 res = common_getxattr(f, in, name, value, size);
1392 size = 0;
1393 if (res > 0) {
1394 size = res;
1395 res = 0;
1396 }
1397 memset(out, 0, sizeof(struct fuse_out_header));
1398 out->unique = in->unique;
1399 out->error = res;
1400
1401 send_reply_raw(f, outbuf, sizeof(struct fuse_out_header) + size, 0);
1402 free(outbuf);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001403 }
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001404}
1405
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001406static void do_getxattr_size(struct fuse *f, struct fuse_in_header *in,
1407 const char *name)
1408{
1409 int res;
1410 struct fuse_getxattr_out arg;
1411
1412 res = common_getxattr(f, in, name, NULL, 0);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001413 if (res >= 0) {
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001414 arg.size = res;
1415 res = 0;
1416 }
1417 send_reply(f, in, res, &arg, sizeof(arg));
1418}
1419
1420static void do_getxattr(struct fuse *f, struct fuse_in_header *in,
1421 struct fuse_getxattr_in *arg)
1422{
1423 char *name = PARAM(arg);
1424
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001425 if (arg->size)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001426 do_getxattr_read(f, in, name, arg->size);
1427 else
1428 do_getxattr_size(f, in, name);
1429}
1430
1431static int common_listxattr(struct fuse *f, struct fuse_in_header *in,
1432 char *list, size_t size)
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001433{
1434 int res;
1435 char *path;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001436
1437 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001438 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001439 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001440 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001441 if (f->op.listxattr)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001442 res = f->op.listxattr(path, list, size);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001443 free(path);
1444 }
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001445 return res;
1446}
1447
1448static void do_listxattr_read(struct fuse *f, struct fuse_in_header *in,
1449 size_t size)
1450{
1451 int res;
1452 char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001453 if (outbuf == NULL)
1454 send_reply(f, in, -ENOMEM, NULL, 0);
1455 else {
1456 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
1457 char *list = outbuf + sizeof(struct fuse_out_header);
1458
1459 res = common_listxattr(f, in, list, size);
1460 size = 0;
1461 if (res > 0) {
1462 size = res;
1463 res = 0;
1464 }
1465 memset(out, 0, sizeof(struct fuse_out_header));
1466 out->unique = in->unique;
1467 out->error = res;
1468
1469 send_reply_raw(f, outbuf, sizeof(struct fuse_out_header) + size, 0);
1470 free(outbuf);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001471 }
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001472}
1473
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001474static void do_listxattr_size(struct fuse *f, struct fuse_in_header *in)
1475{
1476 int res;
1477 struct fuse_getxattr_out arg;
1478
1479 res = common_listxattr(f, in, NULL, 0);
1480 if (res >= 0) {
1481 arg.size = res;
1482 res = 0;
1483 }
1484 send_reply(f, in, res, &arg, sizeof(arg));
1485}
1486
1487static void do_listxattr(struct fuse *f, struct fuse_in_header *in,
1488 struct fuse_getxattr_in *arg)
1489{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001490 if (arg->size)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001491 do_listxattr_read(f, in, arg->size);
1492 else
1493 do_listxattr_size(f, in);
1494}
1495
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001496static void do_removexattr(struct fuse *f, struct fuse_in_header *in,
1497 char *name)
1498{
1499 int res;
1500 char *path;
1501
1502 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001503 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001504 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001505 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001506 if (f->op.removexattr)
1507 res = f->op.removexattr(path, name);
1508 free(path);
1509 }
1510 send_reply(f, in, res, NULL, 0);
1511}
1512
1513
Miklos Szeredi43696432001-11-18 19:15:05 +00001514static void free_cmd(struct fuse_cmd *cmd)
1515{
1516 free(cmd->buf);
1517 free(cmd);
1518}
1519
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001520void __fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd)
Miklos Szeredia181e612001-11-06 12:03:23 +00001521{
Miklos Szeredia181e612001-11-06 12:03:23 +00001522 struct fuse_in_header *in = (struct fuse_in_header *) cmd->buf;
1523 void *inarg = cmd->buf + sizeof(struct fuse_in_header);
1524 size_t argsize;
Miklos Szeredid169f312004-09-22 08:48:26 +00001525 struct fuse_context *ctx = fuse_get_context();
Miklos Szeredia181e612001-11-06 12:03:23 +00001526
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001527 dec_avail(f);
Miklos Szeredi33232032001-11-19 17:55:51 +00001528
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001529 if ((f->flags & FUSE_DEBUG)) {
Miklos Szeredia13d9002004-11-02 17:32:03 +00001530 printf("unique: %i, opcode: %s (%i), nodeid: %li, insize: %i\n",
1531 in->unique, opname(in->opcode), in->opcode, in->nodeid,
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001532 cmd->buflen);
Miklos Szeredic0938ea2001-11-07 12:35:06 +00001533 fflush(stdout);
1534 }
Miklos Szeredife25def2001-12-20 15:38:05 +00001535
Miklos Szeredid169f312004-09-22 08:48:26 +00001536 ctx->fuse = f;
Miklos Szeredife25def2001-12-20 15:38:05 +00001537 ctx->uid = in->uid;
1538 ctx->gid = in->gid;
Miklos Szeredi1f18db52004-09-27 06:54:49 +00001539 ctx->pid = in->pid;
Miklos Szeredia181e612001-11-06 12:03:23 +00001540
1541 argsize = cmd->buflen - sizeof(struct fuse_in_header);
1542
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001543 switch (in->opcode) {
Miklos Szeredia181e612001-11-06 12:03:23 +00001544 case FUSE_LOOKUP:
1545 do_lookup(f, in, (char *) inarg);
1546 break;
1547
Miklos Szeredia181e612001-11-06 12:03:23 +00001548 case FUSE_GETATTR:
1549 do_getattr(f, in);
1550 break;
1551
1552 case FUSE_SETATTR:
1553 do_setattr(f, in, (struct fuse_setattr_in *) inarg);
1554 break;
1555
1556 case FUSE_READLINK:
1557 do_readlink(f, in);
1558 break;
1559
1560 case FUSE_GETDIR:
1561 do_getdir(f, in);
1562 break;
1563
1564 case FUSE_MKNOD:
1565 do_mknod(f, in, (struct fuse_mknod_in *) inarg);
1566 break;
1567
1568 case FUSE_MKDIR:
1569 do_mkdir(f, in, (struct fuse_mkdir_in *) inarg);
1570 break;
1571
1572 case FUSE_UNLINK:
Miklos Szeredib5958612004-02-20 14:10:49 +00001573 do_unlink(f, in, (char *) inarg);
1574 break;
1575
Miklos Szeredia181e612001-11-06 12:03:23 +00001576 case FUSE_RMDIR:
Miklos Szeredib5958612004-02-20 14:10:49 +00001577 do_rmdir(f, in, (char *) inarg);
Miklos Szeredia181e612001-11-06 12:03:23 +00001578 break;
1579
1580 case FUSE_SYMLINK:
1581 do_symlink(f, in, (char *) inarg,
1582 ((char *) inarg) + strlen((char *) inarg) + 1);
1583 break;
1584
1585 case FUSE_RENAME:
1586 do_rename(f, in, (struct fuse_rename_in *) inarg);
1587 break;
1588
1589 case FUSE_LINK:
1590 do_link(f, in, (struct fuse_link_in *) inarg);
1591 break;
1592
1593 case FUSE_OPEN:
1594 do_open(f, in, (struct fuse_open_in *) inarg);
1595 break;
1596
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001597 case FUSE_FLUSH:
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001598 do_flush(f, in, (struct fuse_flush_in *) inarg);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001599 break;
1600
Miklos Szeredi9478e862002-12-11 09:50:26 +00001601 case FUSE_RELEASE:
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001602 do_release(f, in, (struct fuse_release_in *) inarg);
Miklos Szeredi9478e862002-12-11 09:50:26 +00001603 break;
1604
Miklos Szeredia181e612001-11-06 12:03:23 +00001605 case FUSE_READ:
1606 do_read(f, in, (struct fuse_read_in *) inarg);
1607 break;
1608
1609 case FUSE_WRITE:
1610 do_write(f, in, (struct fuse_write_in *) inarg);
1611 break;
1612
Mark Glinesd84b39a2002-01-07 16:32:02 +00001613 case FUSE_STATFS:
1614 do_statfs(f, in);
1615 break;
1616
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001617 case FUSE_FSYNC:
1618 do_fsync(f, in, (struct fuse_fsync_in *) inarg);
1619 break;
1620
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001621 case FUSE_SETXATTR:
1622 do_setxattr(f, in, (struct fuse_setxattr_in *) inarg);
1623 break;
1624
1625 case FUSE_GETXATTR:
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001626 do_getxattr(f, in, (struct fuse_getxattr_in *) inarg);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001627 break;
1628
1629 case FUSE_LISTXATTR:
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001630 do_listxattr(f, in, (struct fuse_getxattr_in *) inarg);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001631 break;
1632
1633 case FUSE_REMOVEXATTR:
1634 do_removexattr(f, in, (char *) inarg);
1635 break;
1636
Miklos Szeredia181e612001-11-06 12:03:23 +00001637 default:
Miklos Szeredi43696432001-11-18 19:15:05 +00001638 send_reply(f, in, -ENOSYS, NULL, 0);
Miklos Szeredia181e612001-11-06 12:03:23 +00001639 }
Miklos Szeredi43696432001-11-18 19:15:05 +00001640
1641 free_cmd(cmd);
Miklos Szeredia181e612001-11-06 12:03:23 +00001642}
1643
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001644int __fuse_exited(struct fuse* f)
1645{
1646 return f->exited;
1647}
1648
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001649struct fuse_cmd *__fuse_read_cmd(struct fuse *f)
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001650{
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001651 ssize_t res;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001652 struct fuse_cmd *cmd;
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001653 struct fuse_in_header *in;
1654 void *inarg;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001655
Miklos Szeredi43696432001-11-18 19:15:05 +00001656 cmd = (struct fuse_cmd *) malloc(sizeof(struct fuse_cmd));
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001657 if (cmd == NULL) {
1658 fprintf(stderr, "fuse: failed to allocate cmd in read\n");
1659 return NULL;
1660 }
Miklos Szeredi43696432001-11-18 19:15:05 +00001661 cmd->buf = (char *) malloc(FUSE_MAX_IN);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001662 if (cmd->buf == NULL) {
1663 fprintf(stderr, "fuse: failed to allocate read buffer\n");
1664 free(cmd);
1665 return NULL;
1666 }
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001667 in = (struct fuse_in_header *) cmd->buf;
1668 inarg = cmd->buf + sizeof(struct fuse_in_header);
Miklos Szeredi43696432001-11-18 19:15:05 +00001669
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001670 res = read(f->fd, cmd->buf, FUSE_MAX_IN);
1671 if (res == -1) {
1672 free_cmd(cmd);
1673 if (__fuse_exited(f) || errno == EINTR)
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001674 return NULL;
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001675
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001676 /* ENODEV means we got unmounted, so we silenty return failure */
1677 if (errno != ENODEV) {
1678 /* BAD... This will happen again */
1679 perror("fuse: reading device");
1680 }
1681
1682 fuse_exit(f);
1683 return NULL;
1684 }
1685 if ((size_t) res < sizeof(struct fuse_in_header)) {
1686 free_cmd(cmd);
1687 /* Cannot happen */
1688 fprintf(stderr, "short read on fuse device\n");
1689 fuse_exit(f);
1690 return NULL;
1691 }
1692 cmd->buflen = res;
1693
1694 /* Forget is special, it can be done without messing with threads. */
1695 if (in->opcode == FUSE_FORGET) {
1696 do_forget(f, in, (struct fuse_forget_in *) inarg);
1697 free_cmd(cmd);
1698 return NULL;
1699 }
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001700
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001701 return cmd;
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001702}
1703
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001704int fuse_loop(struct fuse *f)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001705{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001706 if (f == NULL)
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001707 return -1;
Miklos Szeredic40748a2004-02-20 16:38:45 +00001708
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001709 while (1) {
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001710 struct fuse_cmd *cmd;
1711
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001712 if (__fuse_exited(f))
Miklos Szeredi874e3c12004-11-01 23:15:20 +00001713 break;
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001714
1715 cmd = __fuse_read_cmd(f);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001716 if (cmd == NULL)
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001717 continue;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001718
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001719 __fuse_process_cmd(f, cmd);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001720 }
Miklos Szeredi874e3c12004-11-01 23:15:20 +00001721 f->exited = 0;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001722 return 0;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001723}
1724
Miklos Szeredi891b8742004-07-29 09:27:49 +00001725int fuse_invalidate(struct fuse *f, const char *path)
1726{
1727 int res;
1728 int err;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001729 nodeid_t nodeid;
1730 unsigned long ino;
Miklos Szeredi891b8742004-07-29 09:27:49 +00001731 struct fuse_user_header h;
1732
Miklos Szeredia13d9002004-11-02 17:32:03 +00001733 err = path_lookup(f, path, &nodeid, &ino);
Miklos Szeredi891b8742004-07-29 09:27:49 +00001734 if (err) {
1735 if (err == -ENOENT)
1736 return 0;
1737 else
1738 return err;
1739 }
1740
1741 memset(&h, 0, sizeof(struct fuse_user_header));
1742 h.opcode = FUSE_INVALIDATE;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001743 h.nodeid = nodeid;
Miklos Szeredi891b8742004-07-29 09:27:49 +00001744 h.ino = ino;
1745
1746 if ((f->flags & FUSE_DEBUG)) {
Miklos Szeredia13d9002004-11-02 17:32:03 +00001747 printf("INVALIDATE nodeid: %li\n", nodeid);
Miklos Szeredi891b8742004-07-29 09:27:49 +00001748 fflush(stdout);
1749 }
1750
1751 res = write(f->fd, &h, sizeof(struct fuse_user_header));
1752 if (res == -1) {
1753 if (errno != ENOENT) {
1754 perror("fuse: writing device");
1755 return -errno;
1756 }
1757 }
1758 return 0;
1759}
1760
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001761void fuse_exit(struct fuse *f)
1762{
1763 f->exited = 1;
1764}
1765
Miklos Szeredid169f312004-09-22 08:48:26 +00001766struct fuse_context *fuse_get_context()
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001767{
Miklos Szeredid169f312004-09-22 08:48:26 +00001768 static struct fuse_context context;
1769 if (fuse_getcontext)
1770 return fuse_getcontext();
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001771 else
Miklos Szeredid169f312004-09-22 08:48:26 +00001772 return &context;
1773}
1774
1775void __fuse_set_getcontext_func(struct fuse_context *(*func)(void))
1776{
1777 fuse_getcontext = func;
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001778}
1779
Miklos Szeredic40748a2004-02-20 16:38:45 +00001780static int check_version(struct fuse *f)
1781{
1782 int res;
Miklos Szeredi162bcbb2004-11-29 23:43:44 +00001783 const char *version_file = FUSE_VERSION_FILE_NEW;
Miklos Szeredif3845c42004-11-20 11:18:34 +00001784 FILE *vf = fopen(version_file, "r");
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001785 if (vf == NULL) {
Miklos Szeredia25d4c22004-11-23 22:32:16 +00001786 version_file = FUSE_VERSION_FILE_OLD;
Miklos Szeredif3845c42004-11-20 11:18:34 +00001787 vf = fopen(version_file, "r");
1788 if (vf == NULL) {
Miklos Szeredia25d4c22004-11-23 22:32:16 +00001789 struct stat tmp;
1790 if (stat(FUSE_DEV_OLD, &tmp) != -1) {
1791 fprintf(stderr, "fuse: kernel interface too old, need >= %i.%i\n",
1792 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
1793 return -1;
1794 } else {
1795 fprintf(stderr, "fuse: warning: version of kernel interface unknown\n");
1796 return 0;
1797 }
Miklos Szeredif3845c42004-11-20 11:18:34 +00001798 }
Miklos Szeredic40748a2004-02-20 16:38:45 +00001799 }
1800 res = fscanf(vf, "%i.%i", &f->majorver, &f->minorver);
1801 fclose(vf);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001802 if (res != 2) {
Miklos Szeredif3845c42004-11-20 11:18:34 +00001803 fprintf(stderr, "fuse: error reading %s\n", version_file);
Miklos Szeredic40748a2004-02-20 16:38:45 +00001804 return -1;
1805 }
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001806 if (f->majorver != FUSE_KERNEL_VERSION) {
Miklos Szeredic40748a2004-02-20 16:38:45 +00001807 fprintf(stderr, "fuse: bad kernel interface major version: needs %i\n",
1808 FUSE_KERNEL_VERSION);
1809 return -1;
1810 }
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001811 if (f->minorver < FUSE_KERNEL_MINOR_VERSION_NEED) {
Miklos Szeredi256739a2004-11-20 12:22:37 +00001812 fprintf(stderr, "fuse: kernel interface too old: need >= %i.%i\n",
Miklos Szeredic40748a2004-02-20 16:38:45 +00001813 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
1814 return -1;
1815 }
1816
1817 return 0;
1818}
1819
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001820
1821int fuse_is_lib_option(const char *opt)
1822{
1823 if (strcmp(opt, "debug") == 0 ||
Miklos Szeredia13d9002004-11-02 17:32:03 +00001824 strcmp(opt, "hard_remove") == 0 ||
1825 strcmp(opt, "use_ino") == 0)
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001826 return 1;
1827 else
1828 return 0;
1829}
1830
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001831static int parse_lib_opts(struct fuse *f, const char *opts)
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001832{
1833 if (opts) {
1834 char *xopts = strdup(opts);
1835 char *s = xopts;
1836 char *opt;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001837
1838 if (xopts == NULL)
1839 return -1;
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001840
1841 while((opt = strsep(&s, ","))) {
1842 if (strcmp(opt, "debug") == 0)
1843 f->flags |= FUSE_DEBUG;
1844 else if (strcmp(opt, "hard_remove") == 0)
1845 f->flags |= FUSE_HARD_REMOVE;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001846 else if (strcmp(opt, "use_ino") == 0)
1847 f->flags |= FUSE_USE_INO;
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001848 else
1849 fprintf(stderr, "fuse: warning: unknown option `%s'\n", opt);
1850 }
1851 free(xopts);
1852 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001853 return 0;
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001854}
1855
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001856struct fuse *fuse_new_common(int fd, const char *opts,
1857 const struct fuse_operations *op,
1858 size_t op_size, int compat)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001859{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001860 struct fuse *f;
1861 struct node *root;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001862
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001863 if (sizeof(struct fuse_operations_i) < op_size) {
1864 fprintf(stderr, "fuse: warning: library too old, some operations may not not work\n");
1865 op_size = sizeof(struct fuse_operations_i);
1866 }
1867
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001868 f = (struct fuse *) calloc(1, sizeof(struct fuse));
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001869 if (f == NULL)
1870 goto out;
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001871
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001872 if (check_version(f) == -1)
1873 goto out_free;
Miklos Szeredic40748a2004-02-20 16:38:45 +00001874
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001875 if (parse_lib_opts(f, opts) == -1)
1876 goto out_free;
1877
Miklos Szeredi8cffdb92001-11-09 14:49:18 +00001878 f->fd = fd;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001879 f->ctr = 0;
Miklos Szeredi76f65782004-02-19 16:55:40 +00001880 f->generation = 0;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001881 /* FIXME: Dynamic hash table */
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001882 f->name_table_size = 14057;
1883 f->name_table = (struct node **)
1884 calloc(1, sizeof(struct node *) * f->name_table_size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001885 if (f->name_table == NULL)
1886 goto out_free;
1887
Miklos Szeredia13d9002004-11-02 17:32:03 +00001888 f->id_table_size = 14057;
1889 f->id_table = (struct node **)
1890 calloc(1, sizeof(struct node *) * f->id_table_size);
1891 if (f->id_table == NULL)
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001892 goto out_free_name_table;
1893
Miklos Szeredia25d4c22004-11-23 22:32:16 +00001894#ifndef USE_UCLIBC
1895 pthread_mutex_init(&f->lock, NULL);
1896#else
1897 {
1898 pthread_mutexattr_t attr;
1899 pthread_mutexattr_init(&attr);
1900 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
1901 pthread_mutex_init(&f->lock, &attr);
1902 pthread_mutexattr_destroy(&attr);
1903 }
1904#endif
Miklos Szeredi33232032001-11-19 17:55:51 +00001905 f->numworker = 0;
1906 f->numavail = 0;
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001907 memcpy(&f->op, op, op_size);
1908 f->compat = compat;
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001909 f->exited = 0;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001910
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001911 root = (struct node *) calloc(1, sizeof(struct node));
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001912 if (root == NULL)
Miklos Szeredia13d9002004-11-02 17:32:03 +00001913 goto out_free_id_table;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001914
Miklos Szeredi8cffdb92001-11-09 14:49:18 +00001915 root->mode = 0;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001916 root->rdev = 0;
1917 root->name = strdup("/");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001918 if (root->name == NULL)
1919 goto out_free_root;
1920
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001921 root->parent = 0;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001922 root->nodeid = FUSE_ROOT_ID;
Miklos Szeredi76f65782004-02-19 16:55:40 +00001923 root->generation = 0;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001924 hash_id(f, root);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001925
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001926 return f;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001927
1928 out_free_root:
1929 free(root);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001930 out_free_id_table:
1931 free(f->id_table);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001932 out_free_name_table:
1933 free(f->name_table);
1934 out_free:
1935 free(f);
1936 out:
1937 fprintf(stderr, "fuse: failed to allocate fuse object\n");
1938 return NULL;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001939}
1940
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001941struct fuse *fuse_new(int fd, const char *opts,
1942 const struct fuse_operations *op, size_t op_size)
1943{
1944 return fuse_new_common(fd, opts, op, op_size, 0);
1945}
1946
1947struct fuse *_fuse_new_compat2(int fd, const char *opts,
1948 const struct _fuse_operations_compat2 *op)
1949{
1950 return fuse_new_common(fd, opts, (struct fuse_operations *) op,
1951 sizeof(struct _fuse_operations_compat2), 21);
1952}
1953
1954struct fuse *_fuse_new_compat1(int fd, int flags,
1955 const struct _fuse_operations_compat1 *op)
1956{
1957 char *opts = NULL;
1958 if (flags & _FUSE_DEBUG_COMPAT1)
1959 opts = "debug";
1960 return fuse_new_common(fd, opts, (struct fuse_operations *) op,
1961 sizeof(struct _fuse_operations_compat1), 11);
1962}
1963
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001964void fuse_destroy(struct fuse *f)
1965{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001966 size_t i;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001967 for (i = 0; i < f->id_table_size; i++) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001968 struct node *node;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001969
Miklos Szeredia13d9002004-11-02 17:32:03 +00001970 for (node = f->id_table[i]; node != NULL; node = node->id_next) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001971 if (node->is_hidden) {
Miklos Szeredia13d9002004-11-02 17:32:03 +00001972 char *path = get_path(f, node->nodeid);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001973 if (path)
1974 f->op.unlink(path);
1975 }
1976 }
1977 }
Miklos Szeredia13d9002004-11-02 17:32:03 +00001978 for (i = 0; i < f->id_table_size; i++) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001979 struct node *node;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001980 struct node *next;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001981
Miklos Szeredia13d9002004-11-02 17:32:03 +00001982 for (node = f->id_table[i]; node != NULL; node = next) {
1983 next = node->id_next;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001984 free_node(node);
1985 }
1986 }
Miklos Szeredia13d9002004-11-02 17:32:03 +00001987 free(f->id_table);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001988 free(f->name_table);
Miklos Szeredia181e612001-11-06 12:03:23 +00001989 pthread_mutex_destroy(&f->lock);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001990 free(f);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001991}
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001992
1993__asm__(".symver _fuse_new_compat2,fuse_new@");