blob: 40230af1181c7a8731ab3b9b5b83241728963b33 [file] [log] [blame]
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi149f6072005-01-10 12:29:28 +00003 Copyright (C) 2001-2005 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00004
Miklos Szeredi8b39a9f2002-10-25 12:41:16 +00005 This program can be distributed under the terms of the GNU LGPL.
6 See the file COPYING.LIB
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00007*/
8
Miklos Szeredicb264512004-06-23 18:52:50 +00009#include <config.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000010#include "fuse_i.h"
Miklos Szeredi0f62d722005-01-04 12:45:54 +000011#include "fuse_compat.h"
Miklos Szeredib9b94cd2004-12-01 18:56:39 +000012#include "fuse_kernel.h"
Miklos Szeredi30e093a2005-04-03 17:44:54 +000013#include "fuse_kernel_compat5.h"
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000014
Miklos Szeredi0f62d722005-01-04 12:45:54 +000015#include <stdio.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000016#include <string.h>
Miklos Szeredi97c61e92001-11-07 12:09:43 +000017#include <stdlib.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000018#include <unistd.h>
Miklos Szeredi97c61e92001-11-07 12:09:43 +000019#include <limits.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000020#include <errno.h>
Miklos Szeredi0f62d722005-01-04 12:45:54 +000021#include <assert.h>
22#include <stdint.h>
Miklos Szeredi019b4e92001-12-26 18:08:09 +000023#include <sys/param.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000024
Miklos Szeredi0f62d722005-01-04 12:45:54 +000025/* FUSE flags: */
26
27/** Enable debuging output */
28#define FUSE_DEBUG (1 << 1)
29
30/** If a file is removed but it's still open, don't hide the file but
31 remove it immediately */
32#define FUSE_HARD_REMOVE (1 << 2)
33
34/** Use st_ino field in getattr instead of generating inode numbers */
35#define FUSE_USE_INO (1 << 3)
36
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +000037#define FUSE_KERNEL_MINOR_VERSION_NEED 1
Miklos Szeredia25d4c22004-11-23 22:32:16 +000038#define FUSE_VERSION_FILE_OLD "/proc/fs/fuse/version"
Miklos Szeredi162bcbb2004-11-29 23:43:44 +000039#define FUSE_VERSION_FILE_NEW "/sys/fs/fuse/version"
Miklos Szeredia25d4c22004-11-23 22:32:16 +000040#define FUSE_DEV_OLD "/proc/fs/fuse/dev"
41
Miklos Szeredi97c61e92001-11-07 12:09:43 +000042#define FUSE_MAX_PATH 4096
Miklos Szeredi30e093a2005-04-03 17:44:54 +000043#define PARAM_T(inarg, type) (((char *)(inarg)) + sizeof(type))
44#define PARAM(inarg) PARAM_T(inarg, *(inarg))
45#define PARAM_COMPAT(f, inarg, type) \
46 ((f)->major == 5 ? PARAM_T(inarg, struct type ## _compat5) : PARAM(inarg))
47
48#define MEMBER_COMPAT(f, ptr, memb, type) \
49 ((f)->major == 5 ? &((struct type ## _compat5 *) (ptr))->memb : &ptr->memb)
50
51#define SIZEOF_COMPAT(f, type) \
52 ((f)->major == 5 ? sizeof(struct type ## _compat5) : sizeof(struct type))
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000053
Miklos Szeredi254d5ed2004-03-02 11:11:24 +000054#define ENTRY_REVALIDATE_TIME 1 /* sec */
55#define ATTR_REVALIDATE_TIME 1 /* sec */
56
Miklos Szeredi0f62d722005-01-04 12:45:54 +000057
58struct node {
59 struct node *name_next;
60 struct node *id_next;
61 nodeid_t nodeid;
62 unsigned int generation;
63 int refctr;
64 nodeid_t parent;
65 char *name;
66 uint64_t version;
67 int open_count;
68 int is_hidden;
69};
70
71struct fuse_dirhandle {
72 struct fuse *fuse;
Miklos Szeredib92d9782005-02-07 16:10:49 +000073 unsigned char *contents;
74 unsigned len;
Miklos Szeredi3ead28e2005-01-18 21:23:41 +000075 int filled;
Miklos Szerediede1f7a2005-04-01 21:08:57 +000076 unsigned long fh;
77};
78
79struct fuse_dirbuf {
80 struct fuse *fuse;
81 char *buf;
82 unsigned len;
Miklos Szeredi0f62d722005-01-04 12:45:54 +000083};
84
85struct fuse_cmd {
86 char *buf;
87 size_t buflen;
88};
89
90
Miklos Szeredid169f312004-09-22 08:48:26 +000091static struct fuse_context *(*fuse_getcontext)(void) = NULL;
92
Miklos Szeredic8ba2372002-12-10 12:26:00 +000093static const char *opname(enum fuse_opcode opcode)
94{
Miklos Szeredie5183742005-02-02 11:14:04 +000095 switch (opcode) {
Miklos Szeredi3ed84232004-03-30 15:17:26 +000096 case FUSE_LOOKUP: return "LOOKUP";
97 case FUSE_FORGET: return "FORGET";
98 case FUSE_GETATTR: return "GETATTR";
99 case FUSE_SETATTR: return "SETATTR";
100 case FUSE_READLINK: return "READLINK";
101 case FUSE_SYMLINK: return "SYMLINK";
Miklos Szeredi3ed84232004-03-30 15:17:26 +0000102 case FUSE_MKNOD: return "MKNOD";
103 case FUSE_MKDIR: return "MKDIR";
104 case FUSE_UNLINK: return "UNLINK";
105 case FUSE_RMDIR: return "RMDIR";
106 case FUSE_RENAME: return "RENAME";
107 case FUSE_LINK: return "LINK";
108 case FUSE_OPEN: return "OPEN";
109 case FUSE_READ: return "READ";
110 case FUSE_WRITE: return "WRITE";
111 case FUSE_STATFS: return "STATFS";
Miklos Szeredi99f20742004-05-19 08:01:10 +0000112 case FUSE_FLUSH: return "FLUSH";
Miklos Szeredi3ed84232004-03-30 15:17:26 +0000113 case FUSE_RELEASE: return "RELEASE";
114 case FUSE_FSYNC: return "FSYNC";
115 case FUSE_SETXATTR: return "SETXATTR";
116 case FUSE_GETXATTR: return "GETXATTR";
117 case FUSE_LISTXATTR: return "LISTXATTR";
118 case FUSE_REMOVEXATTR: return "REMOVEXATTR";
Miklos Szeredi3f0005f2005-01-04 19:24:31 +0000119 case FUSE_INIT: return "INIT";
Miklos Szeredi3ead28e2005-01-18 21:23:41 +0000120 case FUSE_OPENDIR: return "OPENDIR";
121 case FUSE_READDIR: return "READDIR";
122 case FUSE_RELEASEDIR: return "RELEASEDIR";
Miklos Szeredi4283ee72005-03-21 12:09:04 +0000123 case FUSE_FSYNCDIR: return "FSYNCDIR";
Miklos Szeredi99f20742004-05-19 08:01:10 +0000124 default: return "???";
Miklos Szeredic8ba2372002-12-10 12:26:00 +0000125 }
126}
127
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000128static inline void fuse_dec_avail(struct fuse *f)
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000129{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000130 pthread_mutex_lock(&f->worker_lock);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000131 f->numavail --;
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000132 pthread_mutex_unlock(&f->worker_lock);
133}
134
135static inline void fuse_inc_avail(struct fuse *f)
136{
137 pthread_mutex_lock(&f->worker_lock);
138 f->numavail ++;
139 pthread_mutex_unlock(&f->worker_lock);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000140}
141
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000142static struct node *get_node_nocheck(struct fuse *f, nodeid_t nodeid)
Miklos Szeredia181e612001-11-06 12:03:23 +0000143{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000144 size_t hash = nodeid % f->id_table_size;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000145 struct node *node;
146
Miklos Szeredia13d9002004-11-02 17:32:03 +0000147 for (node = f->id_table[hash]; node != NULL; node = node->id_next)
148 if (node->nodeid == nodeid)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000149 return node;
Miklos Szeredie5183742005-02-02 11:14:04 +0000150
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000151 return NULL;
Miklos Szeredia181e612001-11-06 12:03:23 +0000152}
153
Miklos Szeredia13d9002004-11-02 17:32:03 +0000154static struct node *get_node(struct fuse *f, nodeid_t nodeid)
Miklos Szeredia181e612001-11-06 12:03:23 +0000155{
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000156 struct node *node = get_node_nocheck(f, nodeid);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000157 if (!node) {
158 fprintf(stderr, "fuse internal error: node %lu not found\n",
159 nodeid);
160 abort();
161 }
162 return node;
Miklos Szeredia181e612001-11-06 12:03:23 +0000163}
164
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000165static void free_node(struct node *node)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000166{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000167 free(node->name);
168 free(node);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000169}
170
Miklos Szeredia13d9002004-11-02 17:32:03 +0000171static void unhash_id(struct fuse *f, struct node *node)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000172{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000173 size_t hash = node->nodeid % f->id_table_size;
174 struct node **nodep = &f->id_table[hash];
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000175
Miklos Szeredie5183742005-02-02 11:14:04 +0000176 for (; *nodep != NULL; nodep = &(*nodep)->id_next)
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000177 if (*nodep == node) {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000178 *nodep = node->id_next;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000179 return;
180 }
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000181}
182
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000183static void hash_id(struct fuse *f, struct node *node)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000184{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000185 size_t hash = node->nodeid % f->id_table_size;
186 node->id_next = f->id_table[hash];
Miklos Szeredie5183742005-02-02 11:14:04 +0000187 f->id_table[hash] = node;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000188}
189
Miklos Szeredia13d9002004-11-02 17:32:03 +0000190static unsigned int name_hash(struct fuse *f, nodeid_t parent, const char *name)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000191{
192 unsigned int hash = *name;
193
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000194 if (hash)
195 for (name += 1; *name != '\0'; name++)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000196 hash = (hash << 5) - hash + *name;
197
198 return (hash + parent) % f->name_table_size;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000199}
200
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000201static void unref_node(struct fuse *f, struct node *node);
202
203static void unhash_name(struct fuse *f, struct node *node)
204{
205 if (node->name) {
206 size_t hash = name_hash(f, node->parent, node->name);
207 struct node **nodep = &f->name_table[hash];
Miklos Szeredie5183742005-02-02 11:14:04 +0000208
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000209 for (; *nodep != NULL; nodep = &(*nodep)->name_next)
210 if (*nodep == node) {
211 *nodep = node->name_next;
212 node->name_next = NULL;
213 unref_node(f, get_node(f, node->parent));
214 free(node->name);
215 node->name = NULL;
216 node->parent = 0;
217 return;
218 }
219 fprintf(stderr, "fuse internal error: unable to unhash node: %lu\n",
220 node->nodeid);
221 abort();
222 }
223}
224
225static int hash_name(struct fuse *f, struct node *node, nodeid_t parent,
226 const char *name)
227{
228 size_t hash = name_hash(f, parent, name);
229 node->name = strdup(name);
230 if (node->name == NULL)
231 return -1;
232
233 get_node(f, parent)->refctr ++;
234 node->parent = parent;
235 node->name_next = f->name_table[hash];
236 f->name_table[hash] = node;
237 return 0;
238}
239
240static void delete_node(struct fuse *f, struct node *node)
241{
242 assert(!node->name);
243 unhash_id(f, node);
244 free_node(node);
245}
246
247static void unref_node(struct fuse *f, struct node *node)
248{
249 assert(node->refctr > 0);
250 node->refctr --;
251 if (!node->refctr)
252 delete_node(f, node);
253}
254
255static nodeid_t next_id(struct fuse *f)
256{
257 do {
258 f->ctr++;
259 if (!f->ctr)
260 f->generation ++;
261 } while (f->ctr == 0 || get_node_nocheck(f, f->ctr) != NULL);
262 return f->ctr;
263}
264
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000265static struct node *lookup_node(struct fuse *f, nodeid_t parent,
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000266 const char *name)
267{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000268 size_t hash = name_hash(f, parent, name);
269 struct node *node;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000270
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000271 for (node = f->name_table[hash]; node != NULL; node = node->name_next)
272 if (node->parent == parent && strcmp(node->name, name) == 0)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000273 return node;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000274
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000275 return NULL;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000276}
277
Miklos Szeredia13d9002004-11-02 17:32:03 +0000278static struct node *find_node(struct fuse *f, nodeid_t parent, char *name,
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000279 struct fuse_attr *attr, uint64_t version)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000280{
281 struct node *node;
Miklos Szeredia181e612001-11-06 12:03:23 +0000282 int mode = attr->mode & S_IFMT;
283 int rdev = 0;
Miklos Szeredie5183742005-02-02 11:14:04 +0000284
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000285 if (S_ISCHR(mode) || S_ISBLK(mode))
Miklos Szeredia181e612001-11-06 12:03:23 +0000286 rdev = attr->rdev;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000287
Miklos Szeredia181e612001-11-06 12:03:23 +0000288 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000289 node = lookup_node(f, parent, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000290 if (node != NULL) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000291 if (!(f->flags & FUSE_USE_INO))
Miklos Szeredie5183742005-02-02 11:14:04 +0000292 attr->ino = node->nodeid;
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000293 } else {
294 node = (struct node *) calloc(1, sizeof(struct node));
295 if (node == NULL)
296 goto out_err;
Miklos Szeredie5183742005-02-02 11:14:04 +0000297
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000298 node->refctr = 1;
299 node->nodeid = next_id(f);
300 if (!(f->flags & FUSE_USE_INO))
301 attr->ino = node->nodeid;
302 node->open_count = 0;
303 node->is_hidden = 0;
304 node->generation = f->generation;
305 if (hash_name(f, node, parent, name) == -1) {
306 free(node);
307 node = NULL;
308 goto out_err;
309 }
310 hash_id(f, node);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000311 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000312 node->version = version;
Miklos Szeredic2309912004-09-21 13:40:38 +0000313 out_err:
Miklos Szeredia181e612001-11-06 12:03:23 +0000314 pthread_mutex_unlock(&f->lock);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000315 return node;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000316}
317
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000318static char *add_name(char *buf, char *s, const char *name)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000319{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000320 size_t len = strlen(name);
321 s -= len;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000322 if (s <= buf) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000323 fprintf(stderr, "fuse: path too long: ...%s\n", s + len);
324 return NULL;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000325 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000326 strncpy(s, name, len);
327 s--;
328 *s = '/';
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000329
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000330 return s;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000331}
332
Miklos Szeredia13d9002004-11-02 17:32:03 +0000333static char *get_path_name(struct fuse *f, nodeid_t nodeid, const char *name)
Miklos Szeredib483c932001-10-29 14:57:57 +0000334{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000335 char buf[FUSE_MAX_PATH];
336 char *s = buf + FUSE_MAX_PATH - 1;
337 struct node *node;
Miklos Szeredie5183742005-02-02 11:14:04 +0000338
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000339 *s = '\0';
Miklos Szeredia181e612001-11-06 12:03:23 +0000340
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000341 if (name != NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000342 s = add_name(buf, s, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000343 if (s == NULL)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000344 return NULL;
345 }
346
347 pthread_mutex_lock(&f->lock);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000348 for (node = get_node(f, nodeid); node && node->nodeid != FUSE_ROOT_ID;
349 node = get_node(f, node->parent)) {
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000350 if (node->name == NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000351 s = NULL;
352 break;
353 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000354
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000355 s = add_name(buf, s, node->name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000356 if (s == NULL)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000357 break;
358 }
359 pthread_mutex_unlock(&f->lock);
360
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000361 if (node == NULL || s == NULL)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000362 return NULL;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000363 else if (*s == '\0')
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000364 return strdup("/");
365 else
366 return strdup(s);
367}
Miklos Szeredia181e612001-11-06 12:03:23 +0000368
Miklos Szeredia13d9002004-11-02 17:32:03 +0000369static char *get_path(struct fuse *f, nodeid_t nodeid)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000370{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000371 return get_path_name(f, nodeid, NULL);
Miklos Szeredib483c932001-10-29 14:57:57 +0000372}
373
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000374static void forget_node(struct fuse *f, nodeid_t nodeid, uint64_t version)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000375{
Miklos Szeredia181e612001-11-06 12:03:23 +0000376 struct node *node;
377
378 pthread_mutex_lock(&f->lock);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000379 node = get_node(f, nodeid);
380 if (node->version == version && nodeid != FUSE_ROOT_ID) {
381 node->version = 0;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000382 unhash_name(f, node);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000383 unref_node(f, node);
Miklos Szeredia181e612001-11-06 12:03:23 +0000384 }
385 pthread_mutex_unlock(&f->lock);
386
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000387}
388
Miklos Szeredia13d9002004-11-02 17:32:03 +0000389static void remove_node(struct fuse *f, nodeid_t dir, const char *name)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000390{
Miklos Szeredia181e612001-11-06 12:03:23 +0000391 struct node *node;
392
393 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000394 node = lookup_node(f, dir, name);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000395 if (node != NULL)
396 unhash_name(f, node);
Miklos Szeredia181e612001-11-06 12:03:23 +0000397 pthread_mutex_unlock(&f->lock);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000398}
399
Miklos Szeredia13d9002004-11-02 17:32:03 +0000400static int rename_node(struct fuse *f, nodeid_t olddir, const char *oldname,
401 nodeid_t newdir, const char *newname, int hide)
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000402{
Miklos Szeredia181e612001-11-06 12:03:23 +0000403 struct node *node;
404 struct node *newnode;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000405 int err = 0;
Miklos Szeredie5183742005-02-02 11:14:04 +0000406
Miklos Szeredia181e612001-11-06 12:03:23 +0000407 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000408 node = lookup_node(f, olddir, oldname);
409 newnode = lookup_node(f, newdir, newname);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000410 if (node == NULL)
411 goto out;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000412
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000413 if (newnode != NULL) {
414 if (hide) {
415 fprintf(stderr, "fuse: hidden file got created during hiding\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000416 err = -EBUSY;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000417 goto out;
418 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000419 unhash_name(f, newnode);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000420 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000421
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000422 unhash_name(f, node);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000423 if (hash_name(f, node, newdir, newname) == -1) {
424 err = -ENOMEM;
425 goto out;
426 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000427
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000428 if (hide)
429 node->is_hidden = 1;
430
431 out:
Miklos Szeredia181e612001-11-06 12:03:23 +0000432 pthread_mutex_unlock(&f->lock);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000433 return err;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000434}
435
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000436static void convert_stat(struct stat *stbuf, struct fuse_attr *attr)
437{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000438 attr->ino = stbuf->st_ino;
Miklos Szeredib5958612004-02-20 14:10:49 +0000439 attr->mode = stbuf->st_mode;
440 attr->nlink = stbuf->st_nlink;
441 attr->uid = stbuf->st_uid;
442 attr->gid = stbuf->st_gid;
443 attr->rdev = stbuf->st_rdev;
444 attr->size = stbuf->st_size;
445 attr->blocks = stbuf->st_blocks;
446 attr->atime = stbuf->st_atime;
Miklos Szeredib5958612004-02-20 14:10:49 +0000447 attr->mtime = stbuf->st_mtime;
Miklos Szeredib5958612004-02-20 14:10:49 +0000448 attr->ctime = stbuf->st_ctime;
Miklos Szeredicb264512004-06-23 18:52:50 +0000449#ifdef HAVE_STRUCT_STAT_ST_ATIM
450 attr->atimensec = stbuf->st_atim.tv_nsec;
451 attr->mtimensec = stbuf->st_mtim.tv_nsec;
Miklos Szeredib5958612004-02-20 14:10:49 +0000452 attr->ctimensec = stbuf->st_ctim.tv_nsec;
Miklos Szeredicb264512004-06-23 18:52:50 +0000453#endif
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000454}
455
Miklos Szerediede1f7a2005-04-01 21:08:57 +0000456static int fill_dir5(void *buf, const char *name, int type, ino_t ino,
457 off_t off)
458{
459 size_t namelen = strlen(name);
460 size_t size;
461 struct fuse_dirbuf *db = (struct fuse_dirbuf *) buf;
462 struct fuse_dirent *dirent;
463
464 if (db->len <= FUSE_NAME_OFFSET)
465 return 1;
466
467 dirent = (struct fuse_dirent *) db->buf;
468 if ((db->fuse->flags & FUSE_USE_INO))
469 dirent->ino = ino;
470 else
471 dirent->ino = (unsigned long) -1;
472 dirent->off = off;
473 dirent->namelen = namelen;
474 dirent->type = type;
475 size = FUSE_DIRENT_SIZE(dirent);
476 if (db->len < size)
477 return 1;
478
479 strncpy(dirent->name, name, namelen);
480 db->len -= size;
481 db->buf += size;
482 return db->len > FUSE_NAME_OFFSET ? 0 : 1;
483}
484
Miklos Szeredi30e093a2005-04-03 17:44:54 +0000485static int fill_dir_compat5(struct fuse_dirhandle *dh, const char *name,
486 int type, ino_t ino)
487{
488 size_t namelen = strlen(name);
489 size_t entsize = sizeof(struct fuse_dirhandle) + namelen + 8;
490 struct fuse_dirent_compat5 *dirent;
491
492 if (namelen > FUSE_NAME_MAX)
493 namelen = FUSE_NAME_MAX;
494
495 dh->contents = realloc(dh->contents, dh->len + entsize);
496 if (dh->contents == NULL)
497 return -ENOMEM;
498
499 dirent = (struct fuse_dirent_compat5 *) (dh->contents + dh->len);
500 memset(dirent, 0, entsize);
501 if ((dh->fuse->flags & FUSE_USE_INO))
502 dirent->ino = ino;
503 else
504 dirent->ino = (unsigned long) -1;
505 dirent->namelen = namelen;
506 strncpy(dirent->name, name, namelen);
507 dirent->type = type;
508 dh->len += FUSE_DIRENT_SIZE_COMPAT5(dirent);
509 return 0;
510}
511
512static int fill_dir_new(struct fuse_dirhandle *dh, const char *name, int type,
513 ino_t ino)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000514{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000515 size_t namelen = strlen(name);
Miklos Szeredib92d9782005-02-07 16:10:49 +0000516 size_t entsize = sizeof(struct fuse_dirhandle) + namelen + 8;
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000517 struct fuse_dirent *dirent;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000518
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000519 if (namelen > FUSE_NAME_MAX)
520 namelen = FUSE_NAME_MAX;
521
Miklos Szeredib92d9782005-02-07 16:10:49 +0000522 dh->contents = realloc(dh->contents, dh->len + entsize);
523 if (dh->contents == NULL)
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000524 return -ENOMEM;
525
Miklos Szeredib92d9782005-02-07 16:10:49 +0000526 dirent = (struct fuse_dirent *) (dh->contents + dh->len);
527 memset(dirent, 0, entsize);
Miklos Szeredi8fb48fe2004-11-08 14:48:52 +0000528 if ((dh->fuse->flags & FUSE_USE_INO))
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000529 dirent->ino = ino;
Miklos Szeredi8fb48fe2004-11-08 14:48:52 +0000530 else
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000531 dirent->ino = (unsigned long) -1;
532 dirent->namelen = namelen;
533 strncpy(dirent->name, name, namelen);
534 dirent->type = type;
Miklos Szeredib92d9782005-02-07 16:10:49 +0000535 dh->len += FUSE_DIRENT_SIZE(dirent);
Miklos Szerediede1f7a2005-04-01 21:08:57 +0000536 dirent->off = dh->len;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000537 return 0;
538}
539
Miklos Szeredi30e093a2005-04-03 17:44:54 +0000540static int fill_dir(struct fuse_dirhandle *dh, const char *name, int type,
541 ino_t ino)
542{
543 if (dh->fuse->major == 5)
544 return fill_dir_compat5(dh, name, type, ino);
545 else
546 return fill_dir_new(dh, name, type, ino);
547}
548
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000549static int send_reply_raw(struct fuse *f, char *outbuf, size_t outsize)
Miklos Szeredi43696432001-11-18 19:15:05 +0000550{
551 int res;
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000552 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
553 out->len = outsize;
Miklos Szeredi43696432001-11-18 19:15:05 +0000554
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000555 if ((f->flags & FUSE_DEBUG)) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000556 printf(" unique: %llu, error: %i (%s), outsize: %i\n",
557 out->unique, out->error, strerror(-out->error), outsize);
Miklos Szeredi43696432001-11-18 19:15:05 +0000558 fflush(stdout);
559 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000560
Miklos Szeredi4e71c9f2004-02-09 12:05:14 +0000561 /* This needs to be done before the reply, otherwise the scheduler
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000562 could play tricks with us, and only let the counter be
563 increased long after the operation is done */
564 fuse_inc_avail(f);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000565
Miklos Szeredi43696432001-11-18 19:15:05 +0000566 res = write(f->fd, outbuf, outsize);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000567 if (res == -1) {
Miklos Szeredi43696432001-11-18 19:15:05 +0000568 /* ENOENT means the operation was interrupted */
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000569 if (!f->exited && errno != ENOENT)
Miklos Szeredi96249982001-11-21 12:21:19 +0000570 perror("fuse: writing device");
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000571 return -errno;
Miklos Szeredi43696432001-11-18 19:15:05 +0000572 }
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000573 return 0;
Miklos Szeredi43696432001-11-18 19:15:05 +0000574}
575
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000576static int send_reply(struct fuse *f, struct fuse_in_header *in, int error,
577 void *arg, size_t argsize)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000578{
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000579 int res;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000580 char *outbuf;
581 size_t outsize;
582 struct fuse_out_header *out;
583
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000584 if (error <= -1000 || error > 0) {
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000585 fprintf(stderr, "fuse: bad error value: %i\n", error);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000586 error = -ERANGE;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000587 }
588
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000589 if (error)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000590 argsize = 0;
591
592 outsize = sizeof(struct fuse_out_header) + argsize;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000593 outbuf = (char *) malloc(outsize);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000594 if (outbuf == NULL) {
595 fprintf(stderr, "fuse: failed to allocate reply buffer\n");
596 res = -ENOMEM;
597 } else {
598 out = (struct fuse_out_header *) outbuf;
599 memset(out, 0, sizeof(struct fuse_out_header));
600 out->unique = in->unique;
601 out->error = error;
602 if (argsize != 0)
603 memcpy(outbuf + sizeof(struct fuse_out_header), arg, argsize);
Miklos Szeredie5183742005-02-02 11:14:04 +0000604
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000605 res = send_reply_raw(f, outbuf, outsize);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000606 free(outbuf);
607 }
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000608
609 return res;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000610}
611
Miklos Szeredia13d9002004-11-02 17:32:03 +0000612static int is_open(struct fuse *f, nodeid_t dir, const char *name)
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000613{
614 struct node *node;
615 int isopen = 0;
616 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000617 node = lookup_node(f, dir, name);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000618 if (node && node->open_count > 0)
619 isopen = 1;
620 pthread_mutex_unlock(&f->lock);
621 return isopen;
622}
623
Miklos Szeredia13d9002004-11-02 17:32:03 +0000624static char *hidden_name(struct fuse *f, nodeid_t dir, const char *oldname,
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000625 char *newname, size_t bufsize)
626{
627 struct stat buf;
628 struct node *node;
629 struct node *newnode;
630 char *newpath;
631 int res;
632 int failctr = 10;
633
634 if (!f->op.getattr)
635 return NULL;
636
637 do {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000638 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000639 node = lookup_node(f, dir, oldname);
640 if (node == NULL) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000641 pthread_mutex_unlock(&f->lock);
642 return NULL;
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000643 }
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000644 do {
645 f->hidectr ++;
646 snprintf(newname, bufsize, ".fuse_hidden%08x%08x",
Miklos Szeredia13d9002004-11-02 17:32:03 +0000647 (unsigned int) node->nodeid, f->hidectr);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000648 newnode = lookup_node(f, dir, newname);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000649 } while(newnode);
650 pthread_mutex_unlock(&f->lock);
Miklos Szeredie5183742005-02-02 11:14:04 +0000651
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000652 newpath = get_path_name(f, dir, newname);
653 if (!newpath)
654 break;
Miklos Szeredie5183742005-02-02 11:14:04 +0000655
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000656 res = f->op.getattr(newpath, &buf);
657 if (res != 0)
658 break;
659 free(newpath);
660 newpath = NULL;
661 } while(--failctr);
662
663 return newpath;
664}
665
Miklos Szeredia13d9002004-11-02 17:32:03 +0000666static int hide_node(struct fuse *f, const char *oldpath, nodeid_t dir,
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000667 const char *oldname)
668{
669 char newname[64];
670 char *newpath;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000671 int err = -EBUSY;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000672
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000673 if (f->op.rename && f->op.unlink) {
674 newpath = hidden_name(f, dir, oldname, newname, sizeof(newname));
675 if (newpath) {
676 int res = f->op.rename(oldpath, newpath);
677 if (res == 0)
678 err = rename_node(f, dir, oldname, dir, newname, 1);
679 free(newpath);
680 }
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000681 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000682 return err;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000683}
684
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000685static int lookup_path(struct fuse *f, nodeid_t nodeid, uint64_t version,
686 char *name, const char *path,
687 struct fuse_entry_out *arg)
Miklos Szeredi76f65782004-02-19 16:55:40 +0000688{
689 int res;
690 struct stat buf;
691
692 res = f->op.getattr(path, &buf);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000693 if (res == 0) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000694 struct node *node;
695
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000696 memset(arg, 0, sizeof(struct fuse_entry_out));
Miklos Szeredi76f65782004-02-19 16:55:40 +0000697 convert_stat(&buf, &arg->attr);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000698 node = find_node(f, nodeid, name, &arg->attr, version);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000699 if (node == NULL)
700 res = -ENOMEM;
701 else {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000702 arg->nodeid = node->nodeid;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000703 arg->generation = node->generation;
704 arg->entry_valid = ENTRY_REVALIDATE_TIME;
705 arg->entry_valid_nsec = 0;
706 arg->attr_valid = ATTR_REVALIDATE_TIME;
707 arg->attr_valid_nsec = 0;
708 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000709 printf(" NODEID: %lu\n", (unsigned long) arg->nodeid);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000710 fflush(stdout);
711 }
Miklos Szeredi76f65782004-02-19 16:55:40 +0000712 }
713 }
714 return res;
715}
716
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000717static void do_lookup(struct fuse *f, struct fuse_in_header *in, char *name)
718{
719 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000720 int res2;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000721 char *path;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000722 struct fuse_entry_out arg;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000723
Miklos Szeredi5e183482001-10-31 14:52:35 +0000724 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000725 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000726 if (path != NULL) {
727 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi6ebe2342002-01-06 21:44:16 +0000728 printf("LOOKUP %s\n", path);
729 fflush(stdout);
730 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000731 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000732 if (f->op.getattr)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000733 res = lookup_path(f, in->nodeid, in->unique, name, path, &arg);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000734 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000735 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000736 res2 = send_reply(f, in, res, &arg, sizeof(arg));
737 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000738 forget_node(f, arg.nodeid, in->unique);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000739}
740
Miklos Szeredia181e612001-11-06 12:03:23 +0000741static void do_forget(struct fuse *f, struct fuse_in_header *in,
742 struct fuse_forget_in *arg)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000743{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000744 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000745 printf("FORGET %lu/%llu\n", (unsigned long) in->nodeid,
746 arg->version);
Miklos Szeredi43696432001-11-18 19:15:05 +0000747 fflush(stdout);
748 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000749 forget_node(f, in->nodeid, arg->version);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000750}
751
752static void do_getattr(struct fuse *f, struct fuse_in_header *in)
753{
754 int res;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000755 char *path;
756 struct stat buf;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000757 struct fuse_attr_out arg;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000758
Miklos Szeredi5e183482001-10-31 14:52:35 +0000759 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000760 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000761 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +0000762 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000763 if (f->op.getattr)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000764 res = f->op.getattr(path, &buf);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000765 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000766 }
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000767
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000768 if (res == 0) {
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000769 memset(&arg, 0, sizeof(struct fuse_attr_out));
770 arg.attr_valid = ATTR_REVALIDATE_TIME;
771 arg.attr_valid_nsec = 0;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000772 convert_stat(&buf, &arg.attr);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000773 if (!(f->flags & FUSE_USE_INO))
774 arg.attr.ino = in->nodeid;
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000775 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000776
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000777 send_reply(f, in, res, &arg, sizeof(arg));
778}
779
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000780static int do_chmod(struct fuse *f, const char *path, struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000781{
782 int res;
783
784 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000785 if (f->op.chmod)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000786 res = f->op.chmod(path, attr->mode);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000787
788 return res;
Miklos Szeredie5183742005-02-02 11:14:04 +0000789}
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000790
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000791static int do_chown(struct fuse *f, const char *path, struct fuse_attr *attr,
Miklos Szeredi2e50d432001-12-20 12:17:25 +0000792 int valid)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000793{
794 int res;
795 uid_t uid = (valid & FATTR_UID) ? attr->uid : (uid_t) -1;
796 gid_t gid = (valid & FATTR_GID) ? attr->gid : (gid_t) -1;
Miklos Szeredie5183742005-02-02 11:14:04 +0000797
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000798 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000799 if (f->op.chown)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000800 res = f->op.chown(path, uid, gid);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000801
802 return res;
803}
804
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000805static int do_truncate(struct fuse *f, const char *path,
806 struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000807{
808 int res;
809
810 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000811 if (f->op.truncate)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000812 res = f->op.truncate(path, attr->size);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000813
814 return res;
815}
816
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000817static int do_utime(struct fuse *f, const char *path, struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000818{
819 int res;
820 struct utimbuf buf;
821 buf.actime = attr->atime;
822 buf.modtime = attr->mtime;
823 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000824 if (f->op.utime)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000825 res = f->op.utime(path, &buf);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000826
827 return res;
828}
829
Miklos Szeredi5e183482001-10-31 14:52:35 +0000830static void do_setattr(struct fuse *f, struct fuse_in_header *in,
831 struct fuse_setattr_in *arg)
832{
833 int res;
834 char *path;
835 int valid = arg->valid;
Miklos Szeredi30e093a2005-04-03 17:44:54 +0000836 struct fuse_attr *attr = MEMBER_COMPAT(f, arg, attr, fuse_setattr_in);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000837 struct fuse_attr_out outarg;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000838
839 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000840 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000841 if (path != NULL) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000842 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000843 if (f->op.getattr) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000844 res = 0;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000845 if (!res && (valid & FATTR_MODE))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000846 res = do_chmod(f, path, attr);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000847 if (!res && (valid & (FATTR_UID | FATTR_GID)))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000848 res = do_chown(f, path, attr, valid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000849 if (!res && (valid & FATTR_SIZE))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000850 res = do_truncate(f, path, attr);
Miklos Szeredie5183742005-02-02 11:14:04 +0000851 if (!res && (valid & (FATTR_ATIME | FATTR_MTIME)) ==
Miklos Szeredib5958612004-02-20 14:10:49 +0000852 (FATTR_ATIME | FATTR_MTIME))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000853 res = do_utime(f, path, attr);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000854 if (!res) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000855 struct stat buf;
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000856 res = f->op.getattr(path, &buf);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000857 if (!res) {
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000858 memset(&outarg, 0, sizeof(struct fuse_attr_out));
859 outarg.attr_valid = ATTR_REVALIDATE_TIME;
860 outarg.attr_valid_nsec = 0;
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000861 convert_stat(&buf, &outarg.attr);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000862 if (!(f->flags & FUSE_USE_INO))
863 outarg.attr.ino = in->nodeid;
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000864 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000865 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000866 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000867 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000868 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000869 send_reply(f, in, res, &outarg, sizeof(outarg));
Miklos Szeredi5e183482001-10-31 14:52:35 +0000870}
871
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000872static void do_readlink(struct fuse *f, struct fuse_in_header *in)
873{
874 int res;
875 char link[PATH_MAX + 1];
Miklos Szeredib483c932001-10-29 14:57:57 +0000876 char *path;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000877
Miklos Szeredi5e183482001-10-31 14:52:35 +0000878 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000879 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000880 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +0000881 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000882 if (f->op.readlink)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000883 res = f->op.readlink(path, link, sizeof(link));
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000884 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000885 }
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000886 link[PATH_MAX] = '\0';
Miklos Szeredi4b2bef42002-01-09 12:23:27 +0000887 send_reply(f, in, res, link, res == 0 ? strlen(link) : 0);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000888}
889
Miklos Szeredi3ead28e2005-01-18 21:23:41 +0000890static int common_getdir(struct fuse *f, struct fuse_in_header *in,
891 struct fuse_dirhandle *dh)
892{
893 int res;
894 char *path;
895
896 res = -ENOENT;
897 path = get_path(f, in->nodeid);
898 if (path != NULL) {
899 res = -ENOSYS;
900 if (f->op.getdir)
901 res = f->op.getdir(path, dh, fill_dir);
902 free(path);
903 }
904 return res;
905}
906
Miklos Szeredib483c932001-10-29 14:57:57 +0000907static void do_mknod(struct fuse *f, struct fuse_in_header *in,
908 struct fuse_mknod_in *inarg)
909{
910 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000911 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000912 char *path;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000913 char *name = PARAM(inarg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000914 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000915
Miklos Szeredi5e183482001-10-31 14:52:35 +0000916 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000917 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000918 if (path != NULL) {
919 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000920 printf("MKNOD %s\n", path);
921 fflush(stdout);
922 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000923 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000924 if (f->op.mknod && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000925 res = f->op.mknod(path, inarg->mode, inarg->rdev);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000926 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000927 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000928 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000929 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000930 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000931 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
932 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000933 forget_node(f, outarg.nodeid, in->unique);
Miklos Szeredib483c932001-10-29 14:57:57 +0000934}
935
936static void do_mkdir(struct fuse *f, struct fuse_in_header *in,
937 struct fuse_mkdir_in *inarg)
938{
939 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000940 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000941 char *path;
Miklos Szeredi30e093a2005-04-03 17:44:54 +0000942 char *name = PARAM_COMPAT(f, inarg, fuse_mkdir_in);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000943 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000944
Miklos Szeredi5e183482001-10-31 14:52:35 +0000945 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000946 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000947 if (path != NULL) {
948 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000949 printf("MKDIR %s\n", path);
950 fflush(stdout);
951 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000952 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000953 if (f->op.mkdir && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000954 res = f->op.mkdir(path, inarg->mode);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000955 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000956 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000957 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000958 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000959 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000960 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
961 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000962 forget_node(f, outarg.nodeid, in->unique);
Miklos Szeredib483c932001-10-29 14:57:57 +0000963}
964
Miklos Szeredib5958612004-02-20 14:10:49 +0000965static void do_unlink(struct fuse *f, struct fuse_in_header *in, char *name)
Miklos Szeredib483c932001-10-29 14:57:57 +0000966{
967 int res;
968 char *path;
969
Miklos Szeredi5e183482001-10-31 14:52:35 +0000970 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000971 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000972 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000973 if (f->flags & FUSE_DEBUG) {
974 printf("UNLINK %s\n", path);
975 fflush(stdout);
976 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000977 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000978 if (f->op.unlink) {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000979 if (!(f->flags & FUSE_HARD_REMOVE) && is_open(f, in->nodeid, name))
980 res = hide_node(f, path, in->nodeid, name);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000981 else {
982 res = f->op.unlink(path);
983 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +0000984 remove_node(f, in->nodeid, name);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000985
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000986 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000987 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000988 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000989 }
Miklos Szeredib5958612004-02-20 14:10:49 +0000990 send_reply(f, in, res, NULL, 0);
991}
992
993static void do_rmdir(struct fuse *f, struct fuse_in_header *in, char *name)
994{
995 int res;
996 char *path;
997
998 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000999 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001000 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001001 if (f->flags & FUSE_DEBUG) {
1002 printf("RMDIR %s\n", path);
1003 fflush(stdout);
1004 }
Miklos Szeredib5958612004-02-20 14:10:49 +00001005 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001006 if (f->op.rmdir) {
Miklos Szeredib5958612004-02-20 14:10:49 +00001007 res = f->op.rmdir(path);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001008 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +00001009 remove_node(f, in->nodeid, name);
Miklos Szeredib5958612004-02-20 14:10:49 +00001010 }
1011 free(path);
1012 }
Miklos Szeredib483c932001-10-29 14:57:57 +00001013 send_reply(f, in, res, NULL, 0);
1014}
1015
1016static void do_symlink(struct fuse *f, struct fuse_in_header *in, char *name,
1017 char *link)
1018{
1019 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +00001020 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +00001021 char *path;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +00001022 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +00001023
Miklos Szeredi5e183482001-10-31 14:52:35 +00001024 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001025 path = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001026 if (path != NULL) {
1027 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +00001028 printf("SYMLINK %s\n", path);
1029 fflush(stdout);
1030 }
Miklos Szeredi5e183482001-10-31 14:52:35 +00001031 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001032 if (f->op.symlink && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +00001033 res = f->op.symlink(link, path);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001034 if (res == 0)
Miklos Szeredia13d9002004-11-02 17:32:03 +00001035 res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg);
Miklos Szeredi76f65782004-02-19 16:55:40 +00001036 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001037 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001038 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +00001039 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
1040 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001041 forget_node(f, outarg.nodeid, in->unique);
Miklos Szeredi2778f6c2004-06-21 09:45:30 +00001042
Miklos Szeredib483c932001-10-29 14:57:57 +00001043}
1044
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001045static void do_rename(struct fuse *f, struct fuse_in_header *in,
1046 struct fuse_rename_in *inarg)
1047{
1048 int res;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001049 nodeid_t olddir = in->nodeid;
1050 nodeid_t newdir = inarg->newdir;
Miklos Szeredi6bf8b682002-10-28 08:49:39 +00001051 char *oldname = PARAM(inarg);
1052 char *newname = oldname + strlen(oldname) + 1;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001053 char *oldpath;
1054 char *newpath;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001055
Miklos Szeredi5e183482001-10-31 14:52:35 +00001056 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +00001057 oldpath = get_path_name(f, olddir, oldname);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001058 if (oldpath != NULL) {
Miklos Szeredia181e612001-11-06 12:03:23 +00001059 newpath = get_path_name(f, newdir, newname);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001060 if (newpath != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001061 if (f->flags & FUSE_DEBUG) {
1062 printf("RENAME %s -> %s\n", oldpath, newpath);
1063 fflush(stdout);
1064 }
Miklos Szeredi5e183482001-10-31 14:52:35 +00001065 res = -ENOSYS;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001066 if (f->op.rename) {
1067 res = 0;
Miklos Szeredie5183742005-02-02 11:14:04 +00001068 if (!(f->flags & FUSE_HARD_REMOVE) &&
Miklos Szeredi2529ca22004-07-13 15:36:52 +00001069 is_open(f, newdir, newname))
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001070 res = hide_node(f, newpath, newdir, newname);
1071 if (res == 0) {
1072 res = f->op.rename(oldpath, newpath);
1073 if (res == 0)
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001074 res = rename_node(f, olddir, oldname, newdir, newname, 0);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001075 }
1076 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001077 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001078 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001079 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001080 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001081 send_reply(f, in, res, NULL, 0);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001082}
1083
1084static void do_link(struct fuse *f, struct fuse_in_header *in,
Miklos Szeredi5e183482001-10-31 14:52:35 +00001085 struct fuse_link_in *arg)
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001086{
1087 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +00001088 int res2;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001089 char *oldpath;
1090 char *newpath;
Miklos Szeredi76f65782004-02-19 16:55:40 +00001091 char *name = PARAM(arg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +00001092 struct fuse_entry_out outarg;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001093
Miklos Szeredi5e183482001-10-31 14:52:35 +00001094 res = -ENOENT;
Miklos Szeredied6b5dd2005-01-26 17:07:59 +00001095 oldpath = get_path(f, arg->oldnodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001096 if (oldpath != NULL) {
Miklos Szeredied6b5dd2005-01-26 17:07:59 +00001097 newpath = get_path_name(f, in->nodeid, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001098 if (newpath != NULL) {
1099 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +00001100 printf("LINK %s\n", newpath);
1101 fflush(stdout);
1102 }
Miklos Szeredi5e183482001-10-31 14:52:35 +00001103 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001104 if (f->op.link && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +00001105 res = f->op.link(oldpath, newpath);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001106 if (res == 0)
Miklos Szeredied6b5dd2005-01-26 17:07:59 +00001107 res = lookup_path(f, in->nodeid, in->unique, name,
Miklos Szeredi76f65782004-02-19 16:55:40 +00001108 newpath, &outarg);
1109 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001110 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001111 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001112 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001113 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +00001114 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
1115 if (res == 0 && res2 == -ENOENT)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001116 forget_node(f, outarg.nodeid, in->unique);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +00001117}
1118
Miklos Szeredi5e183482001-10-31 14:52:35 +00001119static void do_open(struct fuse *f, struct fuse_in_header *in,
1120 struct fuse_open_in *arg)
1121{
1122 int res;
1123 char *path;
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001124 struct fuse_open_out outarg;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001125 struct fuse_file_info fi;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001126
Miklos Szeredied3c97c2005-02-15 17:04:50 +00001127 memset(&outarg, 0, sizeof(outarg));
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001128 memset(&fi, 0, sizeof(fi));
1129 fi.flags = arg->flags;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001130 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001131 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001132 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +00001133 res = -ENOSYS;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001134 if (f->op.open) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001135 if (!f->compat)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001136 res = f->op.open(path, &fi);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001137 else
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001138 res = ((struct fuse_operations_compat2 *) &f->op)->open(path, fi.flags);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001139 }
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +00001140 }
Miklos Szeredi73798f92004-07-12 15:55:11 +00001141 if (res == 0) {
1142 int res2;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001143 outarg.fh = fi.fh;
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001144 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001145 printf("OPEN[%lu] flags: 0x%x\n", fi.fh, arg->flags);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001146 fflush(stdout);
1147 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001148
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001149 pthread_mutex_lock(&f->lock);
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001150 res2 = send_reply(f, in, res, &outarg, SIZEOF_COMPAT(f, fuse_open_out));
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001151 if(res2 == -ENOENT) {
1152 /* The open syscall was interrupted, so it must be cancelled */
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001153 if(f->op.release) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001154 if (!f->compat)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001155 f->op.release(path, &fi);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001156 else
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001157 ((struct fuse_operations_compat2 *) &f->op)->release(path, fi.flags);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001158 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001159 } else {
1160 struct node *node = get_node(f, in->nodeid);
1161 node->open_count ++;
1162 }
Miklos Szeredi73798f92004-07-12 15:55:11 +00001163 pthread_mutex_unlock(&f->lock);
Miklos Szeredi73798f92004-07-12 15:55:11 +00001164 } else
1165 send_reply(f, in, res, NULL, 0);
1166
Miklos Szeredi9a31cca2004-06-26 21:11:25 +00001167 if (path)
1168 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001169}
1170
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001171static void do_flush(struct fuse *f, struct fuse_in_header *in,
1172 struct fuse_flush_in *arg)
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001173{
1174 char *path;
1175 int res;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001176 struct fuse_file_info fi;
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001177
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001178 memset(&fi, 0, sizeof(fi));
1179 fi.fh = arg->fh;
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001180 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001181 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001182 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001183 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001184 printf("FLUSH[%lu]\n", (unsigned long) arg->fh);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001185 fflush(stdout);
1186 }
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001187 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001188 if (f->op.flush)
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001189 res = f->op.flush(path, &fi);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001190 free(path);
1191 }
1192 send_reply(f, in, res, NULL, 0);
1193}
1194
Miklos Szeredi9478e862002-12-11 09:50:26 +00001195static void do_release(struct fuse *f, struct fuse_in_header *in,
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001196 struct fuse_release_in *arg)
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001197{
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001198 struct node *node;
1199 char *path;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001200 struct fuse_file_info fi;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001201 int unlink_hidden;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001202
1203 memset(&fi, 0, sizeof(fi));
1204 fi.flags = arg->flags;
1205 fi.fh = arg->fh;
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001206
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001207 pthread_mutex_lock(&f->lock);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001208 node = get_node(f, in->nodeid);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001209 assert(node->open_count > 0);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001210 --node->open_count;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001211 unlink_hidden = (node->is_hidden && !node->open_count);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001212 pthread_mutex_unlock(&f->lock);
1213
Miklos Szeredia13d9002004-11-02 17:32:03 +00001214 path = get_path(f, in->nodeid);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001215 if (f->flags & FUSE_DEBUG) {
1216 printf("RELEASE[%lu] flags: 0x%x\n", fi.fh, fi.flags);
1217 fflush(stdout);
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001218 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001219 if (f->op.release) {
1220 if (!f->compat)
1221 f->op.release(path ? path : "-", &fi);
1222 else if (path)
1223 ((struct fuse_operations_compat2 *) &f->op)->release(path, fi.flags);
1224 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001225
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001226 if(unlink_hidden && path)
1227 f->op.unlink(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001228
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001229 if (path)
1230 free(path);
1231
Miklos Szeredi556d03d2004-06-30 11:13:41 +00001232 send_reply(f, in, 0, NULL, 0);
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001233}
1234
Miklos Szeredi5e183482001-10-31 14:52:35 +00001235static void do_read(struct fuse *f, struct fuse_in_header *in,
1236 struct fuse_read_in *arg)
1237{
1238 int res;
1239 char *path;
Miklos Szeredi43696432001-11-18 19:15:05 +00001240 char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + arg->size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001241 if (outbuf == NULL)
1242 send_reply(f, in, -ENOMEM, NULL, 0);
1243 else {
1244 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
1245 char *buf = outbuf + sizeof(struct fuse_out_header);
1246 size_t size;
1247 size_t outsize;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001248 struct fuse_file_info fi;
Miklos Szeredie5183742005-02-02 11:14:04 +00001249
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001250 memset(&fi, 0, sizeof(fi));
1251 fi.fh = arg->fh;
1252
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001253 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001254 path = get_path(f, in->nodeid);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001255 if (path != NULL) {
1256 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001257 printf("READ[%lu] %u bytes from %llu\n",
1258 (unsigned long) arg->fh, arg->size, arg->offset);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001259 fflush(stdout);
1260 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001261
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001262 res = -ENOSYS;
1263 if (f->op.read)
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001264 res = f->op.read(path, buf, arg->size, arg->offset, &fi);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001265 free(path);
Miklos Szeredi6ebe2342002-01-06 21:44:16 +00001266 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001267
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001268 size = 0;
1269 if (res >= 0) {
1270 size = res;
1271 res = 0;
1272 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001273 printf(" READ[%lu] %u bytes\n", (unsigned long) arg->fh,
1274 size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001275 fflush(stdout);
1276 }
Miklos Szeredi6ebe2342002-01-06 21:44:16 +00001277 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001278 memset(out, 0, sizeof(struct fuse_out_header));
1279 out->unique = in->unique;
1280 out->error = res;
1281 outsize = sizeof(struct fuse_out_header) + size;
Miklos Szeredie5183742005-02-02 11:14:04 +00001282
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001283 send_reply_raw(f, outbuf, outsize);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001284 free(outbuf);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001285 }
Miklos Szeredi5e183482001-10-31 14:52:35 +00001286}
Miklos Szeredib483c932001-10-29 14:57:57 +00001287
Miklos Szeredia181e612001-11-06 12:03:23 +00001288static void do_write(struct fuse *f, struct fuse_in_header *in,
1289 struct fuse_write_in *arg)
1290{
1291 int res;
1292 char *path;
Miklos Szerediad051c32004-07-02 09:22:50 +00001293 struct fuse_write_out outarg;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001294 struct fuse_file_info fi;
1295
1296 memset(&fi, 0, sizeof(fi));
1297 fi.fh = arg->fh;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001298 fi.writepage = arg->write_flags & 1;
Miklos Szeredia181e612001-11-06 12:03:23 +00001299
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001300 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001301 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001302 if (path != NULL) {
1303 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi1eea0322004-09-27 18:50:11 +00001304 printf("WRITE%s[%lu] %u bytes to %llu\n",
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001305 (arg->write_flags & 1) ? "PAGE" : "",
1306 (unsigned long) arg->fh, arg->size, arg->offset);
Miklos Szeredi6ebe2342002-01-06 21:44:16 +00001307 fflush(stdout);
1308 }
1309
Miklos Szeredia181e612001-11-06 12:03:23 +00001310 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001311 if (f->op.write)
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001312 res = f->op.write(path, PARAM(arg), arg->size, arg->offset, &fi);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001313 free(path);
Miklos Szeredia181e612001-11-06 12:03:23 +00001314 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001315
1316 if (res >= 0) {
Miklos Szerediad051c32004-07-02 09:22:50 +00001317 outarg.size = res;
1318 res = 0;
Miklos Szeredia181e612001-11-06 12:03:23 +00001319 }
1320
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001321 send_reply(f, in, res, &outarg, SIZEOF_COMPAT(f, fuse_write_out));
Miklos Szeredia181e612001-11-06 12:03:23 +00001322}
1323
Miklos Szeredi77f39942004-03-25 11:17:52 +00001324static int default_statfs(struct statfs *buf)
1325{
1326 buf->f_namelen = 255;
1327 buf->f_bsize = 512;
1328 return 0;
1329}
1330
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001331static void convert_statfs_compat(struct fuse_statfs_compat1 *compatbuf,
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001332 struct statfs *statfs)
1333{
1334 statfs->f_bsize = compatbuf->block_size;
1335 statfs->f_blocks = compatbuf->blocks;
1336 statfs->f_bfree = compatbuf->blocks_free;
1337 statfs->f_bavail = compatbuf->blocks_free;
1338 statfs->f_files = compatbuf->files;
1339 statfs->f_ffree = compatbuf->files_free;
1340 statfs->f_namelen = compatbuf->namelen;
1341}
1342
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001343static void convert_statfs(struct statfs *statfs, struct fuse_kstatfs *kstatfs)
1344{
1345 kstatfs->bsize = statfs->f_bsize;
1346 kstatfs->blocks = statfs->f_blocks;
1347 kstatfs->bfree = statfs->f_bfree;
1348 kstatfs->bavail = statfs->f_bavail;
1349 kstatfs->files = statfs->f_files;
1350 kstatfs->ffree = statfs->f_ffree;
1351 kstatfs->namelen = statfs->f_namelen;
1352}
1353
Mark Glinesd84b39a2002-01-07 16:32:02 +00001354static void do_statfs(struct fuse *f, struct fuse_in_header *in)
1355{
1356 int res;
Mark Glinesd84b39a2002-01-07 16:32:02 +00001357 struct fuse_statfs_out arg;
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001358 struct statfs buf;
Mark Glinesd84b39a2002-01-07 16:32:02 +00001359
Miklos Szeredi77f39942004-03-25 11:17:52 +00001360 memset(&buf, 0, sizeof(struct statfs));
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001361 if (f->op.statfs) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001362 if (!f->compat || f->compat > 11)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001363 res = f->op.statfs("/", &buf);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001364 else {
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001365 struct fuse_statfs_compat1 compatbuf;
1366 memset(&compatbuf, 0, sizeof(struct fuse_statfs_compat1));
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001367 res = ((struct fuse_operations_compat1 *) &f->op)->statfs(&compatbuf);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001368 if (res == 0)
1369 convert_statfs_compat(&compatbuf, &buf);
1370 }
1371 }
Miklos Szeredi77f39942004-03-25 11:17:52 +00001372 else
1373 res = default_statfs(&buf);
1374
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001375 if (res == 0)
Miklos Szeredi77f39942004-03-25 11:17:52 +00001376 convert_statfs(&buf, &arg.st);
Miklos Szeredi4b2bef42002-01-09 12:23:27 +00001377
Mark Glinesd84b39a2002-01-07 16:32:02 +00001378 send_reply(f, in, res, &arg, sizeof(arg));
1379}
1380
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001381static void do_fsync(struct fuse *f, struct fuse_in_header *in,
1382 struct fuse_fsync_in *inarg)
1383{
1384 int res;
1385 char *path;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001386 struct fuse_file_info fi;
1387
1388 memset(&fi, 0, sizeof(fi));
1389 fi.fh = inarg->fh;
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001390
1391 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001392 path = get_path(f, in->nodeid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001393 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001394 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001395 printf("FSYNC[%lu]\n", (unsigned long) inarg->fh);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001396 fflush(stdout);
1397 }
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001398 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001399 if (f->op.fsync)
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001400 res = f->op.fsync(path, inarg->fsync_flags & 1, &fi);
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001401 free(path);
1402 }
1403 send_reply(f, in, res, NULL, 0);
1404}
1405
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001406static void do_setxattr(struct fuse *f, struct fuse_in_header *in,
1407 struct fuse_setxattr_in *arg)
1408{
1409 int res;
1410 char *path;
1411 char *name = PARAM(arg);
1412 unsigned char *value = name + strlen(name) + 1;
1413
1414 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001415 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001416 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001417 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001418 if (f->op.setxattr)
1419 res = f->op.setxattr(path, name, value, arg->size, arg->flags);
1420 free(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001421 }
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001422 send_reply(f, in, res, NULL, 0);
1423}
1424
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001425static int common_getxattr(struct fuse *f, struct fuse_in_header *in,
1426 const char *name, char *value, size_t size)
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001427{
1428 int res;
1429 char *path;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001430
1431 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001432 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001433 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001434 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001435 if (f->op.getxattr)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001436 res = f->op.getxattr(path, name, value, size);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001437 free(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001438 }
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001439 return res;
1440}
1441
1442static void do_getxattr_read(struct fuse *f, struct fuse_in_header *in,
1443 const char *name, size_t size)
1444{
1445 int res;
1446 char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001447 if (outbuf == NULL)
1448 send_reply(f, in, -ENOMEM, NULL, 0);
1449 else {
1450 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
1451 char *value = outbuf + sizeof(struct fuse_out_header);
Miklos Szeredie5183742005-02-02 11:14:04 +00001452
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001453 res = common_getxattr(f, in, name, value, size);
1454 size = 0;
1455 if (res > 0) {
1456 size = res;
1457 res = 0;
1458 }
1459 memset(out, 0, sizeof(struct fuse_out_header));
1460 out->unique = in->unique;
1461 out->error = res;
Miklos Szeredie5183742005-02-02 11:14:04 +00001462
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001463 send_reply_raw(f, outbuf, sizeof(struct fuse_out_header) + size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001464 free(outbuf);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001465 }
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001466}
1467
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001468static void do_getxattr_size(struct fuse *f, struct fuse_in_header *in,
1469 const char *name)
1470{
1471 int res;
1472 struct fuse_getxattr_out arg;
1473
1474 res = common_getxattr(f, in, name, NULL, 0);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001475 if (res >= 0) {
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001476 arg.size = res;
1477 res = 0;
1478 }
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001479 send_reply(f, in, res, &arg, SIZEOF_COMPAT(f, fuse_getxattr_out));
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001480}
1481
1482static void do_getxattr(struct fuse *f, struct fuse_in_header *in,
1483 struct fuse_getxattr_in *arg)
1484{
1485 char *name = PARAM(arg);
Miklos Szeredie5183742005-02-02 11:14:04 +00001486
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001487 if (arg->size)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001488 do_getxattr_read(f, in, name, arg->size);
1489 else
1490 do_getxattr_size(f, in, name);
1491}
1492
1493static int common_listxattr(struct fuse *f, struct fuse_in_header *in,
1494 char *list, size_t size)
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001495{
1496 int res;
1497 char *path;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001498
1499 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001500 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001501 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001502 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001503 if (f->op.listxattr)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001504 res = f->op.listxattr(path, list, size);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001505 free(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001506 }
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001507 return res;
1508}
1509
1510static void do_listxattr_read(struct fuse *f, struct fuse_in_header *in,
1511 size_t size)
1512{
1513 int res;
1514 char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001515 if (outbuf == NULL)
1516 send_reply(f, in, -ENOMEM, NULL, 0);
1517 else {
1518 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
1519 char *list = outbuf + sizeof(struct fuse_out_header);
Miklos Szeredie5183742005-02-02 11:14:04 +00001520
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001521 res = common_listxattr(f, in, list, size);
1522 size = 0;
1523 if (res > 0) {
1524 size = res;
1525 res = 0;
1526 }
1527 memset(out, 0, sizeof(struct fuse_out_header));
1528 out->unique = in->unique;
1529 out->error = res;
Miklos Szeredie5183742005-02-02 11:14:04 +00001530
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001531 send_reply_raw(f, outbuf, sizeof(struct fuse_out_header) + size);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001532 free(outbuf);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001533 }
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001534}
1535
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001536static void do_listxattr_size(struct fuse *f, struct fuse_in_header *in)
1537{
1538 int res;
1539 struct fuse_getxattr_out arg;
1540
1541 res = common_listxattr(f, in, NULL, 0);
1542 if (res >= 0) {
1543 arg.size = res;
1544 res = 0;
1545 }
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001546 send_reply(f, in, res, &arg, SIZEOF_COMPAT(f, fuse_getxattr_out));
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001547}
1548
1549static void do_listxattr(struct fuse *f, struct fuse_in_header *in,
1550 struct fuse_getxattr_in *arg)
1551{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001552 if (arg->size)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001553 do_listxattr_read(f, in, arg->size);
1554 else
1555 do_listxattr_size(f, in);
1556}
1557
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001558static void do_removexattr(struct fuse *f, struct fuse_in_header *in,
1559 char *name)
1560{
1561 int res;
1562 char *path;
1563
1564 res = -ENOENT;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001565 path = get_path(f, in->nodeid);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001566 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001567 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001568 if (f->op.removexattr)
1569 res = f->op.removexattr(path, name);
1570 free(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001571 }
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001572 send_reply(f, in, res, NULL, 0);
1573}
1574
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001575static void do_init(struct fuse *f, struct fuse_in_header *in,
1576 struct fuse_init_in_out *arg)
1577{
1578 struct fuse_init_in_out outarg;
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001579
1580 if (in->padding == 5) {
1581 arg->minor = arg->major;
1582 arg->major = in->padding;
1583 }
1584
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001585 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001586 printf("INIT: %u.%u\n", arg->major, arg->minor);
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001587 fflush(stdout);
1588 }
1589 f->got_init = 1;
Miklos Szeredi159bd7e2005-02-28 17:32:16 +00001590 if (f->op.init)
1591 f->user_data = f->op.init();
1592
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001593 if (arg->major == 5) {
1594 f->major = 5;
1595 f->minor = 1;
1596 } else {
1597 f->major = FUSE_KERNEL_VERSION;
1598 f->minor = FUSE_KERNEL_MINOR_VERSION;
1599 }
1600 memset(&outarg, 0, sizeof(outarg));
1601 outarg.major = f->major;
1602 outarg.minor = f->minor;
1603
1604 if (f->flags & FUSE_DEBUG) {
1605 printf(" INIT: %u.%u\n", outarg.major, outarg.minor);
1606 fflush(stdout);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001607 }
1608
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001609 send_reply(f, in, 0, &outarg, sizeof(outarg));
1610}
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001611
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001612static struct fuse_dirhandle *get_dirhandle(unsigned long fh)
1613{
1614 return (struct fuse_dirhandle *) fh;
1615}
1616
1617static void do_opendir(struct fuse *f, struct fuse_in_header *in,
1618 struct fuse_open_in *arg)
1619{
1620 int res;
1621 struct fuse_open_out outarg;
1622 struct fuse_dirhandle *dh;
1623
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001624 dh = (struct fuse_dirhandle *) malloc(sizeof(struct fuse_dirhandle));
1625 if (dh == NULL) {
1626 send_reply(f, in, -ENOMEM, NULL, 0);
1627 return;
1628 }
1629 memset(dh, 0, sizeof(struct fuse_dirhandle));
1630 dh->fuse = f;
1631 dh->contents = NULL;
1632 dh->len = 0;
1633 dh->filled = 0;
1634
1635 memset(&outarg, 0, sizeof(outarg));
1636 outarg.fh = (unsigned long) dh;
1637
Miklos Szeredif43f0632005-02-28 11:46:56 +00001638 if (f->op.opendir) {
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001639 struct fuse_file_info fi;
Miklos Szeredif43f0632005-02-28 11:46:56 +00001640 char *path;
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001641
1642 memset(&fi, 0, sizeof(fi));
1643 fi.flags = arg->flags;
1644
Miklos Szeredif43f0632005-02-28 11:46:56 +00001645 res = -ENOENT;
1646 path = get_path(f, in->nodeid);
1647 if (path != NULL) {
Miklos Szeredif43f0632005-02-28 11:46:56 +00001648 res = f->op.opendir(path, &fi);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001649 dh->fh = fi.fh;
Miklos Szeredif43f0632005-02-28 11:46:56 +00001650 }
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001651 if (res == 0) {
1652 int res2;
1653 pthread_mutex_lock(&f->lock);
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001654 res2 = send_reply(f, in, res, &outarg, SIZEOF_COMPAT(f, fuse_open_out));
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001655 if(res2 == -ENOENT) {
1656 /* The opendir syscall was interrupted, so it must be
1657 cancelled */
1658 if(f->op.releasedir)
1659 f->op.releasedir(path, &fi);
1660 free(dh);
1661 }
1662 pthread_mutex_unlock(&f->lock);
1663 } else {
Miklos Szeredif43f0632005-02-28 11:46:56 +00001664 send_reply(f, in, res, NULL, 0);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001665 free(dh);
Miklos Szeredif43f0632005-02-28 11:46:56 +00001666 }
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001667 free(path);
1668 } else
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001669 send_reply(f, in, 0, &outarg, SIZEOF_COMPAT(f, fuse_open_out));
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001670}
1671
1672static void do_readdir(struct fuse *f, struct fuse_in_header *in,
1673 struct fuse_read_in *arg)
1674{
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001675 int res;
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001676 struct fuse_dirhandle *dh = get_dirhandle(arg->fh);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001677 struct fuse_out_header *out;
1678 char *outbuf;
1679 char *buf;
1680 size_t size = 0;
1681 size_t outsize;
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001682
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001683 outbuf = (char *) malloc(sizeof(struct fuse_out_header) + arg->size);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001684 if (outbuf == NULL) {
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001685 send_reply(f, in, -ENOMEM, NULL, 0);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001686 return;
1687 }
1688 buf = outbuf + sizeof(struct fuse_out_header);
1689
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001690 if (f->op.readdir && f->major != 5) {
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001691 struct fuse_dirbuf db;
1692 struct fuse_file_info fi;
1693 char *path;
1694
1695 db.fuse = f;
1696 db.buf = buf;
1697 db.len = arg->size;
1698
1699 memset(&fi, 0, sizeof(fi));
1700 fi.fh = dh->fh;
1701
1702 path = get_path(f, in->nodeid);
1703 res = f->op.readdir(path ? path : "-", &db, fill_dir5, arg->offset, &fi);
1704 free(path);
1705 if (res)
1706 goto err;
1707
1708 size = arg->size - db.len;
1709 } else {
1710 if (!dh->filled) {
1711 res = common_getdir(f, in, dh);
1712 if (res)
1713 goto err;
1714 dh->filled = 1;
1715 }
1716
Miklos Szeredib92d9782005-02-07 16:10:49 +00001717 if (arg->offset < dh->len) {
1718 size = arg->size;
1719 if (arg->offset + size > dh->len)
1720 size = dh->len - arg->offset;
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001721
Miklos Szeredib92d9782005-02-07 16:10:49 +00001722 memcpy(buf, dh->contents + arg->offset, size);
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001723 }
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001724 }
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001725 out = (struct fuse_out_header *) outbuf;
1726 memset(out, 0, sizeof(struct fuse_out_header));
1727 out->unique = in->unique;
1728 out->error = 0;
1729 outsize = sizeof(struct fuse_out_header) + size;
1730
1731 send_reply_raw(f, outbuf, outsize);
1732 free(outbuf);
1733 return;
1734
1735 err:
1736 send_reply(f, in, res, NULL, 0);
1737 free(outbuf);
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001738}
1739
1740static void do_releasedir(struct fuse *f, struct fuse_in_header *in,
1741 struct fuse_release_in *arg)
1742{
1743 struct fuse_dirhandle *dh = get_dirhandle(arg->fh);
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001744 if (f->op.releasedir) {
1745 char *path;
1746 struct fuse_file_info fi;
1747
1748 memset(&fi, 0, sizeof(fi));
1749 fi.fh = dh->fh;
1750
1751 path = get_path(f, in->nodeid);
1752 f->op.releasedir(path ? path : "-", &fi);
1753 free(path);
1754 }
Miklos Szeredib92d9782005-02-07 16:10:49 +00001755 free(dh->contents);
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001756 free(dh);
1757 send_reply(f, in, 0, NULL, 0);
1758}
1759
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001760static void do_fsyncdir(struct fuse *f, struct fuse_in_header *in,
1761 struct fuse_fsync_in *inarg)
1762{
1763 int res;
1764 char *path;
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001765 struct fuse_dirhandle *dh = get_dirhandle(inarg->fh);
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001766 struct fuse_file_info fi;
1767
1768 memset(&fi, 0, sizeof(fi));
Miklos Szerediede1f7a2005-04-01 21:08:57 +00001769 fi.fh = dh->fh;
1770
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001771 res = -ENOENT;
1772 path = get_path(f, in->nodeid);
1773 if (path != NULL) {
1774 res = -ENOSYS;
1775 if (f->op.fsyncdir)
1776 res = f->op.fsyncdir(path, inarg->fsync_flags & 1, &fi);
1777 free(path);
1778 }
1779 send_reply(f, in, res, NULL, 0);
1780}
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001781
Miklos Szeredi43696432001-11-18 19:15:05 +00001782static void free_cmd(struct fuse_cmd *cmd)
1783{
1784 free(cmd->buf);
1785 free(cmd);
1786}
1787
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001788void fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd)
Miklos Szeredia181e612001-11-06 12:03:23 +00001789{
Miklos Szeredia181e612001-11-06 12:03:23 +00001790 struct fuse_in_header *in = (struct fuse_in_header *) cmd->buf;
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001791 void *inarg = cmd->buf + SIZEOF_COMPAT(f, fuse_in_header);
Miklos Szeredid169f312004-09-22 08:48:26 +00001792 struct fuse_context *ctx = fuse_get_context();
Miklos Szeredia181e612001-11-06 12:03:23 +00001793
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001794 fuse_dec_avail(f);
Miklos Szeredi33232032001-11-19 17:55:51 +00001795
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001796 if ((f->flags & FUSE_DEBUG)) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001797 printf("unique: %llu, opcode: %s (%i), nodeid: %lu, insize: %i\n",
1798 in->unique, opname(in->opcode), in->opcode,
1799 (unsigned long) in->nodeid, cmd->buflen);
Miklos Szeredic0938ea2001-11-07 12:35:06 +00001800 fflush(stdout);
1801 }
Miklos Szeredife25def2001-12-20 15:38:05 +00001802
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001803 if (!f->got_init && in->opcode != FUSE_INIT) {
1804 /* Old kernel version probably */
1805 send_reply(f, in, -EPROTO, NULL, 0);
1806 goto out;
1807 }
1808
Miklos Szeredid169f312004-09-22 08:48:26 +00001809 ctx->fuse = f;
Miklos Szeredife25def2001-12-20 15:38:05 +00001810 ctx->uid = in->uid;
1811 ctx->gid = in->gid;
Miklos Szeredi1f18db52004-09-27 06:54:49 +00001812 ctx->pid = in->pid;
Miklos Szeredi159bd7e2005-02-28 17:32:16 +00001813 ctx->private_data = f->user_data;
Miklos Szeredie5183742005-02-02 11:14:04 +00001814
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001815 switch (in->opcode) {
Miklos Szeredia181e612001-11-06 12:03:23 +00001816 case FUSE_LOOKUP:
1817 do_lookup(f, in, (char *) inarg);
1818 break;
1819
Miklos Szeredia181e612001-11-06 12:03:23 +00001820 case FUSE_GETATTR:
1821 do_getattr(f, in);
1822 break;
1823
1824 case FUSE_SETATTR:
1825 do_setattr(f, in, (struct fuse_setattr_in *) inarg);
1826 break;
1827
1828 case FUSE_READLINK:
1829 do_readlink(f, in);
1830 break;
1831
Miklos Szeredia181e612001-11-06 12:03:23 +00001832 case FUSE_MKNOD:
1833 do_mknod(f, in, (struct fuse_mknod_in *) inarg);
1834 break;
Miklos Szeredie5183742005-02-02 11:14:04 +00001835
Miklos Szeredia181e612001-11-06 12:03:23 +00001836 case FUSE_MKDIR:
1837 do_mkdir(f, in, (struct fuse_mkdir_in *) inarg);
1838 break;
Miklos Szeredie5183742005-02-02 11:14:04 +00001839
Miklos Szeredia181e612001-11-06 12:03:23 +00001840 case FUSE_UNLINK:
Miklos Szeredib5958612004-02-20 14:10:49 +00001841 do_unlink(f, in, (char *) inarg);
1842 break;
1843
Miklos Szeredia181e612001-11-06 12:03:23 +00001844 case FUSE_RMDIR:
Miklos Szeredib5958612004-02-20 14:10:49 +00001845 do_rmdir(f, in, (char *) inarg);
Miklos Szeredia181e612001-11-06 12:03:23 +00001846 break;
1847
1848 case FUSE_SYMLINK:
Miklos Szeredie5183742005-02-02 11:14:04 +00001849 do_symlink(f, in, (char *) inarg,
Miklos Szeredia181e612001-11-06 12:03:23 +00001850 ((char *) inarg) + strlen((char *) inarg) + 1);
1851 break;
1852
1853 case FUSE_RENAME:
1854 do_rename(f, in, (struct fuse_rename_in *) inarg);
1855 break;
Miklos Szeredie5183742005-02-02 11:14:04 +00001856
Miklos Szeredia181e612001-11-06 12:03:23 +00001857 case FUSE_LINK:
1858 do_link(f, in, (struct fuse_link_in *) inarg);
1859 break;
1860
1861 case FUSE_OPEN:
1862 do_open(f, in, (struct fuse_open_in *) inarg);
1863 break;
1864
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001865 case FUSE_FLUSH:
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001866 do_flush(f, in, (struct fuse_flush_in *) inarg);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001867 break;
1868
Miklos Szeredi9478e862002-12-11 09:50:26 +00001869 case FUSE_RELEASE:
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001870 do_release(f, in, (struct fuse_release_in *) inarg);
Miklos Szeredi9478e862002-12-11 09:50:26 +00001871 break;
1872
Miklos Szeredia181e612001-11-06 12:03:23 +00001873 case FUSE_READ:
1874 do_read(f, in, (struct fuse_read_in *) inarg);
1875 break;
1876
1877 case FUSE_WRITE:
1878 do_write(f, in, (struct fuse_write_in *) inarg);
1879 break;
1880
Mark Glinesd84b39a2002-01-07 16:32:02 +00001881 case FUSE_STATFS:
1882 do_statfs(f, in);
1883 break;
1884
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001885 case FUSE_FSYNC:
1886 do_fsync(f, in, (struct fuse_fsync_in *) inarg);
1887 break;
1888
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001889 case FUSE_SETXATTR:
1890 do_setxattr(f, in, (struct fuse_setxattr_in *) inarg);
1891 break;
1892
1893 case FUSE_GETXATTR:
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001894 do_getxattr(f, in, (struct fuse_getxattr_in *) inarg);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001895 break;
1896
1897 case FUSE_LISTXATTR:
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001898 do_listxattr(f, in, (struct fuse_getxattr_in *) inarg);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001899 break;
1900
1901 case FUSE_REMOVEXATTR:
1902 do_removexattr(f, in, (char *) inarg);
1903 break;
1904
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001905 case FUSE_INIT:
1906 do_init(f, in, (struct fuse_init_in_out *) inarg);
1907 break;
1908
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001909 case FUSE_OPENDIR:
1910 do_opendir(f, in, (struct fuse_open_in *) inarg);
1911 break;
1912
1913 case FUSE_READDIR:
1914 do_readdir(f, in, (struct fuse_read_in *) inarg);
1915 break;
1916
1917 case FUSE_RELEASEDIR:
1918 do_releasedir(f, in, (struct fuse_release_in *) inarg);
1919 break;
1920
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001921 case FUSE_FSYNCDIR:
1922 do_fsyncdir(f, in, (struct fuse_fsync_in *) inarg);
1923 break;
1924
Miklos Szeredia181e612001-11-06 12:03:23 +00001925 default:
Miklos Szeredi43696432001-11-18 19:15:05 +00001926 send_reply(f, in, -ENOSYS, NULL, 0);
Miklos Szeredia181e612001-11-06 12:03:23 +00001927 }
Miklos Szeredi43696432001-11-18 19:15:05 +00001928
Miklos Szeredi3f0005f2005-01-04 19:24:31 +00001929 out:
Miklos Szeredi43696432001-11-18 19:15:05 +00001930 free_cmd(cmd);
Miklos Szeredia181e612001-11-06 12:03:23 +00001931}
1932
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001933int fuse_exited(struct fuse* f)
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001934{
1935 return f->exited;
1936}
1937
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001938struct fuse_cmd *fuse_read_cmd(struct fuse *f)
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001939{
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001940 ssize_t res;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001941 struct fuse_cmd *cmd;
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001942 struct fuse_in_header *in;
1943 void *inarg;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001944
Miklos Szeredi43696432001-11-18 19:15:05 +00001945 cmd = (struct fuse_cmd *) malloc(sizeof(struct fuse_cmd));
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001946 if (cmd == NULL) {
1947 fprintf(stderr, "fuse: failed to allocate cmd in read\n");
1948 return NULL;
1949 }
Miklos Szeredi43696432001-11-18 19:15:05 +00001950 cmd->buf = (char *) malloc(FUSE_MAX_IN);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001951 if (cmd->buf == NULL) {
1952 fprintf(stderr, "fuse: failed to allocate read buffer\n");
1953 free(cmd);
1954 return NULL;
1955 }
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001956 in = (struct fuse_in_header *) cmd->buf;
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001957 inarg = cmd->buf + SIZEOF_COMPAT(f, fuse_in_header);
Miklos Szeredi43696432001-11-18 19:15:05 +00001958
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001959 res = read(f->fd, cmd->buf, FUSE_MAX_IN);
1960 if (res == -1) {
1961 free_cmd(cmd);
Miklos Szeredie56818b2004-12-12 11:45:24 +00001962 if (fuse_exited(f) || errno == EINTR || errno == ENOENT)
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001963 return NULL;
Miklos Szeredie5183742005-02-02 11:14:04 +00001964
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001965 /* ENODEV means we got unmounted, so we silenty return failure */
1966 if (errno != ENODEV) {
1967 /* BAD... This will happen again */
1968 perror("fuse: reading device");
1969 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001970
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001971 fuse_exit(f);
1972 return NULL;
1973 }
Miklos Szeredi30e093a2005-04-03 17:44:54 +00001974 if ((size_t) res < SIZEOF_COMPAT(f, fuse_in_header)) {
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001975 free_cmd(cmd);
1976 /* Cannot happen */
1977 fprintf(stderr, "short read on fuse device\n");
1978 fuse_exit(f);
1979 return NULL;
1980 }
1981 cmd->buflen = res;
Miklos Szeredie5183742005-02-02 11:14:04 +00001982
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001983 /* Forget is special, it can be done without messing with threads. */
1984 if (in->opcode == FUSE_FORGET) {
1985 do_forget(f, in, (struct fuse_forget_in *) inarg);
1986 free_cmd(cmd);
1987 return NULL;
1988 }
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001989
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001990 return cmd;
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001991}
1992
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001993int fuse_loop(struct fuse *f)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001994{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001995 if (f == NULL)
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001996 return -1;
Miklos Szeredic40748a2004-02-20 16:38:45 +00001997
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001998 while (1) {
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001999 struct fuse_cmd *cmd;
2000
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002001 if (fuse_exited(f))
Miklos Szeredi874e3c12004-11-01 23:15:20 +00002002 break;
Miklos Szeredi0f48a262002-12-05 14:23:01 +00002003
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002004 cmd = fuse_read_cmd(f);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00002005 if (cmd == NULL)
Miklos Szeredi0f48a262002-12-05 14:23:01 +00002006 continue;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002007
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002008 fuse_process_cmd(f, cmd);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002009 }
Miklos Szeredi874e3c12004-11-01 23:15:20 +00002010 f->exited = 0;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002011 return 0;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002012}
2013
Miklos Szeredi891b8742004-07-29 09:27:49 +00002014int fuse_invalidate(struct fuse *f, const char *path)
2015{
Miklos Szeredie56818b2004-12-12 11:45:24 +00002016 (void) f;
2017 (void) path;
2018 return -EINVAL;
Miklos Szeredi891b8742004-07-29 09:27:49 +00002019}
2020
Miklos Szeredi0f48a262002-12-05 14:23:01 +00002021void fuse_exit(struct fuse *f)
2022{
2023 f->exited = 1;
2024}
2025
Miklos Szeredid169f312004-09-22 08:48:26 +00002026struct fuse_context *fuse_get_context()
Miklos Szeredi2e50d432001-12-20 12:17:25 +00002027{
Miklos Szeredid169f312004-09-22 08:48:26 +00002028 static struct fuse_context context;
2029 if (fuse_getcontext)
2030 return fuse_getcontext();
Miklos Szeredi2e50d432001-12-20 12:17:25 +00002031 else
Miklos Szeredid169f312004-09-22 08:48:26 +00002032 return &context;
2033}
2034
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002035void fuse_set_getcontext_func(struct fuse_context *(*func)(void))
Miklos Szeredid169f312004-09-22 08:48:26 +00002036{
2037 fuse_getcontext = func;
Miklos Szeredi2e50d432001-12-20 12:17:25 +00002038}
2039
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002040int fuse_is_lib_option(const char *opt)
2041{
2042 if (strcmp(opt, "debug") == 0 ||
Miklos Szeredia13d9002004-11-02 17:32:03 +00002043 strcmp(opt, "hard_remove") == 0 ||
2044 strcmp(opt, "use_ino") == 0)
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002045 return 1;
2046 else
2047 return 0;
2048}
2049
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002050static int parse_lib_opts(struct fuse *f, const char *opts)
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002051{
2052 if (opts) {
2053 char *xopts = strdup(opts);
2054 char *s = xopts;
2055 char *opt;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002056
Miklos Szeredie56818b2004-12-12 11:45:24 +00002057 if (xopts == NULL) {
2058 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002059 return -1;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002060 }
Miklos Szeredie5183742005-02-02 11:14:04 +00002061
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002062 while((opt = strsep(&s, ","))) {
2063 if (strcmp(opt, "debug") == 0)
2064 f->flags |= FUSE_DEBUG;
2065 else if (strcmp(opt, "hard_remove") == 0)
2066 f->flags |= FUSE_HARD_REMOVE;
Miklos Szeredia13d9002004-11-02 17:32:03 +00002067 else if (strcmp(opt, "use_ino") == 0)
2068 f->flags |= FUSE_USE_INO;
Miklos Szeredie5183742005-02-02 11:14:04 +00002069 else
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002070 fprintf(stderr, "fuse: warning: unknown option `%s'\n", opt);
2071 }
2072 free(xopts);
2073 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002074 return 0;
Miklos Szeredibd7661b2004-07-23 17:16:29 +00002075}
2076
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002077struct fuse *fuse_new_common(int fd, const char *opts,
2078 const struct fuse_operations *op,
2079 size_t op_size, int compat)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002080{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002081 struct fuse *f;
2082 struct node *root;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002083
Miklos Szeredi0f62d722005-01-04 12:45:54 +00002084 if (sizeof(struct fuse_operations) < op_size) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002085 fprintf(stderr, "fuse: warning: library too old, some operations may not not work\n");
Miklos Szeredi0f62d722005-01-04 12:45:54 +00002086 op_size = sizeof(struct fuse_operations);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002087 }
2088
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002089 f = (struct fuse *) calloc(1, sizeof(struct fuse));
Miklos Szeredie56818b2004-12-12 11:45:24 +00002090 if (f == NULL) {
2091 fprintf(stderr, "fuse: failed to allocate fuse object\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002092 goto out;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002093 }
Miklos Szeredi2df1c042001-11-06 15:07:17 +00002094
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002095 if (parse_lib_opts(f, opts) == -1)
2096 goto out_free;
2097
Miklos Szeredi8cffdb92001-11-09 14:49:18 +00002098 f->fd = fd;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002099 f->ctr = 0;
Miklos Szeredi76f65782004-02-19 16:55:40 +00002100 f->generation = 0;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00002101 /* FIXME: Dynamic hash table */
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002102 f->name_table_size = 14057;
2103 f->name_table = (struct node **)
2104 calloc(1, sizeof(struct node *) * f->name_table_size);
Miklos Szeredie56818b2004-12-12 11:45:24 +00002105 if (f->name_table == NULL) {
2106 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002107 goto out_free;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002108 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002109
Miklos Szeredia13d9002004-11-02 17:32:03 +00002110 f->id_table_size = 14057;
2111 f->id_table = (struct node **)
2112 calloc(1, sizeof(struct node *) * f->id_table_size);
Miklos Szeredie56818b2004-12-12 11:45:24 +00002113 if (f->id_table == NULL) {
2114 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002115 goto out_free_name_table;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002116 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002117
Miklos Szeredia25d4c22004-11-23 22:32:16 +00002118#ifndef USE_UCLIBC
2119 pthread_mutex_init(&f->lock, NULL);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00002120 pthread_mutex_init(&f->worker_lock, NULL);
Miklos Szeredia25d4c22004-11-23 22:32:16 +00002121#else
2122 {
2123 pthread_mutexattr_t attr;
2124 pthread_mutexattr_init(&attr);
2125 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
2126 pthread_mutex_init(&f->lock, &attr);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00002127 pthread_mutex_init(&f->worker_lock, &attr);
Miklos Szeredia25d4c22004-11-23 22:32:16 +00002128 pthread_mutexattr_destroy(&attr);
2129 }
2130#endif
Miklos Szeredi33232032001-11-19 17:55:51 +00002131 f->numworker = 0;
2132 f->numavail = 0;
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002133 memcpy(&f->op, op, op_size);
2134 f->compat = compat;
Miklos Szeredi0f48a262002-12-05 14:23:01 +00002135 f->exited = 0;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002136
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002137 root = (struct node *) calloc(1, sizeof(struct node));
Miklos Szeredie56818b2004-12-12 11:45:24 +00002138 if (root == NULL) {
2139 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredia13d9002004-11-02 17:32:03 +00002140 goto out_free_id_table;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002141 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002142
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002143 root->name = strdup("/");
Miklos Szeredie56818b2004-12-12 11:45:24 +00002144 if (root->name == NULL) {
2145 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002146 goto out_free_root;
Miklos Szeredie56818b2004-12-12 11:45:24 +00002147 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002148
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002149 root->parent = 0;
Miklos Szeredia13d9002004-11-02 17:32:03 +00002150 root->nodeid = FUSE_ROOT_ID;
Miklos Szeredi76f65782004-02-19 16:55:40 +00002151 root->generation = 0;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00002152 root->refctr = 1;
Miklos Szeredia13d9002004-11-02 17:32:03 +00002153 hash_id(f, root);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002154
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002155 return f;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002156
2157 out_free_root:
2158 free(root);
Miklos Szeredia13d9002004-11-02 17:32:03 +00002159 out_free_id_table:
2160 free(f->id_table);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002161 out_free_name_table:
2162 free(f->name_table);
2163 out_free:
2164 free(f);
2165 out:
Miklos Szeredib2cf9562004-09-16 08:42:40 +00002166 return NULL;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002167}
2168
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002169struct fuse *fuse_new(int fd, const char *opts,
2170 const struct fuse_operations *op, size_t op_size)
2171{
2172 return fuse_new_common(fd, opts, op, op_size, 0);
2173}
2174
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002175struct fuse *fuse_new_compat2(int fd, const char *opts,
2176 const struct fuse_operations_compat2 *op)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002177{
2178 return fuse_new_common(fd, opts, (struct fuse_operations *) op,
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002179 sizeof(struct fuse_operations_compat2), 21);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002180}
2181
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002182struct fuse *fuse_new_compat1(int fd, int flags,
2183 const struct fuse_operations_compat1 *op)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002184{
2185 char *opts = NULL;
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002186 if (flags & FUSE_DEBUG_COMPAT1)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002187 opts = "debug";
2188 return fuse_new_common(fd, opts, (struct fuse_operations *) op,
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002189 sizeof(struct fuse_operations_compat1), 11);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002190}
2191
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002192void fuse_destroy(struct fuse *f)
2193{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002194 size_t i;
Miklos Szeredia13d9002004-11-02 17:32:03 +00002195 for (i = 0; i < f->id_table_size; i++) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002196 struct node *node;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002197
Miklos Szeredia13d9002004-11-02 17:32:03 +00002198 for (node = f->id_table[i]; node != NULL; node = node->id_next) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002199 if (node->is_hidden) {
Miklos Szeredia13d9002004-11-02 17:32:03 +00002200 char *path = get_path(f, node->nodeid);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002201 if (path)
2202 f->op.unlink(path);
2203 }
2204 }
2205 }
Miklos Szeredia13d9002004-11-02 17:32:03 +00002206 for (i = 0; i < f->id_table_size; i++) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002207 struct node *node;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002208 struct node *next;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00002209
Miklos Szeredia13d9002004-11-02 17:32:03 +00002210 for (node = f->id_table[i]; node != NULL; node = next) {
2211 next = node->id_next;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002212 free_node(node);
2213 }
2214 }
Miklos Szeredia13d9002004-11-02 17:32:03 +00002215 free(f->id_table);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002216 free(f->name_table);
Miklos Szeredia181e612001-11-06 12:03:23 +00002217 pthread_mutex_destroy(&f->lock);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00002218 pthread_mutex_destroy(&f->worker_lock);
Miklos Szeredi159bd7e2005-02-28 17:32:16 +00002219 if (f->op.destroy)
2220 f->op.destroy(f->user_data);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00002221 free(f);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00002222}
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00002223
Miklos Szeredif458b8c2004-12-07 16:46:42 +00002224__asm__(".symver fuse_exited,__fuse_exited@");
2225__asm__(".symver fuse_process_cmd,__fuse_process_cmd@");
2226__asm__(".symver fuse_read_cmd,__fuse_read_cmd@");
2227__asm__(".symver fuse_set_getcontext_func,__fuse_set_getcontext_func@");
2228__asm__(".symver fuse_new_compat2,fuse_new@");