blob: 82adeb274d43fac9c5bbb596cd1a69ec4141a1cd [file] [log] [blame]
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi149f6072005-01-10 12:29:28 +00003 Copyright (C) 2001-2005 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00004
Miklos Szeredi8b39a9f2002-10-25 12:41:16 +00005 This program can be distributed under the terms of the GNU LGPL.
6 See the file COPYING.LIB
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00007*/
8
Miklos Szeredicb264512004-06-23 18:52:50 +00009#include <config.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000010#include "fuse_i.h"
Miklos Szeredi0f62d722005-01-04 12:45:54 +000011#include "fuse_compat.h"
Miklos Szeredib9b94cd2004-12-01 18:56:39 +000012#include "fuse_kernel.h"
Miklos Szeredi30e093a2005-04-03 17:44:54 +000013#include "fuse_kernel_compat5.h"
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000014
Miklos Szeredi0f62d722005-01-04 12:45:54 +000015#include <stdio.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000016#include <string.h>
Miklos Szeredi97c61e92001-11-07 12:09:43 +000017#include <stdlib.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000018#include <unistd.h>
Miklos Szeredi97c61e92001-11-07 12:09:43 +000019#include <limits.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000020#include <errno.h>
Miklos Szeredi0f62d722005-01-04 12:45:54 +000021#include <assert.h>
22#include <stdint.h>
Miklos Szeredi019b4e92001-12-26 18:08:09 +000023#include <sys/param.h>
Miklos Szerediab974562005-04-07 15:40:21 +000024#include <sys/uio.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000025
Miklos Szeredi0f62d722005-01-04 12:45:54 +000026/* FUSE flags: */
27
28/** Enable debuging output */
29#define FUSE_DEBUG (1 << 1)
30
31/** If a file is removed but it's still open, don't hide the file but
32 remove it immediately */
33#define FUSE_HARD_REMOVE (1 << 2)
34
35/** Use st_ino field in getattr instead of generating inode numbers */
36#define FUSE_USE_INO (1 << 3)
37
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +000038#define FUSE_KERNEL_MINOR_VERSION_NEED 1
Miklos Szeredia25d4c22004-11-23 22:32:16 +000039#define FUSE_VERSION_FILE_OLD "/proc/fs/fuse/version"
Miklos Szeredi162bcbb2004-11-29 23:43:44 +000040#define FUSE_VERSION_FILE_NEW "/sys/fs/fuse/version"
Miklos Szeredia25d4c22004-11-23 22:32:16 +000041#define FUSE_DEV_OLD "/proc/fs/fuse/dev"
42
Miklos Szeredi97c61e92001-11-07 12:09:43 +000043#define FUSE_MAX_PATH 4096
Miklos Szeredi30e093a2005-04-03 17:44:54 +000044#define PARAM_T(inarg, type) (((char *)(inarg)) + sizeof(type))
45#define PARAM(inarg) PARAM_T(inarg, *(inarg))
46#define PARAM_COMPAT(f, inarg, type) \
47 ((f)->major == 5 ? PARAM_T(inarg, struct type ## _compat5) : PARAM(inarg))
48
49#define MEMBER_COMPAT(f, ptr, memb, type) \
50 ((f)->major == 5 ? &((struct type ## _compat5 *) (ptr))->memb : &ptr->memb)
51
52#define SIZEOF_COMPAT(f, type) \
53 ((f)->major == 5 ? sizeof(struct type ## _compat5) : sizeof(struct type))
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000054
Miklos Szeredi254d5ed2004-03-02 11:11:24 +000055#define ENTRY_REVALIDATE_TIME 1 /* sec */
56#define ATTR_REVALIDATE_TIME 1 /* sec */
57
Miklos Szeredi0f62d722005-01-04 12:45:54 +000058
59struct node {
60 struct node *name_next;
61 struct node *id_next;
62 nodeid_t nodeid;
63 unsigned int generation;
64 int refctr;
65 nodeid_t parent;
66 char *name;
67 uint64_t version;
68 int open_count;
69 int is_hidden;
70};
71
72struct fuse_dirhandle {
Miklos Szerediab974562005-04-07 15:40:21 +000073 pthread_mutex_t lock;
Miklos Szeredi0f62d722005-01-04 12:45:54 +000074 struct fuse *fuse;
Miklos Szeredib92d9782005-02-07 16:10:49 +000075 unsigned char *contents;
Miklos Szerediab974562005-04-07 15:40:21 +000076 int allocated;
Miklos Szeredib92d9782005-02-07 16:10:49 +000077 unsigned len;
Miklos Szerediab974562005-04-07 15:40:21 +000078 unsigned needlen;
Miklos Szeredi3ead28e2005-01-18 21:23:41 +000079 int filled;
Miklos Szerediede1f7a2005-04-01 21:08:57 +000080 unsigned long fh;
Miklos Szerediab974562005-04-07 15:40:21 +000081 int error;
Miklos Szeredi0f62d722005-01-04 12:45:54 +000082};
83
84struct fuse_cmd {
85 char *buf;
86 size_t buflen;
87};
88
89
Miklos Szeredid169f312004-09-22 08:48:26 +000090static struct fuse_context *(*fuse_getcontext)(void) = NULL;
91
Miklos Szerediab974562005-04-07 15:40:21 +000092#ifndef USE_UCLIBC
93#define mutex_init(mut) pthread_mutex_init(mut, NULL)
94#else
95static void mutex_init(pthread_mutex_t mut)
96{
97 pthread_mutexattr_t attr;
98 pthread_mutexattr_init(&attr);
99 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
100 pthread_mutex_init(mut, &attr);
101 pthread_mutexattr_destroy(&attr);
102}
103#endif
104
Miklos Szeredic8ba2372002-12-10 12:26:00 +0000105static const char *opname(enum fuse_opcode opcode)
106{
Miklos Szeredie5183742005-02-02 11:14:04 +0000107 switch (opcode) {
Miklos Szeredi3ed84232004-03-30 15:17:26 +0000108 case FUSE_LOOKUP: return "LOOKUP";
109 case FUSE_FORGET: return "FORGET";
110 case FUSE_GETATTR: return "GETATTR";
111 case FUSE_SETATTR: return "SETATTR";
112 case FUSE_READLINK: return "READLINK";
113 case FUSE_SYMLINK: return "SYMLINK";
Miklos Szeredi3ed84232004-03-30 15:17:26 +0000114 case FUSE_MKNOD: return "MKNOD";
115 case FUSE_MKDIR: return "MKDIR";
116 case FUSE_UNLINK: return "UNLINK";
117 case FUSE_RMDIR: return "RMDIR";
118 case FUSE_RENAME: return "RENAME";
119 case FUSE_LINK: return "LINK";
120 case FUSE_OPEN: return "OPEN";
121 case FUSE_READ: return "READ";
122 case FUSE_WRITE: return "WRITE";
123 case FUSE_STATFS: return "STATFS";
Miklos Szeredi99f20742004-05-19 08:01:10 +0000124 case FUSE_FLUSH: return "FLUSH";
Miklos Szeredi3ed84232004-03-30 15:17:26 +0000125 case FUSE_RELEASE: return "RELEASE";
126 case FUSE_FSYNC: return "FSYNC";
127 case FUSE_SETXATTR: return "SETXATTR";
128 case FUSE_GETXATTR: return "GETXATTR";
129 case FUSE_LISTXATTR: return "LISTXATTR";
130 case FUSE_REMOVEXATTR: return "REMOVEXATTR";
Miklos Szeredi3f0005f2005-01-04 19:24:31 +0000131 case FUSE_INIT: return "INIT";
Miklos Szeredi3ead28e2005-01-18 21:23:41 +0000132 case FUSE_OPENDIR: return "OPENDIR";
133 case FUSE_READDIR: return "READDIR";
134 case FUSE_RELEASEDIR: return "RELEASEDIR";
Miklos Szeredi4283ee72005-03-21 12:09:04 +0000135 case FUSE_FSYNCDIR: return "FSYNCDIR";
Miklos Szeredi99f20742004-05-19 08:01:10 +0000136 default: return "???";
Miklos Szeredic8ba2372002-12-10 12:26:00 +0000137 }
138}
139
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000140static inline void fuse_dec_avail(struct fuse *f)
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000141{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000142 pthread_mutex_lock(&f->worker_lock);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000143 f->numavail --;
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000144 pthread_mutex_unlock(&f->worker_lock);
145}
146
147static inline void fuse_inc_avail(struct fuse *f)
148{
149 pthread_mutex_lock(&f->worker_lock);
150 f->numavail ++;
151 pthread_mutex_unlock(&f->worker_lock);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000152}
153
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000154static struct node *get_node_nocheck(struct fuse *f, nodeid_t nodeid)
Miklos Szeredia181e612001-11-06 12:03:23 +0000155{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000156 size_t hash = nodeid % f->id_table_size;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000157 struct node *node;
158
Miklos Szeredia13d9002004-11-02 17:32:03 +0000159 for (node = f->id_table[hash]; node != NULL; node = node->id_next)
160 if (node->nodeid == nodeid)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000161 return node;
Miklos Szeredie5183742005-02-02 11:14:04 +0000162
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000163 return NULL;
Miklos Szeredia181e612001-11-06 12:03:23 +0000164}
165
Miklos Szeredia13d9002004-11-02 17:32:03 +0000166static struct node *get_node(struct fuse *f, nodeid_t nodeid)
Miklos Szeredia181e612001-11-06 12:03:23 +0000167{
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000168 struct node *node = get_node_nocheck(f, nodeid);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000169 if (!node) {
170 fprintf(stderr, "fuse internal error: node %lu not found\n",
171 nodeid);
172 abort();
173 }
174 return node;
Miklos Szeredia181e612001-11-06 12:03:23 +0000175}
176
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000177static void free_node(struct node *node)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000178{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000179 free(node->name);
180 free(node);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000181}
182
Miklos Szeredia13d9002004-11-02 17:32:03 +0000183static void unhash_id(struct fuse *f, struct node *node)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000184{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000185 size_t hash = node->nodeid % f->id_table_size;
186 struct node **nodep = &f->id_table[hash];
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000187
Miklos Szeredie5183742005-02-02 11:14:04 +0000188 for (; *nodep != NULL; nodep = &(*nodep)->id_next)
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000189 if (*nodep == node) {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000190 *nodep = node->id_next;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000191 return;
192 }
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000193}
194
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000195static void hash_id(struct fuse *f, struct node *node)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000196{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000197 size_t hash = node->nodeid % f->id_table_size;
198 node->id_next = f->id_table[hash];
Miklos Szeredie5183742005-02-02 11:14:04 +0000199 f->id_table[hash] = node;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000200}
201
Miklos Szeredia13d9002004-11-02 17:32:03 +0000202static unsigned int name_hash(struct fuse *f, nodeid_t parent, const char *name)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000203{
204 unsigned int hash = *name;
205
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000206 if (hash)
207 for (name += 1; *name != '\0'; name++)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000208 hash = (hash << 5) - hash + *name;
209
210 return (hash + parent) % f->name_table_size;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000211}
212
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000213static void unref_node(struct fuse *f, struct node *node);
214
215static void unhash_name(struct fuse *f, struct node *node)
216{
217 if (node->name) {
218 size_t hash = name_hash(f, node->parent, node->name);
219 struct node **nodep = &f->name_table[hash];
Miklos Szeredie5183742005-02-02 11:14:04 +0000220
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000221 for (; *nodep != NULL; nodep = &(*nodep)->name_next)
222 if (*nodep == node) {
223 *nodep = node->name_next;
224 node->name_next = NULL;
225 unref_node(f, get_node(f, node->parent));
226 free(node->name);
227 node->name = NULL;
228 node->parent = 0;
229 return;
230 }
231 fprintf(stderr, "fuse internal error: unable to unhash node: %lu\n",
232 node->nodeid);
233 abort();
234 }
235}
236
237static int hash_name(struct fuse *f, struct node *node, nodeid_t parent,
238 const char *name)
239{
240 size_t hash = name_hash(f, parent, name);
241 node->name = strdup(name);
242 if (node->name == NULL)
243 return -1;
244
245 get_node(f, parent)->refctr ++;
246 node->parent = parent;
247 node->name_next = f->name_table[hash];
248 f->name_table[hash] = node;
249 return 0;
250}
251
252static void delete_node(struct fuse *f, struct node *node)
253{
254 assert(!node->name);
255 unhash_id(f, node);
256 free_node(node);
257}
258
259static void unref_node(struct fuse *f, struct node *node)
260{
261 assert(node->refctr > 0);
262 node->refctr --;
263 if (!node->refctr)
264 delete_node(f, node);
265}
266
267static nodeid_t next_id(struct fuse *f)
268{
269 do {
270 f->ctr++;
271 if (!f->ctr)
272 f->generation ++;
273 } while (f->ctr == 0 || get_node_nocheck(f, f->ctr) != NULL);
274 return f->ctr;
275}
276
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000277static struct node *lookup_node(struct fuse *f, nodeid_t parent,
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000278 const char *name)
279{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000280 size_t hash = name_hash(f, parent, name);
281 struct node *node;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000282
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000283 for (node = f->name_table[hash]; node != NULL; node = node->name_next)
284 if (node->parent == parent && strcmp(node->name, name) == 0)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000285 return node;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000286
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000287 return NULL;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000288}
289
Miklos Szeredia13d9002004-11-02 17:32:03 +0000290static struct node *find_node(struct fuse *f, nodeid_t parent, char *name,
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000291 struct fuse_attr *attr, uint64_t version)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000292{
293 struct node *node;
Miklos Szeredia181e612001-11-06 12:03:23 +0000294 int mode = attr->mode & S_IFMT;
295 int rdev = 0;
Miklos Szeredie5183742005-02-02 11:14:04 +0000296
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000297 if (S_ISCHR(mode) || S_ISBLK(mode))
Miklos Szeredia181e612001-11-06 12:03:23 +0000298 rdev = attr->rdev;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000299
Miklos Szeredia181e612001-11-06 12:03:23 +0000300 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000301 node = lookup_node(f, parent, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000302 if (node != NULL) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000303 if (!(f->flags & FUSE_USE_INO))
Miklos Szeredie5183742005-02-02 11:14:04 +0000304 attr->ino = node->nodeid;
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000305 } else {
306 node = (struct node *) calloc(1, sizeof(struct node));
307 if (node == NULL)
308 goto out_err;
Miklos Szeredie5183742005-02-02 11:14:04 +0000309
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000310 node->refctr = 1;
311 node->nodeid = next_id(f);
312 if (!(f->flags & FUSE_USE_INO))
313 attr->ino = node->nodeid;
314 node->open_count = 0;
315 node->is_hidden = 0;
316 node->generation = f->generation;
317 if (hash_name(f, node, parent, name) == -1) {
318 free(node);
319 node = NULL;
320 goto out_err;
321 }
322 hash_id(f, node);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000323 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000324 node->version = version;
Miklos Szeredic2309912004-09-21 13:40:38 +0000325 out_err:
Miklos Szeredia181e612001-11-06 12:03:23 +0000326 pthread_mutex_unlock(&f->lock);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000327 return node;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000328}
329
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000330static char *add_name(char *buf, char *s, const char *name)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000331{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000332 size_t len = strlen(name);
333 s -= len;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000334 if (s <= buf) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000335 fprintf(stderr, "fuse: path too long: ...%s\n", s + len);
336 return NULL;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000337 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000338 strncpy(s, name, len);
339 s--;
340 *s = '/';
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000341
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000342 return s;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000343}
344
Miklos Szeredia13d9002004-11-02 17:32:03 +0000345static char *get_path_name(struct fuse *f, nodeid_t nodeid, const char *name)
Miklos Szeredib483c932001-10-29 14:57:57 +0000346{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000347 char buf[FUSE_MAX_PATH];
348 char *s = buf + FUSE_MAX_PATH - 1;
349 struct node *node;
Miklos Szeredie5183742005-02-02 11:14:04 +0000350
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000351 *s = '\0';
Miklos Szeredia181e612001-11-06 12:03:23 +0000352
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000353 if (name != NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000354 s = add_name(buf, s, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000355 if (s == NULL)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000356 return NULL;
357 }
358
359 pthread_mutex_lock(&f->lock);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000360 for (node = get_node(f, nodeid); node && node->nodeid != FUSE_ROOT_ID;
361 node = get_node(f, node->parent)) {
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000362 if (node->name == NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000363 s = NULL;
364 break;
365 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000366
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000367 s = add_name(buf, s, node->name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000368 if (s == NULL)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000369 break;
370 }
371 pthread_mutex_unlock(&f->lock);
372
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000373 if (node == NULL || s == NULL)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000374 return NULL;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000375 else if (*s == '\0')
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000376 return strdup("/");
377 else
378 return strdup(s);
379}
Miklos Szeredia181e612001-11-06 12:03:23 +0000380
Miklos Szeredia13d9002004-11-02 17:32:03 +0000381static char *get_path(struct fuse *f, nodeid_t nodeid)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000382{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000383 return get_path_name(f, nodeid, NULL);
Miklos Szeredib483c932001-10-29 14:57:57 +0000384}
385
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000386static void forget_node(struct fuse *f, nodeid_t nodeid, uint64_t version)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000387{
Miklos Szeredia181e612001-11-06 12:03:23 +0000388 struct node *node;
389
390 pthread_mutex_lock(&f->lock);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000391 node = get_node(f, nodeid);
392 if (node->version == version && nodeid != FUSE_ROOT_ID) {
393 node->version = 0;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000394 unhash_name(f, node);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000395 unref_node(f, node);
Miklos Szeredia181e612001-11-06 12:03:23 +0000396 }
397 pthread_mutex_unlock(&f->lock);
398
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000399}
400
Miklos Szeredia13d9002004-11-02 17:32:03 +0000401static void remove_node(struct fuse *f, nodeid_t dir, const char *name)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000402{
Miklos Szeredia181e612001-11-06 12:03:23 +0000403 struct node *node;
404
405 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000406 node = lookup_node(f, dir, name);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000407 if (node != NULL)
408 unhash_name(f, node);
Miklos Szeredia181e612001-11-06 12:03:23 +0000409 pthread_mutex_unlock(&f->lock);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000410}
411
Miklos Szeredia13d9002004-11-02 17:32:03 +0000412static int rename_node(struct fuse *f, nodeid_t olddir, const char *oldname,
413 nodeid_t newdir, const char *newname, int hide)
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000414{
Miklos Szeredia181e612001-11-06 12:03:23 +0000415 struct node *node;
416 struct node *newnode;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000417 int err = 0;
Miklos Szeredie5183742005-02-02 11:14:04 +0000418
Miklos Szeredia181e612001-11-06 12:03:23 +0000419 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000420 node = lookup_node(f, olddir, oldname);
421 newnode = lookup_node(f, newdir, newname);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000422 if (node == NULL)
423 goto out;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000424
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000425 if (newnode != NULL) {
426 if (hide) {
427 fprintf(stderr, "fuse: hidden file got created during hiding\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000428 err = -EBUSY;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000429 goto out;
430 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000431 unhash_name(f, newnode);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000432 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000433
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000434 unhash_name(f, node);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000435 if (hash_name(f, node, newdir, newname) == -1) {
436 err = -ENOMEM;
437 goto out;
438 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000439
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000440 if (hide)
441 node->is_hidden = 1;
442
443 out:
Miklos Szeredia181e612001-11-06 12:03:23 +0000444 pthread_mutex_unlock(&f->lock);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000445 return err;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000446}
447
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000448static void convert_stat(struct stat *stbuf, struct fuse_attr *attr)
449{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000450 attr->ino = stbuf->st_ino;
Miklos Szeredib5958612004-02-20 14:10:49 +0000451 attr->mode = stbuf->st_mode;
452 attr->nlink = stbuf->st_nlink;
453 attr->uid = stbuf->st_uid;
454 attr->gid = stbuf->st_gid;
455 attr->rdev = stbuf->st_rdev;
456 attr->size = stbuf->st_size;
457 attr->blocks = stbuf->st_blocks;
458 attr->atime = stbuf->st_atime;
Miklos Szeredib5958612004-02-20 14:10:49 +0000459 attr->mtime = stbuf->st_mtime;
Miklos Szeredib5958612004-02-20 14:10:49 +0000460 attr->ctime = stbuf->st_ctime;
Miklos Szeredicb264512004-06-23 18:52:50 +0000461#ifdef HAVE_STRUCT_STAT_ST_ATIM
462 attr->atimensec = stbuf->st_atim.tv_nsec;
463 attr->mtimensec = stbuf->st_mtim.tv_nsec;
Miklos Szeredib5958612004-02-20 14:10:49 +0000464 attr->ctimensec = stbuf->st_ctim.tv_nsec;
Miklos Szeredicb264512004-06-23 18:52:50 +0000465#endif
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000466}
467
Miklos Szerediab974562005-04-07 15:40:21 +0000468static size_t iov_length(const struct iovec *iov, size_t count)
Miklos Szerediede1f7a2005-04-01 21:08:57 +0000469{
Miklos Szerediab974562005-04-07 15:40:21 +0000470 size_t seg;
471 size_t ret = 0;
Miklos Szerediede1f7a2005-04-01 21:08:57 +0000472
Miklos Szerediab974562005-04-07 15:40:21 +0000473 for (seg = 0; seg < count; seg++)
474 ret += iov[seg].iov_len;
475 return ret;
Miklos Szerediede1f7a2005-04-01 21:08:57 +0000476}
477
Miklos Szerediab974562005-04-07 15:40:21 +0000478static int send_reply_raw(struct fuse *f, const struct iovec iov[],
479 size_t count)
Miklos Szeredi43696432001-11-18 19:15:05 +0000480{
481 int res;
Miklos Szerediab974562005-04-07 15:40:21 +0000482 unsigned outsize = iov_length(iov, count);
483 struct fuse_out_header *out = (struct fuse_out_header *) iov[0].iov_base;
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000484 out->len = outsize;
Miklos Szeredi43696432001-11-18 19:15:05 +0000485
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000486 if ((f->flags & FUSE_DEBUG)) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000487 printf(" unique: %llu, error: %i (%s), outsize: %i\n",
488 out->unique, out->error, strerror(-out->error), outsize);
Miklos Szeredi43696432001-11-18 19:15:05 +0000489 fflush(stdout);
490 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000491
Miklos Szeredi4e71c9f2004-02-09 12:05:14 +0000492 /* This needs to be done before the reply, otherwise the scheduler
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000493 could play tricks with us, and only let the counter be
494 increased long after the operation is done */
495 fuse_inc_avail(f);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000496
Miklos Szerediab974562005-04-07 15:40:21 +0000497 res = writev(f->fd, iov, count);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000498 if (res == -1) {
Miklos Szeredi43696432001-11-18 19:15:05 +0000499 /* ENOENT means the operation was interrupted */
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000500 if (!f->exited && errno != ENOENT)
Miklos Szeredi96249982001-11-21 12:21:19 +0000501 perror("fuse: writing device");
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000502 return -errno;
Miklos Szeredi43696432001-11-18 19:15:05 +0000503 }
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000504 return 0;
Miklos Szeredi43696432001-11-18 19:15:05 +0000505}
506
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000507static int send_reply(struct fuse *f, struct fuse_in_header *in, int error,
508 void *arg, size_t argsize)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000509{
Miklos Szerediab974562005-04-07 15:40:21 +0000510 struct fuse_out_header out;
511 struct iovec iov[2];
512 size_t count;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000513
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000514 if (error <= -1000 || error > 0) {
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000515 fprintf(stderr, "fuse: bad error value: %i\n", error);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000516 error = -ERANGE;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000517 }
518
Miklos Szerediab974562005-04-07 15:40:21 +0000519 out.unique = in->unique;
520 out.error = error;
521 count = 1;
522 iov[0].iov_base = &out;
523 iov[0].iov_len = sizeof(struct fuse_out_header);
524 if (argsize && !error) {
525 count++;
526 iov[1].iov_base = arg;
527 iov[1].iov_len = argsize;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000528 }
Miklos Szerediab974562005-04-07 15:40:21 +0000529 return send_reply_raw(f, iov, count);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000530}
531
Miklos Szeredia13d9002004-11-02 17:32:03 +0000532static int is_open(struct fuse *f, nodeid_t dir, const char *name)
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000533{
534 struct node *node;
535 int isopen = 0;
536 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000537 node = lookup_node(f, dir, name);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000538 if (node && node->open_count > 0)
539 isopen = 1;
540 pthread_mutex_unlock(&f->lock);
541 return isopen;
542}
543
Miklos Szeredia13d9002004-11-02 17:32:03 +0000544static char *hidden_name(struct fuse *f, nodeid_t dir, const char *oldname,
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000545 char *newname, size_t bufsize)
546{
547 struct stat buf;
548 struct node *node;
549 struct node *newnode;
550 char *newpath;
551 int res;
552 int failctr = 10;
553
554 if (!f->op.getattr)
555 return NULL;
556
557 do {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000558 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000559 node = lookup_node(f, dir, oldname);
560 if (node == NULL) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000561 pthread_mutex_unlock(&f->lock);
562 return NULL;
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000563 }
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000564 do {
565 f->hidectr ++;
566 snprintf(newname, bufsize, ".fuse_hidden%08x%08x",
Miklos Szeredia13d9002004-11-02 17:32:03 +0000567 (unsigned int) node->nodeid, f->hidectr);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000568 newnode = lookup_node(f, dir, newname);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000569 } while(newnode);
570 pthread_mutex_unlock(&f->lock);
Miklos Szeredie5183742005-02-02 11:14:04 +0000571
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000572 newpath = get_path_name(f, dir, newname);
573 if (!newpath)
574 break;
Miklos Szeredie5183742005-02-02 11:14:04 +0000575
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000576 res = f->op.getattr(newpath, &buf);
577 if (res != 0)
578 break;
579 free(newpath);
580 newpath = NULL;
581 } while(--failctr);
582
583 return newpath;
584}
585
Miklos Szeredia13d9002004-11-02 17:32:03 +0000586static int hide_node(struct fuse *f, const char *oldpath, nodeid_t dir,
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000587 const char *oldname)
588{
589 char newname[64];
590 char *newpath;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000591 int err = -EBUSY;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000592
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000593 if (f->op.rename && f->op.unlink) {
594 newpath = hidden_name(f, dir, oldname, newname, sizeof(newname));
595 if (newpath) {
596 int res = f->op.rename(oldpath, newpath);
597 if (res == 0)
598 err = rename_node(f, dir, oldname, dir, newname, 1);
599 free(newpath);
600 }
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000601 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000602 return err;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000603}
604
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000605static int lookup_path(struct fuse *f, nodeid_t nodeid, uint64_t version,
606 char *name, const char *path,
607 struct fuse_entry_out *arg)
Miklos Szeredi76f65782004-02-19 16:55:40 +0000608{
609 int res;
610 struct stat buf;
611
612 res = f->op.getattr(path, &buf);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000613 if (res == 0) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000614 struct node *node;
615
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000616 memset(arg, 0, sizeof(struct fuse_entry_out));
Miklos Szeredi76f65782004-02-19 16:55:40 +0000617 convert_stat(&buf, &arg->attr);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000618 node = find_node(f, nodeid, name, &arg->attr, version);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000619 if (node == NULL)
620 res = -ENOMEM;
621 else {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000622 arg->nodeid = node->nodeid;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000623 arg->generation = node->generation;
624 arg->entry_valid = ENTRY_REVALIDATE_TIME;
625 arg->entry_valid_nsec = 0;
626 arg->attr_valid = ATTR_REVALIDATE_TIME;
627 arg->attr_valid_nsec = 0;
628 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000629 printf(" NODEID: %lu\n", (unsigned long) arg->nodeid);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000630 fflush(stdout);
631 }
Miklos Szeredi76f65782004-02-19 16:55:40 +0000632 }
633 }
634 return res;
635}
636
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000637static void do_lookup(struct fuse *f, struct fuse_in_header *in, char *name)
638{
639 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000640 int res2;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000641 char *path;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000642 struct fuse_entry_out arg;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000643
Miklos Szeredi5e183482001-10-31 14:52:35 +0000644 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000645 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000646 if (path != NULL) {
647 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi6ebe2342002-01-06 21:44:16 +0000648 printf("LOOKUP %s\n", path);
649 fflush(stdout);
650 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000651 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000652 if (f->op.getattr)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000653 res = lookup_path(f, in->nodeid, in->unique, name, path, &arg);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000654 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000655 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000656 res2 = send_reply(f, in, res, &arg, sizeof(arg));
657 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000658 forget_node(f, arg.nodeid, in->unique);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000659}
660
Miklos Szeredia181e612001-11-06 12:03:23 +0000661static void do_forget(struct fuse *f, struct fuse_in_header *in,
662 struct fuse_forget_in *arg)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000663{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000664 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000665 printf("FORGET %lu/%llu\n", (unsigned long) in->nodeid,
666 arg->version);
Miklos Szeredi43696432001-11-18 19:15:05 +0000667 fflush(stdout);
668 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000669 forget_node(f, in->nodeid, arg->version);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000670}
671
672static void do_getattr(struct fuse *f, struct fuse_in_header *in)
673{
674 int res;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000675 char *path;
676 struct stat buf;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000677 struct fuse_attr_out arg;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000678
Miklos Szeredi5e183482001-10-31 14:52:35 +0000679 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000680 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000681 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +0000682 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000683 if (f->op.getattr)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000684 res = f->op.getattr(path, &buf);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000685 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000686 }
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000687
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000688 if (res == 0) {
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000689 memset(&arg, 0, sizeof(struct fuse_attr_out));
690 arg.attr_valid = ATTR_REVALIDATE_TIME;
691 arg.attr_valid_nsec = 0;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000692 convert_stat(&buf, &arg.attr);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000693 if (!(f->flags & FUSE_USE_INO))
694 arg.attr.ino = in->nodeid;
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000695 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000696
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000697 send_reply(f, in, res, &arg, sizeof(arg));
698}
699
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000700static int do_chmod(struct fuse *f, const char *path, struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000701{
702 int res;
703
704 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000705 if (f->op.chmod)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000706 res = f->op.chmod(path, attr->mode);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000707
708 return res;
Miklos Szeredie5183742005-02-02 11:14:04 +0000709}
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000710
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000711static int do_chown(struct fuse *f, const char *path, struct fuse_attr *attr,
Miklos Szeredi2e50d432001-12-20 12:17:25 +0000712 int valid)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000713{
714 int res;
715 uid_t uid = (valid & FATTR_UID) ? attr->uid : (uid_t) -1;
716 gid_t gid = (valid & FATTR_GID) ? attr->gid : (gid_t) -1;
Miklos Szeredie5183742005-02-02 11:14:04 +0000717
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000718 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000719 if (f->op.chown)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000720 res = f->op.chown(path, uid, gid);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000721
722 return res;
723}
724
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000725static int do_truncate(struct fuse *f, const char *path,
726 struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000727{
728 int res;
729
730 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000731 if (f->op.truncate)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000732 res = f->op.truncate(path, attr->size);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000733
734 return res;
735}
736
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000737static int do_utime(struct fuse *f, const char *path, struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000738{
739 int res;
740 struct utimbuf buf;
741 buf.actime = attr->atime;
742 buf.modtime = attr->mtime;
743 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000744 if (f->op.utime)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000745 res = f->op.utime(path, &buf);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000746
747 return res;
748}
749
Miklos Szeredi5e183482001-10-31 14:52:35 +0000750static void do_setattr(struct fuse *f, struct fuse_in_header *in,
751 struct fuse_setattr_in *arg)
752{
753 int res;
754 char *path;
755 int valid = arg->valid;
Miklos Szeredi30e093a2005-04-03 17:44:54 +0000756 struct fuse_attr *attr = MEMBER_COMPAT(f, arg, attr, fuse_setattr_in);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000757 struct fuse_attr_out outarg;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000758
759 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000760 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000761 if (path != NULL) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000762 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000763 if (f->op.getattr) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000764 res = 0;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000765 if (!res && (valid & FATTR_MODE))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000766 res = do_chmod(f, path, attr);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000767 if (!res && (valid & (FATTR_UID | FATTR_GID)))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000768 res = do_chown(f, path, attr, valid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000769 if (!res && (valid & FATTR_SIZE))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000770 res = do_truncate(f, path, attr);
Miklos Szeredie5183742005-02-02 11:14:04 +0000771 if (!res && (valid & (FATTR_ATIME | FATTR_MTIME)) ==
Miklos Szeredib5958612004-02-20 14:10:49 +0000772 (FATTR_ATIME | FATTR_MTIME))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000773 res = do_utime(f, path, attr);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000774 if (!res) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000775 struct stat buf;
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000776 res = f->op.getattr(path, &buf);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000777 if (!res) {
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000778 memset(&outarg, 0, sizeof(struct fuse_attr_out));
779 outarg.attr_valid = ATTR_REVALIDATE_TIME;
780 outarg.attr_valid_nsec = 0;
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000781 convert_stat(&buf, &outarg.attr);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000782 if (!(f->flags & FUSE_USE_INO))
783 outarg.attr.ino = in->nodeid;
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000784 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000785 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000786 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000787 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000788 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000789 send_reply(f, in, res, &outarg, sizeof(outarg));
Miklos Szeredi5e183482001-10-31 14:52:35 +0000790}
791
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000792static void do_readlink(struct fuse *f, struct fuse_in_header *in)
793{
794 int res;
795 char link[PATH_MAX + 1];
Miklos Szeredib483c932001-10-29 14:57:57 +0000796 char *path;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000797
Miklos Szeredi5e183482001-10-31 14:52:35 +0000798 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000799 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000800 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +0000801 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000802 if (f->op.readlink)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000803 res = f->op.readlink(path, link, sizeof(link));
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000804 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000805 }
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000806 link[PATH_MAX] = '\0';
Miklos Szeredi4b2bef42002-01-09 12:23:27 +0000807 send_reply(f, in, res, link, res == 0 ? strlen(link) : 0);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000808}
809
Miklos Szeredib483c932001-10-29 14:57:57 +0000810static void do_mknod(struct fuse *f, struct fuse_in_header *in,
811 struct fuse_mknod_in *inarg)
812{
813 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000814 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000815 char *path;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000816 char *name = PARAM(inarg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000817 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000818
Miklos Szeredi5e183482001-10-31 14:52:35 +0000819 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000820 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000821 if (path != NULL) {
822 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000823 printf("MKNOD %s\n", path);
824 fflush(stdout);
825 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000826 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000827 if (f->op.mknod && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000828 res = f->op.mknod(path, inarg->mode, inarg->rdev);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000829 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000830 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000831 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000832 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000833 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000834 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
835 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000836 forget_node(f, outarg.nodeid, in->unique);
Miklos Szeredib483c932001-10-29 14:57:57 +0000837}
838
839static void do_mkdir(struct fuse *f, struct fuse_in_header *in,
840 struct fuse_mkdir_in *inarg)
841{
842 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000843 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000844 char *path;
Miklos Szeredi30e093a2005-04-03 17:44:54 +0000845 char *name = PARAM_COMPAT(f, inarg, fuse_mkdir_in);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000846 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000847
Miklos Szeredi5e183482001-10-31 14:52:35 +0000848 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000849 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000850 if (path != NULL) {
851 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000852 printf("MKDIR %s\n", path);
853 fflush(stdout);
854 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000855 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000856 if (f->op.mkdir && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000857 res = f->op.mkdir(path, inarg->mode);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000858 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000859 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000860 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000861 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000862 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000863 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
864 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000865 forget_node(f, outarg.nodeid, in->unique);
Miklos Szeredib483c932001-10-29 14:57:57 +0000866}
867
Miklos Szeredib5958612004-02-20 14:10:49 +0000868static void do_unlink(struct fuse *f, struct fuse_in_header *in, char *name)
Miklos Szeredib483c932001-10-29 14:57:57 +0000869{
870 int res;
871 char *path;
872
Miklos Szeredi5e183482001-10-31 14:52:35 +0000873 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000874 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000875 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000876 if (f->flags & FUSE_DEBUG) {
877 printf("UNLINK %s\n", path);
878 fflush(stdout);
879 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000880 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000881 if (f->op.unlink) {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000882 if (!(f->flags & FUSE_HARD_REMOVE) && is_open(f, in->nodeid, name))
883 res = hide_node(f, path, in->nodeid, name);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000884 else {
885 res = f->op.unlink(path);
886 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000887 remove_node(f, in->nodeid, name);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000888
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000889 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000890 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000891 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000892 }
Miklos Szeredib5958612004-02-20 14:10:49 +0000893 send_reply(f, in, res, NULL, 0);
894}
895
896static void do_rmdir(struct fuse *f, struct fuse_in_header *in, char *name)
897{
898 int res;
899 char *path;
900
901 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000902 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000903 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000904 if (f->flags & FUSE_DEBUG) {
905 printf("RMDIR %s\n", path);
906 fflush(stdout);
907 }
Miklos Szeredib5958612004-02-20 14:10:49 +0000908 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000909 if (f->op.rmdir) {
Miklos Szeredib5958612004-02-20 14:10:49 +0000910 res = f->op.rmdir(path);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000911 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000912 remove_node(f, in->nodeid, name);
Miklos Szeredib5958612004-02-20 14:10:49 +0000913 }
914 free(path);
915 }
Miklos Szeredib483c932001-10-29 14:57:57 +0000916 send_reply(f, in, res, NULL, 0);
917}
918
919static void do_symlink(struct fuse *f, struct fuse_in_header *in, char *name,
920 char *link)
921{
922 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000923 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000924 char *path;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000925 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000926
Miklos Szeredi5e183482001-10-31 14:52:35 +0000927 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000928 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000929 if (path != NULL) {
930 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000931 printf("SYMLINK %s\n", path);
932 fflush(stdout);
933 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000934 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000935 if (f->op.symlink && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000936 res = f->op.symlink(link, path);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000937 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000938 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000939 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000940 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000941 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000942 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
943 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000944 forget_node(f, outarg.nodeid, in->unique);
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000945
Miklos Szeredib483c932001-10-29 14:57:57 +0000946}
947
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000948static void do_rename(struct fuse *f, struct fuse_in_header *in,
949 struct fuse_rename_in *inarg)
950{
951 int res;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000952 nodeid_t olddir = in->nodeid;
953 nodeid_t newdir = inarg->newdir;
Miklos Szeredi6bf8b682002-10-28 08:49:39 +0000954 char *oldname = PARAM(inarg);
955 char *newname = oldname + strlen(oldname) + 1;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000956 char *oldpath;
957 char *newpath;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000958
Miklos Szeredi5e183482001-10-31 14:52:35 +0000959 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000960 oldpath = get_path_name(f, olddir, oldname);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000961 if (oldpath != NULL) {
Miklos Szeredia181e612001-11-06 12:03:23 +0000962 newpath = get_path_name(f, newdir, newname);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000963 if (newpath != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000964 if (f->flags & FUSE_DEBUG) {
965 printf("RENAME %s -> %s\n", oldpath, newpath);
966 fflush(stdout);
967 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000968 res = -ENOSYS;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000969 if (f->op.rename) {
970 res = 0;
Miklos Szeredie5183742005-02-02 11:14:04 +0000971 if (!(f->flags & FUSE_HARD_REMOVE) &&
Miklos Szeredi2529ca22004-07-13 15:36:52 +0000972 is_open(f, newdir, newname))
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000973 res = hide_node(f, newpath, newdir, newname);
974 if (res == 0) {
975 res = f->op.rename(oldpath, newpath);
976 if (res == 0)
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000977 res = rename_node(f, olddir, oldname, newdir, newname, 0);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000978 }
979 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000980 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000981 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000982 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000983 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000984 send_reply(f, in, res, NULL, 0);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000985}
986
987static void do_link(struct fuse *f, struct fuse_in_header *in,
Miklos Szeredi5e183482001-10-31 14:52:35 +0000988 struct fuse_link_in *arg)
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000989{
990 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000991 int res2;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000992 char *oldpath;
993 char *newpath;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000994 char *name = PARAM(arg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000995 struct fuse_entry_out outarg;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000996
Miklos Szeredi5e183482001-10-31 14:52:35 +0000997 res = -ENOENT;
Miklos Szeredied6b5dd2005-01-26 17:07:59 +0000998 oldpath = get_path(f, arg->oldnodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000999 if (oldpath != NULL) {
Miklos Szeredied6b5dd2005-01-26 17:07:59 +00001000 newpath = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001001 if (newpath != NULL) {
1002 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +00001003 printf("LINK %s\n", newpath);
1004 fflush(stdout);
1005 }
Miklos Szeredi5e183482001-10-31 14:52:35 +00001006 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001007 if (f->op.link && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +00001008 res = f->op.link(oldpath, newpath);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001009 if (res == 0)
Miklos Szeredied6b5dd2005-01-26 17:07:59 +00001010 res = lookup_path(f, in->nodeid, in->unique, name,
Miklos Szeredi76f65782004-02-19 16:55:40 +00001011 newpath, &outarg);
1012 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001013 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001014 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001015 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001016 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +00001017 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
1018 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001019 forget_node(f, outarg.nodeid, in->unique);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001020}
1021
Miklos Szeredi5e183482001-10-31 14:52:35 +00001022static void do_open(struct fuse *f, struct fuse_in_header *in,
1023 struct fuse_open_in *arg)
1024{
1025 int res;
1026 char *path;
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001027 struct fuse_open_out outarg;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001028 struct fuse_file_info fi;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001029
Miklos Szeredied3c97c2005-02-15 17:04:50 +00001030 memset(&outarg, 0, sizeof(outarg));
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001031 memset(&fi, 0, sizeof(fi));
1032 fi.flags = arg->flags;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001033 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001034 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001035 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +00001036 res = -ENOSYS;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001037 if (f->op.open) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001038 if (!f->compat)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001039 res = f->op.open(path, &fi);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001040 else
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001041 res = ((struct fuse_operations_compat2 *) &f->op)->open(path, fi.flags);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001042 }
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +00001043 }
Miklos Szeredi73798f92004-07-12 15:55:11 +00001044 if (res == 0) {
1045 int res2;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001046 outarg.fh = fi.fh;
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001047 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001048 printf("OPEN[%lu] flags: 0x%x\n", fi.fh, arg->flags);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001049 fflush(stdout);
1050 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001051
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001052 pthread_mutex_lock(&f->lock);
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001053 res2 = send_reply(f, in, res, &outarg, SIZEOF_COMPAT(f, fuse_open_out));
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001054 if(res2 == -ENOENT) {
1055 /* The open syscall was interrupted, so it must be cancelled */
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001056 if(f->op.release) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001057 if (!f->compat)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001058 f->op.release(path, &fi);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001059 else
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001060 ((struct fuse_operations_compat2 *) &f->op)->release(path, fi.flags);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001061 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001062 } else {
1063 struct node *node = get_node(f, in->nodeid);
1064 node->open_count ++;
1065 }
Miklos Szeredi73798f92004-07-12 15:55:11 +00001066 pthread_mutex_unlock(&f->lock);
Miklos Szeredi73798f92004-07-12 15:55:11 +00001067 } else
1068 send_reply(f, in, res, NULL, 0);
1069
Miklos Szeredi9a31cca2004-06-26 21:11:25 +00001070 if (path)
1071 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001072}
1073
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001074static void do_flush(struct fuse *f, struct fuse_in_header *in,
1075 struct fuse_flush_in *arg)
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001076{
1077 char *path;
1078 int res;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001079 struct fuse_file_info fi;
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001080
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001081 memset(&fi, 0, sizeof(fi));
1082 fi.fh = arg->fh;
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001083 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001084 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001085 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001086 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001087 printf("FLUSH[%lu]\n", (unsigned long) arg->fh);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001088 fflush(stdout);
1089 }
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001090 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001091 if (f->op.flush)
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001092 res = f->op.flush(path, &fi);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001093 free(path);
1094 }
1095 send_reply(f, in, res, NULL, 0);
1096}
1097
Miklos Szeredi9478e862002-12-11 09:50:26 +00001098static void do_release(struct fuse *f, struct fuse_in_header *in,
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001099 struct fuse_release_in *arg)
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001100{
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001101 struct node *node;
1102 char *path;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001103 struct fuse_file_info fi;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001104 int unlink_hidden;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001105
1106 memset(&fi, 0, sizeof(fi));
1107 fi.flags = arg->flags;
1108 fi.fh = arg->fh;
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001109
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001110 pthread_mutex_lock(&f->lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001111 node = get_node(f, in->nodeid);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001112 assert(node->open_count > 0);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001113 --node->open_count;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001114 unlink_hidden = (node->is_hidden && !node->open_count);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001115 pthread_mutex_unlock(&f->lock);
1116
Miklos Szeredia13d9002004-11-02 17:32:03 +00001117 path = get_path(f, in->nodeid);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001118 if (f->flags & FUSE_DEBUG) {
1119 printf("RELEASE[%lu] flags: 0x%x\n", fi.fh, fi.flags);
1120 fflush(stdout);
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001121 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001122 if (f->op.release) {
1123 if (!f->compat)
1124 f->op.release(path ? path : "-", &fi);
1125 else if (path)
1126 ((struct fuse_operations_compat2 *) &f->op)->release(path, fi.flags);
1127 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001128
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001129 if(unlink_hidden && path)
1130 f->op.unlink(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001131
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001132 if (path)
1133 free(path);
1134
Miklos Szeredi556d03d2004-06-30 11:13:41 +00001135 send_reply(f, in, 0, NULL, 0);
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001136}
1137
Miklos Szeredi5e183482001-10-31 14:52:35 +00001138static void do_read(struct fuse *f, struct fuse_in_header *in,
1139 struct fuse_read_in *arg)
1140{
1141 int res;
1142 char *path;
Miklos Szerediab974562005-04-07 15:40:21 +00001143 size_t size;
1144 char *buf;
1145 struct fuse_file_info fi;
1146
1147 buf = (char *) malloc(arg->size);
1148 if (buf == NULL) {
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001149 send_reply(f, in, -ENOMEM, NULL, 0);
Miklos Szerediab974562005-04-07 15:40:21 +00001150 return;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001151 }
Miklos Szerediab974562005-04-07 15:40:21 +00001152
1153 memset(&fi, 0, sizeof(fi));
1154 fi.fh = arg->fh;
1155
1156 res = -ENOENT;
1157 path = get_path(f, in->nodeid);
1158 if (path != NULL) {
1159 if (f->flags & FUSE_DEBUG) {
1160 printf("READ[%lu] %u bytes from %llu\n",
1161 (unsigned long) arg->fh, arg->size, arg->offset);
1162 fflush(stdout);
1163 }
1164
1165 res = -ENOSYS;
1166 if (f->op.read)
1167 res = f->op.read(path, buf, arg->size, arg->offset, &fi);
1168 free(path);
1169 }
1170
1171 size = 0;
1172 if (res >= 0) {
1173 size = res;
1174 res = 0;
1175 if (f->flags & FUSE_DEBUG) {
1176 printf(" READ[%lu] %u bytes\n", (unsigned long) arg->fh,
1177 size);
1178 fflush(stdout);
1179 }
1180 }
1181
1182 send_reply(f, in, res, buf, size);
1183 free(buf);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001184}
Miklos Szeredib483c932001-10-29 14:57:57 +00001185
Miklos Szeredia181e612001-11-06 12:03:23 +00001186static void do_write(struct fuse *f, struct fuse_in_header *in,
1187 struct fuse_write_in *arg)
1188{
1189 int res;
1190 char *path;
Miklos Szerediad051c32004-07-02 09:22:50 +00001191 struct fuse_write_out outarg;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001192 struct fuse_file_info fi;
1193
1194 memset(&fi, 0, sizeof(fi));
1195 fi.fh = arg->fh;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001196 fi.writepage = arg->write_flags & 1;
Miklos Szeredia181e612001-11-06 12:03:23 +00001197
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001198 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001199 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001200 if (path != NULL) {
1201 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi1eea0322004-09-27 18:50:11 +00001202 printf("WRITE%s[%lu] %u bytes to %llu\n",
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001203 (arg->write_flags & 1) ? "PAGE" : "",
1204 (unsigned long) arg->fh, arg->size, arg->offset);
Miklos Szeredi6ebe2342002-01-06 21:44:16 +00001205 fflush(stdout);
1206 }
1207
Miklos Szeredia181e612001-11-06 12:03:23 +00001208 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001209 if (f->op.write)
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001210 res = f->op.write(path, PARAM(arg), arg->size, arg->offset, &fi);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001211 free(path);
Miklos Szeredia181e612001-11-06 12:03:23 +00001212 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001213
Miklos Szerediab974562005-04-07 15:40:21 +00001214 memset(&outarg, 0, sizeof(outarg));
Miklos Szeredie5183742005-02-02 11:14:04 +00001215 if (res >= 0) {
Miklos Szerediad051c32004-07-02 09:22:50 +00001216 outarg.size = res;
1217 res = 0;
Miklos Szeredia181e612001-11-06 12:03:23 +00001218 }
1219
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001220 send_reply(f, in, res, &outarg, SIZEOF_COMPAT(f, fuse_write_out));
Miklos Szeredia181e612001-11-06 12:03:23 +00001221}
1222
Miklos Szeredi77f39942004-03-25 11:17:52 +00001223static int default_statfs(struct statfs *buf)
1224{
1225 buf->f_namelen = 255;
1226 buf->f_bsize = 512;
1227 return 0;
1228}
1229
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001230static void convert_statfs_compat(struct fuse_statfs_compat1 *compatbuf,
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001231 struct statfs *statfs)
1232{
1233 statfs->f_bsize = compatbuf->block_size;
1234 statfs->f_blocks = compatbuf->blocks;
1235 statfs->f_bfree = compatbuf->blocks_free;
1236 statfs->f_bavail = compatbuf->blocks_free;
1237 statfs->f_files = compatbuf->files;
1238 statfs->f_ffree = compatbuf->files_free;
1239 statfs->f_namelen = compatbuf->namelen;
1240}
1241
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001242static void convert_statfs(struct statfs *statfs, struct fuse_kstatfs *kstatfs)
1243{
1244 kstatfs->bsize = statfs->f_bsize;
1245 kstatfs->blocks = statfs->f_blocks;
1246 kstatfs->bfree = statfs->f_bfree;
1247 kstatfs->bavail = statfs->f_bavail;
1248 kstatfs->files = statfs->f_files;
1249 kstatfs->ffree = statfs->f_ffree;
1250 kstatfs->namelen = statfs->f_namelen;
1251}
1252
Mark Glinesd84b39a2002-01-07 16:32:02 +00001253static void do_statfs(struct fuse *f, struct fuse_in_header *in)
1254{
1255 int res;
Mark Glinesd84b39a2002-01-07 16:32:02 +00001256 struct fuse_statfs_out arg;
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001257 struct statfs buf;
Mark Glinesd84b39a2002-01-07 16:32:02 +00001258
Miklos Szeredi77f39942004-03-25 11:17:52 +00001259 memset(&buf, 0, sizeof(struct statfs));
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001260 if (f->op.statfs) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001261 if (!f->compat || f->compat > 11)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001262 res = f->op.statfs("/", &buf);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001263 else {
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001264 struct fuse_statfs_compat1 compatbuf;
1265 memset(&compatbuf, 0, sizeof(struct fuse_statfs_compat1));
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001266 res = ((struct fuse_operations_compat1 *) &f->op)->statfs(&compatbuf);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001267 if (res == 0)
1268 convert_statfs_compat(&compatbuf, &buf);
1269 }
1270 }
Miklos Szeredi77f39942004-03-25 11:17:52 +00001271 else
1272 res = default_statfs(&buf);
1273
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001274 if (res == 0)
Miklos Szeredi77f39942004-03-25 11:17:52 +00001275 convert_statfs(&buf, &arg.st);
Miklos Szeredi4b2bef42002-01-09 12:23:27 +00001276
Mark Glinesd84b39a2002-01-07 16:32:02 +00001277 send_reply(f, in, res, &arg, sizeof(arg));
1278}
1279
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001280static void do_fsync(struct fuse *f, struct fuse_in_header *in,
1281 struct fuse_fsync_in *inarg)
1282{
1283 int res;
1284 char *path;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001285 struct fuse_file_info fi;
1286
1287 memset(&fi, 0, sizeof(fi));
1288 fi.fh = inarg->fh;
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001289
1290 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001291 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001292 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001293 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001294 printf("FSYNC[%lu]\n", (unsigned long) inarg->fh);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001295 fflush(stdout);
1296 }
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001297 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001298 if (f->op.fsync)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001299 res = f->op.fsync(path, inarg->fsync_flags & 1, &fi);
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001300 free(path);
1301 }
1302 send_reply(f, in, res, NULL, 0);
1303}
1304
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001305static void do_setxattr(struct fuse *f, struct fuse_in_header *in,
1306 struct fuse_setxattr_in *arg)
1307{
1308 int res;
1309 char *path;
1310 char *name = PARAM(arg);
1311 unsigned char *value = name + strlen(name) + 1;
1312
1313 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001314 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001315 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001316 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001317 if (f->op.setxattr)
1318 res = f->op.setxattr(path, name, value, arg->size, arg->flags);
1319 free(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001320 }
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001321 send_reply(f, in, res, NULL, 0);
1322}
1323
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001324static int common_getxattr(struct fuse *f, struct fuse_in_header *in,
1325 const char *name, char *value, size_t size)
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001326{
1327 int res;
1328 char *path;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001329
1330 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001331 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001332 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001333 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001334 if (f->op.getxattr)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001335 res = f->op.getxattr(path, name, value, size);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001336 free(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001337 }
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001338 return res;
1339}
1340
1341static void do_getxattr_read(struct fuse *f, struct fuse_in_header *in,
1342 const char *name, size_t size)
1343{
1344 int res;
Miklos Szerediab974562005-04-07 15:40:21 +00001345 char *value = (char *) malloc(size);
1346 if (value == NULL) {
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001347 send_reply(f, in, -ENOMEM, NULL, 0);
Miklos Szerediab974562005-04-07 15:40:21 +00001348 return;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001349 }
Miklos Szerediab974562005-04-07 15:40:21 +00001350 res = common_getxattr(f, in, name, value, size);
1351 size = 0;
1352 if (res > 0) {
1353 size = res;
1354 res = 0;
1355 }
1356 send_reply(f, in, res, value, size);
1357 free(value);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001358}
1359
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001360static void do_getxattr_size(struct fuse *f, struct fuse_in_header *in,
1361 const char *name)
1362{
1363 int res;
1364 struct fuse_getxattr_out arg;
1365
Miklos Szerediab974562005-04-07 15:40:21 +00001366 memset(&arg, 0, sizeof(arg));
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001367 res = common_getxattr(f, in, name, NULL, 0);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001368 if (res >= 0) {
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001369 arg.size = res;
1370 res = 0;
1371 }
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001372 send_reply(f, in, res, &arg, SIZEOF_COMPAT(f, fuse_getxattr_out));
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001373}
1374
1375static void do_getxattr(struct fuse *f, struct fuse_in_header *in,
1376 struct fuse_getxattr_in *arg)
1377{
1378 char *name = PARAM(arg);
Miklos Szeredie5183742005-02-02 11:14:04 +00001379
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001380 if (arg->size)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001381 do_getxattr_read(f, in, name, arg->size);
1382 else
1383 do_getxattr_size(f, in, name);
1384}
1385
1386static int common_listxattr(struct fuse *f, struct fuse_in_header *in,
1387 char *list, size_t size)
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001388{
1389 int res;
1390 char *path;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001391
1392 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001393 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001394 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001395 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001396 if (f->op.listxattr)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001397 res = f->op.listxattr(path, list, size);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001398 free(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001399 }
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001400 return res;
1401}
1402
1403static void do_listxattr_read(struct fuse *f, struct fuse_in_header *in,
1404 size_t size)
1405{
1406 int res;
Miklos Szerediab974562005-04-07 15:40:21 +00001407 char *list = (char *) malloc(size);
1408 if (list == NULL) {
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001409 send_reply(f, in, -ENOMEM, NULL, 0);
Miklos Szerediab974562005-04-07 15:40:21 +00001410 return;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001411 }
Miklos Szerediab974562005-04-07 15:40:21 +00001412 res = common_listxattr(f, in, list, size);
1413 size = 0;
1414 if (res > 0) {
1415 size = res;
1416 res = 0;
1417 }
1418 send_reply(f, in, res, list, size);
1419 free(list);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001420}
1421
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001422static void do_listxattr_size(struct fuse *f, struct fuse_in_header *in)
1423{
1424 int res;
1425 struct fuse_getxattr_out arg;
1426
Miklos Szerediab974562005-04-07 15:40:21 +00001427 memset(&arg, 0, sizeof(arg));
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001428 res = common_listxattr(f, in, NULL, 0);
1429 if (res >= 0) {
1430 arg.size = res;
1431 res = 0;
1432 }
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001433 send_reply(f, in, res, &arg, SIZEOF_COMPAT(f, fuse_getxattr_out));
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001434}
1435
1436static void do_listxattr(struct fuse *f, struct fuse_in_header *in,
1437 struct fuse_getxattr_in *arg)
1438{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001439 if (arg->size)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001440 do_listxattr_read(f, in, arg->size);
1441 else
1442 do_listxattr_size(f, in);
1443}
1444
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001445static void do_removexattr(struct fuse *f, struct fuse_in_header *in,
1446 char *name)
1447{
1448 int res;
1449 char *path;
1450
1451 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001452 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001453 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001454 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001455 if (f->op.removexattr)
1456 res = f->op.removexattr(path, name);
1457 free(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001458 }
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001459 send_reply(f, in, res, NULL, 0);
1460}
1461
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001462static void do_init(struct fuse *f, struct fuse_in_header *in,
1463 struct fuse_init_in_out *arg)
1464{
1465 struct fuse_init_in_out outarg;
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001466
1467 if (in->padding == 5) {
1468 arg->minor = arg->major;
1469 arg->major = in->padding;
1470 }
1471
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001472 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001473 printf("INIT: %u.%u\n", arg->major, arg->minor);
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001474 fflush(stdout);
1475 }
1476 f->got_init = 1;
Miklos Szeredi159bd7e2005-02-28 17:32:16 +00001477 if (f->op.init)
1478 f->user_data = f->op.init();
1479
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001480 if (arg->major == 5) {
1481 f->major = 5;
1482 f->minor = 1;
1483 } else {
1484 f->major = FUSE_KERNEL_VERSION;
1485 f->minor = FUSE_KERNEL_MINOR_VERSION;
1486 }
1487 memset(&outarg, 0, sizeof(outarg));
1488 outarg.major = f->major;
1489 outarg.minor = f->minor;
1490
1491 if (f->flags & FUSE_DEBUG) {
1492 printf(" INIT: %u.%u\n", outarg.major, outarg.minor);
1493 fflush(stdout);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001494 }
1495
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001496 send_reply(f, in, 0, &outarg, sizeof(outarg));
1497}
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001498
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001499static struct fuse_dirhandle *get_dirhandle(unsigned long fh)
1500{
1501 return (struct fuse_dirhandle *) fh;
1502}
1503
1504static void do_opendir(struct fuse *f, struct fuse_in_header *in,
1505 struct fuse_open_in *arg)
1506{
1507 int res;
1508 struct fuse_open_out outarg;
1509 struct fuse_dirhandle *dh;
1510
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001511 dh = (struct fuse_dirhandle *) malloc(sizeof(struct fuse_dirhandle));
1512 if (dh == NULL) {
1513 send_reply(f, in, -ENOMEM, NULL, 0);
1514 return;
1515 }
1516 memset(dh, 0, sizeof(struct fuse_dirhandle));
1517 dh->fuse = f;
1518 dh->contents = NULL;
1519 dh->len = 0;
1520 dh->filled = 0;
Miklos Szerediab974562005-04-07 15:40:21 +00001521 mutex_init(&dh->lock);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001522
1523 memset(&outarg, 0, sizeof(outarg));
1524 outarg.fh = (unsigned long) dh;
1525
Miklos Szeredif43f0632005-02-28 11:46:56 +00001526 if (f->op.opendir) {
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001527 struct fuse_file_info fi;
Miklos Szeredif43f0632005-02-28 11:46:56 +00001528 char *path;
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001529
1530 memset(&fi, 0, sizeof(fi));
1531 fi.flags = arg->flags;
1532
Miklos Szeredif43f0632005-02-28 11:46:56 +00001533 res = -ENOENT;
1534 path = get_path(f, in->nodeid);
1535 if (path != NULL) {
Miklos Szeredif43f0632005-02-28 11:46:56 +00001536 res = f->op.opendir(path, &fi);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001537 dh->fh = fi.fh;
Miklos Szeredif43f0632005-02-28 11:46:56 +00001538 }
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001539 if (res == 0) {
1540 int res2;
1541 pthread_mutex_lock(&f->lock);
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001542 res2 = send_reply(f, in, res, &outarg, SIZEOF_COMPAT(f, fuse_open_out));
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001543 if(res2 == -ENOENT) {
1544 /* The opendir syscall was interrupted, so it must be
1545 cancelled */
1546 if(f->op.releasedir)
1547 f->op.releasedir(path, &fi);
Miklos Szerediab974562005-04-07 15:40:21 +00001548 pthread_mutex_destroy(&dh->lock);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001549 free(dh);
1550 }
1551 pthread_mutex_unlock(&f->lock);
1552 } else {
Miklos Szeredif43f0632005-02-28 11:46:56 +00001553 send_reply(f, in, res, NULL, 0);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001554 free(dh);
Miklos Szeredif43f0632005-02-28 11:46:56 +00001555 }
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001556 free(path);
Miklos Szerediab974562005-04-07 15:40:21 +00001557 } else
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001558 send_reply(f, in, 0, &outarg, SIZEOF_COMPAT(f, fuse_open_out));
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001559}
1560
Miklos Szerediab974562005-04-07 15:40:21 +00001561static int fill_dir_common(struct fuse_dirhandle *dh, const char *name,
1562 int type, ino_t ino, off_t off)
1563{
1564 unsigned namelen = strlen(name);
1565 unsigned entlen;
1566 unsigned entsize;
1567 unsigned padlen;
1568 unsigned newlen;
1569 unsigned char *newptr;
1570
1571 if (!(dh->fuse->flags & FUSE_USE_INO))
1572 ino = (ino_t) -1;
1573
1574 if (namelen > FUSE_NAME_MAX)
1575 namelen = FUSE_NAME_MAX;
1576 else if (!namelen) {
1577 dh->error = -EIO;
1578 return 1;
1579 }
1580
1581 entlen = (dh->fuse->major == 5 ?
1582 FUSE_NAME_OFFSET_COMPAT5 : FUSE_NAME_OFFSET) + namelen;
1583 entsize = FUSE_DIRENT_ALIGN(entlen);
1584 padlen = entsize - entlen;
1585 newlen = dh->len + entsize;
1586 if (off && dh->fuse->major != 5) {
1587 dh->filled = 0;
1588 if (newlen > dh->needlen)
1589 return 1;
1590 }
1591
1592 newptr = realloc(dh->contents, newlen);
1593 if (!newptr) {
1594 dh->error = -ENOMEM;
1595 return 1;
1596 }
1597 dh->contents = newptr;
1598 if (dh->fuse->major == 5) {
1599 struct fuse_dirent_compat5 *dirent;
1600 dirent = (struct fuse_dirent_compat5 *) (dh->contents + dh->len);
1601 dirent->ino = ino;
1602 dirent->namelen = namelen;
1603 dirent->type = type;
1604 strncpy(dirent->name, name, namelen);
1605 } else {
1606 struct fuse_dirent *dirent;
1607 dirent = (struct fuse_dirent *) (dh->contents + dh->len);
1608 dirent->ino = ino;
1609 dirent->off = off ? off : newlen;
1610 dirent->namelen = namelen;
1611 dirent->type = type;
1612 strncpy(dirent->name, name, namelen);
1613 }
1614 if (padlen)
1615 memset(dh->contents + dh->len + entlen, 0, padlen);
1616 dh->len = newlen;
1617 return 0;
1618}
1619
1620static int fill_dir(void *buf, const char *name, const struct stat *stat,
1621 off_t off)
1622{
1623 int type = stat ? (stat->st_mode & 0170000) >> 12 : 0;
1624 ino_t ino = stat ? stat->st_ino : (ino_t) -1;
1625 return fill_dir_common(buf, name, type, ino, off);
1626}
1627
1628static int fill_dir_old(struct fuse_dirhandle *dh, const char *name, int type,
1629 ino_t ino)
1630{
1631 fill_dir_common(dh, name, type, ino, 0);
1632 return dh->error;
1633}
1634
1635static int readdir_fill(struct fuse *f, struct fuse_in_header *in,
1636 struct fuse_read_in *arg, struct fuse_dirhandle *dh)
1637{
1638 int err = -ENOENT;
1639 char *path = get_path(f, in->nodeid);
1640 if (path != NULL) {
1641 struct fuse_file_info fi;
1642
1643 memset(&fi, 0, sizeof(fi));
1644 fi.fh = dh->fh;
1645
1646 dh->len = 0;
1647 dh->error = 0;
1648 dh->needlen = arg->size;
1649 dh->filled = 1;
1650 err = -ENOSYS;
1651 if (f->op.readdir) {
1652 off_t offset = f->major == 5 ? 0 : arg->offset;
1653 err = f->op.readdir(path, dh, fill_dir, offset, &fi);
1654 } else if (f->op.getdir)
1655 err = f->op.getdir(path, dh, fill_dir_old);
1656 if (!err)
1657 err = dh->error;
1658 if (err)
1659 dh->filled = 0;
1660 free(path);
1661 }
1662 return err;
1663}
1664
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001665static void do_readdir(struct fuse *f, struct fuse_in_header *in,
1666 struct fuse_read_in *arg)
1667{
Miklos Szerediab974562005-04-07 15:40:21 +00001668 int err = 0;
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001669 struct fuse_dirhandle *dh = get_dirhandle(arg->fh);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001670 size_t size = 0;
Miklos Szerediab974562005-04-07 15:40:21 +00001671 unsigned char *buf = NULL;
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001672
Miklos Szerediab974562005-04-07 15:40:21 +00001673 pthread_mutex_lock(&dh->lock);
1674 if (!dh->filled) {
1675 err = readdir_fill(f, in, arg, dh);
1676 if (err)
1677 goto out;
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001678 }
Miklos Szerediab974562005-04-07 15:40:21 +00001679 if (dh->filled) {
Miklos Szeredib92d9782005-02-07 16:10:49 +00001680 if (arg->offset < dh->len) {
1681 size = arg->size;
1682 if (arg->offset + size > dh->len)
1683 size = dh->len - arg->offset;
Miklos Szerediab974562005-04-07 15:40:21 +00001684 buf = dh->contents + arg->offset;
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001685 }
Miklos Szerediab974562005-04-07 15:40:21 +00001686 } else {
1687 size = dh->len;
1688 buf = dh->contents;
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001689 }
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001690
Miklos Szerediab974562005-04-07 15:40:21 +00001691 out:
1692 send_reply(f, in, err, buf, size);
1693 pthread_mutex_unlock(&dh->lock);
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001694}
1695
1696static void do_releasedir(struct fuse *f, struct fuse_in_header *in,
1697 struct fuse_release_in *arg)
1698{
1699 struct fuse_dirhandle *dh = get_dirhandle(arg->fh);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001700 if (f->op.releasedir) {
1701 char *path;
1702 struct fuse_file_info fi;
Miklos Szerediab974562005-04-07 15:40:21 +00001703
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001704 memset(&fi, 0, sizeof(fi));
1705 fi.fh = dh->fh;
1706
1707 path = get_path(f, in->nodeid);
1708 f->op.releasedir(path ? path : "-", &fi);
1709 free(path);
1710 }
Miklos Szerediab974562005-04-07 15:40:21 +00001711 pthread_mutex_lock(&dh->lock);
1712 pthread_mutex_unlock(&dh->lock);
1713 pthread_mutex_destroy(&dh->lock);
Miklos Szeredib92d9782005-02-07 16:10:49 +00001714 free(dh->contents);
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001715 free(dh);
1716 send_reply(f, in, 0, NULL, 0);
1717}
1718
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001719static void do_fsyncdir(struct fuse *f, struct fuse_in_header *in,
1720 struct fuse_fsync_in *inarg)
1721{
1722 int res;
1723 char *path;
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001724 struct fuse_dirhandle *dh = get_dirhandle(inarg->fh);
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001725 struct fuse_file_info fi;
1726
1727 memset(&fi, 0, sizeof(fi));
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001728 fi.fh = dh->fh;
1729
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001730 res = -ENOENT;
1731 path = get_path(f, in->nodeid);
1732 if (path != NULL) {
1733 res = -ENOSYS;
1734 if (f->op.fsyncdir)
1735 res = f->op.fsyncdir(path, inarg->fsync_flags & 1, &fi);
1736 free(path);
1737 }
1738 send_reply(f, in, res, NULL, 0);
1739}
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001740
Miklos Szeredi43696432001-11-18 19:15:05 +00001741static void free_cmd(struct fuse_cmd *cmd)
1742{
1743 free(cmd->buf);
1744 free(cmd);
1745}
1746
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001747void fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd)
Miklos Szeredia181e612001-11-06 12:03:23 +00001748{
Miklos Szeredia181e612001-11-06 12:03:23 +00001749 struct fuse_in_header *in = (struct fuse_in_header *) cmd->buf;
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001750 void *inarg = cmd->buf + SIZEOF_COMPAT(f, fuse_in_header);
Miklos Szeredid169f312004-09-22 08:48:26 +00001751 struct fuse_context *ctx = fuse_get_context();
Miklos Szeredia181e612001-11-06 12:03:23 +00001752
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001753 fuse_dec_avail(f);
Miklos Szeredi33232032001-11-19 17:55:51 +00001754
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001755 if ((f->flags & FUSE_DEBUG)) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001756 printf("unique: %llu, opcode: %s (%i), nodeid: %lu, insize: %i\n",
1757 in->unique, opname(in->opcode), in->opcode,
1758 (unsigned long) in->nodeid, cmd->buflen);
Miklos Szeredic0938ea2001-11-07 12:35:06 +00001759 fflush(stdout);
1760 }
Miklos Szeredife25def2001-12-20 15:38:05 +00001761
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001762 if (!f->got_init && in->opcode != FUSE_INIT) {
1763 /* Old kernel version probably */
1764 send_reply(f, in, -EPROTO, NULL, 0);
1765 goto out;
1766 }
1767
Miklos Szeredid169f312004-09-22 08:48:26 +00001768 ctx->fuse = f;
Miklos Szeredife25def2001-12-20 15:38:05 +00001769 ctx->uid = in->uid;
1770 ctx->gid = in->gid;
Miklos Szeredi1f18db52004-09-27 06:54:49 +00001771 ctx->pid = in->pid;
Miklos Szeredi159bd7e2005-02-28 17:32:16 +00001772 ctx->private_data = f->user_data;
Miklos Szeredie5183742005-02-02 11:14:04 +00001773
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001774 switch (in->opcode) {
Miklos Szeredia181e612001-11-06 12:03:23 +00001775 case FUSE_LOOKUP:
1776 do_lookup(f, in, (char *) inarg);
1777 break;
1778
Miklos Szeredia181e612001-11-06 12:03:23 +00001779 case FUSE_GETATTR:
1780 do_getattr(f, in);
1781 break;
1782
1783 case FUSE_SETATTR:
1784 do_setattr(f, in, (struct fuse_setattr_in *) inarg);
1785 break;
1786
1787 case FUSE_READLINK:
1788 do_readlink(f, in);
1789 break;
1790
Miklos Szeredia181e612001-11-06 12:03:23 +00001791 case FUSE_MKNOD:
1792 do_mknod(f, in, (struct fuse_mknod_in *) inarg);
1793 break;
Miklos Szeredie5183742005-02-02 11:14:04 +00001794
Miklos Szeredia181e612001-11-06 12:03:23 +00001795 case FUSE_MKDIR:
1796 do_mkdir(f, in, (struct fuse_mkdir_in *) inarg);
1797 break;
Miklos Szeredie5183742005-02-02 11:14:04 +00001798
Miklos Szeredia181e612001-11-06 12:03:23 +00001799 case FUSE_UNLINK:
Miklos Szeredib5958612004-02-20 14:10:49 +00001800 do_unlink(f, in, (char *) inarg);
1801 break;
1802
Miklos Szeredia181e612001-11-06 12:03:23 +00001803 case FUSE_RMDIR:
Miklos Szeredib5958612004-02-20 14:10:49 +00001804 do_rmdir(f, in, (char *) inarg);
Miklos Szeredia181e612001-11-06 12:03:23 +00001805 break;
1806
1807 case FUSE_SYMLINK:
Miklos Szeredie5183742005-02-02 11:14:04 +00001808 do_symlink(f, in, (char *) inarg,
Miklos Szeredia181e612001-11-06 12:03:23 +00001809 ((char *) inarg) + strlen((char *) inarg) + 1);
1810 break;
1811
1812 case FUSE_RENAME:
1813 do_rename(f, in, (struct fuse_rename_in *) inarg);
1814 break;
Miklos Szeredie5183742005-02-02 11:14:04 +00001815
Miklos Szeredia181e612001-11-06 12:03:23 +00001816 case FUSE_LINK:
1817 do_link(f, in, (struct fuse_link_in *) inarg);
1818 break;
1819
1820 case FUSE_OPEN:
1821 do_open(f, in, (struct fuse_open_in *) inarg);
1822 break;
1823
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001824 case FUSE_FLUSH:
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001825 do_flush(f, in, (struct fuse_flush_in *) inarg);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001826 break;
1827
Miklos Szeredi9478e862002-12-11 09:50:26 +00001828 case FUSE_RELEASE:
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001829 do_release(f, in, (struct fuse_release_in *) inarg);
Miklos Szeredi9478e862002-12-11 09:50:26 +00001830 break;
1831
Miklos Szeredia181e612001-11-06 12:03:23 +00001832 case FUSE_READ:
1833 do_read(f, in, (struct fuse_read_in *) inarg);
1834 break;
1835
1836 case FUSE_WRITE:
1837 do_write(f, in, (struct fuse_write_in *) inarg);
1838 break;
1839
Mark Glinesd84b39a2002-01-07 16:32:02 +00001840 case FUSE_STATFS:
1841 do_statfs(f, in);
1842 break;
1843
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001844 case FUSE_FSYNC:
1845 do_fsync(f, in, (struct fuse_fsync_in *) inarg);
1846 break;
1847
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001848 case FUSE_SETXATTR:
1849 do_setxattr(f, in, (struct fuse_setxattr_in *) inarg);
1850 break;
1851
1852 case FUSE_GETXATTR:
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001853 do_getxattr(f, in, (struct fuse_getxattr_in *) inarg);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001854 break;
1855
1856 case FUSE_LISTXATTR:
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001857 do_listxattr(f, in, (struct fuse_getxattr_in *) inarg);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001858 break;
1859
1860 case FUSE_REMOVEXATTR:
1861 do_removexattr(f, in, (char *) inarg);
1862 break;
1863
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001864 case FUSE_INIT:
1865 do_init(f, in, (struct fuse_init_in_out *) inarg);
1866 break;
1867
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001868 case FUSE_OPENDIR:
1869 do_opendir(f, in, (struct fuse_open_in *) inarg);
1870 break;
1871
1872 case FUSE_READDIR:
1873 do_readdir(f, in, (struct fuse_read_in *) inarg);
1874 break;
1875
1876 case FUSE_RELEASEDIR:
1877 do_releasedir(f, in, (struct fuse_release_in *) inarg);
1878 break;
1879
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001880 case FUSE_FSYNCDIR:
1881 do_fsyncdir(f, in, (struct fuse_fsync_in *) inarg);
1882 break;
1883
Miklos Szeredia181e612001-11-06 12:03:23 +00001884 default:
Miklos Szeredi43696432001-11-18 19:15:05 +00001885 send_reply(f, in, -ENOSYS, NULL, 0);
Miklos Szeredia181e612001-11-06 12:03:23 +00001886 }
Miklos Szeredi43696432001-11-18 19:15:05 +00001887
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001888 out:
Miklos Szeredi43696432001-11-18 19:15:05 +00001889 free_cmd(cmd);
Miklos Szeredia181e612001-11-06 12:03:23 +00001890}
1891
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001892int fuse_exited(struct fuse* f)
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001893{
1894 return f->exited;
1895}
1896
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001897struct fuse_cmd *fuse_read_cmd(struct fuse *f)
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001898{
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001899 ssize_t res;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001900 struct fuse_cmd *cmd;
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001901 struct fuse_in_header *in;
1902 void *inarg;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001903
Miklos Szeredi43696432001-11-18 19:15:05 +00001904 cmd = (struct fuse_cmd *) malloc(sizeof(struct fuse_cmd));
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001905 if (cmd == NULL) {
1906 fprintf(stderr, "fuse: failed to allocate cmd in read\n");
1907 return NULL;
1908 }
Miklos Szeredi43696432001-11-18 19:15:05 +00001909 cmd->buf = (char *) malloc(FUSE_MAX_IN);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001910 if (cmd->buf == NULL) {
1911 fprintf(stderr, "fuse: failed to allocate read buffer\n");
1912 free(cmd);
1913 return NULL;
1914 }
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001915 in = (struct fuse_in_header *) cmd->buf;
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001916 inarg = cmd->buf + SIZEOF_COMPAT(f, fuse_in_header);
Miklos Szeredi43696432001-11-18 19:15:05 +00001917
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001918 res = read(f->fd, cmd->buf, FUSE_MAX_IN);
1919 if (res == -1) {
1920 free_cmd(cmd);
Miklos Szeredie56818b2004-12-12 11:45:24 +00001921 if (fuse_exited(f) || errno == EINTR || errno == ENOENT)
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001922 return NULL;
Miklos Szeredie5183742005-02-02 11:14:04 +00001923
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001924 /* ENODEV means we got unmounted, so we silenty return failure */
1925 if (errno != ENODEV) {
1926 /* BAD... This will happen again */
1927 perror("fuse: reading device");
1928 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001929
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001930 fuse_exit(f);
1931 return NULL;
1932 }
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001933 if ((size_t) res < SIZEOF_COMPAT(f, fuse_in_header)) {
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001934 free_cmd(cmd);
1935 /* Cannot happen */
1936 fprintf(stderr, "short read on fuse device\n");
1937 fuse_exit(f);
1938 return NULL;
1939 }
1940 cmd->buflen = res;
Miklos Szeredie5183742005-02-02 11:14:04 +00001941
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001942 /* Forget is special, it can be done without messing with threads. */
1943 if (in->opcode == FUSE_FORGET) {
1944 do_forget(f, in, (struct fuse_forget_in *) inarg);
1945 free_cmd(cmd);
1946 return NULL;
1947 }
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001948
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001949 return cmd;
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001950}
1951
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001952int fuse_loop(struct fuse *f)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001953{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001954 if (f == NULL)
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001955 return -1;
Miklos Szeredic40748a2004-02-20 16:38:45 +00001956
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001957 while (1) {
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001958 struct fuse_cmd *cmd;
1959
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001960 if (fuse_exited(f))
Miklos Szeredi874e3c12004-11-01 23:15:20 +00001961 break;
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001962
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001963 cmd = fuse_read_cmd(f);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001964 if (cmd == NULL)
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001965 continue;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001966
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001967 fuse_process_cmd(f, cmd);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001968 }
Miklos Szeredi874e3c12004-11-01 23:15:20 +00001969 f->exited = 0;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001970 return 0;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001971}
1972
Miklos Szeredi891b8742004-07-29 09:27:49 +00001973int fuse_invalidate(struct fuse *f, const char *path)
1974{
Miklos Szeredie56818b2004-12-12 11:45:24 +00001975 (void) f;
1976 (void) path;
1977 return -EINVAL;
Miklos Szeredi891b8742004-07-29 09:27:49 +00001978}
1979
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001980void fuse_exit(struct fuse *f)
1981{
1982 f->exited = 1;
1983}
1984
Miklos Szeredid169f312004-09-22 08:48:26 +00001985struct fuse_context *fuse_get_context()
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001986{
Miklos Szeredid169f312004-09-22 08:48:26 +00001987 static struct fuse_context context;
1988 if (fuse_getcontext)
1989 return fuse_getcontext();
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001990 else
Miklos Szeredid169f312004-09-22 08:48:26 +00001991 return &context;
1992}
1993
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001994void fuse_set_getcontext_func(struct fuse_context *(*func)(void))
Miklos Szeredid169f312004-09-22 08:48:26 +00001995{
1996 fuse_getcontext = func;
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001997}
1998
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001999int fuse_is_lib_option(const char *opt)
2000{
2001 if (strcmp(opt, "debug") == 0 ||
Miklos Szeredia13d9002004-11-02 17:32:03 +00002002 strcmp(opt, "hard_remove") == 0 ||
2003 strcmp(opt, "use_ino") == 0)
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002004 return 1;
2005 else
2006 return 0;
2007}
2008
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002009static int parse_lib_opts(struct fuse *f, const char *opts)
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002010{
2011 if (opts) {
2012 char *xopts = strdup(opts);
2013 char *s = xopts;
2014 char *opt;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002015
Miklos Szeredie56818b2004-12-12 11:45:24 +00002016 if (xopts == NULL) {
2017 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002018 return -1;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002019 }
Miklos Szeredie5183742005-02-02 11:14:04 +00002020
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002021 while((opt = strsep(&s, ","))) {
2022 if (strcmp(opt, "debug") == 0)
2023 f->flags |= FUSE_DEBUG;
2024 else if (strcmp(opt, "hard_remove") == 0)
2025 f->flags |= FUSE_HARD_REMOVE;
Miklos Szeredia13d9002004-11-02 17:32:03 +00002026 else if (strcmp(opt, "use_ino") == 0)
2027 f->flags |= FUSE_USE_INO;
Miklos Szeredie5183742005-02-02 11:14:04 +00002028 else
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002029 fprintf(stderr, "fuse: warning: unknown option `%s'\n", opt);
2030 }
2031 free(xopts);
2032 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002033 return 0;
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002034}
2035
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002036struct fuse *fuse_new_common(int fd, const char *opts,
2037 const struct fuse_operations *op,
2038 size_t op_size, int compat)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002039{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002040 struct fuse *f;
2041 struct node *root;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002042
Miklos Szeredi0f62d722005-01-04 12:45:54 +00002043 if (sizeof(struct fuse_operations) < op_size) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002044 fprintf(stderr, "fuse: warning: library too old, some operations may not not work\n");
Miklos Szeredi0f62d722005-01-04 12:45:54 +00002045 op_size = sizeof(struct fuse_operations);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002046 }
2047
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002048 f = (struct fuse *) calloc(1, sizeof(struct fuse));
Miklos Szeredie56818b2004-12-12 11:45:24 +00002049 if (f == NULL) {
2050 fprintf(stderr, "fuse: failed to allocate fuse object\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002051 goto out;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002052 }
Miklos Szeredi2df1c042001-11-06 15:07:17 +00002053
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002054 if (parse_lib_opts(f, opts) == -1)
2055 goto out_free;
2056
Miklos Szeredi8cffdb92001-11-09 14:49:18 +00002057 f->fd = fd;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002058 f->ctr = 0;
Miklos Szeredi76f65782004-02-19 16:55:40 +00002059 f->generation = 0;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00002060 /* FIXME: Dynamic hash table */
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002061 f->name_table_size = 14057;
2062 f->name_table = (struct node **)
2063 calloc(1, sizeof(struct node *) * f->name_table_size);
Miklos Szeredie56818b2004-12-12 11:45:24 +00002064 if (f->name_table == NULL) {
2065 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002066 goto out_free;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002067 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002068
Miklos Szeredia13d9002004-11-02 17:32:03 +00002069 f->id_table_size = 14057;
2070 f->id_table = (struct node **)
2071 calloc(1, sizeof(struct node *) * f->id_table_size);
Miklos Szeredie56818b2004-12-12 11:45:24 +00002072 if (f->id_table == NULL) {
2073 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002074 goto out_free_name_table;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002075 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002076
Miklos Szerediab974562005-04-07 15:40:21 +00002077 mutex_init(&f->lock);
2078 mutex_init(&f->worker_lock);
Miklos Szeredi33232032001-11-19 17:55:51 +00002079 f->numworker = 0;
2080 f->numavail = 0;
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002081 memcpy(&f->op, op, op_size);
2082 f->compat = compat;
Miklos Szeredi0f48a262002-12-05 14:23:01 +00002083 f->exited = 0;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002084
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002085 root = (struct node *) calloc(1, sizeof(struct node));
Miklos Szeredie56818b2004-12-12 11:45:24 +00002086 if (root == NULL) {
2087 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredia13d9002004-11-02 17:32:03 +00002088 goto out_free_id_table;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002089 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002090
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002091 root->name = strdup("/");
Miklos Szeredie56818b2004-12-12 11:45:24 +00002092 if (root->name == NULL) {
2093 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002094 goto out_free_root;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002095 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002096
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002097 root->parent = 0;
Miklos Szeredia13d9002004-11-02 17:32:03 +00002098 root->nodeid = FUSE_ROOT_ID;
Miklos Szeredi76f65782004-02-19 16:55:40 +00002099 root->generation = 0;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00002100 root->refctr = 1;
Miklos Szeredia13d9002004-11-02 17:32:03 +00002101 hash_id(f, root);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002102
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002103 return f;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002104
2105 out_free_root:
2106 free(root);
Miklos Szeredia13d9002004-11-02 17:32:03 +00002107 out_free_id_table:
2108 free(f->id_table);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002109 out_free_name_table:
2110 free(f->name_table);
2111 out_free:
2112 free(f);
2113 out:
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002114 return NULL;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002115}
2116
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002117struct fuse *fuse_new(int fd, const char *opts,
2118 const struct fuse_operations *op, size_t op_size)
2119{
2120 return fuse_new_common(fd, opts, op, op_size, 0);
2121}
2122
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002123struct fuse *fuse_new_compat2(int fd, const char *opts,
2124 const struct fuse_operations_compat2 *op)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002125{
2126 return fuse_new_common(fd, opts, (struct fuse_operations *) op,
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002127 sizeof(struct fuse_operations_compat2), 21);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002128}
2129
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002130struct fuse *fuse_new_compat1(int fd, int flags,
2131 const struct fuse_operations_compat1 *op)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002132{
2133 char *opts = NULL;
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002134 if (flags & FUSE_DEBUG_COMPAT1)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002135 opts = "debug";
2136 return fuse_new_common(fd, opts, (struct fuse_operations *) op,
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002137 sizeof(struct fuse_operations_compat1), 11);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002138}
2139
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002140void fuse_destroy(struct fuse *f)
2141{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002142 size_t i;
Miklos Szeredia13d9002004-11-02 17:32:03 +00002143 for (i = 0; i < f->id_table_size; i++) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002144 struct node *node;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002145
Miklos Szeredia13d9002004-11-02 17:32:03 +00002146 for (node = f->id_table[i]; node != NULL; node = node->id_next) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002147 if (node->is_hidden) {
Miklos Szeredia13d9002004-11-02 17:32:03 +00002148 char *path = get_path(f, node->nodeid);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002149 if (path)
2150 f->op.unlink(path);
2151 }
2152 }
2153 }
Miklos Szeredia13d9002004-11-02 17:32:03 +00002154 for (i = 0; i < f->id_table_size; i++) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002155 struct node *node;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002156 struct node *next;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002157
Miklos Szeredia13d9002004-11-02 17:32:03 +00002158 for (node = f->id_table[i]; node != NULL; node = next) {
2159 next = node->id_next;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002160 free_node(node);
2161 }
2162 }
Miklos Szeredia13d9002004-11-02 17:32:03 +00002163 free(f->id_table);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002164 free(f->name_table);
Miklos Szeredia181e612001-11-06 12:03:23 +00002165 pthread_mutex_destroy(&f->lock);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00002166 pthread_mutex_destroy(&f->worker_lock);
Miklos Szeredi159bd7e2005-02-28 17:32:16 +00002167 if (f->op.destroy)
2168 f->op.destroy(f->user_data);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002169 free(f);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002170}
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002171
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002172__asm__(".symver fuse_exited,__fuse_exited@");
2173__asm__(".symver fuse_process_cmd,__fuse_process_cmd@");
2174__asm__(".symver fuse_read_cmd,__fuse_read_cmd@");
2175__asm__(".symver fuse_set_getcontext_func,__fuse_set_getcontext_func@");
2176__asm__(".symver fuse_new_compat2,fuse_new@");