blob: 9bc933fd59dda6303af877e5f28d72d5dfc3c43e [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 Szeredi0111f9d2005-04-22 12:04:55 +000038/** Only allow root or the owner to access the filesystem */
39#define FUSE_ALLOW_ROOT (1 << 4)
40
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +000041#define FUSE_KERNEL_MINOR_VERSION_NEED 1
Miklos Szeredia25d4c22004-11-23 22:32:16 +000042#define FUSE_VERSION_FILE_OLD "/proc/fs/fuse/version"
Miklos Szeredi162bcbb2004-11-29 23:43:44 +000043#define FUSE_VERSION_FILE_NEW "/sys/fs/fuse/version"
Miklos Szeredia25d4c22004-11-23 22:32:16 +000044#define FUSE_DEV_OLD "/proc/fs/fuse/dev"
45
Miklos Szeredi97c61e92001-11-07 12:09:43 +000046#define FUSE_MAX_PATH 4096
Miklos Szeredi30e093a2005-04-03 17:44:54 +000047#define PARAM_T(inarg, type) (((char *)(inarg)) + sizeof(type))
48#define PARAM(inarg) PARAM_T(inarg, *(inarg))
49#define PARAM_COMPAT(f, inarg, type) \
50 ((f)->major == 5 ? PARAM_T(inarg, struct type ## _compat5) : PARAM(inarg))
51
52#define MEMBER_COMPAT(f, ptr, memb, type) \
53 ((f)->major == 5 ? &((struct type ## _compat5 *) (ptr))->memb : &ptr->memb)
54
55#define SIZEOF_COMPAT(f, type) \
56 ((f)->major == 5 ? sizeof(struct type ## _compat5) : sizeof(struct type))
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000057
Miklos Szeredi254d5ed2004-03-02 11:11:24 +000058#define ENTRY_REVALIDATE_TIME 1 /* sec */
59#define ATTR_REVALIDATE_TIME 1 /* sec */
60
Miklos Szeredi0f62d722005-01-04 12:45:54 +000061
62struct node {
63 struct node *name_next;
64 struct node *id_next;
65 nodeid_t nodeid;
66 unsigned int generation;
67 int refctr;
68 nodeid_t parent;
69 char *name;
70 uint64_t version;
71 int open_count;
72 int is_hidden;
73};
74
75struct fuse_dirhandle {
Miklos Szerediab974562005-04-07 15:40:21 +000076 pthread_mutex_t lock;
Miklos Szeredi0f62d722005-01-04 12:45:54 +000077 struct fuse *fuse;
Miklos Szeredib92d9782005-02-07 16:10:49 +000078 unsigned char *contents;
Miklos Szerediab974562005-04-07 15:40:21 +000079 int allocated;
Miklos Szeredib92d9782005-02-07 16:10:49 +000080 unsigned len;
Miklos Szerediab974562005-04-07 15:40:21 +000081 unsigned needlen;
Miklos Szeredi3ead28e2005-01-18 21:23:41 +000082 int filled;
Miklos Szerediede1f7a2005-04-01 21:08:57 +000083 unsigned long fh;
Miklos Szerediab974562005-04-07 15:40:21 +000084 int error;
Miklos Szeredi0f62d722005-01-04 12:45:54 +000085};
86
87struct fuse_cmd {
88 char *buf;
89 size_t buflen;
90};
91
92
Miklos Szeredid169f312004-09-22 08:48:26 +000093static struct fuse_context *(*fuse_getcontext)(void) = NULL;
94
Miklos Szerediab974562005-04-07 15:40:21 +000095#ifndef USE_UCLIBC
96#define mutex_init(mut) pthread_mutex_init(mut, NULL)
97#else
98static void mutex_init(pthread_mutex_t mut)
99{
100 pthread_mutexattr_t attr;
101 pthread_mutexattr_init(&attr);
102 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
103 pthread_mutex_init(mut, &attr);
104 pthread_mutexattr_destroy(&attr);
105}
106#endif
107
Miklos Szeredic8ba2372002-12-10 12:26:00 +0000108static const char *opname(enum fuse_opcode opcode)
109{
Miklos Szeredie5183742005-02-02 11:14:04 +0000110 switch (opcode) {
Miklos Szeredi3ed84232004-03-30 15:17:26 +0000111 case FUSE_LOOKUP: return "LOOKUP";
112 case FUSE_FORGET: return "FORGET";
113 case FUSE_GETATTR: return "GETATTR";
114 case FUSE_SETATTR: return "SETATTR";
115 case FUSE_READLINK: return "READLINK";
116 case FUSE_SYMLINK: return "SYMLINK";
Miklos Szeredi3ed84232004-03-30 15:17:26 +0000117 case FUSE_MKNOD: return "MKNOD";
118 case FUSE_MKDIR: return "MKDIR";
119 case FUSE_UNLINK: return "UNLINK";
120 case FUSE_RMDIR: return "RMDIR";
121 case FUSE_RENAME: return "RENAME";
122 case FUSE_LINK: return "LINK";
123 case FUSE_OPEN: return "OPEN";
124 case FUSE_READ: return "READ";
125 case FUSE_WRITE: return "WRITE";
126 case FUSE_STATFS: return "STATFS";
Miklos Szeredi99f20742004-05-19 08:01:10 +0000127 case FUSE_FLUSH: return "FLUSH";
Miklos Szeredi3ed84232004-03-30 15:17:26 +0000128 case FUSE_RELEASE: return "RELEASE";
129 case FUSE_FSYNC: return "FSYNC";
130 case FUSE_SETXATTR: return "SETXATTR";
131 case FUSE_GETXATTR: return "GETXATTR";
132 case FUSE_LISTXATTR: return "LISTXATTR";
133 case FUSE_REMOVEXATTR: return "REMOVEXATTR";
Miklos Szeredi3f0005f2005-01-04 19:24:31 +0000134 case FUSE_INIT: return "INIT";
Miklos Szeredi3ead28e2005-01-18 21:23:41 +0000135 case FUSE_OPENDIR: return "OPENDIR";
136 case FUSE_READDIR: return "READDIR";
137 case FUSE_RELEASEDIR: return "RELEASEDIR";
Miklos Szeredi4283ee72005-03-21 12:09:04 +0000138 case FUSE_FSYNCDIR: return "FSYNCDIR";
Miklos Szeredi99f20742004-05-19 08:01:10 +0000139 default: return "???";
Miklos Szeredic8ba2372002-12-10 12:26:00 +0000140 }
141}
142
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000143static inline void fuse_dec_avail(struct fuse *f)
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000144{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000145 pthread_mutex_lock(&f->worker_lock);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000146 f->numavail --;
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000147 pthread_mutex_unlock(&f->worker_lock);
148}
149
150static inline void fuse_inc_avail(struct fuse *f)
151{
152 pthread_mutex_lock(&f->worker_lock);
153 f->numavail ++;
154 pthread_mutex_unlock(&f->worker_lock);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000155}
156
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000157static struct node *get_node_nocheck(struct fuse *f, nodeid_t nodeid)
Miklos Szeredia181e612001-11-06 12:03:23 +0000158{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000159 size_t hash = nodeid % f->id_table_size;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000160 struct node *node;
161
Miklos Szeredia13d9002004-11-02 17:32:03 +0000162 for (node = f->id_table[hash]; node != NULL; node = node->id_next)
163 if (node->nodeid == nodeid)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000164 return node;
Miklos Szeredie5183742005-02-02 11:14:04 +0000165
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000166 return NULL;
Miklos Szeredia181e612001-11-06 12:03:23 +0000167}
168
Miklos Szeredia13d9002004-11-02 17:32:03 +0000169static struct node *get_node(struct fuse *f, nodeid_t nodeid)
Miklos Szeredia181e612001-11-06 12:03:23 +0000170{
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000171 struct node *node = get_node_nocheck(f, nodeid);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000172 if (!node) {
173 fprintf(stderr, "fuse internal error: node %lu not found\n",
174 nodeid);
175 abort();
176 }
177 return node;
Miklos Szeredia181e612001-11-06 12:03:23 +0000178}
179
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000180static void free_node(struct node *node)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000181{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000182 free(node->name);
183 free(node);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000184}
185
Miklos Szeredia13d9002004-11-02 17:32:03 +0000186static void unhash_id(struct fuse *f, struct node *node)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000187{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000188 size_t hash = node->nodeid % f->id_table_size;
189 struct node **nodep = &f->id_table[hash];
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000190
Miklos Szeredie5183742005-02-02 11:14:04 +0000191 for (; *nodep != NULL; nodep = &(*nodep)->id_next)
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000192 if (*nodep == node) {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000193 *nodep = node->id_next;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000194 return;
195 }
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000196}
197
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000198static void hash_id(struct fuse *f, struct node *node)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000199{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000200 size_t hash = node->nodeid % f->id_table_size;
201 node->id_next = f->id_table[hash];
Miklos Szeredie5183742005-02-02 11:14:04 +0000202 f->id_table[hash] = node;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000203}
204
Miklos Szeredia13d9002004-11-02 17:32:03 +0000205static unsigned int name_hash(struct fuse *f, nodeid_t parent, const char *name)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000206{
207 unsigned int hash = *name;
208
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000209 if (hash)
210 for (name += 1; *name != '\0'; name++)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000211 hash = (hash << 5) - hash + *name;
212
213 return (hash + parent) % f->name_table_size;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000214}
215
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000216static void unref_node(struct fuse *f, struct node *node);
217
218static void unhash_name(struct fuse *f, struct node *node)
219{
220 if (node->name) {
221 size_t hash = name_hash(f, node->parent, node->name);
222 struct node **nodep = &f->name_table[hash];
Miklos Szeredie5183742005-02-02 11:14:04 +0000223
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000224 for (; *nodep != NULL; nodep = &(*nodep)->name_next)
225 if (*nodep == node) {
226 *nodep = node->name_next;
227 node->name_next = NULL;
228 unref_node(f, get_node(f, node->parent));
229 free(node->name);
230 node->name = NULL;
231 node->parent = 0;
232 return;
233 }
234 fprintf(stderr, "fuse internal error: unable to unhash node: %lu\n",
235 node->nodeid);
236 abort();
237 }
238}
239
240static int hash_name(struct fuse *f, struct node *node, nodeid_t parent,
241 const char *name)
242{
243 size_t hash = name_hash(f, parent, name);
244 node->name = strdup(name);
245 if (node->name == NULL)
246 return -1;
247
248 get_node(f, parent)->refctr ++;
249 node->parent = parent;
250 node->name_next = f->name_table[hash];
251 f->name_table[hash] = node;
252 return 0;
253}
254
255static void delete_node(struct fuse *f, struct node *node)
256{
257 assert(!node->name);
258 unhash_id(f, node);
259 free_node(node);
260}
261
262static void unref_node(struct fuse *f, struct node *node)
263{
264 assert(node->refctr > 0);
265 node->refctr --;
266 if (!node->refctr)
267 delete_node(f, node);
268}
269
270static nodeid_t next_id(struct fuse *f)
271{
272 do {
273 f->ctr++;
274 if (!f->ctr)
275 f->generation ++;
276 } while (f->ctr == 0 || get_node_nocheck(f, f->ctr) != NULL);
277 return f->ctr;
278}
279
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000280static struct node *lookup_node(struct fuse *f, nodeid_t parent,
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000281 const char *name)
282{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000283 size_t hash = name_hash(f, parent, name);
284 struct node *node;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000285
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000286 for (node = f->name_table[hash]; node != NULL; node = node->name_next)
287 if (node->parent == parent && strcmp(node->name, name) == 0)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000288 return node;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000289
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000290 return NULL;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000291}
292
Miklos Szeredia13d9002004-11-02 17:32:03 +0000293static struct node *find_node(struct fuse *f, nodeid_t parent, char *name,
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000294 struct fuse_attr *attr, uint64_t version)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000295{
296 struct node *node;
Miklos Szeredia181e612001-11-06 12:03:23 +0000297 int mode = attr->mode & S_IFMT;
298 int rdev = 0;
Miklos Szeredie5183742005-02-02 11:14:04 +0000299
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000300 if (S_ISCHR(mode) || S_ISBLK(mode))
Miklos Szeredia181e612001-11-06 12:03:23 +0000301 rdev = attr->rdev;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000302
Miklos Szeredia181e612001-11-06 12:03:23 +0000303 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000304 node = lookup_node(f, parent, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000305 if (node != NULL) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000306 if (!(f->flags & FUSE_USE_INO))
Miklos Szeredie5183742005-02-02 11:14:04 +0000307 attr->ino = node->nodeid;
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000308 } else {
309 node = (struct node *) calloc(1, sizeof(struct node));
310 if (node == NULL)
311 goto out_err;
Miklos Szeredie5183742005-02-02 11:14:04 +0000312
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000313 node->refctr = 1;
314 node->nodeid = next_id(f);
315 if (!(f->flags & FUSE_USE_INO))
316 attr->ino = node->nodeid;
317 node->open_count = 0;
318 node->is_hidden = 0;
319 node->generation = f->generation;
320 if (hash_name(f, node, parent, name) == -1) {
321 free(node);
322 node = NULL;
323 goto out_err;
324 }
325 hash_id(f, node);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000326 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000327 node->version = version;
Miklos Szeredic2309912004-09-21 13:40:38 +0000328 out_err:
Miklos Szeredia181e612001-11-06 12:03:23 +0000329 pthread_mutex_unlock(&f->lock);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000330 return node;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000331}
332
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000333static char *add_name(char *buf, char *s, const char *name)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000334{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000335 size_t len = strlen(name);
336 s -= len;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000337 if (s <= buf) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000338 fprintf(stderr, "fuse: path too long: ...%s\n", s + len);
339 return NULL;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000340 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000341 strncpy(s, name, len);
342 s--;
343 *s = '/';
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000344
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000345 return s;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000346}
347
Miklos Szeredia13d9002004-11-02 17:32:03 +0000348static char *get_path_name(struct fuse *f, nodeid_t nodeid, const char *name)
Miklos Szeredib483c932001-10-29 14:57:57 +0000349{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000350 char buf[FUSE_MAX_PATH];
351 char *s = buf + FUSE_MAX_PATH - 1;
352 struct node *node;
Miklos Szeredie5183742005-02-02 11:14:04 +0000353
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000354 *s = '\0';
Miklos Szeredia181e612001-11-06 12:03:23 +0000355
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000356 if (name != NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000357 s = add_name(buf, s, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000358 if (s == NULL)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000359 return NULL;
360 }
361
362 pthread_mutex_lock(&f->lock);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000363 for (node = get_node(f, nodeid); node && node->nodeid != FUSE_ROOT_ID;
364 node = get_node(f, node->parent)) {
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000365 if (node->name == NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000366 s = NULL;
367 break;
368 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000369
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000370 s = add_name(buf, s, node->name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000371 if (s == NULL)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000372 break;
373 }
374 pthread_mutex_unlock(&f->lock);
375
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000376 if (node == NULL || s == NULL)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000377 return NULL;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000378 else if (*s == '\0')
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000379 return strdup("/");
380 else
381 return strdup(s);
382}
Miklos Szeredia181e612001-11-06 12:03:23 +0000383
Miklos Szeredia13d9002004-11-02 17:32:03 +0000384static char *get_path(struct fuse *f, nodeid_t nodeid)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000385{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000386 return get_path_name(f, nodeid, NULL);
Miklos Szeredib483c932001-10-29 14:57:57 +0000387}
388
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000389static void forget_node(struct fuse *f, nodeid_t nodeid, uint64_t version)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000390{
Miklos Szeredia181e612001-11-06 12:03:23 +0000391 struct node *node;
392
393 pthread_mutex_lock(&f->lock);
Miklos Szeredid0cf1fb2005-05-06 10:10:38 +0000394 node = get_node_nocheck(f, nodeid);
395 if (node && node->version == version && nodeid != FUSE_ROOT_ID) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000396 node->version = 0;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000397 unhash_name(f, node);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000398 unref_node(f, node);
Miklos Szeredia181e612001-11-06 12:03:23 +0000399 }
400 pthread_mutex_unlock(&f->lock);
401
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000402}
403
Miklos Szeredia13d9002004-11-02 17:32:03 +0000404static void remove_node(struct fuse *f, nodeid_t dir, const char *name)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000405{
Miklos Szeredia181e612001-11-06 12:03:23 +0000406 struct node *node;
407
408 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000409 node = lookup_node(f, dir, name);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000410 if (node != NULL)
411 unhash_name(f, node);
Miklos Szeredia181e612001-11-06 12:03:23 +0000412 pthread_mutex_unlock(&f->lock);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000413}
414
Miklos Szeredia13d9002004-11-02 17:32:03 +0000415static int rename_node(struct fuse *f, nodeid_t olddir, const char *oldname,
416 nodeid_t newdir, const char *newname, int hide)
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000417{
Miklos Szeredia181e612001-11-06 12:03:23 +0000418 struct node *node;
419 struct node *newnode;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000420 int err = 0;
Miklos Szeredie5183742005-02-02 11:14:04 +0000421
Miklos Szeredia181e612001-11-06 12:03:23 +0000422 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000423 node = lookup_node(f, olddir, oldname);
424 newnode = lookup_node(f, newdir, newname);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000425 if (node == NULL)
426 goto out;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000427
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000428 if (newnode != NULL) {
429 if (hide) {
430 fprintf(stderr, "fuse: hidden file got created during hiding\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000431 err = -EBUSY;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000432 goto out;
433 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000434 unhash_name(f, newnode);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000435 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000436
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000437 unhash_name(f, node);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000438 if (hash_name(f, node, newdir, newname) == -1) {
439 err = -ENOMEM;
440 goto out;
441 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000442
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000443 if (hide)
444 node->is_hidden = 1;
445
446 out:
Miklos Szeredia181e612001-11-06 12:03:23 +0000447 pthread_mutex_unlock(&f->lock);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000448 return err;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000449}
450
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000451static void convert_stat(struct stat *stbuf, struct fuse_attr *attr)
452{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000453 attr->ino = stbuf->st_ino;
Miklos Szeredib5958612004-02-20 14:10:49 +0000454 attr->mode = stbuf->st_mode;
455 attr->nlink = stbuf->st_nlink;
456 attr->uid = stbuf->st_uid;
457 attr->gid = stbuf->st_gid;
458 attr->rdev = stbuf->st_rdev;
459 attr->size = stbuf->st_size;
460 attr->blocks = stbuf->st_blocks;
461 attr->atime = stbuf->st_atime;
Miklos Szeredib5958612004-02-20 14:10:49 +0000462 attr->mtime = stbuf->st_mtime;
Miklos Szeredib5958612004-02-20 14:10:49 +0000463 attr->ctime = stbuf->st_ctime;
Miklos Szeredicb264512004-06-23 18:52:50 +0000464#ifdef HAVE_STRUCT_STAT_ST_ATIM
465 attr->atimensec = stbuf->st_atim.tv_nsec;
466 attr->mtimensec = stbuf->st_mtim.tv_nsec;
Miklos Szeredib5958612004-02-20 14:10:49 +0000467 attr->ctimensec = stbuf->st_ctim.tv_nsec;
Miklos Szeredicb264512004-06-23 18:52:50 +0000468#endif
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000469}
470
Miklos Szerediab974562005-04-07 15:40:21 +0000471static size_t iov_length(const struct iovec *iov, size_t count)
Miklos Szerediede1f7a2005-04-01 21:08:57 +0000472{
Miklos Szerediab974562005-04-07 15:40:21 +0000473 size_t seg;
474 size_t ret = 0;
Miklos Szerediede1f7a2005-04-01 21:08:57 +0000475
Miklos Szerediab974562005-04-07 15:40:21 +0000476 for (seg = 0; seg < count; seg++)
477 ret += iov[seg].iov_len;
478 return ret;
Miklos Szerediede1f7a2005-04-01 21:08:57 +0000479}
480
Miklos Szerediab974562005-04-07 15:40:21 +0000481static int send_reply_raw(struct fuse *f, const struct iovec iov[],
482 size_t count)
Miklos Szeredi43696432001-11-18 19:15:05 +0000483{
484 int res;
Miklos Szerediab974562005-04-07 15:40:21 +0000485 unsigned outsize = iov_length(iov, count);
486 struct fuse_out_header *out = (struct fuse_out_header *) iov[0].iov_base;
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000487 out->len = outsize;
Miklos Szeredi43696432001-11-18 19:15:05 +0000488
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000489 if ((f->flags & FUSE_DEBUG)) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000490 printf(" unique: %llu, error: %i (%s), outsize: %i\n",
491 out->unique, out->error, strerror(-out->error), outsize);
Miklos Szeredi43696432001-11-18 19:15:05 +0000492 fflush(stdout);
493 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000494
Miklos Szeredi4e71c9f2004-02-09 12:05:14 +0000495 /* This needs to be done before the reply, otherwise the scheduler
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000496 could play tricks with us, and only let the counter be
497 increased long after the operation is done */
498 fuse_inc_avail(f);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000499
Miklos Szerediab974562005-04-07 15:40:21 +0000500 res = writev(f->fd, iov, count);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000501 if (res == -1) {
Miklos Szeredi43696432001-11-18 19:15:05 +0000502 /* ENOENT means the operation was interrupted */
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000503 if (!f->exited && errno != ENOENT)
Miklos Szeredi96249982001-11-21 12:21:19 +0000504 perror("fuse: writing device");
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000505 return -errno;
Miklos Szeredi43696432001-11-18 19:15:05 +0000506 }
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000507 return 0;
Miklos Szeredi43696432001-11-18 19:15:05 +0000508}
509
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000510static int send_reply(struct fuse *f, struct fuse_in_header *in, int error,
511 void *arg, size_t argsize)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000512{
Miklos Szerediab974562005-04-07 15:40:21 +0000513 struct fuse_out_header out;
514 struct iovec iov[2];
515 size_t count;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000516
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000517 if (error <= -1000 || error > 0) {
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000518 fprintf(stderr, "fuse: bad error value: %i\n", error);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000519 error = -ERANGE;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000520 }
521
Miklos Szerediab974562005-04-07 15:40:21 +0000522 out.unique = in->unique;
523 out.error = error;
524 count = 1;
525 iov[0].iov_base = &out;
526 iov[0].iov_len = sizeof(struct fuse_out_header);
527 if (argsize && !error) {
528 count++;
529 iov[1].iov_base = arg;
530 iov[1].iov_len = argsize;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000531 }
Miklos Szerediab974562005-04-07 15:40:21 +0000532 return send_reply_raw(f, iov, count);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000533}
534
Miklos Szeredia13d9002004-11-02 17:32:03 +0000535static int is_open(struct fuse *f, nodeid_t dir, const char *name)
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000536{
537 struct node *node;
538 int isopen = 0;
539 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000540 node = lookup_node(f, dir, name);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000541 if (node && node->open_count > 0)
542 isopen = 1;
543 pthread_mutex_unlock(&f->lock);
544 return isopen;
545}
546
Miklos Szeredia13d9002004-11-02 17:32:03 +0000547static char *hidden_name(struct fuse *f, nodeid_t dir, const char *oldname,
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000548 char *newname, size_t bufsize)
549{
550 struct stat buf;
551 struct node *node;
552 struct node *newnode;
553 char *newpath;
554 int res;
555 int failctr = 10;
556
557 if (!f->op.getattr)
558 return NULL;
559
560 do {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000561 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000562 node = lookup_node(f, dir, oldname);
563 if (node == NULL) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000564 pthread_mutex_unlock(&f->lock);
565 return NULL;
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000566 }
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000567 do {
568 f->hidectr ++;
569 snprintf(newname, bufsize, ".fuse_hidden%08x%08x",
Miklos Szeredia13d9002004-11-02 17:32:03 +0000570 (unsigned int) node->nodeid, f->hidectr);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000571 newnode = lookup_node(f, dir, newname);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000572 } while(newnode);
573 pthread_mutex_unlock(&f->lock);
Miklos Szeredie5183742005-02-02 11:14:04 +0000574
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000575 newpath = get_path_name(f, dir, newname);
576 if (!newpath)
577 break;
Miklos Szeredie5183742005-02-02 11:14:04 +0000578
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000579 res = f->op.getattr(newpath, &buf);
580 if (res != 0)
581 break;
582 free(newpath);
583 newpath = NULL;
584 } while(--failctr);
585
586 return newpath;
587}
588
Miklos Szeredia13d9002004-11-02 17:32:03 +0000589static int hide_node(struct fuse *f, const char *oldpath, nodeid_t dir,
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000590 const char *oldname)
591{
592 char newname[64];
593 char *newpath;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000594 int err = -EBUSY;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000595
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000596 if (f->op.rename && f->op.unlink) {
597 newpath = hidden_name(f, dir, oldname, newname, sizeof(newname));
598 if (newpath) {
599 int res = f->op.rename(oldpath, newpath);
600 if (res == 0)
601 err = rename_node(f, dir, oldname, dir, newname, 1);
602 free(newpath);
603 }
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000604 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000605 return err;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000606}
607
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000608static int lookup_path(struct fuse *f, nodeid_t nodeid, uint64_t version,
609 char *name, const char *path,
610 struct fuse_entry_out *arg)
Miklos Szeredi76f65782004-02-19 16:55:40 +0000611{
612 int res;
613 struct stat buf;
614
615 res = f->op.getattr(path, &buf);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000616 if (res == 0) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000617 struct node *node;
618
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000619 memset(arg, 0, sizeof(struct fuse_entry_out));
Miklos Szeredi76f65782004-02-19 16:55:40 +0000620 convert_stat(&buf, &arg->attr);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000621 node = find_node(f, nodeid, name, &arg->attr, version);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000622 if (node == NULL)
623 res = -ENOMEM;
624 else {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000625 arg->nodeid = node->nodeid;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000626 arg->generation = node->generation;
627 arg->entry_valid = ENTRY_REVALIDATE_TIME;
628 arg->entry_valid_nsec = 0;
629 arg->attr_valid = ATTR_REVALIDATE_TIME;
630 arg->attr_valid_nsec = 0;
631 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000632 printf(" NODEID: %lu\n", (unsigned long) arg->nodeid);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000633 fflush(stdout);
634 }
Miklos Szeredi76f65782004-02-19 16:55:40 +0000635 }
636 }
637 return res;
638}
639
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000640static void do_lookup(struct fuse *f, struct fuse_in_header *in, char *name)
641{
642 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000643 int res2;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000644 char *path;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000645 struct fuse_entry_out arg;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000646
Miklos Szeredi5e183482001-10-31 14:52:35 +0000647 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000648 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000649 if (path != NULL) {
650 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi6ebe2342002-01-06 21:44:16 +0000651 printf("LOOKUP %s\n", path);
652 fflush(stdout);
653 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000654 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000655 if (f->op.getattr)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000656 res = lookup_path(f, in->nodeid, in->unique, name, path, &arg);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000657 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000658 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000659 res2 = send_reply(f, in, res, &arg, sizeof(arg));
660 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000661 forget_node(f, arg.nodeid, in->unique);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000662}
663
Miklos Szeredia181e612001-11-06 12:03:23 +0000664static void do_forget(struct fuse *f, struct fuse_in_header *in,
665 struct fuse_forget_in *arg)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000666{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000667 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000668 printf("FORGET %lu/%llu\n", (unsigned long) in->nodeid,
669 arg->version);
Miklos Szeredi43696432001-11-18 19:15:05 +0000670 fflush(stdout);
671 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000672 forget_node(f, in->nodeid, arg->version);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000673}
674
675static void do_getattr(struct fuse *f, struct fuse_in_header *in)
676{
677 int res;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000678 char *path;
679 struct stat buf;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000680 struct fuse_attr_out arg;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000681
Miklos Szeredi5e183482001-10-31 14:52:35 +0000682 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000683 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000684 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +0000685 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000686 if (f->op.getattr)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000687 res = f->op.getattr(path, &buf);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000688 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000689 }
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000690
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000691 if (res == 0) {
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000692 memset(&arg, 0, sizeof(struct fuse_attr_out));
693 arg.attr_valid = ATTR_REVALIDATE_TIME;
694 arg.attr_valid_nsec = 0;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000695 convert_stat(&buf, &arg.attr);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000696 if (!(f->flags & FUSE_USE_INO))
697 arg.attr.ino = in->nodeid;
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000698 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000699
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000700 send_reply(f, in, res, &arg, sizeof(arg));
701}
702
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000703static int do_chmod(struct fuse *f, const char *path, struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000704{
705 int res;
706
707 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000708 if (f->op.chmod)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000709 res = f->op.chmod(path, attr->mode);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000710
711 return res;
Miklos Szeredie5183742005-02-02 11:14:04 +0000712}
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000713
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000714static int do_chown(struct fuse *f, const char *path, struct fuse_attr *attr,
Miklos Szeredi2e50d432001-12-20 12:17:25 +0000715 int valid)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000716{
717 int res;
718 uid_t uid = (valid & FATTR_UID) ? attr->uid : (uid_t) -1;
719 gid_t gid = (valid & FATTR_GID) ? attr->gid : (gid_t) -1;
Miklos Szeredie5183742005-02-02 11:14:04 +0000720
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000721 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000722 if (f->op.chown)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000723 res = f->op.chown(path, uid, gid);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000724
725 return res;
726}
727
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000728static int do_truncate(struct fuse *f, const char *path,
729 struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000730{
731 int res;
732
733 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000734 if (f->op.truncate)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000735 res = f->op.truncate(path, attr->size);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000736
737 return res;
738}
739
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000740static int do_utime(struct fuse *f, const char *path, struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000741{
742 int res;
743 struct utimbuf buf;
744 buf.actime = attr->atime;
745 buf.modtime = attr->mtime;
746 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000747 if (f->op.utime)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000748 res = f->op.utime(path, &buf);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000749
750 return res;
751}
752
Miklos Szeredi5e183482001-10-31 14:52:35 +0000753static void do_setattr(struct fuse *f, struct fuse_in_header *in,
754 struct fuse_setattr_in *arg)
755{
756 int res;
757 char *path;
758 int valid = arg->valid;
Miklos Szeredi30e093a2005-04-03 17:44:54 +0000759 struct fuse_attr *attr = MEMBER_COMPAT(f, arg, attr, fuse_setattr_in);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000760 struct fuse_attr_out outarg;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000761
762 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000763 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000764 if (path != NULL) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000765 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000766 if (f->op.getattr) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000767 res = 0;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000768 if (!res && (valid & FATTR_MODE))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000769 res = do_chmod(f, path, attr);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000770 if (!res && (valid & (FATTR_UID | FATTR_GID)))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000771 res = do_chown(f, path, attr, valid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000772 if (!res && (valid & FATTR_SIZE))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000773 res = do_truncate(f, path, attr);
Miklos Szeredie5183742005-02-02 11:14:04 +0000774 if (!res && (valid & (FATTR_ATIME | FATTR_MTIME)) ==
Miklos Szeredib5958612004-02-20 14:10:49 +0000775 (FATTR_ATIME | FATTR_MTIME))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000776 res = do_utime(f, path, attr);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000777 if (!res) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000778 struct stat buf;
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000779 res = f->op.getattr(path, &buf);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000780 if (!res) {
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000781 memset(&outarg, 0, sizeof(struct fuse_attr_out));
782 outarg.attr_valid = ATTR_REVALIDATE_TIME;
783 outarg.attr_valid_nsec = 0;
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000784 convert_stat(&buf, &outarg.attr);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000785 if (!(f->flags & FUSE_USE_INO))
786 outarg.attr.ino = in->nodeid;
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000787 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000788 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000789 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000790 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000791 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000792 send_reply(f, in, res, &outarg, sizeof(outarg));
Miklos Szeredi5e183482001-10-31 14:52:35 +0000793}
794
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000795static void do_readlink(struct fuse *f, struct fuse_in_header *in)
796{
797 int res;
798 char link[PATH_MAX + 1];
Miklos Szeredib483c932001-10-29 14:57:57 +0000799 char *path;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000800
Miklos Szeredi5e183482001-10-31 14:52:35 +0000801 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000802 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000803 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +0000804 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000805 if (f->op.readlink)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000806 res = f->op.readlink(path, link, sizeof(link));
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000807 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000808 }
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000809 link[PATH_MAX] = '\0';
Miklos Szeredi4b2bef42002-01-09 12:23:27 +0000810 send_reply(f, in, res, link, res == 0 ? strlen(link) : 0);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000811}
812
Miklos Szeredib483c932001-10-29 14:57:57 +0000813static void do_mknod(struct fuse *f, struct fuse_in_header *in,
814 struct fuse_mknod_in *inarg)
815{
816 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000817 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000818 char *path;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000819 char *name = PARAM(inarg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000820 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000821
Miklos Szeredi5e183482001-10-31 14:52:35 +0000822 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000823 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000824 if (path != NULL) {
825 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000826 printf("MKNOD %s\n", path);
827 fflush(stdout);
828 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000829 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000830 if (f->op.mknod && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000831 res = f->op.mknod(path, inarg->mode, inarg->rdev);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000832 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000833 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000834 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000835 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000836 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000837 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
838 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000839 forget_node(f, outarg.nodeid, in->unique);
Miklos Szeredib483c932001-10-29 14:57:57 +0000840}
841
842static void do_mkdir(struct fuse *f, struct fuse_in_header *in,
843 struct fuse_mkdir_in *inarg)
844{
845 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000846 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000847 char *path;
Miklos Szeredi30e093a2005-04-03 17:44:54 +0000848 char *name = PARAM_COMPAT(f, inarg, fuse_mkdir_in);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000849 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000850
Miklos Szeredi5e183482001-10-31 14:52:35 +0000851 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000852 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000853 if (path != NULL) {
854 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000855 printf("MKDIR %s\n", path);
856 fflush(stdout);
857 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000858 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000859 if (f->op.mkdir && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000860 res = f->op.mkdir(path, inarg->mode);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000861 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000862 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000863 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000864 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000865 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000866 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
867 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000868 forget_node(f, outarg.nodeid, in->unique);
Miklos Szeredib483c932001-10-29 14:57:57 +0000869}
870
Miklos Szeredib5958612004-02-20 14:10:49 +0000871static void do_unlink(struct fuse *f, struct fuse_in_header *in, char *name)
Miklos Szeredib483c932001-10-29 14:57:57 +0000872{
873 int res;
874 char *path;
875
Miklos Szeredi5e183482001-10-31 14:52:35 +0000876 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000877 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000878 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000879 if (f->flags & FUSE_DEBUG) {
880 printf("UNLINK %s\n", path);
881 fflush(stdout);
882 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000883 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000884 if (f->op.unlink) {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000885 if (!(f->flags & FUSE_HARD_REMOVE) && is_open(f, in->nodeid, name))
886 res = hide_node(f, path, in->nodeid, name);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000887 else {
888 res = f->op.unlink(path);
889 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000890 remove_node(f, in->nodeid, name);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000891
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000892 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000893 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000894 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000895 }
Miklos Szeredib5958612004-02-20 14:10:49 +0000896 send_reply(f, in, res, NULL, 0);
897}
898
899static void do_rmdir(struct fuse *f, struct fuse_in_header *in, char *name)
900{
901 int res;
902 char *path;
903
904 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000905 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000906 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000907 if (f->flags & FUSE_DEBUG) {
908 printf("RMDIR %s\n", path);
909 fflush(stdout);
910 }
Miklos Szeredib5958612004-02-20 14:10:49 +0000911 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000912 if (f->op.rmdir) {
Miklos Szeredib5958612004-02-20 14:10:49 +0000913 res = f->op.rmdir(path);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000914 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000915 remove_node(f, in->nodeid, name);
Miklos Szeredib5958612004-02-20 14:10:49 +0000916 }
917 free(path);
918 }
Miklos Szeredib483c932001-10-29 14:57:57 +0000919 send_reply(f, in, res, NULL, 0);
920}
921
922static void do_symlink(struct fuse *f, struct fuse_in_header *in, char *name,
923 char *link)
924{
925 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000926 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000927 char *path;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000928 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000929
Miklos Szeredi5e183482001-10-31 14:52:35 +0000930 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000931 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000932 if (path != NULL) {
933 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000934 printf("SYMLINK %s\n", path);
935 fflush(stdout);
936 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000937 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000938 if (f->op.symlink && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000939 res = f->op.symlink(link, path);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000940 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000941 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000942 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000943 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000944 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000945 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
946 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000947 forget_node(f, outarg.nodeid, in->unique);
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000948
Miklos Szeredib483c932001-10-29 14:57:57 +0000949}
950
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000951static void do_rename(struct fuse *f, struct fuse_in_header *in,
952 struct fuse_rename_in *inarg)
953{
954 int res;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000955 nodeid_t olddir = in->nodeid;
956 nodeid_t newdir = inarg->newdir;
Miklos Szeredi6bf8b682002-10-28 08:49:39 +0000957 char *oldname = PARAM(inarg);
958 char *newname = oldname + strlen(oldname) + 1;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000959 char *oldpath;
960 char *newpath;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000961
Miklos Szeredi5e183482001-10-31 14:52:35 +0000962 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000963 oldpath = get_path_name(f, olddir, oldname);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000964 if (oldpath != NULL) {
Miklos Szeredia181e612001-11-06 12:03:23 +0000965 newpath = get_path_name(f, newdir, newname);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000966 if (newpath != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000967 if (f->flags & FUSE_DEBUG) {
968 printf("RENAME %s -> %s\n", oldpath, newpath);
969 fflush(stdout);
970 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000971 res = -ENOSYS;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000972 if (f->op.rename) {
973 res = 0;
Miklos Szeredie5183742005-02-02 11:14:04 +0000974 if (!(f->flags & FUSE_HARD_REMOVE) &&
Miklos Szeredi2529ca22004-07-13 15:36:52 +0000975 is_open(f, newdir, newname))
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000976 res = hide_node(f, newpath, newdir, newname);
977 if (res == 0) {
978 res = f->op.rename(oldpath, newpath);
979 if (res == 0)
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000980 res = rename_node(f, olddir, oldname, newdir, newname, 0);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000981 }
982 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000983 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000984 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000985 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000986 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000987 send_reply(f, in, res, NULL, 0);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000988}
989
990static void do_link(struct fuse *f, struct fuse_in_header *in,
Miklos Szeredi5e183482001-10-31 14:52:35 +0000991 struct fuse_link_in *arg)
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000992{
993 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000994 int res2;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000995 char *oldpath;
996 char *newpath;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000997 char *name = PARAM(arg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000998 struct fuse_entry_out outarg;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000999
Miklos Szeredi5e183482001-10-31 14:52:35 +00001000 res = -ENOENT;
Miklos Szeredied6b5dd2005-01-26 17:07:59 +00001001 oldpath = get_path(f, arg->oldnodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001002 if (oldpath != NULL) {
Miklos Szeredied6b5dd2005-01-26 17:07:59 +00001003 newpath = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001004 if (newpath != NULL) {
1005 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +00001006 printf("LINK %s\n", newpath);
1007 fflush(stdout);
1008 }
Miklos Szeredi5e183482001-10-31 14:52:35 +00001009 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001010 if (f->op.link && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +00001011 res = f->op.link(oldpath, newpath);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001012 if (res == 0)
Miklos Szeredied6b5dd2005-01-26 17:07:59 +00001013 res = lookup_path(f, in->nodeid, in->unique, name,
Miklos Szeredi76f65782004-02-19 16:55:40 +00001014 newpath, &outarg);
1015 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001016 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001017 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001018 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001019 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +00001020 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
1021 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001022 forget_node(f, outarg.nodeid, in->unique);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001023}
1024
Miklos Szeredi5e183482001-10-31 14:52:35 +00001025static void do_open(struct fuse *f, struct fuse_in_header *in,
1026 struct fuse_open_in *arg)
1027{
1028 int res;
1029 char *path;
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001030 struct fuse_open_out outarg;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001031 struct fuse_file_info fi;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001032
Miklos Szeredied3c97c2005-02-15 17:04:50 +00001033 memset(&outarg, 0, sizeof(outarg));
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001034 memset(&fi, 0, sizeof(fi));
1035 fi.flags = arg->flags;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001036 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001037 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001038 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +00001039 res = -ENOSYS;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001040 if (f->op.open) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001041 if (!f->compat)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001042 res = f->op.open(path, &fi);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001043 else
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001044 res = ((struct fuse_operations_compat2 *) &f->op)->open(path, fi.flags);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001045 }
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +00001046 }
Miklos Szeredi73798f92004-07-12 15:55:11 +00001047 if (res == 0) {
1048 int res2;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001049 outarg.fh = fi.fh;
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001050 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001051 printf("OPEN[%lu] flags: 0x%x\n", fi.fh, arg->flags);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001052 fflush(stdout);
1053 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001054
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001055 pthread_mutex_lock(&f->lock);
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001056 res2 = send_reply(f, in, res, &outarg, SIZEOF_COMPAT(f, fuse_open_out));
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001057 if(res2 == -ENOENT) {
1058 /* The open syscall was interrupted, so it must be cancelled */
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001059 if(f->op.release) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001060 if (!f->compat)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001061 f->op.release(path, &fi);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001062 else
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001063 ((struct fuse_operations_compat2 *) &f->op)->release(path, fi.flags);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001064 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001065 } else {
1066 struct node *node = get_node(f, in->nodeid);
1067 node->open_count ++;
1068 }
Miklos Szeredi73798f92004-07-12 15:55:11 +00001069 pthread_mutex_unlock(&f->lock);
Miklos Szeredi73798f92004-07-12 15:55:11 +00001070 } else
1071 send_reply(f, in, res, NULL, 0);
1072
Miklos Szeredi9a31cca2004-06-26 21:11:25 +00001073 if (path)
1074 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001075}
1076
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001077static void do_flush(struct fuse *f, struct fuse_in_header *in,
1078 struct fuse_flush_in *arg)
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001079{
1080 char *path;
1081 int res;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001082 struct fuse_file_info fi;
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001083
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001084 memset(&fi, 0, sizeof(fi));
1085 fi.fh = arg->fh;
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001086 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001087 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001088 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001089 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001090 printf("FLUSH[%lu]\n", (unsigned long) arg->fh);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001091 fflush(stdout);
1092 }
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001093 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001094 if (f->op.flush)
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001095 res = f->op.flush(path, &fi);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001096 free(path);
1097 }
1098 send_reply(f, in, res, NULL, 0);
1099}
1100
Miklos Szeredi9478e862002-12-11 09:50:26 +00001101static void do_release(struct fuse *f, struct fuse_in_header *in,
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001102 struct fuse_release_in *arg)
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001103{
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001104 struct node *node;
1105 char *path;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001106 struct fuse_file_info fi;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001107 int unlink_hidden;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001108
1109 memset(&fi, 0, sizeof(fi));
1110 fi.flags = arg->flags;
1111 fi.fh = arg->fh;
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001112
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001113 pthread_mutex_lock(&f->lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001114 node = get_node(f, in->nodeid);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001115 assert(node->open_count > 0);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001116 --node->open_count;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001117 unlink_hidden = (node->is_hidden && !node->open_count);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001118 pthread_mutex_unlock(&f->lock);
1119
Miklos Szeredia13d9002004-11-02 17:32:03 +00001120 path = get_path(f, in->nodeid);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001121 if (f->flags & FUSE_DEBUG) {
1122 printf("RELEASE[%lu] flags: 0x%x\n", fi.fh, fi.flags);
1123 fflush(stdout);
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001124 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001125 if (f->op.release) {
1126 if (!f->compat)
1127 f->op.release(path ? path : "-", &fi);
1128 else if (path)
1129 ((struct fuse_operations_compat2 *) &f->op)->release(path, fi.flags);
1130 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001131
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001132 if(unlink_hidden && path)
1133 f->op.unlink(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001134
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001135 if (path)
1136 free(path);
1137
Miklos Szeredi556d03d2004-06-30 11:13:41 +00001138 send_reply(f, in, 0, NULL, 0);
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001139}
1140
Miklos Szeredi5e183482001-10-31 14:52:35 +00001141static void do_read(struct fuse *f, struct fuse_in_header *in,
1142 struct fuse_read_in *arg)
1143{
1144 int res;
1145 char *path;
Miklos Szerediab974562005-04-07 15:40:21 +00001146 size_t size;
1147 char *buf;
1148 struct fuse_file_info fi;
1149
1150 buf = (char *) malloc(arg->size);
1151 if (buf == NULL) {
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001152 send_reply(f, in, -ENOMEM, NULL, 0);
Miklos Szerediab974562005-04-07 15:40:21 +00001153 return;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001154 }
Miklos Szerediab974562005-04-07 15:40:21 +00001155
1156 memset(&fi, 0, sizeof(fi));
1157 fi.fh = arg->fh;
1158
1159 res = -ENOENT;
1160 path = get_path(f, in->nodeid);
1161 if (path != NULL) {
1162 if (f->flags & FUSE_DEBUG) {
1163 printf("READ[%lu] %u bytes from %llu\n",
1164 (unsigned long) arg->fh, arg->size, arg->offset);
1165 fflush(stdout);
1166 }
1167
1168 res = -ENOSYS;
1169 if (f->op.read)
1170 res = f->op.read(path, buf, arg->size, arg->offset, &fi);
1171 free(path);
1172 }
1173
1174 size = 0;
1175 if (res >= 0) {
1176 size = res;
1177 res = 0;
1178 if (f->flags & FUSE_DEBUG) {
1179 printf(" READ[%lu] %u bytes\n", (unsigned long) arg->fh,
1180 size);
1181 fflush(stdout);
1182 }
1183 }
1184
1185 send_reply(f, in, res, buf, size);
1186 free(buf);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001187}
Miklos Szeredib483c932001-10-29 14:57:57 +00001188
Miklos Szeredia181e612001-11-06 12:03:23 +00001189static void do_write(struct fuse *f, struct fuse_in_header *in,
1190 struct fuse_write_in *arg)
1191{
1192 int res;
1193 char *path;
Miklos Szerediad051c32004-07-02 09:22:50 +00001194 struct fuse_write_out outarg;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001195 struct fuse_file_info fi;
1196
1197 memset(&fi, 0, sizeof(fi));
1198 fi.fh = arg->fh;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001199 fi.writepage = arg->write_flags & 1;
Miklos Szeredia181e612001-11-06 12:03:23 +00001200
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001201 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001202 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001203 if (path != NULL) {
1204 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi1eea0322004-09-27 18:50:11 +00001205 printf("WRITE%s[%lu] %u bytes to %llu\n",
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001206 (arg->write_flags & 1) ? "PAGE" : "",
1207 (unsigned long) arg->fh, arg->size, arg->offset);
Miklos Szeredi6ebe2342002-01-06 21:44:16 +00001208 fflush(stdout);
1209 }
1210
Miklos Szeredia181e612001-11-06 12:03:23 +00001211 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001212 if (f->op.write)
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001213 res = f->op.write(path, PARAM(arg), arg->size, arg->offset, &fi);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001214 free(path);
Miklos Szeredia181e612001-11-06 12:03:23 +00001215 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001216
Miklos Szerediab974562005-04-07 15:40:21 +00001217 memset(&outarg, 0, sizeof(outarg));
Miklos Szeredie5183742005-02-02 11:14:04 +00001218 if (res >= 0) {
Miklos Szerediad051c32004-07-02 09:22:50 +00001219 outarg.size = res;
1220 res = 0;
Miklos Szeredia181e612001-11-06 12:03:23 +00001221 }
1222
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001223 send_reply(f, in, res, &outarg, SIZEOF_COMPAT(f, fuse_write_out));
Miklos Szeredia181e612001-11-06 12:03:23 +00001224}
1225
Miklos Szeredi77f39942004-03-25 11:17:52 +00001226static int default_statfs(struct statfs *buf)
1227{
1228 buf->f_namelen = 255;
1229 buf->f_bsize = 512;
1230 return 0;
1231}
1232
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001233static void convert_statfs_compat(struct fuse_statfs_compat1 *compatbuf,
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001234 struct statfs *statfs)
1235{
1236 statfs->f_bsize = compatbuf->block_size;
1237 statfs->f_blocks = compatbuf->blocks;
1238 statfs->f_bfree = compatbuf->blocks_free;
1239 statfs->f_bavail = compatbuf->blocks_free;
1240 statfs->f_files = compatbuf->files;
1241 statfs->f_ffree = compatbuf->files_free;
1242 statfs->f_namelen = compatbuf->namelen;
1243}
1244
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001245static void convert_statfs(struct statfs *statfs, struct fuse_kstatfs *kstatfs)
1246{
1247 kstatfs->bsize = statfs->f_bsize;
1248 kstatfs->blocks = statfs->f_blocks;
1249 kstatfs->bfree = statfs->f_bfree;
1250 kstatfs->bavail = statfs->f_bavail;
1251 kstatfs->files = statfs->f_files;
1252 kstatfs->ffree = statfs->f_ffree;
1253 kstatfs->namelen = statfs->f_namelen;
1254}
1255
Mark Glinesd84b39a2002-01-07 16:32:02 +00001256static void do_statfs(struct fuse *f, struct fuse_in_header *in)
1257{
1258 int res;
Mark Glinesd84b39a2002-01-07 16:32:02 +00001259 struct fuse_statfs_out arg;
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001260 struct statfs buf;
Mark Glinesd84b39a2002-01-07 16:32:02 +00001261
Miklos Szeredi77f39942004-03-25 11:17:52 +00001262 memset(&buf, 0, sizeof(struct statfs));
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001263 if (f->op.statfs) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001264 if (!f->compat || f->compat > 11)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001265 res = f->op.statfs("/", &buf);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001266 else {
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001267 struct fuse_statfs_compat1 compatbuf;
1268 memset(&compatbuf, 0, sizeof(struct fuse_statfs_compat1));
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001269 res = ((struct fuse_operations_compat1 *) &f->op)->statfs(&compatbuf);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001270 if (res == 0)
1271 convert_statfs_compat(&compatbuf, &buf);
1272 }
1273 }
Miklos Szeredi77f39942004-03-25 11:17:52 +00001274 else
1275 res = default_statfs(&buf);
1276
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001277 if (res == 0)
Miklos Szeredi77f39942004-03-25 11:17:52 +00001278 convert_statfs(&buf, &arg.st);
Miklos Szeredi4b2bef42002-01-09 12:23:27 +00001279
Mark Glinesd84b39a2002-01-07 16:32:02 +00001280 send_reply(f, in, res, &arg, sizeof(arg));
1281}
1282
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001283static void do_fsync(struct fuse *f, struct fuse_in_header *in,
1284 struct fuse_fsync_in *inarg)
1285{
1286 int res;
1287 char *path;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001288 struct fuse_file_info fi;
1289
1290 memset(&fi, 0, sizeof(fi));
1291 fi.fh = inarg->fh;
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001292
1293 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001294 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001295 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001296 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001297 printf("FSYNC[%lu]\n", (unsigned long) inarg->fh);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001298 fflush(stdout);
1299 }
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001300 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001301 if (f->op.fsync)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001302 res = f->op.fsync(path, inarg->fsync_flags & 1, &fi);
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001303 free(path);
1304 }
1305 send_reply(f, in, res, NULL, 0);
1306}
1307
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001308static void do_setxattr(struct fuse *f, struct fuse_in_header *in,
1309 struct fuse_setxattr_in *arg)
1310{
1311 int res;
1312 char *path;
1313 char *name = PARAM(arg);
1314 unsigned char *value = name + strlen(name) + 1;
1315
1316 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001317 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001318 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001319 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001320 if (f->op.setxattr)
1321 res = f->op.setxattr(path, name, value, arg->size, arg->flags);
1322 free(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001323 }
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001324 send_reply(f, in, res, NULL, 0);
1325}
1326
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001327static int common_getxattr(struct fuse *f, struct fuse_in_header *in,
1328 const char *name, char *value, size_t size)
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001329{
1330 int res;
1331 char *path;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001332
1333 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001334 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001335 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001336 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001337 if (f->op.getxattr)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001338 res = f->op.getxattr(path, name, value, size);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001339 free(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001340 }
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001341 return res;
1342}
1343
1344static void do_getxattr_read(struct fuse *f, struct fuse_in_header *in,
1345 const char *name, size_t size)
1346{
1347 int res;
Miklos Szerediab974562005-04-07 15:40:21 +00001348 char *value = (char *) malloc(size);
1349 if (value == NULL) {
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001350 send_reply(f, in, -ENOMEM, NULL, 0);
Miklos Szerediab974562005-04-07 15:40:21 +00001351 return;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001352 }
Miklos Szerediab974562005-04-07 15:40:21 +00001353 res = common_getxattr(f, in, name, value, size);
1354 size = 0;
1355 if (res > 0) {
1356 size = res;
1357 res = 0;
1358 }
1359 send_reply(f, in, res, value, size);
1360 free(value);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001361}
1362
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001363static void do_getxattr_size(struct fuse *f, struct fuse_in_header *in,
1364 const char *name)
1365{
1366 int res;
1367 struct fuse_getxattr_out arg;
1368
Miklos Szerediab974562005-04-07 15:40:21 +00001369 memset(&arg, 0, sizeof(arg));
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001370 res = common_getxattr(f, in, name, NULL, 0);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001371 if (res >= 0) {
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001372 arg.size = res;
1373 res = 0;
1374 }
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001375 send_reply(f, in, res, &arg, SIZEOF_COMPAT(f, fuse_getxattr_out));
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001376}
1377
1378static void do_getxattr(struct fuse *f, struct fuse_in_header *in,
1379 struct fuse_getxattr_in *arg)
1380{
1381 char *name = PARAM(arg);
Miklos Szeredie5183742005-02-02 11:14:04 +00001382
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001383 if (arg->size)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001384 do_getxattr_read(f, in, name, arg->size);
1385 else
1386 do_getxattr_size(f, in, name);
1387}
1388
1389static int common_listxattr(struct fuse *f, struct fuse_in_header *in,
1390 char *list, size_t size)
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001391{
1392 int res;
1393 char *path;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001394
1395 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001396 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001397 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001398 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001399 if (f->op.listxattr)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001400 res = f->op.listxattr(path, list, size);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001401 free(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001402 }
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001403 return res;
1404}
1405
1406static void do_listxattr_read(struct fuse *f, struct fuse_in_header *in,
1407 size_t size)
1408{
1409 int res;
Miklos Szerediab974562005-04-07 15:40:21 +00001410 char *list = (char *) malloc(size);
1411 if (list == NULL) {
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001412 send_reply(f, in, -ENOMEM, NULL, 0);
Miklos Szerediab974562005-04-07 15:40:21 +00001413 return;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001414 }
Miklos Szerediab974562005-04-07 15:40:21 +00001415 res = common_listxattr(f, in, list, size);
1416 size = 0;
1417 if (res > 0) {
1418 size = res;
1419 res = 0;
1420 }
1421 send_reply(f, in, res, list, size);
1422 free(list);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001423}
1424
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001425static void do_listxattr_size(struct fuse *f, struct fuse_in_header *in)
1426{
1427 int res;
1428 struct fuse_getxattr_out arg;
1429
Miklos Szerediab974562005-04-07 15:40:21 +00001430 memset(&arg, 0, sizeof(arg));
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001431 res = common_listxattr(f, in, NULL, 0);
1432 if (res >= 0) {
1433 arg.size = res;
1434 res = 0;
1435 }
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001436 send_reply(f, in, res, &arg, SIZEOF_COMPAT(f, fuse_getxattr_out));
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001437}
1438
1439static void do_listxattr(struct fuse *f, struct fuse_in_header *in,
1440 struct fuse_getxattr_in *arg)
1441{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001442 if (arg->size)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001443 do_listxattr_read(f, in, arg->size);
1444 else
1445 do_listxattr_size(f, in);
1446}
1447
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001448static void do_removexattr(struct fuse *f, struct fuse_in_header *in,
1449 char *name)
1450{
1451 int res;
1452 char *path;
1453
1454 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001455 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001456 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001457 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001458 if (f->op.removexattr)
1459 res = f->op.removexattr(path, name);
1460 free(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001461 }
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001462 send_reply(f, in, res, NULL, 0);
1463}
1464
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001465static void do_init(struct fuse *f, struct fuse_in_header *in,
1466 struct fuse_init_in_out *arg)
1467{
1468 struct fuse_init_in_out outarg;
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001469
1470 if (in->padding == 5) {
1471 arg->minor = arg->major;
1472 arg->major = in->padding;
1473 }
1474
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001475 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001476 printf("INIT: %u.%u\n", arg->major, arg->minor);
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001477 fflush(stdout);
1478 }
1479 f->got_init = 1;
Miklos Szeredi159bd7e2005-02-28 17:32:16 +00001480 if (f->op.init)
1481 f->user_data = f->op.init();
1482
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001483 if (arg->major == 5) {
1484 f->major = 5;
1485 f->minor = 1;
1486 } else {
1487 f->major = FUSE_KERNEL_VERSION;
1488 f->minor = FUSE_KERNEL_MINOR_VERSION;
1489 }
1490 memset(&outarg, 0, sizeof(outarg));
1491 outarg.major = f->major;
1492 outarg.minor = f->minor;
1493
1494 if (f->flags & FUSE_DEBUG) {
1495 printf(" INIT: %u.%u\n", outarg.major, outarg.minor);
1496 fflush(stdout);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001497 }
1498
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001499 send_reply(f, in, 0, &outarg, sizeof(outarg));
1500}
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001501
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001502static struct fuse_dirhandle *get_dirhandle(unsigned long fh)
1503{
1504 return (struct fuse_dirhandle *) fh;
1505}
1506
1507static void do_opendir(struct fuse *f, struct fuse_in_header *in,
1508 struct fuse_open_in *arg)
1509{
1510 int res;
1511 struct fuse_open_out outarg;
1512 struct fuse_dirhandle *dh;
1513
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001514 dh = (struct fuse_dirhandle *) malloc(sizeof(struct fuse_dirhandle));
1515 if (dh == NULL) {
1516 send_reply(f, in, -ENOMEM, NULL, 0);
1517 return;
1518 }
1519 memset(dh, 0, sizeof(struct fuse_dirhandle));
1520 dh->fuse = f;
1521 dh->contents = NULL;
1522 dh->len = 0;
1523 dh->filled = 0;
Miklos Szerediab974562005-04-07 15:40:21 +00001524 mutex_init(&dh->lock);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001525
1526 memset(&outarg, 0, sizeof(outarg));
1527 outarg.fh = (unsigned long) dh;
1528
Miklos Szeredif43f0632005-02-28 11:46:56 +00001529 if (f->op.opendir) {
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001530 struct fuse_file_info fi;
Miklos Szeredif43f0632005-02-28 11:46:56 +00001531 char *path;
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001532
1533 memset(&fi, 0, sizeof(fi));
1534 fi.flags = arg->flags;
1535
Miklos Szeredif43f0632005-02-28 11:46:56 +00001536 res = -ENOENT;
1537 path = get_path(f, in->nodeid);
1538 if (path != NULL) {
Miklos Szeredif43f0632005-02-28 11:46:56 +00001539 res = f->op.opendir(path, &fi);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001540 dh->fh = fi.fh;
Miklos Szeredif43f0632005-02-28 11:46:56 +00001541 }
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001542 if (res == 0) {
1543 int res2;
1544 pthread_mutex_lock(&f->lock);
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001545 res2 = send_reply(f, in, res, &outarg, SIZEOF_COMPAT(f, fuse_open_out));
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001546 if(res2 == -ENOENT) {
1547 /* The opendir syscall was interrupted, so it must be
1548 cancelled */
1549 if(f->op.releasedir)
1550 f->op.releasedir(path, &fi);
Miklos Szerediab974562005-04-07 15:40:21 +00001551 pthread_mutex_destroy(&dh->lock);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001552 free(dh);
1553 }
1554 pthread_mutex_unlock(&f->lock);
1555 } else {
Miklos Szeredif43f0632005-02-28 11:46:56 +00001556 send_reply(f, in, res, NULL, 0);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001557 free(dh);
Miklos Szeredif43f0632005-02-28 11:46:56 +00001558 }
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001559 free(path);
Miklos Szerediab974562005-04-07 15:40:21 +00001560 } else
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001561 send_reply(f, in, 0, &outarg, SIZEOF_COMPAT(f, fuse_open_out));
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001562}
1563
Miklos Szerediab974562005-04-07 15:40:21 +00001564static int fill_dir_common(struct fuse_dirhandle *dh, const char *name,
1565 int type, ino_t ino, off_t off)
1566{
1567 unsigned namelen = strlen(name);
1568 unsigned entlen;
1569 unsigned entsize;
1570 unsigned padlen;
1571 unsigned newlen;
1572 unsigned char *newptr;
1573
1574 if (!(dh->fuse->flags & FUSE_USE_INO))
1575 ino = (ino_t) -1;
1576
1577 if (namelen > FUSE_NAME_MAX)
1578 namelen = FUSE_NAME_MAX;
1579 else if (!namelen) {
1580 dh->error = -EIO;
1581 return 1;
1582 }
1583
1584 entlen = (dh->fuse->major == 5 ?
1585 FUSE_NAME_OFFSET_COMPAT5 : FUSE_NAME_OFFSET) + namelen;
1586 entsize = FUSE_DIRENT_ALIGN(entlen);
1587 padlen = entsize - entlen;
1588 newlen = dh->len + entsize;
1589 if (off && dh->fuse->major != 5) {
1590 dh->filled = 0;
1591 if (newlen > dh->needlen)
1592 return 1;
1593 }
1594
1595 newptr = realloc(dh->contents, newlen);
1596 if (!newptr) {
1597 dh->error = -ENOMEM;
1598 return 1;
1599 }
1600 dh->contents = newptr;
1601 if (dh->fuse->major == 5) {
1602 struct fuse_dirent_compat5 *dirent;
1603 dirent = (struct fuse_dirent_compat5 *) (dh->contents + dh->len);
1604 dirent->ino = ino;
1605 dirent->namelen = namelen;
1606 dirent->type = type;
1607 strncpy(dirent->name, name, namelen);
1608 } else {
1609 struct fuse_dirent *dirent;
1610 dirent = (struct fuse_dirent *) (dh->contents + dh->len);
1611 dirent->ino = ino;
1612 dirent->off = off ? off : newlen;
1613 dirent->namelen = namelen;
1614 dirent->type = type;
1615 strncpy(dirent->name, name, namelen);
1616 }
1617 if (padlen)
1618 memset(dh->contents + dh->len + entlen, 0, padlen);
1619 dh->len = newlen;
1620 return 0;
1621}
1622
1623static int fill_dir(void *buf, const char *name, const struct stat *stat,
1624 off_t off)
1625{
1626 int type = stat ? (stat->st_mode & 0170000) >> 12 : 0;
1627 ino_t ino = stat ? stat->st_ino : (ino_t) -1;
1628 return fill_dir_common(buf, name, type, ino, off);
1629}
1630
1631static int fill_dir_old(struct fuse_dirhandle *dh, const char *name, int type,
1632 ino_t ino)
1633{
1634 fill_dir_common(dh, name, type, ino, 0);
1635 return dh->error;
1636}
1637
1638static int readdir_fill(struct fuse *f, struct fuse_in_header *in,
1639 struct fuse_read_in *arg, struct fuse_dirhandle *dh)
1640{
1641 int err = -ENOENT;
1642 char *path = get_path(f, in->nodeid);
1643 if (path != NULL) {
1644 struct fuse_file_info fi;
1645
1646 memset(&fi, 0, sizeof(fi));
1647 fi.fh = dh->fh;
1648
1649 dh->len = 0;
1650 dh->error = 0;
1651 dh->needlen = arg->size;
1652 dh->filled = 1;
1653 err = -ENOSYS;
1654 if (f->op.readdir) {
1655 off_t offset = f->major == 5 ? 0 : arg->offset;
1656 err = f->op.readdir(path, dh, fill_dir, offset, &fi);
1657 } else if (f->op.getdir)
1658 err = f->op.getdir(path, dh, fill_dir_old);
1659 if (!err)
1660 err = dh->error;
1661 if (err)
1662 dh->filled = 0;
1663 free(path);
1664 }
1665 return err;
1666}
1667
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001668static void do_readdir(struct fuse *f, struct fuse_in_header *in,
1669 struct fuse_read_in *arg)
1670{
Miklos Szerediab974562005-04-07 15:40:21 +00001671 int err = 0;
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001672 struct fuse_dirhandle *dh = get_dirhandle(arg->fh);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001673 size_t size = 0;
Miklos Szerediab974562005-04-07 15:40:21 +00001674 unsigned char *buf = NULL;
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001675
Miklos Szerediab974562005-04-07 15:40:21 +00001676 pthread_mutex_lock(&dh->lock);
1677 if (!dh->filled) {
1678 err = readdir_fill(f, in, arg, dh);
1679 if (err)
1680 goto out;
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001681 }
Miklos Szerediab974562005-04-07 15:40:21 +00001682 if (dh->filled) {
Miklos Szeredib92d9782005-02-07 16:10:49 +00001683 if (arg->offset < dh->len) {
1684 size = arg->size;
1685 if (arg->offset + size > dh->len)
1686 size = dh->len - arg->offset;
Miklos Szerediab974562005-04-07 15:40:21 +00001687 buf = dh->contents + arg->offset;
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001688 }
Miklos Szerediab974562005-04-07 15:40:21 +00001689 } else {
1690 size = dh->len;
1691 buf = dh->contents;
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001692 }
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001693
Miklos Szerediab974562005-04-07 15:40:21 +00001694 out:
1695 send_reply(f, in, err, buf, size);
1696 pthread_mutex_unlock(&dh->lock);
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001697}
1698
1699static void do_releasedir(struct fuse *f, struct fuse_in_header *in,
1700 struct fuse_release_in *arg)
1701{
1702 struct fuse_dirhandle *dh = get_dirhandle(arg->fh);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001703 if (f->op.releasedir) {
1704 char *path;
1705 struct fuse_file_info fi;
Miklos Szerediab974562005-04-07 15:40:21 +00001706
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001707 memset(&fi, 0, sizeof(fi));
1708 fi.fh = dh->fh;
1709
1710 path = get_path(f, in->nodeid);
1711 f->op.releasedir(path ? path : "-", &fi);
1712 free(path);
1713 }
Miklos Szerediab974562005-04-07 15:40:21 +00001714 pthread_mutex_lock(&dh->lock);
1715 pthread_mutex_unlock(&dh->lock);
1716 pthread_mutex_destroy(&dh->lock);
Miklos Szeredib92d9782005-02-07 16:10:49 +00001717 free(dh->contents);
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001718 free(dh);
1719 send_reply(f, in, 0, NULL, 0);
1720}
1721
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001722static void do_fsyncdir(struct fuse *f, struct fuse_in_header *in,
1723 struct fuse_fsync_in *inarg)
1724{
1725 int res;
1726 char *path;
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001727 struct fuse_dirhandle *dh = get_dirhandle(inarg->fh);
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001728 struct fuse_file_info fi;
1729
1730 memset(&fi, 0, sizeof(fi));
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001731 fi.fh = dh->fh;
1732
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001733 res = -ENOENT;
1734 path = get_path(f, in->nodeid);
1735 if (path != NULL) {
1736 res = -ENOSYS;
1737 if (f->op.fsyncdir)
1738 res = f->op.fsyncdir(path, inarg->fsync_flags & 1, &fi);
1739 free(path);
1740 }
1741 send_reply(f, in, res, NULL, 0);
1742}
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001743
Miklos Szeredi43696432001-11-18 19:15:05 +00001744static void free_cmd(struct fuse_cmd *cmd)
1745{
1746 free(cmd->buf);
1747 free(cmd);
1748}
1749
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001750void fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd)
Miklos Szeredia181e612001-11-06 12:03:23 +00001751{
Miklos Szeredia181e612001-11-06 12:03:23 +00001752 struct fuse_in_header *in = (struct fuse_in_header *) cmd->buf;
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001753 void *inarg = cmd->buf + SIZEOF_COMPAT(f, fuse_in_header);
Miklos Szeredid169f312004-09-22 08:48:26 +00001754 struct fuse_context *ctx = fuse_get_context();
Miklos Szeredia181e612001-11-06 12:03:23 +00001755
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001756 fuse_dec_avail(f);
Miklos Szeredi33232032001-11-19 17:55:51 +00001757
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001758 if ((f->flags & FUSE_DEBUG)) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001759 printf("unique: %llu, opcode: %s (%i), nodeid: %lu, insize: %i\n",
1760 in->unique, opname(in->opcode), in->opcode,
1761 (unsigned long) in->nodeid, cmd->buflen);
Miklos Szeredic0938ea2001-11-07 12:35:06 +00001762 fflush(stdout);
1763 }
Miklos Szeredife25def2001-12-20 15:38:05 +00001764
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001765 if (!f->got_init && in->opcode != FUSE_INIT) {
1766 /* Old kernel version probably */
1767 send_reply(f, in, -EPROTO, NULL, 0);
1768 goto out;
1769 }
1770
Miklos Szeredi0111f9d2005-04-22 12:04:55 +00001771 if ((f->flags & FUSE_ALLOW_ROOT) && in->uid != f->owner && in->uid != 0 &&
1772 in->opcode != FUSE_INIT && in->opcode != FUSE_READ &&
1773 in->opcode != FUSE_WRITE && in->opcode != FUSE_FSYNC &&
1774 in->opcode != FUSE_RELEASE && in->opcode != FUSE_READDIR &&
1775 in->opcode != FUSE_FSYNCDIR && in->opcode != FUSE_RELEASEDIR) {
1776 send_reply(f, in, -EACCES, NULL, 0);
1777 goto out;
1778 }
1779
Miklos Szeredid169f312004-09-22 08:48:26 +00001780 ctx->fuse = f;
Miklos Szeredife25def2001-12-20 15:38:05 +00001781 ctx->uid = in->uid;
1782 ctx->gid = in->gid;
Miklos Szeredi1f18db52004-09-27 06:54:49 +00001783 ctx->pid = in->pid;
Miklos Szeredi159bd7e2005-02-28 17:32:16 +00001784 ctx->private_data = f->user_data;
Miklos Szeredie5183742005-02-02 11:14:04 +00001785
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001786 switch (in->opcode) {
Miklos Szeredia181e612001-11-06 12:03:23 +00001787 case FUSE_LOOKUP:
1788 do_lookup(f, in, (char *) inarg);
1789 break;
1790
Miklos Szeredia181e612001-11-06 12:03:23 +00001791 case FUSE_GETATTR:
1792 do_getattr(f, in);
1793 break;
1794
1795 case FUSE_SETATTR:
1796 do_setattr(f, in, (struct fuse_setattr_in *) inarg);
1797 break;
1798
1799 case FUSE_READLINK:
1800 do_readlink(f, in);
1801 break;
1802
Miklos Szeredia181e612001-11-06 12:03:23 +00001803 case FUSE_MKNOD:
1804 do_mknod(f, in, (struct fuse_mknod_in *) inarg);
1805 break;
Miklos Szeredie5183742005-02-02 11:14:04 +00001806
Miklos Szeredia181e612001-11-06 12:03:23 +00001807 case FUSE_MKDIR:
1808 do_mkdir(f, in, (struct fuse_mkdir_in *) inarg);
1809 break;
Miklos Szeredie5183742005-02-02 11:14:04 +00001810
Miklos Szeredia181e612001-11-06 12:03:23 +00001811 case FUSE_UNLINK:
Miklos Szeredib5958612004-02-20 14:10:49 +00001812 do_unlink(f, in, (char *) inarg);
1813 break;
1814
Miklos Szeredia181e612001-11-06 12:03:23 +00001815 case FUSE_RMDIR:
Miklos Szeredib5958612004-02-20 14:10:49 +00001816 do_rmdir(f, in, (char *) inarg);
Miklos Szeredia181e612001-11-06 12:03:23 +00001817 break;
1818
1819 case FUSE_SYMLINK:
Miklos Szeredie5183742005-02-02 11:14:04 +00001820 do_symlink(f, in, (char *) inarg,
Miklos Szeredia181e612001-11-06 12:03:23 +00001821 ((char *) inarg) + strlen((char *) inarg) + 1);
1822 break;
1823
1824 case FUSE_RENAME:
1825 do_rename(f, in, (struct fuse_rename_in *) inarg);
1826 break;
Miklos Szeredie5183742005-02-02 11:14:04 +00001827
Miklos Szeredia181e612001-11-06 12:03:23 +00001828 case FUSE_LINK:
1829 do_link(f, in, (struct fuse_link_in *) inarg);
1830 break;
1831
1832 case FUSE_OPEN:
1833 do_open(f, in, (struct fuse_open_in *) inarg);
1834 break;
1835
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001836 case FUSE_FLUSH:
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001837 do_flush(f, in, (struct fuse_flush_in *) inarg);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001838 break;
1839
Miklos Szeredi9478e862002-12-11 09:50:26 +00001840 case FUSE_RELEASE:
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001841 do_release(f, in, (struct fuse_release_in *) inarg);
Miklos Szeredi9478e862002-12-11 09:50:26 +00001842 break;
1843
Miklos Szeredia181e612001-11-06 12:03:23 +00001844 case FUSE_READ:
1845 do_read(f, in, (struct fuse_read_in *) inarg);
1846 break;
1847
1848 case FUSE_WRITE:
1849 do_write(f, in, (struct fuse_write_in *) inarg);
1850 break;
1851
Mark Glinesd84b39a2002-01-07 16:32:02 +00001852 case FUSE_STATFS:
1853 do_statfs(f, in);
1854 break;
1855
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001856 case FUSE_FSYNC:
1857 do_fsync(f, in, (struct fuse_fsync_in *) inarg);
1858 break;
1859
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001860 case FUSE_SETXATTR:
1861 do_setxattr(f, in, (struct fuse_setxattr_in *) inarg);
1862 break;
1863
1864 case FUSE_GETXATTR:
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001865 do_getxattr(f, in, (struct fuse_getxattr_in *) inarg);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001866 break;
1867
1868 case FUSE_LISTXATTR:
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001869 do_listxattr(f, in, (struct fuse_getxattr_in *) inarg);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001870 break;
1871
1872 case FUSE_REMOVEXATTR:
1873 do_removexattr(f, in, (char *) inarg);
1874 break;
1875
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001876 case FUSE_INIT:
1877 do_init(f, in, (struct fuse_init_in_out *) inarg);
1878 break;
1879
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001880 case FUSE_OPENDIR:
1881 do_opendir(f, in, (struct fuse_open_in *) inarg);
1882 break;
1883
1884 case FUSE_READDIR:
1885 do_readdir(f, in, (struct fuse_read_in *) inarg);
1886 break;
1887
1888 case FUSE_RELEASEDIR:
1889 do_releasedir(f, in, (struct fuse_release_in *) inarg);
1890 break;
1891
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001892 case FUSE_FSYNCDIR:
1893 do_fsyncdir(f, in, (struct fuse_fsync_in *) inarg);
1894 break;
1895
Miklos Szeredia181e612001-11-06 12:03:23 +00001896 default:
Miklos Szeredi43696432001-11-18 19:15:05 +00001897 send_reply(f, in, -ENOSYS, NULL, 0);
Miklos Szeredia181e612001-11-06 12:03:23 +00001898 }
Miklos Szeredi43696432001-11-18 19:15:05 +00001899
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001900 out:
Miklos Szeredi43696432001-11-18 19:15:05 +00001901 free_cmd(cmd);
Miklos Szeredia181e612001-11-06 12:03:23 +00001902}
1903
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001904int fuse_exited(struct fuse* f)
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001905{
1906 return f->exited;
1907}
1908
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001909struct fuse_cmd *fuse_read_cmd(struct fuse *f)
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001910{
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001911 ssize_t res;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001912 struct fuse_cmd *cmd;
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001913 struct fuse_in_header *in;
1914 void *inarg;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001915
Miklos Szeredi43696432001-11-18 19:15:05 +00001916 cmd = (struct fuse_cmd *) malloc(sizeof(struct fuse_cmd));
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001917 if (cmd == NULL) {
1918 fprintf(stderr, "fuse: failed to allocate cmd in read\n");
1919 return NULL;
1920 }
Miklos Szeredi43696432001-11-18 19:15:05 +00001921 cmd->buf = (char *) malloc(FUSE_MAX_IN);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001922 if (cmd->buf == NULL) {
1923 fprintf(stderr, "fuse: failed to allocate read buffer\n");
1924 free(cmd);
1925 return NULL;
1926 }
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001927 in = (struct fuse_in_header *) cmd->buf;
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001928 inarg = cmd->buf + SIZEOF_COMPAT(f, fuse_in_header);
Miklos Szeredi43696432001-11-18 19:15:05 +00001929
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001930 res = read(f->fd, cmd->buf, FUSE_MAX_IN);
1931 if (res == -1) {
1932 free_cmd(cmd);
Miklos Szeredie56818b2004-12-12 11:45:24 +00001933 if (fuse_exited(f) || errno == EINTR || errno == ENOENT)
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001934 return NULL;
Miklos Szeredie5183742005-02-02 11:14:04 +00001935
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001936 /* ENODEV means we got unmounted, so we silenty return failure */
1937 if (errno != ENODEV) {
1938 /* BAD... This will happen again */
1939 perror("fuse: reading device");
1940 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001941
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001942 fuse_exit(f);
1943 return NULL;
1944 }
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001945 if ((size_t) res < SIZEOF_COMPAT(f, fuse_in_header)) {
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001946 free_cmd(cmd);
1947 /* Cannot happen */
1948 fprintf(stderr, "short read on fuse device\n");
1949 fuse_exit(f);
1950 return NULL;
1951 }
1952 cmd->buflen = res;
Miklos Szeredie5183742005-02-02 11:14:04 +00001953
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001954 /* Forget is special, it can be done without messing with threads. */
1955 if (in->opcode == FUSE_FORGET) {
1956 do_forget(f, in, (struct fuse_forget_in *) inarg);
1957 free_cmd(cmd);
1958 return NULL;
1959 }
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001960
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001961 return cmd;
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001962}
1963
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001964int fuse_loop(struct fuse *f)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001965{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001966 if (f == NULL)
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001967 return -1;
Miklos Szeredic40748a2004-02-20 16:38:45 +00001968
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001969 while (1) {
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001970 struct fuse_cmd *cmd;
1971
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001972 if (fuse_exited(f))
Miklos Szeredi874e3c12004-11-01 23:15:20 +00001973 break;
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001974
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001975 cmd = fuse_read_cmd(f);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001976 if (cmd == NULL)
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001977 continue;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001978
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001979 fuse_process_cmd(f, cmd);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001980 }
Miklos Szeredi874e3c12004-11-01 23:15:20 +00001981 f->exited = 0;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001982 return 0;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001983}
1984
Miklos Szeredi891b8742004-07-29 09:27:49 +00001985int fuse_invalidate(struct fuse *f, const char *path)
1986{
Miklos Szeredie56818b2004-12-12 11:45:24 +00001987 (void) f;
1988 (void) path;
1989 return -EINVAL;
Miklos Szeredi891b8742004-07-29 09:27:49 +00001990}
1991
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001992void fuse_exit(struct fuse *f)
1993{
1994 f->exited = 1;
1995}
1996
Miklos Szeredid169f312004-09-22 08:48:26 +00001997struct fuse_context *fuse_get_context()
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001998{
Miklos Szeredid169f312004-09-22 08:48:26 +00001999 static struct fuse_context context;
2000 if (fuse_getcontext)
2001 return fuse_getcontext();
Miklos Szeredi2e50d432001-12-20 12:17:25 +00002002 else
Miklos Szeredid169f312004-09-22 08:48:26 +00002003 return &context;
2004}
2005
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002006void fuse_set_getcontext_func(struct fuse_context *(*func)(void))
Miklos Szeredid169f312004-09-22 08:48:26 +00002007{
2008 fuse_getcontext = func;
Miklos Szeredi2e50d432001-12-20 12:17:25 +00002009}
2010
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002011int fuse_is_lib_option(const char *opt)
2012{
2013 if (strcmp(opt, "debug") == 0 ||
Miklos Szeredia13d9002004-11-02 17:32:03 +00002014 strcmp(opt, "hard_remove") == 0 ||
Miklos Szeredi0111f9d2005-04-22 12:04:55 +00002015 strcmp(opt, "use_ino") == 0 ||
2016 strcmp(opt, "allow_root") == 0)
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002017 return 1;
2018 else
2019 return 0;
2020}
2021
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002022static int parse_lib_opts(struct fuse *f, const char *opts)
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002023{
2024 if (opts) {
2025 char *xopts = strdup(opts);
2026 char *s = xopts;
2027 char *opt;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002028
Miklos Szeredie56818b2004-12-12 11:45:24 +00002029 if (xopts == NULL) {
2030 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002031 return -1;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002032 }
Miklos Szeredie5183742005-02-02 11:14:04 +00002033
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002034 while((opt = strsep(&s, ","))) {
2035 if (strcmp(opt, "debug") == 0)
2036 f->flags |= FUSE_DEBUG;
2037 else if (strcmp(opt, "hard_remove") == 0)
2038 f->flags |= FUSE_HARD_REMOVE;
Miklos Szeredia13d9002004-11-02 17:32:03 +00002039 else if (strcmp(opt, "use_ino") == 0)
2040 f->flags |= FUSE_USE_INO;
Miklos Szeredi0111f9d2005-04-22 12:04:55 +00002041 else if (strcmp(opt, "allow_root") == 0)
2042 f->flags |= FUSE_ALLOW_ROOT;
Miklos Szeredie5183742005-02-02 11:14:04 +00002043 else
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002044 fprintf(stderr, "fuse: warning: unknown option `%s'\n", opt);
2045 }
2046 free(xopts);
2047 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002048 return 0;
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002049}
2050
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002051struct fuse *fuse_new_common(int fd, const char *opts,
2052 const struct fuse_operations *op,
2053 size_t op_size, int compat)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002054{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002055 struct fuse *f;
2056 struct node *root;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002057
Miklos Szeredi0f62d722005-01-04 12:45:54 +00002058 if (sizeof(struct fuse_operations) < op_size) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002059 fprintf(stderr, "fuse: warning: library too old, some operations may not not work\n");
Miklos Szeredi0f62d722005-01-04 12:45:54 +00002060 op_size = sizeof(struct fuse_operations);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002061 }
2062
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002063 f = (struct fuse *) calloc(1, sizeof(struct fuse));
Miklos Szeredie56818b2004-12-12 11:45:24 +00002064 if (f == NULL) {
2065 fprintf(stderr, "fuse: failed to allocate fuse object\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002066 goto out;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002067 }
Miklos Szeredi2df1c042001-11-06 15:07:17 +00002068
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002069 if (parse_lib_opts(f, opts) == -1)
2070 goto out_free;
2071
Miklos Szeredi8cffdb92001-11-09 14:49:18 +00002072 f->fd = fd;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002073 f->ctr = 0;
Miklos Szeredi76f65782004-02-19 16:55:40 +00002074 f->generation = 0;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00002075 /* FIXME: Dynamic hash table */
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002076 f->name_table_size = 14057;
2077 f->name_table = (struct node **)
2078 calloc(1, sizeof(struct node *) * f->name_table_size);
Miklos Szeredie56818b2004-12-12 11:45:24 +00002079 if (f->name_table == NULL) {
2080 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002081 goto out_free;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002082 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002083
Miklos Szeredia13d9002004-11-02 17:32:03 +00002084 f->id_table_size = 14057;
2085 f->id_table = (struct node **)
2086 calloc(1, sizeof(struct node *) * f->id_table_size);
Miklos Szeredie56818b2004-12-12 11:45:24 +00002087 if (f->id_table == NULL) {
2088 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002089 goto out_free_name_table;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002090 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002091
Miklos Szerediab974562005-04-07 15:40:21 +00002092 mutex_init(&f->lock);
2093 mutex_init(&f->worker_lock);
Miklos Szeredi33232032001-11-19 17:55:51 +00002094 f->numworker = 0;
2095 f->numavail = 0;
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002096 memcpy(&f->op, op, op_size);
2097 f->compat = compat;
Miklos Szeredi0f48a262002-12-05 14:23:01 +00002098 f->exited = 0;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002099
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002100 root = (struct node *) calloc(1, sizeof(struct node));
Miklos Szeredie56818b2004-12-12 11:45:24 +00002101 if (root == NULL) {
2102 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredia13d9002004-11-02 17:32:03 +00002103 goto out_free_id_table;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002104 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002105
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002106 root->name = strdup("/");
Miklos Szeredie56818b2004-12-12 11:45:24 +00002107 if (root->name == NULL) {
2108 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002109 goto out_free_root;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002110 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002111
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002112 root->parent = 0;
Miklos Szeredia13d9002004-11-02 17:32:03 +00002113 root->nodeid = FUSE_ROOT_ID;
Miklos Szeredi76f65782004-02-19 16:55:40 +00002114 root->generation = 0;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00002115 root->refctr = 1;
Miklos Szeredia13d9002004-11-02 17:32:03 +00002116 hash_id(f, root);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002117
Miklos Szeredi0111f9d2005-04-22 12:04:55 +00002118 f->owner = getuid();
2119
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002120 return f;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002121
2122 out_free_root:
2123 free(root);
Miklos Szeredia13d9002004-11-02 17:32:03 +00002124 out_free_id_table:
2125 free(f->id_table);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002126 out_free_name_table:
2127 free(f->name_table);
2128 out_free:
2129 free(f);
2130 out:
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002131 return NULL;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002132}
2133
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002134struct fuse *fuse_new(int fd, const char *opts,
2135 const struct fuse_operations *op, size_t op_size)
2136{
2137 return fuse_new_common(fd, opts, op, op_size, 0);
2138}
2139
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002140struct fuse *fuse_new_compat2(int fd, const char *opts,
2141 const struct fuse_operations_compat2 *op)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002142{
2143 return fuse_new_common(fd, opts, (struct fuse_operations *) op,
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002144 sizeof(struct fuse_operations_compat2), 21);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002145}
2146
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002147struct fuse *fuse_new_compat1(int fd, int flags,
2148 const struct fuse_operations_compat1 *op)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002149{
2150 char *opts = NULL;
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002151 if (flags & FUSE_DEBUG_COMPAT1)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002152 opts = "debug";
2153 return fuse_new_common(fd, opts, (struct fuse_operations *) op,
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002154 sizeof(struct fuse_operations_compat1), 11);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002155}
2156
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002157void fuse_destroy(struct fuse *f)
2158{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002159 size_t i;
Miklos Szeredia13d9002004-11-02 17:32:03 +00002160 for (i = 0; i < f->id_table_size; i++) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002161 struct node *node;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002162
Miklos Szeredia13d9002004-11-02 17:32:03 +00002163 for (node = f->id_table[i]; node != NULL; node = node->id_next) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002164 if (node->is_hidden) {
Miklos Szeredia13d9002004-11-02 17:32:03 +00002165 char *path = get_path(f, node->nodeid);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002166 if (path)
2167 f->op.unlink(path);
2168 }
2169 }
2170 }
Miklos Szeredia13d9002004-11-02 17:32:03 +00002171 for (i = 0; i < f->id_table_size; i++) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002172 struct node *node;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002173 struct node *next;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002174
Miklos Szeredia13d9002004-11-02 17:32:03 +00002175 for (node = f->id_table[i]; node != NULL; node = next) {
2176 next = node->id_next;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002177 free_node(node);
2178 }
2179 }
Miklos Szeredia13d9002004-11-02 17:32:03 +00002180 free(f->id_table);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002181 free(f->name_table);
Miklos Szeredia181e612001-11-06 12:03:23 +00002182 pthread_mutex_destroy(&f->lock);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00002183 pthread_mutex_destroy(&f->worker_lock);
Miklos Szeredi159bd7e2005-02-28 17:32:16 +00002184 if (f->op.destroy)
2185 f->op.destroy(f->user_data);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002186 free(f);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002187}
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002188
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002189__asm__(".symver fuse_exited,__fuse_exited@");
2190__asm__(".symver fuse_process_cmd,__fuse_process_cmd@");
2191__asm__(".symver fuse_read_cmd,__fuse_read_cmd@");
2192__asm__(".symver fuse_set_getcontext_func,__fuse_set_getcontext_func@");
2193__asm__(".symver fuse_new_compat2,fuse_new@");