blob: 99bca56e65703d56ba225e914b398d7e780f94fa [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 +000064static inline void dec_avail(struct fuse *f)
65{
66 pthread_mutex_lock(&f->lock);
67 f->numavail --;
68 pthread_mutex_unlock(&f->lock);
69}
70
Miklos Szeredif458b8c2004-12-07 16:46:42 +000071static struct node *get_node_nocheck(struct fuse *f, nodeid_t nodeid)
Miklos Szeredia181e612001-11-06 12:03:23 +000072{
Miklos Szeredia13d9002004-11-02 17:32:03 +000073 size_t hash = nodeid % f->id_table_size;
Miklos Szeredi97c61e92001-11-07 12:09:43 +000074 struct node *node;
75
Miklos Szeredia13d9002004-11-02 17:32:03 +000076 for (node = f->id_table[hash]; node != NULL; node = node->id_next)
77 if (node->nodeid == nodeid)
Miklos Szeredi97c61e92001-11-07 12:09:43 +000078 return node;
79
80 return NULL;
Miklos Szeredia181e612001-11-06 12:03:23 +000081}
82
Miklos Szeredia13d9002004-11-02 17:32:03 +000083static struct node *get_node(struct fuse *f, nodeid_t nodeid)
Miklos Szeredia181e612001-11-06 12:03:23 +000084{
Miklos Szeredif458b8c2004-12-07 16:46:42 +000085 struct node *node = get_node_nocheck(f, nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +000086 if (node != NULL)
Miklos Szeredi97c61e92001-11-07 12:09:43 +000087 return node;
88
Miklos Szeredia13d9002004-11-02 17:32:03 +000089 fprintf(stderr, "fuse internal error: inode %lu not found\n", nodeid);
Miklos Szeredi97c61e92001-11-07 12:09:43 +000090 abort();
Miklos Szeredia181e612001-11-06 12:03:23 +000091}
92
Miklos Szeredia13d9002004-11-02 17:32:03 +000093static void hash_id(struct fuse *f, struct node *node)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000094{
Miklos Szeredia13d9002004-11-02 17:32:03 +000095 size_t hash = node->nodeid % f->id_table_size;
96 node->id_next = f->id_table[hash];
97 f->id_table[hash] = node;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000098}
99
Miklos Szeredia13d9002004-11-02 17:32:03 +0000100static void unhash_id(struct fuse *f, struct node *node)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000101{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000102 size_t hash = node->nodeid % f->id_table_size;
103 struct node **nodep = &f->id_table[hash];
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000104
Miklos Szeredia13d9002004-11-02 17:32:03 +0000105 for (; *nodep != NULL; nodep = &(*nodep)->id_next)
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000106 if (*nodep == node) {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000107 *nodep = node->id_next;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000108 return;
109 }
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000110}
111
Miklos Szeredia13d9002004-11-02 17:32:03 +0000112static nodeid_t next_id(struct fuse *f)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000113{
Miklos Szeredi76f65782004-02-19 16:55:40 +0000114 do {
115 f->ctr++;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000116 if (!f->ctr)
Miklos Szeredi76f65782004-02-19 16:55:40 +0000117 f->generation ++;
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000118 } while (f->ctr == 0 || get_node_nocheck(f, f->ctr) != NULL);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000119 return f->ctr;
120}
121
122static void free_node(struct node *node)
123{
124 free(node->name);
125 free(node);
126}
127
Miklos Szeredia13d9002004-11-02 17:32:03 +0000128static unsigned int name_hash(struct fuse *f, nodeid_t parent, const char *name)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000129{
130 unsigned int hash = *name;
131
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000132 if (hash)
133 for (name += 1; *name != '\0'; name++)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000134 hash = (hash << 5) - hash + *name;
135
136 return (hash + parent) % f->name_table_size;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000137}
138
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000139static struct node *lookup_node(struct fuse *f, nodeid_t parent,
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000140 const char *name)
141{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000142 size_t hash = name_hash(f, parent, name);
143 struct node *node;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000144
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000145 for (node = f->name_table[hash]; node != NULL; node = node->name_next)
146 if (node->parent == parent && strcmp(node->name, name) == 0)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000147 return node;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000148
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000149 return NULL;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000150}
151
Miklos Szeredia13d9002004-11-02 17:32:03 +0000152static int hash_name(struct fuse *f, struct node *node, nodeid_t parent,
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000153 const char *name)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000154{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000155 size_t hash = name_hash(f, parent, name);
Miklos Szeredia181e612001-11-06 12:03:23 +0000156 node->parent = parent;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000157 node->name = strdup(name);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000158 if (node->name == NULL)
159 return -1;
160
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000161 node->name_next = f->name_table[hash];
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000162 f->name_table[hash] = node;
163 return 0;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000164}
165
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000166static void unhash_name(struct fuse *f, struct node *node)
Miklos Szeredia181e612001-11-06 12:03:23 +0000167{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000168 if (node->name != NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000169 size_t hash = name_hash(f, node->parent, node->name);
170 struct node **nodep = &f->name_table[hash];
171
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000172 for (; *nodep != NULL; nodep = &(*nodep)->name_next)
173 if (*nodep == node) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000174 *nodep = node->name_next;
175 node->name_next = NULL;
176 free(node->name);
177 node->name = NULL;
178 node->parent = 0;
179 return;
180 }
181 fprintf(stderr, "fuse internal error: unable to unhash node: %lu\n",
Miklos Szeredia13d9002004-11-02 17:32:03 +0000182 node->nodeid);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000183 abort();
Miklos Szeredia181e612001-11-06 12:03:23 +0000184 }
185}
186
Miklos Szeredia13d9002004-11-02 17:32:03 +0000187static struct node *find_node(struct fuse *f, nodeid_t parent, char *name,
Miklos Szeredi76f65782004-02-19 16:55:40 +0000188 struct fuse_attr *attr, int version)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000189{
190 struct node *node;
Miklos Szeredia181e612001-11-06 12:03:23 +0000191 int mode = attr->mode & S_IFMT;
192 int rdev = 0;
193
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000194 if (S_ISCHR(mode) || S_ISBLK(mode))
Miklos Szeredia181e612001-11-06 12:03:23 +0000195 rdev = attr->rdev;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000196
Miklos Szeredia181e612001-11-06 12:03:23 +0000197 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000198 node = lookup_node(f, parent, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000199 if (node != NULL) {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000200 if (node->mode == mode && node->rdev == rdev &&
201 (!(f->flags & FUSE_USE_INO) || node->ino == attr->ino)) {
202 if (!(f->flags & FUSE_USE_INO))
203 attr->ino = node->nodeid;
204
Miklos Szeredia181e612001-11-06 12:03:23 +0000205 goto out;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000206 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000207
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000208 unhash_name(f, node);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000209 }
210
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000211 node = (struct node *) calloc(1, sizeof(struct node));
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000212 if (node == NULL)
Miklos Szeredic2309912004-09-21 13:40:38 +0000213 goto out_err;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000214
Miklos Szeredia13d9002004-11-02 17:32:03 +0000215 node->nodeid = next_id(f);
216 if (!(f->flags & FUSE_USE_INO))
217 attr->ino = node->nodeid;
Miklos Szeredia181e612001-11-06 12:03:23 +0000218 node->mode = mode;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000219 node->rdev = rdev;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000220 node->ino = attr->ino;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000221 node->open_count = 0;
222 node->is_hidden = 0;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000223 node->generation = f->generation;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000224 if (hash_name(f, node, parent, name) == -1) {
225 free(node);
Miklos Szeredic2309912004-09-21 13:40:38 +0000226 node = NULL;
227 goto out_err;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000228 }
Miklos Szeredia13d9002004-11-02 17:32:03 +0000229 hash_id(f, node);
Miklos Szeredia181e612001-11-06 12:03:23 +0000230
Miklos Szeredic2309912004-09-21 13:40:38 +0000231 out:
Miklos Szeredia181e612001-11-06 12:03:23 +0000232 node->version = version;
Miklos Szeredic2309912004-09-21 13:40:38 +0000233 out_err:
Miklos Szeredia181e612001-11-06 12:03:23 +0000234 pthread_mutex_unlock(&f->lock);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000235 return node;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000236}
237
Miklos Szeredia13d9002004-11-02 17:32:03 +0000238static int path_lookup(struct fuse *f, const char *path, nodeid_t *nodeidp,
239 unsigned long *inop)
Miklos Szeredi891b8742004-07-29 09:27:49 +0000240{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000241 nodeid_t nodeid;
242 unsigned long ino;
Miklos Szeredi891b8742004-07-29 09:27:49 +0000243 int err;
244 char *s;
245 char *name;
246 char *tmp = strdup(path);
247 if (!tmp)
248 return -ENOMEM;
249
250 pthread_mutex_lock(&f->lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000251 nodeid = FUSE_ROOT_ID;
252 ino = nodeid;
Miklos Szeredi891b8742004-07-29 09:27:49 +0000253 err = 0;
254 for (s = tmp; (name = strsep(&s, "/")) != NULL; ) {
255 if (name[0]) {
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000256 struct node *node = lookup_node(f, nodeid, name);
Miklos Szeredi891b8742004-07-29 09:27:49 +0000257 if (node == NULL) {
258 err = -ENOENT;
259 break;
260 }
Miklos Szeredia13d9002004-11-02 17:32:03 +0000261 nodeid = node->nodeid;
Miklos Szeredi891b8742004-07-29 09:27:49 +0000262 ino = node->ino;
263 }
264 }
265 pthread_mutex_unlock(&f->lock);
266 free(tmp);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000267 if (!err) {
268 *nodeidp = nodeid;
Miklos Szeredi891b8742004-07-29 09:27:49 +0000269 *inop = ino;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000270 }
Miklos Szeredi891b8742004-07-29 09:27:49 +0000271
272 return err;
273}
274
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000275static char *add_name(char *buf, char *s, const char *name)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000276{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000277 size_t len = strlen(name);
278 s -= len;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000279 if (s <= buf) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000280 fprintf(stderr, "fuse: path too long: ...%s\n", s + len);
281 return NULL;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000282 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000283 strncpy(s, name, len);
284 s--;
285 *s = '/';
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000286
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000287 return s;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000288}
289
Miklos Szeredia13d9002004-11-02 17:32:03 +0000290static char *get_path_name(struct fuse *f, nodeid_t nodeid, const char *name)
Miklos Szeredib483c932001-10-29 14:57:57 +0000291{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000292 char buf[FUSE_MAX_PATH];
293 char *s = buf + FUSE_MAX_PATH - 1;
294 struct node *node;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000295
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000296 *s = '\0';
Miklos Szeredia181e612001-11-06 12:03:23 +0000297
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000298 if (name != NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000299 s = add_name(buf, s, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000300 if (s == NULL)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000301 return NULL;
302 }
303
304 pthread_mutex_lock(&f->lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000305 for (node = get_node(f, nodeid); node->nodeid != FUSE_ROOT_ID;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000306 node = get_node(f, node->parent)) {
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000307 if (node->name == NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000308 s = NULL;
309 break;
310 }
311
312 s = add_name(buf, s, node->name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000313 if (s == NULL)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000314 break;
315 }
316 pthread_mutex_unlock(&f->lock);
317
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000318 if (s == NULL)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000319 return NULL;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000320 else if (*s == '\0')
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000321 return strdup("/");
322 else
323 return strdup(s);
324}
Miklos Szeredia181e612001-11-06 12:03:23 +0000325
Miklos Szeredia13d9002004-11-02 17:32:03 +0000326static char *get_path(struct fuse *f, nodeid_t nodeid)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000327{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000328 return get_path_name(f, nodeid, NULL);
Miklos Szeredib483c932001-10-29 14:57:57 +0000329}
330
Miklos Szeredia13d9002004-11-02 17:32:03 +0000331static void destroy_node(struct fuse *f, nodeid_t nodeid, int version)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000332{
Miklos Szeredia181e612001-11-06 12:03:23 +0000333 struct node *node;
334
335 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000336 node = get_node_nocheck(f, nodeid);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000337 if (node && node->version == version && nodeid != FUSE_ROOT_ID) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000338 unhash_name(f, node);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000339 unhash_id(f, node);
Miklos Szeredia181e612001-11-06 12:03:23 +0000340 free_node(node);
341 }
342 pthread_mutex_unlock(&f->lock);
343
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000344}
345
Miklos Szeredia13d9002004-11-02 17:32:03 +0000346static void remove_node(struct fuse *f, nodeid_t dir, const char *name)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000347{
Miklos Szeredia181e612001-11-06 12:03:23 +0000348 struct node *node;
349
350 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000351 node = lookup_node(f, dir, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000352 if (node == NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000353 fprintf(stderr, "fuse internal error: unable to remove node %lu/%s\n",
354 dir, name);
355 abort();
356 }
357 unhash_name(f, node);
Miklos Szeredia181e612001-11-06 12:03:23 +0000358 pthread_mutex_unlock(&f->lock);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000359}
360
Miklos Szeredia13d9002004-11-02 17:32:03 +0000361static int rename_node(struct fuse *f, nodeid_t olddir, const char *oldname,
362 nodeid_t newdir, const char *newname, int hide)
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000363{
Miklos Szeredia181e612001-11-06 12:03:23 +0000364 struct node *node;
365 struct node *newnode;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000366 int err = 0;
Miklos Szeredia181e612001-11-06 12:03:23 +0000367
368 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000369 node = lookup_node(f, olddir, oldname);
370 newnode = lookup_node(f, newdir, newname);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000371 if (node == NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000372 fprintf(stderr, "fuse internal error: unable to rename node %lu/%s\n",
373 olddir, oldname);
374 abort();
375 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000376
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000377 if (newnode != NULL) {
378 if (hide) {
379 fprintf(stderr, "fuse: hidden file got created during hiding\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000380 err = -EBUSY;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000381 goto out;
382 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000383 unhash_name(f, newnode);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000384 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000385
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000386 unhash_name(f, node);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000387 if (hash_name(f, node, newdir, newname) == -1) {
388 err = -ENOMEM;
389 goto out;
390 }
391
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000392 if (hide)
393 node->is_hidden = 1;
394
395 out:
Miklos Szeredia181e612001-11-06 12:03:23 +0000396 pthread_mutex_unlock(&f->lock);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000397 return err;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000398}
399
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000400static void convert_stat(struct stat *stbuf, struct fuse_attr *attr)
401{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000402 attr->ino = stbuf->st_ino;
Miklos Szeredib5958612004-02-20 14:10:49 +0000403 attr->mode = stbuf->st_mode;
404 attr->nlink = stbuf->st_nlink;
405 attr->uid = stbuf->st_uid;
406 attr->gid = stbuf->st_gid;
407 attr->rdev = stbuf->st_rdev;
408 attr->size = stbuf->st_size;
409 attr->blocks = stbuf->st_blocks;
410 attr->atime = stbuf->st_atime;
Miklos Szeredib5958612004-02-20 14:10:49 +0000411 attr->mtime = stbuf->st_mtime;
Miklos Szeredib5958612004-02-20 14:10:49 +0000412 attr->ctime = stbuf->st_ctime;
Miklos Szeredicb264512004-06-23 18:52:50 +0000413#ifdef HAVE_STRUCT_STAT_ST_ATIM
414 attr->atimensec = stbuf->st_atim.tv_nsec;
415 attr->mtimensec = stbuf->st_mtim.tv_nsec;
Miklos Szeredib5958612004-02-20 14:10:49 +0000416 attr->ctimensec = stbuf->st_ctim.tv_nsec;
Miklos Szeredicb264512004-06-23 18:52:50 +0000417#endif
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000418}
419
Miklos Szeredi8fb48fe2004-11-08 14:48:52 +0000420static int fill_dir(struct fuse_dirhandle *dh, const char *name, int type,
421 ino_t ino)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000422{
423 struct fuse_dirent dirent;
424 size_t reclen;
425 size_t res;
426
Miklos Szeredi8fb48fe2004-11-08 14:48:52 +0000427 if ((dh->fuse->flags & FUSE_USE_INO))
428 dirent.ino = ino;
429 else
430 dirent.ino = (unsigned long) -1;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000431 dirent.namelen = strlen(name);
432 strncpy(dirent.name, name, sizeof(dirent.name));
433 dirent.type = type;
434 reclen = FUSE_DIRENT_SIZE(&dirent);
435 res = fwrite(&dirent, reclen, 1, dh->fp);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000436 if (res == 0) {
Miklos Szeredi96249982001-11-21 12:21:19 +0000437 perror("fuse: writing directory file");
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000438 return -EIO;
439 }
440 return 0;
441}
442
Miklos Szeredi73798f92004-07-12 15:55:11 +0000443static int send_reply_raw(struct fuse *f, char *outbuf, size_t outsize,
444 int locked)
Miklos Szeredi43696432001-11-18 19:15:05 +0000445{
446 int res;
447
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000448 if ((f->flags & FUSE_DEBUG)) {
Miklos Szeredi43696432001-11-18 19:15:05 +0000449 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
450 printf(" unique: %i, error: %i (%s), outsize: %i\n", out->unique,
451 out->error, strerror(-out->error), outsize);
452 fflush(stdout);
453 }
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000454
Miklos Szeredi4e71c9f2004-02-09 12:05:14 +0000455 /* This needs to be done before the reply, otherwise the scheduler
456 could play tricks with us, and only let the counter be increased
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000457 long after the operation is done */
Miklos Szeredi73798f92004-07-12 15:55:11 +0000458 if (!locked)
459 pthread_mutex_lock(&f->lock);
460 f->numavail ++;
461 if (!locked)
462 pthread_mutex_unlock(&f->lock);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000463
Miklos Szeredi43696432001-11-18 19:15:05 +0000464 res = write(f->fd, outbuf, outsize);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000465 if (res == -1) {
Miklos Szeredi43696432001-11-18 19:15:05 +0000466 /* ENOENT means the operation was interrupted */
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000467 if (!f->exited && errno != ENOENT)
Miklos Szeredi96249982001-11-21 12:21:19 +0000468 perror("fuse: writing device");
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000469 return -errno;
Miklos Szeredi43696432001-11-18 19:15:05 +0000470 }
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000471 return 0;
Miklos Szeredi43696432001-11-18 19:15:05 +0000472}
473
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000474static int do_send_reply(struct fuse *f, struct fuse_in_header *in, int error,
475 void *arg, size_t argsize, int locked)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000476{
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000477 int res;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000478 char *outbuf;
479 size_t outsize;
480 struct fuse_out_header *out;
481
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000482 if (error <= -1000 || error > 0) {
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000483 fprintf(stderr, "fuse: bad error value: %i\n", error);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000484 error = -ERANGE;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000485 }
486
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000487 if (error)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000488 argsize = 0;
489
490 outsize = sizeof(struct fuse_out_header) + argsize;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000491 outbuf = (char *) malloc(outsize);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000492 if (outbuf == NULL) {
493 fprintf(stderr, "fuse: failed to allocate reply buffer\n");
494 res = -ENOMEM;
495 } else {
496 out = (struct fuse_out_header *) outbuf;
497 memset(out, 0, sizeof(struct fuse_out_header));
498 out->unique = in->unique;
499 out->error = error;
500 if (argsize != 0)
501 memcpy(outbuf + sizeof(struct fuse_out_header), arg, argsize);
502
503 res = send_reply_raw(f, outbuf, outsize, locked);
504 free(outbuf);
505 }
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000506
507 return res;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000508}
509
Miklos Szeredi73798f92004-07-12 15:55:11 +0000510static int send_reply(struct fuse *f, struct fuse_in_header *in, int error,
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000511 void *arg, size_t argsize)
Miklos Szeredi73798f92004-07-12 15:55:11 +0000512{
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000513 return do_send_reply(f, in, error, arg, argsize, 0);
514}
515
516static int send_reply_locked(struct fuse *f, struct fuse_in_header *in,
517 int error, void *arg, size_t argsize)
518{
519 return do_send_reply(f, in, error, arg, argsize, 1);
Miklos Szeredi73798f92004-07-12 15:55:11 +0000520}
521
Miklos Szeredia13d9002004-11-02 17:32:03 +0000522static int is_open(struct fuse *f, nodeid_t dir, const char *name)
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000523{
524 struct node *node;
525 int isopen = 0;
526 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000527 node = lookup_node(f, dir, name);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000528 if (node && node->open_count > 0)
529 isopen = 1;
530 pthread_mutex_unlock(&f->lock);
531 return isopen;
532}
533
Miklos Szeredia13d9002004-11-02 17:32:03 +0000534static char *hidden_name(struct fuse *f, nodeid_t dir, const char *oldname,
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000535 char *newname, size_t bufsize)
536{
537 struct stat buf;
538 struct node *node;
539 struct node *newnode;
540 char *newpath;
541 int res;
542 int failctr = 10;
543
544 if (!f->op.getattr)
545 return NULL;
546
547 do {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000548 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000549 node = lookup_node(f, dir, oldname);
550 if (node == NULL) {
551 fprintf(stderr, "fuse internal error: node %lu/%s not found\n",
552 dir, oldname);
553 abort();
554 }
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000555 do {
556 f->hidectr ++;
557 snprintf(newname, bufsize, ".fuse_hidden%08x%08x",
Miklos Szeredia13d9002004-11-02 17:32:03 +0000558 (unsigned int) node->nodeid, f->hidectr);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000559 newnode = lookup_node(f, dir, newname);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000560 } while(newnode);
561 pthread_mutex_unlock(&f->lock);
562
563 newpath = get_path_name(f, dir, newname);
564 if (!newpath)
565 break;
566
567 res = f->op.getattr(newpath, &buf);
568 if (res != 0)
569 break;
570 free(newpath);
571 newpath = NULL;
572 } while(--failctr);
573
574 return newpath;
575}
576
Miklos Szeredia13d9002004-11-02 17:32:03 +0000577static int hide_node(struct fuse *f, const char *oldpath, nodeid_t dir,
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000578 const char *oldname)
579{
580 char newname[64];
581 char *newpath;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000582 int err = -EBUSY;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000583
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000584 if (f->op.rename && f->op.unlink) {
585 newpath = hidden_name(f, dir, oldname, newname, sizeof(newname));
586 if (newpath) {
587 int res = f->op.rename(oldpath, newpath);
588 if (res == 0)
589 err = rename_node(f, dir, oldname, dir, newname, 1);
590 free(newpath);
591 }
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000592 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000593 return err;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000594}
595
Miklos Szeredia13d9002004-11-02 17:32:03 +0000596static int lookup_path(struct fuse *f, nodeid_t nodeid, int version, char *name,
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000597 const char *path, struct fuse_entry_out *arg)
Miklos Szeredi76f65782004-02-19 16:55:40 +0000598{
599 int res;
600 struct stat buf;
601
602 res = f->op.getattr(path, &buf);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000603 if (res == 0) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000604 struct node *node;
605
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000606 memset(arg, 0, sizeof(struct fuse_entry_out));
Miklos Szeredi76f65782004-02-19 16:55:40 +0000607 convert_stat(&buf, &arg->attr);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000608 node = find_node(f, nodeid, name, &arg->attr, version);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000609 if (node == NULL)
610 res = -ENOMEM;
611 else {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000612 arg->nodeid = node->nodeid;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000613 arg->generation = node->generation;
614 arg->entry_valid = ENTRY_REVALIDATE_TIME;
615 arg->entry_valid_nsec = 0;
616 arg->attr_valid = ATTR_REVALIDATE_TIME;
617 arg->attr_valid_nsec = 0;
618 if (f->flags & FUSE_DEBUG) {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000619 printf(" NODEID: %li\n", arg->nodeid);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000620 fflush(stdout);
621 }
Miklos Szeredi76f65782004-02-19 16:55:40 +0000622 }
623 }
624 return res;
625}
626
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000627static void do_lookup(struct fuse *f, struct fuse_in_header *in, char *name)
628{
629 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000630 int res2;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000631 char *path;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000632 struct fuse_entry_out arg;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000633
Miklos Szeredi5e183482001-10-31 14:52:35 +0000634 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000635 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000636 if (path != NULL) {
637 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi6ebe2342002-01-06 21:44:16 +0000638 printf("LOOKUP %s\n", path);
639 fflush(stdout);
640 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000641 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000642 if (f->op.getattr)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000643 res = lookup_path(f, in->nodeid, in->unique, name, path, &arg);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000644 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000645 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000646 res2 = send_reply(f, in, res, &arg, sizeof(arg));
647 if (res == 0 && res2 == -ENOENT)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000648 destroy_node(f, arg.nodeid, in->unique);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000649}
650
Miklos Szeredia181e612001-11-06 12:03:23 +0000651static void do_forget(struct fuse *f, struct fuse_in_header *in,
652 struct fuse_forget_in *arg)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000653{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000654 if (f->flags & FUSE_DEBUG) {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000655 printf("FORGET %li/%i\n", in->nodeid, arg->version);
Miklos Szeredi43696432001-11-18 19:15:05 +0000656 fflush(stdout);
657 }
Miklos Szeredia13d9002004-11-02 17:32:03 +0000658 destroy_node(f, in->nodeid, arg->version);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000659}
660
661static void do_getattr(struct fuse *f, struct fuse_in_header *in)
662{
663 int res;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000664 char *path;
665 struct stat buf;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000666 struct fuse_attr_out arg;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000667
Miklos Szeredi5e183482001-10-31 14:52:35 +0000668 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000669 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000670 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +0000671 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000672 if (f->op.getattr)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000673 res = f->op.getattr(path, &buf);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000674 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000675 }
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000676
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000677 if (res == 0) {
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000678 memset(&arg, 0, sizeof(struct fuse_attr_out));
679 arg.attr_valid = ATTR_REVALIDATE_TIME;
680 arg.attr_valid_nsec = 0;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000681 convert_stat(&buf, &arg.attr);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000682 if (!(f->flags & FUSE_USE_INO))
683 arg.attr.ino = in->nodeid;
684 else {
685 struct node *node = get_node(f, in->nodeid);
686 node->ino = arg.attr.ino;
687 }
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000688 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000689
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000690 send_reply(f, in, res, &arg, sizeof(arg));
691}
692
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000693static int do_chmod(struct fuse *f, const char *path, struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000694{
695 int res;
696
697 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000698 if (f->op.chmod)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000699 res = f->op.chmod(path, attr->mode);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000700
701 return res;
702}
703
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000704static int do_chown(struct fuse *f, const char *path, struct fuse_attr *attr,
Miklos Szeredi2e50d432001-12-20 12:17:25 +0000705 int valid)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000706{
707 int res;
708 uid_t uid = (valid & FATTR_UID) ? attr->uid : (uid_t) -1;
709 gid_t gid = (valid & FATTR_GID) ? attr->gid : (gid_t) -1;
710
711 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000712 if (f->op.chown)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000713 res = f->op.chown(path, uid, gid);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000714
715 return res;
716}
717
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000718static int do_truncate(struct fuse *f, const char *path,
719 struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000720{
721 int res;
722
723 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000724 if (f->op.truncate)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000725 res = f->op.truncate(path, attr->size);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000726
727 return res;
728}
729
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000730static int do_utime(struct fuse *f, const char *path, struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000731{
732 int res;
733 struct utimbuf buf;
734 buf.actime = attr->atime;
735 buf.modtime = attr->mtime;
736 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000737 if (f->op.utime)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000738 res = f->op.utime(path, &buf);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000739
740 return res;
741}
742
Miklos Szeredi5e183482001-10-31 14:52:35 +0000743static void do_setattr(struct fuse *f, struct fuse_in_header *in,
744 struct fuse_setattr_in *arg)
745{
746 int res;
747 char *path;
748 int valid = arg->valid;
749 struct fuse_attr *attr = &arg->attr;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000750 struct fuse_attr_out outarg;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000751
752 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000753 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000754 if (path != NULL) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000755 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000756 if (f->op.getattr) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000757 res = 0;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000758 if (!res && (valid & FATTR_MODE))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000759 res = do_chmod(f, path, attr);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000760 if (!res && (valid & (FATTR_UID | FATTR_GID)))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000761 res = do_chown(f, path, attr, valid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000762 if (!res && (valid & FATTR_SIZE))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000763 res = do_truncate(f, path, attr);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000764 if (!res && (valid & (FATTR_ATIME | FATTR_MTIME)) ==
Miklos Szeredib5958612004-02-20 14:10:49 +0000765 (FATTR_ATIME | FATTR_MTIME))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000766 res = do_utime(f, path, attr);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000767 if (!res) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000768 struct stat buf;
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000769 res = f->op.getattr(path, &buf);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000770 if (!res) {
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000771 memset(&outarg, 0, sizeof(struct fuse_attr_out));
772 outarg.attr_valid = ATTR_REVALIDATE_TIME;
773 outarg.attr_valid_nsec = 0;
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000774 convert_stat(&buf, &outarg.attr);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000775 if (!(f->flags & FUSE_USE_INO))
776 outarg.attr.ino = in->nodeid;
777 else {
778 struct node *node = get_node(f, in->nodeid);
779 node->ino = outarg.attr.ino;
780 }
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000781 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000782 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000783 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000784 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000785 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000786 send_reply(f, in, res, &outarg, sizeof(outarg));
Miklos Szeredi5e183482001-10-31 14:52:35 +0000787}
788
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000789static void do_readlink(struct fuse *f, struct fuse_in_header *in)
790{
791 int res;
792 char link[PATH_MAX + 1];
Miklos Szeredib483c932001-10-29 14:57:57 +0000793 char *path;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000794
Miklos Szeredi5e183482001-10-31 14:52:35 +0000795 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000796 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000797 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +0000798 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000799 if (f->op.readlink)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000800 res = f->op.readlink(path, link, sizeof(link));
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000801 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000802 }
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000803 link[PATH_MAX] = '\0';
Miklos Szeredi4b2bef42002-01-09 12:23:27 +0000804 send_reply(f, in, res, link, res == 0 ? strlen(link) : 0);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000805}
806
807static void do_getdir(struct fuse *f, struct fuse_in_header *in)
808{
809 int res;
810 struct fuse_getdir_out arg;
Miklos Szeredia181e612001-11-06 12:03:23 +0000811 struct fuse_dirhandle dh;
Miklos Szeredib483c932001-10-29 14:57:57 +0000812 char *path;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000813
Miklos Szeredib483c932001-10-29 14:57:57 +0000814 dh.fuse = f;
815 dh.fp = tmpfile();
Miklos Szeredia13d9002004-11-02 17:32:03 +0000816 dh.dir = in->nodeid;
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000817
Miklos Szeredi65afea12004-09-14 07:13:45 +0000818 res = -EIO;
819 if (dh.fp == NULL)
820 perror("fuse: failed to create temporary file");
821 else {
822 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000823 path = get_path(f, in->nodeid);
Miklos Szeredi65afea12004-09-14 07:13:45 +0000824 if (path != NULL) {
825 res = -ENOSYS;
826 if (f->op.getdir)
Miklos Szeredi8fb48fe2004-11-08 14:48:52 +0000827 res = f->op.getdir(path, &dh, fill_dir);
Miklos Szeredi65afea12004-09-14 07:13:45 +0000828 free(path);
829 }
830 fflush(dh.fp);
831 }
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000832 memset(&arg, 0, sizeof(struct fuse_getdir_out));
Miklos Szeredi65afea12004-09-14 07:13:45 +0000833 if (res == 0)
834 arg.fd = fileno(dh.fp);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000835 send_reply(f, in, res, &arg, sizeof(arg));
Miklos Szeredi65afea12004-09-14 07:13:45 +0000836 if (dh.fp != NULL)
837 fclose(dh.fp);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000838}
839
Miklos Szeredib483c932001-10-29 14:57:57 +0000840static void do_mknod(struct fuse *f, struct fuse_in_header *in,
841 struct fuse_mknod_in *inarg)
842{
843 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000844 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000845 char *path;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000846 char *name = PARAM(inarg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000847 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000848
Miklos Szeredi5e183482001-10-31 14:52:35 +0000849 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000850 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000851 if (path != NULL) {
852 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000853 printf("MKNOD %s\n", path);
854 fflush(stdout);
855 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000856 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000857 if (f->op.mknod && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000858 res = f->op.mknod(path, inarg->mode, inarg->rdev);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000859 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000860 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000861 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000862 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000863 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000864 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
865 if (res == 0 && res2 == -ENOENT)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000866 destroy_node(f, outarg.nodeid, in->unique);
Miklos Szeredib483c932001-10-29 14:57:57 +0000867}
868
869static void do_mkdir(struct fuse *f, struct fuse_in_header *in,
870 struct fuse_mkdir_in *inarg)
871{
872 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000873 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000874 char *path;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000875 char *name = PARAM(inarg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000876 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000877
Miklos Szeredi5e183482001-10-31 14:52:35 +0000878 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000879 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000880 if (path != NULL) {
881 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000882 printf("MKDIR %s\n", path);
883 fflush(stdout);
884 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000885 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000886 if (f->op.mkdir && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000887 res = f->op.mkdir(path, inarg->mode);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000888 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000889 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000890 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000891 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000892 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000893 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
894 if (res == 0 && res2 == -ENOENT)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000895 destroy_node(f, outarg.nodeid, in->unique);
Miklos Szeredib483c932001-10-29 14:57:57 +0000896}
897
Miklos Szeredib5958612004-02-20 14:10:49 +0000898static void do_unlink(struct fuse *f, struct fuse_in_header *in, char *name)
Miklos Szeredib483c932001-10-29 14:57:57 +0000899{
900 int res;
901 char *path;
902
Miklos Szeredi5e183482001-10-31 14:52:35 +0000903 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000904 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000905 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000906 if (f->flags & FUSE_DEBUG) {
907 printf("UNLINK %s\n", path);
908 fflush(stdout);
909 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000910 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000911 if (f->op.unlink) {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000912 if (!(f->flags & FUSE_HARD_REMOVE) && is_open(f, in->nodeid, name))
913 res = hide_node(f, path, in->nodeid, name);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000914 else {
915 res = f->op.unlink(path);
916 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000917 remove_node(f, in->nodeid, name);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000918 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000919 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000920 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000921 }
Miklos Szeredib5958612004-02-20 14:10:49 +0000922 send_reply(f, in, res, NULL, 0);
923}
924
925static void do_rmdir(struct fuse *f, struct fuse_in_header *in, char *name)
926{
927 int res;
928 char *path;
929
930 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000931 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000932 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000933 if (f->flags & FUSE_DEBUG) {
934 printf("RMDIR %s\n", path);
935 fflush(stdout);
936 }
Miklos Szeredib5958612004-02-20 14:10:49 +0000937 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000938 if (f->op.rmdir) {
Miklos Szeredib5958612004-02-20 14:10:49 +0000939 res = f->op.rmdir(path);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000940 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000941 remove_node(f, in->nodeid, name);
Miklos Szeredib5958612004-02-20 14:10:49 +0000942 }
943 free(path);
944 }
Miklos Szeredib483c932001-10-29 14:57:57 +0000945 send_reply(f, in, res, NULL, 0);
946}
947
948static void do_symlink(struct fuse *f, struct fuse_in_header *in, char *name,
949 char *link)
950{
951 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000952 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000953 char *path;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000954 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000955
Miklos Szeredi5e183482001-10-31 14:52:35 +0000956 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000957 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000958 if (path != NULL) {
959 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000960 printf("SYMLINK %s\n", path);
961 fflush(stdout);
962 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000963 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000964 if (f->op.symlink && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000965 res = f->op.symlink(link, path);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000966 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000967 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000968 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000969 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000970 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000971 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
972 if (res == 0 && res2 == -ENOENT)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000973 destroy_node(f, outarg.nodeid, in->unique);
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000974
Miklos Szeredib483c932001-10-29 14:57:57 +0000975}
976
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000977static void do_rename(struct fuse *f, struct fuse_in_header *in,
978 struct fuse_rename_in *inarg)
979{
980 int res;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000981 nodeid_t olddir = in->nodeid;
982 nodeid_t newdir = inarg->newdir;
Miklos Szeredi6bf8b682002-10-28 08:49:39 +0000983 char *oldname = PARAM(inarg);
984 char *newname = oldname + strlen(oldname) + 1;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000985 char *oldpath;
986 char *newpath;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000987
Miklos Szeredi5e183482001-10-31 14:52:35 +0000988 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000989 oldpath = get_path_name(f, olddir, oldname);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000990 if (oldpath != NULL) {
Miklos Szeredia181e612001-11-06 12:03:23 +0000991 newpath = get_path_name(f, newdir, newname);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000992 if (newpath != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000993 if (f->flags & FUSE_DEBUG) {
994 printf("RENAME %s -> %s\n", oldpath, newpath);
995 fflush(stdout);
996 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000997 res = -ENOSYS;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000998 if (f->op.rename) {
999 res = 0;
Miklos Szeredi2529ca22004-07-13 15:36:52 +00001000 if (!(f->flags & FUSE_HARD_REMOVE) &&
1001 is_open(f, newdir, newname))
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001002 res = hide_node(f, newpath, newdir, newname);
1003 if (res == 0) {
1004 res = f->op.rename(oldpath, newpath);
1005 if (res == 0)
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001006 res = rename_node(f, olddir, oldname, newdir, newname, 0);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001007 }
1008 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001009 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001010 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001011 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001012 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001013 send_reply(f, in, res, NULL, 0);
1014}
1015
1016static void do_link(struct fuse *f, struct fuse_in_header *in,
Miklos Szeredi5e183482001-10-31 14:52:35 +00001017 struct fuse_link_in *arg)
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001018{
1019 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +00001020 int res2;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001021 char *oldpath;
1022 char *newpath;
Miklos Szeredi76f65782004-02-19 16:55:40 +00001023 char *name = PARAM(arg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +00001024 struct fuse_entry_out outarg;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001025
Miklos Szeredi5e183482001-10-31 14:52:35 +00001026 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001027 oldpath = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001028 if (oldpath != NULL) {
Miklos Szeredi76f65782004-02-19 16:55:40 +00001029 newpath = get_path_name(f, arg->newdir, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001030 if (newpath != NULL) {
1031 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +00001032 printf("LINK %s\n", newpath);
1033 fflush(stdout);
1034 }
Miklos Szeredi5e183482001-10-31 14:52:35 +00001035 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001036 if (f->op.link && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +00001037 res = f->op.link(oldpath, newpath);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001038 if (res == 0)
Miklos Szeredi76f65782004-02-19 16:55:40 +00001039 res = lookup_path(f, arg->newdir, in->unique, name,
1040 newpath, &outarg);
1041 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001042 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001043 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001044 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001045 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +00001046 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
1047 if (res == 0 && res2 == -ENOENT)
Miklos Szeredia13d9002004-11-02 17:32:03 +00001048 destroy_node(f, outarg.nodeid, in->unique);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001049}
1050
Miklos Szeredi5e183482001-10-31 14:52:35 +00001051static void do_open(struct fuse *f, struct fuse_in_header *in,
1052 struct fuse_open_in *arg)
1053{
1054 int res;
1055 char *path;
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001056 struct fuse_open_out outarg;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001057 struct fuse_file_info fi;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001058
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001059 memset(&fi, 0, sizeof(fi));
1060 fi.flags = arg->flags;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001061 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001062 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001063 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +00001064 res = -ENOSYS;
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001065 if (f->op.open.curr) {
1066 if (!f->compat)
1067 res = f->op.open.curr(path, &fi);
1068 else
1069 res = f->op.open.compat2(path, fi.flags);
1070 }
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +00001071 }
Miklos Szeredi73798f92004-07-12 15:55:11 +00001072 if (res == 0) {
1073 int res2;
1074
1075 /* If the request is interrupted the lock must be held until
1076 the cancellation is finished. Otherwise there could be
1077 races with rename/unlink, against which the kernel can't
1078 protect */
1079 pthread_mutex_lock(&f->lock);
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001080 outarg.fh = fi.fh;
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001081 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi1eea0322004-09-27 18:50:11 +00001082 printf("OPEN[%lu] flags: 0x%x\n", outarg.fh, arg->flags);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001083 fflush(stdout);
1084 }
1085
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001086 res2 = send_reply_locked(f, in, res, &outarg, sizeof(outarg));
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001087 if(res2 == -ENOENT) {
1088 /* The open syscall was interrupted, so it must be cancelled */
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001089 if(f->op.release.curr) {
1090 if (!f->compat)
1091 f->op.release.curr(path, &fi);
1092 else
1093 f->op.release.compat2(path, fi.flags);
1094 }
Miklos Szeredi73798f92004-07-12 15:55:11 +00001095 } else
Miklos Szeredia13d9002004-11-02 17:32:03 +00001096 get_node(f, in->nodeid)->open_count ++;
Miklos Szeredi73798f92004-07-12 15:55:11 +00001097 pthread_mutex_unlock(&f->lock);
1098
1099 } else
1100 send_reply(f, in, res, NULL, 0);
1101
Miklos Szeredi9a31cca2004-06-26 21:11:25 +00001102 if (path)
1103 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001104}
1105
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001106static void do_flush(struct fuse *f, struct fuse_in_header *in,
1107 struct fuse_flush_in *arg)
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001108{
1109 char *path;
1110 int res;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001111 struct fuse_file_info fi;
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001112
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001113 memset(&fi, 0, sizeof(fi));
1114 fi.fh = arg->fh;
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001115 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001116 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001117 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001118 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi1eea0322004-09-27 18:50:11 +00001119 printf("FLUSH[%lu]\n", arg->fh);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001120 fflush(stdout);
1121 }
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001122 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001123 if (f->op.flush)
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001124 res = f->op.flush(path, &fi);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001125 free(path);
1126 }
1127 send_reply(f, in, res, NULL, 0);
1128}
1129
Miklos Szeredi9478e862002-12-11 09:50:26 +00001130static void do_release(struct fuse *f, struct fuse_in_header *in,
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001131 struct fuse_release_in *arg)
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001132{
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001133 struct node *node;
1134 char *path;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001135 struct fuse_file_info fi;
1136
1137 memset(&fi, 0, sizeof(fi));
1138 fi.flags = arg->flags;
1139 fi.fh = arg->fh;
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001140
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001141 pthread_mutex_lock(&f->lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001142 node = get_node(f, in->nodeid);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001143 --node->open_count;
1144 pthread_mutex_unlock(&f->lock);
1145
Miklos Szeredia13d9002004-11-02 17:32:03 +00001146 path = get_path(f, in->nodeid);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001147 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001148 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi1eea0322004-09-27 18:50:11 +00001149 printf("RELEASE[%lu]\n", arg->fh);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001150 fflush(stdout);
1151 }
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001152 if (f->op.release.curr) {
1153 if (!f->compat)
1154 f->op.release.curr(path, &fi);
1155 else
1156 f->op.release.compat2(path, fi.flags);
1157 }
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001158
1159 if(node->is_hidden && node->open_count == 0)
1160 /* can now clean up this hidden file */
1161 f->op.unlink(path);
1162
1163 free(path);
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001164 }
Miklos Szeredi556d03d2004-06-30 11:13:41 +00001165 send_reply(f, in, 0, NULL, 0);
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001166}
1167
Miklos Szeredi5e183482001-10-31 14:52:35 +00001168static void do_read(struct fuse *f, struct fuse_in_header *in,
1169 struct fuse_read_in *arg)
1170{
1171 int res;
1172 char *path;
Miklos Szeredi43696432001-11-18 19:15:05 +00001173 char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + arg->size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001174 if (outbuf == NULL)
1175 send_reply(f, in, -ENOMEM, NULL, 0);
1176 else {
1177 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
1178 char *buf = outbuf + sizeof(struct fuse_out_header);
1179 size_t size;
1180 size_t outsize;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001181 struct fuse_file_info fi;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001182
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001183 memset(&fi, 0, sizeof(fi));
1184 fi.fh = arg->fh;
1185
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001186 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001187 path = get_path(f, in->nodeid);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001188 if (path != NULL) {
1189 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi1eea0322004-09-27 18:50:11 +00001190 printf("READ[%lu] %u bytes from %llu\n", arg->fh, arg->size,
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001191 arg->offset);
1192 fflush(stdout);
1193 }
1194
1195 res = -ENOSYS;
1196 if (f->op.read)
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001197 res = f->op.read(path, buf, arg->size, arg->offset, &fi);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001198 free(path);
Miklos Szeredi6ebe2342002-01-06 21:44:16 +00001199 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001200
1201 size = 0;
1202 if (res >= 0) {
1203 size = res;
1204 res = 0;
1205 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi1eea0322004-09-27 18:50:11 +00001206 printf(" READ[%lu] %u bytes\n", arg->fh, size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001207 fflush(stdout);
1208 }
Miklos Szeredi6ebe2342002-01-06 21:44:16 +00001209 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001210 memset(out, 0, sizeof(struct fuse_out_header));
1211 out->unique = in->unique;
1212 out->error = res;
1213 outsize = sizeof(struct fuse_out_header) + size;
1214
1215 send_reply_raw(f, outbuf, outsize, 0);
1216 free(outbuf);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001217 }
Miklos Szeredi5e183482001-10-31 14:52:35 +00001218}
Miklos Szeredib483c932001-10-29 14:57:57 +00001219
Miklos Szeredia181e612001-11-06 12:03:23 +00001220static void do_write(struct fuse *f, struct fuse_in_header *in,
1221 struct fuse_write_in *arg)
1222{
1223 int res;
1224 char *path;
Miklos Szerediad051c32004-07-02 09:22:50 +00001225 struct fuse_write_out outarg;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001226 struct fuse_file_info fi;
1227
1228 memset(&fi, 0, sizeof(fi));
1229 fi.fh = arg->fh;
Miklos Szeredid59bb9d2004-12-07 10:04:24 +00001230 fi.writepage = arg->writepage;
Miklos Szeredia181e612001-11-06 12:03:23 +00001231
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001232 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001233 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001234 if (path != NULL) {
1235 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi1eea0322004-09-27 18:50:11 +00001236 printf("WRITE%s[%lu] %u bytes to %llu\n",
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001237 arg->writepage ? "PAGE" : "", arg->fh, arg->size,
1238 arg->offset);
Miklos Szeredi6ebe2342002-01-06 21:44:16 +00001239 fflush(stdout);
1240 }
1241
Miklos Szeredia181e612001-11-06 12:03:23 +00001242 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001243 if (f->op.write)
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001244 res = f->op.write(path, PARAM(arg), arg->size, arg->offset, &fi);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001245 free(path);
Miklos Szeredia181e612001-11-06 12:03:23 +00001246 }
1247
Miklos Szerediad051c32004-07-02 09:22:50 +00001248 if (res >= 0) {
1249 outarg.size = res;
1250 res = 0;
Miklos Szeredia181e612001-11-06 12:03:23 +00001251 }
1252
Miklos Szerediad051c32004-07-02 09:22:50 +00001253 send_reply(f, in, res, &outarg, sizeof(outarg));
Miklos Szeredia181e612001-11-06 12:03:23 +00001254}
1255
Miklos Szeredi77f39942004-03-25 11:17:52 +00001256static int default_statfs(struct statfs *buf)
1257{
1258 buf->f_namelen = 255;
1259 buf->f_bsize = 512;
1260 return 0;
1261}
1262
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001263static void convert_statfs_compat(struct fuse_statfs_compat1 *compatbuf,
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001264 struct statfs *statfs)
1265{
1266 statfs->f_bsize = compatbuf->block_size;
1267 statfs->f_blocks = compatbuf->blocks;
1268 statfs->f_bfree = compatbuf->blocks_free;
1269 statfs->f_bavail = compatbuf->blocks_free;
1270 statfs->f_files = compatbuf->files;
1271 statfs->f_ffree = compatbuf->files_free;
1272 statfs->f_namelen = compatbuf->namelen;
1273}
1274
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001275static void convert_statfs(struct statfs *statfs, struct fuse_kstatfs *kstatfs)
1276{
1277 kstatfs->bsize = statfs->f_bsize;
1278 kstatfs->blocks = statfs->f_blocks;
1279 kstatfs->bfree = statfs->f_bfree;
1280 kstatfs->bavail = statfs->f_bavail;
1281 kstatfs->files = statfs->f_files;
1282 kstatfs->ffree = statfs->f_ffree;
1283 kstatfs->namelen = statfs->f_namelen;
1284}
1285
Mark Glinesd84b39a2002-01-07 16:32:02 +00001286static void do_statfs(struct fuse *f, struct fuse_in_header *in)
1287{
1288 int res;
Mark Glinesd84b39a2002-01-07 16:32:02 +00001289 struct fuse_statfs_out arg;
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001290 struct statfs buf;
Mark Glinesd84b39a2002-01-07 16:32:02 +00001291
Miklos Szeredi77f39942004-03-25 11:17:52 +00001292 memset(&buf, 0, sizeof(struct statfs));
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001293 if (f->op.statfs.curr) {
1294 if (!f->compat || f->compat > 11)
1295 res = f->op.statfs.curr("/", &buf);
1296 else {
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001297 struct fuse_statfs_compat1 compatbuf;
1298 memset(&compatbuf, 0, sizeof(struct fuse_statfs_compat1));
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001299 res = f->op.statfs.compat1(&compatbuf);
1300 if (res == 0)
1301 convert_statfs_compat(&compatbuf, &buf);
1302 }
1303 }
Miklos Szeredi77f39942004-03-25 11:17:52 +00001304 else
1305 res = default_statfs(&buf);
1306
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001307 if (res == 0)
Miklos Szeredi77f39942004-03-25 11:17:52 +00001308 convert_statfs(&buf, &arg.st);
Miklos Szeredi4b2bef42002-01-09 12:23:27 +00001309
Mark Glinesd84b39a2002-01-07 16:32:02 +00001310 send_reply(f, in, res, &arg, sizeof(arg));
1311}
1312
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001313static void do_fsync(struct fuse *f, struct fuse_in_header *in,
1314 struct fuse_fsync_in *inarg)
1315{
1316 int res;
1317 char *path;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001318 struct fuse_file_info fi;
1319
1320 memset(&fi, 0, sizeof(fi));
1321 fi.fh = inarg->fh;
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001322
1323 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001324 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001325 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001326 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi1eea0322004-09-27 18:50:11 +00001327 printf("FSYNC[%lu]\n", inarg->fh);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001328 fflush(stdout);
1329 }
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001330 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001331 if (f->op.fsync)
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001332 res = f->op.fsync(path, inarg->datasync, &fi);
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001333 free(path);
1334 }
1335 send_reply(f, in, res, NULL, 0);
1336}
1337
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001338static void do_setxattr(struct fuse *f, struct fuse_in_header *in,
1339 struct fuse_setxattr_in *arg)
1340{
1341 int res;
1342 char *path;
1343 char *name = PARAM(arg);
1344 unsigned char *value = name + strlen(name) + 1;
1345
1346 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001347 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001348 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001349 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001350 if (f->op.setxattr)
1351 res = f->op.setxattr(path, name, value, arg->size, arg->flags);
1352 free(path);
1353 }
1354 send_reply(f, in, res, NULL, 0);
1355}
1356
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001357static int common_getxattr(struct fuse *f, struct fuse_in_header *in,
1358 const char *name, char *value, size_t size)
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001359{
1360 int res;
1361 char *path;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001362
1363 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001364 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001365 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001366 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001367 if (f->op.getxattr)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001368 res = f->op.getxattr(path, name, value, size);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001369 free(path);
1370 }
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001371 return res;
1372}
1373
1374static void do_getxattr_read(struct fuse *f, struct fuse_in_header *in,
1375 const char *name, size_t size)
1376{
1377 int res;
1378 char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001379 if (outbuf == NULL)
1380 send_reply(f, in, -ENOMEM, NULL, 0);
1381 else {
1382 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
1383 char *value = outbuf + sizeof(struct fuse_out_header);
1384
1385 res = common_getxattr(f, in, name, value, size);
1386 size = 0;
1387 if (res > 0) {
1388 size = res;
1389 res = 0;
1390 }
1391 memset(out, 0, sizeof(struct fuse_out_header));
1392 out->unique = in->unique;
1393 out->error = res;
1394
1395 send_reply_raw(f, outbuf, sizeof(struct fuse_out_header) + size, 0);
1396 free(outbuf);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001397 }
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001398}
1399
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001400static void do_getxattr_size(struct fuse *f, struct fuse_in_header *in,
1401 const char *name)
1402{
1403 int res;
1404 struct fuse_getxattr_out arg;
1405
1406 res = common_getxattr(f, in, name, NULL, 0);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001407 if (res >= 0) {
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001408 arg.size = res;
1409 res = 0;
1410 }
1411 send_reply(f, in, res, &arg, sizeof(arg));
1412}
1413
1414static void do_getxattr(struct fuse *f, struct fuse_in_header *in,
1415 struct fuse_getxattr_in *arg)
1416{
1417 char *name = PARAM(arg);
1418
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001419 if (arg->size)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001420 do_getxattr_read(f, in, name, arg->size);
1421 else
1422 do_getxattr_size(f, in, name);
1423}
1424
1425static int common_listxattr(struct fuse *f, struct fuse_in_header *in,
1426 char *list, size_t size)
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001427{
1428 int res;
1429 char *path;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001430
1431 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001432 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001433 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001434 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001435 if (f->op.listxattr)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001436 res = f->op.listxattr(path, list, size);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001437 free(path);
1438 }
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001439 return res;
1440}
1441
1442static void do_listxattr_read(struct fuse *f, struct fuse_in_header *in,
1443 size_t size)
1444{
1445 int res;
1446 char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001447 if (outbuf == NULL)
1448 send_reply(f, in, -ENOMEM, NULL, 0);
1449 else {
1450 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
1451 char *list = outbuf + sizeof(struct fuse_out_header);
1452
1453 res = common_listxattr(f, in, list, size);
1454 size = 0;
1455 if (res > 0) {
1456 size = res;
1457 res = 0;
1458 }
1459 memset(out, 0, sizeof(struct fuse_out_header));
1460 out->unique = in->unique;
1461 out->error = res;
1462
1463 send_reply_raw(f, outbuf, sizeof(struct fuse_out_header) + size, 0);
1464 free(outbuf);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001465 }
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001466}
1467
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001468static void do_listxattr_size(struct fuse *f, struct fuse_in_header *in)
1469{
1470 int res;
1471 struct fuse_getxattr_out arg;
1472
1473 res = common_listxattr(f, in, NULL, 0);
1474 if (res >= 0) {
1475 arg.size = res;
1476 res = 0;
1477 }
1478 send_reply(f, in, res, &arg, sizeof(arg));
1479}
1480
1481static void do_listxattr(struct fuse *f, struct fuse_in_header *in,
1482 struct fuse_getxattr_in *arg)
1483{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001484 if (arg->size)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001485 do_listxattr_read(f, in, arg->size);
1486 else
1487 do_listxattr_size(f, in);
1488}
1489
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001490static void do_removexattr(struct fuse *f, struct fuse_in_header *in,
1491 char *name)
1492{
1493 int res;
1494 char *path;
1495
1496 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001497 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001498 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001499 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001500 if (f->op.removexattr)
1501 res = f->op.removexattr(path, name);
1502 free(path);
1503 }
1504 send_reply(f, in, res, NULL, 0);
1505}
1506
1507
Miklos Szeredi43696432001-11-18 19:15:05 +00001508static void free_cmd(struct fuse_cmd *cmd)
1509{
1510 free(cmd->buf);
1511 free(cmd);
1512}
1513
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001514void fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd)
Miklos Szeredia181e612001-11-06 12:03:23 +00001515{
Miklos Szeredia181e612001-11-06 12:03:23 +00001516 struct fuse_in_header *in = (struct fuse_in_header *) cmd->buf;
1517 void *inarg = cmd->buf + sizeof(struct fuse_in_header);
1518 size_t argsize;
Miklos Szeredid169f312004-09-22 08:48:26 +00001519 struct fuse_context *ctx = fuse_get_context();
Miklos Szeredia181e612001-11-06 12:03:23 +00001520
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001521 dec_avail(f);
Miklos Szeredi33232032001-11-19 17:55:51 +00001522
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001523 if ((f->flags & FUSE_DEBUG)) {
Miklos Szeredia13d9002004-11-02 17:32:03 +00001524 printf("unique: %i, opcode: %s (%i), nodeid: %li, insize: %i\n",
1525 in->unique, opname(in->opcode), in->opcode, in->nodeid,
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001526 cmd->buflen);
Miklos Szeredic0938ea2001-11-07 12:35:06 +00001527 fflush(stdout);
1528 }
Miklos Szeredife25def2001-12-20 15:38:05 +00001529
Miklos Szeredid169f312004-09-22 08:48:26 +00001530 ctx->fuse = f;
Miklos Szeredife25def2001-12-20 15:38:05 +00001531 ctx->uid = in->uid;
1532 ctx->gid = in->gid;
Miklos Szeredi1f18db52004-09-27 06:54:49 +00001533 ctx->pid = in->pid;
Miklos Szeredia181e612001-11-06 12:03:23 +00001534
1535 argsize = cmd->buflen - sizeof(struct fuse_in_header);
1536
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001537 switch (in->opcode) {
Miklos Szeredia181e612001-11-06 12:03:23 +00001538 case FUSE_LOOKUP:
1539 do_lookup(f, in, (char *) inarg);
1540 break;
1541
Miklos Szeredia181e612001-11-06 12:03:23 +00001542 case FUSE_GETATTR:
1543 do_getattr(f, in);
1544 break;
1545
1546 case FUSE_SETATTR:
1547 do_setattr(f, in, (struct fuse_setattr_in *) inarg);
1548 break;
1549
1550 case FUSE_READLINK:
1551 do_readlink(f, in);
1552 break;
1553
1554 case FUSE_GETDIR:
1555 do_getdir(f, in);
1556 break;
1557
1558 case FUSE_MKNOD:
1559 do_mknod(f, in, (struct fuse_mknod_in *) inarg);
1560 break;
1561
1562 case FUSE_MKDIR:
1563 do_mkdir(f, in, (struct fuse_mkdir_in *) inarg);
1564 break;
1565
1566 case FUSE_UNLINK:
Miklos Szeredib5958612004-02-20 14:10:49 +00001567 do_unlink(f, in, (char *) inarg);
1568 break;
1569
Miklos Szeredia181e612001-11-06 12:03:23 +00001570 case FUSE_RMDIR:
Miklos Szeredib5958612004-02-20 14:10:49 +00001571 do_rmdir(f, in, (char *) inarg);
Miklos Szeredia181e612001-11-06 12:03:23 +00001572 break;
1573
1574 case FUSE_SYMLINK:
1575 do_symlink(f, in, (char *) inarg,
1576 ((char *) inarg) + strlen((char *) inarg) + 1);
1577 break;
1578
1579 case FUSE_RENAME:
1580 do_rename(f, in, (struct fuse_rename_in *) inarg);
1581 break;
1582
1583 case FUSE_LINK:
1584 do_link(f, in, (struct fuse_link_in *) inarg);
1585 break;
1586
1587 case FUSE_OPEN:
1588 do_open(f, in, (struct fuse_open_in *) inarg);
1589 break;
1590
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001591 case FUSE_FLUSH:
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001592 do_flush(f, in, (struct fuse_flush_in *) inarg);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001593 break;
1594
Miklos Szeredi9478e862002-12-11 09:50:26 +00001595 case FUSE_RELEASE:
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001596 do_release(f, in, (struct fuse_release_in *) inarg);
Miklos Szeredi9478e862002-12-11 09:50:26 +00001597 break;
1598
Miklos Szeredia181e612001-11-06 12:03:23 +00001599 case FUSE_READ:
1600 do_read(f, in, (struct fuse_read_in *) inarg);
1601 break;
1602
1603 case FUSE_WRITE:
1604 do_write(f, in, (struct fuse_write_in *) inarg);
1605 break;
1606
Mark Glinesd84b39a2002-01-07 16:32:02 +00001607 case FUSE_STATFS:
1608 do_statfs(f, in);
1609 break;
1610
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001611 case FUSE_FSYNC:
1612 do_fsync(f, in, (struct fuse_fsync_in *) inarg);
1613 break;
1614
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001615 case FUSE_SETXATTR:
1616 do_setxattr(f, in, (struct fuse_setxattr_in *) inarg);
1617 break;
1618
1619 case FUSE_GETXATTR:
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001620 do_getxattr(f, in, (struct fuse_getxattr_in *) inarg);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001621 break;
1622
1623 case FUSE_LISTXATTR:
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001624 do_listxattr(f, in, (struct fuse_getxattr_in *) inarg);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001625 break;
1626
1627 case FUSE_REMOVEXATTR:
1628 do_removexattr(f, in, (char *) inarg);
1629 break;
1630
Miklos Szeredia181e612001-11-06 12:03:23 +00001631 default:
Miklos Szeredi43696432001-11-18 19:15:05 +00001632 send_reply(f, in, -ENOSYS, NULL, 0);
Miklos Szeredia181e612001-11-06 12:03:23 +00001633 }
Miklos Szeredi43696432001-11-18 19:15:05 +00001634
1635 free_cmd(cmd);
Miklos Szeredia181e612001-11-06 12:03:23 +00001636}
1637
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001638int fuse_exited(struct fuse* f)
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001639{
1640 return f->exited;
1641}
1642
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001643struct fuse_cmd *fuse_read_cmd(struct fuse *f)
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001644{
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001645 ssize_t res;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001646 struct fuse_cmd *cmd;
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001647 struct fuse_in_header *in;
1648 void *inarg;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001649
Miklos Szeredi43696432001-11-18 19:15:05 +00001650 cmd = (struct fuse_cmd *) malloc(sizeof(struct fuse_cmd));
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001651 if (cmd == NULL) {
1652 fprintf(stderr, "fuse: failed to allocate cmd in read\n");
1653 return NULL;
1654 }
Miklos Szeredi43696432001-11-18 19:15:05 +00001655 cmd->buf = (char *) malloc(FUSE_MAX_IN);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001656 if (cmd->buf == NULL) {
1657 fprintf(stderr, "fuse: failed to allocate read buffer\n");
1658 free(cmd);
1659 return NULL;
1660 }
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001661 in = (struct fuse_in_header *) cmd->buf;
1662 inarg = cmd->buf + sizeof(struct fuse_in_header);
Miklos Szeredi43696432001-11-18 19:15:05 +00001663
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001664 res = read(f->fd, cmd->buf, FUSE_MAX_IN);
1665 if (res == -1) {
1666 free_cmd(cmd);
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001667 if (fuse_exited(f) || errno == EINTR)
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001668 return NULL;
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001669
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001670 /* ENODEV means we got unmounted, so we silenty return failure */
1671 if (errno != ENODEV) {
1672 /* BAD... This will happen again */
1673 perror("fuse: reading device");
1674 }
1675
1676 fuse_exit(f);
1677 return NULL;
1678 }
1679 if ((size_t) res < sizeof(struct fuse_in_header)) {
1680 free_cmd(cmd);
1681 /* Cannot happen */
1682 fprintf(stderr, "short read on fuse device\n");
1683 fuse_exit(f);
1684 return NULL;
1685 }
1686 cmd->buflen = res;
1687
1688 /* Forget is special, it can be done without messing with threads. */
1689 if (in->opcode == FUSE_FORGET) {
1690 do_forget(f, in, (struct fuse_forget_in *) inarg);
1691 free_cmd(cmd);
1692 return NULL;
1693 }
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001694
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001695 return cmd;
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001696}
1697
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001698int fuse_loop(struct fuse *f)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001699{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001700 if (f == NULL)
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001701 return -1;
Miklos Szeredic40748a2004-02-20 16:38:45 +00001702
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001703 while (1) {
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001704 struct fuse_cmd *cmd;
1705
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001706 if (fuse_exited(f))
Miklos Szeredi874e3c12004-11-01 23:15:20 +00001707 break;
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001708
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001709 cmd = fuse_read_cmd(f);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001710 if (cmd == NULL)
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001711 continue;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001712
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001713 fuse_process_cmd(f, cmd);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001714 }
Miklos Szeredi874e3c12004-11-01 23:15:20 +00001715 f->exited = 0;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001716 return 0;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001717}
1718
Miklos Szeredi891b8742004-07-29 09:27:49 +00001719int fuse_invalidate(struct fuse *f, const char *path)
1720{
1721 int res;
1722 int err;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001723 nodeid_t nodeid;
1724 unsigned long ino;
Miklos Szeredi891b8742004-07-29 09:27:49 +00001725 struct fuse_user_header h;
1726
Miklos Szeredia13d9002004-11-02 17:32:03 +00001727 err = path_lookup(f, path, &nodeid, &ino);
Miklos Szeredi891b8742004-07-29 09:27:49 +00001728 if (err) {
1729 if (err == -ENOENT)
1730 return 0;
1731 else
1732 return err;
1733 }
1734
1735 memset(&h, 0, sizeof(struct fuse_user_header));
1736 h.opcode = FUSE_INVALIDATE;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001737 h.nodeid = nodeid;
Miklos Szeredi891b8742004-07-29 09:27:49 +00001738 h.ino = ino;
1739
1740 if ((f->flags & FUSE_DEBUG)) {
Miklos Szeredia13d9002004-11-02 17:32:03 +00001741 printf("INVALIDATE nodeid: %li\n", nodeid);
Miklos Szeredi891b8742004-07-29 09:27:49 +00001742 fflush(stdout);
1743 }
1744
1745 res = write(f->fd, &h, sizeof(struct fuse_user_header));
1746 if (res == -1) {
1747 if (errno != ENOENT) {
1748 perror("fuse: writing device");
1749 return -errno;
1750 }
1751 }
1752 return 0;
1753}
1754
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001755void fuse_exit(struct fuse *f)
1756{
1757 f->exited = 1;
1758}
1759
Miklos Szeredid169f312004-09-22 08:48:26 +00001760struct fuse_context *fuse_get_context()
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001761{
Miklos Szeredid169f312004-09-22 08:48:26 +00001762 static struct fuse_context context;
1763 if (fuse_getcontext)
1764 return fuse_getcontext();
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001765 else
Miklos Szeredid169f312004-09-22 08:48:26 +00001766 return &context;
1767}
1768
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001769void fuse_set_getcontext_func(struct fuse_context *(*func)(void))
Miklos Szeredid169f312004-09-22 08:48:26 +00001770{
1771 fuse_getcontext = func;
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001772}
1773
Miklos Szeredic40748a2004-02-20 16:38:45 +00001774static int check_version(struct fuse *f)
1775{
1776 int res;
Miklos Szeredi162bcbb2004-11-29 23:43:44 +00001777 const char *version_file = FUSE_VERSION_FILE_NEW;
Miklos Szeredif3845c42004-11-20 11:18:34 +00001778 FILE *vf = fopen(version_file, "r");
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001779 if (vf == NULL) {
Miklos Szeredia25d4c22004-11-23 22:32:16 +00001780 version_file = FUSE_VERSION_FILE_OLD;
Miklos Szeredif3845c42004-11-20 11:18:34 +00001781 vf = fopen(version_file, "r");
1782 if (vf == NULL) {
Miklos Szeredia25d4c22004-11-23 22:32:16 +00001783 struct stat tmp;
1784 if (stat(FUSE_DEV_OLD, &tmp) != -1) {
1785 fprintf(stderr, "fuse: kernel interface too old, need >= %i.%i\n",
1786 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
1787 return -1;
1788 } else {
1789 fprintf(stderr, "fuse: warning: version of kernel interface unknown\n");
1790 return 0;
1791 }
Miklos Szeredif3845c42004-11-20 11:18:34 +00001792 }
Miklos Szeredic40748a2004-02-20 16:38:45 +00001793 }
1794 res = fscanf(vf, "%i.%i", &f->majorver, &f->minorver);
1795 fclose(vf);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001796 if (res != 2) {
Miklos Szeredif3845c42004-11-20 11:18:34 +00001797 fprintf(stderr, "fuse: error reading %s\n", version_file);
Miklos Szeredic40748a2004-02-20 16:38:45 +00001798 return -1;
1799 }
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001800 if (f->majorver != FUSE_KERNEL_VERSION) {
Miklos Szeredic40748a2004-02-20 16:38:45 +00001801 fprintf(stderr, "fuse: bad kernel interface major version: needs %i\n",
1802 FUSE_KERNEL_VERSION);
1803 return -1;
1804 }
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001805 if (f->minorver < FUSE_KERNEL_MINOR_VERSION_NEED) {
Miklos Szeredi256739a2004-11-20 12:22:37 +00001806 fprintf(stderr, "fuse: kernel interface too old: need >= %i.%i\n",
Miklos Szeredic40748a2004-02-20 16:38:45 +00001807 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
1808 return -1;
1809 }
1810
1811 return 0;
1812}
1813
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001814
1815int fuse_is_lib_option(const char *opt)
1816{
1817 if (strcmp(opt, "debug") == 0 ||
Miklos Szeredia13d9002004-11-02 17:32:03 +00001818 strcmp(opt, "hard_remove") == 0 ||
1819 strcmp(opt, "use_ino") == 0)
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001820 return 1;
1821 else
1822 return 0;
1823}
1824
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001825static int parse_lib_opts(struct fuse *f, const char *opts)
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001826{
1827 if (opts) {
1828 char *xopts = strdup(opts);
1829 char *s = xopts;
1830 char *opt;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001831
1832 if (xopts == NULL)
1833 return -1;
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001834
1835 while((opt = strsep(&s, ","))) {
1836 if (strcmp(opt, "debug") == 0)
1837 f->flags |= FUSE_DEBUG;
1838 else if (strcmp(opt, "hard_remove") == 0)
1839 f->flags |= FUSE_HARD_REMOVE;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001840 else if (strcmp(opt, "use_ino") == 0)
1841 f->flags |= FUSE_USE_INO;
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001842 else
1843 fprintf(stderr, "fuse: warning: unknown option `%s'\n", opt);
1844 }
1845 free(xopts);
1846 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001847 return 0;
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001848}
1849
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001850struct fuse *fuse_new_common(int fd, const char *opts,
1851 const struct fuse_operations *op,
1852 size_t op_size, int compat)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001853{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001854 struct fuse *f;
1855 struct node *root;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001856
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001857 if (sizeof(struct fuse_operations_i) < op_size) {
1858 fprintf(stderr, "fuse: warning: library too old, some operations may not not work\n");
1859 op_size = sizeof(struct fuse_operations_i);
1860 }
1861
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001862 f = (struct fuse *) calloc(1, sizeof(struct fuse));
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001863 if (f == NULL)
1864 goto out;
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001865
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001866 if (check_version(f) == -1)
1867 goto out_free;
Miklos Szeredic40748a2004-02-20 16:38:45 +00001868
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001869 if (parse_lib_opts(f, opts) == -1)
1870 goto out_free;
1871
Miklos Szeredi8cffdb92001-11-09 14:49:18 +00001872 f->fd = fd;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001873 f->ctr = 0;
Miklos Szeredi76f65782004-02-19 16:55:40 +00001874 f->generation = 0;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001875 /* FIXME: Dynamic hash table */
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001876 f->name_table_size = 14057;
1877 f->name_table = (struct node **)
1878 calloc(1, sizeof(struct node *) * f->name_table_size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001879 if (f->name_table == NULL)
1880 goto out_free;
1881
Miklos Szeredia13d9002004-11-02 17:32:03 +00001882 f->id_table_size = 14057;
1883 f->id_table = (struct node **)
1884 calloc(1, sizeof(struct node *) * f->id_table_size);
1885 if (f->id_table == NULL)
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001886 goto out_free_name_table;
1887
Miklos Szeredia25d4c22004-11-23 22:32:16 +00001888#ifndef USE_UCLIBC
1889 pthread_mutex_init(&f->lock, NULL);
1890#else
1891 {
1892 pthread_mutexattr_t attr;
1893 pthread_mutexattr_init(&attr);
1894 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
1895 pthread_mutex_init(&f->lock, &attr);
1896 pthread_mutexattr_destroy(&attr);
1897 }
1898#endif
Miklos Szeredi33232032001-11-19 17:55:51 +00001899 f->numworker = 0;
1900 f->numavail = 0;
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001901 memcpy(&f->op, op, op_size);
1902 f->compat = compat;
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001903 f->exited = 0;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001904
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001905 root = (struct node *) calloc(1, sizeof(struct node));
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001906 if (root == NULL)
Miklos Szeredia13d9002004-11-02 17:32:03 +00001907 goto out_free_id_table;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001908
Miklos Szeredi8cffdb92001-11-09 14:49:18 +00001909 root->mode = 0;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001910 root->rdev = 0;
1911 root->name = strdup("/");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001912 if (root->name == NULL)
1913 goto out_free_root;
1914
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001915 root->parent = 0;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001916 root->nodeid = FUSE_ROOT_ID;
Miklos Szeredi76f65782004-02-19 16:55:40 +00001917 root->generation = 0;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001918 hash_id(f, root);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001919
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001920 return f;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001921
1922 out_free_root:
1923 free(root);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001924 out_free_id_table:
1925 free(f->id_table);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001926 out_free_name_table:
1927 free(f->name_table);
1928 out_free:
1929 free(f);
1930 out:
1931 fprintf(stderr, "fuse: failed to allocate fuse object\n");
1932 return NULL;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001933}
1934
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001935struct fuse *fuse_new(int fd, const char *opts,
1936 const struct fuse_operations *op, size_t op_size)
1937{
1938 return fuse_new_common(fd, opts, op, op_size, 0);
1939}
1940
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001941struct fuse *fuse_new_compat2(int fd, const char *opts,
1942 const struct fuse_operations_compat2 *op)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001943{
1944 return fuse_new_common(fd, opts, (struct fuse_operations *) op,
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001945 sizeof(struct fuse_operations_compat2), 21);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001946}
1947
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001948struct fuse *fuse_new_compat1(int fd, int flags,
1949 const struct fuse_operations_compat1 *op)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001950{
1951 char *opts = NULL;
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001952 if (flags & FUSE_DEBUG_COMPAT1)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001953 opts = "debug";
1954 return fuse_new_common(fd, opts, (struct fuse_operations *) op,
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001955 sizeof(struct fuse_operations_compat1), 11);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001956}
1957
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001958void fuse_destroy(struct fuse *f)
1959{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001960 size_t i;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001961 for (i = 0; i < f->id_table_size; i++) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001962 struct node *node;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001963
Miklos Szeredia13d9002004-11-02 17:32:03 +00001964 for (node = f->id_table[i]; node != NULL; node = node->id_next) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001965 if (node->is_hidden) {
Miklos Szeredia13d9002004-11-02 17:32:03 +00001966 char *path = get_path(f, node->nodeid);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001967 if (path)
1968 f->op.unlink(path);
1969 }
1970 }
1971 }
Miklos Szeredia13d9002004-11-02 17:32:03 +00001972 for (i = 0; i < f->id_table_size; i++) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001973 struct node *node;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001974 struct node *next;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001975
Miklos Szeredia13d9002004-11-02 17:32:03 +00001976 for (node = f->id_table[i]; node != NULL; node = next) {
1977 next = node->id_next;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001978 free_node(node);
1979 }
1980 }
Miklos Szeredia13d9002004-11-02 17:32:03 +00001981 free(f->id_table);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001982 free(f->name_table);
Miklos Szeredia181e612001-11-06 12:03:23 +00001983 pthread_mutex_destroy(&f->lock);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001984 free(f);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001985}
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001986
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001987__asm__(".symver fuse_exited,__fuse_exited@");
1988__asm__(".symver fuse_process_cmd,__fuse_process_cmd@");
1989__asm__(".symver fuse_read_cmd,__fuse_read_cmd@");
1990__asm__(".symver fuse_set_getcontext_func,__fuse_set_getcontext_func@");
1991__asm__(".symver fuse_new_compat2,fuse_new@");