blob: 04fb2f0fac50c65ee9a61be4f90d80e40a3a6fa4 [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;
Miklos Szeredi38009022005-05-08 19:47:22 +000071 uint64_t nlookup;
Miklos Szeredi0f62d722005-01-04 12:45:54 +000072 int open_count;
73 int is_hidden;
74};
75
76struct fuse_dirhandle {
Miklos Szerediab974562005-04-07 15:40:21 +000077 pthread_mutex_t lock;
Miklos Szeredi0f62d722005-01-04 12:45:54 +000078 struct fuse *fuse;
Miklos Szeredib92d9782005-02-07 16:10:49 +000079 unsigned char *contents;
Miklos Szerediab974562005-04-07 15:40:21 +000080 int allocated;
Miklos Szeredib92d9782005-02-07 16:10:49 +000081 unsigned len;
Miklos Szerediab974562005-04-07 15:40:21 +000082 unsigned needlen;
Miklos Szeredi3ead28e2005-01-18 21:23:41 +000083 int filled;
Miklos Szerediede1f7a2005-04-01 21:08:57 +000084 unsigned long fh;
Miklos Szerediab974562005-04-07 15:40:21 +000085 int error;
Miklos Szeredi0f62d722005-01-04 12:45:54 +000086};
87
88struct fuse_cmd {
89 char *buf;
90 size_t buflen;
91};
92
93
Miklos Szeredid169f312004-09-22 08:48:26 +000094static struct fuse_context *(*fuse_getcontext)(void) = NULL;
95
Miklos Szerediab974562005-04-07 15:40:21 +000096#ifndef USE_UCLIBC
97#define mutex_init(mut) pthread_mutex_init(mut, NULL)
98#else
99static void mutex_init(pthread_mutex_t mut)
100{
101 pthread_mutexattr_t attr;
102 pthread_mutexattr_init(&attr);
103 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
104 pthread_mutex_init(mut, &attr);
105 pthread_mutexattr_destroy(&attr);
106}
107#endif
108
Miklos Szeredic8ba2372002-12-10 12:26:00 +0000109static const char *opname(enum fuse_opcode opcode)
110{
Miklos Szeredie5183742005-02-02 11:14:04 +0000111 switch (opcode) {
Miklos Szeredi3ed84232004-03-30 15:17:26 +0000112 case FUSE_LOOKUP: return "LOOKUP";
113 case FUSE_FORGET: return "FORGET";
114 case FUSE_GETATTR: return "GETATTR";
115 case FUSE_SETATTR: return "SETATTR";
116 case FUSE_READLINK: return "READLINK";
117 case FUSE_SYMLINK: return "SYMLINK";
Miklos Szeredi3ed84232004-03-30 15:17:26 +0000118 case FUSE_MKNOD: return "MKNOD";
119 case FUSE_MKDIR: return "MKDIR";
120 case FUSE_UNLINK: return "UNLINK";
121 case FUSE_RMDIR: return "RMDIR";
122 case FUSE_RENAME: return "RENAME";
123 case FUSE_LINK: return "LINK";
124 case FUSE_OPEN: return "OPEN";
125 case FUSE_READ: return "READ";
126 case FUSE_WRITE: return "WRITE";
127 case FUSE_STATFS: return "STATFS";
Miklos Szeredi99f20742004-05-19 08:01:10 +0000128 case FUSE_FLUSH: return "FLUSH";
Miklos Szeredi3ed84232004-03-30 15:17:26 +0000129 case FUSE_RELEASE: return "RELEASE";
130 case FUSE_FSYNC: return "FSYNC";
131 case FUSE_SETXATTR: return "SETXATTR";
132 case FUSE_GETXATTR: return "GETXATTR";
133 case FUSE_LISTXATTR: return "LISTXATTR";
134 case FUSE_REMOVEXATTR: return "REMOVEXATTR";
Miklos Szeredi3f0005f2005-01-04 19:24:31 +0000135 case FUSE_INIT: return "INIT";
Miklos Szeredi3ead28e2005-01-18 21:23:41 +0000136 case FUSE_OPENDIR: return "OPENDIR";
137 case FUSE_READDIR: return "READDIR";
138 case FUSE_RELEASEDIR: return "RELEASEDIR";
Miklos Szeredi4283ee72005-03-21 12:09:04 +0000139 case FUSE_FSYNCDIR: return "FSYNCDIR";
Miklos Szeredi99f20742004-05-19 08:01:10 +0000140 default: return "???";
Miklos Szeredic8ba2372002-12-10 12:26:00 +0000141 }
142}
143
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000144static inline void fuse_dec_avail(struct fuse *f)
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000145{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000146 pthread_mutex_lock(&f->worker_lock);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000147 f->numavail --;
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000148 pthread_mutex_unlock(&f->worker_lock);
149}
150
151static inline void fuse_inc_avail(struct fuse *f)
152{
153 pthread_mutex_lock(&f->worker_lock);
154 f->numavail ++;
155 pthread_mutex_unlock(&f->worker_lock);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000156}
157
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000158static struct node *get_node_nocheck(struct fuse *f, nodeid_t nodeid)
Miklos Szeredia181e612001-11-06 12:03:23 +0000159{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000160 size_t hash = nodeid % f->id_table_size;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000161 struct node *node;
162
Miklos Szeredia13d9002004-11-02 17:32:03 +0000163 for (node = f->id_table[hash]; node != NULL; node = node->id_next)
164 if (node->nodeid == nodeid)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000165 return node;
Miklos Szeredie5183742005-02-02 11:14:04 +0000166
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000167 return NULL;
Miklos Szeredia181e612001-11-06 12:03:23 +0000168}
169
Miklos Szeredia13d9002004-11-02 17:32:03 +0000170static struct node *get_node(struct fuse *f, nodeid_t nodeid)
Miklos Szeredia181e612001-11-06 12:03:23 +0000171{
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000172 struct node *node = get_node_nocheck(f, nodeid);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000173 if (!node) {
174 fprintf(stderr, "fuse internal error: node %lu not found\n",
175 nodeid);
176 abort();
177 }
178 return node;
Miklos Szeredia181e612001-11-06 12:03:23 +0000179}
180
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000181static void free_node(struct node *node)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000182{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000183 free(node->name);
184 free(node);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000185}
186
Miklos Szeredia13d9002004-11-02 17:32:03 +0000187static void unhash_id(struct fuse *f, struct node *node)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000188{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000189 size_t hash = node->nodeid % f->id_table_size;
190 struct node **nodep = &f->id_table[hash];
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000191
Miklos Szeredie5183742005-02-02 11:14:04 +0000192 for (; *nodep != NULL; nodep = &(*nodep)->id_next)
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000193 if (*nodep == node) {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000194 *nodep = node->id_next;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000195 return;
196 }
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000197}
198
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000199static void hash_id(struct fuse *f, struct node *node)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000200{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000201 size_t hash = node->nodeid % f->id_table_size;
202 node->id_next = f->id_table[hash];
Miklos Szeredie5183742005-02-02 11:14:04 +0000203 f->id_table[hash] = node;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000204}
205
Miklos Szeredia13d9002004-11-02 17:32:03 +0000206static unsigned int name_hash(struct fuse *f, nodeid_t parent, const char *name)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000207{
208 unsigned int hash = *name;
209
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000210 if (hash)
211 for (name += 1; *name != '\0'; name++)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000212 hash = (hash << 5) - hash + *name;
213
214 return (hash + parent) % f->name_table_size;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000215}
216
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000217static void unref_node(struct fuse *f, struct node *node);
218
219static void unhash_name(struct fuse *f, struct node *node)
220{
221 if (node->name) {
222 size_t hash = name_hash(f, node->parent, node->name);
223 struct node **nodep = &f->name_table[hash];
Miklos Szeredie5183742005-02-02 11:14:04 +0000224
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000225 for (; *nodep != NULL; nodep = &(*nodep)->name_next)
226 if (*nodep == node) {
227 *nodep = node->name_next;
228 node->name_next = NULL;
229 unref_node(f, get_node(f, node->parent));
230 free(node->name);
231 node->name = NULL;
232 node->parent = 0;
233 return;
234 }
235 fprintf(stderr, "fuse internal error: unable to unhash node: %lu\n",
236 node->nodeid);
237 abort();
238 }
239}
240
241static int hash_name(struct fuse *f, struct node *node, nodeid_t parent,
242 const char *name)
243{
244 size_t hash = name_hash(f, parent, name);
245 node->name = strdup(name);
246 if (node->name == NULL)
247 return -1;
248
249 get_node(f, parent)->refctr ++;
250 node->parent = parent;
251 node->name_next = f->name_table[hash];
252 f->name_table[hash] = node;
253 return 0;
254}
255
256static void delete_node(struct fuse *f, struct node *node)
257{
Miklos Szeredi38009022005-05-08 19:47:22 +0000258 if (f->flags & FUSE_DEBUG) {
259 printf("delete: %lu\n", node->nodeid);
260 fflush(stdout);
261 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000262 assert(!node->name);
263 unhash_id(f, node);
264 free_node(node);
265}
266
267static void unref_node(struct fuse *f, struct node *node)
268{
269 assert(node->refctr > 0);
270 node->refctr --;
271 if (!node->refctr)
272 delete_node(f, node);
273}
274
275static nodeid_t next_id(struct fuse *f)
276{
277 do {
278 f->ctr++;
279 if (!f->ctr)
280 f->generation ++;
281 } while (f->ctr == 0 || get_node_nocheck(f, f->ctr) != NULL);
282 return f->ctr;
283}
284
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000285static struct node *lookup_node(struct fuse *f, nodeid_t parent,
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000286 const char *name)
287{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000288 size_t hash = name_hash(f, parent, name);
289 struct node *node;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000290
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000291 for (node = f->name_table[hash]; node != NULL; node = node->name_next)
292 if (node->parent == parent && strcmp(node->name, name) == 0)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000293 return node;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000294
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000295 return NULL;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000296}
297
Miklos Szeredia13d9002004-11-02 17:32:03 +0000298static struct node *find_node(struct fuse *f, nodeid_t parent, char *name,
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000299 struct fuse_attr *attr, uint64_t version)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000300{
301 struct node *node;
Miklos Szeredia181e612001-11-06 12:03:23 +0000302 int mode = attr->mode & S_IFMT;
303 int rdev = 0;
Miklos Szeredie5183742005-02-02 11:14:04 +0000304
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000305 if (S_ISCHR(mode) || S_ISBLK(mode))
Miklos Szeredia181e612001-11-06 12:03:23 +0000306 rdev = attr->rdev;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000307
Miklos Szeredia181e612001-11-06 12:03:23 +0000308 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000309 node = lookup_node(f, parent, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000310 if (node != NULL) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000311 if (!(f->flags & FUSE_USE_INO))
Miklos Szeredie5183742005-02-02 11:14:04 +0000312 attr->ino = node->nodeid;
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000313 } else {
314 node = (struct node *) calloc(1, sizeof(struct node));
315 if (node == NULL)
316 goto out_err;
Miklos Szeredie5183742005-02-02 11:14:04 +0000317
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000318 node->refctr = 1;
319 node->nodeid = next_id(f);
320 if (!(f->flags & FUSE_USE_INO))
321 attr->ino = node->nodeid;
322 node->open_count = 0;
323 node->is_hidden = 0;
324 node->generation = f->generation;
325 if (hash_name(f, node, parent, name) == -1) {
326 free(node);
327 node = NULL;
328 goto out_err;
329 }
330 hash_id(f, node);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000331 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000332 node->version = version;
Miklos Szeredi38009022005-05-08 19:47:22 +0000333 node->nlookup ++;
Miklos Szeredic2309912004-09-21 13:40:38 +0000334 out_err:
Miklos Szeredia181e612001-11-06 12:03:23 +0000335 pthread_mutex_unlock(&f->lock);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000336 return node;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000337}
338
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000339static char *add_name(char *buf, char *s, const char *name)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000340{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000341 size_t len = strlen(name);
342 s -= len;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000343 if (s <= buf) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000344 fprintf(stderr, "fuse: path too long: ...%s\n", s + len);
345 return NULL;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000346 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000347 strncpy(s, name, len);
348 s--;
349 *s = '/';
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000350
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000351 return s;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000352}
353
Miklos Szeredia13d9002004-11-02 17:32:03 +0000354static char *get_path_name(struct fuse *f, nodeid_t nodeid, const char *name)
Miklos Szeredib483c932001-10-29 14:57:57 +0000355{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000356 char buf[FUSE_MAX_PATH];
357 char *s = buf + FUSE_MAX_PATH - 1;
358 struct node *node;
Miklos Szeredie5183742005-02-02 11:14:04 +0000359
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000360 *s = '\0';
Miklos Szeredia181e612001-11-06 12:03:23 +0000361
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000362 if (name != NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000363 s = add_name(buf, s, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000364 if (s == NULL)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000365 return NULL;
366 }
367
368 pthread_mutex_lock(&f->lock);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000369 for (node = get_node(f, nodeid); node && node->nodeid != FUSE_ROOT_ID;
370 node = get_node(f, node->parent)) {
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000371 if (node->name == NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000372 s = NULL;
373 break;
374 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000375
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000376 s = add_name(buf, s, node->name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000377 if (s == NULL)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000378 break;
379 }
380 pthread_mutex_unlock(&f->lock);
381
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000382 if (node == NULL || s == NULL)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000383 return NULL;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000384 else if (*s == '\0')
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000385 return strdup("/");
386 else
387 return strdup(s);
388}
Miklos Szeredia181e612001-11-06 12:03:23 +0000389
Miklos Szeredia13d9002004-11-02 17:32:03 +0000390static char *get_path(struct fuse *f, nodeid_t nodeid)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000391{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000392 return get_path_name(f, nodeid, NULL);
Miklos Szeredib483c932001-10-29 14:57:57 +0000393}
394
Miklos Szeredi38009022005-05-08 19:47:22 +0000395static void forget_node(struct fuse *f, nodeid_t nodeid, uint64_t nlookup)
396{
397 struct node *node;
398 if (nodeid == FUSE_ROOT_ID)
399 return;
400 pthread_mutex_lock(&f->lock);
401 node = get_node(f, nodeid);
402 assert(node->nlookup >= nlookup);
403 node->nlookup -= nlookup;
404 if (!node->nlookup) {
405 unhash_name(f, node);
406 unref_node(f, node);
407 }
408 pthread_mutex_unlock(&f->lock);
409}
410
411static void forget_node_old(struct fuse *f, nodeid_t nodeid, uint64_t version)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000412{
Miklos Szeredia181e612001-11-06 12:03:23 +0000413 struct node *node;
414
415 pthread_mutex_lock(&f->lock);
Miklos Szeredid0cf1fb2005-05-06 10:10:38 +0000416 node = get_node_nocheck(f, nodeid);
417 if (node && node->version == version && nodeid != FUSE_ROOT_ID) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000418 node->version = 0;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000419 unhash_name(f, node);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000420 unref_node(f, node);
Miklos Szeredia181e612001-11-06 12:03:23 +0000421 }
422 pthread_mutex_unlock(&f->lock);
Miklos Szeredi38009022005-05-08 19:47:22 +0000423}
Miklos Szeredia181e612001-11-06 12:03:23 +0000424
Miklos Szeredi38009022005-05-08 19:47:22 +0000425static void cancel_lookup(struct fuse *f, nodeid_t nodeid, uint64_t version)
426{
427 if (f->major <= 6)
428 forget_node_old(f, nodeid, version);
429 else
430 forget_node(f, nodeid, 1);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000431}
432
Miklos Szeredia13d9002004-11-02 17:32:03 +0000433static void remove_node(struct fuse *f, nodeid_t dir, const char *name)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000434{
Miklos Szeredia181e612001-11-06 12:03:23 +0000435 struct node *node;
436
437 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000438 node = lookup_node(f, dir, name);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000439 if (node != NULL)
440 unhash_name(f, node);
Miklos Szeredia181e612001-11-06 12:03:23 +0000441 pthread_mutex_unlock(&f->lock);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000442}
443
Miklos Szeredia13d9002004-11-02 17:32:03 +0000444static int rename_node(struct fuse *f, nodeid_t olddir, const char *oldname,
445 nodeid_t newdir, const char *newname, int hide)
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000446{
Miklos Szeredia181e612001-11-06 12:03:23 +0000447 struct node *node;
448 struct node *newnode;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000449 int err = 0;
Miklos Szeredie5183742005-02-02 11:14:04 +0000450
Miklos Szeredia181e612001-11-06 12:03:23 +0000451 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000452 node = lookup_node(f, olddir, oldname);
453 newnode = lookup_node(f, newdir, newname);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000454 if (node == NULL)
455 goto out;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000456
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000457 if (newnode != NULL) {
458 if (hide) {
459 fprintf(stderr, "fuse: hidden file got created during hiding\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000460 err = -EBUSY;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000461 goto out;
462 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000463 unhash_name(f, newnode);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000464 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000465
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000466 unhash_name(f, node);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000467 if (hash_name(f, node, newdir, newname) == -1) {
468 err = -ENOMEM;
469 goto out;
470 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000471
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000472 if (hide)
473 node->is_hidden = 1;
474
475 out:
Miklos Szeredia181e612001-11-06 12:03:23 +0000476 pthread_mutex_unlock(&f->lock);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000477 return err;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000478}
479
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000480static void convert_stat(struct stat *stbuf, struct fuse_attr *attr)
481{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000482 attr->ino = stbuf->st_ino;
Miklos Szeredib5958612004-02-20 14:10:49 +0000483 attr->mode = stbuf->st_mode;
484 attr->nlink = stbuf->st_nlink;
485 attr->uid = stbuf->st_uid;
486 attr->gid = stbuf->st_gid;
487 attr->rdev = stbuf->st_rdev;
488 attr->size = stbuf->st_size;
489 attr->blocks = stbuf->st_blocks;
490 attr->atime = stbuf->st_atime;
Miklos Szeredib5958612004-02-20 14:10:49 +0000491 attr->mtime = stbuf->st_mtime;
Miklos Szeredib5958612004-02-20 14:10:49 +0000492 attr->ctime = stbuf->st_ctime;
Miklos Szeredicb264512004-06-23 18:52:50 +0000493#ifdef HAVE_STRUCT_STAT_ST_ATIM
494 attr->atimensec = stbuf->st_atim.tv_nsec;
495 attr->mtimensec = stbuf->st_mtim.tv_nsec;
Miklos Szeredib5958612004-02-20 14:10:49 +0000496 attr->ctimensec = stbuf->st_ctim.tv_nsec;
Miklos Szeredicb264512004-06-23 18:52:50 +0000497#endif
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000498}
499
Miklos Szerediab974562005-04-07 15:40:21 +0000500static size_t iov_length(const struct iovec *iov, size_t count)
Miklos Szerediede1f7a2005-04-01 21:08:57 +0000501{
Miklos Szerediab974562005-04-07 15:40:21 +0000502 size_t seg;
503 size_t ret = 0;
Miklos Szerediede1f7a2005-04-01 21:08:57 +0000504
Miklos Szerediab974562005-04-07 15:40:21 +0000505 for (seg = 0; seg < count; seg++)
506 ret += iov[seg].iov_len;
507 return ret;
Miklos Szerediede1f7a2005-04-01 21:08:57 +0000508}
509
Miklos Szerediab974562005-04-07 15:40:21 +0000510static int send_reply_raw(struct fuse *f, const struct iovec iov[],
511 size_t count)
Miklos Szeredi43696432001-11-18 19:15:05 +0000512{
513 int res;
Miklos Szerediab974562005-04-07 15:40:21 +0000514 unsigned outsize = iov_length(iov, count);
515 struct fuse_out_header *out = (struct fuse_out_header *) iov[0].iov_base;
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000516 out->len = outsize;
Miklos Szeredi43696432001-11-18 19:15:05 +0000517
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000518 if ((f->flags & FUSE_DEBUG)) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000519 printf(" unique: %llu, error: %i (%s), outsize: %i\n",
520 out->unique, out->error, strerror(-out->error), outsize);
Miklos Szeredi43696432001-11-18 19:15:05 +0000521 fflush(stdout);
522 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000523
Miklos Szeredi4e71c9f2004-02-09 12:05:14 +0000524 /* This needs to be done before the reply, otherwise the scheduler
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000525 could play tricks with us, and only let the counter be
526 increased long after the operation is done */
527 fuse_inc_avail(f);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000528
Miklos Szerediab974562005-04-07 15:40:21 +0000529 res = writev(f->fd, iov, count);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000530 if (res == -1) {
Miklos Szeredi43696432001-11-18 19:15:05 +0000531 /* ENOENT means the operation was interrupted */
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000532 if (!f->exited && errno != ENOENT)
Miklos Szeredi96249982001-11-21 12:21:19 +0000533 perror("fuse: writing device");
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000534 return -errno;
Miklos Szeredi43696432001-11-18 19:15:05 +0000535 }
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000536 return 0;
Miklos Szeredi43696432001-11-18 19:15:05 +0000537}
538
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000539static int send_reply(struct fuse *f, struct fuse_in_header *in, int error,
540 void *arg, size_t argsize)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000541{
Miklos Szerediab974562005-04-07 15:40:21 +0000542 struct fuse_out_header out;
543 struct iovec iov[2];
544 size_t count;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000545
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000546 if (error <= -1000 || error > 0) {
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000547 fprintf(stderr, "fuse: bad error value: %i\n", error);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000548 error = -ERANGE;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000549 }
550
Miklos Szerediab974562005-04-07 15:40:21 +0000551 out.unique = in->unique;
552 out.error = error;
553 count = 1;
554 iov[0].iov_base = &out;
555 iov[0].iov_len = sizeof(struct fuse_out_header);
556 if (argsize && !error) {
557 count++;
558 iov[1].iov_base = arg;
559 iov[1].iov_len = argsize;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000560 }
Miklos Szerediab974562005-04-07 15:40:21 +0000561 return send_reply_raw(f, iov, count);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000562}
563
Miklos Szeredia13d9002004-11-02 17:32:03 +0000564static int is_open(struct fuse *f, nodeid_t dir, const char *name)
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000565{
566 struct node *node;
567 int isopen = 0;
568 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000569 node = lookup_node(f, dir, name);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000570 if (node && node->open_count > 0)
571 isopen = 1;
572 pthread_mutex_unlock(&f->lock);
573 return isopen;
574}
575
Miklos Szeredia13d9002004-11-02 17:32:03 +0000576static char *hidden_name(struct fuse *f, nodeid_t dir, const char *oldname,
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000577 char *newname, size_t bufsize)
578{
579 struct stat buf;
580 struct node *node;
581 struct node *newnode;
582 char *newpath;
583 int res;
584 int failctr = 10;
585
586 if (!f->op.getattr)
587 return NULL;
588
589 do {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000590 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000591 node = lookup_node(f, dir, oldname);
592 if (node == NULL) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000593 pthread_mutex_unlock(&f->lock);
594 return NULL;
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000595 }
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000596 do {
597 f->hidectr ++;
598 snprintf(newname, bufsize, ".fuse_hidden%08x%08x",
Miklos Szeredia13d9002004-11-02 17:32:03 +0000599 (unsigned int) node->nodeid, f->hidectr);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000600 newnode = lookup_node(f, dir, newname);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000601 } while(newnode);
602 pthread_mutex_unlock(&f->lock);
Miklos Szeredie5183742005-02-02 11:14:04 +0000603
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000604 newpath = get_path_name(f, dir, newname);
605 if (!newpath)
606 break;
Miklos Szeredie5183742005-02-02 11:14:04 +0000607
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000608 res = f->op.getattr(newpath, &buf);
609 if (res != 0)
610 break;
611 free(newpath);
612 newpath = NULL;
613 } while(--failctr);
614
615 return newpath;
616}
617
Miklos Szeredia13d9002004-11-02 17:32:03 +0000618static int hide_node(struct fuse *f, const char *oldpath, nodeid_t dir,
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000619 const char *oldname)
620{
621 char newname[64];
622 char *newpath;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000623 int err = -EBUSY;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000624
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000625 if (f->op.rename && f->op.unlink) {
626 newpath = hidden_name(f, dir, oldname, newname, sizeof(newname));
627 if (newpath) {
628 int res = f->op.rename(oldpath, newpath);
629 if (res == 0)
630 err = rename_node(f, dir, oldname, dir, newname, 1);
631 free(newpath);
632 }
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000633 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000634 return err;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000635}
636
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000637static int lookup_path(struct fuse *f, nodeid_t nodeid, uint64_t version,
638 char *name, const char *path,
639 struct fuse_entry_out *arg)
Miklos Szeredi76f65782004-02-19 16:55:40 +0000640{
641 int res;
642 struct stat buf;
643
644 res = f->op.getattr(path, &buf);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000645 if (res == 0) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000646 struct node *node;
647
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000648 memset(arg, 0, sizeof(struct fuse_entry_out));
Miklos Szeredi76f65782004-02-19 16:55:40 +0000649 convert_stat(&buf, &arg->attr);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000650 node = find_node(f, nodeid, name, &arg->attr, version);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000651 if (node == NULL)
652 res = -ENOMEM;
653 else {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000654 arg->nodeid = node->nodeid;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000655 arg->generation = node->generation;
656 arg->entry_valid = ENTRY_REVALIDATE_TIME;
657 arg->entry_valid_nsec = 0;
658 arg->attr_valid = ATTR_REVALIDATE_TIME;
659 arg->attr_valid_nsec = 0;
660 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000661 printf(" NODEID: %lu\n", (unsigned long) arg->nodeid);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000662 fflush(stdout);
663 }
Miklos Szeredi76f65782004-02-19 16:55:40 +0000664 }
665 }
666 return res;
667}
668
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000669static void do_lookup(struct fuse *f, struct fuse_in_header *in, char *name)
670{
671 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000672 int res2;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000673 char *path;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000674 struct fuse_entry_out arg;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000675
Miklos Szeredi5e183482001-10-31 14:52:35 +0000676 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000677 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000678 if (path != NULL) {
679 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi6ebe2342002-01-06 21:44:16 +0000680 printf("LOOKUP %s\n", path);
681 fflush(stdout);
682 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000683 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000684 if (f->op.getattr)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000685 res = lookup_path(f, in->nodeid, in->unique, name, path, &arg);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000686 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000687 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000688 res2 = send_reply(f, in, res, &arg, sizeof(arg));
689 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi38009022005-05-08 19:47:22 +0000690 cancel_lookup(f, arg.nodeid, in->unique);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000691}
692
Miklos Szeredia181e612001-11-06 12:03:23 +0000693static void do_forget(struct fuse *f, struct fuse_in_header *in,
694 struct fuse_forget_in *arg)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000695{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000696 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000697 printf("FORGET %lu/%llu\n", (unsigned long) in->nodeid,
Miklos Szeredi38009022005-05-08 19:47:22 +0000698 arg->nlookup);
Miklos Szeredi43696432001-11-18 19:15:05 +0000699 fflush(stdout);
700 }
Miklos Szeredi38009022005-05-08 19:47:22 +0000701 if (f->major <= 6)
702 forget_node_old(f, in->nodeid, arg->nlookup);
703 else
704 forget_node(f, in->nodeid, arg->nlookup);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000705}
706
707static void do_getattr(struct fuse *f, struct fuse_in_header *in)
708{
709 int res;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000710 char *path;
711 struct stat buf;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000712 struct fuse_attr_out arg;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000713
Miklos Szeredi5e183482001-10-31 14:52:35 +0000714 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000715 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000716 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +0000717 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000718 if (f->op.getattr)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000719 res = f->op.getattr(path, &buf);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000720 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000721 }
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000722
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000723 if (res == 0) {
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000724 memset(&arg, 0, sizeof(struct fuse_attr_out));
725 arg.attr_valid = ATTR_REVALIDATE_TIME;
726 arg.attr_valid_nsec = 0;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000727 convert_stat(&buf, &arg.attr);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000728 if (!(f->flags & FUSE_USE_INO))
729 arg.attr.ino = in->nodeid;
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000730 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000731
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000732 send_reply(f, in, res, &arg, sizeof(arg));
733}
734
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000735static int do_chmod(struct fuse *f, const char *path, struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000736{
737 int res;
738
739 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000740 if (f->op.chmod)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000741 res = f->op.chmod(path, attr->mode);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000742
743 return res;
Miklos Szeredie5183742005-02-02 11:14:04 +0000744}
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000745
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000746static int do_chown(struct fuse *f, const char *path, struct fuse_attr *attr,
Miklos Szeredi2e50d432001-12-20 12:17:25 +0000747 int valid)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000748{
749 int res;
750 uid_t uid = (valid & FATTR_UID) ? attr->uid : (uid_t) -1;
751 gid_t gid = (valid & FATTR_GID) ? attr->gid : (gid_t) -1;
Miklos Szeredie5183742005-02-02 11:14:04 +0000752
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000753 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000754 if (f->op.chown)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000755 res = f->op.chown(path, uid, gid);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000756
757 return res;
758}
759
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000760static int do_truncate(struct fuse *f, const char *path,
761 struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000762{
763 int res;
764
765 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000766 if (f->op.truncate)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000767 res = f->op.truncate(path, attr->size);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000768
769 return res;
770}
771
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000772static int do_utime(struct fuse *f, const char *path, struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000773{
774 int res;
775 struct utimbuf buf;
776 buf.actime = attr->atime;
777 buf.modtime = attr->mtime;
778 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000779 if (f->op.utime)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000780 res = f->op.utime(path, &buf);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000781
782 return res;
783}
784
Miklos Szeredi5e183482001-10-31 14:52:35 +0000785static void do_setattr(struct fuse *f, struct fuse_in_header *in,
786 struct fuse_setattr_in *arg)
787{
788 int res;
789 char *path;
790 int valid = arg->valid;
Miklos Szeredi30e093a2005-04-03 17:44:54 +0000791 struct fuse_attr *attr = MEMBER_COMPAT(f, arg, attr, fuse_setattr_in);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000792 struct fuse_attr_out outarg;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000793
794 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000795 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000796 if (path != NULL) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000797 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000798 if (f->op.getattr) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000799 res = 0;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000800 if (!res && (valid & FATTR_MODE))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000801 res = do_chmod(f, path, attr);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000802 if (!res && (valid & (FATTR_UID | FATTR_GID)))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000803 res = do_chown(f, path, attr, valid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000804 if (!res && (valid & FATTR_SIZE))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000805 res = do_truncate(f, path, attr);
Miklos Szeredie5183742005-02-02 11:14:04 +0000806 if (!res && (valid & (FATTR_ATIME | FATTR_MTIME)) ==
Miklos Szeredib5958612004-02-20 14:10:49 +0000807 (FATTR_ATIME | FATTR_MTIME))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000808 res = do_utime(f, path, attr);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000809 if (!res) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000810 struct stat buf;
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000811 res = f->op.getattr(path, &buf);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000812 if (!res) {
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000813 memset(&outarg, 0, sizeof(struct fuse_attr_out));
814 outarg.attr_valid = ATTR_REVALIDATE_TIME;
815 outarg.attr_valid_nsec = 0;
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000816 convert_stat(&buf, &outarg.attr);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000817 if (!(f->flags & FUSE_USE_INO))
818 outarg.attr.ino = in->nodeid;
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000819 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000820 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000821 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000822 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000823 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000824 send_reply(f, in, res, &outarg, sizeof(outarg));
Miklos Szeredi5e183482001-10-31 14:52:35 +0000825}
826
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000827static void do_readlink(struct fuse *f, struct fuse_in_header *in)
828{
829 int res;
830 char link[PATH_MAX + 1];
Miklos Szeredib483c932001-10-29 14:57:57 +0000831 char *path;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000832
Miklos Szeredi5e183482001-10-31 14:52:35 +0000833 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000834 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000835 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +0000836 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000837 if (f->op.readlink)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000838 res = f->op.readlink(path, link, sizeof(link));
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000839 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000840 }
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000841 link[PATH_MAX] = '\0';
Miklos Szeredi4b2bef42002-01-09 12:23:27 +0000842 send_reply(f, in, res, link, res == 0 ? strlen(link) : 0);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000843}
844
Miklos Szeredib483c932001-10-29 14:57:57 +0000845static void do_mknod(struct fuse *f, struct fuse_in_header *in,
846 struct fuse_mknod_in *inarg)
847{
848 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000849 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000850 char *path;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000851 char *name = PARAM(inarg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000852 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000853
Miklos Szeredi5e183482001-10-31 14:52:35 +0000854 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000855 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000856 if (path != NULL) {
857 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000858 printf("MKNOD %s\n", path);
859 fflush(stdout);
860 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000861 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000862 if (f->op.mknod && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000863 res = f->op.mknod(path, inarg->mode, inarg->rdev);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000864 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000865 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000866 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000867 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000868 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000869 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
870 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi38009022005-05-08 19:47:22 +0000871 cancel_lookup(f, outarg.nodeid, in->unique);
Miklos Szeredib483c932001-10-29 14:57:57 +0000872}
873
874static void do_mkdir(struct fuse *f, struct fuse_in_header *in,
875 struct fuse_mkdir_in *inarg)
876{
877 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000878 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000879 char *path;
Miklos Szeredi30e093a2005-04-03 17:44:54 +0000880 char *name = PARAM_COMPAT(f, inarg, fuse_mkdir_in);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000881 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000882
Miklos Szeredi5e183482001-10-31 14:52:35 +0000883 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000884 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000885 if (path != NULL) {
886 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000887 printf("MKDIR %s\n", path);
888 fflush(stdout);
889 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000890 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000891 if (f->op.mkdir && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000892 res = f->op.mkdir(path, inarg->mode);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000893 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000894 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000895 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000896 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000897 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000898 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
899 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi38009022005-05-08 19:47:22 +0000900 cancel_lookup(f, outarg.nodeid, in->unique);
Miklos Szeredib483c932001-10-29 14:57:57 +0000901}
902
Miklos Szeredib5958612004-02-20 14:10:49 +0000903static void do_unlink(struct fuse *f, struct fuse_in_header *in, char *name)
Miklos Szeredib483c932001-10-29 14:57:57 +0000904{
905 int res;
906 char *path;
907
Miklos Szeredi5e183482001-10-31 14:52:35 +0000908 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000909 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000910 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000911 if (f->flags & FUSE_DEBUG) {
912 printf("UNLINK %s\n", path);
913 fflush(stdout);
914 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000915 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000916 if (f->op.unlink) {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000917 if (!(f->flags & FUSE_HARD_REMOVE) && is_open(f, in->nodeid, name))
918 res = hide_node(f, path, in->nodeid, name);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000919 else {
920 res = f->op.unlink(path);
921 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000922 remove_node(f, in->nodeid, name);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000923
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000924 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000925 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000926 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000927 }
Miklos Szeredib5958612004-02-20 14:10:49 +0000928 send_reply(f, in, res, NULL, 0);
929}
930
931static void do_rmdir(struct fuse *f, struct fuse_in_header *in, char *name)
932{
933 int res;
934 char *path;
935
936 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000937 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000938 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000939 if (f->flags & FUSE_DEBUG) {
940 printf("RMDIR %s\n", path);
941 fflush(stdout);
942 }
Miklos Szeredib5958612004-02-20 14:10:49 +0000943 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000944 if (f->op.rmdir) {
Miklos Szeredib5958612004-02-20 14:10:49 +0000945 res = f->op.rmdir(path);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000946 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000947 remove_node(f, in->nodeid, name);
Miklos Szeredib5958612004-02-20 14:10:49 +0000948 }
949 free(path);
950 }
Miklos Szeredib483c932001-10-29 14:57:57 +0000951 send_reply(f, in, res, NULL, 0);
952}
953
954static void do_symlink(struct fuse *f, struct fuse_in_header *in, char *name,
955 char *link)
956{
957 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000958 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000959 char *path;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000960 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000961
Miklos Szeredi5e183482001-10-31 14:52:35 +0000962 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000963 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000964 if (path != NULL) {
965 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000966 printf("SYMLINK %s\n", path);
967 fflush(stdout);
968 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000969 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000970 if (f->op.symlink && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000971 res = f->op.symlink(link, path);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000972 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000973 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000974 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000975 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000976 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000977 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
978 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi38009022005-05-08 19:47:22 +0000979 cancel_lookup(f, outarg.nodeid, in->unique);
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000980
Miklos Szeredib483c932001-10-29 14:57:57 +0000981}
982
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000983static void do_rename(struct fuse *f, struct fuse_in_header *in,
984 struct fuse_rename_in *inarg)
985{
986 int res;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000987 nodeid_t olddir = in->nodeid;
988 nodeid_t newdir = inarg->newdir;
Miklos Szeredi6bf8b682002-10-28 08:49:39 +0000989 char *oldname = PARAM(inarg);
990 char *newname = oldname + strlen(oldname) + 1;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000991 char *oldpath;
992 char *newpath;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000993
Miklos Szeredi5e183482001-10-31 14:52:35 +0000994 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000995 oldpath = get_path_name(f, olddir, oldname);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000996 if (oldpath != NULL) {
Miklos Szeredia181e612001-11-06 12:03:23 +0000997 newpath = get_path_name(f, newdir, newname);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000998 if (newpath != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000999 if (f->flags & FUSE_DEBUG) {
1000 printf("RENAME %s -> %s\n", oldpath, newpath);
1001 fflush(stdout);
1002 }
Miklos Szeredi5e183482001-10-31 14:52:35 +00001003 res = -ENOSYS;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001004 if (f->op.rename) {
1005 res = 0;
Miklos Szeredie5183742005-02-02 11:14:04 +00001006 if (!(f->flags & FUSE_HARD_REMOVE) &&
Miklos Szeredi2529ca22004-07-13 15:36:52 +00001007 is_open(f, newdir, newname))
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001008 res = hide_node(f, newpath, newdir, newname);
1009 if (res == 0) {
1010 res = f->op.rename(oldpath, newpath);
1011 if (res == 0)
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001012 res = rename_node(f, olddir, oldname, newdir, newname, 0);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001013 }
1014 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001015 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001016 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001017 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001018 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001019 send_reply(f, in, res, NULL, 0);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001020}
1021
1022static void do_link(struct fuse *f, struct fuse_in_header *in,
Miklos Szeredi5e183482001-10-31 14:52:35 +00001023 struct fuse_link_in *arg)
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001024{
1025 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +00001026 int res2;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001027 char *oldpath;
1028 char *newpath;
Miklos Szeredi76f65782004-02-19 16:55:40 +00001029 char *name = PARAM(arg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +00001030 struct fuse_entry_out outarg;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001031
Miklos Szeredi5e183482001-10-31 14:52:35 +00001032 res = -ENOENT;
Miklos Szeredied6b5dd2005-01-26 17:07:59 +00001033 oldpath = get_path(f, arg->oldnodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001034 if (oldpath != NULL) {
Miklos Szeredied6b5dd2005-01-26 17:07:59 +00001035 newpath = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001036 if (newpath != NULL) {
1037 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +00001038 printf("LINK %s\n", newpath);
1039 fflush(stdout);
1040 }
Miklos Szeredi5e183482001-10-31 14:52:35 +00001041 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001042 if (f->op.link && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +00001043 res = f->op.link(oldpath, newpath);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001044 if (res == 0)
Miklos Szeredied6b5dd2005-01-26 17:07:59 +00001045 res = lookup_path(f, in->nodeid, in->unique, name,
Miklos Szeredi76f65782004-02-19 16:55:40 +00001046 newpath, &outarg);
1047 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001048 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001049 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001050 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001051 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +00001052 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
1053 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi38009022005-05-08 19:47:22 +00001054 cancel_lookup(f, outarg.nodeid, in->unique);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001055}
1056
Miklos Szeredi5e183482001-10-31 14:52:35 +00001057static void do_open(struct fuse *f, struct fuse_in_header *in,
1058 struct fuse_open_in *arg)
1059{
1060 int res;
1061 char *path;
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001062 struct fuse_open_out outarg;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001063 struct fuse_file_info fi;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001064
Miklos Szeredied3c97c2005-02-15 17:04:50 +00001065 memset(&outarg, 0, sizeof(outarg));
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001066 memset(&fi, 0, sizeof(fi));
1067 fi.flags = arg->flags;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001068 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001069 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001070 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +00001071 res = -ENOSYS;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001072 if (f->op.open) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001073 if (!f->compat)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001074 res = f->op.open(path, &fi);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001075 else
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001076 res = ((struct fuse_operations_compat2 *) &f->op)->open(path, fi.flags);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001077 }
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +00001078 }
Miklos Szeredi73798f92004-07-12 15:55:11 +00001079 if (res == 0) {
1080 int res2;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001081 outarg.fh = fi.fh;
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001082 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001083 printf("OPEN[%lu] flags: 0x%x\n", fi.fh, arg->flags);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001084 fflush(stdout);
1085 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001086
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001087 pthread_mutex_lock(&f->lock);
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001088 res2 = send_reply(f, in, res, &outarg, SIZEOF_COMPAT(f, fuse_open_out));
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001089 if(res2 == -ENOENT) {
1090 /* The open syscall was interrupted, so it must be cancelled */
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001091 if(f->op.release) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001092 if (!f->compat)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001093 f->op.release(path, &fi);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001094 else
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001095 ((struct fuse_operations_compat2 *) &f->op)->release(path, fi.flags);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001096 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001097 } else {
1098 struct node *node = get_node(f, in->nodeid);
1099 node->open_count ++;
1100 }
Miklos Szeredi73798f92004-07-12 15:55:11 +00001101 pthread_mutex_unlock(&f->lock);
Miklos Szeredi73798f92004-07-12 15:55:11 +00001102 } else
1103 send_reply(f, in, res, NULL, 0);
1104
Miklos Szeredi9a31cca2004-06-26 21:11:25 +00001105 if (path)
1106 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001107}
1108
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001109static void do_flush(struct fuse *f, struct fuse_in_header *in,
1110 struct fuse_flush_in *arg)
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001111{
1112 char *path;
1113 int res;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001114 struct fuse_file_info fi;
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001115
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001116 memset(&fi, 0, sizeof(fi));
1117 fi.fh = arg->fh;
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001118 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001119 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001120 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001121 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001122 printf("FLUSH[%lu]\n", (unsigned long) arg->fh);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001123 fflush(stdout);
1124 }
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001125 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001126 if (f->op.flush)
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001127 res = f->op.flush(path, &fi);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001128 free(path);
1129 }
1130 send_reply(f, in, res, NULL, 0);
1131}
1132
Miklos Szeredi9478e862002-12-11 09:50:26 +00001133static void do_release(struct fuse *f, struct fuse_in_header *in,
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001134 struct fuse_release_in *arg)
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001135{
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001136 struct node *node;
1137 char *path;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001138 struct fuse_file_info fi;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001139 int unlink_hidden;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001140
1141 memset(&fi, 0, sizeof(fi));
1142 fi.flags = arg->flags;
1143 fi.fh = arg->fh;
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001144
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001145 pthread_mutex_lock(&f->lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001146 node = get_node(f, in->nodeid);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001147 assert(node->open_count > 0);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001148 --node->open_count;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001149 unlink_hidden = (node->is_hidden && !node->open_count);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001150 pthread_mutex_unlock(&f->lock);
1151
Miklos Szeredia13d9002004-11-02 17:32:03 +00001152 path = get_path(f, in->nodeid);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001153 if (f->flags & FUSE_DEBUG) {
1154 printf("RELEASE[%lu] flags: 0x%x\n", fi.fh, fi.flags);
1155 fflush(stdout);
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001156 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001157 if (f->op.release) {
1158 if (!f->compat)
1159 f->op.release(path ? path : "-", &fi);
1160 else if (path)
1161 ((struct fuse_operations_compat2 *) &f->op)->release(path, fi.flags);
1162 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001163
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001164 if(unlink_hidden && path)
1165 f->op.unlink(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001166
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001167 if (path)
1168 free(path);
1169
Miklos Szeredi556d03d2004-06-30 11:13:41 +00001170 send_reply(f, in, 0, NULL, 0);
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001171}
1172
Miklos Szeredi5e183482001-10-31 14:52:35 +00001173static void do_read(struct fuse *f, struct fuse_in_header *in,
1174 struct fuse_read_in *arg)
1175{
1176 int res;
1177 char *path;
Miklos Szerediab974562005-04-07 15:40:21 +00001178 size_t size;
1179 char *buf;
1180 struct fuse_file_info fi;
1181
1182 buf = (char *) malloc(arg->size);
1183 if (buf == NULL) {
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001184 send_reply(f, in, -ENOMEM, NULL, 0);
Miklos Szerediab974562005-04-07 15:40:21 +00001185 return;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001186 }
Miklos Szerediab974562005-04-07 15:40:21 +00001187
1188 memset(&fi, 0, sizeof(fi));
1189 fi.fh = arg->fh;
1190
1191 res = -ENOENT;
1192 path = get_path(f, in->nodeid);
1193 if (path != NULL) {
1194 if (f->flags & FUSE_DEBUG) {
1195 printf("READ[%lu] %u bytes from %llu\n",
1196 (unsigned long) arg->fh, arg->size, arg->offset);
1197 fflush(stdout);
1198 }
1199
1200 res = -ENOSYS;
1201 if (f->op.read)
1202 res = f->op.read(path, buf, arg->size, arg->offset, &fi);
1203 free(path);
1204 }
1205
1206 size = 0;
1207 if (res >= 0) {
1208 size = res;
1209 res = 0;
1210 if (f->flags & FUSE_DEBUG) {
1211 printf(" READ[%lu] %u bytes\n", (unsigned long) arg->fh,
1212 size);
1213 fflush(stdout);
1214 }
1215 }
1216
1217 send_reply(f, in, res, buf, size);
1218 free(buf);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001219}
Miklos Szeredib483c932001-10-29 14:57:57 +00001220
Miklos Szeredia181e612001-11-06 12:03:23 +00001221static void do_write(struct fuse *f, struct fuse_in_header *in,
1222 struct fuse_write_in *arg)
1223{
1224 int res;
1225 char *path;
Miklos Szerediad051c32004-07-02 09:22:50 +00001226 struct fuse_write_out outarg;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001227 struct fuse_file_info fi;
1228
1229 memset(&fi, 0, sizeof(fi));
1230 fi.fh = arg->fh;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001231 fi.writepage = arg->write_flags & 1;
Miklos Szeredia181e612001-11-06 12:03:23 +00001232
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001233 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001234 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001235 if (path != NULL) {
1236 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi1eea0322004-09-27 18:50:11 +00001237 printf("WRITE%s[%lu] %u bytes to %llu\n",
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001238 (arg->write_flags & 1) ? "PAGE" : "",
1239 (unsigned long) arg->fh, arg->size, arg->offset);
Miklos Szeredi6ebe2342002-01-06 21:44:16 +00001240 fflush(stdout);
1241 }
1242
Miklos Szeredia181e612001-11-06 12:03:23 +00001243 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001244 if (f->op.write)
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001245 res = f->op.write(path, PARAM(arg), arg->size, arg->offset, &fi);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001246 free(path);
Miklos Szeredia181e612001-11-06 12:03:23 +00001247 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001248
Miklos Szerediab974562005-04-07 15:40:21 +00001249 memset(&outarg, 0, sizeof(outarg));
Miklos Szeredie5183742005-02-02 11:14:04 +00001250 if (res >= 0) {
Miklos Szerediad051c32004-07-02 09:22:50 +00001251 outarg.size = res;
1252 res = 0;
Miklos Szeredia181e612001-11-06 12:03:23 +00001253 }
1254
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001255 send_reply(f, in, res, &outarg, SIZEOF_COMPAT(f, fuse_write_out));
Miklos Szeredia181e612001-11-06 12:03:23 +00001256}
1257
Miklos Szeredi77f39942004-03-25 11:17:52 +00001258static int default_statfs(struct statfs *buf)
1259{
1260 buf->f_namelen = 255;
1261 buf->f_bsize = 512;
1262 return 0;
1263}
1264
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001265static void convert_statfs_compat(struct fuse_statfs_compat1 *compatbuf,
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001266 struct statfs *statfs)
1267{
1268 statfs->f_bsize = compatbuf->block_size;
1269 statfs->f_blocks = compatbuf->blocks;
1270 statfs->f_bfree = compatbuf->blocks_free;
1271 statfs->f_bavail = compatbuf->blocks_free;
1272 statfs->f_files = compatbuf->files;
1273 statfs->f_ffree = compatbuf->files_free;
1274 statfs->f_namelen = compatbuf->namelen;
1275}
1276
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001277static void convert_statfs(struct statfs *statfs, struct fuse_kstatfs *kstatfs)
1278{
1279 kstatfs->bsize = statfs->f_bsize;
1280 kstatfs->blocks = statfs->f_blocks;
1281 kstatfs->bfree = statfs->f_bfree;
1282 kstatfs->bavail = statfs->f_bavail;
1283 kstatfs->files = statfs->f_files;
1284 kstatfs->ffree = statfs->f_ffree;
1285 kstatfs->namelen = statfs->f_namelen;
1286}
1287
Mark Glinesd84b39a2002-01-07 16:32:02 +00001288static void do_statfs(struct fuse *f, struct fuse_in_header *in)
1289{
1290 int res;
Mark Glinesd84b39a2002-01-07 16:32:02 +00001291 struct fuse_statfs_out arg;
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001292 struct statfs buf;
Mark Glinesd84b39a2002-01-07 16:32:02 +00001293
Miklos Szeredi77f39942004-03-25 11:17:52 +00001294 memset(&buf, 0, sizeof(struct statfs));
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001295 if (f->op.statfs) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001296 if (!f->compat || f->compat > 11)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001297 res = f->op.statfs("/", &buf);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001298 else {
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001299 struct fuse_statfs_compat1 compatbuf;
1300 memset(&compatbuf, 0, sizeof(struct fuse_statfs_compat1));
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001301 res = ((struct fuse_operations_compat1 *) &f->op)->statfs(&compatbuf);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001302 if (res == 0)
1303 convert_statfs_compat(&compatbuf, &buf);
1304 }
1305 }
Miklos Szeredi77f39942004-03-25 11:17:52 +00001306 else
1307 res = default_statfs(&buf);
1308
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001309 if (res == 0)
Miklos Szeredi77f39942004-03-25 11:17:52 +00001310 convert_statfs(&buf, &arg.st);
Miklos Szeredi4b2bef42002-01-09 12:23:27 +00001311
Mark Glinesd84b39a2002-01-07 16:32:02 +00001312 send_reply(f, in, res, &arg, sizeof(arg));
1313}
1314
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001315static void do_fsync(struct fuse *f, struct fuse_in_header *in,
1316 struct fuse_fsync_in *inarg)
1317{
1318 int res;
1319 char *path;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001320 struct fuse_file_info fi;
1321
1322 memset(&fi, 0, sizeof(fi));
1323 fi.fh = inarg->fh;
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001324
1325 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001326 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001327 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001328 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001329 printf("FSYNC[%lu]\n", (unsigned long) inarg->fh);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001330 fflush(stdout);
1331 }
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001332 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001333 if (f->op.fsync)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001334 res = f->op.fsync(path, inarg->fsync_flags & 1, &fi);
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001335 free(path);
1336 }
1337 send_reply(f, in, res, NULL, 0);
1338}
1339
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001340static void do_setxattr(struct fuse *f, struct fuse_in_header *in,
1341 struct fuse_setxattr_in *arg)
1342{
1343 int res;
1344 char *path;
1345 char *name = PARAM(arg);
1346 unsigned char *value = name + strlen(name) + 1;
1347
1348 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001349 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001350 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001351 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001352 if (f->op.setxattr)
1353 res = f->op.setxattr(path, name, value, arg->size, arg->flags);
1354 free(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001355 }
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001356 send_reply(f, in, res, NULL, 0);
1357}
1358
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001359static int common_getxattr(struct fuse *f, struct fuse_in_header *in,
1360 const char *name, char *value, size_t size)
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001361{
1362 int res;
1363 char *path;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001364
1365 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001366 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001367 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001368 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001369 if (f->op.getxattr)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001370 res = f->op.getxattr(path, name, value, size);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001371 free(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001372 }
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001373 return res;
1374}
1375
1376static void do_getxattr_read(struct fuse *f, struct fuse_in_header *in,
1377 const char *name, size_t size)
1378{
1379 int res;
Miklos Szerediab974562005-04-07 15:40:21 +00001380 char *value = (char *) malloc(size);
1381 if (value == NULL) {
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001382 send_reply(f, in, -ENOMEM, NULL, 0);
Miklos Szerediab974562005-04-07 15:40:21 +00001383 return;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001384 }
Miklos Szerediab974562005-04-07 15:40:21 +00001385 res = common_getxattr(f, in, name, value, size);
1386 size = 0;
1387 if (res > 0) {
1388 size = res;
1389 res = 0;
1390 }
1391 send_reply(f, in, res, value, size);
1392 free(value);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001393}
1394
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001395static void do_getxattr_size(struct fuse *f, struct fuse_in_header *in,
1396 const char *name)
1397{
1398 int res;
1399 struct fuse_getxattr_out arg;
1400
Miklos Szerediab974562005-04-07 15:40:21 +00001401 memset(&arg, 0, sizeof(arg));
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001402 res = common_getxattr(f, in, name, NULL, 0);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001403 if (res >= 0) {
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001404 arg.size = res;
1405 res = 0;
1406 }
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001407 send_reply(f, in, res, &arg, SIZEOF_COMPAT(f, fuse_getxattr_out));
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001408}
1409
1410static void do_getxattr(struct fuse *f, struct fuse_in_header *in,
1411 struct fuse_getxattr_in *arg)
1412{
1413 char *name = PARAM(arg);
Miklos Szeredie5183742005-02-02 11:14:04 +00001414
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001415 if (arg->size)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001416 do_getxattr_read(f, in, name, arg->size);
1417 else
1418 do_getxattr_size(f, in, name);
1419}
1420
1421static int common_listxattr(struct fuse *f, struct fuse_in_header *in,
1422 char *list, size_t size)
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001423{
1424 int res;
1425 char *path;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001426
1427 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001428 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001429 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001430 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001431 if (f->op.listxattr)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001432 res = f->op.listxattr(path, list, size);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001433 free(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001434 }
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001435 return res;
1436}
1437
1438static void do_listxattr_read(struct fuse *f, struct fuse_in_header *in,
1439 size_t size)
1440{
1441 int res;
Miklos Szerediab974562005-04-07 15:40:21 +00001442 char *list = (char *) malloc(size);
1443 if (list == NULL) {
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001444 send_reply(f, in, -ENOMEM, NULL, 0);
Miklos Szerediab974562005-04-07 15:40:21 +00001445 return;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001446 }
Miklos Szerediab974562005-04-07 15:40:21 +00001447 res = common_listxattr(f, in, list, size);
1448 size = 0;
1449 if (res > 0) {
1450 size = res;
1451 res = 0;
1452 }
1453 send_reply(f, in, res, list, size);
1454 free(list);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001455}
1456
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001457static void do_listxattr_size(struct fuse *f, struct fuse_in_header *in)
1458{
1459 int res;
1460 struct fuse_getxattr_out arg;
1461
Miklos Szerediab974562005-04-07 15:40:21 +00001462 memset(&arg, 0, sizeof(arg));
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001463 res = common_listxattr(f, in, NULL, 0);
1464 if (res >= 0) {
1465 arg.size = res;
1466 res = 0;
1467 }
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001468 send_reply(f, in, res, &arg, SIZEOF_COMPAT(f, fuse_getxattr_out));
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001469}
1470
1471static void do_listxattr(struct fuse *f, struct fuse_in_header *in,
1472 struct fuse_getxattr_in *arg)
1473{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001474 if (arg->size)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001475 do_listxattr_read(f, in, arg->size);
1476 else
1477 do_listxattr_size(f, in);
1478}
1479
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001480static void do_removexattr(struct fuse *f, struct fuse_in_header *in,
1481 char *name)
1482{
1483 int res;
1484 char *path;
1485
1486 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001487 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001488 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001489 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001490 if (f->op.removexattr)
1491 res = f->op.removexattr(path, name);
1492 free(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001493 }
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001494 send_reply(f, in, res, NULL, 0);
1495}
1496
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001497static void do_init(struct fuse *f, struct fuse_in_header *in,
1498 struct fuse_init_in_out *arg)
1499{
1500 struct fuse_init_in_out outarg;
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001501
1502 if (in->padding == 5) {
1503 arg->minor = arg->major;
1504 arg->major = in->padding;
1505 }
1506
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001507 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001508 printf("INIT: %u.%u\n", arg->major, arg->minor);
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001509 fflush(stdout);
1510 }
1511 f->got_init = 1;
Miklos Szeredi159bd7e2005-02-28 17:32:16 +00001512 if (f->op.init)
1513 f->user_data = f->op.init();
1514
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001515 if (arg->major == 5) {
1516 f->major = 5;
1517 f->minor = 1;
Miklos Szeredi38009022005-05-08 19:47:22 +00001518 } else if (arg->major == 6) {
1519 f->major = 6;
1520 f->minor = 1;
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001521 } else {
1522 f->major = FUSE_KERNEL_VERSION;
1523 f->minor = FUSE_KERNEL_MINOR_VERSION;
1524 }
1525 memset(&outarg, 0, sizeof(outarg));
1526 outarg.major = f->major;
1527 outarg.minor = f->minor;
1528
1529 if (f->flags & FUSE_DEBUG) {
1530 printf(" INIT: %u.%u\n", outarg.major, outarg.minor);
1531 fflush(stdout);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001532 }
1533
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001534 send_reply(f, in, 0, &outarg, sizeof(outarg));
1535}
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001536
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001537static struct fuse_dirhandle *get_dirhandle(unsigned long fh)
1538{
1539 return (struct fuse_dirhandle *) fh;
1540}
1541
1542static void do_opendir(struct fuse *f, struct fuse_in_header *in,
1543 struct fuse_open_in *arg)
1544{
1545 int res;
1546 struct fuse_open_out outarg;
1547 struct fuse_dirhandle *dh;
1548
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001549 dh = (struct fuse_dirhandle *) malloc(sizeof(struct fuse_dirhandle));
1550 if (dh == NULL) {
1551 send_reply(f, in, -ENOMEM, NULL, 0);
1552 return;
1553 }
1554 memset(dh, 0, sizeof(struct fuse_dirhandle));
1555 dh->fuse = f;
1556 dh->contents = NULL;
1557 dh->len = 0;
1558 dh->filled = 0;
Miklos Szerediab974562005-04-07 15:40:21 +00001559 mutex_init(&dh->lock);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001560
1561 memset(&outarg, 0, sizeof(outarg));
1562 outarg.fh = (unsigned long) dh;
1563
Miklos Szeredif43f0632005-02-28 11:46:56 +00001564 if (f->op.opendir) {
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001565 struct fuse_file_info fi;
Miklos Szeredif43f0632005-02-28 11:46:56 +00001566 char *path;
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001567
1568 memset(&fi, 0, sizeof(fi));
1569 fi.flags = arg->flags;
1570
Miklos Szeredif43f0632005-02-28 11:46:56 +00001571 res = -ENOENT;
1572 path = get_path(f, in->nodeid);
1573 if (path != NULL) {
Miklos Szeredif43f0632005-02-28 11:46:56 +00001574 res = f->op.opendir(path, &fi);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001575 dh->fh = fi.fh;
Miklos Szeredif43f0632005-02-28 11:46:56 +00001576 }
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001577 if (res == 0) {
1578 int res2;
1579 pthread_mutex_lock(&f->lock);
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001580 res2 = send_reply(f, in, res, &outarg, SIZEOF_COMPAT(f, fuse_open_out));
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001581 if(res2 == -ENOENT) {
1582 /* The opendir syscall was interrupted, so it must be
1583 cancelled */
1584 if(f->op.releasedir)
1585 f->op.releasedir(path, &fi);
Miklos Szerediab974562005-04-07 15:40:21 +00001586 pthread_mutex_destroy(&dh->lock);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001587 free(dh);
1588 }
1589 pthread_mutex_unlock(&f->lock);
1590 } else {
Miklos Szeredif43f0632005-02-28 11:46:56 +00001591 send_reply(f, in, res, NULL, 0);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001592 free(dh);
Miklos Szeredif43f0632005-02-28 11:46:56 +00001593 }
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001594 free(path);
Miklos Szerediab974562005-04-07 15:40:21 +00001595 } else
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001596 send_reply(f, in, 0, &outarg, SIZEOF_COMPAT(f, fuse_open_out));
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001597}
1598
Miklos Szerediab974562005-04-07 15:40:21 +00001599static int fill_dir_common(struct fuse_dirhandle *dh, const char *name,
1600 int type, ino_t ino, off_t off)
1601{
1602 unsigned namelen = strlen(name);
1603 unsigned entlen;
1604 unsigned entsize;
1605 unsigned padlen;
1606 unsigned newlen;
1607 unsigned char *newptr;
1608
1609 if (!(dh->fuse->flags & FUSE_USE_INO))
1610 ino = (ino_t) -1;
1611
1612 if (namelen > FUSE_NAME_MAX)
1613 namelen = FUSE_NAME_MAX;
1614 else if (!namelen) {
1615 dh->error = -EIO;
1616 return 1;
1617 }
1618
1619 entlen = (dh->fuse->major == 5 ?
1620 FUSE_NAME_OFFSET_COMPAT5 : FUSE_NAME_OFFSET) + namelen;
1621 entsize = FUSE_DIRENT_ALIGN(entlen);
1622 padlen = entsize - entlen;
1623 newlen = dh->len + entsize;
1624 if (off && dh->fuse->major != 5) {
1625 dh->filled = 0;
1626 if (newlen > dh->needlen)
1627 return 1;
1628 }
1629
1630 newptr = realloc(dh->contents, newlen);
1631 if (!newptr) {
1632 dh->error = -ENOMEM;
1633 return 1;
1634 }
1635 dh->contents = newptr;
1636 if (dh->fuse->major == 5) {
1637 struct fuse_dirent_compat5 *dirent;
1638 dirent = (struct fuse_dirent_compat5 *) (dh->contents + dh->len);
1639 dirent->ino = ino;
1640 dirent->namelen = namelen;
1641 dirent->type = type;
1642 strncpy(dirent->name, name, namelen);
1643 } else {
1644 struct fuse_dirent *dirent;
1645 dirent = (struct fuse_dirent *) (dh->contents + dh->len);
1646 dirent->ino = ino;
1647 dirent->off = off ? off : newlen;
1648 dirent->namelen = namelen;
1649 dirent->type = type;
1650 strncpy(dirent->name, name, namelen);
1651 }
1652 if (padlen)
1653 memset(dh->contents + dh->len + entlen, 0, padlen);
1654 dh->len = newlen;
1655 return 0;
1656}
1657
1658static int fill_dir(void *buf, const char *name, const struct stat *stat,
1659 off_t off)
1660{
1661 int type = stat ? (stat->st_mode & 0170000) >> 12 : 0;
1662 ino_t ino = stat ? stat->st_ino : (ino_t) -1;
1663 return fill_dir_common(buf, name, type, ino, off);
1664}
1665
1666static int fill_dir_old(struct fuse_dirhandle *dh, const char *name, int type,
1667 ino_t ino)
1668{
1669 fill_dir_common(dh, name, type, ino, 0);
1670 return dh->error;
1671}
1672
1673static int readdir_fill(struct fuse *f, struct fuse_in_header *in,
1674 struct fuse_read_in *arg, struct fuse_dirhandle *dh)
1675{
1676 int err = -ENOENT;
1677 char *path = get_path(f, in->nodeid);
1678 if (path != NULL) {
1679 struct fuse_file_info fi;
1680
1681 memset(&fi, 0, sizeof(fi));
1682 fi.fh = dh->fh;
1683
1684 dh->len = 0;
1685 dh->error = 0;
1686 dh->needlen = arg->size;
1687 dh->filled = 1;
1688 err = -ENOSYS;
1689 if (f->op.readdir) {
1690 off_t offset = f->major == 5 ? 0 : arg->offset;
1691 err = f->op.readdir(path, dh, fill_dir, offset, &fi);
1692 } else if (f->op.getdir)
1693 err = f->op.getdir(path, dh, fill_dir_old);
1694 if (!err)
1695 err = dh->error;
1696 if (err)
1697 dh->filled = 0;
1698 free(path);
1699 }
1700 return err;
1701}
1702
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001703static void do_readdir(struct fuse *f, struct fuse_in_header *in,
1704 struct fuse_read_in *arg)
1705{
Miklos Szerediab974562005-04-07 15:40:21 +00001706 int err = 0;
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001707 struct fuse_dirhandle *dh = get_dirhandle(arg->fh);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001708 size_t size = 0;
Miklos Szerediab974562005-04-07 15:40:21 +00001709 unsigned char *buf = NULL;
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001710
Miklos Szerediab974562005-04-07 15:40:21 +00001711 pthread_mutex_lock(&dh->lock);
1712 if (!dh->filled) {
1713 err = readdir_fill(f, in, arg, dh);
1714 if (err)
1715 goto out;
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001716 }
Miklos Szerediab974562005-04-07 15:40:21 +00001717 if (dh->filled) {
Miklos Szeredib92d9782005-02-07 16:10:49 +00001718 if (arg->offset < dh->len) {
1719 size = arg->size;
1720 if (arg->offset + size > dh->len)
1721 size = dh->len - arg->offset;
Miklos Szerediab974562005-04-07 15:40:21 +00001722 buf = dh->contents + arg->offset;
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001723 }
Miklos Szerediab974562005-04-07 15:40:21 +00001724 } else {
1725 size = dh->len;
1726 buf = dh->contents;
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001727 }
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001728
Miklos Szerediab974562005-04-07 15:40:21 +00001729 out:
1730 send_reply(f, in, err, buf, size);
1731 pthread_mutex_unlock(&dh->lock);
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001732}
1733
1734static void do_releasedir(struct fuse *f, struct fuse_in_header *in,
1735 struct fuse_release_in *arg)
1736{
1737 struct fuse_dirhandle *dh = get_dirhandle(arg->fh);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001738 if (f->op.releasedir) {
1739 char *path;
1740 struct fuse_file_info fi;
Miklos Szerediab974562005-04-07 15:40:21 +00001741
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001742 memset(&fi, 0, sizeof(fi));
1743 fi.fh = dh->fh;
1744
1745 path = get_path(f, in->nodeid);
1746 f->op.releasedir(path ? path : "-", &fi);
1747 free(path);
1748 }
Miklos Szerediab974562005-04-07 15:40:21 +00001749 pthread_mutex_lock(&dh->lock);
1750 pthread_mutex_unlock(&dh->lock);
1751 pthread_mutex_destroy(&dh->lock);
Miklos Szeredib92d9782005-02-07 16:10:49 +00001752 free(dh->contents);
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001753 free(dh);
1754 send_reply(f, in, 0, NULL, 0);
1755}
1756
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001757static void do_fsyncdir(struct fuse *f, struct fuse_in_header *in,
1758 struct fuse_fsync_in *inarg)
1759{
1760 int res;
1761 char *path;
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001762 struct fuse_dirhandle *dh = get_dirhandle(inarg->fh);
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001763 struct fuse_file_info fi;
1764
1765 memset(&fi, 0, sizeof(fi));
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001766 fi.fh = dh->fh;
1767
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001768 res = -ENOENT;
1769 path = get_path(f, in->nodeid);
1770 if (path != NULL) {
1771 res = -ENOSYS;
1772 if (f->op.fsyncdir)
1773 res = f->op.fsyncdir(path, inarg->fsync_flags & 1, &fi);
1774 free(path);
1775 }
1776 send_reply(f, in, res, NULL, 0);
1777}
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001778
Miklos Szeredi43696432001-11-18 19:15:05 +00001779static void free_cmd(struct fuse_cmd *cmd)
1780{
1781 free(cmd->buf);
1782 free(cmd);
1783}
1784
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001785void fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd)
Miklos Szeredia181e612001-11-06 12:03:23 +00001786{
Miklos Szeredia181e612001-11-06 12:03:23 +00001787 struct fuse_in_header *in = (struct fuse_in_header *) cmd->buf;
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001788 void *inarg = cmd->buf + SIZEOF_COMPAT(f, fuse_in_header);
Miklos Szeredid169f312004-09-22 08:48:26 +00001789 struct fuse_context *ctx = fuse_get_context();
Miklos Szeredia181e612001-11-06 12:03:23 +00001790
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001791 fuse_dec_avail(f);
Miklos Szeredi33232032001-11-19 17:55:51 +00001792
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001793 if ((f->flags & FUSE_DEBUG)) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001794 printf("unique: %llu, opcode: %s (%i), nodeid: %lu, insize: %i\n",
1795 in->unique, opname(in->opcode), in->opcode,
1796 (unsigned long) in->nodeid, cmd->buflen);
Miklos Szeredic0938ea2001-11-07 12:35:06 +00001797 fflush(stdout);
1798 }
Miklos Szeredife25def2001-12-20 15:38:05 +00001799
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001800 if (!f->got_init && in->opcode != FUSE_INIT) {
1801 /* Old kernel version probably */
1802 send_reply(f, in, -EPROTO, NULL, 0);
1803 goto out;
1804 }
1805
Miklos Szeredi0111f9d2005-04-22 12:04:55 +00001806 if ((f->flags & FUSE_ALLOW_ROOT) && in->uid != f->owner && in->uid != 0 &&
1807 in->opcode != FUSE_INIT && in->opcode != FUSE_READ &&
1808 in->opcode != FUSE_WRITE && in->opcode != FUSE_FSYNC &&
1809 in->opcode != FUSE_RELEASE && in->opcode != FUSE_READDIR &&
1810 in->opcode != FUSE_FSYNCDIR && in->opcode != FUSE_RELEASEDIR) {
1811 send_reply(f, in, -EACCES, NULL, 0);
1812 goto out;
1813 }
1814
Miklos Szeredid169f312004-09-22 08:48:26 +00001815 ctx->fuse = f;
Miklos Szeredife25def2001-12-20 15:38:05 +00001816 ctx->uid = in->uid;
1817 ctx->gid = in->gid;
Miklos Szeredi1f18db52004-09-27 06:54:49 +00001818 ctx->pid = in->pid;
Miklos Szeredi159bd7e2005-02-28 17:32:16 +00001819 ctx->private_data = f->user_data;
Miklos Szeredie5183742005-02-02 11:14:04 +00001820
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001821 switch (in->opcode) {
Miklos Szeredia181e612001-11-06 12:03:23 +00001822 case FUSE_LOOKUP:
1823 do_lookup(f, in, (char *) inarg);
1824 break;
1825
Miklos Szeredia181e612001-11-06 12:03:23 +00001826 case FUSE_GETATTR:
1827 do_getattr(f, in);
1828 break;
1829
1830 case FUSE_SETATTR:
1831 do_setattr(f, in, (struct fuse_setattr_in *) inarg);
1832 break;
1833
1834 case FUSE_READLINK:
1835 do_readlink(f, in);
1836 break;
1837
Miklos Szeredia181e612001-11-06 12:03:23 +00001838 case FUSE_MKNOD:
1839 do_mknod(f, in, (struct fuse_mknod_in *) inarg);
1840 break;
Miklos Szeredie5183742005-02-02 11:14:04 +00001841
Miklos Szeredia181e612001-11-06 12:03:23 +00001842 case FUSE_MKDIR:
1843 do_mkdir(f, in, (struct fuse_mkdir_in *) inarg);
1844 break;
Miklos Szeredie5183742005-02-02 11:14:04 +00001845
Miklos Szeredia181e612001-11-06 12:03:23 +00001846 case FUSE_UNLINK:
Miklos Szeredib5958612004-02-20 14:10:49 +00001847 do_unlink(f, in, (char *) inarg);
1848 break;
1849
Miklos Szeredia181e612001-11-06 12:03:23 +00001850 case FUSE_RMDIR:
Miklos Szeredib5958612004-02-20 14:10:49 +00001851 do_rmdir(f, in, (char *) inarg);
Miklos Szeredia181e612001-11-06 12:03:23 +00001852 break;
1853
1854 case FUSE_SYMLINK:
Miklos Szeredie5183742005-02-02 11:14:04 +00001855 do_symlink(f, in, (char *) inarg,
Miklos Szeredia181e612001-11-06 12:03:23 +00001856 ((char *) inarg) + strlen((char *) inarg) + 1);
1857 break;
1858
1859 case FUSE_RENAME:
1860 do_rename(f, in, (struct fuse_rename_in *) inarg);
1861 break;
Miklos Szeredie5183742005-02-02 11:14:04 +00001862
Miklos Szeredia181e612001-11-06 12:03:23 +00001863 case FUSE_LINK:
1864 do_link(f, in, (struct fuse_link_in *) inarg);
1865 break;
1866
1867 case FUSE_OPEN:
1868 do_open(f, in, (struct fuse_open_in *) inarg);
1869 break;
1870
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001871 case FUSE_FLUSH:
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001872 do_flush(f, in, (struct fuse_flush_in *) inarg);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001873 break;
1874
Miklos Szeredi9478e862002-12-11 09:50:26 +00001875 case FUSE_RELEASE:
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001876 do_release(f, in, (struct fuse_release_in *) inarg);
Miklos Szeredi9478e862002-12-11 09:50:26 +00001877 break;
1878
Miklos Szeredia181e612001-11-06 12:03:23 +00001879 case FUSE_READ:
1880 do_read(f, in, (struct fuse_read_in *) inarg);
1881 break;
1882
1883 case FUSE_WRITE:
1884 do_write(f, in, (struct fuse_write_in *) inarg);
1885 break;
1886
Mark Glinesd84b39a2002-01-07 16:32:02 +00001887 case FUSE_STATFS:
1888 do_statfs(f, in);
1889 break;
1890
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001891 case FUSE_FSYNC:
1892 do_fsync(f, in, (struct fuse_fsync_in *) inarg);
1893 break;
1894
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001895 case FUSE_SETXATTR:
1896 do_setxattr(f, in, (struct fuse_setxattr_in *) inarg);
1897 break;
1898
1899 case FUSE_GETXATTR:
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001900 do_getxattr(f, in, (struct fuse_getxattr_in *) inarg);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001901 break;
1902
1903 case FUSE_LISTXATTR:
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001904 do_listxattr(f, in, (struct fuse_getxattr_in *) inarg);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001905 break;
1906
1907 case FUSE_REMOVEXATTR:
1908 do_removexattr(f, in, (char *) inarg);
1909 break;
1910
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001911 case FUSE_INIT:
1912 do_init(f, in, (struct fuse_init_in_out *) inarg);
1913 break;
1914
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001915 case FUSE_OPENDIR:
1916 do_opendir(f, in, (struct fuse_open_in *) inarg);
1917 break;
1918
1919 case FUSE_READDIR:
1920 do_readdir(f, in, (struct fuse_read_in *) inarg);
1921 break;
1922
1923 case FUSE_RELEASEDIR:
1924 do_releasedir(f, in, (struct fuse_release_in *) inarg);
1925 break;
1926
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001927 case FUSE_FSYNCDIR:
1928 do_fsyncdir(f, in, (struct fuse_fsync_in *) inarg);
1929 break;
1930
Miklos Szeredia181e612001-11-06 12:03:23 +00001931 default:
Miklos Szeredi43696432001-11-18 19:15:05 +00001932 send_reply(f, in, -ENOSYS, NULL, 0);
Miklos Szeredia181e612001-11-06 12:03:23 +00001933 }
Miklos Szeredi43696432001-11-18 19:15:05 +00001934
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001935 out:
Miklos Szeredi43696432001-11-18 19:15:05 +00001936 free_cmd(cmd);
Miklos Szeredia181e612001-11-06 12:03:23 +00001937}
1938
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001939int fuse_exited(struct fuse* f)
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001940{
1941 return f->exited;
1942}
1943
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001944struct fuse_cmd *fuse_read_cmd(struct fuse *f)
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001945{
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001946 ssize_t res;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001947 struct fuse_cmd *cmd;
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001948 struct fuse_in_header *in;
1949 void *inarg;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001950
Miklos Szeredi43696432001-11-18 19:15:05 +00001951 cmd = (struct fuse_cmd *) malloc(sizeof(struct fuse_cmd));
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001952 if (cmd == NULL) {
1953 fprintf(stderr, "fuse: failed to allocate cmd in read\n");
1954 return NULL;
1955 }
Miklos Szeredi43696432001-11-18 19:15:05 +00001956 cmd->buf = (char *) malloc(FUSE_MAX_IN);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001957 if (cmd->buf == NULL) {
1958 fprintf(stderr, "fuse: failed to allocate read buffer\n");
1959 free(cmd);
1960 return NULL;
1961 }
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001962 in = (struct fuse_in_header *) cmd->buf;
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001963 inarg = cmd->buf + SIZEOF_COMPAT(f, fuse_in_header);
Miklos Szeredi43696432001-11-18 19:15:05 +00001964
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001965 res = read(f->fd, cmd->buf, FUSE_MAX_IN);
1966 if (res == -1) {
1967 free_cmd(cmd);
Miklos Szeredie56818b2004-12-12 11:45:24 +00001968 if (fuse_exited(f) || errno == EINTR || errno == ENOENT)
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001969 return NULL;
Miklos Szeredie5183742005-02-02 11:14:04 +00001970
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001971 /* ENODEV means we got unmounted, so we silenty return failure */
1972 if (errno != ENODEV) {
1973 /* BAD... This will happen again */
1974 perror("fuse: reading device");
1975 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001976
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001977 fuse_exit(f);
1978 return NULL;
1979 }
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001980 if ((size_t) res < SIZEOF_COMPAT(f, fuse_in_header)) {
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001981 free_cmd(cmd);
1982 /* Cannot happen */
1983 fprintf(stderr, "short read on fuse device\n");
1984 fuse_exit(f);
1985 return NULL;
1986 }
1987 cmd->buflen = res;
Miklos Szeredie5183742005-02-02 11:14:04 +00001988
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001989 /* Forget is special, it can be done without messing with threads. */
1990 if (in->opcode == FUSE_FORGET) {
1991 do_forget(f, in, (struct fuse_forget_in *) inarg);
1992 free_cmd(cmd);
1993 return NULL;
1994 }
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001995
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001996 return cmd;
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001997}
1998
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001999int fuse_loop(struct fuse *f)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002000{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00002001 if (f == NULL)
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002002 return -1;
Miklos Szeredic40748a2004-02-20 16:38:45 +00002003
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00002004 while (1) {
Miklos Szeredi0f48a262002-12-05 14:23:01 +00002005 struct fuse_cmd *cmd;
2006
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002007 if (fuse_exited(f))
Miklos Szeredi874e3c12004-11-01 23:15:20 +00002008 break;
Miklos Szeredi0f48a262002-12-05 14:23:01 +00002009
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002010 cmd = fuse_read_cmd(f);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00002011 if (cmd == NULL)
Miklos Szeredi0f48a262002-12-05 14:23:01 +00002012 continue;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002013
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002014 fuse_process_cmd(f, cmd);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002015 }
Miklos Szeredi874e3c12004-11-01 23:15:20 +00002016 f->exited = 0;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002017 return 0;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002018}
2019
Miklos Szeredi891b8742004-07-29 09:27:49 +00002020int fuse_invalidate(struct fuse *f, const char *path)
2021{
Miklos Szeredie56818b2004-12-12 11:45:24 +00002022 (void) f;
2023 (void) path;
2024 return -EINVAL;
Miklos Szeredi891b8742004-07-29 09:27:49 +00002025}
2026
Miklos Szeredi0f48a262002-12-05 14:23:01 +00002027void fuse_exit(struct fuse *f)
2028{
2029 f->exited = 1;
2030}
2031
Miklos Szeredid169f312004-09-22 08:48:26 +00002032struct fuse_context *fuse_get_context()
Miklos Szeredi2e50d432001-12-20 12:17:25 +00002033{
Miklos Szeredid169f312004-09-22 08:48:26 +00002034 static struct fuse_context context;
2035 if (fuse_getcontext)
2036 return fuse_getcontext();
Miklos Szeredi2e50d432001-12-20 12:17:25 +00002037 else
Miklos Szeredid169f312004-09-22 08:48:26 +00002038 return &context;
2039}
2040
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002041void fuse_set_getcontext_func(struct fuse_context *(*func)(void))
Miklos Szeredid169f312004-09-22 08:48:26 +00002042{
2043 fuse_getcontext = func;
Miklos Szeredi2e50d432001-12-20 12:17:25 +00002044}
2045
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002046int fuse_is_lib_option(const char *opt)
2047{
2048 if (strcmp(opt, "debug") == 0 ||
Miklos Szeredia13d9002004-11-02 17:32:03 +00002049 strcmp(opt, "hard_remove") == 0 ||
Miklos Szeredi0111f9d2005-04-22 12:04:55 +00002050 strcmp(opt, "use_ino") == 0 ||
2051 strcmp(opt, "allow_root") == 0)
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002052 return 1;
2053 else
2054 return 0;
2055}
2056
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002057static int parse_lib_opts(struct fuse *f, const char *opts)
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002058{
2059 if (opts) {
2060 char *xopts = strdup(opts);
2061 char *s = xopts;
2062 char *opt;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002063
Miklos Szeredie56818b2004-12-12 11:45:24 +00002064 if (xopts == NULL) {
2065 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002066 return -1;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002067 }
Miklos Szeredie5183742005-02-02 11:14:04 +00002068
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002069 while((opt = strsep(&s, ","))) {
2070 if (strcmp(opt, "debug") == 0)
2071 f->flags |= FUSE_DEBUG;
2072 else if (strcmp(opt, "hard_remove") == 0)
2073 f->flags |= FUSE_HARD_REMOVE;
Miklos Szeredia13d9002004-11-02 17:32:03 +00002074 else if (strcmp(opt, "use_ino") == 0)
2075 f->flags |= FUSE_USE_INO;
Miklos Szeredi0111f9d2005-04-22 12:04:55 +00002076 else if (strcmp(opt, "allow_root") == 0)
2077 f->flags |= FUSE_ALLOW_ROOT;
Miklos Szeredie5183742005-02-02 11:14:04 +00002078 else
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002079 fprintf(stderr, "fuse: warning: unknown option `%s'\n", opt);
2080 }
2081 free(xopts);
2082 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002083 return 0;
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002084}
2085
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002086struct fuse *fuse_new_common(int fd, const char *opts,
2087 const struct fuse_operations *op,
2088 size_t op_size, int compat)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002089{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002090 struct fuse *f;
2091 struct node *root;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002092
Miklos Szeredi0f62d722005-01-04 12:45:54 +00002093 if (sizeof(struct fuse_operations) < op_size) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002094 fprintf(stderr, "fuse: warning: library too old, some operations may not not work\n");
Miklos Szeredi0f62d722005-01-04 12:45:54 +00002095 op_size = sizeof(struct fuse_operations);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002096 }
2097
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002098 f = (struct fuse *) calloc(1, sizeof(struct fuse));
Miklos Szeredie56818b2004-12-12 11:45:24 +00002099 if (f == NULL) {
2100 fprintf(stderr, "fuse: failed to allocate fuse object\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002101 goto out;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002102 }
Miklos Szeredi2df1c042001-11-06 15:07:17 +00002103
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002104 if (parse_lib_opts(f, opts) == -1)
2105 goto out_free;
2106
Miklos Szeredi8cffdb92001-11-09 14:49:18 +00002107 f->fd = fd;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002108 f->ctr = 0;
Miklos Szeredi76f65782004-02-19 16:55:40 +00002109 f->generation = 0;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00002110 /* FIXME: Dynamic hash table */
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002111 f->name_table_size = 14057;
2112 f->name_table = (struct node **)
2113 calloc(1, sizeof(struct node *) * f->name_table_size);
Miklos Szeredie56818b2004-12-12 11:45:24 +00002114 if (f->name_table == NULL) {
2115 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002116 goto out_free;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002117 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002118
Miklos Szeredia13d9002004-11-02 17:32:03 +00002119 f->id_table_size = 14057;
2120 f->id_table = (struct node **)
2121 calloc(1, sizeof(struct node *) * f->id_table_size);
Miklos Szeredie56818b2004-12-12 11:45:24 +00002122 if (f->id_table == NULL) {
2123 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002124 goto out_free_name_table;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002125 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002126
Miklos Szerediab974562005-04-07 15:40:21 +00002127 mutex_init(&f->lock);
2128 mutex_init(&f->worker_lock);
Miklos Szeredi33232032001-11-19 17:55:51 +00002129 f->numworker = 0;
2130 f->numavail = 0;
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002131 memcpy(&f->op, op, op_size);
2132 f->compat = compat;
Miklos Szeredi0f48a262002-12-05 14:23:01 +00002133 f->exited = 0;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002134
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002135 root = (struct node *) calloc(1, sizeof(struct node));
Miklos Szeredie56818b2004-12-12 11:45:24 +00002136 if (root == NULL) {
2137 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredia13d9002004-11-02 17:32:03 +00002138 goto out_free_id_table;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002139 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002140
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002141 root->name = strdup("/");
Miklos Szeredie56818b2004-12-12 11:45:24 +00002142 if (root->name == NULL) {
2143 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002144 goto out_free_root;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002145 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002146
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002147 root->parent = 0;
Miklos Szeredia13d9002004-11-02 17:32:03 +00002148 root->nodeid = FUSE_ROOT_ID;
Miklos Szeredi76f65782004-02-19 16:55:40 +00002149 root->generation = 0;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00002150 root->refctr = 1;
Miklos Szeredi38009022005-05-08 19:47:22 +00002151 root->nlookup = 1;
Miklos Szeredia13d9002004-11-02 17:32:03 +00002152 hash_id(f, root);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002153
Miklos Szeredi0111f9d2005-04-22 12:04:55 +00002154 f->owner = getuid();
2155
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002156 return f;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002157
2158 out_free_root:
2159 free(root);
Miklos Szeredia13d9002004-11-02 17:32:03 +00002160 out_free_id_table:
2161 free(f->id_table);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002162 out_free_name_table:
2163 free(f->name_table);
2164 out_free:
2165 free(f);
2166 out:
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002167 return NULL;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002168}
2169
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002170struct fuse *fuse_new(int fd, const char *opts,
2171 const struct fuse_operations *op, size_t op_size)
2172{
2173 return fuse_new_common(fd, opts, op, op_size, 0);
2174}
2175
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002176struct fuse *fuse_new_compat2(int fd, const char *opts,
2177 const struct fuse_operations_compat2 *op)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002178{
2179 return fuse_new_common(fd, opts, (struct fuse_operations *) op,
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002180 sizeof(struct fuse_operations_compat2), 21);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002181}
2182
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002183struct fuse *fuse_new_compat1(int fd, int flags,
2184 const struct fuse_operations_compat1 *op)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002185{
2186 char *opts = NULL;
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002187 if (flags & FUSE_DEBUG_COMPAT1)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002188 opts = "debug";
2189 return fuse_new_common(fd, opts, (struct fuse_operations *) op,
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002190 sizeof(struct fuse_operations_compat1), 11);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002191}
2192
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002193void fuse_destroy(struct fuse *f)
2194{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002195 size_t i;
Miklos Szeredia13d9002004-11-02 17:32:03 +00002196 for (i = 0; i < f->id_table_size; i++) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002197 struct node *node;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002198
Miklos Szeredia13d9002004-11-02 17:32:03 +00002199 for (node = f->id_table[i]; node != NULL; node = node->id_next) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002200 if (node->is_hidden) {
Miklos Szeredia13d9002004-11-02 17:32:03 +00002201 char *path = get_path(f, node->nodeid);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002202 if (path)
2203 f->op.unlink(path);
2204 }
2205 }
2206 }
Miklos Szeredia13d9002004-11-02 17:32:03 +00002207 for (i = 0; i < f->id_table_size; i++) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002208 struct node *node;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002209 struct node *next;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002210
Miklos Szeredia13d9002004-11-02 17:32:03 +00002211 for (node = f->id_table[i]; node != NULL; node = next) {
2212 next = node->id_next;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002213 free_node(node);
2214 }
2215 }
Miklos Szeredia13d9002004-11-02 17:32:03 +00002216 free(f->id_table);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002217 free(f->name_table);
Miklos Szeredia181e612001-11-06 12:03:23 +00002218 pthread_mutex_destroy(&f->lock);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00002219 pthread_mutex_destroy(&f->worker_lock);
Miklos Szeredi159bd7e2005-02-28 17:32:16 +00002220 if (f->op.destroy)
2221 f->op.destroy(f->user_data);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002222 free(f);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002223}
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002224
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002225__asm__(".symver fuse_exited,__fuse_exited@");
2226__asm__(".symver fuse_process_cmd,__fuse_process_cmd@");
2227__asm__(".symver fuse_read_cmd,__fuse_read_cmd@");
2228__asm__(".symver fuse_set_getcontext_func,__fuse_set_getcontext_func@");
2229__asm__(".symver fuse_new_compat2,fuse_new@");