blob: 0980388e9ae3e3db189fd48c0314d600e0e0d7df [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 Szeredi85c74fc2001-10-28 19:44:14 +000013
Miklos Szeredi0f62d722005-01-04 12:45:54 +000014#include <stdio.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000015#include <string.h>
Miklos Szeredi97c61e92001-11-07 12:09:43 +000016#include <stdlib.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000017#include <unistd.h>
Miklos Szeredi97c61e92001-11-07 12:09:43 +000018#include <limits.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000019#include <errno.h>
Miklos Szeredi0f62d722005-01-04 12:45:54 +000020#include <assert.h>
21#include <stdint.h>
Miklos Szeredi019b4e92001-12-26 18:08:09 +000022#include <sys/param.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000023
Miklos Szeredi0f62d722005-01-04 12:45:54 +000024/* FUSE flags: */
25
26/** Enable debuging output */
27#define FUSE_DEBUG (1 << 1)
28
29/** If a file is removed but it's still open, don't hide the file but
30 remove it immediately */
31#define FUSE_HARD_REMOVE (1 << 2)
32
33/** Use st_ino field in getattr instead of generating inode numbers */
34#define FUSE_USE_INO (1 << 3)
35
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +000036#define FUSE_KERNEL_MINOR_VERSION_NEED 1
Miklos Szeredia25d4c22004-11-23 22:32:16 +000037#define FUSE_VERSION_FILE_OLD "/proc/fs/fuse/version"
Miklos Szeredi162bcbb2004-11-29 23:43:44 +000038#define FUSE_VERSION_FILE_NEW "/sys/fs/fuse/version"
Miklos Szeredia25d4c22004-11-23 22:32:16 +000039#define FUSE_DEV_OLD "/proc/fs/fuse/dev"
40
Miklos Szeredi97c61e92001-11-07 12:09:43 +000041#define FUSE_MAX_PATH 4096
Miklos Szeredi6bf8b682002-10-28 08:49:39 +000042#define PARAM(inarg) (((char *)(inarg)) + sizeof(*inarg))
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000043
Miklos Szeredi254d5ed2004-03-02 11:11:24 +000044#define ENTRY_REVALIDATE_TIME 1 /* sec */
45#define ATTR_REVALIDATE_TIME 1 /* sec */
46
Miklos Szeredi0f62d722005-01-04 12:45:54 +000047
48struct node {
49 struct node *name_next;
50 struct node *id_next;
51 nodeid_t nodeid;
52 unsigned int generation;
53 int refctr;
54 nodeid_t parent;
55 char *name;
56 uint64_t version;
57 int open_count;
58 int is_hidden;
59};
60
61struct fuse_dirhandle {
62 struct fuse *fuse;
Miklos Szeredi0f62d722005-01-04 12:45:54 +000063 FILE *fp;
Miklos Szeredi3ead28e2005-01-18 21:23:41 +000064 int filled;
Miklos Szeredi0f62d722005-01-04 12:45:54 +000065};
66
67struct fuse_cmd {
68 char *buf;
69 size_t buflen;
70};
71
72
Miklos Szeredid169f312004-09-22 08:48:26 +000073static struct fuse_context *(*fuse_getcontext)(void) = NULL;
74
Miklos Szeredi3ead28e2005-01-18 21:23:41 +000075/* Compatibility with kernel ABI version 5.1 */
76struct fuse_getdir_out {
77 __u32 fd;
78};
79
Miklos Szeredie5183742005-02-02 11:14:04 +000080#define FUSE_GETDIR 7
Miklos Szeredi3ead28e2005-01-18 21:23:41 +000081
Miklos Szeredic8ba2372002-12-10 12:26:00 +000082static const char *opname(enum fuse_opcode opcode)
83{
Miklos Szeredie5183742005-02-02 11:14:04 +000084 switch (opcode) {
Miklos Szeredi3ed84232004-03-30 15:17:26 +000085 case FUSE_LOOKUP: return "LOOKUP";
86 case FUSE_FORGET: return "FORGET";
87 case FUSE_GETATTR: return "GETATTR";
88 case FUSE_SETATTR: return "SETATTR";
89 case FUSE_READLINK: return "READLINK";
90 case FUSE_SYMLINK: return "SYMLINK";
91 case FUSE_GETDIR: return "GETDIR";
92 case FUSE_MKNOD: return "MKNOD";
93 case FUSE_MKDIR: return "MKDIR";
94 case FUSE_UNLINK: return "UNLINK";
95 case FUSE_RMDIR: return "RMDIR";
96 case FUSE_RENAME: return "RENAME";
97 case FUSE_LINK: return "LINK";
98 case FUSE_OPEN: return "OPEN";
99 case FUSE_READ: return "READ";
100 case FUSE_WRITE: return "WRITE";
101 case FUSE_STATFS: return "STATFS";
Miklos Szeredi99f20742004-05-19 08:01:10 +0000102 case FUSE_FLUSH: return "FLUSH";
Miklos Szeredi3ed84232004-03-30 15:17:26 +0000103 case FUSE_RELEASE: return "RELEASE";
104 case FUSE_FSYNC: return "FSYNC";
105 case FUSE_SETXATTR: return "SETXATTR";
106 case FUSE_GETXATTR: return "GETXATTR";
107 case FUSE_LISTXATTR: return "LISTXATTR";
108 case FUSE_REMOVEXATTR: return "REMOVEXATTR";
Miklos Szeredi3f0005f2005-01-04 19:24:31 +0000109 case FUSE_INIT: return "INIT";
Miklos Szeredi3ead28e2005-01-18 21:23:41 +0000110 case FUSE_OPENDIR: return "OPENDIR";
111 case FUSE_READDIR: return "READDIR";
112 case FUSE_RELEASEDIR: return "RELEASEDIR";
Miklos Szeredi99f20742004-05-19 08:01:10 +0000113 default: return "???";
Miklos Szeredic8ba2372002-12-10 12:26:00 +0000114 }
115}
116
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000117static inline void fuse_dec_avail(struct fuse *f)
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000118{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000119 pthread_mutex_lock(&f->worker_lock);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000120 f->numavail --;
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000121 pthread_mutex_unlock(&f->worker_lock);
122}
123
124static inline void fuse_inc_avail(struct fuse *f)
125{
126 pthread_mutex_lock(&f->worker_lock);
127 f->numavail ++;
128 pthread_mutex_unlock(&f->worker_lock);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000129}
130
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000131static struct node *get_node_nocheck(struct fuse *f, nodeid_t nodeid)
Miklos Szeredia181e612001-11-06 12:03:23 +0000132{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000133 size_t hash = nodeid % f->id_table_size;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000134 struct node *node;
135
Miklos Szeredia13d9002004-11-02 17:32:03 +0000136 for (node = f->id_table[hash]; node != NULL; node = node->id_next)
137 if (node->nodeid == nodeid)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000138 return node;
Miklos Szeredie5183742005-02-02 11:14:04 +0000139
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000140 return NULL;
Miklos Szeredia181e612001-11-06 12:03:23 +0000141}
142
Miklos Szeredia13d9002004-11-02 17:32:03 +0000143static struct node *get_node(struct fuse *f, nodeid_t nodeid)
Miklos Szeredia181e612001-11-06 12:03:23 +0000144{
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000145 struct node *node = get_node_nocheck(f, nodeid);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000146 if (!node) {
147 fprintf(stderr, "fuse internal error: node %lu not found\n",
148 nodeid);
149 abort();
150 }
151 return node;
Miklos Szeredia181e612001-11-06 12:03:23 +0000152}
153
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000154static void free_node(struct node *node)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000155{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000156 free(node->name);
157 free(node);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000158}
159
Miklos Szeredia13d9002004-11-02 17:32:03 +0000160static void unhash_id(struct fuse *f, struct node *node)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000161{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000162 size_t hash = node->nodeid % f->id_table_size;
163 struct node **nodep = &f->id_table[hash];
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000164
Miklos Szeredie5183742005-02-02 11:14:04 +0000165 for (; *nodep != NULL; nodep = &(*nodep)->id_next)
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000166 if (*nodep == node) {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000167 *nodep = node->id_next;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000168 return;
169 }
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000170}
171
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000172static void hash_id(struct fuse *f, struct node *node)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000173{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000174 size_t hash = node->nodeid % f->id_table_size;
175 node->id_next = f->id_table[hash];
Miklos Szeredie5183742005-02-02 11:14:04 +0000176 f->id_table[hash] = node;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000177}
178
Miklos Szeredia13d9002004-11-02 17:32:03 +0000179static unsigned int name_hash(struct fuse *f, nodeid_t parent, const char *name)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000180{
181 unsigned int hash = *name;
182
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000183 if (hash)
184 for (name += 1; *name != '\0'; name++)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000185 hash = (hash << 5) - hash + *name;
186
187 return (hash + parent) % f->name_table_size;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000188}
189
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000190static void unref_node(struct fuse *f, struct node *node);
191
192static void unhash_name(struct fuse *f, struct node *node)
193{
194 if (node->name) {
195 size_t hash = name_hash(f, node->parent, node->name);
196 struct node **nodep = &f->name_table[hash];
Miklos Szeredie5183742005-02-02 11:14:04 +0000197
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000198 for (; *nodep != NULL; nodep = &(*nodep)->name_next)
199 if (*nodep == node) {
200 *nodep = node->name_next;
201 node->name_next = NULL;
202 unref_node(f, get_node(f, node->parent));
203 free(node->name);
204 node->name = NULL;
205 node->parent = 0;
206 return;
207 }
208 fprintf(stderr, "fuse internal error: unable to unhash node: %lu\n",
209 node->nodeid);
210 abort();
211 }
212}
213
214static int hash_name(struct fuse *f, struct node *node, nodeid_t parent,
215 const char *name)
216{
217 size_t hash = name_hash(f, parent, name);
218 node->name = strdup(name);
219 if (node->name == NULL)
220 return -1;
221
222 get_node(f, parent)->refctr ++;
223 node->parent = parent;
224 node->name_next = f->name_table[hash];
225 f->name_table[hash] = node;
226 return 0;
227}
228
229static void delete_node(struct fuse *f, struct node *node)
230{
231 assert(!node->name);
232 unhash_id(f, node);
233 free_node(node);
234}
235
236static void unref_node(struct fuse *f, struct node *node)
237{
238 assert(node->refctr > 0);
239 node->refctr --;
240 if (!node->refctr)
241 delete_node(f, node);
242}
243
244static nodeid_t next_id(struct fuse *f)
245{
246 do {
247 f->ctr++;
248 if (!f->ctr)
249 f->generation ++;
250 } while (f->ctr == 0 || get_node_nocheck(f, f->ctr) != NULL);
251 return f->ctr;
252}
253
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000254static struct node *lookup_node(struct fuse *f, nodeid_t parent,
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000255 const char *name)
256{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000257 size_t hash = name_hash(f, parent, name);
258 struct node *node;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000259
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000260 for (node = f->name_table[hash]; node != NULL; node = node->name_next)
261 if (node->parent == parent && strcmp(node->name, name) == 0)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000262 return node;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000263
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000264 return NULL;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000265}
266
Miklos Szeredia13d9002004-11-02 17:32:03 +0000267static struct node *find_node(struct fuse *f, nodeid_t parent, char *name,
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000268 struct fuse_attr *attr, uint64_t version)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000269{
270 struct node *node;
Miklos Szeredia181e612001-11-06 12:03:23 +0000271 int mode = attr->mode & S_IFMT;
272 int rdev = 0;
Miklos Szeredie5183742005-02-02 11:14:04 +0000273
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000274 if (S_ISCHR(mode) || S_ISBLK(mode))
Miklos Szeredia181e612001-11-06 12:03:23 +0000275 rdev = attr->rdev;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000276
Miklos Szeredia181e612001-11-06 12:03:23 +0000277 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000278 node = lookup_node(f, parent, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000279 if (node != NULL) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000280 if (!(f->flags & FUSE_USE_INO))
Miklos Szeredie5183742005-02-02 11:14:04 +0000281 attr->ino = node->nodeid;
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000282 } else {
283 node = (struct node *) calloc(1, sizeof(struct node));
284 if (node == NULL)
285 goto out_err;
Miklos Szeredie5183742005-02-02 11:14:04 +0000286
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000287 node->refctr = 1;
288 node->nodeid = next_id(f);
289 if (!(f->flags & FUSE_USE_INO))
290 attr->ino = node->nodeid;
291 node->open_count = 0;
292 node->is_hidden = 0;
293 node->generation = f->generation;
294 if (hash_name(f, node, parent, name) == -1) {
295 free(node);
296 node = NULL;
297 goto out_err;
298 }
299 hash_id(f, node);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000300 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000301 node->version = version;
Miklos Szeredic2309912004-09-21 13:40:38 +0000302 out_err:
Miklos Szeredia181e612001-11-06 12:03:23 +0000303 pthread_mutex_unlock(&f->lock);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000304 return node;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000305}
306
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000307static char *add_name(char *buf, char *s, const char *name)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000308{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000309 size_t len = strlen(name);
310 s -= len;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000311 if (s <= buf) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000312 fprintf(stderr, "fuse: path too long: ...%s\n", s + len);
313 return NULL;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000314 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000315 strncpy(s, name, len);
316 s--;
317 *s = '/';
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000318
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000319 return s;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000320}
321
Miklos Szeredia13d9002004-11-02 17:32:03 +0000322static char *get_path_name(struct fuse *f, nodeid_t nodeid, const char *name)
Miklos Szeredib483c932001-10-29 14:57:57 +0000323{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000324 char buf[FUSE_MAX_PATH];
325 char *s = buf + FUSE_MAX_PATH - 1;
326 struct node *node;
Miklos Szeredie5183742005-02-02 11:14:04 +0000327
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000328 *s = '\0';
Miklos Szeredia181e612001-11-06 12:03:23 +0000329
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000330 if (name != NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000331 s = add_name(buf, s, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000332 if (s == NULL)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000333 return NULL;
334 }
335
336 pthread_mutex_lock(&f->lock);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000337 for (node = get_node(f, nodeid); node && node->nodeid != FUSE_ROOT_ID;
338 node = get_node(f, node->parent)) {
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000339 if (node->name == NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000340 s = NULL;
341 break;
342 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000343
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000344 s = add_name(buf, s, node->name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000345 if (s == NULL)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000346 break;
347 }
348 pthread_mutex_unlock(&f->lock);
349
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000350 if (node == NULL || s == NULL)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000351 return NULL;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000352 else if (*s == '\0')
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000353 return strdup("/");
354 else
355 return strdup(s);
356}
Miklos Szeredia181e612001-11-06 12:03:23 +0000357
Miklos Szeredia13d9002004-11-02 17:32:03 +0000358static char *get_path(struct fuse *f, nodeid_t nodeid)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000359{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000360 return get_path_name(f, nodeid, NULL);
Miklos Szeredib483c932001-10-29 14:57:57 +0000361}
362
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000363static void forget_node(struct fuse *f, nodeid_t nodeid, uint64_t version)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000364{
Miklos Szeredia181e612001-11-06 12:03:23 +0000365 struct node *node;
366
367 pthread_mutex_lock(&f->lock);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000368 node = get_node(f, nodeid);
369 if (node->version == version && nodeid != FUSE_ROOT_ID) {
370 node->version = 0;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000371 unhash_name(f, node);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000372 unref_node(f, node);
Miklos Szeredia181e612001-11-06 12:03:23 +0000373 }
374 pthread_mutex_unlock(&f->lock);
375
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000376}
377
Miklos Szeredia13d9002004-11-02 17:32:03 +0000378static void remove_node(struct fuse *f, nodeid_t dir, const char *name)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000379{
Miklos Szeredia181e612001-11-06 12:03:23 +0000380 struct node *node;
381
382 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000383 node = lookup_node(f, dir, name);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000384 if (node != NULL)
385 unhash_name(f, node);
Miklos Szeredia181e612001-11-06 12:03:23 +0000386 pthread_mutex_unlock(&f->lock);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000387}
388
Miklos Szeredia13d9002004-11-02 17:32:03 +0000389static int rename_node(struct fuse *f, nodeid_t olddir, const char *oldname,
390 nodeid_t newdir, const char *newname, int hide)
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000391{
Miklos Szeredia181e612001-11-06 12:03:23 +0000392 struct node *node;
393 struct node *newnode;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000394 int err = 0;
Miklos Szeredie5183742005-02-02 11:14:04 +0000395
Miklos Szeredia181e612001-11-06 12:03:23 +0000396 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000397 node = lookup_node(f, olddir, oldname);
398 newnode = lookup_node(f, newdir, newname);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000399 if (node == NULL)
400 goto out;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000401
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000402 if (newnode != NULL) {
403 if (hide) {
404 fprintf(stderr, "fuse: hidden file got created during hiding\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000405 err = -EBUSY;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000406 goto out;
407 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000408 unhash_name(f, newnode);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000409 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000410
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000411 unhash_name(f, node);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000412 if (hash_name(f, node, newdir, newname) == -1) {
413 err = -ENOMEM;
414 goto out;
415 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000416
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000417 if (hide)
418 node->is_hidden = 1;
419
420 out:
Miklos Szeredia181e612001-11-06 12:03:23 +0000421 pthread_mutex_unlock(&f->lock);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000422 return err;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000423}
424
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000425static void convert_stat(struct stat *stbuf, struct fuse_attr *attr)
426{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000427 attr->ino = stbuf->st_ino;
Miklos Szeredib5958612004-02-20 14:10:49 +0000428 attr->mode = stbuf->st_mode;
429 attr->nlink = stbuf->st_nlink;
430 attr->uid = stbuf->st_uid;
431 attr->gid = stbuf->st_gid;
432 attr->rdev = stbuf->st_rdev;
433 attr->size = stbuf->st_size;
434 attr->blocks = stbuf->st_blocks;
435 attr->atime = stbuf->st_atime;
Miklos Szeredib5958612004-02-20 14:10:49 +0000436 attr->mtime = stbuf->st_mtime;
Miklos Szeredib5958612004-02-20 14:10:49 +0000437 attr->ctime = stbuf->st_ctime;
Miklos Szeredicb264512004-06-23 18:52:50 +0000438#ifdef HAVE_STRUCT_STAT_ST_ATIM
439 attr->atimensec = stbuf->st_atim.tv_nsec;
440 attr->mtimensec = stbuf->st_mtim.tv_nsec;
Miklos Szeredib5958612004-02-20 14:10:49 +0000441 attr->ctimensec = stbuf->st_ctim.tv_nsec;
Miklos Szeredicb264512004-06-23 18:52:50 +0000442#endif
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000443}
444
Miklos Szeredi8fb48fe2004-11-08 14:48:52 +0000445static int fill_dir(struct fuse_dirhandle *dh, const char *name, int type,
446 ino_t ino)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000447{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000448 size_t namelen = strlen(name);
449 struct fuse_dirent *dirent;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000450 size_t reclen;
451 size_t res;
452
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000453 if (namelen > FUSE_NAME_MAX)
454 namelen = FUSE_NAME_MAX;
455
456 dirent = calloc(1, sizeof(struct fuse_dirent) + namelen + 8);
457 if (dirent == NULL)
458 return -ENOMEM;
459
Miklos Szeredi8fb48fe2004-11-08 14:48:52 +0000460 if ((dh->fuse->flags & FUSE_USE_INO))
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000461 dirent->ino = ino;
Miklos Szeredi8fb48fe2004-11-08 14:48:52 +0000462 else
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000463 dirent->ino = (unsigned long) -1;
464 dirent->namelen = namelen;
465 strncpy(dirent->name, name, namelen);
466 dirent->type = type;
467 reclen = FUSE_DIRENT_SIZE(dirent);
468 res = fwrite(dirent, reclen, 1, dh->fp);
469 free(dirent);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000470 if (res == 0) {
Miklos Szeredi96249982001-11-21 12:21:19 +0000471 perror("fuse: writing directory file");
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000472 return -EIO;
473 }
474 return 0;
475}
476
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000477static int send_reply_raw(struct fuse *f, char *outbuf, size_t outsize)
Miklos Szeredi43696432001-11-18 19:15:05 +0000478{
479 int res;
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000480 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
481 out->len = outsize;
Miklos Szeredi43696432001-11-18 19:15:05 +0000482
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000483 if ((f->flags & FUSE_DEBUG)) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000484 printf(" unique: %llu, error: %i (%s), outsize: %i\n",
485 out->unique, out->error, strerror(-out->error), outsize);
Miklos Szeredi43696432001-11-18 19:15:05 +0000486 fflush(stdout);
487 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000488
Miklos Szeredi4e71c9f2004-02-09 12:05:14 +0000489 /* This needs to be done before the reply, otherwise the scheduler
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000490 could play tricks with us, and only let the counter be
491 increased long after the operation is done */
492 fuse_inc_avail(f);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000493
Miklos Szeredi43696432001-11-18 19:15:05 +0000494 res = write(f->fd, outbuf, outsize);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000495 if (res == -1) {
Miklos Szeredi43696432001-11-18 19:15:05 +0000496 /* ENOENT means the operation was interrupted */
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000497 if (!f->exited && errno != ENOENT)
Miklos Szeredi96249982001-11-21 12:21:19 +0000498 perror("fuse: writing device");
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000499 return -errno;
Miklos Szeredi43696432001-11-18 19:15:05 +0000500 }
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000501 return 0;
Miklos Szeredi43696432001-11-18 19:15:05 +0000502}
503
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000504static int send_reply(struct fuse *f, struct fuse_in_header *in, int error,
505 void *arg, size_t argsize)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000506{
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000507 int res;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000508 char *outbuf;
509 size_t outsize;
510 struct fuse_out_header *out;
511
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000512 if (error <= -1000 || error > 0) {
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000513 fprintf(stderr, "fuse: bad error value: %i\n", error);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000514 error = -ERANGE;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000515 }
516
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000517 if (error)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000518 argsize = 0;
519
520 outsize = sizeof(struct fuse_out_header) + argsize;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000521 outbuf = (char *) malloc(outsize);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000522 if (outbuf == NULL) {
523 fprintf(stderr, "fuse: failed to allocate reply buffer\n");
524 res = -ENOMEM;
525 } else {
526 out = (struct fuse_out_header *) outbuf;
527 memset(out, 0, sizeof(struct fuse_out_header));
528 out->unique = in->unique;
529 out->error = error;
530 if (argsize != 0)
531 memcpy(outbuf + sizeof(struct fuse_out_header), arg, argsize);
Miklos Szeredie5183742005-02-02 11:14:04 +0000532
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000533 res = send_reply_raw(f, outbuf, outsize);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000534 free(outbuf);
535 }
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000536
537 return res;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000538}
539
Miklos Szeredia13d9002004-11-02 17:32:03 +0000540static int is_open(struct fuse *f, nodeid_t dir, const char *name)
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000541{
542 struct node *node;
543 int isopen = 0;
544 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000545 node = lookup_node(f, dir, name);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000546 if (node && node->open_count > 0)
547 isopen = 1;
548 pthread_mutex_unlock(&f->lock);
549 return isopen;
550}
551
Miklos Szeredia13d9002004-11-02 17:32:03 +0000552static char *hidden_name(struct fuse *f, nodeid_t dir, const char *oldname,
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000553 char *newname, size_t bufsize)
554{
555 struct stat buf;
556 struct node *node;
557 struct node *newnode;
558 char *newpath;
559 int res;
560 int failctr = 10;
561
562 if (!f->op.getattr)
563 return NULL;
564
565 do {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000566 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000567 node = lookup_node(f, dir, oldname);
568 if (node == NULL) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000569 pthread_mutex_unlock(&f->lock);
570 return NULL;
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000571 }
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000572 do {
573 f->hidectr ++;
574 snprintf(newname, bufsize, ".fuse_hidden%08x%08x",
Miklos Szeredia13d9002004-11-02 17:32:03 +0000575 (unsigned int) node->nodeid, f->hidectr);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000576 newnode = lookup_node(f, dir, newname);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000577 } while(newnode);
578 pthread_mutex_unlock(&f->lock);
Miklos Szeredie5183742005-02-02 11:14:04 +0000579
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000580 newpath = get_path_name(f, dir, newname);
581 if (!newpath)
582 break;
Miklos Szeredie5183742005-02-02 11:14:04 +0000583
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000584 res = f->op.getattr(newpath, &buf);
585 if (res != 0)
586 break;
587 free(newpath);
588 newpath = NULL;
589 } while(--failctr);
590
591 return newpath;
592}
593
Miklos Szeredia13d9002004-11-02 17:32:03 +0000594static int hide_node(struct fuse *f, const char *oldpath, nodeid_t dir,
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000595 const char *oldname)
596{
597 char newname[64];
598 char *newpath;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000599 int err = -EBUSY;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000600
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000601 if (f->op.rename && f->op.unlink) {
602 newpath = hidden_name(f, dir, oldname, newname, sizeof(newname));
603 if (newpath) {
604 int res = f->op.rename(oldpath, newpath);
605 if (res == 0)
606 err = rename_node(f, dir, oldname, dir, newname, 1);
607 free(newpath);
608 }
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000609 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000610 return err;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000611}
612
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000613static int lookup_path(struct fuse *f, nodeid_t nodeid, uint64_t version,
614 char *name, const char *path,
615 struct fuse_entry_out *arg)
Miklos Szeredi76f65782004-02-19 16:55:40 +0000616{
617 int res;
618 struct stat buf;
619
620 res = f->op.getattr(path, &buf);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000621 if (res == 0) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000622 struct node *node;
623
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000624 memset(arg, 0, sizeof(struct fuse_entry_out));
Miklos Szeredi76f65782004-02-19 16:55:40 +0000625 convert_stat(&buf, &arg->attr);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000626 node = find_node(f, nodeid, name, &arg->attr, version);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000627 if (node == NULL)
628 res = -ENOMEM;
629 else {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000630 arg->nodeid = node->nodeid;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000631 arg->generation = node->generation;
632 arg->entry_valid = ENTRY_REVALIDATE_TIME;
633 arg->entry_valid_nsec = 0;
634 arg->attr_valid = ATTR_REVALIDATE_TIME;
635 arg->attr_valid_nsec = 0;
636 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000637 printf(" NODEID: %lu\n", (unsigned long) arg->nodeid);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000638 fflush(stdout);
639 }
Miklos Szeredi76f65782004-02-19 16:55:40 +0000640 }
641 }
642 return res;
643}
644
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000645static void do_lookup(struct fuse *f, struct fuse_in_header *in, char *name)
646{
647 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000648 int res2;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000649 char *path;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000650 struct fuse_entry_out arg;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000651
Miklos Szeredi5e183482001-10-31 14:52:35 +0000652 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000653 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000654 if (path != NULL) {
655 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi6ebe2342002-01-06 21:44:16 +0000656 printf("LOOKUP %s\n", path);
657 fflush(stdout);
658 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000659 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000660 if (f->op.getattr)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000661 res = lookup_path(f, in->nodeid, in->unique, name, path, &arg);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000662 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000663 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000664 res2 = send_reply(f, in, res, &arg, sizeof(arg));
665 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000666 forget_node(f, arg.nodeid, in->unique);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000667}
668
Miklos Szeredia181e612001-11-06 12:03:23 +0000669static void do_forget(struct fuse *f, struct fuse_in_header *in,
670 struct fuse_forget_in *arg)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000671{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000672 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000673 printf("FORGET %lu/%llu\n", (unsigned long) in->nodeid,
674 arg->version);
Miklos Szeredi43696432001-11-18 19:15:05 +0000675 fflush(stdout);
676 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000677 forget_node(f, in->nodeid, arg->version);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000678}
679
680static void do_getattr(struct fuse *f, struct fuse_in_header *in)
681{
682 int res;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000683 char *path;
684 struct stat buf;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000685 struct fuse_attr_out arg;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000686
Miklos Szeredi5e183482001-10-31 14:52:35 +0000687 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000688 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000689 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +0000690 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000691 if (f->op.getattr)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000692 res = f->op.getattr(path, &buf);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000693 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000694 }
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000695
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000696 if (res == 0) {
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000697 memset(&arg, 0, sizeof(struct fuse_attr_out));
698 arg.attr_valid = ATTR_REVALIDATE_TIME;
699 arg.attr_valid_nsec = 0;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000700 convert_stat(&buf, &arg.attr);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000701 if (!(f->flags & FUSE_USE_INO))
702 arg.attr.ino = in->nodeid;
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000703 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000704
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000705 send_reply(f, in, res, &arg, sizeof(arg));
706}
707
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000708static int do_chmod(struct fuse *f, const char *path, struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000709{
710 int res;
711
712 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000713 if (f->op.chmod)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000714 res = f->op.chmod(path, attr->mode);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000715
716 return res;
Miklos Szeredie5183742005-02-02 11:14:04 +0000717}
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000718
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000719static int do_chown(struct fuse *f, const char *path, struct fuse_attr *attr,
Miklos Szeredi2e50d432001-12-20 12:17:25 +0000720 int valid)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000721{
722 int res;
723 uid_t uid = (valid & FATTR_UID) ? attr->uid : (uid_t) -1;
724 gid_t gid = (valid & FATTR_GID) ? attr->gid : (gid_t) -1;
Miklos Szeredie5183742005-02-02 11:14:04 +0000725
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000726 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000727 if (f->op.chown)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000728 res = f->op.chown(path, uid, gid);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000729
730 return res;
731}
732
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000733static int do_truncate(struct fuse *f, const char *path,
734 struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000735{
736 int res;
737
738 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000739 if (f->op.truncate)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000740 res = f->op.truncate(path, attr->size);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000741
742 return res;
743}
744
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000745static int do_utime(struct fuse *f, const char *path, struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000746{
747 int res;
748 struct utimbuf buf;
749 buf.actime = attr->atime;
750 buf.modtime = attr->mtime;
751 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000752 if (f->op.utime)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000753 res = f->op.utime(path, &buf);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000754
755 return res;
756}
757
Miklos Szeredi5e183482001-10-31 14:52:35 +0000758static void do_setattr(struct fuse *f, struct fuse_in_header *in,
759 struct fuse_setattr_in *arg)
760{
761 int res;
762 char *path;
763 int valid = arg->valid;
764 struct fuse_attr *attr = &arg->attr;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000765 struct fuse_attr_out outarg;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000766
767 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000768 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000769 if (path != NULL) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000770 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000771 if (f->op.getattr) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000772 res = 0;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000773 if (!res && (valid & FATTR_MODE))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000774 res = do_chmod(f, path, attr);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000775 if (!res && (valid & (FATTR_UID | FATTR_GID)))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000776 res = do_chown(f, path, attr, valid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000777 if (!res && (valid & FATTR_SIZE))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000778 res = do_truncate(f, path, attr);
Miklos Szeredie5183742005-02-02 11:14:04 +0000779 if (!res && (valid & (FATTR_ATIME | FATTR_MTIME)) ==
Miklos Szeredib5958612004-02-20 14:10:49 +0000780 (FATTR_ATIME | FATTR_MTIME))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000781 res = do_utime(f, path, attr);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000782 if (!res) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000783 struct stat buf;
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000784 res = f->op.getattr(path, &buf);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000785 if (!res) {
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000786 memset(&outarg, 0, sizeof(struct fuse_attr_out));
787 outarg.attr_valid = ATTR_REVALIDATE_TIME;
788 outarg.attr_valid_nsec = 0;
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000789 convert_stat(&buf, &outarg.attr);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000790 if (!(f->flags & FUSE_USE_INO))
791 outarg.attr.ino = in->nodeid;
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000792 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000793 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000794 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000795 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000796 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000797 send_reply(f, in, res, &outarg, sizeof(outarg));
Miklos Szeredi5e183482001-10-31 14:52:35 +0000798}
799
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000800static void do_readlink(struct fuse *f, struct fuse_in_header *in)
801{
802 int res;
803 char link[PATH_MAX + 1];
Miklos Szeredib483c932001-10-29 14:57:57 +0000804 char *path;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000805
Miklos Szeredi5e183482001-10-31 14:52:35 +0000806 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000807 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000808 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +0000809 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000810 if (f->op.readlink)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000811 res = f->op.readlink(path, link, sizeof(link));
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000812 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000813 }
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000814 link[PATH_MAX] = '\0';
Miklos Szeredi4b2bef42002-01-09 12:23:27 +0000815 send_reply(f, in, res, link, res == 0 ? strlen(link) : 0);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000816}
817
Miklos Szeredi3ead28e2005-01-18 21:23:41 +0000818static int common_getdir(struct fuse *f, struct fuse_in_header *in,
819 struct fuse_dirhandle *dh)
820{
821 int res;
822 char *path;
823
824 res = -ENOENT;
825 path = get_path(f, in->nodeid);
826 if (path != NULL) {
827 res = -ENOSYS;
828 if (f->op.getdir)
829 res = f->op.getdir(path, dh, fill_dir);
830 free(path);
831 }
832 return res;
833}
834
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000835static void do_getdir(struct fuse *f, struct fuse_in_header *in)
836{
837 int res;
838 struct fuse_getdir_out arg;
Miklos Szeredia181e612001-11-06 12:03:23 +0000839 struct fuse_dirhandle dh;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000840
Miklos Szeredib483c932001-10-29 14:57:57 +0000841 dh.fuse = f;
842 dh.fp = tmpfile();
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000843
Miklos Szeredi65afea12004-09-14 07:13:45 +0000844 res = -EIO;
845 if (dh.fp == NULL)
846 perror("fuse: failed to create temporary file");
847 else {
Miklos Szeredi3ead28e2005-01-18 21:23:41 +0000848 res = common_getdir(f, in, &dh);
Miklos Szeredi65afea12004-09-14 07:13:45 +0000849 fflush(dh.fp);
850 }
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000851 memset(&arg, 0, sizeof(struct fuse_getdir_out));
Miklos Szeredi65afea12004-09-14 07:13:45 +0000852 if (res == 0)
853 arg.fd = fileno(dh.fp);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000854 send_reply(f, in, res, &arg, sizeof(arg));
Miklos Szeredi65afea12004-09-14 07:13:45 +0000855 if (dh.fp != NULL)
856 fclose(dh.fp);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000857}
858
Miklos Szeredib483c932001-10-29 14:57:57 +0000859static void do_mknod(struct fuse *f, struct fuse_in_header *in,
860 struct fuse_mknod_in *inarg)
861{
862 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000863 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000864 char *path;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000865 char *name = PARAM(inarg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000866 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000867
Miklos Szeredi5e183482001-10-31 14:52:35 +0000868 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000869 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000870 if (path != NULL) {
871 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000872 printf("MKNOD %s\n", path);
873 fflush(stdout);
874 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000875 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000876 if (f->op.mknod && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000877 res = f->op.mknod(path, inarg->mode, inarg->rdev);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000878 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000879 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000880 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000881 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000882 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000883 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
884 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000885 forget_node(f, outarg.nodeid, in->unique);
Miklos Szeredib483c932001-10-29 14:57:57 +0000886}
887
888static void do_mkdir(struct fuse *f, struct fuse_in_header *in,
889 struct fuse_mkdir_in *inarg)
890{
891 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000892 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000893 char *path;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000894 char *name = PARAM(inarg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000895 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000896
Miklos Szeredi5e183482001-10-31 14:52:35 +0000897 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000898 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000899 if (path != NULL) {
900 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000901 printf("MKDIR %s\n", path);
902 fflush(stdout);
903 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000904 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000905 if (f->op.mkdir && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000906 res = f->op.mkdir(path, inarg->mode);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000907 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000908 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000909 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000910 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000911 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000912 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
913 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000914 forget_node(f, outarg.nodeid, in->unique);
Miklos Szeredib483c932001-10-29 14:57:57 +0000915}
916
Miklos Szeredib5958612004-02-20 14:10:49 +0000917static void do_unlink(struct fuse *f, struct fuse_in_header *in, char *name)
Miklos Szeredib483c932001-10-29 14:57:57 +0000918{
919 int res;
920 char *path;
921
Miklos Szeredi5e183482001-10-31 14:52:35 +0000922 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000923 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000924 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000925 if (f->flags & FUSE_DEBUG) {
926 printf("UNLINK %s\n", path);
927 fflush(stdout);
928 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000929 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000930 if (f->op.unlink) {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000931 if (!(f->flags & FUSE_HARD_REMOVE) && is_open(f, in->nodeid, name))
932 res = hide_node(f, path, in->nodeid, name);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000933 else {
934 res = f->op.unlink(path);
935 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000936 remove_node(f, in->nodeid, name);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000937
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000938 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000939 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000940 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000941 }
Miklos Szeredib5958612004-02-20 14:10:49 +0000942 send_reply(f, in, res, NULL, 0);
943}
944
945static void do_rmdir(struct fuse *f, struct fuse_in_header *in, char *name)
946{
947 int res;
948 char *path;
949
950 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000951 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000952 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000953 if (f->flags & FUSE_DEBUG) {
954 printf("RMDIR %s\n", path);
955 fflush(stdout);
956 }
Miklos Szeredib5958612004-02-20 14:10:49 +0000957 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000958 if (f->op.rmdir) {
Miklos Szeredib5958612004-02-20 14:10:49 +0000959 res = f->op.rmdir(path);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000960 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000961 remove_node(f, in->nodeid, name);
Miklos Szeredib5958612004-02-20 14:10:49 +0000962 }
963 free(path);
964 }
Miklos Szeredib483c932001-10-29 14:57:57 +0000965 send_reply(f, in, res, NULL, 0);
966}
967
968static void do_symlink(struct fuse *f, struct fuse_in_header *in, char *name,
969 char *link)
970{
971 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000972 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000973 char *path;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000974 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000975
Miklos Szeredi5e183482001-10-31 14:52:35 +0000976 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000977 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000978 if (path != NULL) {
979 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000980 printf("SYMLINK %s\n", path);
981 fflush(stdout);
982 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000983 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000984 if (f->op.symlink && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000985 res = f->op.symlink(link, path);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000986 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000987 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000988 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000989 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000990 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000991 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
992 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000993 forget_node(f, outarg.nodeid, in->unique);
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000994
Miklos Szeredib483c932001-10-29 14:57:57 +0000995}
996
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000997static void do_rename(struct fuse *f, struct fuse_in_header *in,
998 struct fuse_rename_in *inarg)
999{
1000 int res;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001001 nodeid_t olddir = in->nodeid;
1002 nodeid_t newdir = inarg->newdir;
Miklos Szeredi6bf8b682002-10-28 08:49:39 +00001003 char *oldname = PARAM(inarg);
1004 char *newname = oldname + strlen(oldname) + 1;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001005 char *oldpath;
1006 char *newpath;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001007
Miklos Szeredi5e183482001-10-31 14:52:35 +00001008 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +00001009 oldpath = get_path_name(f, olddir, oldname);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001010 if (oldpath != NULL) {
Miklos Szeredia181e612001-11-06 12:03:23 +00001011 newpath = get_path_name(f, newdir, newname);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001012 if (newpath != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001013 if (f->flags & FUSE_DEBUG) {
1014 printf("RENAME %s -> %s\n", oldpath, newpath);
1015 fflush(stdout);
1016 }
Miklos Szeredi5e183482001-10-31 14:52:35 +00001017 res = -ENOSYS;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001018 if (f->op.rename) {
1019 res = 0;
Miklos Szeredie5183742005-02-02 11:14:04 +00001020 if (!(f->flags & FUSE_HARD_REMOVE) &&
Miklos Szeredi2529ca22004-07-13 15:36:52 +00001021 is_open(f, newdir, newname))
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001022 res = hide_node(f, newpath, newdir, newname);
1023 if (res == 0) {
1024 res = f->op.rename(oldpath, newpath);
1025 if (res == 0)
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001026 res = rename_node(f, olddir, oldname, newdir, newname, 0);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001027 }
1028 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001029 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001030 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001031 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001032 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001033 send_reply(f, in, res, NULL, 0);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001034}
1035
1036static void do_link(struct fuse *f, struct fuse_in_header *in,
Miklos Szeredi5e183482001-10-31 14:52:35 +00001037 struct fuse_link_in *arg)
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001038{
1039 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +00001040 int res2;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001041 char *oldpath;
1042 char *newpath;
Miklos Szeredi76f65782004-02-19 16:55:40 +00001043 char *name = PARAM(arg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +00001044 struct fuse_entry_out outarg;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001045
Miklos Szeredi5e183482001-10-31 14:52:35 +00001046 res = -ENOENT;
Miklos Szeredied6b5dd2005-01-26 17:07:59 +00001047 oldpath = get_path(f, arg->oldnodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001048 if (oldpath != NULL) {
Miklos Szeredied6b5dd2005-01-26 17:07:59 +00001049 newpath = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001050 if (newpath != NULL) {
1051 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +00001052 printf("LINK %s\n", newpath);
1053 fflush(stdout);
1054 }
Miklos Szeredi5e183482001-10-31 14:52:35 +00001055 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001056 if (f->op.link && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +00001057 res = f->op.link(oldpath, newpath);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001058 if (res == 0)
Miklos Szeredied6b5dd2005-01-26 17:07:59 +00001059 res = lookup_path(f, in->nodeid, in->unique, name,
Miklos Szeredi76f65782004-02-19 16:55:40 +00001060 newpath, &outarg);
1061 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001062 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001063 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001064 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001065 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +00001066 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
1067 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001068 forget_node(f, outarg.nodeid, in->unique);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001069}
1070
Miklos Szeredi5e183482001-10-31 14:52:35 +00001071static void do_open(struct fuse *f, struct fuse_in_header *in,
1072 struct fuse_open_in *arg)
1073{
1074 int res;
1075 char *path;
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001076 struct fuse_open_out outarg;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001077 struct fuse_file_info fi;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001078
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001079 memset(&fi, 0, sizeof(fi));
1080 fi.flags = arg->flags;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001081 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001082 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001083 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +00001084 res = -ENOSYS;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001085 if (f->op.open) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001086 if (!f->compat)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001087 res = f->op.open(path, &fi);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001088 else
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001089 res = ((struct fuse_operations_compat2 *) &f->op)->open(path, fi.flags);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001090 }
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +00001091 }
Miklos Szeredi73798f92004-07-12 15:55:11 +00001092 if (res == 0) {
1093 int res2;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001094 outarg.fh = fi.fh;
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001095 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001096 printf("OPEN[%lu] flags: 0x%x\n", fi.fh, arg->flags);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001097 fflush(stdout);
1098 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001099
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001100 pthread_mutex_lock(&f->lock);
1101 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001102 if(res2 == -ENOENT) {
1103 /* The open syscall was interrupted, so it must be cancelled */
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001104 if(f->op.release) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001105 if (!f->compat)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001106 f->op.release(path, &fi);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001107 else
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001108 ((struct fuse_operations_compat2 *) &f->op)->release(path, fi.flags);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001109 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001110 } else {
1111 struct node *node = get_node(f, in->nodeid);
1112 node->open_count ++;
1113 }
Miklos Szeredi73798f92004-07-12 15:55:11 +00001114 pthread_mutex_unlock(&f->lock);
Miklos Szeredi73798f92004-07-12 15:55:11 +00001115 } else
1116 send_reply(f, in, res, NULL, 0);
1117
Miklos Szeredi9a31cca2004-06-26 21:11:25 +00001118 if (path)
1119 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001120}
1121
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001122static void do_flush(struct fuse *f, struct fuse_in_header *in,
1123 struct fuse_flush_in *arg)
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001124{
1125 char *path;
1126 int res;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001127 struct fuse_file_info fi;
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001128
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001129 memset(&fi, 0, sizeof(fi));
1130 fi.fh = arg->fh;
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001131 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001132 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001133 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001134 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001135 printf("FLUSH[%lu]\n", (unsigned long) arg->fh);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001136 fflush(stdout);
1137 }
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001138 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001139 if (f->op.flush)
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001140 res = f->op.flush(path, &fi);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001141 free(path);
1142 }
1143 send_reply(f, in, res, NULL, 0);
1144}
1145
Miklos Szeredi9478e862002-12-11 09:50:26 +00001146static void do_release(struct fuse *f, struct fuse_in_header *in,
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001147 struct fuse_release_in *arg)
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001148{
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001149 struct node *node;
1150 char *path;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001151 struct fuse_file_info fi;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001152 int unlink_hidden;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001153
1154 memset(&fi, 0, sizeof(fi));
1155 fi.flags = arg->flags;
1156 fi.fh = arg->fh;
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001157
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001158 pthread_mutex_lock(&f->lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001159 node = get_node(f, in->nodeid);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001160 assert(node->open_count > 0);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001161 --node->open_count;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001162 unlink_hidden = (node->is_hidden && !node->open_count);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001163 pthread_mutex_unlock(&f->lock);
1164
Miklos Szeredia13d9002004-11-02 17:32:03 +00001165 path = get_path(f, in->nodeid);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001166 if (f->flags & FUSE_DEBUG) {
1167 printf("RELEASE[%lu] flags: 0x%x\n", fi.fh, fi.flags);
1168 fflush(stdout);
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001169 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001170 if (f->op.release) {
1171 if (!f->compat)
1172 f->op.release(path ? path : "-", &fi);
1173 else if (path)
1174 ((struct fuse_operations_compat2 *) &f->op)->release(path, fi.flags);
1175 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001176
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001177 if(unlink_hidden && path)
1178 f->op.unlink(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001179
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001180 if (path)
1181 free(path);
1182
Miklos Szeredi556d03d2004-06-30 11:13:41 +00001183 send_reply(f, in, 0, NULL, 0);
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001184}
1185
Miklos Szeredi5e183482001-10-31 14:52:35 +00001186static void do_read(struct fuse *f, struct fuse_in_header *in,
1187 struct fuse_read_in *arg)
1188{
1189 int res;
1190 char *path;
Miklos Szeredi43696432001-11-18 19:15:05 +00001191 char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + arg->size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001192 if (outbuf == NULL)
1193 send_reply(f, in, -ENOMEM, NULL, 0);
1194 else {
1195 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
1196 char *buf = outbuf + sizeof(struct fuse_out_header);
1197 size_t size;
1198 size_t outsize;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001199 struct fuse_file_info fi;
Miklos Szeredie5183742005-02-02 11:14:04 +00001200
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001201 memset(&fi, 0, sizeof(fi));
1202 fi.fh = arg->fh;
1203
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001204 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001205 path = get_path(f, in->nodeid);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001206 if (path != NULL) {
1207 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001208 printf("READ[%lu] %u bytes from %llu\n",
1209 (unsigned long) arg->fh, arg->size, arg->offset);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001210 fflush(stdout);
1211 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001212
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001213 res = -ENOSYS;
1214 if (f->op.read)
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001215 res = f->op.read(path, buf, arg->size, arg->offset, &fi);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001216 free(path);
Miklos Szeredi6ebe2342002-01-06 21:44:16 +00001217 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001218
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001219 size = 0;
1220 if (res >= 0) {
1221 size = res;
1222 res = 0;
1223 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001224 printf(" READ[%lu] %u bytes\n", (unsigned long) arg->fh,
1225 size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001226 fflush(stdout);
1227 }
Miklos Szeredi6ebe2342002-01-06 21:44:16 +00001228 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001229 memset(out, 0, sizeof(struct fuse_out_header));
1230 out->unique = in->unique;
1231 out->error = res;
1232 outsize = sizeof(struct fuse_out_header) + size;
Miklos Szeredie5183742005-02-02 11:14:04 +00001233
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001234 send_reply_raw(f, outbuf, outsize);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001235 free(outbuf);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001236 }
Miklos Szeredi5e183482001-10-31 14:52:35 +00001237}
Miklos Szeredib483c932001-10-29 14:57:57 +00001238
Miklos Szeredia181e612001-11-06 12:03:23 +00001239static void do_write(struct fuse *f, struct fuse_in_header *in,
1240 struct fuse_write_in *arg)
1241{
1242 int res;
1243 char *path;
Miklos Szerediad051c32004-07-02 09:22:50 +00001244 struct fuse_write_out outarg;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001245 struct fuse_file_info fi;
1246
1247 memset(&fi, 0, sizeof(fi));
1248 fi.fh = arg->fh;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001249 fi.writepage = arg->write_flags & 1;
Miklos Szeredia181e612001-11-06 12:03:23 +00001250
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001251 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001252 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001253 if (path != NULL) {
1254 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi1eea0322004-09-27 18:50:11 +00001255 printf("WRITE%s[%lu] %u bytes to %llu\n",
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001256 (arg->write_flags & 1) ? "PAGE" : "",
1257 (unsigned long) arg->fh, arg->size, arg->offset);
Miklos Szeredi6ebe2342002-01-06 21:44:16 +00001258 fflush(stdout);
1259 }
1260
Miklos Szeredia181e612001-11-06 12:03:23 +00001261 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001262 if (f->op.write)
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001263 res = f->op.write(path, PARAM(arg), arg->size, arg->offset, &fi);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001264 free(path);
Miklos Szeredia181e612001-11-06 12:03:23 +00001265 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001266
1267 if (res >= 0) {
Miklos Szerediad051c32004-07-02 09:22:50 +00001268 outarg.size = res;
1269 res = 0;
Miklos Szeredia181e612001-11-06 12:03:23 +00001270 }
1271
Miklos Szerediad051c32004-07-02 09:22:50 +00001272 send_reply(f, in, res, &outarg, sizeof(outarg));
Miklos Szeredia181e612001-11-06 12:03:23 +00001273}
1274
Miklos Szeredi77f39942004-03-25 11:17:52 +00001275static int default_statfs(struct statfs *buf)
1276{
1277 buf->f_namelen = 255;
1278 buf->f_bsize = 512;
1279 return 0;
1280}
1281
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001282static void convert_statfs_compat(struct fuse_statfs_compat1 *compatbuf,
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001283 struct statfs *statfs)
1284{
1285 statfs->f_bsize = compatbuf->block_size;
1286 statfs->f_blocks = compatbuf->blocks;
1287 statfs->f_bfree = compatbuf->blocks_free;
1288 statfs->f_bavail = compatbuf->blocks_free;
1289 statfs->f_files = compatbuf->files;
1290 statfs->f_ffree = compatbuf->files_free;
1291 statfs->f_namelen = compatbuf->namelen;
1292}
1293
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001294static void convert_statfs(struct statfs *statfs, struct fuse_kstatfs *kstatfs)
1295{
1296 kstatfs->bsize = statfs->f_bsize;
1297 kstatfs->blocks = statfs->f_blocks;
1298 kstatfs->bfree = statfs->f_bfree;
1299 kstatfs->bavail = statfs->f_bavail;
1300 kstatfs->files = statfs->f_files;
1301 kstatfs->ffree = statfs->f_ffree;
1302 kstatfs->namelen = statfs->f_namelen;
1303}
1304
Mark Glinesd84b39a2002-01-07 16:32:02 +00001305static void do_statfs(struct fuse *f, struct fuse_in_header *in)
1306{
1307 int res;
Mark Glinesd84b39a2002-01-07 16:32:02 +00001308 struct fuse_statfs_out arg;
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001309 struct statfs buf;
Mark Glinesd84b39a2002-01-07 16:32:02 +00001310
Miklos Szeredi77f39942004-03-25 11:17:52 +00001311 memset(&buf, 0, sizeof(struct statfs));
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001312 if (f->op.statfs) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001313 if (!f->compat || f->compat > 11)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001314 res = f->op.statfs("/", &buf);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001315 else {
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001316 struct fuse_statfs_compat1 compatbuf;
1317 memset(&compatbuf, 0, sizeof(struct fuse_statfs_compat1));
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001318 res = ((struct fuse_operations_compat1 *) &f->op)->statfs(&compatbuf);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001319 if (res == 0)
1320 convert_statfs_compat(&compatbuf, &buf);
1321 }
1322 }
Miklos Szeredi77f39942004-03-25 11:17:52 +00001323 else
1324 res = default_statfs(&buf);
1325
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001326 if (res == 0)
Miklos Szeredi77f39942004-03-25 11:17:52 +00001327 convert_statfs(&buf, &arg.st);
Miklos Szeredi4b2bef42002-01-09 12:23:27 +00001328
Mark Glinesd84b39a2002-01-07 16:32:02 +00001329 send_reply(f, in, res, &arg, sizeof(arg));
1330}
1331
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001332static void do_fsync(struct fuse *f, struct fuse_in_header *in,
1333 struct fuse_fsync_in *inarg)
1334{
1335 int res;
1336 char *path;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001337 struct fuse_file_info fi;
1338
1339 memset(&fi, 0, sizeof(fi));
1340 fi.fh = inarg->fh;
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001341
1342 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001343 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001344 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001345 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001346 printf("FSYNC[%lu]\n", (unsigned long) inarg->fh);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001347 fflush(stdout);
1348 }
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001349 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001350 if (f->op.fsync)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001351 res = f->op.fsync(path, inarg->fsync_flags & 1, &fi);
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001352 free(path);
1353 }
1354 send_reply(f, in, res, NULL, 0);
1355}
1356
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001357static void do_setxattr(struct fuse *f, struct fuse_in_header *in,
1358 struct fuse_setxattr_in *arg)
1359{
1360 int res;
1361 char *path;
1362 char *name = PARAM(arg);
1363 unsigned char *value = name + strlen(name) + 1;
1364
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.setxattr)
1370 res = f->op.setxattr(path, name, value, arg->size, arg->flags);
1371 free(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001372 }
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001373 send_reply(f, in, res, NULL, 0);
1374}
1375
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001376static int common_getxattr(struct fuse *f, struct fuse_in_header *in,
1377 const char *name, char *value, size_t size)
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001378{
1379 int res;
1380 char *path;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001381
1382 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001383 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001384 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001385 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001386 if (f->op.getxattr)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001387 res = f->op.getxattr(path, name, value, size);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001388 free(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001389 }
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001390 return res;
1391}
1392
1393static void do_getxattr_read(struct fuse *f, struct fuse_in_header *in,
1394 const char *name, size_t size)
1395{
1396 int res;
1397 char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001398 if (outbuf == NULL)
1399 send_reply(f, in, -ENOMEM, NULL, 0);
1400 else {
1401 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
1402 char *value = outbuf + sizeof(struct fuse_out_header);
Miklos Szeredie5183742005-02-02 11:14:04 +00001403
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001404 res = common_getxattr(f, in, name, value, size);
1405 size = 0;
1406 if (res > 0) {
1407 size = res;
1408 res = 0;
1409 }
1410 memset(out, 0, sizeof(struct fuse_out_header));
1411 out->unique = in->unique;
1412 out->error = res;
Miklos Szeredie5183742005-02-02 11:14:04 +00001413
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001414 send_reply_raw(f, outbuf, sizeof(struct fuse_out_header) + size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001415 free(outbuf);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001416 }
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001417}
1418
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001419static void do_getxattr_size(struct fuse *f, struct fuse_in_header *in,
1420 const char *name)
1421{
1422 int res;
1423 struct fuse_getxattr_out arg;
1424
1425 res = common_getxattr(f, in, name, NULL, 0);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001426 if (res >= 0) {
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001427 arg.size = res;
1428 res = 0;
1429 }
1430 send_reply(f, in, res, &arg, sizeof(arg));
1431}
1432
1433static void do_getxattr(struct fuse *f, struct fuse_in_header *in,
1434 struct fuse_getxattr_in *arg)
1435{
1436 char *name = PARAM(arg);
Miklos Szeredie5183742005-02-02 11:14:04 +00001437
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001438 if (arg->size)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001439 do_getxattr_read(f, in, name, arg->size);
1440 else
1441 do_getxattr_size(f, in, name);
1442}
1443
1444static int common_listxattr(struct fuse *f, struct fuse_in_header *in,
1445 char *list, size_t size)
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001446{
1447 int res;
1448 char *path;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001449
1450 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001451 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001452 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001453 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001454 if (f->op.listxattr)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001455 res = f->op.listxattr(path, list, size);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001456 free(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001457 }
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001458 return res;
1459}
1460
1461static void do_listxattr_read(struct fuse *f, struct fuse_in_header *in,
1462 size_t size)
1463{
1464 int res;
1465 char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001466 if (outbuf == NULL)
1467 send_reply(f, in, -ENOMEM, NULL, 0);
1468 else {
1469 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
1470 char *list = outbuf + sizeof(struct fuse_out_header);
Miklos Szeredie5183742005-02-02 11:14:04 +00001471
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001472 res = common_listxattr(f, in, list, size);
1473 size = 0;
1474 if (res > 0) {
1475 size = res;
1476 res = 0;
1477 }
1478 memset(out, 0, sizeof(struct fuse_out_header));
1479 out->unique = in->unique;
1480 out->error = res;
Miklos Szeredie5183742005-02-02 11:14:04 +00001481
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001482 send_reply_raw(f, outbuf, sizeof(struct fuse_out_header) + size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001483 free(outbuf);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001484 }
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001485}
1486
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001487static void do_listxattr_size(struct fuse *f, struct fuse_in_header *in)
1488{
1489 int res;
1490 struct fuse_getxattr_out arg;
1491
1492 res = common_listxattr(f, in, NULL, 0);
1493 if (res >= 0) {
1494 arg.size = res;
1495 res = 0;
1496 }
1497 send_reply(f, in, res, &arg, sizeof(arg));
1498}
1499
1500static void do_listxattr(struct fuse *f, struct fuse_in_header *in,
1501 struct fuse_getxattr_in *arg)
1502{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001503 if (arg->size)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001504 do_listxattr_read(f, in, arg->size);
1505 else
1506 do_listxattr_size(f, in);
1507}
1508
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001509static void do_removexattr(struct fuse *f, struct fuse_in_header *in,
1510 char *name)
1511{
1512 int res;
1513 char *path;
1514
1515 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001516 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001517 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001518 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001519 if (f->op.removexattr)
1520 res = f->op.removexattr(path, name);
1521 free(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001522 }
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001523 send_reply(f, in, res, NULL, 0);
1524}
1525
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001526static void do_init(struct fuse *f, struct fuse_in_header *in,
1527 struct fuse_init_in_out *arg)
1528{
1529 struct fuse_init_in_out outarg;
1530 if (f->flags & FUSE_DEBUG) {
1531 printf(" INIT: %u.%u\n", arg->major, arg->minor);
1532 fflush(stdout);
1533 }
1534 f->got_init = 1;
1535 memset(&outarg, 0, sizeof(outarg));
1536 outarg.major = FUSE_KERNEL_VERSION;
1537 outarg.minor = FUSE_KERNEL_MINOR_VERSION;
1538 send_reply(f, in, 0, &outarg, sizeof(outarg));
1539}
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001540
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001541static struct fuse_dirhandle *get_dirhandle(unsigned long fh)
1542{
1543 return (struct fuse_dirhandle *) fh;
1544}
1545
1546static void do_opendir(struct fuse *f, struct fuse_in_header *in,
1547 struct fuse_open_in *arg)
1548{
1549 int res;
1550 struct fuse_open_out outarg;
1551 struct fuse_dirhandle *dh;
1552
1553 (void) arg;
1554
1555 res = -ENOMEM;
1556 dh = (struct fuse_dirhandle *) malloc(sizeof(struct fuse_dirhandle));
1557 if (dh != NULL) {
1558 dh->fuse = f;
1559 dh->fp = tmpfile();
1560 dh->filled = 0;
Miklos Szeredie5183742005-02-02 11:14:04 +00001561
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001562 res = -EIO;
1563 if (dh->fp == NULL) {
1564 perror("fuse: failed to create temporary file");
1565 free(dh);
1566 } else {
1567 outarg.fh = (unsigned long) dh;
1568 res = 0;
1569 }
1570 }
1571 send_reply(f, in, res, &outarg, sizeof(outarg));
1572}
1573
1574static void do_readdir(struct fuse *f, struct fuse_in_header *in,
1575 struct fuse_read_in *arg)
1576{
1577 int res;
1578 char *outbuf;
1579 struct fuse_dirhandle *dh = get_dirhandle(arg->fh);
1580
1581 if (!dh->filled) {
1582 res = common_getdir(f, in, dh);
Miklos Szeredi01fd89c2005-01-21 11:18:35 +00001583 if (res) {
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001584 send_reply(f, in, res, NULL, 0);
Miklos Szeredi01fd89c2005-01-21 11:18:35 +00001585 return;
1586 }
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001587 dh->filled = 1;
1588 }
1589 outbuf = (char *) malloc(sizeof(struct fuse_out_header) + arg->size);
1590 if (outbuf == NULL)
1591 send_reply(f, in, -ENOMEM, NULL, 0);
1592 else {
1593 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
1594 char *buf = outbuf + sizeof(struct fuse_out_header);
1595 size_t size = 0;
1596 size_t outsize;
1597 fseek(dh->fp, arg->offset, SEEK_SET);
1598 res = fread(buf, 1, arg->size, dh->fp);
1599 if (res == 0 && ferror(dh->fp))
1600 res = -EIO;
1601 else {
1602 size = res;
1603 res = 0;
1604 }
1605 memset(out, 0, sizeof(struct fuse_out_header));
1606 out->unique = in->unique;
1607 out->error = res;
1608 outsize = sizeof(struct fuse_out_header) + size;
Miklos Szeredie5183742005-02-02 11:14:04 +00001609
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001610 send_reply_raw(f, outbuf, outsize);
1611 free(outbuf);
1612 }
1613}
1614
1615static void do_releasedir(struct fuse *f, struct fuse_in_header *in,
1616 struct fuse_release_in *arg)
1617{
1618 struct fuse_dirhandle *dh = get_dirhandle(arg->fh);
1619 fclose(dh->fp);
1620 free(dh);
1621 send_reply(f, in, 0, NULL, 0);
1622}
1623
1624
Miklos Szeredi43696432001-11-18 19:15:05 +00001625static void free_cmd(struct fuse_cmd *cmd)
1626{
1627 free(cmd->buf);
1628 free(cmd);
1629}
1630
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001631void fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd)
Miklos Szeredia181e612001-11-06 12:03:23 +00001632{
Miklos Szeredia181e612001-11-06 12:03:23 +00001633 struct fuse_in_header *in = (struct fuse_in_header *) cmd->buf;
1634 void *inarg = cmd->buf + sizeof(struct fuse_in_header);
1635 size_t argsize;
Miklos Szeredid169f312004-09-22 08:48:26 +00001636 struct fuse_context *ctx = fuse_get_context();
Miklos Szeredia181e612001-11-06 12:03:23 +00001637
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001638 fuse_dec_avail(f);
Miklos Szeredi33232032001-11-19 17:55:51 +00001639
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001640 if ((f->flags & FUSE_DEBUG)) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001641 printf("unique: %llu, opcode: %s (%i), nodeid: %lu, insize: %i\n",
1642 in->unique, opname(in->opcode), in->opcode,
1643 (unsigned long) in->nodeid, cmd->buflen);
Miklos Szeredic0938ea2001-11-07 12:35:06 +00001644 fflush(stdout);
1645 }
Miklos Szeredife25def2001-12-20 15:38:05 +00001646
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001647 if (!f->got_init && in->opcode != FUSE_INIT) {
1648 /* Old kernel version probably */
1649 send_reply(f, in, -EPROTO, NULL, 0);
1650 goto out;
1651 }
1652
Miklos Szeredid169f312004-09-22 08:48:26 +00001653 ctx->fuse = f;
Miklos Szeredife25def2001-12-20 15:38:05 +00001654 ctx->uid = in->uid;
1655 ctx->gid = in->gid;
Miklos Szeredi1f18db52004-09-27 06:54:49 +00001656 ctx->pid = in->pid;
Miklos Szeredie5183742005-02-02 11:14:04 +00001657
Miklos Szeredia181e612001-11-06 12:03:23 +00001658 argsize = cmd->buflen - sizeof(struct fuse_in_header);
Miklos Szeredie5183742005-02-02 11:14:04 +00001659
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001660 switch (in->opcode) {
Miklos Szeredia181e612001-11-06 12:03:23 +00001661 case FUSE_LOOKUP:
1662 do_lookup(f, in, (char *) inarg);
1663 break;
1664
Miklos Szeredia181e612001-11-06 12:03:23 +00001665 case FUSE_GETATTR:
1666 do_getattr(f, in);
1667 break;
1668
1669 case FUSE_SETATTR:
1670 do_setattr(f, in, (struct fuse_setattr_in *) inarg);
1671 break;
1672
1673 case FUSE_READLINK:
1674 do_readlink(f, in);
1675 break;
1676
1677 case FUSE_GETDIR:
1678 do_getdir(f, in);
1679 break;
1680
1681 case FUSE_MKNOD:
1682 do_mknod(f, in, (struct fuse_mknod_in *) inarg);
1683 break;
Miklos Szeredie5183742005-02-02 11:14:04 +00001684
Miklos Szeredia181e612001-11-06 12:03:23 +00001685 case FUSE_MKDIR:
1686 do_mkdir(f, in, (struct fuse_mkdir_in *) inarg);
1687 break;
Miklos Szeredie5183742005-02-02 11:14:04 +00001688
Miklos Szeredia181e612001-11-06 12:03:23 +00001689 case FUSE_UNLINK:
Miklos Szeredib5958612004-02-20 14:10:49 +00001690 do_unlink(f, in, (char *) inarg);
1691 break;
1692
Miklos Szeredia181e612001-11-06 12:03:23 +00001693 case FUSE_RMDIR:
Miklos Szeredib5958612004-02-20 14:10:49 +00001694 do_rmdir(f, in, (char *) inarg);
Miklos Szeredia181e612001-11-06 12:03:23 +00001695 break;
1696
1697 case FUSE_SYMLINK:
Miklos Szeredie5183742005-02-02 11:14:04 +00001698 do_symlink(f, in, (char *) inarg,
Miklos Szeredia181e612001-11-06 12:03:23 +00001699 ((char *) inarg) + strlen((char *) inarg) + 1);
1700 break;
1701
1702 case FUSE_RENAME:
1703 do_rename(f, in, (struct fuse_rename_in *) inarg);
1704 break;
Miklos Szeredie5183742005-02-02 11:14:04 +00001705
Miklos Szeredia181e612001-11-06 12:03:23 +00001706 case FUSE_LINK:
1707 do_link(f, in, (struct fuse_link_in *) inarg);
1708 break;
1709
1710 case FUSE_OPEN:
1711 do_open(f, in, (struct fuse_open_in *) inarg);
1712 break;
1713
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001714 case FUSE_FLUSH:
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001715 do_flush(f, in, (struct fuse_flush_in *) inarg);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001716 break;
1717
Miklos Szeredi9478e862002-12-11 09:50:26 +00001718 case FUSE_RELEASE:
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001719 do_release(f, in, (struct fuse_release_in *) inarg);
Miklos Szeredi9478e862002-12-11 09:50:26 +00001720 break;
1721
Miklos Szeredia181e612001-11-06 12:03:23 +00001722 case FUSE_READ:
1723 do_read(f, in, (struct fuse_read_in *) inarg);
1724 break;
1725
1726 case FUSE_WRITE:
1727 do_write(f, in, (struct fuse_write_in *) inarg);
1728 break;
1729
Mark Glinesd84b39a2002-01-07 16:32:02 +00001730 case FUSE_STATFS:
1731 do_statfs(f, in);
1732 break;
1733
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001734 case FUSE_FSYNC:
1735 do_fsync(f, in, (struct fuse_fsync_in *) inarg);
1736 break;
1737
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001738 case FUSE_SETXATTR:
1739 do_setxattr(f, in, (struct fuse_setxattr_in *) inarg);
1740 break;
1741
1742 case FUSE_GETXATTR:
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001743 do_getxattr(f, in, (struct fuse_getxattr_in *) inarg);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001744 break;
1745
1746 case FUSE_LISTXATTR:
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001747 do_listxattr(f, in, (struct fuse_getxattr_in *) inarg);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001748 break;
1749
1750 case FUSE_REMOVEXATTR:
1751 do_removexattr(f, in, (char *) inarg);
1752 break;
1753
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001754 case FUSE_INIT:
1755 do_init(f, in, (struct fuse_init_in_out *) inarg);
1756 break;
1757
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001758 case FUSE_OPENDIR:
1759 do_opendir(f, in, (struct fuse_open_in *) inarg);
1760 break;
1761
1762 case FUSE_READDIR:
1763 do_readdir(f, in, (struct fuse_read_in *) inarg);
1764 break;
1765
1766 case FUSE_RELEASEDIR:
1767 do_releasedir(f, in, (struct fuse_release_in *) inarg);
1768 break;
1769
Miklos Szeredia181e612001-11-06 12:03:23 +00001770 default:
Miklos Szeredi43696432001-11-18 19:15:05 +00001771 send_reply(f, in, -ENOSYS, NULL, 0);
Miklos Szeredia181e612001-11-06 12:03:23 +00001772 }
Miklos Szeredi43696432001-11-18 19:15:05 +00001773
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001774 out:
Miklos Szeredi43696432001-11-18 19:15:05 +00001775 free_cmd(cmd);
Miklos Szeredia181e612001-11-06 12:03:23 +00001776}
1777
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001778int fuse_exited(struct fuse* f)
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001779{
1780 return f->exited;
1781}
1782
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001783struct fuse_cmd *fuse_read_cmd(struct fuse *f)
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001784{
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001785 ssize_t res;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001786 struct fuse_cmd *cmd;
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001787 struct fuse_in_header *in;
1788 void *inarg;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001789
Miklos Szeredi43696432001-11-18 19:15:05 +00001790 cmd = (struct fuse_cmd *) malloc(sizeof(struct fuse_cmd));
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001791 if (cmd == NULL) {
1792 fprintf(stderr, "fuse: failed to allocate cmd in read\n");
1793 return NULL;
1794 }
Miklos Szeredi43696432001-11-18 19:15:05 +00001795 cmd->buf = (char *) malloc(FUSE_MAX_IN);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001796 if (cmd->buf == NULL) {
1797 fprintf(stderr, "fuse: failed to allocate read buffer\n");
1798 free(cmd);
1799 return NULL;
1800 }
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001801 in = (struct fuse_in_header *) cmd->buf;
1802 inarg = cmd->buf + sizeof(struct fuse_in_header);
Miklos Szeredi43696432001-11-18 19:15:05 +00001803
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001804 res = read(f->fd, cmd->buf, FUSE_MAX_IN);
1805 if (res == -1) {
1806 free_cmd(cmd);
Miklos Szeredie56818b2004-12-12 11:45:24 +00001807 if (fuse_exited(f) || errno == EINTR || errno == ENOENT)
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001808 return NULL;
Miklos Szeredie5183742005-02-02 11:14:04 +00001809
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001810 /* ENODEV means we got unmounted, so we silenty return failure */
1811 if (errno != ENODEV) {
1812 /* BAD... This will happen again */
1813 perror("fuse: reading device");
1814 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001815
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001816 fuse_exit(f);
1817 return NULL;
1818 }
1819 if ((size_t) res < sizeof(struct fuse_in_header)) {
1820 free_cmd(cmd);
1821 /* Cannot happen */
1822 fprintf(stderr, "short read on fuse device\n");
1823 fuse_exit(f);
1824 return NULL;
1825 }
1826 cmd->buflen = res;
Miklos Szeredie5183742005-02-02 11:14:04 +00001827
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001828 /* Forget is special, it can be done without messing with threads. */
1829 if (in->opcode == FUSE_FORGET) {
1830 do_forget(f, in, (struct fuse_forget_in *) inarg);
1831 free_cmd(cmd);
1832 return NULL;
1833 }
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001834
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001835 return cmd;
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001836}
1837
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001838int fuse_loop(struct fuse *f)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001839{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001840 if (f == NULL)
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001841 return -1;
Miklos Szeredic40748a2004-02-20 16:38:45 +00001842
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001843 while (1) {
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001844 struct fuse_cmd *cmd;
1845
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001846 if (fuse_exited(f))
Miklos Szeredi874e3c12004-11-01 23:15:20 +00001847 break;
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001848
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001849 cmd = fuse_read_cmd(f);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001850 if (cmd == NULL)
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001851 continue;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001852
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001853 fuse_process_cmd(f, cmd);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001854 }
Miklos Szeredi874e3c12004-11-01 23:15:20 +00001855 f->exited = 0;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001856 return 0;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001857}
1858
Miklos Szeredi891b8742004-07-29 09:27:49 +00001859int fuse_invalidate(struct fuse *f, const char *path)
1860{
Miklos Szeredie56818b2004-12-12 11:45:24 +00001861 (void) f;
1862 (void) path;
1863 return -EINVAL;
Miklos Szeredi891b8742004-07-29 09:27:49 +00001864}
1865
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001866void fuse_exit(struct fuse *f)
1867{
1868 f->exited = 1;
1869}
1870
Miklos Szeredid169f312004-09-22 08:48:26 +00001871struct fuse_context *fuse_get_context()
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001872{
Miklos Szeredid169f312004-09-22 08:48:26 +00001873 static struct fuse_context context;
1874 if (fuse_getcontext)
1875 return fuse_getcontext();
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001876 else
Miklos Szeredid169f312004-09-22 08:48:26 +00001877 return &context;
1878}
1879
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001880void fuse_set_getcontext_func(struct fuse_context *(*func)(void))
Miklos Szeredid169f312004-09-22 08:48:26 +00001881{
1882 fuse_getcontext = func;
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001883}
1884
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001885int fuse_is_lib_option(const char *opt)
1886{
1887 if (strcmp(opt, "debug") == 0 ||
Miklos Szeredia13d9002004-11-02 17:32:03 +00001888 strcmp(opt, "hard_remove") == 0 ||
1889 strcmp(opt, "use_ino") == 0)
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001890 return 1;
1891 else
1892 return 0;
1893}
1894
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001895static int parse_lib_opts(struct fuse *f, const char *opts)
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001896{
1897 if (opts) {
1898 char *xopts = strdup(opts);
1899 char *s = xopts;
1900 char *opt;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001901
Miklos Szeredie56818b2004-12-12 11:45:24 +00001902 if (xopts == NULL) {
1903 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001904 return -1;
Miklos Szeredie56818b2004-12-12 11:45:24 +00001905 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001906
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001907 while((opt = strsep(&s, ","))) {
1908 if (strcmp(opt, "debug") == 0)
1909 f->flags |= FUSE_DEBUG;
1910 else if (strcmp(opt, "hard_remove") == 0)
1911 f->flags |= FUSE_HARD_REMOVE;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001912 else if (strcmp(opt, "use_ino") == 0)
1913 f->flags |= FUSE_USE_INO;
Miklos Szeredie5183742005-02-02 11:14:04 +00001914 else
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001915 fprintf(stderr, "fuse: warning: unknown option `%s'\n", opt);
1916 }
1917 free(xopts);
1918 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001919 return 0;
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001920}
1921
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001922struct fuse *fuse_new_common(int fd, const char *opts,
1923 const struct fuse_operations *op,
1924 size_t op_size, int compat)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001925{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001926 struct fuse *f;
1927 struct node *root;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001928
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001929 if (sizeof(struct fuse_operations) < op_size) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001930 fprintf(stderr, "fuse: warning: library too old, some operations may not not work\n");
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001931 op_size = sizeof(struct fuse_operations);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001932 }
1933
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001934 f = (struct fuse *) calloc(1, sizeof(struct fuse));
Miklos Szeredie56818b2004-12-12 11:45:24 +00001935 if (f == NULL) {
1936 fprintf(stderr, "fuse: failed to allocate fuse object\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001937 goto out;
Miklos Szeredie56818b2004-12-12 11:45:24 +00001938 }
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001939
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001940 if (parse_lib_opts(f, opts) == -1)
1941 goto out_free;
1942
Miklos Szeredi8cffdb92001-11-09 14:49:18 +00001943 f->fd = fd;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001944 f->ctr = 0;
Miklos Szeredi76f65782004-02-19 16:55:40 +00001945 f->generation = 0;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001946 /* FIXME: Dynamic hash table */
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001947 f->name_table_size = 14057;
1948 f->name_table = (struct node **)
1949 calloc(1, sizeof(struct node *) * f->name_table_size);
Miklos Szeredie56818b2004-12-12 11:45:24 +00001950 if (f->name_table == NULL) {
1951 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001952 goto out_free;
Miklos Szeredie56818b2004-12-12 11:45:24 +00001953 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001954
Miklos Szeredia13d9002004-11-02 17:32:03 +00001955 f->id_table_size = 14057;
1956 f->id_table = (struct node **)
1957 calloc(1, sizeof(struct node *) * f->id_table_size);
Miklos Szeredie56818b2004-12-12 11:45:24 +00001958 if (f->id_table == NULL) {
1959 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001960 goto out_free_name_table;
Miklos Szeredie56818b2004-12-12 11:45:24 +00001961 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001962
Miklos Szeredia25d4c22004-11-23 22:32:16 +00001963#ifndef USE_UCLIBC
1964 pthread_mutex_init(&f->lock, NULL);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001965 pthread_mutex_init(&f->worker_lock, NULL);
Miklos Szeredia25d4c22004-11-23 22:32:16 +00001966#else
1967 {
1968 pthread_mutexattr_t attr;
1969 pthread_mutexattr_init(&attr);
1970 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
1971 pthread_mutex_init(&f->lock, &attr);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001972 pthread_mutex_init(&f->worker_lock, &attr);
Miklos Szeredia25d4c22004-11-23 22:32:16 +00001973 pthread_mutexattr_destroy(&attr);
1974 }
1975#endif
Miklos Szeredi33232032001-11-19 17:55:51 +00001976 f->numworker = 0;
1977 f->numavail = 0;
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001978 memcpy(&f->op, op, op_size);
1979 f->compat = compat;
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001980 f->exited = 0;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001981
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001982 root = (struct node *) calloc(1, sizeof(struct node));
Miklos Szeredie56818b2004-12-12 11:45:24 +00001983 if (root == NULL) {
1984 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredia13d9002004-11-02 17:32:03 +00001985 goto out_free_id_table;
Miklos Szeredie56818b2004-12-12 11:45:24 +00001986 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001987
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001988 root->name = strdup("/");
Miklos Szeredie56818b2004-12-12 11:45:24 +00001989 if (root->name == NULL) {
1990 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001991 goto out_free_root;
Miklos Szeredie56818b2004-12-12 11:45:24 +00001992 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001993
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001994 root->parent = 0;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001995 root->nodeid = FUSE_ROOT_ID;
Miklos Szeredi76f65782004-02-19 16:55:40 +00001996 root->generation = 0;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001997 root->refctr = 1;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001998 hash_id(f, root);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001999
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002000 return f;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002001
2002 out_free_root:
2003 free(root);
Miklos Szeredia13d9002004-11-02 17:32:03 +00002004 out_free_id_table:
2005 free(f->id_table);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002006 out_free_name_table:
2007 free(f->name_table);
2008 out_free:
2009 free(f);
2010 out:
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002011 return NULL;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002012}
2013
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002014struct fuse *fuse_new(int fd, const char *opts,
2015 const struct fuse_operations *op, size_t op_size)
2016{
2017 return fuse_new_common(fd, opts, op, op_size, 0);
2018}
2019
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002020struct fuse *fuse_new_compat2(int fd, const char *opts,
2021 const struct fuse_operations_compat2 *op)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002022{
2023 return fuse_new_common(fd, opts, (struct fuse_operations *) op,
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002024 sizeof(struct fuse_operations_compat2), 21);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002025}
2026
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002027struct fuse *fuse_new_compat1(int fd, int flags,
2028 const struct fuse_operations_compat1 *op)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002029{
2030 char *opts = NULL;
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002031 if (flags & FUSE_DEBUG_COMPAT1)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002032 opts = "debug";
2033 return fuse_new_common(fd, opts, (struct fuse_operations *) op,
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002034 sizeof(struct fuse_operations_compat1), 11);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002035}
2036
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002037void fuse_destroy(struct fuse *f)
2038{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002039 size_t i;
Miklos Szeredia13d9002004-11-02 17:32:03 +00002040 for (i = 0; i < f->id_table_size; i++) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002041 struct node *node;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002042
Miklos Szeredia13d9002004-11-02 17:32:03 +00002043 for (node = f->id_table[i]; node != NULL; node = node->id_next) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002044 if (node->is_hidden) {
Miklos Szeredia13d9002004-11-02 17:32:03 +00002045 char *path = get_path(f, node->nodeid);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002046 if (path)
2047 f->op.unlink(path);
2048 }
2049 }
2050 }
Miklos Szeredia13d9002004-11-02 17:32:03 +00002051 for (i = 0; i < f->id_table_size; i++) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002052 struct node *node;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002053 struct node *next;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002054
Miklos Szeredia13d9002004-11-02 17:32:03 +00002055 for (node = f->id_table[i]; node != NULL; node = next) {
2056 next = node->id_next;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002057 free_node(node);
2058 }
2059 }
Miklos Szeredia13d9002004-11-02 17:32:03 +00002060 free(f->id_table);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002061 free(f->name_table);
Miklos Szeredia181e612001-11-06 12:03:23 +00002062 pthread_mutex_destroy(&f->lock);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00002063 pthread_mutex_destroy(&f->worker_lock);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002064 free(f);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002065}
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002066
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002067__asm__(".symver fuse_exited,__fuse_exited@");
2068__asm__(".symver fuse_process_cmd,__fuse_process_cmd@");
2069__asm__(".symver fuse_read_cmd,__fuse_read_cmd@");
2070__asm__(".symver fuse_set_getcontext_func,__fuse_set_getcontext_func@");
2071__asm__(".symver fuse_new_compat2,fuse_new@");