blob: afc8c74ba97cd9b7f7ae77e859706a8707f32e9e [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 Szeredia181e612001-11-06 12:03:23 +00001236
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001237 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001238 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001239 if (path != NULL) {
1240 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi1eea0322004-09-27 18:50:11 +00001241 printf("WRITE%s[%lu] %u bytes to %llu\n",
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001242 arg->writepage ? "PAGE" : "", arg->fh, arg->size,
1243 arg->offset);
Miklos Szeredi6ebe2342002-01-06 21:44:16 +00001244 fflush(stdout);
1245 }
1246
Miklos Szeredia181e612001-11-06 12:03:23 +00001247 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001248 if (f->op.write)
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001249 res = f->op.write(path, PARAM(arg), arg->size, arg->offset, &fi);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001250 free(path);
Miklos Szeredia181e612001-11-06 12:03:23 +00001251 }
1252
Miklos Szerediad051c32004-07-02 09:22:50 +00001253 if (res >= 0) {
1254 outarg.size = res;
1255 res = 0;
Miklos Szeredia181e612001-11-06 12:03:23 +00001256 }
1257
Miklos Szerediad051c32004-07-02 09:22:50 +00001258 send_reply(f, in, res, &outarg, sizeof(outarg));
Miklos Szeredia181e612001-11-06 12:03:23 +00001259}
1260
Miklos Szeredi77f39942004-03-25 11:17:52 +00001261static int default_statfs(struct statfs *buf)
1262{
1263 buf->f_namelen = 255;
1264 buf->f_bsize = 512;
1265 return 0;
1266}
1267
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001268static void convert_statfs_compat(struct _fuse_statfs_compat1 *compatbuf,
1269 struct statfs *statfs)
1270{
1271 statfs->f_bsize = compatbuf->block_size;
1272 statfs->f_blocks = compatbuf->blocks;
1273 statfs->f_bfree = compatbuf->blocks_free;
1274 statfs->f_bavail = compatbuf->blocks_free;
1275 statfs->f_files = compatbuf->files;
1276 statfs->f_ffree = compatbuf->files_free;
1277 statfs->f_namelen = compatbuf->namelen;
1278}
1279
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001280static void convert_statfs(struct statfs *statfs, struct fuse_kstatfs *kstatfs)
1281{
1282 kstatfs->bsize = statfs->f_bsize;
1283 kstatfs->blocks = statfs->f_blocks;
1284 kstatfs->bfree = statfs->f_bfree;
1285 kstatfs->bavail = statfs->f_bavail;
1286 kstatfs->files = statfs->f_files;
1287 kstatfs->ffree = statfs->f_ffree;
1288 kstatfs->namelen = statfs->f_namelen;
1289}
1290
Mark Glinesd84b39a2002-01-07 16:32:02 +00001291static void do_statfs(struct fuse *f, struct fuse_in_header *in)
1292{
1293 int res;
Mark Glinesd84b39a2002-01-07 16:32:02 +00001294 struct fuse_statfs_out arg;
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001295 struct statfs buf;
Mark Glinesd84b39a2002-01-07 16:32:02 +00001296
Miklos Szeredi77f39942004-03-25 11:17:52 +00001297 memset(&buf, 0, sizeof(struct statfs));
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001298 if (f->op.statfs.curr) {
1299 if (!f->compat || f->compat > 11)
1300 res = f->op.statfs.curr("/", &buf);
1301 else {
1302 struct _fuse_statfs_compat1 compatbuf;
1303 memset(&compatbuf, 0, sizeof(struct _fuse_statfs_compat1));
1304 res = f->op.statfs.compat1(&compatbuf);
1305 if (res == 0)
1306 convert_statfs_compat(&compatbuf, &buf);
1307 }
1308 }
Miklos Szeredi77f39942004-03-25 11:17:52 +00001309 else
1310 res = default_statfs(&buf);
1311
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001312 if (res == 0)
Miklos Szeredi77f39942004-03-25 11:17:52 +00001313 convert_statfs(&buf, &arg.st);
Miklos Szeredi4b2bef42002-01-09 12:23:27 +00001314
Mark Glinesd84b39a2002-01-07 16:32:02 +00001315 send_reply(f, in, res, &arg, sizeof(arg));
1316}
1317
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001318static void do_fsync(struct fuse *f, struct fuse_in_header *in,
1319 struct fuse_fsync_in *inarg)
1320{
1321 int res;
1322 char *path;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001323 struct fuse_file_info fi;
1324
1325 memset(&fi, 0, sizeof(fi));
1326 fi.fh = inarg->fh;
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001327
1328 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001329 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001330 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001331 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi1eea0322004-09-27 18:50:11 +00001332 printf("FSYNC[%lu]\n", inarg->fh);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001333 fflush(stdout);
1334 }
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001335 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001336 if (f->op.fsync)
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001337 res = f->op.fsync(path, inarg->datasync, &fi);
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001338 free(path);
1339 }
1340 send_reply(f, in, res, NULL, 0);
1341}
1342
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001343static void do_setxattr(struct fuse *f, struct fuse_in_header *in,
1344 struct fuse_setxattr_in *arg)
1345{
1346 int res;
1347 char *path;
1348 char *name = PARAM(arg);
1349 unsigned char *value = name + strlen(name) + 1;
1350
1351 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001352 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001353 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001354 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001355 if (f->op.setxattr)
1356 res = f->op.setxattr(path, name, value, arg->size, arg->flags);
1357 free(path);
1358 }
1359 send_reply(f, in, res, NULL, 0);
1360}
1361
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001362static int common_getxattr(struct fuse *f, struct fuse_in_header *in,
1363 const char *name, char *value, size_t size)
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001364{
1365 int res;
1366 char *path;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001367
1368 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001369 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001370 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001371 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001372 if (f->op.getxattr)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001373 res = f->op.getxattr(path, name, value, size);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001374 free(path);
1375 }
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001376 return res;
1377}
1378
1379static void do_getxattr_read(struct fuse *f, struct fuse_in_header *in,
1380 const char *name, size_t size)
1381{
1382 int res;
1383 char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001384 if (outbuf == NULL)
1385 send_reply(f, in, -ENOMEM, NULL, 0);
1386 else {
1387 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
1388 char *value = outbuf + sizeof(struct fuse_out_header);
1389
1390 res = common_getxattr(f, in, name, value, size);
1391 size = 0;
1392 if (res > 0) {
1393 size = res;
1394 res = 0;
1395 }
1396 memset(out, 0, sizeof(struct fuse_out_header));
1397 out->unique = in->unique;
1398 out->error = res;
1399
1400 send_reply_raw(f, outbuf, sizeof(struct fuse_out_header) + size, 0);
1401 free(outbuf);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001402 }
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001403}
1404
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001405static void do_getxattr_size(struct fuse *f, struct fuse_in_header *in,
1406 const char *name)
1407{
1408 int res;
1409 struct fuse_getxattr_out arg;
1410
1411 res = common_getxattr(f, in, name, NULL, 0);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001412 if (res >= 0) {
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001413 arg.size = res;
1414 res = 0;
1415 }
1416 send_reply(f, in, res, &arg, sizeof(arg));
1417}
1418
1419static void do_getxattr(struct fuse *f, struct fuse_in_header *in,
1420 struct fuse_getxattr_in *arg)
1421{
1422 char *name = PARAM(arg);
1423
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001424 if (arg->size)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001425 do_getxattr_read(f, in, name, arg->size);
1426 else
1427 do_getxattr_size(f, in, name);
1428}
1429
1430static int common_listxattr(struct fuse *f, struct fuse_in_header *in,
1431 char *list, size_t size)
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001432{
1433 int res;
1434 char *path;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001435
1436 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001437 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001438 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001439 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001440 if (f->op.listxattr)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001441 res = f->op.listxattr(path, list, size);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001442 free(path);
1443 }
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001444 return res;
1445}
1446
1447static void do_listxattr_read(struct fuse *f, struct fuse_in_header *in,
1448 size_t size)
1449{
1450 int res;
1451 char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001452 if (outbuf == NULL)
1453 send_reply(f, in, -ENOMEM, NULL, 0);
1454 else {
1455 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
1456 char *list = outbuf + sizeof(struct fuse_out_header);
1457
1458 res = common_listxattr(f, in, list, size);
1459 size = 0;
1460 if (res > 0) {
1461 size = res;
1462 res = 0;
1463 }
1464 memset(out, 0, sizeof(struct fuse_out_header));
1465 out->unique = in->unique;
1466 out->error = res;
1467
1468 send_reply_raw(f, outbuf, sizeof(struct fuse_out_header) + size, 0);
1469 free(outbuf);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001470 }
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001471}
1472
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001473static void do_listxattr_size(struct fuse *f, struct fuse_in_header *in)
1474{
1475 int res;
1476 struct fuse_getxattr_out arg;
1477
1478 res = common_listxattr(f, in, NULL, 0);
1479 if (res >= 0) {
1480 arg.size = res;
1481 res = 0;
1482 }
1483 send_reply(f, in, res, &arg, sizeof(arg));
1484}
1485
1486static void do_listxattr(struct fuse *f, struct fuse_in_header *in,
1487 struct fuse_getxattr_in *arg)
1488{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001489 if (arg->size)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001490 do_listxattr_read(f, in, arg->size);
1491 else
1492 do_listxattr_size(f, in);
1493}
1494
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001495static void do_removexattr(struct fuse *f, struct fuse_in_header *in,
1496 char *name)
1497{
1498 int res;
1499 char *path;
1500
1501 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001502 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001503 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001504 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001505 if (f->op.removexattr)
1506 res = f->op.removexattr(path, name);
1507 free(path);
1508 }
1509 send_reply(f, in, res, NULL, 0);
1510}
1511
1512
Miklos Szeredi43696432001-11-18 19:15:05 +00001513static void free_cmd(struct fuse_cmd *cmd)
1514{
1515 free(cmd->buf);
1516 free(cmd);
1517}
1518
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001519void __fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd)
Miklos Szeredia181e612001-11-06 12:03:23 +00001520{
Miklos Szeredia181e612001-11-06 12:03:23 +00001521 struct fuse_in_header *in = (struct fuse_in_header *) cmd->buf;
1522 void *inarg = cmd->buf + sizeof(struct fuse_in_header);
1523 size_t argsize;
Miklos Szeredid169f312004-09-22 08:48:26 +00001524 struct fuse_context *ctx = fuse_get_context();
Miklos Szeredia181e612001-11-06 12:03:23 +00001525
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001526 dec_avail(f);
Miklos Szeredi33232032001-11-19 17:55:51 +00001527
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001528 if ((f->flags & FUSE_DEBUG)) {
Miklos Szeredia13d9002004-11-02 17:32:03 +00001529 printf("unique: %i, opcode: %s (%i), nodeid: %li, insize: %i\n",
1530 in->unique, opname(in->opcode), in->opcode, in->nodeid,
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001531 cmd->buflen);
Miklos Szeredic0938ea2001-11-07 12:35:06 +00001532 fflush(stdout);
1533 }
Miklos Szeredife25def2001-12-20 15:38:05 +00001534
Miklos Szeredid169f312004-09-22 08:48:26 +00001535 ctx->fuse = f;
Miklos Szeredife25def2001-12-20 15:38:05 +00001536 ctx->uid = in->uid;
1537 ctx->gid = in->gid;
Miklos Szeredi1f18db52004-09-27 06:54:49 +00001538 ctx->pid = in->pid;
Miklos Szeredia181e612001-11-06 12:03:23 +00001539
1540 argsize = cmd->buflen - sizeof(struct fuse_in_header);
1541
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001542 switch (in->opcode) {
Miklos Szeredia181e612001-11-06 12:03:23 +00001543 case FUSE_LOOKUP:
1544 do_lookup(f, in, (char *) inarg);
1545 break;
1546
Miklos Szeredia181e612001-11-06 12:03:23 +00001547 case FUSE_GETATTR:
1548 do_getattr(f, in);
1549 break;
1550
1551 case FUSE_SETATTR:
1552 do_setattr(f, in, (struct fuse_setattr_in *) inarg);
1553 break;
1554
1555 case FUSE_READLINK:
1556 do_readlink(f, in);
1557 break;
1558
1559 case FUSE_GETDIR:
1560 do_getdir(f, in);
1561 break;
1562
1563 case FUSE_MKNOD:
1564 do_mknod(f, in, (struct fuse_mknod_in *) inarg);
1565 break;
1566
1567 case FUSE_MKDIR:
1568 do_mkdir(f, in, (struct fuse_mkdir_in *) inarg);
1569 break;
1570
1571 case FUSE_UNLINK:
Miklos Szeredib5958612004-02-20 14:10:49 +00001572 do_unlink(f, in, (char *) inarg);
1573 break;
1574
Miklos Szeredia181e612001-11-06 12:03:23 +00001575 case FUSE_RMDIR:
Miklos Szeredib5958612004-02-20 14:10:49 +00001576 do_rmdir(f, in, (char *) inarg);
Miklos Szeredia181e612001-11-06 12:03:23 +00001577 break;
1578
1579 case FUSE_SYMLINK:
1580 do_symlink(f, in, (char *) inarg,
1581 ((char *) inarg) + strlen((char *) inarg) + 1);
1582 break;
1583
1584 case FUSE_RENAME:
1585 do_rename(f, in, (struct fuse_rename_in *) inarg);
1586 break;
1587
1588 case FUSE_LINK:
1589 do_link(f, in, (struct fuse_link_in *) inarg);
1590 break;
1591
1592 case FUSE_OPEN:
1593 do_open(f, in, (struct fuse_open_in *) inarg);
1594 break;
1595
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001596 case FUSE_FLUSH:
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001597 do_flush(f, in, (struct fuse_flush_in *) inarg);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001598 break;
1599
Miklos Szeredi9478e862002-12-11 09:50:26 +00001600 case FUSE_RELEASE:
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001601 do_release(f, in, (struct fuse_release_in *) inarg);
Miklos Szeredi9478e862002-12-11 09:50:26 +00001602 break;
1603
Miklos Szeredia181e612001-11-06 12:03:23 +00001604 case FUSE_READ:
1605 do_read(f, in, (struct fuse_read_in *) inarg);
1606 break;
1607
1608 case FUSE_WRITE:
1609 do_write(f, in, (struct fuse_write_in *) inarg);
1610 break;
1611
Mark Glinesd84b39a2002-01-07 16:32:02 +00001612 case FUSE_STATFS:
1613 do_statfs(f, in);
1614 break;
1615
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001616 case FUSE_FSYNC:
1617 do_fsync(f, in, (struct fuse_fsync_in *) inarg);
1618 break;
1619
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001620 case FUSE_SETXATTR:
1621 do_setxattr(f, in, (struct fuse_setxattr_in *) inarg);
1622 break;
1623
1624 case FUSE_GETXATTR:
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001625 do_getxattr(f, in, (struct fuse_getxattr_in *) inarg);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001626 break;
1627
1628 case FUSE_LISTXATTR:
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001629 do_listxattr(f, in, (struct fuse_getxattr_in *) inarg);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001630 break;
1631
1632 case FUSE_REMOVEXATTR:
1633 do_removexattr(f, in, (char *) inarg);
1634 break;
1635
Miklos Szeredia181e612001-11-06 12:03:23 +00001636 default:
Miklos Szeredi43696432001-11-18 19:15:05 +00001637 send_reply(f, in, -ENOSYS, NULL, 0);
Miklos Szeredia181e612001-11-06 12:03:23 +00001638 }
Miklos Szeredi43696432001-11-18 19:15:05 +00001639
1640 free_cmd(cmd);
Miklos Szeredia181e612001-11-06 12:03:23 +00001641}
1642
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001643int __fuse_exited(struct fuse* f)
1644{
1645 return f->exited;
1646}
1647
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001648struct fuse_cmd *__fuse_read_cmd(struct fuse *f)
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001649{
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001650 ssize_t res;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001651 struct fuse_cmd *cmd;
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001652 struct fuse_in_header *in;
1653 void *inarg;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001654
Miklos Szeredi43696432001-11-18 19:15:05 +00001655 cmd = (struct fuse_cmd *) malloc(sizeof(struct fuse_cmd));
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001656 if (cmd == NULL) {
1657 fprintf(stderr, "fuse: failed to allocate cmd in read\n");
1658 return NULL;
1659 }
Miklos Szeredi43696432001-11-18 19:15:05 +00001660 cmd->buf = (char *) malloc(FUSE_MAX_IN);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001661 if (cmd->buf == NULL) {
1662 fprintf(stderr, "fuse: failed to allocate read buffer\n");
1663 free(cmd);
1664 return NULL;
1665 }
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001666 in = (struct fuse_in_header *) cmd->buf;
1667 inarg = cmd->buf + sizeof(struct fuse_in_header);
Miklos Szeredi43696432001-11-18 19:15:05 +00001668
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001669 res = read(f->fd, cmd->buf, FUSE_MAX_IN);
1670 if (res == -1) {
1671 free_cmd(cmd);
1672 if (__fuse_exited(f) || errno == EINTR)
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001673 return NULL;
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001674
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001675 /* ENODEV means we got unmounted, so we silenty return failure */
1676 if (errno != ENODEV) {
1677 /* BAD... This will happen again */
1678 perror("fuse: reading device");
1679 }
1680
1681 fuse_exit(f);
1682 return NULL;
1683 }
1684 if ((size_t) res < sizeof(struct fuse_in_header)) {
1685 free_cmd(cmd);
1686 /* Cannot happen */
1687 fprintf(stderr, "short read on fuse device\n");
1688 fuse_exit(f);
1689 return NULL;
1690 }
1691 cmd->buflen = res;
1692
1693 /* Forget is special, it can be done without messing with threads. */
1694 if (in->opcode == FUSE_FORGET) {
1695 do_forget(f, in, (struct fuse_forget_in *) inarg);
1696 free_cmd(cmd);
1697 return NULL;
1698 }
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001699
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001700 return cmd;
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001701}
1702
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001703int fuse_loop(struct fuse *f)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001704{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001705 if (f == NULL)
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001706 return -1;
Miklos Szeredic40748a2004-02-20 16:38:45 +00001707
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001708 while (1) {
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001709 struct fuse_cmd *cmd;
1710
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001711 if (__fuse_exited(f))
Miklos Szeredi874e3c12004-11-01 23:15:20 +00001712 break;
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001713
1714 cmd = __fuse_read_cmd(f);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001715 if (cmd == NULL)
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001716 continue;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001717
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001718 __fuse_process_cmd(f, cmd);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001719 }
Miklos Szeredi874e3c12004-11-01 23:15:20 +00001720 f->exited = 0;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001721 return 0;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001722}
1723
Miklos Szeredi891b8742004-07-29 09:27:49 +00001724int fuse_invalidate(struct fuse *f, const char *path)
1725{
1726 int res;
1727 int err;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001728 nodeid_t nodeid;
1729 unsigned long ino;
Miklos Szeredi891b8742004-07-29 09:27:49 +00001730 struct fuse_user_header h;
1731
Miklos Szeredia13d9002004-11-02 17:32:03 +00001732 err = path_lookup(f, path, &nodeid, &ino);
Miklos Szeredi891b8742004-07-29 09:27:49 +00001733 if (err) {
1734 if (err == -ENOENT)
1735 return 0;
1736 else
1737 return err;
1738 }
1739
1740 memset(&h, 0, sizeof(struct fuse_user_header));
1741 h.opcode = FUSE_INVALIDATE;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001742 h.nodeid = nodeid;
Miklos Szeredi891b8742004-07-29 09:27:49 +00001743 h.ino = ino;
1744
1745 if ((f->flags & FUSE_DEBUG)) {
Miklos Szeredia13d9002004-11-02 17:32:03 +00001746 printf("INVALIDATE nodeid: %li\n", nodeid);
Miklos Szeredi891b8742004-07-29 09:27:49 +00001747 fflush(stdout);
1748 }
1749
1750 res = write(f->fd, &h, sizeof(struct fuse_user_header));
1751 if (res == -1) {
1752 if (errno != ENOENT) {
1753 perror("fuse: writing device");
1754 return -errno;
1755 }
1756 }
1757 return 0;
1758}
1759
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001760void fuse_exit(struct fuse *f)
1761{
1762 f->exited = 1;
1763}
1764
Miklos Szeredid169f312004-09-22 08:48:26 +00001765struct fuse_context *fuse_get_context()
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001766{
Miklos Szeredid169f312004-09-22 08:48:26 +00001767 static struct fuse_context context;
1768 if (fuse_getcontext)
1769 return fuse_getcontext();
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001770 else
Miklos Szeredid169f312004-09-22 08:48:26 +00001771 return &context;
1772}
1773
1774void __fuse_set_getcontext_func(struct fuse_context *(*func)(void))
1775{
1776 fuse_getcontext = func;
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001777}
1778
Miklos Szeredic40748a2004-02-20 16:38:45 +00001779static int check_version(struct fuse *f)
1780{
1781 int res;
Miklos Szeredi162bcbb2004-11-29 23:43:44 +00001782 const char *version_file = FUSE_VERSION_FILE_NEW;
Miklos Szeredif3845c42004-11-20 11:18:34 +00001783 FILE *vf = fopen(version_file, "r");
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001784 if (vf == NULL) {
Miklos Szeredia25d4c22004-11-23 22:32:16 +00001785 version_file = FUSE_VERSION_FILE_OLD;
Miklos Szeredif3845c42004-11-20 11:18:34 +00001786 vf = fopen(version_file, "r");
1787 if (vf == NULL) {
Miklos Szeredia25d4c22004-11-23 22:32:16 +00001788 struct stat tmp;
1789 if (stat(FUSE_DEV_OLD, &tmp) != -1) {
1790 fprintf(stderr, "fuse: kernel interface too old, need >= %i.%i\n",
1791 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
1792 return -1;
1793 } else {
1794 fprintf(stderr, "fuse: warning: version of kernel interface unknown\n");
1795 return 0;
1796 }
Miklos Szeredif3845c42004-11-20 11:18:34 +00001797 }
Miklos Szeredic40748a2004-02-20 16:38:45 +00001798 }
1799 res = fscanf(vf, "%i.%i", &f->majorver, &f->minorver);
1800 fclose(vf);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001801 if (res != 2) {
Miklos Szeredif3845c42004-11-20 11:18:34 +00001802 fprintf(stderr, "fuse: error reading %s\n", version_file);
Miklos Szeredic40748a2004-02-20 16:38:45 +00001803 return -1;
1804 }
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001805 if (f->majorver != FUSE_KERNEL_VERSION) {
Miklos Szeredic40748a2004-02-20 16:38:45 +00001806 fprintf(stderr, "fuse: bad kernel interface major version: needs %i\n",
1807 FUSE_KERNEL_VERSION);
1808 return -1;
1809 }
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001810 if (f->minorver < FUSE_KERNEL_MINOR_VERSION_NEED) {
Miklos Szeredi256739a2004-11-20 12:22:37 +00001811 fprintf(stderr, "fuse: kernel interface too old: need >= %i.%i\n",
Miklos Szeredic40748a2004-02-20 16:38:45 +00001812 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
1813 return -1;
1814 }
1815
1816 return 0;
1817}
1818
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001819
1820int fuse_is_lib_option(const char *opt)
1821{
1822 if (strcmp(opt, "debug") == 0 ||
Miklos Szeredia13d9002004-11-02 17:32:03 +00001823 strcmp(opt, "hard_remove") == 0 ||
1824 strcmp(opt, "use_ino") == 0)
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001825 return 1;
1826 else
1827 return 0;
1828}
1829
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001830static int parse_lib_opts(struct fuse *f, const char *opts)
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001831{
1832 if (opts) {
1833 char *xopts = strdup(opts);
1834 char *s = xopts;
1835 char *opt;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001836
1837 if (xopts == NULL)
1838 return -1;
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001839
1840 while((opt = strsep(&s, ","))) {
1841 if (strcmp(opt, "debug") == 0)
1842 f->flags |= FUSE_DEBUG;
1843 else if (strcmp(opt, "hard_remove") == 0)
1844 f->flags |= FUSE_HARD_REMOVE;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001845 else if (strcmp(opt, "use_ino") == 0)
1846 f->flags |= FUSE_USE_INO;
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001847 else
1848 fprintf(stderr, "fuse: warning: unknown option `%s'\n", opt);
1849 }
1850 free(xopts);
1851 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001852 return 0;
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001853}
1854
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001855struct fuse *fuse_new_common(int fd, const char *opts,
1856 const struct fuse_operations *op,
1857 size_t op_size, int compat)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001858{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001859 struct fuse *f;
1860 struct node *root;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001861
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001862 if (sizeof(struct fuse_operations_i) < op_size) {
1863 fprintf(stderr, "fuse: warning: library too old, some operations may not not work\n");
1864 op_size = sizeof(struct fuse_operations_i);
1865 }
1866
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001867 f = (struct fuse *) calloc(1, sizeof(struct fuse));
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001868 if (f == NULL)
1869 goto out;
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001870
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001871 if (check_version(f) == -1)
1872 goto out_free;
Miklos Szeredic40748a2004-02-20 16:38:45 +00001873
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001874 if (parse_lib_opts(f, opts) == -1)
1875 goto out_free;
1876
Miklos Szeredi8cffdb92001-11-09 14:49:18 +00001877 f->fd = fd;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001878 f->ctr = 0;
Miklos Szeredi76f65782004-02-19 16:55:40 +00001879 f->generation = 0;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001880 /* FIXME: Dynamic hash table */
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001881 f->name_table_size = 14057;
1882 f->name_table = (struct node **)
1883 calloc(1, sizeof(struct node *) * f->name_table_size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001884 if (f->name_table == NULL)
1885 goto out_free;
1886
Miklos Szeredia13d9002004-11-02 17:32:03 +00001887 f->id_table_size = 14057;
1888 f->id_table = (struct node **)
1889 calloc(1, sizeof(struct node *) * f->id_table_size);
1890 if (f->id_table == NULL)
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001891 goto out_free_name_table;
1892
Miklos Szeredia25d4c22004-11-23 22:32:16 +00001893#ifndef USE_UCLIBC
1894 pthread_mutex_init(&f->lock, NULL);
1895#else
1896 {
1897 pthread_mutexattr_t attr;
1898 pthread_mutexattr_init(&attr);
1899 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
1900 pthread_mutex_init(&f->lock, &attr);
1901 pthread_mutexattr_destroy(&attr);
1902 }
1903#endif
Miklos Szeredi33232032001-11-19 17:55:51 +00001904 f->numworker = 0;
1905 f->numavail = 0;
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001906 memcpy(&f->op, op, op_size);
1907 f->compat = compat;
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001908 f->exited = 0;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001909
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001910 root = (struct node *) calloc(1, sizeof(struct node));
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001911 if (root == NULL)
Miklos Szeredia13d9002004-11-02 17:32:03 +00001912 goto out_free_id_table;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001913
Miklos Szeredi8cffdb92001-11-09 14:49:18 +00001914 root->mode = 0;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001915 root->rdev = 0;
1916 root->name = strdup("/");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001917 if (root->name == NULL)
1918 goto out_free_root;
1919
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001920 root->parent = 0;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001921 root->nodeid = FUSE_ROOT_ID;
Miklos Szeredi76f65782004-02-19 16:55:40 +00001922 root->generation = 0;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001923 hash_id(f, root);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001924
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001925 return f;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001926
1927 out_free_root:
1928 free(root);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001929 out_free_id_table:
1930 free(f->id_table);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001931 out_free_name_table:
1932 free(f->name_table);
1933 out_free:
1934 free(f);
1935 out:
1936 fprintf(stderr, "fuse: failed to allocate fuse object\n");
1937 return NULL;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001938}
1939
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001940struct fuse *fuse_new(int fd, const char *opts,
1941 const struct fuse_operations *op, size_t op_size)
1942{
1943 return fuse_new_common(fd, opts, op, op_size, 0);
1944}
1945
1946struct fuse *_fuse_new_compat2(int fd, const char *opts,
1947 const struct _fuse_operations_compat2 *op)
1948{
1949 return fuse_new_common(fd, opts, (struct fuse_operations *) op,
1950 sizeof(struct _fuse_operations_compat2), 21);
1951}
1952
1953struct fuse *_fuse_new_compat1(int fd, int flags,
1954 const struct _fuse_operations_compat1 *op)
1955{
1956 char *opts = NULL;
1957 if (flags & _FUSE_DEBUG_COMPAT1)
1958 opts = "debug";
1959 return fuse_new_common(fd, opts, (struct fuse_operations *) op,
1960 sizeof(struct _fuse_operations_compat1), 11);
1961}
1962
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001963void fuse_destroy(struct fuse *f)
1964{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001965 size_t i;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001966 for (i = 0; i < f->id_table_size; i++) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001967 struct node *node;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001968
Miklos Szeredia13d9002004-11-02 17:32:03 +00001969 for (node = f->id_table[i]; node != NULL; node = node->id_next) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001970 if (node->is_hidden) {
Miklos Szeredia13d9002004-11-02 17:32:03 +00001971 char *path = get_path(f, node->nodeid);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001972 if (path)
1973 f->op.unlink(path);
1974 }
1975 }
1976 }
Miklos Szeredia13d9002004-11-02 17:32:03 +00001977 for (i = 0; i < f->id_table_size; i++) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001978 struct node *node;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001979 struct node *next;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001980
Miklos Szeredia13d9002004-11-02 17:32:03 +00001981 for (node = f->id_table[i]; node != NULL; node = next) {
1982 next = node->id_next;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001983 free_node(node);
1984 }
1985 }
Miklos Szeredia13d9002004-11-02 17:32:03 +00001986 free(f->id_table);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001987 free(f->name_table);
Miklos Szeredia181e612001-11-06 12:03:23 +00001988 pthread_mutex_destroy(&f->lock);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001989 free(f);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001990}
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001991
1992__asm__(".symver _fuse_new_compat2,fuse_new@");